Skip to main content

The easy way to manipulate medical images.

Project description

EasyMedicalImages

The easy way to manipulate medical images

Do not install yet! Still a work in progress.

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
└── ...

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]

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-0.0.4a1.tar.gz (44.8 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-0.0.4a1-cp314-cp314-win_amd64.whl (620.7 kB view details)

Uploaded CPython 3.14Windows x86-64

easy_medical_images-0.0.4a1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (225.6 kB view details)

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

easy_medical_images-0.0.4a1-cp314-cp314-macosx_11_0_arm64.whl (125.5 kB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

easy_medical_images-0.0.4a1-cp314-cp314-macosx_10_15_x86_64.whl (132.0 kB view details)

Uploaded CPython 3.14macOS 10.15+ x86-64

easy_medical_images-0.0.4a1-cp313-cp313-win_amd64.whl (595.2 kB view details)

Uploaded CPython 3.13Windows x86-64

easy_medical_images-0.0.4a1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (225.6 kB view details)

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

easy_medical_images-0.0.4a1-cp313-cp313-macosx_11_0_arm64.whl (125.3 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

easy_medical_images-0.0.4a1-cp313-cp313-macosx_10_15_x86_64.whl (131.8 kB view details)

Uploaded CPython 3.13macOS 10.15+ x86-64

easy_medical_images-0.0.4a1-cp312-cp312-win_amd64.whl (595.2 kB view details)

Uploaded CPython 3.12Windows x86-64

easy_medical_images-0.0.4a1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (225.9 kB view details)

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

easy_medical_images-0.0.4a1-cp312-cp312-macosx_11_0_arm64.whl (125.2 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

easy_medical_images-0.0.4a1-cp312-cp312-macosx_10_15_x86_64.whl (131.8 kB view details)

Uploaded CPython 3.12macOS 10.15+ x86-64

File details

Details for the file easy_medical_images-0.0.4a1.tar.gz.

File metadata

  • Download URL: easy_medical_images-0.0.4a1.tar.gz
  • Upload date:
  • Size: 44.8 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-0.0.4a1.tar.gz
Algorithm Hash digest
SHA256 76f043942d1715f02cae205fc71bb2952d91dd6ae647cea399253d42c589b52d
MD5 28c7b4ddf009366620b8a4c90d916b12
BLAKE2b-256 66f28bc33da167ba79eb3bd0a1d9d2ae41d95010260551de7ce7e30db55ac18c

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a1-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 6cdd554f971a5e001b9f587960f970f16dcad2f3e4bc4e560dee15e522156568
MD5 3a831d4dfd008f9f138493d4b2a7b5b8
BLAKE2b-256 a3af005a6b49e6b2f7355a5acad6500595ac41d2b78b6750600060930d501d1b

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a1-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 7871cf7cb1a6e914a6146f339f94b640d6081f65e3ba03c9fe4bd692cc57b6bc
MD5 63c0ae30148d53152293fd74fd088bd0
BLAKE2b-256 07233bc8f36056a6324fc25b9ba5257241a7f1e01df02aa433cff4d29eeeda1e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a1-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a4cb537e51a2c8d4b0a3d5b9f1a669ce82481c922272493bc92dbdba91c52062
MD5 178d874688e4781149939a308eed2caf
BLAKE2b-256 3c1d4a3906e4afd39036e5c1be9b43f803404e290bff29e775955e536f0dd7a2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a1-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 f79d5ca54a3672f0b7f72d88396ee06b2867cc0efdaeb70bdf445e6125d1fd93
MD5 00ddca8e1868587ed88691c7c25b3fc4
BLAKE2b-256 d4c42c71828e16ba500bbdf345f048c894b4bcc22211d4bfc4bb1fc6ec825401

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a1-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 3e0c43e8ba785743b9d3481b8e3e9a43c8a9a0f85ada86e2c30c9aceab949acc
MD5 a125724496b21c026275b672ea68ec5f
BLAKE2b-256 6fb6a23c9609b40eadc920daeb9f745cf52a18863fdc3b29f0fb1cf40a76cb35

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a1-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 ed7792c4b5778044101c265c3d68077b79ef126355729052121ee22d416bbf6b
MD5 92bfbce5d73b0be9010cf46efd5421b2
BLAKE2b-256 3b0f2bbd297d7a5fc48bb877222556831e2ccc9a8ab31d3395508fee76d37067

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a1-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 b8f9b4037bd1816955d5d35e4e147cb80aea3be090bb6bf7cc1e03b34365a877
MD5 0b39299c1be638f8d12a8e39cfb81432
BLAKE2b-256 bb83f3ef11a0efca37a9e4d5882f25982d5f5c86fe4a8e7f6381d4139fefe604

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a1-cp313-cp313-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 66c01c8448d612db0d40759c09d93f35ff4ff6124549e19b0c6bbc8f8e8ee9c5
MD5 94a87a62755a49392ae278d1dc0d040d
BLAKE2b-256 653e6f640dd48b7450e4ca8f8170c3a4fd11188cfaa5519472779d8b7fe613fc

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a1-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 194a5fb7555fe64ab867172fd26c5420e326d73251a9bb0457c2b357ae440ff4
MD5 9a757ff22c7be5b32248395ab08a2178
BLAKE2b-256 8121a6d258bee2f0f5a7d4a9f3bcc0aed8e1f44a728e290758cfe7f391d23a22

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a1-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3cdd523083837902be3047a85c6fff577f3311c6627b471d8d485cf97c327bf9
MD5 e03df0fbea722df4101fd572fbe395f5
BLAKE2b-256 7d2be9a1a73807c5038fdf99a4b1e819d831de677153a1671a7528449c889a97

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a1-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 479dca45908a3a9c94f8e6196ce117042441fbcd26d651b1ac2d17d9bd2b8fb8
MD5 9ba4bc650bc37688f966c37961486701
BLAKE2b-256 7ed1e64737b8889e87902ff9bd09847b8092246ad27d28c8f81e61e97f42d36a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a1-cp312-cp312-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 edc28b756813176c4219b0b7b562bfc044eab14bd9aa07213084800366e2fa2f
MD5 200a5efebe1d59dbeeff5eb797d08ebf
BLAKE2b-256 e15b52838197a508fcf45521ea0b580bfab8ef4ffbe0a9b43e3bc2188a036f80

See more details on using hashes here.

Provenance

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