Skip to content
This repository has been archived by the owner on Dec 20, 2024. It is now read-only.

Commit

Permalink
enh: add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
oesteban committed Jun 17, 2024
1 parent 2891940 commit ef203f5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 21 deletions.
13 changes: 4 additions & 9 deletions src/eddymotion/data/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,9 +196,9 @@ def downsample(
smooth = datashape[:3] / shape[:3]
data = gaussian_filter(data, smooth)

extents = (
apply_affine(imnii.affine, datashape - 0.5)
- apply_affine(imnii.affine, (-0.5, -0.5, -0.5))
extents = np.abs(
apply_affine(imnii.affine, datashape - 1)
- apply_affine(imnii.affine, (0.0, 0.0, 0.0))
)
newzooms = extents / shape

Expand Down Expand Up @@ -231,8 +231,7 @@ def downsample(
data,
locations.T,
order=order,
mode="constant",
cval=0,
mode="mirror",
prefilter=True,
).reshape(shape)

Expand All @@ -252,7 +251,6 @@ def decimate(
in_file: str,
factor: int | tuple[int, int, int],
smooth: bool | tuple[int, int, int] = True,
order: int = 3,
nonnegative: bool = True,
) -> Nifti1Image:
"""
Expand All @@ -278,9 +276,6 @@ def decimate(
Alternatively, a tuple of three integers can be provided to specify
different smoothing kernel sizes for each spatial dimension. Setting to
False disables smoothing.
order : :obj:`int`, optional (default=3)
The order of the spline interpolation used for downsampling. Higher
orders provide smoother results but are computationally more expensive.
nonnegative : :obj:`bool`, optional (default=``True``)
If True, negative values in the downsampled data are set to zero.
Expand Down
43 changes: 31 additions & 12 deletions test/test_filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,19 @@
)
@pytest.mark.parametrize(
("zoom_x", ),
# [(1.0, ), (-1.0, ), (2.0, ), (-2.0, )],
[(2.0,)],
[(1.0, ), (-1.0, ), (2.0, ), (-2.0, )],
)
@pytest.mark.parametrize(
("zoom_y", ),
# [(1.0, ), (-1.0, ), (2.0, ), (-2.0, )],
[(-2.0,)],
[(1.0, ), (-1.0, ), (2.0, ), (-2.0, )],
)
@pytest.mark.parametrize(
("zoom_z", ),
# [(1.0, ), (-1.0, ), (2.0, ), (-2.0, )],
[(-2.0,)],
[(1.0, ), (-1.0, ), (2.0, ), (-2.0, )],
)
@pytest.mark.parametrize(
("angle_x", ),
# [(0.0, ), (0.2, ), (-0.05, )],
[(-0.05,)]
[(0.0, ), (0.2, ), (-0.05, )],
)
@pytest.mark.parametrize(
("angle_y", ),
Expand Down Expand Up @@ -82,6 +78,7 @@ def test_decimation(
angle_y,
angle_z,
offsets,
outdir,
):
"""Exercise decimation."""

Expand Down Expand Up @@ -120,8 +117,30 @@ def test_decimation(
test_image.to_filename(fname)

# Need to define test oracle. For now, just see if it doesn't smoke.
out = decimate(fname, factor=2, smooth=False, order=1)
out.to_filename(tmp_path / "decimated.nii.gz")
out = decimate(fname, factor=2, smooth=False)

out = downsample(fname, shape=(10, 10, 10), smooth=False, order=0)

if outdir:
from niworkflows.interfaces.reportlets.registration import (
SimpleBeforeAfterRPT as SimpleBeforeAfter,
)

out.to_filename(tmp_path / "decimated.nii.gz")

SimpleBeforeAfter(
after_label="Decimated",
before_label="Original",
after=str(tmp_path / "decimated.nii.gz"),
before=str(fname),
out_report=str(outdir / f'decimated-{tmp_path.name}.svg'),
).run()

out = downsample(fname, shape=(10, 10, 10), smooth=False, order=1)
out.to_filename(tmp_path / "downsampled.nii.gz")
out.to_filename(tmp_path / "downsampled.nii.gz")
SimpleBeforeAfter(
after_label="Downsampled",
before_label="Original",
after=str(tmp_path / "downsampled.nii.gz"),
before=str(fname),
out_report=str(outdir / f'downsampled-{tmp_path.name}.svg'),
).run()

0 comments on commit ef203f5

Please sign in to comment.