test_truth.py 10.1 KB
Newer Older
root's avatar
root committed
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
import warnings

import numpy

from cupy import testing


def _calc_out_shape(shape, axis, keepdims):
    if axis is None:
        axis = list(range(len(shape)))
    elif isinstance(axis, int):
        axis = [axis]
    else:
        axis = list(axis)

    shape = numpy.array(shape)

    if keepdims:
        shape[axis] = 1
    else:
        shape[axis] = -1
        shape = filter(lambda x: x != -1, shape)
    return tuple(shape)


@testing.parameterize(
    *testing.product(
        {'f': ['all', 'any'],
         'x': [numpy.arange(24).reshape(2, 3, 4) - 10,
               numpy.zeros((2, 3, 4)),
               numpy.ones((2, 3, 4)),
               numpy.zeros((0, 3, 4)),
               numpy.ones((0, 3, 4))],
         'axis': [None, (0, 1, 2), 0, 1, 2, (0, 1)],
         'keepdims': [False, True]}))
class TestAllAny:

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_without_out(self, xp, dtype):
        x = xp.asarray(self.x).astype(dtype)
        return getattr(xp, self.f)(x, self.axis, None, self.keepdims)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_with_out(self, xp, dtype):
        x = xp.asarray(self.x).astype(dtype)
        out_shape = _calc_out_shape(x.shape, self.axis, self.keepdims)
        out = xp.empty(out_shape, dtype=x.dtype)
        getattr(xp, self.f)(x, self.axis, out, self.keepdims)
        return out


@testing.parameterize(
    *testing.product(
        {'f': ['all', 'any'],
         'x': [numpy.array([[[numpy.nan]]]),
               numpy.array([[[numpy.nan, 0]]]),
               numpy.array([[[numpy.nan, 1]]]),
               numpy.array([[[numpy.nan, 0, 1]]])],
         'axis': [None, (0, 1, 2), 0, 1, 2, (0, 1)],
         'keepdims': [False, True]}))
class TestAllAnyWithNaN:

    @testing.for_dtypes(
        (numpy.float64, numpy.float32, numpy.float16, numpy.bool_))
    @testing.numpy_cupy_array_equal()
    def test_without_out(self, xp, dtype):
        x = xp.asarray(self.x).astype(dtype)
        return getattr(xp, self.f)(x, self.axis, None, self.keepdims)

    @testing.for_dtypes(
        (numpy.float64, numpy.float32, numpy.float16, numpy.bool_))
    @testing.numpy_cupy_array_equal()
    def test_with_out(self, xp, dtype):
        x = xp.asarray(self.x).astype(dtype)
        out_shape = _calc_out_shape(x.shape, self.axis, self.keepdims)
        out = xp.empty(out_shape, dtype=x.dtype)
        getattr(xp, self.f)(x, self.axis, out, self.keepdims)
        return out


class TestAllAnyAlias:
    @testing.numpy_cupy_array_equal()
    def test_alltrue(self, xp):
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', DeprecationWarning)
            return xp.alltrue(xp.array([1, 2, 3]))

    @testing.numpy_cupy_array_equal()
    def test_sometrue(self, xp):
        with warnings.catch_warnings():
            warnings.simplefilter('ignore', DeprecationWarning)
            return xp.sometrue(xp.array([0]))


@testing.parameterize(
    *testing.product(
        {'f': ['in1d', 'isin'],
         'shape_x': [
             (0, ),
             (3, ),
             (2, 3),
             (2, 1, 3),
             (2, 0, 1),
             (2, 0, 1, 1)
        ],
            'shape_y': [
             (0, ),
             (3, ),
             (2, 3),
             (2, 1, 3),
             (2, 0, 1),
             (2, 0, 1, 1)
        ],
            'assume_unique': [False, True],
            'invert': [False, True]}))
class TestIn1DIsIn:

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test(self, xp, dtype):
        x = testing.shaped_arange(self.shape_x, xp, dtype)
        y = testing.shaped_arange(self.shape_y, xp, dtype)
        if xp is numpy and self.f == 'isin':
            return xp.in1d(x, y, self.assume_unique, self.invert)\
                .reshape(x.shape)
        return getattr(xp, self.f)(x, y, self.assume_unique, self.invert)


class TestSetdiff1d:

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_setdiff1d_same_arrays(self, xp, dtype):
        x = xp.array([1, 2, 3, 4, 5], dtype=dtype)
        y = xp.array([1, 2, 3, 4, 5], dtype=dtype)
        return xp.setdiff1d(x, y, assume_unique=True)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_setdiff1d_diff_size_arr_inputs(self, xp, dtype):
        x = xp.array([3, 4, 9, 1, 5, 4], dtype=dtype)
        y = xp.array([8, 7, 3, 9, 0], dtype=dtype)
        return xp.setdiff1d(x, y)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_setdiff1d_diff_elements(self, xp, dtype):
        x = xp.array([3, 4, 9, 1, 5, 4], dtype=dtype)
        y = xp.array([8, 7, 3, 9, 0], dtype=dtype)
        return xp.setdiff1d(x, y, assume_unique=True)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_setdiff1d_with_2d(self, xp, dtype):
        x = testing.shaped_random((2, 3), xp, dtype=dtype)
        y = testing.shaped_random((3, 5), xp, dtype=dtype)
        return xp.setdiff1d(x, y, assume_unique=True)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_setdiff1d_with_duplicate_elements(self, xp, dtype):
        x = xp.array([1, 2, 3, 2, 2, 6], dtype=dtype)
        y = xp.array([3, 4, 2, 1, 1, 9], dtype=dtype)
        return xp.setdiff1d(x, y)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_setdiff1d_empty_arr(self, xp, dtype):
        x = xp.array([], dtype=dtype)
        y = xp.array([], dtype=dtype)
        return xp.setdiff1d(x, y)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_setdiff1d_more_dim(self, xp, dtype):
        x = testing.shaped_arange((2, 3, 4, 8), xp, dtype=dtype)
        y = testing.shaped_arange((5, 4, 2), xp, dtype=dtype)
        return xp.setdiff1d(x, y, assume_unique=True)

    @testing.numpy_cupy_array_equal()
    def test_setdiff1d_bool_val(self, xp):
        x = xp.array([True, False, True])
        y = xp.array([False])
        return xp.setdiff1d(x, y)


class TestSetxor1d:

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_setxor1d_same_arrays(self, xp, dtype):
        x = xp.array([1, 2, 3, 4, 5], dtype=dtype)
        y = xp.array([1, 2, 3, 4, 5], dtype=dtype)
        return xp.setxor1d(x, y, assume_unique=True)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_setxor1d_diff_size_arr_inputs(self, xp, dtype):
        x = xp.array([3, 4, 9, 1, 5, 4], dtype=dtype)
        y = xp.array([8, 7, 3, 9, 0], dtype=dtype)
        return xp.setxor1d(x, y)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_setxor1d_diff_elements(self, xp, dtype):
        x = xp.array([3, 4, 9, 1, 5, 4], dtype=dtype)
        y = xp.array([8, 7, 3, 9, 0], dtype=dtype)
        return xp.setxor1d(x, y, assume_unique=True)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_setxor1d_with_2d(self, xp, dtype):
        x = testing.shaped_random((2, 3), xp, dtype=dtype)
        y = testing.shaped_random((3, 5), xp, dtype=dtype)
        return xp.setxor1d(x, y)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_setxor1d_with_duplicate_elements(self, xp, dtype):
        x = xp.array([1, 2, 3, 2, 2, 6], dtype=dtype)
        y = xp.array([3, 4, 2, 1, 1, 9], dtype=dtype)
        return xp.setxor1d(x, y)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_setxor1d_empty_arr(self, xp, dtype):
        x = xp.array([], dtype=dtype)
        y = xp.array([], dtype=dtype)
        return xp.setxor1d(x, y)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_setxor1d_more_dim(self, xp, dtype):
        x = testing.shaped_arange((2, 3, 4, 8), xp, dtype=dtype)
        y = testing.shaped_arange((5, 4, 2), xp, dtype=dtype)
        return xp.setxor1d(x, y)

    @testing.numpy_cupy_array_equal()
    def test_setxor1d_bool_val(self, xp):
        x = xp.array([True, False, True])
        y = xp.array([False])
        return xp.setxor1d(x, y)


class TestIntersect1d:

    @testing.for_all_dtypes(no_bool=True)
    @testing.numpy_cupy_array_equal()
    def test_one_dim_with_unique_values(self, xp, dtype):
        a = xp.array([1, 2, 3, 4, 5], dtype=dtype)
        b = xp.array([1, 2, 3, 4, 5], dtype=dtype)
        return xp.intersect1d(a, b, assume_unique=True)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_with_random_val(self, xp, dtype):
        a = xp.array([3, 4, 9, 1, 5, 4], dtype=dtype)
        b = xp.array([8, 7, 3, 9, 0], dtype=dtype)
        return xp.intersect1d(a, b)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_more_dim(self, xp, dtype):
        a = testing.shaped_random((3, 4), xp, dtype=dtype)
        b = testing.shaped_random((5, 2), xp, dtype=dtype)
        return xp.intersect1d(a, b)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_return_indices(self, xp, dtype):
        a = xp.array([2, 3, 4, 1, 9, 4], dtype=dtype)
        b = xp.array([7, 5, 1, 2, 9, 3], dtype=dtype)
        return xp.intersect1d(a, b, return_indices=True)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_multiple_instances(self, xp, dtype):
        a = xp.array([2, 4, 5, 2, 1, 5], dtype=dtype)
        b = xp.array([4, 6, 2, 5, 7, 6], dtype=dtype)
        return xp.intersect1d(a, b, return_indices=True)


class TestUnion1d:

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_union1d(self, xp, dtype):
        x = xp.array([4, 1, 1, 1, 9, 9, 9], dtype=dtype)
        y = xp.array([4, 0, 5, 2, 0, 0, 5], dtype=dtype)
        return xp.union1d(x, y)

    @testing.for_all_dtypes()
    @testing.numpy_cupy_array_equal()
    def test_union1d_2(self, xp, dtype):
        x = testing.shaped_arange((5, 2), xp, dtype=dtype)
        y = testing.shaped_arange((2, 3, 4), xp, dtype=dtype)
        return xp.union1d(x, y)

    @testing.numpy_cupy_array_equal()
    def test_union1d_3(self, xp):
        x = xp.zeros((2, 2), dtype=xp.complex_)
        y = xp.array([[1+1j, 2+3j], [4+1j, 0+7j]])
        return xp.union1d(x, y)