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.4a2.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-0.0.4a2-cp314-cp314-win_amd64.whl (621.1 kB view details)

Uploaded CPython 3.14Windows x86-64

easy_medical_images-0.0.4a2-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (225.9 kB view details)

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

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

Uploaded CPython 3.14macOS 11.0+ ARM64

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

Uploaded CPython 3.14macOS 10.15+ x86-64

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

Uploaded CPython 3.13Windows x86-64

easy_medical_images-0.0.4a2-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-0.0.4a2-cp313-cp313-macosx_11_0_arm64.whl (125.6 kB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

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

Uploaded CPython 3.13macOS 10.15+ x86-64

easy_medical_images-0.0.4a2-cp312-cp312-win_amd64.whl (595.5 kB view details)

Uploaded CPython 3.12Windows x86-64

easy_medical_images-0.0.4a2-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-0.0.4a2-cp312-cp312-macosx_11_0_arm64.whl (125.6 kB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

easy_medical_images-0.0.4a2-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-0.0.4a2.tar.gz.

File metadata

  • Download URL: easy_medical_images-0.0.4a2.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-0.0.4a2.tar.gz
Algorithm Hash digest
SHA256 02dd24e2e71c09ce35057ee137a82c0b5257de0227d63aedc0e9c39ade6e59d2
MD5 7e266395b1db0e5cbd3c464aefabd3cc
BLAKE2b-256 a483fd5b5e5a0bacf4e5a96c9e7badfdca68d6b5b8191c0e5e03c1012437224d

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a2-cp314-cp314-win_amd64.whl
Algorithm Hash digest
SHA256 5b43534fec7120ccdb8245b12d8a1abf39d72862eba79d1a64bfa9fdd116f35c
MD5 93ff677d15700987659ffd445373f6c8
BLAKE2b-256 673979cff58d4129d47668c8ce38d580aa2bdee0e46ddb72b580536f126c5041

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a2-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 5a9fc8595f1db1625a6cda71919df07a3d2119f13878081fed54114a019877e0
MD5 584f0cfdff9caa805281e6afa286e941
BLAKE2b-256 6a537d07fc56959b95c1628ad236d5358bc1d5d27e264ab4647ee37b5e889eb2

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a2-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 6027e2de1401e73a11a99764924f64a8e3d7c95b3b83b270991057d87bceaba3
MD5 09c35436d3e7bddb5c524f3113d65e67
BLAKE2b-256 d35a012974003d57f2869af19c784df8c58bcf6a4b34508d7f788d57c5ff39e1

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a2-cp314-cp314-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 7ce6b565f05e46ea759729ca4427513d18a951758f565c993c295f883f009bf8
MD5 d239b176348a18751c72c36044c1fc1e
BLAKE2b-256 33b6b6e2c9460f96046d0f9ce8b6b21d8328d873f6771ed5e0d7a5a195e0604e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a2-cp313-cp313-win_amd64.whl
Algorithm Hash digest
SHA256 20c31d34decc0ae8432123060cac7473c5a20a91f4e7b4df67d2352c9400dcaf
MD5 a03335ebd475ecb8db3bcd7a8a9c04e8
BLAKE2b-256 2548221f85145142c7db2f5792ef942da6879a00c348dad26091c3ea042acd56

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a2-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4b7c1b6b572dbc335771a89ff505845a4033fa668290104e847e72198818fbda
MD5 c12d65502edcf66ce7fc51890dc90ae0
BLAKE2b-256 6520f29a8f1e1393605f0b796ed8bbf953d91c3569d52820ee93f7800374375a

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d510e72ea1942445dd599c9da2e3d3a3ac05a27de5f6d029f8be4a82fe1b75a7
MD5 dc92c0a21d423fcd6f29363f06375db4
BLAKE2b-256 7b5d27d268b7c49e4cfd3fcc1bf96b0c5ec81ba169bef7bb337b29f67382d507

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a2-cp313-cp313-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 1b3fec1b6621533ac43690bc19889b203e8dc8e87e8dba0b9dc396c3b630c632
MD5 82595ab260ec3b589666db6693d5c484
BLAKE2b-256 6d9b468ed2782c8d590ede38d220d3bcabb90c44dab992fce59f849edadb9f77

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a2-cp312-cp312-win_amd64.whl
Algorithm Hash digest
SHA256 4fb7b94ae58f58b714d24f860a1f6dda956cbdd4155e168ca895c713daa2403c
MD5 ee57a7593a170f2ac27e0725221d6622
BLAKE2b-256 33d657ce47a09a16678383ddf8fb677077666d90d607619dbcfafd60aa0befb5

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a2-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4a96286a501fcf55bf9c11b066f1cc56a03c9a539588a0ed3e472de05efc2fc1
MD5 f831e745c5f0cebd5d5bf863e423d776
BLAKE2b-256 edc2c52dfbaf35108cd906d5d19f80612742586be0963486843a3230ba90edfe

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 5740c0a73f442e417981a3dae25df5c4dd9da918b65cb32d8cd5194e0b28dced
MD5 18182fac85588a56695f95864001315c
BLAKE2b-256 93b2f0252818627c8c5972d1118f9bdc3d88711f38cab154918ea23213e9bd3e

See more details on using hashes here.

Provenance

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

File metadata

File hashes

Hashes for easy_medical_images-0.0.4a2-cp312-cp312-macosx_10_15_x86_64.whl
Algorithm Hash digest
SHA256 914791acdf28c6005622ad3c542c887310aecde4604699d44ca4db8d2703dacc
MD5 3a7fd579b9c73ce72b3042ec033e9404
BLAKE2b-256 4b7eebf9350039695f0b78af5fe8065ed85912d283f42f8adea9bab32221f268

See more details on using hashes here.

Provenance

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