Skip to content

TST: Refactor io/lta to reduce one partial line #246

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 38 additions & 30 deletions nitransforms/io/lta.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
"""Read/write linear transforms."""

import numpy as np
from nibabel.volumeutils import Recoder
from nibabel.affines import voxel_sizes, from_matvec
Expand Down Expand Up @@ -29,12 +30,12 @@ class VolumeGeometry(StringBasedStruct):
template_dtype = np.dtype(
[
("valid", "i4"), # Valid values: 0, 1
("volume", "i4", (3, )), # width, height, depth
("voxelsize", "f4", (3, )), # xsize, ysize, zsize
("volume", "i4", (3,)), # width, height, depth
("voxelsize", "f4", (3,)), # xsize, ysize, zsize
("xras", "f8", (3, 1)), # x_r, x_a, x_s
("yras", "f8", (3, 1)), # y_r, y_a, y_s
("zras", "f8", (3, 1)), # z_r, z_a, z_s
("cras", "f8", (3, )), # c_r, c_a, c_s
("cras", "f8", (3,)), # c_r, c_a, c_s
("filename", "U1024"),
]
) # Not conformant (may be >1024 bytes)
Expand Down Expand Up @@ -109,14 +110,19 @@ def from_string(cls, string):
label, valstring = lines.pop(0).split(" =")
assert label.strip() == key

val = ""
if valstring.strip():
parsed = np.genfromtxt(
parsed = (
np.genfromtxt(
[valstring.encode()], autostrip=True, dtype=cls.dtype[key]
)
if parsed.size:
val = parsed.reshape(sa[key].shape)
sa[key] = val
if valstring.strip()
else None
)

if parsed is not None and parsed.size:
sa[key] = parsed.reshape(sa[key].shape)
else: # pragma: no coverage
"""Do not set sa[key]"""

return volgeom


Expand Down Expand Up @@ -218,11 +224,15 @@ def to_ras(self, moving=None, reference=None):
def to_string(self, partial=False):
"""Convert this transform to text."""
sa = self.structarr
lines = [
"# LTA file created by NiTransforms",
"type = {}".format(sa["type"]),
"nxforms = 1",
] if not partial else []
lines = (
[
"# LTA file created by NiTransforms",
"type = {}".format(sa["type"]),
"nxforms = 1",
]
if not partial
else []
)

# Standard preamble
lines += [
Expand All @@ -232,10 +242,7 @@ def to_string(self, partial=False):
]

# Format parameters matrix
lines += [
" ".join(f"{v:18.15e}" for v in sa["m_L"][i])
for i in range(4)
]
lines += [" ".join(f"{v:18.15e}" for v in sa["m_L"][i]) for i in range(4)]

lines += [
"src volume info",
Expand Down Expand Up @@ -324,10 +331,7 @@ def __getitem__(self, idx):
def to_ras(self, moving=None, reference=None):
"""Set type to RAS2RAS and return the new matrix."""
self.structarr["type"] = 1
return [
xfm.to_ras(moving=moving, reference=reference)
for xfm in self.xforms
]
return [xfm.to_ras(moving=moving, reference=reference) for xfm in self.xforms]

def to_string(self):
"""Convert this LTA into text format."""
Expand Down Expand Up @@ -396,9 +400,11 @@ def from_ras(cls, ras, moving=None, reference=None):
sa["type"] = 1
sa["nxforms"] = ras.shape[0]
for i in range(sa["nxforms"]):
lt._xforms.append(cls._inner_type.from_ras(
ras[i, ...], moving=moving, reference=reference
))
lt._xforms.append(
cls._inner_type.from_ras(
ras[i, ...], moving=moving, reference=reference
)
)

sa["subject"] = "unset"
sa["fscale"] = 0.0
Expand All @@ -407,8 +413,10 @@ def from_ras(cls, ras, moving=None, reference=None):

def _drop_comments(string):
"""Drop comments."""
return "\n".join([
line.split("#")[0].strip()
for line in string.splitlines()
if line.split("#")[0].strip()
])
return "\n".join(
[
line.split("#")[0].strip()
for line in string.splitlines()
if line.split("#")[0].strip()
]
)
Loading