Data and I/O API¶
Unified loaders¶
data_io ¶
Unified I/O module for radiotherapy data.
This module provides high-level functions for loading radiotherapy data from various sources (DICOM, NIfTI) with automatic format detection and intelligent structure organization.
The API is organized in layers: 1. Low-level: Format-specific readers (dicom_io, nifti_io) 2. Mid-level: Type-specific loaders (load_volume, load_structure, etc.) 3. High-level: Auto-detecting folder loaders (load_from_folder, load_structure_set)
Functions:¶
detect_folder_format ¶
Detect the data format in a folder (DICOM or NIfTI).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder_path
|
Union[str, Path]
|
Path to folder to inspect |
required |
Returns:
| Type | Description |
|---|---|
str
|
'dicom' if DICOM files found, 'nifti' if NIfTI files found, 'unknown' otherwise |
Source code in src/dosemetrics/io/data_io.py
load_from_folder ¶
load_from_folder(folder_path: Union[str, Path], format: Optional[str] = None, **kwargs: object) -> Union[StructureSet, Dict[str, Union[np.ndarray, Dict, Tuple]]]
Load radiotherapy data from a folder, auto-detecting format.
This is the highest-level function for loading data. It automatically detects whether the folder contains DICOM or NIfTI data and loads accordingly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder_path
|
Union[str, Path]
|
Path to folder containing data |
required |
format
|
Optional[str]
|
Force specific format ('dicom' or 'nifti'). If None, auto-detects. |
None
|
**kwargs
|
object
|
Additional arguments passed to format-specific loaders |
{}
|
Returns:
| Type | Description |
|---|---|
Union[StructureSet, Dict[str, Union[ndarray, Dict, Tuple]]]
|
A StructureSet by default. Pass |
Union[StructureSet, Dict[str, Union[ndarray, Dict, Tuple]]]
|
receive a format-specific dictionary of arrays and metadata. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If folder doesn't exist |
ValueError
|
If format cannot be determined |
Source code in src/dosemetrics/io/data_io.py
load_structure_set ¶
load_structure_set(folder_path: Union[str, Path], format: Optional[str] = None, name: Optional[str] = None, structure_type_mapping: Optional[Dict[str, StructureType]] = None, **kwargs: object) -> StructureSet
Load a complete StructureSet from a folder, auto-detecting format.
This is the primary high-level function for loading radiotherapy data as a unified StructureSet object. It handles both DICOM and NIfTI formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder_path
|
Union[str, Path]
|
Path to folder containing data |
required |
format
|
Optional[str]
|
Force specific format ('dicom' or 'nifti'). If None, auto-detects. |
None
|
name
|
Optional[str]
|
Name for the structure set. If None, uses folder name. |
None
|
structure_type_mapping
|
Optional[Dict[str, StructureType]]
|
Optional dict mapping structure names to StructureType |
None
|
**kwargs
|
object
|
Additional arguments passed to format-specific loaders For NIfTI: dose_filename (default: "Dose.nii.gz") For DICOM: dose_file_name (specific dose file to use) |
{}
|
Returns:
| Type | Description |
|---|---|
StructureSet
|
StructureSet object with loaded structures. Dose is loaded separately. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If folder doesn't exist |
ValueError
|
If format cannot be determined or no structures found |
Examples:
>>> # Load from NIfTI folder with custom dose filename
>>> structure_set = load_structure_set('path/to/nifti_folder',
... dose_filename='dose_distribution.nii.gz')
>>> # Force format and provide structure types
>>> type_mapping = {'Liver': StructureType.OAR, 'PTV': StructureType.TARGET}
>>> structure_set = load_structure_set('path/to/folder',
... format='nifti',
... structure_type_mapping=type_mapping)
Source code in src/dosemetrics/io/data_io.py
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | |
load_volume ¶
load_volume(file_path: Union[str, Path], format: Optional[str] = None) -> Tuple[np.ndarray, Tuple[float, float, float], Tuple[float, float, float]]
Load a single volume file (DICOM RTDOSE or NIfTI).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Union[str, Path]
|
Path to file or folder (for DICOM CT series) |
required |
format
|
Optional[str]
|
Force specific format ('dicom' or 'nifti'). If None, auto-detects. |
None
|
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, Tuple[float, float, float], Tuple[float, float, float]]
|
Tuple of (volume, spacing, origin) |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
ValueError
|
If format cannot be determined |
Source code in src/dosemetrics/io/data_io.py
load_structure ¶
load_structure(file_path: Union[str, Path], name: Optional[str] = None, structure_type: StructureType = None, format: Optional[str] = None, **kwargs: object) -> Structure
Load a single structure from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
Union[str, Path]
|
Path to NIfTI file containing structure mask |
required |
name
|
Optional[str]
|
Name for the structure. If None, uses filename. |
None
|
structure_type
|
StructureType
|
Type of structure (OAR, TARGET, etc.) |
None
|
format
|
Optional[str]
|
Force specific format ('nifti'). If None, auto-detects. |
None
|
**kwargs
|
object
|
Additional arguments (e.g., threshold for binarization) |
{}
|
Returns:
| Type | Description |
|---|---|
Structure
|
Structure object |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
ValueError
|
If format is not supported (currently only NIfTI single files) |
Note
For DICOM RTSTRUCT files, use load_structure_set() instead as they typically contain multiple structures.
Source code in src/dosemetrics/io/data_io.py
NIfTI helpers¶
nifti_io ¶
NIfTI I/O utilities for radiotherapy data.
This module provides functions to read NIfTI files for: - CT/MR image volumes (real-valued) - Dose distributions (real-valued) - Structure masks (binary)
Uses SimpleITK for robust NIfTI reading with proper handling of spacing and origin.
Classes¶
Functions:¶
read_nifti_volume ¶
read_nifti_volume(nifti_file: Union[str, Path]) -> Tuple[np.ndarray, Tuple[float, float, float], Tuple[float, float, float]]
Read a NIfTI file and return volume with geometric information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nifti_file
|
Union[str, Path]
|
Path to NIfTI file (.nii or .nii.gz) |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, Tuple[float, float, float], Tuple[float, float, float]]
|
Tuple of (volume, spacing, origin) where: - volume: 3D numpy array - spacing: (x, y, z) voxel spacing in mm - origin: (x, y, z) origin coordinates in mm |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
RuntimeError
|
If file cannot be read |
Source code in src/dosemetrics/io/nifti_io.py
read_nifti_mask ¶
read_nifti_mask(nifti_file: Union[str, Path], threshold: float = 0.5) -> Tuple[np.ndarray, Tuple[float, float, float], Tuple[float, float, float]]
Read a NIfTI file as a binary mask.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nifti_file
|
Union[str, Path]
|
Path to NIfTI file containing binary or probability mask |
required |
threshold
|
float
|
Threshold for binarization (values > threshold become True) |
0.5
|
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, Tuple[float, float, float], Tuple[float, float, float]]
|
Tuple of (mask, spacing, origin) where: - mask: 3D boolean numpy array - spacing: (x, y, z) voxel spacing in mm - origin: (x, y, z) origin coordinates in mm |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
Source code in src/dosemetrics/io/nifti_io.py
read_nifti_dose ¶
read_nifti_dose(nifti_file: Union[str, Path]) -> Tuple[np.ndarray, Tuple[float, float, float], Tuple[float, float, float]]
Read a NIfTI file containing dose distribution.
This is an alias for read_nifti_volume with a more semantic name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nifti_file
|
Union[str, Path]
|
Path to NIfTI file containing dose data |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, Tuple[float, float, float], Tuple[float, float, float]]
|
Tuple of (dose_array, spacing, origin) where: - dose_array: 3D numpy array with dose values (typically in Gy) - spacing: (x, y, z) voxel spacing in mm - origin: (x, y, z) origin coordinates in mm |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
Source code in src/dosemetrics/io/nifti_io.py
read_from_nifti ¶
Read a NIfTI file and return only the volume array (backward compatibility).
This function provides backward compatibility with older code that expects only the numpy array. For new code, use read_nifti_volume() to also get spacing and origin information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nifti_file
|
Union[str, Path]
|
Path to NIfTI file |
required |
Returns:
| Type | Description |
|---|---|
ndarray
|
3D numpy array |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
Note
Deprecated. Use read_nifti_volume() for new code to get spacing and origin.
Source code in src/dosemetrics/io/nifti_io.py
is_binary_volume ¶
Check if a volume contains only binary values (0 and 1).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
ndarray
|
Numpy array to check |
required |
tolerance
|
float
|
Tolerance for checking if values are 0 or 1 |
1e-06
|
Returns:
| Type | Description |
|---|---|
bool
|
True if volume is binary, False otherwise |
Source code in src/dosemetrics/io/nifti_io.py
load_nifti_folder ¶
load_nifti_folder(folder_path: Union[str, Path], dose_filename: str = 'Dose.nii.gz', auto_detect_masks: bool = True, return_as_structureset: bool = True, structure_type_mapping: Optional[Dict[str, StructureType]] = None) -> Union[StructureSet, Dict[str, Union[np.ndarray, Dict, Tuple]]]
Load all NIfTI files from a folder.
This function automatically: 1. Detects and loads the dose file 2. Auto-detects whether each file is a binary mask (structure) or real-valued volume (image) 3. Returns a StructureSet (default) or dictionary with organized data
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder_path
|
Union[str, Path]
|
Path to folder containing NIfTI files |
required |
dose_filename
|
str
|
Name of the dose file (default: "Dose.nii.gz") |
'Dose.nii.gz'
|
auto_detect_masks
|
bool
|
Whether to auto-detect binary masks vs real-valued volumes |
True
|
return_as_structureset
|
bool
|
If True (default), returns a StructureSet object. If False, returns raw dictionary. |
True
|
structure_type_mapping
|
Optional[Dict[str, StructureType]]
|
Optional dict mapping structure names to StructureType (only used if return_as_structureset=True) |
None
|
Returns:
| Type | Description |
|---|---|
Union[StructureSet, Dict[str, Union[ndarray, Dict, Tuple]]]
|
If return_as_structureset=True: StructureSet object with loaded data |
Union[StructureSet, Dict[str, Union[ndarray, Dict, Tuple]]]
|
If return_as_structureset=False: Dictionary with keys: - 'dose_volume': Dose distribution array (if found) - 'dose_spacing': Dose spacing tuple (if found) - 'dose_origin': Dose origin tuple (if found) - 'image_volumes': Dict of real-valued volumes {name: {'volume', 'spacing', 'origin'}} - 'structure_masks': Dict of binary masks {name: {'mask', 'spacing', 'origin'}} - 'spacing': Common spacing for all data - 'origin': Common origin for all data |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If folder doesn't exist |
Source code in src/dosemetrics/io/nifti_io.py
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
create_structure_set_from_nifti_folder ¶
create_structure_set_from_nifti_folder(folder_path: Union[str, Path], dose_filename: str = 'Dose.nii.gz', structure_type_mapping: Optional[Dict[str, StructureType]] = None, name: Optional[str] = None) -> StructureSet
Create a StructureSet from NIfTI files in a folder.
This high-level function automatically: 1. Loads dose distribution 2. Auto-detects binary mask files as structures 3. Creates Structure objects with appropriate types 4. Returns a complete StructureSet
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder_path
|
Union[str, Path]
|
Path to folder containing NIfTI files |
required |
dose_filename
|
str
|
Name of the dose file (default: "Dose.nii.gz") |
'Dose.nii.gz'
|
structure_type_mapping
|
Optional[Dict[str, StructureType]]
|
Optional dict mapping structure names to StructureType. If not provided, guesses based on naming conventions. |
None
|
name
|
Optional[str]
|
Name for the structure set. If None, uses folder name. |
None
|
Returns:
| Type | Description |
|---|---|
StructureSet
|
StructureSet object with loaded structures and dose |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If folder doesn't exist |
ValueError
|
If no structures found |
Source code in src/dosemetrics/io/nifti_io.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 | |
read_nifti_structure ¶
read_nifti_structure(nifti_file: Union[str, Path], name: Optional[str] = None, structure_type: StructureType = None, threshold: float = 0.5) -> Structure
Read a single NIfTI file as a Structure object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
nifti_file
|
Union[str, Path]
|
Path to NIfTI file containing binary mask |
required |
name
|
Optional[str]
|
Name for the structure. If None, uses filename. |
None
|
structure_type
|
StructureType
|
Type of structure (OAR, TARGET, etc.) |
None
|
threshold
|
float
|
Threshold for binarization |
0.5
|
Returns:
| Type | Description |
|---|---|
Structure
|
Structure object (OAR, Target, or AvoidanceStructure based on type) |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
Source code in src/dosemetrics/io/nifti_io.py
write_nifti_volume ¶
write_nifti_volume(volume: ndarray, output_file: Union[str, Path], spacing: Tuple[float, float, float] = (1.0, 1.0, 1.0), origin: Tuple[float, float, float] = (0.0, 0.0, 0.0)) -> None
Write a numpy array to a NIfTI file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
volume
|
ndarray
|
3D numpy array to write |
required |
output_file
|
Union[str, Path]
|
Path for output NIfTI file |
required |
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 (x, y, z) mm |
(0.0, 0.0, 0.0)
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If volume is not 3D |
Source code in src/dosemetrics/io/nifti_io.py
write_structure_as_nifti ¶
Write a Structure's mask to a NIfTI file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure
|
Structure
|
Structure object to write |
required |
output_file
|
Union[str, Path]
|
Path for output NIfTI file |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If structure has no mask |
Source code in src/dosemetrics/io/nifti_io.py
write_structure_set_as_nifti ¶
write_structure_set_as_nifti(structure_set: StructureSet, output_folder: Union[str, Path], write_dose: bool = True, dose_filename: str = 'Dose.nii.gz') -> None
Write a StructureSet to NIfTI files in a folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
structure_set
|
StructureSet
|
StructureSet to write |
required |
output_folder
|
Union[str, Path]
|
Path to output folder |
required |
write_dose
|
bool
|
Whether to write dose file |
True
|
dose_filename
|
str
|
Name for dose file |
'Dose.nii.gz'
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If structure_set has no structures |
Source code in src/dosemetrics/io/nifti_io.py
DICOM helpers¶
dicom_io ¶
DICOM I/O utilities for radiotherapy data.
This module provides functions to read DICOM radiotherapy data including: - CT image volumes - RTDOSE (dose distributions) - RTSTRUCT (structure sets/contours) - RTPLAN (treatment plans - metadata only)
Uses pydicom for DICOM parsing and SimpleITK for volume reconstruction.
Classes¶
Functions:¶
read_dicom_ct_volume ¶
read_dicom_ct_volume(ct_directory: Union[str, Path]) -> Tuple[np.ndarray, Tuple[float, float, float], Tuple[float, float, float]]
Read a CT volume from a directory of DICOM slices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ct_directory
|
Union[str, Path]
|
Path to directory containing CT DICOM files |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, Tuple[float, float, float], Tuple[float, float, float]]
|
Tuple of (volume, spacing, origin) where: - volume: 3D numpy array with shape (slices, rows, cols) - spacing: (x, y, z) voxel spacing in mm - origin: (x, y, z) origin coordinates in mm |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If directory doesn't exist or contains no DICOM files |
ValueError
|
If DICOM files don't form a valid CT series |
Source code in src/dosemetrics/io/dicom_io.py
read_dicom_rtdose ¶
read_dicom_rtdose(rtdose_file: Union[str, Path]) -> Tuple[np.ndarray, Tuple[float, float, float], Tuple[float, float, float], float]
Read an RTDOSE DICOM file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rtdose_file
|
Union[str, Path]
|
Path to RTDOSE DICOM file |
required |
Returns:
| Type | Description |
|---|---|
Tuple[ndarray, Tuple[float, float, float], Tuple[float, float, float], float]
|
Tuple of (dose_array, spacing, origin, dose_scaling) where: - dose_array: 3D numpy array with dose values in Gy - spacing: (x, y, z) voxel spacing in mm - origin: (x, y, z) origin coordinates in mm - dose_scaling: Dose grid scaling factor |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
ValueError
|
If file is not a valid RTDOSE DICOM |
Source code in src/dosemetrics/io/dicom_io.py
read_dicom_rtstruct ¶
read_dicom_rtstruct(rtstruct_file: Union[str, Path], reference_image: Optional[Union[Image, Tuple[Tuple[int, ...], Tuple[float, ...], Tuple[float, ...]]]] = None) -> Dict[str, Dict[str, Union[np.ndarray, List]]]
Read an RTSTRUCT DICOM file and extract structure information.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rtstruct_file
|
Union[str, Path]
|
Path to RTSTRUCT DICOM file |
required |
reference_image
|
Optional[Union[Image, Tuple[Tuple[int, ...], Tuple[float, ...], Tuple[float, ...]]]]
|
Optional reference image or (shape, spacing, origin) tuple for mask generation. If None, only contour points are returned without generating binary masks. |
None
|
Returns:
| Type | Description |
|---|---|
Dict[str, Dict[str, Union[ndarray, List]]]
|
Dictionary mapping structure names to dictionaries containing: - 'contours': List of contour point arrays (each is Nx3 array of (x, y, z) points) - 'mask': Binary mask array (only if reference_image provided) - 'roi_number': ROI number from DICOM - 'color': RGB color tuple |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If file doesn't exist |
ValueError
|
If file is not a valid RTSTRUCT DICOM |
Source code in src/dosemetrics/io/dicom_io.py
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 | |
load_dicom_folder ¶
load_dicom_folder(folder_path: Union[str, Path], load_ct: bool = True, load_rtdose: bool = True, load_rtstruct: bool = True, return_as_structureset: bool = True, dose_file_name: Optional[str] = None, structure_type_mapping: Optional[Dict[str, StructureType]] = None) -> Union[StructureSet, Dict[str, Union[np.ndarray, Dict, Tuple]]]
Load all DICOM data from a folder containing CT, RTDOSE, and RTSTRUCT files.
This is a high-level function that automatically detects and loads all DICOM modalities present in the folder.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder_path
|
Union[str, Path]
|
Path to folder containing DICOM files organized in subfolders (e.g., CT/, RTDOSE/, RTSTRUCT/) |
required |
load_ct
|
bool
|
Whether to load CT volume |
True
|
load_rtdose
|
bool
|
Whether to load dose distributions |
True
|
load_rtstruct
|
bool
|
Whether to load structure sets |
True
|
return_as_structureset
|
bool
|
If True (default), returns a StructureSet object. If False, returns raw dictionary. |
True
|
dose_file_name
|
Optional[str]
|
Specific dose file to use (only if return_as_structureset=True) |
None
|
structure_type_mapping
|
Optional[Dict[str, StructureType]]
|
Optional dict mapping structure names to StructureType (only used if return_as_structureset=True) |
None
|
Returns:
| Type | Description |
|---|---|
Union[StructureSet, Dict[str, Union[ndarray, Dict, Tuple]]]
|
If return_as_structureset=True: StructureSet object with loaded data |
Union[StructureSet, Dict[str, Union[ndarray, Dict, Tuple]]]
|
If return_as_structureset=False: Dictionary with keys: - 'ct_volume': CT volume array (if loaded) - 'ct_spacing': CT spacing tuple (if loaded) - 'ct_origin': CT origin tuple (if loaded) - 'dose_volumes': Dict of dose volumes {filename: (array, spacing, origin, scaling)} - 'structures': Dict of structures from RTSTRUCT - 'spacing': Common spacing for all data - 'origin': Common origin for all data |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If folder doesn't exist |
Source code in src/dosemetrics/io/dicom_io.py
296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 | |
create_structure_set_from_dicom ¶
create_structure_set_from_dicom(folder_path: Union[str, Path], dose_file_name: Optional[str] = None, structure_type_mapping: Optional[Dict[str, StructureType]] = None, name: str = 'DICOM StructureSet') -> StructureSet
Create a StructureSet object from DICOM data in a folder.
This is a high-level convenience function that loads DICOM data and creates a complete StructureSet object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
folder_path
|
Union[str, Path]
|
Path to folder containing DICOM subfolders |
required |
dose_file_name
|
Optional[str]
|
Specific dose file to use (e.g., 'RD_1'). If None, uses first found. |
None
|
structure_type_mapping
|
Optional[Dict[str, StructureType]]
|
Optional dict mapping structure names to StructureType |
None
|
name
|
str
|
Name for the structure set |
'DICOM StructureSet'
|
Returns:
| Type | Description |
|---|---|
StructureSet
|
StructureSet object with loaded structures and dose data |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If folder doesn't exist |
ValueError
|
If no structures found |
Source code in src/dosemetrics/io/dicom_io.py
Typical NIfTI workflow¶
from dosemetrics import Dose, StructureType
from dosemetrics.io import load_structure, load_structure_set
dose = Dose.from_nifti("patient/Dose.nii.gz")
structures = load_structure_set("patient")
ptv = load_structure(
"patient/PTV.nii.gz",
name="PTV",
structure_type=StructureType.TARGET,
)
Typical DICOM workflow¶
from dosemetrics import Dose
from dosemetrics.io import load_structure_set
dose = Dose.from_dicom("patient/RTDOSE/plan.dcm")
structures = load_structure_set("patient", format="dicom")
Batch over patient folders¶
from pathlib import Path
from dosemetrics import Dose
from dosemetrics.io import load_structure_set
from dosemetrics.metrics import dvh
results = []
for patient_dir in Path("data").glob("patient_*"):
dose = Dose.from_nifti(patient_dir / "Dose.nii.gz")
structures = load_structure_set(patient_dir)
for name, structure in structures:
stats = dvh.compute_dose_statistics(dose, structure)
results.append({"patient": patient_dir.name, "structure": name, **stats})
Supported formats¶
| Data | NIfTI | DICOM |
|---|---|---|
| Dose | .nii, .nii.gz |
RTDOSE .dcm |
| Structure | binary .nii, .nii.gz |
RTSTRUCT .dcm with reference grid |
| Image | .nii, .nii.gz |
CT series |
NRRD is not currently supported by the public API.