Data Structures API¶
Classes for managing dose distributions, structures, and structure sets.
Dose Class¶
Dose ¶
Dose(dose_array: ndarray, spacing: Tuple[float, float, float], origin: Tuple[float, float, float], name: str = 'Dose', metadata: Optional[Dict] = None)
Represents a 3D dose distribution from RT-DOSE or NIfTI.
A Dose object is a pure data container representing dose distributions. Dose analysis is performed by combining Dose with Structure objects using functions from the metrics subpackage.
Attributes:
| Name | Type | Description |
|---|---|---|
dose_array |
ndarray
|
3D array of dose values in Gy |
spacing |
Tuple[float, float, float]
|
Voxel spacing in (x, y, z) mm |
origin |
Tuple[float, float, float]
|
Origin coordinates in mm |
name |
str
|
Identifier for this dose distribution |
metadata |
Dict
|
Additional metadata (DICOM tags, beam info, etc.) |
Examples:
>>> from dosemetrics.dose import Dose
>>> from dosemetrics.metrics import dvh
>>>
>>> # Load dose from DICOM
>>> dose = Dose.from_dicom("path/to/rtdose.dcm", name="Plan_v1")
>>>
>>> # Load dose from NIfTI
>>> dose = Dose.from_nifti("path/to/dose.nii.gz", name="Predicted")
>>>
>>> # Compute dose statistics (use metrics module)
>>> ptv = structure_set.get_structure("PTV")
>>> stats = statistics.compute_dose_statistics(dose, ptv)
>>> print(f"Mean dose: {stats['mean_dose']:.2f} Gy")
>>>
>>> # Compute DVH (use metrics module)
>>> dose_bins, volumes = dvh.compute_dvh(dose, ptv)
Initialize a Dose distribution.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dose_array
|
ndarray
|
3D array of dose values (Gy) |
required |
spacing
|
Tuple[float, float, float]
|
Voxel spacing in (x, y, z) mm |
required |
origin
|
Tuple[float, float, float]
|
Origin coordinates in mm |
required |
name
|
str
|
Identifier for this dose (e.g., "Plan_v1", "Sum", "Predicted") |
'Dose'
|
metadata
|
Optional[Dict]
|
Additional metadata (DICOM tags, beam info, etc.) |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If dose_array is not 3D |
Source code in src/dosemetrics/dose.py
Attributes¶
Functions¶
is_compatible_with_structure ¶
Check if this dose is spatially compatible with a structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Structure
|
Structure to check compatibility with |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if shapes, spacing, and origin match |
Source code in src/dosemetrics/dose.py
get_dose_in_structure ¶
Extract dose values within a structure mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Structure
|
Structure to extract dose from |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
1D array of dose values inside the structure |
Raises:
| Type | Description |
|---|---|
ValueError
|
If dose and structure are not spatially compatible |
Source code in src/dosemetrics/dose.py
from_nifti
classmethod
¶
Load dose distribution from a NIfTI file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Union[str, Path]
|
Path to NIfTI file (.nii or .nii.gz) |
required |
name
|
Optional[str]
|
Name for this dose (uses filename stem if None) |
None
|
Returns:
| Type | Description |
|---|---|
Dose
|
Dose object |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
ValueError
|
If file cannot be loaded |
Source code in src/dosemetrics/dose.py
from_dicom
classmethod
¶
Load dose distribution from a DICOM RT-DOSE file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Union[str, Path]
|
Path to RT-DOSE DICOM file |
required |
name
|
Optional[str]
|
Name for this dose (uses filename stem if None) |
None
|
Returns:
| Type | Description |
|---|---|
Dose
|
Dose object |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
ValueError
|
If file is not a valid RT-DOSE |
Source code in src/dosemetrics/dose.py
__repr__ ¶
__str__ ¶
Human-readable string representation.
Source code in src/dosemetrics/dose.py
Structure Classes¶
structures ¶
Radiotherapy structure classes for anatomical regions of interest.
This module provides core data structures to represent radiotherapy structures from RTSS DICOM files or NIfTI files. These structures are 3D volumes with binary masks representing anatomical regions of interest such as organs at risk (OARs), target volumes, and avoidance regions.
Structures represent pure geometry. Dose analysis is performed by combining Structure objects with Dose objects using the dosemetrics.dose module.
Classes¶
StructureType ¶
Bases: Enum
Enumeration for different types of radiotherapy structures.
Structure ¶
Structure(name: str, mask: Optional[ndarray] = None, spacing: Tuple[float, float, float] = (1.0, 1.0, 1.0), origin: Tuple[float, float, float] = (0.0, 0.0, 0.0))
Bases: ABC
Base class for radiotherapy structures.
Represents a 3D anatomical structure derived from RTSS DICOM files or equivalent NIfTI masks. Contains geometric information only - dose analysis is performed by combining with Dose objects.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Name/identifier of the structure |
mask |
ndarray
|
3D binary mask array (boolean) |
spacing |
Tuple[float, float, float]
|
Voxel spacing in (x, y, z) mm |
origin |
Tuple[float, float, float]
|
Origin coordinates in mm |
Examples:
>>> # Create a structure
>>> ptv = Target(name="PTV", mask=mask_array, spacing=(1.0, 1.0, 3.0))
>>>
>>> # Get volume
>>> volume_cc = ptv.volume_cc()
>>>
>>> # Compute dose statistics (using Dose object)
>>> from dosemetrics.dose import Dose
>>> dose = Dose.from_dicom("rtdose.dcm")
>>> stats = dose.compute_statistics(ptv)
Initialize a Structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name/identifier of the structure |
required |
mask
|
Optional[ndarray]
|
3D binary mask array (will be converted to bool) |
None
|
spacing
|
Tuple[float, float, float]
|
Voxel spacing in (x, y, z) mm |
(1.0, 1.0, 1.0)
|
origin
|
Tuple[float, float, float]
|
Origin coordinates in mm |
(0.0, 0.0, 0.0)
|
Source code in src/dosemetrics/structures.py
Attributes¶
abstractmethod
property
¶Return the type of this structure.
Functions¶
Set the binary mask for this structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
3D array that will be converted to binary mask |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If mask is not 3D |
Source code in src/dosemetrics/structures.py
Get structure volume in voxels.
Returns:
| Type | Description |
|---|---|
int
|
Number of voxels in the structure (sum of mask) |
Get structure volume in cubic centimeters.
Returns:
| Type | Description |
|---|---|
float
|
Volume in cc (considering voxel spacing) |
Source code in src/dosemetrics/structures.py
Calculate the centroid of the structure in world coordinates.
Returns:
| Type | Description |
|---|---|
Optional[Tuple[float, float, float]]
|
Tuple of (x, y, z) coordinates in mm, or None if no mask |
Source code in src/dosemetrics/structures.py
Get bounding box of the structure in voxel coordinates.
Returns:
| Type | Description |
|---|---|
Optional[Tuple[Tuple[int, int], Tuple[int, int], Tuple[int, int]]]
|
Tuple of ((min_x, max_x), (min_y, max_y), (min_z, max_z)), or None if no mask |
Source code in src/dosemetrics/structures.py
String representation of the structure.
Detailed representation of the structure.
Source code in src/dosemetrics/structures.py
OAR ¶
OAR(name: str, mask: Optional[ndarray] = None, spacing: Tuple[float, float, float] = (1.0, 1.0, 1.0), origin: Tuple[float, float, float] = (0.0, 0.0, 0.0))
Bases: Structure
Organ at Risk (OAR) structure.
Represents critical normal organs that should receive limited radiation dose to avoid complications (e.g., spinal cord, eyes, heart, brainstem).
Source code in src/dosemetrics/structures.py
Target ¶
Target(name: str, mask: Optional[ndarray] = None, spacing: Tuple[float, float, float] = (1.0, 1.0, 1.0), origin: Tuple[float, float, float] = (0.0, 0.0, 0.0))
Bases: Structure
Target volume structure.
Represents volumes that should receive the prescribed radiation dose (e.g., PTV, CTV, GTV).
Source code in src/dosemetrics/structures.py
AvoidanceStructure ¶
AvoidanceStructure(name: str, mask: Optional[ndarray] = None, spacing: Tuple[float, float, float] = (1.0, 1.0, 1.0), origin: Tuple[float, float, float] = (0.0, 0.0, 0.0))
Bases: Structure
Avoidance structure.
Represents regions where dose should be minimized during planning (e.g., critical OAR expansions, sensitive areas).
Source code in src/dosemetrics/structures.py
StructureSet Class¶
StructureSet ¶
StructureSet(spacing: Tuple[float, float, float] = (1.0, 1.0, 1.0), origin: Tuple[float, float, float] = (0.0, 0.0, 0.0), name: str = 'StructureSet')
Collection of radiotherapy structures representing a complete structure set.
Similar to a DICOM RTSS file, this class manages multiple structures (OARs, targets, avoidance regions) with common geometric properties. Contains only geometric information - dose analysis is performed separately by combining with Dose objects.
Attributes:
| Name | Type | Description |
|---|---|---|
structures |
Dict[str, Structure]
|
Dictionary mapping structure names to Structure objects |
spacing |
Tuple[float, float, float]
|
Common voxel spacing for all structures |
origin |
Tuple[float, float, float]
|
Common origin for all structures |
name |
str
|
Identifier for this structure set |
Examples:
>>> # Create a structure set
>>> structure_set = StructureSet(spacing=(1.0, 1.0, 3.0), name="Patient001")
>>>
>>> # Add structures
>>> structure_set.add_structure("PTV", ptv_mask, StructureType.TARGET)
>>> structure_set.add_structure("Brainstem", brain_mask, StructureType.OAR)
>>>
>>> # Access structures
>>> ptv = structure_set.get_structure("PTV")
>>> print(f"PTV volume: {ptv.volume_cc():.2f} cc")
>>>
>>> # For dose analysis, combine with Dose object
>>> from dosemetrics.dose import Dose
>>> dose = Dose.from_dicom("rtdose.dcm")
>>> stats = dose.compute_statistics(ptv)
Initialize an empty StructureSet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
spacing
|
Tuple[float, float, float]
|
Common voxel spacing in (x, y, z) mm |
(1.0, 1.0, 1.0)
|
origin
|
Tuple[float, float, float]
|
Common origin coordinates in mm |
(0.0, 0.0, 0.0)
|
name
|
str
|
Name identifier for this structure set |
'StructureSet'
|
Source code in src/dosemetrics/structure_set.py
Attributes¶
Functions¶
add_structure ¶
add_structure(name: str, mask: ndarray, structure_type: StructureType, structure_class: Optional[type] = None) -> Structure
Add a structure to the set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the structure |
required |
mask
|
ndarray
|
3D binary mask array |
required |
structure_type
|
StructureType
|
Type of structure (OAR, TARGET, etc.) |
required |
structure_class
|
Optional[type]
|
Specific structure class to use (defaults based on type) |
None
|
Returns:
| Type | Description |
|---|---|
Structure
|
The created Structure object |
Raises:
| Type | Description |
|---|---|
ValueError
|
If structure name already exists or mask dimensions are invalid |
Source code in src/dosemetrics/structure_set.py
remove_structure ¶
Remove a structure from the set.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the structure to remove |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If structure name not found |
Source code in src/dosemetrics/structure_set.py
add_structure_object ¶
Add an existing Structure instance to the set.
Convenience wrapper to support tests and workflows that create
Structure objects independently and then attach them to a StructureSet.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Structure
|
A |
required |
Returns:
| Type | Description |
|---|---|
Structure
|
The same |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a structure with the same name already exists, or if spacing/origin are incompatible with the set. |
Source code in src/dosemetrics/structure_set.py
get_structure ¶
Get a structure by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the structure |
required |
Returns:
| Type | Description |
|---|---|
Structure
|
Structure object |
Raises:
| Type | Description |
|---|---|
ValueError
|
If structure name not found |
Source code in src/dosemetrics/structure_set.py
get_structures_by_type ¶
Get all structures of a specific type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure_type
|
StructureType
|
Type to filter by |
required |
Returns:
| Type | Description |
|---|---|
Dict[str, Structure]
|
Dictionary of structures matching the type |
Source code in src/dosemetrics/structure_set.py
get_oars ¶
get_targets ¶
get_avoidance_structures ¶
total_volume_cc ¶
Calculate total volume of all structures in cc.
Returns:
| Type | Description |
|---|---|
float
|
Sum of all structure volumes in cubic centimeters |
geometric_summary ¶
Generate geometric summary for all structures.
Returns:
| Type | Description |
|---|---|
DataFrame
|
DataFrame with geometric properties of each structure including: |
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
DataFrame
|
|
Source code in src/dosemetrics/structure_set.py
__len__ ¶
__iter__ ¶
__getitem__ ¶
__contains__ ¶
__str__ ¶
String representation of the structure set.
Source code in src/dosemetrics/structure_set.py
__repr__ ¶
Detailed representation of the structure set.