def setUp(self) -> None: self.inverse_module = MagicMock() self.input_step = MagicMock() time = pd.date_range('2000-01-01', freq='24H', periods=7) self.inverse_module.inverse_transform.return_value = xr.DataArray([[5, 5], [5, 5], [4, 5], [5, 4], [6, 5], [7, 6], [8, 7]], dims=["time", "horizon"], coords={"time": time, "horizon": [0, 1]}) self.input_step.stop = False self.inverse_step = InverseStep(self.inverse_module, {"input" : self.input_step}, file_manager=MagicMock()) self.inverse_step_result = MagicMock() self.input_step.get_result.return_value = self.inverse_step_result
class TestInverseTransform(unittest.TestCase): def setUp(self) -> None: self.inverse_module = MagicMock() self.input_step = MagicMock() time = pd.date_range('2000-01-01', freq='24H', periods=7) self.inverse_module.inverse_transform.return_value = xr.DataArray( [[5, 5], [5, 5], [4, 5], [5, 4], [6, 5], [7, 6], [8, 7]], dims=["time", "horizon"], coords={ "time": time, "horizon": [0, 1] }) self.input_step._should_stop.return_value = False self.inverse_step = InverseStep(self.inverse_module, {"input": self.input_step}, file_manager=MagicMock()) self.inverse_step_result = MagicMock() self.input_step.get_result.return_value = self.inverse_step_result def tearDown(self) -> None: self.inverse_module = None self.input_step = None self.inverse_step = None def test_get_result(self): # This test checks if the get_result methods works corerctly, i.e. if it returns the correct result of the step and # calculate it if necessary. self.inverse_step.get_result(None, None) self.inverse_module.inverse_transform.assert_called_once_with( input=self.input_step.get_result()) def test_get_result_stop(self): self.input_step.get_result.return_value = None self.inverse_step.get_result(None, None) self.inverse_module.inverse_transform.assert_not_called() self.assertTrue(self.inverse_step._should_stop(None, None)) def test_transform_no_inverse_method(self): self.inverse_module.has_inverse_transform = False self.inverse_module.name = "Magic" with self.assertRaises( KindOfTransformDoesNotExistException) as context: self.inverse_step.get_result(None, None) self.assertEqual(f"The module Magic has no inverse transform", context.exception.message) self.assertEqual(KindOfTransform.INVERSE_TRANSFORM, context.exception.method) self.inverse_module.inverse_transform.assert_not_called()
def create_step(self, module: Base, kwargs: Dict[str, Union[StepInformation, Tuple[StepInformation, ...]]], use_inverse_transform: bool, use_predict_proba: bool, callbacks: List[Union[BaseCallback, Callable[[Dict[str, xr.DataArray]], None]]], condition, batch_size, computation_mode, train_if): """ Creates a appropriate step for the current situation. :param module: The module which should be added to the pipeline :param kwargs: The input steps for the current step :param targets: The target steps for the currrent step :param use_inverse_transform: Should inverse_transform be called instead of transform :param use_predict_proba: Should probabilistic_transform be called instead of transform :param callbacks: Callbacks to use after results are processed. :param condition: A function returning True or False which indicates if the step should be performed :param batch_size: The size of the past time range which should be used for relearning the module :param computation_mode: The computation mode of the step :param train_if: A method for determining if the step should be fitted at a specific timestamp. :return: StepInformation """ arguments = inspect.signature(module.transform).parameters.keys() if "kwargs" not in arguments and not isinstance(module, Pipeline): for argument in arguments: if argument not in kwargs.keys(): raise StepCreationException( f"The module {module.name} miss {argument} as input. The module needs {arguments} as input. " f"{kwargs} are given as input." f"Add {argument}=<desired_input> when adding {module.name} to the pipeline.", module ) # TODO needs to check that inputs are unambigious -> I.e. check that each input has only one output pipeline = self._check_ins(kwargs) input_steps, target_steps = self._split_input_target_steps(kwargs, pipeline) if isinstance(module, Pipeline): step = PipelineStep(module, input_steps, pipeline.file_manager, targets=target_steps, callbacks=callbacks, computation_mode=computation_mode, condition=condition, batch_size=batch_size, train_if=train_if) elif use_inverse_transform: step = InverseStep(module, input_steps, pipeline.file_manager, targets=target_steps, callbacks=callbacks, computation_mode=computation_mode, condition=condition) elif use_predict_proba: step = ProbablisticStep(module, input_steps, pipeline.file_manager, targets=target_steps, callbacks=callbacks, computation_mode=computation_mode, condition=condition) else: step = Step(module, input_steps, pipeline.file_manager, targets=target_steps, callbacks=callbacks, computation_mode=computation_mode, condition=condition, batch_size=batch_size, train_if=train_if) step_id = pipeline.add(module=step, input_ids=[step.id for step in input_steps.values()], target_ids=[step.id for step in target_steps.values()]) step.id = step_id if len(target_steps) > 1: step.last = False for target in target_steps: r_step = step.get_result_step(target) r_id = pipeline.add(module=step, input_ids=[step_id]) r_step.id = r_id return StepInformation(step, pipeline)