Skip to content

Commit 224f235

Browse files
author
elitap
committed
updated jupyter scripts
1 parent 9a5e008 commit 224f235

9 files changed

+444
-295
lines changed

scripts/calc_foreground_label_otsu.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,4 +66,4 @@ def gnerateForgroundMap(dir, filter):
6666
)
6767

6868
args = parser.parse_args()
69-
gnerateForgroundMap(args.dir, args.filter)
69+
gnerateForgroundMap(args.datasetpath, args.filter)
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import os
2+
import argparse
3+
4+
import shutil
5+
import SimpleITK as sitk
6+
import defs
7+
import uuid
8+
import subprocess
9+
from multiprocessing.pool import ThreadPool
10+
import threading
11+
12+
DMAP_SUBDIR = "dmaps"
13+
TMP_SUBDIR = "tmp"
14+
15+
16+
def create_empty_tmp_dir(dir):
17+
tmp = os.path.join(dir, TMP_SUBDIR)
18+
if os.path.exists(tmp):
19+
shutil.rmtree(tmp)
20+
os.makedirs(tmp)
21+
return tmp
22+
23+
24+
def create_dmap_dir(dir):
25+
dmap_dir = os.path.join(dir, DMAP_SUBDIR)
26+
if not os.path.exists(dmap_dir):
27+
os.makedirs(dmap_dir)
28+
29+
30+
def create_dmap((file, dir, tmp_dir)):
31+
itk_img = sitk.ReadImage(os.path.join(dir, file), sitk.sitkUInt8)
32+
dmap_dir = os.path.join(dir, DMAP_SUBDIR)
33+
34+
np_img = sitk.GetArrayFromImage(itk_img)
35+
36+
for key, value in defs.LABELS.iteritems():
37+
np_img_cpy = np_img.copy()
38+
np_img_cpy[np_img != value] = 0
39+
np_img_cpy[np_img == value] = 1
40+
41+
itk_img_save = sitk.GetImageFromArray(np_img_cpy)
42+
43+
itk_img_save.SetOrigin(itk_img.GetOrigin())
44+
itk_img_save.SetDirection(itk_img.GetDirection())
45+
itk_img_save.SetSpacing(itk_img.GetSpacing())
46+
47+
tmp_file = os.path.join(tmp_dir, str(uuid.uuid4()) + ".nii.gz")
48+
sitk.WriteImage(itk_img_save, tmp_file)
49+
50+
filename = os.path.join(dmap_dir, file[:9] + "_" + key + ".nii.gz")
51+
print threading.currentThread(), filename
52+
subprocess.check_output(['plastimatch', 'dmap', '--input', tmp_file, '--output', filename])
53+
54+
55+
def split_seg_files(dir, filter, threads):
56+
tmp_dir = create_empty_tmp_dir(dir)
57+
create_dmap_dir(dir)
58+
data = list()
59+
for f in os.listdir(dir):
60+
if filter in f:
61+
data.append([f, dir, tmp_dir])
62+
63+
t = ThreadPool(threads)
64+
t.map(create_dmap, data)
65+
66+
shutil.rmtree(os.path.join(dir, TMP_SUBDIR))
67+
68+
69+
if __name__ == "__main__":
70+
parser = argparse.ArgumentParser(prog='')
71+
72+
parser.add_argument('--data',
73+
required=True,
74+
help="directory containing the segmentation files which should be split to individual organs"
75+
)
76+
parser.add_argument('--filter',
77+
required=False,
78+
default='segmentation',
79+
help="filter to find the ")
80+
parser.add_argument('--threads','-t',
81+
required=False,
82+
type=int,
83+
default=1)
84+
85+
args = parser.parse_args()
86+
split_seg_files(args.data, args.filter, args.threads)

scripts/collect_dataset_statistics.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import os
55

66

7-
VALID_FILES = [".mha", ".nrrd", ".mhd"]
7+
VALID_FILES = [".mha", ".nrrd", ".mhd", ".gz"]
88

99
class ImageInfo:
1010
def __init__(self, file, size, spacing, minMax, labelBB = None):
@@ -35,6 +35,9 @@ def collectImgWithLabels(dataset, labelpostfix, volumepostfix):
3535

3636
dataset = os.path.abspath(dataset)
3737
imageInfoList = []
38+
cnt_datasets = 0
39+
cnt_invalid = 0
40+
size_dict = dict()
3841

3942
# for test purposes just use some of the files in the dir eg
4043
# os.listdir(dataset)[:6]
@@ -49,6 +52,17 @@ def collectImgWithLabels(dataset, labelpostfix, volumepostfix):
4952
for segmentation in os.listdir(dataset):
5053
if labelpostfix in segmentation and file[:9] in segmentation:
5154
itk_img, segmentationInfo = getImageInfo(segmentation, dataset)
55+
print volumeInfo.file
56+
print segmentationInfo.file
57+
58+
size_dict[file[:9]] = (segmentationInfo.size, segmentationInfo.spacing)
59+
60+
np_img = sitk.GetArrayFromImage(itk_img)
61+
organ_labels = np.unique(np_img)
62+
cnt_datasets += 1
63+
if not np.array_equal(organ_labels, np.array(range(8))):
64+
print "Not all organs present", organ_labels
65+
cnt_invalid += 1
5266

5367
#mand_and_par = sitk.BinaryThreshold(itk_img, 5, 8) #lables for right/left parotis and mandibula
5468

@@ -61,7 +75,8 @@ def collectImgWithLabels(dataset, labelpostfix, volumepostfix):
6175
imageInfoList.append((volumeInfo,segmentationInfo)) #append a tuple (volume, segmentation)
6276
else:
6377
print("no segmentation or foreground found for: ", file)
64-
78+
print "datsets found", cnt_datasets, "invalid datasets found", cnt_invalid, "valid datasets found", cnt_datasets-cnt_invalid
79+
print size_dict
6580
return imageInfoList
6681

6782

@@ -76,14 +91,14 @@ def callculateStatistics(imageInfoList):
7691
for _ ,segmentationInfo in imageInfoList:
7792
size[label_cnt,:] = segmentationInfo.size
7893
spacing[label_cnt, :] = segmentationInfo.spacing
79-
labelbb[label_cnt, :] = segmentationInfo.labelBB[3:]
94+
#labelbb[label_cnt, :] = segmentationInfo.labelBB[3:]
8095
labelMax[label_cnt] = segmentationInfo.minMax[1]
8196
label_cnt += 1
8297

8398
medianSpacing = np.around(np.median(spacing,0),2)
8499
meanSpacing = np.around(np.mean(spacing,0),2)
85100
print("Volume size: max -> x,y,z: ", size.max(0), " min -> x,y,z: ", size.min(0), " avg -> x,y,z: ", size.mean(0), " median -> x,y,z: ", np.median(size,0))
86-
print("BoundingBox size: max -> x,y,z: ", labelbb.max(0), " min -> x,y,z: ", labelbb.min(0), " avg -> x,y,z: ", labelbb.mean(0), " median -> x,y,z: ", np.median(labelbb,0))
101+
#print("BoundingBox size: max -> x,y,z: ", labelbb.max(0), " min -> x,y,z: ", labelbb.min(0), " avg -> x,y,z: ", labelbb.mean(0), " median -> x,y,z: ", np.median(labelbb,0))
87102
print("Volume spacing: max -> x,y,z: ", spacing.max(0), " min -> x,y,z: ", spacing.min(0), " avg -> x,y,z: ", meanSpacing, " medain -> x,y,z: ", medianSpacing)
88103

89104
print("\n")

scripts/combine_segmentations.py

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
import os
2+
import argparse
3+
4+
import numpy as np
5+
import SimpleITK as sitk
6+
import defs
7+
8+
RES_DIR = "../fine_stage/paper/{}/output/50000_{}/orig_size/"
9+
10+
def getSegmentationsFromKey(key, confLst, dataset):
11+
segmentations = []
12+
for conf in confLst:
13+
fstConfDir = RES_DIR.format(confLst[0], dataset)
14+
for file in os.listdir(fstConfDir):
15+
if key in file:
16+
path = os.path.join(fstConfDir, file)
17+
segmentations.append(sitk.ReadImage(path, sitk.sitkUInt8))
18+
break
19+
20+
assert len(confLst) == len(segmentations), "not all segfiles found for " + key
21+
return segmentations
22+
23+
24+
def majorityVote(segmentations):
25+
labelForUndecidedPixels = 0
26+
return sitk.LabelVoting(segmentations, labelForUndecidedPixels)
27+
28+
29+
def staple(segmentations):
30+
threshold = 0.8
31+
32+
result_np = sitk.GetArrayFromImage(segmentations[0]).copy()
33+
result_np[result_np != 0] = 0
34+
35+
for key, value in defs.LABELS.iteritems():
36+
37+
singleOrganSeg = []
38+
for segmentation in segmentations:
39+
segmentation_np = sitk.GetArrayFromImage(segmentation).copy()
40+
segmentation_np[segmentation_np != value] = 0
41+
segmentation_np[segmentation_np == value] = 1
42+
singleOrganSeg.append(npToOrigITK(segmentation_np, segmentation))
43+
44+
45+
reference_segmentation_STAPLE_probabilities = sitk.STAPLE(singleOrganSeg, value)
46+
reference_segmentation_STAPLE = reference_segmentation_STAPLE_probabilities > threshold
47+
STAPLE_np = sitk.GetArrayFromImage(reference_segmentation_STAPLE)
48+
result_np[STAPLE_np == 1] = value
49+
50+
return npToOrigITK(result_np, segmentations[0])
51+
52+
53+
def combineSegmentations(configFile, resultpath, dataset):
54+
55+
if not os.path.exists(resultpath):
56+
os.makedirs(resultpath)
57+
58+
confLst = []
59+
with open(configFile, "r") as fptr:
60+
for config in fptr:
61+
if len(config) == 0 or config[0] == '#':
62+
continue
63+
confLst.append(config.rstrip().replace('.ini', ''))
64+
65+
fstConfDir = RES_DIR.format(confLst[0], dataset)
66+
for file in os.listdir(fstConfDir):
67+
print file
68+
key = file[:9]
69+
segmentations = getSegmentationsFromKey(key, confLst, dataset)
70+
71+
result = staple(segmentations)
72+
#result = majorityVote(segmentations)
73+
74+
outfile = os.path.join(resultpath, key+"_out.nii.gz")
75+
sitk.WriteImage(result, outfile)
76+
77+
78+
def npToOrigITK(np_image, orig_itk_img):
79+
new_itk = sitk.GetImageFromArray(np_image)
80+
new_itk.SetOrigin(orig_itk_img.GetOrigin())
81+
new_itk.SetDirection(orig_itk_img.GetDirection())
82+
new_itk.SetSpacing(orig_itk_img.GetSpacing())
83+
84+
cast_img_filter = sitk.CastImageFilter()
85+
cast_img_filter.SetOutputPixelType(sitk.sitkUInt8)
86+
new_itk = cast_img_filter.Execute(new_itk)
87+
88+
return new_itk
89+
90+
91+
if __name__ == "__main__":
92+
parser = argparse.ArgumentParser(prog='')
93+
94+
parser.add_argument('--configfile',
95+
required=True,
96+
help="file containing the models to be consider for the majority voting"
97+
)
98+
parser.add_argument('--resdir',
99+
required=True,
100+
help="directory where the results are written to"
101+
)
102+
parser.add_argument('--dataset',
103+
choices=['train', 'test'],
104+
default='test'
105+
)
106+
107+
args = parser.parse_args()
108+
combineSegmentations(args.configfile, args.resdir, args.dataset)

scripts/create_final_result_graph.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ def createbarplot(csvfile):
8080
fig.savefig("../results/result_figures/95hd_barplot_ead.eps", dpi=150, bbox_inches='tight')
8181

8282

83-
84-
8583
if __name__ == "__main__":
8684
parser = argparse.ArgumentParser(prog='')
8785

scripts/defs.py

Lines changed: 51 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
DATASET_SIZE_MAP = {'0522c0070': ((540, 540, 128), (1.1, 1.1, 3.0)), '0522c0017': ((456, 456, 130), (1.1, 1.1, 3.0)),
2-
'0522c0014': ((470, 470, 132), (1.1, 1.1, 3.0)), '0522c0248': ((456, 456, 147), (1.1, 1.1, 3.0)),
3-
'0522c0013': ((545, 545, 138), (1.1, 1.1, 3.0)), '0522c0159': ((438, 438, 125), (1.1, 1.1, 3.0)),
4-
'0522c0132': ((545, 545, 115), (1.1, 1.1, 3.0)), '0522c0081': ((438, 438, 133), (1.1, 1.1, 3.0)),
5-
'0522c0079': ((545, 545, 130), (1.1, 1.1, 3.0)), '0522c0057': ((456, 456, 121), (1.1, 1.1, 3.0)),
1+
DATASET_SIZE_MAP_UNISO = {'0522c0070': ((540, 540, 128), (1.1, 1.1, 3.0)), '0522c0017': ((456, 456, 130), (1.1, 1.1, 3.0)),
2+
'0522c0014': ((470, 470, 132), (1.1, 1.1, 3.0)), '0522c0248': ((456, 456, 147), (1.1, 1.1, 3.0)),
3+
'0522c0013': ((545, 545, 138), (1.1, 1.1, 3.0)), '0522c0159': ((438, 438, 125), (1.1, 1.1, 3.0)),
4+
'0522c0132': ((545, 545, 115), (1.1, 1.1, 3.0)), '0522c0081': ((438, 438, 133), (1.1, 1.1, 3.0)),
5+
'0522c0079': ((545, 545, 130), (1.1, 1.1, 3.0)), '0522c0057': ((456, 456, 121), (1.1, 1.1, 3.0)),
66
'0522c0147': ((354, 354, 113), (1.1, 1.1, 3.0)), '0522c0328': ((456, 456, 194), (1.1, 1.1, 3.0)),
77
'0522c0226': ((456, 456, 135), (1.1, 1.1, 3.0)), '0522c0125': ((410, 410, 76), (1.1, 1.1, 3.0)),
88
'0522c0001': ((521, 521, 107), (1.1, 1.1, 3.0)), '0522c0003': ((517, 517, 134), (1.1, 1.1, 3.0)),
@@ -19,12 +19,52 @@
1919
'0522c0806': ((456, 456, 124), (1.1, 1.1, 3.0)), '0522c0845': ((591, 591, 139), (1.1, 1.1, 3.0)),
2020
'0522c0857': ((545, 545, 151), (1.1, 1.1, 3.0))}
2121

22+
DATASET_SIZE_MAP = {'0522c0878': ((545, 545, 360), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
23+
'0522c0661': ((456, 456, 412), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
24+
'0522c0014': ((470, 470, 361), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
25+
'0522c0248': ((456, 456, 400), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
26+
'0522c0013': ((545, 545, 376), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
27+
'0522c0147': ((354, 354, 309), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
28+
'0522c0017': ((456, 456, 355), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
29+
'0522c0669': ((517, 517, 311), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
30+
'0522c0079': ((545, 545, 355), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
31+
'0522c0057': ((456, 456, 330), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
32+
'0522c0857': ((545, 545, 412), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
33+
'0522c0132': ((545, 545, 314), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
34+
'0522c0328': ((456, 456, 529), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
35+
'0522c0667': ((521, 521, 332), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
36+
'0522c0002': ((503, 503, 355), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
37+
'0522c0081': ((438, 438, 364), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
38+
'0522c0659': ((549, 549, 375), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
39+
'0522c0555': ((456, 456, 478), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
40+
'0522c0226': ((456, 456, 368), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
41+
'0522c0788': ((545, 545, 357), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
42+
'0522c0746': ((456, 456, 297), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
43+
'0522c0708': ((568, 568, 361), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
44+
'0522c0125': ((410, 410, 207), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
45+
'0522c0001': ((521, 521, 292), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
46+
'0522c0003': ((517, 517, 365), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
47+
'0522c0727': ((545, 545, 357), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
48+
'0522c0149': ((419, 419, 352), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
49+
'0522c0161': ((438, 438, 309), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
50+
'0522c0009': ((535, 535, 393), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
51+
'0522c0077': ((545, 545, 355), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
52+
'0522c0845': ((591, 591, 380), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
53+
'0522c0159': ((438, 438, 341), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
54+
'0522c0598': ((545, 545, 368), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
55+
'0522c0190': ((545, 545, 379), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
56+
'0522c0251': ((456, 456, 270), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
57+
'0522c0195': ((545, 545, 355), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
58+
'0522c0253': ((438, 438, 364), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
59+
'0522c0070': ((540, 540, 349), (1.100000023841858, 1.100000023841858, 1.100000023841858)),
60+
'0522c0806': ((456, 456, 338), (1.100000023841858, 1.100000023841858, 1.100000023841858))}
61+
2262
ORIG_SIZE_MAP = {'0522c0746': ((512, 512, 109), (0.9800000190734862, 0.9800000190734862, 3.0)),
23-
'0522c0441': ((512, 512, 360), (1.1699999570846558, 1.1699999570846558, 1.25)),
24-
'0522c0708': ((512, 512, 159), (1.2200000286102295, 1.2200000286102295, 2.500629186630249)),
25-
'0522c0667': ((512, 512, 146), (1.1200000047683716, 1.1200000047683716, 2.4999997615814205)),
26-
'0522c0149': ((512, 512, 129), (0.899999976158142, 0.899999976158142, 3.0)),
27-
'0522c0857': ((512, 512, 151), (1.1699999570846558, 1.1699999570846558, 3.0)),
63+
'0522c0441': ((512, 512, 360), (1.1699999570846558, 1.1699999570846558, 1.25)),
64+
'0522c0708': ((512, 512, 159), (1.2200000286102295, 1.2200000286102295, 2.500629186630249)),
65+
'0522c0667': ((512, 512, 146), (1.1200000047683716, 1.1200000047683716, 2.4999997615814205)),
66+
'0522c0149': ((512, 512, 129), (0.899999976158142, 0.899999976158142, 3.0)),
67+
'0522c0857': ((512, 512, 151), (1.1699999570846558, 1.1699999570846558, 3.0)),
2868
'0522c0253': ((512, 512, 160), (0.9399999976158141, 0.9399999976158141, 2.5)),
2969
'0522c0226': ((512, 512, 162), (0.9800000190734862, 0.9800000190734862, 2.5)),
3070
'0522c0457': ((512, 512, 181), (1.25, 1.25, 2.5)),
@@ -92,4 +132,4 @@
92132
}
93133

94134
DATASET_SCALE_SUBDIR = "upscaled"
95-
ORIG_SCALE_SUBDIR = "orig_size"
135+
ORIG_SCALE_SUBDIR = "orig_size"

0 commit comments

Comments
 (0)