From 7236d827ce87bbc4436666920259488b51c44496 Mon Sep 17 00:00:00 2001 From: oana Date: Thu, 4 Jul 2024 13:10:14 +0200 Subject: [PATCH] added normalise func and testing func --- inflammation/models.py | 9 +++++++++ tests/test_models.py | 35 ++++++++++++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/inflammation/models.py b/inflammation/models.py index ef2122ea..86390c53 100644 --- a/inflammation/models.py +++ b/inflammation/models.py @@ -32,3 +32,12 @@ def daily_min(data): """Calculate the daily min of a 2D inflammation data array.""" return np.min(data, axis=0) +def patient_normalise(data): + """Normalise patient data from a 2D inflammation data array.""" + maximum = np.max(data, axis=1) + assert (maximum !=0).all() + with np.errstate(invalid='ignore', divide='ignore'): + normalised = data / maximum[:, np.newaxis] + normalised[np.isnan(normalised)] = 0 + normalised[normalised < 0] = 0 + return normalised diff --git a/tests/test_models.py b/tests/test_models.py index 012a73a6..c1726e97 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -67,4 +67,37 @@ def test_daily_min_string(test): from inflammation.models import daily_min with pytest.raises(TypeError): - error_expected = daily_min(test) \ No newline at end of file + _ = daily_min(test) + +@pytest.mark.parametrize( + "test, expected, expect_raises", + [ + ([[0, 0, 0], [0, 0, 0], [0, 0, 0]], + [[0, 0, 0], [0, 0, 0], [0, 0, 0]], + AssertionError), + ( + "hello", + None, + ValueError, + ), + ([[1, 1, 1], [1, 1, 1], [1, 1, 1]], + [[1, 1, 1], [1, 1, 1], [1, 1, 1]], + None), + ([[-1, -1, 1], [-1, -1, 1], [-1, -1, 1]], + [[0, 0, 1], [0, 0, 1], [0, 0, 1]], + None), + ([[1, 2, 3], [4, 5, 6], [7, 8, 9]], + [[0.33, 0.67, 1], [0.67, 0.83, 1], [0.78, 0.89, 1]], + None), + ]) +def test_patient_normalise(test, expected,expect_raises): + """Test normalisation works for arrays of one and positive integers. + Test with a relative and absolute tolerance of 0.01.""" + from inflammation.models import patient_normalise + if expect_raises is not None: + with pytest.raises(expect_raises): + result = patient_normalise(np.array(test)) + npt.assert_allclose(result, np.array(expected), rtol=1e-2, atol=1e-2) + else: + result = patient_normalise(np.array(test)) + npt.assert_allclose(result, np.array(expected), rtol=1e-2, atol=1e-2)