Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions inflammation-analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,13 @@ def main(args):
for filename in in_files:
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)

Expand Down
10 changes: 10 additions & 0 deletions inflammation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ def daily_min(data):
return np.min(data, axis=0)


def s_dev(data):
"""Computes and returns standard deviation for data."""
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function description can be made longer with more detail. For instance, in what format is the data outputted? Unless we look at the code itself, there is no way of knowing it outputs a dictionary object -- a behaviour different from the other functions in the code -- so I would recommend saying this in the docstring!

mean_data = daily_mean(data)
devs = []
for entry in data:
devs.append((entry - mean_data) * (entry - mean_data))

s_dev2 = sum(devs) / len(data)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing a square root?

return {'standard deviation': s_dev2}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To allow this function to be more general (i.e., so it can be easily used elsewhere in the code), consider making it return s_dev simply as a list, rather than a dictionary object. Then you can turn it into a dictionary in the inflammation_analysis.py file


def patient_normalise(data):
"""
Normalise patient data from a 2D inflammation data array.
Expand Down
11 changes: 11 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
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
Expand Down Expand Up @@ -67,6 +68,16 @@ def test_load_from_json(tmpdir):
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",
[
Expand Down