From eeb9b42c3779bbb098c07e2dd91d40ecf6e63372 Mon Sep 17 00:00:00 2001 From: Bernardo TOD Date: Wed, 3 Jul 2019 17:39:53 +0200 Subject: [PATCH 1/1] stable, day after meeting --- Case_index.txt | 393 +++++++++++++++++++++++++++++++++++++++++++++++++ black-list.txt | 21 +++ draw-plt.py | 36 +++++ regularjson.py | 6 +- topng.py | 50 ++++++- tosampledir.py | 15 +- totraindir.py | 277 ++++++++++++++++++++++------------ 7 files changed, 691 insertions(+), 107 deletions(-) create mode 100644 Case_index.txt create mode 100644 black-list.txt create mode 100644 draw-plt.py diff --git a/Case_index.txt b/Case_index.txt new file mode 100644 index 0000000..5c2c143 --- /dev/null +++ b/Case_index.txt @@ -0,0 +1,393 @@ +Case_0002 +Case_0003 +Case_0004 +Case_0005 +Case_0006 +Case_0007 +Case_0008 +Case_0009 +Case_0011 +Case_0012 +Case_0013 +Case_0014 +Case_0015 +Case_0016 +Case_0017 +Case_0018 +Case_0019 +Case_0021 +Case_0023 +Case_0025 +Case_0026 +Case_0027 +Case_0028 +Case_0029 +Case_0030 +Case_0031 +Case_0032 +Case_0035 +Case_0036 +Case_0037 +Case_0040 +Case_0043 +Case_0044 +Case_0045 +Case_0046 +Case_0047 +Case_0048 +Case_0049 +Case_0050 +Case_0051 +Case_0052 +Case_0053 +Case_0055 +Case_0056 +Case_0059 +Case_0060 +Case_0061 +Case_0063 +Case_0064 +Case_0065 +Case_0066 +Case_0067 +Case_0068 +Case_0069 +Case_0070 +Case_0071 +Case_0072 +Case_0073 +Case_0074 +Case_0075 +Case_0076 +Case_0077 +Case_0078 +Case_0079 +Case_0080 +Case_0081 +Case_0082 +Case_0083 +Case_0084 +Case_0085 +Case_0086 +Case_0087 +Case_0088 +Case_0089 +Case_0090 +Case_0091 +Case_0092 +Case_0093 +Case_0094 +Case_0095 +Case_0096 +Case_0097 +Case_0098 +Case_0099 +Case_0100 +Case_0101 +Case_0103 +Case_0104 +Case_0105 +Case_0106 +Case_0107 +Case_0108 +Case_0200 +Case_0201 +Case_0204 +Case_0205 +Case_0206 +Case_0208 +Case_0209 +Case_0210 +Case_0211 +Case_0212 +Case_0213 +Case_0214 +Case_0215 +Case_0216 +Case_0217 +Case_0218 +Case_0219 +Case_0220 +Case_0221 +Case_0222 +Case_0223 +Case_0224 +Case_0225 +Case_0226 +Case_0227 +Case_0228 +Case_0229 +Case_0230 +Case_0231 +Case_0232 +Case_0233 +Case_0234 +Case_0235 +Case_0236 +Case_0237 +Case_0238 +Case_0239 +Case_0240 +Case_0241 +Case_0242 +Case_0243 +Case_0244 +Case_0245 +Case_0246 +Case_0247 +Case_0248 +Case_0249 +Case_0250 +Case_0251 +Case_0252 +Case_0253 +Case_0254 +Case_0255 +Case_0256 +Case_0257 +Case_0258 +Case_0259 +Case_0260 +Case_0261 +Case_0262 +Case_0263 +Case_0264 +Case_0265 +Case_0266 +Case_0267 +Case_0268 +Case_0269 +Case_0270 +Case_0271 +Case_0272 +Case_0273 +Case_0274 +Case_0275 +Case_0276 +Case_0277 +Case_0278 +Case_0279 +Case_0280 +Case_0281 +Case_0282 +Case_0283 +Case_0284 +Case_0285 +Case_0286 +Case_0287 +Case_0288 +Case_0289 +Case_0290 +Case_0291 +Case_0292 +Case_0293 +Case_0294 +Case_0295 +Case_0296 +Case_0297 +Case_0298 +Case_0299 +Case_0300 +Case_0301 +Case_0302 +Case_0303 +Case_0304 +Case_0305 +Case_0306 +Case_0307 +Case_0308 +Case_0309 +Case_0310 +Case_0311 +Case_0312 +Case_0313 +Case_0314 +Case_0315 +Case_0316 +Case_0317 +Case_0318 +Case_0319 +Case_0320 +Case_0321 +Case_0322 +Case_0323 +Case_0324 +Case_0325 +Case_0326 +Case_0327 +Case_0328 +Case_0329 +Case_0330 +Case_0331 +Case_0332 +Case_0333 +Case_0334 +Case_0335 +Case_0336 +Case_0337 +Case_0338 +Case_0339 +Case_0340 +Case_0341 +Case_0342 +Case_0343 +Case_0344 +Case_0345 +Case_0346 +Case_0347 +Case_0348 +Case_0349 +Case_0350 +Case_0351 +Case_0352 +Case_0353 +Case_0354 +Case_0355 +Case_0356 +Case_0357 +Case_0358 +Case_0359 +Case_0360 +Case_0361 +Case_0362 +Case_0363 +Case_0364 +Case_0365 +Case_0366 +Case_0367 +Case_0368 +Case_0369 +Case_0370 +Case_0371 +Case_0372 +Case_0373 +Case_0374 +Case_0375 +Case_0376 +Case_0377 +Case_0378 +Case_0379 +Case_0380 +Case_0381 +Case_0382 +Case_0383 +Case_0384 +Case_0385 +Case_0386 +Case_0387 +Case_0388 +Case_0389 +Case_0390 +Case_0391 +Case_0392 +Case_0393 +Case_0394 +Case_0395 +Case_0396 +Case_0397 +Case_0398 +Case_0399 +Case_0400 +Case_0401 +Case_0402 +Case_0403 +Case_0404 +Case_0405 +Case_0406 +Case_0407 +Case_0408 +Case_0409 +Case_0410 +Case_0411 +Case_0412 +Case_0413 +Case_0414 +Case_0415 +Case_0416 +Case_0417 +Case_0418 +Case_0419 +Case_0420 +Case_0421 +Case_0422 +Case_0423 +Case_0424 +Case_0425 +Case_0426 +Case_0427 +Case_0428 +Case_0429 +Case_0430 +Case_0431 +Case_0432 +Case_0433 +Case_0434 +Case_0435 +Case_0436 +Case_0437 +Case_0438 +Case_0439 +Case_0440 +Case_0441 +Case_0442 +Case_0443 +Case_0444 +Case_0445 +Case_0446 +Case_0447 +Case_0448 +Case_0449 +Case_0450 +Case_0451 +Case_0452 +Case_0453 +Case_0454 +Case_0455 +Case_0456 +Case_0457 +Case_0458 +Case_0459 +Case_0460 +Case_0461 +Case_0462 +Case_0463 +Case_0464 +Case_0465 +Case_0466 +Case_0467 +Case_0468 +Case_0469 +Case_0470 +Case_0471 +Case_0472 +Case_0473 +Case_0474 +Case_0475 +Case_0476 +Case_0477 +Case_0478 +Case_0479 +Case_0480 +Case_0481 +Case_0482 +Case_0483 +Case_0484 +Case_0485 +Case_0486 +Case_0487 +Case_0488 +Case_0489 +Case_0490 +Case_0491 +Case_0492 +Case_0493 +Case_0494 +Case_0495 +Case_0496 +Case_0497 +Case_0498 +Case_0499 +Case_0500 +Case_0501 +Case_0502 +Case_0503 \ No newline at end of file diff --git a/black-list.txt b/black-list.txt new file mode 100644 index 0000000..5474fbd --- /dev/null +++ b/black-list.txt @@ -0,0 +1,21 @@ +================= OLD ================= + + BLACK_LIST_CASES = ['Case_0009', 'Case_0208', 'Case_0258', 'Case_0259', 'Case_0265', 'Case_0266', 'Case_0300', 'Case_0305', 'Case_0311', + 'Case_0311', 'Case_0315', 'Case_0316', 'Case_0319', 'Case_0320', 'Case_0321', 'Case_0322', 'Case_0323', 'Case_0326', + 'Case_0327', 'Case_0328', 'Case_0329', 'Case_0331', 'Case_0332', 'Case_0333', 'Case_0334', 'Case_0335', + 'Case_0337', + 'Case_0338', 'Case_0339', 'Case_0340', 'Case_0342', 'Case_0343', 'Case_0345', 'Case_0346', 'Case_0347', 'Case_0348', + 'Case_0349', 'Case_0350', 'Case_0382', 'Case_0383', 'Case_0384', 'Case_0386', 'Case_0387', 'Case_0388', 'Case_0389', 'Case_0390', 'Case_0391', 'Case_0392', 'Case_0393', + 'Case_0394', 'Case_0395', 'Case_0396', 'Case_0397', 'Case_0398', 'Case_0399', 'Case_0400', 'Case_0401', 'Case_0402', + 'Case_0402', 'Case_0407', 'Case_0409', + ] + + BLACK_LIST_IMAGES = ['Case_0021-Image00006', 'Case_0021-Image00007', 'Case_0021-Image00008', 'Case_0021-Image00009', + 'Case_0098-Image00001', 'Case_0209-59178790', 'Case_0209-59178801', 'Case_0229-Image00002', 'Case_0260-Image00001', + 'Case_0229-Image00004', 'Case_0229-Image00006', 'Case_0243-Image00004', 'Case_0245-Image00007', 'Case_0271-Image00006', + 'Case_0275-Image00002', 'Case_0086-Image07.dcm', 'Case_0309-Image00002', 'Case_0330-Image00007', 'Case_0336-Image00001', 'Case_0336-Image00008', + 'Case_0336-Image00008', 'Case_0336-Image00009', 'Case_0340-Image00005', 'Case_0340-Image00006', 'Case_0340-Image00007', + 'Case_0351-I2000000', 'Case_0351-I6000000', 'Case_0351-I7000000', 'Case_0352-I1100000', 'Case_0386-Image00001', 'Case_0344-Image00008', + 'Case_0344-Image00009', 'Case_0403-Image00009', 'Case_0404-I2000000', 'Case_0403-Image00009', + 'Case_0404-I2000000', 'Case_0406-Image00008', 'Case_0406-Image000089', 'Case_0406-Image00010', + ] \ No newline at end of file diff --git a/draw-plt.py b/draw-plt.py new file mode 100644 index 0000000..5b65075 --- /dev/null +++ b/draw-plt.py @@ -0,0 +1,36 @@ +import numpy as np +import matplotlib.pyplot as plt + +dep = ( 0, pow(2, 8) ) # 9 bits +arr = ( 0, pow(2, 16) ) # 16 bits + +# deprange = np.arange(0, pow(2, 8), 1) # 9 bits + + +# test << +plt.plot(dep, arr, 'r--', linewidth=.3) +# plt.plot(dep, arr, 'r--', deprange, deprange<<8, 'bs', linewidth=.1) +plt.show() + +print("decalage") +for i in deprange: + print( 'i, i<<8:', (i, i<<8) ) +# test >> + +# # test << +# print("affine transfo") +# count = 0 +# for i in deprange: +# ab = dep +# cd = arr + +# a, b = ab[0], ab[1] +# c, d = cd[0], cd[1] + +# r = max( 0, ( (i-a) * (d-c) / (b-a) + c ) ) +# r2 = i<<8 +# print( 'i, r:', (i, r) ) +# print( 'i, i<<8:', (i, r2) ) +# count += (r != r2) + +# print('count', count) \ No newline at end of file diff --git a/regularjson.py b/regularjson.py index 87addeb..bef52b9 100644 --- a/regularjson.py +++ b/regularjson.py @@ -16,11 +16,11 @@ from pprint import pprint # constants -# json +# json OLD may be it was just a separation for the copy, new -> no more that, stay in one directory # |--> json_GT # |--> json_GT_part2 -RT_PATH = '../../Data/json/' -JSON_GTS = ['json_GT', 'json_GT_part2'] +RT_PATH = '../../Data/Contours_updated/' +JSON_GTS = ['json_GT'] INFA_STR = 'Infarction' EPI_STR = 'Epicardic' ENDO_STR = 'Endocardic' diff --git a/topng.py b/topng.py index fde569b..672fd6d 100644 --- a/topng.py +++ b/topng.py @@ -27,6 +27,7 @@ INPUT_DIR = '../../Data/Images_anonymous/Case_0002/' OUT_DIR = './generated/' CROP_SIZE = (45, 45) # (width, height) +EPI_MIN_PIXELS = 30 * 30 # nombre pixels minimal pour considérer un epicardic, else on ignore, 30 * 30, vu que c'est carré RED_COLOR = 100 def roundall(*l): @@ -108,6 +109,43 @@ def getxy(file): return np.array(tmp, dtype=np.int32) # return tmp +def getimgname(file): + """ + { + 'Image00001': [{ + 'color': '#ff0000', + 'points': [ + {'x': 94.377, 'y': 137.39}, + {'x': 100.38, 'y': 139.55}, + {'x': 103.26, 'y': 142.67}, + {'x': 105.91, 'y': 147.95}, + {'x': 105.42, 'y': 152.76}, + {'x': 100.62, 'y': 156.84}, + {'x': 95.338, 'y': 159.96}, + {'x': 89.573, 'y': 158.52}, + {'x': 84.53, 'y': 153}, + {'x': 82.848, 'y': 149.15}, + {'x': 82.368, 'y': 142.91}, + {'x': 85.01, 'y': 138.11}, + {'x': 89.813, 'y': 137.39}, + {'x': 94.377, 'y': 137.39} + ] + }] + } + return 'Image00001' or return '' + """ + imgXXX = None + # print("file", file) + with open(file) as jsonfile: + data = json.load(jsonfile) + if len(data) > 1: + print("more than one image in same json") + # print("data") + # pprint(data) + for imgXXX in data: pass # get the value of key ~= "Image00001", cause it's dynamic + return imgXXX + + def minmax(file): r = getxy(file) if r is not None: @@ -285,7 +323,7 @@ def readpng(inputfile):# just a specific func to preview a "shape = (X,Y,3)" ima for i, row in enumerate(tab): print(row[0]*65536 + row[0]*256 + row[0], end=" " if i % image.shape[0] != 0 else "\n") -def topng(inputfile, outfile=None, overwrite=True, verbose=False, epimask='', endomask='', centercrop=None, blackmask=False, square=False, redcontour=False): +def topng(inputfile, outfile=None, overwrite=True, verbose=False, epimask='', endomask='', centercrop=None, blackmask=False, square=False, redcontour=False, epiminpixels=-1): """ (verbose) return (64, 64) : the width and height (not verbose) return (img, dicimg) : the image and the dicimg objects @@ -348,9 +386,18 @@ def topng(inputfile, outfile=None, overwrite=True, verbose=False, epimask='', en if endomask: img = hollowmask(img, epimask, endomask) else: + # ignore small epicardic --<< + xmin, ymin, xmax, ymax = minmax(epimask) + pixels = (xmax - xmin) * (ymax - ymin) + if pixels <= epiminpixels: + print( "small epicardic ({}), ignored!".format(inputfile.split('/')[-1]) ) + return + # ignore small epicardic -->> + img = mask(img, epimask) if centercrop: + img = crop(img, epimask, centercrop) @@ -376,6 +423,7 @@ def topng(inputfile, outfile=None, overwrite=True, verbose=False, epimask='', en # np.savetxt(savepath + '.npy', img) # test >> + if np.count_nonzero(img) > 0: # matrix not full of zero cv2.imwrite(savepath, img, [cv2.IMWRITE_PNG_COMPRESSION, 0]) # write png image diff --git a/tosampledir.py b/tosampledir.py index 1846c1d..98d9aa5 100644 --- a/tosampledir.py +++ b/tosampledir.py @@ -3,9 +3,9 @@ from os.path import join, splitext from shutil import move as mv, copyfile as cp import pathlib -def move(indir, outdir, n, distinction_word='', copy=True):# move n ordered files from indir to outdir +def move(indir, outdir, start=None, end=None, distinction_word='', copy=True):# move start ordered files from indir to outdir l = sorted(ls(indir)) - for filename in l[:n]: + for filename in l[start:end]: # print(join(indir, filename), join(outdir, filename)) pathlib.Path(outdir).mkdir(parents=True, exist_ok=True) fname, fextension = splitext(filename) @@ -14,7 +14,10 @@ def move(indir, outdir, n, distinction_word='', copy=True):# move n ordered file if __name__ == '__main__': - move('./generated/90/infarctus/crop-nomask', './sample/train/infarctus', 400, distinction_word="-crop-nomask") # move 400 ordered files from param 1 to param 2 - move('./generated/90/noinfarctus/crop-nomask', './sample/train/noinfarctus', 400, distinction_word="-crop-nomask") - move('./generated/90/infarctus/crop-nomask', './sample/valid/infarctus', 100, distinction_word="-crop-nomask") - move('./generated/90/noinfarctus/crop-nomask', './sample/valid/noinfarctus', 100, distinction_word="-crop-nomask") \ No newline at end of file + move('./generated/updated/infarctus/crop-mask', './sample/train/infarctus', 0, 800, distinction_word="-crop-mask") # move [0 -> 600] ordered files from param 1 to param 2 + move('./generated/updated/infarctus/crop-mask', './sample/valid/infarctus', 800, 1100, distinction_word="-crop-mask") + move('./generated/updated/infarctus/crop-mask', './sample/test/infarctus', 1100, 1200, distinction_word="-crop-mask") + + move('./generated/updated/noinfarctus/crop-mask', './sample/train/noinfarctus', 0, 100, distinction_word="-crop-mask") + move('./generated/updated/noinfarctus/crop-mask', './sample/valid/noinfarctus', 100, 160, distinction_word="-crop-mask") + move('./generated/updated/noinfarctus/crop-mask', './sample/test/noinfarctus', 160, 200, distinction_word="-crop-mask") diff --git a/totraindir.py b/totraindir.py index b901b85..374eb84 100644 --- a/totraindir.py +++ b/totraindir.py @@ -1,20 +1,46 @@ from os import listdir as ls import png import pydicom -from os.path import join +from os.path import join, dirname # locals -from topng import topng, mask, CROP_SIZE -from regularjson import search, RT_PATH, JSON_GTS, INFA_STR, EPI_STR, ENDO_STR +from topng import topng, getimgname, mask, CROP_SIZE, EPI_MIN_PIXELS +from regularjson import search, RT_PATH, INFA_STR, EPI_STR, ENDO_STR # constants GLOB_DIR = '../../Data/Images_anonymous/' -OUT_DIR = './generated/90/' +OUT_DIR = './generated/updated/' INDICE_CASE = 'Case' +PREFIX = '0_' # => 0_Case0002 + +BLACK_LIST_CASES = [ + 'Case_0258', 'Case_0259', + 'Case_0265', 'Case_0266', 'Case_0300', 'Case_0305', + 'Case_0311', 'Case_0316', + 'Case_0320', 'Case_0322', 'Case_0325', 'Case_0326', 'Case_0327', + 'Case_0328', 'Case_0329', 'Case_0337', 'Case_0338', 'Case_0345', + 'Case_0346', 'Case_0348', 'Case_0359', 'Case_0360', 'Case_0361', + 'Case_0362', 'Case_0363', 'Case_0364', 'Case_0365', 'Case_0366', + 'Case_0367', 'Case_0381', 'Case_0385', 'Case_0400', 'Case_0401', 'Case_0415', 'Case_0416', + 'Case_0421','Case_0423', 'Case_0429', 'Case_0442', 'Case_0444', 'Case_0474', +] + +BLACK_LIST_IMAGES = ['Case_0229-Image00002', 'Case_0229-Image00004', 'Case_0229-Image00006', 'Case_0229-Image00007', + 'Case_0243-Image00003.png', 'Case_0243-Image00004', 'Case_0243-Image00005', 'Case_0265-Image00001', 'Case_0262-Image00007', + 'Case_0262-Image00008', 'Case_0264-Image00006', 'Case_0275-Image00006', + 'Case_0307-Image00006', 'Case_0314-Image00001', 'Case_0314-Image00002', + 'Case_0314-Image00008', 'Case_0315-Image00005', 'Case_0321-Image00007', + 'Case_0321-Image00008', 'Case_0328-Image00002', + 'Case_0331-I7000000', + 'Case_0356-Image00007', 'Case_0356-Image00008', 'Case_0356-Image00009', 'Case_0356-Image00009', + 'Case_0358-Image00008', 'Case_0358-Image00009', + 'Case_0377-Image00009', +] 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 -END = None - +END = None # like Case_420, to generate just a part of the dataset +END_INF_CASES = 309 # to manage correct case with infarctus +END_NOINF_CASES = None # to manage correct case with noinfarctus def get(l, i, r): if len(l) <= 1: @@ -23,134 +49,191 @@ def get(l, i, r): if __name__ == '__main__': l = sorted(ls(GLOB_DIR)) + # print("log", l) + # exit() + # Initiliaze wmin = hmin = None # None is important here, confert the 'minimum' algo wmax = hmax = w = h = 0 - for cas in l[START:END]:# cas like 'Case0002' + for j, cas in enumerate(l[START:END]):# cas like 'Case0002' caspath = join(GLOB_DIR, cas) - if not INDICE_CASE in cas:# if the directory name doesn't sound familiar + if not INDICE_CASE in cas: # if the directory name doesn't sound familiar + continue + + if cas in BLACK_LIST_CASES: # if the directory was black listed continue l1 = sorted( ls( caspath ) )# just ls one CaseXXXX and sort the result # $$NOTRUSTREF here we need to sort because there is no trusted ref or id - r = search(RT_PATH, cas) + # print('RT_PATH', RT_PATH) + # print('PREFIX + cas', PREFIX + cas) + r = search(RT_PATH, PREFIX + cas) + # print('r', r) print(cas, end=' ', flush=True) # log CaseXXXX if not r: # if the feature doesn't yet labeled by an expert go to next continue - r = search(r['path'], '.') # to be more dynamic, here can just be '.' instead of '1.2.3.4.5.6' + + # ------<< Searching for .json files + r = search(r['path'], 'contours') # contours/1.2.3.4.5.6 + if not r: + continue + 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' + # ------>> if r: r = r['path'] + # print('ls(r)', ls(r)) try: l2 = sorted( ls(r) ) # $$NOTRUSTREF except NotADirectoryError as e: print("WARN", "a file in unexcepted place") continue - if 0 < len(l2) < len(l1): + # IF THIS KIND OF ERROR OCCURS, CHANGE ON DISK TO GET IT CONFORMED + if 1 == len(l2) < len(l1): # l2 == ["37"] and l1 == ["image001", "image002"..], may be "37" contains searched dirs, so enter.. r = join(r, l2[0]) l2 = sorted( ls(r) ) # $$NOTRUSTREF # Try once : check subdirectory if i'am not the right - if 0 < len(l2) < len(l1): - # TODO: log, json doesn't match with images (labels != features) - print("WARN", "json doesn't match with images (labels != features), Case", cas) - continue - pass - - for i, dic in enumerate(l1): - # print('join', r, l2[i]) - ref = join(r, l2[i]) # logically, should be the json ref's "parent directory" of i dicom image - - infarctus = search(ref, INFA_STR) - epimask = search(ref, EPI_STR) - endomask = search(ref, ENDO_STR) - # print("ref left:", l1[i]) # Testing.. - # print("ref right:", ref) # Testing.. - # print("infarctus:", infarctus) # Testing.. - # topng(join(caspath, dic), '%/%-%' % (join(OUT_DIR, 'infarctus'), cas, dic) - # print('epimask', epimask) - - if epimask:# this condition could be if necessary - # w, h = topng(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'infarctus' if infarctus else 'noinfarctus'), cas, dic), epimask=epimask['path'] if epimask else None) - # img_, dicimg_, minmax_ = topng(join(caspath, dic), - # '{}/{}-{}'.format(join(OUT_DIR, 'infarctus' if infarctus else 'noinfarctus'), cas, dic), - # epimask=epimask['path'] if epimask else None, - # verbose=True, - # ) - - # Possibly data augmentation purpose here - w, h = topng( - join(caspath, dic), - outfile='{}/crop-mask/{}-{}'.format(join(OUT_DIR, - 'infarctus' if infarctus else 'noinfarctus'), - cas, - dic, - ), - epimask=epimask['path'] if epimask else None, - centercrop=CROP_SIZE, - blackmask=True, - ) # crop and mask with black - - if endomask: - w, h = topng( + # if 0 < len(l2) < len(l1): + # # TODO: log, json doesn't match with images (labels != features) + # print("WARN", "json doesn't match with images (labels != features), Case", cas) + # continue + # pass + + for i, dic in enumerate(l1): # l1 => ["image001", "image002"], l2 => ["37", "42"] + if True: + if "{}-{}".format(cas, dic) in BLACK_LIST_IMAGES: # if image was black listed + continue + + epimask = '' + infarctus = '' + endomask = '' + # debug << + # infarctus = True + # debug >> + # search the right json, for that, we open the json + for elt in l2: + # print('join', r, l2[i]) + ref = join(r, elt) # logically, should be the json ref's "parent directory" of i dicom image + + t_epimask = search(ref, EPI_STR) + + parent = dirname(ref) # get the parent dir + + infarctus = search(parent, INFA_STR) + endomask = search(parent, ENDO_STR) + + if t_epimask and t_epimask.get("path"): + timgname = getimgname(t_epimask["path"]) + + if timgname == dic: + epimask = t_epimask + break + else: + print('no epicardic') + + + # print("ref left:", l1[i]) # Testing.. + # print("ref right:", ref) # Testing.. + # print("infarctus:", infarctus) # Testing.. + # topng(join(caspath, dic), '%/%-%' % (join(OUT_DIR, 'infarctus'), cas, dic) + # print('epimask', epimask) + + # debug << + # epimask = False + # debug >> + + if epimask:# this condition could be if necessary + if infarctus and (END_INF_CASES is not None) and j > END_INF_CASES: + continue + if not infarctus and (END_NOINF_CASES is not None) and j > END_NOINF_CASES: + continue + # topng(join(caspath, dic), '{}/{}-{}'.format(join(OUT_DIR, 'infarctus' if infarctus else 'noinfarctus'), cas, dic), epimask=epimask['path'] if epimask else None) + # img_, dicimg_, minmax_ = topng(join(caspath, dic), + # '{}/{}-{}'.format(join(OUT_DIR, 'infarctus' if infarctus else 'noinfarctus'), cas, dic), + # epimask=epimask['path'] if epimask else None, + # verbose=True, + # ) + + # Possibly data augmentation purpose here + print("EPI_MIN_PIXELS", EPI_MIN_PIXELS) + topng( join(caspath, dic), - outfile='{}/crop-mask-hollow/{}-{}'.format(join(OUT_DIR, + outfile='{}/crop-mask/{}-{}'.format(join(OUT_DIR, 'infarctus' if infarctus else 'noinfarctus'), cas, dic, ), epimask=epimask['path'] if epimask else None, - endomask=endomask['path'] if endomask else None, centercrop=CROP_SIZE, blackmask=True, - ) # crop and (mask and fill hollow) with black - - w, h = topng( - join(caspath, dic), - outfile='{}/crop-nomask/{}-{}'.format(join(OUT_DIR, - 'infarctus' if infarctus else 'noinfarctus'), - cas, - dic, - ), - epimask=epimask['path'] if epimask else None, - centercrop=CROP_SIZE, - ) # just crop, don't apply black mask - - # w, h = topng( - # join(caspath, dic), - # outfile='{}/contour/{}-{}'.format(join(OUT_DIR, - # 'infarctus' if infarctus else 'noinfarctus'), - # cas, - # dic, - # ), - # epimask=epimask['path'] if epimask else None, - # redcontour=True, - # ) # draw a red contour for visibily purpose - - # segmentation width & height << - # xmin, ymin, xmax, ymax = minmax_ - # w = xmax - xmin + 1 # +1 : even the position takes a bit - # h = ymax - ymin + 1 - # segmentation width & height >> - - - # # search maximums - # if wmax < w: wmax = w - # if hmax < h: hmax = h - - # # search width minimum - # if wmin is None: wmin = w - # elif wmin > w: wmin = w - - # # search height minimum - # if hmin is None: hmin = h - # elif hmin > h: hmin = h + epiminpixels=EPI_MIN_PIXELS + ) # crop and mask with black + + # if endomask: + # topng( + # join(caspath, dic), + # outfile='{}/crop-mask-hollow/{}-{}'.format(join(OUT_DIR, + # 'infarctus' if infarctus else 'noinfarctus'), + # cas, + # dic, + # ), + # epimask=epimask['path'] if epimask else None, + # endomask=endomask['path'] if endomask else None, + # centercrop=CROP_SIZE, + # blackmask=True, + # ) # crop and (mask and fill hollow) with black + + # topng( + # join(caspath, dic), + # outfile='{}/crop-nomask/{}-{}'.format(join(OUT_DIR, + # 'infarctus' if infarctus else 'noinfarctus'), + # cas, + # dic, + # ), + # epimask=epimask['path'] if epimask else None, + # centercrop=CROP_SIZE, + # ) # just crop, don't apply black mask + + # w, h = BE CAREFULL, be sure w, h is needed, and that topng returns something + # topng( + # join(caspath, dic), + # outfile='{}/contour/{}-{}'.format(join(OUT_DIR, + # 'infarctus' if infarctus else 'noinfarctus'), + # cas, + # dic, + # ), + # epimask=epimask['path'] if epimask else None, + # redcontour=True, + # ) # draw a red contour for visibily purpose + + # segmentation width & height << + # xmin, ymin, xmax, ymax = minmax_ + # w = xmax - xmin + 1 # +1 : even the position takes a bit + # h = ymax - ymin + 1 + # segmentation width & height >> + + + # # search maximums + # if wmax < w: wmax = w + # if hmax < h: hmax = h + + # # search width minimum + # if wmin is None: wmin = w + # elif wmin > w: wmin = w + + # # search height minimum + # if hmin is None: hmin = h + # elif hmin > h: hmin = h + # except Exception as e: + # print("WARN", "something went wrong with this image", dic, str(e)) + # continue # print('min-width, max-width:', (wmin, wmax)) -- 2.39.5