Extras¶
The format classes in FileFormats-Medimage declare “extra” methods (hooks) for functionality that needs external libraries. The hooks are implemented in the sister package fileformats-medimage-extras, which is imported automatically the first time one of them is called. For how hooks and their implementations work in general, see Extras in the FileFormats docs.
$ python3 -m pip install fileformats-medimage-extras
Calling a hook that has no implementation for the format it is called on raises a
NotImplementedError. Other packages can implement the hooks for formats that aren’t
covered here (see Implementing hooks for other formats).
Hooks¶
Image data and headers¶
Hook |
Returns |
Implemented for |
|---|---|---|
|
the voxel data as a numpy array |
|
|
the image dimensions |
|
|
the voxel sizes along each dimension |
|
|
the |
|
|
the header fields |
|
|
the loaded data (see |
|
Diffusion encodings¶
Hook |
Returns |
Implemented for |
|---|---|---|
|
the gradient directions and b-values as an N x 4 array |
|
|
the b-values |
|
Deidentification¶
Hook |
Returns |
Implemented for |
|---|---|---|
|
a deidentified copy of the data, written to |
|
See Deidentification for how recipes are loaded and passed to it, and how to use your own recipe formats.
Sample data¶
FileSet.generate_sample_data, which is used by FileSet.sample(), is implemented
for
Nifti1,NiftiGzandNiftiGzXNiftiGzXwith classifiers describing the image contents, generated from realistic images in medimages4tests:NiftiGzX[T1w],NiftiGzX[T1w, Brain],NiftiGzX[Fmri]andNiftiGzX[Fmri, Brain]DicomDirandDicomSeries(a T1-weighted MRI series, with a random series number)BvalandBvec
>>> from fileformats.medimage import NiftiGzX, T1w, Brain
>>> t1w = NiftiGzX[T1w, Brain].sample()
Converters¶
The extras register converters (as Pydra tasks) that can be run with the
convert method of the format to convert to, or added to Pydra workflows with
get_converter. Keyword arguments to convert/get_converter are passed on to
the converter.
From |
To |
Tool |
|---|---|---|
|
|
|
any |
|
MRtrix3 |
any |
the MRtrix image formats ( |
MRtrix3 |
The DICOM to NIfTI converter takes the following optional arguments, to work around conversion issues
file_postfixselects one of several output files of the conversion by the postfix dcm2niix gives it (see dcm2niix’s file naming), e.g.
"e2"for the second echoside_car_jqa jq expression to edit the JSON side-car with, e.g. to fix or add fields
extract_volumethe (0-based) index of a volume to extract from a 4D image
to_4dwraps a 3D image in a 4D one (can’t be combined with
extract_volume)
>>> from fileformats.medimage import DicomSeries, NiftiGzX
>>> nifti = NiftiGzX.convert(series, file_postfix="e2", side_car_jq=".EchoNumber = 2")
dcm2niix and MRtrix3 need to be installed separately and be on the PATH (see
Installation).
Implementing hooks for other formats¶
The hooks above can be implemented for other formats by any package, with the
@extra_implementation decorator. The first argument of the implementation is the
format it is implemented for, and the rest of its signature has to match the hook’s.
For example, to read the voxel data of MetaImage files
import SimpleITK as sitk
from fileformats.core import extra_implementation
from fileformats.medimage import MedicalImage, MetaImage
from fileformats.medimage.base import DataArrayType
@extra_implementation(MedicalImage.read_array)
def metaimage_read_array(image: MetaImage) -> DataArrayType:
return sitk.GetArrayFromImage(sitk.ReadImage(str(image.fspath)))
The implementations for the formats in this package belong in fileformats-medimage-extras, so contributions of them are welcome. For formats in other namespaces, implement them in the extras package of that namespace (see the extension template).