File: test_selection.py

package info (click to toggle)
pypy3 7.3.19%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 212,236 kB
  • sloc: python: 2,098,316; ansic: 540,565; sh: 21,462; asm: 14,419; cpp: 4,451; makefile: 4,209; objc: 761; xml: 530; exp: 499; javascript: 314; pascal: 244; lisp: 45; csh: 12; awk: 4
file content (422 lines) | stat: -rw-r--r-- 15,488 bytes parent folder | download | duplicates (8)
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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
from pypy.module.micronumpy.test.test_base import BaseNumpyAppTest

class AppTestSorting(BaseNumpyAppTest):
    def test_argsort_dtypes(self):
        from numpy import array, arange
        assert array(2.0).argsort() == 0
        nnp = self.non_native_prefix
        for dtype in ['int', 'float', 'int16', 'float32', 'uint64',
                      nnp + 'i2', complex]:
            a = array([6, 4, -1, 3, 8, 3, 256+20, 100, 101], dtype=dtype)
            exp = list(a)
            exp = sorted(range(len(exp)), key=exp.__getitem__)
            c = a.copy()
            res = a.argsort()
            assert (res == exp).all(), 'Failed sortng %r\na=%r\nres=%r\nexp=%r' % (dtype,a,res,exp)
            assert (a == c).all() # not modified

            a = arange(100, dtype=dtype)
            assert (a.argsort() == a).all()

    def test_argsort_ndim(self):
        from numpy import array
        a = array([[4, 2], [1, 3]])
        assert (a.argsort() == [[1, 0], [0, 1]]).all()
        a = array(range(10) + range(10) + range(10))
        b = a.argsort()
        assert ((b[:3] == [0, 10, 20]).all() or
                (b[:3] == [0, 20, 10]).all())
        #trigger timsort 'run' mode which calls arg_getitem_slice
        a = array(range(100) + range(100) + range(100))
        b = a.argsort()
        assert ((b[:3] == [0, 100, 200]).all() or
                (b[:3] == [0, 200, 100]).all())
        a = array([[[]]]).reshape(3,4,0)
        b = a.argsort()
        assert b.size == 0

    def test_argsort_random(self):
        from numpy import array
        from _random import Random
        rnd = Random(1)
        a = array([rnd.random() for i in range(512*2)]).reshape(512,2)
        a.argsort()

    def test_argsort_axis(self):
        from numpy import array
        a = array([])
        for axis in [None, -1, 0]:
            assert a.argsort(axis=axis).shape == (0,)
        a = array([[4, 2], [1, 3]])
        assert (a.argsort(axis=None) == [2, 1, 3, 0]).all()
        assert (a.argsort(axis=-1) == [[1, 0], [0, 1]]).all()
        assert (a.argsort(axis=0) == [[1, 0], [0, 1]]).all()
        assert (a.argsort(axis=1) == [[1, 0], [0, 1]]).all()
        a = array([[3, 2, 1], [1, 2, 3]])
        assert (a.argsort(axis=0) == [[1, 0, 0], [0, 1, 1]]).all()
        assert (a.argsort(axis=1) == [[2, 1, 0], [0, 1, 2]]).all()

    def test_sort_dtypes(self):
        from numpy import array, arange
        for dtype in ['int', 'float', 'int16', 'float32', 'uint64',
                      'i2', complex]:
            a = array([6, 4, -1, 3, 8, 3, 256+20, 100, 101], dtype=dtype)
            exp = sorted(list(a))
            a.sort()
            assert (a == exp).all(), 'Failed sorting %r\n%r\n%r' % (dtype, a, exp)

            a = arange(100, dtype=dtype)
            c = a.copy()
            a.sort()
            assert (a == c).all(), 'Failed sortng %r\na=%r\nc=%r' % (dtype,a,c)

    def test_sort_nonnative(self):
        from numpy import array
        nnp = self.non_native_prefix
        for dtype in [ nnp + 'i2']:
            a = array([6, 4, -1, 3, 8, 3, 256+20, 100, 101], dtype=dtype)
            b = array([-1, 3, 3, 4, 6, 8, 100, 101, 256+20], dtype=dtype)
            c = a.copy()
            import sys
            if '__pypy__' in sys.builtin_module_names:
                exc = raises(NotImplementedError, a.sort)
                assert exc.value[0].find('supported') >= 0
            #assert (a == b).all(), \
            #    'a,orig,dtype %r,%r,%r' % (a,c,dtype)

    def test_sort_noncontiguous(self):
        from numpy import array
        x = array([[2, 10], [1, 11]])
        assert (x[:, 0].argsort() == [1, 0]).all()
        x[:, 0].sort()
        assert (x == [[1, 10], [2, 11]]).all()

# tests from numpy/tests/test_multiarray.py
    def test_sort_corner_cases(self):
        # test ordering for floats and complex containing nans. It is only
        # necessary to check the lessthan comparison, so sorts that
        # only follow the insertion sort path are sufficient. We only
        # test doubles and complex doubles as the logic is the same.

        # check doubles
        from numpy import array, zeros, arange
        from math import isnan
        nan = float('nan')
        a = array([nan, 1, 0])
        b = a.copy()
        b.sort()
        assert [isnan(bb) for bb in b] == [isnan(aa) for aa in a[::-1]]
        assert (b[:2] == a[::-1][:2]).all()

        b = a.argsort()
        assert (b == [2, 1, 0]).all()

        # check complex
        a = zeros(9, dtype='complex128')
        a.real += [nan, nan, nan, 1, 0, 1, 1, 0, 0]
        a.imag += [nan, 1, 0, nan, nan, 1, 0, 1, 0]
        b = a.copy()
        b.sort()
        assert [isnan(bb) for bb in b] == [isnan(aa) for aa in a[::-1]]
        assert (b[:4] == a[::-1][:4]).all()

        b = a.argsort()
        assert (b == [8, 7, 6, 5, 4, 3, 2, 1, 0]).all()

        # all c scalar sorts use the same code with different types
        # so it suffices to run a quick check with one type. The number
        # of sorted items must be greater than ~50 to check the actual
        # algorithm because quick and merge sort fall over to insertion
        # sort for small arrays.
        a = arange(101)
        b = a[::-1].copy()
        for kind in ['q', 'm', 'h'] :
            msg = "scalar sort, kind=%s" % kind
            c = a.copy();
            c.sort(kind=kind)
            assert (c == a).all(), msg
            c = b.copy();
            c.sort(kind=kind)
            assert (c == a).all(), msg

        # test complex sorts. These use the same code as the scalars
        # but the compare fuction differs.
        ai = a*1j + 1
        bi = b*1j + 1
        for kind in ['q', 'm', 'h'] :
            msg = "complex sort, real part == 1, kind=%s" % kind
            c = ai.copy();
            c.sort(kind=kind)
            assert (c == ai).all(), msg
            c = bi.copy();
            c.sort(kind=kind)
            assert (c == ai).all(), msg
        ai = a + 1j
        bi = b + 1j
        for kind in ['q', 'm', 'h'] :
            msg = "complex sort, imag part == 1, kind=%s" % kind
            c = ai.copy();
            c.sort(kind=kind)
            assert (c == ai).all(), msg
            c = bi.copy();
            c.sort(kind=kind)
            assert (c == ai).all(), msg

        # check axis handling. This should be the same for all type
        # specific sorts, so we only check it for one type and one kind
        a = array([[3, 2], [1, 0]])
        b = array([[1, 0], [3, 2]])
        c = array([[2, 3], [0, 1]])
        d = a.copy()
        d.sort(axis=0)
        assert (d == b).all(), "test sort with axis=0"
        d = a.copy()
        d.sort(axis=1)
        assert (d == c).all(), "test sort with axis=1"
        d = a.copy()
        d.sort()
        assert (d == c).all(), "test sort with default axis"

    def test_sort_corner_cases_string_records(self):
        from numpy import array, dtype
        import sys
        if '__pypy__' in sys.builtin_module_names:
            skip('not implemented yet in PyPy')
        # test string sorts.
        s = 'aaaaaaaa'
        a = array([s + chr(i) for i in range(101)])
        b = a[::-1].copy()
        for kind in ['q', 'm', 'h'] :
            msg = "string sort, kind=%s" % kind
            c = a.copy();
            c.sort(kind=kind)
            assert (c == a).all(), msg
            c = b.copy();
            c.sort(kind=kind)
            assert (c == a).all(), msg


        # test record array sorts.
        dt =dtype([('f', float), ('i', int)])
        a = array([(i, i) for i in range(101)], dtype = dt)
        b = a[::-1]
        for kind in ['q', 'h', 'm'] :
            msg = "object sort, kind=%s" % kind
            c = a.copy();
            c.sort(kind=kind)
            assert (c == a).all(), msg
            c = b.copy();
            c.sort(kind=kind)
            assert (c == a).all(), msg

    def test_sort_unicode(self):
        import sys
        from numpy import array
        # test unicode sorts.
        s = 'aaaaaaaa'
        a = array([s + chr(i) for i in range(101)], dtype=unicode)
        b = a[::-1].copy()
        for kind in ['q', 'm', 'h']:
            msg = "unicode sort, kind=%s" % kind
            c = a.copy()
            if '__pypy__' in sys.builtin_module_names:
                exc = raises(NotImplementedError, "c.sort(kind=kind)")
                assert 'non-numeric types' in exc.value.message
            else:
                c.sort(kind=kind)
                assert (c == a).all(), msg
            c = b.copy()
            if '__pypy__' in sys.builtin_module_names:
                exc = raises(NotImplementedError, "c.sort(kind=kind)")
                assert 'non-numeric types' in exc.value.message
            else:
                c.sort(kind=kind)
                assert (c == a).all(), msg

    def test_sort_objects(self):
        # test object array sorts.
        from numpy import empty
        import sys
        if '__pypy__' in sys.builtin_module_names:
            skip('not implemented yet in PyPy')
        try:
            a = empty((101,), dtype=object)
        except:
            skip('object type not supported yet')
        a[:] = list(range(101))
        b = a[::-1]
        for kind in ['q', 'h', 'm'] :
            msg = "object sort, kind=%s" % kind
            c = a.copy();
            c.sort(kind=kind)
            assert (c == a).all(), msg
            c = b.copy();
            c.sort(kind=kind)
            assert (c == a).all(), msg

    def test_sort_datetime(self):
        from numpy import arange
        # test datetime64 sorts.
        try:
            a = arange(0, 101, dtype='datetime64[D]')
        except:
            skip('datetime type not supported yet')
        b = a[::-1]
        for kind in ['q', 'h', 'm'] :
            msg = "datetime64 sort, kind=%s" % kind
            c = a.copy();
            c.sort(kind=kind)
            assert (c == a).all(), msg
            c = b.copy();
            c.sort(kind=kind)
            assert (c == a).all(), msg

        # test timedelta64 sorts.
        a = arange(0, 101, dtype='timedelta64[D]')
        b = a[::-1]
        for kind in ['q', 'h', 'm'] :
            msg = "timedelta64 sort, kind=%s" % kind
            c = a.copy();
            c.sort(kind=kind)
            assert (c == a).all(), msg
            c = b.copy();
            c.sort(kind=kind)
            assert (c == a).all(), msg

    def test_sort_order(self):
        from numpy import array, zeros
        from sys import byteorder, builtin_module_names
        if '__pypy__' in builtin_module_names:
            skip('not implemented yet in PyPy')
        # Test sorting an array with fields
        x1 = array([21, 32, 14])
        x2 = array(['my', 'first', 'name'])
        x3=array([3.1, 4.5, 6.2])
        r=zeros(3, dtype=[('id','i'),('word','S5'),('number','f')])
        r['id'] = x1
        r['word'] = x2
        r['number'] = x3

        r.sort(order=['id'])
        assert (r['id'] == [14, 21, 32]).all()
        assert (r['word'] == ['name', 'my', 'first']).all()
        assert max(abs(r['number'] - [6.2, 3.1, 4.5])) < 1e-6

        r.sort(order=['word'])
        assert (r['id'] == [32, 21, 14]).all()
        assert (r['word'] == ['first', 'my', 'name']).all()
        assert max(abs(r['number'] - [4.5, 3.1, 6.2])) < 1e-6

        r.sort(order=['number'])
        assert (r['id'] == [21, 32, 14]).all()
        assert (r['word'] == ['my', 'first', 'name']).all()
        assert max(abs(r['number'] - [3.1, 4.5, 6.2])) < 1e-6

        if byteorder == 'little':
            strtype = '>i2'
        else:
            strtype = '<i2'
        mydtype = [('name', 'S5'), ('col2', strtype)]
        r = array([('a', 1), ('b', 255), ('c', 3), ('d', 258)],
                     dtype= mydtype)
        r.sort(order='col2')
        assert (r['col2'] == [1, 3, 255, 258]).all()
        assert (r == array([('a', 1), ('c', 3), ('b', 255), ('d', 258)],
                                 dtype=mydtype)).all()

# tests from numpy/core/tests/test_regression.py
    def test_sort_bigendian(self):
        from numpy import array, dtype
        import sys

        # little endian sorting for big endian machine
        # is not yet supported! IMPL ME
        if sys.byteorder == 'little':
            a = array(range(11), dtype='float64')
            c = a.astype(dtype('<f8'))
            c.sort()
            assert max(abs(a-c)) < 1e-32

    def test_string_argsort_with_zeros(self):
        import numpy as np
        import sys
        x = np.fromstring("\x00\x02\x00\x01", dtype="|S2")
        if '__pypy__' in sys.builtin_module_names:
            exc = raises(NotImplementedError, "x.argsort(kind='m')")
            assert 'non-numeric types' in exc.value.message
            exc = raises(NotImplementedError, "x.argsort(kind='q')")
            assert 'non-numeric types' in exc.value.message
        else:
            assert (x.argsort(kind='m') == np.array([1, 0])).all()
            assert (x.argsort(kind='q') == np.array([1, 0])).all()

    def test_string_sort_with_zeros(self):
        import numpy as np
        import sys
        x = np.fromstring("\x00\x02\x00\x01", dtype="S2")
        y = np.fromstring("\x00\x01\x00\x02", dtype="S2")
        if '__pypy__' in sys.builtin_module_names:
            exc = raises(NotImplementedError, "x.sort(kind='q')")
            assert 'non-numeric types' in exc.value.message
        else:
            x.sort(kind='q')
            assert (x == y).all()

    def test_string_mergesort(self):
        import numpy as np
        import sys
        x = np.array(['a'] * 32)
        if '__pypy__' in sys.builtin_module_names:
            exc = raises(NotImplementedError, "x.argsort(kind='m')")
            assert 'non-numeric types' in exc.value.message
        else:
            assert (x.argsort(kind='m') == np.arange(32)).all()

    def test_searchsort(self):
        import numpy as np

        a = np.array(2)
        raises(ValueError, a.searchsorted, 3)

        a = np.arange(1, 6)

        ret = a.searchsorted(3)
        assert ret == 2
        assert isinstance(ret, np.generic)

        ret = a.searchsorted(np.array(3))
        assert ret == 2
        assert isinstance(ret, np.generic)

        ret = a.searchsorted(np.array([]))
        assert isinstance(ret, np.ndarray)
        assert ret.shape == (0,)

        ret = a.searchsorted(np.array([3]))
        assert ret == 2
        assert isinstance(ret, np.ndarray)

        ret = a.searchsorted(np.array([[2, 3]]))
        assert (ret == [1, 2]).all()
        assert ret.shape == (1, 2)

        ret = a.searchsorted(3, side='right')
        assert ret == 3
        assert isinstance(ret, np.generic)

        assert a.searchsorted(3.1) == 3
        assert a.searchsorted(3.9) == 3

        exc = raises(ValueError, a.searchsorted, 3, side=None)
        assert str(exc.value) == "expected nonempty string for keyword 'side'"
        exc = raises(ValueError, a.searchsorted, 3, side='')
        assert str(exc.value) == "expected nonempty string for keyword 'side'"
        exc = raises(ValueError, a.searchsorted, 3, side=2)
        assert str(exc.value) == "expected nonempty string for keyword 'side'"

        ret = a.searchsorted([-10, 10, 2, 3])
        assert (ret == [0, 5, 1, 2]).all()

        import sys
        if '__pypy__' in sys.builtin_module_names:
            raises(NotImplementedError, "a.searchsorted(3, sorter=range(6))")