コード例 #1
0
ファイル: dell_tests.py プロジェクト: waffle-iron/mmvt
def load_find_electrode_lead_log(output_fol,
                                 logs_fol,
                                 log_name,
                                 threshold,
                                 elc1_ind=0,
                                 elc2_ind=0):
    (_, names, _, threshold) = utils.load(
        op.join(output_fol, '{}_electrodes.pkl'.format(int(threshold))))
    (elc_ind, electrodes, elctrodes_hemis, groups, error_radius,
     min_elcs_for_lead, max_dist_between_electrodes, min_distance,
     do_post_search) = utils.load(
         op.join(output_fol, logs_fol, '{}.pkl'.format(log_name)))

    elcs_already_in_groups = set(fect.flat_list_of_lists(groups))
    if elc1_ind in elcs_already_in_groups or elc2_ind in elcs_already_in_groups:
        print('elcs are in elcs_already_in_groups!')
    electrodes_list = set(range(len(electrodes))) - elcs_already_in_groups
    for ind in (elc1_ind, elc2_ind):
        if ind not in electrodes_list:
            print('{} are not in electrodes_list!'.format(elc_ind))

    group, noise, dists, dists_to_cylinder, gof, best_elc_ind = fect.find_electrode_group(
        elc_ind, electrodes, elctrodes_hemis, groups, error_radius,
        min_elcs_for_lead, max_dist_between_electrodes, min_distance,
        do_post_search)

    print('elcs_already_in_groups: {}'.format(elcs_already_in_groups))
    print('{} points between {} and {}: {}'.format(len(group), names[group[0]],
                                                   names[group[-1]],
                                                   [names[p] for p in group]))
コード例 #2
0
ファイル: dell_tests.py プロジェクト: waffle-iron/mmvt
def check_voxels_around_electrodes(ct_data, output_fol, threshold, ct_header,
                                   brain_header):
    (electrodes, names, hemis, threshold) = utils.load(
        op.join(output_fol, '{}_electrodes.pkl'.format(int(threshold))))
    groups, noise = utils.load(
        op.join(output_fol, '{}_groups.pkl'.format(int(threshold))))
    ct_voxels = fect.t1_ras_tkr_to_ct_voxels(electrodes, ct_header,
                                             brain_header)
    nei_features = []
    for ct_voxel in ct_voxels:
        ct_voxel_nei = fect.get_voxel_neighbors_ct_values(ct_data,
                                                          ct_voxel,
                                                          r=4)
        nei_features.append([np.mean(ct_voxel_nei), np.std(ct_voxel_nei)])

    from sklearn import mixture
    gmm = mixture.GaussianMixture(n_components=2, covariance_type='full')
    gmm.fit(nei_features)
    Y = gmm.predict_proba(nei_features)
    centroids = gmm.means_

    nei_features = np.array(nei_features)
    fig = plt.figure()
    plt.scatter(nei_features[:, 0], nei_features[:, 1])
    plt.scatter(centroids[:, 0], centroids[:, 1], c='r')
    plt.xlabel('mean')
    plt.ylabel('std')
    plt.show()
コード例 #3
0
ファイル: dell_tests.py プロジェクト: waffle-iron/mmvt
def calc_groups_dist_to_dura(elc_name, output_fol, threshold):
    (electrodes, names, hemis, threshold) = utils.load(
        op.join(output_fol, '{}_electrodes.pkl'.format(int(threshold))))
    groups, noise = utils.load(
        op.join(output_fol, '{}_groups.pkl'.format(int(threshold))))
    elc_ind = names.index(elc_name)
    group, in_group_ind = find_electrode_group(elc_ind, groups)

    verts = {}
    for hemi in ['lh', 'rh']:
        # verts[hemi], _ = nib.freesurfer.read_geometry(op.join(subject_fol, 'surf', '{}.dural'.format(hemi)))
        verts[hemi], _ = nib.freesurfer.read_geometry(
            op.join(subject_fol, 'bem', 'watershed', 'mg105_brain_surface'))

    dists = cdist([electrodes[elc_ind]], verts[hemis[elc_ind]])
    close_verts_dists = np.min(dists, axis=1)
    close_verts_ind = np.argmin(dists, axis=1)
    print('{}: {} ({})'.format(names[elc_ind], close_verts_dists,
                               verts[hemis[elc_ind]][close_verts_ind]))

    mean_dists = []
    for group in groups:
        dists = cdist(electrodes[group], verts[hemis[group[0]]])
        close_verts_dists = np.min(dists, axis=1)
        print('{}-{}: {}'.format(names[group[0]], names[group[-1]],
                                 close_verts_dists))
        mean_dists.append(np.max(close_verts_dists))
    plt.barh(np.arange(len(groups)), mean_dists, align='center', alpha=0.5)
    plt.yticks(np.arange(len(groups)),
               ('{}-{}'.format(names[group[0]], names[group[-1]])
                for group in groups))
    plt.title('max groups dist to dural surface')
    plt.show()
    print('asdf')
コード例 #4
0
ファイル: dell_tests.py プロジェクト: waffle-iron/mmvt
def check_voxels_around_electrodes_in_group(ct_data, output_fol, threshold,
                                            ct_header, brain_header):
    (electrodes, names, hemis, threshold) = utils.load(
        op.join(output_fol, '{}_electrodes.pkl'.format(int(threshold))))
    groups, noise = utils.load(
        op.join(output_fol, '{}_groups.pkl'.format(int(threshold))))
    elcs_group, elcs_colors = [], []
    # elcs_nei = np.zeros((len(utils.flat_list_of_lists(groups)), 3))
    elcs_nei = []
    groups_nei = []
    colors = utils.get_distinct_colors(len(groups))
    groups_names = ['{}-{}'.format(names[g[0]], names[g[-1]]) for g in groups]
    for group_ind, (group, color) in enumerate(zip(groups, colors)):
        group_voxels = fect.t1_ras_tkr_to_ct_voxels(
            [electrodes[g] for g in group], ct_header, brain_header)
        groups_elecs_nei = np.array([
            fect.get_voxel_neighbors_ct_values(ct_data, elc_voxel, r=4)
            for elc_voxel in group_voxels
        ])
        elcs_group.append(
            [np.mean(groups_elecs_nei),
             np.var(groups_elecs_nei)])
        # for elc_voxel in group_voxels:
        #     elc_nei = fect.get_voxel_neighbors_ct_values(ct_data, elc_voxel, r=2)
        #     # elcs_nei.append(elc_nei)
        #     elcs_nei.append([np.sum(elc_nei), np.mean(elc_nei), np.var(elc_nei)])
        #     elcs_group.append('{}-{}'.format(names[group[0]], names[group[-1]]))
        #     elcs_colors.append(color)
    elcs_group = np.array(elcs_group)
    groups_names = np.array(groups_names)
    utils.plot_3d_scatter(elcs_group, names=groups_names)
    # elcs_nei = np.array(elcs_nei)
    # utils.plot_3d_PCA(elcs_nei, colors=elcs_colors, legend_labels=elcs_group)
    print('asdf')
コード例 #5
0
ファイル: import_dipoles.py プロジェクト: keshava/mmvt
def calc_dipoles_rois(subject, atlas='laus125', overwrite=False, n_jobs=4):
    links_dir = utils.get_links_dir()
    subjects_dir = utils.get_link_dir(links_dir, 'subjects')
    mmvt_dir = utils.get_link_dir(links_dir, 'mmvt')
    diploes_rois_output_fname = op.join(mmvt_dir, subject, 'meg', 'dipoles_rois.pkl')
    if op.isfile(diploes_rois_output_fname) and not overwrite:
        diploes_rois = utils.load(diploes_rois_output_fname)
        for dip in diploes_rois.keys():
            diploes_rois[dip]['cortical_probs'] *= 1/sum(diploes_rois[dip]['cortical_probs'])
            diploes_rois[dip]['subcortical_probs'] = []
            diploes_rois[dip]['subcortical_rois'] = []
        # coritcal_labels = set(utils.flat_list_of_lists([diploes_rois[k]['cortical_rois'] for k in diploes_rois.keys()]))
        utils.save(diploes_rois, diploes_rois_output_fname)
        return True

    diploes_input_fname = op.join(mmvt_dir, subject, 'meg', 'dipoles.pkl')
    if not op.isfile(diploes_input_fname):
        print('No dipoles file!')
        return False

    labels = lu.read_labels(subject, subjects_dir, atlas, n_jobs=n_jobs)
    labels = list([{'name': label.name, 'hemi': label.hemi, 'vertices': label.vertices}
                   for label in labels])
    if len(labels) == 0:
        print('Can\'t find the labels for atlas {}!'.format(atlas))
        return False

    # find the find_rois package
    mmvt_code_fol = utils.get_mmvt_code_root()
    ela_code_fol = op.join(utils.get_parent_fol(mmvt_code_fol), 'electrodes_rois')
    if not op.isdir(ela_code_fol) or not op.isfile(op.join(ela_code_fol, 'find_rois', 'main.py')):
        print("Can't find ELA folder!")
        print('git pull https://github.com/pelednoam/electrodes_rois.git')
        return False

    # load the find_rois package
    try:
        import sys
        if ela_code_fol not in sys.path:
            sys.path.append(ela_code_fol)
        from find_rois import main as ela
    except:
        print('Can\'t load find_rois package!')
        utils.print_last_error_line()
        return False

    dipoles_dict = utils.load(diploes_input_fname)
    diploles_names, dipoles_pos = [], []
    for cluster_name, dipoles in dipoles_dict.items():
        for begin_t, _, x, y, z, _, _, _, _, _ in dipoles:
            dipole_name = '{}_{}'.format(cluster_name, begin_t) if len(dipoles) > 1 else cluster_name
            diploles_names.append(dipole_name.replace(' ', ''))
            dipoles_pos.append([k * 1e3 for k in [x, y, z]])
    dipoles_rois = ela.identify_roi_from_atlas(
        atlas, labels, diploles_names, dipoles_pos, approx=3, elc_length=0, hit_only_cortex=True,
        subjects_dir=subjects_dir, subject=subject, n_jobs=n_jobs)
    # Convert the list to a dict
    dipoles_rois_dict = {dipoles_rois['name']: dipoles_rois for dipoles_rois in dipoles_rois}
    utils.save(dipoles_rois_dict, diploes_rois_output_fname)
コード例 #6
0
def compare_electrodes_labeling(electrodes,
                                template_system,
                                atlas='aparc.DKTatlas40'):
    template = 'fsaverage' if template_system == 'ras' else 'colin27' if template_system == 'mni' else template_system
    template_elab_files = glob.glob(
        op.join(MMVT_DIR, template, 'electrodes',
                '{}_{}_electrodes_cigar_r_3_l_4.pkl'.format(template, atlas)))
    if len(template_elab_files) == 0:
        print('No electrodes labeling file for {}!'.format(template))
        return
    elab_template = utils.load(template_elab_files[0])
    errors = ''
    for subject in electrodes.keys():
        elab_files = glob.glob(
            op.join(
                MMVT_DIR, subject, 'electrodes',
                '{}_{}_electrodes_cigar_r_3_l_4.pkl'.format(subject, atlas)))
        if len(elab_files) == 0:
            print('No electrodes labeling file for {}!'.format(subject))
            continue
        electrodes_names = [e[0] for e in electrodes[subject]]
        elab = utils.load(elab_files[0])
        elab = [e for e in elab if e['name'] in electrodes_names]
        for elc in electrodes_names:
            no_errors = True
            elc_labeling = [e for e in elab if e['name'] == elc][0]
            elc_labeling_template = [
                e for e in elab_template
                if e['name'] == '{}_{}'.format(subject, elc)
            ][0]
            for roi, prob in zip(elc_labeling['cortical_rois'],
                                 elc_labeling['cortical_probs']):
                no_err, err = compare_rois_and_probs(
                    subject, template, elc, roi, prob,
                    elc_labeling['cortical_rois'],
                    elc_labeling_template['cortical_rois'],
                    elc_labeling_template['cortical_probs'])
                no_errors = no_errors and no_err
                if err != '':
                    errors += err + '\n'
            for roi, prob in zip(elc_labeling['subcortical_rois'],
                                 elc_labeling['subcortical_probs']):
                no_err, err = compare_rois_and_probs(
                    subject, template, elc, roi, prob,
                    elc_labeling['subcortical_rois'],
                    elc_labeling_template['subcortical_rois'],
                    elc_labeling_template['subcortical_probs'])
                no_errors = no_errors and no_err
                if err != '':
                    errors += err + '\n'
            if no_errors:
                print('{},{},Good!'.format(subject, elc))
                errors += '{},{},Good!\n'.format(subject, elc)
    with open(op.join(MMVT_DIR, template, 'electrodes', 'trans_errors.txt'),
              "w") as text_file:
        print(errors, file=text_file)
コード例 #7
0
def load_tatiana_meg_coh(subject, fsaverage, atlas, ds_all, ds_subject, hc_indices, subject_index, conditions, labels):
    for sub, ds, indices in zip([subject, fsaverage], [ds_subject, ds_all], [hc_indices, subject_index]):
        print(sub)
        d = {}
        N = len(labels)
        d['labels'] = labels
        d['hemis'] = [l[-2:] for l in d['labels']]
        locations = utils.load(op.join(SUBJECTS_DIR, sub, 'label', '{}_center_of_mass.pkl'.format(atlas)))
        d['locations'] = np.array([locations[l] for l in d['labels']]) # * 1000.0
        # todo: find why...
        if sub == subject:
            d['locations'] *= 1000.0
        d['conditions'] = conditions
        d['con_indices'], d['con_names'], d['con_types'] = calc_meta_data(d['labels'], d['hemis'], 1, N)
        high_low_diff = np.zeros((len(np.tril_indices(N, -1)[0]), len(ds_all)))
        for ind in range(len(ds)):
            high_low = np.zeros((ds_all[ind]['high'].shape[0], ds_all[ind]['high'].shape[1], 2))
            high_low[:, :, 0] = np.mean(ds_all[ind]['high'][:, :, indices], 2)
            high_low[:, :, 1] = np.mean(ds_all[ind]['low'][:, :, indices], 2)
            ds[ind]['data'] = np.zeros((N, N, 1))
            ds[ind]['data'][:, :, 0] = ds[ind]['pp_mat']
            ds[ind]['data'][np.where(np.isnan(ds[ind]['data']))] = 1
            ds[ind]['con_values'], high_low_diff[:, ind] = create_meg_coh_data(
                ds[ind]['data'], len(d['conditions']), high_low)
        d['con_values'] = np.zeros((ds[0]['con_values'].shape[0], len(ds), 1))
        for ind in range(len(ds)):
            d['con_values'][:, ind, :] = ds[ind]['con_values']
        d['con_colors'] = calc_con_colors(d['con_values'], high_low_diff)
        np.savez(op.join(BLENDER_ROOT_DIR, sub, 'meg_coh_bev.npz'), **d)
コード例 #8
0
ファイル: freesurfer_utils.py プロジェクト: emacadams/mmvt
def test_patch(subject, **kargs):
    from src.utils import preproc_utils as pu
    SUBJECTS_DIR, MMVT_DIR, FREESURFER_HOME = pu.get_links()

    flat_patch_cut_vertices = utils.load(op.join(MMVT_DIR, subject, 'flat_patch_cut_vertices.pkl'))
    for hemi in utils.HEMIS:
        # verts, faces = read_patch(hemi, SUBJECTS_DIR)
        # write_patch(op.join(MMVT_DIR, 'fsaverage', 'surf', '{}.flat.test.pial'.format(hemi)), verts, faces)

        # fs_verts, fs_faces = utils.read_pial('fsaverage', MMVT_DIR, hemi, surface_type='inflated')
        print('Reading inflated surf')
        fs_verts, fs_faces = nib.freesurfer.read_geometry(op.join(SUBJECTS_DIR, subject, 'surf', '{}.inflated'.format(hemi)))
        # flat_verts, flat_faces = read_patch(hemi, SUBJECTS_DIR, surface_type='inflated')
        # good_verts = set(np.unique(flat_faces))
        # bad_verts = np.setdiff1d(np.arange(0, flat_verts.shape[0]), good_verts)
        # bad_faces_inds = set(utils.flat_list_of_lists([verts_faces_lookup[hemi][v] for v in bad_verts]))
        patch_fname = op.join(SUBJECTS_DIR, subject, 'surf', '{}.inflated.patch'.format(hemi))
        print('Writing patch')
        flat_patch_cut_vertices_hemi = set(flat_patch_cut_vertices[hemi])
        write_patch(patch_fname, [(ind, v) for ind, v in enumerate(fs_verts) if ind not in flat_patch_cut_vertices_hemi], set())#flat_faces)

        print('Reading patch')
        patch_verts, patch_faces = read_patch(subject, hemi, SUBJECTS_DIR, surface_type='inflated', patch_fname=patch_fname)

        print('Writing ply')
        patch_verts *= 0.1
        utils.write_ply_file(patch_verts, patch_faces, op.join(MMVT_DIR, subject, 'surf', '{}.flat.pial.test.ply').format(hemi))
    print('Finish!')
コード例 #9
0
ファイル: fMRI_preproc.py プロジェクト: dorianps/mmvt
def init_clusters(subject, contrast_name, input_fol):
    input_fname = op.join(input_fol,
                          'fmri_{}_{}.npy'.format(contrast_name, '{hemi}'))
    contrast_per_hemi, verts_per_hemi = {}, {}
    for hemi in utils.HEMIS:
        fmri_fname = input_fname.format(hemi=hemi)
        if utils.file_type(input_fname) == 'npy':
            x = np.load(fmri_fname)
            contrast_per_hemi[hemi] = x[:, 0]
        else:
            # try nibabel
            x = nib.load(fmri_fname)
            contrast_per_hemi[hemi] = x.get_data().ravel()
        pial_npz_fname = op.join(BLENDER_ROOT_DIR, subject,
                                 '{}.pial.npz'.format(hemi))
        if not op.isfile(pial_npz_fname):
            print('No pial npz file (), creating one'.format(pial_npz_fname))
            verts, faces = utils.read_ply_file(
                op.join(BLENDER_ROOT_DIR, subject, '{}.pial.ply'.format(hemi)))
            np.savez(pial_npz_fname[:-4], verts=verts, faces=faces)
        d = np.load(pial_npz_fname)
        verts_per_hemi[hemi] = d['verts']
    connectivity_fname = op.join(BLENDER_ROOT_DIR, subject,
                                 'spatial_connectivity.pkl')
    if not op.isfile(connectivity_fname):
        from src.preproc import anatomy_preproc
        anatomy_preproc.create_spatial_connectivity(subject)
    connectivity_per_hemi = utils.load(connectivity_fname)
    return contrast_per_hemi, connectivity_per_hemi, verts_per_hemi
コード例 #10
0
def calc_ana(overwrite=False, only_linda=False):
    good_subjects_fname = op.join(root_path, 'good_subjects.npz')
    ana_results_fname = op.join(root_path, 'ana_results.pkl')
    if not op.isfile(ana_results_fname) or not op.isfile(
            good_subjects_fname) or overwrite:
        laterality, to_use, TR, values, all_subjects = read_scoring()
        if only_linda:
            subject_list = get_linda_subjects()
            inds = np.where(np.in1d(all_subjects, subject_list))[0]
            print(inds)
            good_subjects = all_subjects[inds]
            master_grouping = (np.sum(
                (values <= 5).astype(int), axis=1) > 0).astype(int)
            subject_groups = master_grouping[inds]
            disturbed_inds = np.array(np.where(subject_groups == 1)[0])
            preserved_inds = np.array(np.where(subject_groups == 0)[0])
            # laterality = ['L'] * len(good_subjects)
            bad_indices, labels = check_subjects_labels(
                good_subjects, check_labels_indices=False)
        else:
            good_subjects, good_subjects_inds, labels = find_good_inds(
                all_subjects, only_left, TR, fast_TR, to_use, laterality)
            disturbed_inds, preserved_inds = calc_disturbed_preserved_inds(
                good_subjects_inds, values)
        dFC_res, std_mean_res, stat_conn_res = get_subjects_dFC(good_subjects)
        utils.save((dFC_res, std_mean_res, stat_conn_res, disturbed_inds,
                    preserved_inds, good_subjects, labels, laterality),
                   op.join(root_path, 'ana_results.pkl'))
    else:
        (dFC_res, std_mean_res, stat_conn_res, disturbed_inds, preserved_inds, good_subjects, labels, laterality) = \
            utils.load(ana_results_fname)
    print('disturbed_inds: {}'.format(disturbed_inds))
    print('preserved_inds: {}'.format(preserved_inds))
    return dFC_res, std_mean_res, stat_conn_res, disturbed_inds, preserved_inds, good_subjects, labels, laterality
コード例 #11
0
ファイル: fMRI_preproc.py プロジェクト: dorianps/mmvt
def load_clusters_tval_hist(input_fol):
    from itertools import chain
    clusters = utils.load(op.join(input_fol, 'clusters_tval_hist.pkl'))
    res = []
    for t_val, clusters_tval in clusters.items():
        tval = float('{:.2f}'.format(t_val))
        max_size = max([
            max([len(c) for c in clusters_tval[hemi]]) for hemi in utils.HEMIS
        ])
        avg_size = np.mean(
            list(
                chain.from_iterable(([[len(c) for c in clusters_tval[hemi]]
                                      for hemi in utils.HEMIS]))))
        clusters_num = sum(
            map(len, [clusters_tval[hemi] for hemi in utils.HEMIS]))
        res.append((tval, max_size, avg_size, clusters_num))
    res = sorted(res)
    # res = sorted([(t_val, max([len(c) for c in [c_tval[hemi] for hemi in utils.HEMIS]])) for t_val, c_tval in clusters.items()])
    # tvals = [float('{:.2f}'.format(t_val)) for t_val, c_tval in clusters.items()]
    max_sizes = [r[1] for r in res]
    avg_sizes = [r[2] for r in res]
    tvals = [float('{:.2f}'.format(r[0])) for r in res]
    clusters_num = [r[3] for r in res]
    fig, ax1 = plt.subplots()
    ax1.plot(tvals, max_sizes, 'b')
    ax1.set_ylabel('max size', color='b')
    ax2 = ax1.twinx()
    ax2.plot(tvals, clusters_num, 'r')
    # ax2.plot(tvals, avg_sizes, 'g')
    ax2.set_ylabel('#clusters', color='r')
    plt.show()
    print('sdfsd')
コード例 #12
0
ファイル: show_caudate_dlpfc.py プロジェクト: ltirrell/mmvt
def create_coloring_files(electrodes, template_subject):
    fol = utils.make_dir(op.join(MMVT_DIR, template_subject))
    regions_csv_fname = op.join(fol, 'coloring', 'caudate_dlpfc.csv')
    all_electrodes_csv_fname = op.join(fol, 'coloring',
                                       'caudate_dlpfc_all.csv')
    template_electrodes = utils.load(
        op.join(fol, 'electrodes', 'template_electrodes.pkl'))
    regions, colors = None, None
    with open(all_electrodes_csv_fname,
              'w') as all_csv, open(regions_csv_fname, 'w') as regions_csv:
        wr_regions = csv.writer(regions_csv, quoting=csv.QUOTE_NONE)
        wr_all = csv.writer(all_csv, quoting=csv.QUOTE_NONE)
        for subject, subject_electrodes in electrodes.items():
            if regions is None:
                regions = list(subject_electrodes.keys())
                colors = utils.get_distinct_colors(len(regions) + 1)
            all_subject_electrodes = [
                elc_name.split('_')[1]
                for (elc_name, _) in template_electrodes[subject]
            ]
            for elc_name in all_subject_electrodes:
                for region, color in zip(regions, colors[:-1]):
                    if elc_name in electrodes[subject][region]:
                        print('{}: {} {}'.format(subject, elc_name, region))
                        wr_all.writerow(
                            ['{}_{}'.format(subject, elc_name), *color])
                        wr_regions.writerow(
                            ['{}_{}'.format(subject, elc_name), *color])
                        break
                else:
                    wr_all.writerow(
                        ['{}_{}'.format(subject, elc_name), *colors[-1]])
                    wr_regions.writerow(
                        ['{}_{}'.format(subject, elc_name), 1, 1, 1])
コード例 #13
0
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))
コード例 #14
0
def merge_labels(subject, fmri_names):
    utils.delete_folder_files(op.join(MMVT_DIR, subject, 'fmri', 'labels'))
    vertices_labels_lookup = utils.load(
        op.join(MMVT_DIR, subject,
                'aparc.DKTatlas40_vertices_labels_lookup.pkl'))
    output_fol = utils.make_dir(op.join(MMVT_DIR, subject, 'fmri', 'labels'))
    for fmri_name in fmri_names:
        labels = []
        for hemi in utils.HEMIS:
            surf_fname = op.join(
                MMVT_DIR, subject, 'fmri', 'fmri_{}_{}.npy'.format(
                    fmri_name.replace('_insulaopercula', ''), hemi))
            surf_data = np.load(surf_fname)
            vertices_indices = np.where(surf_data >= 0.95)[0]
            if len(vertices_indices) == 0:
                continue
            insulaopercula_vertices = []
            vertices, _ = utils.read_pial(subject, MMVT_DIR, hemi)
            for vert_ind in tqdm(vertices_indices):
                vert_label = vertices_labels_lookup[hemi][vert_ind]
                if vert_label.startswith(
                        'insula'
                ):  # or vert_label.startswith('parsopercularis')
                    insulaopercula_vertices.append(vert_ind)
            label = mne.Label(insulaopercula_vertices,
                              vertices[insulaopercula_vertices],
                              hemi=hemi,
                              name=fmri_name,
                              subject=subject)
            labels.append(label)
            label.save(op.join(output_fol, '{}.label'.format(fmri_name)))
        anat.labels_to_annot(subject, atlas=fmri_name, labels=labels)
        anat.calc_labeles_contours(subject, fmri_name)
コード例 #15
0
ファイル: labels_utils.py プロジェクト: waffle-iron/mmvt
def morph_labels(morph_from_subject, morph_to_subject, atlas, hemi, n_jobs=1):
    labels_fol = op.join(SUBJECTS_DIR, morph_to_subject, 'label')
    labels_fname = op.join(labels_fol,
                           '{}.{}.pkl'.format(hemi, atlas, morph_from_subject))
    annot_file = op.join(SUBJECTS_DIR, morph_from_subject, 'label',
                         '{}.{}.annot'.format(hemi, atlas))
    if not op.isfile(annot_file):
        print("Can't find the annot file in {}!".format(annot_file))
        return []
    if not op.isfile(labels_fname):
        labels = mne.read_labels_from_annot(morph_from_subject,
                                            atlas,
                                            subjects_dir=SUBJECTS_DIR,
                                            hemi=hemi)
        if morph_from_subject != morph_to_subject:
            morphed_labels = []
            for label in labels:
                label.values.fill(1.0)
                morphed_label = label.morph(morph_from_subject,
                                            morph_to_subject, 5, None,
                                            SUBJECTS_DIR, n_jobs)
                morphed_labels.append(morphed_label)
            labels = morphed_labels
        utils.save(labels, labels_fname)
    else:
        labels = utils.load(labels_fname)
    return labels
コード例 #16
0
def post_script(args):
    from src.utils import figures_utils as fu
    from src.utils import utils
    from src.mmvt_addon import clusters_utils as cu

    subject_fol = op.join(su.get_mmvt_dir(), args.subject)
    figures_fol = op.join(subject_fol, 'figures')
    clusters_file_names, _, _ = cu.get_clusters_files('fmri', subject_fol)
    clusters_names = [
        f for f in clusters_file_names if args.clusters_type in f
    ]
    print('clusters_names: {}'.format(clusters_names))
    fmri_files_minmax_fname = op.join(subject_fol, 'fmri',
                                      'fmri_files_minmax_cm.pkl')
    data_min, data_max, colors_map_name = utils.load(fmri_files_minmax_fname)
    for clusters_name, inflated, background_color in product(
            clusters_names, args.inflated, args.background_color):
        print('Combing figures for {}, inflated: {}, background color: {}'.
              format(clusters_name, inflated, background_color))
        perspectives_image_fname = fu.combine_four_brain_perspectives(
            figures_fol, inflated, args.dpi, background_color, clusters_name,
            args.inflated_ratio, True, args.overwrite)
        fu.combine_brain_with_color_bar(
            data_max, data_min, perspectives_image_fname, colors_map_name,
            args.overwrite, args.dpi, args.x_left_crop, args.x_right_crop,
            args.y_top_crop, args.y_buttom_crop, args.w_fac, args.h_fac,
            background_color)
コード例 #17
0
ファイル: fMRI_preproc.py プロジェクト: ofek-schechner/mmvt
def init_clusters(subject, contrast_name, input_fol):
    input_fname = op.join(input_fol, 'fmri_{}_{}.npy'.format(contrast_name, '{hemi}'))
    contrast_per_hemi, verts_per_hemi = {}, {}
    for hemi in utils.HEMIS:
        fmri_fname = input_fname.format(hemi=hemi)
        if utils.file_type(input_fname) == 'npy':
            x = np.load(fmri_fname)
            contrast_per_hemi[hemi] = x[:, 0]
        else:
            # try nibabel
            x = nib.load(fmri_fname)
            contrast_per_hemi[hemi] = x.get_data().ravel()
        pial_npz_fname = op.join(BLENDER_ROOT_DIR, subject, '{}.pial.npz'.format(hemi))
        if not op.isfile(pial_npz_fname):
            print('No pial npz file (), creating one'.format(pial_npz_fname))
            verts, faces = utils.read_ply_file(op.join(BLENDER_ROOT_DIR, subject, '{}.pial.ply'.format(hemi)))
            np.savez(pial_npz_fname[:-4], verts=verts, faces=faces)
        d = np.load(pial_npz_fname)
        verts_per_hemi[hemi] = d['verts']
    connectivity_fname = op.join(BLENDER_ROOT_DIR, subject, 'spatial_connectivity.pkl')
    if not op.isfile(connectivity_fname):
        from src.preproc import anatomy_preproc
        anatomy_preproc.create_spatial_connectivity(subject)
    connectivity_per_hemi = utils.load(connectivity_fname)
    return contrast_per_hemi, connectivity_per_hemi, verts_per_hemi
コード例 #18
0
ファイル: dell_tests.py プロジェクト: waffle-iron/mmvt
def find_points_on_dural_surface(elc1_name,
                                 elc2_name,
                                 dural_verts,
                                 dural_verts_nei,
                                 threshold,
                                 debug=False,
                                 dural_normals=None,
                                 sigma=5,
                                 ang_thresholds=(0.1, 0.5)):
    (electrodes, names, hemis, threshold) = utils.load(
        op.join(output_fol, '{}_electrodes.pkl'.format(int(threshold))))
    elc1_ind = names.index(elc1_name)
    elc2_ind = names.index(elc2_name)
    if len(hemis) != len(electrodes):
        hemis = fect.find_electrodes_hemis(subject_fol, electrodes, None, 1,
                                           dural_verts, dural_normals)
    print('find_points_path_on_dural_surface from {} to {}'.format(
        elc1_name, elc2_name))
    points, indices = fect.find_points_path_on_dural_surface(
        elc1_ind,
        elc2_ind,
        hemis,
        electrodes,
        dural_verts,
        dural_verts_nei,
        names,
        sigma=sigma,
        ang_thresholds=ang_thresholds,
        debug=debug)
    group_names = [names[ind] for ind in indices]
    print(len(group_names), group_names)
コード例 #19
0
ファイル: fMRI_preproc.py プロジェクト: ofek-schechner/mmvt
def load_clusters_tval_hist(input_fol):
    from itertools import chain
    clusters = utils.load(op.join(input_fol, 'clusters_tval_hist.pkl'))
    res = []
    for t_val, clusters_tval in clusters.items():
        tval = float('{:.2f}'.format(t_val))
        max_size = max([max([len(c) for c in clusters_tval[hemi]]) for hemi in utils.HEMIS])
        avg_size = np.mean(list(chain.from_iterable(([[len(c) for c in clusters_tval[hemi]] for hemi in utils.HEMIS]))))
        clusters_num = sum(map(len, [clusters_tval[hemi] for hemi in utils.HEMIS]))
        res.append((tval, max_size, avg_size, clusters_num))
    res = sorted(res)
    # res = sorted([(t_val, max([len(c) for c in [c_tval[hemi] for hemi in utils.HEMIS]])) for t_val, c_tval in clusters.items()])
    # tvals = [float('{:.2f}'.format(t_val)) for t_val, c_tval in clusters.items()]
    max_sizes = [r[1] for r in res]
    avg_sizes = [r[2] for r in res]
    tvals = [float('{:.2f}'.format(r[0])) for r in res]
    clusters_num = [r[3] for r in res]
    fig, ax1 = plt.subplots()
    ax1.plot(tvals, max_sizes, 'b')
    ax1.set_ylabel('max size', color='b')
    ax2 = ax1.twinx()
    ax2.plot(tvals, clusters_num, 'r')
    # ax2.plot(tvals, avg_sizes, 'g')
    ax2.set_ylabel('#clusters', color='r')
    plt.show()
    print('sdfsd')
コード例 #20
0
def read_morphed_electrodes(subjects_electrodes,
                            subject_to='colin27',
                            bipolar=True,
                            prefix='morphed_'):
    fol = utils.make_dir(op.join(MMVT_DIR, subject_to, 'electrodes'))
    bipolar_output_fname = op.join(
        fol, '{}electrodes_bipolar_positions.npz'.format(prefix))
    monopolar_output_fname = op.join(
        fol, '{}electrodes_positions.npz'.format(prefix))
    bad_electrodes, bad_subjects = [], set()
    template_bipolar_electrodes, template_electrodes = defaultdict(
        list), defaultdict(list)
    morphed_electrodes_fname = op.join(MMVT_DIR, subject_to, 'electrodes',
                                       'morphed_electrodes.pkl')
    if False:  #op.isfile(morphed_electrodes_fname):
        template_bipolar_electrodes, template_electrodes = utils.load(
            morphed_electrodes_fname)
    else:
        for subject, electodes_names in subjects_electrodes.items():
            electrodes_pos = {}
            for elecs_bipolar_names in electodes_names:
                electrodes_found = True
                for elec_name in elecs_bipolar_names:
                    elec_input_fname = op.join(
                        MMVT_DIR, subject, 'electrodes', 'ela_morphed',
                        '{}_ela_morphed.npz'.format(elec_name))
                    if not op.isfile(elec_input_fname):
                        print('{} {} not found!'.format(subject, elec_name))
                        bad_electrodes.append('{}_{}'.format(
                            subject, elec_name))
                        bad_subjects.add(subject)
                        electrodes_found = False
                        break
                    else:
                        d = np.load(elec_input_fname)
                    electrodes_pos[elec_name] = d['pos']
                    template_electrodes[subject].append((elec_name, d['pos']))
                if not electrodes_found:
                    continue
                elc1, elc2 = elecs_bipolar_names
                (group, num1), (_, num2) = utils.elec_group_number(
                    elc1), utils.elec_group_number(elc2)
                if num1 > num2:
                    elc1, elc2 = elc2, elc1
                    num1, num2 = num2, num1
                bipolar_elec_name = '{}{}-{}'.format(group, num2, num1)
                pos1, pos2 = electrodes_pos[elc1], electrodes_pos[elc2]
                bipolar_pos = pos1 + (pos2 - pos1) / 2
                template_bipolar_electrodes[subject].append(
                    (bipolar_elec_name, bipolar_pos))
        utils.save(template_bipolar_electrodes, morphed_electrodes_fname)

    save_electrodes(template_electrodes, monopolar_output_fname)
    save_electrodes(template_bipolar_electrodes, bipolar_output_fname)
    print('Bad subjects:')
    print(bad_subjects)
    return monopolar_output_fname, bipolar_output_fname
コード例 #21
0
def calc_sorting_indices(subject, labels):
    meta_data = utils.Bag(
        utils.load(
            op.join(SUBJECTS_DIR, subject, 'electrodes_coh_meta_data.pkl')))
    sorting_indices = np.array(
        utils.find_list_items_in_list(meta_data.electrodes, labels))
    if -1 in sorting_indices:
        raise Exception('You should check your lalbels...')
    return sorting_indices
コード例 #22
0
ファイル: cics.py プロジェクト: bdthombre/mmvt
def calc_scan_rescan_diff(subject, do_plot_hist=True, overwrite=False):
    means_input_fnames = [
        op.join(
            op.join(RESULTS_FOL, 'aparc_aseg_hists', subject, scan_rescan,
                    'aparc_values.pkl')) for scan_rescan in SCAN_RESCAN
    ]
    if not all([op.isfile(fname) for fname in means_input_fnames]):
        print('calc_scan_rescan_diff: {} no all files exist!'.format(subject))
        return
    means_diff_fname = op.join(RESULTS_FOL, 'aparc_aseg_hists', subject,
                               'aparc_values_diffs.pkl')
    mmvt_file_name = '{}_ASL_scan_rescan_diffs'.format(subject)
    mmvt_output_fname = op.join(
        utils.make_dir(op.join(MMVT_DIR, 'fsaverage', 'labels',
                               'labels_data')),
        '{}.npz'.format(mmvt_file_name))
    if op.isfile(mmvt_output_fname) and op.isfile(
            means_diff_fname) and not overwrite:
        print('calc_scan_rescan_diff: files exist for {}'.format(subject))
        return True
    scan_means, rescan_means = [
        utils.load(fname) for fname in means_input_fnames
    ]
    region_names = scan_means.keys()
    diffs = {
        region: scan_means.get(region, 0) - rescan_means.get(region, 0)
        for region in region_names
    }
    utils.save(diffs, means_diff_fname)
    data = np.array([diffs[region_name] for region_name in region_names])
    labels_names = [get_aparc_label_name(region) for region in region_names]
    minmax = utils.calc_abs_minmax(data)

    figure_output_fname = op.join(RESULTS_FOL, 'aparc_aseg_hists', subject,
                                  'labels_scan_rescan_diffs.jpg')
    if do_plot_hist and (not op.isfile(figure_output_fname) or overwrite):
        fig = plt.figure()
        # ax = fig.add_subplot(111)
        x = range(len(labels_names))
        plt.bar(x, data)
        # plt.xticks(x, labels_names, rotation=90)
        plt.title('{} scan-rescan ASL diff'.format(subject))
        plt.ylabel('ASL diff')
        print('Saving bar plot in {}'.format(figure_output_fname))
        plt.savefig(figure_output_fname)
        plt.close()

    np.savez(mmvt_output_fname,
             names=labels_names,
             atlas='aparc',
             data=data,
             title=mmvt_file_name,
             data_min=-minmax,
             data_max=minmax,
             cmap='BuPu-YlOrRd')
コード例 #23
0
ファイル: calculte_casualty.py プロジェクト: bdthombre/mmvt
def find_functional_rois(subject,
                         ictal_clips,
                         modality,
                         seizure_times,
                         atlas,
                         min_cluster_size,
                         inverse_method,
                         overwrite=False,
                         n_jobs=4):
    fwd_usingMEG, fwd_usingEEG = meg.get_fwd_flags(modality)
    modality_fol = op.join(MMVT_DIR, subject, meg.modality_fol(modality))
    stcs_fol = op.join(modality_fol,
                       'ictal-{}-zvals-stcs'.format(inverse_method))
    ictlas_fname = op.join(
        modality_fol, '{}-epilepsy-{}-{}-amplitude-zvals-ictals.pkl'.format(
            subject, inverse_method, modality))
    # Make sure we have a morph map, and if not, create it here, and not in the parallel function
    mne.surface.read_morph_map(subject, subject, subjects_dir=SUBJECTS_DIR)
    connectivity = anat.load_connectivity(subject)
    if overwrite:
        utils.delete_folder_files(
            op.join(MMVT_DIR, subject, modality_fol, 'clusters'))
    if op.isfile(ictlas_fname):
        ictals = utils.load(ictlas_fname)
    else:
        params = [(subject, clip_fname, inverse_method, modality,
                   seizure_times, stcs_fol, n_jobs)
                  for clip_fname in ictal_clips]
        ictals = utils.run_parallel(_calc_ictal_and_baseline_parallel, params,
                                    n_jobs)
        utils.save(ictals, ictlas_fname)
    for stc_name, ictal_stc, mean_baseline in ictals:
        max_ictal = ictal_stc.data.max()
        if max_ictal < mean_baseline:
            print('max ictal ({}) < mean baseline ({})!'.format(
                max_ictal, mean_baseline))
            continue
        meg.find_functional_rois_in_stc(subject,
                                        subject,
                                        atlas,
                                        utils.namebase(stc_name),
                                        mean_baseline,
                                        threshold_is_precentile=False,
                                        extract_time_series_for_clusters=False,
                                        time_index=0,
                                        min_cluster_size=min_cluster_size,
                                        min_cluster_max=mean_baseline,
                                        fwd_usingMEG=fwd_usingMEG,
                                        fwd_usingEEG=fwd_usingEEG,
                                        stc_t_smooth=ictal_stc,
                                        modality=modality,
                                        connectivity=connectivity,
                                        n_jobs=n_jobs)
コード例 #24
0
ファイル: dell_tests.py プロジェクト: waffle-iron/mmvt
def calc_dist_on_cylinder(elc1, elc2, threshold, output_fol, error_radius):
    (electrodes, names, hemis, threshold) = utils.load(
        op.join(output_fol, '{}_electrodes.pkl'.format(int(threshold))))
    elc_ind1, elc_ind2 = names.index(elc1), names.index(elc2)
    groups, noise = utils.load(
        op.join(output_fol, '{}_groups.pkl'.format(int(threshold))))
    groups_mask = [(elc_ind1 in g) for g in groups]
    if sum(groups_mask) == 1:
        group = [g for g, m in zip(groups, groups_mask) if m][0]
        print('Electrodes belongs to {}-{}'.format(names[group[0]],
                                                   names[group[1]]))
    else:
        print('No group was found!')
        return
    _, cylinder, _ = fect.points_in_cylinder(electrodes[group[0]],
                                             electrodes[group[-1]], electrodes,
                                             error_radius)
    closest_points = fect.find_closest_points_on_cylinder(
        electrodes, [elc_ind1, elc_ind2], cylinder)
    dist = np.linalg.norm(closest_points[0] - closest_points[1])
    print(dist)
コード例 #25
0
ファイル: figures_utils.py プロジェクト: pelednoam/mmvt
def example4(subject='colin27', map_name='s32_spmT', figure_name='splitted_lateral_medial_pial_white.png'):
    data_min, data_max = utils.load(
        op.join(MMVT_DIR, subject, 'fmri', 'fmri_activity_map_minmax_{}.pkl'.format(map_name)))
    data_min = utils.ceil_floor(data_min)
    data_max = utils.ceil_floor(data_max)
    figure_fname = op.join(MMVT_DIR, subject, 'figures', figure_name)
    colors_map = 'BuPu_YlOrRd'
    background = 'white' if 'white' in figure_name else 'black'
    fu.combine_brain_with_color_bar(
        data_max, data_min, figure_fname, colors_map,
        x_left_crop=300, x_right_crop=300, y_top_crop=0, y_buttom_crop=0,
        w_fac=1.5, h_fac=1, facecolor=background)
コード例 #26
0
ファイル: fMRI_preproc.py プロジェクト: ofek-schechner/mmvt
def create_functional_rois(subject, contrast_name, clusters_labels_fname='', func_rois_folder=''):
    if clusters_labels_fname == '':
        clusters_labels = utils.load(op.join(
            BLENDER_ROOT_DIR, subject, 'fmri', 'clusters_labels_{}.npy'.format(contrast_name)))
    if func_rois_folder == '':
        func_rois_folder = op.join(SUBJECTS_DIR, subject, 'mmvt', 'fmri', 'functional_rois', '{}_labels'.format(contrast_name))
    utils.delete_folder_files(func_rois_folder)
    for cl in clusters_labels:
        cl_name = 'fmri_{}_{:.2f}'.format(cl['name'], cl['max'])
        new_label = mne.Label(cl['vertices'], cl['coordinates'], hemi=cl['hemi'], name=cl_name,
            filename=None, subject=subject, verbose=None)
        new_label.save(op.join(func_rois_folder, cl_name))
コード例 #27
0
ファイル: dell_tests.py プロジェクト: waffle-iron/mmvt
def find_group_between_pair(elc1,
                            elc2,
                            threshold,
                            output_fol,
                            min_distance=3,
                            error_radius=2):
    (electrodes, names, hemis, threshold) = utils.load(
        op.join(output_fol, '{}_electrodes.pkl'.format(int(threshold))))
    elc_ind1, elc_ind2 = names.index(elc1), names.index(elc2)
    group, too_close_points, dists, dists_to_cylinder = \
        fect.find_group_between_pair(elc_ind1, elc_ind2, electrodes, error_radius, min_distance)
    print(group, too_close_points, dists, dists_to_cylinder)
コード例 #28
0
ファイル: dell_tests.py プロジェクト: waffle-iron/mmvt
def mask_voxels_outside_brain(elc_name, output_fol, threshold, ct_header,
                              brain, aseg, user_fol, subject_fol):
    (electrodes, names, hemis, threshold) = utils.load(
        op.join(output_fol, '{}_electrodes.pkl'.format(int(threshold))))
    # elc_ind = names.index(elc_name)
    # t1_tkras_coords = np.array([electrodes[elc_ind]])
    # ct_voxels = fect.t1_ras_tkr_to_ct_voxels(t1_tkras_coords, ct_header, brain.header)
    ct_voxels = fect.t1_ras_tkr_to_ct_voxels(electrodes, ct_header,
                                             brain.header)
    voxels = fect.mask_voxels_outside_brain(ct_voxels, ct_header, brain,
                                            user_fol, subject_fol, aseg)
    print(voxels)
コード例 #29
0
ファイル: cics.py プロジェクト: bdthombre/mmvt
def calc_scan_rescan_mean_diffs(subjects, do_plot_hist, overwrite):
    all_diffs = defaultdict(list)
    for subject in subjects:
        means_diff_fname = op.join(RESULTS_FOL, 'aparc_aseg_hists', subject,
                                   'aparc_values_diffs.pkl')
        if not op.isfile(means_diff_fname):
            print('No diffs file for {}!'.format(subject))
            continue
        diffs = utils.load(means_diff_fname)
        for region_name, val in diffs.items():
            all_diffs[region_name].append(val)
    region_names = all_diffs.keys()
    data_mean = np.array([
        np.abs(all_diffs[region_name]).mean() for region_name in region_names
    ])
    data_std = np.array(
        [np.abs(all_diffs[region_name]).std() for region_name in region_names])
    labels_names = [get_aparc_label_name(region) for region in region_names]

    figure_output_fname = op.join(RESULTS_FOL, 'aparc_aseg_hists',
                                  'labels_scan_rescan_diffs.jpg')
    if do_plot_hist and (not op.isfile(figure_output_fname) or overwrite):
        fig, ax = plt.subplots()
        x_pos = range(len(labels_names))
        ax.bar(x_pos,
               data_mean,
               align='center',
               alpha=0.5,
               ecolor='black',
               capsize=10)  # yerr=data_std / len(labels_names)
        plt.title('scan-rescan ASL mean diffs')
        plt.ylabel('ASL diff')
        plt.tight_layout()
        print('Saving bar plot in {}'.format(figure_output_fname))
        plt.savefig(figure_output_fname)
        plt.close()

    for data, oper in zip([data_mean, data_std], ['mean', 'std']):
        mmvt_file_name = 'ASL_scan_rescan_diffs_{}'.format(oper)
        mmvt_output_fname = op.join(
            utils.make_dir(
                op.join(MMVT_DIR, 'fsaverage', 'labels', 'labels_data')),
            '{}.npz'.format(mmvt_file_name))
        if op.isfile(mmvt_output_fname) and not overwrite:
            continue
        np.savez(mmvt_output_fname,
                 names=labels_names,
                 atlas='aparc',
                 data=data,
                 title=mmvt_file_name,
                 data_min=0,
                 data_max=data.max(),
                 cmap='YlOrRd')
コード例 #30
0
ファイル: dell_tests.py プロジェクト: waffle-iron/mmvt
def find_closest_points_on_cylinder(elc1, elc2, threshold, output_fol,
                                    error_radius):
    (electrodes, names, hemis, threshold) = utils.load(
        op.join(output_fol, '{}_electrodes.pkl'.format(int(threshold))))
    elc_ind1, elc_ind2 = names.index(elc1), names.index(elc2)
    _, cylinder, _ = fect.points_in_cylinder(electrodes[elc_ind1],
                                             electrodes[elc_ind2], electrodes,
                                             error_radius)
    points_inside_cylinder, too_close_points, dists, dists_to_cylinder = \
        fect.find_group_between_pair(elc_ind1, elc_ind2, electrodes, error_radius, min_distance)
    fect.find_closest_points_on_cylinder(electrodes, points_inside_cylinder,
                                         cylinder)
コード例 #31
0
ファイル: cics.py プロジェクト: bdthombre/mmvt
def _plot_cbf_histograms_parallel(p):
    subject, scan_rescan = p
    input_fname = op.join(MMVT_DIR, subject, 'ASL', scan_rescan,
                          'aparc_aseg_hist.pkl')
    output_fol = utils.make_dir(
        op.join(RESULTS_FOL, 'aparc_aseg_hists', subject, scan_rescan))
    if not op.isfile(input_fname):
        return False
    regions_values = utils.load(input_fname)
    for region_name, region_values in regions_values.values():
        fig_fname = op.join(output_fol, '{}.jpg'.format(region_name))
        plt.hist(region_values, bins=40)
        plt.savefig(fig_fname)
        plt.close()
コード例 #32
0
ファイル: meg_source_psd.py プロジェクト: keshava/mmvt
def _calc_source_ttest(subject):
    fol = op.join(MMVT_DIR, subject, 'meg')
    output_fname = op.join(fol, 'dSPM_mean_flip_vertices_power_spectrum_stat')
    if utils.both_hemi_files_exist('{}-{}.stc'.format(
            output_fname, '{hemi}')) and not args.overwrite:
        print('{} already exist'.format(output_fname))
        return True
    file_name = '{cond}_dSPM_mean_flip_vertices_power_spectrum.pkl'
    if not all([
            op.isfile(op.join(fol, file_name.format(cond=cond.lower())))
            for cond in MSIT_CONDS
    ]):
        print('No stc files for both conditions!')
        return False
    vertices_data = {}
    try:
        for cond in MSIT_CONDS:
            vertices_data[cond], freqs = utils.load(
                op.join(fol, file_name.format(cond=cond.lower())))
    except:
        print('Can\'t read {}'.format(file_name.format(cond=cond.lower())))
        return False
    pvals, vertno = {}, {}
    for hemi in utils.HEMIS:
        vertices_inds = {}
        pvals[hemi] = np.zeros(
            (len(vertices_data[MSIT_CONDS[0]][hemi].keys()), len(freqs)))
        for cond in MSIT_CONDS:
            vertices_inds[cond] = np.array(
                sorted(list(vertices_data[cond][hemi].keys())))
        if not np.all(
                vertices_inds[MSIT_CONDS[0]] == vertices_inds[MSIT_CONDS[1]]):
            raise Exception('Not the same vertices!')
        vertno[hemi] = vertices_inds[MSIT_CONDS[0]]
        params = [(vert_ind, vertices_data[MSIT_CONDS[0]][hemi][vert], vertices_data[MSIT_CONDS[1]][hemi][vert], len(freqs)) \
            for vert_ind, vert in enumerate(vertices_data[MSIT_CONDS[0]][hemi].keys())]
        results = utils.run_parallel(calc_vert_pvals, params, args.n_jobs)
        for vert_pvals, vert_ind in results:
            pvals[hemi][vert_ind, :] = vert_pvals

    data = np.concatenate([pvals['lh'], pvals['rh']])
    vertices = [vertno['lh'], vertno['rh']]
    stc_pvals = mne.SourceEstimate(data,
                                   vertices,
                                   freqs[0],
                                   freqs[1] - freqs[0],
                                   subject=subject)
    print('Writing to {}'.format(output_fname))
    stc_pvals.save(output_fname)
コード例 #33
0
ファイル: import_dipoles.py プロジェクト: keshava/mmvt
def calc_distances_from_rois(subject, dist_threshold=0.05):
    from scipy.spatial.distance import cdist
    import nibabel as nib
    dipoles_dict = utils.load(op.join(MMVT_DIR, subject, 'meg', 'dipoles.pkl'))
    labels_times_fol = op.join(MMVT_DIR, subject, 'meg', 'time_accumulate')
    labels = lu.read_labels(subject, SUBJECTS_DIR, 'laus125')
    labels_center_of_mass = lu.calc_center_of_mass(labels)
    labels_pos = np.array([labels_center_of_mass[l.name] for l in labels])
    labels_dict = {l.name: labels_center_of_mass[l.name] for l in labels}
    outer_skin_surf_fname = op.join(SUBJECTS_DIR, subject, 'surf', 'lh.seghead')
    outer_skin_surf_verts, _ = nib.freesurfer.read_geometry(outer_skin_surf_fname)

    for dipole_name, dipoles in dipoles_dict.items():
        dipole_pos = np.array([dipoles[0][2], dipoles[0][3], dipoles[0][4]])
        lables_times_fname = op.join(labels_times_fol, '{}_labels_times.txt'.format(dipole_name))
        if not op.isfile(lables_times_fname):
            print('Can\'t find {}!'.format(lables_times_fname))
            continue
        dists_from_outer_skin = np.min(cdist(outer_skin_surf_verts * 0.001, [dipole_pos]), 0)[0]
        output_fname = op.join(labels_times_fol, '{}_labels_times_dists.txt'.format(dipole_name))
        lines = utils.csv_file_reader(lables_times_fname, delimiter=':', skip_header=1)
        output, dists = [], []
        labels_dists = cdist(labels_pos, [dipole_pos])
        dists_argmin = np.argmin(labels_dists, 0)[0]
        dists_min = np.min(labels_dists, 0)[0]
        closest_label = labels[dists_argmin].name
        print('Parsing {} ({})'.format(dipole_name, closest_label))
        for line in lines:
            if len(line) == 0:
                continue
            elif len(line) != 2:
                print('{}: Problem parsing "{}"'.format(lables_times_fname, line))
                continue
            label_name, label_time = line
            label_pos = labels_dict.get(label_name, None)
            if label_pos is not None:
                dist_from_dipole = np.linalg.norm(dipole_pos - label_pos)
                dists.append(dist_from_dipole)
            else:
                dist_from_dipole = -1
                dists.append(np.nan)
            output.append('{}: {} ({:.4f})'.format(label_name, label_time, dist_from_dipole))
        for ind, dist in enumerate(dists):
            if dist < dist_threshold:
                output[ind] = '{} ***'.format(output[ind])
        title = '{}: {} {:.4f} dist from outer skin: {:.4f} '.format(
            dipole_name, closest_label, dists_min, dists_from_outer_skin)
        utils.save_arr_to_file(output, output_fname, title)
コード例 #34
0
ファイル: meg_source_psd.py プロジェクト: keshava/mmvt
def filter_pvals_fMRI_clusters(args):
    subjects = args.subject
    max_intersect = []
    stc_name = 'dSPM_mean_flip_vertices_power_spectrum_stat'
    for subject in subjects:
        clusters_root_fol = utils.make_dir(
            op.join(MMVT_DIR, subject, 'meg', 'clusters'))
        res_fname = op.join(
            clusters_root_fol,
            'clusters_labels_dSPM_mean_flip_vertices_power_spectrum_stat.pkl')
        min_vertices_num = 50
        min_sig = 2
        # labels = ['superiorfrontal', 'caudalmiddlefrontal']
        if op.isfile(res_fname):
            clusters_dict = utils.Bag(utils.load(res_fname))
            stc = mne.read_source_estimate(
                op.join(MMVT_DIR, subject, 'meg',
                        '{}-lh.stc'.format(stc_name)))
            uniqueness = len(
                np.where(
                    np.abs(stc.data[:, 0][1:] -
                           stc.data[:, 0][:-1]) > 0.1)[0]) / stc.data.size
            if uniqueness < 0.001:
                print('{}: uniqueness < 0.001! ({})'.format(
                    subject, uniqueness))
                continue
            cluster_freq = stc.times[clusters_dict['time']]
            if 1 < cluster_freq < 120:
                for cluster in clusters_dict.values:
                    for c in cluster['intersects']:
                        max_intersect.append((c['num'], subject, c['name'],
                                              clusters_dict['time'],
                                              '{:.2f}Hz'.format(cluster_freq)))
                    intersects = [(c['name'].split('_')[0], c['num'],
                                   (c['num'] / cluster['size']))
                                  for c in cluster['intersects']
                                  if c['num'] > min_vertices_num]
                    intersects = [
                        '{} ({}, {:.2f}%)'.format(l, num, prob * 100)
                        for l, num, prob in intersects
                    ]  # if l in labels]
                    if len(intersects) > 0 and cluster['max'] > min_sig:
                        print('*** {} ({}={:.2f}Hz): {}: (sig: {})'.format(
                            subject, clusters_dict['time'], cluster_freq,
                            intersects, cluster['max']))
    max_intersect = sorted(max_intersect)[::-1]
    print(' $$$ {}'.format(max_intersect[:5]))
コード例 #35
0
def trans_tal_coords(files, template='colin27', overwrite=False):
    output_fol = utils.make_dir(op.join(MMVT_DIR, template, 'rois_peaks'))
    output_fname = op.join(output_fol, 'rois.pkl')
    if not op.isfile(output_fname) or overwrite:
        rois = get_tal_coordaintes(files)
        utils.save(rois, output_fname)
    else:
        rois = utils.load(output_fname)
    for roi in rois.keys():
        csv_fname = op.join(output_fol, '{}_mni.csv'.format(roi))
        csv_tal_fname = op.join(output_fol, '{}_tal.csv'.format(roi))
        with open(csv_fname, 'w') as csv_file_mni, open(csv_tal_fname,
                                                        'w') as csv_file_tal:
            csv_mni_writer = csv.writer(csv_file_mni, delimiter=',')
            csv_tal_writer = csv.writer(csv_file_tal, delimiter=',')
            for mni, tal in zip(rois[roi]['mni'], rois[roi]['tal']):
                csv_mni_writer.writerow(mni)
                csv_tal_writer.writerow(tal)
コード例 #36
0
def post_script(args):
    from src.utils import figures_utils as fu
    from src.mmvt_addon import fMRI_panel as fmri
    from src.utils import utils

    subject_fol = op.join(su.get_mmvt_dir(), args.subject)
    figures_fol = op.join(subject_fol, 'figures')
    clusters_file_names, _ = fmri.get_clusters_files(subject_fol)
    clusters_names = [f for f in clusters_file_names if args.clusters_type in f]
    print('clusters_names: {}'.format(clusters_names))
    fmri_files_minmax_fname = op.join(subject_fol, 'fmri', 'fmri_files_minmax_cm.pkl')
    data_min, data_max, colors_map_name = utils.load(fmri_files_minmax_fname)
    for clusters_name, inflated, background_color in product(clusters_names, args.inflated, args.background_color):
        print('Combing figures for {}, inflated: {}, background color: {}'.format(
            clusters_name, inflated, background_color))
        perspectives_image_fname = fu.combine_four_brain_perspectives(
            figures_fol, inflated, args.dpi, background_color,
            clusters_name, args.inflated_ratio, True, args.overwrite)
        fu.combine_brain_with_color_bar(
            data_max, data_min, perspectives_image_fname, colors_map_name, args.overwrite, args.dpi,
            args.x_left_crop, args.x_right_crop, args.y_top_crop, args.y_buttom_crop,
            args.w_fac, args.h_fac, background_color)
コード例 #37
0
ファイル: calc_arc_evoked.py プロジェクト: pelednoam/mmvt
def plot_labels(subject, labels_names, title_dict):
    # import seaborn as sns
    from src.mmvt_addon import colors_utils as cu
    sns.set(style="darkgrid")
    sns.set(color_codes=True)
    healthy_data = defaultdict(dict)
    subject_data = defaultdict(dict)
    root_fol = op.join(BLENDER_ROOT_DIR, subject, 'meg_evoked_files')
    if not op.isfile(op.join(root_fol, 'all_data.pkl')):
        for hemi in utils.HEMIS:
            d = np.load(op.join(root_fol, 'healthy_labels_all_data_win_100_{}.npz'.format(hemi)))
            f = np.load(op.join(BLENDER_ROOT_DIR, subject, 'labels_data_{}.npz'.format(hemi)))
            for label_ind, label_name in enumerate(d['names']):
                if label_name in labels_names.keys():
                    for cond_id, cond_name in enumerate(d['conditions']):
                        healthy_data[cond_name][label_name] = d['data'][label_ind, :, cond_id, :]
                        subject_data[cond_name][label_name] = f['data'][label_ind, :, cond_id]
        T = f['data'].shape[1]
        utils.save((subject_data, healthy_data, T), op.join(root_fol, 'all_data.pkl'))
    else:
        subject_data, healthy_data, T = utils.load(op.join(root_fol, 'all_data.pkl'))
    colors = cu.boynton_colors
    utils.make_dir(op.join(BLENDER_ROOT_DIR, subject, 'pics'))
    x_axis = np.arange(-2000, T - 2000, 1000)
    x_labels = [str(int(t / 1000)) for t in x_axis]
    # x_labels[2] = '(Risk onset) 2'
    # x_labels[3] = '(Reward onset) 3'
    img_width, img_height = 1024, 768
    dpi = 200
    ylim = 1.6
    w, h = img_width/dpi, img_height/dpi
    for cond_name in healthy_data.keys():
        sns.plt.figure(figsize=(w, h), dpi=dpi)
        sns.plt.xticks(x_axis, x_labels)
        sns.plt.xlabel('Time (s)')
        sns.plt.title(title_dict[cond_name])
        sns.plt.subplots_adjust(bottom=0.14)
        # sns.set_style('white')
        sns.despine()

        labels = []
        color_ind = 0
        for label_name, label_real_name in labels_names.items():
            sns.tsplot(data=healthy_data[cond_name][label_name].T,
                time=np.arange(-2000, healthy_data[cond_name][label_name].shape[0] - 2000), color=colors[color_ind])
            labels.append('healthy {}'.format(label_real_name))
            color_ind += 1
        for label_name, label_real_name in labels_names.items():
            sns.tsplot(data=subject_data[cond_name][label_name].T,
                time=np.arange(-2000, subject_data[cond_name][label_name].shape[0] - 2000), color=colors[color_ind])
            labels.append('{} {}'.format(subject, label_real_name))
            color_ind += 1
        sns.plt.legend(labels)
        sns.plt.axvline(0, color='k', linestyle='--', lw=1)
        sns.plt.axvline(1000, color='k', linestyle='--', lw=1)
        sns.plt.text(-400, ylim * 0.8, 'Risk onset', rotation=90, fontsize=10)
        sns.plt.text(600, ylim * 0.83, 'Reward onset', rotation=90, fontsize=10)
        sns.plt.ylim([0, ylim])
        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)
コード例 #38
0
ファイル: make_movie.py プロジェクト: ofek-schechner/mmvt
def plot_graph(graph1_ax, data_to_show_in_graph, time_range, xticks, fol, fol2='', graph2_ax=None, xlabel='',
               ylabels=(), xticklabels=(), ylim=None, images=None, green_line=True):
    graph_data, graph_colors = utils.load(op.join(fol, 'data.pkl'))
    if fol2 != '' and op.isfile(op.join(fol2, 'data.pkl')):
        graph_data2, graph_colors2 = utils.load(op.join(fol2, 'data.pkl'))
        if len(graph_data.keys()) == 1 and len(graph_data2.keys()) == 1:
            graph2_data_item = list(graph_data2.keys())[0]
            graph_data['{}2'.format(graph2_data_item)] = graph_data2[graph2_data_item]
    axes = [graph1_ax]
    if graph2_ax:
        axes = [graph1_ax, graph2_ax]

    ind = 0
    from src.mmvt_addon import colors_utils as cu
    # colors = cu.get_distinct_colors(6) / 255# ['r', 'b', 'g']
    colors = ['r', 'b', 'g']
    for data_type, data_values in graph_data.items():
        if isinstance(data_values, numbers.Number):
            continue
        if data_type not in data_to_show_in_graph:
            continue
        ax = axes[ind]
        if ylabels:
            ylabel = data_type if len(ylabels) <= ind else ylabels[ind]
        else:
            ylabel = data_type
        ax.set_ylabel(ylabel, color=colors[ind] if graph2_ax else 'k')
        if graph2_ax:
            for tl in ax.get_yticklabels():
                tl.set_color(colors[ind])
        for k, values in data_values.items():
            if np.allclose(values, 0):
                continue
            color = colors[ind] if len(data_to_show_in_graph) == 2 else tuple(graph_colors[data_type][k])
            # todo: tuple doesn't have ndim, not sure what to do here
            # if graph_colors[data_type][k].ndim > 1:
            if data_type[-1] == '2':
                data_type = data_type[:-1]
            # color = graph_colors[data_type][k]
            # alpha = 0.2
            # dash = [5, 5] if ind == 1 else []
            # if color == (1.0, 1.0, 1.0):
            #     color = np.array(cu.name_to_rgb('orange')) / 255.0
            # ax.plot(time_range[1:-1:4], values, label=k, color=color, alpha=0.9, clip_on=False)#, dashes=dash)# color=tuple(graph_colors[data_type][k]))
            if len(time_range) > len(values):
                time_range = time_range[:len(values)]
            ax.plot(time_range, values, label=k, color=color,
                    alpha=0.9)  # , clip_on=False)#, dashes=dash)# color=tuple(graph_colors[data_type][k]))
        ind += 1

    graph1_ax.set_xlabel(xlabel)
    if not xticklabels is None:
        x_labels = xticks
        for xlable_time, xticklabel in xticklabels:
            if xlable_time in xticks:
                x_labels[x_labels.index(xlable_time)] = xticklabel
        graph1_ax.set_xticklabels(x_labels)

    graph1_ax.set_xlim([time_range[0], time_range[-1]])
    if graph2_ax:
        if ylim:
            ymin, ymax = ylim
        else:
            ymin1, ymax1 = graph1_ax.get_ylim()
            ymin2, ymax2 = graph2_ax.get_ylim()
            ymin = min([ymin1, ymin2])
            ymax = max([ymax1, ymax2])

        graph1_ax.set_ylim([ymin, ymax])
        graph2_ax.set_ylim([ymin, ymax])
    else:
        ymin, ymax = ylim if ylim else graph1_ax.get_ylim()
        graph1_ax.set_ylim([ymin, ymax])

    if green_line:
        t0 = get_t(images, 0, time_range)
        t_line, = graph1_ax.plot([t0, t0], [ymin, ymax], 'g-')
    else:
        t_line = None
    # plt.legend()
    return graph_data, graph_colors, t_line, ymin, ymax