def test_baseadapter__setattr__(self): lm = LinearRegression() lm_strategy = AdapterForFitPredictAdaptee(lm) self.assertEqual(lm_strategy.copy_X, True) lm_strategy.copy_X = False self.assertEqual(lm_strategy.copy_X, False) self.assertEqual('copy_X' in dir(lm_strategy), False)
def test_baseadapter__fit_AdapterForFitPredictAdaptee(self): X = self.X y = self.y lm = LinearRegression() stepstrategy = AdapterForFitPredictAdaptee(lm) self.assertEqual(hasattr(stepstrategy._adaptee, 'coef_'), False) result = stepstrategy.fit(X=X, y=y) self.assertEqual(hasattr(stepstrategy._adaptee, 'coef_'), True) self.assertEqual(stepstrategy, result)
def test_baseadapter__get_params(self): lm = LinearRegression() lm_strategy = AdapterForFitPredictAdaptee(lm) result_lm = lm_strategy.get_params() self.assertEqual(result_lm, { 'copy_X': True, 'fit_intercept': True, 'n_jobs': 1, 'normalize': False })
def test_process__get_fit_signature(self): lm = LinearRegression() gm = GaussianMixture() lm_strategy = AdapterForFitPredictAdaptee(lm) gm_strategy = AdapterForFitPredictAdaptee(gm) step_lm = Process(lm_strategy) step_gm = Process(gm_strategy) result_lm = step_lm._get_fit_signature() result_gm = step_gm._get_fit_signature() self.assertEqual(result_lm, ['X', 'y', 'sample_weight']) self.assertEqual(result_gm, ['X', 'y'])
def test_FitPredictWithDictionaryOutput__get_predict_signature(self): lm = LinearRegression() wrapped_lm = AdapterForFitPredictAdaptee(lm) double_wrap = AdapterForCustomFitPredictWithDictionaryOutputAdaptee( wrapped_lm) result = double_wrap._get_predict_signature() self.assertEqual(result, ['X'])
def test_FitPredictWithDictionaryOutput__get_fit_signature(self): lm = LinearRegression() wrapped_lm = AdapterForFitPredictAdaptee(lm) double_wrap = AdapterForCustomFitPredictWithDictionaryOutputAdaptee( wrapped_lm) result = double_wrap._get_fit_signature() self.assertEqual(sorted(result), sorted(['X', 'y', 'sample_weight']))
def test_process__setattr__(self): lm = LinearRegression() lm_strategy = AdapterForFitPredictAdaptee(lm) step = Process(lm_strategy) self.assertEqual(step.copy_X, True) step.copy_X = False self.assertEqual(step.copy_X, False) self.assertEqual('copy_X' in dir(step), False)
def test_FitPredict__predict(self): X = self.X y = self.y lm = LinearRegression() lm_strategy = AdapterForFitPredictAdaptee(lm) lm_strategy.fit(X=X, y=y) result_lm = lm_strategy.predict(X=X) self.assertEqual(list(result_lm.keys()), ['predict']) self.assertEqual(result_lm['predict'].shape, (self.size, 1)) gm = GaussianMixture() gm_strategy = AdapterForFitPredictAdaptee(gm) gm_strategy.fit(X=X) result_gm = gm_strategy.predict(X=X) self.assertEqual(sorted(list(result_gm.keys())), sorted(['predict', 'predict_proba'])) self.assertEqual(result_gm['predict'].shape, (self.size, ))
def test_process__predict(self): X = self.X y = self.y lm = LinearRegression() gm = GaussianMixture() lm_strategy = AdapterForFitPredictAdaptee(lm) gm_strategy = AdapterForFitPredictAdaptee(gm) step_lm = Process(lm_strategy) step_gm = Process(gm_strategy) step_lm.fit(X=X, y=y) step_gm.fit(X=X) result_lm = step_lm.predict(X=X) result_gm = step_gm.predict(X=X) self.assertEqual(list(result_lm.keys()), ['predict']) self.assertEqual(sorted(list(result_gm.keys())), sorted(['predict', 'predict_proba']))
def test_process__fit_lm(self): X = self.X y = self.y lm = LinearRegression() stepstrategy = AdapterForFitPredictAdaptee(lm) step = Process(stepstrategy) self.assertEqual(hasattr(step, 'coef_'), False) result = step.fit(X=X, y=y) self.assertEqual(hasattr(step, 'coef_'), True) self.assertEqual(step, result)
def test_FitPredictWithDictionaryOutput__predict(self): X = self.X y = self.y.astype(int) gm = GaussianNB() wrapped_gm = AdapterForFitPredictAdaptee(gm) double_wrap = AdapterForCustomFitPredictWithDictionaryOutputAdaptee( wrapped_gm) double_wrap.fit(X=X, y=y) result = double_wrap.predict(X=X) self.assertEqual( sorted(list(result.keys())), sorted(['predict', 'predict_proba', 'predict_log_proba'])) self.assertEqual(result['predict'].shape[0], self.size) self.assertEqual(result['predict_proba'].shape[0], self.size) self.assertEqual(result['predict_log_proba'].shape[0], self.size)
def test_baseadapter__get_fit_signature(self): lm = LinearRegression() gm = GaussianMixture() lm_strategy = AdapterForFitPredictAdaptee(lm) gm_strategy = AdapterForFitPredictAdaptee(gm) result_lm = lm_strategy._get_fit_signature() result_gm = gm_strategy._get_fit_signature() self.assertEqual(sorted(result_lm), sorted(['X', 'y', 'sample_weight'])) self.assertEqual(sorted(result_gm), sorted(['X', 'y']))
def test_process__set_params(self): lm = LinearRegression() lm_strategy = AdapterForFitPredictAdaptee(lm) step = Process(lm_strategy) result_lm_pre = step.get_params() self.assertEqual(result_lm_pre, { 'copy_X': True, 'fit_intercept': True, 'n_jobs': 1, 'normalize': False }) step.set_params(fit_intercept=False) result_lm_post = step.get_params() self.assertEqual( result_lm_post, { 'copy_X': True, 'fit_intercept': False, 'n_jobs': 1, 'normalize': False })
def wrap_adaptee_in_process(adaptee, adapter_class=None): """ This function wraps the objects defined in Pipegraph's steps parameters in order to provide a common interface for them all. This interface declares two main methods: fit and predict. So, no matter whether the adaptee is capable of doing predict, transform or fit_predict, once wrapped the adapter uses predict as method for producing output. Parameters: ----------- adaptee: a Scikit-Learn object, for instance; or a user made custom estimator may be. The object to be wrapped. adapter_class: A user made class; or one already defined in pipegraph.adapters The wrapper. Returns: ------- An object wrapped into a first adapter layer that provides a common fit and predict interface and then wrapped again in a second external layer using the Process class. Besides of being used by PipeGraph itself, the user can find this function useful for inserting a user made block as one of the steps in PipeGraph step's parameter. """ if adapter_class is not None: strategy = adapter_class(adaptee) elif isinstance(adaptee, Process): return adaptee elif isinstance(adaptee, PipeGraph): strategy = AdapterForCustomFitPredictWithDictionaryOutputAdaptee( adaptee) elif adaptee.__class__ in strategies_for_custom_adaptees: strategy = strategies_for_custom_adaptees[adaptee.__class__](adaptee) elif hasattr(adaptee, 'transform'): strategy = AdapterForFitTransformAdaptee(adaptee) elif hasattr(adaptee, 'predict'): strategy = AdapterForFitPredictAdaptee(adaptee) elif hasattr(adaptee, 'fit_predict'): strategy = AdapterForAtomicFitPredictAdaptee(adaptee) else: raise ValueError('Error: Unknown adaptee!') process = Process(strategy) return process
def test_process__get_predict_signature_lm(self): lm = LinearRegression() lm_strategy = AdapterForFitPredictAdaptee(lm) step_lm = Process(lm_strategy) result_lm = step_lm._get_predict_signature() self.assertEqual(result_lm, ['X'])
def test_process__repr__(self): lm = LinearRegression() lm_strategy = AdapterForFitPredictAdaptee(lm) step = Process(lm_strategy) result = step.__repr__() self.assertEqual(result, lm.__repr__())
def test_baseadapter__init(self): lm = LinearRegression() stepstrategy = AdapterForFitPredictAdaptee(lm) self.assertEqual(stepstrategy._adaptee, lm)
def test_FitPredict__get_predict_signature(self): lm = LinearRegression() lm_strategy = AdapterForFitPredictAdaptee(lm) result_lm = lm_strategy._get_predict_signature() self.assertEqual(result_lm, ['X'])
def test_baseadapter__repr__(self): lm = LinearRegression() lm_strategy = AdapterForFitPredictAdaptee(lm) result = lm_strategy.__repr__() self.assertEqual(result, lm.__repr__())
def test_process__init(self): lm = LinearRegression() stepstrategy = AdapterForFitPredictAdaptee(lm) step = Process(stepstrategy) self.assertEqual(step._strategy, stepstrategy)
def test_baseadapter__getattr__(self): lm = LinearRegression() lm_strategy = AdapterForFitPredictAdaptee(lm) self.assertEqual(lm_strategy.copy_X, True)