Skip to content

Commit 73f3db1

Browse files
committed
enh: reduce uncovered lines
1 parent e550a7d commit 73f3db1

File tree

4 files changed

+45
-11
lines changed

4 files changed

+45
-11
lines changed

nitransforms/linear.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ def __eq__(self, other):
120120
>>> xfm2 = Affine(xfm1.matrix)
121121
>>> xfm1 == xfm2
122122
True
123+
>>> xfm1 == Affine()
124+
False
125+
>>> xfm1 == TransformBase()
126+
False
123127
124128
"""
125129
if not hasattr(other, "matrix"):

nitransforms/manip.py

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@
1919
)
2020
from nitransforms.io import itk, x5 as x5io
2121
from nitransforms.io.x5 import from_filename as load_x5
22-
from nitransforms.linear import (
22+
from nitransforms.linear import ( # noqa: F401
2323
Affine,
24-
from_x5 as linear_from_x5, # noqa: F401
24+
from_x5 as linear_from_x5,
2525
)
26-
from nitransforms.nonlinear import (
26+
from nitransforms.nonlinear import ( # noqa: F401
2727
DenseFieldTransform,
28-
from_x5 as nonlinear_from_x5, # noqa: F401
28+
from_x5 as nonlinear_from_x5,
2929
)
3030

3131

@@ -224,8 +224,9 @@ def from_filename(cls, filename, fmt="X5", reference=None, moving=None, x5_chain
224224
raise TransformError("X5 file contains no TransformChain")
225225

226226
chain_path = chain_grp[str(x5_chain)][()]
227-
if isinstance(chain_path, bytes):
228-
chain_path = chain_path.decode()
227+
chain_path = (
228+
chain_path.decode() if isinstance(chain_path, bytes) else chain_path
229+
)
229230

230231
return TransformChain([xfm_list[int(idx)] for idx in chain_path.split("/")])
231232

nitransforms/nonlinear.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,13 @@ def __eq__(self, other):
245245
>>> xfm2 = DenseFieldTransform(test_dir / "someones_displacement_field.nii.gz")
246246
>>> xfm1 == xfm2
247247
True
248+
>>> xfm1 == TransformBase()
249+
False
250+
>>> xfm1 == BSplineFieldTransform(test_dir / "someones_bspline_coefficients.nii.gz")
251+
False
248252
249253
"""
250-
if not hasattr(other, "_field"):
254+
if not hasattr(other, "_field") or self._field.shape != other._field.shape:
251255
return False
252256

253257
_eq = np.allclose(self._field, other._field)
@@ -335,13 +339,26 @@ def __eq__(self, other):
335339
>>> xfm2 = BSplineFieldTransform(test_dir / "someones_bspline_coefficients.nii.gz")
336340
>>> xfm1 == xfm2
337341
True
342+
>>> xfm2._coeffs[:, :, :] = 0 # Let's zero all coefficients
343+
>>> xfm1 == xfm2
344+
False
345+
>>> xfm2 = BSplineFieldTransform(
346+
... test_dir / "someones_bspline_coefficients.nii.gz",
347+
... order=4,
348+
... )
349+
>>> xfm1 == xfm2
350+
False
351+
>>> xfm1 == TransformBase()
352+
False
353+
>>> xfm1 == DenseFieldTransform(test_dir / "someones_displacement_field.nii.gz")
354+
False
338355
339356
"""
340-
if not hasattr(other, "_coeffs"):
357+
if not hasattr(other, "_coeffs") or self._coeffs.shape != other._coeffs.shape:
341358
return False
342359

343-
_eq = np.allclose(self._coeffs, other._coeffs)
344-
_eq = _eq and self._order == other._order
360+
_eq = self._order == other._order
361+
_eq = _eq and np.allclose(self._coeffs, other._coeffs)
345362

346363
if _eq and self._reference != other._reference:
347364
warnings.warn("Coefficients are equal, but references do not match.")

nitransforms/tests/test_manip.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@
77
import numpy as np
88
import nibabel as nb
99
import h5py
10+
from ..base import TransformError
1011
from ..manip import TransformChain
1112
from ..linear import Affine
1213
from ..nonlinear import DenseFieldTransform
14+
from ..io import x5
1315

1416
FMT = {"lta": "fs", "tfm": "itk"}
1517

@@ -45,18 +47,28 @@ def test_collapse_affines(tmp_path, data_path, ext0, ext1, ext2):
4547
def test_transformchain_x5_roundtrip(tmp_path):
4648
"""Round-trip TransformChain with X5 storage."""
4749

50+
# Test empty transform file
51+
x5.to_filename(tmp_path / "empty.x5", [])
52+
with pytest.raises(TransformError):
53+
TransformChain.from_filename(tmp_path / "empty.x5")
54+
4855
mat = np.eye(4)
4956
mat[0, 3] = 1
5057
aff = Affine(mat)
5158

59+
# Test loading X5 with no transforms chains
60+
x5.to_filename(tmp_path / "nochain.x5", [aff.to_x5()])
61+
with pytest.raises(TransformError):
62+
TransformChain.from_filename(tmp_path / "nochain.x5")
63+
5264
field = nb.Nifti1Image(np.zeros((5, 5, 5, 3), dtype="float32"), np.eye(4))
5365
fdata = field.get_fdata()
5466
fdata[..., 1] = 1
5567
field = nb.Nifti1Image(fdata, np.eye(4))
5668
dfield = DenseFieldTransform(field, is_deltas=True)
5769

70+
# Create a chain
5871
chain = TransformChain([aff, aff, aff, dfield])
59-
6072
fname = tmp_path / "chain.x5"
6173
chain.to_filename(fname)
6274

0 commit comments

Comments
 (0)