Skip to main content

The easy way to manipulate medical images.

Project description

EasyMedicalImages

The easy way to manipulate medical images

---------------------------------------------------------------------------

This is a library that focuses on taking CT scans and transforming them into arrays in python; giving the user easy ways to manipulate them as well. This can easily be used for downstream usage of CT images within a Python script.

Installation

Requires Python 3.12 or newer.

pip install easy-medical-images

The high-level batch loader (HighLevel) is a compiled C++ extension. The published wheels include it, so a normal pip install is all you need.

Expected data layout

Each case lives in its own folder containing an image and a segmentation. By default these are named image.nii.gz and tumor_segmentation_v2.nii.gz, but you can override both names (see image_path and seg_path below).

parent_folder/
├── case_0001/
│   ├── image.nii.gz
│   └── tumor_segmentation_v2.nii.gz
├── case_0002/
│   ├── image.nii.gz
│   └── tumor_segmentation_v2.nii.gz
└── ...

Remeber to put the proper root for the input directory path! For example, if your files live on a cloud.

CTImagesLibrary operates on a single case folder. HighLevel operates on a parent folder and loads every case folder inside it.

How it fits together

Generally, this library assumes the user wants a batch of something rather than a specific image. This is reflected in how you first load in your specific instance, and then can add modifiers how you wish.

There are two main sub libraries:

  1. HighLevel: point it at a parent directory and it loads every case in parallel (after the method 'run_on_directory' is called on the instance). This will return a list of (image, seg) array pairs. Use this to pull a whole dataset into memory quickly.
  2. CTImagesLibrary: load one case and get the full manipulation toolkit (slicing, cropping, reorientation, display). Use this when you want to work with an individual scan. See below for examples.

Quick start: load a whole directory

import EasyMedicalImages as EMI

path_to_folder_of_ct_scans = input("Input path to CT scans: ")
instance = EMI.HighLevel(path_to_folder=path_to_folder_of_ct_scans)

img_seg_pairs_mega_list = instance.run_on_directory()  # list of (image, seg) array pairs
'''
img_seg_pairs_mega_list=[(img1, seg1),(img2, seg2)...]
'''

# Each entry is one case
img, seg = img_seg_pairs_mega_list[0]
print(f"{len(img_seg_pairs_mega_list)} cases loaded")
print(f"First image shape: {img.shape}, seg shape: {seg.shape}")

Every entry in img_seg_pairs_mega_list is a tuple of two NumPy float32 arrays: the image and its segmentation. Both are already resampled to isotropic spacing and reoriented to RAS, so they are ready to use.

To access all of the images or all of the segs while keeping order, one can simply do:

all_images=[images for images,_ in img_seg_pairs_mega_list]
all_segmentations=[segmentations for _, segmentations in img_seg_pairs_mega_list]

# Both of the below will print the entire array
print(f"Images: {all_images}")
print(f"Segmentation Masks: {all_segmentations}")

# This just shows the shape, easier to comprehend.
print([(img.shape, seg.shape) for img, seg in img_seg_pairs_mega_list])

Working with a single case

from EasyMedicalImages import CTImagesLibrary
 
case = CTImagesLibrary(path_to_folder="path/to/one/case")
 
# The voxels are automatically processed upon calling of the instance, unlike the batch version.
print(case.mutated_img_data.shape)
print(case.mutated_seg_data.shape)
 
# Display three orthogonal slices (slice numbers are 1-based, ordered X, Y, Z)
case.display_slice(slice_number=(175, 279, 57), img_or_seg="img")
 
# Get those same slices back as arrays
slice_x, slice_y, slice_z = case.return_slice(slice_number=(175, 279, 57), img_or_seg="img")
 
# Crop the whole volume down to the segmented region (modifies the mutated arrays in place)
case.apply_seg_crop_to_image(img_or_seg="both")
print(case.mutated_img_data.shape)  # smaller now

As stated earlier, building CTImagesLibrary automatically: changes the working directory into path_to_folder, loads the image and segmentation, and builds isotropic, RAS-reoriented copies. After construction, both the raw and processed arrays are available as attributes via your_instancename.raw_img_data, or for seg your_instancename.raw_seg_data.

Further detail

This section goes one level deeper on each of the two sublibraries: how you construct it, and what you can call on it once you have an instance. Both are reached through the top-level package (import EasyMedicalImages as EMI, then EMI.HighLevel or EMI.CTImagesLibrary).

HighLevel

The batch sublibrary. Point it at a parent folder, call run_on_directory, and get every case back as (image, seg) array pairs. This is the one to reach for when you want a whole dataset in memory at once.

Construct it (default parameters):

HighLevel(
    path_to_folder,
    image_path="image.nii.gz",
    seg_path="tumor_segmentation_v2.nii.gz",
    verbose=False,
    thread_deadline_time=66.0,
    verbose_workers=True,
)
  • path_to_folder: parent folder whose immediate subfolders are individual cases.
  • image_path, seg_path: file names to look for inside each case folder.
  • verbose: passed through to each worker for extra logging.
  • thread_deadline_time: per-case wall-clock budget in seconds. A worker that runs longer is killed and that case is skipped. This is to prevent a thread, which is just stalling or trying to load a not image, from stoping the whole process.
  • verbose_workers: print per-worker progress (which case each worker picked up, load confirmations, and the final count).

Methods:

  • run_on_directory(directory_path="") Loads every case in the directory and returns a list of (image, seg) tuples, one per case, in the same order as the folders on disk. Both elements are NumPy float32 arrays, already resampled to isotropic spacing and reoriented to RAS. If directory_path is given it overrides path_to_folder for that one call. Cases that fail to load or exceed thread_deadline_time are dropped, so the returned list can be shorter than the number of folders in theory (so don't set this too low).

Loading runs in parallel: one worker subprocess per case, across roughly 80 percent of the available hardware threads. On Linux it respects the CPU affinity set by schedulers such as SLURM, so it behaves correctly on a cluster node. Note that this method returns images and segmentations only; it does not return affines.

  • how_many_threads_available() Returns the number of hardware threads detected (the figure the worker pool is sized from). Honors SLURM cpuset limits on Linux and falls back sensibly when the OS reports nothing.

CTImagesLibrary

The single-case sublibrary. Construct it on one case folder and the data is processed and waiting on the instance; from there you slice, crop, reorient, and display. This is the toolkit HighLevel runs under the hood for each case, so the arrays you get out of either path match.

Construct it (default parameters):

CTImagesLibrary(
    path_to_folder,
    image_path="image.nii.gz",
    seg_path="tumor_segmentation_v2.nii.gz",
    verbose=True,
    return_affines=False,
)

The image and segmentation are loaded and processed during construction, so the attributes below are ready as soon as the instance exists.

Attributes on the instance:

  • raw_img_data, raw_seg_data: the original voxel arrays as loaded.
  • raw_img_affine, raw_seg_affine: the original 4x4 affine matrices.
  • mutated_img_data, mutated_seg_data: isotropic, RAS-reoriented copies. These are usually what you want for machine learning.
  • mutated_img_affine, mutated_seg_affine: affines describing the processed grid.

methods:

  • display_slice(slice_number=(), cmap="viridis", view_axes=("X", "Y", "Z"), load_mutated=True, crop=False, pad=5, img_or_seg="img") Shows one slice per requested axis side by side with matplotlib. slice_number is a 1-based (X, Y, Z) tuple and must contain exactly three values. Set load_mutated=False to view the original (non-isotropic) volume, crop=True to trim each slice to its own non-background region (with pad voxels of margin), and img_or_seg="seg" to view the segmentation instead.

  • return_slice(slice_number=(), view_axes=("X", "Y", "Z"), load_mutated=True, one_list=False, crop=False, pad=5, img_or_seg="img") Returns slices as arrays instead of displaying them. With the default three axes it returns (slice_x, slice_y, slice_z); set one_list=True to get them as a single array of three instead (convenient for batching). Request a single axis (for example view_axes=("Z",)) to get just that one slice back. The arrays match exactly what display_slice shows.

  • apply_seg_crop_to_image(pad=5, return_data=False, img_or_seg="both") Crops the full 3D volume in place to the bounding box of the segmentation, keeping pad voxels of margin. This overwrites mutated_img_data and mutated_seg_data (and their affines) with the cropped versions. Use img_or_seg to crop only "img", only "seg", or "both" (the default). If the segmentation is empty, nothing is cropped. Set return_data=True to also return the cropped arrays.

  • apply_seg_crop_to_slice(slice_number=(), one_list=False, pad=5, img_or_seg="img") The 2D version: returns the X, Y, and Z slices cropped to the segmentation's footprint in each plane. Unlike the 3D version, this runs on the image or the segmentation, not both at once (img_or_seg is "img" by default). slice_number is the same 1-based (X, Y, Z) tuple.

Notes on orientation

The processed arrays (mutated_img_data and friends) are in proper RAS orientation. The slices produced by display_slice and return_slice add a 90-degree flip on the Z axis so they read naturally in matplotlib (similar to how ITK displays them). In short: use the instance arrays when you need correct anatomical orientation, and use return_slice when you want something that looks right on screen.

PyPI link: PyPI

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

easy_medical_images-1.0.1.tar.gz (46.0 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

easy_medical_images-1.0.1-cp314-cp314-win_amd64.whl (621.5 kB view details)

Uploaded CPython 3.14Windows x86-64

easy_medical_images-1.0.1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (226.4 kB view details)

Uploaded CPython 3.14manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

easy_medical_images-1.0.1-cp314-cp314-macosx_11_0_arm64.whl (126.5 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

easy_medical_images-1.0.1-cp314-cp314-macosx_10_15_x86_64.whl (132.8 kB view details)

Uploaded CPython 3.14macOS 10.15+ x86-64

easy_medical_images-1.0.1-cp313-cp313-win_amd64.whl (595.9 kB view details)

Uploaded CPython 3.13Windows x86-64

easy_medical_images-1.0.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (226.4 kB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

easy_medical_images-1.0.1-cp313-cp313-macosx_11_0_arm64.whl (126.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

easy_medical_images-1.0.1-cp313-cp313-macosx_10_15_x86_64.whl (132.6 kB view details)

Uploaded CPython 3.13macOS 10.15+ x86-64

easy_medical_images-1.0.1-cp312-cp312-win_amd64.whl (595.9 kB view details)

Uploaded CPython 3.12Windows x86-64

easy_medical_images-1.0.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (226.6 kB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

easy_medical_images-1.0.1-cp312-cp312-macosx_11_0_arm64.whl (126.3 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

easy_medical_images-1.0.1-cp312-cp312-macosx_10_15_x86_64.whl (132.6 kB view details)

Uploaded CPython 3.12macOS 10.15+ x86-64

File details

Details for the file easy_medical_images-1.0.1.tar.gz.

File metadata

  • Download URL: easy_medical_images-1.0.1.tar.gz
  • Upload date:
  • Size: 46.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for easy_medical_images-1.0.1.tar.gz
Algorithm Hash digest
SHA256 b0f86c5f76ce4f7f5935c4dacc3101573f5ac2c7b213f3dc069f1067d4ba4c6b
MD5 3a8a539f82f4ae748985f15f790fc989
BLAKE2b-256 ae5646785cff86d8e678632513455a7662dc61cd84e4d1d783d7856d2ef6ce1b

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.1.tar.gz:

Publisher: publish.yml on AIM-HI-Lab/EasyMedicalImages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file easy_medical_images-1.0.1-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 9346d820fc91838ee0c2302ba6e4848c0f1944bdac8eb3855603b1f5dd0b7cc5
MD5 68cf5c22a967d0a090a6d28288a574e5
BLAKE2b-256 4341f691c2d98e7aa61173720dbea42aa7cf7ea82288d434f318da6b1839a3cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.1-cp314-cp314-win_amd64.whl:

Publisher: publish.yml on AIM-HI-Lab/EasyMedicalImages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file easy_medical_images-1.0.1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ee5390ea548c9317c7611716a6cca7dac1965e16a912cdb87bdd2008518f7e7c
MD5 1882cf67bab9c0f92dc493fc0d65e8a1
BLAKE2b-256 32954696e87a4d2632e64897ac614cc648b232147a148c9d1653d3d2febdd40c

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish.yml on AIM-HI-Lab/EasyMedicalImages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file easy_medical_images-1.0.1-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.1-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 92be29de02c738b5f2d0e6ac3b8a1fc957467bb1a630e4b3d7a96a828175513f
MD5 68658def604b6cb81582e92b0b0df9e3
BLAKE2b-256 7efe01ce3da1afce71eed56d18a9354d2ccec936e02209ac65ad49ff4ad698bd

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.1-cp314-cp314-macosx_11_0_arm64.whl:

Publisher: publish.yml on AIM-HI-Lab/EasyMedicalImages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file easy_medical_images-1.0.1-cp314-cp314-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.1-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 0698a3f6cb5e3d2f4f39c32a752c8b452c6e30cbcc47b69fadd7bf61aa4682ec
MD5 605788713bbcc66db57d5e9a38df9aaf
BLAKE2b-256 b1782834250af88225c7915a461bd7af1d9a49392beac75219deb0ab486ae7ee

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.1-cp314-cp314-macosx_10_15_x86_64.whl:

Publisher: publish.yml on AIM-HI-Lab/EasyMedicalImages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file easy_medical_images-1.0.1-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 1650ddd29dcecad29465d6cf6779ddba6a4a2a070e66308003dea319655f5cd8
MD5 f06e2d9ed319774d07ffe659d2d0abd1
BLAKE2b-256 fbcffbbeae23baaecc8926f1216081918433dfeeaaef6240fd77545f371633f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.1-cp313-cp313-win_amd64.whl:

Publisher: publish.yml on AIM-HI-Lab/EasyMedicalImages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file easy_medical_images-1.0.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 03723cc6c105cbcf791494943f9bbf923c17268422700db9cad4128255d02058
MD5 5817f04e7a39cbb9d0dbd28c355d51f6
BLAKE2b-256 e2f076ad2d5e2db0a0c4ee45293f0d37b8c437100a48569b6c83160a23e4c1b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish.yml on AIM-HI-Lab/EasyMedicalImages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file easy_medical_images-1.0.1-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 498ebf6479bdd7be452c28517a48a6a3aa6d3c820cfc17f45b058cf8f39fb891
MD5 2a62872e13ae5b35eb5e47b40cd16e71
BLAKE2b-256 923546b5a3202a1eea83f1141b57cfa2b02bc8f1aed390e47f991ca8a63492db

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.1-cp313-cp313-macosx_11_0_arm64.whl:

Publisher: publish.yml on AIM-HI-Lab/EasyMedicalImages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file easy_medical_images-1.0.1-cp313-cp313-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.1-cp313-cp313-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 5df0d4c55de41549c1ff75b9781a4942fc0d760f8ca9f5be71694bb0f0febe88
MD5 899aa6dfe5b3d1f3b1b003b0a75f01c4
BLAKE2b-256 509de2b01c50ada93b4595f70490fbdbb0cd747c76d80fede3ac1d87d8338067

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.1-cp313-cp313-macosx_10_15_x86_64.whl:

Publisher: publish.yml on AIM-HI-Lab/EasyMedicalImages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file easy_medical_images-1.0.1-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 ee10a07448b29d9efa255d7c471104ba5aaebfa6f2528acef3a7b69a54bba058
MD5 51b2664680407867d01237b0a8092fc9
BLAKE2b-256 e0651325b6118345e866070a2a832bc0bcab37e4aff9adb050d8b222bdc92fe9

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.1-cp312-cp312-win_amd64.whl:

Publisher: publish.yml on AIM-HI-Lab/EasyMedicalImages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file easy_medical_images-1.0.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5a5b1142fa2e88539cff015928302f96d9f21f8ce84d025616d9372ccb1079cd
MD5 1101105ff5778029e8394c7ed12969d9
BLAKE2b-256 f9e632265c4a9c01374c7048ad664c4a11b182f6b90259eb017e976a12a8a9de

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: publish.yml on AIM-HI-Lab/EasyMedicalImages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file easy_medical_images-1.0.1-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 947030f757a867e0abf4a06f72669e4893abdcf771d8757103bc96d390894603
MD5 243c53fb48048b341f1c703e793a95af
BLAKE2b-256 ce079321091f010185452fd6c0228b0126f1ba6aa8d90aff95bc17ec40d20289

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.1-cp312-cp312-macosx_11_0_arm64.whl:

Publisher: publish.yml on AIM-HI-Lab/EasyMedicalImages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file easy_medical_images-1.0.1-cp312-cp312-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.1-cp312-cp312-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 a7d441e0b19a9324b145c0c09141ba8ce5611a789409741a1269a07f5b18bf94
MD5 5ecb381804936066af56ced27528f105
BLAKE2b-256 fe20fd0e58cc15580228c56465aa803fba2f173513be9d481d569621a0d4bd37

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.1-cp312-cp312-macosx_10_15_x86_64.whl:

Publisher: publish.yml on AIM-HI-Lab/EasyMedicalImages

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page