Exemple #1
0
def load_VTC_data(FOLDERPATH, LOGS_DIR, SUBJ_LIST, BLOCS_LIST):
    VTC_alldata = []
    for subj in SUBJ_LIST:
        all_subj = []  ## all the data of one subject
        for run in BLOCS_LIST:
            # get events from epochs file
            epo_path, epo_filename = get_SAflow_bids(FOLDERPATH,
                                                     subj,
                                                     run,
                                                     'epo',
                                                     cond=None)
            events_epoched = mne.read_events(
                epo_filename, verbose=False
            )  # get events from the epochs file (so no resp event)
            # get events from original file (only 599 events)
            events_fname, events_fpath = get_SAflow_bids(FOLDERPATH,
                                                         subj,
                                                         run,
                                                         stage='preproc_raw',
                                                         cond=None)
            raw = read_raw_fif(
                events_fpath, preload=False,
                verbose=False)  #, min_duration=2/epochs.info['sfreq'])
            all_events = mne.find_events(raw,
                                         min_duration=2 / raw.info['sfreq'],
                                         verbose=False)
            stim_idx = []
            for i in range(len(all_events)):
                if all_events[i, 2] in [21, 31]:
                    stim_idx.append(i)
            all_events = all_events[stim_idx]
            # compute VTC for all trials
            log_file = find_logfile(subj, run, os.listdir(LOGS_DIR))
            VTC, INbounds, OUTbounds, INzone, OUTzone = get_VTC_from_file(
                LOGS_DIR + log_file, lobound=None, hibound=None)
            epochs_VTC = []
            for event_time in events_epoched[:, 0]:
                idx = list(all_events[:, 0]).index(event_time)
                epochs_VTC.append(VTC[idx])
            all_subj.append(np.array(epochs_VTC))
        VTC_alldata.append(all_subj)
    return VTC_alldata
Exemple #2
0
def split_TFR(filepath,
              subj,
              bloc,
              by='VTC',
              lobound=None,
              hibound=None,
              stage='1600TFR',
              filt_order=3,
              filt_cutoff=0.05):
    if by == 'VTC':
        event_id = {'IN': 1, 'OUT': 0}
        INidx, OUTidx, VTC_epochs, idx_trimmed = get_VTC_epochs(
            LOGS_DIR,
            subj,
            bloc,
            lobound=lobound,
            hibound=hibound,
            stage=stage[:-3] + 'epo',
            save_epochs=False,
            filt_order=filt_order,
            filt_cutoff=filt_cutoff)
        epo_path, epo_filename = get_SAflow_bids(FOLDERPATH,
                                                 subj,
                                                 bloc,
                                                 stage=stage[:-3] + 'epo',
                                                 cond=None)
        epo_events = mne.read_events(
            epo_filename, verbose=False
        )  # get events from the epochs file (so no resp event)
        TFR_path, TFR_filename = get_SAflow_bids(FOLDERPATH,
                                                 subj,
                                                 bloc,
                                                 stage=stage,
                                                 cond=None)
        TFR = mne.time_frequency.read_tfrs(TFR_filename)
        for i, event in enumerate(epo_events):
            if i in INidx:
                TFR[0].events[i, 2] = 1
            if i in OUTidx:
                TFR[0].events[i, 2] = 0
        TFR[0].event_id = event_id
    return TFR
Exemple #3
0
def load_PSD_data(FOLDERPATH,
                  SUBJ_LIST,
                  BLOCS_LIST,
                  time_avg=True,
                  stage='PSD'):
    '''
    Returns a list containing n_subj lists of n_blocs matrices of shape n_freqs X n_channels X n_trials
    '''
    PSD_alldata = []
    for subj in SUBJ_LIST:
        all_subj = []  ## all the data of one subject
        for run in BLOCS_LIST:
            SAflow_bidsname, SAflow_bidspath = get_SAflow_bids(FOLDERPATH,
                                                               subj,
                                                               run,
                                                               stage=stage,
                                                               cond=None)
            mat = loadmat(SAflow_bidspath)['PSD']
            if time_avg == True:
                mat = np.mean(mat,
                              axis=2)  # average PSDs in time across epochs
            all_subj.append(mat)
        PSD_alldata.append(all_subj)
    return PSD_alldata
Exemple #4
0
import os
import os.path as op
from utils import get_SAflow_bids
from neuro import saflow_preproc, find_rawfile
from saflow_params import FOLDERPATH, SUBJ_LIST, BLOCS_LIST, REPORTS_PATH


if __name__ == "__main__":
	# create report path
	try:
		os.mkdir(REPORTS_PATH)
	except:
		print('Report path already exists.')
	for subj in SUBJ_LIST:
		for bloc in BLOCS_LIST:
			filepath, filename = find_rawfile(subj, bloc, FOLDERPATH)
			save_pattern = get_SAflow_bids(FOLDERPATH, subj=subj, run=bloc, stage='preproc_raw')[1]
			#save_pattern =  op.join(FOLDERPATH + filepath, filename[:-3] + '_preproc_raw.fif.gz')
			report_pattern = op.join(REPORTS_PATH, filename[:-3] + '_preproc_report.html')
			full_filepath = FOLDERPATH + filepath + filename
			saflow_preproc(full_filepath, save_pattern, report_pattern)
Exemple #5
0
import mne
import numpy as np
from utils import get_SAflow_bids
from neuro import compute_PSD
from saflow_params import FOLDERPATH, IMG_DIR, FREQS, SUBJ_LIST, BLOCS_LIST
from scipy.io import savemat

### OPEN SEGMENTED FILES AND COMPUTE PSDS
if __name__ == "__main__":
    for subj in SUBJ_LIST:
        for bloc in BLOCS_LIST:
            SAflow_bidsname, SAflow_bidspath = get_SAflow_bids(FOLDERPATH, subj, bloc, stage='epo', cond=None)
            data = mne.read_epochs(SAflow_bidspath)
            psds = compute_PSD(data, data.info['sfreq'], epochs_length = 0.8, f=FREQS)
            PSD_bidsname, PSD_bidspath = get_SAflow_bids(FOLDERPATH, subj, bloc, stage='PSD', cond=None)
            savemat(PSD_bidspath, {'PSD': psds})
Exemple #6
0
def compute_sources(subject, run, mri_available=True):
    epochs_fname = get_SAflow_bids(FOLDERPATH, subject, run, stage='epo')[1]
    epochs = read_epochs(epochs_fname)

    info = epochs.info
    noise_fname = '/storage/Yann/saflow_DATA/saflow_bids/sub-06/ses-recording/meg/sub-06_ses-recording_NOISE_meg.ds'
    noise_raw = read_raw_ctf(noise_fname, preload=True)

    noise_raw.pick_channels(epochs.info['ch_names'])  # Choose channels
    cov = mne.compute_raw_covariance(noise_raw,
                                     method='shrunk',
                                     cv=5,
                                     tmin=0,
                                     tmax=0.8)  #change tmin and tmax ?
    src = mne.setup_source_space('sub-' + str(subject),
                                 subjects_dir=subjects_dir,
                                 add_dist=False)
    fname_src_fsaverage = subjects_dir + '/fsaverage/bem/fsaverage-vol-5-src.fif'

    surface = op.join(subjects_dir, 'sub-' + str(subject), 'bem',
                      'inner_skull.surf')
    vol_src = mne.setup_volume_source_space(
        'sub-' + str(subject),
        subjects_dir=subjects_dir,
        mri='aseg.mgz',
        surface=surface)  #,volume_label='Right-Pallidum')

    trans = get_SAflow_bids(FOLDERPATH, subject, run, stage='epotrans')[1]

    conductivity = (0.3, )  # for single layer
    fwd_filename = get_SAflow_bids(FOLDERPATH, subject, run, stage='epofwd')[1]
    #if not op.isfile(fwd_filename):
    model = mne.make_bem_model(subject='sub-' + str(subject),
                               ico=4,
                               conductivity=conductivity,
                               subjects_dir=subjects_dir)
    bem = mne.make_bem_solution(model)
    fwd = mne.make_forward_solution(info, trans, vol_src, bem, eeg=False)
    mne.write_forward_solution(fwd_filename, fwd, overwrite=True)
    #         else:
    #             fwd = mne.read_forward_solution(fwd_filename)
    inverse_operator = make_inverse_operator(info, fwd, cov, loose=1)

    snr = 1.0
    lambda2 = 1.0 / snr**2

    for j, epoch in enumerate(epochs):
        print('Epoch {} of {}'.format(j, len(epochs)))
        epoch = epoch[np.newaxis, ...]
        epoch = EpochsArray(epoch, info)
        epoch.pick_types(meg='mag')
        if method == 'dSPM':
            stc = apply_inverse_epochs(epoch,
                                       inverse_operator,
                                       lambda2,
                                       method='dSPM')
            src_fs = mne.read_source_spaces(fname_src_fsaverage)
            morph = mne.compute_source_morph(inverse_operator['src'],
                                             subject_from='sub-' +
                                             str(subject),
                                             subjects_dir=subjects_dir,
                                             src_to=src_fs,
                                             verbose=True)

            stc_fsaverage = morph.apply(stc[0])

            savepath = get_SAflow_bids(FOLDERPATH,
                                       subject,
                                       run,
                                       stage='eposources')[1]
            stc_fsaverage.save(savepath)

            del stc_fsaverage
Exemple #7
0
##### OPEN PREPROC FILES AND SEGMENT THEM
from neuro import split_TFR
from saflow_params import FOLDERPATH, SUBJ_LIST, BLOCS_LIST, FEAT_PATH
from utils import get_SAflow_bids
import mne

if __name__ == "__main__":
    for subj in SUBJ_LIST:
        for bloc in BLOCS_LIST:
            TFR_fname, TFR_fpath = get_SAflow_bids(FOLDERPATH,
                                                   subj,
                                                   bloc,
                                                   stage='1600TFR')
            TFR = split_TFR(TFR_fpath,
                            subj,
                            bloc,
                            by='VTC',
                            lobound=None,
                            hibound=None,
                            stage='1600TFR',
                            filt_order=3,
                            filt_cutoff=0.1)
            TFR[0].save(TFR_fpath, overwrite=True)
Exemple #8
0
def split_PSD_data(FOLDERPATH,
                   SUBJ_LIST,
                   BLOCS_LIST,
                   by='VTC',
                   lobound=None,
                   hibound=None,
                   stage='PSD',
                   filt_order=3,
                   filt_cutoff=0.1):
    '''
    This func splits the PSD data into two conditions. It returns a list of 2 (cond1 and cond2), each containing a list of n_subject matrices of shape n_freqs X n_channels X n_trials
    '''
    PSD_alldata = load_PSD_data(FOLDERPATH,
                                SUBJ_LIST,
                                BLOCS_LIST,
                                time_avg=True,
                                stage=stage)
    PSD_cond1 = []
    PSD_cond2 = []
    for subj_idx, subj in enumerate(SUBJ_LIST):
        subj_cond1 = []
        subj_cond2 = []
        for bloc_idx, bloc in enumerate(BLOCS_LIST):
            print('Splitting sub-{}_run-{}'.format(subj, bloc))

            # Obtain indices of the two conditions
            if by == 'VTC':
                INidx, OUTidx, VTC_epochs, idx_trimmed = get_VTC_epochs(
                    LOGS_DIR,
                    subj,
                    bloc,
                    lobound=lobound,
                    hibound=hibound,
                    save_epochs=False,
                    filt_order=filt_order,
                    filt_cutoff=filt_cutoff)
                cond1_idx = INidx
                cond2_idx = OUTidx
            if by == 'odd':
                # Get indices of freq and rare events
                ev_fname, ev_fpath = get_SAflow_bids(FOLDERPATH,
                                                     subj,
                                                     bloc,
                                                     stage='epo')
                events_artrej = mne.read_events(ev_fpath)
                log_file = LOGS_DIR + find_logfile(subj, bloc,
                                                   os.listdir(LOGS_DIR))
                events_fname, events_fpath = get_SAflow_bids(
                    FOLDERPATH, subj, bloc, stage='preproc_raw', cond=None)
                raw = read_raw_fif(
                    events_fpath, preload=False,
                    verbose=False)  #, min_duration=2/epochs.info['sfreq'])
                try:
                    events = mne.find_events(raw,
                                             min_duration=1 /
                                             raw.info['sfreq'],
                                             verbose=False)
                except ValueError:
                    events = mne.find_events(raw,
                                             min_duration=2 /
                                             raw.info['sfreq'],
                                             verbose=False)

                events_noerr, events_comerr, events_omerr = remove_errors(
                    log_file, events)
                events_trimmed, idx_trimmed = trim_events(
                    events_noerr, events_artrej)
                cond1_idx = []
                cond2_idx = []
                for idx, ev in enumerate(events_trimmed):
                    if ev[2] == 21:  # Frequent events
                        cond1_idx.append(idx)
                    if ev[2] == 31:
                        cond2_idx.append(idx)
                cond1_idx = np.array(cond1_idx)
                cond2_idx = np.array(cond2_idx)
                # Add this to keep the same number of trials in both conditions
                random.seed(0)
                cond1_idx = random.choices(cond1_idx, k=len(cond2_idx))
                print('N trials retained for each condition : {}'.format(
                    len(cond2_idx)))
            # Pick the data of each condition
            if bloc_idx == 0:  # if first bloc, init ndarray size using the first matrix
                subj_cond1 = PSD_alldata[subj_idx][bloc_idx][:, :, cond1_idx]
                subj_cond2 = PSD_alldata[subj_idx][bloc_idx][:, :, cond2_idx]
            else:  # if not first bloc, just concatenate along the trials dimension
                subj_cond1 = np.concatenate(
                    (subj_cond1, PSD_alldata[subj_idx][bloc_idx][:, :,
                                                                 cond1_idx]),
                    axis=2)
                subj_cond2 = np.concatenate(
                    (subj_cond2, PSD_alldata[subj_idx][bloc_idx][:, :,
                                                                 cond2_idx]),
                    axis=2)
        PSD_cond1.append(subj_cond1)
        PSD_cond2.append(subj_cond2)
    splitted_PSD = [PSD_cond1, PSD_cond2]
    return splitted_PSD
Exemple #9
0
def get_VTC_epochs(LOGS_DIR,
                   subj,
                   bloc,
                   stage='epo',
                   lobound=None,
                   hibound=None,
                   save_epochs=False,
                   filt_order=3,
                   filt_cutoff=0.1):
    '''
    This functions allows to use the logfile to split the epochs obtained in the epo.fif file.
    It works by comparing the timestamps of IN and OUT events to the timestamps in the epo file events
    It returns IN and OUT indices that are to be used in the split_PSD_data function

    TODO : FIND A WAY TO EARN TIME BY NOT LOADING THE DATA BUT JUST THE EVENTS
    '''

    ### Get events after artifact rejection have been performed
    epo_path, epo_filename = get_SAflow_bids(FOLDERPATH,
                                             subj,
                                             bloc,
                                             stage=stage,
                                             cond=None)
    events_artrej = mne.read_events(
        epo_filename,
        verbose=False)  # get events from the epochs file (so no resp event)

    ### Find logfile to extract VTC
    log_file = LOGS_DIR + find_logfile(subj, bloc, os.listdir(LOGS_DIR))
    VTC, INbounds, OUTbounds, INidx, OUTidx, RT_array = get_VTC_from_file(
        log_file,
        lobound=lobound,
        hibound=hibound,
        filt=True,
        filt_order=filt_order,
        filt_cutoff=filt_cutoff)

    ### Get original events and split them using the VTC
    events_fname, events_fpath = get_SAflow_bids(FOLDERPATH,
                                                 subj,
                                                 bloc,
                                                 stage='preproc_raw',
                                                 cond=None)
    raw = read_raw_fif(events_fpath, preload=False,
                       verbose=False)  #, min_duration=2/epochs.info['sfreq'])
    try:
        events = mne.find_events(raw,
                                 min_duration=1 / raw.info['sfreq'],
                                 verbose=False)
    except ValueError:
        events = mne.find_events(raw,
                                 min_duration=2 / raw.info['sfreq'],
                                 verbose=False)

    events_noerr, events_comerr, events_omerr = remove_errors(log_file, events)
    events_trimmed, idx_trimmed = trim_events(events_noerr, events_artrej)
    #rewrite INidx and OUTidx
    INidx, OUTidx = trim_INOUT_idx(INidx, OUTidx, events_trimmed, events)

    VTC_epo = np.array([VTC[idx] for idx in idx_trimmed])

    return INidx, OUTidx, VTC_epo, idx_trimmed
Exemple #10
0
TFR1_fpath = FEAT_PATH + '1600TFR_IN50_c01.hd5'
TFR2_fpath = FEAT_PATH + '1600TFR_OUT50_c01.hd5'
cond1 = 'IN'
cond2 = 'OUT'

if __name__ == "__main__":
    #
    if not (os.path.isfile(TFR1_fpath)):
        alldata_cond1 = []
        alldata_cond2 = []
        for subj in SUBJ_LIST:
            subjdata_cond1 = []
            subjdata_cond2 = []
            for bloc in BLOCS_LIST:
                TFR_fname, TFR_fpath = get_SAflow_bids(FOLDERPATH,
                                                       subj,
                                                       bloc,
                                                       stage='1600TFR')
                TFR = mne.time_frequency.read_tfrs(TFR_fpath)[0]
                # averaging trials
                subjdata_cond1.append(TFR[cond1].average().data)
                subjdata_cond2.append(TFR[cond2].average().data)
            subjdata_cond1 = np.array(subjdata_cond1)
            subjdata_cond2 = np.array(subjdata_cond2)
            # averaging blocs
            alldata_cond1.append(np.average(subjdata_cond1, axis=0))
            alldata_cond2.append(np.average(subjdata_cond2, axis=0))

        alldata_cond1 = np.array(alldata_cond1)
        TFR_cond1 = TFR.copy()
        TFR_cond1.data = alldata_cond1
        TFR_cond1.save(TFR1_fpath, overwrite=True)
Exemple #11
0
import os.path as op
import numpy as np
from mne.io import read_raw_fif
from mne.viz import plot_alignment
from nipype.utils.filemanip import split_filename as split_f
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
from mayavi import mlab
from saflow_params import FOLDERPATH, FS_SUBJDIR
from utils import get_SAflow_bids

subj = '07'
run = '3'

raw_fname, raw_fpath = get_SAflow_bids(FOLDERPATH,
                                       subj=subj,
                                       run=run,
                                       stage='preproc_raw')
raw = read_raw_fif(raw_fpath, preload=True)

trans_fname, trans_fpath = get_SAflow_bids(FOLDERPATH,
                                           subj=subj,
                                           run=run,
                                           stage='epotrans')
trans_fpath = trans_fpath[:-3]

fig = plot_alignment(raw.info,
                     trans=None,
                     subject='SA' + str(subj),
                     dig=True,
                     mri_fiducials=False,
                     src=None,
Exemple #12
0
##### OPEN PREPROC FILES AND SEGMENT THEM
from utils import get_SAflow_bids
from neuro import segment_files
from saflow_params import FOLDERPATH, LOGS_DIR, SUBJ_LIST, BLOCS_LIST
import pickle

if __name__ == "__main__":
    for subj in SUBJ_LIST:
        for bloc in BLOCS_LIST:
            preproc_path, preproc_filename = get_SAflow_bids(
                FOLDERPATH, subj, bloc, stage='preproc_raw', cond=None)
            epoched_path, epoched_filename = get_SAflow_bids(FOLDERPATH,
                                                             subj,
                                                             bloc,
                                                             stage='-epo',
                                                             cond=None)
            ARlog_path, ARlog_filename = get_SAflow_bids(FOLDERPATH,
                                                         subj,
                                                         bloc,
                                                         stage='ARlog',
                                                         cond=None)
            epochs_clean, AR_log = segment_files(preproc_filename,
                                                 tmin=0,
                                                 tmax=0.8)
            epochs_clean.save(epoched_filename, overwrite=True)
            del epochs_clean
            with open(ARlog_filename, 'wb') as fp:
                pickle.dump(AR_log, fp)