Example #1
0
def zslice(z, x_spec, y_spec, output_space=''):
    """ Return a slice through a 3d box with z fixed.

    Parameters
    ----------
    z : float
       The value at which z is fixed.
    x_spec : ([float,float], int)
       Tuple representing the x-limits of the bounding 
       box and the number of points.
    y_spec : ([float,float], int)
       Tuple representing the y-limits of the bounding 
       box and the number of points.
    output_space : str, optional
       Origin of the range CoordinateSystem.

    Returns
    -------
    affine_transform : AffineTransform
       An affine transform that describes an plane in 
       LPS coordinates with z fixed.

    Examples
    --------
    >>> x_spec = ([-92,92], 93) # voxels of size 2 in x, starting at -92, ending at 92
    >>> y_spec = ([-114,114], 115) # voxels of size 2 in y, starting at -114, ending at 114
    >>> z40 = zslice(40, x_spec, y_spec)
    >>> z40
    AffineTransform(
       function_domain=CoordinateSystem(coord_names=('i_x', 'i_y'), name='slice', coord_dtype=float64),
       function_range=CoordinateSystem(coord_names=('x+LR', 'y+PA', 'z+SI'), name='', coord_dtype=float64),
       affine=array([[   2.,    0.,  -92.],
                     [   0.,    2., -114.],
                     [   0.,    0.,   40.],
                     [   0.,    0.,    1.]])
    )
    >>> 
    >>> z40([0,0])
    array([ -92., -114.,   40.])
    >>> z40([92,114])
    array([  92.,  114.,   40.])

    >>> bounding_box(z40, (x_spec[1], y_spec[1]))
    ([-92.0, 92.0], [-114.0, 114.0], [40.0, 40.0])

    """
    xlim = x_spec[0]
    ylim = y_spec[0]
    shape = (y_spec[1], x_spec[1])
    origin = [xlim[0],ylim[0],z]
    colvectors = [[(xlim[1]-xlim[0])/(shape[1] - 1.),0,0],
                  [0,(ylim[1]-ylim[0])/(shape[0] - 1.),0]]

    T = from_matrix_vector(np.vstack(colvectors).T, origin)
    affine_domain = CoordinateSystem(['i_x', 'i_y'], 'slice')
    affine_range = CoordinateSystem(lps_output_coordnames, output_space)
    return AffineTransform(affine_domain,
                           affine_range,
                           T)
Example #2
0
def yslice(y, x_spec, z_spec, output_space=''):
    """ Return a slice through a 3d box with y fixed.

    Parameters
    ----------
    y : float
       The value at which y is fixed.
    x_spec : sequence
       A sequence with 2 values of form ((float, float), int). The
       (float, float) components are the min and max x values; the int
       is the number of points.
    z_spec : sequence
       As for `x_spec` but for z
    output_space : str, optional
       Origin of the range CoordinateSystem.

    Returns
    -------
    affine_transform : AffineTransform
       An affine transform that describes an plane in
       LPS coordinates with y fixed.

    Examples
    --------
    >>> x_spec = ([-92,92], 93) # voxels of size 2 in x, starting at -92, ending at 92
    >>> z_spec = ([-70,100], 86) # voxels of size 2 in z, starting at -70, ending at 100
    >>> y70 = yslice(70, x_spec, z_spec)
    >>> y70
    AffineTransform(
       function_domain=CoordinateSystem(coord_names=('i_x', 'i_z'), name='slice', coord_dtype=float64),
       function_range=CoordinateSystem(coord_names=('x+LR', 'y+PA', 'z+SI'), name='', coord_dtype=float64),
       affine=array([[  2.,   0., -92.],
                     [  0.,   0.,  70.],
                     [  0.,   2., -70.],
                     [  0.,   0.,   1.]])
    )
    >>> y70([0,0])
    array([-92.,  70., -70.])
    >>> y70([92,85])
    array([  92.,   70.,  100.])
    >>> 
    >>> bounding_box(y70, (x_spec[1], z_spec[1]))
    ((-92.0, 92.0), (70.0, 70.0), (-70.0, 100.0))
    """
    (xmin, xmax), xno = x_spec
    x_tick = (xmax-xmin) / (xno - 1.0)
    (zmin, zmax), zno = z_spec
    z_tick = (zmax-zmin) / (zno - 1.0)
    origin = [xmin, y, zmin]
    colvectors = np.asarray([[x_tick, 0],
                             [0, 0],
                             [0, z_tick]])
    T = from_matrix_vector(colvectors, origin)
    affine_domain = CoordinateSystem(['i_x', 'i_z'], 'slice')
    affine_range = CoordinateSystem(lps_output_coordnames, output_space)
    return AffineTransform(affine_domain,
                           affine_range,
                           T)
Example #3
0
def xslice(x, y_spec, z_spec, output_space=''):
    """
    Return an LPS slice through a 3d box with x fixed.

    Parameters
    ----------
    x : float
       The value at which x is fixed.
    y_spec : sequence
       A sequence with 2 values of form ((float, float), int). The
       (float, float) components are the min and max y values; the int
       is the number of points.
    z_spec : sequence
       As for `y_spec` but for z
    output_space : str, optional
       Origin of the range CoordinateSystem.

    Returns
    -------
    affine_transform : AffineTransform
       An affine transform that describes an plane in
       LPS coordinates with x fixed.

    Examples
    --------
    >>> y_spec = ([-114,114], 115) # voxels of size 2 in y, starting at -114, ending at 114
    >>> z_spec = ([-70,100], 86) # voxels of size 2 in z, starting at -70, ending at 100
    >>> x30 = xslice(30, y_spec, z_spec)
    >>> x30([0,0])
    array([  30., -114.,  -70.])
    >>> x30([114,85])
    array([  30.,  114.,  100.])
    >>> x30
    AffineTransform(
       function_domain=CoordinateSystem(coord_names=('i_y', 'i_z'), name='slice', coord_dtype=float64),
       function_range=CoordinateSystem(coord_names=('x+LR', 'y+PA', 'z+SI'), name='', coord_dtype=float64),
       affine=array([[   0.,    0.,   30.],
                     [   2.,    0., -114.],
                     [   0.,    2.,  -70.],
                     [   0.,    0.,    1.]])
    )
    >>> bounding_box(x30, (y_spec[1], z_spec[1]))
    ((30.0, 30.0), (-114.0, 114.0), (-70.0, 100.0))
    """
    (ymin, ymax), yno = y_spec
    y_tick = (ymax-ymin) / (yno - 1.0)
    (zmin, zmax), zno = z_spec
    z_tick = (zmax-zmin) / (zno - 1.0)
    origin = [x, ymin, zmin]
    colvectors = np.asarray([[0, 0],
                             [y_tick, 0],
                             [0, z_tick]])
    T = from_matrix_vector(colvectors, origin)
    affine_domain = CoordinateSystem(['i_y', 'i_z'], 'slice')
    affine_range = CoordinateSystem(lps_output_coordnames, output_space)
    return AffineTransform(affine_domain,
                           affine_range,
                           T)
Example #4
0
def xslice(x, y_spec, z_spec, output_space=''):
    """
    Return an LPS slice through a 3d box with x fixed.

    Parameters
    ----------
    x : float
       The value at which x is fixed.
    yspec : ([float,float], int)
       Tuple representing the y-limits of the bounding 
       box and the number of points.
    zspec : ([float,float], int)
       Tuple representing the z-limits of the bounding 
       box and the number of points.
    output_space : str, optional
       Origin of the range CoordinateSystem.

    Returns
    -------
    affine_transform : AffineTransform
       An affine transform that describes an plane in 
       LPS coordinates with x fixed.

    Examples
    --------
    >>> y_spec = ([-114,114], 115) # voxels of size 2 in y, starting at -114, ending at 114
    >>> z_spec = ([-70,100], 86) # voxels of size 2 in z, starting at -70, ending at 100
    >>> x30 = xslice(30, y_spec, z_spec)
    >>> x30([0,0])
    array([  30., -114.,  -70.])
    >>> x30([114,85])
    array([  30.,  114.,  100.])
    >>> x30
    AffineTransform(
       function_domain=CoordinateSystem(coord_names=('i_y', 'i_z'), name='slice', coord_dtype=float64),
       function_range=CoordinateSystem(coord_names=('x+LR', 'y+PA', 'z+SI'), name='', coord_dtype=float64),
       affine=array([[   0.,    0.,   30.],
                     [   2.,    0., -114.],
                     [   0.,    2.,  -70.],
                     [   0.,    0.,    1.]])
    )
    >>> bounding_box(x30, (y_spec[1], z_spec[1]))
    ([30.0, 30.0], [-114.0, 114.0], [-70.0, 100.0])

    """
    zlim = z_spec[0]
    ylim = y_spec[0]
    shape = (z_spec[1], y_spec[1])
    origin = [x,ylim[0],zlim[0]]
    colvectors = [[0,(ylim[1]-ylim[0])/(shape[1] - 1.),0],
                  [0,0,(zlim[1]-zlim[0])/(shape[0] - 1.)]]

    T = from_matrix_vector(np.vstack(colvectors).T, origin)
    affine_domain = CoordinateSystem(['i_y', 'i_z'], 'slice')
    affine_range = CoordinateSystem(lps_output_coordnames, output_space)
    return AffineTransform(affine_domain,
                           affine_range,
                           T)
Example #5
0
def zslice(z, x_spec, y_spec, output_space=''):
    """ Return a slice through a 3d box with z fixed.

    Parameters
    ----------
    z : float
       The value at which z is fixed.
    x_spec : sequence
       A sequence with 2 values of form ((float, float), int). The
       (float, float) components are the min and max x values; the int
       is the number of points.
    y_spec : sequence
       As for `x_spec` but for y
    output_space : str, optional
       Origin of the range CoordinateSystem.

    Returns
    -------
    affine_transform : AffineTransform
       An affine transform that describes a plane in LPS coordinates with z
       fixed.

    Examples
    --------
    >>> x_spec = ([-92,92], 93) # voxels of size 2 in x, starting at -92, ending at 92
    >>> y_spec = ([-114,114], 115) # voxels of size 2 in y, starting at -114, ending at 114
    >>> z40 = zslice(40, x_spec, y_spec)
    >>> z40
    AffineTransform(
       function_domain=CoordinateSystem(coord_names=('i_x', 'i_y'), name='slice', coord_dtype=float64),
       function_range=CoordinateSystem(coord_names=('x+LR', 'y+PA', 'z+SI'), name='', coord_dtype=float64),
       affine=array([[   2.,    0.,  -92.],
                     [   0.,    2., -114.],
                     [   0.,    0.,   40.],
                     [   0.,    0.,    1.]])
    )
    >>> z40([0,0])
    array([ -92., -114.,   40.])
    >>> z40([92,114])
    array([  92.,  114.,   40.])
    >>> bounding_box(z40, (x_spec[1], y_spec[1]))
    ((-92.0, 92.0), (-114.0, 114.0), (40.0, 40.0))
    """
    (xmin, xmax), xno = x_spec
    x_tick = (xmax-xmin) / (xno - 1.0)
    (ymin, ymax), yno = y_spec
    y_tick = (ymax-ymin) / (yno - 1.0)
    origin = [xmin, ymin, z]
    colvectors = np.asarray([[x_tick, 0],
                             [0, y_tick],
                             [0, 0]])
    T = from_matrix_vector(colvectors, origin)
    affine_domain = CoordinateSystem(['i_x', 'i_y'], 'slice')
    affine_range = CoordinateSystem(lps_output_coordnames, output_space)
    return AffineTransform(affine_domain,
                           affine_range,
                           T)
Example #6
0
    def from_params(innames, outnames, params):
        """
        Create an `Affine` instance from sequences of innames and outnames.

        Parameters
        ----------
        innames : ``tuple`` of ``string``
           The names of the axes of the input coordinate systems
        outnames : ``tuple`` of ``string``
           The names of the axes of the output coordinate systems
        params : `Affine`, `ndarray` or `(ndarray, ndarray)`
           An affine mapping between the input and output coordinate
           systems.  This can be represented either by a single
           ndarray (which is interpreted as the representation of the
           mapping in homogeneous coordinates) or an (A,b) tuple.

        Returns
        -------
        aff : `Affine` object instance
        
        Notes
        -----
        :Precondition: ``len(shape) == len(names)``
        
        :Raises ValueError: ``if len(shape) != len(names)``
        """
        if type(params) == type(()):
            A, b = params
            params = affines.from_matrix_vector(A, b)

        ndim = (len(innames) + 1, len(outnames) + 1)
        if params.shape != ndim[::-1]:
            raise ValueError('shape and number of axis names do not agree')
        dtype = params.dtype

        input_coords = CoordinateSystem(innames, "input")
        output_coords = CoordinateSystem(outnames, 'output')
        return Affine(params, input_coords, output_coords)
Example #7
0
def test_from_matrix_vector():
    mat, vec, xform = build_xform()
    newxform = affines.from_matrix_vector(mat, vec)
    assert_equal, newxform, xform
Example #8
0
def yslice(y, x_spec, z_spec, output_space=''):
    """ Return a slice through a 3d box with y fixed.

    Parameters
    ----------
    y : float
       The value at which y is fixed.
    xspec : ([float,float], int)
       Tuple representing the x-limits of the bounding 
       box and the number of points.
    zspec : ([float,float], int)
       Tuple representing the z-limits of the bounding 
       box and the number of points.
    output_space : str, optional
       Origin of the range CoordinateSystem.

    Returns
    -------
    affine_transform : AffineTransform
       An affine transform that describes an plane in 
       LPS coordinates with y fixed.

    Examples
    --------
    >>> x_spec = ([-92,92], 93) # voxels of size 2 in x, starting at -92, ending at 92
    >>> z_spec = ([-70,100], 86) # voxels of size 2 in z, starting at -70, ending at 100
    >>> 

    >>> y70 = yslice(70, x_spec, z_spec)
    >>> y70
    AffineTransform(
       function_domain=CoordinateSystem(coord_names=('i_x', 'i_z'), name='slice', coord_dtype=float64),
       function_range=CoordinateSystem(coord_names=('x+LR', 'y+PA', 'z+SI'), name='', coord_dtype=float64),
       affine=array([[  2.,   0., -92.],
                     [  0.,   0.,  70.],
                     [  0.,   2., -70.],
                     [  0.,   0.,   1.]])
    )
    >>> y70([0,0])
    array([-92.,  70., -70.])
    >>> y70([92,85])
    array([  92.,   70.,  100.])
    >>> 
    >>> bounding_box(y70, (x_spec[1], z_spec[1]))
    ([-92.0, 92.0], [70.0, 70.0], [-70.0, 100.0])

    """
    xlim = x_spec[0]
    zlim = z_spec[0]
    shape = (z_spec[1], x_spec[1])

    origin = [xlim[0],y,zlim[0]]
    colvectors = [[(xlim[1]-xlim[0])/(shape[1] - 1.),0,0],
                  [0,0,(zlim[1]-zlim[0])/(shape[0] - 1.)]]

    T = from_matrix_vector(np.vstack(colvectors).T, origin)
    affine_domain = CoordinateSystem(['i_x', 'i_z'], 'slice')
    affine_range = CoordinateSystem(lps_output_coordnames, output_space)
    return AffineTransform(affine_domain,
                           affine_range,
                           T)