pymedio


Namepymedio JSON
Version 0.2.14 PyPI version JSON
download
home_pagehttps://github.com/jcreinhold/pymedio
Summaryread arbitrary medical images in python
upload_time2023-05-27 15:58:59
maintainer
docs_urlNone
authorJacob Reinhold
requires_python>=3.9
licenseMIT license
keywords pymedio
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            =======
pymedio
=======

.. image:: https://img.shields.io/pypi/v/pymedio.svg
        :target: https://pypi.python.org/pypi/pymedio

.. image:: https://img.shields.io/conda/vn/conda-forge/pymedio
        :target: https://anaconda.org/conda-forge/pymedio

.. image:: https://readthedocs.org/projects/pymedio/badge/?version=latest
        :target: https://pymedio.readthedocs.io/en/latest/?version=latest
        :alt: Documentation Status

.. image:: https://img.shields.io/pypi/pyversions/pymedio
        :target: https://www.python.org/

Read arbitrary medical images in Python with various backends for scientific
computing.

Basically, this package is just a modified version of
`torchio <https://github.com/fepegar/torchio>`_ [1]_
and `dicom-numpy <https://github.com/innolitics/dicom-numpy>`_
which eagerly loads images (depending on the backend and settings) and returns
them as `NumPy <https://numpy.org/>`_ arrays (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 license
* Documentation: https://pymedio.readthedocs.io.

Install
-------

The easiest way to install the package is through the following command::

    pip install pymedio

Alternatively, this can be done via conda::

    conda install -c conda-forge pymedio

To install from the source directory, clone the repo and run::

    python setup.py install

To make the package as portable as possible, the package only comes with numpy installed by default.

If you want to load only DICOM images (using `pydicom <https://github.com/pydicom/pydicom>`_) install with::

    pip install "pymedio[dicom]"

If you want to load DICOM and non-DICOM images (using `SimpleITK <https://simpleitk.org/>`_ or
`nibabel <https://nipy.org/nibabel/>`_ as a backend), install with::

    pip install "pymedio[all]"

Basic Usage
-----------

Say you have a directory of DICOM images at the path ``dicom_dir``, and you installed the package with ``dicom``
extras, then you can open it with:

.. code-block:: python

    import pymedio.dicom as miod
    image = miod.DICOMImage.from_path("dicom_dir")

This uses `pydicom <https://github.com/pydicom/pydicom>`_ as a backend to open the image à la
`dicom-numpy <https://github.com/innolitics/dicom-numpy>`_.

If you have a NIfTI image at ``image.nii``, and you installed the package with ``all`` extras, you can open it with:

.. code-block:: python

    import pymedio.image as mioi
    image = 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.,

.. code-block:: python

    image += 1.0
    image *= image

Note that the image will have a ``affine`` attribute 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:

.. code-block:: python

    import torch
    tensor = 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:

.. code-block:: python

    # optionally convert the image to the desired type
    f16_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.,

.. code-block:: python

    from pymedio.base import ImageBase as Image
    f16_image = Image.from_npz("image-f16.npz")
    print(f16_image.affine)

To view the image in a standard reader following some processing (assuming ``all`` extras installed),
you can then do, e.g.,

.. code-block:: python

    import pymedio.image as mioi
    image = 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.

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/jcreinhold/pymedio",
    "name": "pymedio",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.9",
    "maintainer_email": "",
    "keywords": "pymedio",
    "author": "Jacob Reinhold",
    "author_email": "jcreinhold@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/e7/33/e4ee93fc3a7738691cbcfc3709843ab0cfafb1195d01a9c717070c63426f/pymedio-0.2.14.tar.gz",
    "platform": null,
    "description": "=======\npymedio\n=======\n\n.. image:: https://img.shields.io/pypi/v/pymedio.svg\n        :target: https://pypi.python.org/pypi/pymedio\n\n.. image:: https://img.shields.io/conda/vn/conda-forge/pymedio\n        :target: https://anaconda.org/conda-forge/pymedio\n\n.. image:: https://readthedocs.org/projects/pymedio/badge/?version=latest\n        :target: https://pymedio.readthedocs.io/en/latest/?version=latest\n        :alt: Documentation Status\n\n.. image:: https://img.shields.io/pypi/pyversions/pymedio\n        :target: https://www.python.org/\n\nRead arbitrary medical images in Python with various backends for scientific\ncomputing.\n\nBasically, this package is just a modified version of\n`torchio <https://github.com/fepegar/torchio>`_ [1]_\nand `dicom-numpy <https://github.com/innolitics/dicom-numpy>`_\nwhich eagerly loads images (depending on the backend and settings) and returns\nthem as `NumPy <https://numpy.org/>`_ arrays (instead of PyTorch tensors, as\nis the case in torchio).\n\nThere are also various factory functions to load and work with images from\nmemory buffers instead of from disk which is preferable in certain environments,\ne.g., AWS Lambda.\n\nThe main motivation for yet another medical image reader is that I wanted the\nflexibility of torchio in opening almost any medical image type, without requiring\nPyTorch and without the automatic casting of images to arrays of 32-bit floating\npoint precision, which is an unnecessary and memory-intensive operation in many\nscientific computing use cases.\n\n* Free software: MIT license\n* Documentation: https://pymedio.readthedocs.io.\n\nInstall\n-------\n\nThe easiest way to install the package is through the following command::\n\n    pip install pymedio\n\nAlternatively, this can be done via conda::\n\n    conda install -c conda-forge pymedio\n\nTo install from the source directory, clone the repo and run::\n\n    python setup.py install\n\nTo make the package as portable as possible, the package only comes with numpy installed by default.\n\nIf you want to load only DICOM images (using `pydicom <https://github.com/pydicom/pydicom>`_) install with::\n\n    pip install \"pymedio[dicom]\"\n\nIf you want to load DICOM and non-DICOM images (using `SimpleITK <https://simpleitk.org/>`_ or\n`nibabel <https://nipy.org/nibabel/>`_ as a backend), install with::\n\n    pip install \"pymedio[all]\"\n\nBasic Usage\n-----------\n\nSay you have a directory of DICOM images at the path ``dicom_dir``, and you installed the package with ``dicom``\nextras, then you can open it with:\n\n.. code-block:: python\n\n    import pymedio.dicom as miod\n    image = miod.DICOMImage.from_path(\"dicom_dir\")\n\nThis uses `pydicom <https://github.com/pydicom/pydicom>`_ as a backend to open the image \u00e0 la\n`dicom-numpy <https://github.com/innolitics/dicom-numpy>`_.\n\nIf you have a NIfTI image at ``image.nii``, and you installed the package with ``all`` extras, you can open it with:\n\n.. code-block:: python\n\n    import pymedio.image as mioi\n    image = mioi.Image.from_path(\"image.nii\")\n\nIn either case, you can proceed to work with the image data like a normal numpy array, e.g.,\n\n.. code-block:: python\n\n    image += 1.0\n    image *= image\n\nNote that the image will have a ``affine`` attribute which stores the (affine) coordinate transformation\nmatrix to/from scanner coordinates/voxels (even after applying numpy functions/operations to the image).\n\nYou can convert an either image class to a torch tensor\u2014if you have it installed\u2014like:\n\n.. code-block:: python\n\n    import torch\n    tensor = torch.as_tensor(image.torch_compatible())\n\nIf you want to save an image as a non-standard data type (e.g., a 16-bit floating point number), do so with:\n\n.. code-block:: python\n\n    # optionally convert the image to the desired type\n    f16_image = image.astype(np.float16)\n    f16_image.to_npz(\"image-f16.npz\")\n\nThen, in a more resource-constrained environment, you can install pymedio without any extras and run, e.g.,\n\n.. code-block:: python\n\n    from pymedio.base import ImageBase as Image\n    f16_image = Image.from_npz(\"image-f16.npz\")\n    print(f16_image.affine)\n\nTo view the image in a standard reader following some processing (assuming ``all`` extras installed),\nyou can then do, e.g.,\n\n.. code-block:: python\n\n    import pymedio.image as mioi\n    image = mioi.Image.from_npz(\"image-f16.npz\")\n    image.astype(np.float32).save(\"image.nii.gz\")\n\nReferences\n----------\n\n.. [1] P\u00e9rez-Garc\u00eda, Fernando, Rachel Sparks, and Sebastien Ourselin. \"TorchIO: a Python library for efficient loading,\n       preprocessing, augmentation and patch-based sampling of medical images in deep learning.\" Computer Methods and\n       Programs in Biomedicine (2021): 106236.\n",
    "bugtrack_url": null,
    "license": "MIT license",
    "summary": "read arbitrary medical images in python",
    "version": "0.2.14",
    "project_urls": {
        "Bug Tracker": "https://github.com/jcreinhold/pymedio/issues",
        "Documentation": "https://pymedio.readthedocs.io/",
        "Homepage": "https://github.com/jcreinhold/pymedio"
    },
    "split_keywords": [
        "pymedio"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "4ad87afd79c6a0b695a6f70e0fcf86a78451443e1b00e2c36a545cc804d75148",
                "md5": "bfb59b417f9285ba8adfc87cf54aad3d",
                "sha256": "93b832cb636b9122400c5d59fba283ec472cd3956a429c7458f59dfbbe65d750"
            },
            "downloads": -1,
            "filename": "pymedio-0.2.14-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "bfb59b417f9285ba8adfc87cf54aad3d",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 26415,
            "upload_time": "2023-05-27T15:58:57",
            "upload_time_iso_8601": "2023-05-27T15:58:57.878093Z",
            "url": "https://files.pythonhosted.org/packages/4a/d8/7afd79c6a0b695a6f70e0fcf86a78451443e1b00e2c36a545cc804d75148/pymedio-0.2.14-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "e733e4ee93fc3a7738691cbcfc3709843ab0cfafb1195d01a9c717070c63426f",
                "md5": "0780c600bf004de946d4b02601599635",
                "sha256": "7fa1f7277c5b729a6d46e15ccb0249805a8d08a916378a5e658663aba4d295af"
            },
            "downloads": -1,
            "filename": "pymedio-0.2.14.tar.gz",
            "has_sig": false,
            "md5_digest": "0780c600bf004de946d4b02601599635",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 34410,
            "upload_time": "2023-05-27T15:58:59",
            "upload_time_iso_8601": "2023-05-27T15:58:59.652180Z",
            "url": "https://files.pythonhosted.org/packages/e7/33/e4ee93fc3a7738691cbcfc3709843ab0cfafb1195d01a9c717070c63426f/pymedio-0.2.14.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-05-27 15:58:59",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "jcreinhold",
    "github_project": "pymedio",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "tox": true,
    "lcname": "pymedio"
}
        
Elapsed time: 0.10398s