Skip to content

Commit

Permalink
fix(all): Multiple minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
Elias committed Aug 8, 2021
1 parent 44c7c06 commit f621356
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 30 deletions.
13 changes: 4 additions & 9 deletions bin/hello_world.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,20 @@
Uses the main libraries to verify the environment installation.
"""

import sys

from datetime import datetime
import matplotlib.pyplot as plt
import numpy as np
# import pydensecrf.densecrf as crf
import pymia.evaluation.evaluator as pymia_eval
import SimpleITK as sitk
import sklearn.ensemble as sk_ensemble


def main():

print('Welcome to MIALab 2020!')
print(f'Welcome to MIALab {datetime.now().year}!')

# --- scikit-learn
clf = sk_ensemble.RandomForestClassifier(max_depth=2, random_state=0)
sk_ensemble.RandomForestClassifier(max_depth=2, random_state=0)

# --- SimpleITK
image = sitk.Image(256, 128, 64, sitk.sitkInt16)
Expand All @@ -36,11 +34,8 @@ def main():

plt.show()

# --- pydensecrf
# d = crf.DenseCRF(1000, 2)

# --- pymia
eval_ = pymia_eval.SegmentationEvaluator([], {})
pymia_eval.SegmentationEvaluator([], {})

print('Everything seems to work fine!')

Expand Down
15 changes: 8 additions & 7 deletions bin/prepare_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,12 @@ def join_and_check_path(file_id, file_names):
return files

subject_files = {}
subdirs = glob.glob(data_dir + '/*')
for subdir in subdirs:
if not os.path.isdir(subdir):
sub_dirs = glob.glob(data_dir + '/*')
for sub_dir in sub_dirs:
if not os.path.isdir(sub_dir):
continue

id_ = os.path.basename(subdir)
id_ = os.path.basename(sub_dir)

image_files = join_and_check_path(id_, image_names)
label_files = join_and_check_path(id_, label_names)
Expand All @@ -138,14 +138,14 @@ def split_dataset(train_split, subject_files):
return train_subject, test_subject


def transform_and_write(subject_files, image_tranform, label_transform, out_dir):
def transform_and_write(subject_files, image_transform, label_transform, out_dir):

for id_, subject_file in subject_files.items():
print(' - {}'.format(id_))

for in_image_file, out_image_file in subject_file['images']:
image = sitk.ReadImage(in_image_file, sitk.sitkUInt16)
transformed_image = image_tranform(image)
transformed_image = image_transform(image)
out_image_path = os.path.join(out_dir, out_image_file)
if not os.path.exists(os.path.dirname(out_image_path)):
os.makedirs(os.path.dirname(out_image_path))
Expand Down Expand Up @@ -225,7 +225,8 @@ def __call__(self, img: sitk.Image) -> sitk.Image:
merged_img = np.zeros_like(np_img)

for new_label, labels_to_merge in self.to_combine.items():
merged_img[np.in1d(np_img.ravel(), labels_to_merge, assume_unique=True).reshape(np_img.shape)] = new_label
indices = np.reshape(np.in1d(np_img.ravel(), labels_to_merge, assume_unique=True), np_img.shape)
merged_img[indices] = new_label

out_img = sitk.GetImageFromArray(merged_img)
out_img.CopyInformation(img)
Expand Down
4 changes: 2 additions & 2 deletions bin/toy_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ def get_test_data(grid_size: int):

rng = np.linspace(0, grid_size - 1, grid_size)
grid = np.meshgrid(rng, rng)
return np.append(grid[0].reshape(-1, 1), grid[1].reshape(-1, 1), axis=1).astype(np.float32, copy=False)
return np.append(np.reshape(grid[0], (-1, 1)),
np.reshape(grid[1], (-1, 1)), axis=1).astype(np.float32, copy=False)

@staticmethod
def get_test_data_with_label(grid_size: int):
Expand Down Expand Up @@ -240,7 +241,6 @@ def main(result_dir: str, input_file: str):

# apply the forest to test data
print('Decision forest testing...')
predictions = forest.predict(test_data)
probabilities = forest.predict_proba(test_data)

# let's have a look at the feature importance
Expand Down
22 changes: 11 additions & 11 deletions mialab/filtering/postprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ def __str__(self):
#
# Args:
# img_t1 (sitk.Image): The T1-weighted image.
# img_t2 (sitk.Image): The T2-weigthed image.
# img_proba (sitk.Image): The posterior probability image.
# img_t2 (sitk.Image): The T2-weighted image.
# img_probability (sitk.Image): The posterior probability image.
# """
# self.img_t1 = img_t1
# self.img_t2 = img_t2
# self.img_proba = img_proba
# self.img_probability = img_probability
#
#
# class DenseCRF(pymia_fltr.Filter):
Expand Down Expand Up @@ -86,25 +86,25 @@ def __str__(self):
#
# img_t2 = sitk.GetArrayFromImage(params.img_t1)
# img_ir = sitk.GetArrayFromImage(params.img_t2)
# img_proba = sitk.GetArrayFromImage(params.img_proba)
# img_probability = sitk.GetArrayFromImage(params.img_probability)
#
# # some variables
# x = img_proba.shape[2]
# y = img_proba.shape[1]
# z = img_proba.shape[0]
# no_labels = img_proba.shape[3]
# x = img_probability.shape[2]
# y = img_probability.shape[1]
# z = img_probability.shape[0]
# no_labels = img_probability.shape[3]
#
# img_proba = np.rollaxis(img_proba, 3, 0)
# img_probability = np.rollaxis(img_probability, 3, 0)
#
# d = crf.DenseCRF(x * y * z, no_labels) # width, height, nlabels
# U = crf_util.unary_from_softmax(img_proba)
# U = crf_util.unary_from_softmax(img_probability)
# d.setUnaryEnergy(U)
#
# stack = np.stack([img_t2, img_ir], axis=3)
#
# # Create the pairwise bilateral term from the above images.
# # The two `s{dims,chan}` parameters are model hyper-parameters defining
# # the strength of the location and image content bilaterals, respectively.
# # the strength of the location and image content bi-laterals, respectively.
#
# # higher weight equals stronger
# pairwise_energy = crf_util.create_pairwise_bilateral(sdims=(1, 1, 1), schan=(1, 1), img=stack, chdim=3)
Expand Down
2 changes: 1 addition & 1 deletion mialab/utilities/file_access_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ class FileSystemDataCrawler:
>>>
>>> class MyFilePathGenerator(FilePathGenerator):
>>> @staticmethod
>>> def get_full_file_path(id_: str, root_dir: str, file_key, file_extension: str) -> str:
>>> def get_full_file_path(_id: str, root_dir: str, file_key, file_extension: str) -> str:
>>> if file_key == MyImgType.T1:
>>> file_name = 'Image'
>>> elif file_key == MyImgType.GroundTruth:
Expand Down

0 comments on commit f621356

Please sign in to comment.