From 69291777a8430a80ec61c4f30c9b88459d7c121b Mon Sep 17 00:00:00 2001 From: cdunn314 Date: Tue, 6 Oct 2026 15:57:34 -0400 Subject: [PATCH 1/6] Allow for user-defined function for evaluating detector efficiency --- .../activation_foils/compass.py | 8 ++- test/neutron_detection/test_compass.py | 59 +++++++++++++++++++ 2 files changed, 66 insertions(+), 1 deletion(-) diff --git a/libra_toolbox/neutron_detection/activation_foils/compass.py b/libra_toolbox/neutron_detection/activation_foils/compass.py index af8b05a..bd961e3 100644 --- a/libra_toolbox/neutron_detection/activation_foils/compass.py +++ b/libra_toolbox/neutron_detection/activation_foils/compass.py @@ -662,6 +662,7 @@ def get_gamma_emitted( channel_nb: int, search_width: float = 800, summing_method: str = 'sum_gaussian', + efficiency_function = None, ): # find right background detector @@ -687,7 +688,12 @@ def get_gamma_emitted( nb_counts_measured = np.array(nb_counts_measured) nb_counts_measured_err = np.sqrt(nb_counts_measured) - detection_efficiency = np.polyval(efficiency_coeffs, energy) + # check if there is an efficiency function provided, if so use it to get the detection efficiency + if efficiency_function is not None: + detection_efficiency = efficiency_function(energy) + else: + # otherwise, just use the polynomial coefficients to get the detection efficiency + detection_efficiency = np.polyval(efficiency_coeffs, energy) gamma_emmitted = nb_counts_measured / detection_efficiency gamma_emmitted_err = nb_counts_measured_err / detection_efficiency diff --git a/test/neutron_detection/test_compass.py b/test/neutron_detection/test_compass.py index c564ed9..0f17c76 100644 --- a/test/neutron_detection/test_compass.py +++ b/test/neutron_detection/test_compass.py @@ -834,6 +834,65 @@ def test_get_multipeak_area_two_close_peaks(): assert np.isclose(areas[i], expected_area, rtol=1e-2) +@pytest.mark.parametrize( + "efficiency_coeffs, efficiency_function, expected_efficiency", + [ + (np.array([0.0, 0.25]), None, 0.25), + (np.array([0.0, 0.5]), lambda energies: 0.125 * np.asarray(energies), 0.125), + ], +) +def test_get_gamma_emitted_uses_custom_or_polynomial_efficiency( + monkeypatch, efficiency_coeffs, efficiency_function, expected_efficiency +): + # BUILD + nuclide_reactant = Nuclide(name="TestNuclide", atomic_mass=200) + activated_nuclide = Nuclide( + name="ActivatedNuclide", + energy=[1.0], + intensity=[1.0], + half_life=10 * 24 * 3600, + ) + + reaction = Reaction( + reactant=nuclide_reactant, + product=activated_nuclide, + cross_section=20.0, + ) + + foil = ActivationFoil(reaction=reaction, mass=0.1, name="TestFoil") + measurement = compass.SampleMeasurement("sample") + measurement.foil = foil + measurement.start_time = datetime.datetime(2024, 11, 7) + measurement.stop_time = datetime.datetime(2024, 11, 7, 1) + measurement.detectors = [compass.Detector(channel_nb=3)] + measurement.get_detector(3).real_count_time = 3600 + measurement.get_detector(3).live_count_time = 3600 + measurement.get_detector(3).events = np.array([(0, 1.0), (1, 2.0), (2, 3.0)]) + + background_measurement = compass.Measurement("background") + background_measurement.detectors = [compass.Detector(channel_nb=3)] + background_measurement.get_detector(3).real_count_time = 3600 + background_measurement.get_detector(3).live_count_time = 3600 + background_measurement.get_detector(3).events = np.array([(0, 0.0), (1, 0.0)]) + + monkeypatch.setattr(compass, "get_multipeak_area", lambda *args, **kwargs: np.array([100.0])) + + # RUN + gammas_emitted = measurement.get_gamma_emitted( + background_measurement=background_measurement, + efficiency_coeffs=efficiency_coeffs, + calibration_coeffs=np.array([1.0, 0.0]), + channel_nb=3, + search_width=300, + summing_method="sum_gaussian", + efficiency_function=efficiency_function, + ) + + # TEST + expected = 100.0 / expected_efficiency + assert np.isclose(gammas_emitted[0], expected) + + @pytest.mark.parametrize( "efficiency, summing_method", [ From fe3850ca03f0c1c6cca8452b40008a2c7bc75fed Mon Sep 17 00:00:00 2001 From: cdunn314 Date: Tue, 6 Oct 2026 16:07:50 -0400 Subject: [PATCH 2/6] Added get_gamma_emitted documentation --- .../activation_foils/compass.py | 58 +++++++++++++++++++ 1 file changed, 58 insertions(+) diff --git a/libra_toolbox/neutron_detection/activation_foils/compass.py b/libra_toolbox/neutron_detection/activation_foils/compass.py index bd961e3..b22d9c7 100644 --- a/libra_toolbox/neutron_detection/activation_foils/compass.py +++ b/libra_toolbox/neutron_detection/activation_foils/compass.py @@ -664,6 +664,64 @@ def get_gamma_emitted( summing_method: str = 'sum_gaussian', efficiency_function = None, ): + """ + Estimate the emitted gamma-ray counts for the foil product nuclide. + + This method performs background-subtracted peak area extraction in the + specified detector channel, converts the measured energy scale using the + supplied calibration polynomial, and divides the measured peak counts by + the detector efficiency to estimate the total emitted gamma rays. + + Parameters + ---------- + background_measurement : Measurement + The background measurement used to subtract the continuum and + environmental counts from the sample spectrum. + efficiency_coeffs : array-like + Polynomial coefficients defining the detector efficiency as a + function of energy, used with ``np.polyval(efficiency_coeffs, energy)`` + when no custom efficiency function is provided. + calibration_coeffs : array-like + Polynomial coefficients used to convert raw detector channels to + calibrated energies before peak integration. + channel_nb : int + Detector channel number to analyze. + search_width : float, optional + Width, in energy units, used to search for and fit the product peak. + Default is 800. + summing_method : str, optional + Peak-summing strategy passed to ``get_multipeak_area``. Supported + values include ``'sum_gaussian'`` and ``'sum_histogram'``. Default is + ``'sum_gaussian'``. + efficiency_function : callable, optional + Custom function that evaluates detector efficiency as a function of + energy. If provided, it is used instead of the polynomial + efficiency coefficients provided in ``efficiency_coeffs``. + This is useful when efficiency is known from + calibration data or a fitted model instead of a simple polynomial. + + Returns + ------- + tuple[np.ndarray, np.ndarray] + A pair ``(gamma_emmitted, gamma_emmitted_err)`` containing the + estimated emitted gamma-ray counts and their propagated uncertainty. + + Notes + ----- + The effective detection efficiency is determined as follows: + + - If ``efficiency_function`` is not ``None``, then + ``detection_efficiency = efficiency_function(energy)``. + - Otherwise, ``detection_efficiency = np.polyval(efficiency_coeffs, energy)``. + + The quantity returned is therefore: + + gamma_emmitted = nb_counts_measured / detection_efficiency + + where ``nb_counts_measured`` is the background-subtracted counts under + the expected product gamma peak(s). + """ + # find right background detector background_detector = background_measurement.get_detector(channel_nb) From eebea70c336f53c9a5ef609b48193d7ba14a4119 Mon Sep 17 00:00:00 2001 From: cdunn314 Date: Tue, 6 Oct 2026 16:27:05 -0400 Subject: [PATCH 3/6] Added efficiency function kwargs --- .../neutron_detection/activation_foils/compass.py | 8 ++++++-- test/neutron_detection/test_compass.py | 9 +++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/libra_toolbox/neutron_detection/activation_foils/compass.py b/libra_toolbox/neutron_detection/activation_foils/compass.py index b22d9c7..9e0c6d6 100644 --- a/libra_toolbox/neutron_detection/activation_foils/compass.py +++ b/libra_toolbox/neutron_detection/activation_foils/compass.py @@ -663,6 +663,7 @@ def get_gamma_emitted( search_width: float = 800, summing_method: str = 'sum_gaussian', efficiency_function = None, + efficiency_function_kwargs: dict = None ): """ Estimate the emitted gamma-ray counts for the foil product nuclide. @@ -699,6 +700,9 @@ def get_gamma_emitted( efficiency coefficients provided in ``efficiency_coeffs``. This is useful when efficiency is known from calibration data or a fitted model instead of a simple polynomial. + efficiency_function_kwargs : dict, optional + Additional keyword arguments to pass to ``efficiency_function`` if + it requires extra parameters. Returns ------- @@ -711,7 +715,7 @@ def get_gamma_emitted( The effective detection efficiency is determined as follows: - If ``efficiency_function`` is not ``None``, then - ``detection_efficiency = efficiency_function(energy)``. + ``detection_efficiency = efficiency_function(energy, **efficiency_function_kwargs)``. - Otherwise, ``detection_efficiency = np.polyval(efficiency_coeffs, energy)``. The quantity returned is therefore: @@ -748,7 +752,7 @@ def get_gamma_emitted( # check if there is an efficiency function provided, if so use it to get the detection efficiency if efficiency_function is not None: - detection_efficiency = efficiency_function(energy) + detection_efficiency = efficiency_function(energy, **efficiency_function_kwargs) else: # otherwise, just use the polynomial coefficients to get the detection efficiency detection_efficiency = np.polyval(efficiency_coeffs, energy) diff --git a/test/neutron_detection/test_compass.py b/test/neutron_detection/test_compass.py index 0f17c76..a85dd39 100644 --- a/test/neutron_detection/test_compass.py +++ b/test/neutron_detection/test_compass.py @@ -835,14 +835,14 @@ def test_get_multipeak_area_two_close_peaks(): @pytest.mark.parametrize( - "efficiency_coeffs, efficiency_function, expected_efficiency", + "efficiency_coeffs, efficiency_function, efficiency_function_kwargs, expected_efficiency", [ - (np.array([0.0, 0.25]), None, 0.25), - (np.array([0.0, 0.5]), lambda energies: 0.125 * np.asarray(energies), 0.125), + (np.array([0.0, 0.25]), None, None, 0.25), + (np.array([0.0, 0.5]), lambda energies: 0.125 * np.asarray(energies), None, 0.125), ], ) def test_get_gamma_emitted_uses_custom_or_polynomial_efficiency( - monkeypatch, efficiency_coeffs, efficiency_function, expected_efficiency + monkeypatch, efficiency_coeffs, efficiency_function, efficiency_function_kwargs, expected_efficiency ): # BUILD nuclide_reactant = Nuclide(name="TestNuclide", atomic_mass=200) @@ -886,6 +886,7 @@ def test_get_gamma_emitted_uses_custom_or_polynomial_efficiency( search_width=300, summing_method="sum_gaussian", efficiency_function=efficiency_function, + efficiency_function_kwargs=efficiency_function_kwargs, ) # TEST From abcf4dce6b0dba21f568700800c0d132acff9f81 Mon Sep 17 00:00:00 2001 From: cdunn314 Date: Wed, 7 Oct 2026 19:44:28 -0400 Subject: [PATCH 4/6] Switched from kwargs to args on custom efficiency function --- .../neutron_detection/activation_foils/compass.py | 6 +++--- test/neutron_detection/test_compass.py | 12 ++++++++---- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/libra_toolbox/neutron_detection/activation_foils/compass.py b/libra_toolbox/neutron_detection/activation_foils/compass.py index 9e0c6d6..d64e68b 100644 --- a/libra_toolbox/neutron_detection/activation_foils/compass.py +++ b/libra_toolbox/neutron_detection/activation_foils/compass.py @@ -663,7 +663,7 @@ def get_gamma_emitted( search_width: float = 800, summing_method: str = 'sum_gaussian', efficiency_function = None, - efficiency_function_kwargs: dict = None + efficiency_function_args: list = None ): """ Estimate the emitted gamma-ray counts for the foil product nuclide. @@ -700,7 +700,7 @@ def get_gamma_emitted( efficiency coefficients provided in ``efficiency_coeffs``. This is useful when efficiency is known from calibration data or a fitted model instead of a simple polynomial. - efficiency_function_kwargs : dict, optional + efficiency_function_args : list, optional Additional keyword arguments to pass to ``efficiency_function`` if it requires extra parameters. @@ -752,7 +752,7 @@ def get_gamma_emitted( # check if there is an efficiency function provided, if so use it to get the detection efficiency if efficiency_function is not None: - detection_efficiency = efficiency_function(energy, **efficiency_function_kwargs) + detection_efficiency = efficiency_function(energy, *efficiency_function_args) else: # otherwise, just use the polynomial coefficients to get the detection efficiency detection_efficiency = np.polyval(efficiency_coeffs, energy) diff --git a/test/neutron_detection/test_compass.py b/test/neutron_detection/test_compass.py index a85dd39..3e14173 100644 --- a/test/neutron_detection/test_compass.py +++ b/test/neutron_detection/test_compass.py @@ -834,15 +834,19 @@ def test_get_multipeak_area_two_close_peaks(): assert np.isclose(areas[i], expected_area, rtol=1e-2) +def eff_curve_func(E, *a): + return a[0] + np.exp(a[1] * E) + + @pytest.mark.parametrize( - "efficiency_coeffs, efficiency_function, efficiency_function_kwargs, expected_efficiency", + "efficiency_coeffs, efficiency_function, efficiency_function_args, expected_efficiency", [ (np.array([0.0, 0.25]), None, None, 0.25), - (np.array([0.0, 0.5]), lambda energies: 0.125 * np.asarray(energies), None, 0.125), + (np.array([0.0, 0.5]), eff_curve_func, [-0.80, 0.0], 0.20), ], ) def test_get_gamma_emitted_uses_custom_or_polynomial_efficiency( - monkeypatch, efficiency_coeffs, efficiency_function, efficiency_function_kwargs, expected_efficiency + monkeypatch, efficiency_coeffs, efficiency_function, efficiency_function_args, expected_efficiency ): # BUILD nuclide_reactant = Nuclide(name="TestNuclide", atomic_mass=200) @@ -886,7 +890,7 @@ def test_get_gamma_emitted_uses_custom_or_polynomial_efficiency( search_width=300, summing_method="sum_gaussian", efficiency_function=efficiency_function, - efficiency_function_kwargs=efficiency_function_kwargs, + efficiency_function_args=efficiency_function_args, ) # TEST From da0f180e7ea2585fe9a4f7df57e0331984043d5e Mon Sep 17 00:00:00 2001 From: cdunn314 Date: Wed, 7 Oct 2026 19:55:06 -0400 Subject: [PATCH 5/6] Made E an array in test --- test/neutron_detection/test_compass.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/neutron_detection/test_compass.py b/test/neutron_detection/test_compass.py index 3e14173..e14e966 100644 --- a/test/neutron_detection/test_compass.py +++ b/test/neutron_detection/test_compass.py @@ -835,7 +835,11 @@ def test_get_multipeak_area_two_close_peaks(): def eff_curve_func(E, *a): - return a[0] + np.exp(a[1] * E) + eff = 0 + E = np.array(E) + for i,a_i in enumerate(a): + eff += a_i * E**i + return eff @pytest.mark.parametrize( From a9f60e2a8467529159677547a3f3828399e9d151 Mon Sep 17 00:00:00 2001 From: cdunn314 Date: Thu, 8 Oct 2026 12:29:28 -0400 Subject: [PATCH 6/6] Fixed test hopefully --- test/neutron_detection/test_compass.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test/neutron_detection/test_compass.py b/test/neutron_detection/test_compass.py index e14e966..9e5cd25 100644 --- a/test/neutron_detection/test_compass.py +++ b/test/neutron_detection/test_compass.py @@ -846,7 +846,7 @@ def eff_curve_func(E, *a): "efficiency_coeffs, efficiency_function, efficiency_function_args, expected_efficiency", [ (np.array([0.0, 0.25]), None, None, 0.25), - (np.array([0.0, 0.5]), eff_curve_func, [-0.80, 0.0], 0.20), + (np.array([0.0, 0.5]), eff_curve_func, [-0.80, 1.0], 0.20), ], ) def test_get_gamma_emitted_uses_custom_or_polynomial_efficiency( @@ -897,6 +897,7 @@ def test_get_gamma_emitted_uses_custom_or_polynomial_efficiency( efficiency_function_args=efficiency_function_args, ) + print(f"Computed gammas emitted: {gammas_emitted[0]}, Expected: {100.0 / expected_efficiency}") # TEST expected = 100.0 / expected_efficiency assert np.isclose(gammas_emitted[0], expected)