forked from RocaVincent/iguane_harmonization
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathall_in_one.py
65 lines (56 loc) · 2.06 KB
/
all_in_one.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
import sys
import getopt
import pandas as pd
from time import time
import pipeline
def usage():
cmds = [
'python all_in_one.py --in-mri <in_mri> --out_mri <out_mri> [options]',
'python all_in_one.py --in-csv <in-csv> [options]'
]
options = [
['--in-mri <input>','input MR image path, requires --out-mri'],
['--out-mri <output>','output MR image path, requires --in-mri'],
['--in-csv <csv>', 'CSV filepath with two columns: in_paths, out_paths. --in-mri must not be defined'],
['--hd-bet-cpu', 'runs HD-BET with cpu (GPU by default)'],
['--n-procs <n>', 'number of CPUs to use if several inputs (--in-csv option), default=1'],
['--help', 'displays this help']
]
print('\n'.join(cmds))
print('Available options are:')
for opt,doc in options: print(f"\t{opt}{(20-len(opt))*' '}{doc}")
def main():
try:
opts,_ = getopt.getopt(sys.argv[1:], 'h', ['help', 'in-mri=', 'out-mri=', 'in-csv=', 'n-procs=', 'hd-bet-cpu'])
except getopt.GetoptError as err:
print(err)
usage()
sys.exit(2)
in_mri = None
out_mri = None
in_csv = None
n_procs = 1
hd_bet_cpu = False
for opt,arg in opts:
if opt in ('-h', '--help'):
usage()
sys.exit()
if opt == '--in-mri': in_mri = arg
elif opt == '--out-mri': out_mri = arg
elif opt == '--in-csv': in_csv = arg
elif opt == '--hd-bet-cpu': hd_bet_cpu = True
elif opt == '--n-procs': n_procs = int(arg)
if (in_mri is None) ^ (out_mri is None) :
usage()
sys.exit(2)
if not ((in_mri is None) ^ (in_csv is None)):
usage()
sys.exit(2)
if in_csv:
df = pd.read_csv(in_csv)
pipeline.run_multiproc(df.in_paths.tolist(), df.out_paths.tolist(), n_procs, hd_bet_cpu)
else: pipeline.run_singleproc(in_mri, out_mri, hd_bet_cpu)
if __name__ == "__main__":
t_start = time()
main()
print(f"End of execution, total processing time = {time()-t_start:.0f} seconds")