Beispiel #1
0
    def setQForm(self, m, code='scanner'):
        """Sets the qform matrix.

        The supplied value has to be a 4x4 matrix. The matrix will be converted
        to float.

        The inverse qform matrix and the quaternion representation will be
        automatically recalculated.

        :Parameters:
          m: 4x4 ndarray
            The qform matrix.
          code: str | `NIFTI_XFORM_CODE` | int (0..4)
            The type of the coordinate system the qform matrix is describing.
            By default this coordinate system is assumed to be the scanner
            space.  Please refer to the
            :meth:`~nifti.format.NiftiFormat.setXFormCode` method for a
            full list of possible codes and their meaning.

        .. seealso::
          :meth:`~nifti.format.NiftiFormat.getQForm`,
          :meth:`~nifti.format.NiftiFormat.setQFormCode`,
          :meth:`~nifti.format.NiftiFormat.getQFormCode`,
          :meth:`~nifti.format.NiftiFormat.getQuaternion`,
          :meth:`~nifti.format.NiftiFormat.getQOffset`,
          :meth:`~nifti.format.NiftiFormat.setQuaternion`,
          :meth:`~nifti.format.NiftiFormat.setQOffset`,
          :meth:`~nifti.format.NiftiFormat.setQFac`,
          :attr:`~nifti.format.NiftiFormat.qform`,
          :attr:`~nifti.format.NiftiFormat.qform_inv`,
          :attr:`~nifti.format.NiftiFormat.qform_code`,
          :attr:`~nifti.format.NiftiFormat.quatern`,
          :attr:`~nifti.format.NiftiFormat.qoffset`,
          :attr:`~nifti.format.NiftiFormat.qfac`
        """
        if m.shape != (4, 4):
            raise ValueError, "QForm matrix has to be of size 4x4."

        # make sure it is float
        m = m.astype('float')

        ncl.set_mat44( self.__nimg.qto_xyz,
                         m[0,0], m[0,1], m[0,2], m[0,3],
                         m[1,0], m[1,1], m[1,2], m[1,3],
                         m[2,0], m[2,1], m[2,2], m[2,3],
                         m[3,0], m[3,1], m[3,2], m[3,3] )

        # recalculate inverse
        self.__nimg.qto_ijk = \
            ncl.nifti_mat44_inverse( self.__nimg.qto_xyz )

        # update quaternions
        ( self.__nimg.quatern_b, self.__nimg.quatern_c, self.__nimg.quatern_d,
          self.__nimg.qoffset_x, self.__nimg.qoffset_y, self.__nimg.qoffset_z,
          self.__nimg.dx, self.__nimg.dy, self.__nimg.dz,
          self.__nimg.qfac ) = \
            ncl.nifti_mat44_to_quatern( self.__nimg.qto_xyz )

        # set qform code, which decides how the qform matrix is interpreted
        self.setXFormCode('qform', code)
Beispiel #2
0
    def setSForm(self, m, code='mni152'):
        """Sets the sform matrix.

        The supplied value has to be a 4x4 matrix. The matrix elements will be
        converted to floats. By definition the last row of the sform matrix has
        to be (0,0,0,1). However, different values can be assigned, but will
        not be stored when the NIfTI image is saved to a file.

        The inverse sform matrix will be automatically recalculated.

        :Parameters:
          m: 4x4 ndarray
            The sform matrix.
          code: str | `NIFTI_XFORM_CODE` | int (0..4)
            The type of the coordinate system the sform matrix is describing.
            By default this coordinate system is assumed to be the MNI152
            space.  Please refer to the
            :meth:`~nifti.format.NiftiFormat.setXFormCode` method for a
            full list of possible codes and their meaning.

        .. seealso::
          :meth:`~nifti.format.NiftiFormat.getSForm`,
          :meth:`~nifti.format.NiftiFormat.setSFormCode`,
          :meth:`~nifti.format.NiftiFormat.getSFormCode`,
          :attr:`~nifti.format.NiftiFormat.sform`,
          :attr:`~nifti.format.NiftiFormat.sform_code`
        """
        if m.shape != (4, 4):
            raise ValueError, "SForm matrix has to be of size 4x4."

        # make sure it is float
        m = m.astype('float')

        ncl.set_mat44( self.__nimg.sto_xyz,
                         m[0,0], m[0,1], m[0,2], m[0,3],
                         m[1,0], m[1,1], m[1,2], m[1,3],
                         m[2,0], m[2,1], m[2,2], m[2,3],
                         m[3,0], m[3,1], m[3,2], m[3,3] )

        # recalculate inverse
        self.__nimg.sto_ijk = \
            ncl.nifti_mat44_inverse( self.__nimg.sto_xyz )

        # set sform code, which decides how the sform matrix is interpreted
        self.setXFormCode('sform', code)