Ejemplo n.º 1
0
def test_potato_1channel(get_covmats):
    n_trials, n_channels = 6, 1
    covmats_1chan = get_covmats(n_trials, n_channels)
    pt = Potato()
    pt.fit_transform(covmats_1chan)
    pt.predict(covmats_1chan)
    pt.predict_proba(covmats_1chan)
Ejemplo n.º 2
0
    def fit(self, X: np.array, y: np.array) -> object:
        """Fit.

        Do nothing. For compatibility purpose.

        Parameters
        ----------
        X : ndarray, shape (n_trials, n_channels, n_samples)
            ndarray of trials.
        y : ndarray shape (n_trials,)
            labels corresponding to each trial, not used.

        Returns
        -------
        self : SSVEPPotato instance
            The SSVEPPotato instance.
        """
        self.classes = self.classes or set(y)
        self._n_potatoes = len(self.classes)
        self._potatoes = []
        for _class in self.classes:
            X_class = X[y == _class, :, :]
            potato = Potato(n_iter_max=self.n_iter_max).fit(X_class)
            self._potatoes.append(potato)

        return self
Ejemplo n.º 3
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)
Ejemplo n.º 4
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)
Ejemplo n.º 5
0
def test_potato_fit_error(y_fail, get_covmats):
    n_trials, n_channels = 6, 3
    covmats = get_covmats(n_trials, n_channels)
    with pytest.raises(ValueError):
        Potato().fit(covmats, y=y_fail)
Ejemplo n.º 6
0
def test_potato_partial_fit_not_fitted(get_covmats):
    n_trials, n_channels = 6, 3
    covmats = get_covmats(n_trials, n_channels)
    with pytest.raises(ValueError):  # potato not fitted
        Potato().partial_fit(covmats)
Ejemplo n.º 7
0
# -----------------------------
#
# 2D projection of the z-score map of the Riemannian potato, for 2x2 covariance
# matrices (in blue if clean, in red if artifacted) and their reference matrix
# (in black). The colormap defines the z-score and a chosen isocontour defines
# the potato. It reproduces Fig 1 of reference [2]_.

z_th = 2.0  # z-score threshold
train_covs = 40  # nb of matrices to train the potato

###############################################################################

# Calibrate potato by unsupervised training on first matrices: compute a
# reference matrix, mean and standard deviation of distances to this reference.
train_set = range(train_covs)
rpotato = Potato(metric='riemann', threshold=z_th).fit(covs[train_set])
rp_center = rpotato._mdm.covmeans_[0]
epotato = Potato(metric='euclid', threshold=z_th).fit(covs[train_set])
ep_center = epotato._mdm.covmeans_[0]

rp_labels = rpotato.predict(covs[train_set])
rp_colors = ['b' if ll == 1 else 'r' for ll in rp_labels.tolist()]
ep_labels = epotato.predict(covs[train_set])
ep_colors = ['b' if ll == 1 else 'r' for ll in ep_labels.tolist()]

# Zscores in the horizontal 2D plane going through the reference
X, Y = np.meshgrid(np.linspace(1, 31, 100), np.linspace(1, 31, 100))
rp_zscores = get_zscores(X,
                         np.full_like(X, rp_center[0, 1]),
                         Y,
                         potato=rpotato)
Ejemplo n.º 8
0
def test_potato_fit_equal_labels(get_covmats):
    n_trials, n_channels = 6, 3
    covmats = get_covmats(n_trials, n_channels)
    with pytest.raises(ValueError):
        Potato(pos_label=0).fit(covmats)
Ejemplo n.º 9
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)
# Define time-series epoching with a sliding window
duration = 2.5  # duration of epochs
interval = 0.2  # interval between epochs

###############################################################################
# Riemannian potato
# -----------------
#
# Riemannian potato (RP) [2]_ selects all channels and filter between 1 and
# 35 Hz.

# RP definition
z_th = 2.0  # z-score threshold
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])
Ejemplo n.º 11
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)
Ejemplo n.º 12
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
Ejemplo n.º 13
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)
Ejemplo n.º 14
0
             squeeze_me=True)
y_train_all = []
for i in range(8):
    y_train_all.append(d_['MatCoh'][i, 1])

all_pred = []
for s in trange(8):
    X = np.array([i for i in range(120)])
    y_train = y_train_all[s]
    le = LabelEncoder()
    y_train = le.fit_transform(y_train)

    # Patate
    th = 5 # 4
    mat = FeatConn("Coh", s, "test").fit_transform(X)
    pt = Potato(metric='logeuclid', threshold=th).fit(mat)
    atfcoh = (pt.predict(mat) == 1)
    mat = FeatConn("Cov", s, "test").fit_transform(X)
    pt = Potato(metric='logeuclid', threshold=th).fit(mat)
    atfcov = (pt.predict(mat) == 1)
    mat = FeatConn("PLV", s, "test").fit_transform(X)
    pt = Potato(metric='logeuclid', threshold=th).fit(mat)
    atfplv = (pt.predict(mat) == 1)
    atf = np.logical_and(atfcoh, np.logical_and(atfcov, atfplv))
    print ("removed ", len(X)-atf.sum(), "artefacts")
    X_train = X[:80]
    X_train = X_train[atf[:80]]
    # On n'applique pas la patate pour X_test car on doit prédire tout
    X_test = X[80:]
    y_train = y_train[atf[:80]]
Ejemplo n.º 15
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)