forked from ccarey/CBS
-
Notifications
You must be signed in to change notification settings - Fork 1
/
genPreprocBatches_slurm.py
117 lines (99 loc) · 3.97 KB
/
genPreprocBatches_slurm.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#!/usr/bin/env python
import platform
pstr = platform.python_version()
if pstr[0]!="2":
print "This script is designed for Python 2. Other versions may cause errors!"
elif int(pstr[2])<7:
print '''This script has been tested in version 2.7 of python. You are using an earlier version! Please use the default environment as specified on the CBS FAQ page:
http://cbs.fas.harvard.edu/science/core-facilities/neuroimaging/information-investigators/faq
'''
import argparse
import glob,re,os.path,datetime,os
errorlog = ''
def getNumRuns(spath,s):
fruns = glob.glob(spath+'/'+s+'/preproc/*run*')
maxruns = 0
for fname in fruns:
m = re.match('.*run(\d+)',fname)
runnum = int(m.group(1))
if runnum>maxruns:
maxruns = runnum
return maxruns
def getNumPts(spath,s):
global errorlog
fruns = glob.glob(spath+'/'+s+'/preproc/*run*')
#print 'glob:' + spath+'/'+s+'/preproc/*run*'
nPts = reduce(max,map(lambda x: int(re.search('run\d*-(\d+)',x).group(1)),fruns))
#print nPts
nRuns = getNumRuns(spath,s)
for i in range(1,nRuns+1):
fruns = glob.glob(spath+'/'+s+'/preproc/*run'+str.zfill(str(i),3)+'*')
#print 'glob: '+spath+'/'+s+'/preproc/*run'+str.zfill(str(i),3)+'*'
nPts_i = reduce(max,map(lambda x: int(re.search('run\d*-(\d+)',x).group(1)),fruns))
if nPts_i != nPts:
errorlog = errorlog+"Subject " +s+ " has mismached numbers of points"
return None
return nPts
parser = argparse.ArgumentParser(description='Generate new SPM batch files from a template.')
parser.add_argument('-t','--template', help='Template batch file', required=True)
parser.add_argument('-p','--path', help='Path to subjects', required=True, nargs=1)
parser.add_argument('-s','--subjects', help='List of subjects', required=False, nargs='*')
parser.add_argument('-f','--subjectfile', help='File containing subjects', required=False)
args = vars(parser.parse_args())
if args["subjects"]:
sublist = args["subjects"]
elif args["subjectfile"]:
fsf = open(args["subjectfile"],'r')
sublist = []
for l in fsf:
sublist.append(l.strip())
fsf.close()
f = open(args["template"])
origtemplate = f.read()
finalname = 'preproc.m'
finalname = os.path.basename(args["template"])
f.close()
spath = args["path"][0]
# remove a trailing slash if it's there
if spath[-1]=='/':
spath = spath[0:-1]
runList = re.findall('.*run(\d+)',origtemplate)
nOrigRuns = reduce(max,map(int,runList))
epiList = re.findall('.*run\d+-(\d+)',origtemplate)
nPts = reduce(max,map(int,epiList))
origPath = re.findall('(/.*)/preproc/',origtemplate)[0]
generatedBatches = []
for s in sublist:
print "Processing subject: "+s
destPath = spath+'/'+s
subRuns = getNumRuns(spath,s)
subPts = getNumPts(spath,s)
if subRuns != nOrigRuns:
errorlog+="Subject "+ s +" has " + str(subRuns) + " runs, not " + str(nOrigRuns) + " runs.\n"
break
if subPts != nPts:
errorlog+="Subject "+ s +" has " + str(subPts) + " points, not " + str(nPts) + " points.\n"
break
nextBatch = origtemplate
nextBatch = nextBatch.replace(origPath,destPath)
generatedBatches.append(destPath+'/batch/'+finalname)
fout = open(destPath+'/batch/'+finalname,'w')
fout.write(nextBatch)
fout.close()
errfile = spath + "/errors_preproc" + datetime.datetime.now().strftime("%Y_%m_%d_%Hh_%Mm")
if len(errorlog)>0:
fe = open(errfile,'w+')
for err in errorlog.split('\n'):
fe.write(err)
fe.close()
#for gb in generatedBatches:
# bsubcmd = "sbatch -n 1 -t 90"
# bsubcmd = bsubcmd + " -o " + os.path.dirname(gb) + "/../output_files/output_preproc"
#bsubcmd = bsubcmd + datetime.datetime.now().strftime("%Y_%m_%d_%Hh_%Mm")
# bsubcmd = bsubcmd + " -p ncf"
#bsubcmd = bsubcmd + ' matlab -nodisplay -r \"runSPMBatch(\'' + gb + '\')\"'
# print "Running bsub as follows:"
# print bsubcmd
#os.system(bsubcmd)
print errorlog
# append errorlog