Skip to content
Snippets Groups Projects
Commit b5a7e362 authored by Paul McCarthy's avatar Paul McCarthy :mountain_bicyclist:
Browse files

RF: Moved affine.identify to staticmethod Nifti.identifyAffine

parent 0f60a744
No related branches found
No related tags found
No related merge requests found
...@@ -34,6 +34,7 @@ and file names: ...@@ -34,6 +34,7 @@ and file names:
import os import os
import os.path as op import os.path as op
import itertools as it
import string import string
import logging import logging
import tempfile import tempfile
...@@ -408,6 +409,54 @@ class Nifti(notifier.Notifier, meta.Meta): ...@@ -408,6 +409,54 @@ class Nifti(notifier.Notifier, meta.Meta):
return affines, isneuro return affines, isneuro
@staticmethod
def identifyAffine(image, xform, from_=None, to=None):
"""Attempt to identify the source or destination space for the given
affine.
``xform`` is assumed to be an affine transformation which can be used
to transform coordinates between two coordinate systems associated with
``image``.
If one of ``from_`` or ``to`` is provided, the other will be derived.
If neither are provided, both will be derived. See the
:meth:`.Nifti.getAffine` method for details on the valild values that
``from_`` and ``to`` may take.
:arg image: :class:`.Nifti` instance associated with the affine.
:arg xform: ``(4, 4)`` ``numpy`` array encoding an affine
transformation
:arg from_: Label specifying the coordinate system which ``xform``
takes as input
:arg to: Label specifying the coordinate system which ``xform``
produces as output
:returns: A tuple containing:
- A label for the ``from_`` coordinate system
- A label for the ``to`` coordinate system
"""
if (from_ is not None) and (to is not None):
return from_, to
if from_ is not None: froms = [from_]
else: froms = ['voxel', 'fsl', 'world']
if to is not None: tos = [to]
else: tos = ['voxel', 'fsl', 'world']
for from_, to in it.product(froms, tos):
candidate = image.getAffine(from_, to)
if np.all(np.isclose(candidate, xform)):
return from_, to
raise ValueError('Could not identify affine')
def strval(self, key): def strval(self, key):
"""Returns the specified NIFTI header field, converted to a python """Returns the specified NIFTI header field, converted to a python
string, correctly null-terminated, and with non-printable characters string, correctly null-terminated, and with non-printable characters
......
...@@ -21,7 +21,6 @@ transformations. The following functions are available: ...@@ -21,7 +21,6 @@ transformations. The following functions are available:
axisAnglesToRotMat axisAnglesToRotMat
axisBounds axisBounds
rmsdev rmsdev
identify
And a few more functions are provided for working with vectors: And a few more functions are provided for working with vectors:
...@@ -34,9 +33,9 @@ And a few more functions are provided for working with vectors: ...@@ -34,9 +33,9 @@ And a few more functions are provided for working with vectors:
""" """
import collections.abc as abc
import numpy as np import numpy as np
import numpy.linalg as linalg import numpy.linalg as linalg
import collections.abc as abc
def invert(x): def invert(x):
...@@ -583,57 +582,3 @@ def rmsdev(T1, T2, R=None, xc=None): ...@@ -583,57 +582,3 @@ def rmsdev(T1, T2, R=None, xc=None):
erms = np.sqrt(erms) erms = np.sqrt(erms)
return erms return erms
def identify(image, xform, from_=None, to=None):
"""Attempt to identify the source or destination space for the given
affine.
``xform`` is assumed to be an affine transformation which can be used
to transform coordinates between two coordinate systems associated with
``image``.
One of ``from_`` or ``to`` must be provided. Whichever is not provided
will be derived. See the :meth:`.Nifti.getAffine` method for details on
the valild values that ``from_`` and ``to`` may take.
:arg image: :class:`.Nifti` instance associated with the affine.
:arg xform: ``(4, 4)`` ``numpy`` array encoding an affine transformation
:arg from_: Label specifying the coordinate system which ``xform``
takes as input
:arg to: Label specifying the coordinate system which ``xform``
produces as output
:returns: A tuple containing:
- A label for the ``from_`` coordinate system
- A label for the ``to`` coordinate system
"""
if (from_ is None) and (to is None):
raise ValueError('One of from_ or to must be provided')
if (from_ is not None) and (to is not None):
return from_, to
if from_ is None:
voxel = image.getAffine('voxel', to)
fsl = image.getAffine('fsl', to)
world = image.getAffine('world', to)
if np.all(np.isclose(voxel, xform)): return 'voxel', to
if np.all(np.isclose(fsl, xform)): return 'fsl', to
if np.all(np.isclose(world, xform)): return 'world', to
if to is None:
voxel = image.getAffine(from_, 'voxel')
fsl = image.getAffine(from_, 'fsl')
world = image.getAffine(from_, 'world')
if np.all(np.isclose(voxel, xform)): return 'voxel', to
if np.all(np.isclose(fsl, xform)): return 'fsl', to
if np.all(np.isclose(world, xform)): return 'world', to
raise ValueError('Could not identify affine')
...@@ -335,9 +335,10 @@ import numpy as np ...@@ -335,9 +335,10 @@ import numpy as np
import nibabel as nib import nibabel as nib
import h5py import h5py
import fsl.version as version import fsl.version as version
from . import affine import fsl.data.image as fslimage
from . import nonlinear from . import affine
from . import nonlinear
X5_FORMAT = 'X5' X5_FORMAT = 'X5'
...@@ -593,9 +594,11 @@ def _readNonLinearCommon(group): ...@@ -593,9 +594,11 @@ def _readNonLinearCommon(group):
try: try:
if pre is not None: if pre is not None:
refSpace = affine.identify(ref, pre, from_='world')[1] refSpace = fslimage.Nifti.identifyAffine(
ref, pre, from_='world')[1]
if post is not None: if post is not None:
srcSpace = affine.identify(src, post, to='world')[ 0] srcSpace = fslimage.Nifti.identifyAffine(
src, post, to='world')[ 0]
except ValueError: except ValueError:
raise X5Error('Invalid pre/post affine') raise X5Error('Invalid pre/post affine')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment