Skip to content

Commit c689721

Browse files
committed
fix: address a weird qform in-memory rewrite
Some instruction above line 190 was in-place modifying the qform matrix (??). I placed those at the top, directly from the image object (which, BTW preserved the correct qform at that point).
1 parent cdbd778 commit c689721

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

niworkflows/utils/images.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,10 @@ def resample_by_spacing(in_file, zooms, order=3, clip=True):
137137
if isinstance(in_file, (str, Path)):
138138
in_file = nb.load(in_file)
139139

140+
# Prepare output x-forms
141+
sform, scode = in_file.get_sform(coded=True)
142+
qform, qcode = in_file.get_qform(coded=True)
143+
140144
hdr = in_file.header.copy()
141145
dtype = hdr.get_data_dtype()
142146
data = np.asanyarray(in_file.dataobj)
@@ -181,22 +185,20 @@ def resample_by_spacing(in_file, zooms, order=3, clip=True):
181185
if clip:
182186
resampled = np.clip(resampled, a_min=data.min(), a_max=data.max())
183187

188+
# Set new zooms
184189
hdr.set_zooms(zooms)
185-
# Prepare output x-forms
186-
sform, scode = hdr.get_sform(coded=True)
187-
qform, qcode = hdr.get_qform(coded=True)
188190

189191
# Get the original image's affine
190-
affine = in_file.affine
192+
affine = in_file.affine.copy()
191193
# Determine rotations w.r.t. cardinal axis and eccentricity
192194
rot = affine.dot(np.linalg.inv(card))
193195
# Apply to the new cardinal, so that the resampling is consistent
194196
new_affine = rot.dot(new_card)
195197

196-
if scode != 0:
197-
hdr.set_sform(new_affine.dot(np.linalg.inv(affine).dot(sform)), code=int(scode))
198198
if qcode != 0:
199199
hdr.set_qform(new_affine.dot(np.linalg.inv(affine).dot(qform)), code=int(qcode))
200+
if scode != 0:
201+
hdr.set_sform(new_affine.dot(np.linalg.inv(affine).dot(sform)), code=int(scode))
200202
if (scode, qcode) == (0, 0):
201203
hdr.set_qform(new_affine, code=1)
202204
hdr.set_sform(new_affine, code=1)

0 commit comments

Comments
 (0)