-
-
Notifications
You must be signed in to change notification settings - Fork 278
Expand file tree
/
Copy pathtest_task_functions.py
More file actions
317 lines (268 loc) 路 11.7 KB
/
Copy pathtest_task_functions.py
File metadata and controls
317 lines (268 loc) 路 11.7 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
# License: BSD 3-Clause
from __future__ import annotations
import os
import unittest
from typing import cast
from unittest import mock
import pandas as pd
import pytest
import requests
import openml
from openml import OpenMLSplit, OpenMLTask
from openml.exceptions import OpenMLCacheException, OpenMLNotAuthorizedError, OpenMLServerException
from openml.tasks import TaskType
from openml.testing import TestBase, create_request_response
class TestTask(TestBase):
_multiprocess_can_split_ = True
def setUp(self):
super().setUp()
def tearDown(self):
super().tearDown()
@pytest.mark.test_server()
def test__get_cached_tasks(self):
openml.config.set_root_cache_directory(self.static_cache_dir)
tasks = openml.tasks.functions._get_cached_tasks()
assert isinstance(tasks, dict)
assert len(tasks) == 3
assert isinstance(next(iter(tasks.values())), OpenMLTask)
@pytest.mark.test_server()
def test__get_cached_task(self):
openml.config.set_root_cache_directory(self.static_cache_dir)
task = openml.tasks.functions._get_cached_task(1)
assert isinstance(task, OpenMLTask)
def test__get_cached_task_not_cached(self):
openml.config.set_root_cache_directory(self.static_cache_dir)
self.assertRaisesRegex(
OpenMLCacheException,
"Task file for tid 2 not cached",
openml.tasks.functions._get_cached_task,
2,
)
@pytest.mark.test_server()
def test__get_estimation_procedure_list(self):
estimation_procedures = openml.tasks.functions._get_estimation_procedure_list()
assert isinstance(estimation_procedures, list)
assert isinstance(estimation_procedures[0], dict)
assert estimation_procedures[0]["task_type_id"] == TaskType.SUPERVISED_CLASSIFICATION
@pytest.mark.production_server()
@pytest.mark.xfail(reason="failures_issue_1544", strict=False)
def test_list_clustering_task(self):
self.use_production_server()
# as shown by #383, clustering tasks can give list/dict casting problems
openml.tasks.list_tasks(task_type=TaskType.CLUSTERING, size=10)
# the expected outcome is that it doesn't crash. No assertions.
def _check_task(self, task):
assert type(task) == dict
assert len(task) >= 2
assert "did" in task
assert isinstance(task["did"], int)
assert "status" in task
assert isinstance(task["status"], str)
assert task["status"] in ["in_preparation", "active", "deactivated"]
@pytest.mark.test_server()
def test_list_tasks_by_type(self):
num_curves_tasks = 198 # number is flexible, check server if fails
ttid = TaskType.LEARNING_CURVE
tasks = openml.tasks.list_tasks(task_type=ttid)
assert len(tasks) >= num_curves_tasks
for task in tasks.to_dict(orient="index").values():
assert ttid == task["ttid"]
self._check_task(task)
@pytest.mark.test_server()
def test_list_tasks_length(self):
ttid = TaskType.LEARNING_CURVE
tasks = openml.tasks.list_tasks(task_type=ttid)
assert len(tasks) > 100
@pytest.mark.test_server()
def test_list_tasks_empty(self):
tasks = openml.tasks.list_tasks(tag="NoOneWillEverUseThisTag")
assert tasks.empty
@pytest.mark.test_server()
def test_list_tasks_by_tag(self):
# Server starts with 99 active tasks with the tag, and one 'in_preparation',
# so depending on the processing of the last dataset, there may be 99 or 100 matches.
num_basic_tasks = 99
tasks = openml.tasks.list_tasks(tag="OpenML100")
assert len(tasks) >= num_basic_tasks
for task in tasks.to_dict(orient="index").values():
self._check_task(task)
@pytest.mark.test_server()
def test_list_tasks(self):
tasks = openml.tasks.list_tasks()
assert len(tasks) >= 900
for task in tasks.to_dict(orient="index").values():
self._check_task(task)
@pytest.mark.test_server()
def test_list_tasks_paginate(self):
size = 10
max = 100
for i in range(0, max, size):
tasks = openml.tasks.list_tasks(offset=i, size=size)
assert size >= len(tasks)
for task in tasks.to_dict(orient="index").values():
self._check_task(task)
@pytest.mark.test_server()
def test_list_tasks_per_type_paginate(self):
size = 40
max = 100
task_types = [
TaskType.SUPERVISED_CLASSIFICATION,
TaskType.SUPERVISED_REGRESSION,
TaskType.LEARNING_CURVE,
]
for j in task_types:
for i in range(0, max, size):
tasks = openml.tasks.list_tasks(task_type=j, offset=i, size=size)
assert size >= len(tasks)
for task in tasks.to_dict(orient="index").values():
assert j == task["ttid"]
self._check_task(task)
@pytest.mark.test_server()
def test__get_task(self):
openml.config.set_root_cache_directory(self.static_cache_dir)
openml.tasks.get_task(1882)
@unittest.skip(
"Please await outcome of discussion: https://github.com/openml/OpenML/issues/776",
)
@pytest.mark.production_server()
def test__get_task_live(self):
self.use_production_server()
# Test the following task as it used to throw an Unicode Error.
# https://github.com/openml/openml-python/issues/378
openml.tasks.get_task(34536)
@pytest.mark.test_server()
def test_get_task(self):
task = openml.tasks.get_task(1, download_data=True) # anneal; crossvalidation
assert isinstance(task, OpenMLTask)
assert os.path.exists(
os.path.join(openml.config.get_cache_directory(), "tasks", "1", "task.xml")
)
assert not os.path.exists(
os.path.join(openml.config.get_cache_directory(), "tasks", "1", "datasplits.arff")
)
assert os.path.exists(
os.path.join(openml.config.get_cache_directory(), "datasets", "1", "dataset_1.pq")
)
@pytest.mark.test_server()
def test_get_task_lazy(self):
task = openml.tasks.get_task(2, download_data=False) # anneal; crossvalidation
assert isinstance(task, OpenMLTask)
assert os.path.exists(
os.path.join(openml.config.get_cache_directory(), "tasks", "2", "task.xml")
)
assert task.class_labels == ["1", "2", "3", "4", "5", "U"]
assert not os.path.exists(
os.path.join(openml.config.get_cache_directory(), "tasks", "2", "datasplits.arff")
)
# Since the download_data=False is propagated to get_dataset
assert not os.path.exists(
os.path.join(openml.config.get_cache_directory(), "datasets", "2", "dataset.arff")
)
task.download_split()
assert os.path.exists(
os.path.join(openml.config.get_cache_directory(), "tasks", "2", "datasplits.arff")
)
@mock.patch("openml.tasks.functions.get_dataset")
@pytest.mark.test_server()
def test_removal_upon_download_failure(self, get_dataset):
class WeirdException(Exception):
pass
def assert_and_raise(*args, **kwargs):
# Make sure that the file was created!
assert os.path.join(os.getcwd(), "tasks", "1", "tasks.xml")
raise WeirdException()
get_dataset.side_effect = assert_and_raise
try:
openml.tasks.get_task(1) # anneal; crossvalidation
except WeirdException:
pass
# Now the file should no longer exist
assert not os.path.exists(os.path.join(os.getcwd(), "tasks", "1", "tasks.xml"))
@pytest.mark.test_server()
def test_get_task_with_cache(self):
openml.config.set_root_cache_directory(self.static_cache_dir)
task = openml.tasks.get_task(1)
assert isinstance(task, OpenMLTask)
@pytest.mark.production_server()
def test_get_task_different_types(self):
self.use_production_server()
# Regression task
openml.tasks.functions.get_task(5001)
# Learning curve
openml.tasks.functions.get_task(64)
# Issue 538, get_task failing with clustering task.
openml.tasks.functions.get_task(126033)
@pytest.mark.test_server()
def test_download_split(self):
task = openml.tasks.get_task(1) # anneal; crossvalidation
split = task.download_split()
assert type(split) == OpenMLSplit
assert os.path.exists(
os.path.join(openml.config.get_cache_directory(), "tasks", "1", "datasplits.arff")
)
def test_deletion_of_cache_dir(self):
# Simple removal
tid_cache_dir = openml.utils._create_cache_directory_for_id(
"tasks",
1,
)
assert os.path.exists(tid_cache_dir)
openml.utils._remove_cache_dir_for_id("tasks", tid_cache_dir)
assert not os.path.exists(tid_cache_dir)
@mock.patch.object(requests.Session, "delete")
def test_delete_task_not_owned(mock_delete, test_files_directory, test_server_v1, test_apikey_v1):
content_file = test_files_directory / "mock_responses" / "tasks" / "task_delete_not_owned.xml"
mock_delete.return_value = create_request_response(
status_code=412,
content_filepath=content_file,
)
with pytest.raises(
OpenMLNotAuthorizedError,
match="The task can not be deleted because it was not uploaded by you.",
):
openml.tasks.delete_task(1)
task_url = test_server_v1 + "task/1"
assert task_url == mock_delete.call_args.args[0]
assert test_apikey_v1 == mock_delete.call_args.kwargs.get("params", {}).get("api_key")
@mock.patch.object(requests.Session, "delete")
def test_delete_task_with_run(mock_delete, test_files_directory, test_server_v1, test_apikey_v1):
content_file = test_files_directory / "mock_responses" / "tasks" / "task_delete_has_runs.xml"
mock_delete.return_value = create_request_response(
status_code=412,
content_filepath=content_file,
)
with pytest.raises(
OpenMLNotAuthorizedError,
match="The task can not be deleted because it still has associated entities:",
):
openml.tasks.delete_task(3496)
task_url = test_server_v1 + "task/3496"
assert task_url == mock_delete.call_args.args[0]
assert test_apikey_v1 == mock_delete.call_args.kwargs.get("params", {}).get("api_key")
@mock.patch.object(requests.Session, "delete")
def test_delete_success(mock_delete, test_files_directory, test_server_v1, test_apikey_v1):
content_file = test_files_directory / "mock_responses" / "tasks" / "task_delete_successful.xml"
mock_delete.return_value = create_request_response(
status_code=200,
content_filepath=content_file,
)
success = openml.tasks.delete_task(361323)
assert success
task_url = test_server_v1 + "task/361323"
assert task_url == mock_delete.call_args.args[0]
assert test_apikey_v1 == mock_delete.call_args.kwargs.get("params", {}).get("api_key")
@mock.patch.object(requests.Session, "delete")
def test_delete_unknown_task(mock_delete, test_files_directory, test_server_v1, test_apikey_v1):
content_file = test_files_directory / "mock_responses" / "tasks" / "task_delete_not_exist.xml"
mock_delete.return_value = create_request_response(
status_code=412,
content_filepath=content_file,
)
with pytest.raises(
OpenMLServerException,
match="Task does not exist",
):
openml.tasks.delete_task(9_999_999)
task_url = test_server_v1 + "task/9999999"
assert task_url == mock_delete.call_args.args[0]
assert test_apikey_v1 == mock_delete.call_args.kwargs.get("params", {}).get("api_key")