Example #1
0
    def get_nifti(self, topo_view, base_nifti=None, **kwargs):
        """
        Process the nifti

        Parameters
        ----------
        topo_view: array-like
            Topological view to create nifti. 3D.

        Returns
        -------
        image: nipy image
            Nifti image from topological view.
        """
        if base_nifti is None:
            assert self.base_nifti is not None, ("`base.nii` not in dataset "
                                                 "directory. You may need to "
                                                 "reprocess.")
            base_nifti = self.base_nifti
            image = Image.from_image(base_nifti, data=topo_view)
        else:
            if isinstance(base_nifti, str):
                base_nifti = load_image(base_nifti)
            base2new_affine = np.linalg.inv(base_nifti.affine).dot(
                self.base_nifti.affine)
            cmap = AffineTransform("kji", "zxy", base2new_affine)
            image = Image.from_image(base_nifti, data=topo_view, coordmap=cmap)

        return image
Example #2
0
    def get_nifti(self, topo_view, base_nifti=None, **kwargs):
        """
        Process the nifti

        Parameters
        ----------
        topo_view: array-like
            Topological view to create nifti. 3D.

        Returns
        -------
        image: nipy image
            Nifti image from topological view.
        """
        if base_nifti is None:
            assert self.base_nifti is not None, ("`base.nii` not in dataset "
                                                 "directory. You may need to "
                                                 "reprocess.")
            base_nifti = self.base_nifti
            image = Image.from_image(base_nifti, data=topo_view)
        else:
            if isinstance(base_nifti, str):
                base_nifti = load_image(base_nifti)
            base2new_affine = np.linalg.inv(
                base_nifti.affine).dot(self.base_nifti.affine)
            cmap = AffineTransform("kji", "zxy", base2new_affine)
            image = Image.from_image(base_nifti, data=topo_view, coordmap=cmap)

        return image
Example #3
0
def sources_to_nifti(CHECKPOINT, MASKMAT, BASENIFTI, ONAME, savepath, voxels, win):
    bnifti = load_image(BASENIFTI)
    mask = loadmat(MASKMAT)['mask']
    model = np.load(CHECKPOINT) # Numpy array of sources from Infomax ICA

    for i in range(len(model)): # Goes component by component

        W = model[i,:].reshape([voxels,win])

        f = zeros(len(mask))
        idx = where(mask==1)
        data = zeros((bnifti.shape[0],bnifti.shape[1],bnifti.shape[2],W.shape[1]))

        f[idx[0].tolist()] = detrend(W)/std(W)

        for j in range(0,W.shape[1]):
            data[:,:,:,j] = reshape(f,(bnifti.shape[0],bnifti.shape[1],bnifti.shape[2] ), order='F')

        img = Image.from_image(bnifti,data=data)

        os.chdir(savepath)

        fn = ONAME + "%s.nii" % (str(i)) # Where result should be saved and under what name

        save_image(img,fn)
Example #4
0
    def make_image(self, X, base_nifti):
        '''Create a nitfi image from array.

        Args:
            X (numpy.array): array from which to make nifti image.
            base_nifti (nipy.core.api.Image): nifti image template.

        Returns:
            nipy.core.api.Image

        '''
        image = Image.from_image(base_nifti, data=X)
        return image
Example #5
0
    def make_image(self, X, base_nifti):
        '''Create a nitfi image from array.

        Args:
            X (numpy.array): array from which to make nifti image.
            base_nifti (nipy.core.api.Image): nifti image template.

        Returns:
            nipy.core.api.Image

        '''
        image = Image.from_image(base_nifti, data=X)
        return image
Example #6
0
def save_npy_to_nifti(npy_data, filename, base_nifti_filename):
    """
    Saves numpy to nifti.

    Arguments:
        npy_data: numpy array
        filename: filename to save
        base_nifti_filename: base nifti filename
    """

    bnifti = load_image(base_nifti_filename)
    img = Image.from_image(bnifti, data=npy_data.astype('uint8'))
    save_image(img, filename)
    print('Saved {}..'.format(filename))
Example #7
0
    def get_nifti(self, W, base_nifti=None):
        """
        Function to make a nifti file from weights.

        Parameters
        ----------
        W: array-like
            Weights.
        """

        m, r, c, d = W.shape
        if basenifti is None:
            base_nifti = self.base_nifti
        else:
            base2new_affine = np.linalg.inv(
                base_nifti.get_affine()).dot(self.base_nifti.get_affine())

        data = np.zeros([r, c, d, m], dtype=W.dtype)

        for i in range(m):
            data[:, :, :, i]  = W[i]

        image = Image.from_image(base_nifti, data=data)
        return image
Example #8
0
    def get_nifti(self, W, base_nifti=None):
        """
        Function to make a nifti file from weights.

        Parameters
        ----------
        W: array-like
            Weights.
        """

        m, r, c, d = W.shape
        if basenifti is None:
            base_nifti = self.base_nifti
        else:
            base2new_affine = np.linalg.inv(base_nifti.get_affine()).dot(
                self.base_nifti.get_affine())

        data = np.zeros([r, c, d, m], dtype=W.dtype)

        for i in range(m):
            data[:, :, :, i] = W[i]

        image = Image.from_image(base_nifti, data=data)
        return image
Example #9
0
def convert_npy_to_nii(npy_file, base_nifti_filename):
  npy_data = np.load(npy_file).astype('uint8')
  bnifti = load_image(base_nifti_filename)
  img = Image.from_image(bnifti, data=npy_data)
  print (img.get_data().shape, img.get_data().max(), img.get_data().min(), img.get_data().dtype)
  save_image(img, npy_file[:-4] + '.nii.gz')
Example #10
0
File: fmri.py Project: nidl/cortex
 def make_image(self, X, base_nifti, do_pca=True):
     if self.pca is not None and do_pca and self.pca_components:
         X = self.pca.inverse_transform(X)
     image = Image.from_image(base_nifti, data=X)
     return image