Skip to content

Commit

Permalink
add global swap_batch_dim option to python fv
Browse files Browse the repository at this point in the history
  • Loading branch information
ahoopes committed Jan 6, 2021
1 parent 35fc57a commit c0a577b
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions python/freesurfer/freeview.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,14 @@ def __init__(self, data, name=None):
self.data = data
self.name = name

def __init__(self, transpose=None):
def __init__(self, swap_batch_dim=False):
'''
Args:
swap_batch_dim: Move the first axis to the last if image input is a numpy array. Default is False.
'''
self.tempdir = None
self.flags = []
self.transpose = transpose
self.swap_batch_dim = swap_batch_dim

def copy(self):
'''
Expand Down Expand Up @@ -234,13 +238,16 @@ def _vol_to_file(self, volume, name=None, force=None, ext='mgz', swap_batch_dim=
# and let the filename creation get handled below
if isinstance(volume, np.ndarray):

# swap batch axis if specified
if swap_batch_dim:
# swap batch axis to frame axis if specified
if swap_batch_dim or self.swap_batch_dim:
if volume.shape[-1] == 1:
volume = volume[..., 0]
volume = np.moveaxis(volume, 0, -1)

orig_shape = volume.shape
volume = self._convert_ndarray(volume) if force is None else force(volume.squeeze())
if volume is None:
error('cannot convert array of shape %s' % str(volume.shape))
error('cannot convert array of shape %s' % str(orig_shape))
return None

# configure filename
Expand All @@ -259,9 +266,6 @@ def _vol_to_file(self, volume, name=None, force=None, ext='mgz', swap_batch_dim=
error('cannot save annotation without embedded lookup table')
return None

if self.transpose is not None:
volume = np.transpose(volume, self.transpose)

# check if fs array container
if isinstance(volume, (Overlay, Image, Volume)):
volume.write(filename)
Expand Down

0 comments on commit c0a577b

Please sign in to comment.