Exemplo n.º 1
0
        sns.plt.xlim([-2000, 7000])
        # sns.plt.show()
        pic_fname = op.join(BLENDER_ROOT_DIR, subject, 'pics', '{}_vs_health_{}.jpg'.format(subject, cond_name))
        print('Saving {}'.format(pic_fname))
        sns.plt.savefig(pic_fname, dpi=dpi)
        # sns.plt.close()

if __name__ == '__main__':
    target_subject = 'pp009'
    raw_cleaning_method = 'tsss'
    task = 'ARC'
    atlas = 'arc_april2016'
    fsaverage = 'fscopy'
    inverse_method = 'dSPM'
    # fname_format = '{subject}_arc_rer_{raw_cleaning_method}_{cond}-{ana_type}.{file_type}'
    fname_format, events_id, event_digit = meg.get_fname_format(task)
    root_fol = '/autofs/space/sophia_002/users/DARPA-MEG/arc/ave/'
    fwd_fol = '/autofs/space/sophia_002/users/DARPA-MEG/arc/fwd/'
    remote_subjects_dir = '/autofs/space/lilli_001/users/DARPA-Recons'
    neccesary_files = {'..': ['sub_cortical_codes.txt'], 'mri': ['aseg.mgz', 'norm.mgz', 'ribbon.mgz'],
        'surf': ['rh.pial', 'lh.pial', 'rh.sphere.reg', 'lh.sphere.reg', 'lh.white', 'rh.white']}
    overwrite_epochs = True
    overwrite_evoked = True
    # # root_fol = op.join(SUBJECTS_MEG_DIR, task, subject)

    # create_evoked_responses(root_fol, task, atlas, events_id, fname_format,
    #     fwd_fol, neccesary_files, remote_subjects_dir, fsaverage, raw_cleaning_method, inverse_method,
    #     overwrite_epochs, overwrite_evoked)
    # copy_evokes(task, root_fol, target_subject, raw_cleaning_method)
    # average_all_evoked_responses(op.join(BLENDER_ROOT_DIR, target_subject, 'meg_evoked_files'))
    plot_labels(target_subject, {'vlpfc-rh': 'right VLPFC', 'ofc-lh': 'left OFC'},
Exemplo n.º 2
0
        sns.plt.xlim([-2000, 7000])
        # sns.plt.show()
        pic_fname = op.join(BLENDER_ROOT_DIR, subject, 'pics', '{}_vs_health_{}.jpg'.format(subject, cond_name))
        print('Saving {}'.format(pic_fname))
        sns.plt.savefig(pic_fname, dpi=dpi)
        # sns.plt.close()

if __name__ == '__main__':
    target_subject = 'pp009'
    raw_cleaning_method = 'tsss'
    task = 'ARC'
    atlas = 'arc_april2016'
    fsaverage = 'fscopy'
    inverse_method = 'dSPM'
    # fname_format = '{subject}_arc_rer_{raw_cleaning_method}_{cond}-{ana_type}.{file_type}'
    fname_format, events_id, event_digit = meg.get_fname_format(task)
    root_fol = '/autofs/space/sophia_002/users/DARPA-MEG/arc/ave/'
    fwd_fol = '/autofs/space/sophia_002/users/DARPA-MEG/arc/fwd/'
    remote_subjects_dir = '/autofs/space/lilli_001/users/DARPA-Recons'
    neccesary_files = {'..': ['sub_cortical_codes.txt'], 'mri': ['aseg.mgz', 'norm.mgz', 'ribbon.mgz'],
        'surf': ['rh.pial', 'lh.pial', 'rh.sphere.reg', 'lh.sphere.reg', 'lh.white', 'rh.white']}
    overwrite_epochs = True
    overwrite_evoked = True
    # # root_fol = op.join(SUBJECTS_MEG_DIR, task, subject)

    # create_evoked_responses(root_fol, task, atlas, events_id, fname_format,
    #     fwd_fol, neccesary_files, remote_subjects_dir, fsaverage, raw_cleaning_method, inverse_method,
    #     overwrite_epochs, overwrite_evoked)
    # copy_evokes(task, root_fol, target_subject, raw_cleaning_method)
    # average_all_evoked_responses(op.join(BLENDER_ROOT_DIR, target_subject, 'meg_evoked_files'))
    plot_labels(target_subject, {'vlpfc-rh': 'right VLPFC', 'ofc-lh': 'left OFC'},
Exemplo 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
Exemplo 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