deepmr.io.write_image

Contents

deepmr.io.write_image#

deepmr.io.write_image(filename, image, head=None, dataformat='nifti', filepath='./', series_description='', series_number_offset=0, series_number_scale=1000, rescale=False, anonymize=False, verbose=False)[source]#

Write image to disk.

Parameters:
  • filename (str) – Name of the file.

  • image (np.ndarray) – Complex image data of shape (ncontrasts, nslices, ny, n). See 'Notes' for additional information.

  • head (Header, optional) – Structure containing trajectory of shape (ncontrasts, nviews, npts, ndim) and meta information (shape, resolution, spacing, etc). If None, assume 1mm isotropic resolution, contiguous slices and axial orientation. The default is None.

  • dataformat (str, optional) – Available formats (dicom or nifti). The default is nifti.

  • filepath (str, optional) – Path to file. The default is ./.

  • series_description (str, optional) – Custom series description. The default is "" (empty string).

  • series_number_offset (int, optional) – Series number offset with respect to the acquired one. Final series number is series_number_scale * acquired_series_number + series_number_offset. he default is 0.

  • series_number_scale (int, optional) – Series number multiplicative scaling with respect to the acquired one. Final series number is series_number_scale * acquired_series_number + series_number_offset. The default is 1000.

  • rescale (bool, optional) – If true, rescale image intensity between 0 and int16_max. Beware! Avoid this if you are working with quantitative maps. The default is False.

  • anonymize (bool, optional) – If True, remove sensible info from header. The default is False.

  • verbose (bool, optional) – Verbosity flag. The default is False.

Example

>>> import deepmr
>>> import tempfile

Get the filenames for an example DICOM file.

>>> filepath = deepmr.testdata("dicom")

Load the file contents.

>>> image_orig, head_orig = deepmr.io.read_image(filepath)
>>> with tempfile.TemporaryDirectory() as tempdir:
>>>     dcmpath = os.path.join(tempdir, "dicomtest")
>>>     niftipath = os.path.join(tempdir, "niftitest.nii")
>>>     deepmr.io.write_image(dcmpath, image_orig, head_orig, dataformat="dicom")
>>>     deepmr.io.write_image(niftipath, image_orig, head_orig, dataformat="nifti")
>>>     deepmr.io.write_image(dcmpath, image_orig, head_orig, dataformat="dicom")
>>>     deepmr.io.write_image(niftipath, image_orig, head_orig, dataformat="nifti")
>>>     image_dcm, head_dcm = deepmr.io.read_image(dcmpath)
>>>     image_nii, head_nii = deepmr.io.read_image(niftipath)

The result is a image/header pair. Image contains image-space data. Here, it represents a 2D cartesian acquisition with 3 echoes, 2 slices and 192x192 matrix size.

>>> image_dcm.shape
torch.Size([3, 2, 192, 192])
>>> image_nii.shape
torch.Size([3, 2, 192, 192])

Head contains the acquisition information. We can inspect the image shape and resolution:

>>> head_dcm.shape
tensor([  2, 192, 192])
>>> head_nii.shape
tensor([  2, 192, 192])
>>> head_dcm.ref_dicom.SpacingBetweenSlices
'10.5'
>>> head_nii.ref_dicom.SpacingBetweenSlices
'10.5'
>>> head_dcm.ref_dicom.SliceThickness
'7.0'
>>> head_nii.ref_dicom.SliceThickness
'7.0'
>>> head_dcm.ref_dicom.PixelSpacing
[0.67, 0.67]
>>> head_nii.ref_dicom.PixelSpacing
[0.67,0.67]

Head also contains contrast information (for forward simulation and parameter inference):

>>> head_dcm.FA
180.0
>>> head_nii.FA
180.0
>>> head_dcm.TE
tensor([  20.0, 40.0, 60.0])
>>> head_nii.TE
tensor([  20.0, 40.0, 60.0])
>>> head_dcm.TR
3000.0
>>> head_nii.TR
3000.0

Notes

When the image to be written is the result of a reconstruction performed on k-space data loaded using deepmr.io.read_rawdata(), axis order depends on acquisition mode:

  • 2Dcart: (nslices, ncontrasts, ny, nx)

  • 2Dnoncart: (nslices, ncontrasts, ny, nx)

  • 3Dcart: (ncontrasts, nz, ny, nx)

  • 3Dnoncart: (nx, ncontrasts, nz, ny)

In this case, image should be transposed to (ncontrasts, nslices, ny, nx) or (ncontrasts, nz, ny, nx) for 2D/3D acquisitions, respectively. If provided, head will contain the appropriate permutation order (head.transpose()):

  • 2Dcart: head.transpose = [1, 0, 2, 3]

  • 2Dnoncart: head.transpose = [1, 0, 2, 3]

  • 3Dcart: head.transpose = [0, 1, 2, 3]

  • 3Dnoncart: head.transpose = [1, 2, 3, 0]

If head is not provided, the user shoudl manually transpose the image tensor to match the required shape.