Exemplo n.º 1
0
def test_potato_specific_labels(get_covmats):
    n_trials, n_channels = 6, 3
    covmats = get_covmats(n_trials, n_channels)
    pt = Potato(threshold=1, pos_label=2, neg_label=7)
    pt.fit(covmats)
    assert_array_equal(np.unique(pt.predict(covmats)), [2, 7])
    # fit with custom positive label
    pt.fit(covmats, y=[2] * n_trials)
Exemplo n.º 2
0
def test_Potato_init():
    """Test Potato"""
    covset = generate_cov(20, 3)
    labels = np.array([0, 1]).repeat(10)

    # init
    pt = Potato()

    # fit no labels
    pt.fit(covset)

    # fit with labels
    assert_raises(ValueError, pt.fit, covset, y=[1])
    assert_raises(ValueError, pt.fit, covset, y=[0] * 20)
    assert_raises(ValueError, pt.fit, covset, y=[0, 2, 3] + [1] * 17)
    pt.fit(covset, labels)

    # transform
    pt.transform(covset)

    # transform
    pt.predict(covset)

    # lower threshold
    pt = Potato(threshold=1)
    pt.fit(covset)
def test_Potato_init():
    """Test Potato"""
    covset = generate_cov(20, 3)
    labels = np.array([0, 1]).repeat(10)

    # init
    pt = Potato()

    # fit no labels
    pt.fit(covset)

    # fit with labels
    assert_raises(ValueError, pt.fit, covset, y=[1])
    assert_raises(ValueError, pt.fit, covset, y=[0] * 20)
    assert_raises(ValueError, pt.fit, covset, y=[0, 2, 3] + [1] * 17)
    pt.fit(covset, labels)

    # transform
    pt.transform(covset)

    # predict
    pt.predict(covset)

    # lower threshold
    pt = Potato(threshold=1)
    pt.fit(covset)

    # test positive labels
    pt = Potato(threshold=1, pos_label=2, neg_label=7)
    pt.fit(covset)
    assert_array_equal(np.unique(pt.predict(covset)), [2, 7])

    # test with custom positive label
    pt.fit(covset, y=[2]*20)

    # different positive and neg label
    assert_raises(ValueError, Potato, pos_label=0)
Exemplo n.º 4
0
def test_Potato_init():
    """Test Potato"""
    covset = generate_cov(20, 3)
    labels = np.array([0, 1]).repeat(10)

    # init
    pt = Potato()

    # fit no labels
    pt.fit(covset)

    # fit with labels
    assert_raises(ValueError, pt.fit, covset, y=[1])
    assert_raises(ValueError, pt.fit, covset, y=[0] * 20)
    assert_raises(ValueError, pt.fit, covset, y=[0, 2, 3] + [1] * 17)
    pt.fit(covset, labels)

    # transform
    pt.transform(covset)

    # predict
    pt.predict(covset)

    # lower threshold
    pt = Potato(threshold=1)
    pt.fit(covset)

    # test positive labels
    pt = Potato(threshold=1, pos_label=2, neg_label=7)
    pt.fit(covset)
    assert_array_equal(np.unique(pt.predict(covset)), [2, 7])

    # test with custom positive label
    pt.fit(covset, y=[2] * 20)

    # different positive and neg label
    assert_raises(ValueError, Potato, pos_label=0)
Exemplo n.º 5
0
def test_potato_threshold(get_covmats):
    n_trials, n_channels = 6, 3
    covmats = get_covmats(n_trials, n_channels)
    pt = Potato(threshold=1)
    pt.fit(covmats)
low_freq, high_freq = 1., 35.
rp = Potato(metric='riemann', threshold=z_th)

# EEG processing for RP
rp_sig = filter_bandpass(raw, low_freq, high_freq)  # band-pass filter
rp_epochs = make_fixed_length_epochs(  # epoch time-series
    rp_sig,
    duration=duration,
    overlap=duration - interval,
    verbose=False)
rp_covs = Covariances(estimator='scm').transform(rp_epochs.get_data())

# RP training
train_covs = 45  # nb of matrices for training
train_set = range(train_covs)
rp.fit(rp_covs[train_set])

###############################################################################
# Riemannian potato field
# -----------------------
#
# Riemannian potato field (RPF) [1]_ combines several potatoes of low
# dimensionality, each one designed to capture a different kind of artifact
# typically affecting some specific spatial area (i.e. subsets of channels)
# and/or specific frequency bands.
#
# BCI or NFB applications aim at the modulation specific brain oscillations, it
# is thus advisable to exclude such frequencies from potatoes so as to prevent
# desirable brain modulations to be detected as artifactual.

# RPF definition
Exemplo n.º 7
0
def test_Potato_init():
    """Test Potato"""
    covset = generate_cov(20, 3)
    labels = np.array([0, 1]).repeat(10)

    # init
    pt = Potato()

    # fit no labels
    pt.fit(covset)

    # fit with labels
    with pytest.raises(ValueError):
        pt.fit(covset, y=[1])

    with pytest.raises(ValueError):
        pt.fit(covset, y=[0] * 20)

    with pytest.raises(ValueError):
        pt.fit(covset, y=[0, 2, 3] + [1] * 17)

    pt.fit(covset, labels)

    # transform
    pt.transform(covset)
    pt.transform(covset[0][np.newaxis, ...])  # transform a single trial

    # predict
    pt.predict(covset)
    pt.predict(covset[0][np.newaxis, ...])  # predict a single trial

    # predict_proba
    pt.predict_proba(covset)
    pt.predict_proba(covset[0][np.newaxis, ...])

    # lower threshold
    pt = Potato(threshold=1)
    pt.fit(covset)

    # test positive labels
    pt = Potato(threshold=1, pos_label=2, neg_label=7)
    pt.fit(covset)
    assert_array_equal(np.unique(pt.predict(covset)), [2, 7])

    # test with custom positive label
    pt.fit(covset, y=[2] * 20)

    # different positive and neg label
    with pytest.raises(ValueError):
        Potato(pos_label=0)
Exemplo n.º 8
0
class MDMWithPotato(MDM):
    """Classification by Minimum Distance to Mean combined with an artifact rejection using a Potato
    """

    def __init__(self, metric='riemann', n_jobs=1, threshold=3, n_iter_max=100,
                 rejected_label=-1):
        """Init."""
        # store params for cloning purpose
        super().__init__(metric=metric, n_jobs=n_jobs)
        self.potato = Potato(metric=metric, threshold=threshold,
                             n_iter_max=n_iter_max,
                             neg_label=0,
                             pos_label=1)
        self.rejected_label = rejected_label

    def fit(self, X, y, sample_weight=None):
        """Fit (estimates) the centroids and the potato

        Parameters
        ----------
        X : ndarray, shape (n_trials, n_channels, n_channels)
            ndarray of SPD matrices.
        y : ndarray shape (n_trials, 1)
            labels corresponding to each trial.
        sample_weight : None | ndarray shape (n_trials, 1)
            the weights of each sample. if None, each sample is treated with
            equal weights.

        Returns
        -------
        self : MDM instance
            The MDM instance.
        """
        self.potato.fit(deepcopy(X))
        super().fit(X, y, sample_weight)
        return self

    def predict(self, X):
        """get the predictions.

        Parameters
        ----------
        X : ndarray, shape (n_trials, n_channels, n_channels)
            ndarray of SPD matrices.

        Returns
        -------
        pred : ndarray of int, shape (n_trials, 1)
            the prediction for each trials according to the closest centroid.
        """
        mdm_predict = super().predict(X)
        potato_predict = self.potato.predict(X)
        mdm_predict[potato_predict == 0] = self.rejected_label
        return mdm_predict

    def transform(self, X):
        """get the distance to each centroid.

        Parameters
        ----------
        X : ndarray, shape (n_trials, n_channels, n_channels)
            ndarray of SPD matrices.

        Returns
        -------
        dist : ndarray, shape (n_trials, n_classes)
            the distance to each centroid according to the metric.
        """
        return super().transform(X)

    def fit_predict(self, X, y):
        """Fit and predict in one function."""
        super().fit(X, y)
        self.potato.fit(X)
        return self.predict(X)

    def predict_proba(self, X):
        """Predict proba using softmax and set to NaN the rejected trials

        Parameters
        ----------
        X : ndarray, shape (n_trials, n_channels, n_channels)
            ndarray of SPD matrices.

        Returns
        -------
        prob : ndarray, shape (n_trials, n_classes)
            the softmax probabilities for each class.
        """
        mdm_proba = softmax(-super()._predict_distances(X))
        potato_predict = self.potato.predict(X)
        mdm_proba[potato_predict == 0] = np.NaN
        return mdm_proba