-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmypath.py
308 lines (265 loc) · 10.1 KB
/
mypath.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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
from set_args import args
import os
import time
import numpy as np
from functools import wraps
def mkdir_dcrt(fun): # decorator to create directory if not exist
"""
A decorator to make directory output from function if not exist.
:param fun: a function which outputs a directory
:return: decorated function
"""
@wraps(fun)
def decorated(*args, **kwargs):
output = fun(*args, **kwargs)
if '.' in output.split('/')[-1]:
output = os.path.dirname(output)
if not os.path.exists(output):
os.makedirs(output)
print('successfully create directory:', output)
else:
if not os.path.exists(output):
os.makedirs(output)
print('successfully create directory:', output)
return fun(*args, **kwargs)
return decorated
# def get_short_names(long_names):
# """
# get task list according to a list of model names. one task may corresponds to multiple models.
# :param model_names: a list of model names
# :return: a list of tasks
# """
# net_task_dict = {
# "net_itgt_ls_rc": "nils",
# "net_itgt_lb_rc": "nilb",
# "net_itgt_vs_rc": "nivs",
# "net_itgt_lu_rc": "nilu",
# "net_itgt_aw_rc": "niaw",
#
# "net_recon": "nnl",
#
# "net_lesion": "nle",
# "net_lobe": "nlb",
# "net_vessel": "nvs",
# "net_lung": "nlu", # avoid repeating nol
# "net_airway": "naw"
# }
# return list(map(net_task_dict.get, long_names))
class Mypath(object):
"""
Here, I use 'fpath' to indicatate full file path and name, 'path' to respresent the directory,
'file_name' to respresent the file name in the parent directory.
"""
def __init__(self, task, current_time=None):
"""
initial valuables.
:param task: task name, e.g. 'lobe'
:param current_time: a string represent the current time. It is used to name models and log files. If None, the time will be generated automatically.
"""
self.task = task
self.dir_path = os.path.dirname(os.path.realpath(__file__)) # abosolute path of the current script
self.model_path = os.path.join(self.dir_path, 'models')
self.log_path = os.path.join(self.dir_path, 'logs')
self.data_path = os.path.join(self.dir_path, 'data')
self.results_path = os.path.join(self.dir_path, 'results')
if current_time:
self.str_name = current_time
else:
self.str_name = str(int(time.time())) + '_' + str(np.random.randint(1000))
# long_names = args.model_names.split('-')
# long_names = [i.lstrip() for i in long_names] # remove backspace before each model name
# short_names = get_short_names(long_names)
# short_names = '-'.join(short_names)
# self.setting = '_lrlb' + str(args.lr_lb) \
# + 'lrvs' + str(args.lr_vs) \
# + 'lbio' + str(args.lb_io) \
# + 'net' + str(short_names) \
# + 'pm' + str(args.p_middle) \
# + 'nld' + str(args.recon_dir) \
# + 'ao' + str(args.ao_lb) \
# + 'ds' + str(args.ds_lb) \
# + 'pps' + str(args.patches_per_scan) \
# + 'lbnb' + str(args.lb_tr_nb) \
# + 'vsnb' + str(args.vs_tr_nb) \
# + 'nlnb' + str(args.rc_tr_nb) \
# + 'ptsz' + str(args.ptch_sz) \
# + 'fat' + str(args.fat) \
# + 'ptzsz' + str(args.ptch_z_sz)
# a = len(self.str_name) # name longer than 256 would be invalid to write and read
def sub_dir(self):
"""
Sub directory of tasks. It is used to choose different datasets (like 'GLUCOLD', 'SSc').
:return: sub directory name
"""
if self.task == 'lesion':
sub_dir = args.ls_sub_dir
elif self.task == 'lobe':
sub_dir = args.lb_sub_dir
elif self.task == 'vessel':
sub_dir = args.vs_sub_dir
elif self.task == "lung":
sub_dir = args.lu_sub_dir
elif self.task == "airway":
sub_dir = args.aw_sub_dir
elif self.task == 'recon':
sub_dir = args.rc_sub_dir
else:
raise Exception("task is not valid: ", self.task)
return sub_dir
@mkdir_dcrt
def data_dir(self, phase):
"""
data directory.
:return: data dataset directory for a specific task
"""
data_dir = self.data_path + '/' + self.task + '/' + phase
return data_dir
@mkdir_dcrt
def task_log_dir(self):
"""
log directory.
:return: directory to save logs
"""
task_log_dir = self.log_path + '/' + self.task
return task_log_dir
@mkdir_dcrt
def task_model_dir(self):
"""
model directory.
:return: directory to save models
"""
task_model_dir = self.model_path + '/' + self.task
return task_model_dir
@mkdir_dcrt
def tr_va_log_fpath(self):
"""
log full path to save training and validation measuremets during training.
:return: log full path with suffix .log
"""
task_log_dir = self.task_log_dir()
return task_log_dir + '/' + self.str_name + 'tr_va.log'
@mkdir_dcrt
def log_fpath(self, phase):
"""
log full path to save training measuremets during training.
:return: log full path with suffix .log
"""
task_log_dir = self.task_log_dir()
return task_log_dir + '/' + self.str_name + phase + '.log'
@mkdir_dcrt
def model_figure_path(self):
"""
Directory where to save figures of model architecture.
:return: model figure directory
"""
model_figure_path = self.dir_path + '/figures'
return model_figure_path
@mkdir_dcrt
def json_fpath(self):
"""
full path of model json.
:return: model json full path
"""
task_model_path = self.task_model_dir()
return task_model_path + '/' + self.str_name + '.json'
@mkdir_dcrt
def args_fpath(self):
"""
full path of model arguments.
:return: model arguments full path
"""
task_model_path = self.task_model_dir()
return task_model_path + '/' + self.str_name + '_args.py'
@mkdir_dcrt
def model_fpath_best_patch(self, phase, str_name=None):
"""
full path to save best model according to training loss.
:return: full path
"""
task_model_path = self.task_model_dir()
if str_name is None:
return task_model_path + '/' + self.str_name + '_patch_' + phase + '.hdf5'
else:
return task_model_path + '/' + str_name + '_patch_' + phase + '.hdf5'
@mkdir_dcrt
def model_fpath_best_whole(self, phase='train', str_name=None):
"""
full path to save best model according to training loss.
:return: full path
"""
task_model_path = self.task_model_dir()
if self.task == "recon":
if str_name is None:
return task_model_path + '/' + self.str_name + '_patch_' + phase + '.hdf5'
else:
return task_model_path + '/' + str_name + '_patch_' + phase + '.hdf5'
else:
if str_name is None:
return task_model_path + '/' + self.str_name + '_' + phase + '.hdf5'
else:
return task_model_path + '/' + str_name + '_' + phase + '.hdf5'
@mkdir_dcrt
def ori_ct_path(self, phase, sub_dir=None):
"""
absolute directory of the original ct for training dataset
:param sub_dir:
:param phase: 'train' or 'valid'
:return: directory name
"""
if sub_dir is None:
data_path = self.data_path + '/' + self.task + '/' + phase + '/ori_ct/' + self.sub_dir()
else:
data_path = self.data_path + '/' + self.task + '/' + phase + '/ori_ct/' + sub_dir
return data_path
@mkdir_dcrt
def gdth_path(self, phase, sub_dir=None):
"""
absolute directory of the ground truth of ct for training dataset
:param sub_dir:
:param phase: 'train' or 'valid'
:return: directory name
"""
if sub_dir is None:
gdth_path = self.data_path + '/' + self.task + '/' + phase + '/gdth_ct/' + self.sub_dir()
else:
gdth_path = self.data_path + '/' + self.task + '/' + phase + '/gdth_ct/' + sub_dir
return gdth_path
@mkdir_dcrt
def pred_path(self, phase, sub_dir=None, cntd_pts=False):
"""
absolute directory of the prediction results of ct for training dataset
:param cntd_pts:
:param sub_dir:
:param phase: 'train' or 'valid'
:return: directory name
"""
if sub_dir is None:
pred_path = self.results_path + '/' + self.task + '/' + phase + '/pred/' + self.sub_dir() + '/' + self.str_name
else:
pred_path = self.results_path + '/' + self.task + '/' + phase + '/pred/' + sub_dir + '/' + self.str_name
if cntd_pts:
pred_path += "/cntd_pts"
return pred_path
@mkdir_dcrt
def dices_fpath(self, phase):
"""
full path of the saved dice
:param phase: 'train' or 'valid'
:return: file name to save dice
"""
pred_path = self.pred_path(phase)
return pred_path + '/dices.csv'
@mkdir_dcrt
def all_metrics_fpath(self, phase, fissure=False, sub_dir=sub_dir):
"""
full path of the saved dice
:param sub_dir:
:param fissure:
:param phase: 'train' or 'valid'
:return: file name to save dice
"""
pred_path = self.pred_path(phase, sub_dir=sub_dir)
if fissure:
return pred_path + '/all_metrics_fissure.csv'
else:
return pred_path + '/all_metrics.csv'