Skip to content

Loaders API

pictologics.loader

Image Loading Module

This module handles the loading of medical images from various formats (NIfTI, DICOM) into a standardized Image class. It abstracts away file format differences to provide a consistent interface for the rest of the library.

Key Features:

  • Unified Image Class: Stores 3D data, spacing, origin, direction, and modality.
  • Format Support:
    • NIfTI (.nii, .nii.gz) via nibabel.
    • NRRD (.nrrd, .nhdr, .seg.nrrd) and MetaImage (.mha, .mhd), with the readers of pictologics.loaders.
    • DICOM Series (directory of DICOM files) via pydicom.
    • Single DICOM files.
  • Automatic Detection: load_image automatically detects format and dimensionality.
  • Robust DICOM Sorting: Sorts slices based on spatial position and orientation.

Axis Conventions:

All image arrays are stored in (X, Y, Z) order to match ITK/SimpleITK conventions:

  • X (axis 0): Left-Right direction (columns in DICOM terminology)
  • Y (axis 1): Anterior-Posterior direction (rows in DICOM terminology)
  • Z (axis 2): Superior-Inferior direction (slices)

This differs from raw DICOM and matplotlib conventions:

  • DICOM pixel_array: Returns (Rows, Columns) = (Y, X) for 2D slices
  • Matplotlib imshow: Expects (height, width) = (Y, X)

The loaders handle the necessary axis transformations automatically. The visualization utilities (visualize_slices(), save_slices()) transpose the slices for display.

World Coordinate Frame:

Origin and direction metadata are in the LPS+ world frame (Left, Posterior, Superior) for every format, as in DICOM and ITK/SimpleITK:

  • DICOM (series, single files, SEG): as defined by ImagePositionPatient / ImageOrientationPatient.
  • NIfTI: the affine of the file is in RAS+ (Right, Anterior, Superior), so the loader changes the sign of its X and Y rows, as SimpleITK does.

So an image and its masks can come from different formats. A mask that holds the grid of its reference_image in another voxel order (axes flipped or swapped, as some converters write NIfTI files) is turned to the voxel order of the reference image when it loads.

Image dataclass

A standardized container for 3D medical image data and metadata.

This class serves as the common interface for all image processing operations in the library, abstracting away the differences between file formats like DICOM and NIfTI.

Note

origin and direction are in the LPS+ world frame for every source format (the NIfTI loader converts the RAS+ affine) — see the module docstring ("World Coordinate Frame"). Equality (==) compares object identity: element-wise comparison of the array fields would be ambiguous, so dataclass-generated equality is disabled.

Attributes:

Name Type Description
array NDArray[floating[Any]]

The 3D image data with shape (x, y, z).

spacing tuple[float, float, float]

Voxel spacing in millimeters (mm) along the (x, y, z) axes.

origin tuple[float, float, float]

World coordinates of the image origin (center of the first voxel) in millimeters (mm).

direction Optional[NDArray[floating[Any]]]

3x3 direction cosine matrix defining the orientation of the image axes in world space. Defaults to identity matrix.

modality str

The imaging modality (e.g., 'CT', 'MR', 'PT'). Defaults to 'Unknown'.

source_mask Optional[NDArray[bool_]]

Optional boolean mask indicating which voxels contain valid source data (True) vs sentinel/invalid values (False). When set, preprocessing operations like resampling and filtering will exclude invalid voxels from interpolation/convolution to prevent sentinel value contamination. If None, all voxels are assumed to contain valid data (traditional behavior).

frame_of_reference_uid Optional[str]

The DICOM FrameOfReferenceUID of the scan (DICOM images, SEG and RTSTRUCT masks); None for other formats. A mask that loads onto a reference image with another UID gives a warning.

Example
import numpy as np
from pictologics.loader import Image

array = np.zeros((10, 10, 5), dtype=np.float32)
image = Image(array=array, spacing=(1.0, 1.0, 2.0), origin=(0.0, 0.0, 0.0))
print(image.array.shape)
# (10, 10, 5)
print(image.has_source_mask)
# False
Source code in pictologics/loader.py
@dataclass(eq=False)
class Image:
    """
    A standardized container for 3D medical image data and metadata.

    This class serves as the common interface for all image processing operations
    in the library, abstracting away the differences between file formats like
    DICOM and NIfTI.

    Note:
        ``origin`` and ``direction`` are in the LPS+ world frame for every source
        format (the NIfTI loader converts the RAS+ affine) — see the module
        docstring ("World Coordinate Frame"). Equality (``==``) compares object
        identity: element-wise comparison of the array fields would be ambiguous,
        so dataclass-generated equality is disabled.

    Attributes:
        array (npt.NDArray[np.floating[Any]]): The 3D image data with shape (x, y, z).
        spacing (tuple[float, float, float]): Voxel spacing in millimeters (mm)
            along the (x, y, z) axes.
        origin (tuple[float, float, float]): World coordinates of the image origin
            (center of the first voxel) in millimeters (mm).
        direction (Optional[npt.NDArray[np.floating[Any]]]): 3x3 direction cosine matrix
            defining the orientation of the image axes in world space. Defaults to identity
            matrix.
        modality (str): The imaging modality (e.g., 'CT', 'MR', 'PT'). Defaults to 'Unknown'.
        source_mask (Optional[npt.NDArray[np.bool_]]): Optional boolean mask indicating
            which voxels contain valid source data (True) vs sentinel/invalid values (False).
            When set, preprocessing operations like resampling and filtering will exclude
            invalid voxels from interpolation/convolution to prevent sentinel value contamination.
            If None, all voxels are assumed to contain valid data (traditional behavior).
        frame_of_reference_uid (Optional[str]): The DICOM FrameOfReferenceUID of the scan
            (DICOM images, SEG and RTSTRUCT masks); None for other formats. A mask that
            loads onto a reference image with another UID gives a warning.

    Example:
        ```python
        import numpy as np
        from pictologics.loader import Image

        array = np.zeros((10, 10, 5), dtype=np.float32)
        image = Image(array=array, spacing=(1.0, 1.0, 2.0), origin=(0.0, 0.0, 0.0))
        print(image.array.shape)
        # (10, 10, 5)
        print(image.has_source_mask)
        # False
        ```
    """

    array: npt.NDArray[np.floating[Any]]
    spacing: tuple[float, float, float]
    origin: tuple[float, float, float]
    direction: Optional[npt.NDArray[np.floating[Any]]] = None
    modality: str = "Unknown"
    source_mask: Optional[npt.NDArray[np.bool_]] = None
    frame_of_reference_uid: Optional[str] = None

    @property
    def has_source_mask(self) -> bool:
        """Whether this image has a source validity mask (sentinel values are excluded)."""
        return self.source_mask is not None

    def with_source_mask(
        self,
        mask: "npt.NDArray[np.bool_] | npt.NDArray[np.integer[Any]] | Image",
        copy: bool = True,
    ) -> "Image":
        """
        Return a copy of this image with a source validity mask applied.

        The source mask indicates which voxels contain valid data (True) vs
        sentinel/invalid values (False). When set, spatial operations like
        resampling and filtering will exclude invalid voxels.

        Args:
            mask: Boolean array, integer array (>0 = valid), or Image object.
                  Must have the same shape as the image array.
            copy: Copy the image array (the default). With False, the new image
                  shares the array of this image: no copy of the voxels (419 MB for a
                  512 x 512 x 200 CT). A change to one array then shows in the other.
                  Pictologics does not write into the array of an image.

        Returns:
            New Image with source_mask set.

        Raises:
            ValueError: If mask shape or physical geometry doesn't match image geometry.

        Example:
            ```python
            from pictologics.loader import load_image

            image = load_image("image_with_sentinel.nii.gz")
            roi_mask = load_image("roi_mask.nii.gz")

            # Use ROI mask as source validity mask
            image_with_source = image.with_source_mask(roi_mask)

            # Now resampling will exclude sentinel voxels
            from pictologics.preprocessing import resample_image
            resampled = resample_image(image_with_source, new_spacing=(1, 1, 1))
            ```
        """
        if isinstance(mask, Image):
            _validate_geometry(mask, self, "source mask", "image")
        values: npt.NDArray[Any] = mask.array if isinstance(mask, Image) else mask
        mask_arr: npt.NDArray[np.bool_] = np.greater(values, 0)

        if mask_arr.shape != self.array.shape:
            raise ValueError(
                f"Source mask shape {mask_arr.shape} must match image shape {self.array.shape}"
            )

        return Image(
            array=self.array.copy() if copy else self.array,
            spacing=self.spacing,
            origin=self.origin,
            direction=(self.direction if self.direction is None else self.direction.copy()),
            modality=self.modality,
            source_mask=mask_arr,
            frame_of_reference_uid=self.frame_of_reference_uid,
        )

has_source_mask property

Whether this image has a source validity mask (sentinel values are excluded).

with_source_mask(mask, copy=True)

Return a copy of this image with a source validity mask applied.

The source mask indicates which voxels contain valid data (True) vs sentinel/invalid values (False). When set, spatial operations like resampling and filtering will exclude invalid voxels.

Parameters:

Name Type Description Default
mask 'npt.NDArray[np.bool_] | npt.NDArray[np.integer[Any]] | Image'

Boolean array, integer array (>0 = valid), or Image object. Must have the same shape as the image array.

required
copy bool

Copy the image array (the default). With False, the new image shares the array of this image: no copy of the voxels (419 MB for a 512 x 512 x 200 CT). A change to one array then shows in the other. Pictologics does not write into the array of an image.

True

Returns:

Type Description
'Image'

New Image with source_mask set.

Raises:

Type Description
ValueError

If mask shape or physical geometry doesn't match image geometry.

Example
from pictologics.loader import load_image

image = load_image("image_with_sentinel.nii.gz")
roi_mask = load_image("roi_mask.nii.gz")

# Use ROI mask as source validity mask
image_with_source = image.with_source_mask(roi_mask)

# Now resampling will exclude sentinel voxels
from pictologics.preprocessing import resample_image
resampled = resample_image(image_with_source, new_spacing=(1, 1, 1))
Source code in pictologics/loader.py
def with_source_mask(
    self,
    mask: "npt.NDArray[np.bool_] | npt.NDArray[np.integer[Any]] | Image",
    copy: bool = True,
) -> "Image":
    """
    Return a copy of this image with a source validity mask applied.

    The source mask indicates which voxels contain valid data (True) vs
    sentinel/invalid values (False). When set, spatial operations like
    resampling and filtering will exclude invalid voxels.

    Args:
        mask: Boolean array, integer array (>0 = valid), or Image object.
              Must have the same shape as the image array.
        copy: Copy the image array (the default). With False, the new image
              shares the array of this image: no copy of the voxels (419 MB for a
              512 x 512 x 200 CT). A change to one array then shows in the other.
              Pictologics does not write into the array of an image.

    Returns:
        New Image with source_mask set.

    Raises:
        ValueError: If mask shape or physical geometry doesn't match image geometry.

    Example:
        ```python
        from pictologics.loader import load_image

        image = load_image("image_with_sentinel.nii.gz")
        roi_mask = load_image("roi_mask.nii.gz")

        # Use ROI mask as source validity mask
        image_with_source = image.with_source_mask(roi_mask)

        # Now resampling will exclude sentinel voxels
        from pictologics.preprocessing import resample_image
        resampled = resample_image(image_with_source, new_spacing=(1, 1, 1))
        ```
    """
    if isinstance(mask, Image):
        _validate_geometry(mask, self, "source mask", "image")
    values: npt.NDArray[Any] = mask.array if isinstance(mask, Image) else mask
    mask_arr: npt.NDArray[np.bool_] = np.greater(values, 0)

    if mask_arr.shape != self.array.shape:
        raise ValueError(
            f"Source mask shape {mask_arr.shape} must match image shape {self.array.shape}"
        )

    return Image(
        array=self.array.copy() if copy else self.array,
        spacing=self.spacing,
        origin=self.origin,
        direction=(self.direction if self.direction is None else self.direction.copy()),
        modality=self.modality,
        source_mask=mask_arr,
        frame_of_reference_uid=self.frame_of_reference_uid,
    )

load_image(path, dataset_index=0, recursive=False, reference_image=None, transpose_axes=None, fill_value=0.0, apply_rescale=True, subvoxel_tolerance=0.5, subvoxel_warning_threshold=0.01, min_overlap_fraction=0.5, series_uid=None, suv=None, stored_type=False)

Load a medical image from a file path or directory.

This is the main entry point for loading data. It automatically detects whether the input is a NIfTI file, DICOM directory/file (single DICOM or series), or a DICOM Segmentation (SEG) object and standardizes it into an Image object.

The resulting image array is always 3D with dimensions (x, y, z).

Note

For DICOM SEG files, this function uses pictologics.loaders.load_seg() internally. For more control over segment extraction (e.g., selecting specific segments or extracting them separately), use load_seg() directly. dataset_index and fill_value do not apply to SEG files and are ignored with a UserWarning if set to non-default values.

Note

Every format gives its geometry in the LPS+ world frame (the NIfTI loader converts the RAS+ affine), so an image and its masks can come from different formats. See the module docstring ("World Coordinate Frame").

Parameters:

Name Type Description Default
path str | Path | list | DicomPhaseInfo

The absolute or relative path to the image file (e.g., .nii.gz, .dcm or file with no extension) or the directory containing DICOM files. It can also be the DICOM files of one series: a list of file paths, or a DicomPhaseInfo from get_dicom_phases(), which loads that phase without reading the other files of the folder again.

required
dataset_index int

For multi-volume datasets, specifies which volume to extract (0-indexed). This works for:

  • 4D NIfTI, NRRD and MetaImage files: Selects which time point/volume to load (for a 3D Slicer .seg.nrrd file: the layer of segments).
  • Multi-phase DICOM series: Selects which phase to load (e.g., cardiac phases, temporal positions, echo numbers). Use pictologics.utilities.get_dicom_phases() to discover available phases.
  • Enhanced multiframe DICOM files: Selects the phase when the frames hold more than one volume (frames at repeated positions).

Defaults to 0 (the first volume/phase).

0
recursive bool

If True and path is a directory, recursively searches subdirectories and loads the DICOM series from the folder containing the most DICOM files. Defaults to False.

False
reference_image Optional[Image]

If provided and the loaded image has different dimensions than the reference, it will be repositioned into the reference coordinate space using spatial metadata (origin, spacing). This is useful for loading cropped segmentation masks that need to match a full-sized image. A loaded image whose axes are the reference axes in another order or with other signs (the same grid in another voxel order) is first turned to the voxel order of the reference.

None
transpose_axes tuple[int, int, int] | None

Optional axis transposition to apply before repositioning. Use this if the mask's axis order differs from the reference. E.g., (0, 2, 1) swaps Y and Z axes. Only used when reference_image is provided; when set, repositioning is performed even if the loaded shape already matches the reference.

None
fill_value float

Fill value for regions outside the loaded image when repositioning (default: 0.0). Only used when reference_image is provided.

0.0
apply_rescale bool

If True (default), apply RescaleSlope and RescaleIntercept transformation for DICOM files to convert stored pixel values to real-world values (e.g., Hounsfield Units for CT). The DICOM image is then float64, also without rescale tags, like a NIfTI image (nibabel's get_fdata()). Set to False if you need raw stored values.

True
subvoxel_tolerance float

Maximum permitted fractional-voxel offset when repositioning (default: 0.5). Only used when reference_image is provided. See _position_in_reference for full description.

0.5
subvoxel_warning_threshold float

Fractional-voxel drift above which a UserWarning is emitted during repositioning (default: 0.01). Only used when reference_image is provided.

0.01
min_overlap_fraction float

Minimum fraction of the mask volume that must intersect with the reference image space (default: 0.5). Only used when reference_image is provided.

0.5
series_uid str | None

The SeriesInstanceUID of the series to load when a DICOM folder holds more than one image series (for example two reconstructions of one scan). Without it, such a folder raises an error that lists the series. Only used for DICOM folders.

None
suv str | None

Convert a DICOM PET image (Modality PT) to its standardized uptake value: "bw" (body weight, g/ml), "lbm" (lean body mass by the Janmahasatian formula, g/ml), "lbm_james" (lean body mass by the James formula, g/ml) or "bsa" (body surface area by the Du Bois formula, cm2/ml). The factor follows the QIBA vendor-neutral pseudo-code: it needs attenuation and decay corrected images, and the dose, its half-life and the injection and scan times from the header; the lean body mass and the body surface area also need the height (and the lean body mass the sex). None (default): no conversion.

None
stored_type bool

Keep the stored type of a NIfTI file that has an integer type (int8, uint8, int16, uint16, int32, uint32), native byte order and no scaling (slope 1, intercept 0), for a mask or a label map: the array is then that type instead of float64. Any other NIfTI file, and every other format, loads as without the keyword. The pipeline loads its masks and label maps with it. Default False.

False

Returns:

Name Type Description
Image Image

An Image object containing the 3D numpy array and metadata (spacing, origin, etc.).

Raises:

Type Description
ValueError

If the path does not exist, the file format is not supported, the file is corrupt/unreadable, a DICOM folder holds more than one image series and series_uid does not choose one, or suv is set for an image that is not DICOM PET or lacks an attribute that the SUV needs.

Example

Loading a NIfTI file:

from pictologics.loader import load_image

# Load a standard brain scan
img = load_image("data/brain.nii.gz")
print(f"Image shape: {img.array.shape}")
# Output: Image shape: (256, 256, 128)

Loading a DICOM series:

# Load a CT scan from a folder of DICOM files
img_ct = load_image("data/patients/001/CT_scan/")
print(f"Voxel spacing: {img_ct.spacing}")
# Output: Voxel spacing: (0.97, 0.97, 2.5)

Loading a single DICOM file:

# Load a single DICOM file (even without .dcm extension)
img_slice = load_image("data/slice_001")
print(f"Modality: {img_slice.modality}")

Recursive DICOM loading:

# Finds the deep subfolder with actual DICOM files
img = load_image("data/patients/001/", recursive=True)

Loading a specific volume from a 4D file:

# Load the 5th time point from a 4D fMRI file
fmri_vol = load_image("data/fmri.nii.gz", dataset_index=4)

Loading a cropped mask and repositioning to match main image:

main_img = load_image("ct_scan/")
mask = load_image("cropped_mask.dcm", reference_image=main_img)
# mask now has same shape as main_img

Loading a DICOM SEG file (auto-detected):

# DICOM SEG files are automatically detected and loaded
seg = load_image("segmentation.dcm")
print(f"Modality: {seg.modality}")  # Output: Modality: SEG
# Segments are combined into a label image by default

Loading a specific phase from a multi-phase DICOM series:

from pictologics.utilities import get_dicom_phases

# Discover available phases
phases = get_dicom_phases("cardiac_ct/")
print(f"Found {len(phases)} phases")
for p in phases:
    print(f"  {p.index}: {p.label} ({p.num_slices} slices)")

# Load the 5th phase (40%)
img = load_image("cardiac_ct/", dataset_index=4)

Loading a PET series as SUV (body weight):

pet = load_image("pet_series/", suv="bw")

Source code in pictologics/loader.py
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
def load_image(
    path: str | Path | Sequence[str | Path] | Any,
    dataset_index: int = 0,
    recursive: bool = False,
    reference_image: Optional[Image] = None,
    transpose_axes: tuple[int, int, int] | None = None,
    fill_value: float = 0.0,
    apply_rescale: bool = True,
    subvoxel_tolerance: float = 0.5,
    subvoxel_warning_threshold: float = 0.01,
    min_overlap_fraction: float = 0.5,
    series_uid: Optional[str] = None,
    suv: Optional[str] = None,
    stored_type: bool = False,
) -> Image:
    """
    Load a medical image from a file path or directory.

    This is the main entry point for loading data. It automatically detects whether
    the input is a NIfTI file, DICOM directory/file (single DICOM or series), or
    a DICOM Segmentation (SEG) object and standardizes it into an `Image` object.

    The resulting image array is always 3D with dimensions (x, y, z).

    Note:
        For DICOM SEG files, this function uses ``pictologics.loaders.load_seg()``
        internally. For more control over segment extraction (e.g., selecting specific
        segments or extracting them separately), use ``load_seg()`` directly.
        ``dataset_index`` and ``fill_value`` do not apply to SEG files and are
        ignored with a ``UserWarning`` if set to non-default values.

    Note:
        Every format gives its geometry in the LPS+ world frame (the NIfTI loader
        converts the RAS+ affine), so an image and its masks can come from different
        formats. See the module docstring ("World Coordinate Frame").

    Args:
        path (str | Path | list | DicomPhaseInfo): The absolute or relative path to the
            image file (e.g., .nii.gz, .dcm or file with no extension) or the directory
            containing DICOM files. It can also be the DICOM files of one series: a list
            of file paths, or a ``DicomPhaseInfo`` from ``get_dicom_phases()``, which
            loads that phase without reading the other files of the folder again.
        dataset_index (int, optional): For multi-volume datasets, specifies which
            volume to extract (0-indexed). This works for:

            - **4D NIfTI, NRRD and MetaImage files**: Selects which time point/volume to
              load (for a 3D Slicer ``.seg.nrrd`` file: the layer of segments).
            - **Multi-phase DICOM series**: Selects which phase to load (e.g., cardiac
              phases, temporal positions, echo numbers). Use
              ``pictologics.utilities.get_dicom_phases()`` to discover available phases.
            - **Enhanced multiframe DICOM files**: Selects the phase when the frames
              hold more than one volume (frames at repeated positions).

            Defaults to 0 (the first volume/phase).
        recursive (bool, optional): If True and `path` is a directory, recursively searches
            subdirectories and loads the DICOM series from the folder containing the most
            DICOM files. Defaults to False.
        reference_image (Optional[Image]): If provided and the loaded image has different
            dimensions than the reference, it will be repositioned into the reference
            coordinate space using spatial metadata (origin, spacing). This is useful for
            loading cropped segmentation masks that need to match a full-sized image. A
            loaded image whose axes are the reference axes in another order or with other
            signs (the same grid in another voxel order) is first turned to the voxel
            order of the reference.
        transpose_axes (tuple[int, int, int] | None): Optional axis transposition to apply
            before repositioning. Use this if the mask's axis order differs from the reference.
            E.g., (0, 2, 1) swaps Y and Z axes. Only used when reference_image is provided;
            when set, repositioning is performed even if the loaded shape already matches
            the reference.
        fill_value (float): Fill value for regions outside the loaded image when
            repositioning (default: 0.0). Only used when reference_image is provided.
        apply_rescale (bool): If True (default), apply RescaleSlope and RescaleIntercept
            transformation for DICOM files to convert stored pixel values to real-world
            values (e.g., Hounsfield Units for CT). The DICOM image is then float64, also
            without rescale tags, like a NIfTI image (nibabel's get_fdata()). Set to False
            if you need raw stored values.
        subvoxel_tolerance (float): Maximum permitted fractional-voxel offset when
            repositioning (default: 0.5). Only used when reference_image is provided.
            See ``_position_in_reference`` for full description.
        subvoxel_warning_threshold (float): Fractional-voxel drift above which a
            ``UserWarning`` is emitted during repositioning (default: 0.01). Only used
            when reference_image is provided.
        min_overlap_fraction (float): Minimum fraction of the mask volume that must
            intersect with the reference image space (default: 0.5). Only used when
            reference_image is provided.
        series_uid (str | None): The SeriesInstanceUID of the series to load when a DICOM
            folder holds more than one image series (for example two reconstructions of
            one scan). Without it, such a folder raises an error that lists the series.
            Only used for DICOM folders.
        suv (str | None): Convert a DICOM PET image (Modality PT) to its standardized
            uptake value: ``"bw"`` (body weight, g/ml), ``"lbm"`` (lean body mass by the
            Janmahasatian formula, g/ml), ``"lbm_james"`` (lean body mass by the James
            formula, g/ml) or ``"bsa"`` (body surface area by the Du Bois formula,
            cm2/ml). The factor follows the QIBA vendor-neutral pseudo-code: it needs
            attenuation and decay corrected images, and the dose, its half-life and the
            injection and scan times from the header; the lean body mass and the body
            surface area also need the height (and the lean body mass the sex). None
            (default): no conversion.
        stored_type (bool): Keep the stored type of a NIfTI file that has an integer type
            (int8, uint8, int16, uint16, int32, uint32), native byte order and no scaling
            (slope 1, intercept 0), for a mask or a label map: the array is then that type
            instead of float64. Any other NIfTI file, and every other format, loads as
            without the keyword. The pipeline loads its masks and label maps with it.
            Default False.

    Returns:
        Image: An `Image` object containing the 3D numpy array and metadata (spacing, origin, etc.).

    Raises:
        ValueError: If the path does not exist, the file format is not supported,
            the file is corrupt/unreadable, a DICOM folder holds more than one
            image series and ``series_uid`` does not choose one, or ``suv`` is set
            for an image that is not DICOM PET or lacks an attribute that the SUV needs.

    Example:
        **Loading a NIfTI file:**
        ```python
        from pictologics.loader import load_image

        # Load a standard brain scan
        img = load_image("data/brain.nii.gz")
        print(f"Image shape: {img.array.shape}")
        # Output: Image shape: (256, 256, 128)
        ```

        **Loading a DICOM series:**
        ```python
        # Load a CT scan from a folder of DICOM files
        img_ct = load_image("data/patients/001/CT_scan/")
        print(f"Voxel spacing: {img_ct.spacing}")
        # Output: Voxel spacing: (0.97, 0.97, 2.5)
        ```

        **Loading a single DICOM file:**
        ```python
        # Load a single DICOM file (even without .dcm extension)
        img_slice = load_image("data/slice_001")
        print(f"Modality: {img_slice.modality}")
        ```

        **Recursive DICOM loading:**
        ```python
        # Finds the deep subfolder with actual DICOM files
        img = load_image("data/patients/001/", recursive=True)
        ```

        **Loading a specific volume from a 4D file:**
        ```python
        # Load the 5th time point from a 4D fMRI file
        fmri_vol = load_image("data/fmri.nii.gz", dataset_index=4)
        ```

        **Loading a cropped mask and repositioning to match main image:**
        ```python
        main_img = load_image("ct_scan/")
        mask = load_image("cropped_mask.dcm", reference_image=main_img)
        # mask now has same shape as main_img
        ```

        **Loading a DICOM SEG file (auto-detected):**
        ```python
        # DICOM SEG files are automatically detected and loaded
        seg = load_image("segmentation.dcm")
        print(f"Modality: {seg.modality}")  # Output: Modality: SEG
        # Segments are combined into a label image by default
        ```

        **Loading a specific phase from a multi-phase DICOM series:**
        ```python
        from pictologics.utilities import get_dicom_phases

        # Discover available phases
        phases = get_dicom_phases("cardiac_ct/")
        print(f"Found {len(phases)} phases")
        for p in phases:
            print(f"  {p.index}: {p.label} ({p.num_slices} slices)")

        # Load the 5th phase (40%)
        img = load_image("cardiac_ct/", dataset_index=4)
        ```

        **Loading a PET series as SUV (body weight):**
        ```python
        pet = load_image("pet_series/", suv="bw")
        ```
    """
    if suv is not None and not apply_rescale:
        raise ValueError("suv needs apply_rescale=True (the activity concentrations).")
    phase_files = getattr(path, "file_paths", None)  # a DicomPhaseInfo
    if phase_files is not None or isinstance(path, (list, tuple)):
        loaded_image = _load_dicom_series(
            phase_files if phase_files is not None else path,
            dataset_index,
            apply_rescale,
            series_uid,
            suv,
        )
        if reference_image is not None:
            _validate_geometry(loaded_image, reference_image, "loaded image", "reference image")
        return loaded_image

    path = str(path)
    path_obj = Path(path)
    if not path_obj.exists():
        raise ValueError(f"The specified path does not exist: {path}")

    try:
        if path_obj.is_dir():
            target_path = path_obj
            if recursive:
                target_path = _find_best_dicom_series_dir(path_obj)
            loaded_image = _load_dicom_series(
                target_path, dataset_index, apply_rescale, series_uid, suv
            )
        elif suv is not None and path.lower().endswith(
            (".nii", ".nii.gz", ".nrrd", ".nhdr", ".mha", ".mhd")
        ):
            raise ValueError(f"suv needs a DICOM PET image, not the file '{path}'.")
        elif path.lower().endswith((".nii", ".nii.gz")):
            loaded_image = _load_nifti(path, dataset_index, stored_type)
        elif path.lower().endswith((".nrrd", ".nhdr")):
            from pictologics.loaders.nrrd_loader import _load_nrrd

            loaded_image = _load_nrrd(path, dataset_index)
        elif path.lower().endswith((".mha", ".mhd")):
            from pictologics.loaders.metaimage_loader import _load_metaimage

            loaded_image = _load_metaimage(path, dataset_index)
        else:
            # Attempt to load as a single DICOM file if extension is not NIfTI
            # Check if it's a DICOM SEG or RTSTRUCT file first
            sop_class = _dicom_sop_class(path)  # one header read for the two tests
            if suv is not None and sop_class in (*_SEG_SOP_CLASSES, _RTSTRUCT_SOP_CLASS):
                raise ValueError(f"suv needs a DICOM PET image, not the mask file '{path}'.")
            if sop_class in _SEG_SOP_CLASSES:
                from pictologics.loaders.seg_loader import load_seg

                if dataset_index != 0 or fill_value != 0.0:
                    warnings.warn(
                        "dataset_index and fill_value are ignored for DICOM SEG files. "
                        "Use pictologics.loaders.load_seg() directly for segment selection.",
                        UserWarning,
                        stacklevel=2,
                    )
                seg_result = load_seg(
                    path,
                    reference_image=reference_image,
                    transpose_axes=transpose_axes,
                    subvoxel_tolerance=subvoxel_tolerance,
                    subvoxel_warning_threshold=subvoxel_warning_threshold,
                    min_overlap_fraction=min_overlap_fraction,
                )
                # load_seg gives one label image with its default combine_segments; a dict
                # of masks gives its first mask. load_seg places the mask on the reference.
                if isinstance(seg_result, dict):
                    return next(iter(seg_result.values()))
                return seg_result
            if sop_class == _RTSTRUCT_SOP_CLASS:
                from pictologics.loaders.rtstruct_loader import load_rtstruct

                if reference_image is None:
                    raise ValueError(
                        f"'{path}' is an RTSTRUCT file: its contours need reference_image "
                        "(the image grid to fill)."
                    )
                return cast(
                    Image,
                    load_rtstruct(
                        path,
                        reference_image,
                        subvoxel_tolerance=subvoxel_tolerance,
                        subvoxel_warning_threshold=subvoxel_warning_threshold,
                    ),
                )

            try:
                loaded_image = _load_dicom_file(path, apply_rescale, dataset_index, suv)
            except _DicomContentError:
                raise
            except Exception as read_error:
                raise ValueError(
                    f"Unsupported file format or unable to read file: {path}. Supported: "
                    "NIfTI (.nii, .nii.gz), NRRD (.nrrd, .nhdr, .seg.nrrd), MetaImage (.mha, "
                    ".mhd), and DICOM files and folders (also SEG, and RTSTRUCT with "
                    "reference_image)."
                ) from read_error
    except Exception as e:
        # Re-raise ValueErrors directly, wrap others
        if isinstance(e, ValueError):
            raise e
        raise ValueError(f"Failed to load image from '{path}': {e}") from e

    # Apply repositioning if reference_image is provided and shapes differ,
    # or if an explicit axis transposition was requested (a transposed mask may
    # coincidentally have the same shape as the reference, e.g. cubic volumes).
    if reference_image is not None:
        if transpose_axes is None:
            loaded_image = _reoriented(loaded_image, reference_image)
        # A mask of the shape of the image with another origin (by whole voxels, or by a
        # part of a voxel) is placed as a mask of another shape is
        same_origin = np.allclose(loaded_image.origin, reference_image.origin, atol=1e-5, rtol=1e-5)
        if (
            loaded_image.array.shape != reference_image.array.shape
            or transpose_axes is not None
            or not same_origin
        ):
            loaded_image = _position_in_reference(
                loaded_image,
                reference_image,
                fill_value,
                transpose_axes,
                subvoxel_tolerance,
                subvoxel_warning_threshold,
                min_overlap_fraction,
            )
        else:
            _warn_if_other_frame(loaded_image.frame_of_reference_uid, reference_image, "The image")
            _validate_geometry(loaded_image, reference_image, "loaded image", "reference image")

    return loaded_image

save_image(image, path)

Save an image, a mask or a response map as a NIfTI file (.nii or .nii.gz).

The geometry goes back to the RAS+ affine of NIfTI (the X and Y rows change sign), so load_image reads the same array and geometry. NIfTI keeps the geometry in float32. A bool array is saved as uint8, and a 64-bit integer array as int32 when its values fit (few programs read 64-bit NIfTI integers); other arrays keep their type (float64 images, float32 response maps, uint8 masks).

Parameters:

Name Type Description Default
image Image

The Image to save.

required
path str | Path

The file path, ending with .nii or .nii.gz. A missing folder is made.

required

Raises:

Type Description
ValueError

If the path does not end with .nii or .nii.gz.

Example
from pictologics import load_image, load_rtstruct, save_image

ct = load_image("ct_scan/")
masks = load_rtstruct("rtstruct.dcm", ct, roi_names=["GTV"], combine_rois=False)
save_image(masks["GTV"], "gtv.nii.gz")
Source code in pictologics/loader.py
def save_image(image: Image, path: str | Path) -> None:
    """Save an image, a mask or a response map as a NIfTI file (.nii or .nii.gz).

    The geometry goes back to the RAS+ affine of NIfTI (the X and Y rows change sign),
    so ``load_image`` reads the same array and geometry. NIfTI keeps the geometry in
    float32. A bool array is saved as uint8, and a 64-bit integer array as int32 when its
    values fit (few programs read 64-bit NIfTI integers); other arrays keep their type
    (float64 images, float32 response maps, uint8 masks).

    Args:
        image: The Image to save.
        path: The file path, ending with .nii or .nii.gz. A missing folder is made.

    Raises:
        ValueError: If the path does not end with .nii or .nii.gz.

    Example:
        ```python
        from pictologics import load_image, load_rtstruct, save_image

        ct = load_image("ct_scan/")
        masks = load_rtstruct("rtstruct.dcm", ct, roi_names=["GTV"], combine_rois=False)
        save_image(masks["GTV"], "gtv.nii.gz")
        ```
    """
    path = str(path)
    if not path.lower().endswith((".nii", ".nii.gz")):
        raise ValueError(f"save_image writes NIfTI files (.nii, .nii.gz), not '{path}'.")
    affine = np.eye(4)
    affine[:3, :3] = _direction_matrix(image.direction) * np.asarray(image.spacing, dtype=float)
    affine[:3, 3] = image.origin
    affine[:2] *= -1.0  # LPS+ to RAS+: the X and Y rows change sign
    array: npt.NDArray[Any] = (
        image.array.view(np.uint8) if image.array.dtype == np.bool_ else image.array
    )
    if array.dtype.kind in "iu" and array.dtype.itemsize == 8 and array.size:
        # nibabel refuses 64-bit integers without a dtype (few programs read them): int32
        # when the values fit
        info = np.iinfo(np.int32)
        if info.min <= array.min() and array.max() <= info.max:
            array = array.astype(np.int32)
    if (
        array.ndim == 3
        and array.flags.c_contiguous
        and array.size >= _ROW_ORDER_MIN_SIZE
        and array.dtype in _COLUMN_ORDER_DTYPES
    ):
        # nibabel writes the voxels in column order, one slice at a time. A slice of a
        # row-order array costs a cache line per voxel; a column-order copy is fast.
        column = np.empty(array.shape, dtype=array.dtype, order="F")
        _to_row_order_numba(array, column)
        array = column
    nifti = Nifti1Image(array, affine, dtype=array.dtype)  # type: ignore[no-untyped-call]
    # Code 1: scanner coordinates, as ITK writes
    nifti.set_qform(affine, code=1)  # type: ignore[no-untyped-call]
    nifti.set_sform(affine, code=1)  # type: ignore[no-untyped-call]
    nifti.header.set_xyzt_units("mm")  # type: ignore[no-untyped-call]
    Path(path).parent.mkdir(parents=True, exist_ok=True)
    if (
        path.lower().endswith(".gz")
        and array.ndim == 3
        and array.flags.f_contiguous
        and array.size >= _ROW_ORDER_MIN_SIZE
        and array.dtype in _COLUMN_ORDER_DTYPES
    ):
        _write_gzip_nifti(nifti, array, path)
    else:
        nifti.to_filename(path)

load_and_merge_images(image_paths, reference_image=None, conflict_resolution='max', dataset_index=0, recursive=False, binarize=None, reposition_to_reference=False, transpose_axes=None, fill_value=0.0, relabel_masks=False, apply_rescale=True, subvoxel_tolerance=0.5, subvoxel_warning_threshold=0.01, min_overlap_fraction=0.5)

Load multiple images (e.g., masks or partial scans) and merge them into a single image.

This function loads images from the provided paths, validates that they all share the same geometry (dimensions, spacing, origin, direction), and merges them according to the specified conflict resolution strategy.

Use Cases: - Merging multiple segmentation masks into a single ROI. - Merging split image volumes (though typically less common than mask merging). - Merging cropped/bounding-box segmentation masks (with reposition_to_reference=True).

Format & Path Support: Since this function uses load_image internally for each path, it supports: - NIfTI files (.nii, .nii.gz). - NRRD and MetaImage files (.nrrd, .nhdr, .mha, .mhd). - DICOM series (directories containing DICOM files). - Single DICOM files (with or without .dcm extension). - Nested directories (if paths point to folders containing DICOMs).

Parameters:

Name Type Description Default
image_paths Sequence[str | Path]

List of absolute or relative paths to the images. These can be file paths or directory paths.

required
reference_image Optional[Image]

An optional reference image (e.g., the scan corresponding to the masks). If provided, the merged image is validated against this image's geometry. Required when reposition_to_reference=True.

None
conflict_resolution str

Strategy to resolve voxel values when multiple images have non-zero values at the same location. Options: - 'max': Use the maximum value (default). - 'min': Use the minimum value. - 'first': Keep the value from the first image encountered (earlier in list). - 'last': Overwrite with the value from the last image encountered (later in list).

'max'
dataset_index int

For multi-volume datasets, specifies which volume to extract for all images (0-indexed). This works for:

  • 4D NIfTI, NRRD and MetaImage files: Selects which time point/volume to load (for a 3D Slicer .seg.nrrd file: the layer of segments).
  • Multi-phase DICOM series: Selects which phase to load (e.g., cardiac phases, temporal positions, echo numbers). Use pictologics.utilities.get_dicom_phases() to discover available phases.

Defaults to 0 (the first volume/phase).

0
recursive bool

If True, recursively searches subdirectories for each path in image_paths. Defaults to False.

False
binarize bool | int | list[int] | tuple[int, int] | None

Rules for binarizing the merged image. - None (default): No binarization. - True: Sets all voxels > 0 to 1, others to 0. - int (e.g., 2): Sets voxels == value to 1, others to 0. - list[int] (e.g., [1, 2]): Sets voxels in list to 1, others to 0. - tuple[int, int] (e.g., (1, 10)): Sets voxels in inclusive range to 1, others to 0.

None
reposition_to_reference bool

If True and reference_image is provided, each loaded image will be repositioned into the reference coordinate space before merging. This is required when loading cropped segmentation masks that have different dimensions than the reference. Geometry validation is performed AFTER repositioning. Defaults to False.

False
transpose_axes tuple[int, int, int] | None

Axis transposition to apply when repositioning. E.g., (0, 2, 1) swaps Y and Z axes. Only used when reposition_to_reference=True.

None
fill_value float

Fill value for regions outside cropped masks when repositioning (default: 0.0). Only used when reposition_to_reference=True.

0.0
relabel_masks bool

If True, assigns unique label values (1, 2, 3, ...) to each mask file based on its order in image_paths. This converts binary [0,1] masks into multi-label masks where each file gets a distinct label, useful for visualization with different colors. Label assignment respects the order of image_paths. Defaults to False.

False
apply_rescale bool

If True (default), apply RescaleSlope and RescaleIntercept transformation for DICOM files to convert stored pixel values to real-world values (e.g., Hounsfield Units for CT). Set to False if you need raw stored values.

True
subvoxel_tolerance float

Maximum permitted fractional-voxel offset when repositioning (default: 0.5). Only used when reposition_to_reference=True. See _position_in_reference for full description.

0.5
subvoxel_warning_threshold float

Fractional-voxel drift above which a UserWarning is emitted during repositioning (default: 0.01). Only used when reposition_to_reference=True.

0.01
min_overlap_fraction float

Minimum fraction of each mask volume that must intersect with the reference image space (default: 0.5). Only used when reposition_to_reference=True.

0.5
Note

The binarize parameter is intended for mask filtering (e.g., selecting specific ROI labels). To filter image intensity values (e.g., HU ranges), use the preprocessing steps in the radiomics pipeline configuration instead.

Returns:

Name Type Description
Image Image

A new Image object containing the merged data. Its array has the common type of the loaded arrays (float64 for NIfTI, NRRD and MetaImage files and rescaled DICOM; the stored type of SEG files and of DICOM with apply_rescale=False). With binarize, it is uint8; with relabel_masks, the smallest unsigned type of the labels. With reposition_to_reference, the merge starts as fill_value in the type of the first image, or float64 when that type does not hold fill_value exactly.

Raises:

Type Description
ValueError

If image_paths is empty, if an invalid conflict_resolution is provided, if reposition_to_reference=True but reference_image is not provided, or if the images (or reference) have mismatched geometries.

Example

Merging cropped segmentation masks:

main_img = load_image("ct_scan/", recursive=True)
seg_paths = [str(f) for f in Path("masks/").glob("*.dcm")]

merged = load_and_merge_images(
    seg_paths,
    reference_image=main_img,
    reposition_to_reference=True,
    conflict_resolution="max",
)

Source code in pictologics/loader.py
def load_and_merge_images(
    image_paths: Sequence[str | Path],
    reference_image: Optional[Image] = None,
    conflict_resolution: str = "max",
    dataset_index: int = 0,
    recursive: bool = False,
    binarize: bool | int | list[int] | tuple[int, int] | None = None,
    reposition_to_reference: bool = False,
    transpose_axes: tuple[int, int, int] | None = None,
    fill_value: float = 0.0,
    relabel_masks: bool = False,
    apply_rescale: bool = True,
    subvoxel_tolerance: float = 0.5,
    subvoxel_warning_threshold: float = 0.01,
    min_overlap_fraction: float = 0.5,
) -> Image:
    """
    Load multiple images (e.g., masks or partial scans) and merge them into a single image.

    This function loads images from the provided paths, validates that they all share
    the same geometry (dimensions, spacing, origin, direction), and merges them
    according to the specified conflict resolution strategy.

    **Use Cases:**
    - Merging multiple segmentation masks into a single ROI.
    - Merging split image volumes (though typically less common than mask merging).
    - Merging cropped/bounding-box segmentation masks (with `reposition_to_reference=True`).

    **Format & Path Support:**
    Since this function uses `load_image` internally for each path, it supports:
    - **NIfTI files** (.nii, .nii.gz).
    - **NRRD and MetaImage files** (.nrrd, .nhdr, .mha, .mhd).
    - **DICOM series** (directories containing DICOM files).
    - **Single DICOM files** (with or without .dcm extension).
    - **Nested directories** (if paths point to folders containing DICOMs).

    Args:
        image_paths (Sequence[str | Path]): List of absolute or relative paths to the images.
            These can be file paths or directory paths.
        reference_image (Optional[Image]): An optional reference image (e.g., the scan
            corresponding to the masks). If provided, the merged image is validated
            against this image's geometry. Required when `reposition_to_reference=True`.
        conflict_resolution (str): Strategy to resolve voxel values when multiple images
            have non-zero values at the same location. Options:
            - 'max': Use the maximum value (default).
            - 'min': Use the minimum value.
            - 'first': Keep the value from the first image encountered (earlier in list).
            - 'last': Overwrite with the value from the last image encountered (later in list).
        dataset_index (int, optional): For multi-volume datasets, specifies which
            volume to extract for all images (0-indexed). This works for:

            - **4D NIfTI, NRRD and MetaImage files**: Selects which time point/volume to
              load (for a 3D Slicer ``.seg.nrrd`` file: the layer of segments).
            - **Multi-phase DICOM series**: Selects which phase to load (e.g., cardiac
              phases, temporal positions, echo numbers). Use
              ``pictologics.utilities.get_dicom_phases()`` to discover available phases.

            Defaults to 0 (the first volume/phase).
        recursive (bool, optional): If True, recursively searches subdirectories
            for each path in `image_paths`. Defaults to False.
        binarize (bool | int | list[int] | tuple[int, int] | None, optional):
            Rules for binarizing the merged image.
            - `None` (default): No binarization.
            - `True`: Sets all voxels > 0 to 1, others to 0.
            - `int` (e.g., 2): Sets voxels == value to 1, others to 0.
            - `list[int]` (e.g., [1, 2]): Sets voxels in list to 1, others to 0.
            - `tuple[int, int]` (e.g., (1, 10)): Sets voxels in inclusive range to 1, others to 0.
        reposition_to_reference (bool): If True and reference_image is provided,
            each loaded image will be repositioned into the reference coordinate
            space before merging. This is required when loading cropped segmentation
            masks that have different dimensions than the reference. Geometry validation
            is performed AFTER repositioning. Defaults to False.
        transpose_axes (tuple[int, int, int] | None): Axis transposition to apply
            when repositioning. E.g., (0, 2, 1) swaps Y and Z axes.
            Only used when `reposition_to_reference=True`.
        fill_value (float): Fill value for regions outside cropped masks when
            repositioning (default: 0.0). Only used when `reposition_to_reference=True`.
        relabel_masks (bool): If True, assigns unique label values (1, 2, 3, ...)
            to each mask file based on its order in `image_paths`. This converts
            binary [0,1] masks into multi-label masks where each file gets a
            distinct label, useful for visualization with different colors.
            Label assignment respects the order of `image_paths`. Defaults to False.
        apply_rescale (bool): If True (default), apply RescaleSlope and RescaleIntercept
            transformation for DICOM files to convert stored pixel values to real-world
            values (e.g., Hounsfield Units for CT). Set to False if you need raw stored values.
        subvoxel_tolerance (float): Maximum permitted fractional-voxel offset when
            repositioning (default: 0.5). Only used when ``reposition_to_reference=True``.
            See ``_position_in_reference`` for full description.
        subvoxel_warning_threshold (float): Fractional-voxel drift above which a
            ``UserWarning`` is emitted during repositioning (default: 0.01). Only used
            when ``reposition_to_reference=True``.
        min_overlap_fraction (float): Minimum fraction of each mask volume that must
            intersect with the reference image space (default: 0.5). Only used when
            ``reposition_to_reference=True``.

    Note:
        The `binarize` parameter is intended for **mask filtering** (e.g., selecting specific
        ROI labels). To filter image intensity values (e.g., HU ranges), use the preprocessing
        steps in the radiomics pipeline configuration instead.

    Returns:
        Image: A new `Image` object containing the merged data. Its array has the common
            type of the loaded arrays (float64 for NIfTI, NRRD and MetaImage files and
            rescaled DICOM; the stored type of SEG files and of DICOM with
            ``apply_rescale=False``). With `binarize`, it is uint8; with `relabel_masks`,
            the smallest unsigned type of the labels. With `reposition_to_reference`, the
            merge starts as `fill_value` in the type of the first image, or float64 when
            that type does not hold `fill_value` exactly.

    Raises:
        ValueError: If `image_paths` is empty, if an invalid `conflict_resolution` is provided,
            if `reposition_to_reference=True` but `reference_image` is not provided,
            or if the images (or reference) have mismatched geometries.

    Example:
        **Merging cropped segmentation masks:**
        ```python
        main_img = load_image("ct_scan/", recursive=True)
        seg_paths = [str(f) for f in Path("masks/").glob("*.dcm")]

        merged = load_and_merge_images(
            seg_paths,
            reference_image=main_img,
            reposition_to_reference=True,
            conflict_resolution="max",
        )
        ```
    """
    if not image_paths:
        raise ValueError("image_paths cannot be empty.")

    valid_strategies = {"max", "min", "first", "last"}
    if conflict_resolution not in valid_strategies:
        raise ValueError(
            f"Invalid conflict_resolution '{conflict_resolution}'. "
            f"Must be one of {valid_strategies}."
        )

    if reposition_to_reference and reference_image is None:
        raise ValueError("reference_image must be provided when reposition_to_reference=True.")

    # The labels of relabel_masks: the smallest unsigned type that holds the count of files
    label_type = np.min_scalar_type(len(image_paths))
    merged_array: Optional[npt.NDArray[Any]] = None

    if reposition_to_reference:
        # Mode: Reposition each image to reference space, then merge
        assert reference_image is not None  # Already validated above

        for i, path in enumerate(image_paths):
            try:
                current_image = load_image(
                    path,
                    dataset_index=dataset_index,
                    recursive=recursive,
                    apply_rescale=apply_rescale,
                    stored_type=relabel_masks,
                )
            except Exception as e:
                raise ValueError(f"Failed to load image '{path}': {e}") from e

            # Where the image lies in reference space; outside its box it is all fill,
            # so only the box merges (the repositioned full-size array is not made)
            data, source, target = _placement(
                current_image,
                reference_image,
                transpose_axes,
                subvoxel_tolerance,
                subvoxel_warning_threshold,
                min_overlap_fraction,
            )
            if merged_array is None:  # the merge, in reference geometry and the first type
                kind = _merge_type(label_type if relabel_masks else data.dtype, fill_value)
                merged_array = _filled(reference_image.array.shape, fill_value, kind)
            if source is None or target is None:
                continue
            current_array = data[source]

            # Merge with conflict resolution, in the common type. With relabel_masks,
            # each value other than the fill value merges as the mask index + 1 (the
            # merge already holds these labels).
            label: Any = None
            if relabel_masks:
                label = label_type.type(i + 1)
            else:
                merged_array = _widened(merged_array, current_array)
            _merge_into(merged_array[target], current_array, fill_value, conflict_resolution, label)
            del current_image, data, current_array  # free the image before the next load
        assert merged_array is not None  # image_paths is not empty

        # Use reference geometry for output
        consensus_spacing = reference_image.spacing
        consensus_origin = reference_image.origin
        consensus_direction = reference_image.direction

    else:
        # Mode: Standard merging with strict geometry validation
        # Load the first image to serve as the consensus geometry
        try:
            consensus_image = load_image(
                image_paths[0],
                dataset_index=dataset_index,
                recursive=recursive,
                apply_rescale=apply_rescale,
                stored_type=relabel_masks,
            )
        except Exception as e:
            raise ValueError(f"Failed to load first image '{image_paths[0]}': {e}") from e

        # The merge goes into the array of the first image, which this function loaded
        merged_array = consensus_image.array

        # Apply relabeling for the first image
        if relabel_masks:
            merged_array = (merged_array != 0).astype(label_type)

        # Iterate through remaining images
        for idx, path in enumerate(image_paths[1:], start=2):
            try:
                current_image = load_image(
                    path,
                    dataset_index=dataset_index,
                    recursive=recursive,
                    apply_rescale=apply_rescale,
                    stored_type=relabel_masks,
                )
            except Exception as e:
                raise ValueError(f"Failed to load image '{path}': {e}") from e

            _validate_geometry(current_image, consensus_image, f"image '{path}'", "consensus image")

            current_array = current_image.array

            # Merge with conflict resolution, in the common type. With relabel_masks,
            # each value other than 0 merges as the mask index (from 2 for the second
            # file).
            label = None
            if relabel_masks:
                label = label_type.type(idx)
            else:
                merged_array = _widened(merged_array, current_array)
            _merge_into(merged_array, current_array, 0, conflict_resolution, label)
            del current_image, current_array  # free the image before the next load

        consensus_spacing = consensus_image.spacing
        consensus_origin = consensus_image.origin
        consensus_direction = consensus_image.direction

        # Validate against reference image if provided (for non-reposition mode)
        if reference_image is not None:
            final_merged_image = Image(
                array=merged_array,
                spacing=consensus_spacing,
                origin=consensus_origin,
                direction=consensus_direction,
                modality="Image",
            )
            _validate_geometry(
                final_merged_image, reference_image, "merged image", "reference image"
            )

    # Apply binarization if requested
    if binarize is not None:
        mask_out: npt.NDArray[Any] = np.zeros_like(merged_array, dtype=np.uint8)
        if isinstance(binarize, bool) and binarize is True:
            mask_out[merged_array > 0] = 1
        elif isinstance(binarize, int) and not isinstance(binarize, bool):
            mask_out[merged_array == binarize] = 1
        elif isinstance(binarize, list):
            mask_out[np.isin(merged_array, binarize)] = 1
        elif isinstance(binarize, tuple) and len(binarize) == 2:
            mask_out[(merged_array >= binarize[0]) & (merged_array <= binarize[1])] = 1
        else:
            if binarize is not False:
                raise ValueError(f"Unsupported binarize value: {binarize}")
            mask_out = merged_array

        if binarize is not False:
            merged_array = mask_out

    return Image(
        array=merged_array,
        spacing=consensus_spacing,
        origin=consensus_origin,
        direction=consensus_direction,
        modality="MergedImage",
    )

create_full_mask(reference_image, dtype=np.uint8)

Create a whole-image ROI mask matching a reference image.

This utility is primarily used when a user does not provide a segmentation mask. The returned mask has the same geometry (shape, spacing, origin, direction) as the reference image and contains a value of 1 for every voxel.

Parameters:

Name Type Description Default
reference_image Image

Image whose geometry should be copied.

required
dtype DTypeLike

Numpy dtype to use for the mask array. Defaults to np.uint8.

uint8

Returns:

Type Description
Image

An Image mask with array == 1 everywhere.

Raises:

Type Description
ValueError

If the reference image does not have a valid 3D array.

Example
import numpy as np
from pictologics.loader import Image, create_full_mask

image = Image(array=np.zeros((10, 10, 5)), spacing=(1.0, 1.0, 2.0), origin=(0.0, 0.0, 0.0))
mask = create_full_mask(image)
print(mask.array.shape, mask.array.dtype)
# (10, 10, 5) uint8
print(mask.array.min(), mask.array.max())
# 1 1
Source code in pictologics/loader.py
def create_full_mask(reference_image: Image, dtype: DTypeLike = np.uint8) -> Image:
    """Create a whole-image ROI mask matching a reference image.

    This utility is primarily used when a user does not provide a segmentation mask.
    The returned mask has the same geometry (shape, spacing, origin, direction) as
    the reference image and contains a value of 1 for every voxel.

    Args:
        reference_image: Image whose geometry should be copied.
        dtype: Numpy dtype to use for the mask array. Defaults to `np.uint8`.

    Returns:
        An `Image` mask with `array == 1` everywhere.

    Raises:
        ValueError: If the reference image does not have a valid 3D array.

    Example:
        ```python
        import numpy as np
        from pictologics.loader import Image, create_full_mask

        image = Image(array=np.zeros((10, 10, 5)), spacing=(1.0, 1.0, 2.0), origin=(0.0, 0.0, 0.0))
        mask = create_full_mask(image)
        print(mask.array.shape, mask.array.dtype)
        # (10, 10, 5) uint8
        print(mask.array.min(), mask.array.max())
        # 1 1
        ```
    """
    if reference_image.array.ndim != 3:
        raise ValueError(
            f"reference_image.array must be 3D, got shape {reference_image.array.shape}"
        )

    mask_array = np.ones(reference_image.array.shape, dtype=dtype)
    return Image(
        array=mask_array,
        spacing=reference_image.spacing,
        origin=reference_image.origin,
        direction=reference_image.direction,
        modality="mask",
        frame_of_reference_uid=reference_image.frame_of_reference_uid,
    )

pictologics.loaders.seg_loader

DICOM Segmentation (SEG) Loader

This module provides functionality for loading DICOM Segmentation objects as pictologics Image instances. SEG files are specialized DICOM objects that store segmentation masks with multi-segment support.

The files are read with pydicom, and the frames are decoded one at a time, only for the requested segments.

load_seg(path, segment_numbers=None, combine_segments=True, reference_image=None, transpose_axes=None, subvoxel_tolerance=0.5, subvoxel_warning_threshold=0.01, min_overlap_fraction=0.5, fractional_threshold=0.5)

Load a DICOM SEG file as a mask Image.

This function loads a DICOM Segmentation object and converts it to the standard pictologics Image format. The resulting Image has the same structure as images returned by load_image():

  • array: uint8 (uint16 above 255 labels) with shape (X, Y, Z)
  • spacing: tuple[float, float, float] in mm
  • origin: tuple[float, float, float] in mm
  • direction: Optional[npt.NDArray[np.floating[Any]]] - 3x3 direction cosines
  • modality: str - set to "SEG"

Parameters:

Name Type Description Default
path str | Path

Path to the DICOM SEG file.

required
segment_numbers list[int] | None

Specific segment numbers to extract. If None, all segments are extracted. Segment numbers are 1-indexed as per DICOM convention.

None
combine_segments bool

Controls how segments are returned:

  • True (default): Returns a single Image where each segment is encoded as its segment number (1, 2, 3...) in the voxel values. Background voxels are 0. This is useful when you want a single label map for visualization or when segments are mutually exclusive (e.g., organ segmentation where each voxel belongs to one structure).

  • False: Returns a dict mapping segment numbers to individual binary Image masks. Each mask contains only 0s and 1s. This is useful when:

  • Segments may overlap (e.g., nested structures like tumor within organ)

  • You need to process each segment independently (e.g., extract radiomics from each segment separately)
  • You want to select specific segments for different analyses
True
reference_image 'Image | None'

Optional reference Image for geometry alignment. When provided, the output mask is placed on the grid of the reference (repositioned, not resampled: a mask of another spacing raises an error).

None
transpose_axes tuple[int, int, int] | None

Optional axis transposition to apply before reference alignment.

None
subvoxel_tolerance float

Maximum permitted fractional-voxel offset during reference alignment.

0.5
subvoxel_warning_threshold float

Fractional-voxel drift above which a warning is emitted during reference alignment.

0.01
min_overlap_fraction float

Minimum fraction of mask volume that must overlap the reference image during alignment.

0.5
fractional_threshold float

For a FRACTIONAL SEG (probability or occupancy), a voxel is in a segment when its value is at least this fraction of the MaximumFractionalValue (and above 0). Default 0.5. BINARY and LABELMAP SEGs do not use it.

0.5

Returns:

Type Description
'Image | dict[int, Image]'

If combine_segments is True: A single Image with segment labels; the labels

'Image | dict[int, Image]'

are uint8, or uint16 for segment numbers above 255.

'Image | dict[int, Image]'

If combine_segments is False: A dict of {segment_number: Image}.

Raises:

Type Description
ValueError

If the file is not a valid DICOM SEG object.

FileNotFoundError

If the file does not exist.

Example

Load a SEG file with all segments combined (label map):

from pictologics.loaders import load_seg
import numpy as np

mask = load_seg("segmentation.dcm")
print(mask.array.shape)  # (X, Y, Z)
print(np.unique(mask.array))  # [0, 1, 2, ...]

Load specific segments as separate binary masks:

masks = load_seg("segmentation.dcm", segment_numbers=[1, 2], combine_segments=False)
for seg_num, mask in masks.items():
    print(f"Segment {seg_num}: {mask.array.sum()} voxels")

Align mask to a reference CT image:

from pictologics import load_image

ct = load_image("ct_scan/")
mask = load_seg("segmentation.dcm", reference_image=ct)
assert mask.array.shape == ct.array.shape
Source code in pictologics/loaders/seg_loader.py
def load_seg(
    path: str | Path,
    segment_numbers: list[int] | None = None,
    combine_segments: bool = True,
    reference_image: "Image | None" = None,
    transpose_axes: tuple[int, int, int] | None = None,
    subvoxel_tolerance: float = 0.5,
    subvoxel_warning_threshold: float = 0.01,
    min_overlap_fraction: float = 0.5,
    fractional_threshold: float = 0.5,
) -> "Image | dict[int, Image]":
    """Load a DICOM SEG file as a mask Image.

    This function loads a DICOM Segmentation object and converts it to
    the standard pictologics Image format. The resulting Image has the
    same structure as images returned by load_image():

    - array: uint8 (uint16 above 255 labels) with shape (X, Y, Z)
    - spacing: tuple[float, float, float] in mm
    - origin: tuple[float, float, float] in mm
    - direction: Optional[npt.NDArray[np.floating[Any]]] - 3x3 direction cosines
    - modality: str - set to "SEG"

    Args:
        path: Path to the DICOM SEG file.
        segment_numbers: Specific segment numbers to extract. If None, all
            segments are extracted. Segment numbers are 1-indexed as per
            DICOM convention.
        combine_segments: Controls how segments are returned:

            - **True (default)**: Returns a single Image where each segment
              is encoded as its segment number (1, 2, 3...) in the voxel values.
              Background voxels are 0. This is useful when you want a single
              label map for visualization or when segments are mutually exclusive
              (e.g., organ segmentation where each voxel belongs to one structure).

            - **False**: Returns a dict mapping segment numbers to individual
              binary Image masks. Each mask contains only 0s and 1s. This is
              useful when:

              - Segments may overlap (e.g., nested structures like tumor
                within organ)
              - You need to process each segment independently (e.g., extract
                radiomics from each segment separately)
              - You want to select specific segments for different analyses

        reference_image: Optional reference Image for geometry alignment.
            When provided, the output mask is placed on the grid of the reference
            (repositioned, not resampled: a mask of another spacing raises an error).
        transpose_axes: Optional axis transposition to apply before reference
            alignment.
        subvoxel_tolerance: Maximum permitted fractional-voxel offset during
            reference alignment.
        subvoxel_warning_threshold: Fractional-voxel drift above which a warning
            is emitted during reference alignment.
        min_overlap_fraction: Minimum fraction of mask volume that must overlap
            the reference image during alignment.
        fractional_threshold: For a FRACTIONAL SEG (probability or occupancy), a
            voxel is in a segment when its value is at least this fraction of the
            MaximumFractionalValue (and above 0). Default 0.5. BINARY and LABELMAP
            SEGs do not use it.

    Returns:
        If combine_segments is True: A single Image with segment labels; the labels
        are uint8, or uint16 for segment numbers above 255.
        If combine_segments is False: A dict of {segment_number: Image}.

    Raises:
        ValueError: If the file is not a valid DICOM SEG object.
        FileNotFoundError: If the file does not exist.

    Example:
        Load a SEG file with all segments combined (label map):

        ```python
        from pictologics.loaders import load_seg
        import numpy as np

        mask = load_seg("segmentation.dcm")
        print(mask.array.shape)  # (X, Y, Z)
        print(np.unique(mask.array))  # [0, 1, 2, ...]
        ```

        Load specific segments as separate binary masks:

        ```python
        masks = load_seg("segmentation.dcm", segment_numbers=[1, 2], combine_segments=False)
        for seg_num, mask in masks.items():
            print(f"Segment {seg_num}: {mask.array.sum()} voxels")
        ```

        Align mask to a reference CT image:

        ```python
        from pictologics import load_image

        ct = load_image("ct_scan/")
        mask = load_seg("segmentation.dcm", reference_image=ct)
        assert mask.array.shape == ct.array.shape
        ```
    """
    from pydicom.pixels.utils import iter_pixels

    from pictologics.loader import Image, _frame_uid, _row_order

    if not 0.0 <= fractional_threshold <= 1.0:
        raise ValueError(f"fractional_threshold must be in [0, 1], not {fractional_threshold}.")
    seg = _read_seg(path)

    # Get available segment numbers
    available_segments = [s.SegmentNumber for s in seg.SegmentSequence]

    # Determine which segments to extract. A label map may describe its background as
    # segment 0, which is not a segment to extract by default.
    if segment_numbers is None:
        target_segments = [n for n in available_segments if n != 0 or not _is_labelmap(seg)]
    else:
        # Validate requested segments exist
        for seg_num in segment_numbers:
            if seg_num not in available_segments:
                raise ValueError(
                    f"Segment {seg_num} not found. Available segments: {available_segments}"
                )
        target_segments = segment_numbers

    # Where each frame goes, and the geometry
    n_frames = int(getattr(seg, "NumberOfFrames", 1) or 1)
    layout = _frame_layout(seg, n_frames)
    spacing, origin, direction = _extract_seg_geometry(seg, layout)

    # Decode only the frames of the requested segments (a label map: all frames)
    wanted = set(target_segments)
    indices = [
        i
        for i in range(n_frames)
        if layout.slices[i] < layout.n_slices
        and (_is_labelmap(seg) or layout.segments[i] in wanted)
    ]
    frames = zip(indices, iter_pixels(seg, indices=indices), strict=True)
    level = _inside_level(seg, fractional_threshold)

    if combine_segments:
        # Create combined label image
        combined_array = _extract_combined_segments(seg, frames, target_segments, layout, level)

        # Reorder axes from (Z, Y, X) or (frames, rows, cols) to (X, Y, Z); a large mask
        # goes to row order, like the images.
        combined_array = _row_order(np.transpose(combined_array, (2, 1, 0)))

        result = Image(
            array=combined_array,
            spacing=spacing,
            origin=origin,
            direction=direction,
            modality="SEG",
            frame_of_reference_uid=_frame_uid(seg),
        )

        # Align to reference if provided
        if reference_image is not None:
            result = _align_to_reference(
                result,
                reference_image,
                transpose_axes=transpose_axes,
                subvoxel_tolerance=subvoxel_tolerance,
                subvoxel_warning_threshold=subvoxel_warning_threshold,
                min_overlap_fraction=min_overlap_fraction,
            )

        return result
    else:
        # Return dict of individual segment masks
        result_dict: dict[int, Image] = {}

        masks = _extract_segment_masks(seg, frames, target_segments, layout, level)
        labelmap = _is_labelmap(seg)
        for seg_num in target_segments:
            # The slices of the frames of the segment (of a label map: of all frames)
            planes = [
                layout.slices[i] for i in indices if labelmap or layout.segments[i] == seg_num
            ]
            # Reorder axes from (Z, Y, X) to (X, Y, Z)
            mask_image = Image(
                array=_segment_row_order(
                    masks.pop(seg_num), min(planes, default=0), max(planes, default=-1) + 1
                ),
                spacing=spacing,
                origin=origin,
                direction=direction,
                modality="SEG",
                frame_of_reference_uid=_frame_uid(seg),
            )

            # Align to reference if provided
            if reference_image is not None:
                mask_image = _align_to_reference(
                    mask_image,
                    reference_image,
                    transpose_axes=transpose_axes,
                    subvoxel_tolerance=subvoxel_tolerance,
                    subvoxel_warning_threshold=subvoxel_warning_threshold,
                    min_overlap_fraction=min_overlap_fraction,
                )

            result_dict[seg_num] = mask_image

        return result_dict

get_segment_info(path)

Get information about the segments of a DICOM SEG file, the ROIs of a DICOM RTSTRUCT file, or the segments of a 3D Slicer .seg.nrrd file.

Parameters:

Name Type Description Default
path str | Path

Path to the DICOM SEG file, the RTSTRUCT file, or the .seg.nrrd file.

required

Returns:

Type Description
list[dict[str, str | int]]

List of dicts with segment information. For DICOM SEG:

list[dict[str, str | int]]
  • segment_number: int
list[dict[str, str | int]]
  • segment_label: str
list[dict[str, str | int]]
  • segment_description: str (if available)
list[dict[str, str | int]]
  • algorithm_type: str (if available)
list[dict[str, str | int]]

For RTSTRUCT:

list[dict[str, str | int]]
  • segment_number: int (the ROI Number, the label of load_rtstruct)
list[dict[str, str | int]]
  • segment_label: str (the ROI Name)
list[dict[str, str | int]]
  • contour_count: int (closed planar contours)
list[dict[str, str | int]]
  • interpreted_type: str (RTROIInterpretedType, if available)
list[dict[str, str | int]]

For .seg.nrrd (the fields Segment<N>_... of the header):

list[dict[str, str | int]]
  • segment_index: int (N)
list[dict[str, str | int]]
  • segment_label: str (the segment name)
list[dict[str, str | int]]
  • segment_id: str
list[dict[str, str | int]]
  • label_value: int (the value of the segment in its layer)
list[dict[str, str | int]]
  • layer: int (the volume that load_image(path, dataset_index=layer) gives, when overlapping segments need more than one layer)

Raises:

Type Description
ValueError

If the file is not a valid DICOM SEG or RTSTRUCT object, or a NRRD file without segments.

Example
from pictologics.loaders import get_segment_info

segments = get_segment_info("segmentation.dcm")
for seg in segments:
    print(f"{seg['segment_number']}: {seg['segment_label']}")
Source code in pictologics/loaders/seg_loader.py
def get_segment_info(path: str | Path) -> list[dict[str, str | int]]:
    """Get information about the segments of a DICOM SEG file, the ROIs of a DICOM
    RTSTRUCT file, or the segments of a 3D Slicer .seg.nrrd file.

    Args:
        path: Path to the DICOM SEG file, the RTSTRUCT file, or the .seg.nrrd file.

    Returns:
        List of dicts with segment information. For DICOM SEG:
        - segment_number: int
        - segment_label: str
        - segment_description: str (if available)
        - algorithm_type: str (if available)

        For RTSTRUCT:
        - segment_number: int (the ROI Number, the label of ``load_rtstruct``)
        - segment_label: str (the ROI Name)
        - contour_count: int (closed planar contours)
        - interpreted_type: str (RTROIInterpretedType, if available)

        For .seg.nrrd (the fields ``Segment<N>_...`` of the header):
        - segment_index: int (N)
        - segment_label: str (the segment name)
        - segment_id: str
        - label_value: int (the value of the segment in its layer)
        - layer: int (the volume that ``load_image(path, dataset_index=layer)`` gives,
            when overlapping segments need more than one layer)

    Raises:
        ValueError: If the file is not a valid DICOM SEG or RTSTRUCT object, or a NRRD
            file without segments.

    Example:
        ```python
        from pictologics.loaders import get_segment_info

        segments = get_segment_info("segmentation.dcm")
        for seg in segments:
            print(f"{seg['segment_number']}: {seg['segment_label']}")
        ```
    """
    if str(path).lower().endswith((".nrrd", ".nhdr")):
        from pictologics.loaders.nrrd_loader import _nrrd_segment_info

        return _nrrd_segment_info(path)
    from pictologics.loader import _RTSTRUCT_SOP_CLASS, _dicom_sop_class

    if _dicom_sop_class(str(path)) == _RTSTRUCT_SOP_CLASS:
        from pictologics.loaders.rtstruct_loader import _rtstruct_info

        return _rtstruct_info(path)
    seg = _read_seg(path, stop_before_pixels=True)  # the header only

    segments = []
    for segment in seg.SegmentSequence:
        info: dict[str, str | int] = {
            "segment_number": segment.SegmentNumber,
            "segment_label": getattr(segment, "SegmentLabel", ""),
        }

        if hasattr(segment, "SegmentDescription"):
            info["segment_description"] = segment.SegmentDescription

        if hasattr(segment, "SegmentAlgorithmType"):
            info["algorithm_type"] = segment.SegmentAlgorithmType

        segments.append(info)

    return segments

pictologics.loaders.rtstruct_loader

DICOM RTSTRUCT Loader

Reads DICOM RT Structure Sets: the closed planar contours of each ROI, filled onto the voxel grid of a reference image. A voxel is in an ROI when its center lies inside an odd number of the contours of the ROI on its slice (the even-odd rule), so a contour inside another contour cuts a hole.

load_rtstruct(path, reference_image, roi_names=None, combine_rois=True, subvoxel_tolerance=0.5, subvoxel_warning_threshold=0.01)

Load the ROIs of a DICOM RTSTRUCT file as masks on the grid of reference_image.

The closed planar contours of each ROI are filled onto the slices of the reference image: a voxel is in the ROI when its center lies inside an odd number of the contours of the ROI on its slice, so a contour inside another contour cuts a hole. A center on an edge is inside on one side of the edge only, so two ROIs that share an edge share no voxel. The plane of each contour must be a slice plane of the reference image (axial, sagittal or coronal on its grid).

Parameters:

Name Type Description Default
path str | Path

Path to the RTSTRUCT file.

required
reference_image Image

The image whose grid the masks fill (the image that the contours were drawn on, in the LPS+ frame as every loaded image).

required
roi_names list[str] | None

The names of the ROIs to load. None loads every ROI with closed contours (the others are left out with a warning).

None
combine_rois bool

True gives one label image, with the ROI Number of each ROI as its label (a later ROI wins where ROIs overlap; uint8, or uint16 for ROI numbers above 255). False gives a dict of binary masks by ROI name, which keeps overlapping ROIs.

True
subvoxel_tolerance float

Largest distance (in voxels) of a contour from the nearest slice of the reference image; a contour farther away raises an error.

0.5
subvoxel_warning_threshold float

Distance (in voxels) above which a warning tells that the contours are filled on the nearest slices.

0.01

Returns:

Type Description
Image | dict[str, Image]

One label Image (combine_rois=True) or a dict {ROI name: binary mask Image},

Image | dict[str, Image]

with the geometry of the reference image and modality "RTSTRUCT".

Raises:

Type Description
ValueError

If the file is not an RTSTRUCT, a requested ROI is not in the file or has no closed contours, ROI names repeat where names must be unique, or a contour does not lie on the slices of the reference image.

FileNotFoundError

If the file does not exist.

Example
from pictologics import load_image
from pictologics.loaders import get_segment_info, load_rtstruct

ct = load_image("ct_scan/")
print(get_segment_info("rtstruct.dcm"))  # ROI numbers and names
masks = load_rtstruct("rtstruct.dcm", ct, roi_names=["GTV", "PTV"], combine_rois=False)
Source code in pictologics/loaders/rtstruct_loader.py
def load_rtstruct(
    path: str | Path,
    reference_image: Image,
    roi_names: list[str] | None = None,
    combine_rois: bool = True,
    subvoxel_tolerance: float = 0.5,
    subvoxel_warning_threshold: float = 0.01,
) -> Image | dict[str, Image]:
    """Load the ROIs of a DICOM RTSTRUCT file as masks on the grid of `reference_image`.

    The closed planar contours of each ROI are filled onto the slices of the reference
    image: a voxel is in the ROI when its center lies inside an odd number of the
    contours of the ROI on its slice, so a contour inside another contour cuts a hole.
    A center on an edge is inside on one side of the edge only, so two ROIs that share
    an edge share no voxel. The plane of each contour must be a slice plane of the
    reference image (axial, sagittal or coronal on its grid).

    Args:
        path: Path to the RTSTRUCT file.
        reference_image: The image whose grid the masks fill (the image that the
            contours were drawn on, in the LPS+ frame as every loaded image).
        roi_names: The names of the ROIs to load. None loads every ROI with closed
            contours (the others are left out with a warning).
        combine_rois: True gives one label image, with the ROI Number of each ROI as
            its label (a later ROI wins where ROIs overlap; uint8, or uint16 for ROI
            numbers above 255). False gives a dict of binary masks by ROI name, which
            keeps overlapping ROIs.
        subvoxel_tolerance: Largest distance (in voxels) of a contour from the nearest
            slice of the reference image; a contour farther away raises an error.
        subvoxel_warning_threshold: Distance (in voxels) above which a warning tells
            that the contours are filled on the nearest slices.

    Returns:
        One label Image (combine_rois=True) or a dict {ROI name: binary mask Image},
        with the geometry of the reference image and modality "RTSTRUCT".

    Raises:
        ValueError: If the file is not an RTSTRUCT, a requested ROI is not in the file
            or has no closed contours, ROI names repeat where names must be unique, or a
            contour does not lie on the slices of the reference image.
        FileNotFoundError: If the file does not exist.

    Example:
        ```python
        from pictologics import load_image
        from pictologics.loaders import get_segment_info, load_rtstruct

        ct = load_image("ct_scan/")
        print(get_segment_info("rtstruct.dcm"))  # ROI numbers and names
        masks = load_rtstruct("rtstruct.dcm", ct, roi_names=["GTV", "PTV"], combine_rois=False)
        ```
    """
    dataset = _read_rtstruct(path)
    rois = _rois(dataset)
    counts = Counter(name for name, _ in rois.values())
    if roi_names is None:
        chosen = [number for number, (_, contours) in rois.items() if contours]
        left_out = [name for name, contours in rois.values() if not contours]
        if left_out:
            warnings.warn(
                f"Left out the ROIs without closed contours: {', '.join(map(repr, left_out))}.",
                UserWarning,
                stacklevel=2,
            )
    else:
        numbers = {name: number for number, (name, _) in rois.items()}
        for name in roi_names:
            if name not in numbers:
                raise ValueError(f"ROI {name!r} is not in '{path}'. Its ROIs: {list(counts)}.")
            if counts[name] > 1:
                raise ValueError(f"ROI name {name!r} occurs {counts[name]} times in '{path}'.")
            if not rois[numbers[name]][1]:
                raise ValueError(f"ROI {name!r} has no closed contours in '{path}'.")
        chosen = [numbers[name] for name in roi_names]
    if not combine_rois:
        repeated = sorted({rois[n][0] for n in chosen if counts[rois[n][0]] > 1})
        if repeated:
            raise ValueError(
                f"ROI names {repeated} repeat in '{path}': use combine_rois=True (labels "
                "by ROI Number)."
            )
    frames = {
        int(item.ROINumber): str(item.ReferencedFrameOfReferenceUID)
        for item in getattr(dataset, "StructureSetROISequence", [])
        if getattr(item, "ReferencedFrameOfReferenceUID", "")
    }
    for uid in sorted({frames[n] for n in chosen if n in frames}):
        names = ", ".join(repr(rois[n][0]) for n in chosen if frames.get(n) == uid)
        _warn_if_other_frame(uid, reference_image, f"The RTSTRUCT ROI {names}")
    # The mask of each ROI comes in the box of its spans (no full-size mask for an ROI):
    # the label map takes the box at once, a dict mask copies it into zeros
    shape = reference_image.array.shape
    labels: npt.NDArray[Any] | None = None
    if combine_rois:
        labels = np.zeros(shape, np.uint8 if max(chosen, default=0) <= 255 else np.uint16)
    masks: dict[int, npt.NDArray[np.uint8]] = {}
    empty = []
    for number in chosen:
        box, part = _filled(
            rois[number], reference_image, subvoxel_tolerance, subvoxel_warning_threshold
        )
        if not part.any():
            empty.append(number)
        if labels is not None:
            labels[box][part] = number  # a later ROI wins where ROIs overlap
        else:
            masks[number] = mask = np.zeros(shape, np.uint8)
            mask[box] = part
    for number in empty:
        warnings.warn(
            f"ROI {rois[number][0]!r} covers no voxel center of the reference image.",
            UserWarning,
            stacklevel=2,
        )

    def image(array: npt.NDArray[Any]) -> Image:
        return Image(
            array=array,
            spacing=reference_image.spacing,
            origin=reference_image.origin,
            direction=reference_image.direction,
            modality="RTSTRUCT",
            frame_of_reference_uid=reference_image.frame_of_reference_uid,
        )

    if labels is None:
        return {rois[number][0]: image(mask) for number, mask in masks.items()}
    return image(labels)