From 265d3b0111f5abb136d1b6d31aab7f839fb22c39 Mon Sep 17 00:00:00 2001 From: Pim Nelissen Date: Fri, 20 Feb 2026 11:46:45 +0100 Subject: [PATCH] Add isotope lookup dictionary, so isotopes can be loaded from string in config. --- src/pg_rad/isotopes/__init__.py | 7 +++---- src/pg_rad/isotopes/isotope.py | 23 +++++++++++++++++++++++ src/pg_rad/isotopes/presets.py | 10 ---------- src/pg_rad/objects/sources.py | 19 ++++++++++--------- 4 files changed, 36 insertions(+), 23 deletions(-) delete mode 100644 src/pg_rad/isotopes/presets.py diff --git a/src/pg_rad/isotopes/__init__.py b/src/pg_rad/isotopes/__init__.py index 6f000c0..b926aa8 100644 --- a/src/pg_rad/isotopes/__init__.py +++ b/src/pg_rad/isotopes/__init__.py @@ -2,9 +2,8 @@ __ignore__ = ["logger"] from pg_rad.isotopes import isotope -from pg_rad.isotopes import presets -from pg_rad.isotopes.isotope import (Isotope,) -from pg_rad.isotopes.presets import (CS137,) +from pg_rad.isotopes.isotope import (CS137, Isotope, get_isotope, + preset_isotopes,) -__all__ = ['CS137', 'Isotope', 'isotope', 'presets'] +__all__ = ['CS137', 'Isotope', 'get_isotope', 'isotope', 'preset_isotopes'] diff --git a/src/pg_rad/isotopes/isotope.py b/src/pg_rad/isotopes/isotope.py index 5706acd..218b36f 100644 --- a/src/pg_rad/isotopes/isotope.py +++ b/src/pg_rad/isotopes/isotope.py @@ -1,3 +1,5 @@ +from typing import Dict, Type + from pg_rad.physics.attenuation import get_mass_attenuation_coeff @@ -25,3 +27,24 @@ class Isotope: self.E = E self.b = b self.mu_mass_air = get_mass_attenuation_coeff(E / 1000) + + +class CS137(Isotope): + def __init__(self): + super().__init__( + name="Cs-137", + E=661.66, + b=0.851 + ) + + +preset_isotopes: Dict[str, Type[Isotope]] = { + "CS137": CS137 +} + + +def get_isotope(isotope_str: str) -> Isotope: + """Lazy factory function to create isotope objects.""" + if isotope_str not in preset_isotopes: + raise ValueError(f"Unknown isotope: {isotope_str}") + return preset_isotopes[isotope_str]() diff --git a/src/pg_rad/isotopes/presets.py b/src/pg_rad/isotopes/presets.py deleted file mode 100644 index d8135d6..0000000 --- a/src/pg_rad/isotopes/presets.py +++ /dev/null @@ -1,10 +0,0 @@ -from .isotope import Isotope - - -class CS137(Isotope): - def __init__(self): - super().__init__( - name="Cs-137", - E=661.66, - b=0.851 - ) diff --git a/src/pg_rad/objects/sources.py b/src/pg_rad/objects/sources.py index 7659704..74a0ad3 100644 --- a/src/pg_rad/objects/sources.py +++ b/src/pg_rad/objects/sources.py @@ -1,7 +1,7 @@ import logging from .objects import BaseObject -from pg_rad.isotopes.isotope import Isotope +from pg_rad.isotopes.isotope import Isotope, get_isotope logger = logging.getLogger(__name__) @@ -11,16 +11,16 @@ class PointSource(BaseObject): def __init__( self, - activity: int, - isotope: Isotope, - pos: tuple[float, float, float] = (0, 0, 0), + activity_MBq: int, + isotope: str, + position: tuple[float, float, float] = (0, 0, 0), name: str | None = None, color: str = 'red' ): """A point source. Args: - activity (int): Activity A in MBq. + activity_MBq (int): Activity A in MBq. isotope (Isotope): The isotope. pos (tuple[float, float, float], optional): Position of the PointSource. @@ -37,16 +37,17 @@ class PointSource(BaseObject): if name is None: name = f"Source {self.id}" - super().__init__(pos, name, color) + super().__init__(position, name, color) - self.activity = activity - self.isotope = isotope + self.activity = activity_MBq + self.isotope: Isotope = get_isotope(isotope) logger.debug(f"Source created: {self.name}") def __repr__(self): + x, y, z = self.position repr_str = (f"PointSource(name={self.name}, " - + f"pos={(self.x, self.y, self.z)}, " + + f"pos={(x, y, z)}, " + f"A={self.activity} MBq), " + f"isotope={self.isotope.name}.")