Exemple #1
0
def _plot_modalities_parallel(p):
    subject, modalities, inverse_method, figures_fol, band, window_fname, max_t, overwrite = p
    window_name = utils.namebase(window_fname)
    plt.figure()
    for modality in modalities:
        modality_fol = op.join(MMVT_DIR, subject,
                               'eeg' if modality == 'eeg' else 'meg')
        fig_fname = op.join(figures_fol, '{}-{}.jpg'.format(window_name, band))
        if op.isfile(fig_fname) and not overwrite:
            print('{} already exist'.format(fig_fname))
            break
        stc_band_fname = op.join(
            modality_fol, '{}-epilepsy-{}-{}-{}_{}-zvals-lh.stc'.format(
                subject, inverse_method, modality, window_name, band))
        if not op.isfile(stc_band_fname):
            print('Can\'t find {}!'.format(stc_band_fname))
            break
        print('Loading {} ({})'.format(
            stc_band_fname, utils.file_modification_time(stc_band_fname)))
        stc = mne.read_source_estimate(stc_band_fname)
        data = np.max(stc.data[:, :max_t], axis=0) if max_t > 0 else np.max(
            stc.data, axis=0)
        plt.plot(data.T)
    else:
        plt.title('{} {}'.format(window_name, band))
        plt.legend(modalities)
        print('Saving {} {}'.format(window_name, band))
        plt.savefig(fig_fname, dpi=300)
        plt.close()
Exemple #2
0
def calc_labels_avg(target_subject, hemi, atlas, fmri_fname, res_dir, cwd, overwrite=True, output_txt_fname='',
                    output_sum_fname='', ret_files_name=False, **kargs):
    def get_labels_names(line):
        label_name = line.split()[4]
        label_nums = utils.find_num_in_str(label_name)
        label_num = label_nums[-1] if len(label_nums) > 0 else ''
        if label_num != '':
            name_len = label_name.find('_{}'.format(label_num)) + len(str(label_num)) + 1
            label_name = '{}-{}'.format(label_name[:name_len], hemi)
        return label_name

    if output_txt_fname == '':
        output_txt_fname = op.join(res_dir, '{}_{}_{}.txt'.format(utils.namebase(fmri_fname), atlas, hemi))
    if output_sum_fname == '':
        output_sum_fname = op.join(res_dir, '{}_{}_{}.sum'.format(utils.namebase(fmri_fname), atlas, hemi))
    if not op.isfile(output_txt_fname) or not op.isfile(output_sum_fname) or overwrite:
        print('Running mri_segstats on {} ({})'.format(fmri_fname, utils.file_modification_time(fmri_fname)))
        utils.partial_run_script(locals(), cwd=cwd)(mri_segstats)
    if not op.isfile(output_txt_fname):
        raise Exception('The output file was not created!')
    labels_data = np.genfromtxt(output_txt_fname).T
    labels_names = utils.read_list_from_file(output_sum_fname, get_labels_names, 'rb')
    if ret_files_name:
        return labels_data, labels_names, output_txt_fname, output_sum_fname
    else:
        return labels_data, labels_names
def save_template_electrodes_to_template(template_electrodes,
                                         bipolar,
                                         mmvt_dir,
                                         template_system='mni',
                                         prefix='',
                                         postfix=''):
    output_fname = '{}electrodes{}_positions.npz'.format(
        prefix, '_bipolar' if bipolar else '', postfix)
    template = 'fsaverage' if template_system == 'ras' else 'colin27' if template_system == 'mni' else template_system
    if template_electrodes is None:
        input_fname = op.join(mmvt_dir, template, 'electrodes',
                              'template_electrodes.pkl')
        print('Reading {} ({})'.format(
            input_fname, utils.file_modification_time(input_fname)))
        template_electrodes = utils.load(input_fname)
    fol = utils.make_dir(op.join(MMVT_DIR, template, 'electrodes'))
    output_fname = op.join(fol, output_fname)
    elecs_coordinates = np.array(
        utils.flat_list_of_lists([[e[1] for e in template_electrodes[subject]]
                                  for subject in template_electrodes.keys()]))
    elecs_names = utils.flat_list_of_lists(
        [[e[0] for e in template_electrodes[subject]]
         for subject in template_electrodes.keys()])
    np.savez(output_fname,
             pos=elecs_coordinates,
             names=elecs_names,
             pos_org=[])
    print('Electrodes were saved to {}'.format(output_fname))
def read_morphed_electrodes(electrodes,
                            template_system,
                            subjects_dir,
                            mmvt_dir,
                            overwrite=False):
    subject_to = 'fsaverage5' if template_system == 'ras' else 'colin27' if template_system == 'mni' else template_system
    output_fname = op.join(mmvt_dir, subject_to, 'electrodes',
                           'template_electrodes.pkl')
    if op.isfile(output_fname) and not overwrite:
        return
    t1_header = nib.load(op.join(subjects_dir, subject_to, 'mri',
                                 'T1.mgz')).header
    trans = t1_header.get_vox2ras_tkr()
    template_electrodes = defaultdict(list)
    bad_subjects, good_subjects = [], []
    for subject in electrodes.keys():
        if subject == subject_to:
            continue
        input_fname = op.join(subjects_dir, subject, 'electrodes',
                              f'stim_electrodes_to_{subject_to}.txt')
        if not op.isfile(input_fname):
            bad_subjects.append(subject)
            continue
        print('Reading {} ({})'.format(
            input_fname, utils.file_modification_time(input_fname)))
        vox = np.genfromtxt(input_fname, dtype=np.float, delimiter=' ')
        tkregs = apply_trans(trans, vox)
        for tkreg, (elc_name, _) in zip(tkregs, electrodes[subject]):
            template_electrodes[subject].append(
                (f'{subject}_{elc_name}', tkreg))
        good_subjects.append(subject)
    utils.save(template_electrodes, output_fname)
    print('read_morphed_electrodes: {}'.format(op.isfile(output_fname)))
    print('good subjects: {}'.format(good_subjects))
    print('bad subjects: {}'.format(bad_subjects))
def read_morphed_electrodes(electrodes,
                            template_system,
                            subjects_dir,
                            mmvt_dir,
                            overwrite=False):
    subject_to = 'fsaverage' if template_system == 'ras' else 'colin27' if template_system == 'mni' else template_system
    fol = utils.make_dir(op.join(mmvt_dir, subject_to, 'electrodes'))
    output_fname = op.join(fol, 'template_electrodes.pkl')
    if op.isfile(output_fname) and not overwrite:
        return
    subject_to_mri = subject_to  #'cvs_avg35_inMNI152' if subject_to == 'fsaverage' else subject_to
    t1_header = nib.load(op.join(subjects_dir, subject_to_mri, 'mri',
                                 'T1.mgz')).header
    brain_mask_fname = op.join(subjects_dir, subject_to_mri, 'mri',
                               'brainmask.mgz')
    brain_mask = nib.load(brain_mask_fname).get_data() if op.isfile(
        brain_mask_fname) else None
    trans = t1_header.get_vox2ras_tkr()
    template_electrodes = defaultdict(list)
    bad_subjects, good_subjects = [], []
    for subject in electrodes.keys():
        if subject == subject_to:
            continue
        morphed_electrodes_file_name = 'electrodes_morph_to_{}.txt'.format(
            subject_to)
        input_fname = op.join(MMVT_DIR, subject, 'electrodes',
                              morphed_electrodes_file_name)
        if not op.isfile(input_fname):
            print(
                'read_morphed_electrodes: Can\'t find {}!'.format(input_fname))
            bad_subjects.append(subject)
            continue
        print('Reading {} ({})'.format(
            input_fname, utils.file_modification_time(input_fname)))
        voxels = np.genfromtxt(input_fname, dtype=np.float, delimiter=' ')
        electrodes_names = [elc_name for (elc_name, _) in electrodes[subject]]
        if subject_to == 'fsaverage':
            voxels = tut.mni152_mni305(voxels)
        check_if_electrodes_inside_the_brain(subject, voxels, electrodes_names,
                                             brain_mask)
        write_morphed_electrodes_vox_into_csv(subject, subject_to, voxels,
                                              electrodes_names)
        tkregs = apply_trans(trans, voxels)
        for tkreg, (elc_name, _) in zip(tkregs, electrodes[subject]):
            template_electrodes[subject].append(
                ('{}_{}'.format(subject, elc_name), tkreg))
        good_subjects.append(subject)
    utils.save(template_electrodes, output_fname)
    print('read_morphed_electrodes: {}'.format(op.isfile(output_fname)))
    print('good subjects: {}'.format(good_subjects))
    print('bad subjects: {}'.format(bad_subjects))
def get_subjects_dFC(subjects):
    pcs = ['mean', 1, 2, 4, 8]
    dFC_res = {pc: None for pc in pcs}
    std_mean_res = {pc: None for pc in pcs}
    stat_conn_res = {pc: None for pc in pcs}
    for subject_ind, subject in enumerate(subjects):
        fol = op.join(MMVT_DIR, subject, 'connectivity')
        for pc in pcs:
            if pc == 'mean':
                fname = op.join(fol, 'fmri_corr_cv_mean_mean.npz')
            else:
                fname = op.join(
                    fol, 'fmri_mi_vec_cv_mean_pca{}.npz'.format(
                        '' if pc == 1 else '_{}'.format(pc)))
            if not op.isfile(fname):
                print('{} not exist!'.format(fname))
                continue
            print('Loading {} ({})'.format(
                fname, utils.file_modification_time(fname)))
            d = np.load(fname)
            dFC = d['dFC']
            std_mean = d['std_mean']
            if pc == 'mean':
                sandya_fname = '/space/franklin/1/users/sx424/mem_flex/from_Linda_data/{}/dFC_unnorm102.npy'.format(
                    subject)
                if not op.isfile(sandya_fname):
                    print('{} does not have dFC ts file'.format(subject))
                else:
                    sandaya_data = np.load(sandya_fname)
                    allclose = np.allclose(sandaya_data, std_mean)
                    if not allclose:
                        print('Not close!')
                        # raise Exception('Not close!')
                    else:
                        print('All close!')
            # stat_conn = d['stat_conn']
            if dFC_res[pc] is None:
                dFC_res[pc] = np.zeros((len(subjects), *dFC.shape))
            if std_mean_res[pc] is None:
                std_mean_res[pc] = np.zeros((len(subjects), *std_mean.shape))
            # if stat_conn_res[pc] is None:
            #     stat_conn_res[pc] = np.zeros((len(subjects), *stat_conn.shape))
            dFC_res[pc][subject_ind] = dFC
            std_mean_res[pc][subject_ind] = std_mean
            # stat_conn_res[pc][subject_ind] = stat_conn

    return dFC_res, std_mean_res, stat_conn_res
def trans_morphed_electrodes_to_tkreg(subject,
                                      subject_to,
                                      electrodes,
                                      trans,
                                      brain_mask,
                                      subjects_electrodes=None):
    template_electrodes = []
    morphed_electrodes_file_name = 'electrodes_morph_to_{}.txt'.format(
        subject_to)
    input_fname = op.join(MMVT_DIR, subject, 'electrodes',
                          morphed_electrodes_file_name)
    if not op.isfile(input_fname):
        print('read_morphed_electrodes: Can\'t find {}!'.format(input_fname))
        return None
    print('Reading {} ({})'.format(input_fname,
                                   utils.file_modification_time(input_fname)))
    voxels = np.genfromtxt(input_fname, dtype=np.float, delimiter=' ')
    electrodes_names = [elc_name for (elc_name, _) in electrodes[subject]]
    if subject_to == 'fsaverage':
        voxels = tut.mni152_mni305(voxels)
    #check_if_electrodes_inside_the_brain(subject, voxels, electrodes_names, brain_mask)
    tkregs = apply_trans(trans, voxels)
    write_morphed_electrodes_vox_into_csv(subject, subject_to, tkregs,
                                          electrodes_names)
    if subjects_electrodes is not None and subject in subjects_electrodes:
        elecs = [
            elc_name for elc_name, _ in electrodes[subject]
            if elc_name in subjects_electrodes[subject]
        ]
        if len(elecs) == 0:
            print('No electrodes for {}! ({})'.format(
                subject, subjects_electrodes[subject]))
    for tkreg, vox, (elc_name, _) in zip(tkregs, voxels, electrodes[subject]):
        # if subjects_electrodes is not None and elc_name in subjects_electrodes[subject]:
        try:
            vox = np.rint(vox).astype(int)
            if brain_mask[vox[0], vox[1], vox[2]] == 0:
                print('{}: {} is outside the brain!'.format(subject, elc_name))
            template_electrodes.append(('{}_{}'.format(subject,
                                                       elc_name), tkreg))
        except:
            print('Error with {}, {}, voxels={}'.format(
                subject, elc_name, vox))

    return template_electrodes
Exemple #8
0
def plot_sensors_powers(subject,
                        windows_fnames,
                        baseline_window_fname,
                        modality,
                        inverse_method='dSPM',
                        high_gamma_max=120,
                        percentiles=[5, 95],
                        sig_threshold=2,
                        plot_non_norm_powers=False,
                        plot_baseline_stat=False,
                        save_fig=True,
                        bad_channels=[],
                        overwrite=False,
                        parallel=True):
    root_dir = op.join(EEG_DIR if modality == 'eeg' else MEG_DIR, subject)
    input_fname_template = op.join(
        root_dir, '{}-epilepsy-{}-{}-{}-sensors_power.npy'.format(
            subject, inverse_method, modality, '{window}'))
    norm_powers_fname = op.join(
        root_dir, '{}-epilepsy-{}-{}-{}-sensors_norm_power_minmax.npz'.format(
            subject, inverse_method, modality, '{window}'))
    basealine_powers_fname = op.join(
        root_dir, '{}-epilepsy-{}-{}-{}-sensors_power_minmax.npz'.format(
            subject, inverse_method, modality, '{window}'))
    figs_fol = utils.make_dir(
        op.join(MMVT_DIR, subject, 'epilepsy-figures',
                'sensors-power-spectrum'))
    figures_template = op.join(
        figs_fol, '{}-epilepsy-{}-{}-{}-{}-sensors-power.jpg'.format(
            subject, inverse_method, modality, '{window}', '{method}'))

    baseline_window = utils.namebase(baseline_window_fname)
    if not op.isfile(input_fname_template.format(window=baseline_window)):
        print('No baseline powers! {}'.format(
            input_fname_template.format(window=baseline_window)))
        return
    baseline_fname = basealine_powers_fname.format(
        window=utils.namebase(baseline_window))
    if op.isfile(baseline_fname) and not overwrite:
        d = np.load(baseline_fname)
        baseline_mean, baseline_std = d['mean'], d['std']
    else:
        baseline = np.load(input_fname_template.format(
            window=baseline_window)).astype(np.float32)
        baseline_std = np.std(
            baseline, axis=2, keepdims=True
        )  # the standard deviation (over time and vertices) of log baseline values
        baseline_mean = np.mean(
            baseline, axis=2, keepdims=True
        )  # the standard deviation (over time) of log baseline values
        np.savez(baseline_fname, mean=baseline_mean, std=baseline_std)
    if plot_baseline_stat and modality != 'meeg':
        plot_sensors_baseline_powers(baseline_mean, baseline_std, modality,
                                     baseline_window, baseline_window_fname,
                                     figures_template, bad_channels,
                                     high_gamma_max, overwrite)
    for window_fname in windows_fnames:
        window = utils.namebase(window_fname)
        times = epi_utils.get_window_times(window_fname, downsample=2)
        fname = norm_powers_fname.format(window=window)
        if op.isfile(fname) and not overwrite:
            print('Loading {} ({})'.format(
                utils.namebase(fname), utils.file_modification_time(fname)))
            d = np.load(fname)
            norm_powers_min, norm_powers_max = d['min'], d['max']
        else:
            # dividing by the mean of baseline values, taking the log, and
            #           dividing by the standard deviation of log baseline values
            #           ('zlogratio')
            if not op.isfile(input_fname_template.format(window=window)):
                print('No window powers! {}'.format(
                    input_fname_template.format(window=window)))
                continue
            powers = np.load(
                input_fname_template.format(window=window)).astype(np.float32)
            if plot_non_norm_powers and not op.isfile(figures_template.format(window=window, method='max')) \
                    and not overwrite:
                powers_max = np.max(powers, axis=0)  # over vertices
                plot_power_spectrum(powers_max,
                                    times,
                                    figures_template.format(window=window,
                                                            method='max'),
                                    baseline_correction=False,
                                    high_gamma_max=high_gamma_max)

            if baseline_std.shape[1] < powers.shape[1]:
                print('baseline freqs dim is {} and powers freqs dim is {}!'.
                      format(baseline.shape[1], powers.shape[1]))
                powers = powers[:, :baseline.shape[1], :]
            norm_powers = (powers - baseline_mean)  # / baseline_std
            norm_powers_min, norm_powers_max, min_vertices, max_vertices = epi_utils.calc_powers_abs_minmax(
                norm_powers, both_min_and_max=True)
            np.savez(fname, min=norm_powers_min, max=norm_powers_max)

        print('***** {} is baseline corrected using {} *******'.format(
            window, utils.namebase(baseline_window)))
        figure_fname = figures_template.format(
            window=window, method='pos_and_neg') if save_fig else ''
        plot_positive_and_negative_power_spectrum(
            norm_powers_min,
            norm_powers_max,
            times,
            '{} {}'.format(modality, window),
            figure_fname=figure_fname,
            high_gamma_max=high_gamma_max,
            show_only_sig_in_graph=True)