Skip to content

Commit 48184b5

Browse files
oestebanjhlegarreta
authored andcommitted
enh: add tests
1 parent fd89b21 commit 48184b5

File tree

2 files changed

+35
-21
lines changed

2 files changed

+35
-21
lines changed

src/nifreeze/data/filtering.py

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -196,9 +196,9 @@ def downsample(
196196
smooth = datashape[:3] / shape[:3]
197197
data = gaussian_filter(data, smooth)
198198

199-
extents = (
200-
apply_affine(imnii.affine, datashape - 0.5)
201-
- apply_affine(imnii.affine, (-0.5, -0.5, -0.5))
199+
extents = np.abs(
200+
apply_affine(imnii.affine, datashape - 1)
201+
- apply_affine(imnii.affine, (0.0, 0.0, 0.0))
202202
)
203203
newzooms = extents / shape
204204

@@ -231,8 +231,7 @@ def downsample(
231231
data,
232232
locations.T,
233233
order=order,
234-
mode="constant",
235-
cval=0,
234+
mode="mirror",
236235
prefilter=True,
237236
).reshape(shape)
238237

@@ -252,7 +251,6 @@ def decimate(
252251
in_file: str,
253252
factor: int | tuple[int, int, int],
254253
smooth: bool | tuple[int, int, int] = True,
255-
order: int = 3,
256254
nonnegative: bool = True,
257255
) -> Nifti1Image:
258256
"""
@@ -278,9 +276,6 @@ def decimate(
278276
Alternatively, a tuple of three integers can be provided to specify
279277
different smoothing kernel sizes for each spatial dimension. Setting to
280278
False disables smoothing.
281-
order : :obj:`int`, optional (default=3)
282-
The order of the spline interpolation used for downsampling. Higher
283-
orders provide smoother results but are computationally more expensive.
284279
nonnegative : :obj:`bool`, optional (default=``True``)
285280
If True, negative values in the downsampled data are set to zero.
286281

test/test_filtering.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,23 +38,19 @@
3838
)
3939
@pytest.mark.parametrize(
4040
("zoom_x", ),
41-
# [(1.0, ), (-1.0, ), (2.0, ), (-2.0, )],
42-
[(2.0,)],
41+
[(1.0, ), (-1.0, ), (2.0, ), (-2.0, )],
4342
)
4443
@pytest.mark.parametrize(
4544
("zoom_y", ),
46-
# [(1.0, ), (-1.0, ), (2.0, ), (-2.0, )],
47-
[(-2.0,)],
45+
[(1.0, ), (-1.0, ), (2.0, ), (-2.0, )],
4846
)
4947
@pytest.mark.parametrize(
5048
("zoom_z", ),
51-
# [(1.0, ), (-1.0, ), (2.0, ), (-2.0, )],
52-
[(-2.0,)],
49+
[(1.0, ), (-1.0, ), (2.0, ), (-2.0, )],
5350
)
5451
@pytest.mark.parametrize(
5552
("angle_x", ),
56-
# [(0.0, ), (0.2, ), (-0.05, )],
57-
[(-0.05,)]
53+
[(0.0, ), (0.2, ), (-0.05, )],
5854
)
5955
@pytest.mark.parametrize(
6056
("angle_y", ),
@@ -82,6 +78,7 @@ def test_decimation(
8278
angle_y,
8379
angle_z,
8480
offsets,
81+
outdir,
8582
):
8683
"""Exercise decimation."""
8784

@@ -120,8 +117,30 @@ def test_decimation(
120117
test_image.to_filename(fname)
121118

122119
# Need to define test oracle. For now, just see if it doesn't smoke.
123-
out = decimate(fname, factor=2, smooth=False, order=1)
124-
out.to_filename(tmp_path / "decimated.nii.gz")
120+
out = decimate(fname, factor=2, smooth=False)
121+
122+
out = downsample(fname, shape=(10, 10, 10), smooth=False, order=0)
123+
124+
if outdir:
125+
from niworkflows.interfaces.reportlets.registration import (
126+
SimpleBeforeAfterRPT as SimpleBeforeAfter,
127+
)
128+
129+
out.to_filename(tmp_path / "decimated.nii.gz")
130+
131+
SimpleBeforeAfter(
132+
after_label="Decimated",
133+
before_label="Original",
134+
after=str(tmp_path / "decimated.nii.gz"),
135+
before=str(fname),
136+
out_report=str(outdir / f'decimated-{tmp_path.name}.svg'),
137+
).run()
125138

126-
out = downsample(fname, shape=(10, 10, 10), smooth=False, order=1)
127-
out.to_filename(tmp_path / "downsampled.nii.gz")
139+
out.to_filename(tmp_path / "downsampled.nii.gz")
140+
SimpleBeforeAfter(
141+
after_label="Downsampled",
142+
before_label="Original",
143+
after=str(tmp_path / "downsampled.nii.gz"),
144+
before=str(fname),
145+
out_report=str(outdir / f'downsampled-{tmp_path.name}.svg'),
146+
).run()

0 commit comments

Comments
 (0)