From e2b247e9cdb01365a6732043451efb337a830380 Mon Sep 17 00:00:00 2001 From: "@bd0982" Date: Tue, 4 Jun 2024 14:46:23 +0100 Subject: [PATCH 1/4] continuing course --- requirements.txt | 43 +++++++++++++++++++++++++++++++++++++++ tests/test_models.py | 48 +++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 1 deletion(-) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 00000000..35aec075 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,43 @@ +blinker==1.4 +command-not-found==0.3 +contourpy==1.2.1 +cryptography==3.4.8 +cycler==0.12.1 +dbus-python==1.2.18 +distro==1.7.0 +distro-info===1.1build1 +exceptiongroup==1.2.1 +fonttools==4.53.0 +httplib2==0.20.2 +importlib-metadata==4.6.4 +iniconfig==2.0.0 +jeepney==0.7.1 +keyring==23.5.0 +kiwisolver==1.4.5 +launchpadlib==1.10.16 +lazr.restfulclient==0.14.4 +lazr.uri==1.0.6 +matplotlib==3.9.0 +more-itertools==8.10.0 +netifaces==0.11.0 +numpy==1.26.4 +oauthlib==3.2.0 +packaging==24.0 +pillow==10.3.0 +pluggy==1.5.0 +PyGObject==3.42.1 +PyJWT==2.3.0 +pyparsing==2.4.7 +pytest==8.2.1 +python-apt==2.3.0+ubuntu2.1 +python-dateutil==2.9.0.post0 +PyYAML==5.4.1 +SecretStorage==3.3.1 +six==1.16.0 +systemd-python==234 +tomli==2.0.1 +ubuntu-advantage-tools==27.11.3 +ufw==0.36.1 +unattended-upgrades==0.1 +wadllib==1.3.6 +zipp==1.0.0 diff --git a/tests/test_models.py b/tests/test_models.py index 292d00c4..c966433e 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.""" @@ -37,3 +37,49 @@ def test_load_from_json(tmpdir): 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)) + From 17963e7f48188dc082a06717591f56454aa4ffdf Mon Sep 17 00:00:00 2001 From: "@bd0982" Date: Tue, 4 Jun 2024 14:59:23 +0100 Subject: [PATCH 2/4] Setting up github actions --- .github/workflows/main.yml | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 .github/workflows/main.yml 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 From 9ab9d88b9f738a8c6d52b6f5a172f682649ae18e Mon Sep 17 00:00:00 2001 From: "@bd0982" Date: Tue, 4 Jun 2024 15:03:17 +0100 Subject: [PATCH 3/4] fixed requirements.txt importing everything lol --- requirements.txt | 46 +++----------------------------------------- tests/test_models.py | 1 + 2 files changed, 4 insertions(+), 43 deletions(-) diff --git a/requirements.txt b/requirements.txt index 35aec075..4eb65962 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,43 +1,3 @@ -blinker==1.4 -command-not-found==0.3 -contourpy==1.2.1 -cryptography==3.4.8 -cycler==0.12.1 -dbus-python==1.2.18 -distro==1.7.0 -distro-info===1.1build1 -exceptiongroup==1.2.1 -fonttools==4.53.0 -httplib2==0.20.2 -importlib-metadata==4.6.4 -iniconfig==2.0.0 -jeepney==0.7.1 -keyring==23.5.0 -kiwisolver==1.4.5 -launchpadlib==1.10.16 -lazr.restfulclient==0.14.4 -lazr.uri==1.0.6 -matplotlib==3.9.0 -more-itertools==8.10.0 -netifaces==0.11.0 -numpy==1.26.4 -oauthlib==3.2.0 -packaging==24.0 -pillow==10.3.0 -pluggy==1.5.0 -PyGObject==3.42.1 -PyJWT==2.3.0 -pyparsing==2.4.7 -pytest==8.2.1 -python-apt==2.3.0+ubuntu2.1 -python-dateutil==2.9.0.post0 -PyYAML==5.4.1 -SecretStorage==3.3.1 -six==1.16.0 -systemd-python==234 -tomli==2.0.1 -ubuntu-advantage-tools==27.11.3 -ufw==0.36.1 -unattended-upgrades==0.1 -wadllib==1.3.6 -zipp==1.0.0 +numpy +pytest +pytest-cov \ No newline at end of file diff --git a/tests/test_models.py b/tests/test_models.py index c966433e..e3c0fb79 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -31,6 +31,7 @@ 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: From 750c43e3de65bc4d11c03bdcff2c0c915e606d11 Mon Sep 17 00:00:00 2001 From: "@bd0982" Date: Tue, 4 Jun 2024 15:34:46 +0100 Subject: [PATCH 4/4] Ran linter --- inflammation/models.py | 4 ++++ tests/test_models.py | 10 ++++++++++ 2 files changed, 14 insertions(+) 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/tests/test_models.py b/tests/test_models.py index e3c0fb79..50f0dff2 100644 --- a/tests/test_models.py +++ b/tests/test_models.py @@ -84,3 +84,13 @@ def test_daily_mean(test, expected): 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