def get_response_epoch(subject, session):
    epochs, meta = preprocessing.get_epochs_for_subject(subject,
                                                        "stimulus",
                                                        sessions=session)
    epochs = epochs.pick_channels(
        [x for x in epochs.ch_names if x.startswith("M")])
    response, meta = preprocessing.get_epochs_for_subject(subject,
                                                          "response",
                                                          sessions=session)
    response = response.pick_channels(
        [x for x in response.ch_names if x.startswith("M")])
    # Find trials that are present in both time periods
    overlap = list(
        set(epochs.events[:, 2]).intersection(set(response.events[:, 2])))
    epochs = epochs[[str(l) for l in overlap]]
    response = response[[str(l) for l in overlap]]
    id_time = (-0.25 <= epochs.times) & (epochs.times <= 0)
    means = epochs._data[:, :, id_time].mean(-1)
    epochs._data -= means[:, :, np.newaxis]

    response._data = response._data - means[:, :, np.newaxis]

    # Now also baseline stimulus period
    epochs, meta = preprocessing.get_epochs_for_subject(subject,
                                                        "stimulus",
                                                        sessions=session)
    epochs = epochs.pick_channels(
        [x for x in epochs.ch_names if x.startswith("M")])
    id_time = (-0.25 <= epochs.times) & (epochs.times <= 0)
    means = epochs._data[:, :, id_time].mean(-1)
    epochs._data -= means[:, :, np.newaxis]
    data_cov = pymeglcmv.get_cov(epochs, tmin=0, tmax=1.35)
    return data_cov, response
def get_stim_epoch(subject, session):
    epochs, meta = preprocessing.get_epochs_for_subject(subject,
                                                        "stimulus",
                                                        sessions=session)
    epochs = epochs.pick_channels(
        [x for x in epochs.ch_names if x.startswith("M")])
    id_time = (-0.25 <= epochs.times) & (epochs.times <= 0)
    means = epochs._data[:, :, id_time].mean(-1)
    epochs._data -= means[:, :, np.newaxis]
    data_cov = pymeglcmv.get_cov(epochs, tmin=0, tmax=1.35)
    return data_cov, epochs
def get_stim_epoch(subject, session, recording):
    filenames = glob(get_filenames(subject, session, recording)[0])
    epochs = preprocessing.load_epochs(filenames)
    epochs = preprocessing.concatenate_epochs(epochs, None)
    epochs = epochs.pick_channels(
        [x for x in epochs.ch_names if x.startswith('M')])

    id_time = (-0.25 <= epochs.times) & (epochs.times <= 0)
    means = epochs._data[:, :, id_time].mean(-1)
    epochs._data -= means[:, :, np.newaxis]
    data_cov = lcmv.get_cov(epochs, tmin=0, tmax=3)
    return data_cov, epochs, filenames
def get_response_epoch(subject, session, recording):
    stimulus = glob(get_filenames(subject, session, recording)[0])
    response = glob(get_filenames(subject, session, recording)[1])

    stimulus = preprocessing.load_epochs(stimulus)
    stimulus = preprocessing.concatenate_epochs(stimulus, None)
    stimulus = stimulus.pick_channels(
        [x for x in stimulus.ch_names if x.startswith('M')])
    response = preprocessing.load_epochs(response)
    response = preprocessing.concatenate_epochs(response, None)
    response = stimulus.pick_channels(
        [x for x in response.ch_names if x.startswith('M')])

    id_time = (-0.25 <= stimulus.times) & (stimulus.times <= 0)
    means = stimulus._data[:, :, id_time].mean(-1)
    stimulus._data = stimulus._data - means[:, :, np.newaxis]
    response._data = response._data - means[:, :, np.newaxis]
    data_cov = lcmv.get_cov(stimulus, tmin=0, tmax=3)
    return data_cov, response
Пример #5
0
def do_source_recon(subj, session, njobs=4):

    epochs_filename_stim = os.path.join(data_folder, "epochs", subj, session,
                                        '{}-epo.fif.gz'.format('stimlock'))
    epochs_filename_resp = os.path.join(data_folder, "epochs", subj, session,
                                        '{}-epo.fif.gz'.format('resplock'))
    trans_filename = os.path.join(data_folder, "transformation_matrix",
                                  '{}_{}-trans.fif'.format(subj, session))

    if os.path.isfile(epochs_filename_stim):

        runs = sorted([
            run.split('/')[-1] for run in glob.glob(
                os.path.join(data_folder, "raw", subj, session, "meg", "*.ds"))
        ])
        center = int(np.floor(len(runs) / 2.0))
        raw_filename = os.path.join(data_folder, "raw", subj, session, "meg",
                                    runs[center])

        # # make transformation matrix:
        # sr.make_trans(subj, raw_filename, epochs_filename, trans_filename)

        # load labels:
        labels = sr.get_labels(subject=subj,
                               filters=['*wang*.label', '*JWG*.label'],
                               annotations=['HCPMMP1'])
        labels = sr.labels_exclude(labels=labels,
                                   exclude_filters=[
                                       'wang2015atlas.IPS4',
                                       'wang2015atlas.IPS5',
                                       'wang2015atlas.SPL', 'JWG_lat_Unknown'
                                   ])
        labels = sr.labels_remove_overlap(labels=labels,
                                          priority_filters=['wang', 'JWG'])
        print(labels)

        # load epochs:
        epochs_stim = mne.read_epochs(epochs_filename_stim)
        epochs_stim = epochs_stim.pick_channels(
            [x for x in epochs_stim.ch_names if x.startswith('M')])
        epochs_resp = mne.read_epochs(epochs_filename_resp)
        epochs_resp = epochs_resp.pick_channels(
            [x for x in epochs_resp.ch_names if x.startswith('M')])

        # baseline stuff:
        overlap = list(
            set(epochs_stim.events[:, 2]).intersection(
                set(epochs_resp.events[:, 2])))
        epochs_stim = epochs_stim[[str(l) for l in overlap]]
        epochs_resp = epochs_resp[[str(l) for l in overlap]]
        id_time = (-0.3 <= epochs_stim.times) & (epochs_stim.times <= -0.2)
        means = epochs_stim._data[:, :, id_time].mean(-1)
        epochs_stim._data = epochs_stim._data - means[:, :, np.newaxis]
        epochs_resp._data = epochs_resp._data - means[:, :, np.newaxis]

        # TFR settings:
        fois_h = np.arange(42, 162, 4)
        fois_l = np.arange(2, 42, 2)
        tfr_params = {
            'HF': {
                'foi': fois_h,
                'cycles': fois_h * 0.4,
                'time_bandwidth': 5 + 1,
                'n_jobs': njobs,
                'est_val': fois_h,
                'est_key': 'HF'
            },
            'LF': {
                'foi': fois_l,
                'cycles': fois_l * 0.4,
                'time_bandwidth': 1 + 1,
                'n_jobs': njobs,
                'est_val': fois_l,
                'est_key': 'LF'
            }
        }

        # get cov:
        data_cov = lcmv.get_cov(epochs_stim, tmin=0, tmax=1)
        noise_cov = None

        # get lead field:
        forward, bem, source = sr.get_leadfield(
            subject=subj,
            raw_filename=raw_filename,
            epochs_filename=epochs_filename_stim,
            trans_filename=trans_filename,
            conductivity=(0.3, 0.006, 0.3),
            njobs=njobs)

        # do source level analysis:
        for tl, epochs in zip(['stimlock', 'resplock'],
                              [epochs_stim, epochs_resp]):
            for signal_type in ['LF', 'HF']:
                print(signal_type)

                # events:
                events = epochs.events[:, 2]
                data = []
                filters = lcmv.setup_filters(epochs.info,
                                             forward,
                                             data_cov,
                                             None,
                                             labels,
                                             njobs=njobs)

                # in chunks:
                chunks = 100
                for i in range(0, len(events), chunks):
                    filename = os.path.join(data_folder, "source_level", 'lcmv_{}_{}_{}_{}_{}-source.hdf'.\
                        format(subj, session, tl, signal_type, i))
                    # if os.path.isfile(filename):
                    #     continue
                    M = lcmv.reconstruct_tfr(filters,
                                             epochs.info,
                                             epochs._data[i:i + chunks],
                                             events[i:i + chunks],
                                             epochs.times,
                                             est_args=tfr_params[signal_type],
                                             njobs=njobs)
                    M.to_hdf(filename, 'epochs')

                    del M