1 from os import listdir as ls
4 from os.path import join, dirname
7 from topng import topng, getimgname, mask, CROP_SIZE, EPI_MIN_PIXELS
8 from regularjson import search, RT_PATH, INFA_STR, EPI_STR, ENDO_STR
11 GLOB_DIR = '../../Data/Images_anonymous/'
12 OUT_DIR = './generated/updated/'
14 PREFIX = '0_' # => 0_Case0002
17 'Case_0258', 'Case_0259',
18 'Case_0265', 'Case_0266', 'Case_0300', 'Case_0305',
19 'Case_0311', 'Case_0316',
20 'Case_0320', 'Case_0322', 'Case_0325', 'Case_0326', 'Case_0327',
21 'Case_0328', 'Case_0329', 'Case_0337', 'Case_0338', 'Case_0345',
22 'Case_0346', 'Case_0348', 'Case_0359', 'Case_0360', 'Case_0361',
23 'Case_0362', 'Case_0363', 'Case_0364', 'Case_0365', 'Case_0366',
24 'Case_0367', 'Case_0381', 'Case_0385', 'Case_0400', 'Case_0401', 'Case_0415', 'Case_0416',
25 'Case_0421','Case_0423', 'Case_0429', 'Case_0442', 'Case_0444', 'Case_0474',
28 BLACK_LIST_IMAGES = ['Case_0229-Image00002', 'Case_0229-Image00004', 'Case_0229-Image00006', 'Case_0229-Image00007',
29 'Case_0243-Image00003.png', 'Case_0243-Image00004', 'Case_0243-Image00005', 'Case_0265-Image00001', 'Case_0262-Image00007',
30 'Case_0262-Image00008', 'Case_0264-Image00006', 'Case_0275-Image00006',
31 'Case_0307-Image00006', 'Case_0314-Image00001', 'Case_0314-Image00002',
32 'Case_0314-Image00008', 'Case_0315-Image00005', 'Case_0321-Image00007',
33 'Case_0321-Image00008', 'Case_0328-Image00002',
35 'Case_0356-Image00007', 'Case_0356-Image00008', 'Case_0356-Image00009', 'Case_0356-Image00009',
36 'Case_0358-Image00008', 'Case_0358-Image00009',
37 'Case_0377-Image00009',
40 START = None # to manage the number of Patient Case to use in training + validation, if START == None => 0, if END == None => last index, it will use all in GLOB_DIR
41 END = None # like Case_420, to generate just a part of the dataset
42 END_INF_CASES = 309 # to manage correct case with infarctus
43 END_NOINF_CASES = None # to manage correct case with noinfarctus
47 return sorted( ls(r) )
49 if __name__ == '__main__':
50 l = sorted(ls(GLOB_DIR))
56 wmin = hmin = None # None is important here, confert the 'minimum' algo
57 wmax = hmax = w = h = 0
59 for j, cas in enumerate(l[START:END]):# cas like 'Case0002'
60 caspath = join(GLOB_DIR, cas)
62 if not INDICE_CASE in cas: # if the directory name doesn't sound familiar
65 if cas in BLACK_LIST_CASES: # if the directory was black listed
68 l1 = sorted( ls( caspath ) )# just ls one CaseXXXX and sort the result
69 # $$NOTRUSTREF here we need to sort because there is no trusted ref or id
71 # print('RT_PATH', RT_PATH)
72 # print('PREFIX + cas', PREFIX + cas)
73 r = search(RT_PATH, PREFIX + cas)
76 print(cas, end=' ', flush=True) # log CaseXXXX
78 if not r: # if the feature doesn't yet labeled by an expert go to next
81 # ------<< Searching for .json files
82 r = search(r['path'], 'contours') # contours/1.2.3.4.5.6
85 r = search(r['path'], '1.2.3.4.5.6') # to be more dynamic, here can just be '.' instead of '1.2.3.4.5.6'
90 # print('ls(r)', ls(r))
92 l2 = sorted( ls(r) ) # $$NOTRUSTREF
93 except NotADirectoryError as e:
94 print("WARN", "a file in unexcepted place")
97 # IF THIS KIND OF ERROR OCCURS, CHANGE ON DISK TO GET IT CONFORMED
98 if 1 == len(l2) < len(l1): # l2 == ["37"] and l1 == ["image001", "image002"..], may be "37" contains searched dirs, so enter..
100 l2 = sorted( ls(r) ) # $$NOTRUSTREF
101 # Try once : check subdirectory if i'am not the right
103 # if 0 < len(l2) < len(l1):
104 # # TODO: log, json doesn't match with images (labels != features)
105 # print("WARN", "json doesn't match with images (labels != features), Case", cas)
109 for i, dic in enumerate(l1): # l1 => ["image001", "image002"], l2 => ["37", "42"]
111 if "{}-{}".format(cas, dic) in BLACK_LIST_IMAGES: # if image was black listed
120 # search the right json, for that, we open the json
122 # print('join', r, l2[i])
123 ref = join(r, elt) # logically, should be the json ref's "parent directory" of i dicom image
125 t_epimask = search(ref, EPI_STR)
127 parent = dirname(ref) # get the parent dir
129 infarctus = search(parent, INFA_STR)
130 endomask = search(parent, ENDO_STR)
132 if t_epimask and t_epimask.get("path"):
133 timgname = getimgname(t_epimask["path"])
139 print('no epicardic')
142 # print("ref left:", l1[i]) # Testing..
143 # print("ref right:", ref) # Testing..
144 # print("infarctus:", infarctus) # Testing..
145 # topng(join(caspath, dic), '%/%-%' % (join(OUT_DIR, 'infarctus'), cas, dic)
146 # print('epimask', epimask)
152 if epimask:# this condition could be if necessary
153 if infarctus and (END_INF_CASES is not None) and j > END_INF_CASES:
155 if not infarctus and (END_NOINF_CASES is not None) and j > END_NOINF_CASES:
157 # topng(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'infarctus' if infarctus else 'noinfarctus'), cas, dic), epimask=epimask['path'] if epimask else None)
158 # img_, dicimg_, minmax_ = topng(join(caspath, dic),
159 # '{}/{}-{}'.format(join(OUT_DIR, 'infarctus' if infarctus else 'noinfarctus'), cas, dic),
160 # epimask=epimask['path'] if epimask else None,
164 # Possibly data augmentation purpose here
165 print("EPI_MIN_PIXELS", EPI_MIN_PIXELS)
168 outfile='{}/crop-mask/{}-{}'.format(join(OUT_DIR,
169 'infarctus' if infarctus else 'noinfarctus'),
173 epimask=epimask['path'] if epimask else None,
174 centercrop=CROP_SIZE,
176 epiminpixels=EPI_MIN_PIXELS
177 ) # crop and mask with black
181 # join(caspath, dic),
182 # outfile='{}/crop-mask-hollow/{}-{}'.format(join(OUT_DIR,
183 # 'infarctus' if infarctus else 'noinfarctus'),
187 # epimask=epimask['path'] if epimask else None,
188 # endomask=endomask['path'] if endomask else None,
189 # centercrop=CROP_SIZE,
191 # ) # crop and (mask and fill hollow) with black
194 # join(caspath, dic),
195 # outfile='{}/crop-nomask/{}-{}'.format(join(OUT_DIR,
196 # 'infarctus' if infarctus else 'noinfarctus'),
200 # epimask=epimask['path'] if epimask else None,
201 # centercrop=CROP_SIZE,
202 # ) # just crop, don't apply black mask
204 # w, h = BE CAREFULL, be sure w, h is needed, and that topng returns something
206 # join(caspath, dic),
207 # outfile='{}/contour/{}-{}'.format(join(OUT_DIR,
208 # 'infarctus' if infarctus else 'noinfarctus'),
212 # epimask=epimask['path'] if epimask else None,
214 # ) # draw a red contour for visibily purpose
216 # segmentation width & height <<
217 # xmin, ymin, xmax, ymax = minmax_
218 # w = xmax - xmin + 1 # +1 : even the position takes a bit
219 # h = ymax - ymin + 1
220 # segmentation width & height >>
224 # if wmax < w: wmax = w
225 # if hmax < h: hmax = h
227 # # search width minimum
228 # if wmin is None: wmin = w
229 # elif wmin > w: wmin = w
231 # # search height minimum
232 # if hmin is None: hmin = h
233 # elif hmin > h: hmin = h
234 # except Exception as e:
235 # print("WARN", "something went wrong with this image", dic, str(e))
239 # print('min-width, max-width:', (wmin, wmax))
240 # print('min-height, max-height:', (hmin, hmax))