From cb3f83fe918c14c37e3713f0803bcd503658eed8 Mon Sep 17 00:00:00 2001 From: ye-zhi Date: Sat, 12 Dec 2015 18:07:42 -0800 Subject: [PATCH] outlier test --- code/utils/{ => functions}/outlier.py | 0 code/utils/tests/test_outliers.py | 58 +++++++++++++++++++++++++++ 2 files changed, 58 insertions(+) rename code/utils/{ => functions}/outlier.py (100%) create mode 100644 code/utils/tests/test_outliers.py diff --git a/code/utils/outlier.py b/code/utils/functions/outlier.py similarity index 100% rename from code/utils/outlier.py rename to code/utils/functions/outlier.py diff --git a/code/utils/tests/test_outliers.py b/code/utils/tests/test_outliers.py new file mode 100644 index 0000000..0ae1ba9 --- /dev/null +++ b/code/utils/tests/test_outliers.py @@ -0,0 +1,58 @@ +""" Tests functions in outliers module +Largely lifted straight from the tests provided for diagnostics.py in HW2 and janewliang's diagnosis_script.py + +Run at the project directory with: + nosetests code/utils/tests/test_outliers.py +""" + +# Loading modules. +import os +import sys +import numpy as np +import nibabel as nib +from nose.tools import assert_equal +from numpy.testing import assert_almost_equal, assert_array_equal + + +# Add path to functions to the system path. +sys.path.append(os.path.join(os.path.dirname(__file__), "../functions/")) + +from outlier import * +project_path=os.path.join(os.path.dirname(__file__), '../../../') + +def test_vol_std(): + # create the test data + shape = (2, 3, 4) + L = np.prod(shape) + t = 10 + data_2d = np.random.normal(size=(L, t)) + test_stds = np.std(data_2d, axis=0) + data_4d = np.reshape(data_2d, shape + (t,)) + stds = vol_std(data_4d) + assert_almost_equal(test_stds, stds) + +def test_iqr_outliers(): + data = np.arange(101) + # the data is from 0 to 100, so the iqr = 50 + exp_lo = 25 - 1.5*50 + exp_hi = 75 + 1.5*50 + indices, thresholds = iqr_outliers(data) + assert_array_equal(indices, []) + assert_equal(thresholds, (exp_lo, exp_hi)) + # Reverse the data + # check the results will not change + indices, thresholds = iqr_outliers(data[::-1]) + assert_array_equal(indices, []) + assert_equal(thresholds, (exp_lo, exp_hi)) + # Add outliers + data[0] = -100 + data[1] = 200 + data[100] = 1 + indices, thresholds = iqr_outliers(data) + assert_array_equal(indices, [0, 1]) + assert_equal(thresholds, (exp_lo, exp_hi)) + # Reversed the data + indices, thresholds = iqr_outliers(data[::-1]) + assert_array_equal(indices, [99, 100]) + assert_equal(thresholds, (exp_lo, exp_hi)) +