From f04c82e468cb820bc5c454873be297db1d2dfb9c Mon Sep 17 00:00:00 2001 From: RemDelaporteMathurin Date: Fri, 8 Nov 2024 15:59:59 -0500 Subject: [PATCH] added tests --- test/tritium/test_libra_run_samples.py | 83 ++++++++++++++++++++++++++ test/tritium/test_libra_samples.py | 59 ++++++++++++++++++ test/tritium/test_lsc_samples.py | 62 +++++++++++++++++++ 3 files changed, 204 insertions(+) create mode 100644 test/tritium/test_libra_run_samples.py create mode 100644 test/tritium/test_libra_samples.py create mode 100644 test/tritium/test_lsc_samples.py diff --git a/test/tritium/test_libra_run_samples.py b/test/tritium/test_libra_run_samples.py new file mode 100644 index 0000000..135c04c --- /dev/null +++ b/test/tritium/test_libra_run_samples.py @@ -0,0 +1,83 @@ +import pytest +import pint +from datetime import datetime +from libra_toolbox.tritium.lsc_measurements import LIBRARun, LIBRASample, LSCSample +from libra_toolbox.tritium.model import ureg + + +def test_get_cumulative_activity(): + # Create sample activities + activity1 = 10 * ureg.Bq + activity2 = 20 * ureg.Bq + activity3 = 30 * ureg.Bq + activity4 = 40 * ureg.Bq + + # Create LSCSample instances + sample1 = LSCSample(activity1, "Sample1") + sample2 = LSCSample(activity2, "Sample2") + sample3 = LSCSample(activity3, "Sample3") + sample4 = LSCSample(activity4, "Sample4") + + # Mark background as subtracted + sample1.background_substracted = True + sample2.background_substracted = True + sample3.background_substracted = True + sample4.background_substracted = True + + # Create LIBRASample instances + libra_sample1 = LIBRASample([sample1, sample2], "01/01/2023 12:00 PM") + libra_sample2 = LIBRASample([sample3, sample4], "01/02/2023 12:00 PM") + + # Create LIBRARun instance + libra_run = LIBRARun([libra_sample1, libra_sample2], "01/01/2023 12:00 PM") + + # Test cumulative activity + cumulative_activity = libra_run.get_cumulative_activity() + assert cumulative_activity.magnitude.tolist() == [30, 100] + assert cumulative_activity.units == ureg.Bq + + +def test_get_cumulative_activity_without_background_subtracted(): + # Create sample activities + activity1 = 10 * ureg.Bq + activity2 = 20 * ureg.Bq + + # Create LSCSample instances + sample1 = LSCSample(activity1, "Sample1") + sample2 = LSCSample(activity2, "Sample2") + + # Create LIBRASample instance + libra_sample = LIBRASample([sample1, sample2], "01/01/2023 12:00 PM") + + # Create LIBRARun instance + libra_run = LIBRARun([libra_sample], "01/01/2023 12:00 PM") + + # Test cumulative activity without background subtracted + with pytest.raises( + ValueError, + match="Background must be substracted before calculating cumulative activity", + ): + libra_run.get_cumulative_activity() + + +def test_relative_times(): + # Create LSCSample instances + sample1 = LSCSample(10 * ureg.Bq, "Sample1") + sample2 = LSCSample(20 * ureg.Bq, "Sample2") + + # Create LIBRASample instances + libra_sample1 = LIBRASample([sample1], "01/02/2023 12:00 PM") + libra_sample2 = LIBRASample([sample2], "01/03/2023 12:00 PM") + + # Create LIBRARun instance + start_time = "01/01/2023 12:00 PM" + libra_run = LIBRARun([libra_sample1, libra_sample2], start_time) + + # Test relative times + relative_times = libra_run.relative_times + assert relative_times == [ + datetime.strptime("01/02/2023 12:00 PM", "%m/%d/%Y %I:%M %p") + - datetime.strptime(start_time, "%m/%d/%Y %I:%M %p"), + datetime.strptime("01/03/2023 12:00 PM", "%m/%d/%Y %I:%M %p") + - datetime.strptime(start_time, "%m/%d/%Y %I:%M %p"), + ] diff --git a/test/tritium/test_libra_samples.py b/test/tritium/test_libra_samples.py new file mode 100644 index 0000000..432542c --- /dev/null +++ b/test/tritium/test_libra_samples.py @@ -0,0 +1,59 @@ +import pytest +from datetime import datetime, timedelta +from pint import UnitRegistry +from libra_toolbox.tritium.lsc_measurements import LIBRASample, LSCSample + +ureg = UnitRegistry() + + +def test_get_relative_time(): + sample_time = "01/01/2023 12:00 PM" + start_time = "01/01/2023 10:00 AM" + sample = LIBRASample([], sample_time) + expected_relative_time = timedelta(hours=2) + assert sample.get_relative_time(start_time) == expected_relative_time + + +def test_substract_background(): + activity1 = 10 * ureg.Bq + activity2 = 5 * ureg.Bq + sample1 = LSCSample(activity1, "Sample1") + sample2 = LSCSample(activity2, "Sample2") + background_sample = LSCSample(2 * ureg.Bq, "Background") + sample = LIBRASample([sample1, sample2], "01/01/2023 12:00 PM") + sample.substract_background(background_sample) + assert sample1.activity == 8 * ureg.Bq + assert sample2.activity == 3 * ureg.Bq + assert sample1.background_substracted + assert sample2.background_substracted + + +def test_get_soluble_activity(): + activity1 = 10 * ureg.Bq + activity2 = 5 * ureg.Bq + sample1 = LSCSample(activity1, "Sample1") + sample2 = LSCSample(activity2, "Sample2") + sample = LIBRASample([sample1, sample2], "01/01/2023 12:00 PM") + assert sample.get_soluble_activity() == 15 * ureg.Bq + + +def test_get_insoluble_activity(): + activity1 = 10 * ureg.Bq + activity2 = 5 * ureg.Bq + activity3 = 3 * ureg.Bq + sample1 = LSCSample(activity1, "Sample1") + sample2 = LSCSample(activity2, "Sample2") + sample3 = LSCSample(activity3, "Sample3") + sample = LIBRASample([sample1, sample2, sample3], "01/01/2023 12:00 PM") + assert sample.get_insoluble_activity() == 3 * ureg.Bq + + +def test_get_total_activity(): + activity1 = 10 * ureg.Bq + activity2 = 5 * ureg.Bq + activity3 = 3 * ureg.Bq + sample1 = LSCSample(activity1, "Sample1") + sample2 = LSCSample(activity2, "Sample2") + sample3 = LSCSample(activity3, "Sample3") + sample = LIBRASample([sample1, sample2, sample3], "01/01/2023 12:00 PM") + assert sample.get_total_activity() == 18 * ureg.Bq diff --git a/test/tritium/test_lsc_samples.py b/test/tritium/test_lsc_samples.py new file mode 100644 index 0000000..1fe6577 --- /dev/null +++ b/test/tritium/test_lsc_samples.py @@ -0,0 +1,62 @@ +from libra_toolbox.tritium.lsc_measurements import ( + LSCSample, + LIBRASample, + LIBRARun, + LSCFileReader, +) +from libra_toolbox.tritium.model import ureg + +from pathlib import Path +import pytest + + +def test_lscsample_init(): + activity = 1.0 * ureg.Bq + name = "Sample1" + sample = LSCSample(activity, name) + assert sample.activity == activity + assert sample.name == name + assert not sample.background_substracted + + +def test_lscsample_str(): + sample = LSCSample(1.0 * ureg.Bq, "Sample1") + assert str(sample) == "Sample Sample1" + + +def test_lscsample_substract_background(): + sample = LSCSample(1.0 * ureg.Bq, "Sample1") + background_sample = LSCSample(0.5 * ureg.Bq, "Background") + sample.substract_background(background_sample) + assert sample.activity == 0.5 * ureg.Bq + assert sample.background_substracted + + with pytest.raises(ValueError, match="Background already substracted"): + sample.substract_background(background_sample) + + +def test_lscsample_from_file(): + file_reader = LSCFileReader( + Path(__file__).parent / "TEST_CSV.csv", + vial_labels=[ + "1L-OV-1-0-1", + "1L-OV-1-0-2", + "1L-OV-1-0-3", + "1L-OV-1-0-4", + None, + "1L-IV-1-0-1", + "1L-IV-1-0-2", + "1L-IV-1-0-3", + "1L-IV-1-0-4", + None, + "Sample1", + "1L-IV-1-1-2", + "1L-IV-1-1-3", + "1L-IV-1-1-4", + ], + ) + + file_reader.read_file() + sample = LSCSample.from_file(file_reader, "Sample1") + assert sample.activity == 0.334 * ureg.Bq + assert sample.name == "Sample1"