import io_meld as io import os #parse commandline arguments pointing to subject_dir etc parser = argparse.ArgumentParser(description='filter intrinsic curvature') parser.add_argument('subject_dir', type=str, help='path to subject dir') parser.add_argument('subject_ids', type=str, help='textfile containing list of subject ids') args = parser.parse_args() #save subjects dir and subject ids. import the text file containing subject ids subject_dir = args.subject_dir subject_ids_filename = args.subject_ids subject_ids = np.loadtxt(subject_dir + subject_ids_filename, dtype='str') hemis = ['lh', 'rh'] for h in hemis: for s in subject_ids: if not os.path.isfile(subject_dir + s + '/xhemi/surf_meld/' + h + '.pial.K_filtered.mgh'): demo = nb.load(subject_dir + s + '/surf_meld/' + h + '.pial.K.mgh') curvature = io.load_mgh(subject_dir + s + '/surf_meld/' + h + '.pial.K.mgh') curvature = np.absolute(curvature) io.save_mgh( subject_dir + s + '/surf_meld/' + h + '.pial.K_filtered.mgh', curvature, demo)
subject_ids=np.loadtxt(subject_dir+subject_ids_filename, dtype='str') measures=['.thickness_z_on_lh.sm10.mgh', '.asym.thickness_z.sm10.mgh', '.w-g.pct_z_on_lh.sm10.mgh','.asym.w-g.pct_z.sm10.mgh', '.pial.K_filtered_2_z_on_lh.sm20.mgh','.asym.pial.K_filtered_2_z.sm20.mgh', '.curv_on_lh.mgh','.sulc_on_lh.mgh', '.gm_FLAIR_0.75_z_on_lh.sm10.mgh','.gm_FLAIR_0.5_z_on_lh.sm10.mgh', '.gm_FLAIR_0.25_z_on_lh.sm10.mgh','.gm_FLAIR_0_z_on_lh.sm10.mgh', '.wm_FLAIR_0.5_z_on_lh.sm10.mgh','.wm_FLAIR_1_z_on_lh.sm10.mgh', '.asym.gm_FLAIR_0.75_z.sm10.mgh','.asym.gm_FLAIR_0.5_z.sm10.mgh', '.asym.gm_FLAIR_0.25_z.sm10.mgh','.asym.gm_FLAIR_0_z.sm10.mgh', '.asym.wm_FLAIR_0.5_z.sm10.mgh','.asym.wm_FLAIR_1_z.sm10.mgh'] hemis=['lh','rh'] demo=nb.load(subject_dir+subject_ids[0]+'/xhemi/surf/'+hemis[0]+measures[0]) for h in hemis: for m in measures: control_data=np.zeros((len(subject_ids),len(demo.get_data()))) k=-1 for s in subject_ids: k+=1 control_data[k]=io.import_mgh(subject_dir+s+'/xhemi/surf/'+h+m) mean=np.mean(control_data,axis=0) std=np.std(control_data,axis=0) io.save_mgh(subject_dir+'template_control/'+h+'.mu'+m,mean,demo) io.save_mgh(subject_dir+'template_control/'+h+'.std'+m,mean,demo)
#save subjects dir and subject ids. import the text file containing subject ids subject_dir=args.subject_dir subject_ids_filename=args.subject_ids subject_ids=np.loadtxt(subject_dir+subject_ids_filename, dtype='str') measures=['.thickness_z_on_lh.sm10.mgh', '.asym.thickness_z.sm10.mgh', '.w-g.pct_z_on_lh.sm10.mgh','.asym.w-g.pct_z.sm10.mgh', '.pial.K_filtered_2_z_on_lh.sm20.mgh','.asym.pial.K_filtered_2_z.sm20.mgh', '.curv_on_lh.mgh','.sulc_on_lh.mgh', '.gm_FLAIR_0.75_z_on_lh.sm10.mgh','.gm_FLAIR_0.5_z_on_lh.sm10.mgh', '.gm_FLAIR_0.25_z_on_lh.sm10.mgh','.gm_FLAIR_0_z_on_lh.sm10.mgh', '.wm_FLAIR_0.5_z_on_lh.sm10.mgh','.wm_FLAIR_1_z_on_lh.sm10.mgh', '.asym.gm_FLAIR_0.75_z.sm10.mgh','.asym.gm_FLAIR_0.5_z.sm10.mgh', '.asym.gm_FLAIR_0.25_z.sm10.mgh','.asym.gm_FLAIR_0_z.sm10.mgh', '.asym.wm_FLAIR_0.5_z.sm10.mgh','.asym.wm_FLAIR_1_z.sm10.mgh'] hemis=['lh','rh'] demo=nb.load(subject_dir+subject_ids[0]+'/xhemi/surf/'+hemis[0]+measures[0]) for h in hemis: for m in measures: control_mu=io.load_mgh('template_control/'+h+'.mu'+m) control_std=io.load_mgh('template_control/'+h+'.std'+m) for s in subject_ids: subject_measure=io.load_mgh(subject_dir+s+'/xhemi/surf/'+h+m) z_measure=(subject_measure-control_mu)/control_std) io.save_mgh(subject_dir+s+'/xhemi/surf/'+h+'inter_z.'+m,z_measure,demo)
) break print("Creating T1 control means and stds") for h in hemis: for m in measures: control_data = np.zeros( (len(control_subjects), len(demo.get_data()))) k = -1 for s in control_subjects: k += 1 control_data[k] = io.import_mgh(subject_dir + s + '/xhemi/surf_meld/' + h + m) mean = np.mean(control_data, axis=0) std = np.std(control_data, axis=0) io.save_mgh(os.path.join(control_dir, f, h + '.mu' + m), mean, demo) io.save_mgh(os.path.join(control_dir, f, h + '.std' + m), std, demo) #count how many FLAIR controls flair_controls = [] for fs_id in control_subjects: if os.path.isfile( subject_dir + fs_id + '/xhemi/surf_meld/lh.on_lh.intra_z.gm_FLAIR_0.75.sm10.mgh'): flair_controls.append(fs_id) if len(flair_controls) < 20: print( "Found " + str(len(flair_controls)) + " FLAIR controls. As this is less than 20, we will use MELD template FLAIR controls for normalisation step"
for h in hemis: for s in subject_ids: demo = nb.load( os.path.join(subject_dir, s, 'surf_meld', h + measures[0])) cortex = nb.freesurfer.io.read_label( os.path.join(subject_dir, s, 'label', h + '.cortex.label')) for m in measures: if not os.path.isfile( os.path.join(subject_dir, s, 'surf_meld', h + '.intra_z' + m)): subject_measure = io.load_mgh( os.path.join(subject_dir, s, 'surf_meld', h + m)) z_measure = (subject_measure - np.mean( subject_measure[cortex])) / np.std(subject_measure[cortex]) io.save_mgh( os.path.join(subject_dir, s, 'surf_meld', h + '.intra_z' + m), z_measure, demo) #only do FLAIR measures if present if os.path.isfile( os.path.join(subject_dir, s, 'surf_meld', h + flair_measures[0])): for m in flair_measures: if not os.path.isfile( os.path.join(subject_dir, s, 'surf_meld', h + '.intra_z' + m)): subject_measure = io.load_mgh( os.path.join(subject_dir, s, 'surf_meld', h + m)) z_measure = (subject_measure - np.mean(subject_measure[cortex])) / np.std( subject_measure[cortex]) io.save_mgh(
elif '15T' in fs_id: f='15T' if os.path.isfile(subject_dir + fs_id + '/xhemi/surf_meld/lh.on_lh.intra_z.gm_FLAIR_0.75.sm10.mgh'): measures=['.on_lh.intra_z.thickness.sm10.mgh', '.asym.on_lh.intra_z.thickness.sm10.mgh', '.on_lh.intra_z.w-g.pct.sm10.mgh','.asym.on_lh.intra_z.w-g.pct.sm10.mgh', '.on_lh.intra_z.pial.K_filtered.sm20.mgh','.asym.on_lh.intra_z.pial.K_filtered.sm20.mgh', '.on_lh.curv.mgh','.on_lh.sulc.mgh', '.asym.on_lh.curv.mgh','.asym.on_lh.sulc.mgh', '.on_lh.intra_z.gm_FLAIR_0.75.sm10.mgh','.on_lh.intra_z.gm_FLAIR_0.5.sm10.mgh', '.on_lh.intra_z.gm_FLAIR_0.25.sm10.mgh','.on_lh.intra_z.gm_FLAIR_0.sm10.mgh', '.on_lh.intra_z.wm_FLAIR_0.5.sm10.mgh','.on_lh.intra_z.wm_FLAIR_1.sm10.mgh', '.asym.on_lh.intra_z.gm_FLAIR_0.75.sm10.mgh','.asym.on_lh.intra_z.gm_FLAIR_0.5.sm10.mgh', '.asym.on_lh.intra_z.gm_FLAIR_0.25.sm10.mgh','.asym.on_lh.intra_z.gm_FLAIR_0.sm10.mgh', '.asym.on_lh.intra_z.wm_FLAIR_0.5.sm10.mgh','.asym.on_lh.intra_z.wm_FLAIR_1.sm10.mgh'] else: measures=['.on_lh.intra_z.thickness.sm10.mgh', '.asym.on_lh.intra_z.thickness.sm10.mgh', '.on_lh.intra_z.w-g.pct.sm10.mgh','.asym.on_lh.intra_z.w-g.pct.sm10.mgh', '.on_lh.intra_z.pial.K_filtered.sm20.mgh','.asym.on_lh.intra_z.pial.K_filtered.sm20.mgh', '.on_lh.curv.mgh','.on_lh.sulc.mgh', '.asym.on_lh.curv.mgh','.asym.on_lh.sulc.mgh'] demo=nb.load(subject_dir+fs_id+'/xhemi/surf_meld/'+hemis[0]+measures[0]) for h in hemis: for m in measures: control_mu=io.load_mgh(os.path.join(control_dir,f,h+'.mu'+m)) control_std=io.load_mgh(os.path.join(control_dir,f,h+'.std'+m)) subject_measure=io.load_mgh(subject_dir+fs_id+'/xhemi/surf_meld/'+h+m) z_measure=np.divide((subject_measure-control_mu),control_std,out=np.zeros_like(subject_measure), where=control_std!=0) io.save_mgh(subject_dir+fs_id+'/xhemi/surf_meld/'+h+'.inter_z'+m,z_measure,demo)