Skip to content

Commit

Permalink
added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
RemDelaporteMathurin committed Nov 8, 2024
1 parent 7f8ced2 commit f04c82e
Show file tree
Hide file tree
Showing 3 changed files with 204 additions and 0 deletions.
83 changes: 83 additions & 0 deletions test/tritium/test_libra_run_samples.py
Original file line number Diff line number Diff line change
@@ -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"),
]
59 changes: 59 additions & 0 deletions test/tritium/test_libra_samples.py
Original file line number Diff line number Diff line change
@@ -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
62 changes: 62 additions & 0 deletions test/tritium/test_lsc_samples.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit f04c82e

Please sign in to comment.