Exemple #1
0
def _run_func_in_parallel(parallel_params):
    func, params, check_if_should_run, thread_ind, verbose = parallel_params
    flags = []
    now = time.time()
    for run_num, p in enumerate(params):
        if not check_if_should_run:
            utils.time_to_go(now, run_num, len(params), 1, thread_ind)
        subject, site, subject_dir, scan_rescan = [
            p[key]
            for key in ['subject', 'site', 'subject_dir', 'scan_rescan']
        ]
        overwrite, print_only = [
            p.get(key, False) for key in ['overwrite', 'print_only']
        ]
        flag = False
        if func.__name__ == 'register_cbf_to_t1':
            flag = register_cbf_to_t1(subject, subject_dir, scan_rescan,
                                      overwrite, print_only,
                                      check_if_should_run, verbose)
        elif func.__name__ == 'calc_volume_fractions':
            flag = calc_volume_fractions(subject, subject_dir, scan_rescan,
                                         overwrite, print_only,
                                         check_if_should_run, verbose)
        elif func.__name__ == 'register_aseg_to_cbf':
            flag = register_aseg_to_cbf(subject, subject_dir, scan_rescan,
                                        overwrite, print_only,
                                        check_if_should_run, verbose)
        flags.append(flag)
    return flags
Exemple #2
0
def calc_electrodes_coh(subject, conditions, mat_fname, t_max, from_t_ind, to_t_ind, sfreq=1000, fmin=55, fmax=110, bw=15,
                        dt=0.1, window_len=0.1, n_jobs=6):

    from mne.connectivity import spectral_connectivity
    import time

    input_file = op.join(SUBJECTS_DIR, subject, 'electrodes', mat_fname)
    d = sio.loadmat(input_file)
    output_file = op.join(MMVT_DIR, subject, 'electrodes_coh.npy')
    windows = np.linspace(0, t_max - dt, t_max / dt)
    for cond, data in enumerate([d[cond] for cond in conditions]):
        if cond == 0:
            coh_mat = np.zeros((data.shape[1], data.shape[1], len(windows), 2))
            # coh_mat = np.load(output_file)
            # continue
        ds_data = downsample_data(data)
        ds_data = ds_data[:, :, from_t_ind:to_t_ind]
        now = time.time()
        for win, tmin in enumerate(windows):
            print('cond {}, tmin {}'.format(cond, tmin))
            utils.time_to_go(now, win + 1, len(windows))
            con_cnd, _, _, _, _ = spectral_connectivity(
                ds_data, method='coh', mode='multitaper', sfreq=sfreq,
                fmin=fmin, fmax=fmax, mt_adaptive=True, n_jobs=n_jobs, mt_bandwidth=bw, mt_low_bias=True,
                tmin=tmin, tmax=tmin + window_len)
            con_cnd = np.mean(con_cnd, axis=2)
            coh_mat[:, :, win, cond] = con_cnd
            # plt.matshow(con_cnd)
            # plt.show()
        np.save(output_file[:-4], coh_mat)
    return coh_mat
def transfer_electrodes_to_template_system(electrodes,
                                           template_system,
                                           use_mri_robust_lta=False,
                                           vox2vox=False):
    import time
    teamplte_electrodes = defaultdict(list)
    for subject in electrodes.keys():
        # if subject != 'mg101':
        #     continue
        now, N = time.time(), len(electrodes[subject])
        for run, (elc_name, coords) in enumerate(electrodes[subject]):
            utils.time_to_go(now, run, N, runs_num_to_print=5)
            if use_mri_robust_lta:
                if vox2vox:
                    template_cords = lta_transfer_vox2vox(subject, coords)
                else:
                    template_cords = lta_transfer_ras2ras(subject, coords)
            else:
                if template_system == 'ras':
                    template_cords = fu.transform_subject_to_ras_coordinates(
                        subject, coords, SUBJECTS_DIR)
                elif template_system == 'mni':
                    template_cords = fu.transform_subject_to_mni_coordinates(
                        subject, coords, SUBJECTS_DIR)
                else:
                    template_cords = fu.transform_subject_to_subject_coordinates(
                        subject, template_system, coords, SUBJECTS_DIR)
            if template_cords is not None:
                teamplte_electrodes[subject].append((elc_name, template_cords))
    return teamplte_electrodes
Exemple #4
0
def split_cerebellum_hemis(subject, mask_fname, new_maks_name, subregions_num):
    import time
    if op.isfile(new_maks_name):
        return subregions_num * 2
    mask = nib.load(mask_fname)
    mask_data = mask.get_data()
    aseg_data = nib.load(op.join(SUBJECTS_DIR, subject, 'mri', 'aseg.mgz')).get_data()
    new_mask_data = np.zeros(mask_data.shape)
    lookup = utils.read_freesurfer_lookup_table(return_dict=True)
    inv_lookup = {name:val for val,name in lookup.items()}
    right_cerebral = inv_lookup['Right-Cerebellum-Cortex']
    left_cerebral = inv_lookup['Left-Cerebellum-Cortex']
    now = time.time()
    for seg_id in range(1, subregions_num + 1):
        utils.time_to_go(now, seg_id-1, subregions_num, 1)
        seg_inds = np.where(mask_data == seg_id)
        aseg_segs = aseg_data[seg_inds]
        seg_inds = np.vstack((seg_inds[0], seg_inds[1], seg_inds[2]))
        right_inds = seg_inds[:, np.where(aseg_segs == right_cerebral)[0]]
        left_inds = seg_inds[:, np.where(aseg_segs == left_cerebral)[0]]
        new_mask_data[right_inds[0], right_inds[1], right_inds[2]] = seg_id
        new_mask_data[left_inds[0], left_inds[1], left_inds[2]] = seg_id + subregions_num
    new_mask = nib.Nifti1Image(new_mask_data, affine=mask.get_affine())
    print('Saving new mask to {}'.format(new_maks_name))
    nib.save(new_mask, new_maks_name)
    return subregions_num * 2
def calc_source_ttest(args):
    # utils.run_parallel(_morph_stcs_pvals, args.subject, args.n_jobs)
    import time
    subjects = args.subject
    now = time.time()
    for run, subject in enumerate(subjects):
        utils.time_to_go(now, run, len(subjects), 1)
        print('subject: {}'.format(subject))
        _calc_source_ttest(subject)
Exemple #6
0
    def update_img(image_index):
        # print(image_fname)
        utils.time_to_go(now, image_index, len(images))
        image = mpimg.imread(images[image_index])
        im.set_data(image)
        if im2:
            image2 = mpimg.imread(images2[image_index])
            im2.set_data(image2)

        current_t = get_t(images, image_index, time_range)
        t_line.set_data([current_t, current_t], [ymin, ymax])
        return [im]
Exemple #7
0
    def update_img(image_index):
        # print(image_fname)
        utils.time_to_go(now, image_index, len(images))
        image = mpimg.imread(images[image_index])
        im.set_data(image)
        if im2:
            image2 = mpimg.imread(images2[image_index])
            im2.set_data(image2)

        current_t = get_t(images, image_index, time_range)
        t_line.set_data([current_t, current_t], [ymin, ymax])
        return [im]
Exemple #8
0
def analyze_graphs(subject,
                   fif_files,
                   graph_func,
                   bands,
                   modality,
                   overwrite=False,
                   n_jobs=4):
    now = time.time()
    for run, fif_fname in enumerate(fif_files):
        utils.time_to_go(now, run, len(fif_files), runs_num_to_print=1)
        for band_name in bands.keys():
            analyze_graph(subject, fif_fname, band_name, graph_func, modality,
                          overwrite, n_jobs)
Exemple #9
0
def calc_closeness_centrality(p):
    con, times_chunk = p
    vals = []
    now = time.time()
    for run, t in enumerate(times_chunk):
        utils.time_to_go(now, run, len(times_chunk), 10)
        con_t = con[:, :, t]
        g = nx.from_numpy_matrix(con_t)
        # x = nx.closeness_centrality(g)
        x = nx.degree_centrality(g)
        vals.append([x[k] for k in range(len(x))])
    vals = np.array(vals)
    return vals, times_chunk
Exemple #10
0
def calc_electrodes_coh(subject,
                        conditions,
                        mat_fname,
                        t_max,
                        from_t_ind,
                        to_t_ind,
                        sfreq=1000,
                        fmin=55,
                        fmax=110,
                        bw=15,
                        dt=0.1,
                        window_len=0.1,
                        n_jobs=6):

    from mne.connectivity import spectral_connectivity
    import time

    input_file = op.join(SUBJECTS_DIR, subject, 'electrodes', mat_fname)
    d = sio.loadmat(input_file)
    output_file = op.join(BLENDER_ROOT_DIR, subject, 'electrodes_coh.npy')
    windows = np.linspace(0, t_max - dt, t_max / dt)
    for cond, data in enumerate([d[cond] for cond in conditions]):
        if cond == 0:
            coh_mat = np.zeros((data.shape[1], data.shape[1], len(windows), 2))
            # coh_mat = np.load(output_file)
            # continue
        ds_data = downsample_data(data)
        ds_data = ds_data[:, :, from_t_ind:to_t_ind]
        now = time.time()
        for win, tmin in enumerate(windows):
            print('cond {}, tmin {}'.format(cond, tmin))
            utils.time_to_go(now, win + 1, len(windows))
            con_cnd, _, _, _, _ = spectral_connectivity(ds_data,
                                                        method='coh',
                                                        mode='multitaper',
                                                        sfreq=sfreq,
                                                        fmin=fmin,
                                                        fmax=fmax,
                                                        mt_adaptive=True,
                                                        n_jobs=n_jobs,
                                                        mt_bandwidth=bw,
                                                        mt_low_bias=True,
                                                        tmin=tmin,
                                                        tmax=tmin + window_len)
            con_cnd = np.mean(con_cnd, axis=2)
            coh_mat[:, :, win, cond] = con_cnd
            # plt.matshow(con_cnd)
            # plt.show()
        np.save(output_file[:-4], coh_mat)
    return coh_mat
Exemple #11
0
    def update_img(image_index):
        # print(image_fname)
        utils.time_to_go(now, image_index, len(images))
        image = mpimg.imread(images[image_index])
        im.set_data(image)
        if im2:
            image2 = mpimg.imread(images2[image_index])
            im2.set_data(image2)

        current_t = get_t(images, image_index, time_range)
        if not current_t is None:
            t_line.set_data([current_t, current_t], [ymin, ymax])
            # print('Reading image {}, current t {}'.format(images[image_index], current_t))
            return [im]
        else:
            return None
Exemple #12
0
def calc_connectivity(subject,
                      atlas,
                      connectivity_method,
                      files,
                      bands,
                      modality,
                      overwrite=False,
                      n_jobs=4):
    conn_fol = op.join(MMVT_DIR, subject, 'connectivity')
    now = time.time()
    for run, fif_fname in enumerate(files):
        utils.time_to_go(now, run, len(files), runs_num_to_print=1)
        file_name = utils.namebase(fif_fname)
        labels_data_name = 'labels_data_{}-epilepsy-{}-{}-{}_{}_mean_flip_{}.npz'.format(
            subject, 'dSPM', modality, file_name, atlas, '{hemi}')
        if not utils.both_hemi_files_exist(
                op.join(MMVT_DIR, subject, modality, labels_data_name)):
            print('labels data does not exist for {}!'.format(file_name))
        for band_name, band_freqs in bands.items():
            output_fname = op.join(
                conn_fol,
                '{}_{}_{}_{}.npy'.format(modality, file_name, band_name,
                                         connectivity_method))
            if op.isfile(output_fname) and not overwrite:
                print('{} {} connectivity for {} already exist'.format(
                    connectivity_method, band_name, file_name))
                continue
            print('calc_meg_connectivity: {}'.format(band_name))
            con_args = connectivity.read_cmd_args(
                utils.Bag(
                    subject=subject,
                    atlas=atlas,
                    function='calc_lables_connectivity',
                    connectivity_modality=modality,
                    connectivity_method=connectivity_method,
                    labels_data_name=labels_data_name,
                    windows_length=500,
                    windows_shift=100,
                    identifier='{}_{}'.format(file_name, band_name),
                    fmin=band_freqs[0],
                    fmax=band_freqs[1],
                    sfreq=2035,  # clin MEG
                    recalc_connectivity=False,
                    save_mmvt_connectivity=True,
                    threshold_percentile=80,
                    n_jobs=n_jobs))
            connectivity.call_main(con_args)
Exemple #13
0
def compare_coh_windows(subject, task, conditions, electrodes, freqs=((8, 12), (12, 25), (25,55), (55,110)), do_plot=False):
    electrodes_coh = np.load(op.join(ELECTRODES_DIR, subject, task, 'electrodes_coh_windows.npy'))
    meg_electrodes_coh = np.load(op.join(ELECTRODES_DIR, subject, task, 'meg_electrodes_ts_coh_windows.npy'))
    figs_fol = op.join(MMVT_DIR, subject, 'figs', 'coh_windows')
    utils.make_dir(figs_fol)
    results = []
    for cond_id, cond in enumerate(conditions):
        now = time.time()
        for freq_id, freq in enumerate(freqs):
            freq = '{}-{}'.format(*freq)
            indices = list(utils.lower_rec_indices(electrodes_coh.shape[0]))
            for ind, (i, j) in enumerate(indices):
                utils.time_to_go(now, ind, len(indices))
                meg = meg_electrodes_coh[i, j, :, freq_id, cond_id][:22]
                elc = electrodes_coh[i, j, :, freq_id, cond_id][:22]

                elc_diff = np.max(elc) - np.min(elc)
                meg *= elc_diff / (np.max(meg) - np.min(meg))
                meg += np.mean(elc) - np.mean(meg)

                if sum(meg) > len(meg) * 0.99:
                    continue
                data_diff = meg - elc
                # data_diff = data_diff / max(data_diff)
                rms = np.sqrt(np.mean(np.power(data_diff, 2)))
                corr = np.corrcoef(meg, elc)[0, 1]
                results.append(dict(elc1=electrodes[i], elc2=electrodes[j], cond=cond, freq=freq, rms=rms, corr=corr))
                if False: #do_plot and electrodes[i]=='RPT7' and electrodes[j] == 'RPT5': #corr > 10 and rms < 3:
                    plt.figure()
                    plt.plot(meg, label='pred')
                    plt.plot(elc, label='elec')
                    plt.legend()
                    # plt.title('{}-{} {} {}'.format(electrodes[i], electrodes[j], freq, cond)) # (rms:{:.2f})
                    plt.savefig(op.join(figs_fol, '{:.2f}-{}-{}-{}-{}.jpg'.format(rms, electrodes[i], electrodes[j], freq, cond)))
                    plt.close()

    results_fname = op.join(figs_fol, 'results{}.csv'.format('_bipolar' if bipolar else ''))
    rmss, corrs = [], []
    with open(results_fname, 'w') as output_file:
        for res in results:
            output_file.write('{},{},{},{},{},{}\n'.format(
                res['elc1'], res['elc2'], res['cond'], res['freq'], res['rms'], res['corr']))
            rmss.append(res['rms'])
            corrs.append(res['corr'])
    rmss = np.array(rmss)
    corrs = np.array(corrs)
    pass
def compare_coh_windows(subject, task, conditions, electrodes, freqs=((8, 12), (12, 25), (25,55), (55,110)), do_plot=False):
    electrodes_coh = np.load(op.join(ELECTRODES_DIR, subject, task, 'electrodes_coh_windows.npy'))
    meg_electrodes_coh = np.load(op.join(ELECTRODES_DIR, subject, task, 'meg_electrodes_ts_coh_windows.npy'))
    figs_fol = op.join(MMVT_DIR, subject, 'figs', 'coh_windows')
    utils.make_dir(figs_fol)
    results = []
    for cond_id, cond in enumerate(conditions):
        now = time.time()
        for freq_id, freq in enumerate(freqs):
            freq = '{}-{}'.format(*freq)
            indices = list(utils.lower_rec_indices(electrodes_coh.shape[0]))
            for ind, (i, j) in enumerate(indices):
                utils.time_to_go(now, ind, len(indices))
                meg = meg_electrodes_coh[i, j, :, freq_id, cond_id][:22]
                elc = electrodes_coh[i, j, :, freq_id, cond_id][:22]

                elc_diff = np.max(elc) - np.min(elc)
                meg *= elc_diff / (np.max(meg) - np.min(meg))
                meg += np.mean(elc) - np.mean(meg)

                if sum(meg) > len(meg) * 0.99:
                    continue
                data_diff = meg - elc
                # data_diff = data_diff / max(data_diff)
                rms = np.sqrt(np.mean(np.power(data_diff, 2)))
                corr = np.corrcoef(meg, elc)[0, 1]
                results.append(dict(elc1=electrodes[i], elc2=electrodes[j], cond=cond, freq=freq, rms=rms, corr=corr))
                if electrodes[i]=='RAF6' and electrodes[j] == 'LOF4': #corr > 10 and rms < 3:
                    plt.figure()
                    plt.plot(meg, label='prediction')
                    plt.plot(elc, label='electrode')
                    plt.legend()
                    # plt.title('{}-{} {} {}'.format(electrodes[i], electrodes[j], freq, cond)) # (rms:{:.2f})
                    plt.savefig(op.join(figs_fol, '{:.2f}-{}-{}-{}-{}.jpg'.format(rms, electrodes[i], electrodes[j], freq, cond)))
                    plt.close()

    results_fname = op.join(figs_fol, 'results{}.csv'.format('_bipolar' if bipolar else ''))
    rmss, corrs = [], []
    with open(results_fname, 'w') as output_file:
        for res in results:
            output_file.write('{},{},{},{},{},{}\n'.format(
                res['elc1'], res['elc2'], res['cond'], res['freq'], res['rms'], res['corr']))
            rmss.append(res['rms'])
            corrs.append(res['corr'])
    rmss = np.array(rmss)
    corrs = np.array(corrs)
    pass
Exemple #15
0
def _calc_graph_func(p):
    con, times_chunk, graph_func = p
    vals = []
    now = time.time()
    for run, t in enumerate(times_chunk):
        utils.time_to_go(now, run, len(times_chunk), 10)
        con_t = con[:, :, t]
        g = nx.from_numpy_matrix(con_t)
        if graph_func == 'closeness_centrality':
            x = nx.closeness_centrality(g)
        elif graph_func == 'degree_centrality':
            x = nx.degree_centrality(g)
        elif graph_func == 'eigenvector_centrality':
            x = nx.eigenvector_centrality(g, max_iter=10000)
        elif graph_func == 'katz_centrality':
            x = nx.katz_centrality(g, max_iter=100000)
        else:
            raise Exception('Wrong graph func!')
        vals.append([x[k] for k in range(len(x))])
    vals = np.array(vals)
    return vals, times_chunk
def calc_electrodes_coh_windows(subject, input_fname, conditions, bipolar, meg_electordes_names, meg_electrodes_data, tmin=0, tmax=2.5, sfreq=1000,
                freqs=((8, 12), (12, 25), (25,55), (55,110)), bw=15, dt=0.1, window_len=0.2, n_jobs=6):
    output_file = op.join(ELECTRODES_DIR, subject, task, 'electrodes_coh_{}windows_{}.npy'.format('bipolar_' if bipolar else '', window_len))
    if input_fname[-3:] == 'mat':
        d = sio.loadmat(matlab_input_file)
        conds_data = [d[conditions[0]], d[conditions[1]]]
        electrodes = get_electrodes_names(subject, task)
    elif input_fname[-3:] == 'npz':
        d = np.load(input_fname)
        conds_data = d['data']
        conditions = d['conditions']
        electrodes = d['names'].tolist()
        pass

    indices_to_remove, electrodes_indices = electrodes_tp_remove(electrodes, meg_electordes_names)
    windows = np.linspace(tmin, tmax - dt, tmax / dt)
    for cond, data in enumerate(conds_data):
        data = scipy.delete(data, indices_to_remove, 1)
        data = data[:, electrodes_indices, :]
        data = downsample_data(data)
        data = data[:, :, :meg_electrodes_data.shape[2]]
        if cond == 0:
            coh_mat = np.zeros((data.shape[1], data.shape[1], len(windows), len(freqs), 2))
            # coh_mat = np.load(output_file)
            # continue
        now = time.time()
        for freq_ind, (fmin, fmax) in enumerate(freqs):
            for win, tmin in enumerate(windows):
                try:
                    print('cond {}, tmin {}'.format(cond, tmin))
                    utils.time_to_go(now, win + 1, len(windows))
                    con_cnd, _, _, _, _ = spectral_connectivity(
                        data, method='coh', mode='multitaper', sfreq=sfreq,
                        fmin=fmin, fmax=fmax, mt_adaptive=True, n_jobs=n_jobs, mt_bandwidth=bw, mt_low_bias=True,
                        tmin=tmin, tmax=tmin + window_len)
                    con_cnd = np.mean(con_cnd, axis=2)
                    coh_mat[:, :, win, freq_ind, cond] = con_cnd
                except:
                    print('Error with freq {} and win {}'.format(freq_ind, win))
    np.save(output_file[:-4], coh_mat)
Exemple #17
0
def calc_electrodes_coh_windows(subject, input_fname, conditions, bipolar, meg_electordes_names, meg_electrodes_data, tmin=0, tmax=2.5, sfreq=1000,
                freqs=((8, 12), (12, 25), (25,55), (55,110)), bw=15, dt=0.1, window_len=0.2, n_jobs=6):
    output_file = op.join(ELECTRODES_DIR, subject, task, 'electrodes_coh_{}windows_{}.npy'.format('bipolar_' if bipolar else '', window_len))
    if input_fname[-3:] == 'mat':
        d = sio.loadmat(matlab_input_file)
        conds_data = [d[conditions[0]], d[conditions[1]]]
        electrodes = get_electrodes_names(subject, task)
    elif input_fname[-3:] == 'npz':
        d = np.load(input_fname)
        conds_data = d['data']
        conditions = d['conditions']
        electrodes = d['names'].tolist()
        pass

    indices_to_remove, electrodes_indices = electrodes_tp_remove(electrodes, meg_electordes_names)
    windows = np.linspace(tmin, tmax - dt, tmax / dt)
    for cond, data in enumerate(conds_data):
        data = scipy.delete(data, indices_to_remove, 1)
        data = data[:, electrodes_indices, :]
        data = downsample_data(data)
        data = data[:, :, :meg_electrodes_data.shape[2]]
        if cond == 0:
            coh_mat = np.zeros((data.shape[1], data.shape[1], len(windows), len(freqs), 2))
            # coh_mat = np.load(output_file)
            # continue
        now = time.time()
        for freq_ind, (fmin, fmax) in enumerate(freqs):
            for win, tmin in enumerate(windows):
                try:
                    print('cond {}, tmin {}'.format(cond, tmin))
                    utils.time_to_go(now, win + 1, len(windows))
                    con_cnd, _, _, _, _ = spectral_connectivity(
                        data, method='coh', mode='multitaper', sfreq=sfreq,
                        fmin=fmin, fmax=fmax, mt_adaptive=True, n_jobs=n_jobs, mt_bandwidth=bw, mt_low_bias=True,
                        tmin=tmin, tmax=tmin + window_len)
                    con_cnd = np.mean(con_cnd, axis=2)
                    coh_mat[:, :, win, freq_ind, cond] = con_cnd
                except:
                    print('Error with freq {} and win {}'.format(freq_ind, win))
    np.save(output_file[:-4], coh_mat)
Exemple #18
0
def filter_meg_labels_ts(subject,
                         inv_method='dSPM',
                         em='mean_flip',
                         atlas='electrodes_labels',
                         low_freq_cut_off=10,
                         fs=1000,
                         do_plot=False,
                         n_jobs=4):
    folders = glob.glob(
        op.join(MMVT_DIR, subject, 'meg',
                '{}_*_*_Resting_eeg_meg_Demi_ar-epo'.format(subject)))
    now = time.time()
    for fol_ind, fol in enumerate(folders):
        utils.time_to_go(now, fol_ind, len(folders), runs_num_to_print=1)
        files = glob.glob(
            op.join(
                fol, 'labels_data_rest_{}_{}_{}_?h.npz'.format(
                    atlas, inv_method, em)))
        for fname in files:
            hemi = lu.get_hemi_from_name(utils.namebase(fname))
            d = utils.Bag(np.load(fname))
            L = d.data.shape[0]  # labels * time * epochs
            filter_data = np.empty(d.data.shape)
            params = [(d.data[label_ind], label_ind, fs, low_freq_cut_off,
                       do_plot) for label_ind in range(L)]
            results = utils.run_parallel(_filter_meg_label_ts_parallel, params,
                                         n_jobs)
            for filter_label_data, label_ind in results:
                filter_data[label_ind] = filter_label_data
            new_fname = op.join(
                fol, 'labels_data_rest_{}_{}_{}_filter_{}.npz'.format(
                    atlas, inv_method, em, hemi))
            np.savez(new_fname,
                     data=filter_data,
                     names=d.names,
                     conditions=d.conditions)
Exemple #19
0
def parcelate(subject,
              atlas,
              hemi,
              surface_type,
              vertices_labels_ids_lookup=None,
              overwrite_vertices_labels_lookup=False):
    output_fol = op.join(MMVT_DIR, subject, 'labels',
                         '{}.{}.{}'.format(atlas, surface_type, hemi))
    utils.make_dir(output_fol)
    vtx, fac = utils.read_ply_file(
        op.join(MMVT_DIR, subject, 'surf',
                '{}.{}.ply'.format(hemi, surface_type)))
    if vertices_labels_ids_lookup is None or overwrite_vertices_labels_lookup:
        vertices_labels_ids_lookup = lu.create_vertices_labels_lookup(
            subject, atlas, True, overwrite_vertices_labels_lookup)[hemi]
    labels = lu.read_labels(subject, SUBJECTS_DIR, atlas, hemi=hemi)
    if 'unknown-{}'.format(hemi) not in [l.name for l in labels]:
        labels.append(lu.Label([], name='unknown-{}'.format(hemi), hemi=hemi))

    nV = vtx.shape[0]
    nF = fac.shape[0]
    nL = len(labels)
    # print('The number of unique labels is {}'.format(nL))

    vtxL = [[] for _ in range(nL)]
    facL = [[] for _ in range(nL)]

    now = time.time()
    for f in range(nF):
        utils.time_to_go(now, f, nF, runs_num_to_print=50000)
        # Current face & labels
        Cfac = fac[f]
        Cidx = [vertices_labels_ids_lookup[vert_ind] for vert_ind in Cfac]
        # Depending on how many vertices of the current face
        # are in different labels, behave differently
        # nuCidx = len(np.unique(Cidx))
        # if nuCidx == 1: # If all vertices share same label
        # same_label = utils.all_items_equall(Cidx)
        # if same_label:
        if Cidx[0] == Cidx[1] == Cidx[2]:
            # Add the current face to the list of faces of the
            # respective label, and don't create new faces
            facL[Cidx[0]] += [Cfac.tolist()]
        else:  # If 2 or 3 vertices are in different labels
            # Create 3 new vertices at the midpoints of the 3 edges
            vtxCfac = vtx[Cfac]
            vtxnew = (vtxCfac + vtxCfac[[1, 2, 0]]) / 2
            vtx = np.concatenate((vtx, vtxnew))
            # Define 4 new faces, with care preserve normals (all CCW)
            facnew = [[Cfac[0], nV, nV + 2], [nV, Cfac[1], nV + 1],
                      [nV + 2, nV + 1, Cfac[2]], [nV, nV + 1, nV + 2]]
            # Update nV for the next loop
            nV = vtx.shape[0]
            # Add the new faces to their respective labels
            facL[Cidx[0]] += [facnew[0]]
            facL[Cidx[1]] += [facnew[1]]
            facL[Cidx[2]] += [facnew[2]]
            freq_Cidx = mode(Cidx)
            facL[freq_Cidx] += [facnew[3]]  # central face
    # Having defined new faces and assigned all faces to labels, now
    # select the vertices and redefine faces to use the new vertex indices
    # Also, create the file for the indices
    # fidx = fopen(sprintf('%s.index.csv', srfprefix), 'w');

    # params = []
    # for lab in range(nL):
    #     facL_lab = facL[lab]
    #     facL_lab_flat = utils.list_flatten(facL_lab)
    #     vidx = list(set(facL_lab_flat))
    #     vtxL_lab = vtx[vidx]
    #     params.append((facL_lab, vtxL_lab, vidx, nV, labels[lab].name, hemi, output_fol))
    # utils.run_parallel(writing_ply_files_parallel, params, njobs=n_jobs)
    #
    ret = True
    for lab in range(nL):
        ret = ret and writing_ply_files(subject, surface_type, lab, facL[lab],
                                        vtx, vtxL, labels, hemi, output_fol)
    return ret
Exemple #20
0
def calc_ieeg_connectivity(subject,
                           connectivity_method,
                           graph_func,
                           sfreq,
                           big_window_length,
                           windows_length,
                           windows_shift,
                           overwrite=False,
                           n_jobs=4):
    mmvt_root = op.join(MMVT_DIR, subject, 'electrodes')
    output_fol = utils.make_dir(
        op.join(mmvt_root, '{}_{}'.format(connectivity_method, graph_func)))
    files_dict = {}
    data_files = glob.glob(op.join(mmvt_root, 'electrodes_data_*.npy'))
    ictal_ids = sorted(
        list(set([utils.namebase(f).split('_')[2] for f in data_files])))
    now = time.time()
    for run, id in enumerate(ictal_ids):
        utils.time_to_go(now, run, len(ictal_ids), 1)
        data_fname = op.join(mmvt_root, 'electrodes_data_{}.npy'.format(id))
        files_dict[utils.namebase(data_fname)] = {
            'baseline': [],
            'ictal': [data_fname]
        }
        data = np.load(data_fname).squeeze()
        for band_name, (fmin, fmax) in bands.items():
            output_fname = op.join(
                output_fol,
                '{}_{}_{}_{}.npy'.format(utils.namebase(data_fname),
                                         connectivity_method, graph_func,
                                         band_name))
            if op.isfile(output_fname) and not overwrite:
                continue
            print(utils.namebase(output_fname))
            con = connectivity.calc_mi(data, windows_length, windows_shift,
                                       sfreq, fmin, fmax, n_jobs)
            graph_data = analyze_graph_data(con, n_jobs)
            np.save(output_fname, graph_data)

        baseline_fname = op.join(mmvt_root,
                                 'electrodes_baseline_{}.npy'.format(id))
        data = np.load(baseline_fname).squeeze()
        w = sfreq * big_window_length
        windows = connectivity.calc_windows(data.shape[1], w, w)
        for base_ind, w in enumerate(windows):
            for band_name, (fmin, fmax) in bands.items():
                output_fname = op.join(
                    output_fol,
                    '{}_{}_{}_{}_{}.npy'.format(utils.namebase(baseline_fname),
                                                connectivity_method,
                                                graph_func, base_ind,
                                                band_name))
                files_dict[utils.namebase(data_fname)]['baseline'].append(
                    output_fname)
                if op.isfile(output_fname) and not overwrite:
                    continue
                print(utils.namebase(output_fname))
                con = connectivity.calc_mi(data[:, w[0]:w[1]], windows_length,
                                           windows_shift, sfreq, fmin, fmax,
                                           n_jobs)
                graph_data = analyze_graph_data(con, n_jobs)
                np.save(output_fname, graph_data)
    return files_dict
from src.utils import utils
from src.utils import preproc_utils as pu
from src.preproc import anatomy as anat

SUBJECTS_DIR, MMVT_DIR, FREESURFER_HOME = pu.get_links()


def create_surf(subject, subcortical_code, subcortical_name):
    aseg = nib.load(op.join(SUBJECTS_DIR, subject, 'mri',
                            'aseg.mgz')).get_data()
    t1_header = nib.load(op.join(SUBJECTS_DIR, subject, 'mri',
                                 'T1.mgz')).header
    aseg[aseg != subcortical_code] = 255
    aseg[aseg == subcortical_code] = 10
    aseg = np.array(aseg, dtype=np.float)
    aseg_smooth = scipy.ndimage.gaussian_filter(aseg, sigma=1)
    verts_vox, faces, _, _ = measure.marching_cubes(aseg_smooth, 100)
    # Doesn't seem to fix the normals directions that should be out...
    faces = measure.correct_mesh_orientation(aseg_smooth, verts_vox, faces)
    verts = utils.apply_trans(t1_header.get_vox2ras_tkr(), verts_vox)
    fol = utils.make_dir(op.join(MMVT_DIR, subject, 'subcortical_test'))
    ply_file_name = op.join(fol, '{}.ply'.format(subcortical_name))
    utils.write_ply_file(verts, faces, ply_file_name, False)


if __name__ == '__main__':
    subs = anat.load_subcortical_lookup_table()
    now, N = time.time(), len(subs)
    for ind, (k, name) in enumerate(subs.items()):
        utils.time_to_go(now, ind, N, runs_num_to_print=1)
        create_surf('mg116', k, name)
Exemple #22
0
def post_meg_preproc(args):
    inv_method, em, atlas = 'dSPM', 'mean_flip', 'darpa_atlas'
    bands = dict(theta=[4, 8],
                 alpha=[8, 15],
                 beta=[15, 30],
                 gamma=[30, 55],
                 high_gamma=[65, 200])
    norm_times = (500, 2500)
    do_plot = False

    subjects = args.subject
    res_fol = utils.make_dir(
        op.join(utils.get_parent_fol(MMVT_DIR), 'msit-ecr'))
    subjects_with_results = {}
    labels = lu.read_labels(subjects[0], SUBJECTS_DIR, atlas)
    labels_names = [l.name for l in labels]
    labels_num = len(labels_names)
    epochs_max_num = 50
    template_brain = 'colin27'

    now = time.time()
    bands_power_mmvt_all = []
    for subject_ind, subject in enumerate(subjects):
        utils.time_to_go(now, subject_ind, len(subjects), runs_num_to_print=1)
        subjects_with_results[subject] = {}
        input_fol = utils.make_dir(
            op.join(MEG_DIR, subject, 'labels_induced_power'))
        plots_fol = utils.make_dir(op.join(input_fol, 'plots'))
        args.subject = subject
        bands_power_mmvt = {'rh': {}, 'lh': {}}
        for task_ind, task in enumerate(args.tasks):
            task = task.lower()
            input_fnames = glob.glob(
                op.join(
                    input_fol, '{}_*_{}_{}_induced_power.npz'.format(
                        task, inv_method, em)))
            if len(input_fnames) < 1:  # labels_num:
                print('No enough files for {} {}!'.format(subject, task))
                subjects_with_results[subject][task] = False
                continue
            # input_dname = ecr_caudalanteriorcingulate-lh_dSPM_mean_flip_induced_power
            # if not do_plot:
            #     continue
            bands_power = np.empty((len(bands), labels_num, epochs_max_num))
            for input_fname in input_fnames:
                d = utils.Bag(np.load(input_fname))  # label_name, atlas, data
                # label_power = np.empty((len(bands), epochs_num, T)) (5, 50, 3501)
                label_power, label_name = d.data, d.label_name
                # for band_ind in range(len(bands)):
                #     label_power[band_ind] /= label_power[band_ind][:, norm_times[0]:norm_times[1]].mean()
                label_ind = labels_names.index(label_name)
                hemi = labels[label_ind].hemi
                for band_ind, band in enumerate(bands.keys()):
                    label_power_norm = label_power[
                        band_ind][:, norm_times[0]:norm_times[1]].mean(
                            axis=1)[:epochs_max_num]
                    if len(label_power_norm) != epochs_max_num:
                        print('{} does have {} epochs!'.format(
                            input_fname, len(label_power_norm)))
                        break
                    bands_power[band_ind, label_ind] = label_power_norm
                    if band not in bands_power_mmvt[hemi]:
                        bands_power_mmvt[hemi][band] = np.empty(
                            (len(labels_names), label_power[band_ind].shape[1],
                             1, len(args.tasks)))
                    bands_power_mmvt[hemi][band][
                        label_ind, :, 0,
                        task_ind] = label_power[band_ind].mean(axis=0)
                fig_fname = op.join(plots_fol,
                                    'power_{}_{}.jpg'.format(label_name, task))
                if do_plot:  # not op.isfile(fig_fname) and
                    times = np.arange(
                        0,
                        label_power.shape[2]) if 'times' not in d else d.times
                    plot_label_power(label_power, times, label_name, bands,
                                     task, fig_fname)
            for band_ind, band in enumerate(bands.keys()):
                power_fname = op.join(
                    res_fol, subject, '{}_labels_{}_{}_{}_power.npz'.format(
                        task.lower(), inv_method, em, band))
                np.savez(power_fname,
                         data=np.array(bands_power[band_ind]),
                         names=labels_names)
            subjects_with_results[subject][task] = True

        if all(subjects_with_results[subject].values()):
            bands_power_mmvt_all.append(bands_power_mmvt)
        else:
            print('{} does not have both tasks data!'.format(subject))

    labels_data_template = op.join(MMVT_DIR, template_brain, 'meg',
                                   'labels_data_power_{}_{}_{}_{}_{}.npz'
                                   )  # task, atlas, extract_method, hemi
    for hemi in utils.HEMIS:
        for band_ind, band in enumerate(bands.keys()):
            power = np.array([x[hemi][band]
                              for x in bands_power_mmvt_all]).mean(axis=0)
            labels_output_fname = meg.get_labels_data_fname(
                labels_data_template, inv_method, band, atlas, em, hemi)
            utils.make_dir(utils.get_parent_fol(labels_output_fname))
            np.savez(labels_output_fname,
                     data=power,
                     names=labels_names,
                     conditions=args.tasks)

    have_all = len([
        subject for subject, results in subjects_with_results.items()
        if all(results.values())
    ])
    print('{}/{} with all files'.format(have_all, len(subjects)))
    print(subjects_with_results)