示例#1
0
def test_xform():
    """
    Testing affine transformation of coordinates
    """

    coords = np.array([[1, 2], [1, 2], [1, 2]])

    npt.assert_equal(ozu.xform(coords, np.eye(4)), coords)

    # Scaling by a factor of 2:
    aff = np.array([[2, 0, 0, 0], [0, 2, 0, 0], [0, 0, 2, 0], [0, 0, 0, 1]])

    npt.assert_equal(ozu.xform(coords, aff), coords * 2)

    # Translation by 1:
    aff = np.array([[1, 0, 0, 1], [0, 1, 0, 1], [0, 0, 1, 1], [0, 0, 0, 1]])

    npt.assert_equal(ozu.xform(coords, aff), coords + 1)

    # Test error handling:

    coords = np.array([[1, 2], [1, 2]])

    npt.assert_raises(ValueError, ozu.xform, coords, aff)

    # Restore the sensible coordinates:
    coords = np.array([[1, 2], [1, 2], [1, 2]])

    aff = np.eye(3)

    npt.assert_raises(ValueError, ozu.xform, coords, aff)
示例#2
0
    def xform(self, affine=None, inplace=True):
        """
        Transform the fiber coordinates according to an affine transformation

        Parameters
        ----------
        affine: optional, 4 by 4 matrix
            Per default, the fiber's own affine will be used. If this input is
            provided, this affine will be used instead of the fiber's
            affine, and the new affine will be the inverse of this matrix.

        inplace: optional, bool
            Per default, the transformation occurs inplace, meaning that the
            Fiber is mutated inplace. However, if you don't want that to
            happen, setting this to False will cause this function to return
            another Fiber with transformed coordinates and the inverse of the
            original affine.

        Note
        ----
        Transforming inverts the affine, such that calling xform() twice gives
        you back what you had in the first place.
        
        """
        # If the affine optional kwarg was provided use that:
        if affine is None:
            if self.affine is None:
                if inplace:
                    return # Don't do anything and return
                else:
                    # Give me back an identical Fiber:
                    return Fiber(self.coords,
                                 None,
                                 self.fiber_stats,
                                 self.node_stats)
                
            # Use the affine provided on initialization:
            else:
                affine = self.affine

        # Do it: 
        xyz_new = ozu.xform(self.coords, affine)

        # Just put the new coords instead of the old ones:
        if inplace:
            self.coords = xyz_new
            # And adjust the affine to be the inverse transform:
            self.affine = affine.getI()
        # Generate a new fiber and return it:
        else: 
            return Fiber(self.coords,
                         affine.getI(),
                         self.fiber_stats,
                         self.node_stats)
示例#3
0
    def xform(self, affine=None, inplace=True):
        """
        Transform the fiber coordinates according to an affine transformation

        Parameters
        ----------
        affine: optional, 4 by 4 matrix
            Per default, the fiber's own affine will be used. If this input is
            provided, this affine will be used instead of the fiber's
            affine, and the new affine will be the inverse of this matrix.

        inplace: optional, bool
            Per default, the transformation occurs inplace, meaning that the
            Fiber is mutated inplace. However, if you don't want that to
            happen, setting this to False will cause this function to return
            another Fiber with transformed coordinates and the inverse of the
            original affine.

        Note
        ----
        Transforming inverts the affine, such that calling xform() twice gives
        you back what you had in the first place.
        
        """
        # If the affine optional kwarg was provided use that:
        if affine is None:
            if self.affine is None:
                if inplace:
                    return  # Don't do anything and return
                else:
                    # Give me back an identical Fiber:
                    return Fiber(self.coords, None, self.fiber_stats,
                                 self.node_stats)

            # Use the affine provided on initialization:
            else:
                affine = self.affine

        # Do it:
        xyz_new = ozu.xform(self.coords, affine)

        # Just put the new coords instead of the old ones:
        if inplace:
            self.coords = xyz_new
            # And adjust the affine to be the inverse transform:
            self.affine = affine.getI()
        # Generate a new fiber and return it:
        else:
            return Fiber(self.coords, affine.getI(), self.fiber_stats,
                         self.node_stats)
示例#4
0
def test_xform():
    """
    Testing affine transformation of coordinates
    """

    coords = np.array([[1,2],[1,2],[1,2]])

    npt.assert_equal(ozu.xform(coords, np.eye(4)), coords)

    # Scaling by a factor of 2:
    aff = np.array([[2,0,0,0],
                    [0,2,0,0],
                    [0,0,2,0],
                    [0,0,0,1]])

    npt.assert_equal(ozu.xform(coords, aff), coords * 2) 

    # Translation by 1: 
    aff = np.array([[1,0,0,1],
                    [0,1,0,1],
                    [0,0,1,1],
                    [0,0,0,1]])

    npt.assert_equal(ozu.xform(coords, aff), coords + 1) 

    # Test error handling:

    coords = np.array([[1,2],[1,2]])

    npt.assert_raises(ValueError, ozu.xform, coords, aff)

    # Restore the sensible coordinates: 
    coords =  np.array([[1,2],[1,2],[1,2]])

    aff = np.eye(3)
    
    npt.assert_raises(ValueError, ozu.xform, coords, aff)
# <codecell>

if subject == 'HT':
    dwi_ni = ni.load('/home/arokem/data/osmosis/HT/0012_01_DTI_2mm_b2000_150dir_aligned_trilin.nii.gz')
    t1_ni = ni.load('/home/arokem/anatomy/takemura/t1.nii.gz')
if subject == 'FP':
    dwi_ni = ni.load('/home/arokem/data/osmosis/FP/0005_01_DTI_2mm_150dir_2x_b2000_aligned_trilin.nii.gz')
    t1_ni = ni.load('/home/arokem/anatomy/pestilli//t1.nii.gz')

# <codecell>

vol = ozu.nans(dwi_ni.shape[:3])

# <codecell>

cc_coords_xform = ozu.xform(cc_coords_mat, np.matrix(dwi_ni.get_affine()).getI())

# <codecell>

vol[cc_coords_xform[0,:].astype(int), cc_coords_xform[1,:].astype(int), cc_coords_xform[2,:].astype(int)] = 1

# <codecell>

new_ni = ni.Nifti1Image(vol, dwi_ni.get_affine())

# <codecell>

new_ni.to_filename('/home/arokem/data/osmosis/%s/%s_cc.nii.gz'%(subject, subject))