def freesurfer_surface_to_blender_surface(subject, hemi='both', overwrite=False): for hemi in utils.get_hemis(hemi): utils.make_dir(op.join(MMVT_DIR, subject, 'surf')) for surf_type in ['inflated', 'pial']: surf_name = op.join(SUBJECTS_DIR, subject, 'surf', '{}.{}'.format(hemi, surf_type)) surf_wavefront_name = '{}.asc'.format(surf_name) surf_new_name = '{}.srf'.format(surf_name) hemi_ply_fname = '{}.ply'.format(surf_name) mmvt_hemi_ply_fname = op.join(MMVT_DIR, subject, 'surf', '{}.{}.ply'.format(hemi, surf_type)) mmvt_hemi_npz_fname = op.join(MMVT_DIR, subject, 'surf', '{}.{}.npz'.format(hemi, surf_type)) if overwrite or not op.isfile(mmvt_hemi_ply_fname) and not op.isfile(mmvt_hemi_npz_fname): print('{} {}: convert srf to asc'.format(hemi, surf_type)) utils.run_script('mris_convert {} {}'.format(surf_name, surf_wavefront_name)) os.rename(surf_wavefront_name, surf_new_name) print('{} {}: convert asc to ply'.format(hemi, surf_type)) convert_hemis_srf_to_ply(subject, hemi, surf_type) if op.isfile(mmvt_hemi_ply_fname): os.remove(mmvt_hemi_ply_fname) shutil.copy(hemi_ply_fname, mmvt_hemi_ply_fname) ply_fname = op.join(MMVT_DIR, subject, 'surf', '{}.{}.ply'.format(hemi, surf_type)) if not op.isfile(mmvt_hemi_npz_fname): verts, faces = utils.read_ply_file(ply_fname) np.savez(mmvt_hemi_npz_fname, verts=verts, faces=faces) return utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.pial.ply')) and \ utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.pial.npz')) and \ utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.inflated.ply')) and \ utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.inflated.npz'))
def init(subject, atlas, n_jobs): from src.utils import geometry_utils as gu if not utils.both_hemi_files_exist( op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format( '{hemi}', atlas))): anat.create_annotation(subject, atlas) if not utils.both_hemi_files_exist( op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format( '{hemi}', atlas))): raise Exception( 'Can\'t find the cortical atlas {} for subject {}'.format( atlas, subject)) labels_vertices = find_rois.read_labels_vertices(SUBJECTS_DIR, subject, atlas, n_jobs) labels = lu.read_labels(subject, SUBJECTS_DIR, atlas) labels_names = [l.name for l in labels] aseg_atlas_fname = op.join(SUBJECTS_DIR, subject, 'mri', 'aseg.mgz') aseg_data = nib.load(aseg_atlas_fname).get_data() lut = fu.import_freesurfer_lut() pia_verts = {} for hemi in ['rh', 'lh']: pia_verts[hemi], _ = gu.read_surface( op.join(SUBJECTS_DIR, subject, 'surf', '{}.pial'.format(hemi))) # pia_verts[hemi], _ = nib.freesurfer.read_geometry( # op.join(SUBJECTS_DIR, subject, 'surf', '{}.pial'.format(hemi))) subs_center_of_mass, subs_names = calc_subcorticals_pos( subject, aseg_data, lut) labels_center_of_mass = lu.calc_center_of_mass(labels, ret_mat=True) * 1000 regions_center_of_mass = np.concatenate( (labels_center_of_mass, subs_center_of_mass)) regions_names = labels_names + subs_names # save_com_as_elecs(subject, regions_center_of_mass, regions_names, atlas) # save_com_as_elecs(subject, subs_center_of_mass, subs_names, atlas) return labels_vertices, regions_center_of_mass, regions_names, aseg_data, lut, pia_verts,
def create_annot_from_mad(args): remote_subject_dir_template = '/mnt/cashlab/Original Data/MG/{subject}/{subject}_Notes_and_Images/{subject}_SurferOutput' for subject in args.subject: remote_subject_dir = remote_subject_dir_template.format(subject=subject) if utils.both_hemi_files_exist(op.join(remote_subject_dir, 'label', '{hemi}.aparc.DKTatlas.annot')): print('{} has already both annot files!'.format(subject)) continue args = anat.read_cmd_args(dict( subject=subject.lower(), atlas=args.atlas, remote_subject_dir=remote_subject_dir_template, function='create_annotation', ignore_missing=True, )) pu.run_on_subjects(args, anat.main) if not utils.both_hemi_files_exist(op.join(SUBJECTS_DIR, subject.lower(), 'label', '{hemi}.aparc.DKTatlas.annot')): print('Couldn\'t create annot files for {}!'.format(subject)) continue local_annot_fol = utils.make_dir(op.join(SUBJECTS_DIR, 'annot_files', subject.lower())) for hemi in utils.HEMIS: local_annot_fname = op.join(SUBJECTS_DIR, subject.lower(), 'label', '{}.aparc.DKTatlas.annot'.format(hemi)) remote_annot_fname = op.join(remote_subject_dir, 'label', '{}.aparc.DKTatlas.annot'.format(hemi)) local_temp_annot_fname = op.join(local_annot_fol, '{}.aparc.DKTatlas.annot'.format(hemi)) if not op.isfile(remote_annot_fname): if op.isfile(local_annot_fname): utils.copy_file(local_annot_fname, local_temp_annot_fname) else: print('Can\'t copy {} for {}, it doesn\'t exist!'.format(local_annot_fname, subject))
def _calc_zvals_parallel(p): subject, clip_fname, baseline_fnames, modality, inverse_method, fol, from_index, to_index, use_abs, overwrite = p stc_zvals_fname = op.join( fol, '{}-epilepsy-{}-{}-{}-amplitude-zvals'.format( subject, inverse_method, modality, utils.namebase(clip_fname))) if utils.both_hemi_files_exist('{}-{}.stc'.format( stc_zvals_fname, '{hemi}')) and not overwrite: return True stc_fname = op.join( MMVT_DIR, subject, meg.modality_fol(modality), 'ictal-{}-stcs'.format(inverse_method), '{}-epilepsy-{}-{}-{}'.format(subject, inverse_method, modality, utils.namebase(clip_fname))) if not utils.both_hemi_files_exist('{}-{}.stc'.format(stc_fname, '{hemi}')): print('Error finding {}!'.format(stc_fname)) return False return meg.calc_stc_zvals( subject, '{}-rh.stc'.format(stc_fname), baseline_fnames, stc_zvals_fname, # '{}-rh.stc'.format(baseline_fname) use_abs, from_index, to_index, True, overwrite)
def read_xls(xls_fname, subject_to='colin27', atlas='aparc.DKTatlas', overwrite=False, check_morph_file=False): bipolar = True template_header = nib.load( op.join(SUBJECTS_DIR, subject_to, 'mri', 'T1.mgz')).header subjects_electrodes = defaultdict(list) electrodes_colors = defaultdict(list) for line in utils.xlsx_reader(xls_fname, skip_rows=1): subject, _, elec_name, _, anat_group = line subject = subject.replace('\'', '') if subject == '': break if check_morph_file: electrodes_fname = op.join( MMVT_DIR, subject, 'electrodes', 'electrodes_morph_to_{}.txt'.format(subject_to)) if not op.isfile(electrodes_fname): continue elec_group, num1, num2 = utils.elec_group_number(elec_name, bipolar) if '{}{}-{}'.format(elec_group, num2, num1) != elec_name: num1, num2 = str(num1).zfill(2), str(num2).zfill(2) if '{}{}-{}'.format(elec_group, num2, num1) != elec_name: raise Exception('Wrong group or numbers!') for num in [num1, num2]: subjects_electrodes[subject].append('{}{}'.format(elec_group, num)) electrodes_colors[subject].append((elec_name, int(anat_group))) subjects = list(subjects_electrodes.keys()) bad_subjects = [] for subject in subjects: atlas = utils.fix_atlas_name(subject, atlas, SUBJECTS_DIR) if not utils.both_hemi_files_exist( op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format( '{hemi}', atlas))): anat.create_annotation(subject, atlas) if not utils.both_hemi_files_exist( op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format('{hemi}', atlas))): print('No atlas for {}!'.format(atlas)) bad_subjects.append((subject, 'No atlas')) continue try: ela_morph_electrodes.calc_elas(subject, subject_to, subjects_electrodes[subject], bipolar=False, atlas=atlas, overwrite=overwrite) except: err = utils.print_last_error_line() bad_subjects.append((subject, err)) continue print(bad_subjects)
def labels_to_annot(subject, subjects_dir='', aparc_name='aparc250', labels_fol='', overwrite=True, labels=[], fix_unknown=True): if subjects_dir == '': subjects_dir = os.environ['SUBJECTS_DIR'] subject_dir = op.join(subjects_dir, subject) annot_files_exist = utils.both_hemi_files_exist( op.join(subject_dir, 'label', '{}.{}.annot'.format('{hemi}', aparc_name))) if annot_files_exist and not overwrite: return True if len(labels) == 0: labels_fol = op.join(subject_dir, 'label', aparc_name) if labels_fol == '' else labels_fol labels_files = glob.glob(op.join(labels_fol, '*.label')) if len(labels_files) == 0: if not annot_files_exist: raise Exception('labels_to_annot: No labels files!') else: print("Can't find label files, using the annot files instead") return True for label_file in labels_files: if fix_unknown and 'unknown' in utils.namebase(label_file): continue label = mne.read_label(label_file) # print(label.name) label.name = get_label_hemi_invariant_name(label.name) labels.append(label) labels.sort(key=lambda l: l.name) if overwrite: for hemi in HEMIS: utils.remove_file( op.join(subject_dir, 'label', '{}.{}.annot'.format(hemi, aparc_name))) try: mne.write_labels_to_annot(subject=subject, labels=labels, parc=aparc_name, overwrite=overwrite, subjects_dir=subjects_dir) except: print('Error in writing annot file!') # print(traceback.format_exc()) return False return utils.both_hemi_files_exist( op.join(subject_dir, 'label', '{}.{}.annot'.format('{hemi}', aparc_name)))
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
def _calc_fMRI_rois(p): subject, atlas, fmri_dir, n_jobs = p fMRI_cluster_fname = op.join( MMVT_DIR, subject, 'fmri', 'clusters_labels_MSIT_I-C_{}.pkl'.format(atlas)) if op.isfile(fMRI_cluster_fname) and \ utils.both_hemi_files_exist(op.join(SUBJECTS_DIR, subject, 'label', '{hemi}.MSIT_I-C.annot')): print('{} already have fMRI clusters'.format(subject)) return True fmri_fnames = glob.glob( op.join(fmri_dir, subject, '**', 'msit_I-C.analysis.lh', 'I-C', 'sig.nii.gz')) if len(fmri_fnames) == 0: print('Couldn\'t find MSIT fmri file for {}'.format(subject)) return False try: surf_template_fname = fmri.load_surf_file(subject, fmri_fnames[0]) fmri.find_clusters(subject, surf_template_fname, 2, atlas, task='MSIT', create_clusters_labels=True, new_atlas_name='MSIT_I-C', n_jobs=n_jobs) except: print(traceback.format_exc())
def morph_labels_from_fsaverage(subject, subjects_dir, mmvt_dir, aparc_name='aparc250', fs_labels_fol='', sub_labels_fol='', n_jobs=6, fsaverage='fsaverage', overwrite=False): subject_dir = op.join(subjects_dir, subject) labels_fol = op.join(subjects_dir, fsaverage, 'label', aparc_name) if fs_labels_fol=='' else fs_labels_fol sub_labels_fol = op.join(subject_dir, 'label', aparc_name) if sub_labels_fol=='' else sub_labels_fol if not op.isdir(sub_labels_fol): os.makedirs(sub_labels_fol) subject_annot_files_exist = utils.both_hemi_files_exist(op.join(subjects_dir, subject, 'label', '{}.{}.annot'.format( '{hemi}', aparc_name))) if subject_annot_files_exist: labels = read_labels(subject, subjects_dir, aparc_name, n_jobs=n_jobs) else: labels = read_labels(fsaverage, subjects_dir, aparc_name, n_jobs=n_jobs) if len(labels) == 0: raise Exception('morph_labels_from_fsaverage: No labels files found!') if subject == fsaverage: return surf_loaded = False for fs_label in labels: label_file = op.join(labels_fol, '{}.label'.format(fs_label.name)) local_label_name = op.join(sub_labels_fol, '{}.label'.format(op.splitext(op.split(label_file)[1])[0])) if not op.isfile(local_label_name) or overwrite: # fs_label = mne.read_label(label_file) fs_label.values.fill(1.0) sub_label = fs_label.morph(fsaverage, subject, grade=None, n_jobs=n_jobs, subjects_dir=subjects_dir) if np.all(sub_label.pos == 0): if not surf_loaded: verts = {} for hemi in HEMIS: hemi_verts, _ = utils.read_pial_npz(subject, mmvt_dir, hemi) verts[hemi] = hemi_verts surf_loaded = True sub_label.pos = verts[sub_label.hemi][sub_label.vertices] sub_label.save(local_label_name)
def create_aparc_aseg_file(subject, atlas, overwrite_aseg_file=False, print_only=False, args={}): if not utils.both_hemi_files_exist(op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format('{hemi}', atlas))): print('No annot file was found for {}!'.format(atlas)) print('Run python -m src.preproc.anatomy -s {} -a {} -f create_surfaces,create_annotation'.format(subject, atlas)) return False # aparc_aseg_fname ret = fu.create_aparc_aseg_file( subject, atlas, SUBJECTS_DIR, overwrite_aseg_file, print_only, mmvt_args=args) if isinstance(ret, Iterable): ret, aparc_aseg_fname = ret if not ret: return False aparc_aseg_file = utils.namebase_with_ext(aparc_aseg_fname) utils.make_dir(op.join(MMVT_DIR, subject, 'freeview')) blender_file = op.join(MMVT_DIR, subject, 'freeview', aparc_aseg_file) utils.remove_file(blender_file) shutil.copyfile(aparc_aseg_fname, blender_file) atlas_mat_fname = utils.change_fname_extension(blender_file, 'npy') if not op.isfile(atlas_mat_fname) or overwrite_aseg_file: d = nib.load(blender_file) x = d.get_data() np.save(atlas_mat_fname, x) return op.isfile(blender_file) and op.isfile(atlas_mat_fname)
def compare_connectivity(subject, atlas, n_jobs=6): for name, fol, template in zip(['hesheng', 'linda', 'freesurfer'], [hesheng_surf_fol, linda_surf_fol, fs_surf_fol], [hesheng_template, linda_hemi_template, fs_surf_template]): output_fname_template = op.join( fmri.MMVT_DIR, subject, 'fmri', '{}_labels_data_laus125_mean_{}.npz'.format(name, '{hemi}')) if not utils.both_hemi_files_exist(output_fname_template): args = fmri.read_cmd_args(dict( subject=subject, atlas=atlas, function='analyze_4d_data', fmri_file_template=template, remote_fmri_dir=fol, labels_extract_mode='mean', overwrite_labels_data=False)) pu.run_on_subjects(args, fmri.main) for hemi in utils.HEMIS: os.rename(op.join(fmri.MMVT_DIR, subject, 'fmri', 'labels_data_laus125_mean_{}.npz'.format(hemi)), output_fname_template.format(hemi=hemi)) args = con.read_cmd_args(dict( subject=subject, atlas='laus125', function='calc_lables_connectivity', connectivity_modality='fmri', connectivity_method='corr,cv', labels_extract_mode='mean', windows_length=34, windows_shift=4, save_mmvt_connectivity=False, calc_subs_connectivity=False, labels_name=name, recalc_connectivity=True, n_jobs=n_jobs)) pu.run_on_subjects(args, con.main) conn_fol = op.join(con.MMVT_DIR, subject, 'connectivity') coloring_fol = op.join(con.MMVT_DIR, subject, 'coloring') os.rename(op.join(conn_fol, 'fmri_corr.npy'), op.join(conn_fol, 'fmri_corr_{}.npy'.format(name))) os.rename(op.join(conn_fol, 'fmri_corr_cv_mean.npz'), op.join(conn_fol, 'mri_corr_cv_mean_{}.npz'.format(name))) os.rename(op.join(conn_fol, 'fmri_corr_cv_mean_mean.npz'), op.join(conn_fol, 'fmri_corr_cv_mean_mean_{}.npz'.format(name))) os.rename(op.join(coloring_fol, 'fmri_corr_cv_mean.csv'), op.join(coloring_fol, 'fmri_corr_cv_mean_{}.csv'.format(name)))
def get_atlas_labels_names(subject, atlas, subjects_dir, delim='-', pos='end', return_flat_labels_list=False, include_unknown=False, include_corpuscallosum=False, n_jobs=1): annot_fname_hemi = op.join(subjects_dir, subject, 'label', '{}.{}.annot'.format('{hemi}', atlas)) labels_names_hemis = dict(lh=[], rh=[]) all_labels = [] if utils.both_hemi_files_exist(annot_fname_hemi): for hemi in ['rh', 'lh']: annot_fname = op.join(subjects_dir, subject, 'label', '{}.{}.annot'.format(hemi, atlas)) _, _, labels_names = mne.label._read_annot(annot_fname) labels_names = fix_labels_names(labels_names, hemi, delim, pos) all_labels.extend(labels_names) labels_names_hemis[hemi] = labels_names else: all_labels = read_labels_parallel(subject, subjects_dir, atlas, labels_fol='' , n_jobs=n_jobs) for label in all_labels: labels_names_hemis[label.hemi].append(label.name) if len(labels_names_hemis['rh']) == 0 or len(labels_names_hemis['lh']) == 0: raise Exception("Can't read {} labels for atlas {}".format(subject, atlas)) if return_flat_labels_list: if not include_unknown: all_labels = [l for l in all_labels if 'unknown' not in l] if not include_corpuscallosum: all_labels = [l for l in all_labels if 'corpuscallosum' not in l] return all_labels else: if not include_unknown: for hemi in HEMIS: labels_names_hemis[hemi] = [l for l in labels_names_hemis[hemi] if 'unknown' not in l] if not include_corpuscallosum: for hemi in HEMIS: labels_names_hemis[hemi] = [l for l in labels_names_hemis[hemi] if 'corpuscallosum' not in l] return labels_names_hemis
def create_aparc_aseg_file(subject, atlas, subjects_dir, overwrite_aseg_file=False, print_only=False, **kargs): if not utils.both_hemi_files_exist( op.join(subjects_dir, subject, 'label', '{}.{}.annot'.format( '{hemi}', atlas))): print('No annot file was found for {}!'.format(atlas)) return False, '' # The atlas var need to be in the locals for the APARC2ASEG call aparc_aseg_file = '{}+aseg.mgz'.format(atlas) mri_file_fol = op.join(subjects_dir, subject, 'label') aparc_aseg_fname = op.join(mri_file_fol, aparc_aseg_file) rs = utils.partial_run_script(locals(), print_only=print_only, cwd=mri_file_fol) if not op.isfile(aparc_aseg_fname) or overwrite_aseg_file: now = time.time() rs(mri_aparc2aseg) if op.isfile(aparc_aseg_fname) and op.getmtime(aparc_aseg_fname) > now: return True, aparc_aseg_fname else: print('Failed to create {}'.format(aparc_aseg_fname)) return False, '' return True, aparc_aseg_fname
def calc_hesheng_surf(subject, atlas): subject_fol = op.join(fmri.MMVT_DIR, subject, 'fmri') if not (utils.both_hemi_files_exist( op.join(subject_fol, 'fmri_hesheng_{hemi}.npy')) and op.isfile(op.join(subject_fol, 'hesheng_minmax.pkl'))): # Copy and rename Hesheng's files hesheng_fnames = glob.glob( op.join(hesheng_surf_fol.format(subject=subject), hesheng_template.format(subject=subject))) for fname in hesheng_fnames: hemi = lu.get_label_hemi_invariant_name(utils.namebase(fname)) target_file = op.join(fmri.FMRI_DIR, subject, 'hesheng_{}.nii.gz'.format(hemi)) mgz_target_file = utils.change_fname_extension(target_file, 'mgz') if not op.isfile(mgz_target_file): shutil.copy(fname, target_file) fu.nii_gz_to_mgz(target_file) os.remove(target_file) # Load Hesheng's files args = fmri.read_cmd_args( dict(subject=subject, atlas=atlas, function='load_surf_files', overwrite_surf_data=True, fmri_file_template='hesheng_{hemi}.mgz')) pu.run_on_subjects(args, fmri.main)
def create_unknown_labels(subject, atlas): labels_fol = op.join(SUBJECTS_DIR, subject, 'label', atlas) utils.make_dir(labels_fol) unknown_labels_fname_template = op.join( labels_fol, 'unknown-{}.label'.format('{hemi}')) if utils.both_hemi_files_exist(unknown_labels_fname_template): unknown_labels = { hemi: mne.read_label(unknown_labels_fname_template.format(hemi=hemi), subject) for hemi in utils.HEMIS } return unknown_labels unknown_labels = {} for hemi in utils.HEMIS: labels = read_labels(subject, SUBJECTS_DIR, atlas, hemi=hemi) unknown_label_name = 'unknown-{}'.format(hemi) labels_names = [l.name for l in labels] if unknown_label_name not in labels_names: verts, _ = utils.read_pial(subject, MMVT_DIR, hemi) unknown_verts = set(range(verts.shape[0])) for label in labels: unknown_verts -= set(label.vertices) unknown_verts = np.array(sorted(list(unknown_verts))) unknown_label = mne.Label(unknown_verts, hemi=hemi, name=unknown_label_name, subject=subject) else: unknown_label = labels[labels_names.index(unknown_label_name)] unknown_labels[hemi] = unknown_label if not op.isfile(unknown_labels_fname_template.format(hemi=hemi)): unknown_label.save(unknown_labels_fname_template.format(hemi=hemi)) return unknown_labels
def find_template_brain_with_annot_file(aparc_name, fsaverage, subjects_dir, find_in_all=True): fs_found = False if find_in_all: fsaverage = [ utils.namebase(d) for d in glob.glob(op.join(subjects_dir, 'fs*')) ] elif isinstance(fsaverage, str): fsaverage = [fsaverage] for fsav in fsaverage: fsaverage_annot_files_exist = utils.both_hemi_files_exist( op.join(subjects_dir, fsav, 'label', '{}.{}.annot'.format('{hemi}', aparc_name))) fsaverage_labels_exist = len( glob.glob( op.join(subjects_dir, fsav, 'label', aparc_name, '*.label'))) > 0 if fsaverage_annot_files_exist or fsaverage_labels_exist: fsaverage = fsav fs_found = True break if not fs_found: print("Can't find the annot file for any of the templates brain!") return '' else: return fsaverage
def calc_linda_surf(subject, atlas): # Check for Linda's output fname if not utils.both_hemi_files_exist(op.join(fmri.MMVT_DIR, subject, 'fmri', 'fmri_linda_{}.npy'.format('{hemi}'))) \ and not op.isfile(op.join(fmri.MMVT_DIR, subject, 'fmri', 'linda_minmax.pkl')): # Find Linda's files linda_volume_fnames = glob.glob(op.join( linda_fol.format(subject=subject), linda_vol_template.format(subject=subject))) linda_volume_folder = utils.get_parent_fol(linda_volume_fnames[0]) # project linda files on the surface args = fmri.read_cmd_args(dict( subject=subject, function='project_volume_to_surface', remote_fmri_dir=linda_volume_folder, fmri_file_template=linda_vol_template.format(subject=subject), overwrite_surf_data=True)) pu.run_on_subjects(args, fmri.main) # rename Linda's files linda_fnames = glob.glob(op.join(fmri.MMVT_DIR, subject, 'fmri', 'fmri_{}'.format( linda_template_npy.format(subject=subject)))) for fname in linda_fnames: hemi = lu.get_label_hemi(utils.namebase(fname)) target_file = op.join(fmri.MMVT_DIR, subject, 'fmri', 'fmri_linda_{}.npy'.format(hemi)) if not op.isfile(target_file): os.rename(fname, target_file) # rename minmax file linda_minmax_name = '{}.pkl'.format(utils.namebase(glob.glob(op.join( fmri.MMVT_DIR, subject, 'fmri', '{}_minmax.pkl'.format( utils.namebase(linda_vol_template.format(subject=subject)))))[0])) os.rename(op.join(fmri.MMVT_DIR, subject, 'fmri', linda_minmax_name), op.join(fmri.MMVT_DIR, subject, 'fmri', 'linda_minmax.pkl')) # delete mgz files mgz_files = glob.glob(op.join(fmri.MMVT_DIR, subject, 'fmri', 'fmri_{}_?h.mgz'.format( utils.namebase(linda_vol_template.format(subject=subject))))) for mgz_file in mgz_files: os.remove(mgz_file)
def load_fieldtrip_volumetric_data(args): # http://www.fieldtriptoolbox.org/reference/ft_sourceinterpolate # http://www.fieldtriptoolbox.org/reference/ft_sourceplot # https://github.com/fieldtrip/fieldtrip/blob/master/ft_sourceplot.m # https://github.com/fieldtrip/fieldtrip/blob/master/ft_sourceinterpolate.m # -s nihpd-asym -f load_fieldtrip_volumetric_data --fieldtrip_data_field_name stat2 --fieldtrip_data_name sourceInterp --overwrite_stc 1 --overwrite_nii_file 1 import scipy.io as sio import nibabel as nib from src.preproc import meg import numpy as np from src.utils import freesurfer_utils as fu overwrite = False subject = args.subject[0] data_name = 'sourceInterp' volumetric_meg_fname = op.join(MEG_DIR, subject, '{}.nii'.format(data_name)) if not op.isfile(volumetric_meg_fname) or overwrite: fname = op.join(MEG_DIR, subject, '{}.mat'.format(data_name)) # load Matlab/Fieldtrip data mat = sio.loadmat(fname, squeeze_me=True, struct_as_record=False) ft_data = mat[data_name] data = ft_data.stat2 affine = ft_data.transform nib.save(nib.Nifti1Image(data, affine), volumetric_meg_fname) surface_output_template = op.join(MEG_DIR, subject, '{}_{}.mgz'.format(data_name, '{hemi}')) if not utils.both_hemi_files_exist(surface_output_template) or overwrite: fu.project_on_surface(subject, volumetric_meg_fname, surface_output_template, overwrite_surf_data=True, modality='meg') data = {hemi:np.load(op.join(MMVT_DIR, subject, 'meg', 'meg_{}_{}.npy'.format(data_name, hemi))) for hemi in utils.HEMIS} stc = meg.create_stc_t(subject, data['rh'], data['lh']) stc.save(op.join(MMVT_DIR, subject, 'meg', data_name))
def get_atlas_labels_names(subject, atlas, delim='-', pos='end', return_flat_labels_list=False, include_unknown=False, include_corpuscallosum=False, n_jobs=1): annot_fname_hemi = op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format('{hemi}', atlas)) labels_names_hemis = dict(lh=[], rh=[]) all_labels = [] if utils.both_hemi_files_exist(annot_fname_hemi): for hemi in ['rh', 'lh']: annot_fname = op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format(hemi, atlas)) _, _, labels_names = mne.label._read_annot(annot_fname) labels_names = fix_labels_names(labels_names, hemi, delim, pos) all_labels.extend(labels_names) labels_names_hemis[hemi] = labels_names else: all_labels = utils.read_labels_parallel(subject, SUBJECTS_DIR, atlas, n_jobs) for label in all_labels: labels_names_hemis[label.hemi].append(label.name) if len(labels_names_hemis['rh']) == 0 or len(labels_names_hemis['lh']) == 0: raise Exception("Can't read {} labels for atlas {}".format(subject, atlas)) if return_flat_labels_list: if not include_unknown: all_labels = [l for l in all_labels if 'unknown' not in l] if not include_corpuscallosum: all_labels = [l for l in all_labels if 'corpuscallosum' not in l] return all_labels else: if not include_unknown: for hemi in HEMIS: labels_names_hemis[hemi] = [l for l in labels_names_hemis[hemi] if 'unknown' not in l] if not include_corpuscallosum: for hemi in HEMIS: labels_names_hemis[hemi] = [l for l in labels_names_hemis[hemi] if 'corpuscallosum' not in l] return labels_names_hemis
def create_annotation_from_fsaverage(subject, aparc_name='aparc250', fsaverage='fsaverage', overwrite_annotation=False, overwrite_morphing=False, do_solve_labels_collisions=False, morph_labels_from_fsaverage=True, fs_labels_fol='', n_jobs=6): annotations_exist = np.all([op.isfile(op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format(hemi, aparc_name))) for hemi in HEMIS]) existing_freesurfer_annotations = ['aparc.DKTatlas40.annot', 'aparc.annot', 'aparc.a2009s.annot'] if '{}.annot'.format(aparc_name) in existing_freesurfer_annotations: morph_labels_from_fsaverage = False do_solve_labels_collisions = False if not annotations_exist: utils.make_dir(op.join(SUBJECTS_DIR, subject, 'label')) annotations_exist = fu.create_annotation_file(subject, aparc_name, subjects_dir=SUBJECTS_DIR, freesurfer_home=FREE_SURFER_HOME) if morph_labels_from_fsaverage: utils.morph_labels_from_fsaverage(subject, SUBJECTS_DIR, aparc_name, n_jobs=n_jobs, fsaverage=fsaverage, overwrite=overwrite_morphing, fs_labels_fol=fs_labels_fol) if do_solve_labels_collisions: solve_labels_collisions(subject, aparc_name, fsaverage, n_jobs) # Note that using the current mne version this code won't work, because of collissions between hemis # You need to change the mne.write_labels_to_annot code for that. if overwrite_annotation or not annotations_exist: try: utils.labels_to_annot(subject, SUBJECTS_DIR, aparc_name, overwrite=overwrite_annotation) except: print("Can't write labels to annotation! Trying to solve labels collision") solve_labels_collisions(subject, aparc_name, fsaverage, n_jobs) try: utils.labels_to_annot(subject, SUBJECTS_DIR, aparc_name, overwrite=overwrite_annotation) except: print("Can't write labels to annotation! Solving the labels collision didn't help...") print(traceback.format_exc()) return utils.both_hemi_files_exist(op.join( SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format('{hemi}', aparc_name)))
def _morph_electrodes_parallel(p): subjects, atlas, subject_to, subjects_electrodes, annotation_template, overwrite = p bad_subjects = [] for subject in subjects: get_subject_files_from_mad([subject], atlas) atlas = utils.fix_atlas_name(subject, atlas, SUBJECTS_DIR) if not utils.both_hemi_files_exist( op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format( '{hemi}', atlas))): err = '' try: anat.create_annotation(subject, atlas, annotation_template, n_jobs=1, overwrite_vertices_labels_lookup=True) except: print(traceback.format_exc()) err = utils.print_last_error_line() if not utils.both_hemi_files_exist( op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format('{hemi}', atlas))): bad_subjects.append( (subject, 'No atlas' if err == '' else err)) continue try: electrodes = list( set(utils.flat_list_of_lists(subjects_electrodes[subject]))) if not overwrite: electrodes = [ elc_name for elc_name in electrodes if not op.isfile( op.join(MMVT_DIR, subject, 'electrodes', '{}_ela_morphed.npz'.format(elc_name))) ] ela_morph_electrodes.calc_elas(subject, subject_to, electrodes, bipolar=False, atlas=atlas, overwrite=overwrite, n_jobs=1) except: print(traceback.format_exc()) err = utils.print_last_error_line() bad_subjects.append((subject, err)) return bad_subjects
def freesurfer_surface_to_blender_surface(subject, hemi='both', overwrite=False): # verts, faces = {}, {} for hemi in utils.get_hemis(hemi): utils.make_dir(op.join(MMVT_DIR, subject, 'surf')) for surf_type in ['inflated', 'pial']: surf_name = op.join(SUBJECTS_DIR, subject, 'surf', '{}.{}'.format(hemi, surf_type)) surf_wavefront_name = '{}.asc'.format(surf_name) surf_new_name = '{}.srf'.format(surf_name) hemi_ply_fname = '{}.ply'.format(surf_name) mmvt_hemi_ply_fname = op.join(MMVT_DIR, subject, 'surf', '{}.{}.ply'.format(hemi, surf_type)) mmvt_hemi_npz_fname = op.join(MMVT_DIR, subject, 'surf', '{}.{}.npz'.format(hemi, surf_type)) if overwrite or not op.isfile(mmvt_hemi_ply_fname) and not op.isfile(mmvt_hemi_npz_fname): print('{}: convert srf to asc'.format(hemi)) utils.run_script('mris_convert {} {}'.format(surf_name, surf_wavefront_name)) os.rename(surf_wavefront_name, surf_new_name) print('{}: convert asc to ply'.format(hemi)) convert_hemis_srf_to_ply(subject, hemi, surf_type) # if surf_type == 'inflated': # verts, faces = utils.read_ply_file(hemi_ply_fname) # verts_offset = 5.5 if hemi == 'rh' else -5.5 # verts[:, 0] = verts[:, 0] + verts_offset # utils.write_ply_file(verts, faces, '{}_offset.ply'.format(surf_name)) if op.isfile(mmvt_hemi_ply_fname): os.remove(mmvt_hemi_ply_fname) shutil.copy(hemi_ply_fname, mmvt_hemi_ply_fname) ply_fname = op.join(MMVT_DIR, subject, 'surf', '{}.{}.ply'.format(hemi, surf_type)) if not op.isfile(mmvt_hemi_npz_fname): verts, faces = utils.read_ply_file(ply_fname) np.savez(mmvt_hemi_npz_fname, verts=verts, faces=faces) # verts[hemi], faces[hemi] = utils.read_ply_file(mmvt_hemi_npz_fname) # if not op.isfile(op.join(MMVT_DIR, subject, 'cortex.pial.npz')): # faces['rh'] += np.max(faces['lh']) + 1 # verts_cortex = np.vstack((verts['lh'], verts['rh'])) # faces_cortex = np.vstack((faces['lh'], faces['rh'])) # utils.write_ply_file(verts_cortex, faces_cortex, op.join(MMVT_DIR, subject, 'cortex.pial.ply')) # np.savez(op.join(MMVT_DIR, subject, 'cortex.pial.npz'), verts=verts_cortex, faces=faces_cortex) return utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.pial.ply')) and \ utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.pial.npz')) and \ utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.inflated.ply')) and \ utils.both_hemi_files_exist(op.join(MMVT_DIR, subject, 'surf', '{hemi}.inflated.npz'))
def post_meg_preproc(args): inv_method, em = 'MNE', 'mean_flip' atlas = 'darpa_atlas' bands = dict(theta=[4, 8], alpha=[8, 15], beta=[15, 30], gamma=[30, 55], high_gamma=[65, 200]) times = (-2, 4) subjects = args.subject res_fol = utils.make_dir( op.join(utils.get_parent_fol(MMVT_DIR), 'msit-ecr')) for subject in subjects: args.subject = subject for task in args.tasks: task = task.lower() if not utils.both_hemi_files_exist( op.join( MMVT_DIR, subject, 'meg', 'labels_data_{}_{}_{}_{}_lh.npz'.format( task, atlas, inv_method, em, '{hemi}'))): print('label data can\'t be found for {} {}'.format( subject, task)) continue utils.make_dir(op.join(res_fol, subject)) meg.calc_labels_func(subject, task, atlas, inv_method, em, tmin=0, tmax=0.5, times=times, norm_data=False) meg.calc_labels_mean_power_bands(subject, task, atlas, inv_method, em, tmin=times[0], tmax=times[1], overwrite=True) for fname in [ f for f in glob.glob( op.join(MMVT_DIR, subject, 'labels', 'labels_data', '*')) if op.isfile(f) ]: shutil.copyfile( fname, op.join(res_fol, subject, utils.namebase_with_ext(fname)))
def main(subject, atlas, connectivity_method, graph_func, remote_subject_dir, files, n_jobs=4): meg_fol = op.join(MMVT_DIR, subject, 'meg') for fname in files: file_name = utils.namebase(fname) files_fol = utils.make_dir(op.join(meg_fol, file_name)) files_exist = \ utils.both_hemi_files_exist(op.join(files_fol, '{}_all-dSPM-{}.stc'.format(subject, '{hemi}'))) and \ utils.both_hemi_files_exist(op.join(files_fol, 'labels_data_epilepsy_laus125_dSPM_mean_flip_{hemi}.npz')) if not files_exist: args = meg.read_cmd_args( dict(subject=subject, task='epilepsy', function='calc_stc,calc_labels_avg_per_condition', atlas=atlas, evo_fname=fname, remote_subject_dir=remote_subject_dir, overwrite_labels_data=True, n_jobs=n_jobs)) meg.call_main(args) analyse_connectivity(subject, connectivity_method, graph_func, file_name, atlas, n_jobs) for hemi in utils.HEMIS: utils.move_file( op.join(meg_fol, '{}_all-dSPM-{}.stc'.format(subject, hemi)), files_fol) utils.move_file( op.join( meg_fol, 'labels_data_epilepsy_laus125_dSPM_mean_flip_{}.npz'. format(hemi)), files_fol) plot_all_files_graph_max(subject, files, graph_func)
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)
def check_if_all_done(new_raw_fname, cond, inverse_method, calc_stc_per_session, stc_template_hemi, labels_data_template, args): all_done = all([ op.isfile(f) for f in [new_raw_fname, args.epo_fname, args.evo_fname] ]) if all_done: all_done = \ all([op.isfile(f) for f in [args.inv_fname, args.fwd_fname]]) and \ utils.both_hemi_files_exist(stc_template_hemi.format(cond=cond, method=inverse_method, hemi='{hemi}')) # and \ # all([utils.both_hemi_files_exist(labels_data_template.format(args.atlas, em, '{hemi}')) and \ # op.isfile(meg.get_labels_minmax_template(labels_data_template).format(args.atlas, em)) # for em in args.extract_mode]) return all_done if calc_stc_per_session else True return all_done
def fix_unknown_labels(subject, atlas): for hemi in utils.HEMIS: labels = read_labels(subject, SUBJECTS_DIR, atlas, hemi=hemi) labels_names = [l.name for l in labels] while 'unknown-{}'.format(hemi) in labels_names: del labels[labels_names.index('unknown-{}'.format(hemi))] labels_names = [l.name for l in labels] mne.write_labels_to_annot(labels, subject=subject, parc=atlas, overwrite=True, subjects_dir=SUBJECTS_DIR, hemi=hemi) return utils.both_hemi_files_exist( op.join(SUBJECTS_DIR, subject, 'label', '{}-{}.annot'.format(atlas, '{hemi}')))
def read_hemi_labels(subject, subjects_dir, atlas, hemi, surf_name='pial', labels_fol=''): # todo: replace with labels utils read labels function labels_fol = op.join(subjects_dir, subject, 'label', atlas) if labels_fol=='' else labels_fol annot_fname_template = op.join(subjects_dir, subject, 'label', '{}.{}.annot'.format('{hemi}', atlas)) if utils.both_hemi_files_exist(annot_fname_template): labels = mne.read_labels_from_annot(subject, atlas, hemi, surf_name) if len(labels) == 0: raise Exception('No labels were found in the {} annot file!'.format(annot_fname_template)) else: labels = [] for label_file in glob.glob(op.join(labels_fol, '*{}.label'.format(hemi))): label = mne.read_label(label_file) labels.append(label) if len(labels) == 0: raise Exception('No labels were found in {}!'.format(labels_fol)) return labels
def read_hemi_labels(subject, subjects_dir, atlas, hemi, surf_name='pial', labels_fol=''): # todo: replace with labels utils read labels function labels_fol = op.join(subjects_dir, subject, 'label', atlas) if labels_fol=='' else labels_fol annot_fname_template = op.join(subjects_dir, subject, 'label', '{}.{}.annot'.format('{hemi}', atlas)) if utils.both_hemi_files_exist(annot_fname_template): labels = mne.read_labels_from_annot(subject, atlas, hemi, surf_name) if len(labels) == 0: raise Exception('No labels were found in the {} annot file!'.format(annot_fname_template)) else: labels = [] for label_file in glob.glob(op.join(labels_fol, '*{}.label'.format(hemi))): label = mne.read_label(label_file) labels.append(label) if len(labels) == 0: raise Exception('No labels were found in {}!'.format(labels_fol)) return labels
def norm_stc(subject, stc_name): norm_stc_template = op.join(MMVT_DIR, subject, 'meg', '{}-norm-{}.stc'.format(stc_name, '{hemi}')) if utils.both_hemi_files_exist(norm_stc_template): return norm_stc_template.format(hemi='lh') stc_fname = op.join(MMVT_DIR, subject, 'meg', '{}-lh.stc'.format(stc_name)) stc = mne.read_source_estimate(stc_fname) stc_max = utils.max_stc(stc) norm_data = stc.data / stc_max stc_norm = mne.SourceEstimate(norm_data, stc.vertices, 0, 0, subject=subject) stc_norm.save(op.join(MMVT_DIR, subject, 'meg', '{}-norm'.format(stc_name)))
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)
def create_lut_file_for_atlas(subject, atlas): if not utils.both_hemi_files_exist( op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format( '{hemi}', atlas))): print('No annot file was found for {}!'.format(atlas)) print( 'Run python -m src.preproc.anatomy -s {} -a {} -f create_surfaces,create_annotation' .format(subject, atlas)) return False # Read the subcortical segmentation from the freesurfer lut new_lut_fname = op.join(SUBJECTS_DIR, subject, 'label', '{}ColorLUT.txt'.format(atlas)) mmvt_lut_fname = op.join(MMVT_DIR, subject, 'freeview', '{}ColorLUT.txt'.format(atlas)) # if op.isfile(mmvt_lut_fname) and not args.overwrite_aseg_file: # return lut = utils.read_freesurfer_lookup_table(get_colors=True) lut_new = [[l[0], l[1].astype(str), l[2], l[3], l[4], l[5]] for l in lut if l[0] < 1000] for hemi, offset in zip(['lh', 'rh'], [1000, 2000]): if hemi == 'lh': lut_new.append([offset, 'ctx-lh-unknown', 25, 5, 25, 0]) else: lut_new.append([offset, 'ctx-rh-unknown', 25, 5, 25, 0]) _, ctab, names = _read_annot( op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format(hemi, atlas))) names = [name.astype(str) for name in names] for index, (label, cval) in enumerate(zip(names, ctab)): r, g, b, a, _ = cval lut_new.append([index + offset + 1, label, r, g, b, a]) lut_new.sort(key=lambda x: x[0]) # Add the values above 3000 for l in [l for l in lut if l[0] >= 3000]: lut_new.append([l[0], l[1].astype(str), l[2], l[3], l[4], l[5]]) with open(new_lut_fname, 'w') as fp: csv_writer = csv.writer(fp, delimiter='\t') csv_writer.writerows(lut_new) # np.savetxt(new_lut_fname, lut_new, delimiter='\t', fmt="%s") utils.make_dir(op.join(MMVT_DIR, subject, 'freeview')) shutil.copyfile(new_lut_fname, mmvt_lut_fname) lut_npz_fname = utils.change_fname_extension(mmvt_lut_fname, 'npz') x = np.genfromtxt(mmvt_lut_fname, dtype=np.str) np.savez(lut_npz_fname, names=x[:, 1], ids=x[:, 0].astype(int)) return op.isfile(mmvt_lut_fname) and op.isfile(lut_npz_fname)
def create_annotation_from_fsaverage(subject, aparc_name='aparc250', fsaverage='fsaverage', remote_subject_dir='', overwrite_annotation=False, overwrite_morphing=False, do_solve_labels_collisions=False, morph_labels_from_fsaverage=True, fs_labels_fol='', n_jobs=6): annotations_exist = np.all([op.isfile(op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format(hemi, aparc_name))) for hemi in HEMIS]) if not annotations_exist: utils.make_dir(op.join(SUBJECTS_DIR, subject, 'label')) remote_annotations_exist = np.all([op.isfile(op.join(remote_subject_dir, 'label', '{}.{}.annot'.format( hemi, aparc_name))) for hemi in HEMIS]) if remote_annotations_exist: for hemi in HEMIS: shutil.copy(op.join(remote_subject_dir, 'label', '{}.{}.annot'.format(hemi, aparc_name)), op.join(SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format(hemi, aparc_name))) return True existing_freesurfer_annotations = ['aparc.DKTatlas40.annot', 'aparc.annot', 'aparc.a2009s.annot'] if '{}.annot'.format(aparc_name) in existing_freesurfer_annotations: morph_labels_from_fsaverage = False do_solve_labels_collisions = False if not annotations_exist: utils.make_dir(op.join(SUBJECTS_DIR, subject, 'label')) annotations_exist = fu.create_annotation_file(subject, aparc_name, subjects_dir=SUBJECTS_DIR, freesurfer_home=FREE_SURFER_HOME) if morph_labels_from_fsaverage: lu.morph_labels_from_fsaverage(subject, SUBJECTS_DIR, MMVT_DIR, aparc_name, n_jobs=n_jobs, fsaverage=fsaverage, overwrite=overwrite_morphing, fs_labels_fol=fs_labels_fol) if do_solve_labels_collisions: solve_labels_collisions(subject, aparc_name, fsaverage, n_jobs) # Note that using the current mne version this code won't work, because of collissions between hemis # You need to change the mne.write_labels_to_annot code for that. if overwrite_annotation or not annotations_exist: try: utils.labels_to_annot(subject, SUBJECTS_DIR, aparc_name, overwrite=overwrite_annotation) except: print("Can't write labels to annotation! Trying to solve labels collision") print(traceback.format_exc()) solve_labels_collisions(subject, aparc_name, fsaverage, n_jobs) try: utils.labels_to_annot(subject, SUBJECTS_DIR, aparc_name, overwrite=overwrite_annotation) except: print("Can't write labels to annotation! Solving the labels collision didn't help...") print(traceback.format_exc()) return utils.both_hemi_files_exist(op.join( SUBJECTS_DIR, subject, 'label', '{}.{}.annot'.format('{hemi}', aparc_name)))
def save_hemis_curv(subject, atlas): out_curv_file = op.join(MMVT_DIR, subject, 'surf', '{hemi}.curv.npy') # out_border_file = op.join(MMVT_DIR, subject, 'surf', '{hemi}.curv.borders.npy') # if utils.both_hemi_files_exist(out_file): # return True for hemi in utils.HEMIS: # Load in curvature values from the ?h.curv file. if not op.isfile(out_curv_file.format(hemi=hemi)): curv_path = op.join(SUBJECTS_DIR, subject, 'surf', '{}.curv'.format(hemi)) curv = nib.freesurfer.read_morph_data(curv_path) bin_curv = np.array(curv > 0, np.int) np.save(out_curv_file.format(hemi=hemi), bin_curv) else: bin_curv = np.load(out_curv_file.format(hemi=hemi)) labels_fol = op.join(MMVT_DIR, subject, 'surf', '{}_{}_curves'.format(atlas, hemi)) utils.make_dir(labels_fol) labels = lu.read_labels(subject, SUBJECTS_DIR, atlas, hemi=hemi) for label in labels: labels_curv = bin_curv[label.vertices] np.save(op.join(labels_fol, '{}_curv.npy'.format(label.name)), labels_curv) return utils.both_hemi_files_exist(out_curv_file) # and utils.both_hemi_files_exist(out_border_file)
def save_hemis_curv(subject, atlas): out_curv_file = op.join(MMVT_DIR, subject, 'surf', '{hemi}.curv.npy') # out_border_file = op.join(MMVT_DIR, subject, 'surf', '{hemi}.curv.borders.npy') # if utils.both_hemi_files_exist(out_file): # return True for hemi in utils.HEMIS: # Load in curvature values from the ?h.curv file. if not op.isfile(out_curv_file.format(hemi=hemi)): curv_path = op.join(SUBJECTS_DIR, subject, 'surf', '{}.curv'.format(hemi)) curv = nib.freesurfer.read_morph_data(curv_path) bin_curv = np.array(curv > 0, np.int) np.save(out_curv_file.format(hemi=hemi), bin_curv) else: bin_curv = np.load(out_curv_file.format(hemi=hemi)) labels_fol = op.join(MMVT_DIR, subject, 'surf', '{}_{}_curves'.format(atlas, hemi)) utils.make_dir(labels_fol) labels = lu.read_labels(subject, SUBJECTS_DIR, atlas, hemi=hemi) for label in labels: labels_curv = bin_curv[label.vertices] np.save(op.join(labels_fol, '{}_curv.npy'.format(label.name)), labels_curv) return utils.both_hemi_files_exist(out_curv_file) # and utils.both_hemi_files_exist(out_border_file)
def create_spatial_connectivity(subject): try: verts_neighbors_fname = op.join(MMVT_DIR, subject, 'verts_neighbors_{hemi}.pkl') connectivity_fname = op.join(MMVT_DIR, subject, 'spatial_connectivity.pkl') if utils.both_hemi_files_exist(verts_neighbors_fname) and op.isfile(verts_neighbors_fname): return True connectivity_per_hemi = {} for hemi in utils.HEMIS: neighbors = defaultdict(list) d = np.load(op.join(MMVT_DIR, subject, 'surf', '{}.pial.npz'.format(hemi))) connectivity_per_hemi[hemi] = mne.spatial_tris_connectivity(d['faces']) rows, cols = connectivity_per_hemi[hemi].nonzero() for ind in range(len(rows)): neighbors[rows[ind]].append(cols[ind]) utils.save(neighbors, verts_neighbors_fname.format(hemi=hemi)) utils.save(connectivity_per_hemi, connectivity_fname) success = True except: print('Error in create_spatial_connectivity!') print(traceback.format_exc()) success = False return success
def freesurfer_surface_to_blender_surface(subject, hemi='both', overwrite=False): for hemi in utils.get_hemis(hemi): surf_name = op.join(SUBJECTS_DIR, subject, 'surf', '{}.pial'.format(hemi)) surf_wavefront_name = '{}.asc'.format(surf_name) surf_new_name = '{}.srf'.format(surf_name) hemi_ply_fname = '{}.ply'.format(surf_name) mmvt_hemi_ply_fname = op.join(MMVT_DIR, subject, '{}.pial.ply'.format(hemi)) if overwrite or not op.isfile(hemi_ply_fname): print('{}: convert srf to asc'.format(hemi)) utils.run_script('mris_convert {} {}'.format(surf_name, surf_wavefront_name)) os.rename(surf_wavefront_name, surf_new_name) print('{}: convert asc to ply'.format(hemi)) convert_hemis_srf_to_ply(subject, hemi) for hemi in utils.get_hemis(hemi): if not op.isfile(mmvt_hemi_ply_fname): shutil.copy(hemi_ply_fname, mmvt_hemi_ply_fname) ply_fname = op.join(SUBJECTS_DIR, subject, 'surf', '{}.pial.ply'.format(hemi)) verts, faces = utils.read_ply_file(ply_fname) np.savez(op.join(SUBJECTS_DIR, subject, 'mmvt', '{}.pial'.format(hemi)), verts=verts, faces=faces) shutil.copyfile(op.join(SUBJECTS_DIR, subject, 'mmvt', '{}.pial.npz'.format(hemi)), op.join(MMVT_DIR, subject, '{}.pial.npz'.format(hemi))) return utils.both_hemi_files_exist(op.join(SUBJECTS_DIR, subject, 'surf', '{hemi}.pial.ply'))