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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
5 changes: 4 additions & 1 deletion inflammation-analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ 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)
'min': models.daily_min(inflammation_data),
**(models.s_dev(inflammation_data))
}


views.visualize(view_data)


Expand Down
66 changes: 56 additions & 10 deletions inflammation/compute_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,72 @@
import numpy as np

from inflammation import models, views
from unittest.mock import Mock
import numpy.testing as npt


def analyse_data(data_dir):
"""Calculate the standard deviation by day between datasets
class CSVDataSource:
def __init__(self,dir_path):
self.dir_path = dir_path

Gets all the inflammation csvs within a directory, works out the mean
inflammation value for each day across all datasets, then graphs the
standard deviation of these means."""
data_file_paths = glob.glob(os.path.join(data_dir, 'inflammation*.csv'))
def load_inflammation_data(self):
files_of_interest = 'inflammation*.csv'
data_file_paths = glob.glob(os.path.join(self.dir_path, files_of_interest))

if len(data_file_paths) == 0:
raise ValueError(f"No inflammation csv's found in path {self.dir_path}")

data = map(models.load_csv, data_file_paths)
return data


class JSONDataSource:
"""
Loads all the inflammation JSON's within a specified folder.
"""
def __init__(self, dir_path):
self.dir_path = dir_path

def load_inflammation_data(self):
data_file_paths = glob.glob(os.path.join(self.dir_path, 'inflammation*.json'))
if len(data_file_paths) == 0:
raise ValueError(f"No inflammation csv's found in path {data_dir}")
data = map(models.load_csv, data_file_paths)
raise ValueError(f"No inflammation JSON's found in path {self.dir_path}")
data = map(models.load_json, data_file_paths)
return list(data)


def compute_standard_deviation_by_day(data):
means_by_day = map(models.daily_mean, data)
means_by_day_matrix = np.stack(list(means_by_day))

daily_standard_deviation = np.std(means_by_day_matrix, axis=0)
return daily_standard_deviation


def analyse_data(data_source):
"""Calculate the standard deviation by day between datasets

Gets all the inflammation csvs within a directory, works out the mean
inflammation value for each day across all datasets, then graphs the
standard deviation of these means."""

data = data_source.load_inflammation_data()

daily_standard_deviation = compute_standard_deviation_by_day(data)

graph_data = {
'standard deviation by day': daily_standard_deviation,
}
views.visualize(graph_data)

return daily_standard_deviation
#views.visualize(graph_data)


def test_compute_data_mock_source():
from inflammation.compute_data import analyse_data
data_source = Mock()
data_source.load_inflammation_data.return_value = [[[0, 2, 0]], [[0, 1, 0]]]

result = analyse_data(data_source)
npt.assert_array_almost_equal(result, [0, math.sqrt(0.25) ,0])

24 changes: 24 additions & 0 deletions inflammation/example_with_classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import math

class Circle:
def __init__(self, radius):
self.radius = radius

def get_area(self):
return math.pi * self.radius * self.radius

class Rectangle:
def __init__(self,width,height):
self.width = width
self.height = height

def get_area(self):
return self.width * self.height

my_circle = Circle(10)
your_rectangle = Rectangle(20,30)

shapes = [my_circle, your_rectangle]

for shape in shapes:
print(shape.get_area())
15 changes: 14 additions & 1 deletion inflammation/models.py
Comment thread
sl6391 marked this conversation as resolved.
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,17 @@ def daily_min(data):
:returns: An array of minimum values of measurements for each day.
"""
return np.min(data, axis=0)



def standard_deviation(data):
"""Computes and returns standard deviation for data.
Input = 2d inflammation data
Output = Standard deviation of input data
"""
mmm = np.mean(data, axis=0)
devs = []
for entry in data:
devs.append((entry - mmm) * (entry - mmm))

standard_deviation2 = sum(devs) / len(data)
return {'standard deviation': standard_deviation2}
1 change: 0 additions & 1 deletion inflammation/views.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
"""Module containing code for plotting inflammation data."""

from matplotlib import pyplot as plt
import numpy as np


def visualize(data_dict):
Expand Down
51 changes: 51 additions & 0 deletions tests/test_compute_data.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#! /usr/bin/env python

from pathlib import Path
import numpy.testing as npt
import pytest
from unittest.mock import Mock


@pytest.mark.parametrize(
"data,expected_output",
[
([[[0, 1, 0],[0, 2, 0]]], [0, 0, 0]),
],
ids=['Two patients in same file'])

def test_compute_standard_deviation_by_day(data,expected_output):
from inflammation.compute_data import compute_standard_deviation_by_day

result = compute_standard_deviation_by_day(data)
npt.assert_almost_equal(result,expected_output)


def test_analyse_data():
from inflammation.compute_data import analyse_data, CSVDataSource
path = "data"
data_source = CSVDataSource(path)
result = analyse_data(data_source)

npt.assert_almost_equal(result, [0. , 0.22510286, 0.18157299, 0.1264423 , 0.9495481 ,
0.27118211, 0.25104719, 0.22330897, 0.89680503, 0.21573875,
1.24235548, 0.63042094, 1.57511696, 2.18850242, 0.3729574 ,
0.69395538, 2.52365162, 0.3179312 , 1.22850657, 1.63149639,
2.45861227, 1.55556052, 2.8214853 , 0.92117578, 0.76176979,
2.18346188, 0.55368435, 1.78441632, 0.26549221, 1.43938417,
0.78959769, 0.64913879, 1.16078544, 0.42417995, 0.36019114,
0.80801707, 0.50323031, 0.47574665, 0.45197398, 0.22070227])


def test_with_mocks():
print('Hello')
mock_shape1 = Mock()
mock_shape1.get_area.return_value = 10

mock_shape2 = Mock()
mock_shape2.get_area.return_value = 20

shapes = [mock_shape1,mock_shape2]

for shape in shapes:
print(shape.get_area())

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)
76 changes: 76 additions & 0 deletions venv/bin/activate
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# This file must be used with "source bin/activate" *from bash*
# you cannot run it directly

deactivate () {
# reset old environment variables
if [ -n "${_OLD_VIRTUAL_PATH:-}" ] ; then
PATH="${_OLD_VIRTUAL_PATH:-}"
export PATH
unset _OLD_VIRTUAL_PATH
fi
if [ -n "${_OLD_VIRTUAL_PYTHONHOME:-}" ] ; then
PYTHONHOME="${_OLD_VIRTUAL_PYTHONHOME:-}"
export PYTHONHOME
unset _OLD_VIRTUAL_PYTHONHOME
fi

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
hash -r
fi

if [ -n "${_OLD_VIRTUAL_PS1:-}" ] ; then
PS1="${_OLD_VIRTUAL_PS1:-}"
export PS1
unset _OLD_VIRTUAL_PS1
fi

unset VIRTUAL_ENV
if [ ! "$1" = "nondestructive" ] ; then
# Self destruct!
unset -f deactivate
fi
}

# unset irrelevant variables
deactivate nondestructive

VIRTUAL_ENV="/home/sl6391/python-intermediate-inflammation/venv"
export VIRTUAL_ENV

_OLD_VIRTUAL_PATH="$PATH"
PATH="$VIRTUAL_ENV/bin:$PATH"
export PATH

# unset PYTHONHOME if set
# this will fail if PYTHONHOME is set to the empty string (which is bad anyway)
# could use `if (set -u; : $PYTHONHOME) ;` in bash
if [ -n "${PYTHONHOME:-}" ] ; then
_OLD_VIRTUAL_PYTHONHOME="${PYTHONHOME:-}"
unset PYTHONHOME
fi

if [ -z "${VIRTUAL_ENV_DISABLE_PROMPT:-}" ] ; then
_OLD_VIRTUAL_PS1="${PS1:-}"
if [ "x(venv) " != x ] ; then
PS1="(venv) ${PS1:-}"
else
if [ "`basename \"$VIRTUAL_ENV\"`" = "__" ] ; then
# special case for Aspen magic directories
# see http://www.zetadev.com/software/aspen/
PS1="[`basename \`dirname \"$VIRTUAL_ENV\"\``] $PS1"
else
PS1="(`basename \"$VIRTUAL_ENV\"`)$PS1"
fi
fi
export PS1
fi

# This should detect bash and zsh, which have a hash command that must
# be called to get it to forget past commands. Without forgetting
# past commands the $PATH changes we made may not be respected
if [ -n "${BASH:-}" -o -n "${ZSH_VERSION:-}" ] ; then
hash -r
fi
37 changes: 37 additions & 0 deletions venv/bin/activate.csh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# This file must be used with "source bin/activate.csh" *from csh*.
# You cannot run it directly.
# Created by Davide Di Blasi <davidedb@gmail.com>.
# Ported to Python 3.3 venv by Andrew Svetlov <andrew.svetlov@gmail.com>

alias deactivate 'test $?_OLD_VIRTUAL_PATH != 0 && setenv PATH "$_OLD_VIRTUAL_PATH" && unset _OLD_VIRTUAL_PATH; rehash; test $?_OLD_VIRTUAL_PROMPT != 0 && set prompt="$_OLD_VIRTUAL_PROMPT" && unset _OLD_VIRTUAL_PROMPT; unsetenv VIRTUAL_ENV; test "\!:*" != "nondestructive" && unalias deactivate'

# Unset irrelevant variables.
deactivate nondestructive

setenv VIRTUAL_ENV "/home/sl6391/python-intermediate-inflammation/venv"

set _OLD_VIRTUAL_PATH="$PATH"
setenv PATH "$VIRTUAL_ENV/bin:$PATH"


set _OLD_VIRTUAL_PROMPT="$prompt"

if (! "$?VIRTUAL_ENV_DISABLE_PROMPT") then
if ("venv" != "") then
set env_name = "venv"
else
if (`basename "VIRTUAL_ENV"` == "__") then
# special case for Aspen magic directories
# see http://www.zetadev.com/software/aspen/
set env_name = `basename \`dirname "$VIRTUAL_ENV"\``
else
set env_name = `basename "$VIRTUAL_ENV"`
endif
endif
set prompt = "[$env_name] $prompt"
unset env_name
endif

alias pydoc python -m pydoc

rehash
Loading