<commit_before>import sys
import numpy as np
import mne
from mne.decoding import GeneralizationAcrossTime
from sklearn.externals import joblib
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

from my_settings import (tf_folder, data_path, epochs_folder)

import matplotlib
matplotlib.use('Agg')

subject = sys.argv[1]

# Load epochs from both conditions
data_ctl_left = np.load(tf_folder + "%s_ctl_left-4-complex-tfr.npy" % (subject
                                                                       ))
data_ent_left = np.load(tf_folder + "%s_ent_left-4-complex-tfr.npy" % (subject
                                                                       ))
data_ctl_right = np.load(tf_folder + "%s_ctl_right-4-complex-tfr.npy" % (
    subject))
data_ent_right = np.load(tf_folder + "%s_ent_right-4-complex-tfr.npy" % (
    subject))
epochs = mne.read_epochs(
    epochs_folder + "%s_trial_start-epo.fif" % subject, preload=False)

X = np.vstack([
    np.mean(np.angle(data_ctl_left), axis=2),
    np.mean(np.angle(data_ctl_right), axis=2),
    np.mean(np.angle(data_ent_left), axis=2),
    np.mean(np.angle(data_ent_right), axis=2)
])
y = np.concatenate([
    np.zeros(len(data_ctl_left)), np.ones(len(data_ctl_right)),
    np.ones(len(data_ent_left)) * 2, np.ones(len(data_ent_right)) * 3
])

# Create epochs to use for classification
n_trial, n_chan, n_time = X.shape
events = np.vstack((range(n_trial), np.zeros(n_trial, int), y.astype(int))).T

info = epochs.info
epochs_data = mne.EpochsArray(data=X, info=info, events=events, verbose=False)
epochs_data.times = epochs.times[::4][:-1]

# Crop and downsmample to make it faster
epochs_data.crop(tmin=None, tmax=1)

# Equalise channels and epochs, and concatenate epochs
epochs_data.equalize_event_counts(["0", "1", "2", "3"])

# Classifier
clf = make_pipeline(
    StandardScaler(),
    LogisticRegression(C=1, solver="lbfgs", multi_class="multinomial"))

# Setup the y vector and GAT
gat = GeneralizationAcrossTime(
    predict_mode='mean-prediction', scorer="accuracy", n_jobs=1)

# Fit model
print("fitting GAT")
gat.fit(epochs_data)

# Scoring
print("Scoring GAT")
gat.score(epochs_data)

# Save model
joblib.dump(gat, data_path + "decode_time_gen/%s_gat_all.jl" % subject)

# make matrix plot and save it
fig = gat.plot(cmap="viridis", title="Temporal Gen for subject: %s" % subject)
fig.savefig(data_path + "decode_time_gen/%s_gat_matrix_all.png" % subject)

fig = gat.plot_diagonal(
    chance=0.5, title="Temporal Gen for subject: %s" % subject)
fig.savefig(data_path + "decode_time_gen/%s_gat_diagonal_all.png" % subject)<commit_msg>Changed save name<commit_after>import sys
import numpy as np
import mne
from mne.decoding import GeneralizationAcrossTime
from sklearn.externals import joblib
from sklearn.linear_model import LogisticRegression
from sklearn.pipeline import make_pipeline
from sklearn.preprocessing import StandardScaler

from my_settings import (tf_folder, data_path, epochs_folder)

import matplotlib
matplotlib.use('Agg')

subject = sys.argv[1]

# Load epochs from both conditions
data_ctl_left = np.load(tf_folder + "%s_ctl_left-4-complex-tfr.npy" % (subject
                                                                       ))
data_ent_left = np.load(tf_folder + "%s_ent_left-4-complex-tfr.npy" % (subject
                                                                       ))
data_ctl_right = np.load(tf_folder + "%s_ctl_right-4-complex-tfr.npy" % (
    subject))
data_ent_right = np.load(tf_folder + "%s_ent_right-4-complex-tfr.npy" % (
    subject))
epochs = mne.read_epochs(
    epochs_folder + "%s_trial_start-epo.fif" % subject, preload=False)

X = np.vstack([
    np.mean(np.angle(data_ctl_left), axis=2),
    np.mean(np.angle(data_ctl_right), axis=2),
    np.mean(np.angle(data_ent_left), axis=2),
    np.mean(np.angle(data_ent_right), axis=2)
])
y = np.concatenate([
    np.zeros(len(data_ctl_left)), np.ones(len(data_ctl_right)),
    np.ones(len(data_ent_left)) * 2, np.ones(len(data_ent_right)) * 3
])

# Create epochs to use for classification
n_trial, n_chan, n_time = X.shape
events = np.vstack((range(n_trial), np.zeros(n_trial, int), y.astype(int))).T

info = epochs.info
epochs_data = mne.EpochsArray(data=X, info=info, events=events, verbose=False)
epochs_data.times = epochs.times[::4][:-1]

# Crop and downsmample to make it faster
epochs_data.crop(tmin=None, tmax=1)

# Equalise channels and epochs, and concatenate epochs
epochs_data.equalize_event_counts(["0", "1", "2", "3"])

# Classifier
clf = make_pipeline(
    StandardScaler(),
    LogisticRegression(C=1, solver="lbfgs", multi_class="multinomial"))

# Setup the y vector and GAT
gat = GeneralizationAcrossTime(
    predict_mode='mean-prediction', scorer="accuracy", n_jobs=1)

# Fit model
print("fitting GAT")
gat.fit(epochs_data)

# Scoring
print("Scoring GAT")
gat.score(epochs_data)

# Save model
joblib.dump(gat, data_path + "decode_time_gen/%s_gat_all_phase.jl" % subject)

# make matrix plot and save it
fig = gat.plot(cmap="viridis", title="Temporal Gen for subject: %s" % subject)
fig.savefig(data_path + "decode_time_gen/%s_gat_matrix_all_phase.png" %
            subject)

fig = gat.plot_diagonal(
    chance=0.5, title="Temporal Gen for subject: %s" % subject)
fig.savefig(data_path + "decode_time_gen/%s_gat_diagonal_all_phase.png" %
            subject)
{'fraction_non_alphanumeric': 0.12766740250183958, 'fraction_numerical': 0.008094186902133923, 'mean_word_length': 4.067101584342963, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '903050', 'n_tokens_mistral': 2003, 'n_tokens_neox': 1967, 'n_words': 381}