Skip to content

Commit a6dc91b

Browse files
committed
fix: in reality, there's nothing we must do :D
1 parent 3e62230 commit a6dc91b

File tree

3 files changed

+66
-134
lines changed

3 files changed

+66
-134
lines changed

docs/notebooks/02_afni_deoblique.ipynb

+46-124
Large diffs are not rendered by default.

nitransforms/io/afni.py

+19-10
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
from math import pi
33
import numpy as np
44
from nibabel.affines import (
5+
append_diag,
6+
apply_affine,
57
from_matvec,
68
obliquity,
79
voxel_sizes,
@@ -205,14 +207,18 @@ def _afni_warpdrive(nii, forward=True, ras=False):
205207
206208
"""
207209
oblique = nii.affine
208-
plumb = oblique[:3, :3] / np.abs(oblique[:3, :3]).max(0)
209-
plumb[np.abs(plumb) < 1.0] = 0
210-
plumb *= voxel_sizes(oblique)
211-
212-
R = from_matvec(plumb @ np.linalg.inv(oblique[:3, :3]), (0, 0, 0))
213-
plumb_orig = np.linalg.inv(R[:3, :3]) @ oblique[:3, 3]
214-
print(plumb_orig)
215-
R[:3, 3] = R[:3, :3] @ (plumb_orig - oblique[:3, 3])
210+
shape = np.array(nii.shape[:3])
211+
plumb_r = oblique[:3, :3] / np.abs(oblique[:3, :3]).max(0)
212+
plumb_r[np.abs(plumb_r) < 1.0] = 0
213+
plumb_r *= voxel_sizes(oblique)
214+
plumb = np.eye(4)
215+
plumb[:3, :3] = plumb_r
216+
obliq_o = apply_affine(oblique, 0.5 * (shape - 1))
217+
plumb_c = apply_affine(plumb, 0.5 * (shape - 1))
218+
plumb[:3, 3] = -plumb_c + obliq_o
219+
print(obliq_o, apply_affine(plumb, 0.5 * (shape - 1)))
220+
221+
R = plumb @ np.linalg.inv(oblique)
216222
if not ras:
217223
# Change sign to match AFNI's warpdrive_matvec signs
218224
B = np.ones((2, 2))
@@ -228,5 +234,8 @@ def _afni_header(nii, field="WARPDRIVE_MATVEC_FOR_000000"):
228234
root.find(f".//*[@atr_name='{field}']").text,
229235
sep="\n",
230236
dtype="float32"
231-
).reshape((3, 4))
232-
return np.vstack((retval, (0, 0, 0, 1)))
237+
)
238+
if retval.size == 12:
239+
return np.vstack((retval.reshape((3, 4)), (0, 0, 0, 1)))
240+
241+
return retval
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
affine-RAS.afni

0 commit comments

Comments
 (0)