Skip to content

Commit

Permalink
added normalise func and testing func
Browse files Browse the repository at this point in the history
  • Loading branch information
oana-ctrl committed Jul 4, 2024
1 parent bd2a1f2 commit 7236d82
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
9 changes: 9 additions & 0 deletions inflammation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
35 changes: 34 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
_ = 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)

0 comments on commit 7236d82

Please sign in to comment.