def mk_native_atlas(featdir,atlasfile,atlas_descriptor): # make the atlas in native space if it doesn't exist # just use linear reg, as fnirt does goofy stuff nativeatlas=featdir+'/%s_native.nii.gz'%atlas_descriptor if not os.path.exists(nativeatlas): cmd='flirt -in %s -out %s -ref %s -applyxfm -init %s/reg/standard2example_func.mat -interp nearestneighbour'%(atlasfile,nativeatlas,featdir+'/example_func.nii.gz',featdir) run_shell_cmd(cmd) return nativeatlas
def mk_native_atlas(featdir, atlasfile, atlas_descriptor): # make the atlas in native space if it doesn't exist # just use linear reg, as fnirt does goofy stuff nativeatlas = featdir + '/%s_native.nii.gz' % atlas_descriptor if not os.path.exists(nativeatlas): cmd = 'flirt -in %s -out %s -ref %s -applyxfm -init %s/reg/standard2example_func.mat -interp nearestneighbour' % ( atlasfile, nativeatlas, featdir + '/example_func.nii.gz', featdir) run_shell_cmd(cmd) return nativeatlas
except: tarballs_to_make['model']=1 for dataset in datasets: sublist=[s for s in os.listdir(os.path.join(basedir,dataset)) if s.find('sub')==0] if tarballs_to_make['raw']==1: tarcmd='cd %s; tar zcvf %s/%s_raw.tgz %s/models %s/README* %s/*.txt '%(basedir,tbdir,dataset,dataset,dataset,dataset) for s in sublist: l=os.path.join(dataset,s) tarcmd=tarcmd+'%s/behav %s/model/model*/onsets %s/BOLD/*/QA %s/BOLD/*/bold.nii.gz %s/anatomy/highres*.nii.gz %s/anatomy/inplane*.nii.gz %s/README* '%(l,l,l,l,l,l,l) print tarcmd run_shell_cmd(tarcmd) if tarballs_to_make['model']==1: tarcmd='tar zcvf %s/%s_models.tgz %s/models %s/*.txt '%(tbdir,dataset,dataset,dataset) for s in sublist: l=dataset+'/'+s tarcmd=tarcmd+'%s/model/model* %s/README* '%(l,l) print tarcmd run_shell_cmd(tarcmd) # now make checksum file if tarballs_to_make['raw']==1: checksumfile='%s/%s_raw_checksums.txt'%(tbdir,dataset)
""" import os,sys from run_shell_cmd import * import numpy as N import pickle def usage(): """Print the docstring and exit with error.""" sys.stdout.write(__doc__) sys.exit(2) basedir=os.path.abspath(os.curdir) flist=run_shell_cmd('find . -name "res4d_sc.txt"') roidata={} nbadrois={} missingcount=N.zeros(305) for f in flist: f_s=f.split('/') ds=f_s[1] if not roidata.has_key(ds): roidata[ds]={} sub=int(f_s[2][3:6]) if not roidata[ds].has_key(sub): roidata[ds][sub]={} model=int(f_s[4][5:8]) if not roidata[ds][sub].has_key(model):
outdir='/corral-repl/utexas/poldracklab/openfmri/analyses/paper_analysis_April2013/classifier/' from run_shell_cmd import * from scipy.stats import scoreatpercentile import numpy as N import os cmd='cat %s/randlabel/*SVM*.txt > %s/all_svm_randlabel.txt'%(outdir,outdir) run_shell_cmd(cmd) data=N.loadtxt(os.path.join(outdir,'all_svm_randlabel.txt')) print 'mean: ',N.mean(data) print '95 pct: ',scoreatpercentile(data,95)
bolddata=bold.get_data() for run in range(nruns): if not os.path.exists(basedir+'BOLD/task001_run%03d'%int(run+1)): os.mkdir(basedir+'BOLD/task001_run%03d'%int(run+1)) start=run*runlength end=run*runlength + runlength+1 rundata=bolddata[:,:,:,start:end] newimg=nibabel.Nifti1Image(rundata,bold.get_affine()) newimg.to_filename(basedir+'BOLD/task001_run%03d/bold.nii.gz'%int(run+1)) # deface and move anatomy process_anatomy=1 if process_anatomy: run_shell_cmd('deface.py %s/anat.nii.gz'%basedir) shutil.move('%s/anat_defaced.nii.gz'%basedir,'%s/anatomy/highres001.nii.gz'%basedir) # create onset files ## from the original paper: ## Each time series began and ended with 12 s of rest and contained ## eight stimulus blocks of 24-s duration, one for each category, ## sep- arated by 12-s intervals of rest. Stimuli were present- ed ## for 500 ms with an interstimulus interval of 1500 ms. Repetitions ## of meaningful stimuli were pictures of the same face or object ## photographed from dif- ferent angles. process_behav=1 stimdur=0.5 SOA=2
import os from run_shell_cmd import * import nibabel import numpy dlh = {} volume = {} cdata = {} minclust = {} for n in [25, 50, 75, 100, 125]: if not dlh.has_key(n): print n, ': estimating smoothness' l = run_shell_cmd( 'smoothest -d %d -r data/res4d_%dsubs.nii.gz -m mask.nii.gz' % (n, n)) dlh[n] = float(l[0].split(' ')[1]) volume[n] = float(l[1].split(' ')[1]) if not os.path.exists('data/tt_%dsubs_tstat1.nii.gz' % n): print 'running randomize' cmd = 'randomise -i data/funcdata_%dsubs.nii.gz -o tt_%dsubs -1 -n 1' % ( n, n) run_shell_cmd(cmd) # convert t to z stat tstat_img = nibabel.load('data/tt_%dsubs_tstat1.nii.gz' % n) tstat = tstat_img.get_data() zstat = t_to_z(tstat) zstat_img = nibabel.Nifti1Image(zstat, tstat.get_affine()) zstat_img.to_filename('data/tt_%dsubs_zstat1.nii.gz' % n)
#!/usr/bin/env python """ get checksums for all images USAGE: get_image_checksums.py <dsdir> """ from run_shell_cmd import * def usage(): """Print the docstring and exit with error.""" sys.stdout.write(__doc__) sys.exit(2) import sys if len(sys.argv) > 1: taskid = sys.argv[1] else: taskid = 'ds001' # usage() o = run_shell_cmd('find %s/sub*/{BOLD,anatomy} -name "*.nii.gz"' % taskid) checksums = [] outfile = open('checksums_%s.txt' % taskid, 'w') for f in o: outfile.write(run_shell_cmd('md5sum %s' % f)[0] + '\n') outfile.close()
def run(self): # Parse out important parameters for this block conf = self.conf subj = conf.subjID nextInputFilename = conf.nextInputFilename[-1] subjMaskDir = conf.subjMaskDir subjfMRIDir = conf.subjfMRIDir freesurferDir = conf.freesurferDir hcpdata = conf.hcpData input = conf.fs_input subjDir = conf.subjDir logname = conf.logname os.chdir(subjMaskDir) print '---------------------' print 'Creating gray matter mask based on Freesurfer autosegmentation for subject ', subj # adjusting for use if HCP pipeline was used if input== 'wmparc.nii.gz': input = 'wmparc.nii.gz' analysisDir = subjDir + '/MNINonLinear/' else: input = input analysisDir = freesurferDir + '/mri/' print 'saving from', analysisDir, 'to', os.getcwd() if input == 'wmparc.nii.gz': run_shell_cmd('cp -v ' + analysisDir + input + ' ' + str(subj) + '_fs_seg.nii.gz',logname) else: run_shell_cmd('cp -v ' + analysisDir + input + ' .',logname) if input != 'wmparc.nii.gz': run_shell_cmd('mri_convert -i ' + input + ' -ot nii ' + str(subj) + '_fs_seg.nii',logname) if input != 'wmparc.nii.gz': run_shell_cmd("3dcalc -overwrite -a " + str(subj) + "_fs_seg.nii -expr 'a' -prefix " + str(subj) + "_fs_seg.nii.gz",logname) # Delete intermediary files run_shell_cmd('rm ' + str(subj) + '_fs_seg.nii ' + str(subj) + '_' + input + ' ' + str(subj) + 'mask_temp.nii.gz',logname) print "Any error after this output should be noted and checked... there shouldn't be errors after this flag" if input == 'wmparc.nii.gz': # use wmparc maskval set maskValSet="8 9 10 11 12 13 16 17 18 19 20 26 27 28 47 48 49 50 51 52 53 54 55 56 58 59 60 96 97 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035" maskValSet = maskValSet.split(' ') else: # Using aseg (not aparc+aseg) maskValSet = [8, 9, 10, 11, 12, 13, 16,17, 18, 19, 20, 26,27, 28, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 58, 59, 60, 96, 97, 3, 42] #Add segments to mask maskNum = 1 for maskval in maskValSet: if maskNum == 1: run_shell_cmd('3dcalc -a ' + str(subj) + '_fs_seg.nii.gz -expr "equals(a,' + str(maskval) + ')" -prefix ' + str(subj) + 'mask_temp.nii.gz -overwrite',logname) else: run_shell_cmd('3dcalc -a ' + str(subj) + '_fs_seg.nii.gz -b ' + str(subj) + 'mask_temp.nii.gz -expr "equals(a,' + str(maskval) + ')+b" -prefix ' + str(subj) + 'mask_temp.nii.gz -overwrite',logname) maskNum += 1 # Make mask binary run_shell_cmd("3dcalc -a " + str(subj) + "mask_temp.nii.gz -expr 'ispositive(a)' -prefix " + str(subj) + '_gmMask.nii.gz -overwrite',logname) if hcpdata == False: # Transform to TLRC space # First convert files from NIfTI to AFNI run_shell_cmd('3dcopy ' + subjMaskDir + str(subj) + '_gmMask.nii.gz ' + subjMaskDir + str(subj) + '_gmMask', logname) run_shell_cmd('rm -v ' + subjMaskDir + str(subj) + '_gmMask.nii.gz',logname) # Talairach command using AFNI functions run_shell_cmd('@auto_tlrc -apar ' + subjfMRIDir + 'anat_mprage_skullstripped+tlrc -input ' + str(subj) + '_gmMask+orig -overwrite',logname) run_shell_cmd('3dresample -overwrite -master ' + subjfMRIDir + nextInputFilename + '.nii.gz -inset ' + str(subj) + '_gmMask+tlrc -prefix ' + str(subj) + '_gmMask_func+tlrc',logname) run_shell_cmd('3dcopy ' + subjMaskDir + str(subj) + '_gmMask+tlrc ' + subjMaskDir + str(subj) + '_gmMask.nii.gz', logname) run_shell_cmd('3dcopy ' + subjMaskDir + str(subj) + '_gmMask_func+tlrc ' + subjMaskDir + str(subj) + '_gmMask_func.nii.gz', logname) run_shell_cmd('rm -v ' + subjMaskDir + str(subj) + '_gmMask+tlrc* ' + subjMaskDir + '_gmMask_func+tlrc*', logname) else: # Resample to functional space run_shell_cmd('3dresample -overwrite -master ' + subjfMRIDir + nextInputFilename + '.nii.gz -inset ' + str(subj) + '_gmMask.nii.gz -prefix ' + str(subj) + '_gmMask_func.nii.gz',logname) # Dilate mask by 1 functional voxel (just in case the resampled anatomical mask is off by a bit) run_shell_cmd("3dLocalstat -overwrite -nbhd 'SPHERE(-1)' -stat 'max' -prefix " + str(subj) + '_gmMask_func_dil1vox.nii.gz ' + str(subj) + '_gmMask_func.nii.gz',logname) run_shell_cmd('rm ' + str(subj) + 'mask_temp.nii.gz',logname)
def run(self): # Parse out important parameters for this block conf = self.conf subj = conf.subjID nextInputFilename = conf.nextInputFilename[-1] subjMaskDir = conf.subjMaskDir subjfMRIDir = conf.subjfMRIDir freesurferDir = conf.freesurferDir hcpdata = conf.hcpData input = conf.fs_input logname = conf.logname subjDir = conf.subjDir os.chdir(subjMaskDir) print '---------------------' print 'Create white matter mask, and erode it for subject', subj, '(MAKE SURE EROSIAN DOESN\'T REMOVE ALL VENTRICLE VOXELS)' # adjusting for use if HCP pipeline was used if input== 'wmparc.nii.gz': input = 'wmparc.nii.gz' analysisDir = subjDir + '/MNINonLinear/' else: input = input analysisDir = freesurferDir + '/mri/' print 'saving from', analysisDir, 'to', os.getcwd() if input == 'wmparc.nii.gz': run_shell_cmd('cp -v ' + analysisDir + input + ' ' + str(subj) + '_fs_seg.nii.gz',logname) else: #run_shell_cmd('cp -v ' + analysisDir + input + ' ' + str(subj) + '_fs_seg.nii',logname) run_shell_cmd('cp -v ' + analysisDir + input + ' .',logname) if input != 'wmparc.nii.gz': run_shell_cmd('mri_convert -i ' + input + ' -ot nii ' + str(subj) + '_fs_seg.nii',logname) if input != 'wmparc.nii.gz': run_shell_cmd("3dcalc -overwrite -a " + str(subj) + "_fs_seg.nii -expr 'a' -prefix " + str(subj) + "_fs_seg.nii.gz",logname) # Delete intermediary files run_shell_cmd('rm ' + str(subj) + '_fs_seg.nii ' + str(subj) + '_' + input + ' ' + str(subj) + 'mask_temp.nii.gz',logname) if input == 'wmparc.nii.gz': # use wmparc.nii.gz maskvals maskValSet="250 251 252 253 254 255 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 5001 5002" maskValSet = maskValSet.split(' ') else: # Using aseg (not aparc+aseg) # including all white matter maskValSet = [2, 7, 41, 46] # Add segments to mask maskNum = 1 for maskval in maskValSet: if maskNum == 1: run_shell_cmd("3dcalc -a " + str(subj) + '_fs_seg.nii.gz -expr "equals(a,' + str(maskval) + ')" -prefix ' + str(subj) + 'mask_temp.nii.gz -overwrite',logname) else: run_shell_cmd('3dcalc -a ' + str(subj) + '_fs_seg.nii.gz -b ' + str(subj) + 'mask_temp.nii.gz -expr "equals(a,' + str(maskval) + ')+b" -prefix ' + str(subj) + 'mask_temp.nii.gz -overwrite',logname) maskNum += 1 # Make mask binary run_shell_cmd("3dcalc -a " + str(subj) + "mask_temp.nii.gz -expr 'ispositive(a)' -prefix " + str(subj) + '_wmMask.nii.gz -overwrite',logname) # Transform to TLRC space if hcpdata == False: # Transform to TLRC space # First convert files from NIfTI to AFNI run_shell_cmd('3dcopy ' + subjMaskDir + str(subj) + '_wmMask.nii.gz ' + subjMaskDir + str(subj) + '_wmMask', logname) run_shell_cmd('rm -v ' + subjMaskDir + str(subj) + '_wmMask.nii.gz',logname) # Talairach command using AFNI functions run_shell_cmd('@auto_tlrc -apar ' + subjfMRIDir + 'anat_mprage_skullstripped+tlrc -input ' + str(subj) + '_wmMask+orig -overwrite',logname) run_shell_cmd('3dresample -overwrite -master ' + subjfMRIDir + nextInputFilename + '.nii.gz -inset ' + str(subj) + '_wmMask+tlrc -prefix ' + str(subj) + '_wmMask_func+tlrc',logname) run_shell_cmd('3dcopy ' + subjMaskDir + str(subj) + '_wmMask+tlrc ' + subjMaskDir + str(subj) + '_wmMask.nii.gz', logname) run_shell_cmd('3dcopy ' + subjMaskDir + str(subj) + '_wmMask_func+tlrc ' + subjMaskDir + str(subj) + '_wmMask_func.nii.gz', logname) run_shell_cmd('rm -v ' + subjMaskDir + str(subj) + '_wmMask+tlrc* ' + subjMaskDir + '_wmMask_func+tlrc*', logname) else: # Resample to functional space run_shell_cmd('3dresample -overwrite -master ' + subjfMRIDir + nextInputFilename + '.nii.gz -inset ' + str(subj) + '_wmMask.nii.gz -prefix ' + str(subj) + '_wmMask_func.nii.gz',logname) # Subtract gray matter mask from white matter mask (avoiding negative #s) run_shell_cmd('3dcalc -a ' + str(subj) + '_wmMask_func.nii.gz -b ' + str(subj) + "_gmMask_func_dil1vox.nii.gz -expr 'step(a-b)' -prefix " + str(subj) + '_wmMask_func_eroded.nii.gz -overwrite',logname) run_shell_cmd('rm ' + str(subj) + 'mask_temp.nii.gz',logname)
print 'cannot open %s' % dscon outfilename = td + '/group_stats_report.html' outfile = open(outfilename, 'w') outfile.write('<html>\n<body>\n') copes = glob.glob(os.path.join(td, 'cope[0-9][0-9][0-9].nii.gz')) copes.sort() for c in copes: copenum = int(os.path.basename(c).split('.')[0].lstrip('cope')) print '%d: %s' % (copenum, con_names[copenum - 1]) overlay_cmd = 'overlay 0 0 %s -a %s 2.3 5 %s' % ( args.bgimg, c.replace('.nii.gz', '_tstat1.nii.gz'), c.replace('.nii.gz', '_tstat1_overlay.nii.gz')) print overlay_cmd run_shell_cmd(overlay_cmd) slicer_cmd = 'slicer %s -S 4 1000 %s' % ( c.replace('.nii.gz', '_tstat1_overlay.nii.gz'), c.replace('.nii.gz', '_tstat1_overlay.png')) print slicer_cmd run_shell_cmd(slicer_cmd) outfile.write('%s cope%03d (%s)<br>\n' % (td, copenum, con_names[copenum - 1])) outfile.write('<img src="%s" width=100%%/>\n' % c.replace('.nii.gz', '_tstat1_overlay.png')) outfile.write('</body>\n</html>\n') outfile.close()
#!/usr/bin/env python """ get checksums for all images USAGE: get_image_checksums.py <dsdir> """ from run_shell_cmd import * def usage(): """Print the docstring and exit with error.""" sys.stdout.write(__doc__) sys.exit(2) import sys if len(sys.argv)>1: taskid=sys.argv[1] else: taskid='ds001' # usage() o=run_shell_cmd('find %s/sub*/{BOLD,anatomy} -name "*.nii.gz"'%taskid) checksums=[] outfile=open('checksums_%s.txt'%taskid,'w') for f in o: outfile.write(run_shell_cmd('md5sum %s'%f)[0]+'\n') outfile.close()
def main(): # first run N4 bias correction imgfile=sys.argv[1] subdir=imgfile.rstrip('highres001.nii.gz') if not os.path.exists(subdir): print '%s does not exist!'%subdir sys.exit(0) cmd='%sN4BiasFieldCorrection -i %s/highres001.nii.gz -d 3 -o %s/highres001_bfc.nii.gz -c [50,0.0001]'%(ANTSPATH,subdir,subdir) print cmd if not test: run_shell_cmd(cmd,cwd=subdir) # then align bias-corrected whole-head image to template PARAMS="-r Gauss[3,0] -t SyN[0.25] -i 30x90x20 --use-Histogram-Matching --number-of-affine-iterations 10000x10000x10000x10000x10000 --rigid-affine false" template_wholehead='%s/data/standard/MNI152_T1_2mm.nii.gz'%FSLDIR template_brain='%s/data/standard/MNI152_T1_2mm_brain.nii.gz'%FSLDIR template_mask='%s/data/standard/MNI152_T1_2mm_brain_mask.nii.gz'%FSLDIR cmd='%sANTS 3 -m CC[%s,%shighres001_bfc.nii.gz,1,4] -o highres001_ANTS.nii.gz %s'%(ANTSPATH,template_wholehead,subdir,PARAMS) print cmd if not test: run_shell_cmd(cmd,cwd=subdir) # create a version of the MNI mask aligned to subject space cmd='WarpImageMultiTransform 3 %s %shighres001_brain_mask.nii.gz -R %shighres001.nii.gz -i %shighres001_ANTSAffine.txt %shighres001_ANTSInverseWarp.nii.gz --use-NN'%(template_mask,subdir,subdir,subdir,subdir) print cmd if not test: run_shell_cmd(cmd,cwd=subdir) cmd='fslmaths %shighres001.nii.gz -mul %shighres001_brain_mask.nii.gz %shighres001_brain.nii.gz'%(subdir,subdir,subdir) print cmd if not test: run_shell_cmd(cmd,cwd=subdir) # rerun warp from stripped highres to stripped template cmd='%sANTS 3 -m CC[%s,%shighres001_brain.nii.gz,1,4] -o highres001_ANTSstd.nii.gz %s'%(ANTSPATH,template_brain,subdir,PARAMS) print cmd if not test: run_shell_cmd(cmd,cwd=subdir) cmd='WarpImageMultiTransform 3 %shighres001_brain.nii.gz %shighres001_reg2std_ANTS.nii.gz -R %s %shighres001_ANTSWarp.nii.gz %shighres001_ANTSAffine.txt'%(subdir,subdir,template_brain,subdir,subdir) print cmd if not test: run_shell_cmd(cmd,cwd=subdir)
for run in range(nruns): if not os.path.exists(basedir + 'BOLD/task001_run%03d' % int(run + 1)): os.mkdir(basedir + 'BOLD/task001_run%03d' % int(run + 1)) start = run * runlength end = run * runlength + runlength + 1 rundata = bolddata[:, :, :, start:end] newimg = nibabel.Nifti1Image(rundata, bold.get_affine()) newimg.to_filename(basedir + 'BOLD/task001_run%03d/bold.nii.gz' % int(run + 1)) # deface and move anatomy process_anatomy = 1 if process_anatomy: run_shell_cmd('deface.py %s/anat.nii.gz' % basedir) shutil.move('%s/anat_defaced.nii.gz' % basedir, '%s/anatomy/highres001.nii.gz' % basedir) # create onset files ## from the original paper: ## Each time series began and ended with 12 s of rest and contained ## eight stimulus blocks of 24-s duration, one for each category, ## sep- arated by 12-s intervals of rest. Stimuli were present- ed ## for 500 ms with an interstimulus interval of 1500 ms. Repetitions ## of meaningful stimuli were pictures of the same face or object ## photographed from dif- ferent angles. process_behav = 1 stimdur = 0.5 SOA = 2
(suffix, comp)) posthresh_dis[comp] = N.min(corrdata_thresh[corrdata_thresh > 0]) negthresh_dis[comp] = N.max(corrdata_thresh[corrdata_thresh < 0]) posmax_dis[comp] = N.max(corrdata_thresh[corrdata_thresh > 0]) negmax_dis[comp] = N.min(corrdata_thresh[corrdata_thresh < 0]) # make slice images if 1 == 0: for comp in range(nccacomps): if posthresh[comp] > 0: # first take back to 3mm space cmd = '/work/01329/poldrack/software_lonestar/fsl/bin/flirt -in /scratch/01329/poldrack/textmining/paper/CCA_corr/cca%s_comp%03d_6mm_thresh.nii.gz -applyxfm -init /work/01329/poldrack/software_lonestar/fsl/etc/flirtsch/ident.mat -out %sCCA_corr/cca%s_comp%03d_thresh.nii.gz -paddingsize 0.0 -interp trilinear -ref /scratch/01329/poldrack/textmining/paper/data_preparation/all_peakimages.nii.gz' % ( suffix, comp, basedir, suffix, comp) print cmd run_shell_cmd(cmd) cmd = 'overlay 1 0 /work/01329/poldrack/software_lonestar/atlases/MNI152lin_3mm.nii.gz -a %sCCA_corr/cca%s_comp%03d_thresh.nii.gz %f %f %sCCA_corr/cca%s_comp%03d_thresh.nii.gz %f %f cbar.png ysb %sCCA_corr/cca%s_comp%03d_rend' % ( basedir, suffix, comp, posthresh[comp], posmax[comp], basedir, suffix, comp, negthresh[comp], negmax[comp], basedir, suffix, comp) print cmd run_shell_cmd(cmd) cmd = 'slicer %sCCA_corr/cca%s_comp%03d_rend.nii.gz -u -S 2 750 %sCCA_corr/cca%s_comp%03d_slices.png' % ( basedir, suffix, comp, basedir, suffix, comp) print cmd run_shell_cmd(cmd) # make slice images for disorders if 1 == 0: for comp in range(nccacomps): if posthresh_dis[comp] > 0:
def run(self): # Parse out important parameters for this block conf = self.conf logname = conf.logname subj = conf.subjID nextInputFilename = conf.nextInputFilename[-1] subjMaskDir = conf.subjMaskDir subjfMRIDir = conf.subjfMRIDir freesurferDir = conf.freesurferDir subjDir = conf.subjDir hcpdata = conf.hcpData input = conf.fs_input os.chdir(subjMaskDir) print '---------------------' print 'Create ventricle mask, and erode it for subject ' + str(subj) + ' (MAKE SURE EROSION DOESN\'T REMOVE ALL VENTRICLE VOXELS)' # adjusting for use if HCP pipeline was used if input== 'wmparc.nii.gz': input = 'wmparc.nii.gz' analysisDir = subjDir + '/MNINonLinear/' else: input = input analysisDir = freesurferDir + '/mri/' print 'saving from', analysisDir, 'to', os.getcwd() if input == 'wmparc.nii.gz': run_shell_cmd('cp -v ' + analysisDir + input + ' ' + str(subj) + '_fs_seg.nii.gz',logname) else: run_shell_cmd('cp -v ' + analysisDir + input + ' .',logname) #run_shell_cmd('cp -v ' + analysisDir + input + ' ' + str(subj) + '_fs_seg.nii',logname) if input != 'wmparc.nii.gz': run_shell_cmd('mri_convert -i ' + input + ' -ot nii ' + str(subj) + '_fs_seg.nii',logname) if input != 'wmparc.nii.gz': run_shell_cmd("3dcalc -overwrite -a " + str(subj) + "_fs_seg.nii -expr 'a' -prefix " + str(subj) + "_fs_seg.nii.gz",logname) # Delete intermediary files run_shell_cmd('rm ' + str(subj) + '_fs_seg.nii ' + str(subj) + '_' + input + ' ' + str(subj) + 'mask_temp.nii.gz',logname) # Using aseg (not aparc+aseg) # including ventricles maskValSet = [4, 43, 14, 15] # Add segments to mask maskNum = 1 for maskval in maskValSet: if maskNum == 1: run_shell_cmd("3dcalc -a " + str(subj) + '_fs_seg.nii.gz -expr "equals(a,' + str(maskval) + ')" -prefix ' + str(subj) + 'mask_temp.nii.gz -overwrite',logname) else: run_shell_cmd('3dcalc -a ' + str(subj) + '_fs_seg.nii.gz -b ' + str(subj) + 'mask_temp.nii.gz -expr "equals(a,' + str(maskval) + ')+b" -prefix ' + str(subj) + 'mask_temp.nii.gz -overwrite',logname) maskNum += 1 # Make mask binary run_shell_cmd("3dcalc -a " + str(subj) + "mask_temp.nii.gz -expr 'ispositive(a)' -prefix " + str(subj) + '_ventricles.nii.gz -overwrite',logname) # Transform to TLRC space if hcpdata == False: # Transform to TLRC space # First convert files from NIfTI to AFNI run_shell_cmd('3dcopy ' + subjMaskDir + str(subj) + '_ventricles.nii.gz ' + subjMaskDir + str(subj) + '_ventricles', logname) run_shell_cmd('rm -v ' + subjMaskDir + str(subj) + '_ventricles.nii.gz',logname) # Talairach command using AFNI functions run_shell_cmd('@auto_tlrc -apar ' + subjfMRIDir + 'anat_mprage_skullstripped+tlrc -input ' + str(subj) + '_ventricles+orig -overwrite',logname) run_shell_cmd('3dresample -overwrite -master ' + subjfMRIDir + nextInputFilename + '.nii.gz -inset ' + str(subj) + '_ventricles+tlrc -prefix ' + str(subj) + '_ventricles_func+tlrc',logname) run_shell_cmd('3dcopy ' + subjMaskDir + str(subj) + '_ventricles+tlrc ' + subjMaskDir + str(subj) + '_ventricles.nii.gz', logname) run_shell_cmd('3dcopy ' + subjMaskDir + str(subj) + '_ventricles_func+tlrc ' + subjMaskDir + str(subj) + '_ventricles_func.nii.gz', logname) run_shell_cmd('rm -v ' + subjMaskDir + str(subj) + '_ventricles+tlrc* ' + subjMaskDir + '_ventricles_func+tlrc*', logname) else: # Resample to functional space run_shell_cmd('3dresample -overwrite -master ' + subjfMRIDir + nextInputFilename + '.nii.gz -inset ' + str(subj) + '_ventricles.nii.gz -prefix ' + str(subj) + '_ventricles_func.nii.gz',logname) # Subtract gray matter mask from white matter mask (avoiding negative #s) run_shell_cmd('3dcalc -a ' + str(subj) + '_ventricles_func.nii.gz -b ' + str(subj) + "_gmMask_func_dil1vox.nii.gz -expr 'step(a-b)' -prefix " + str(subj) + '_ventricles_func_eroded.nii.gz -overwrite',logname) run_shell_cmd('rm ' + str(subj) + 'mask_temp.nii.gz',logname)
from run_shell_cmd import * datasets=['ds001', 'ds002', 'ds003', 'ds005', 'ds006', 'ds007', 'ds008', 'ds011', 'ds017A', 'ds017B', 'ds018', 'ds051', 'ds052', 'ds101', 'ds102', 'ds105', 'ds107'] tarballs_to_make={'raw':0,'model':1} for dataset in datasets: sublist=[s for s in os.listdir(basedir+dataset) if s.find('sub')==0] if tarballs_to_make['raw']==1: tarcmd='tar zcvf %s/%s_raw.tgz %s/models %s/*.txt '%(tbdir,dataset,dataset,dataset) for s in sublist: l=dataset+'/'+s tarcmd=tarcmd+'%s/behav %s/model/model*/onsets %s/BOLD/*/bold.nii.gz %s/anatomy/highres*.nii.gz %s/anatomy/inplane*.nii.gz %s/README* '%(l,l,l,l,l,l) print tarcmd run_shell_cmd(tarcmd) if tarballs_to_make['model']==1: tarcmd='tar zcvf %s/%s_models.tgz %s/models %s/*.txt '%(tbdir,dataset,dataset,dataset) for s in sublist: l=dataset+'/'+s tarcmd=tarcmd+'%s/model/model* %s/BOLD/*/bold_mcf.par* %s/README* '%(l,l,l) print tarcmd run_shell_cmd(tarcmd)
if os.path.exists(dscon): con_info=open(dscon).readlines() con_names=[i.strip().split('\t')[1] for i in con_info if i.find('ContrastName')>-1] else: print 'cannot open %s'%dscon outfilename=td+'/group_stats_report.html' outfile=open(outfilename,'w') outfile.write('<html>\n<body>\n') copes=glob.glob(os.path.join(td,'cope[0-9][0-9][0-9].nii.gz')) copes.sort() for c in copes: copenum=int(os.path.basename(c).split('.')[0].lstrip('cope')) print '%d: %s'%(copenum,con_names[copenum-1]) overlay_cmd='overlay 0 0 %s -a %s 2.3 5 %s'%(args.bgimg,c.replace('.nii.gz','_tstat1.nii.gz'),c.replace('.nii.gz','_tstat1_overlay.nii.gz')) print overlay_cmd run_shell_cmd(overlay_cmd) slicer_cmd='slicer %s -S 4 1000 %s'%(c.replace('.nii.gz','_tstat1_overlay.nii.gz'),c.replace('.nii.gz','_tstat1_overlay.png')) print slicer_cmd run_shell_cmd(slicer_cmd) outfile.write('%s cope%03d (%s)<br>\n'%(td,copenum,con_names[copenum-1])) outfile.write('<img src="%s" width=100%%/>\n'%c.replace('.nii.gz','_tstat1_overlay.png')) outfile.write('</body>\n</html>\n') outfile.close()