forked from benb111/outlier-detect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_outlierdetect.py
203 lines (165 loc) · 7.24 KB
/
test_outlierdetect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#!/usr/bin/env python
# encoding: utf-8
"""
test_outlierdetect.py
Created by Ben Birnbaum on 2012-08-27.
Unit tests for outlierdetect.py. To run:
python test_outlierdetect.py
You must have scipy and pandas installed for the tests to load and run.
"""
import unittest
import numpy as np
import pandas as pd
from outlierdetect import *
from outlierdetect import _get_frequencies
from outlierdetect import _STATS_AVAILABLE
def float_eq(a, b, eps=.001):
return abs(a - b) < eps
if _STATS_AVAILABLE:
class TestMultinomialModel(unittest.TestCase):
def setUp(self):
self.model = MultinomialModel()
def test_compute_outlier_scores(self):
frequencies = {
'a' : {'y' : 12, 'n' : 23, '-' : 11},
'b' : {'y' : 23, 'n' : 49, '-' : 39},
'c' : {'y' : 16, 'n' : 12, '-' : 14},
}
outlier_scores= self.model.compute_outlier_scores(frequencies)
self.assertEquals(sorted(outlier_scores.keys()), ['a', 'b', 'c'])
self.assertTrue(float_eq(outlier_scores['a'], 1.3593))
self.assertTrue(float_eq(outlier_scores['b'], 3.2995))
self.assertTrue(float_eq(outlier_scores['c'], 3.7355))
def test_numeric_response_values(self):
frequencies = {
'a' : {1 : 12, 2 : 23, 3 : 11},
'b' : {1 : 23, 2 : 49, 3 : 39},
'c' : {1 : 16, 2 : 12, 3 : 14},
}
outlier_scores = self.model.compute_outlier_scores(frequencies)
self.assertTrue(float_eq(outlier_scores['a'], 1.3593))
self.assertTrue(float_eq(outlier_scores['b'], 3.2995))
self.assertTrue(float_eq(outlier_scores['c'], 3.7355))
class TestSValueModel(unittest.TestCase):
def setUp(self):
self.model = SValueModel()
def test_compute_outlier_scores(self):
frequencies = {
'a' : {'y' : 8, 'n' : 1, '-' : 1},
'b' : {'y' : 14, 'n' : 4, '-' : 2},
'c' : {'y' : 1, 'n' : 0, '-' : 1},
'd' : {'y' : 9, 'n' : 1, '-' : 0},
'e' : {'y' : 18, 'n' : 12, '-' : 0},
}
outlier_scores = self.model.compute_outlier_scores(frequencies)
self.assertEqual(sorted(outlier_scores.keys()), ['a', 'b', 'c', 'd', 'e'])
self.assertTrue(float_eq(outlier_scores['a'], .333333))
self.assertTrue(float_eq(outlier_scores['b'], .333333))
self.assertTrue(float_eq(outlier_scores['c'], 2.333333))
self.assertTrue(float_eq(outlier_scores['d'], 1))
self.assertTrue(float_eq(outlier_scores['e'], 1.6666667))
def test_numeric_response_values(self):
frequencies = {
'a' : {1 : 8, 2 : 1, 3 : 1},
'b' : {1 : 14, 2 : 4, 3 : 2},
'c' : {1 : 1, 2 : 0, 3 : 1},
'd' : {1 : 9, 2 : 1, 3 : 0},
'e' : {1 : 18, 2 : 12, 3 : 0},
}
outlier_scores = self.model.compute_outlier_scores(frequencies)
self.assertEqual(sorted(outlier_scores.keys()), ['a', 'b', 'c', 'd', 'e'])
self.assertTrue(float_eq(outlier_scores['a'], .333333))
self.assertTrue(float_eq(outlier_scores['b'], .333333))
self.assertTrue(float_eq(outlier_scores['c'], 2.333333))
self.assertTrue(float_eq(outlier_scores['d'], 1))
self.assertTrue(float_eq(outlier_scores['e'], 1.6666667))
class TestGetFrequencies(unittest.TestCase):
def setUp(self):
self.data_rec_array = np.array([
('a', 'yes'),
('b', 'no'),
('a', 'yes'),
('a', 'yes'),
('b', 'no' ),
('a', 'no' ),
], dtype=[('interviewer', 'a1'), ('question', 'a3')])
self.data_pandas = pd.DataFrame({
'interviewer' : ['a', 'b', 'a', 'a', 'b', 'a'],
'question' : ['yes', 'no', 'yes', 'yes', 'no', 'no'],
})
self.agg_to_data = {
'a': self.data_pandas[self.data_pandas['interviewer'] == 'a'],
'b': self.data_pandas[self.data_pandas['interviewer'] == 'b'],
}
def test_get_frequencies_rec_array(self):
freq, _ = _get_frequencies(self.data_rec_array, 'question', ['yes', 'no'], 'interviewer', 'a', self.agg_to_data)
self.assertEquals(
freq,
{'yes' : 3, 'no' : 1})
freq, _ = _get_frequencies(self.data_rec_array, 'question', ['yes', 'no'], 'interviewer', 'b', self.agg_to_data)
self.assertEquals(
freq,
{'yes' : 0, 'no' : 2})
def test_get_frequencies_pandas(self):
freq, _ = _get_frequencies(self.data_pandas, 'question', ['yes', 'no'], 'interviewer', 'a', self.agg_to_data)
self.assertEquals(
freq,
{'yes' : 3, 'no' : 1})
freq, _ = _get_frequencies(self.data_pandas, 'question', ['yes', 'no'], 'interviewer', 'b', self.agg_to_data)
self.assertEquals(
freq,
{'yes' : 0, 'no' : 2})
class TestInterfaceFunctions(unittest.TestCase):
def setUp(self):
self.data_rec_array = np.array([
('a', 'n', 'n'),
('a', 'y', 'y'),
('a', 'n', 'y'),
('a', 'n', 'n'),
('b', 'n', 'y'),
('b', 'n', 'n'),
('b', 'y', 'n'),
('b', 'n', 'n'),
('b', 'n', 'n'),
('b', 'y', 'n'),
('c', 'n', 'y'),
('c', 'y', 'y'),
('c', 'n', 'y'),
('c', 'n', 'n'),
('c', 'y', 'n'),
('c', 'n', 'n'),
], dtype=[('interviewer', 'a1'), ('q1', 'a1'), ('q2', 'a1')])
self.data_pandas = pd.DataFrame(self.data_rec_array)
self.q1_frequencies = {
'a' : {'y' : 1, 'n' : 3},
'b' : {'y' : 2, 'n' : 4},
'c' : {'y' : 2, 'n' : 4},
}
self.q2_frequencies = {
'a' : {'y' : 2, 'n' : 2},
'b' : {'y' : 1, 'n' : 5},
'c' : {'y' : 3, 'n' : 3},
}
def _test_function_using_model(self, f, model, data):
outlier_scores, _ = f(data, 'interviewer', ['q1', 'q2'])
q1_scores = model.compute_outlier_scores(self.q1_frequencies)
q2_scores = model.compute_outlier_scores(self.q2_frequencies)
for interviewer in ['a', 'b', 'c']:
self.assertEquals(outlier_scores[interviewer]['q1'], q1_scores[interviewer])
self.assertEquals(outlier_scores[interviewer]['q2'], q2_scores[interviewer])
if _STATS_AVAILABLE:
def test_run_mma(self):
self._test_function_using_model(run_mma, MultinomialModel(), self.data_rec_array)
self._test_function_using_model(run_mma, MultinomialModel(), self.data_pandas)
def test_run_sva(self):
self._test_function_using_model(run_sva, SValueModel(), self.data_rec_array)
self._test_function_using_model(run_sva, SValueModel(), self.data_pandas)
def test_works_when_some_interviewers_are_missing_values(self):
run_sva(self.data_rec_array[2:], 'interviewer', ['q1'])
run_sva(self.data_pandas[2:], 'interviewer', ['q1'])
if _STATS_AVAILABLE:
run_mma(self.data_rec_array[2:], 'interviewer', ['q1'])
run_mma(self.data_pandas[2:], 'interviewer', ['q1'])
if __name__ == '__main__':
unittest.main()