Skip to content

Commit

Permalink
Preprocessing: add option to only pad and never crop (#54)
Browse files Browse the repository at this point in the history
Preprocessing: add option to only pad and never crop
Update version to 2.1.8
  • Loading branch information
joeranbosma authored Sep 18, 2024
1 parent 704b7c1 commit d3e1036
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
long_description = fh.read()

setuptools.setup(
version='2.1.7',
version='2.1.8',
author_email='[email protected]',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
2 changes: 1 addition & 1 deletion src/picai_prep/dcm2mha.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,7 @@ def _read_image_pydicom(self, path: Optional[PathLike] = None) -> sitk.Image:
# collect all available metadata (with DICOM tags, e.g. 0010|1010, as keys)
value = self.get_pydicom_value(ref, key)
if value is not None:
key = str(key).replace(", ", "|").replace("(", "").replace(")", "")
key = str(key).replace(",", "|").replace("(", "").replace(")", "").replace(" ", "")
image.SetMetaData(key, value)

return image
Expand Down
4 changes: 4 additions & 0 deletions src/picai_prep/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def crop_or_pad(
size: Optional[Iterable[int]] = (20, 256, 256),
physical_size: Optional[Iterable[float]] = None,
crop_only: bool = False,
pad_only: bool = False,
) -> "Union[sitk.Image, npt.NDArray[Any]]":
"""
Resize image by cropping and/or padding
Expand Down Expand Up @@ -208,6 +209,9 @@ def crop_or_pad(
padding[i][0] = (size[i] - shape[i]) // 2
padding[i][1] = size[i] - shape[i] - padding[i][0]
else:
if pad_only:
continue

# create slicer object to crop image
idx_start = int(np.floor((shape[i] - size[i]) / 2.))
idx_end = idx_start + size[i]
Expand Down
16 changes: 10 additions & 6 deletions tests/test_dcm2mha.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def test_dcm2mha_commandline(
]

# run command
subprocess.check_call(cmd, shell=(sys.platform == 'win32'))
subprocess.check_call(cmd, shell=(sys.platform == "win32"))

# compare output
for patient_id, subject_id in [
Expand Down Expand Up @@ -148,7 +148,7 @@ def test_resolve_duplicates(

# check if duplicates were resolved
matched_series = [serie for serie in case.valid_series if "t2w" in serie.mappings]
assert len(matched_series) == 1, 'More than one serie after resolving duplicates!'
assert len(matched_series) == 1, "More than one serie after resolving duplicates!"


def test_value_match_contains(
Expand Down Expand Up @@ -185,7 +185,7 @@ def test_value_match_contains(

# check if duplicates were resolved
matched_series = [serie for serie in case.valid_series if "test" in serie.mappings]
assert len(matched_series) == 11, 'Empty lower_strip_contains should match all series!'
assert len(matched_series) == 11, "Empty lower_strip_contains should match all series!"


def test_value_match_multiple_keys(
Expand Down Expand Up @@ -236,7 +236,7 @@ def test_value_match_multiple_keys(

# check if duplicates were resolved
matched_series = [serie for serie in case.valid_series if "test" in serie.mappings]
assert len(matched_series) == 3, 'Should find three diffusion scans!'
assert len(matched_series) == 3, "Should find three diffusion scans!"


@pytest.mark.parametrize("input_dir", [
Expand Down Expand Up @@ -265,7 +265,9 @@ def test_image_reader(input_dir: str):
metadata_sitk = {key: image_sitk.GetMetaData(key) for key in image_sitk.GetMetaDataKeys()}
metadata_pydicom = {key: image_pydicom.GetMetaData(key) for key in image_pydicom.GetMetaDataKeys()}
keys = set(metadata_pydicom.keys()) & set(metadata_sitk.keys())
assert len(keys) > 1, 'No metadata found!'
assert len(metadata_sitk.keys()) > 1, "No metadata found with SimpleITK!"
assert len(metadata_pydicom.keys()) > 1, "No metadata found with pydicom!"
assert len(keys) > 1, f"No metadata found! SimpleITK: {list(metadata_sitk.keys())}, pydicom: {list(metadata_pydicom.keys())}"
assert {k: metadata_sitk[k] for k in keys} == {k: metadata_pydicom[k] for k in keys}


Expand Down Expand Up @@ -298,7 +300,9 @@ def test_image_reader_dicom_zip(input_dir: str):

# compare metadata
keys = set(metadata1.keys()) & set(metadata2.keys())
assert len(keys) > 1, 'No metadata found!'
assert len(metadata1.keys()) > 1, "No metadata found with first reader!"
assert len(metadata2.keys()) > 1, "No metadata found with second reader!"
assert len(keys) > 1, f"No metadata found! First reader: {list(metadata1.keys())}, second reader: {list(metadata2.keys())}"
assert {k: metadata1[k] for k in keys} == {k: metadata2[k] for k in keys}


Expand Down

0 comments on commit d3e1036

Please sign in to comment.