Skip to content

Commit

Permalink
Configurable resampling interpolator (#55)
Browse files Browse the repository at this point in the history
Allow setting the resampling interpolator for preprocessing.

Dependency updates:
*
[`requirements_dev.txt`](diffhunk://#diff-c9c796259f3852b51b531b79cbf07820145088d4f108fb006ab44b15f0b15934L1-R7):
Updated versions of `flake8`, `tox`, `pytest`, `pytest-cov`, `mypy`,
`build`, and `twine`.
*
[`setup.py`](diffhunk://#diff-60f61ab7a8d1910d86d9fda2261620314edcae5894d5aaa236b821c7256badd7L8-R8):
Incremented the version number from `2.1.8` to `2.1.9`.

Interpolation settings:
*
[`src/picai_prep/preprocessing.py`](diffhunk://#diff-f6c4c6e211631a6dba78bcca09d032a3147c3d8b4949bdca958e80dee0b93833R39-R48):
Added `scan_interpolator` and `lbl_interpolator` attributes to the
`PreprocessingSettings` class.
*
[`src/picai_prep/preprocessing.py`](diffhunk://#diff-f6c4c6e211631a6dba78bcca09d032a3147c3d8b4949bdca958e80dee0b93833R80):
Modified the `resample_img` function to accept an `interpolation`
parameter and use it if provided.
[[1]](diffhunk://#diff-f6c4c6e211631a6dba78bcca09d032a3147c3d8b4949bdca958e80dee0b93833R80)
[[2]](diffhunk://#diff-f6c4c6e211631a6dba78bcca09d032a3147c3d8b4949bdca958e80dee0b93833L110-R117)
*
[`src/picai_prep/preprocessing.py`](diffhunk://#diff-f6c4c6e211631a6dba78bcca09d032a3147c3d8b4949bdca958e80dee0b93833L257-R270):
Updated the `resample_to_first_scan` and `resample_spacing` methods to
use the new interpolation settings from `PreprocessingSettings`.
[[1]](diffhunk://#diff-f6c4c6e211631a6dba78bcca09d032a3147c3d8b4949bdca958e80dee0b93833L257-R270)
[[2]](diffhunk://#diff-f6c4c6e211631a6dba78bcca09d032a3147c3d8b4949bdca958e80dee0b93833L275-R288)
  • Loading branch information
joeranbosma authored Nov 27, 2024
1 parent d3e1036 commit 9544f73
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
14 changes: 7 additions & 7 deletions requirements_dev.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
flake8==3.9.2
tox==3.24.3
pytest==6.2.5
pytest-cov==2.12.1
mypy===0.910
build
twine
flake8==7.1.1
tox==4.18.1
pytest==8.3.2
pytest-cov==5.0.0
mypy===1.11.2
build==1.2.2
twine==5.1.1
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.8',
version='2.1.9',
author_email='[email protected]',
long_description=long_description,
long_description_content_type="text/markdown",
Expand Down
17 changes: 12 additions & 5 deletions src/picai_prep/preprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,16 @@ class PreprocessingSettings():
- physical_size: size in mm of the target image (z, y, x)
- crop_only: only crop to specified size (i.e., do not pad)
- align_segmentation: whether to align the scans using the centroid of the provided segmentation
- scan_interpolator: interpolation method for scans
- lbl_interpolator: interpolation method for labels
"""
matrix_size: Optional[Iterable[int]] = None
spacing: Optional[Iterable[float]] = None
physical_size: Optional[Iterable[float]] = None
crop_only: bool = False
align_segmentation: Optional[sitk.Image] = None
scan_interpolator: int = sitk.sitkBSpline
lbl_interpolator: int = sitk.sitkNearestNeighbor

def __post_init__(self):
if self.physical_size is None and self.spacing is not None and self.matrix_size is not None:
Expand Down Expand Up @@ -73,6 +77,7 @@ def resample_img(
out_spacing: Iterable[float] = (2.0, 2.0, 2.0),
out_size: Optional[Iterable[int]] = None,
is_label: bool = False,
interpolation = None,
pad_value: Optional[Union[float, int]] = 0.,
) -> sitk.Image:
"""
Expand Down Expand Up @@ -107,7 +112,9 @@ def resample_img(
resample.SetOutputOrigin(image.GetOrigin())
resample.SetTransform(sitk.Transform())
resample.SetDefaultPixelValue(pad_value)
if is_label:
if interpolation is not None:
resample.SetInterpolator(interpolation)
elif is_label:
resample.SetInterpolator(sitk.sitkNearestNeighbor)
else:
resample.SetInterpolator(sitk.sitkBSpline)
Expand Down Expand Up @@ -254,13 +261,13 @@ def resample_to_first_scan(self):
# set up resampler to resolution, field of view, etc. of first scan
resampler = sitk.ResampleImageFilter() # default linear
resampler.SetReferenceImage(self.scans[0])
resampler.SetInterpolator(sitk.sitkBSpline)
resampler.SetInterpolator(self.settings.scan_interpolator)

# resample other images
self.scans[1:] = [resampler.Execute(scan) for scan in self.scans[1:]]

# resample annotation
resampler.SetInterpolator(sitk.sitkNearestNeighbor)
resampler.SetInterpolator(self.settings.lbl_interpolator)
if self.lbl is not None:
self.lbl = resampler.Execute(self.lbl)

Expand All @@ -272,13 +279,13 @@ def resample_spacing(self, spacing: Optional[Iterable[float]] = None):

# resample scans to target resolution
self.scans = [
resample_img(scan, out_spacing=spacing, is_label=False)
resample_img(scan, out_spacing=spacing, interpolation=self.settings.scan_interpolator)
for scan in self.scans
]

# resample annotation to target resolution
if self.lbl is not None:
self.lbl = resample_img(self.lbl, out_spacing=spacing, is_label=True)
self.lbl = resample_img(self.lbl, out_spacing=spacing, interpolation=self.settings.lbl_interpolator)

def centre_crop_or_pad(self):
"""Centre crop and/or pad scans and label"""
Expand Down

0 comments on commit 9544f73

Please sign in to comment.