add bkg count option in input

This commit is contained in:
Pim Nelissen
2026-04-21 10:53:53 +02:00
parent 8017159c5b
commit 591e2fb41a
5 changed files with 33 additions and 7 deletions

View File

@ -11,13 +11,17 @@ def generate_background(
cps_array: np.ndarray, cps_array: np.ndarray,
detector: Detector, detector: Detector,
energy_keV: float, energy_keV: float,
lam_inp: int | None = None
) -> np.ndarray: ) -> np.ndarray:
""" """
Generate synthetic background cps for a given detector and energy. Generate synthetic background cps for a given detector and energy.
""" """
if not lam_inp:
ROI_lo, ROI_hi = get_roi_from_fwhm(detector, energy_keV) ROI_lo, ROI_hi = get_roi_from_fwhm(detector, energy_keV)
lam = get_cps_from_roi(detector, ROI_lo, ROI_hi) lam = get_cps_from_roi(detector, ROI_lo, ROI_hi)
else:
lam = lam_inp
rng = np.random.default_rng() rng = np.random.default_rng()
return rng.poisson(lam=lam, size=cps_array.shape) return rng.poisson(lam=lam, size=cps_array.shape)

View File

@ -104,7 +104,7 @@ class ConfigParser:
def _parse_options(self) -> SimulationOptionsSpec: def _parse_options(self) -> SimulationOptionsSpec:
options = self.config.get("options", {}) options = self.config.get("options", {})
allowed = {"air_density_kg_per_m3", "seed"} allowed = {"air_density_kg_per_m3", "seed", "bkg_cps"}
self._warn_unknown_keys( self._warn_unknown_keys(
section="options", section="options",
provided=set(options.keys()), provided=set(options.keys()),
@ -116,12 +116,14 @@ class ConfigParser:
defaults.DEFAULT_AIR_DENSITY defaults.DEFAULT_AIR_DENSITY
) )
seed = options.get("seed") seed = options.get("seed")
bkg_cps = options.get("bkg_cps")
if not isinstance(air_density, float) or air_density <= 0: if not isinstance(air_density, float) or air_density <= 0:
raise InvalidConfigValueError( raise InvalidConfigValueError(
"options.air_density_kg_per_m3 must be a positive float " "options.air_density_kg_per_m3 must be a positive float "
"in kg/m^3." "in kg/m^3."
) )
if ( if (
seed is not None or seed is not None or
(isinstance(seed, int) and seed <= 0) (isinstance(seed, int) and seed <= 0)
@ -130,9 +132,17 @@ class ConfigParser:
"Seed must be a positive integer value." "Seed must be a positive integer value."
) )
if bkg_cps is not None and (
not isinstance(bkg_cps, int) or bkg_cps < 0
):
raise InvalidConfigValueError(
"Background CPS must be an integer >= 0."
)
return SimulationOptionsSpec( return SimulationOptionsSpec(
air_density=air_density, air_density=air_density,
seed=seed, seed=seed,
bkg_cps=bkg_cps
) )
def _parse_path(self) -> PathSpec: def _parse_path(self) -> PathSpec:

View File

@ -18,6 +18,7 @@ class RuntimeSpec:
class SimulationOptionsSpec: class SimulationOptionsSpec:
air_density: float air_density: float
seed: int | None = None seed: int | None = None
bkg_cps: int | None = None
@dataclass @dataclass

View File

@ -109,6 +109,7 @@ def calculate_counts_along_path(
detector: "Detector", detector: "Detector",
velocity: float, velocity: float,
points_per_segment: int = 10, points_per_segment: int = 10,
bkg_cps_input: int | None = None
) -> Tuple[np.ndarray, np.ndarray]: ) -> Tuple[np.ndarray, np.ndarray]:
"""Compute the counts recorded in each acquisition period in the landscape. """Compute the counts recorded in each acquisition period in the landscape.
@ -116,6 +117,7 @@ def calculate_counts_along_path(
landscape (Landscape): _description_ landscape (Landscape): _description_
detector (Detector): _description_ detector (Detector): _description_
points_per_segment (int, optional): _description_. Defaults to 100. points_per_segment (int, optional): _description_. Defaults to 100.
bkg_cps_input (int | None, optional): Optional background CPS.
Returns: Returns:
Tuple[np.ndarray, np.ndarray]: Array of acquisition points and Tuple[np.ndarray, np.ndarray]: Array of acquisition points and
@ -148,9 +150,17 @@ def calculate_counts_along_path(
landscape, full_positions, detector landscape, full_positions, detector
) )
if bkg_cps_input is None:
bkg = generate_background( bkg = generate_background(
cps, detector, landscape.point_sources[0].isotope.E cps, detector, landscape.point_sources[0].isotope.E
) )
elif bkg_cps_input == 0:
bkg = bkg_cps_input
else:
bkg = generate_background(
cps, detector, landscape.point_sources[0].isotope.E,
lam_inp=bkg_cps_input
)
cps_with_bg = cps + bkg cps_with_bg = cps + bkg
# reshape so each segment is on a row # reshape so each segment is on a row

View File

@ -47,7 +47,8 @@ class SimulationEngine:
calculate_counts_along_path( calculate_counts_along_path(
self.landscape, self.landscape,
self.detector, self.detector,
velocity=self.runtime_spec.speed velocity=self.runtime_spec.speed,
bkg_cps_input=self.sim_spec.bkg_cps
) )
) )