def test_raises_type_error(mock_fit): """ If a fit method with required X and y arguments raises a TypeError, it's re-raised (for a different reason) when it's called with one argument """ with pytest.raises(TypeError): _call_fit(Trans().fit, 'X', 'y', kwarg='kwarg')
def test_called_with_x_and_y(mock_fit): """ Fit method with required X and y arguments is called with both and with any additional keywords """ _call_fit(Trans().fit, 'X', 'y', kwarg='kwarg') mock_fit.assert_called_with('X', 'y', kwarg='kwarg')
def fit_predict(self, factor_container, y=None, **fit_params): fc_fit, fit_params = self._pre_transform(factor_container, y, **fit_params) if hasattr(self.steps[-1][-1], "fit_predict"): return _call_fit(self.steps[-1][-1].fit_predict, fc_fit, y, **fit_params) else: return _call_fit(self.steps[-1][-1].fit, fc_fit, y, **fit_params).predict(fc_fit)
def _pre_transform(self, factor_container, y=None, **fit_params): fit_params_steps = dict((step, {}) for step, _ in self.steps) for pname, pval in six.iteritems(fit_params): step, param = pname.split('__', 1) fit_params_steps[step][param] = pval fc = factor_container for name, transform in self.steps[:-1]: if hasattr(transform, "fit_transform"): fc_fit = _call_fit(transform.fit_transform, fc, y, **fit_params_steps[name]) else: fc_fit = _call_fit(transform.fit, fc, y, **fit_params_steps[name]).transform(fc) if not isinstance(fc_fit, FactorContainer): try: fc.replace_data(fc_fit) except ValueError: raise ValueError( 'Failed in chain step {0}, please set out_container=True explicitly'.format(transform)) else: fc = fc_fit return fc, fit_params_steps[self.steps[-1][0]]
def _pre_transform(self, factor_container, y=None, **fit_params): fit_params_steps = dict((step, {}) for step, _ in self.steps) for pname, pval in six.iteritems(fit_params): step, param = pname.split('__', 1) fit_params_steps[step][param] = pval fc = factor_container for name, transform in self.steps[:-1]: if hasattr(transform, "fit_transform"): fc_fit = _call_fit(transform.fit_transform, fc, y, **fit_params_steps[name]) else: fc_fit = _call_fit(transform.fit, fc, y, **fit_params_steps[name]).transform(fc) if not isinstance(fc_fit, FactorContainer): try: fc.replace_data(fc_fit) except ValueError: raise ValueError( 'Failed in chain step {0}, please set out_container=True explicitly' .format(transform)) else: fc = fc_fit return fc, fit_params_steps[self.steps[-1][0]]
def fit(self, factor_container, y=None, **fit_params): fc_fit, fit_params = self._pre_transform(factor_container, y, **fit_params) _call_fit(self.steps[-1][-1].fit, factor_container, y, **fit_params) return self