Skip to main content

The easy way to manipulate medical images.

Project description

EasyMedicalImages

The easy way to manipulate medical images

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

Project details


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.0.tar.gz (45.1 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.0-cp314-cp314-win_amd64.whl (621.1 kB view details)

Uploaded CPython 3.14Windows x86-64

easy_medical_images-1.0.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (226.0 kB view details)

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

easy_medical_images-1.0.0-cp314-cp314-macosx_11_0_arm64.whl (125.8 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

easy_medical_images-1.0.0-cp314-cp314-macosx_10_15_x86_64.whl (132.3 kB view details)

Uploaded CPython 3.14macOS 10.15+ x86-64

easy_medical_images-1.0.0-cp313-cp313-win_amd64.whl (595.5 kB view details)

Uploaded CPython 3.13Windows x86-64

easy_medical_images-1.0.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (226.0 kB view details)

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

easy_medical_images-1.0.0-cp313-cp313-macosx_11_0_arm64.whl (125.7 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

easy_medical_images-1.0.0-cp313-cp313-macosx_10_15_x86_64.whl (132.2 kB view details)

Uploaded CPython 3.13macOS 10.15+ x86-64

easy_medical_images-1.0.0-cp312-cp312-win_amd64.whl (595.6 kB view details)

Uploaded CPython 3.12Windows x86-64

easy_medical_images-1.0.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (226.2 kB view details)

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

easy_medical_images-1.0.0-cp312-cp312-macosx_11_0_arm64.whl (125.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

easy_medical_images-1.0.0-cp312-cp312-macosx_10_15_x86_64.whl (132.1 kB view details)

Uploaded CPython 3.12macOS 10.15+ x86-64

File details

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

File metadata

  • Download URL: easy_medical_images-1.0.0.tar.gz
  • Upload date:
  • Size: 45.1 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.0.tar.gz
Algorithm Hash digest
SHA256 1648fd7bf5a426992a3dfe2c2517357b7340bcd8dc7ee2d8605e2aa84838c8b5
MD5 e219be35ccd473d3a11083b1b73b5fdc
BLAKE2b-256 cff64d21952524b8f2e03eb96411bfd1a05d8550669a3ade5ce85c6e97cc252f

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.0.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.0-cp314-cp314-win_amd64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.0-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 2e97f5ecb0cc661b9d19a363ec6a32fc433cc668d0a455526974c2b6a743f588
MD5 db064438fe380e215ad11fdcb558a440
BLAKE2b-256 4f23e29b483e27599e2f96f5e6ec0e331456eec07f8413665ae1bd1e0cdaf583

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.0-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.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 78df4292a69f6c6e0998ccdd66d55889072f6524f4dcecb42e8d91b259824b76
MD5 3333b898992be009449c0833459d1391
BLAKE2b-256 9ca571a7b415aa36b10710d701a89a38c8041d1a0b4fa7a9e26b9bd53bd64f73

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.0-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.0-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.0-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 52990d63f7834ca50a2f094b3a97a0f5fb1fe1eea894850713c1ef195efab426
MD5 21efe6690622e67225de134a002d5650
BLAKE2b-256 96916a032e6ddd14c6e9be14391f0183a897087b8ecaeb7ffebe733d5906ed2c

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.0-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.0-cp314-cp314-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.0-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 e7ed74d1431b808211c222a8255364ed28e3495f7e334b19273729d3ad184e85
MD5 12bb78ba97d21182e3d97ed5d17fefee
BLAKE2b-256 38aff32f1340e425e0577fc4ace650e08d69a854481311ba63395d0eef8c8cfa

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.0-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.0-cp313-cp313-win_amd64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.0-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 b7fe4513bd032a35bf81363c95d5bb8c6d474843fc6452259dd24c3780898ead
MD5 389d8fc2c4cc0a780c215e6987945e43
BLAKE2b-256 06762b1e2cf526fca2109d3063210e7736385b41937bff66f4042999767ecad8

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.0-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.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bb410c65ab0a5cd4345141ccb4b17fb995dbca132a821721eadf1b58fceb82c3
MD5 ddd439ae0d7a743369188e22aac19387
BLAKE2b-256 31d52b40e54907e066cb50395cb90d5947ca37bd2b0900ad9133d774feae6374

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.0-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.0-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.0-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 667b20a77f054ab5bba4ebdddd349a6ac4ea5f355493a39d6338d64e89834593
MD5 c93640c9f9377e87150b2dc6ce810e67
BLAKE2b-256 2ceee64d815a9f6de3a168aea1cfbe63028b5d9f63a1985e3ac7cab5556c1958

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.0-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.0-cp313-cp313-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.0-cp313-cp313-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 dcda36ccf9e57b262aa3d3c1c5db444a4fe524afce616e9f605042524bd6823f
MD5 e2873f1f8f78cbe681749360690b4c0c
BLAKE2b-256 aba37b257441af442423a29035e3ec080fc566071c06de7c23d27397e3bb0a1e

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.0-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.0-cp312-cp312-win_amd64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.0-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 5a56e590f8502150715faaf3a68e05489a85acba102b63a34ad92e20b974518a
MD5 337cca0fe01d7605c378ab690431b045
BLAKE2b-256 a92a5f6bae527352bd36400a119f7c47065d993d116ea9af5a84146f5f445201

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.0-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.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 59d2021f733a4bc0872fe190fdd0b7a40581cee90a4973ecc55f024421e0854d
MD5 47aeab6d5ccda42a2fc6a2f4c964770c
BLAKE2b-256 6ba1d0651176e1e2d985ef8856331c886b09bac7633527f6ded76603d058b8b3

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.0-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.0-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.0-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8550c6e47c1b04aceb8b52b9fc1166451dcc8b12c7ec67be642985b2b1426ae1
MD5 cb53d493b2c4b63fafe2a980126dec4f
BLAKE2b-256 9c768ef4fd36514012441f3a14c30f4178fa5a654e1255f2467208e58d7f69d6

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.0-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.0-cp312-cp312-macosx_10_15_x86_64.whl.

File metadata

File hashes

Hashes for easy_medical_images-1.0.0-cp312-cp312-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 4e6ddb90960bdca7a5da68536aed7a8675a06946287d4986cc9aa38805a222d7
MD5 30f78758368ca692ca2fce80653ff6e1
BLAKE2b-256 4b4b0bfb28748aabdb39a546353703eb6b37d12d161a88fe21d736ff74652e45

See more details on using hashes here.

Provenance

The following attestation bundles were made for easy_medical_images-1.0.0-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