-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_main.py
227 lines (182 loc) · 7.21 KB
/
test_main.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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
import numpy as np
from easyquery import Query, QueryMaker
def test_valid_init():
"""
test valid Query object creation
"""
q1 = Query() # noqa: F841
q2 = Query(None) # noqa: F841
q3 = Query('x > 2')
q4 = Query(lambda t: t['x'] > 2) # noqa: F841
q5 = Query((lambda c: c > 2, 'x')) # noqa: F841
q6 = Query('x > 2', lambda t: t['x'] > 2, (lambda c: c > 2, 'x')) # noqa: F841
q7 = Query(q3) # noqa: F841
q8 = Query(q3, 'x > 2') # noqa: F841
def check_invalid_init(*queries):
try:
q = Query(*queries) # noqa: F841
except ValueError:
pass
else:
raise AssertionError
def test_invalid_init():
"""
test invalid Query object creation
"""
for q in (1, [lambda x: x > 1, 'a'], (lambda x: x > 1,), ('a', lambda x: x > 1)):
check_invalid_init(q)
def gen_test_table():
return np.array(
[
(1, 5, 4.5, "abcd"),
(1, 1, 6.2, "pqrs"),
(3, 2, 0.5, "asdf"),
(5, 5, -3.5, "wxyz"),
(-2, -5, np.inf, "fwmt"),
],
dtype=np.dtype([('a', '<i8'), ('b', '<i8'), ('c', '<f8'), ('s', '<U4')]),
)
def check_query_on_table(table, query_object, true_mask=None):
if true_mask is None:
true_mask = np.ones(len(table), bool)
stable1, stable2 = query_object.split(table)
assert (query_object.filter(table) == table[true_mask]).all(), 'filter not correct'
assert (stable1 == table[true_mask]).all(), 'split not correct'
assert (stable2 == table[~true_mask]).all(), 'split not correct'
assert query_object.count(table) == np.count_nonzero(true_mask), 'count not correct'
assert (query_object.mask(table) == true_mask).all(), 'mask not correct'
assert (query_object.where(table) == np.flatnonzero(true_mask)).all(), 'where not correct'
def check_query_on_dict_table(table, query_object, true_mask=None):
if true_mask is None:
true_mask = np.ones(len(next(table.values())), bool)
ftable = query_object.filter(table)
ftable_true = {k: table[k][true_mask] for k in table}
stable1, stable2 = query_object.split(table)
stable1_true = ftable_true
stable2_true = {k: table[k][~true_mask] for k in table}
assert set(ftable) == set(ftable_true), 'filter not correct'
assert all((ftable[k] == ftable_true[k]).all() for k in ftable), 'filter not correct'
assert set(stable1) == set(stable1_true), 'split not correct'
assert all((stable1[k] == stable1_true[k]).all() for k in ftable), 'split not correct'
assert set(stable2) == set(stable2_true), 'split not correct'
assert all((stable2[k] == stable2_true[k]).all() for k in ftable), 'split not correct'
assert query_object.count(table) == np.count_nonzero(true_mask), 'count not correct'
assert (query_object.mask(table) == true_mask).all(), 'mask not correct'
assert (query_object.where(table) == np.flatnonzero(true_mask)).all(), 'where not correct'
def test_simple_query():
"""
test simple queries
"""
t = gen_test_table()
check_query_on_table(t, Query(), None)
check_query_on_table(t, Query('a > 3'), t['a'] > 3)
check_query_on_table(t, Query('a == 100'), t['a'] == 100)
check_query_on_table(t, Query('b > c'), t['b'] > t['c'])
check_query_on_table(t, Query('a < 3', 'b > c'), (t['a'] < 3) & (t['b'] > t['c']))
def do_compound_query(t, query_class, check_query):
q1 = query_class('a == 1')
m1 = t['a'] == 1
q2 = query_class('a == b')
m2 = t['a'] == t['b']
q3 = 'b > c'
m3 = t['b'] > t['c']
q4 = ~~q2
m4 = ~~m2
q5 = q1 & q2 | q3
m5 = m1 & m2 | m3
q6 = ~q1 | q2 ^ q3
m6 = ~m1 | m2 ^ m3
q7 = q5 ^ q6
m7 = m5 ^ m6
q7 |= q2
m7 |= m2
q8 = q3 | q4
m8 = m3 | m4
q9 = q5.copy()
m9 = m5
check_query(t, q1, m1)
check_query(t, q2, m2)
check_query(t, q4, m4)
check_query(t, q5, m5)
check_query(t, q6, m6)
check_query(t, q7, m7)
check_query(t, q8, m8)
check_query(t, q9, m9)
def test_compound_query():
"""
test compound queries
"""
do_compound_query(gen_test_table(), Query, check_query_on_table)
class DictQuery(Query):
@staticmethod
def _get_table_len(table):
return len(next(table.values()))
@staticmethod
def _mask_table(table, mask):
return {k: v[mask] for k, v in table.items()}
def test_derive_class():
t = gen_test_table()
t = {k: t[k] for k in t.dtype.names}
do_compound_query(t, DictQuery, check_query_on_dict_table)
def test_variable_names():
q1 = Query('log(a) > b**2.0')
q2 = Query((lambda x, y: x + y < 1, 'c', 'd'))
q3 = q1 & 'a + 2'
q4 = ~q2
q5 = q1 ^ q2
q6 = Query('sin(5)')
q7 = Query()
assert set(q1.variable_names) == {'a', 'b'}
assert set(q2.variable_names) == {'c', 'd'}
assert set(q3.variable_names) == {'a', 'b'}
assert set(q4.variable_names) == {'c', 'd'}
assert set(q5.variable_names) == {'a', 'b', 'c', 'd'}
assert set(q6.variable_names) == set()
assert set(q7.variable_names) == set()
def test_filter_column_slice():
t = gen_test_table()
q = Query('a > 2')
assert (q.filter(t, 'b') == t['b'][t['a'] > 2]).all()
assert (q.split(t, 'b')[1] == t['b'][~(t['a'] > 2)]).all()
q = Query('a > 2', 'b < 2')
assert (q.filter(t, 'c') == t['c'][(t['a'] > 2) & (t['b'] < 2)]).all()
assert (q.split(t, 'c')[1] == t['c'][~((t['a'] > 2) & (t['b'] < 2))]).all()
q = Query(None)
assert (q.filter(t, 'a') == t['a']).all()
assert len(q.split(t, 'a')[1]) == 0
def test_query_maker():
t = gen_test_table()
test_elements = [1, 2, 3]
check_query_on_table(t, QueryMaker.in1d("a", test_elements), np.in1d(t["a"], test_elements))
check_query_on_table(t, QueryMaker.isin("a", test_elements), np.isin(t["a"], test_elements))
check_query_on_table(t, QueryMaker.equal("s", "abcd"), t["s"] == "abcd")
check_query_on_table(t, QueryMaker.equals("s", "abcd"), t["s"] == "abcd")
check_query_on_table(t, QueryMaker.not_equal("s", "abcd"), t["s"] != "abcd")
check_query_on_table(t, QueryMaker.startswith("s", "a"), np.char.startswith(t["s"], "a"))
check_query_on_table(t, QueryMaker.endswith("s", "s"), np.char.endswith(t["s"], "s"))
check_query_on_table(t, QueryMaker.contains("s", "a"), np.char.find(t["s"], "a") > -1)
check_query_on_table(t, QueryMaker.find("s", "a"), np.char.find(t["s"], "a") > -1)
check_query_on_table(t, QueryMaker.isfinite("c"), np.isfinite(t["c"]))
check_query_on_table(t, QueryMaker.isnan("c"), np.isnan(t["c"]))
check_query_on_table(t, QueryMaker.isnotnan("c"), ~np.isnan(t["c"]))
check_query_on_table(t, QueryMaker.isclose("a", "b"), np.isclose(t["a"], t["b"]))
check_query_on_table(
t,
QueryMaker.reduce_compare(["a", "b"], np.max, np.greater, 1),
np.maximum(t["a"], t["b"]) > 1,
)
check_query_on_table(
t,
QueryMaker.reduce_compare(["a", "b"], np.mean, np.less_equal, 2),
0.5 * (t["a"] + t["b"]) <= 2,
)
assert QueryMaker.equal_columns("s", "s").mask(t).all()
if __name__ == '__main__':
test_valid_init()
test_invalid_init()
test_simple_query()
test_compound_query()
test_derive_class()
test_variable_names()
test_filter_column_slice()
test_query_maker()