diff --git a/libra_toolbox/tritium/lsc_measurements.py b/libra_toolbox/tritium/lsc_measurements.py index 77b81ee..6fd916c 100644 --- a/libra_toolbox/tritium/lsc_measurements.py +++ b/libra_toolbox/tritium/lsc_measurements.py @@ -73,6 +73,11 @@ def substract_background(self, background_sample: "LSCSample"): if self.background_substracted: raise ValueError("Background already substracted") self.activity -= background_sample.activity + if self.activity.magnitude < 0: + warnings.warn( + f"Activity of {self.name} is negative after substracting background. Setting to zero." + ) + self.activity = 0 * ureg.Bq self.background_substracted = True @staticmethod diff --git a/test/tritium/test_lsc_samples.py b/test/tritium/test_lsc_samples.py index 1c550ca..ab18423 100644 --- a/test/tritium/test_lsc_samples.py +++ b/test/tritium/test_lsc_samples.py @@ -35,6 +35,18 @@ def test_lscsample_substract_background(): sample.substract_background(background_sample) +def test_lscsample_substract_background_when_negative(): + sample = LSCSample(0.5 * ureg.Bq, "Sample1") + background_sample = LSCSample(1.0 * ureg.Bq, "Background") + with pytest.warns( + UserWarning, + match=f"Activity of {sample.name} is negative after substracting background. Setting to zero.", + ): + sample.substract_background(background_sample) + assert sample.activity == 0 * ureg.Bq + assert sample.background_substracted + + def test_lscsample_from_file(): file_reader = LSCFileReader( Path(__file__).parent / "TEST_CSV.csv",