]> AND Private Git Repository - myo-class.git/blob - totraindir.py
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
b901b85bb7ea34c22bf7730eb83f58f8327e717d
[myo-class.git] / totraindir.py
1 from os import listdir as ls
2 import png
3 import pydicom
4 from os.path import join
5
6 # locals
7 from topng import topng, mask, CROP_SIZE
8 from regularjson import search, RT_PATH, JSON_GTS, INFA_STR, EPI_STR, ENDO_STR
9
10 # constants
11 GLOB_DIR = '../../Data/Images_anonymous/'
12 OUT_DIR = './generated/90/'
13 INDICE_CASE = 'Case'
14
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
16 END = None
17
18
19 def get(l, i, r):
20         if len(l) <= 1:
21                 return sorted( ls(r) )
22
23 if __name__ == '__main__':
24         l = sorted(ls(GLOB_DIR))
25
26         # Initiliaze 
27         wmin = hmin = None # None is important here, confert the 'minimum' algo
28         wmax = hmax = w = h = 0
29
30         for cas in l[START:END]:# cas like 'Case0002'
31                 caspath = join(GLOB_DIR, cas)
32
33                 if not INDICE_CASE in cas:# if the directory name doesn't sound familiar
34                         continue
35
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
38
39                 r = search(RT_PATH, cas)
40
41                 print(cas, end=' ', flush=True) # log CaseXXXX
42
43                 if not r: # if the feature doesn't yet labeled by an expert go to next
44                         continue
45                 r = search(r['path'], '.') # to be more dynamic, here can just be '.' instead of '1.2.3.4.5.6'
46
47                 if r:
48                         r = r['path']
49                         try:
50                                 l2 = sorted( ls(r) ) # $$NOTRUSTREF
51                         except NotADirectoryError as e:
52                                 print("WARN", "a file in unexcepted place")
53                                 continue
54
55                         if 0 < len(l2) < len(l1):
56                                 r = join(r, l2[0])
57                                 l2 = sorted( ls(r) ) # $$NOTRUSTREF
58                                 # Try once : check subdirectory if i'am not the right
59
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)
63                                 continue
64                                 pass
65
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's "parent directory" of i dicom image
69
70                                 infarctus = search(ref, INFA_STR)
71                                 epimask = search(ref, EPI_STR)
72                                 endomask = search(ref, ENDO_STR)
73                                 # print("ref left:", l1[i]) # Testing..
74                                 # print("ref right:", ref) # Testing..
75                                 # print("infarctus:", infarctus) # Testing..
76                                 # topng(join(caspath, dic), '%/%-%' % (join(OUT_DIR, 'infarctus'), cas, dic)
77                                 # print('epimask', epimask)
78
79                                 if epimask:# this condition could be if necessary
80                                         # w, h = topng(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'infarctus' if infarctus else 'noinfarctus'), cas, dic), epimask=epimask['path'] if epimask else None)
81                                         # img_, dicimg_, minmax_ = topng(join(caspath, dic),
82                                         #       '{}/{}-{}'.format(join(OUT_DIR, 'infarctus' if infarctus else 'noinfarctus'), cas, dic),
83                                         #       epimask=epimask['path'] if epimask else None,
84                                         #       verbose=True,
85                                         # )
86
87                                         # Possibly data augmentation purpose here
88                                         w, h = topng(
89                                                 join(caspath, dic),
90                                                 outfile='{}/crop-mask/{}-{}'.format(join(OUT_DIR,
91                                                         'infarctus' if infarctus else 'noinfarctus'),
92                                                         cas,
93                                                         dic,
94                                                 ),
95                                                 epimask=epimask['path'] if epimask else None,
96                                                 centercrop=CROP_SIZE,
97                                                 blackmask=True,
98                                         ) # crop and mask with black
99
100                                         if endomask:
101                                                 w, h = topng(
102                                                         join(caspath, dic),
103                                                         outfile='{}/crop-mask-hollow/{}-{}'.format(join(OUT_DIR,
104                                                                 'infarctus' if infarctus else 'noinfarctus'),
105                                                                 cas,
106                                                                 dic,
107                                                         ),
108                                                         epimask=epimask['path'] if epimask else None,
109                                                         endomask=endomask['path'] if endomask else None,
110                                                         centercrop=CROP_SIZE,
111                                                         blackmask=True,
112                                                 ) # crop and (mask and fill hollow) with black
113
114                                         w, h = topng(
115                                                 join(caspath, dic),
116                                                 outfile='{}/crop-nomask/{}-{}'.format(join(OUT_DIR,
117                                                         'infarctus' if infarctus else 'noinfarctus'),
118                                                         cas,
119                                                         dic,
120                                                 ),
121                                                 epimask=epimask['path'] if epimask else None,
122                                                 centercrop=CROP_SIZE,
123                                         ) # just crop, don't apply black mask
124
125                                         # w, h = topng(
126                                         #       join(caspath, dic),
127                                         #       outfile='{}/contour/{}-{}'.format(join(OUT_DIR,
128                                         #               'infarctus' if infarctus else 'noinfarctus'),
129                                         #               cas,
130                                         #               dic,
131                                         #       ),
132                                         #       epimask=epimask['path'] if epimask else None,
133                                         #       redcontour=True,
134                                         # ) # draw a red contour for visibily purpose
135
136                                         # segmentation width & height <<
137                                         # xmin, ymin, xmax, ymax = minmax_
138                                         # w = xmax - xmin + 1 # +1 : even the position takes a bit
139                                         # h = ymax - ymin + 1
140                                         # segmentation width & height >>
141
142
143                                         # # search maximums
144                                         # if wmax < w: wmax = w 
145                                         # if hmax < h: hmax = h
146
147                                         # # search width minimum
148                                         # if wmin is None: wmin = w
149                                         # elif wmin > w: wmin = w
150                                         
151                                         # # search height minimum
152                                         # if hmin is None: hmin = h
153                                         # elif hmin > h: hmin = h
154
155
156         # print('min-width, max-width:', (wmin, wmax))
157         # print('min-height, max-height:', (hmin, hmax))
158
159         # print('Ended!')