timedecoder module

class timedecoder.TimeDecoder(classifier, scaling='normalization', do_pca=0, n_components=0.95)

Bases: object

Class representing a Decoding/MVPA/Classification pipeline This decoding works with 3D data having a temporal dimension.

Attributes:
clf : sklearn model

Scikit learn classification model

clf_name : str

Classification model name

scaler_name : str
Scaling method. Possibles values :
  • ‘standardization’ : sklearn.preprocessing.StandardScaler - Standardize features by removing the mean and scaling to unit variance
  • ‘normalization’ : sklearn.preprocessing.MinMaxScaler - Transforms features by scaling each feature to a given range
  • ‘robust’ : sklearn.preprocessing.RobustScaler - Scale features using statistics that are robust to outliers
scaler : sklearn Scaler

Scikit-Learn scaler

do_pca : bool

If True, will run a PCA and keep only the first components given the n_components attribute

n_components : int, float, None or string

Number of components to keep. if n_components is not set all components are kept:

n_components == min(n_samples, n_features)

If 0 < n_components < 1 and svd_solver == 'full', select the number of components such that the amount of variance that needs to be explained is greater than the percentage specified by n_components. If svd_solver == 'arpack', the number of components must be strictly less than the minimum of n_features and n_samples. Hence, the None case results in:

n_components == min(n_samples, n_features) - 1

Methods

decode(X, y, y_dict[, n_splits, n_iter, …]) Do the temporal decoding of 3D data X given labels y For each time point, run
decode_mpver(X, y, y_dict[, n_splits, …]) Do the temporal decoding of 3D data X given labels y - Multiprocesses version
plot_scores(scores, y_dict, n_iter, …[, …]) Plot the decoding performances measures listed in scores
single_feature_decoding(X, y, y_dict[, …]) Apply the classifier on each feature separately.
temporal_generalization(X, y, y_dict[, …]) Temporal Generalisation.
decode(X, y, y_dict, n_splits=5, n_iter=1, resample='rus', smote_kind='regular', near_miss_ver=1, do_plot=1, score_to_plot=['auc', 'accuracy'], compute_auc=True, permute_train_labels=False)

Do the temporal decoding of 3D data X given labels y For each time point, run

Parameters:
X : 3D array [n_features, n_pnts, n_trials]

Temporal feature array

y : array [n_trials]

Label array

y_dict : dict

Label dictionnary

n_splits : int

Number of folds for the Stratified K-Folds cross-validation

n_iter : int

Number of times the whole decoding pipeline is repeated

resample : str (default: ‘rus’)

Resampling method. Possible values : ['ros', 'rus', 'nearmiss', 'smote'’‘, 'adasyn'’‘, 'smoteenn'’‘, 'smotomek'’‘] - See timedecoder.get_sampler()

smote_kind : str (default: ‘regular’)

The type of SMOTE algorithm to use one of the following options: 'regular', 'borderline1', 'borderline2', 'svm'. Only used if method=='SMOTE'.

near_miss_ver : int

Version of the NearMiss to use. Possible values are 1, 2 or 3.

do_plot : bool (default: True)

Plot the decoding results

score_to_plot : str | list
The different scores to plot. Possible values :
  • ‘accuracy’
  • ‘precision’
  • ‘recall’
  • ‘f1’
  • ‘class_accuracy’
  • ‘auc’
permute_train_labels : bool (default: False)

If True, permute the training labels. Used for computing a chance level.

compute_auc : bool (default: True)

If True, compute the Area Under the Curve score.

Returns:
scores : dict

Dictionnary containing the different scores : ‘accuracy’, ‘precision’, ‘recall’, ‘f1’, ‘class_accuracy’ and ‘auc’

decode_mpver(X, y, y_dict, n_splits=5, n_iter=3, resample='rus', smote_kind='regular', near_miss_ver=1, do_plot=1, score_to_plot=['auc', 'accuracy'], compute_auc=True, permute_train_labels=False, n_processes=6)

Do the temporal decoding of 3D data X given labels y - Multiprocesses version

Parameters:
X : 3D array [n_features, n_pnts, n_trials]

Temporal feature array

y : array [n_trials]

Label array

y_dict : dict

Label/Class dictionnary

n_splits : int

Number of folds for the Stratified K-Folds cross-validation

n_iter : int

Number of times the whole decoding pipeline is repeated

resample : str (default: ‘rus’)

Resampling method. Possible values : ['ros', 'rus', 'nearmiss', 'smote'’‘, 'adasyn'’‘, 'smoteenn'’‘, 'smotomek'’‘] - See timedecoder.get_sampler()

smote_kind : str (default: ‘regular’)

The type of SMOTE algorithm to use one of the following options: 'regular', 'borderline1', 'borderline2', 'svm'. Only used if method=='SMOTE'.

near_miss_ver : int

Version of the NearMiss to use. Possible values are 1, 2 or 3.

do_plot : bool (default: True)

Plot the decoding results

score_to_plot : str | list
The different scores to plot. Possible values :
  • ‘accuracy’
  • ‘precision’
  • ‘recall’
  • ‘f1’
  • ‘class_accuracy’
  • ‘auc’
compute_auc : bool (default: True)

If True, compute the Area Under the Curve score.

permute_train_labels : bool (default: False)

If True, permute the training labels. Used for computing a chance level.

n_processes : int (default: 6)

Number of processes to use in parralel

Returns:
scores : dict

Dictionnary containing the different scores : ‘accuracy’, ‘precision’, ‘recall’, ‘f1’, ‘class_accuracy’ and ‘auc’

plot_scores(scores, y_dict, n_iter, n_splits, n_pnts, resample, score_to_plot=[])

Plot the decoding performances measures listed in scores

Parameters:
scores : dict
y_dict : dict

Label / Class dictionnary

n_splits : int

Number of folds for the Stratified K-Folds cross-validation

n_iter : int

Number of times the whole decoding pipeline is repeated

n_pnts : int

Number of time points

resample : str

Name of the resampling method used for the decoding

score_to_plot : list

Scores to plot

single_feature_decoding(X, y, y_dict, n_splits=5, n_iter=3, resample='Rus', smote_kind='regular', near_miss_ver=1, do_plot=1, score_to_plot=['auc', 'accuracy'], compute_auc=True, permute_train_labels=False, n_processes=6, features_names=[])

Apply the classifier on each feature separately. Plot the classification results as an image.

Parameters:
X : 3D array [n_features, n_pnts, n_trials]

Temporal feature array

y : array [n_trials]

Label array

y_dict : dict

Label dictionnary

n_splits : int

Number of folds for the Stratified K-Folds cross-validation

n_iter : int

Number of times the whole decoding pipeline is repeated

resample : str (default: ‘rus’)

Resampling method. Possible values : ['ros', 'rus', 'nearmiss', 'smote'’‘, 'adasyn'’‘, 'smoteenn'’‘, 'smotomek'’‘] - See timedecoder.get_sampler()

smote_kind : str (default: ‘regular’)

The type of SMOTE algorithm to use one of the following options: 'regular', 'borderline1', 'borderline2', 'svm'. Only used if method=='SMOTE'.

near_miss_ver : int

Version of the NearMiss to use. Possible values are 1, 2 or 3.

do_plot : bool (default: True)

Plot the decoding results

score_to_plot : str | list
The different scores to plot. Possible values :
  • ‘accuracy’
  • ‘precision’
  • ‘recall’
  • ‘f1’
  • ‘class_accuracy’
  • ‘auc’
compute_auc : bool (default: True)

If True, compute the Area Under the Curve score.

permute_train_labels : bool (default: False)

If True, permute the training labels. Used for computing a chance level.

n_processes : int (default: 6)

Number of processes to use in parralel

features_names : list | None

Name of each feature, used for the results figure.

Returns:
scores : dict

Dictionnary containing the different scores : ‘accuracy’, ‘precision’, ‘recall’, ‘f1’, ‘class_accuracy’ and ‘auc’

temporal_generalization(X, y, y_dict, n_splits=5, n_iter=3, resample='None', smote_kind='regular', near_miss_ver=1, permute_train_labels=False, n_processes=6)

Temporal Generalisation. For each time points, t_i, t_j, train the classifier at t_i, test at time t_j

Parameters:
X : 3D array [n_features, n_pnts, n_trials]

Temporal feature array

y : array [n_trials]

Label array

y_dict : dict

Label/Class dictionnary

n_splits : int

Number of folds for the Stratified K-Folds cross-validation

n_iter : int

Number of times the whole decoding pipeline is repeated

resample : str (default: ‘rus’)

Resampling method. Possible values : ['ros', 'rus', 'nearmiss', 'smote'’‘, 'adasyn'’‘, 'smoteenn'’‘, 'smotomek'’‘] - See timedecoder.get_sampler()

smote_kind : str (default: ‘regular’)

The type of SMOTE algorithm to use one of the following options: 'regular', 'borderline1', 'borderline2', 'svm'. Only used if method=='SMOTE'.

near_miss_ver : int

Version of the NearMiss to use. Possible values are 1, 2 or 3.

permute_train_labels : bool (default: False)

If True, permute the training labels. Used for computing a chance level.

n_processes : int (default: 6)

Number of processes to use in parralel

Returns:
accuracy : array

Accuracy score. Size [n_iter, n_pnts, n_pnts]

auc : array

Area Under the Curve score. Size [n_iter, n_pnts, n_pnts]

timedecoder.apply_pca(X, n_components=0.95)

Not used ? - Should not be here

timedecoder.get_sampler(method, smote_kind='regular', near_miss_ver=1)

Return a resampler instance from the imbalanced-learn API Resampling is used when the number of trials is not the same across the 2 classes. For more details see https://imbalanced-learn.readthedocs.io/en/stable/user_guide.html

Parameters:
method : str
Resampling method. Possible values :
  • ‘ros’, ‘randomoversampler’ : imblearn.over_sampling.RandomOverSampler - Object to over-sample the minority class(es) by picking samples at random with replacement.
  • ‘rus’, ‘randomundersampler’ : imblearn.under_sampling.RandomUnderSampler - Under-sample the majority class(es) by randomly picking samples with or without replacement.
  • ‘smote’ : imblearn.over_sampling.RandomOverSampler - Class to perform over-sampling using SMOTE, Synthetic Minority Over-sampling Technique.
  • ‘adasyn’ : imblearn.over_sampling.ADASYN - Perform over-sampling using Adaptive Synthetic (ADASYN) sampling approach for imbalanced datasets.
  • ‘nearmiss’ : imblearn.over_sampling.RandomOverSampler - Class to perform under-sampling based on NearMiss methods.
  • ‘smoteenn’ : imblearn.combine.SMOTEENN - Combine over- and under-sampling using SMOTE and Edited Nearest Neighbours.
  • ‘smotetomek’ : imblearn.combine.SMOTETomek - Combine over- and under-sampling using SMOTE and Tomek links.
smote_kind : str (default: ‘regular’)
The type of SMOTE algorithm to use one of the following options: 'regular', 'borderline1',

'borderline2', 'svm'. Only used if method=='SMOTE'.

near_miss_ver : int

Version of the NearMiss to use. Possible values are 1, 2 or 3.

Returns:
sampler : imblearn sampler

The sampler from the imbalanced-learn API

timedecoder.print_classes_composition(y, y_dict)

Print the number of trials for the different classes

Parameters:
y : array

Trial labels

y_dict : dict

Dictionnary containing the name of each class

timedecoder.timethis(func)

Timing Wrapper