def collectStats(backgrounds): ''' Summarise cortical thickness in more than one subjects. 'backgrounds' should given in python list format. For each background, 1. Creates labels using makeLabel 2. Merges labels according to the roiDcit using mergeLabel 3. Estimates cortical thickness in each merged labels (dict) 4. Converts dict to pandas Dataframe using dictWithTuple2df 5. save df to freesurfer_dir/tmp/thick_kev.csv Than mean df is returned. ''' #collectStats('ha','ho',ha') # 8 cortex dictionary roiDict = get_cortical_rois() subjectDict = {} for background in backgrounds: # freesurfer sub-directory description FS_description = [ 'bem', 'mri', 'scripts', 'src', 'stats', 'surf', 'tmp' ] freesurfer_dir = ccncpy.subDirSearch(FS_description, background) print freesurfer_dir if len(freesurfer_dir) > 1: sys.exit( re.sub( '\s+', ' ', 'There are more than 1 freesurfer directory \ under {0}'.format(subject_loc))) else: freesurfer_dir = ''.join(freesurfer_dir) if not os.path.isfile( os.path.join(freesurfer_dir, 'tmp', 'thick_kev.csv')): makeLabel(freesurfer_dir) mergeLabel(freesurfer_dir, roiDict) thicknessDict = getThickness(freesurfer_dir, roiDict) thicknessDf = dictWithTuple2df(thicknessDict) thicknessDf.to_csv( os.path.join(freesurfer_dir, 'tmp', 'thick_kev.csv')) else: thicknessDf = pd.read_csv( os.path.join(freesurfer_dir, 'tmp', 'thick_kev.csv')) subjectDict[background] = thicknessDf # sum up dataframes in a subjectDict dictionary finalDf = pd.concat([x for x in subjectDict.values()]) # return mean return finalDf.groupby('subroi').mean().reset_index()
def collectStats(background_subject_locs): """ Summarise cortical thickness in more than one subjects. 'background_subject_locs' should given in python list format. For each background, 1. Creates labels using makeLabel 2. Merges labels according to the roiDcit using mergeLabel 3. Estimates cortical thickness in each merged labels (dict) 4. Converts dict to pandas Dataframe using dictWithTuple2df 5. save df to freesurfer_dir/tmp/thick_kev.csv Than mean df is returned. """ # collectStats('ha','ho',ha') # 8 cortex dictionary roiDict = get_cortical_rois() subjectDict = {} for background in background_subject_locs: # freesurfer sub-directory description FS_description = ["bem", "mri", "scripts", "src", "stats", "surf", "tmp"] freesurfer_dir = ccncpy.subDirSearch(FS_description, background) if len(freesurfer_dir) > 1: sys.exit( re.sub( "\s+", " ", "There are more than 1 freesurfer directory \ under {0}".format( subject_loc ), ) ) else: freesurfer_dir = "".join(freesurfer_dir) if not os.path.isfile(os.path.join(freesurfer_dir, "tmp", "thick_kev.csv")): makeLabel(freesurfer_dir) mergeLabel(freesurfer_dir, roiDict) infoDict = getInfoFromLabel(freesurfer_dir, roiDict) infoDf = dictWithTuple2df(infoDict) infoDf.to_csv(os.path.join(freesurfer_dir, "tmp", "thick_kev.csv")) else: infoDf = pd.read_csv(os.path.join(freesurfer_dir, "tmp", "thick_kev.csv")) subjectDict[background] = infoDf # sum up dataframes in a subjectDict dictionary finalDf = pd.concat([x for x in subjectDict.values()]) # return mean return finalDf.groupby("subroi").mean().reset_index()
def collectStats(backgrounds): ''' Summarise cortical thickness in more than one subjects. 'backgrounds' should given in python list format. For each background, 1. Creates labels using makeLabel 2. Merges labels according to the roiDcit using mergeLabel 3. Estimates cortical thickness in each merged labels (dict) 4. Converts dict to pandas Dataframe using dictWithTuple2df 5. save df to freesurfer_dir/tmp/thick_kev.csv Than mean df is returned. ''' #collectStats('ha','ho',ha') # 8 cortex dictionary roiDict = get_cortical_rois() subjectDict = {} for background in backgrounds: # freesurfer sub-directory description FS_description= ['bem','mri','scripts','src','stats','surf','tmp'] freesurfer_dir = ccncpy.subDirSearch(FS_description, background) print freesurfer_dir if len(freesurfer_dir) > 1: sys.exit(re.sub('\s+',' ', 'There are more than 1 freesurfer directory \ under {0}'.format(subject_loc))) else: freesurfer_dir = ''.join(freesurfer_dir) if not os.path.isfile(os.path.join(freesurfer_dir,'tmp','thick_kev.csv')): makeLabel(freesurfer_dir) mergeLabel(freesurfer_dir, roiDict) thicknessDict = getThickness(freesurfer_dir, roiDict) thicknessDf = dictWithTuple2df(thicknessDict) thicknessDf.to_csv(os.path.join(freesurfer_dir,'tmp','thick_kev.csv')) else: thicknessDf = pd.read_csv(os.path.join(freesurfer_dir,'tmp','thick_kev.csv')) subjectDict[background] = thicknessDf # sum up dataframes in a subjectDict dictionary finalDf = pd.concat([x for x in subjectDict.values()]) # return mean return finalDf.groupby('subroi').mean().reset_index()
def get_freesurferDir(dir): FS_description = ["bem", "mri", "scripts", "src", "stats", "surf", "tmp"] freesurfer_dir = ccncpy.subDirSearch(FS_description, dir) if len(freesurfer_dir) > 1: print "Please choose one data" print "======================" for num, i in enumerate(freesurfer_dir): print "{number} : {location}".format(number=num, location=i) choice = raw_input("\t: ") return "".join(freesurfer_dir[choice]) else: return "".join(freesurfer_dir)
def get_freesurferDir(dir): FS_description = ['bem', 'mri', 'scripts', 'src', 'stats', 'surf', 'tmp'] freesurfer_dir = ccncpy.subDirSearch(FS_description, dir) if len(freesurfer_dir) > 1: print 'Please choose one data' print '======================' for num, i in enumerate(freesurfer_dir): print '{number} : {location}'.format(number=num, location=i) choice = raw_input('\t: ') return ''.join(freesurfer_dir[choice]) else: return ''.join(freesurfer_dir)
def main(subject_loc, backgrounds, roi_list, meanDfLoc,verbose, brain): ########################################################## # Find freesurfer dir ########################################################## FS_description= ['bem','mri','scripts', 'src','stats','surf','tmp'] freesurfer_dir = ccncpy.subDirSearch(FS_description, subject_loc) if len(freesurfer_dir) > 1: print freesurfer_dir sys.exit(re.sub('\s+',' ', 'There are more than 1 freesurfer directory \ under {0}'.format(subject_loc))) else: freesurfer_dir = ''.join(freesurfer_dir) ########################################################## # Freesurfer setting ########################################################## os.environ["FREESURFER_HOME"] = '/Applications/freesurfer' # where is the freesurfer directory os.environ["SUBJECTS_DIR"] = '{0}'.format(os.path.dirname(freesurfer_dir)) ########################################################## # Summarize cortical thickness ########################################################## # if no mean table is given but background list is given if meanDfLoc == False and backgrounds != None: print 'Calculating cortical thickness in {0}'.format(backgrounds) # make mean table meanDfName = raw_input('Name of the background subject : ') if verbose: meanDf = collectStats_v2(backgrounds) else: meanDf = collectStats(backgrounds) # if no mean table is given, and backround list is empty # use subject_loc as the background elif meanDfLoc == False and backgrounds == None: print 'No background subjects are given, now running only {0}'.format(subject_loc) meanDfName = '' if verbose: meanDf = collectStats_v2([subject_loc]) else: meanDf = collectStats([subject_loc]) # if meanDfLoc is given else: print 'Now comparing with mean_thickness.csv in /ccnc_bin/meanThickness/' meanDfName = 'NOR' #meanDf = pd.read_csv('/ccnc_bin/meanThickness/new_mean_OCT15.csv') if verbose: meanDf = pd.read_csv('/ccnc_bin/meanThickness/detailed_mean_2015_12_28.csv', index_col=0) else: meanDf = pd.read_csv('/ccnc_bin/meanThickness/new_mean_OCT15.csv', index_col=0) ########################################################## # Get roi dict : 8 cortex each hemisphere ########################################################## roiDict = get_cortical_rois() ########################################################## # annotation2label --> merge labels --> freesurfer/tmp ########################################################## if args.graph: if verbose: thicknessDf = collectStats_v2([os.path.dirname(freesurfer_dir)])#backgrounds) makeBrainPic(freesurfer_dir, thicknessDf, meanDf) else: thicknessDf = collectStats([os.path.dirname(freesurfer_dir)])#backgrounds) makeBrainPic(freesurfer_dir, thicknessDf, meanDf)
def main(subject_loc, backgrounds, roi_list, meanDfLoc, verbose, brain): ########################################################## # Find freesurfer dir ########################################################## FS_description = ['bem', 'mri', 'scripts', 'src', 'stats', 'surf', 'tmp'] freesurfer_dir = ccncpy.subDirSearch(FS_description, subject_loc) if len(freesurfer_dir) > 1: print freesurfer_dir sys.exit( re.sub( '\s+', ' ', 'There are more than 1 freesurfer directory \ under {0}'.format(subject_loc))) else: freesurfer_dir = ''.join(freesurfer_dir) ########################################################## # Freesurfer setting ########################################################## os.environ["FREESURFER_HOME"] = '/Applications/freesurfer' # where is the freesurfer directory os.environ["SUBJECTS_DIR"] = '{0}'.format(os.path.dirname(freesurfer_dir)) ########################################################## # Summarize cortical thickness ########################################################## # if no mean table is given but background list is given if meanDfLoc == False and backgrounds != None: print 'Calculating cortical thickness in {0}'.format(backgrounds) # make mean table meanDfName = raw_input('Name of the background subject : ') if verbose: meanDf = collectStats_v2(backgrounds) else: meanDf = collectStats(backgrounds) # if no mean table is given, and backround list is empty # use subject_loc as the background elif meanDfLoc == False and backgrounds == None: print 'No background subjects are given, now running only {0}'.format( subject_loc) meanDfName = '' if verbose: meanDf = collectStats_v2([subject_loc]) else: meanDf = collectStats([subject_loc]) # if meanDfLoc is given else: print 'Now comparing with mean_thickness.csv in /ccnc_bin/meanThickness/' meanDfName = 'NOR' #meanDf = pd.read_csv('/ccnc_bin/meanThickness/new_mean_OCT15.csv') if verbose: meanDf = pd.read_csv( '/ccnc_bin/meanThickness/detailed_mean_2015_12_28.csv', index_col=0) else: meanDf = pd.read_csv('/ccnc_bin/meanThickness/new_mean_OCT15.csv', index_col=0) ########################################################## # Get roi dict : 8 cortex each hemisphere ########################################################## roiDict = get_cortical_rois() ########################################################## # annotation2label --> merge labels --> freesurfer/tmp ########################################################## if args.graph: if verbose: thicknessDf = collectStats_v2([os.path.dirname(freesurfer_dir) ]) #backgrounds) makeBrainPic(freesurfer_dir, thicknessDf, meanDf) else: thicknessDf = collectStats([os.path.dirname(freesurfer_dir) ]) #backgrounds) makeBrainPic(freesurfer_dir, thicknessDf, meanDf)