-
Notifications
You must be signed in to change notification settings - Fork 1
/
DTC_launcher_arguments_test.py
265 lines (207 loc) · 9.09 KB
/
DTC_launcher_arguments_test.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author: Eleftherios and Serge
Wenlin make some changes to track on the whole brain
Wenlin add for loop to run all the animals 2018-20-25
"""
from time import time
import numpy as np
import os
import multiprocessing as mp
import pickle
from tract_manager import create_tracts, evaluate_tracts, dwi_preprocessing, MyPool
from bvec_handler import extractbvec_fromheader
from BIAC_tools import send_mail
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
from BIAC_tools import isempty
import argparse
parser = argparse.ArgumentParser(epilog="TractEVAL (Compare tracts) version 1.0")
parser.add_argument("--v","--verbose", help="output verbosity", action="store_true")
parser.add_argument("--i", type=str, help="Input file path")
parser.add_argument("--isub","--save",help="Save all transition trk files to specified path")
parser.add_argument("--idwi", type=str,help="Input dwi path")
parser.add_argument("--itrk", type=str, help="Input trk path")
parser.add_argument("--o", type=str,help="Output pickle values path and figures if of unspecified")
parser.add_argument("--of", type=str,help="Output figures path")
parser.add_argument("--vis",help="enable Visualization",action="store_true")
parser.add_argument("--s","--save",help="Save all transition trk files to specified path")
args = parser.parse_args()
if args.v: verbose=True
else: verbose = False
if args.vis: display=True
else: display=False
if not args.idwi:
if not args.i:
#windll.Kernel32.SetConsoleTextAttribute(std_output_hdl, 12)
print('ERROR : Input path not specified')
#windll.Kernel32.SetConsoleTextAttribute(std_output_hdl, 7)
sys.exit()
else:
dwipath=args.i
else:
dwipath=args.idwi
if not args.trk:
if not args.i:
#windll.Kernel32.SetConsoleTextAttribute(std_output_hdl, 12)
print('ERROR : Input path not specified')
#windll.Kernel32.SetConsoleTextAttribute(std_output_hdl, 7)
sys.exit()
else:
trkpath=args.i
else:
trkpath=args.itrk
if not args.isub:
if os.path.exists(os.joinpath(trkpath,"subject_list.txt"):
subject_list=os.joinpath(trkpath,"subject_list.txt")
if not args.o:
print("No output specified, using trk path for all outputs")
figspath=trkpath
picklepath=trkpath
else:
if not args.of:
figspath=args.o
else:
figspath=args.of
l = ['N57433','N57434','N57435','N57436','N57437','N57440']
#l = ['N57433']
#l = ['N54717']
max_processors = 100
if mp.cpu_count() < max_processors:
max_processors = mp.cpu_count()
print("Running on ", max_processors, " processors")
#pool = mp.Pool(mp.cpu_count())
# please set the parameter here
# mypath = '/Users/alex/brain_data/E3E4/wenlin/'
#dwipath = '/Users/alex/brain_data/19abb14/C57_RAS/'
#BIGGUS_DISKUS = os.environ.get('BIGGUS_DISKUS')
BIGGUS_DISKUS = "/Volumes/Badea/Lab/mouse"
BIGGUS_DISKUS = "/mnt/BIAC/munin3.dhe.duke.edu/Badea/Lab/mouse/"
dwipath = BIGGUS_DISKUS + "/C57_JS/DWI_RAS/"
#outtrkpath = '/Users/alex/bass/testdata/' + 'braindata_results/'
#outtrkpath = '/Users/alex/bass/testdata/lifetest/'
outtrkpath = BIGGUS_DISKUS + "/C57_JS/TRK_RAS/"
figspath = BIGGUS_DISKUS + "/C57_JS/Figures_RAS/"
#figspath = '/Users/alex/bass/testdata/lifetest/'
outpathpickle = BIGGUS_DISKUS + "/C57_JS/PicklesFig_RAS/"
atlas_legends = BIGGUS_DISKUS + "/atlases/CHASSSYMM3AtlasLegends.xlsx"
df = pd.read_excel(atlas_legends, sheet_name='Sheet1')
df['Structure'] = df['Structure'].str.lower()
stepsize = 2
subject_processes = np.size(l)
subject_processes = 1
if max_processors < subject_processes:
subject_processes = max_processors
# accepted values are "small" for one in ten streamlines, "all or "large" for all streamlines,
# "none" or None variable for neither and "both" for both of them
function_processes = np.int(max_processors/subject_processes)
rois = ["hypothalamus","septum"]
ratio = 1
saved_streamlines = "all"
savefa="no"
verbose=True
denoise='mpca'
savedenoise=True
display=False
savefig=False
doprune=True
#strproperty = "_pypxmz_wholebrain"
allsave=True
if len(rois)==1:
strproperty = "_" + rois[0] + "_"
elif len(rois)>1:
strproperty="_"
for roi in rois:
strproperty = strproperty + roi[0:4]
strproperty = strproperty + "_"
#labellist=[163,1163,120,1120]
#labelslist=[121,1121]#corpuscallosum
labelslist=[]#fimbria
for roi in rois:
rslt_df = df.loc[df['Structure'] == roi.lower()]
if roi.lower() == "wholebrain" or roi.lower() == "brain":
labelslist=None
else:
labelslist=np.concatenate((labelslist,np.array(rslt_df.index2)))
if isempty(labelslist) and roi.lower() != "wholebrain" and roi.lower() != "brain":
txt = "Warning: Unrecognized roi, will take whole brain as ROI. The roi specified was: " + roi
print(txt)
#labelslist=None
bvec_orient=[-2,1,3]
# ---------------------------------------------------------
tall = time()
tract_results=[]
if verbose:
txt=("Process running with % d max processes available on % d subjects with % d subjects in parallel each using % d processes"
% (mp.cpu_count(), np.size(l), subject_processes, function_processes))
print(txt)
send_mail(txt,subject="Main process start msg ")
duration1=time()
if subject_processes>1:
if function_processes>1:
pool = MyPool(subject_processes)
else:
pool = mp.Pool(subject_processes)
# tract_results = pool.starmap_async(create_tracts, [(dwipath, outtrkpath, subject, stepsize, function_processes, strproperty,
# saved_streamlines, savefa, labelslist, bvec_orient, verbose) for subject in
# l]).get()
tract_results = pool.starmap_async(evaluate_tracts, [(dwipath, outtrkpath, subject, stepsize, saved_streamlines,
labelslist, outpathpickle, figspath, function_processes,
allsave, display, strproperty, ratio, verbose) for subject in l]).get()
pool.close()
else:
for subject in l:
# tract_results.append(create_tracts(dwipath, outtrkpath, subject, stepsize, function_processes, strproperty,
# saved_streamlines, savefa, labelslist, bvec_orient, verbose))
tract_results.append(evaluate_tracts(dwipath, outtrkpath, subject, stepsize, saved_streamlines, labelslist,
outpathpickle, figspath, function_processes, allsave, display, strproperty,
ratio, verbose))
#dwip_results = pool.starmap_async(dwi_preprocessing[(dwipath,outpath,subject,denoise,savefa,function_processes, verbose) for subject in l]).get()
#tract_results = pool.starmap_async(create_tracts,[(dwipath, outpath, subject, stepsize, function_processes,
# saved_streamlines, denoise, savefa, verbose) for subject in l]).get()
subject=l[0]
#dwip_results = dwi_preprocessing(dwipath,dwipath,subject,denoise,savedenoise=savedenoise, savefa=savefa, processes=function_processes, verbose=verbose)
#tract_results = dwi_create_tracts(dwipath, outtrkpath, subject, stepsize, function_processes,
# saved_streamlines, denoise, savefa, verbose)
#tracteval_results = evaluate_tracts(dwipath, outtrkpath, subject, stepsize, saved_streamlines, outpathfig=figspath,
# processes=function_processes, doprune=True, display=display, verbose=verbose)
tall = time() - tall
if verbose:
text = ("Process was finished after %.3f s" % (tall))
print(text)
send_mail(text, subject="End Process information")
#picklepath = '/Users/alex/jacques/allsubjects_test_eval.p'
#pickle.dump(tracteval_results, open(picklepath,"wb"))
# for j in range(np.size(l)):
# print(j+1)
# subject = l[j]
#pool.starmap_async(create_tracts(mypath,outpath,subject,step_size,function_processes))
duration_all = time() - tall
print('All animals tracking finished, running time is {}'.format(duration_all))
"""
example to use parallel in parallel
def sleepawhile(t):
print("Sleeping %i seconds..." % t)
time.sleep(t)
return t
def work(num_procs):
print("Creating %i (daemon) workers and jobs in child." % num_procs)
pool = multiprocessing.Pool(num_procs)
result = pool.map(sleepawhile,
[randint(1, 5) for x in range(num_procs)])
# The following is not really needed, since the (daemon) workers of the
# child's pool are killed when the child is terminated, but it's good
# practice to cleanup after ourselves anyway.
pool.close()
pool.join()
return result
def test():
print("Creating 5 (non-daemon) workers and jobs in main process.")
pool = MyPool(5)
result = pool.map(work, [randint(1, 5) for x in range(5)])
pool.close()
pool.join()
print(result)
"""