From 075e95bd6ffd1a2bb4728868beb20b9a132bb935 Mon Sep 17 00:00:00 2001 From: Thomas Kiley Date: Thu, 27 Jul 2023 14:24:36 +0100 Subject: [PATCH 1/3] Add implementation of standard deviation on data --- inflammation-analysis.py | 3 ++- inflammation/models.py | 10 ++++++++++ tests/test_models.py | 13 ++++++++++++- 3 files changed, 24 insertions(+), 2 deletions(-) diff --git a/inflammation-analysis.py b/inflammation-analysis.py index e1564317..4c612feb 100644 --- a/inflammation-analysis.py +++ b/inflammation-analysis.py @@ -27,7 +27,8 @@ def main(args): for filename in InFiles: inflammation_data = models.load_csv(filename) - view_data = {'average': models.daily_mean(inflammation_data), 'max': models.daily_max(inflammation_data), 'min': models.daily_min(inflammation_data)} + view_data = {'average': models.daily_mean(inflammation_data), 'max': models.daily_max(inflammation_data), 'min': models.daily_min(inflammation_data), **(models.s_dev(inflammation_data))} + views.visualize(view_data) diff --git a/inflammation/models.py b/inflammation/models.py index 94387ee3..89cfd31e 100644 --- a/inflammation/models.py +++ b/inflammation/models.py @@ -54,3 +54,13 @@ def daily_min(data): """Calculate the daily min of a 2d inflammation data array.""" return np.min(data, axis=0) + +def s_dev(data): + """Computes and returns standard deviation for data.""" + mmm = np.mean(data, axis=0) + devs = [] + for entry in data: + devs.append((entry - mmm) * (entry - mmm)) + + s_dev2 = sum(devs) / len(data) + return {'standard deviation': s_dev2} diff --git a/tests/test_models.py b/tests/test_models.py index 292d00c4..b8adbfb0 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,14 @@ 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('data, expected_standard_deviation', [ + ([0, 0, 0], 0.0), + ([1.0, 1.0, 1.0], 0), + ([0.0, 2.0], 1.0) +]) +def test_daily_standard_deviation(data, expected_standard_deviation): + from inflammation.models import s_dev + result_data = s_dev(data)['standard deviation'] + npt.assert_approx_equal(result_data, expected_standard_deviation) From 179c581ae14737d9eea9a06cf74c36c082801f5e Mon Sep 17 00:00:00 2001 From: clmould <86794332+clmould@users.noreply.github.com> Date: Thu, 23 Nov 2023 16:06:14 +0000 Subject: [PATCH 2/3] Update models.py --- inflammation/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inflammation/models.py b/inflammation/models.py index 89cfd31e..bafeafae 100644 --- a/inflammation/models.py +++ b/inflammation/models.py @@ -57,7 +57,7 @@ def daily_min(data): def s_dev(data): """Computes and returns standard deviation for data.""" - mmm = np.mean(data, axis=0) + mean_data = daily_mean(data) devs = [] for entry in data: devs.append((entry - mmm) * (entry - mmm)) From 23f6700bc67f53eb0a03ef89ac2450584f158e3b Mon Sep 17 00:00:00 2001 From: clmould <86794332+clmould@users.noreply.github.com> Date: Fri, 24 Nov 2023 13:15:48 +0000 Subject: [PATCH 3/3] Update models.py --- inflammation/models.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inflammation/models.py b/inflammation/models.py index 6a23e357..6b1318e4 100644 --- a/inflammation/models.py +++ b/inflammation/models.py @@ -75,7 +75,7 @@ def s_dev(data): mean_data = daily_mean(data) devs = [] for entry in data: - devs.append((entry - mmm) * (entry - mmm)) + devs.append((entry - mean_data) * (entry - mean_data)) s_dev2 = sum(devs) / len(data) return {'standard deviation': s_dev2}