def test_label_io(): """Test IO of label files """ label = read_label(label_fname) write_label('foo', label) label2 = read_label('foo-lh.label') for key in label.keys(): if key in ['comment', 'hemi']: assert_true(label[key] == label2[key]) else: assert_array_almost_equal(label[key], label2[key], 5)
def _merge_rois(mer_path, label_list): """ Function to merge a list of given labels. Parameters ---------- mer_path: str The directory for storing merged ROIs. label_list: list Labels to be merged """ class_list = [] class_list.append(label_list[0]) for test_fn in label_list[1:]: test_label = mne.read_label(test_fn) i = 0 belong = False while (i < len(class_list)) and (belong is False): class_label = mne.read_label(class_list[i]) label_name = class_label.name if test_label.hemi != class_label.hemi: i = i + 1 continue overlapped = len( np.intersect1d(test_label.vertices, class_label.vertices)) if overlapped > 0: com_label = test_label + class_label pre_test = test_label.name.split('_')[0] pre_class = class_label.name.split('_')[0] # label_name = pre_class + '_%s-%s' %(pre_test,class_label.name.split('-')[-1]) if pre_test != pre_class: pre_class += ',%s' % pre_test pre_class = list(set(pre_class.split(','))) new_pre = '' for pre in pre_class[:-1]: new_pre += '%s,' % pre new_pre = pre_class[-1] label_name = '%s_' % (new_pre) + \ class_label.name.split('_')[-1] os.remove(class_list[i]) os.remove(test_fn) fn_newlabel = mer_path + '%s.label' % label_name if os.path.isfile(fn_newlabel): fn_newlabel = fn_newlabel[:fn_newlabel.rfind( '_')] + '_new, %s' % fn_newlabel.split('_')[-1] mne.write_label(fn_newlabel, com_label) class_list[i] = fn_newlabel belong = True i = i + 1 if belong is False: class_list.append(test_fn) return len(class_list)
def _merge_rois(mer_path, label_list): """ Function to merge a list of given labels. Parameters ---------- mer_path: str The directory for storing merged ROIs. label_list: list Labels to be merged """ class_list = [] class_list.append(label_list[0]) for test_fn in label_list[1:]: test_label = mne.read_label(test_fn) i = 0 belong = False while (i < len(class_list)) and (belong is False): class_label = mne.read_label(class_list[i]) label_name = class_label.name if test_label.hemi != class_label.hemi: i = i + 1 continue overlapped = len(np.intersect1d(test_label.vertices, class_label.vertices)) if overlapped > 0: com_label = test_label + class_label pre_test = test_label.name.split('_')[0] pre_class = class_label.name.split('_')[0] # label_name = pre_class + '_%s-%s' %(pre_test,class_label.name.split('-')[-1]) if pre_test != pre_class: pre_class += ',%s' % pre_test pre_class = list(set(pre_class.split(','))) new_pre = '' for pre in pre_class[:-1]: new_pre += '%s,' % pre new_pre = pre_class[-1] label_name = '%s_' % (new_pre) + \ class_label.name.split('_')[-1] os.remove(class_list[i]) os.remove(test_fn) fn_newlabel = mer_path + '%s.label' %label_name if os.path.isfile(fn_newlabel): fn_newlabel = fn_newlabel[:fn_newlabel.rfind('_')] + '_new, %s' % fn_newlabel.split('_')[-1] mne.write_label(fn_newlabel, com_label) class_list[i] = fn_newlabel belong = True i = i + 1 if belong is False: class_list.append(test_fn) return len(class_list)
def ROIs_Merging(subject): import os, mne import numpy as np subject_path = subjects_dir + subject list_dirs = os.walk(subject_path + '/func_labels/') #list_dirs = os.walk(subjects_dir + subject) #color = ['#990033', '#9900CC', '#FF6600', '#FF3333', '#00CC33'] #/home/qdong/freesurfer/subjects/101611/func_labels/stim_func_superiortemporal-lh.label tri_list = [''] res_list = [''] for root, dirs, files in list_dirs: for f in files: label_fname = os.path.join(root, f) if f[0:3] == 'tri': tri_list.append(label_fname) elif f[0:3] == 'res': res_list.append(label_fname) tri_list = tri_list[1:] res_list = res_list[1:] mer_path = subject_path + '/func_labels/merged/' isExists = os.path.exists(mer_path) if not isExists: os.makedirs(mer_path) com_list = [''] for fn_tri in tri_list: tri_label = mne.read_label(fn_tri) com_label = tri_label.copy() for fn_res in res_list: res_label = mne.read_label(fn_res) if tri_label.hemi != res_label.hemi: continue if len(np.intersect1d(tri_label.vertices, res_label.vertices)) > 0: com_label = tri_label + res_label tri_label.name += ',%s' % res_label.name com_list.append( fn_res) #Keep the overlapped ROIs related with res mne.write_label(mer_path + '%s' % tri_label.name, com_label) # save the independent res ROIs com_list = com_list[1:] ind_list = list(set(res_list) - set(com_list)) for fn_res in ind_list: res_label = mne.read_label(fn_res) res_label.save(mer_path + '%s' % res_label.name)
n_jobs=n_jobs, seed=0) print "Time elapsed : %s (s)" % (time() - t0) clusters = [c.reshape(n_times, n_vertices).T for c in clusters] #you get a cluster for every single thing that crossed the first-stage threshold # stc_log_pv_cluster = copy.deepcopy(mean_stc1) # stc_log_pv_cluster.data = np.zeros_like(stc_log_pv_cluster.data) # for pv, c in zip(cluster_pv, clusters): # stc_log_pv_cluster.data[c] = -np.log10(pv) # # stc_log_pv_cluster.save(prefix + 'clusters_pv_%s_%s' % (stat_name, t)) stc_cluster = copy.deepcopy(template_stc) #you only write out a cluster to an stc file if it crosses the second-stage threshold for k, c in enumerate(clusters): stc_cluster._data = c if cluster_pv[ k] < 0.3: ##This is the threshold for saving an stc file with cluster stcFileName = '/cluster/kuperberg/SemPrMM/MEG/results/source_space/cluster_stats/' + prefix + '%d-%d_cluster%d_%s_thresh_%s_pv_%.3f' % ( args.t1 * 1000, args.t2 * 1000, k, stat_name, t, cluster_pv[k]) print stcFileName stc_cluster.save(stcFileName) #stc_cluster.save('/cluster/kuperberg/SemPrMM/MEG/results/source_space/cluster_stats/' + prefix + '%d-%d_cluster%d_%s_thresh_%s_pv_%.3f' % (args.t1*1000,args.t2*1000,k, stat_name, t, cluster_pv[k])) labelArray = mne.stc_to_label(stc_cluster, 'fsaverage') label = labelArray[0] mne.write_label(stcFileName, label) print 'pv : %s' % np.sort(cluster_pv)[:5]
''' Creates labels baased on the MNI teamplate descriptions that Philip sent me for the subcortical analysis ''' import mne import numpy as np import env fname = '/Users/sudregp/Documents/surfaces/thalamus_right_morpho_labels_test.txt' hemi = 'rh' labels = np.genfromtxt(fname) for label_num in np.unique(labels): label_name = env.fsl + 'mni/label/' + hemi + '.' + str(int(label_num)) label = mne.Label(np.nonzero(labels == label_num)[0], hemi=hemi) mne.write_label(label_name, label)
def save_label(self, filename): """ save the label of the surface """ mne.write_label(filename, self.label)
belong = False while (i < len(class_list)) and (belong == False): class_label = mne.read_label(class_list[i]) label_name = class_label.name com_label = class_label.copy() if test_label.hemi != class_label.hemi: i = i + 1 continue if len(np.intersect1d(test_label.vertices, class_label.vertices)) > 0: com_label = test_label + class_label if test_label.name.split('_')[0] not in label_name: label_name = '%s,' % ( test_label.name.split('_')[0]) + label_name if os.path.dirname(class_list[i]) == mer_path: os.remove(class_list[i]) mne.write_label(mer_path + '/%s' % label_name, com_label) print label_name class_list[i] = mer_path + '/%s.label' % label_name belong = True i = i + 1 if belong == False: class_list.append(test_fn) import shutil com_path = subject_path + '/func_labels/common' list_dirs = os.walk(subject_path + '/func_labels/merged/') label_list = [''] for root, dirs, files in list_dirs: for f in files: label_fname = os.path.join(root, f) label_list.append(label_fname)
n_permutations=n_permutations, tail=0, stat_fun=stat_fun, connectivity=connectivity, n_jobs=n_jobs, seed=0) print "Time elapsed : %s (s)" % (time() - t0) clusters = [c.reshape(n_times, n_vertices).T for c in clusters] #you get a cluster for every single thing that crossed the first-stage threshold # stc_log_pv_cluster = copy.deepcopy(mean_stc1) # stc_log_pv_cluster.data = np.zeros_like(stc_log_pv_cluster.data) # for pv, c in zip(cluster_pv, clusters): # stc_log_pv_cluster.data[c] = -np.log10(pv) # # stc_log_pv_cluster.save(prefix + 'clusters_pv_%s_%s' % (stat_name, t)) stc_cluster = copy.deepcopy(template_stc) #you only write out a cluster to an stc file if it crosses the second-stage threshold for k, c in enumerate(clusters): stc_cluster.data = c if cluster_pv[k] < 0.15: ##This is the threshold for saving an stc file with cluster stcFileName = '/cluster/kuperberg/SemPrMM/MEG/results/source_space/cluster_stats/' + prefix + '%d-%d_cluster%d_%s_thresh_%s_pv_%.3f' % (args.t1*1000,args.t2*1000,k, stat_name, t, cluster_pv[k]) stc_cluster.save(stcFileName) #stc_cluster.save('/cluster/kuperberg/SemPrMM/MEG/results/source_space/cluster_stats/' + prefix + '%d-%d_cluster%d_%s_thresh_%s_pv_%.3f' % (args.t1*1000,args.t2*1000,k, stat_name, t, cluster_pv[k])) labelArray = mne.stc_to_label(stc_cluster, 'fsaverage') label = labelArray[0] mne.write_label(stcFileName, label) print 'pv : %s' % np.sort(cluster_pv)[:5]
def create_source_models(subject, save=False, json_fname='default'): """ Create cortical and subcortical source models Pipeline for: i) importing BrainVISA white meshes for positions and MarsAtlas textures for areas ii) create transformation file from BV to head coordinates iii) create source spaces with cortical and subcortical dipoles, Parameters ---------- subject : str Subject name database : To delete, database reference for trans file, useless from next version save : bool | True Allows to save source spaces and respective labels in the default directory Returns ------- surf_src : instance of SourceSpace Cortical surface source spaces, lh and rh surf_labels : instance of Labels Cortical surfaces labels vol_src : instance of VolSourceSpace Subcortical volumes source space, lh and rh vol_labels : instance of Labels Subcortical volumes Labels """ if json_fname == 'default': read_dir = op.join(op.abspath(__package__), 'config') json_fname = op.join(read_dir, 'db_info.son') database, project, db_mne, db_bv, db_fs = read_databases(json_fname) raw_dir, prep_dir, trans_dir, mri_dir, src_dir, bem_dir, fwd_dir, hga_dir = read_directories( json_fname) ########################################################################### # ------------------------------------------------------------------------- # BrainVISA anatomical data # ------------------------------------------------------------------------- # BV decimated white meshes (cortical sources) fname_surf_L = op.join(db_bv, project, subject, 't1mri', 'default_acquisition', 'default_analysis', 'segmentation', 'mesh', 'surface_analysis', '{0}_Lwhite_remeshed_hiphop.gii'.format(subject)) fname_surf_R = op.join(db_bv, project, subject, 't1mri', 'default_acquisition', 'default_analysis', 'segmentation', 'mesh', 'surface_analysis', '{0}_Rwhite_remeshed_hiphop.gii'.format(subject)) # BV texture (MarsAtlas labels) for decimated white meshes # (cortical sources) fname_tex_L = op.join(db_bv, 'hiphop138-multiscale', 'Decimated', '4K', 'hiphop138_Lwhite_dec_4K_parcels_marsAtlas.gii') fname_tex_R = op.join(db_bv, 'hiphop138-multiscale', 'Decimated', '4K', 'hiphop138_Rwhite_dec_4K_parcels_marsAtlas.gii') # Labelling xls file fname_atlas = op.join(db_mne, project, 'marsatlas', 'MarsAtlas_BV_2015.xls') # Color palette (still used???) fname_color = op.join(db_mne, project, 'marsatlas', 'MarsAtlas.ima') # MarsAtlas volume parcellation fname_vol = op.join(db_bv, project, subject, 't1mri', 'default_acquisition', 'default_analysis', 'segmentation', 'mesh', 'surface_analysis', '{0}_parcellation.nii.gz'.format(subject)) # ------------------------------------------------------------------------- # Transformation files BV to FS # ------------------------------------------------------------------------- # Referential file list # (3 transformation files to transform BV meshes to FS space) fname_trans_ref = op.join(db_mne, project, 'referential', 'referential.txt') # This file contains the transformations for subject_01 fname_trans_out = op.join(db_mne, project, subject, 'ref', '{0}-trans.trm'.format(subject)) name_lobe_vol = ['Subcortical'] # --------------------------------------------------------------------- # Setting up the source space from BrainVISA results # --------------------------------------------------------------------- # http://martinos.org/mne/stable/manual/cookbook.html#source-localization # Create .trm file transformation from BrainVisa to FreeSurfer needed # for brain.py function for surface only create_trans(subject, database, fname_trans_ref, fname_trans_out) # Calculate cortical sources and MarsAtlas labels print('\n---------- Cortical sources ----------\n') surf_src, surf_labels = get_brain_surf_sources(subject, fname_surf_L, fname_surf_R, fname_tex_L, fname_tex_R, fname_trans_out, fname_atlas, fname_color) if save == True: print('\nSaving surface source space and labels.....') mne.write_source_spaces(op.join(src_dir.format(subject), '{0}_surf-src.fif'.format(subject)), surf_src, overwrite=True) for sl in surf_labels: mne.write_label( op.join(src_dir.format(subject), '{0}_surf-lab'.format(subject)), sl) print('[done]') # Create BEM model if needed print('\nBEM model is needed for volume source space\n') if not check_bem(json_fname, subject): create_bem(json_fname, subject) print('\n---------- Subcortical sources ----------\n') vol_src, vol_labels = get_brain_vol_sources(subject, fname_vol, json_fname, name_lobe_vol, fname_trans_out, fname_atlas, space=5.) if save == True: print('Saving volume source space and labels.....') mne.write_source_spaces(op.join(src_dir.format(subject), '{0}_vol-src.fif'.format(subject)), vol_src, overwrite=True) for vl in vol_labels: mne.write_label( op.join(src_dir.format(subject), '{0}_vol-lab'.format(subject)), vl) print('[done]') # print('\n---------- Sources Completed ----------\n') return surf_src, surf_labels, vol_src, vol_labels
for fn_label1 in label_list: label1 = mne.read_label(fn_label1) com_label = label1.copy() label_name = label1.name for fn_label2 in label_list: label2 = mne.read_label(fn_label2) if label1.hemi != label2.hemi: continue if len(np.intersect1d(label1.vertices, label2.vertices)) > 0: com_label = label1 + label2 if label2.name.split('_')[0] not in label_name: label_name = '%s,' %(label2.name.split('_')[0]) + label_name subjects = (label_name.split('_')[0]).split(',') if len(set(subjects)) >= 4: mne.write_label(mer_path + '%s' %label_name, com_label) # mne.write_label(mer_path + '%s' %label1.name, com_label) list_dirs = os.walk(mer_path) label_list = [''] for root, dirs, files in list_dirs: for f in files: label_fname = os.path.join(root, f) label_list.append(label_fname) label_list=label_list[1:] com_path = subject_path+'/func_labels/common/' isExists=os.path.exists(com_path) if not isExists: os.makedirs(com_path)
def create_source_models(subject, bem_dir=None, trans_dir=None, src_dir=None, json_fname='default', decim='orig', save=False): """ Create cortical and subcortical source models Pipeline for: i) importing BrainVISA white meshes for positions and MarsAtlas textures for areas ii) create transformation file from BV to head coordinates iii) create source spaces with cortical and subcortical dipoles, Parameters ---------- subject : str Subject name database : To delete, database reference for trans file, useless from next version save : bool | True Allows to save source spaces and respective labels in the default directory Returns ------- surf_src : instance of SourceSpace Cortical surface source spaces, lh and rh surf_labels : instance of Labels Cortical surfaces labels vol_src : instance of VolSourceSpace Subcortical volumes source space, lh and rh vol_labels : instance of Labels Subcortical volumes Labels """ # if json_fname == 'default': # read_dir = op.join(op.abspath(__package__), 'config') # json_fname = op.join(read_dir, 'db_coords.json') # # database, project, db_mne, db_bv, db_fs = read_databases(json_fname) # raw_dir, prep_dir, trans_dir, mri_dir, src_dir, bem_dir, fwd_dir, hga_dir = read_directories(json_fname) ########################################################################### # ------------------------------------------------------------------------- # BrainVISA anatomical data # ------------------------------------------------------------------------- db_fs, db_bv, db_mne = read_db_coords(json_fname) if db_mne != None: _, _, trans_dir, _, src_dir, bem_dir, _, _ = mne_directories(db_mne) trans_dir = trans_dir.format(subject) src_dir = src_dir.format(subject) bem_dir = bem_dir.format(subject) assert decim in [ 'orig', '1K', '2K', '3K', '4K' ], 'decim should be one of this string: orig, 1K, 2K, 3K, 4K' assert op.exists(bem_dir), 'bem_dir is not an existing folder' assert op.exists(trans_dir), 'trans_dir is not an existing folder' assert op.exists(src_dir), 'src_dir is not an existing folder' # BV decimated white meshes (cortical sources) # fname_surf_L = op.join(db_bv, project, subject, 't1mri', 'default_acquisition', 'default_analysis', 'segmentation', # 'mesh', 'surface_analysis', '{0}_Lwhite_remeshed_hiphop.gii'.format(subject)) # # fname_surf_R = op.join(db_bv, project, subject, 't1mri', 'default_acquisition', 'default_analysis', 'segmentation', # 'mesh', 'surface_analysis', '{0}_Rwhite_remeshed_hiphop.gii'.format(subject)) # BV texture (MarsAtlas labels) for decimated white meshes # (cortical sources) if decim == 'orig': # BV decimated white meshes (cortical sources) fname_surf_L = op.join(db_bv, subject, 't1mri', 'default_acquisition', 'default_analysis', 'segmentation', 'mesh', '{0}_Lwhite.gii'.format(subject)) fname_surf_R = op.join(db_bv, subject, 't1mri', 'default_acquisition', 'default_analysis', 'segmentation', 'mesh', '{0}_Rwhite.gii'.format(subject)) # BV texture (MarsAtlas labels) for decimated white meshes fname_tex_L = op.join( db_bv, subject, 't1mr', 'default_acquisition', 'default_analysis', 'segmentation', 'mesh', 'surface_analysis', '{0}_Lwhite_parcels_marsAtlas.gii'.format(subject)) fname_tex_R = op.join( db_bv, subject, 't1mr', 'default_acquisition', 'default_analysis', 'segmentation', 'mesh', 'surface_analysis', '{0}_Rwhite_parcels_marsAtlas.gii'.format(subject)) else: if not op.exists(op.join(db_bv, 'hiphop138-multiscale')): get_decimated(db_bv) # BV decimated white meshes (cortical sources) fname_surf_L = op.join( db_bv, subject, 't1mri', 'default_acquisition', 'default_analysis', 'segmentation', 'mesh', 'surface_analysis', '{0}_Lwhite_remeshed_hiphop.gii'.format(subject)) fname_surf_R = op.join( db_bv, subject, 't1mri', 'default_acquisition', 'default_analysis', 'segmentation', 'mesh', 'surface_analysis', '{0}_Rwhite_remeshed_hiphop.gii'.format(subject)) # BV texture (MarsAtlas labels) for decimated white meshes fname_tex_L = op.join( db_bv, 'hiphop138-multiscale', 'Decimated', decim, 'hiphop138_Lwhite_dec_{0}_parcels_marsAtlas.gii'.format(decim)) fname_tex_R = op.join( db_bv, 'hiphop138-multiscale', 'Decimated', decim, 'hiphop138_Rwhite_dec_{0}_parcels_marsAtlas.gii'.format(decim)) # Labelling xls file # fname_atlas = op.join(db_mne, project, 'marsatlas', 'MarsAtlas_BV_2015.xls') # Color palette (still used???) # fname_color = op.join(db_mne, project, 'marsatlas', 'MarsAtlas.ima') # MarsAtlas volume parcellation # fname_vol = op.join(db_bv, subject, 't1mri', 'default_acquisition', 'default_analysis', 'segmentation', # 'mesh', 'surface_analysis', '{0}_parcellation.nii.gz'.format(subject)) # ------------------------------------------------------------------------- # Transformation files BV to FS # ------------------------------------------------------------------------- # Referential file list # (3 transformation files to transform BV meshes to FS space) # fname_trans_ref = op.join(db_mne, project, 'referential', 'referential.txt') # This file contains the transformations trans_out = op.join(trans_dir, '{0}-trans.trm'.format(subject)) # if trans_out is None: # trans_out = op.join(db_bv, project, subject, 't1mri', 'default_acquisition', 'default_analysis', # 'segmentation', 'mesh', '{0}-trans.trm'.format(subject)) # elif type(trans_out) == str: # trans_out = op.join(trans_out, '{0}-trans.trm'.format(subject)) # name_lobe_vol = ['Subcortical'] # --------------------------------------------------------------------- # Setting up the source space from BrainVISA results # --------------------------------------------------------------------- # http://martinos.org/mne/stable/manual/cookbook.html#source-localization # Create .trm file transformation from BrainVisa to FreeSurfer needed # for brain.py function for surface only create_trans(subject, db_fs, db_bv, trans_out) # Calculate cortical sources and MarsAtlas labels print('\n---------- Cortical sources ----------\n') surf_src, surf_labels = get_brain_surf_sources(subject, fname_surf_L, fname_surf_R, fname_tex_L, fname_tex_R, trans_out) if save == True: # src_dir = op.join(db_mne, project, subject, 'src') # if not op.exists(src_dir): # os.mkdir(src_dir) # print('\nSaving surface source space and labels in {0}'.format(src_dir)) # mne.write_source_spaces(op.join(src_dir.format(subject), '{0}_surf-src.fif'.format(subject)), surf_src, overwrite=True) # for sl in surf_labels: # mne.write_label(op.join(src_dir.format(subject), '{0}_surf-lab'.format(subject)), sl) # print('[done]') print( '\nSaving surface source space and labels in {0}'.format(src_dir)) mne.write_source_spaces(op.join(src_dir, '{0}_surf-src.fif'.format(subject)), surf_src, overwrite=True) for sl in surf_labels: mne.write_label(op.join(src_dir, '{0}_surf-lab'.format(subject)), sl) print('[done]') # Create BEM model if needed print('\nBEM model is needed for volume source space\n') if not check_bem(subject, bem_dir): print('BEM model not found... creating bem model') create_bem(subject, bem_dir, json_fname) print('\n---------- Subcortical sources ----------\n') vol_src, vol_labels = get_brain_vol_sources(subject, bem_dir, space=5., json_fname=json_fname) if save == True: print('Saving volume source space and labels in {0}'.format(src_dir)) mne.write_source_spaces(op.join(src_dir, '{0}_vol-src.fif'.format(subject)), vol_src, overwrite=True) for vl in vol_labels: mne.write_label(op.join(src_dir, '{0}_vol-lab'.format(subject)), vl) print('[done]') # print('\n---------- Sources Completed ----------\n') return surf_src, surf_labels, vol_src, vol_labels