def solve_labels_collision(subject, subjects_dir, atlas, backup_atlas, n_jobs=1):
    now = time.time()
    print('Read labels')
    labels = utils.read_labels_parallel(subject, subjects_dir, atlas, n_jobs)
    backup_labels_fol = op.join(subjects_dir, subject, 'label', backup_atlas)
    labels_fol = op.join(subjects_dir, subject, 'label', atlas)
    if op.isdir(backup_labels_fol):
        shutil.rmtree(backup_labels_fol)
    os.rename(labels_fol, backup_labels_fol)
    utils.make_dir(labels_fol)
    hemis_verts, labels_hemi, pia_verts = {}, {}, {}
    print('Read surface ({:.2f}s)'.format(time.time() - now))
    for hemi in HEMIS:
        surf_fname = op.join(subjects_dir, subject, 'surf', '{}.pial'.format(hemi))
        hemis_verts[hemi], _ = mne.surface.read_surface(surf_fname)
        labels_hemi[hemi] = [l for l in labels if l.hemi == hemi]
    print('Calc centroids ({:.2f}s)'.format(time.time() - now))
    centroids = calc_labels_centroids(labels_hemi, hemis_verts)
    for hemi in HEMIS:
        print('Calc vertices labeling for {} ({:.2f}s)'.format(hemi, time.time() - now))
        hemi_centroids_dist = cdist(hemis_verts[hemi], centroids[hemi])
        vertices_labels_indices = np.argmin(hemi_centroids_dist, axis=1)
        labels_hemi_chunks = utils.chunks(list(enumerate(labels_hemi[hemi])), len(labels_hemi[hemi]) / n_jobs)
        params = [(labels_hemi_chunk, atlas, vertices_labels_indices, hemis_verts, labels_fol) for labels_hemi_chunk in labels_hemi_chunks]
        print('Save labels for {} ({:.2f}s)'.format(hemi, time.time() - now))
        utils.run_parallel(_save_new_labels_parallel, params, n_jobs)
Example #2
0
def calc_vector_mean_cov_cl(ys, fol, hs_tr, measure, atlas, k_type='triangular', sim=False, overwrite=False, n_jobs=1):
    output_fname = op.join(fol, 'vector_mean_cov_cl{}_{}_{}_{}.npz'.format('_sim' if sim else '', k_type, measure, atlas))
    if op.isfile(output_fname) and not overwrite:
        d = np.load(output_fname)
        if np.any(np.array(d['hs_tr']) != np.array(hs_tr)):
            overwrite = True
    if not op.isfile(output_fname) or overwrite:
        # d = np.load(op.join(fol, 'vector_mean_var{}_{}.npz'.format('_sim' if sim else '', k_type)))
        # means_est = d['means'] #, d['hs_tr'], d['hs_ms']
        means_est = None
        mean_ll_cl, mean_pan_cl = np.zeros((len(hs_tr))), np.zeros((len(hs_tr)))
        cov_ll_cl, cov_pan_cl = np.zeros((len(hs_tr))), np.zeros((len(hs_tr)))

        h_chunks = utils.chunks(list(enumerate(hs_tr)), len(hs_tr) / n_jobs)
        params = [(ys, means_est, h_chunk, k_type) for h_chunk in h_chunks]
        results = utils.run_parallel(_calc_vector_mean_and_cov_cl_parallel, params, n_jobs)
        for chunk_mean_ll_cl, chunk_mean_pan_cl, chunk_cov_ll_cl, chunk_cov_pan_cl in results:
            for h_ind in chunk_mean_ll_cl.keys():
                mean_ll_cl[h_ind] = chunk_mean_ll_cl[h_ind]
                mean_pan_cl[h_ind] = chunk_mean_pan_cl[h_ind]
                cov_ll_cl[h_ind] = chunk_cov_ll_cl[h_ind]
                cov_pan_cl[h_ind] = chunk_cov_pan_cl[h_ind]
        print('Saving results in {}'.format(output_fname))
        np.savez(output_fname, mean_ll_cl=mean_ll_cl, mean_pan_cl=mean_pan_cl, cov_ll_cl=cov_ll_cl,
                 cov_pan_cl=cov_pan_cl, hs_tr=hs_tr)
Example #3
0
def est_mean_and_var(ys, names, hs_tr, hs_s, t_axis, fol, k_type='triangular', sim=False, overwrite=False,
                     specific_label='', n_jobs=1):
    output_fname = op.join(fol, 'mean_var{}_{}_{}-{}.npz'.format('_sim' if sim else '', k_type, hs_s[0], hs_s[-1]))
    if op.isfile(output_fname) and not overwrite:
        d = np.load(op.join(fol, 'mean_var_sim_{}.npz'.format(k_type)))
        if np.any(np.array(d['hs_tr']) != np.array(hs_tr)):
            overwrite = True
            print('The parameter hs_tr is not the same as in the saved file, recalculating.')
    W = len(hs_s)
    if not op.isfile(output_fname) or overwrite:
        # all_means, all_vars = np.zeros((ys.shape[0], W)), np.zeros((ys.shape[0], W))
        all_means = np.zeros((len(hs_tr), len(names), ys.shape[1]))
        all_vars = np.zeros((len(hs_tr), len(names), ys.shape[1]))

        h_chunks = utils.chunks(list(zip(hs_tr, hs_s, range(W))), W / n_jobs)
        params = [(ys, names, h_chunk, t_axis, fol, k_type, specific_label) for h_chunk in h_chunks]
        results = utils.run_parallel(_est_mean_and_var_parallel, params, n_jobs)
        # for chunk_means, chunk_vars in results:
        #     for (h_ind, mean), var in zip(chunk_means.items(), chunk_vars.values()):
        #         all_means[h_ind] = mean
        #         all_vars[h_ind] = var
        for (chunk_means, chunk_vars) in results:
            for h_ind, label_ind in chunk_means.keys():
                all_means[h_ind, label_ind, :] = chunk_means[(h_ind, label_ind)]
                all_vars[h_ind, label_ind, :] = chunk_vars[(h_ind, label_ind)]

        # for ind, (h_tr, h_s) in enumerate(zip(hs_tr, hs_s)):
        #     print('h: {}s'.format(h_s))
        #     all_means[ind] = est_vector_mean_ll(ys, h_tr, k_type)
        #     all_vars[ind] = est_vector_est_var_t(ys, all_means[ind], h_tr, k_type)
        np.savez(output_fname, means=all_means, vars=all_vars, hs_tr=hs_tr, hs_ms=hs_s)
def read_labels_from_folder(subject, subjects_dir, atlas, n_jobs):
    labels_files = glob.glob(op.join(subjects_dir, subject, 'label', atlas, '*.label'))
    files_chunks = utils.chunks(labels_files, len(labels_files) / n_jobs)
    results = utils.run_parallel(_read_labels_parallel, files_chunks, n_jobs)
    labels = []
    for labels_chunk in results:
        labels.extend(labels_chunk)
    return labels
Example #5
0
def calc_vector_mean_cl(ys, fol, hs_tr, k_type='triangular', sim=False, overwrite=False, n_jobs=1):
    output_fname = op.join(fol, 'vector_mean_cl{}_{}.npz'.format('_sim' if sim else '', k_type))
    if op.isfile(output_fname) and not overwrite:
        d = np.load(output_fname)
        if np.any(np.array(d['hs_tr']) != np.array(hs_tr)):
            overwrite = True
    if not op.isfile(output_fname) or overwrite:
        d = np.load(op.join(fol, 'vector_mean_var{}_{}.npz'.format('_sim' if sim else '', k_type)))
        means_est, vars_est, hs_tr, hs_ms = d['means'], d['vars'], d['hs_tr'], d['hs_ms']
        mean_cl, var_cl = np.zeros((len(hs_tr))), np.zeros((len(hs_tr)))

        h_chunks = utils.chunks(list(enumerate(hs_tr)), len(hs_tr) / n_jobs)
        params = [(ys, means_est, h_chunk, k_type) for h_chunk in h_chunks]
        results = utils.run_parallel(_calc_vector_mean_cl_parallel, params, n_jobs)
        for chunk_mean_cl in results:
            for h_ind in chunk_mean_cl.keys():
                mean_cl[h_ind] = chunk_mean_cl[h_ind]
        # for hs_ind, hs_tr in enumerate(hs_tr):
        #     mean_cl[hs_ind] = vector_mean_cl_stat(ys, means[hs_ind], hs_tr, k_type)
        np.savez(output_fname, mean_cl=mean_cl, var_cl=var_cl, hs_tr=hs_tr, hs_ms=hs_ms)
Example #6
0
def calc_mean_var_cl(ys, fol, hs_tr, hs_s, labels_names, k_type='triangular', sim=False, overwrite=False,
                     specific_label='', n_jobs=1):
    output_fname = op.join(fol, 'mean_var{}_cl_{}_{}-{}.npz'.format('_sim' if sim else '', k_type, hs_s[0], hs_s[-1]))
    if op.isfile(output_fname) and not overwrite:
        d = np.load(output_fname)
        if np.any(np.array(d['hs_tr']) != np.array(hs_tr)):
            overwrite = True
    if not op.isfile(output_fname) or overwrite:
        intpu_fname = op.join(fol, 'mean_var{}_{}_{}-{}.npz'.format('_sim' if sim else '', k_type, hs_s[0], hs_s[-1]))
        d = np.load(intpu_fname)
        means_est, vars_est, hs_tr, hs_ms = d['means'], d['vars'], d['hs_tr'], d['hs_ms']
        mean_cl, var_cl = np.zeros((ys.shape[0], len(hs_tr))), np.zeros((ys.shape[0], len(hs_tr)))
        params_to_chunk = []
        for (h_tr_ind, h_tr), (label_ind, label_name) in product(enumerate(hs_tr), enumerate(labels_names)):# range(ys.shape[0])):
            params_to_chunk.append((h_tr_ind, h_tr, label_ind, label_name))
        h_l_chunks = utils.chunks(params_to_chunk, len(params_to_chunk) / n_jobs)
        params = [(ys, means_est, vars_est, h_l_chunk, k_type, specific_label) for h_l_chunk in h_l_chunks]
        results = utils.run_parallel(_calc_mean_var_cl_parallel, params, n_jobs)
        for (chunk_mean_cl, chunk_var_cl) in results:
            for h_ind, label_ind in chunk_mean_cl.keys():
                mean_cl[label_ind, h_ind] = chunk_mean_cl[(h_ind, label_ind)]
                var_cl[label_ind, h_ind] = chunk_var_cl[(h_ind, label_ind)]
        np.savez(output_fname, mean_cl=mean_cl, var_cl=var_cl, hs_tr=hs_tr, hs_ms=hs_ms)