Skip to content

Commit 852b02d

Browse files
committed
fix: when adding tests to better cover the new code, a bug with FSL was discovered
1 parent f31e223 commit 852b02d

File tree

4 files changed

+50
-5
lines changed

4 files changed

+50
-5
lines changed

nitransforms/io/fsl.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,7 @@ def to_filename(self, filename):
109109
output_dir = Path(filename).parent
110110
output_dir.mkdir(exist_ok=True, parents=True)
111111
for i, xfm in enumerate(self.xforms):
112-
(output_dir / ".".join((str(filename), "%03d" % i))).write_text(
113-
xfm.to_string()
114-
)
112+
(output_dir / f"{filename}.{i:03d}").write_text(str(xfm))
115113

116114
def to_ras(self, moving=None, reference=None):
117115
"""Return a nitransforms' internal RAS matrix."""

nitransforms/io/itk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from h5py import File as H5File
66
from nibabel import Nifti1Header, Nifti1Image
77
from nibabel.affines import from_matvec
8-
from .base import (
8+
from nitransforms.io.base import (
99
BaseLinearTransformList,
1010
DisplacementsField,
1111
LinearParameters,

nitransforms/linear.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,17 @@ def from_filename(cls, filename, fmt=None, reference=None, moving=None):
203203
"""Create an affine from a transform file."""
204204
fmtlist = [fmt] if fmt is not None else ("itk", "lta", "afni", "fsl")
205205

206-
is_array = cls != Affine
206+
if fmt is not None and not Path(filename).exists():
207+
if fmt != "fsl":
208+
raise FileNotFoundError(
209+
f"[Errno 2] No such file or directory: '{filename}'"
210+
)
211+
elif not Path(f"{filename}.000").exists():
212+
raise FileNotFoundError(
213+
f"[Errno 2] No such file or directory: '{filename}[.000]'"
214+
)
207215

216+
is_array = cls != Affine
208217
errors = []
209218
for potential_fmt in fmtlist:
210219
if (potential_fmt == "itk" and Path(filename).suffix == ".mat"):

nitransforms/tests/test_linear.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,12 @@ def test_linear_typeerrors2(data_path):
4848
nitl.Affine.from_filename(data_path / "itktflist.tfm", fmt="itk")
4949

5050

51+
def test_linear_filenotfound(data_path):
52+
"""Exercise errors in Affine creation."""
53+
with pytest.raises(FileNotFoundError):
54+
nitl.Affine.from_filename("doesnotexist.tfm", fmt="itk")
55+
56+
5157
def test_linear_valueerror():
5258
"""Exercise errors in Affine creation."""
5359
with pytest.raises(ValueError):
@@ -85,6 +91,38 @@ def test_loadsave_itk(tmp_path, data_path, testdata_path):
8591
)
8692

8793

94+
@pytest.mark.parametrize(
95+
"image_orientation",
96+
[
97+
"RAS",
98+
"LAS",
99+
"LPS",
100+
"oblique",
101+
],
102+
)
103+
def test_itkmat_loadsave(tmpdir, data_path, image_orientation):
104+
tmpdir.chdir()
105+
106+
io.itk.ITKLinearTransform.from_filename(
107+
data_path / f"affine-{image_orientation}.itk.tfm"
108+
).to_filename(f"affine-{image_orientation}.itk.mat")
109+
110+
xfm = nitl.load(data_path / f"affine-{image_orientation}.itk.tfm", fmt="itk")
111+
mat1 = nitl.load(f"affine-{image_orientation}.itk.mat", fmt="itk")
112+
113+
assert xfm == mat1
114+
115+
mat2 = nitl.Affine.from_filename(f"affine-{image_orientation}.itk.mat", fmt="itk")
116+
117+
assert xfm == mat2
118+
119+
mat3 = nitl.LinearTransformsMapping.from_filename(
120+
f"affine-{image_orientation}.itk.mat", fmt="itk"
121+
)
122+
123+
assert xfm == mat3
124+
125+
88126
@pytest.mark.parametrize("autofmt", (False, True))
89127
@pytest.mark.parametrize("fmt", ["itk", "fsl", "afni", "lta"])
90128
def test_loadsave(tmp_path, data_path, testdata_path, autofmt, fmt):

0 commit comments

Comments
 (0)