Example #1
0
def save_labels_from_vertices_lookup(subject,
                                     atlas,
                                     subjects_dir,
                                     mmvt_dir,
                                     surf_type='pial',
                                     read_labels_from_fol='',
                                     overwrite_vertices_labels_lookup=False,
                                     n_jobs=6):
    lookup = create_vertices_labels_lookup(
        subject,
        atlas,
        read_labels_from_fol=read_labels_from_fol,
        overwrite=overwrite_vertices_labels_lookup)
    labels_fol = op.join(subjects_dir, subject, 'label', atlas)
    surf = utils.load_surf(subject, mmvt_dir, subjects_dir)
    utils.delete_folder_files(labels_fol)
    ok = True
    for hemi in utils.HEMIS:
        labels_vertices = defaultdict(list)
        # surf_fname = op.join(subjects_dir, subject, 'surf', '{}.{}'.format(hemi, surf_type))
        # surf, _ = mne.surface.read_surface(surf_fname)
        for vertice, label in lookup[hemi].items():
            labels_vertices[label].append(vertice)
        chunks_indices = np.array_split(np.arange(len(labels_vertices)),
                                        n_jobs)
        labels_vertices_items = list(labels_vertices.items())
        chunks = [([labels_vertices_items[ind] for ind in chunk_indices],
                   subject, labels_vertices, surf, hemi, labels_fol)
                  for chunk_indices in chunks_indices]
        results = utils.run_parallel(_save_labels_from_vertices_lookup_hemi,
                                     chunks, n_jobs)
        ok = ok and all(results)
    return ok
Example #2
0
def subcortical_segmentation(subject, overwrite_subcortical_objs=False):
    # Must source freesurfer. You need to have write permissions on SUBJECTS_DIR
    script_fname = op.join(BRAINDER_SCRIPTS_DIR, 'aseg2srf')
    if not op.isfile(script_fname):
        raise Exception('The subcortical segmentation script is missing! {}'.format(script_fname))
    if not utils.is_exe(script_fname):
        utils.set_exe_permissions(script_fname)

    # aseg2srf: For every subcortical region (8 10 11 12 13 16 17 18 26 47 49 50 51 52 53 54 58):
    # 1) mri_pretess: Changes region segmentation so that the neighbors of all voxels have a face in common
    # 2) mri_tessellate: Creates surface by tessellating
    # 3) mris_smooth: Smooth the new surface
    # 4) mris_convert: Convert the new surface into srf format

    function_output_fol = op.join(SUBJECTS_DIR, subject, 'mmvt', 'subcortical_objs')
    renamed_output_fol = op.join(SUBJECTS_DIR, subject, 'mmvt', 'subcortical')
    lookup = load_subcortical_lookup_table()
    obj_files = glob.glob(op.join(function_output_fol, '*.srf'))
    if len(obj_files) < len(lookup) or overwrite_subcortical_objs:
        utils.delete_folder_files(function_output_fol)
        # utils.delete_folder_files(aseg_to_srf_output_fol)
        utils.delete_folder_files(renamed_output_fol)
        print('Trying to write into {}'.format(function_output_fol))
        utils.run_script(ASEG_TO_SRF.format(subject))
        # os.rename(aseg_to_srf_output_fol, function_output_fol)
    ply_files = glob.glob(op.join(renamed_output_fol, '*.ply'))
    if len(ply_files) < len(lookup) or overwrite_subcortical_objs:
        convert_and_rename_subcortical_files(subject, function_output_fol, renamed_output_fol, lookup)
    flag_ok = len(glob.glob(op.join(renamed_output_fol, '*.ply'))) == len(lookup) and \
        len(glob.glob(op.join(renamed_output_fol, '*.npz'))) == len(lookup)
    return flag_ok
Example #3
0
def _calc_pvals_fMRI_clusters(p, extract_time_series_for_clusters=False):
    subject, overwrite = p
    stc_name = 'dSPM_mean_flip_vertices_power_spectrum_stat'
    if not utils.both_hemi_files_exist(
            op.join(MMVT_DIR, subject, 'meg', '{}-{}.stc'.format(
                stc_name, '{hemi}'))):
        print('{}: Can\'t find {}!'.format(subject, stc_name))
        return False
    args.subject = subject
    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')
    if not op.isfile(res_fname) or args.overwrite:
        utils.delete_folder_files(clusters_root_fol)
        _args = meg.read_cmd_args(
            dict(subject=subject,
                 mri_subject=subject,
                 atlas='MSIT_I-C',
                 function='find_functional_rois_in_stc',
                 stc_name=stc_name,
                 threshold=-np.log10(0.01),
                 threshold_is_precentile=False,
                 extract_time_series_for_clusters=False,
                 save_func_labels=True,
                 calc_cluster_contours=True,
                 n_jobs=args.n_jobs))
        try:
            meg.call_main(_args)
        except:
            print(traceback.format_exc())
        if not op.isfile(res_fname):
            print('Cluster output can\'t be found!')
            return False
Example #4
0
def change_frames_names(fol,
                        images_prefix,
                        images_type,
                        images_format_len,
                        new_fol_name='new_images'):
    import shutil
    images = glob.glob(
        op.join(fol, '{}*.{}'.format(images_prefix, images_type)))
    images.sort(key=lambda x: int(utils.namebase(x)[len(images_prefix):]))
    images_formats = {
        1: '{0:0>1}',
        2: '{0:0>2}',
        3: '{0:0>3}',
        4: '{0:0>4}',
        5: '{0:0>5}'
    }
    root = op.join(op.sep.join(images[0].split(op.sep)[:-1]))
    new_fol = op.join(root, new_fol_name)
    utils.delete_folder_files(new_fol)
    utils.make_dir(new_fol)
    for num, image_fname in enumerate(images):
        num_str = images_formats[images_format_len].format(num + 1)
        new_image_fname = op.join(
            new_fol, '{}{}.{}'.format(images_prefix, num_str, images_type))
        print('{} -> {}'.format(image_fname, new_image_fname))
        utils.copy_file(image_fname, new_image_fname)
    return new_fol
Example #5
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)
Example #6
0
def calc_msit_functional_rois(args):
    clusters_root_fol = utils.make_dir(
        op.join(MMVT_DIR, args.subject[0], 'meg', 'clusters'))
    utils.delete_folder_files(clusters_root_fol)
    # conditions = ['neutral', 'interference']
    # for cond in conditions:
    _args = meg.read_cmd_args(
        dict(
            subject=args.subject,
            mri_subject=args.mri_subject,
            task='MSIT',
            data_per_task=True,
            # atlas='laus125',
            function='find_functional_rois_in_stc',
            inverse_method='dSPM',
            stc_name=
            '{subject}_msit_nTSSS_interference_interference-neutral_1-15-dSPM',
            inv_fname='{subject}_msit_nTSSS_interference_1-15-inv',
            label_name_template='*',
            peak_mode='pos',
            threshold=99.5,
            min_cluster_max=5,
            min_cluster_size=100,
            # recreate_src_spacing='ico5'
            # clusters_label='precentral'
        ))
    meg.call_main(_args)
Example #7
0
def create_dup_frames_links(subject, dup_frames, fol):
    fol = op.join(MMVT_DIR, subject, 'figures', fol)
    utils.delete_folder_files(fol)
    utils.make_dir(fol)
    for ind, frame in enumerate(dup_frames):
        utils.make_link(
            frame, op.join(fol, 'dup_{}.{}'.format(ind,
                                                   utils.file_type(frame))))
    return fol
def _mri_cvs_register_parallel(p):
    subjects, subject_to, subjects_dir, overwrite, print_only = p
    for subject_from in subjects:
        if overwrite and not print_only:
            utils.delete_folder_files(
                op.join(subjects_dir, subject_from,
                        'mri_cvs_register_to_{}'.format(subject_to)))
        rs = utils.partial_run_script(locals(), print_only=print_only)
        rs(mri_cvs_register)
Example #9
0
def rename_cortical(lookup, fol, new_fol):
    ply_files = glob.glob(op.join(fol, '*.ply'))
    utils.delete_folder_files(new_fol)
    for ply_file in ply_files:
        base_name = op.basename(ply_file)
        num = int(base_name.split('.')[-2])
        hemi = base_name.split('.')[0]
        name = lookup[hemi].get(num, num)
        new_name = '{}-{}'.format(name, hemi)
        shutil.copy(ply_file, op.join(new_fol, '{}.ply'.format(new_name)))
Example #10
0
def rename_cortical(lookup, fol, new_fol):
    ply_files = glob.glob(op.join(fol, '*.ply'))
    utils.delete_folder_files(new_fol)
    for ply_file in ply_files:
        base_name = op.basename(ply_file)
        num = int(base_name.split('.')[-2])
        hemi = base_name.split('.')[0]
        name = lookup[hemi].get(num, num)
        new_name = '{}-{}'.format(name, hemi)
        shutil.copy(ply_file, op.join(new_fol, '{}.ply'.format(new_name)))
Example #11
0
def convert_and_rename_subcortical_files(subject, fol, new_fol, lookup, mmvt_subcorticals_fol_name='subcortical'):
    obj_files = glob.glob(op.join(fol, '*.srf'))
    utils.delete_folder_files(new_fol)
    for obj_file in obj_files:
        num = int(op.basename(obj_file)[:-4].split('_')[-1])
        new_name = lookup.get(num, '')
        if new_name != '':
            utils.srf2ply(obj_file, op.join(new_fol, '{}.ply'.format(new_name)))
            verts, faces = utils.read_ply_file(op.join(new_fol, '{}.ply'.format(new_name)))
            np.savez(op.join(new_fol, '{}.npz'.format(new_name)), verts=verts, faces=faces)
    copy_subcorticals_to_mmvt(new_fol, subject, mmvt_subcorticals_fol_name)
Example #12
0
def convert_and_rename_subcortical_files(subject, fol, new_fol, lookup):
    obj_files = glob.glob(op.join(fol, '*.srf'))
    utils.delete_folder_files(new_fol)
    for obj_file in obj_files:
        num = int(op.basename(obj_file)[:-4].split('_')[-1])
        new_name = lookup.get(num, '')
        if new_name != '':
            utils.srf2ply(obj_file, op.join(new_fol, '{}.ply'.format(new_name)))
            verts, faces = utils.read_ply_file(op.join(new_fol, '{}.ply'.format(new_name)))
            np.savez(op.join(new_fol, '{}.npz'.format(new_name)), verts=verts, faces=faces)
        copy_subcorticals_to_blender(new_fol, subject)
Example #13
0
def duplicate_frames(fol, multiplier=50, pics_type='png'):
    import shutil
    pics = get_pics(fol, pics_type)
    new_fol = '{}_dup'.format(fol)
    utils.delete_folder_files(new_fol)
    pic_ind = 0
    shutil.copy(op.join(fol, 'data.pkl'), op.join(new_fol, 'data.pkl'))
    for t, pic in enumerate(pics):
        for _ in range(multiplier):
            new_pic_name = op.join(new_fol, '{}_t{}.{}'.format(pic_ind, t, pics_type))
            shutil.copy(pic, new_pic_name)
            pic_ind += 1
Example #14
0
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))
Example #15
0
def duplicate_frames(fol, multiplier=50, pics_type='png'):
    import shutil
    pics = get_pics(fol, pics_type)
    new_fol = '{}_dup'.format(fol)
    utils.delete_folder_files(new_fol)
    pic_ind = 0
    utils.copy_file(op.join(fol, 'data.pkl'), op.join(new_fol, 'data.pkl'))
    for t, pic in enumerate(pics):
        for _ in range(multiplier):
            new_pic_name = op.join(new_fol, '{}_t{}.{}'.format(pic_ind, t, pics_type))
            utils.copy_file(pic, new_pic_name)
            pic_ind += 1
Example #16
0
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)
Example #17
0
def convert_and_rename_subcortical_files(subject, fol, new_fol, lookup):
    obj_files = glob.glob(op.join(fol, '*.srf'))
    utils.delete_folder_files(new_fol)
    for obj_file in obj_files:
        num = int(op.basename(obj_file)[:-4].split('_')[-1])
        new_name = lookup.get(num, '')
        if new_name != '':
            utils.srf2ply(obj_file, op.join(new_fol, '{}.ply'.format(new_name)))
            verts, faces = utils.read_ply_file(op.join(new_fol, '{}.ply'.format(new_name)))
            np.savez(op.join(new_fol, '{}.npz'.format(new_name)), verts=verts, faces=faces)
    blender_fol = op.join(MMVT_DIR, subject, 'subcortical')
    if op.isdir(blender_fol):
        shutil.rmtree(blender_fol)
    shutil.copytree(new_fol, blender_fol)
Example #18
0
def find_functional_rois(
        subject, ictal_clips, modality, seizure_times, atlas, min_cluster_size, inverse_method, mean_baseline,
        windows_length=0.1, windows_shift=0.05, overwrite=False, n_jobs=4):
    modality_fol = op.join(MMVT_DIR, subject, meg.modality_fol(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'))
    windows = calc_windows(seizure_times, windows_length, windows_shift)
    params = []
    for ictal_clip in ictal_clips:
        for from_t, to_t in windows:
            params.append((subject, modality, ictal_clip, atlas, inverse_method, mean_baseline, from_t, to_t,
                           min_cluster_size, connectivity))
    utils.run_parallel(_find_functional_rois_parallel, params, n_jobs)
Example #19
0
def find_diff_clusters(subject, atlas='laus125', overwrite=True):
    clusters_name = 'CBF_scan_rescan'
    if overwrite:
        utils.delete_folder_files(op.join(
            MMVT_DIR, subject, 'fmri',
            'clusters_labels_{}_{}'.format(clusters_name, atlas)),
                                  delete_folder=True)
        utils.delete_file(
            op.join(MMVT_DIR, subject, 'fmri',
                    'clusters_labels_{}_{}.pkl'.format(clusters_name, atlas)))
    fMRI.find_clusters(subject,
                       'CBF_scan_rescan',
                       2,
                       'laus125',
                       2,
                       1,
                       create_clusters_labels=True,
                       new_atlas_name='CBF_scan_rescan')
def _mri_cvs_register_parallel(p):
    subjects, subject_to, subjects_dir, overwrite, print_only = p
    for subject_from in subjects:
        # output_fname = op.join(SUBJECTS_DIR, subject_from, 'mri_cvs_register_to_colin27', 'combined_tocolin27_elreg_afteraseg-norm.tm3d')
        # if op.isfile(output_fname) and not overwrite:
        #     print('Already done for {}'.format(subject_from))
        #     continue
        # else:
        #     print('Running mri_cvs_register for {}'.format(subject_from))
        if overwrite and not print_only:
            utils.delete_folder_files(
                op.join(subjects_dir, subject_from,
                        'mri_cvs_register_to_{}'.format(subject_to)))
        rs = utils.partial_run_script(locals(), print_only=print_only)
        if subject_to == 'fsaverage':
            rs(mri_cvs_register_mni)
        else:
            rs(mri_cvs_register)
Example #21
0
def calc_contoures(subject,
                   fmri_names,
                   thresholds_min=2,
                   thresholds_max=None,
                   thresholds_dx=2,
                   n_jobs=4):
    utils.delete_folder_files(op.join(MMVT_DIR, subject, 'fmri', 'clusters'))
    if thresholds_max is None:
        thresholds_max = thresholds_min
    for fmri_name in fmri_names:
        fmri.contrast_to_contours(subject,
                                  fmri_name,
                                  thresholds_min,
                                  thresholds_max,
                                  thresholds_dx,
                                  min_cluster_size=10,
                                  find_clusters_overlapped_labeles=True,
                                  atlas='aparc.DKTatlas',
                                  n_jobs=n_jobs)
def calc_functional_rois(conds, args):
    clusters_root_fol = op.join(MMVT_DIR, subject, 'meg', 'clusters')
    utils.delete_folder_files(clusters_root_fol)
    for cond in conds.keys():
        _args = meg.read_cmd_args(
            dict(
                subject=args.subject,
                mri_subject=args.mri_subject,
                atlas='laus250',
                function='find_functional_rois_in_stc',
                inverse_method='MNE',
                stc_name='{}-MNE-1-15'.format(cond),
                label_name_template='precentral*',
                inv_fname='{}-inv'.format(cond),
                threshold=99.5,
                min_cluster_max=0.2,
                min_cluster_size=100,
                # clusters_label='precentral'
            ))
        meg.call_main(_args)
Example #23
0
def recon_all_clin(args):
    # python -m src.preproc.examples.anatomy -s nmr01426 -f recon_all_clin --clin_fol clin_6966926 --dicoms_fol Prisma_fit-67026-20200618-141203-000586
    import os
    for subject, clin_fol, dicoms_fol in zip(args.subject, args.clin_fol,
                                             args.dicoms_fol):
        clin_full_fol = utils.make_dir(
            op.join(args.clin_root, clin_fol, 'mne_dicom'))
        memprage_fols = glob.glob(op.join(clin_full_fol, '*MEMPRAGE*'))
        print('mne_organize_dicom output fol: {}'.format(clin_full_fol))
        if len(memprage_fols) > 0:
            ret = au.is_true(
                input(
                    'It seems like you already have memprage folders, are you sure you want to rerun?'
                ))
            if not ret:
                continue
            utils.delete_folder_files(clin_full_fol)
        fs_dir = utils.make_dir(op.join(args.fs_root, subject))
        print('FreeSurfer output fol: {}'.format(fs_dir))
        dicoms_full_path = op.join(args.dicoms_root, dicoms_fol)
        if not op.isdir(dicoms_full_path):
            print('{} does not exist!'.format(dicoms_full_path))
            continue
        rs = utils.partial_run_script(locals(), print_only=args.print_only)
        os.chdir(clin_full_fol)
        rs('mne_organize_dicom {dicoms_full_path}')
        anat.recon_all(subject,
                       clin_full_fol,
                       overwrite=True,
                       subjects_dir=args.fs_root,
                       print_only=False,
                       n_jobs=args.n_jobs)
        args = anat.read_cmd_args(
            dict(
                subject=subject,
                function='all,create_skull_surfaces',
                remote_subject_dir=op.join(args.fs_root, subject),
                n_jobs=args.n_jobs,
            ))
        pu.run_on_subjects(args, anat.main)
Example #24
0
def subcortical_segmentation(subject, overwrite_subcorticals=False, model='subcortical', lookup=None,
                             mask_name='aseg.mgz', mmvt_subcorticals_fol_name='subcortical',
                             template_subject='', norm_name='norm.mgz', overwrite=True):
    # 1) mri_pretess: Changes region segmentation so that the neighbors of all voxels have a face in common
    # 2) mri_tessellate: Creates surface by tessellating
    # 3) mris_smooth: Smooth the new surface
    # 4) mris_convert: Convert the new surface into srf format

    template_subject = subject if template_subject == '' else template_subject
    norm_fname = op.join(SUBJECTS_DIR, template_subject, 'mri', norm_name)
    if not op.isfile(norm_fname):
        print('norm file does not exist! {}'.format(norm_fname))
        return False

    mask_fname = op.join(SUBJECTS_DIR, template_subject, 'mri', mask_name)
    if not op.isfile(mask_fname):
        print('mask file does not exist! {}'.format(mask_fname))
        return False

    codes_file = op.join(MMVT_DIR, 'sub_cortical_codes.txt')
    if not op.isfile(codes_file):
        print('subcortical codes file does not exist! {}'.format(codes_file))
        return False

    # subcortical_lookup = np.genfromtxt(codes_file, dtype=str, delimiter=',')
    function_output_fol = op.join(SUBJECTS_DIR, subject, 'mmvt', '{}_objs'.format(model))
    utils.make_dir(function_output_fol)
    renamed_output_fol = op.join(SUBJECTS_DIR, subject, 'mmvt', model)
    utils.make_dir(renamed_output_fol)
    if lookup is None:
        lookup = load_subcortical_lookup_table()

    obj_files = glob.glob(op.join(function_output_fol, '*.srf'))
    errors = []
    if len(obj_files) < len(lookup) or overwrite_subcorticals:
        if overwrite:
            utils.delete_folder_files(function_output_fol)
            utils.delete_folder_files(renamed_output_fol)
        print('Trying to write into {}'.format(function_output_fol))
        for region_id in lookup.keys():
            if op.isfile(op.join(function_output_fol, '{}.srf'.format(region_id))):
                continue
            ret = fu.aseg_to_srf(subject, SUBJECTS_DIR, function_output_fol, region_id, mask_fname, norm_fname,
                           overwrite_subcorticals)
            if not ret:
                errors.append(lookup[region_id])
    if len(errors) > 0:
        print('Errors: {}'.format(','.join(errors)))
    ply_files = glob.glob(op.join(renamed_output_fol, '*.ply'))
    if len(ply_files) < len(lookup) or overwrite_subcorticals:
        convert_and_rename_subcortical_files(subject, function_output_fol, renamed_output_fol, lookup,
                                             mmvt_subcorticals_fol_name)
    blender_dir = op.join(MMVT_DIR, subject, mmvt_subcorticals_fol_name)
    if not op.isdir(blender_dir) or len(glob.glob(op.join(blender_dir, '*.ply'))) < len(ply_files) or overwrite_subcorticals:
        utils.delete_folder_files(blender_dir)
        copy_subcorticals_to_mmvt(renamed_output_fol, subject, mmvt_subcorticals_fol_name)
    flag_ok = len(glob.glob(op.join(blender_dir, '*.ply'))) >= len(lookup) and \
        len(glob.glob(op.join(blender_dir, '*.npz'))) >= len(lookup)
    return flag_ok
Example #25
0
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))
Example #26
0
def save_labels_from_vertices_lookup(subject,
                                     atlas,
                                     subjects_dir,
                                     surf_type='pial',
                                     read_labels_from_fol=''):
    # todo: Doens't work on DC, laus250. check that out
    lookup = create_vertices_labels_lookup(
        subject, atlas, read_labels_from_fol=read_labels_from_fol)
    labels_fol = op.join(subjects_dir, subject, 'label', atlas)
    utils.delete_folder_files(labels_fol)
    for hemi in utils.HEMIS:
        labels_vertices = defaultdict(list)
        surf_fname = op.join(subjects_dir, subject, 'surf',
                             '{}.{}'.format(hemi, surf_type))
        surf, _ = mne.surface.read_surface(surf_fname)
        for vertice, label in lookup[hemi].items():
            labels_vertices[label].append(vertice)
        for label, vertices in labels_vertices.items():
            new_label = mne.Label(sorted(vertices),
                                  surf[vertices],
                                  hemi=hemi,
                                  name=label,
                                  subject=subject)
            new_label.save(op.join(labels_fol, label))
Example #27
0
def subcortical_segmentation(subject, overwrite_subcortical_objs=False):
    # Must source freesurfer. You need to have write permissions on SUBJECTS_DIR
    if not op.isfile(op.join(SUBJECTS_DIR, subject, 'mri', 'norm.mgz')):
        return False
    script_fname = op.join(BRAINDER_SCRIPTS_DIR, 'aseg2srf')
    if not op.isfile(script_fname):
        raise Exception('The subcortical segmentation script is missing! {}'.format(script_fname))
    if not utils.is_exe(script_fname):
        utils.set_exe_permissions(script_fname)

    # aseg2srf: For every subcortical region (8 10 11 12 13 16 17 18 26 47 49 50 51 52 53 54 58):
    # 1) mri_pretess: Changes region segmentation so that the neighbors of all voxels have a face in common
    # 2) mri_tessellate: Creates surface by tessellating
    # 3) mris_smooth: Smooth the new surface
    # 4) mris_convert: Convert the new surface into srf format

    function_output_fol = op.join(SUBJECTS_DIR, subject, 'mmvt', 'subcortical_objs')
    renamed_output_fol = op.join(SUBJECTS_DIR, subject, 'mmvt', 'subcortical')
    lookup = load_subcortical_lookup_table()
    obj_files = glob.glob(op.join(function_output_fol, '*.srf'))
    if len(obj_files) < len(lookup) or overwrite_subcortical_objs:
        utils.delete_folder_files(function_output_fol)
        # utils.delete_folder_files(aseg_to_srf_output_fol)
        utils.delete_folder_files(renamed_output_fol)
        print('Trying to write into {}'.format(function_output_fol))
        utils.run_script(ASEG_TO_SRF.format(subject))
        # os.rename(aseg_to_srf_output_fol, function_output_fol)
    ply_files = glob.glob(op.join(renamed_output_fol, '*.ply'))
    if len(ply_files) < len(lookup) or overwrite_subcortical_objs:
        convert_and_rename_subcortical_files(subject, function_output_fol, renamed_output_fol, lookup)
    blender_dir = op.join(MMVT_DIR, subject, 'subcortical')
    if not op.isdir(blender_dir) or len(glob.glob(op.join(blender_dir, '*.ply'))) < len(ply_files):
        copy_subcorticals_to_blender(renamed_output_fol, subject)
    flag_ok = len(glob.glob(op.join(blender_dir, '*.ply'))) == len(lookup) and \
        len(glob.glob(op.join(blender_dir, '*.npz'))) == len(lookup)
    return flag_ok
Example #28
0
def calc_cbf_histograms(subject,
                        scan_rescan,
                        low_threshold,
                        high_threshold,
                        cortex_frac_threshold=0.9,
                        overwrite=False,
                        do_plot=True):
    subject_fol = op.join(HOME_FOL, site, subject, scan_rescan)
    output_fol = utils.make_dir(
        op.join(op.join(RESULTS_FOL, 'aparc_aseg_hists', subject,
                        scan_rescan)))
    values_output_fname = op.join(
        op.join(RESULTS_FOL, 'aparc_aseg_hists', subject, scan_rescan,
                'aparc_aseg_hist.pkl'))
    means_output_fname = op.join(
        op.join(RESULTS_FOL, 'aparc_aseg_hists', subject, scan_rescan,
                'aparc_values.pkl'))
    if op.isfile(values_output_fname) and op.isfile(
            means_output_fname) and not overwrite and not do_plot:
        return True
    cics_cbf_fname = op.join(HOME_FOL, site, subject, scan_rescan, 'CBF.nii')
    if not op.isfile(cics_cbf_fname):
        print('calc_cbf_histograms: Cannot find {}!'.format(cics_cbf_fname))
        return False
    aparc_aseg_fname = op.join(subject_fol, 'aparc+aseg_cbf.mgz')
    if not op.isfile(aparc_aseg_fname):
        print('calc_cbf_histograms: Cannot find {}!'.format(aparc_aseg_fname))
        return False
    cortex_frac_fname = op.join(HOME_FOL, site, subject, scan_rescan,
                                'CBF.cortex.mgz')
    if not op.isfile(cortex_frac_fname):
        print('No cortex frac!')
        return False
    cbf_data = nib.load(cics_cbf_fname).get_data()
    cortex_frac = nib.load(cortex_frac_fname).get_data()
    lut = utils.read_freesurfer_lookup_table(return_dict=True)
    aparc_aseg = nib.load(aparc_aseg_fname).get_data()
    unique_codes = list(range(1001, 1036)) + list(range(2001, 2036))
    if overwrite and do_plot:
        utils.delete_folder_files(output_fol)
    regions_values, regions_means = {}, {}
    output_str = ''
    for code in tqdm(unique_codes):
        region_name = lut.get(code, None)
        if region_name is None:
            # print('{} not in lut!'.format(code))
            continue
        fig_fname = op.join(output_fol, '{}.jpg'.format(region_name))
        if op.isfile(fig_fname) and not overwrite:
            continue
        region_values = cbf_data[np.where(aparc_aseg == code)]
        cortex_frac_values = cortex_frac[np.where(aparc_aseg == code)]
        cortex_indices = np.where(cortex_frac_values > cortex_frac_threshold)
        if len(cortex_indices[0]) == 0:
            output_str += '{} has no voxels with cortex_frac > {}!\n'.format(
                region_name, cortex_frac_threshold)
            continue
        region_values = region_values[cortex_indices]
        # regions_values[region_name] = region_values.copy()
        in_bounderies_indices = np.where((low_threshold < region_values)
                                         & (region_values < high_threshold))
        region_values = region_values[in_bounderies_indices]
        regions_values[region_name] = region_values.copy()
        regions_means[region_name] = np.mean(regions_values[region_name])
        # if len(outliers) > 0:
        #     output_str += '{} has {} out of {} values < {}\n'.format(
        #         region_name, len(outliers), len(region_values), low_threshold)
        # outliers = np.where(region_values > high_threshold)[0]
        # if len(outliers) > 0:
        #     output_str += '{} has {} out of {} values > {}\n'.format(
        #         region_name, len(outliers), len(region_values), high_threshold)
        # region_values[region_values < low_threshold] = low_threshold
        # region_values[region_values > high_threshold] = high_threshold
        if do_plot:
            plt.hist(region_values, bins=40)
            plt.savefig(fig_fname)
            plt.close()
    print(output_str)
    utils.save(regions_values, values_output_fname)
    utils.save(regions_means, means_output_fname)
Example #29
0
def language(args):
    # -f language -s nmr01361 --clinical_dir clin_4090354
    # -s nmr01353 -f clean_4d_data --fsd sycabs --remote_fmri_dir "/space/megraid/clinical/MEG-MRI/seder/freesurfer" --nconditions 4
    if args.clinical_dir == '':
        print('You should set the clinical_dir first. Example: clin_4090354')
        return
    clinical_root_dir = op.join(args.remote_fmri_dir, args.clinical_dir)
    if not op.isdir(clinical_root_dir):
        print('{} does not exist!'.format(clinical_root_dir))

    task = 'sycabs'
    fwhm = 6
    subject = args.subject[0]
    remote_mri_dir = args.remote_clinical_subjects_dir
    subject_mri_dir = op.join(remote_mri_dir, subject)
    mri_subject_task_dir = utils.make_dir(op.join(subject_mri_dir, task))
    clinical_dirs = glob.glob(op.join(clinical_root_dir, '*'))
    clinical_dirs = [
        d for d in clinical_dirs if utils.namebase(d) != 'mne_dcm'
    ]
    remote_fmri_dir = utils.select_one_file(clinical_dirs)
    fmri_fols = sorted(glob.glob(op.join(remote_fmri_dir, '*_SyCAbs')))
    par_fol = utils.make_dir(op.join(remote_mri_dir, subject, 'par'))
    par_files = glob.glob(op.join(par_fol, '*.par'))
    sessions = sorted(
        [utils.find_num_in_str(utils.namebase(d))[0] for d in fmri_fols])

    # Warning: You first need to put the original ones in the following folder:
    if len(par_files) == 0:
        print('\n *** Please put the original par files in {} and rerun ***'.
              format(op.join(remote_mri_dir, subject, 'par')))
        return

    par_files.sort(key=lambda x: int(utils.namebase(x).split('_')[-1]))
    ret = input('''
        Patient: {}
        MRI folder: {}
        fMRI root folder: {}
        fMRI sessions: {}
        Session and pars: {}
        Do you want to continue (y/n)? '''.format(
        subject, subject_mri_dir, remote_fmri_dir,
        [utils.namebase(d) for d in fmri_fols],
        list(zip([utils.namebase(f) for f in par_files], sessions))))
    if not au.is_true(ret):
        return

    # You need first to run src.preproc.anatomy
    if not op.isfile(anat.get_blend_fname(subject, args.atlas)):
        args = anat.read_cmd_args(
            dict(
                subject=subject,
                remote_subject_dir=subject_mri_dir,
                ignore_missing=True,
            ))
        pu.run_on_subjects(args, anat.main)

    # convert the fMRI dicom files to nii
    for fmri_fol in fmri_fols:
        ses_num = utils.find_num_in_str(utils.namebase(fmri_fol))[0]
        ses_files = glob.glob(op.join(fmri_fol, '**', '*.*'), recursive=True)
        output_fname = op.join(
            utils.make_dir(op.join(mri_subject_task_dir, ses_num)), 'f.nii.gz')
        if not op.isfile(output_fname):
            fu.mri_convert(ses_files[0], output_fname)

    # Convert and arrange the par file
    from src.misc.fmri_scripts import convert_par
    for par_file, session in zip(par_files, sessions):
        fs_par_fname = op.join(mri_subject_task_dir, session,
                               '{}.par'.format(task))
        # if not op.isfile(fs_par_fname):
        warnings = convert_par.sycabs(par_file, fs_par_fname)
        if warnings != '':
            print(
                '\n *** Please fix the problems with the par convertion ({}) and rerun ***\n'
                .format(par_file))
            return

    for hemi in utils.HEMIS:
        utils.delete_folder_files(
            op.join(remote_mri_dir, '{}_sm{}_{}'.format(task, fwhm, hemi)))

    # Run the FreeSurfer analysis
    args = fmri.read_cmd_args(
        dict(subject=subject,
             atlas=args.atlas,
             function='clean_4d_data',
             fsd=task,
             fwhm=fwhm,
             remote_fmri_dir=remote_mri_dir,
             nconditions=4,
             ignore_missing=True,
             print_only=False,
             overwrite_4d_preproc=False))
    pu.run_on_subjects(args, fmri.main)

    # Load the fMRI results
    args = fmri.read_cmd_args(
        dict(
            subject=subject,
            atlas=args.atlas,
            function='load_surf_files',
            fmri_file_template=op.join(MMVT_DIR, subject, 'fmri',
                                       'words_v_symbols_{hemi}.mgz'),
        ))
    pu.run_on_subjects(args, fmri.main)
Example #30
0
def calc_cortical_histograms(subject,
                             scan_rescan,
                             atlas,
                             low_threshold=40,
                             high_threshold=100,
                             overwrite=False,
                             do_plot=True,
                             n_jobs=4):
    if not lu.check_labels(subject, atlas, SUBJECTS_DIR, MMVT_DIR):
        return False
    output_fol = utils.make_dir(
        op.join(MMVT_DIR, subject, 'ASL', scan_rescan,
                'labels_hists_{}'.format(atlas)))
    output_fname = op.join(MMVT_DIR, subject, 'ASL', scan_rescan,
                           'labels_hists_{}.pkl'.format(atlas))
    if overwrite:
        utils.delete_folder_files(output_fol)
    print('Saving figures into {}'.format(output_fol))
    labels_data = {}
    for hemi in utils.HEMIS:
        npy_data_fname = op.join(
            MMVT_DIR, subject, 'fmri',
            'fmri_CBF_{}_{}.npy'.format(scan_rescan, hemi))
        if not op.isfile(npy_data_fname):
            print('Cannot find hemi data file! ({})'.format(npy_data_fname))
            return False
        surf_fname = op.join(SUBJECTS_DIR, subject, 'surf',
                             '{}.pial'.format(hemi))
        if not op.isfile(surf_fname):
            print('Cannot find surface file! ({})'.format(surf_fname))
            return False
        hemi_vals = np.load(npy_data_fname)
        labels = lu.read_labels(subject,
                                SUBJECTS_DIR,
                                atlas,
                                hemi=hemi,
                                n_jobs=n_jobs)
        labels_vertives_num = max([max(l.vertices) for l in labels])
        hemi_data_vertices_num = len(hemi_vals) - 1
        if labels_vertives_num != hemi_data_vertices_num:
            print(
                'Wrong number of vertices or labels! labels_vertives_num ({}) != len({})'
                .format(labels_vertives_num, len(hemi_vals)))
            return False
        for label in tqdm(labels):
            fig_fname = op.join(output_fol, '{}.jpg'.format(label.name))
            if op.isfile(fig_fname) and not overwrite:
                continue
            labels_data[label.name] = label_data = hemi_vals[label.vertices]
            outliers = np.where(label_data < low_threshold)[0]
            if len(outliers) > 0:
                print('{} has {}/{} values < {}'.format(
                    label.name, len(outliers), len(label_data), low_threshold))
            outliers = np.where(label_data > high_threshold)[0]
            if len(outliers) > 0:
                print('{} has {}/{} values > {}'.format(
                    label.name, len(outliers), len(label_data),
                    high_threshold))
            label_data[label_data < low_threshold] = low_threshold
            label_data[label_data > high_threshold] = high_threshold
            if do_plot:
                plt.hist(label_data, bins=40)
                plt.savefig(fig_fname)
                plt.close()
    utils.save(labels_data, output_fname)