/
test-cut.py
407 lines (327 loc) · 14.6 KB
/
test-cut.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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#-------------------------------------------------------------------------------
# Copyright 2020-2021 H2O.ai
#
# Permission is hereby granted, free of charge, to any person obtaining a
# copy of this software and associated documentation files (the "Software"),
# to deal in the Software without restriction, including without limitation
# the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the
# Software is furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
#-------------------------------------------------------------------------------
import math
import pytest
import random
from datatable import dt, stype, f, cut, FExpr
from tests import assert_equals
#-------------------------------------------------------------------------------
# Errors
#-------------------------------------------------------------------------------
def test_cut_error_noargs():
msg = r"Function datatable\.cut\(\) requires exactly 1 positional " \
r"argument, but none were given"
with pytest.raises(TypeError, match=msg):
cut()
def test_cut_error_wrong_column_type():
DT = dt.Frame([[1, 0], ["1", "0"]])
msg = r"cut\(\) can only be applied to numeric columns, instead column 1 " \
"has an stype: str32"
with pytest.raises(TypeError, match=msg):
DT[:, cut(DT)]
def test_cut_error_wrong_column_type_zero_rows():
DT = dt.Frame(str = [] / dt.str32)
msg = r"cut\(\) can only be applied to numeric columns, instead column 0 " \
"has an stype: str32"
with pytest.raises(TypeError, match=msg):
DT[:, cut(DT)]
def test_cut_error_float_nbins():
msg = "Expected an integer, instead got <class 'float'>"
DT = dt.Frame(range(10))
with pytest.raises(TypeError, match=msg):
DT[:, cut(DT, nbins = 1.5)]
def test_cut_error_noniterable_bins():
msg = "bins parameter must be a list or a tuple, instead got <class 'float'>"
DT = dt.Frame(range(10))
with pytest.raises(TypeError, match=msg):
DT[:, cut(DT, bins = 1.5)]
def test_cut_error_string_bins():
msg = "bins parameter must be a list or a tuple, instead got <class 'str'>"
DT = dt.Frame(range(10))
with pytest.raises(TypeError, match=msg):
DT[:, cut(DT, bins = "bin1")]
def test_cut_error_zero_nbins():
msg = "Number of bins must be positive, instead got: 0"
DT = dt.Frame(range(10))
with pytest.raises(ValueError, match=msg):
DT[:, cut(DT, nbins = 0)]
def test_cut_error_one_bin_edge():
msg = "To bin data at least two edges are required, instead for the frame 0 got: 1"
DT = dt.Frame(range(10))
with pytest.raises(ValueError, match=msg):
DT[:, cut(DT, bins = [dt.Frame([1])])]
def test_cut_error_none_bin_edge():
msg = "Bin edges must be numeric values only, instead for the frame 0 got None at row 2"
DT = dt.Frame(range(10))
with pytest.raises(ValueError, match=msg):
DT[:, cut(DT, bins = [dt.Frame([1, 2, None, 3])])]
def test_cut_error_bin_edges_not_increasing():
msg = "Bin edges must be strictly increasing, instead for the frame 0 at rows 2 and 3 the values are 4 and 3.99"
DT = dt.Frame(range(10))
with pytest.raises(ValueError, match=msg):
DT[:, cut(DT, bins = [dt.Frame([1, 2, 4.0, 3.99])])]
def test_cut_error_negative_nbins():
msg = "Number of bins must be positive, instead got: -10"
DT = dt.Frame(range(10))
with pytest.raises(ValueError, match=msg):
DT[:, cut(DT, nbins = -10)]
def test_cut_error_negative_nbins_list():
msg = r"All elements in nbins must be positive, got nbins\[0\]: 0"
DT = dt.Frame([[3, 1, 4], [1, 5, 9]])
with pytest.raises(ValueError, match=msg):
DT[:, cut(DT, nbins = [0, -1])]
def test_cut_error_inconsistent_nbins():
msg = ("When nbins has more than one element, its length must be the same as "
"the number of columns in the frame/expression, i.e. 2, instead got: 3")
DT = dt.Frame([[3, 1, 4], [1, 5, 9]])
with pytest.raises(ValueError, match=msg):
DT[:, cut(DT, nbins = [10, 11, 12])]
def test_cut_error_inconsistent_bins():
msg = ("Number of elements in bins must be equal to the number of columns "
"in the frame/expression, i.e. 2, instead got: 1")
DT = dt.Frame([[3, 1, 4], [1, 5, 9]])
with pytest.raises(ValueError, match=msg):
DT[:, cut(DT, bins = [dt.Frame([1, 2])])]
def test_cut_error_wrong_right():
msg = r"Argument right_closed in function datatable\.cut\(\) should " \
r"be a boolean, instead got <class 'int'>"
DT = dt.Frame(range(10))
with pytest.raises(TypeError, match=msg):
DT[:, cut(DT, right_closed = 1492)]
def test_cut_error_groupby():
msg = r"cut\(\) cannot be used in a groupby context"
DT = dt.Frame(range(10))
with pytest.raises(NotImplementedError, match=msg):
DT[:, cut(f[0]), f[0]]
#-------------------------------------------------------------------------------
# Normal
#-------------------------------------------------------------------------------
def test_cut_empty_frame():
DT = dt.Frame()
expr_cut = cut(DT)
assert isinstance(expr_cut, FExpr)
assert_equals(DT[:, f[:]], DT)
def test_cut_default_nbins():
DT = dt.Frame({"trivial": range(10)})
DT_cut = DT[:, cut(f[:])]
expr_cut = cut(DT)
assert isinstance(expr_cut, FExpr)
assert_equals(DT, DT_cut)
def test_cut_trivial_bins():
DT_data = dt.Frame({"data": range(10)})
DT_bins = dt.Frame({"bins": range(-1, 10)})
cut_fexpr = cut(f[:], bins = [DT_bins])
for i in range(5):
DT_cut = DT_data[:, cut_fexpr]
expr_cut = cut(DT_data, bins = [DT_bins])
assert isinstance(expr_cut, FExpr)
assert_equals(DT_data, DT_cut)
def test_cut_expr():
DT = dt.Frame([range(0, 30, 3), range(0, 20, 2)])
DT_cut = DT[:, cut(cut(f[0] - f[1]))]
assert_equals(dt.Frame(range(10)), DT_cut)
def test_cut_one_row_nbins():
nbins = [1, 2, 3, 4]
DT = dt.Frame([[True], [404], [3.1415926], [None]])
DT_cut_right = DT[:, cut(DT, nbins = nbins)]
DT_cut_left = DT[:, cut(DT, nbins = nbins, right_closed = False)]
assert DT_cut_right.to_list() == [[0], [0], [1], [None]]
assert DT_cut_left.to_list() == [[0], [1], [1], [None]]
def test_cut_one_row_bins():
DT_bins = [dt.Frame([0, 1]),
dt.Frame(range(1000)),
dt.Frame([-100, 3.1415926, 100]),
dt.Frame(range(5))]
DT = dt.Frame([[True], [404], [3.1415926], [None]])
DT_cut_right = DT[:, cut(DT, bins = DT_bins)]
DT_cut_left = DT[:, cut(DT, bins = DT_bins, right_closed = False)]
assert DT_cut_right.to_list() == [[0], [403], [0], [None]]
assert DT_cut_left.to_list() == [[None], [404], [1], [None]]
def test_cut_small_nbins():
nbins = [4, 2, 5, 4, 10, 3, 2, 5]
colnames = ["bool", "int_pos", "int_neg", "int", "float",
"inf_max", "inf_min", "inf"]
DT = dt.Frame(
[[True, None, False, False, True, None],
[3, None, 4, 1, 5, 4],
[-5, -1, -1, -1, None, 0],
[None, -5, -314, 0, 5, 314],
[None, 1.4, 4.1, 1.5, 5.9, 1.4],
[math.inf, 1.4, 4.1, 1.5, 5.9, 1.4],
[-math.inf, 1.4, 4.1, 1.5, 5.9, 1.4],
[-math.inf, 1.4, 4.1, math.inf, 5.9, 1.4]],
names = colnames
)
DT_ref_right = dt.Frame(
[[3, None, 0, 0, 3, None],
[0, None, 1, 0, 1, 1],
[0, 3, 3, 3, None, 4],
[None, 1, 0, 1, 2, 3],
[None, 0, 5, 0, 9, 0],
[None] * DT.nrows,
[None] * DT.nrows,
[None] * DT.nrows],
names = colnames,
stypes = [stype.int32] * DT.ncols
)
DT_ref_left = dt.Frame(
[[3, None, 0, 0, 3, None],
[1, None, 1, 0, 1, 1],
[0, 4, 4, 4, None, 4],
[None, 1, 0, 2, 2, 3],
[None, 0, 6, 0, 9, 0],
[None] * DT.nrows,
[None] * DT.nrows,
[None] * DT.nrows],
names = colnames,
stypes = [stype.int32] * DT.ncols
)
DT_cut_list = DT[:, cut(DT, nbins = nbins)]
DT_cut_tuple = DT[:, cut(DT, nbins = tuple(nbins))]
DT_cut_list_left = DT[:, cut(DT, nbins = nbins, right_closed = False)]
assert_equals(DT_ref_right, DT_cut_list)
assert_equals(DT_ref_right, DT_cut_tuple)
assert_equals(DT_ref_left, DT_cut_list_left)
def test_cut_small_bins():
DT_bins = [dt.Frame([-1, 0, 1, 2]),
dt.Frame(range(10)),
dt.Frame(range(-10, 0)),
dt.Frame([-1000, 0, 314]),
dt.Frame(range(10)),
dt.Frame([0, 1.4, 2.8, 4.2, 5.6]),
dt.Frame([0, 1.4, 2.8, 4.2, 5.6, 7.0]),
dt.Frame([-5, 0, 15])]
colnames = ["bool", "int_pos", "int_neg", "int",
"float", "inf_max", "inf_min", "inf"]
DT = dt.Frame(
[[True, None, False, False, True, None],
[3, None, 4, 1, 5, 4],
[-5, -1, -1, -1, None, 0],
[None, -5, -314, 0, 5, 314],
[None, 1.4, 4.1, 1.5, 5.9, 1.4],
[math.inf, 1.4, 4.1, 1.5, 5.9, 1.4],
[-math.inf, 1.4, -4.1, 1.5, 5.9, 1.4],
[-math.inf, 1.4, 4.1, math.inf, 5.9, 1.4]],
names = colnames
)
DT_ref_right = dt.Frame(
[[1, None, 0, 0, 1, None],
[2, None, 3, 0, 4, 3],
[4, 8, 8, 8, None, None],
[None, 0, 0, 0, 1, 1],
[None, 1, 4, 1, 5, 1],
[None, 0, 2, 1, None, 0],
[None, 0, None, 1, 4, 0],
[None, 1, 1, None, 1, 1]],
names = colnames,
stypes = [stype.int32] * DT.ncols
)
DT_ref_left = dt.Frame(
[[2, None, 1, 1, 2, None],
[3, None, 4, 1, 5, 4],
[5, None, None, None, None, None],
[None, 0, 0, 1, 1, None],
[None, 1, 4, 1, 5, 1],
[None, 1, 2, 1, None, 1],
[None, 1, None, 1, 4, 1],
[None, 1, 1, None, 1, 1]],
names = colnames,
stypes = [stype.int32] * DT.ncols
)
DT_cut_list = DT[:, cut(DT, bins = DT_bins)]
DT_cut_tuple = DT[:, cut(DT, bins = tuple(DT_bins))]
DT_cut_list_left = DT[:, cut(DT, bins = DT_bins, right_closed = False)]
assert_equals(DT_ref_right, DT_cut_list)
assert_equals(DT_ref_right, DT_cut_tuple)
assert_equals(DT_ref_left, DT_cut_list_left)
@pytest.mark.skip(reason="This test is used for dev only as may rarely fail "
"due to pandas inconsistency, see test_cut_pandas_issue_35126")
@pytest.mark.parametrize("seed", [random.getrandbits(32) for _ in range(5)])
def test_cut_vs_pandas_random_nbins(pandas, seed):
random.seed(seed)
max_nbins = 20
max_elements = 20
max_value = 100
n_elements = random.randint(1, max_elements)
nbins = [random.randint(1, max_nbins) for _ in range(3)]
right_closed = bool(random.getrandbits(1))
data = [[] for _ in range(3)]
for _ in range(n_elements):
data[0].append(random.randint(0, 1))
data[1].append(random.randint(-max_value, max_value))
data[2].append(random.random() * 2 * max_value - max_value)
DT = dt.Frame(data, types = [dt.bool8, dt.int32, dt.float64])
DT_cut = DT[:, cut(DT, nbins = nbins, right_closed = right_closed)]
PD_cut = [pandas.cut(data[i], nbins[i], labels=False, right=right_closed) for i in range(3)]
assert [list(PD_cut[i]) for i in range(3)] == DT_cut.to_list()
#-------------------------------------------------------------------------------
# pandas.cut() behaves inconsistently in this test, i.e.
#
# pandas.cut(data, nbins, labels = False)
#
# results in `[0 21 41]` bins, while it should be `[0 20 41]`.
#
# See the following issue for more details
# https://github.com/pandas-dev/pandas/issues/35126
#-------------------------------------------------------------------------------
@pytest.mark.skip(reason="This test is used for dev only")
def test_cut_pandas_issue_35126(pandas):
nbins = 42
data = [-97, 0, 97]
DT = dt.Frame(data)
DT_cut_right = DT[:, cut(DT, nbins = nbins)]
DT_cut_left = DT[:, cut(DT, nbins = nbins, right_closed = False)]
assert DT_cut_right.to_list() == [[0, 20, 41]]
assert DT_cut_left.to_list() == [[0, 21, 41]]
# Testing that Pandas results are inconsistent
PD = pandas.cut(data, nbins, labels = False)
assert list(PD) == [0, 21, 41]
@pytest.mark.parametrize("seed", [random.getrandbits(32) for _ in range(5)])
def test_cut_vs_pandas_random_bins(pandas, seed):
random.seed(seed)
max_bins = 20
max_elements = 20
max_value = 100
n_elements = random.randint(1, max_elements)
right_closed = bool(random.getrandbits(1))
DT_bins = []
bins = [[] for _ in range(3)]
for i in range(3):
nbins = random.randint(2, max_bins)
bins[i] = random.sample(range(-max_value, max_value), nbins)
bins[i].sort()
DT_bins.append(dt.Frame(bins[i]))
data = [[] for _ in range(3)]
for _ in range(n_elements):
data[0].append(random.randint(0, 1))
data[1].append(random.randint(-max_value, max_value))
data[2].append(random.random() * 2 * max_value - max_value)
DT = dt.Frame(data, stypes = [stype.bool8, stype.int32, stype.float64])
DT_cut = DT[:, cut(DT, bins = DT_bins, right_closed = right_closed)]
PD_cut = [pandas.cut(data[i], bins[i], labels=False, right=right_closed) for i in range(3)]
PD_l = [list(PD_cut[i]) for i in range(3)]
# Replace `nan`s with `None` for pandas
for i in range(3):
PD_l[i] = [None if math.isnan(PD_l[i][j]) else PD_l[i][j] for j in range(n_elements)]
assert PD_l == DT_cut.to_list()