Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add implementation of standard deviation on data #1

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -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)

Expand Down
10 changes: 10 additions & 0 deletions inflammation/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
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!

mmm = np.mean(data, axis=0)
Copy link

Choose a reason for hiding this comment

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

mmm is not a descriptive variable name. Perhaps consider renaming it to mean_data

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

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

13 changes: 12 additions & 1 deletion tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand Down Expand Up @@ -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)