diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml new file mode 100644 index 00000000..dd94cab4 --- /dev/null +++ b/.github/workflows/main.yml @@ -0,0 +1,34 @@ +name: CI + +# We can specify which Github events will trigger a CI build +on: push + +# now define a single job 'build' (but could define more) +jobs: + + build: + + # we can also specify the OS to run tests on + runs-on: ubuntu-latest + + # a job is a seq of steps + steps: + + # Next we need to checkout out repository, and set up Python + # A 'name' is just an optional label shown in the log - helpful to clarify progress - and can be anything + - name: Checkout repository + uses: actions/checkout@v2 + + - name: Set up Python 3.9 + uses: actions/setup-python@v2 + with: + python-version: "3.9" + + - name: Install Python dependencies + run: | + python3 -m pip install --upgrade pip + pip3 install -r requirements.txt + + - name: Test with PyTest + run: | + python -m pytest --cov=inflammation.models tests/test_models.py \ No newline at end of file diff --git a/inflammation/models.py b/inflammation/models.py index 94387ee3..df8f97ae 100644 --- a/inflammation/models.py +++ b/inflammation/models.py @@ -54,3 +54,7 @@ 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.""" + max_data = np.max(data, axis=1) + return data / max_data[:, np.newaxis] \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..4eb65962 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +numpy +pytest +pytest-cov \ No newline at end of file diff --git a/tests/test_models.py b/tests/test_models.py index 292d00c4..50f0dff2 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -3,7 +3,7 @@ import numpy as np import numpy.testing as npt import os - +import pytest def test_daily_mean_zeros(): """Test that mean function works for an array of zeros.""" @@ -31,9 +31,66 @@ def test_daily_mean_integers(): npt.assert_array_equal(daily_mean(test_input), test_result) def test_load_from_json(tmpdir): + from inflammation.models import load_json example_path = os.path.join(tmpdir, 'example.json') with open(example_path, 'w') as temp_json_file: temp_json_file.write('[{"observations":[1, 2, 3]},{"observations":[4, 5, 6]}]') result = load_json(example_path) npt.assert_array_equal(result, [[1, 2, 3], [4, 5, 6]]) + + +@pytest.mark.parametrize( + "test, expected", + [ + ([ [0, 0, 0], [0, 0, 0], [0, 0, 0] ], [0, 0, 0]), + ([ [4, 2, 5], [1, 6, 2], [4, 1, 9] ], [4, 6, 9]), + ([ [4, -2, 5], [1, -6, 2], [-4, -1, 9] ], [4, -1, 9]), + ]) + +def test_daily_max(test, expected): + """Test max function works for zeroes, positive integers, mix of positive/negative integers.""" + from inflammation.models import daily_max + npt.assert_array_equal(daily_max(np.array(test)), np.array(expected)) + + +@pytest.mark.parametrize( + "test, expected", + [ + ([ [0, 0, 0], [0, 0, 0], [0, 0, 0] ], [0, 0, 0]), + ([ [4, 2, 5], [1, 6, 2], [4, 1, 9] ], [1, 1, 2]), + ([ [4, -2, 5], [1, -6, 2], [-4, -1, 9] ], [-4, -6, 2]), + ]) +def test_daily_min(test, expected): + """Test min function works for zeroes, positive integers, mix of positive/negative integers.""" + from inflammation.models import daily_min + npt.assert_array_equal(daily_min(np.array(test)), np.array(expected)) + +def test_daily_min_string(): + """Test for TypeError when passing strings""" + from inflammation.models import daily_min + + with pytest.raises(TypeError): + error_expected = daily_min ([['Hello', 'there'], ['General', 'Kenobi']]) + +@pytest.mark.parametrize( + "test, expected", + [ + ([ [0, 0], [0, 0], [0, 0] ], [0, 0]), + ([ [1, 2], [3, 4], [5, 6] ], [3, 4]), + ]) +def test_daily_mean(test, expected): + """Test mean function works for array of zeroes and positive integers.""" + from inflammation.models import daily_mean + npt.assert_array_equal(daily_mean(np.array(test)), np.array(expected)) + +@pytest.mark.parametrize( + "test, expected", + [ + ([[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[0.33, 0.67, 1], [0.67, 0.83, 1], [0.78, 0.89, 1]]) + ]) +def test_patient_normalise(test, expected): + """Test normalisation works for arrays of one and positive integers. + Assumption that test accuracy of two decimal places is sufficient.""" + from inflammation.models import patient_normalise + npt.assert_almost_equal(patient_normalise(np.array(test)), np.array(expected), decimal=2) \ No newline at end of file