Example #1
0
   def __init__(self, data, affine, axis_names, metadata={}, 
                lps=True):
      """ Creates a new nipy image with an affine mapping.

      Parameters
      ----------

      data : ndarray
         ndarray representing the data.

      affine : 4x4 ndarray
         affine transformation to the reference coordinate system

      axis_names : [string]
         names of the axes in the coordinate system.
      """

      if len(axis_names) < 3:
         raise ValueError('XYZImage must have a minimum of 3 axes')

      # The first three axes are assumed to be the
      # spatial ones
      xyz_transform = XYZTransform(affine, axis_names[:3], lps)
      nonspatial_names = axis_names[3:]
        
      if nonspatial_names:
         nonspatial_affine_transform = AffineTransform.from_start_step(nonspatial_names, nonspatial_names, [0]*(data.ndim-3), [1]*(data.ndim-3))
         full_dimensional_affine_transform = cmap_product(xyz_transform, nonspatial_affine_transform)
      else:
         full_dimensional_affine_transform = xyz_transform 

      self._xyz_transform = xyz_transform

      Image.__init__(self, data, full_dimensional_affine_transform,
                     metadata=metadata)
Example #2
0
def merge_images(images, cls=Image, clobber=False,
                 axis='merge'):
    """
    Create a new file based image by combining a series of images together.
    The resulting CoordinateMap are essentially copies of images[0].coordmap

    Parameters
    ----------
    images : [`Image`]
        The list of images to be merged
    cls : ``class``
        The class of image to create
    clobber : ``bool``
        Overwrite the file if it already exists
    axis : ``string``
        Name of the concatenated axis.
        
    Returns
    -------
    ``cls``
    
    """
    
    n = len(images)
    im0 = images[0]
    coordmap = cmap_product(Affine(np.identity(2), 
                                   CoordinateSystem([axis]), 
                                   CoordinateSystem([axis])),
                            im0.coordmap)
    data = np.empty(shape=(n,) + im0.shape)
    for i, image in enumerate(images):
        data[i] = np.asarray(image)[:]
    return Image(data, coordmap)
Example #3
0
    def __init__(self, data, affine, coord_sys, metadata=None):
        """ Creates a new nipy image with an affine mapping.

            Parameters
            ----------

            data : ndarray
                ndarray representing the data.
            affine : 4x4 ndarray
                affine transformation to the reference coordinate system
            coord_system : string
                name of the reference coordinate system.
        """

        function_domain = CoordinateSystem(['axis%d' % i for i in range(3)], 
                                        name=coord_sys)
        function_range = CoordinateSystem(['x','y','z'], name='world')
        spatial_coordmap = AffineTransform(function_domain, function_range,
                                           affine)

        nonspatial_names = ['axis%d' % i for i in range(3, data.ndim)]
        if nonspatial_names:
            nonspatial_coordmap = AffineTransform.from_start_step(nonspatial_names, nonspatial_names, [0]*(data.ndim-3), [1]*(data.ndim-3))
            full_coordmap = cmap_product(coordmap, nonspatial_coordmap)
        else:
            full_coordmap = spatial_coordmap 

        self._spatial_coordmap = spatial_coordmap

        self.coord_sys = coord_sys
        Image.__init__(self, data, full_coordmap) 
        if metadata is not None:
            self.metadata = metadata
Example #4
0
def product_homomorphism(*elements):
    """
    Given a sequence of elements of the same subclass of MatrixGroup,
    they can be thought of as an element of the topological product,
    which has a natural group structure.

    If all of the elements are of the same subclass, then there is a
    natural group homomorphism from the product space to a larger
    MatrixGroup. The matrices of the elements of the larger group will
    be block diagonal with blocks of the size corresponding to the
    dimensions of each corresponding element.

    This function is that homomorphism.
    """
    notsame = filter(lambda x: type(x) != type(elements[0]), elements)
    if notsame:
        raise ValueError, 'all elements should be members of the same group'

    newcmap = cmap_product(*elements)
    matrix = newcmap.affine[:-1,:-1]
    return elements[0].__class__(matrix, newcmap.input_coords)
Example #5
0
def product_homomorphism(*elements):
    """
    Given a sequence of elements of the same subclass of MatrixGroup,
    they can be thought of as an element of the topological product,
    which has a natural group structure.

    If all of the elements are of the same subclass, then there is a
    natural group homomorphism from the product space to a larger
    MatrixGroup. The matrices of the elements of the larger group will
    be block diagonal with blocks of the size corresponding to the
    dimensions of each corresponding element.

    This function is that homomorphism.
    """
    notsame = filter(lambda x: type(x) != type(elements[0]), elements)
    if notsame:
        raise ValueError, 'all elements should be members of the same group'

    newcmap = cmap_product(*elements)
    matrix = newcmap.affine[:-1, :-1]
    return elements[0].__class__(matrix, newcmap.function_domain)