def test_transformers_not_modified(self): # given mock = self.DataTransformerMock1() p = Pipeline([mock], copy=True) # when p.transform(constant_timeseries(1, 10)) # then self.assertFalse(mock.transform_called)
def test_inverse_transform(self): # given data = constant_timeseries(0., 3) transformers = [self.PlusTenTransformer(), self.TimesTwoTransformer()] p = Pipeline(transformers) # when transformed = p.transform(data) back = p.inverse_transform(transformed) # then self.assertEqual(data, back)
def test_transform(self): # given mock1 = self.DataTransformerMock1() mock2 = self.DataTransformerMock2() data = constant_timeseries(0, 3) transformers = [mock1] * 10 + [mock2] * 10 p = Pipeline(transformers) # when p.fit(data) transformed = p.transform(data) # then self.assertEqual(63, len(transformed)) self.assertEqual([0] * 3 + [1] * 30 + [2] * 30, list(transformed.values())) for t in transformers: self.assertTrue(t.transform_called) self.assertFalse(t.inverse_transform_called)
def test_multi_ts(self): series1 = constant_timeseries(0., 3) series2 = constant_timeseries(1., 3) data = [series1, series2] mapper1 = InvertibleMapper(fn=lambda x: x + 10, inverse_fn=lambda x: x - 10) mapper2 = InvertibleMapper(fn=lambda x: x * 10, inverse_fn=lambda x: x / 10) transformers = [mapper1, mapper2] p = Pipeline(transformers) # when transformed = p.transform(data) back = p.inverse_transform(transformed) # then self.assertEqual(data, back)
def test_pipeline_partial_inverse(self): series = constant_timeseries(0., 3) def plus_ten(x): return x + 10 mapper = Mapper(fn=plus_ten) mapper_inv = InvertibleMapper(fn=lambda x: x + 2, inverse_fn=lambda x: x - 2) series_plus_ten = mapper.transform(series) pipeline = Pipeline([mapper, mapper_inv]) transformed = pipeline.transform(series) # should fail, since partial is False by default with self.assertRaises(ValueError): pipeline.inverse_transform(transformed) back = pipeline.inverse_transform(transformed, partial=True) # while the +/- 2 is inverted, the +10 operation is not self.assertEqual(series_plus_ten, back)