1 from os import listdir as ls
4 from os.path import join
7 from topng import topng
8 from regularjson import search, RT_PATH, JSON_GTS, INFA_STR
11 GLOB_DIR = '../../Data/Images_anonymous/'
12 OUT_DIR = './generated/train/'
15 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
21 return sorted( ls(r) )
23 if __name__ == '__main__':
24 l = sorted(ls(GLOB_DIR))
27 wmin = hmin = None # None is important here, confert the 'minimum' algo
28 wmax = hmax = w = h = 0
30 for cas in l[START:END]:# cas like 'Case0002'
31 caspath = join(GLOB_DIR, cas)
33 if not INDICE_CASE in cas:# if the directory name doesn't sound familiar
36 l1 = sorted( ls( caspath ) )# just ls one CaseXXXX and sort the result
37 # $$NOTRUSTREF here we need to sort because there is no trusted ref or id
39 r = search(RT_PATH, cas)
41 print(cas, end=' ', flush=True) # log CaseXXXX
43 if not r: # if the feature doesn't yet labeled by an expert go to next
45 r = search(r['path'], '.') # to be more dynamic, here can just be '.' instead of '1.2.3.4.5.6'
50 l2 = sorted( ls(r) ) # $$NOTRUSTREF
51 except NotADirectoryError as e:
52 print("WARN", "a file in unexcepted place")
55 if 0 < len(l2) < len(l1):
57 l2 = sorted( ls(r) ) # $$NOTRUSTREF
58 # Try once : check subdirectory if i'am not the right
60 if 0 < len(l2) < len(l1):
61 # TODO: log, json doesn't match with images (labels != features)
62 print("WARN", "json doesn't match with images (labels != features), Case", cas)
66 for i, dic in enumerate(l1):
67 # print('join', r, l2[i])
68 ref = join(r, l2[i]) # logically, should be the json ref of i dicom image
70 infarctus = search(ref, INFA_STR)
72 # print("infarctus:", infarctus) # Testing..
73 # topng(join(caspath, dic), '%/%-%' % (join(OUT_DIR, 'infarctus'), cas, dic)
74 # print(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'infarctus'), cas, dic)) # Testing..
75 w, h = topng(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'infarctus'), cas, dic))
77 # print("no infarctus:", infarctus) # Testing..
78 # print(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'noinfarctus'), cas, dic)) # Testing..
79 w, h = topng(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'noinfarctus'), cas, dic))
85 # search width minimum
86 if wmin is None: wmin = w
87 elif wmin > w: wmin = w
89 # search height minimum
90 if hmin is None: hmin = h
91 elif hmin > h: hmin = h
93 print('min-width, max-width:', (wmin, wmax))
94 print('min-height, max-height:', (hmin, hmax))