Skip to content

Commit 00c769a

Browse files
authored
Merge pull request #50 from yhuang43/master
implement in-place image.framed.reorient()
2 parents 977f6ea + 7b29b54 commit 00c769a

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

surfa/image/framed.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ def transform(self, trf=None, method='linear', rotation='corner', resample=True,
442442

443443
raise ValueError("Pass \'trf\' as either an Affine or Warp object")
444444

445-
def reorient(self, orientation, copy=True):
445+
def reorient(self, orientation, copy=True, inplace=False):
446446
"""
447447
Realigns image data and world matrix to conform to a specific slice orientation.
448448
@@ -452,6 +452,8 @@ def reorient(self, orientation, copy=True):
452452
Case-insensitive orientation string.
453453
copy : bool
454454
Return copy of image even if target orientation is already satisfied.
455+
inplace : bool
456+
Reorient the image data in place if it is True.
455457
456458
Returns
457459
-------
@@ -488,8 +490,13 @@ def reorient(self, orientation, copy=True):
488490
voxsize = voxsize_swapped
489491

490492
# initialize new
491-
data = self.data.copy()
492-
affine = self.geom.vox2world.matrix.copy()
493+
if (not inplace):
494+
data = self.data.copy()
495+
affine = self.geom.vox2world.matrix.copy()
496+
else:
497+
data = self.data
498+
self.geom.vox2world.matrix.flags.writeable = True
499+
affine = self.geom.vox2world.matrix
493500

494501
# align axes
495502
affine[:, world_axes_trg] = affine[:, world_axes_src]
@@ -507,12 +514,18 @@ def reorient(self, orientation, copy=True):
507514
affine[:, i] = - affine[:, i]
508515
affine[:3, 3] = affine[:3, 3] - affine[:3, i] * (data.shape[i] - 1)
509516

510-
# update geometry
511-
target_geom = ImageGeometry(
512-
shape=data.shape[:3],
513-
vox2world=affine,
514-
voxsize=voxsize)
515-
return self.new(data, target_geom)
517+
if (not inplace):
518+
# update geometry
519+
target_geom = ImageGeometry(
520+
shape=data.shape[:3],
521+
vox2world=affine,
522+
voxsize=voxsize)
523+
return self.new(data, target_geom)
524+
else:
525+
self.geom.update(voxsize=voxsize, vox2world=affine)
526+
self.data = data
527+
return self
528+
516529

517530
def reshape(self, shape, center='image', copy=True):
518531
"""

0 commit comments

Comments
 (0)