Example #1
0
def make_circular_data():
    from jr.meg import mat2mne
    # Toy circular data
    n_trial, n_chan, n_time = 198, 100, 4
    angles = np.linspace(0, 2 * np.pi, 7)[:-1]  # equidistant angles

    # ---- template topography for each angle
    X0 = np.linspace(0, 2, np.sqrt(n_chan)) - 1
    coefs = list()
    for a, angle in enumerate(angles):
        Xm, Ym = np.meshgrid(X0, X0)
        Xm += np.cos(angle)
        Ym += np.sin(angle)
        coefs.append(np.exp(-((Xm**2) + (Ym**2))))

    # ---- add noisy topo to each trial at time=1, pi shift at time=2
    snr = 10.
    np.random.RandomState(0)
    X = np.random.randn(n_trial, n_chan, n_time) / snr
    y = np.arange(n_trial) % len(angles)
    for trial in range(n_trial):
        X[trial, :, 1] += coefs[y[trial]].flatten()
        X[trial, :,
          2] += coefs[y[(trial + len(angles) / 2) % len(angles)]].flatten()

    # ---- export in mne structure
    events = np.array(y * 10, int)  # need integers, and avoid duplicate
    epochs = mat2mne(X, events=events)
    return epochs, angles[y]
Example #2
0
def make_circular_data():
    from jr.meg import mat2mne
    # Toy circular data
    n_trial, n_chan, n_time = 198, 100, 4
    angles = np.linspace(0, 2 * np.pi, 7)[:-1]  # equidistant angles

    # ---- template topography for each angle
    X0 = np.linspace(0, 2, np.sqrt(n_chan)) - 1
    coefs = list()
    for a, angle in enumerate(angles):
        Xm, Ym = np.meshgrid(X0, X0)
        Xm += np.cos(angle)
        Ym += np.sin(angle)
        coefs.append(np.exp(-((Xm ** 2) + (Ym ** 2))))

    # ---- add noisy topo to each trial at time=1, pi shift at time=2
    snr = 10.
    np.random.RandomState(0)
    X = np.random.randn(n_trial, n_chan, n_time) / snr
    y = np.arange(n_trial) % len(angles)
    for trial in range(n_trial):
        X[trial, :, 1] += coefs[y[trial]].flatten()
        X[trial, :, 2] += coefs[
            y[(trial + len(angles)/2) % len(angles)]].flatten()

    # ---- export in mne structure
    events = np.array(y * 10, int)  # need integers, and avoid duplicate
    epochs = mat2mne(X, events=events)
    return epochs, angles[y]
Example #3
0
def quick_score(X, y, clf=None, scorer=None):
    from sklearn.cross_validation import KFold
    regression = (len(np.unique(y)) > 2) & isinstance(y[0], float)
    if scorer is None:
        scorer = scorer_spearman if regression else scorer_auc
    if clf is None:
        clf = RidgeCV(alphas=[(2 * C) ** -1 for C in [1e-4, 1e-2, 1]])\
            if regression else force_predict(LogisticRegression(), axis=1)
    sel = np.where(~np.isnan(y))[0]
    X = X[sel, :, :]
    y = y[sel]
    epochs = mat2mne(X, sfreq=100)
    clf = make_pipeline(StandardScaler(), clf)
    cv = KFold(len(y), 5) if regression else None
    gat = GeneralizationAcrossTime(clf=clf, n_jobs=-1, scorer=scorer, cv=cv)
    gat.fit(epochs, y)
    gat.score(epochs, y)
    return gat
Example #4
0
def quick_score(X, y, clf=None, scorer=None):
    from sklearn.cross_validation import KFold
    regression = (len(np.unique(y)) > 2) & isinstance(y[0], float)
    if scorer is None:
        scorer = scorer_spearman if regression else scorer_auc
    if clf is None:
        clf = RidgeCV(alphas=[(2 * C) ** -1 for C in [1e-4, 1e-2, 1]])\
            if regression else force_predict(LogisticRegression(), axis=1)
    sel = np.where(~np.isnan(y))[0]
    X = X[sel, :, :]
    y = y[sel]
    epochs = mat2mne(X, sfreq=100)
    clf = make_pipeline(StandardScaler(), clf)
    cv = KFold(len(y), 5) if regression else None
    gat = GeneralizationAcrossTime(clf=clf, n_jobs=-1, scorer=scorer, cv=cv)
    gat.fit(epochs, y)
    gat.score(epochs, y)
    return gat