Skip to content

Commit 323c9d6

Browse files
authored
Merge pull request ellisdg#150 from ellisdg/update_to_brats18
Update modalities for brats18
2 parents ff5953b + 4621a05 commit 323c9d6

File tree

5 files changed

+87
-7
lines changed

5 files changed

+87
-7
lines changed

brats/preprocess.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,17 @@ def normalize_image(in_file, out_file, bias_correction=True):
116116
return out_file
117117

118118

119-
def convert_brats_folder(in_folder, out_folder, truth_name="GlistrBoost_ManuallyCorrected",
120-
no_bias_correction_modalities=None):
119+
def convert_brats_folder(in_folder, out_folder, truth_name='seg', no_bias_correction_modalities=None):
121120
for name in config["all_modalities"]:
122-
image_file = get_image(in_folder, name)
121+
try:
122+
image_file = get_image(in_folder, name)
123+
except RuntimeError as error:
124+
if name == 't1ce':
125+
image_file = get_image(in_folder, 't1Gd')
126+
truth_name = "GlistrBoost_ManuallyCorrected"
127+
else:
128+
raise error
129+
123130
out_file = os.path.abspath(os.path.join(out_folder, name + ".nii.gz"))
124131
perform_bias_correction = no_bias_correction_modalities and name not in no_bias_correction_modalities
125132
normalize_image(image_file, out_file, bias_correction=perform_bias_correction)
@@ -128,6 +135,7 @@ def convert_brats_folder(in_folder, out_folder, truth_name="GlistrBoost_Manually
128135
truth_file = get_image(in_folder, truth_name)
129136
except RuntimeError:
130137
truth_file = get_image(in_folder, truth_name.split("_")[0])
138+
131139
out_file = os.path.abspath(os.path.join(out_folder, "truth.nii.gz"))
132140
shutil.copy(truth_file, out_file)
133141
check_origin(out_file, get_image(in_folder, config["all_modalities"][0]))

brats/train.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
config["patch_shape"] = (64, 64, 64) # switch to None to train on the whole image
1414
config["labels"] = (1, 2, 4) # the label numbers on the input image
1515
config["n_labels"] = len(config["labels"])
16-
config["all_modalities"] = ["t1", "t1Gd", "flair", "t2"]
16+
config["all_modalities"] = ["t1", "t1ce", "flair", "t2"]
1717
config["training_modalities"] = config["all_modalities"] # change this if you want to only use some of the modalities
1818
config["nb_channels"] = len(config["training_modalities"])
1919
if "patch_shape" in config and config["patch_shape"] is not None:

brats/train_isensee2017.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
config["labels"] = (1, 2, 4) # the label numbers on the input image
1414
config["n_base_filters"] = 16
1515
config["n_labels"] = len(config["labels"])
16-
config["all_modalities"] = ["t1", "t1Gd", "flair", "t2"]
16+
config["all_modalities"] = ["t1", "t1ce", "flair", "t2"]
1717
config["training_modalities"] = config["all_modalities"] # change this if you want to only use some of the modalities
1818
config["nb_channels"] = len(config["training_modalities"])
1919
if "patch_shape" in config and config["patch_shape"] is not None:

test/test_brats.py

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
from unittest import TestCase
2+
import os
3+
import shutil
4+
import sys
5+
import nibabel as nib
6+
import numpy as np
7+
8+
9+
def create_blank_image(filename, image_shape=(140, 140, 140)):
10+
data = np.zeros(image_shape, dtype=np.int16)
11+
affine = np.diag(np.ones(4))
12+
image = nib.Nifti1Image(dataobj=data, affine=affine)
13+
image.to_filename(filename)
14+
15+
16+
class TestPreprocess(TestCase):
17+
def setUp(self):
18+
self.temp_brats_dir = os.path.abspath("temp_brats")
19+
os.makedirs(self.temp_brats_dir)
20+
sys.path.append('../brats')
21+
self.preprocessed_dir = os.path.abspath("temp_preprocessed_brats")
22+
23+
def tearDown(self):
24+
for directory in (self.temp_brats_dir, self.preprocessed_dir):
25+
if os.path.exists(directory):
26+
shutil.rmtree(directory)
27+
28+
def create_replica_dataset(self, subject_ids, scan_types, directory_name):
29+
gbm_dir = os.path.join(self.temp_brats_dir, directory_name)
30+
for subject_id in subject_ids:
31+
subject_dir = os.path.join(gbm_dir, subject_id)
32+
os.makedirs(subject_dir)
33+
for scan_label in scan_types:
34+
basename = '{}_{}.nii.gz'.format(subject_id, scan_label)
35+
scan_filename = os.path.join(subject_dir, basename)
36+
create_blank_image(scan_filename)
37+
38+
def create_replica_dataset_pre2018(self):
39+
self.create_replica_dataset(subject_ids=('TCGA-00-000',),
40+
scan_types=('flair',
41+
'GlistrBoost',
42+
'GlistrBoost_ManuallyCorrected',
43+
't1',
44+
't1Gd',
45+
't2'),
46+
directory_name='Pre-operative_TCGA_GBM_NIfTI_and_Segmentations')
47+
self.create_replica_dataset(subject_ids=('TCGA-01-000',),
48+
scan_types=('flair',
49+
'GlistrBoost',
50+
't1',
51+
't1Gd',
52+
't2'),
53+
directory_name='Pre-operative_TCGA_GBM_NIfTI_and_Segmentations')
54+
55+
def create_replica_dataset_2018(self):
56+
self.create_replica_dataset(subject_ids=('Brats18_1900_1_1',),
57+
scan_types=('flair',
58+
't1',
59+
't1ce',
60+
't2',
61+
'seg'),
62+
directory_name='HGG')
63+
64+
def test_preprocess_pre2018(self):
65+
from preprocess import convert_brats_data
66+
self.create_replica_dataset_pre2018()
67+
convert_brats_data(self.temp_brats_dir, self.preprocessed_dir)
68+
69+
def test_preprocess_2018(self):
70+
self.create_replica_dataset_2018()
71+
from preprocess import convert_brats_data
72+
convert_brats_data(self.temp_brats_dir, self.preprocessed_dir)
73+

test/test_generator.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,7 @@ def test_generator_with_permutations(self):
188188
permute=True)
189189
training_generator, validation_generator, n_training_steps, n_validation_steps = generators
190190

191-
for x in training_generator:
192-
break
191+
_ = next(training_generator)
193192

194193
self.rm_tmp_files()
195194

0 commit comments

Comments
 (0)