Skip to content

Commit 0f7e429

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 a14d51a commit 0f7e429

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
@@ -139,6 +139,10 @@ def resample_by_spacing(in_file, zooms, order=3, clip=True):
139139
if isinstance(in_file, (str, Path)):
140140
in_file = nb.load(in_file)
141141

142+
# Prepare output x-forms
143+
sform, scode = in_file.get_sform(coded=True)
144+
qform, qcode = in_file.get_qform(coded=True)
145+
142146
hdr = in_file.header.copy()
143147
dtype = hdr.get_data_dtype()
144148
data = np.asanyarray(in_file.dataobj)
@@ -183,22 +187,20 @@ def resample_by_spacing(in_file, zooms, order=3, clip=True):
183187
if clip:
184188
resampled = np.clip(resampled, a_min=data.min(), a_max=data.max())
185189

190+
# Set new zooms
186191
hdr.set_zooms(zooms)
187-
# Prepare output x-forms
188-
sform, scode = hdr.get_sform(coded=True)
189-
qform, qcode = hdr.get_qform(coded=True)
190192

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

198-
if scode != 0:
199-
hdr.set_sform(new_affine.dot(np.linalg.inv(affine).dot(sform)), code=int(scode))
200200
if qcode != 0:
201201
hdr.set_qform(new_affine.dot(np.linalg.inv(affine).dot(qform)), code=int(qcode))
202+
if scode != 0:
203+
hdr.set_sform(new_affine.dot(np.linalg.inv(affine).dot(sform)), code=int(scode))
202204
if (scode, qcode) == (0, 0):
203205
hdr.set_qform(new_affine, code=1)
204206
hdr.set_sform(new_affine, code=1)

0 commit comments

Comments
 (0)