-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_models.py
More file actions
120 lines (100 loc) · 3.73 KB
/
test_models.py
File metadata and controls
120 lines (100 loc) · 3.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""Tests for statistics functions within the Model layer."""
import os
import numpy as np
import numpy.testing as npt
import pytest
from inflammation.models import daily_mean
from inflammation.models import daily_max
from inflammation.models import daily_min
from inflammation.models import load_json
@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."""
npt.assert_array_equal(daily_mean(np.array(test)), np.array(expected))
@pytest.mark.parametrize(
"test, expected",
[
([[0, 0], [0, 0], [0, 0]], [0, 0]),
([[3, 4], [5, 6], [7, 8]], [7, 8]),
([[4, -2, 5], [1, -6, 2], [-4, -1, 9]], [4, -1, 9]),
],
)
def test_daily_max(test, expected):
"""Test that the max function works for an array of zeros and positive integers."""
# Need to use Numpy testing functions to compare arrays
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]),
([[5, 6], [7, 8], [9, 10]], [5, 6]),
([[4, -2, 5], [1, -6, 2], [-4, -1, 9]], [-4, -6, 2]),
],
)
def test_daily_min(test, expected):
"""Test that max function works for an array of zeros and positive integers."""
npt.assert_array_equal(daily_min(np.array(test)), np.array(expected))
def test_daily_min_string():
"""Test for TypeError when passing strings"""
with pytest.raises(TypeError):
error_expected = daily_min([["Hello", "there"], ["General", "Kenobi"]])
def test_load_from_json(tmpdir):
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('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)
@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]], None),
([[1, 1, 1], [1, 1, 1], [1, 1, 1]], [[1, 1, 1], [1, 1, 1], [1, 1, 1]], None),
(
[[float("nan"), 1, 1], [1, 1, 1], [1, 1, 1]],
[[0, 1, 1], [1, 1, 1], [1, 1, 1]],
None,
),
(
[[1, 2, 3], [4, 5, float("nan")], [7, 8, 9]],
[[0.33, 0.67, 1], [0.8, 1, 0], [0.78, 0.89, 1]],
None,
),
(
[[-1, 2, 3], [4, 5, 6], [7, 8, 9]],
[[0, 0.67, 1], [0.67, 0.83, 1], [0.78, 0.89, 1]],
ValueError,
),
(
[[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."""
from inflammation.models import patient_normalise
if expect_raises is not None:
with pytest.raises(expect_raises):
npt.assert_almost_equal(
patient_normalise(np.array(test)), np.array(expected), decimal=2
)
else:
npt.assert_almost_equal(
patient_normalise(np.array(test)), np.array(expected), decimal=2
)