forked from openml/openml-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_flow_functions.py
More file actions
539 lines (471 loc) 路 20.1 KB
/
test_flow_functions.py
File metadata and controls
539 lines (471 loc) 路 20.1 KB
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
# License: BSD 3-Clause
from __future__ import annotations
import copy
import functools
import unittest
from collections import OrderedDict
from multiprocessing.managers import Value
from openml_sklearn import SklearnExtension
from packaging.version import Version
from unittest import mock
from unittest.mock import patch
import pandas as pd
import pytest
import requests
import sklearn
from sklearn import ensemble
import openml
from openml.exceptions import OpenMLNotAuthorizedError, OpenMLServerException
from openml.testing import TestBase, create_request_response
@pytest.mark.usefixtures("long_version")
class TestFlowFunctions(TestBase):
_multiprocess_can_split_ = True
def setUp(self):
super().setUp()
def tearDown(self):
super().tearDown()
def _check_flow(self, flow):
assert type(flow) == dict
assert len(flow) == 6
assert isinstance(flow["id"], int)
assert isinstance(flow["name"], str)
assert isinstance(flow["full_name"], str)
assert isinstance(flow["version"], str)
# There are some runs on openml.org that can have an empty external version
ext_version_str_or_none = (
isinstance(flow["external_version"], str) or flow["external_version"] is None
)
assert ext_version_str_or_none
@pytest.mark.production()
def test_list_flows(self):
self.use_production_server()
# We can only perform a smoke test here because we test on dynamic
# data from the internet...
flows = openml.flows.list_flows()
# 3000 as the number of flows on openml.org
assert len(flows) >= 1500
for flow in flows.to_dict(orient="index").values():
self._check_flow(flow)
@pytest.mark.production()
def test_list_flows_output_format(self):
self.use_production_server()
# We can only perform a smoke test here because we test on dynamic
# data from the internet...
flows = openml.flows.list_flows()
assert isinstance(flows, pd.DataFrame)
assert len(flows) >= 1500
@pytest.mark.production()
def test_list_flows_empty(self):
self.use_production_server()
openml.config.server = self.production_server
flows = openml.flows.list_flows(tag="NoOneEverUsesThisTag123")
assert flows.empty
@pytest.mark.production()
def test_list_flows_by_tag(self):
self.use_production_server()
flows = openml.flows.list_flows(tag="weka")
assert len(flows) >= 5
for flow in flows.to_dict(orient="index").values():
self._check_flow(flow)
@pytest.mark.production()
def test_list_flows_paginate(self):
self.use_production_server()
size = 10
maximum = 100
for i in range(0, maximum, size):
flows = openml.flows.list_flows(offset=i, size=size)
assert size >= len(flows)
for flow in flows.to_dict(orient="index").values():
self._check_flow(flow)
def test_are_flows_equal(self):
flow = openml.flows.OpenMLFlow(
name="Test",
description="Test flow",
model=None,
components=OrderedDict(),
parameters=OrderedDict(),
parameters_meta_info=OrderedDict(),
external_version="1",
tags=["abc", "def"],
language="English",
dependencies="abc",
class_name="Test",
custom_name="Test",
)
# Test most important values that can be set by a user
openml.flows.functions.assert_flows_equal(flow, flow)
for attribute, new_value in [
("name", "Tes"),
("external_version", "2"),
("language", "english"),
("dependencies", "ab"),
("class_name", "Tes"),
("custom_name", "Tes"),
]:
new_flow = copy.deepcopy(flow)
setattr(new_flow, attribute, new_value)
assert getattr(flow, attribute) != getattr(new_flow, attribute)
self.assertRaises(
ValueError,
openml.flows.functions.assert_flows_equal,
flow,
new_flow,
)
# Test that the API ignores several keys when comparing flows
openml.flows.functions.assert_flows_equal(flow, flow)
for attribute, new_value in [
("flow_id", 1),
("uploader", 1),
("version", 1),
("upload_date", "18.12.1988"),
("binary_url", "openml.org"),
("binary_format", "gzip"),
("binary_md5", "12345"),
("model", []),
("tags", ["abc", "de"]),
]:
new_flow = copy.deepcopy(flow)
setattr(new_flow, attribute, new_value)
assert getattr(flow, attribute) != getattr(new_flow, attribute)
openml.flows.functions.assert_flows_equal(flow, new_flow)
# Now test for parameters
flow.parameters["abc"] = 1.0
flow.parameters["def"] = 2.0
openml.flows.functions.assert_flows_equal(flow, flow)
new_flow = copy.deepcopy(flow)
new_flow.parameters["abc"] = 3.0
self.assertRaises(ValueError, openml.flows.functions.assert_flows_equal, flow, new_flow)
# Now test for components (subflows)
parent_flow = copy.deepcopy(flow)
subflow = copy.deepcopy(flow)
parent_flow.components["subflow"] = subflow
openml.flows.functions.assert_flows_equal(parent_flow, parent_flow)
self.assertRaises(
ValueError,
openml.flows.functions.assert_flows_equal,
parent_flow,
subflow,
)
new_flow = copy.deepcopy(parent_flow)
new_flow.components["subflow"].name = "Subflow name"
self.assertRaises(
ValueError,
openml.flows.functions.assert_flows_equal,
parent_flow,
new_flow,
)
def test_are_flows_equal_ignore_parameter_values(self):
paramaters = OrderedDict((("a", 5), ("b", 6)))
parameters_meta_info = OrderedDict((("a", None), ("b", None)))
flow = openml.flows.OpenMLFlow(
name="Test",
description="Test flow",
model=None,
components=OrderedDict(),
parameters=paramaters,
parameters_meta_info=parameters_meta_info,
external_version="1",
tags=["abc", "def"],
language="English",
dependencies="abc",
class_name="Test",
custom_name="Test",
)
openml.flows.functions.assert_flows_equal(flow, flow)
openml.flows.functions.assert_flows_equal(flow, flow, ignore_parameter_values=True)
new_flow = copy.deepcopy(flow)
new_flow.parameters["a"] = 7
with pytest.raises(ValueError) as excinfo:
openml.flows.functions.assert_flows_equal(flow, new_flow)
assert str(paramaters) in str(excinfo.value) and str(new_flow.parameters) in str(
excinfo.value
)
openml.flows.functions.assert_flows_equal(flow, new_flow, ignore_parameter_values=True)
del new_flow.parameters["a"]
with pytest.raises(ValueError) as excinfo:
openml.flows.functions.assert_flows_equal(flow, new_flow)
assert str(paramaters) in str(excinfo.value) and str(new_flow.parameters) in str(
excinfo.value
)
self.assertRaisesRegex(
ValueError,
r"Flow Test: parameter set of flow differs from the parameters "
r"stored on the server.",
openml.flows.functions.assert_flows_equal,
flow,
new_flow,
ignore_parameter_values=True,
)
def test_are_flows_equal_ignore_if_older(self):
paramaters = OrderedDict((("a", 5), ("b", 6)))
parameters_meta_info = OrderedDict((("a", None), ("b", None)))
flow_upload_date = "2017-01-31T12-01-01"
assert_flows_equal = openml.flows.functions.assert_flows_equal
flow = openml.flows.OpenMLFlow(
name="Test",
description="Test flow",
model=None,
components=OrderedDict(),
parameters=paramaters,
parameters_meta_info=parameters_meta_info,
external_version="1",
tags=["abc", "def"],
language="English",
dependencies="abc",
class_name="Test",
custom_name="Test",
upload_date=flow_upload_date,
)
assert_flows_equal(flow, flow, ignore_parameter_values_on_older_children=flow_upload_date)
assert_flows_equal(flow, flow, ignore_parameter_values_on_older_children=None)
new_flow = copy.deepcopy(flow)
new_flow.parameters["a"] = 7
self.assertRaises(
ValueError,
assert_flows_equal,
flow,
new_flow,
ignore_parameter_values_on_older_children=flow_upload_date,
)
self.assertRaises(
ValueError,
assert_flows_equal,
flow,
new_flow,
ignore_parameter_values_on_older_children=None,
)
new_flow.upload_date = "2016-01-31T12-01-01"
self.assertRaises(
ValueError,
assert_flows_equal,
flow,
new_flow,
ignore_parameter_values_on_older_children=flow_upload_date,
)
assert_flows_equal(flow, flow, ignore_parameter_values_on_older_children=None)
@pytest.mark.sklearn()
@unittest.skipIf(
Version(sklearn.__version__) < Version("0.20"),
reason="OrdinalEncoder introduced in 0.20. "
"No known models with list of lists parameters in older versions.",
)
def test_sklearn_to_flow_list_of_lists(self):
from sklearn.preprocessing import OrdinalEncoder
ordinal_encoder = OrdinalEncoder(categories=[[0, 1], [0, 1]])
extension = SklearnExtension()
# Test serialization works
flow = extension.model_to_flow(ordinal_encoder)
# Test flow is accepted by server
self._add_sentinel_to_flow_name(flow)
flow.publish()
TestBase._mark_entity_for_removal("flow", flow.flow_id, flow.name)
TestBase.logger.info(f"collected from {__file__.split('/')[-1]}: {flow.flow_id}")
# Test deserialization works
server_flow = openml.flows.get_flow(flow.flow_id, reinstantiate=True)
assert server_flow.parameters["categories"] == "[[0, 1], [0, 1]]"
assert server_flow.model.categories == flow.model.categories
@pytest.mark.production()
def test_get_flow1(self):
# Regression test for issue #305
# Basically, this checks that a flow without an external version can be loaded
self.use_production_server()
flow = openml.flows.get_flow(1)
assert flow.external_version is None
@pytest.mark.sklearn()
def test_get_flow_reinstantiate_model(self):
model = ensemble.RandomForestClassifier(n_estimators=33)
extension = openml.extensions.get_extension_by_model(model)
flow = extension.model_to_flow(model)
flow.publish(raise_error_if_exists=False)
TestBase._mark_entity_for_removal("flow", flow.flow_id, flow.name)
TestBase.logger.info(f"collected from {__file__.split('/')[-1]}: {flow.flow_id}")
downloaded_flow = openml.flows.get_flow(flow.flow_id, reinstantiate=True)
assert isinstance(downloaded_flow.model, sklearn.ensemble.RandomForestClassifier)
def test_get_flow_reinstantiate_model_no_extension(self):
# Flow 10 is a WEKA flow
self.assertRaisesRegex(
ValueError,
".* flow: 10 \(weka.SMO\). ",
openml.flows.get_flow,
flow_id=10,
reinstantiate=True,
)
@pytest.mark.sklearn()
@unittest.skipIf(
Version(sklearn.__version__) == Version("0.19.1"),
reason="Requires scikit-learn!=0.19.1, because target flow is from that version.",
)
@pytest.mark.production()
def test_get_flow_with_reinstantiate_strict_with_wrong_version_raises_exception(self):
self.use_production_server()
flow = 8175
expected = "Trying to deserialize a model with dependency sklearn==0.19.1 not satisfied."
self.assertRaisesRegex(
ValueError,
expected,
openml.flows.get_flow,
flow_id=flow,
reinstantiate=True,
strict_version=True,
)
@pytest.mark.sklearn()
@unittest.skipIf(
Version(sklearn.__version__) >= Version("1.0.0"),
reason="Requires scikit-learn < 1.0.0.",
# Because scikit-learn dropped min_impurity_split hyperparameter in 1.0,
# and the requested flow is from 1.0.0 exactly.
)
@pytest.mark.production()
def test_get_flow_reinstantiate_flow_not_strict_post_1(self):
self.use_production_server()
flow = openml.flows.get_flow(flow_id=19190, reinstantiate=True, strict_version=False)
assert flow.flow_id is None
assert "sklearn==1.0.0" not in flow.dependencies
@pytest.mark.sklearn()
@unittest.skipIf(
(Version(sklearn.__version__) < Version("0.23.2"))
or (Version(sklearn.__version__) >= Version("1.0")),
reason="Requires scikit-learn 0.23.2 or ~0.24.",
# Because these still have min_impurity_split, but with new scikit-learn module structure."
)
@pytest.mark.production()
def test_get_flow_reinstantiate_flow_not_strict_023_and_024(self):
self.use_production_server()
flow = openml.flows.get_flow(flow_id=18587, reinstantiate=True, strict_version=False)
assert flow.flow_id is None
assert "sklearn==0.23.1" not in flow.dependencies
@pytest.mark.sklearn()
@unittest.skipIf(
Version(sklearn.__version__) > Version("0.23"),
reason="Requires scikit-learn<=0.23, because the scikit-learn module structure changed.",
)
@pytest.mark.production()
def test_get_flow_reinstantiate_flow_not_strict_pre_023(self):
self.use_production_server()
flow = openml.flows.get_flow(flow_id=8175, reinstantiate=True, strict_version=False)
assert flow.flow_id is None
assert "sklearn==0.19.1" not in flow.dependencies
@pytest.mark.sklearn()
def test_get_flow_id(self):
if self.long_version:
list_all = openml.utils._list_all
else:
list_all = functools.lru_cache()(openml.utils._list_all)
with patch("openml.utils._list_all", list_all):
clf = sklearn.tree.DecisionTreeClassifier()
flow = openml.extensions.get_extension_by_model(clf).model_to_flow(clf).publish()
TestBase._mark_entity_for_removal("flow", flow.flow_id, flow.name)
TestBase.logger.info(
f"collected from {__file__.split('/')[-1]}: {flow.flow_id}",
)
assert openml.flows.get_flow_id(model=clf, exact_version=True) == flow.flow_id
flow_ids = openml.flows.get_flow_id(model=clf, exact_version=False)
assert flow.flow_id in flow_ids
assert len(flow_ids) > 0
# Check that the output of get_flow_id is identical if only the name is given, no matter
# whether exact_version is set to True or False.
flow_ids_exact_version_True = openml.flows.get_flow_id(
name=flow.name,
exact_version=True,
)
flow_ids_exact_version_False = openml.flows.get_flow_id(
name=flow.name,
exact_version=False,
)
assert flow_ids_exact_version_True == flow_ids_exact_version_False
assert flow.flow_id in flow_ids_exact_version_True
def test_delete_flow(self):
flow = openml.OpenMLFlow(
name="sklearn.dummy.DummyClassifier",
class_name="sklearn.dummy.DummyClassifier",
description="test description",
model=sklearn.dummy.DummyClassifier(),
components=OrderedDict(),
parameters=OrderedDict(),
parameters_meta_info=OrderedDict(),
external_version="1",
tags=[],
language="English",
dependencies=None,
)
flow, _ = self._add_sentinel_to_flow_name(flow, None)
flow.publish()
_flow_id = flow.flow_id
assert openml.flows.delete_flow(_flow_id)
@mock.patch.object(requests.Session, "delete")
def test_delete_flow_not_owned(mock_delete, test_files_directory, test_api_key):
openml.config.start_using_configuration_for_example()
content_file = test_files_directory / "mock_responses" / "flows" / "flow_delete_not_owned.xml"
mock_delete.return_value = create_request_response(
status_code=412,
content_filepath=content_file,
)
with pytest.raises(
OpenMLNotAuthorizedError,
match="The flow can not be deleted because it was not uploaded by you.",
):
openml.flows.delete_flow(40_000)
flow_url = "https://test.openml.org/api/v1/xml/flow/40000"
assert flow_url == mock_delete.call_args.args[0]
assert test_api_key == mock_delete.call_args.kwargs.get("params", {}).get("api_key")
@mock.patch.object(requests.Session, "delete")
def test_delete_flow_with_run(mock_delete, test_files_directory, test_api_key):
openml.config.start_using_configuration_for_example()
content_file = test_files_directory / "mock_responses" / "flows" / "flow_delete_has_runs.xml"
mock_delete.return_value = create_request_response(
status_code=412,
content_filepath=content_file,
)
with pytest.raises(
OpenMLNotAuthorizedError,
match="The flow can not be deleted because it still has associated entities:",
):
openml.flows.delete_flow(40_000)
flow_url = "https://test.openml.org/api/v1/xml/flow/40000"
assert flow_url == mock_delete.call_args.args[0]
assert test_api_key == mock_delete.call_args.kwargs.get("params", {}).get("api_key")
@mock.patch.object(requests.Session, "delete")
def test_delete_subflow(mock_delete, test_files_directory, test_api_key):
openml.config.start_using_configuration_for_example()
content_file = test_files_directory / "mock_responses" / "flows" / "flow_delete_is_subflow.xml"
mock_delete.return_value = create_request_response(
status_code=412,
content_filepath=content_file,
)
with pytest.raises(
OpenMLNotAuthorizedError,
match="The flow can not be deleted because it still has associated entities:",
):
openml.flows.delete_flow(40_000)
flow_url = "https://test.openml.org/api/v1/xml/flow/40000"
assert flow_url == mock_delete.call_args.args[0]
assert test_api_key == mock_delete.call_args.kwargs.get("params", {}).get("api_key")
@mock.patch.object(requests.Session, "delete")
def test_delete_flow_success(mock_delete, test_files_directory, test_api_key):
openml.config.start_using_configuration_for_example()
content_file = test_files_directory / "mock_responses" / "flows" / "flow_delete_successful.xml"
mock_delete.return_value = create_request_response(
status_code=200,
content_filepath=content_file,
)
success = openml.flows.delete_flow(33364)
assert success
flow_url = "https://test.openml.org/api/v1/xml/flow/33364"
assert flow_url == mock_delete.call_args.args[0]
assert test_api_key == mock_delete.call_args.kwargs.get("params", {}).get("api_key")
@mock.patch.object(requests.Session, "delete")
def test_delete_unknown_flow(mock_delete, test_files_directory, test_api_key):
openml.config.start_using_configuration_for_example()
content_file = test_files_directory / "mock_responses" / "flows" / "flow_delete_not_exist.xml"
mock_delete.return_value = create_request_response(
status_code=412,
content_filepath=content_file,
)
with pytest.raises(
OpenMLServerException,
match="flow does not exist",
):
openml.flows.delete_flow(9_999_999)
flow_url = "https://test.openml.org/api/v1/xml/flow/9999999"
assert flow_url == mock_delete.call_args.args[0]
assert test_api_key == mock_delete.call_args.kwargs.get("params", {}).get("api_key")