Esempio n. 1
0
def calc_subject_evoked_response(subject, root_fol, task, atlas, events_id, fname_format, fwd_fol, neccesary_files,
            remote_subjects_dir, fsaverage, raw_cleaning_method, inverse_method, indices=None,
            overwrite_epochs=False, overwrite_evoked=False, positive=True, moving_average_win_size=100):
    meg.init_globals(subject, fname_format=fname_format, raw_cleaning_method=raw_cleaning_method,
                     subjects_meg_dir=SUBJECTS_MEG_DIR, task=task, subjects_mri_dir=SUBJECTS_DIR,
                     BLENDER_ROOT_DIR=BLENDER_ROOT_DIR, files_includes_cond=True, fwd_no_cond=True)
    epochs_fname = '{}_arc_rer_{}-epo.fif'.format(subject, raw_cleaning_method)
    events_fname = '{}_arc_rer_{}-epo.csv'.format(subject, raw_cleaning_method)
    if indices is None:
        indices = find_events_indices(op.join(root_fol, events_fname))
    if not indices is None:
        utils.make_dir(op.join(SUBJECTS_MEG_DIR, task, subject))
        utils.make_dir(op.join(SUBJECTS_DIR, subject, 'mmvt'))
        utils.make_dir(op.join(BLENDER_ROOT_DIR, subject))
        # utils.prepare_local_subjects_folder(
        #     neccesary_files, subject, remote_subjects_dir, SUBJECTS_DIR, print_traceback=False)
        # anatomy_preproc.freesurfer_surface_to_blender_surface(subject, overwrite=False)
        # anatomy_preproc.create_annotation_file_from_fsaverage(subject, atlas, fsaverage, False, False, False, True)
        # calc_evoked(indices, op.join(root_fol, epochs_fname), overwrite_epochs, overwrite_evoked)
        fwd_fname = '{}_arc_rer_tsss-fwd.fif'.format(subject)
        if not op.isfile(op.join(SUBJECTS_MEG_DIR, task, subject, fwd_fname)):
            shutil.copy(op.join(fwd_fol, fwd_fname), op.join(SUBJECTS_MEG_DIR, task, subject, fwd_fname))
        # meg_preproc.calc_inverse_operator(events_id, calc_for_cortical_fwd=True, calc_for_sub_cortical_fwd=False)
        # stcs = meg_preproc.calc_stc_per_condition(events_id, inverse_method)
        stcs = None
        for hemi in utils.HEMIS:
            meg.calc_labels_avg_per_condition(
                atlas, hemi, 'pial', events_id, labels_from_annot=False, labels_fol='', stcs=stcs,
                inverse_method=inverse_method, positive=positive, moving_average_win_size=moving_average_win_size,
                do_plot=True)
Esempio n. 2
0
def calc_subject_evoked_response(subject, root_fol, task, atlas, events_id, fname_format, fwd_fol, neccesary_files,
            remote_subjects_dir, fsaverage, raw_cleaning_method, inverse_method, indices=None,
            overwrite_epochs=False, overwrite_evoked=False, positive=True, moving_average_win_size=100):
    meg.init_globals(subject, fname_format=fname_format, raw_cleaning_method=raw_cleaning_method,
                     subjects_meg_dir=SUBJECTS_MEG_DIR, task=task, subjects_mri_dir=SUBJECTS_DIR,
                     BLENDER_ROOT_DIR=BLENDER_ROOT_DIR, files_includes_cond=True, fwd_no_cond=True)
    epochs_fname = '{}_arc_rer_{}-epo.fif'.format(subject, raw_cleaning_method)
    events_fname = '{}_arc_rer_{}-epo.csv'.format(subject, raw_cleaning_method)
    if indices is None:
        indices = find_events_indices(op.join(root_fol, events_fname))
    if not indices is None:
        utils.make_dir(op.join(SUBJECTS_MEG_DIR, task, subject))
        utils.make_dir(op.join(SUBJECTS_DIR, subject, 'mmvt'))
        utils.make_dir(op.join(BLENDER_ROOT_DIR, subject))
        # utils.prepare_subject_folder(
        #     neccesary_files, subject, remote_subjects_dir, SUBJECTS_DIR, print_traceback=False)
        # anatomy_preproc.freesurfer_surface_to_blender_surface(subject, overwrite=False)
        # anatomy_preproc.create_annotation_file_from_fsaverage(subject, atlas, fsaverage, False, False, False, True)
        # calc_evoked(indices, op.join(root_fol, epochs_fname), overwrite_epochs, overwrite_evoked)
        fwd_fname = '{}_arc_rer_tsss-fwd.fif'.format(subject)
        if not op.isfile(op.join(SUBJECTS_MEG_DIR, task, subject, fwd_fname)):
            utils.copy_file(op.join(fwd_fol, fwd_fname), op.join(SUBJECTS_MEG_DIR, task, subject, fwd_fname))
        # meg_preproc.calc_inverse_operator(events_id, calc_for_cortical_fwd=True, calc_for_sub_cortical_fwd=False)
        # stcs = meg_preproc.calc_stc_per_condition(events_id, inverse_method)
        stcs = None
        for hemi in utils.HEMIS:
            meg.calc_labels_avg_per_condition(
                atlas, hemi, 'pial', events_id, labels_from_annot=False, labels_fol='', stcs=stcs,
                positive=positive, moving_average_win_size=moving_average_win_size, inverse_method=inverse_method,
                do_plot=True)
Esempio n. 3
0
def get_meg(subject, mri_subject, task, elecs_probs, bipolar, vertices_num_threshold=30, read_from_stc=False,
            meg_single_trials=False, do_plot=True):
    # meg_files = glob.glob(op.join(MMVT_DIR, subject, 'activity_map_{}'.format(hemi), '*.npy'))
    # meg_data = np.zeros((len(meg_files), len(vertices)))
    # for meg_file in meg_files:
    #     t = int(re.findall('\d+', utils.namebase(meg_file))[0])
    #     meg_data_t = np.load(meg_file)
    #     meg_data[t] = meg_data_t[vertices, 0]
    #     print("sdf")
    electordes_data_fname = op.join(MMVT_DIR, mri_subject, 'electrodes', 'electrodes_{}data.npz'.format(
        'bipolar_' if bipolar else ''))
    electordes_evokde_data_fname = op.join(ELECTRODES_DIR, mri_subject, task, 'evoked_{}data.npy').format(
        'bipolar_' if bipolar else '')

    if not op.isfile(electordes_data_fname) or not op.isfile(electordes_evokde_data_fname):
        print('No electrodes data file!')
        print(electordes_data_fname)
        print(electordes_evokde_data_fname)
        return None
    f = np.load(electordes_data_fname)
    evoked_data = np.load(electordes_evokde_data_fname)
    conds = np.array([cond.decode() if isinstance(cond, np.bytes_) else cond for cond in f['conditions']])
    names = np.array([name.decode() if isinstance(name, np.bytes_) else name for name in f['names']])
    figs_fol = op.join(MMVT_DIR, mri_subject, 'figs', 'meg-electrodes2', 'bipolar' if bipolar else 'unipolar')
    utils.make_dir(figs_fol)
    fname_format, fname_format_cond, events_id = meg.get_fname_format(task)
    if task == 'MSIT':
        cleaning_method = 'nTSSS'
        constrast = 'interference'
        keys_dict = {'neutral': 'noninterference', 'interference': 'interference'}
    elif task == 'ECR':
        cleaning_method = ''
        constrast = ''
        keys_dict = {'C': 'congruent', 'I': 'incongruent'}
    inverse_method = 'dSPM'
    meg.init_globals(subject, mri_subject, fname_format, fname_format_cond, cleaning_method=cleaning_method,
                     task=task, subjects_meg_dir=SUBJECTS_MEG_DIR, subjects_mri_dir=SUBJECTS_DIR,
                     mmvt_dir=MMVT_DIR, files_includes_cond=True, fwd_no_cond=True,
                     constrast=constrast)

    # for elec_probs in elecs_probs:
    #     elec_probs['data'] = {cond:None for cond in events_id.keys()}
        # if len(elec_probs['cortical_indices_dists']) > 0:
        #     print(elec_probs['name'], len(elec_probs['cortical_indices']), np.min(elec_probs['cortical_indices_dists']))

    meg_elecs, errors, dists = [], [], []
    elec_meg_data_st = None
    for cond_id, cond in enumerate(events_id.keys()):
        meg_cond = keys_dict[cond]
        if read_from_stc:
            stc_fname = meg.STC_HEMI_SMOOTH.format(cond=cond, method=inverse_method, hemi='rh')
            stc = mne.read_source_estimate(stc_fname)
            cond_ind = np.where(meg_cond == conds)[0][0]
        else:
            if meg_single_trials:
                meg_data = np.load(op.join(SUBJECTS_MEG_DIR, task, subject, 'labels_ts_{}.npy'.format(cond)))
            meg_evo_data = {}
            for hemi in utils.HEMIS:
                meg_evo_data[hemi] = np.load(
                    op.join(MMVT_DIR, mri_subject, op.basename(meg.LBL.format(atlas, hemi))))
            meg_conds = np.array([cond.decode() if isinstance(cond, np.bytes_) else cond for cond in meg_evo_data['rh']['conditions']])
            meg_labels = {hemi:np.array([name.decode() if isinstance(name, np.bytes_) else name for name in meg_evo_data[hemi]['names']]) for hemi in utils.HEMIS}
            cond_ind = np.where(cond == meg_conds)[0][0]

        for elec_probs_ind, elec_probs in enumerate(elecs_probs):
            try:
                # len(elec_probs['cortical_indices']) > vertices_num_threshold
                if len(elec_probs['cortical_indices_dists']) > 0 and \
                                len(elec_probs['cortical_rois']) > 0:
                                # np.min(elec_probs['cortical_indices_dists']) < 1 and \
                                # elec_probs['approx'] == 3:
                    # print(elec_probs['name'], elec_probs['hemi'], len(elec_probs['cortical_indices']))
                    elec_inds = np.where(elec_probs['name'] == names)[0]
                    if len(elec_inds) == 1:
                        elec_ind = elec_inds[0]
                    else:
                        print('{} found {} in names'.format(elec_probs['name'], len(elec_inds)))
                        continue
                    if read_from_stc:
                        data = stc.rh_data if elec_probs['hemi'] == 'rh' else stc.lh_data
                        T = data.shape[1]
                        elec_meg_data = data[elec_probs['cortical_indices']]
                        dists = elec_probs['cortical_indices_dists']
                        norm_dists = dists / np.sum(dists)
                        norm_dists = np.reshape(norm_dists, (1, len(norm_dists)))
                        elec_meg_data = np.dot(norm_dists, elec_meg_data)
                        elec_meg_data = np.reshape(elec_meg_data, ((T)))
                    else:
                        meg_labels_inds = np.array([np.where(label == meg_labels[elec_probs['hemi']])[0][0] \
                                                    for label in elec_probs['cortical_rois']])
                        probs = elec_probs['cortical_probs']
                        probs = np.reshape(probs, (1, len(probs)))
                        if meg_single_trials:
                            data = meg_data[:, meg_labels_inds, :]
                            if elec_meg_data_st is None:
                                # n_epochs, n_signals, n_times
                                elec_meg_data_st = np.zeros((data.shape[0], len(elecs_probs), data.shape[2], 2))
                            for trial in range(data.shape[0]):
                                elec_meg_data_st[trial, elec_probs_ind, :, cond_id] = np.dot(probs, data[trial])[0, :]
                        else:
                            data = meg_evo_data[elec_probs['hemi']]['data'][meg_labels_inds, :, cond_ind]
                            elec_meg_data = np.dot(probs, data)[0, :]
                    if not meg_single_trials:
                        # elec_data = f['data'][elec_ind, :, cond_ind]
                        elec_data = evoked_data[elec_ind, :, cond_ind]
                        elec_data_diff = np.max(elec_data) - np.min(elec_data)
                        elec_meg_data *= elec_data_diff / (np.max(elec_meg_data) - np.min(elec_meg_data))
                        # elec_meg_data += elec_data[0] - elec_meg_data[0]
                        elec_meg_data += np.mean(elec_data) - np.mean(elec_meg_data)
                        # elec_probs['data'][cond] = elec_meg_data

                        elec_meg_data, elec_data = utils.trim_to_same_size(elec_meg_data, elec_data)
                        data_diff = elec_meg_data-elec_data
                        data_diff = data_diff / max(data_diff)
                        rms = np.sqrt(np.mean(np.power(data_diff, 2)))
                        dist = min(elec_probs['cortical_indices_dists'])
                        errors.append(rms)
                        dists.append(dist)
                        meg_elecs.append(dict(name=elec_probs['name'],rms=rms, dist=dist, cond=cond, approx=elec_probs['approx']))
                        if do_plot: # and elec_probs['name'] == 'RPT8':
                            plt.figure()
                            plt.plot(elec_meg_data, label='pred')
                            plt.plot(elec_data, label='elec')
                            plt.xlabel('Time(ms)')
                            plt.ylabel('Voltage (mV)')
                            plt.legend()
                            plt.title('{}-{}'.format(elec_probs['name'], cond))
                            plt.savefig(op.join(figs_fol, '{:.2f}-{}-{}.jpg'.format(rms, elec_probs['name'], cond)))
                            plt.close()
            except:
                print('Error with {}!'.format(elec_probs['name']))
    if meg_single_trials:
        np.save(op.join(ELECTRODES_DIR, mri_subject, task, 'meg_electrodes_{}ts'.format('bipolar_' if bipolar else '')), elec_meg_data_st)
    else:
        rmss, dists = [], []
        results_fname = op.join(figs_fol, 'results{}.csv'.format('_bipolar' if bipolar else ''))
        with open(results_fname, 'w') as output_file:
            for res in meg_elecs:
                output_file.write('{},{},{},{},{}\n'.format(res['name'], res['cond'], res['rms'], res['dist'], res['approx']))
                rmss.append(res['rms'])
                dists.append(res['dist'])
        plt.hist(rmss, 20)
        plt.xlabel('mV')
        plt.savefig(op.join(figs_fol, 'rmss{}.jpg'.format('_bipolar' if bipolar else '')))
        return elecs_probs
Esempio n. 4
0
def get_meg(subject, mri_subject, task, elecs_probs, bipolar, vertices_num_threshold=30, read_from_stc=False,
            meg_single_trials=False, do_plot=True):
    # meg_files = glob.glob(op.join(MMVT_DIR, subject, 'activity_map_{}'.format(hemi), '*.npy'))
    # meg_data = np.zeros((len(meg_files), len(vertices)))
    # for meg_file in meg_files:
    #     t = int(re.findall('\d+', utils.namebase(meg_file))[0])
    #     meg_data_t = np.load(meg_file)
    #     meg_data[t] = meg_data_t[vertices, 0]
    #     print("sdf")
    electordes_data_fname = op.join(MMVT_DIR, mri_subject, 'electrodes', 'electrodes_{}data.npz'.format(
        'bipolar_' if bipolar else ''))
    electordes_evokde_data_fname = op.join(ELECTRODES_DIR, mri_subject, task, 'evoked_{}data.npy').format(
        'bipolar_' if bipolar else '')

    if not op.isfile(electordes_data_fname) or not op.isfile(electordes_evokde_data_fname):
        print('No electrodes data file!')
        print(electordes_data_fname)
        print(electordes_evokde_data_fname)
        return None
    f = np.load(electordes_data_fname)
    evoked_data = np.load(electordes_evokde_data_fname)
    conds = np.array([cond.decode() if isinstance(cond, np.bytes_) else cond for cond in f['conditions']])
    names = np.array([name.decode() if isinstance(name, np.bytes_) else name for name in f['names']])
    figs_fol = op.join(MMVT_DIR, mri_subject, 'figs', 'meg-electrodes2', 'bipolar' if bipolar else 'unipolar')
    utils.make_dir(figs_fol)
    fname_format, fname_format_cond, events_id = meg.get_fname_format(task)
    if task == 'MSIT':
        cleaning_method = 'nTSSS'
        constrast = 'interference'
        keys_dict = {'neutral': 'noninterference', 'interference': 'interference'}
    elif task == 'ECR':
        cleaning_method = ''
        constrast = ''
        keys_dict = {'C': 'congruent', 'I': 'incongruent'}
    inverse_method = 'dSPM'
    meg.init_globals(subject, mri_subject, fname_format, fname_format_cond, cleaning_method=cleaning_method,
                     subjects_meg_dir=SUBJECTS_MEG_DIR, task=task, subjects_mri_dir=SUBJECTS_DIR,
                     mmvt_dir=MMVT_DIR, files_includes_cond=True, fwd_no_cond=True,
                     constrast=constrast)

    # for elec_probs in elecs_probs:
    #     elec_probs['data'] = {cond:None for cond in events_id.keys()}
        # if len(elec_probs['cortical_indices_dists']) > 0:
        #     print(elec_probs['name'], len(elec_probs['cortical_indices']), np.min(elec_probs['cortical_indices_dists']))

    meg_elecs, errors, dists = [], [], []
    elec_meg_data_st = None
    for cond_id, cond in enumerate(events_id.keys()):
        meg_cond = keys_dict[cond]
        if read_from_stc:
            stc_fname = meg.STC_HEMI_SMOOTH.format(cond=cond, method=inverse_method, hemi='rh')
            stc = mne.read_source_estimate(stc_fname)
            cond_ind = np.where(meg_cond == conds)[0][0]
        else:
            if meg_single_trials:
                meg_data = np.load(op.join(SUBJECTS_MEG_DIR, task, subject, 'labels_ts_{}.npy'.format(cond)))
            meg_evo_data = {}
            for hemi in utils.HEMIS:
                meg_evo_data[hemi] = np.load(
                    op.join(MMVT_DIR, mri_subject, op.basename(meg.LBL.format(hemi))))
            meg_conds = np.array([cond.decode() if isinstance(cond, np.bytes_) else cond for cond in meg_evo_data['rh']['conditions']])
            meg_labels = {hemi:np.array([name.decode() if isinstance(name, np.bytes_) else name for name in meg_evo_data[hemi]['names']]) for hemi in utils.HEMIS}
            cond_ind = np.where(cond == meg_conds)[0][0]

        for elec_probs_ind, elec_probs in enumerate(elecs_probs):
            try:
                # len(elec_probs['cortical_indices']) > vertices_num_threshold
                if len(elec_probs['cortical_indices_dists']) > 0 and \
                                len(elec_probs['cortical_rois']) > 0:
                                # np.min(elec_probs['cortical_indices_dists']) < 1 and \
                                # elec_probs['approx'] == 3:
                    # print(elec_probs['name'], elec_probs['hemi'], len(elec_probs['cortical_indices']))
                    elec_inds = np.where(elec_probs['name'] == names)[0]
                    if len(elec_inds) == 1:
                        elec_ind = elec_inds[0]
                    else:
                        print('{} found {} in names'.format(elec_probs['name'], len(elec_inds)))
                        continue
                    if read_from_stc:
                        data = stc.rh_data if elec_probs['hemi'] == 'rh' else stc.lh_data
                        T = data.shape[1]
                        elec_meg_data = data[elec_probs['cortical_indices']]
                        dists = elec_probs['cortical_indices_dists']
                        norm_dists = dists / np.sum(dists)
                        norm_dists = np.reshape(norm_dists, (1, len(norm_dists)))
                        elec_meg_data = np.dot(norm_dists, elec_meg_data)
                        elec_meg_data = np.reshape(elec_meg_data, ((T)))
                    else:
                        meg_labels_inds = np.array([np.where(label == meg_labels[elec_probs['hemi']])[0][0] \
                                                    for label in elec_probs['cortical_rois']])
                        probs = elec_probs['cortical_probs']
                        probs = np.reshape(probs, (1, len(probs)))
                        if meg_single_trials:
                            data = meg_data[:, meg_labels_inds, :]
                            if elec_meg_data_st is None:
                                # n_epochs, n_signals, n_times
                                elec_meg_data_st = np.zeros((data.shape[0], len(elecs_probs), data.shape[2], 2))
                            for trial in range(data.shape[0]):
                                elec_meg_data_st[trial, elec_probs_ind, :, cond_id] = np.dot(probs, data[trial])[0, :]
                        else:
                            data = meg_evo_data[elec_probs['hemi']]['data'][meg_labels_inds, :, cond_ind]
                            elec_meg_data = np.dot(probs, data)[0, :]
                    if not meg_single_trials:
                        # elec_data = f['data'][elec_ind, :, cond_ind]
                        elec_data = evoked_data[elec_ind, :, cond_ind]
                        elec_data_diff = np.max(elec_data) - np.min(elec_data)
                        elec_meg_data *= elec_data_diff / (np.max(elec_meg_data) - np.min(elec_meg_data))
                        # elec_meg_data += elec_data[0] - elec_meg_data[0]
                        elec_meg_data += np.mean(elec_data) - np.mean(elec_meg_data)
                        # elec_probs['data'][cond] = elec_meg_data

                        elec_meg_data, elec_data = utils.trim_to_same_size(elec_meg_data, elec_data)
                        data_diff = elec_meg_data-elec_data
                        data_diff = data_diff / max(data_diff)
                        rms = np.sqrt(np.mean(np.power(data_diff, 2)))
                        dist = min(elec_probs['cortical_indices_dists'])
                        errors.append(rms)
                        dists.append(dist)
                        meg_elecs.append(dict(name=elec_probs['name'],rms=rms, dist=dist, cond=cond, approx=elec_probs['approx']))
                        if do_plot: # and elec_probs['name'] == 'RPT8':
                            plt.figure()
                            plt.plot(elec_meg_data, label='pred')
                            plt.plot(elec_data, label='elec')
                            plt.xlabel('Time(ms)')
                            plt.ylabel('Voltage (mV)')
                            plt.legend()
                            plt.title('{}-{}'.format(elec_probs['name'], cond))
                            plt.savefig(op.join(figs_fol, '{:.2f}-{}-{}.jpg'.format(rms, elec_probs['name'], cond)))
                            plt.close()
            except:
                print('Error with {}!'.format(elec_probs['name']))
    if meg_single_trials:
        np.save(op.join(ELECTRODES_DIR, mri_subject, task, 'meg_electrodes_{}ts'.format('bipolar_' if bipolar else '')), elec_meg_data_st)
    else:
        rmss, dists = [], []
        results_fname = op.join(figs_fol, 'results{}.csv'.format('_bipolar' if bipolar else ''))
        with open(results_fname, 'w') as output_file:
            for res in meg_elecs:
                output_file.write('{},{},{},{},{}\n'.format(res['name'], res['cond'], res['rms'], res['dist'], res['approx']))
                rmss.append(res['rms'])
                dists.append(res['dist'])
        plt.hist(rmss, 20)
        plt.xlabel('mV')
        plt.savefig(op.join(figs_fol, 'rmss{}.jpg'.format('_bipolar' if bipolar else '')))
        return elecs_probs