How to use the nibabel.nifti1.Nifti1Image 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 neuropoly / spinalcordtoolbox / unit_testing / test_resample.py View on Github external
def fake_4dimage_nipy():
    """
    :return: an empty 4-d nipy Image
    """
    nx, ny, nz, nt = 9, 9, 9, 3  # image dimension
    data = np.zeros((nx, ny, nz, nt), dtype=np.int8)
    data[4, 4, 4, 0] = 1.
    affine = np.eye(4)
    # Create nibabel object
    nii = nib.nifti1.Nifti1Image(data, affine)
    # return nipy object
    return nifti2nipy(nii)
github neuropoly / spinalcordtoolbox / unit_testing / test_resample.py View on Github external
def fake_3dimage_nipy():
    """
    :return: an empty 3-d nipy Image
    """
    nx, ny, nz = 9, 9, 9  # image dimension
    data = np.zeros((nx, ny, nz), dtype=np.int8)
    data[4, 4, 4] = 1.
    affine = np.eye(4)
    # Create nibabel object
    nii = nib.nifti1.Nifti1Image(data, affine)
    # return nipy object
    return nifti2nipy(nii)
github neuropoly / spinalcordtoolbox / unit_testing / test_image.py View on Github external
def fake_3dimage_custom(data):
    """
    :return: a Nifti1Image (3D) in RAS+ space
    """
    affine = np.eye(4)
    return nibabel.nifti1.Nifti1Image(data, affine)
github BNUCNL / FreeROI / froi / core / dataobject.py View on Github external
}
        if sys.maxint > 2 ** 32: # The platform is 64 bit
            data_type[np.float128] = NIFTI_TYPE_FLOAT128
            data_type[np.float64] = NIFTI_TYPE_FLOAT64
            data_type[np.int64] = NIFTI_TYPE_INT64
            data_type[np.uint64] = NIFTI_TYPE_UINT64
            data_type[np.complex64] = NIFTI_TYPE_COMPLEX64
            data_type[np.complex128] = NIFTI_TYPE_COMPLEX128
            data_type[np.complex256] = NIFTI_TYPE_COMPLEX256

        data = np.rot90(self._data, 3)
        if data_type.has_key(data.dtype.type):
            self._header['datatype'] = data_type[data.dtype.type]
        self._header['cal_max'] = data.max()
        self._header['cal_min'] = 0
        image = nib.nifti1.Nifti1Image(data, None, self._header)
        nib.nifti1.save(image, file_path)
github LTS5 / cmp_nipype / cmp / stages / preprocessing / preprocessing.py View on Github external
diffusion_file = nib.load(self.inputs.in_file)
        diffusion = diffusion_file.get_data()
        affine = diffusion_file.get_affine()
        dim = diffusion.shape
        if self.inputs.start > 0 and self.inputs.end > dim[3]-1:
            error('End volume is set to %d but it should be bellow %d' % (self.inputs.end, dim[3]-1))
        padding_idx1 = range(0,self.inputs.start)
        if len(padding_idx1) > 0:
            temp = diffusion[:,:,:,0:self.inputs.start]
            nib.save(nib.nifti1.Nifti1Image(temp,affine),os.path.abspath('padding1.nii.gz'))
        temp = diffusion[:,:,:,self.inputs.start:self.inputs.end+1]
        nib.save(nib.nifti1.Nifti1Image(temp,affine),os.path.abspath('data.nii.gz'))
        padding_idx2 = range(self.inputs.end,dim[3]-1)
        if len(padding_idx2) > 0:
            temp = diffusion[:,:,:,self.inputs.end+1:dim[3]]
            nib.save(nib.nifti1.Nifti1Image(temp,affine),os.path.abspath('padding2.nii.gz'))        
            
        return runtime
github IBT-FMI / SAMRI / samri / report / roi.py View on Github external
iterations : int, optional
		The number of times the one-cell-increment erosion will be applied.
	save_as : str, optional
		Path under which to save the eroded mask.
		If no value is specified the image object will only be returned.

	Returns
	-------
	nibabel.nifti1.Nifti1Image
		Eroded mask image.
	"""
	from scipy import ndimage
	mask_img = nib.load(mask)
	mask_data = mask_img.get_data()
	mask_data = ndimage.morphology.binary_erosion(mask_data, iterations=iterations)
	new_mask = nib.nifti1.Nifti1Image(mask_data, mask_img.affine, mask_img.header)
	if save_as:
		nib.save(new_mask, save_as)
	return new_mask
github the-virtual-brain / tvb-recon / bnm / recon / algo / __init__.py View on Github external
def simple_label_config(aparc_fname, out_fname):
    "Rewrite label volume to have contiguous values like mrtrix' labelconfig."
    aparc = nibabel.load(aparc_fname)
    vol = aparc.get_data()
    uval = np.unique(vol)
    uval_map = np.r_[:uval.max() + 1]
    uval_map[uval] = np.r_[:uval.size]
    uvol = uval_map[vol]
    uparc = nibabel.nifti1.Nifti1Image(uvol, aparc.affine)
    nibabel.save(uparc, out_fname)
github nipy / nibabel / nibabel / nifti2.py View on Github external
class Nifti2PairHeader(Nifti2Header):
    """ Class for NIfTI2 pair header """
    # Signal whether this is single (header + data) file
    is_single = False


class Nifti2Pair(Nifti1Pair):
    """ Class for NIfTI2 format image, header pair
    """
    header_class = Nifti2PairHeader
    _meta_sniff_len = header_class.sizeof_hdr


class Nifti2Image(Nifti1Image):
    """ Class for single file NIfTI2 format image
    """
    header_class = Nifti2Header
    _meta_sniff_len = header_class.sizeof_hdr


def load(filename):
    """ Load NIfTI2 single or pair image from `filename`

    Parameters
    ----------
    filename : str
        filename of image to be loaded

    Returns
    -------
github nipy / nipy / nipy / labs / mask.py View on Github external
affine = get_affine(nim)
            else:
                mean_volume += nim.get_data().squeeze()
        mean_volume /= float(len(list(input_filename)))
    del nim
    if np.isnan(mean_volume).any():
        tmp = mean_volume.copy()
        tmp[np.isnan(tmp)] = 0
        mean_volume = tmp

    mask = compute_mask(mean_volume, first_volume, m, M, cc,
                        opening=opening, exclude_zeros=exclude_zeros)

    if output_filename is not None:
        header['descrip'] = 'mask'
        output_image = nifti1.Nifti1Image(mask.astype(np.uint8),
                                            affine=affine,
                                            header=header)
        save(output_image, output_filename)
    if not return_mean:
        return mask
    else:
        return mask, mean_volume
github the-virtual-brain / tvb-recon / bnm / recon / algo / __init__.py View on Github external
def label_vol_from_tdi(tdi_nii_fname, out_fname, lo=1):
    "Make label volume from tckmap output."
    nii = nibabel.load(tdi_nii_fname)
    tdi = nii.get_data().copy()
    mask = tdi > lo
    tdi[~mask] = 0
    tdi[mask] = np.r_[:mask.sum()]
    out_nii = nibabel.nifti1.Nifti1Image(tdi, nii.affine)
    nibabel.save(out_nii, out_fname)