-
Notifications
You must be signed in to change notification settings - Fork 179
/
test_basic_statistics.py
242 lines (198 loc) · 8.21 KB
/
test_basic_statistics.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
# ==============================================================================
# Copyright 2023 Intel Corporation
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
import numpy as np
import pytest
from numpy.testing import assert_allclose
from scipy import sparse as sp
from daal4py.sklearn._utils import daal_check_version
from onedal.basic_statistics import BasicStatistics
from onedal.basic_statistics.tests.utils import options_and_tests
from onedal.tests.utils._device_selection import get_queues
options_and_tests_csr = [
("sum", "sum", (5e-6, 1e-9)),
("min", "min", (0, 0)),
("max", "max", (0, 0)),
("mean", "mean", (5e-6, 1e-9)),
]
@pytest.mark.parametrize("queue", get_queues())
@pytest.mark.parametrize("result_option", options_and_tests.keys())
@pytest.mark.parametrize("row_count", [100, 1000])
@pytest.mark.parametrize("column_count", [10, 100])
@pytest.mark.parametrize("weighted", [True, False])
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_single_option_on_random_data(
queue, result_option, row_count, column_count, weighted, dtype
):
function, tols = options_and_tests[result_option]
fp32tol, fp64tol = tols
seed = 77
gen = np.random.default_rng(seed)
data = gen.uniform(low=-0.3, high=+0.7, size=(row_count, column_count))
data = data.astype(dtype=dtype)
if weighted:
weights = gen.uniform(low=-0.5, high=+1.0, size=row_count)
weights = weights.astype(dtype=dtype)
else:
weights = None
basicstat = BasicStatistics(result_options=result_option)
result = basicstat.fit(data, sample_weight=weights, queue=queue)
res = getattr(result, result_option)
if weighted:
weighted_data = np.diag(weights) @ data
gtr = function(weighted_data)
else:
gtr = function(data)
tol = fp32tol if res.dtype == np.float32 else fp64tol
assert_allclose(gtr, res, atol=tol)
@pytest.mark.parametrize("queue", get_queues())
@pytest.mark.parametrize("row_count", [100, 1000])
@pytest.mark.parametrize("column_count", [10, 100])
@pytest.mark.parametrize("weighted", [True, False])
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_multiple_options_on_random_data(queue, row_count, column_count, weighted, dtype):
seed = 42
gen = np.random.default_rng(seed)
data = gen.uniform(low=-0.3, high=+0.7, size=(row_count, column_count))
data = data.astype(dtype=dtype)
if weighted:
weights = gen.uniform(low=-0.5, high=+1.0, size=row_count)
weights = weights.astype(dtype=dtype)
else:
weights = None
basicstat = BasicStatistics(result_options=["mean", "max", "sum"])
result = basicstat.fit(data, sample_weight=weights, queue=queue)
res_mean, res_max, res_sum = result.mean, result.max, result.sum
if weighted:
weighted_data = np.diag(weights) @ data
gtr_mean, gtr_max, gtr_sum = (
options_and_tests["mean"][0](weighted_data),
options_and_tests["max"][0](weighted_data),
options_and_tests["sum"][0](weighted_data),
)
else:
gtr_mean, gtr_max, gtr_sum = (
options_and_tests["mean"][0](data),
options_and_tests["max"][0](data),
options_and_tests["sum"][0](data),
)
tol = 5e-4 if res_mean.dtype == np.float32 else 1e-7
assert_allclose(gtr_mean, res_mean, atol=tol)
assert_allclose(gtr_max, res_max, atol=tol)
assert_allclose(gtr_sum, res_sum, atol=tol)
@pytest.mark.parametrize("queue", get_queues())
@pytest.mark.parametrize("row_count", [100, 1000])
@pytest.mark.parametrize("column_count", [10, 100])
@pytest.mark.parametrize("weighted", [True, False])
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_all_option_on_random_data(queue, row_count, column_count, weighted, dtype):
seed = 77
gen = np.random.default_rng(seed)
data = gen.uniform(low=-0.3, high=+0.7, size=(row_count, column_count))
data = data.astype(dtype=dtype)
if weighted:
weights = gen.uniform(low=-0.5, high=+1.0, size=row_count)
weights = weights.astype(dtype=dtype)
else:
weights = None
basicstat = BasicStatistics(result_options="all")
result = basicstat.fit(data, sample_weight=weights, queue=queue)
if weighted:
weighted_data = np.diag(weights) @ data
for result_option in options_and_tests:
function, tols = options_and_tests[result_option]
fp32tol, fp64tol = tols
res = getattr(result, result_option)
if weighted:
gtr = function(weighted_data)
else:
gtr = function(data)
tol = fp32tol if res.dtype == np.float32 else fp64tol
assert_allclose(gtr, res, atol=tol)
@pytest.mark.parametrize("queue", get_queues())
@pytest.mark.parametrize("result_option", options_and_tests.keys())
@pytest.mark.parametrize("data_size", [100, 1000])
@pytest.mark.parametrize("weighted", [True, False])
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_1d_input_on_random_data(queue, result_option, data_size, weighted, dtype):
function, tols = options_and_tests[result_option]
fp32tol, fp64tol = tols
seed = 77
gen = np.random.default_rng(seed)
data = gen.uniform(low=-0.3, high=+0.7, size=data_size)
data = data.astype(dtype=dtype)
if weighted:
weights = gen.uniform(low=-0.5, high=+1.0, size=data_size)
weights = weights.astype(dtype=dtype)
else:
weights = None
basicstat = BasicStatistics(result_options=result_option)
result = basicstat.fit(data, sample_weight=weights, queue=queue)
res = getattr(result, result_option)
if weighted:
weighted_data = weights * data
gtr = function(weighted_data)
else:
gtr = function(data)
tol = fp32tol if res.dtype == np.float32 else fp64tol
assert_allclose(gtr, res, atol=tol)
@pytest.mark.skipif(not hasattr(sp, "random_array"), reason="requires scipy>=1.12.0")
@pytest.mark.parametrize("queue", get_queues())
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_basic_csr(queue, dtype):
seed = 42
row_count, column_count = 5000, 3008
gen = np.random.default_rng(seed)
data = sp.random_array(
shape=(row_count, column_count),
density=0.01,
format="csr",
dtype=dtype,
random_state=gen,
)
basicstat = BasicStatistics(result_options="mean")
result = basicstat.fit(data, queue=queue)
res_mean = result.mean
gtr_mean = data.mean(axis=0)
tol = 5e-6 if res_mean.dtype == np.float32 else 1e-9
assert_allclose(gtr_mean, res_mean, rtol=tol)
@pytest.mark.skipif(not hasattr(sp, "random_array"), reason="requires scipy>=1.12.0")
@pytest.mark.parametrize("queue", get_queues())
@pytest.mark.parametrize("option", options_and_tests_csr)
@pytest.mark.parametrize("dtype", [np.float32, np.float64])
def test_options_csr(queue, option, dtype):
result_option, function, tols = option
fp32tol, fp64tol = tols
if result_option == "max":
pytest.skip("There is a bug in oneDAL's max computations on GPU")
seed = 42
row_count, column_count = 20046, 4007
gen = np.random.default_rng(seed)
data = sp.random_array(
shape=(row_count, column_count),
density=0.002,
format="csr",
dtype=dtype,
random_state=gen,
)
basicstat = BasicStatistics(result_options=result_option)
result = basicstat.fit(data, queue=queue)
res = getattr(result, result_option)
func = getattr(data, function)
gtr = func(axis=0)
if type(gtr).__name__ != "ndarray":
gtr = gtr.toarray().flatten()
tol = fp32tol if res.dtype == np.float32 else fp64tol
assert_allclose(gtr, res, rtol=tol)