Skip to content

Commit f4331fb

Browse files
authored
Merge branch 'master' into sandbox
2 parents c7a958c + ce08e37 commit f4331fb

File tree

5 files changed

+50
-1
lines changed

5 files changed

+50
-1
lines changed

nitransforms/io/afni.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,17 @@ def from_image(cls, imgobj):
209209

210210
return imgobj.__class__(field, imgobj.affine, hdr)
211211

212+
@classmethod
213+
def to_image(cls, imgobj):
214+
"""Export a displacements field from a nibabel object."""
215+
216+
hdr = imgobj.header.copy()
217+
218+
warp_data = imgobj.get_fdata().reshape(imgobj.shape[:3] + (1, imgobj.shape[-1]))
219+
warp_data[..., (0, 1)] *= -1
220+
221+
return imgobj.__class__(warp_data, imgobj.affine, hdr)
222+
212223

213224
def _is_oblique(affine, thres=OBLIQUITY_THRESHOLD_DEG):
214225
"""

nitransforms/io/base.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,17 @@ def from_image(cls, imgobj):
146146
"""Import a displacements field from a nibabel image object."""
147147
raise NotImplementedError
148148

149+
@classmethod
150+
def to_filename(cls, img, filename):
151+
"""Export a displacements field to a NIfTI file."""
152+
imgobj = cls.to_image(img)
153+
imgobj.to_filename(filename)
154+
155+
@classmethod
156+
def to_image(cls, imgobj):
157+
"""Export a displacements field image from a nitransforms image object."""
158+
raise NotImplementedError
159+
149160

150161
def _ensure_image(img):
151162
if isinstance(img, (str, Path)):

nitransforms/io/fsl.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,17 @@ def from_image(cls, imgobj):
190190

191191
return imgobj.__class__(field, imgobj.affine, hdr)
192192

193+
@classmethod
194+
def to_image(cls, imgobj):
195+
"""Export a displacements field from a nibabel object."""
196+
197+
hdr = imgobj.header.copy()
198+
199+
warp_data = imgobj.get_fdata()
200+
warp_data[..., 0] *= -1
201+
202+
return imgobj.__class__(warp_data, imgobj.affine, hdr)
203+
193204

194205
def _fsl_aff_adapt(space):
195206
"""

nitransforms/io/itk.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,18 @@ def from_image(cls, imgobj):
352352

353353
return imgobj.__class__(field, imgobj.affine, hdr)
354354

355+
@classmethod
356+
def to_image(cls, imgobj):
357+
"""Export a displacements field from a nibabel object."""
358+
359+
hdr = imgobj.header.copy()
360+
hdr.set_intent("vector")
361+
362+
warp_data = imgobj.get_fdata().reshape(imgobj.shape[:3] + (1, imgobj.shape[-1]))
363+
warp_data[..., (0, 1)] *= -1
364+
365+
return imgobj.__class__(warp_data, imgobj.affine, hdr)
366+
355367

356368
class ITKCompositeH5:
357369
"""A data structure for ITK's HDF5 files."""

nitransforms/nonlinear.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,13 +173,17 @@ def map(self, x, inverse=False):
173173
ijk.T,
174174
order=3,
175175
mode="constant",
176-
cval=0,
176+
cval=np.nan,
177177
prefilter=True,
178178
)
179179
for i in range(self.reference.ndim)
180180
)
181181
).T
182182

183+
# Set NaN values back to the original coordinates value = no displacement
184+
new_map[np.isnan(new_map)] = x[np.isnan(new_map)]
185+
return new_map
186+
183187
def __matmul__(self, b):
184188
"""
185189
Compose with a transform on the right.

0 commit comments

Comments
 (0)