Skip to content

Commit 395a3d2

Browse files
committed
fix: process exceptions when trying to open X5
1 parent 8010fa0 commit 395a3d2

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

nitransforms/io/x5.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,21 @@ def to_filename(fname: str | Path, x5_list: List[X5Transform]):
140140

141141
def from_filename(fname: str | Path) -> List[X5Transform]:
142142
"""Read a list of :class:`X5Transform` objects from an X5 HDF5 file."""
143-
with h5py.File(str(fname), "r") as in_file:
144-
if in_file.attrs.get("Format") != "X5":
145-
raise ValueError("Input file is not in X5 format")
146-
147-
tg = in_file["TransformGroup"]
148-
return [
149-
_read_x5_group(node)
150-
for _, node in sorted(tg.items(), key=lambda kv: int(kv[0]))
151-
]
143+
try:
144+
with h5py.File(str(fname), "r") as in_file:
145+
if in_file.attrs.get("Format") != "X5":
146+
raise TypeError("Input file is not in X5 format")
147+
148+
tg = in_file["TransformGroup"]
149+
return [
150+
_read_x5_group(node)
151+
for _, node in sorted(tg.items(), key=lambda kv: int(kv[0]))
152+
]
153+
except OSError as err:
154+
if "file signature not found" in err.args[0]:
155+
raise TypeError("Input file is not HDF5.")
156+
157+
raise
152158

153159

154160
def _read_x5_group(node) -> X5Transform:

nitransforms/tests/test_linear.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ def test_linear_to_x5(tmpdir, store_inverse):
265265

266266
aff.to_filename("export1.x5", x5_inverse=store_inverse)
267267

268+
# Test round trip
269+
assert aff == nitl.Affine.from_filename("export1.x5", fmt="X5")
270+
268271
# Test with Domain
269272
img = nb.Nifti1Image(np.zeros((2, 2, 2), dtype="float32"), np.eye(4))
270273
img_path = Path(tmpdir) / "ref.nii.gz"

nitransforms/tests/test_x5.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,5 @@ def test_from_filename_invalid(tmp_path):
7373
with H5File(fname, "w") as f:
7474
f.attrs["Format"] = "NOTX5"
7575

76-
with pytest.raises(ValueError):
76+
with pytest.raises(TypeError):
7777
from_filename(fname)

0 commit comments

Comments
 (0)