Skip to content

Commit

Permalink
Updated test resources
Browse files Browse the repository at this point in the history
  • Loading branch information
vruusmann committed Oct 16, 2024
1 parent d198f96 commit 3f89161
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 28 deletions.
3 changes: 0 additions & 3 deletions sklearn2pmml/preprocessing/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -443,9 +443,6 @@ def __init__(self, mapping):
def _transform_dict(self):
return self.mapping

def fit(self, X, y = None):
return self

def transform(self, X):
X1d = to_1d(X)
transform_dict = self._transform_dict()
Expand Down
46 changes: 21 additions & 25 deletions sklearn2pmml/preprocessing/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -413,21 +413,7 @@ def test_sequence_transform(self):
Xt = mapper.fit_transform(X)
self.assertEqual([[1], [1], [1]], Xt.tolist())

class NumberFormatterTest(TestCase):

def test_transform(self):
transformer = NumberFormatter(pattern = "%3d")
self.assertTrue(hasattr(transformer, "pattern"))
X = Series([-1, 0, 1.5])
Xt = transformer.fit_transform(X)
self.assertIsInstance(Xt, Series)
self.assertEqual([" -1", " 0", " 1"], Xt.tolist())
X = numpy.asarray([[-1], [0], [1.5]])
Xt = transformer.fit_transform(X)
self.assertIsInstance(Xt, numpy.ndarray)
self.assertEqual([[" -1"], [" 0"], [" 1"]], Xt.tolist())

class DateTimeFormatterTest(TestCase):
class DateTimeFormatterTest(TransformerTest):

def test_transform(self):
transformer = DateTimeFormatter(pattern = "%m/%d/%y")
Expand All @@ -439,6 +425,18 @@ def test_transform(self):
Xt = mapper.fit_transform(X)
self.assertEqual([["08/20/04"], ["08/21/04"]], Xt.tolist())

class NumberFormatterTest(TransformerTest):

def test_transform(self):
transformer = NumberFormatter(pattern = "%3d")
self.assertTrue(hasattr(transformer, "pattern"))
X = Series([-1, 0, 1.5])
self.assertEqual([" -1", " 0", " 1"], self._transform1d(transformer, X).tolist())
X = numpy.asarray([[-1], [0], [1.5]])
Xt = transformer.fit_transform(X)
self.assertIsInstance(Xt, numpy.ndarray)
self.assertEqual([[" -1"], [" 0"], [" 1"]], Xt.tolist())

class IdentityTransformerTest(TestCase):

def test_transform(self):
Expand All @@ -448,7 +446,7 @@ def test_transform(self):
self.assertIsInstance(Xt, DataFrame)
self.assertEqual([["A"], ["B"], ["C"]], Xt.values.tolist())

class LookupTransformerTest(TestCase):
class LookupTransformerTest(TransformerTest):

def test_transform_float(self):
mapping = {
Expand All @@ -467,9 +465,7 @@ def test_transform_float(self):
LookupTransformer(mapping, int(0))
transformer = LookupTransformer(mapping, float("NaN"))
X = Series([0.0, 45.0, 90.0])
self.assertEqual([math.cos(0.0), math.cos(45.0), math.cos(90.0)], transformer.transform(X).tolist())
X = numpy.array([[0.0], [90.0]])
self.assertEqual([[math.cos(0.0)], [math.cos(90.0)]], transformer.transform(X).tolist())
self.assertEqual([math.cos(0.0), math.cos(45.0), math.cos(90.0)], self._transform1d(transformer, X).tolist())
X = numpy.array([float("NaN"), 180.0])
self.assertTrue(_list_equal([[float("NaN")], [float("NaN")]], transformer.transform(X).tolist()))
transformer = LookupTransformer(mapping, -999.0)
Expand All @@ -487,13 +483,13 @@ def test_transform_string(self):
mapping.pop(None)
transformer = LookupTransformer(mapping, None)
X = Series(["one", "two", "three"])
self.assertEqual(["ein", "zwei", "drei"], transformer.transform(X).tolist())
self.assertEqual(["ein", "zwei", "drei"], self._transform1d(transformer, X).tolist())
X = numpy.array([[None], ["zero"]])
self.assertEqual([[None], [None]], transformer.transform(X).tolist())
transformer = LookupTransformer(mapping, "(other)")
self.assertEqual([[None], ["(other)"]], transformer.transform(X).tolist())

class FilterLookupTransformerTest(TestCase):
class FilterLookupTransformerTest(TransformerTest):

def test_transform_int(self):
mapping = {
Expand All @@ -505,8 +501,8 @@ def test_transform_int(self):
FilterLookupTransformer(mapping)
mapping.pop(1)
transformer = FilterLookupTransformer(mapping)
X = numpy.array([[0], [-1], [3], [2]])
self.assertEqual([[1], [-1], [3], [1]], transformer.transform(X).tolist())
X = Series([0, -1, 3, 2])
self.assertEqual([1, -1, 3, 1], self._transform1d(transformer, X).tolist())

def test_transform_string(self):
mapping = {
Expand All @@ -517,8 +513,8 @@ def test_transform_string(self):
FilterLookupTransformer(mapping)
mapping["blue"] = "green"
transformer = FilterLookupTransformer(mapping)
X = numpy.array([["red"], ["orange"], [None], ["green"], ["blue"]])
self.assertEqual([["red"], ["yellow"], [None], ["green"], ["green"]], transformer.transform(X).tolist())
X = Series(["red", "orange", None, "green", "blue"])
self.assertEqual(["red", "yellow", None, "green", "green"], self._transform1d(transformer, X).tolist())

class MultiLookupTransformerTest(TestCase):

Expand Down

0 comments on commit 3f89161

Please sign in to comment.