forked from ReubenDo/U-HVED
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinference.py
104 lines (70 loc) · 3.95 KB
/
inference.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
import itertools
import os
import configparser
import argparse
import sys
MODALITIES_img = ['T1', 'T1c', 'T2', 'Flair']
cwd = os.getcwd()
#sys.path.append(os.path.join(cwd,'extensions/'))
sys.path.insert(0,os.path.join(os.path.dirname(os.path.abspath(__file__)),'extensions'))
print(sys.path)
def find_exclusion(path):
path_folder = os.path.dirname(path)
name = os.path.basename(path).split('.')[0]
list_elements = [f for f in os.listdir(path_folder) if os.path.isfile(os.path.join(path_folder, f)) and name in f]
list_elements.remove(os.path.basename(path))
list_elements = [k.split('.')[0] for k in list_elements]
return list_elements
def change_config(net, mod_path, output_mod):
config = configparser.ConfigParser()
config.read(['./utils/config.ini'])
print(config.sections())
## Network session
if net=='u_hemis':
config.set('NETWORK', 'name', 'u_hemis.u_hemis_net.U_HeMIS')
config.set('SYSTEM', 'model_dir', './models/u_hemis/')
else:
config.set('NETWORK', 'name', 'u_hved.u_hved_net.U_HVED')
config.set('SYSTEM', 'model_dir', './models/u_hved/')
## Provided modality
choices = [mod_path[k]!=False for k in MODALITIES_img]
config.set('MULTIMODAL', 'choices', str(tuple(choices)))
# Output
config.set('MULTIMODAL', 'output_mod', output_mod)
## Path
example_true = choices.index(True)
for mod in MODALITIES_img:
if mod_path[mod]!=False:
path_folder = os.path.dirname(mod_path[mod])
name = os.path.basename(mod_path[mod])
not_names = find_exclusion(mod_path[mod])
name = name.split('.')[0]
config.set(mod, 'filename_contains', name[1:])
config.set(mod, 'path_to_search', path_folder+'/')
config.set(mod, 'filename_not_contains', str(not_names).replace("'", "")[1:-1])
else:
path_folder = os.path.dirname(mod_path[MODALITIES_img[example_true]])
name = os.path.basename(mod_path[MODALITIES_img[example_true]])
not_names = find_exclusion(mod_path[MODALITIES_img[example_true]])
name = name.split('.')[0]
config.set(mod, 'filename_contains', name[1:])
config.set(mod, 'path_to_search', path_folder+'/')
config.set(mod, 'filename_not_contains', str(not_names).replace("'", "")[1:-1])
with open('./utils/config_temp.ini', 'w') as configfile:
config.write(configfile)
name_application = 'U_HeMISApplication' if net=='u_hemis' else 'U_HVEDApplication'
os.system('net_run inference -a '+ net+'.application.'+name_application+' -c ./utils/config_temp.ini --output_postfix ' +name+'_output' )
if __name__=="__main__":
parser = argparse.ArgumentParser(description='Inference using U-HVED or U-HeMIS')
# parser.add_argument('--Warmup', '-w', help='Use Warmup or no', default="False")
parser.add_argument('--Network', '-n', help='Choice of the network. Either u_hemis or u_hved', default="u_hved")
parser.add_argument('--T1_input', '-t1', help='Path to the T1 scan', default=False)
parser.add_argument('--T1c_input', '-t1c', help='Path to the T1c scan', default=False)
parser.add_argument('--T2_input', '-t2', help='Path to the T2 scan', default=False)
parser.add_argument('--Flair_input', '-fl', help='Path to the Flair scan', default=False)
parser.add_argument('--output_mod', '-o', help='Choice of the output: seg, T1, T1c, T2 or Flair', default="seg")
args = parser.parse_args()
assert sum([k!=False for k in [args.T1_input, args.T1c_input, args.T2_input, args.Flair_input]]), 'one modality has to been at list provided'
assert args.output_mod in ['T1', 'T1c', 'T2', 'Flair', 'seg'], "output_mod has to be in ['T1', 'T1c', 'T2', 'Flair', 'seg']"
modality_path = {'T1': args.T1_input, 'T1c': args.T1c_input, 'T2': args.T2_input, 'Flair':args.Flair_input}
change_config(args.Network, modality_path, args.output_mod)