test_histogram.py 9.16 KB
Newer Older
1
2
import json

Sascha Herzinger's avatar
Sascha Herzinger committed
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
import pytest
import pandas as pd

from fractalis.analytics.tasks.histogram.main import HistogramTask


class TestHistogramTask:

    task = HistogramTask()

    def test_correct_output(self):
        df = pd.DataFrame([[100, 'foo', 1],
                           [101, 'foo', 2],
                           [102, 'foo', 3],
                           [103, 'foo', 4],
                           [104, 'foo', 5],
                           [105, 'foo', 6],
                           [106, 'foo', 7],
                           [107, 'foo', 8],
                           [108, 'foo', 9],
                           [109, 'foo', 10]],
                          columns=['id', 'feature', 'value'])
        cat_df = pd.DataFrame([[100, 'cat', 'A'],
                               [101, 'cat', 'B'],
                               [102, 'cat', 'A'],
                               [103, 'cat', 'B'],
                               [104, 'cat', 'A'],
                               [105, 'cat', 'B'],
                               [106, 'cat', 'A'],
                               [107, 'cat', 'B'],
                               [108, 'cat', 'A'],
                               [109, 'cat', 'B']],
                              columns=['id', 'feature', 'value'])
        result = self.task.main(id_filter=[],
                                subsets=[],
                                data=df,
                                categories=[cat_df])
40
41
        assert all([key in result for key in
                    ['stats', 'subsets', 'categories', 'label']])
Sascha Herzinger's avatar
Sascha Herzinger committed
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
        assert 'A' in result['stats']
        assert 'B' in result['stats']
        assert 0 in result['stats']['A']
        assert all([stat in result['stats']['A'][0] for stat in
                    ['hist', 'bin_edges', 'mean', 'median', 'variance']])

    def test_can_handle_nas(self):
        df = pd.DataFrame([[100, 'foo', float('nan')],
                           [101, 'foo', 2],
                           [102, 'foo', float('nan')],
                           [103, 'foo', 4],
                           [104, 'foo', float('nan')],
                           [105, 'foo', 6],
                           [106, 'foo', float('nan')],
                           [107, 'foo', 8],
                           [108, 'foo', float('nan')],
                           [109, 'foo', 10]],
                          columns=['id', 'feature', 'value'])
        result = self.task.main(id_filter=[],
                                subsets=[],
                                data=df,
                                categories=[])
        assert result['stats'][''][0]['median'] == 6
        assert result['stats'][''][0]['mean'] == 6
        assert result['stats'][''][0]['variance'] == 8

    def test_can_handle_negatives(self):
        df = pd.DataFrame([[100, 'foo', -2],
                           [101, 'foo', 2],
                           [102, 'foo', -4],
                           [103, 'foo', 4],
                           [104, 'foo', -6],
                           [105, 'foo', 6],
                           [106, 'foo', -8],
                           [107, 'foo', 8],
                           [108, 'foo', -10],
                           [109, 'foo', 10]],
                          columns=['id', 'feature', 'value'])
        result = self.task.main(id_filter=[],
                                subsets=[],
                                data=df,
                                categories=[])
        assert result['stats'][''][0]['median'] == 0
        assert result['stats'][''][0]['mean'] == 0

    def test_can_handle_small_groups(self):
        df = pd.DataFrame([[100, 'foo', 1],
                           [101, 'foo', 2],
                           [102, 'foo', float('nan')],
                           [103, 'foo', 4],
                           [104, 'foo', float('nan')],
                           [105, 'foo', 6],
                           [106, 'foo', float('nan')],
                           [107, 'foo', 8],
                           [108, 'foo', float('nan')],
                           [109, 'foo', 10]],
                          columns=['id', 'feature', 'value'])
        cat_df = pd.DataFrame([[100, 'cat', 'A'],
                               [101, 'cat', 'B'],
                               [102, 'cat', 'A'],
                               [103, 'cat', 'B'],
                               [104, 'cat', 'A'],
                               [105, 'cat', 'B'],
                               [106, 'cat', 'A'],
                               [107, 'cat', 'B'],
                               [108, 'cat', 'A'],
                               [109, 'cat', 'B']],
                              columns=['id', 'feature', 'value'])
        result = self.task.main(id_filter=[],
                                subsets=[],
                                data=df,
                                categories=[cat_df])
        assert result['stats']['A'][0]['median'] == 1
        assert result['stats']['A'][0]['mean'] == 1
        assert result['stats']['A'][0]['variance'] == 0

    def test_skips_empty_groups(self):
        df = pd.DataFrame([[100, 'foo', float('nan')],
                           [101, 'foo', 2],
                           [102, 'foo', float('nan')],
                           [103, 'foo', 4],
                           [104, 'foo', float('nan')],
                           [105, 'foo', 6],
                           [106, 'foo', float('nan')],
                           [107, 'foo', 8],
                           [108, 'foo', float('nan')],
                           [109, 'foo', 10]],
                          columns=['id', 'feature', 'value'])
        cat_df = pd.DataFrame([[100, 'cat', 'A'],
                               [101, 'cat', 'B'],
                               [102, 'cat', 'A'],
                               [103, 'cat', 'B'],
                               [104, 'cat', 'A'],
                               [105, 'cat', 'B'],
                               [106, 'cat', 'A'],
                               [107, 'cat', 'B'],
                               [108, 'cat', 'A'],
                               [109, 'cat', 'B']],
                              columns=['id', 'feature', 'value'])
        result = self.task.main(id_filter=[],
                                subsets=[],
                                data=df,
                                categories=[cat_df])
        assert 'A' not in result['stats']
        assert 'B' in result['stats']

    def test_throws_error_if_all_groups_empty(self):
        df = pd.DataFrame([[100, 'foo', float('nan')],
                           [101, 'foo', float('nan')],
                           [102, 'foo', float('nan')],
                           [103, 'foo', float('nan')],
                           [104, 'foo', float('nan')],
                           [105, 'foo', float('nan')],
                           [106, 'foo', float('nan')],
                           [107, 'foo', float('nan')],
                           [108, 'foo', float('nan')],
                           [109, 'foo', float('nan')]],
                          columns=['id', 'feature', 'value'])
        cat_df = pd.DataFrame([[100, 'cat', 'A'],
                               [101, 'cat', 'B'],
                               [102, 'cat', 'A'],
                               [103, 'cat', 'B'],
                               [104, 'cat', 'A'],
                               [105, 'cat', 'B'],
                               [106, 'cat', 'A'],
                               [107, 'cat', 'B'],
                               [108, 'cat', 'A'],
                               [109, 'cat', 'B']],
                              columns=['id', 'feature', 'value'])
        with pytest.raises(ValueError) as e:
            self.task.main(id_filter=[],
                           subsets=[],
                           data=df,
                           categories=[cat_df])
            assert 'selected numerical variable must be non-empty' in e
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
204
205

    def test_output_is_json_serializable(self):
        df = pd.DataFrame([[100, 'foo', 1],
                           [101, 'foo', 2],
                           [102, 'foo', 3],
                           [103, 'foo', 4],
                           [104, 'foo', 5],
                           [105, 'foo', 6],
                           [106, 'foo', 7],
                           [107, 'foo', 8],
                           [108, 'foo', 9],
                           [109, 'foo', 10]],
                          columns=['id', 'feature', 'value'])
        cat_df = pd.DataFrame([[100, 'cat', 'A'],
                               [101, 'cat', 'B'],
                               [102, 'cat', 'A'],
                               [103, 'cat', 'B'],
                               [104, 'cat', 'A'],
                               [105, 'cat', 'B'],
                               [106, 'cat', 'A'],
                               [107, 'cat', 'B'],
                               [108, 'cat', 'A'],
                               [109, 'cat', 'B']],
                              columns=['id', 'feature', 'value'])
        result = self.task.main(id_filter=[],
                                subsets=[],
                                data=df,
                                categories=[cat_df])
        json.dumps(result)