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
3 changes: 2 additions & 1 deletion inflammation-analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,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)

Expand Down
11 changes: 11 additions & 0 deletions inflammation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,14 @@ def daily_max(data):
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."""
number = np.mean(data, axis=0)

Choose a reason for hiding this comment

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

I don't immediately know what the purpose of the number variable is from its name. Based on its assignment, it is the mean along one axis, so perhaps it should be called data_mean or event just mean?

Copy link
Owner Author

Choose a reason for hiding this comment

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

Resolved by using np.std

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

s_dev2 = sum(devs) / len(data)
Comment on lines +38 to +43

Choose a reason for hiding this comment

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

This is an implementation of standard deviation from the ground up. We could probably replace with a call to np.std.

return {'standard deviation': s_dev2}

Choose a reason for hiding this comment

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

Returning a dict doesn't match what is returned for all of the other functions in this module. We should probably switch to just returning the calculated value.

Choose a reason for hiding this comment

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

And it mixes in responsibilites that should be with the view

19 changes: 18 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np
import numpy.testing as npt

import pytest

def test_daily_mean_zeros():
"""Test that mean function works for an array of zeros."""
Expand All @@ -29,3 +29,20 @@ def test_daily_mean_integers():
# Need to use Numpy testing functions to compare arrays
npt.assert_array_equal(daily_mean(test_input), test_result)

@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)


def test_daily_standard_deviation_on_np_array():
from inflammation.models import s_dev
data = np.array([[0.0]])
expected_std_devs = [[0.0]]
out = s_dev(data)['standard deviation']
npt.assert_array_almost_equal(data, expected_std_devs)