def test_single_input_classification(self):
     pc_predictions = pc_cross_validation.cross_val_predict(self.clf,
                                                            self.X_cls,
                                                            self.y_cls,
                                                            cv=self.cv,
                                                            n_processes=1)
     self.assertTrue(
         np.array_equal(self.cls_predictions,
                        pc_predictions['predict']['y_pred']),
         'pipecaster predictions did not match sklearn control')
 def test_multi_input_classification(self):
     mclf = MultichannelPipeline(n_channels=1)
     mclf.add_layer(self.clf)
     pc_predictions = pc_cross_validation.cross_val_predict(mclf,
                                                            [self.X_cls],
                                                            self.y_cls,
                                                            cv=self.cv,
                                                            n_processes=1)
     self.assertTrue(
         np.array_equal(self.cls_predictions,
                        pc_predictions['predict']['y_pred']),
         'pipecaster predictions did not match sklearn control')
 def test_multi_input_regression(self):
     mrgr = MultichannelPipeline(n_channels=1)
     mrgr.add_layer(self.rgr)
     pc_predictions = pc_cross_validation.cross_val_predict(mrgr,
                                                            [self.X_rgr],
                                                            self.y_rgr,
                                                            cv=self.cv,
                                                            n_processes=1)
     self.assertTrue(
         np.array_equal(self.rgr_predictions,
                        pc_predictions['predict']['y_pred']),
         'pipecaster predictions did not match sklearn control')
 def test_multi_input_classification_parallel(self):
     if n_cpus > 1:
         warnings.filterwarnings("ignore")
         parallel.start_if_needed(n_cpus=n_cpus)
         mclf = MultichannelPipeline(n_channels=1)
         mclf.add_layer(self.clf)
         pc_predictions = pc_cross_validation.cross_val_predict(
             mclf, [self.X_cls], self.y_cls, cv=self.cv, n_processes=n_cpus)
         self.assertTrue(
             np.array_equal(self.cls_predictions,
                            pc_predictions['predict']['y_pred']),
             'pipecaster predictions did not match sklearn control')
         warnings.resetwarnings()
    def fit_transform(self, Xs, y=None, groups=None, **fit_params):
        is_classifier = utils.is_classifier(self.multichannel_predictor)
        if y is not None and is_classifier:
            self.classes_, y = np.unique(y, return_inverse=True)

        self.fit(Xs, y, **fit_params)
        transform_method = self.get_transform_method()

        # if internal cv training is disabled
        if (self.internal_cv is None
                or (type(self.internal_cv) == int and self.internal_cv < 2)):
            y_transform = self.transform(X)
        # internal cv training is enabled
        else:
            split_results = cross_val_predict(
                self.multichannel_predictor,
                Xs,
                y,
                groups=groups,
                predict_method=None,
                transform_method=transform_method,
                score_method=self.score_method,
                cv=self.internal_cv,
                combine_splits=True,
                n_processes=self.cv_processes,
                fit_params=fit_params)

            y_transform = split_results['transform']['y_pred']
            y_score = split_results['score']['y_pred']
            is_binary = (True if is_classifier and len(self.classes_) == 2 else
                         False)
            score_method = split_results['score']['method']
            self.score_ = score_predictions(y, y_score, score_method,
                                            self.scorer, is_classifier,
                                            is_binary)

        # convert predictions to transformed matrix:
        X_t = y_transform
        if len(X_t.shape) == 1:
            X_t = X_t.reshape(-1, 1)

        # drop the redundant prob output from binary classifiers:
        elif (len(X_t.shape) == 2 and X_t.shape[1] == 2
              and utils.is_classifier(self.model)):
            X_t = X_t[:, 1].reshape(-1, 1)

        Xs_t = [None for X in Xs]
        Xs_t[0] = X_t

        return Xs_t
 def test_multi_input_regression_parallel_starmap(self):
     if n_cpus > 2:
         warnings.filterwarnings("ignore")
         parallel.start_if_needed(n_cpus=n_cpus)
         mrgr = MultichannelPipeline(n_channels=1)
         mrgr.add_layer(self.rgr)
         pc_predictions = pc_cross_validation.cross_val_predict(
             mrgr, [self.X_rgr],
             self.y_rgr,
             cv=self.cv,
             n_processes=n_cpus - 1)
         self.assertTrue(
             np.array_equal(self.rgr_predictions,
                            pc_predictions['predict']['y_pred']),
             'pipecaster predictions did not match sklearn '
             'control')
         warnings.resetwarnings()