Package: pymedio
Read arbitrary medical images in Python with various backends for scientific
computing.Basically, this package is just a modified version oftorchio[1]anddicom-numpywhich eagerly loads images (depending on the backend and settings) and returns
them asNumPyarrays (instead of PyTorch tensors, as
is the case in torchio).There are also various factory functions to load and work with images from
memory buffers instead of from disk which is preferable in certain environments,
e.g., AWS Lambda.The main motivation for yet another medical image reader is that I wanted the
flexibility of torchio in opening almost any medical image type, without requiring
PyTorch and without the automatic casting of images to arrays of 32-bit floating
point precision, which is an unnecessary and memory-intensive operation in many
scientific computing use cases.Free software: MIT licenseDocumentation:https://pymedio.readthedocs.io.InstallThe easiest way to install the package is through the following command:pip install pymedioAlternatively, this can be done via conda:conda install -c conda-forge pymedioTo install from the source directory, clone the repo and run:python setup.py installTo make the package as portable as possible, the package only comes with numpy installed by default.If you want to load only DICOM images (usingpydicom) install with:pip install "pymedio[dicom]"If you want to load DICOM and non-DICOM images (usingSimpleITKornibabelas a backend), install with:pip install "pymedio[all]"Basic UsageSay you have a directory of DICOM images at the pathdicom_dir, and you installed the package withdicomextras, then you can open it with:importpymedio.dicomasmiodimage=miod.DICOMImage.from_path("dicom_dir")This usespydicomas a backend to open the image à ladicom-numpy.If you have a NIfTI image atimage.nii, and you installed the package withallextras, you can open it with:importpymedio.imageasmioiimage=mioi.Image.from_path("image.nii")In either case, you can proceed to work with the image data like a normal numpy array, e.g.,image+=1.0image*=imageNote that the image will have aaffineattribute which stores the (affine) coordinate transformation
matrix to/from scanner coordinates/voxels (even after applying numpy functions/operations to the image).You can convert an either image class to a torch tensor—if you have it installed—like:importtorchtensor=torch.as_tensor(image.torch_compatible())If you want to save an image as a non-standard data type (e.g., a 16-bit floating point number), do so with:# optionally convert the image to the desired typef16_image=image.astype(np.float16)f16_image.to_npz("image-f16.npz")Then, in a more resource-constrained environment, you can install pymedio without any extras and run, e.g.,frompymedio.baseimportImageBaseasImagef16_image=Image.from_npz("image-f16.npz")print(f16_image.affine)To view the image in a standard reader following some processing (assumingallextras installed),
you can then do, e.g.,importpymedio.imageasmioiimage=mioi.Image.from_npz("image-f16.npz")image.astype(np.float32).save("image.nii.gz")References[1]Pérez-García, Fernando, Rachel Sparks, and Sebastien Ourselin. “TorchIO: a Python library for efficient loading,
preprocessing, augmentation and patch-based sampling of medical images in deep learning.” Computer Methods and
Programs in Biomedicine (2021): 106236.