Source code for pyRadPlan.machines.base._external_beam
from typing import Optional, Any
import warnings
from pydantic import (
Field,
model_validator,
)
from numpydantic import NDArray, Shape
import numpy as np
from ._base import Machine
[docs]
class ExternalBeamMachine(Machine):
"""
Base class for Machine used for external irradiation.
Attributes
----------
sad : float
The source-to-axis distance of the machine
energies : list[float]
List of available photon energies
"""
energies: NDArray[Shape["1-*"], np.float64]
sad: float = Field(ge=0.0, description="Source-to-axis distance", alias="SAD")
# Model sanitation / validation for compatibility
@classmethod
def _parse_tabulated_energy_data_from_mat(
cls, tabulated_energy_data: dict, returned_data: dict
):
"""Parse the tabulated energy data from a matRad machine file."""
# prepare data structure for validation
if isinstance(tabulated_energy_data, dict):
# We might have a dictionary of lists
if "energy" not in tabulated_energy_data:
raise ValueError("Energy could not be found!")
returned_data["energies"] = np.array(tabulated_energy_data["energy"], dtype=np.float64)
if returned_data["energies"].size == 1:
returned_data["energies"] = returned_data["energies"].flatten()
[docs]
def get_closest_energy_index(self, energy: float) -> int:
"""
Given an energy value, return the closest matching index.
Parameters
----------
energy : float
Energy value to search for
Returns
-------
int
Index of the energy level
"""
return np.argmin(np.abs(self.energies - energy))
[docs]
def get_energy_index(self, energy: float, round_decimals=4) -> Optional[int]:
"""
Given an energy value, return the index of the exact matching.
Parameters
----------
energy : float
Energy value to search for
round_decimals : int, optional
Number of decimals to round the energy values to, by default 4
Returns
-------
Optional[int]
Index of the energy level
"""
ix = np.where(np.round(self.energies, round_decimals) == np.round(energy, round_decimals))[
0
]
if len(ix) == 0:
return None
if len(ix) > 1:
raise ValueError("Multiple matching energies found.")
return ix[0]