pymedio package

Submodules

pymedio.base module

Base image class for image containers Author: Jacob Reinhold <jcreinhold@gmail.com>

class pymedio.base.BasicImage(data: npt.ArrayLike, affine: npt.NDArray | None = None, *, info: str | npt.NDArray[np.str_] | None = None, copy: bool = False)[source]

Bases: ndarray[Any, dtype[DType]]

property affine: ndarray[Any, dtype[float64]]
property direction: tuple[Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float]] | tuple[Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float]]
classmethod from_npz(file: miot.PathLike | BinaryIO, **np_load_kwargs) _Image[source]
get_spacing_string() str[source]
property info: ndarray[Any, dtype[str_]]
property memory: integer[T] | int

Number of bytes that the image array occupies in RAM

property origin: tuple[Union[numpy.floating[T], float], ...]

Center of first voxel in array, in mm.

property repr_properties: list[str]
resample_image(shape: Sequence[int]) _Image[source]
property spacing: tuple[Union[numpy.floating[T], float], ...]

Voxel spacing in mm.

to_npz(file: miot.PathLike | BinaryIO) None[source]
torch_compatible() ndarray[Any, dtype[ScalarType]][source]

pymedio.dicom module

DICOM-specific functions inspired by dicom-numpy: https://github.com/innolitics/dicom-numpy Author: Jacob Reinhold <jcreinhold@gmail.com>

class pymedio.dicom.DICOMDir(slices: 'tuple[pydicom.Dataset, ...]', positions: 'tuple[float, ...]', slice_spacing: 'float', affine: 'npt.NDArray', paths: 'tuple[miot.PathLike, ...] | None' = None)[source]

Bases: object

affine: npt.NDArray
static dicom_datasets_from_zip(zip_file: ZipFile) list[pydicom.Dataset][source]
classmethod from_datasets(datasets: Sequence[pydicom.Dataset], *, paths: Sequence[miot.PathLike] | None = None, max_nonuniformity: float = 0.0005, fail_outside_max_nonuniformity: bool = True, remove_anomalous_images: bool = True) DICOMDir[source]
classmethod from_path(dicom_path: miot.PathLike | Iterable[miot.PathLike], *, max_nonuniformity: float = 0.0005, fail_outside_max_nonuniformity: bool = True, remove_anomalous_images: bool = True, defer_size: str | int | None = '1 KB', extension: str = '.dcm') DICOMDir[source]
classmethod from_zipped_stream(data_stream: IO, *, max_nonuniformity: float = 0.0005, fail_outside_max_nonuniformity: bool = True, remove_anomalous_images: bool = True, encryption_key: bytes | str | None = None, **zip_kwargs: Any) DICOMDir[source]
paths: tuple[miot.PathLike, ...] | None = None
positions: tuple[float, ...]
slice_spacing: float
slices: tuple[pydicom.Dataset, ...]
validate() None[source]
writable(value: bool, /) None[source]
class pymedio.dicom.DICOMImage(data: npt.ArrayLike, affine: npt.NDArray | None = None, *, info: str | npt.NDArray[np.str_] | None = None, copy: bool = False)[source]

Bases: BasicImage[Any, DType]

classmethod from_dicomdir(dicom_dir: DICOMDir, *, rescale: bool | None = None, rescale_dtype: Type[miot.DType] | None = None, order: Literal['F', 'C'] | None = None) DICOMImage[miot.DType][source]
classmethod from_path(dicom_path: miot.PathLike | Iterable[miot.PathLike], *, max_nonuniformity: float = 0.0005, fail_outside_max_nonuniformity: bool = True, remove_anomalous_images: bool = True, rescale: bool | None = None, rescale_dtype: Type[miot.DType] | None = None, order: Literal['F', 'C'] | None = None, extension: str = '.dcm') DICOMImage[miot.DType][source]
classmethod from_zipped_stream(data_stream: IO, *, max_nonuniformity: float = 0.0005, fail_outside_max_nonuniformity: bool = True, remove_anomalous_images: bool = True, encryption_key: bytes | str | None = None, rescale: bool | None = None, rescale_dtype: Type[miot.DType] | None = None, order: Literal['F', 'C'] | None = None, **zip_kwargs: Any) DICOMImage[miot.DType][source]
pymedio.dicom.gather_dicom(dicom_path: miot.PathLike | Iterable[miot.PathLike], *, defer_size: str | int | None = '1 KB', extension: str = '.dcm', return_paths: bool = False) Datasets | tuple[Datasets, tuple[miot.PathLike, ...]][source]

pymedio.exceptions module

Project exceptions Author: Jacob Reinhold <jcreinhold@gmail.com>

exception pymedio.exceptions.DicomImportException[source]

Bases: MedIOException

exception pymedio.exceptions.MedIOException[source]

Bases: Exception

exception pymedio.exceptions.MissingSlicesException[source]

Bases: MedIOException

exception pymedio.exceptions.OutsideMaxNonUniformity[source]

Bases: MedIOException

pymedio.functional module

Functions for reading medical images Taken from torchio and modified to output np.ndarray https://github.com/fepegar/torchio Author: Jacob Reinhold <jcreinhold@gmail.com>

pymedio.functional.read_affine(path: str | PathLike) ndarray[Any, dtype[float64]][source]
pymedio.functional.read_image(path: miot.PathLike, *, dtype: Type[miot.DType] | None = None, eager: bool = True) miot.DataAffine[miot.DType][source]
pymedio.functional.read_matrix(path: str | PathLike) ndarray[Any, dtype[float64]][source]

Read an affine transform and return array

pymedio.functional.write_image(array: ndarray[Any, dtype[ScalarType]], affine: ndarray[Any, dtype[ScalarType]], path: str | PathLike, *, squeeze: bool = True, **write_sitk_kwargs: bool) None[source]
pymedio.functional.write_matrix(matrix: ndarray[Any, dtype[ScalarType]], path: str | PathLike) None[source]

Write an affine transform.

pymedio.image module

Image class to hold a medical image Taken from torchio and modified to use eager load and use np.ndarray instead of torch.tensor https://github.com/fepegar/torchio Author: Jacob Reinhold <jcreinhold@gmail.com>

class pymedio.image.Image(data: npt.ArrayLike, affine: npt.NDArray | None = None, *, info: str | npt.NDArray[np.str_] | None = None, copy: bool = False)[source]

Bases: BasicImage[Any, DType]

axis_name_to_index(axis: str) int[source]
property bounds: ndarray[Any, dtype[float64]]

Position of centers of voxels in smallest and largest indices.

static flip_axis(axis: str) str[source]
classmethod from_dicom_image(dicom_image: DICOMImage) Image[source]
classmethod from_dicom_zipped_stream(data_stream: IO, *, max_nonuniformity: float = 0.0005, fail_outside_max_nonuniformity: bool = True, remove_anomalous_images: bool = True, encryption_key: bytes | str | None = None, rescale: bool | None = None, rescale_dtype: Type[miot.DType] | None = None) Image[miot.DType][source]
classmethod from_path(path: miot.PathLike, *, dtype: Type[miot.DType] | None = None, eager: bool = True) Image[miot.DType][source]
classmethod from_sitk(sitk_image: sitk.Image, *, dtype: Type[miot.DType] | None = None) Image[source]
classmethod from_stream(data_stream: IO, *, dtype: Type[miot.DType] | None = None, gzipped: bool = False, image_class: miof.NibabelImageClass | None = None) Image[miot.DType][source]
classmethod from_zipped_stream(data_stream: IO, *, dtype: Type[miot.DType] | None = None, gzipped: bool = False, image_class: miof.NibabelImageClass | None = None, **zip_kwargs: Any) Image[miot.DType][source]
get_bounds() tuple[tuple[Union[numpy.floating[T], float], Union[numpy.floating[T], float]], tuple[Union[numpy.floating[T], float], Union[numpy.floating[T], float]], tuple[Union[numpy.floating[T], float], Union[numpy.floating[T], float]]][source]

Get minimum and maximum world coordinates occupied by the image.

get_center(lps: bool = False) tuple[Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float]][source]

Get image center in RAS+ or LPS+ coordinates

property orientation: tuple[str, str, str]

Orientation codes.

property repr_properties: list[str]
save(path: str | PathLike, *, squeeze: bool = True) None[source]
to_filename(path: str | PathLike) None[source]
to_nibabel() nibabel.nifti1.Nifti1Image[source]
to_sitk(**kwargs: bool) SimpleITK.Image[source]

Get the image as an instance of sitk.Image.

pymedio.typing module

medio types for type annotations Author: Jacob Reinhold <jcreinhold@gmail.com>

pymedio.typing.Bounds

alias of tuple[tuple[Union[floating[T], float], Union[floating[T], float]], tuple[Union[floating[T], float], Union[floating[T], float]], tuple[Union[floating[T], float], Union[floating[T], float]]]

pymedio.typing.DataAffine

alias of tuple[ndarray[Any, dtype[DType]], ndarray[Any, dtype[float64]]]

pymedio.typing.Shape

alias of tuple[Union[integer[T], int], …]

class pymedio.typing.SupportsArray(*args, **kwargs)[source]

Bases: Protocol[DType]

pymedio.typing.TripletFloat

alias of tuple[Union[floating[T], float], Union[floating[T], float], Union[floating[T], float]]

pymedio.utils module

miscellaneous project functions Author: Jacob Reinhold <jcreinhold@gmail.com>

pymedio.utils.check_uint_to_int(array: ndarray[Any, dtype[ScalarType]]) ndarray[Any, dtype[ScalarType]][source]

convert to int b/c PyTorch won’t take uint16 nor uint32

pymedio.utils.ensure_4d(array: npt.NDArray[miot.DType], *, num_spatial_dims: builtins.int | None = None) npt.NDArray[miot.DType][source]

for PyTorch

pymedio.utils.flipxy_33() ndarray[Any, dtype[float64]][source]
pymedio.utils.flipxy_44() ndarray[Any, dtype[float64]][source]
pymedio.utils.get_metadata_from_ras_affine(affine: ndarray[Any, dtype[ScalarType]], *, is_2d: bool = False, lps: bool = True) tuple[tuple[Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float]], tuple[Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float]], Union[tuple[Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float]], tuple[Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float], Union[numpy.floating[T], float]]]][source]
pymedio.utils.get_rotation_and_spacing_from_affine(affine: ndarray[Any, dtype[DType]]) tuple[numpy.ndarray[typing.Any, numpy.dtype[+DType]], numpy.ndarray[typing.Any, numpy.dtype[+DType]]][source]
pymedio.utils.is_iterable(x: Any) bool[source]
pymedio.utils.to_f64(seq: _SupportsArray[dtype] | _NestedSequence[_SupportsArray[dtype]] | bool | int | float | complex | str | bytes | _NestedSequence[bool | int | float | complex | str | bytes]) ndarray[Any, dtype[float64]][source]

convert a sequence to a float64 array

pymedio.utils.unzip(zipped_list: Iterable[tuple[Any, ...]]) Iterable[tuple[Any, ...]][source]

Module contents

Top-level package for pymedio.