def save_rois_connectivity(subject, args): # atlas, mat_fname, mat_field, conditions, stat=STAT_DIFF, windows=0, # labels_exclude=['unknown', 'corpuscallosum'], threshold=0, threshold_percentile=0, # color_map='jet', norm_by_percentile=True, norm_percs=(1, 99), symetric_colors=True): # args.atlas, args.mat_fname, args.mat_field, args.conditions, args.stat, # args.windows, args.labels_exclude, args.threshold, args.threshold_percentile, # args.color_map, args.norm_by_percentile, args.norm_percs) d = dict() data = sio.loadmat(args.mat_fname)[args.mat_field] d['labels'] = lu.read_labels(subject, SUBJECTS_DIR, args.atlas, exclude=args.labels_exclude, sorted_according_to_annot_file=True) d['locations'] = lu.calc_center_of_mass(d['labels'], ret_mat=True) * 1000 d['hemis'] = ['rh' if l.hemi == 'rh' else 'lh' for l in d['labels']] d['labels'] = [l.name for l in d['labels']] (d['con_colors'], d['con_indices'], d['con_names'], d['con_values'], d['con_types'], d['data_max'], d['data_min']) = calc_connections_colors(data, d['labels'], d['hemis'], args) # args.stat, args.conditions, args.windows, args.threshold, # args.threshold_percentile, args.color_map, args.norm_by_percentile, args.norm_percs, args.symetric_colors) d['conditions'] = args.conditions np.savez(op.join(BLENDER_ROOT_DIR, subject, 'rois_con'), **d)
def calc_labels_center_of_mass(subject, atlas, read_from_annotation=True, surf_name='pial', labels_fol='', labels=None): import csv # if (read_from_annotation): # labels = mne.read_labels_from_annot(subject, atlas, 'both', surf_name, subjects_dir=SUBJECTS_DIR) # if len(labels) == 0: # print('No labels were found in {} annotation file!'.format(atlas)) # else: # labels = [] # if labels_fol == '': # labels_fol = op.join(SUBJECTS_DIR, subject, 'label', atlas) # for label_file in glob.glob(op.join(labels_fol, '*.label')): # label = mne.read_label(label_file) # labels.append(label) # if len(labels) == 0: # print('No labels were found in {}!'.format(labels_fol)) labels = lu.read_labels(subject, SUBJECTS_DIR, atlas) if len(labels) > 0: center_of_mass = lu.calc_center_of_mass(labels) with open(op.join(SUBJECTS_DIR, subject, 'label', '{}_center_of_mass.csv'.format(atlas)), 'w') as csvfile: writer = csv.writer(csvfile, delimiter=',') for label in labels: writer.writerow([label.name, *center_of_mass[label.name]]) com_fname = op.join(SUBJECTS_DIR, subject, 'label', '{}_center_of_mass.pkl'.format(atlas)) blend_fname = op.join(MMVT_DIR, subject, '{}_center_of_mass.pkl'.format(atlas)) utils.save(center_of_mass, com_fname) shutil.copyfile(com_fname, blend_fname) return len(labels) > 0 and op.isfile(com_fname) and op.isfile(blend_fname)
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 save_connectivity_to_blender(subject, atlas, data, conditions, stat, w=0, threshold=0, threshold_percentile=0): d = {} d['labels'] = lu.read_labels(subject, SUBJECTS_DIR, atlas, exclude=('unknown', 'corpuscallosum'), sorted_according_to_annot_file=True) d['locations'] = lu.calc_center_of_mass(d['labels'], ret_mat=True) d['hemis'] = ['rh' if l.hemi == 'rh' else 'lh' for l in d['labels']] d['labels'] = [l.name for l in d['labels']] d['con_colors'], d['con_indices'], d['con_names'], d['con_values'], d['con_types'] = \ calc_connections_colors(data, d['labels'], d['hemis'], stat, w, threshold_percentile=threshold_percentile) d['conditions'] = conditions np.savez(op.join(BLENDER_ROOT_DIR, subject, 'rois_con'), **d)
def save_connectivity_to_blender(subject, atlas, data, conditions, stat, w=0, threshold=0, threshold_percentile=0): d = {} d['labels'] = lu.read_labels(subject, SUBJECTS_DIR, atlas, exclude=['unknown', 'corpuscallosum'], sorted_according_to_annot_file=True) d['locations'] = lu.calc_center_of_mass(d['labels'], ret_mat=True) d['hemis'] = ['rh' if l.hemi == 'rh' else 'lh' for l in d['labels']] d['labels'] = [l.name for l in d['labels']] d['con_colors'], d['con_indices'], d['con_names'], d['con_values'], d['con_types'] = \ calc_connections_colors(data, d['labels'], d['hemis'], stat, w, threshold_percentile=threshold_percentile) d['conditions'] = conditions np.savez(op.join(BLENDER_ROOT_DIR, subject, 'rois_con'), **d)
def calc_distances_from_rois(subject, dist_threshold=0.05): from scipy.spatial.distance import cdist import nibabel as nib dipoles_dict = utils.load(op.join(MMVT_DIR, subject, 'meg', 'dipoles.pkl')) labels_times_fol = op.join(MMVT_DIR, subject, 'meg', 'time_accumulate') labels = lu.read_labels(subject, SUBJECTS_DIR, 'laus125') labels_center_of_mass = lu.calc_center_of_mass(labels) labels_pos = np.array([labels_center_of_mass[l.name] for l in labels]) labels_dict = {l.name: labels_center_of_mass[l.name] for l in labels} outer_skin_surf_fname = op.join(SUBJECTS_DIR, subject, 'surf', 'lh.seghead') outer_skin_surf_verts, _ = nib.freesurfer.read_geometry(outer_skin_surf_fname) for dipole_name, dipoles in dipoles_dict.items(): dipole_pos = np.array([dipoles[0][2], dipoles[0][3], dipoles[0][4]]) lables_times_fname = op.join(labels_times_fol, '{}_labels_times.txt'.format(dipole_name)) if not op.isfile(lables_times_fname): print('Can\'t find {}!'.format(lables_times_fname)) continue dists_from_outer_skin = np.min(cdist(outer_skin_surf_verts * 0.001, [dipole_pos]), 0)[0] output_fname = op.join(labels_times_fol, '{}_labels_times_dists.txt'.format(dipole_name)) lines = utils.csv_file_reader(lables_times_fname, delimiter=':', skip_header=1) output, dists = [], [] labels_dists = cdist(labels_pos, [dipole_pos]) dists_argmin = np.argmin(labels_dists, 0)[0] dists_min = np.min(labels_dists, 0)[0] closest_label = labels[dists_argmin].name print('Parsing {} ({})'.format(dipole_name, closest_label)) for line in lines: if len(line) == 0: continue elif len(line) != 2: print('{}: Problem parsing "{}"'.format(lables_times_fname, line)) continue label_name, label_time = line label_pos = labels_dict.get(label_name, None) if label_pos is not None: dist_from_dipole = np.linalg.norm(dipole_pos - label_pos) dists.append(dist_from_dipole) else: dist_from_dipole = -1 dists.append(np.nan) output.append('{}: {} ({:.4f})'.format(label_name, label_time, dist_from_dipole)) for ind, dist in enumerate(dists): if dist < dist_threshold: output[ind] = '{} ***'.format(output[ind]) title = '{}: {} {:.4f} dist from outer skin: {:.4f} '.format( dipole_name, closest_label, dists_min, dists_from_outer_skin) utils.save_arr_to_file(output, output_fname, title)
def calc_labels_center_of_mass(subject, atlas, read_from_annotation=True, surf_name='pial', labels_fol='', labels=None): import csv labels = lu.read_labels(subject, SUBJECTS_DIR, atlas) if len(labels) > 0: if np.all(labels[0].pos == 0): verts = {} for hemi in utils.HEMIS: verts[hemi], _ = utils.read_pial_npz(subject, MMVT_DIR, hemi) for label in labels: label.pos = verts[label.hemi][label.vertices] center_of_mass = lu.calc_center_of_mass(labels) with open(op.join(SUBJECTS_DIR, subject, 'label', '{}_center_of_mass.csv'.format(atlas)), 'w') as csvfile: writer = csv.writer(csvfile, delimiter=',') for label in labels: writer.writerow([label.name, *center_of_mass[label.name]]) com_fname = op.join(SUBJECTS_DIR, subject, 'label', '{}_center_of_mass.pkl'.format(atlas)) blend_fname = op.join(MMVT_DIR, subject, '{}_center_of_mass.pkl'.format(atlas)) utils.save(center_of_mass, com_fname) shutil.copyfile(com_fname, blend_fname) return len(labels) > 0 and op.isfile(com_fname) and op.isfile(blend_fname)
def save_rois_connectivity(subject, args): # atlas, mat_fname, mat_field, conditions, stat=STAT_DIFF, windows=0, # labels_exclude=['unknown', 'corpuscallosum'], threshold=0, threshold_percentile=0, # color_map='jet', norm_by_percentile=True, norm_percs=(1, 99), symetric_colors=True): # args.atlas, args.mat_fname, args.mat_field, args.conditions, args.stat, # args.windows, args.labels_exclude, args.threshold, args.threshold_percentile, # args.color_map, args.norm_by_percentile, args.norm_percs) d = dict() data = sio.loadmat(args.mat_fname)[args.mat_field] d['labels'] = lu.read_labels( subject, SUBJECTS_DIR, args.atlas, exclude=args.labels_exclude,sorted_according_to_annot_file=True) d['locations'] = lu.calc_center_of_mass(d['labels'], ret_mat=True) * 1000 d['hemis'] = ['rh' if l.hemi == 'rh' else 'lh' for l in d['labels']] d['labels'] = [l.name for l in d['labels']] (d['con_colors'], d['con_indices'], d['con_names'], d['con_values'], d['con_types'], d['data_max'], d['data_min']) = calc_connections_colors(data, d['labels'], d['hemis'], args) # args.stat, args.conditions, args.windows, args.threshold, # args.threshold_percentile, args.color_map, args.norm_by_percentile, args.norm_percs, args.symetric_colors) d['conditions'] = args.conditions np.savez(op.join(MMVT_DIR, subject, 'rois_con'), **d)