How to use the nibabel.affines.apply_affine function in nibabel

To help you get started, we’ve selected a few nibabel examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mwaskom / lyman / lyman / surface.py View on Github external
data = data_img.get_data()
    data_img = nib.MGHImage(data, data_img.affine, data_img.header)
    anat_img = nib.load(anat_file)

    # Get affines that map to scanner and tkr spaces
    Tanat = anat_img.affine
    Tdata = data_img.affine
    Kanat = anat_img.header.get_vox2ras_tkr()

    # Compute the full transform
    # This takes surface xyz -> anat ijk -> scanner xyz -> data ijk
    xfm = inv(Tdata).dot(Tanat).dot(inv(Kanat))

    # Find volume coordinates corresponding to the surface vertices
    vertices, _ = nib.freesurfer.read_geometry(surf_file)
    vertices = nib.affines.apply_affine(xfm, vertices)
    i, j, k = np.round(vertices).astype(int).T

    # Find a mask for surfaces vertices that are in the volume FOV
    ii, jj, kk = data.shape[:3]
    fov = (np.in1d(i, np.arange(ii))
           & np.in1d(j, np.arange(jj))
           & np.in1d(k, np.arange(kk)))

    # Initialize the output array
    n_v = len(vertices)
    if len(data.shape) == 3:
        shape = (n_v,)
    else:
        shape = (n_v, data.shape[-1])
    surf_data = np.full(shape, null_value, np.float)
github BNUCNL / FreeROI / froi / widgets / datamodel.py View on Github external
def get_space_pos(self):
        """Get current cursor position in RAS space."""
        return apply_affine(self._affine, self._cross_pos)
github scilus / scilpy / scripts / scil_compute_seed_density_map.py View on Github external
seeds = tracts_file.tractogram.data_per_streamline['seeds']
    else:
        parser.error('Tractogram does not contain seeds')

    # Transform seeds if they're all in memory
    if not args.lazy_load:
        seeds = apply_affine(np.linalg.inv(tracts_file.affine), seeds)

    # Create seed density map
    shape = tracts_file.header[Field.DIMENSIONS]
    seed_density = np.zeros(shape, dtype=np.int32)
    for seed in seeds:
        # If seeds are lazily loaded, we have to transform them
        # as they get loaded
        if args.lazy_load:
            seed = apply_affine(np.linalg.inv(tracts_file.affine), seed)

        # Set value at mask, either binary or increment
        seed_voxel = np.round(seed).astype(np.int)
        if args.binary is not None:
            seed_density[tuple(seed_voxel)] = args.binary
        else:
            seed_density[tuple(seed_voxel)] += 1

    # Save seed density map
    dm_img = Nifti1Image(seed_density.astype(np.int32),
                         tracts_file.affine)
    dm_img.to_filename(args.seed_density_filename)
github dPys / PyNets / pynets / core / nodemaker.py View on Github external
def mmToVox(img_affine, mmcoords):
    """
    Function to convert a list of mm coordinates to voxel coordinates.

    Parameters
    ----------
    img_affine : array
        4 x 4 2D Numpy array that is the affine of the image space that the coordinates inhabit.
    mmcoords : list
        List of [x, y, z] or (x, y, z) coordinates in mm-space.
    """
    return nib.affines.apply_affine(np.linalg.inv(img_affine), mmcoords)
github nipy / dipy / dipy / io / stateful_tractogram.py View on Github external
def _shift_voxel_origin(self):
        """ Unsafe function to switch the origin from center to corner
        and vice versa """
        if not self.streamlines:
            return

        shift = np.asarray([0.5, 0.5, 0.5])
        if self._space == Space.VOXMM:
            shift = shift * self._voxel_sizes
        elif self._space == Space.RASMM:
            tmp_affine = np.eye(4)
            tmp_affine[0:3, 0:3] = self._affine[0:3, 0:3]
            shift = apply_affine(tmp_affine, shift)
        if self._shifted_origin:
            shift *= -1

        self._tractogram.streamlines._data += shift
        if not self._shifted_origin:
            logging.info('Origin moved to the center of voxel')
        else:
            logging.info('Origin moved to the corner of voxel')

        self._shifted_origin = not self._shifted_origin
github neurostuff / NiMARE / nimare / utils.py View on Github external
def mm2vox(xyz, affine):
    """
    Convert coordinates to matrix subscripts.
    From here:
    http://blog.chrisgorgolewski.org/2014/12/how-to-convert-between-voxel-and-mm.html
    """
    ijk = nib.affines.apply_affine(np.linalg.inv(affine), xyz)
    return ijk
github dPys / PyNets / pynets / core / nodemaker.py View on Github external
def VoxTomm(img_affine, voxcoords):
    """
    Function to convert a list of voxel coordinates to mm coordinates.

    Parameters
    ----------
    img_affine : array
        4 x 4 2D Numpy array that is the affine of the image space that the coordinates inhabit.
    voxcoords : list
        List of [x, y, z] or (x, y, z) coordinates in voxel-space.
    """
    return nib.affines.apply_affine(img_affine, voxcoords)
github nipy / nipy / nipy / algorithms / registration / groupwise_registration.py View on Github external
def scanner_coords(xyz, affine, from_world, to_world):
    Tv = np.dot(from_world, np.dot(affine, to_world))
    XYZ = apply_affine(Tv, xyz)
    return XYZ[:, 0], XYZ[:, 1], XYZ[:, 2]
github dPys / PyNets / pynets / core / nodemaker.py View on Github external
def mmToVox(img_affine, mmcoords):
    """
    Function to convert a list of mm coordinates to voxel coordinates.

    Parameters
    ----------
    img_affine : array
        4 x 4 2D Numpy array that is the affine of the image space that the coordinates inhabit.
    mmcoords : list
        List of [x, y, z] or (x, y, z) coordinates in mm-space.
    """
    return nib.affines.apply_affine(np.linalg.inv(img_affine), mmcoords)