forked from rapidsai/cugraph
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dataset.py
More file actions
349 lines (259 loc) · 10.1 KB
/
test_dataset.py
File metadata and controls
349 lines (259 loc) · 10.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
# Copyright (c) 2022-2023, NVIDIA 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 os
import gc
import sys
import warnings
from pathlib import Path
from tempfile import TemporaryDirectory
import pytest
from cugraph.structure import Graph
from cugraph.testing import (
RAPIDS_DATASET_ROOT_DIR_PATH,
ALL_DATASETS,
WEIGHTED_DATASETS,
SMALL_DATASETS,
)
from cugraph import datasets
# Add the sg marker to all tests in this module.
pytestmark = pytest.mark.sg
###############################################################################
# Fixtures
# module fixture - called once for this module
@pytest.fixture(scope="module")
def tmpdir():
"""
Create a tmp dir for downloads, etc., run a test, then cleanup when the
test is done.
"""
tmpd = TemporaryDirectory()
yield tmpd
# teardown
tmpd.cleanup()
# function fixture - called once for each function in this module
@pytest.fixture(scope="function", autouse=True)
def setup(tmpdir):
"""
Fixture used for individual test setup and teardown. This ensures each
Dataset object starts with the same state and cleans up when the test is
done.
"""
# FIXME: this relies on dataset features (unload) which themselves are
# being tested in this module.
for dataset in ALL_DATASETS:
dataset.unload()
gc.collect()
datasets.set_download_dir(tmpdir.name)
yield
# teardown
for dataset in ALL_DATASETS:
dataset.unload()
gc.collect()
@pytest.fixture()
def setup_deprecation_warning_tests():
"""
Fixture used to set warning filters to 'default' and reload
experimental.datasets module if it has been previously
imported. Tests that import this fixture are expected to
import cugraph.experimental.datasets
"""
warnings.filterwarnings("default")
if "cugraph.experimental.datasets" in sys.modules:
del sys.modules["cugraph.experimental.datasets"]
yield
###############################################################################
# Helper
# check if there is a row where src == dst
def has_loop(df):
df.rename(columns={df.columns[0]: "src", df.columns[1]: "dst"}, inplace=True)
res = df.where(df["src"] == df["dst"])
return res.notnull().values.any()
###############################################################################
# Tests
# setting download_dir to None effectively re-initialized the default
def test_env_var():
os.environ["RAPIDS_DATASET_ROOT_DIR"] = "custom_storage_location"
datasets.set_download_dir(None)
expected_path = Path("custom_storage_location").absolute()
assert datasets.get_download_dir() == expected_path
del os.environ["RAPIDS_DATASET_ROOT_DIR"]
def test_home_dir():
datasets.set_download_dir(None)
expected_path = Path.home() / ".cugraph/datasets"
assert datasets.get_download_dir() == expected_path
def test_set_download_dir():
tmpd = TemporaryDirectory()
datasets.set_download_dir(tmpd.name)
assert datasets.get_download_dir() == Path(tmpd.name).absolute()
tmpd.cleanup()
@pytest.mark.parametrize("dataset", ALL_DATASETS)
def test_download(dataset):
E = dataset.get_edgelist(download=True)
assert E is not None
assert dataset.get_path().is_file()
@pytest.mark.parametrize("dataset", ALL_DATASETS)
def test_get_edgelist(dataset):
E = dataset.get_edgelist(download=True)
assert E is not None
@pytest.mark.parametrize("dataset", ALL_DATASETS)
def test_get_graph(dataset):
G = dataset.get_graph(download=True)
assert G is not None
@pytest.mark.parametrize("dataset", ALL_DATASETS)
def test_metadata(dataset):
M = dataset.metadata
assert M is not None
@pytest.mark.parametrize("dataset", ALL_DATASETS)
def test_get_path(dataset):
tmpd = TemporaryDirectory()
datasets.set_download_dir(tmpd.name)
dataset.get_edgelist(download=True)
assert dataset.get_path().is_file()
tmpd.cleanup()
@pytest.mark.parametrize("dataset", WEIGHTED_DATASETS)
def test_weights(dataset):
G = dataset.get_graph(download=True)
assert G.is_weighted()
G = dataset.get_graph(download=True, ignore_weights=True)
assert not G.is_weighted()
@pytest.mark.parametrize("dataset", SMALL_DATASETS)
def test_create_using(dataset):
G = dataset.get_graph(download=True)
assert not G.is_directed()
G = dataset.get_graph(download=True, create_using=Graph)
assert not G.is_directed()
G = dataset.get_graph(download=True, create_using=Graph(directed=True))
assert G.is_directed()
def test_ctor_with_datafile():
from cugraph.datasets import karate
karate_csv = RAPIDS_DATASET_ROOT_DIR_PATH / "karate.csv"
# test that only a metadata file or csv can be specified, not both
with pytest.raises(ValueError):
datasets.Dataset(metadata_yaml_file="metadata_file", csv_file=karate_csv)
# ensure at least one arg is provided
with pytest.raises(ValueError):
datasets.Dataset()
# ensure csv file has all other required args (col names and col dtypes)
with pytest.raises(ValueError):
datasets.Dataset(csv_file=karate_csv)
with pytest.raises(ValueError):
datasets.Dataset(csv_file=karate_csv, csv_col_names=["src", "dst", "wgt"])
# test with file that DNE
with pytest.raises(FileNotFoundError):
datasets.Dataset(
csv_file="/some/file/that/does/not/exist",
csv_col_names=["src", "dst", "wgt"],
csv_col_dtypes=["int32", "int32", "float32"],
)
expected_karate_edgelist = karate.get_edgelist(download=True)
# test with file path as string, ensure download=True does not break
ds = datasets.Dataset(
csv_file=karate_csv.as_posix(),
csv_col_names=["src", "dst", "wgt"],
csv_col_dtypes=["int32", "int32", "float32"],
)
# cudf.testing.testing.assert_frame_equal() would be good to use to
# compare, but for some reason it seems to be holding a reference to a
# dataframe and gc.collect() does not free everything
el = ds.get_edgelist()
assert len(el) == len(expected_karate_edgelist)
assert str(ds) == "karate"
assert ds.get_path() == karate_csv
# test with file path as Path object
ds = datasets.Dataset(
csv_file=karate_csv,
csv_col_names=["src", "dst", "wgt"],
csv_col_dtypes=["int32", "int32", "float32"],
)
el = ds.get_edgelist()
assert len(el) == len(expected_karate_edgelist)
assert str(ds) == "karate"
assert ds.get_path() == karate_csv
def test_unload():
email_csv = RAPIDS_DATASET_ROOT_DIR_PATH / "email-Eu-core.csv"
ds = datasets.Dataset(
csv_file=email_csv.as_posix(),
csv_col_names=["src", "dst", "wgt"],
csv_col_dtypes=["int32", "int32", "float32"],
)
# FIXME: another (better?) test would be to check free memory and assert
# the memory use increases after get_*(), then returns to the pre-get_*()
# level after unload(). However, that type of test may fail for several
# reasons (the device being monitored is accidentally also being used by
# another process, and the use of memory pools to name two). Instead, just
# test that the internal members get cleared on unload().
assert ds._edgelist is None
ds.get_edgelist()
assert ds._edgelist is not None
ds.unload()
assert ds._edgelist is None
ds.get_graph()
assert ds._edgelist is not None
ds.unload()
assert ds._edgelist is None
@pytest.mark.parametrize("dataset", ALL_DATASETS)
def test_node_and_edge_count(dataset):
dataset_is_directed = dataset.metadata["is_directed"]
G = dataset.get_graph(
download=True, create_using=Graph(directed=dataset_is_directed)
)
# these are the values read directly from .yaml file
meta_node_count = dataset.metadata["number_of_nodes"]
meta_edge_count = dataset.metadata["number_of_edges"]
# value from the cugraph.Graph object
obj_node_count = G.number_of_nodes()
obj_edge_count = G.number_of_edges()
assert obj_node_count == meta_node_count
assert obj_edge_count == meta_edge_count
@pytest.mark.parametrize("dataset", ALL_DATASETS)
def test_is_directed(dataset):
dataset_is_directed = dataset.metadata["is_directed"]
G = dataset.get_graph(
download=True, create_using=Graph(directed=dataset_is_directed)
)
assert G.is_directed() == dataset.metadata["is_directed"]
@pytest.mark.parametrize("dataset", ALL_DATASETS)
def test_has_loop(dataset):
df = dataset.get_edgelist(download=True)
metadata_has_loop = dataset.metadata["has_loop"]
assert has_loop(df) == metadata_has_loop
# TODO: test is symmetric
# @pytest.mark.parametrize("dataset", ALL_DATASETS)
# def test_is_symmetric(dataset):
# G = dataset.get_graph(download=True)
@pytest.mark.parametrize("dataset", ALL_DATASETS)
def test_is_multigraph(dataset):
dataset_is_multigraph = dataset.metadata["is_multigraph"]
G = dataset.get_graph(download=True)
assert G.is_multigraph() == dataset_is_multigraph
#
# Test experimental for DeprecationWarnings
#
def test_experimental_dataset_import(setup_deprecation_warning_tests):
with pytest.deprecated_call():
from cugraph.experimental.datasets import karate
# unload() is called to pass flake8
karate.unload()
def test_experimental_method_warnings(setup_deprecation_warning_tests):
from cugraph.experimental.datasets import (
load_all,
set_download_dir,
get_download_dir,
)
warnings.filterwarnings("default")
tmpd = TemporaryDirectory()
with pytest.deprecated_call():
set_download_dir(tmpd.name)
get_download_dir()
load_all()
tmpd.cleanup()