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

Private GIT Repository
Initialize
[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
8 from regularjson import search, RT_PATH, JSON_GTS, INFA_STR
9
10 # constants
11 GLOB_DIR = '../../Data/Images_anonymous/'
12 OUT_DIR = './train/'
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         for cas in l[START:END]:# cas like 'Case0002'
26                 caspath = join(GLOB_DIR, cas)
27
28                 if not INDICE_CASE in cas:# if the directory name doesn't sound familiar
29                         continue
30
31                 l1 = sorted( ls( caspath ) )# just ls one CaseXXXX and sort the result
32                 # $$NOTRUSTREF here we need to sort because there is no trusted ref or id
33
34                 r = search(RT_PATH, cas)
35
36                 print(cas, end=' ', flush=True) # log CaseXXXX
37
38                 if not r: # if the feature doesn't yet labeled by an expert go to next
39                         continue
40                 r = search(r['path'], '.') # to be more dynamic, here can just be '.' instead of '1.2.3.4.5.6'
41
42                 if r:
43                         r = r['path']
44                         try:
45                                 l2 = sorted( ls(r) ) # $$NOTRUSTREF
46                         except NotADirectoryError as e:
47                                 print("WARN", "a file in unexcepted place")
48                                 continue
49
50                         if 0 < len(l2) < len(l1):
51                                 r = join(r, l2[0])
52                                 l2 = sorted( ls(r) ) # $$NOTRUSTREF
53                                 # Try once : check subdirectory if i'am not the right
54
55                         if 0 < len(l2) < len(l1):
56                                 # TODO: log, json doesn't match with images (labels != features)
57                                 print("WARN", "json doesn't match with images (labels != features), Case", cas)
58                                 continue
59                                 pass
60
61
62                         for i, dic in enumerate(l1):
63                                 # print('join', r, l2[i])
64                                 ref = join(r, l2[i]) # logically, should be the json ref of i dicom image
65
66                                 infarctus = search(ref, INFA_STR)
67                                 if infarctus:
68                                         # print("infarctus:", infarctus) # Testing..
69                                         # topng(join(caspath, dic), '%/%-%' % (join(OUT_DIR, 'infarctus'), cas, dic)
70                                         # print(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'infarctus'), cas, dic)) # Testing..
71                                         topng(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'infarctus'), cas, dic))
72                                 else:
73                                         # print("no infarctus:", infarctus) # Testing..
74                                         # print(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'noinfarctus'), cas, dic)) # Testing..
75                                         topng(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'noinfarctus'), cas, dic))
76
77         print('Ended!')