forked from stellargraph/stellargraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_edge_splitter.py
522 lines (441 loc) · 19.3 KB
/
test_edge_splitter.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
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
# -*- coding: utf-8 -*-
#
# Copyright 2017-2018 Data61, CSIRO
#
# 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 pytest
import os
import numpy as np
import networkx as nx
from stellargraph.data.edge_splitter import EdgeSplitter
from stellargraph.data.epgm import EPGM
import random
import datetime
from datetime import datetime, timedelta
def create_heterogeneous_graph():
# TODO: We test if this graph is connected but there is no guarantee of connectivity in this code
g = nx.Graph()
random.seed(152) # produces the same graph every time
start_date_dt = datetime.strptime("01/01/2015", "%d/%m/%Y")
end_date_dt = datetime.strptime("01/01/2017", "%d/%m/%Y")
start_end_days = (
end_date_dt - start_date_dt
).days # the number of days between start and end dates
# 50 nodes of type person
person_node_ids = list(range(0, 50))
for person in person_node_ids:
g.add_node(person, label="person", elite=random.choice([0, 1]))
# 200 nodes of type paper
paper_node_ids = list(range(50, 250))
g.add_nodes_from(paper_node_ids, label="paper")
# 10 nodes of type venue
venue_node_ids = list(range(250, 260))
g.add_nodes_from(venue_node_ids, label="venue")
# add the person - friend -> person edges
# each person can be friends with 0 to 5 others; edges include a date
for person_id in person_node_ids:
k = random.randrange(5)
friend_ids = set(random.sample(person_node_ids, k=k)) - {
person_id
} # no self loops
for friend in friend_ids:
g.add_edge(
person_id,
friend,
label="friend",
date=(
start_date_dt + timedelta(days=random.randrange(start_end_days))
).strftime("%d/%m/%Y"),
)
# add the person - writes -> paper edges
for person_id in person_node_ids:
k = random.randrange(5)
paper_ids = random.sample(paper_node_ids, k=k)
for paper in paper_ids:
g.add_edge(person_id, paper, label="writes")
# add the paper - published-at -> venue edges
for paper_id in paper_node_ids:
venue_id = random.sample(venue_node_ids, k=1)[
0
] # paper is published at 1 venue only
g.add_edge(paper_id, venue_id, label="published-at")
return g
def read_graph(graph_file, dataset_name, directed=False, weighted=False):
"""
Reads the input network in networkx.
:param graph_file: The directory where graph in EPGM format is stored
:param dataset_name: The name of the graph selected out of all the graph heads in EPGM file
:return: The graph in networkx format
"""
try: # assume args.input points to an EPGM graph
G_epgm = EPGM(graph_file)
graphs = G_epgm.G["graphs"]
if (
dataset_name is None
): # if dataset_name is not given, use the name of the 1st graph head
dataset_name = graphs[0]["meta"]["label"]
print(
"WARNING: dataset name not specified, using dataset '{}' in the 1st graph head".format(
dataset_name
)
)
graph_id = None
for g in graphs:
if g["meta"]["label"] == dataset_name:
graph_id = g["id"]
g = G_epgm.to_nx(graph_id, directed)
if weighted:
raise NotImplementedError
else:
# This is the correct way to set the edge weight in a MultiGraph.
edge_weights = {e: 1 for e in g.edges(keys=True)}
nx.set_edge_attributes(g, name="weight", values=edge_weights)
except: # otherwise, assume arg.input points to an edgelist file
if weighted:
g = nx.read_edgelist(
graph_file,
nodetype=int,
data=(("weight", float),),
create_using=nx.DiGraph(),
)
else:
g = nx.read_edgelist(graph_file, nodetype=int, create_using=nx.DiGraph())
for edge in g.edges():
g[edge[0]][edge[1]]["weight"] = 1
if not directed:
g = g.to_undirected()
if not nx.is_connected(g):
print("Graph is not connected")
# take the largest connected component as the data
g_ccs = (g.subgraph(c).copy() for c in nx.connected_components(g))
g = max(g_ccs, key=len)
print(
"Largest subgraph statistics: {} nodes, {} edges".format(
g.number_of_nodes(), g.number_of_edges()
)
)
print(
"Graph statistics: {} nodes, {} edges".format(
g.number_of_nodes(), g.number_of_edges()
)
)
return g
class TestEdgeSplitterHomogeneous(object):
print(os.getcwd())
if os.getcwd().split("/")[-1] == "tests":
input_dir = os.path.expanduser("resources/data/cora/cora.epgm")
else:
input_dir = os.path.expanduser("tests/resources/data/cora/cora.epgm")
dataset_name = "cora"
g = read_graph(input_dir, dataset_name)
g = nx.Graph(g)
es_obj = EdgeSplitter(g)
def test_split_data_global(self):
p = 0.1
g_test, edge_data_ids_test, edge_data_labels_test = self.es_obj.train_test_split(
p=p, method="global", keep_connected=True
)
# if all goes well, what are the expected return values?
num_sampled_positives = np.sum(edge_data_labels_test == 1)
num_sampled_negatives = np.sum(edge_data_labels_test == 0)
assert num_sampled_positives > 0
assert num_sampled_negatives > 0
assert len(edge_data_ids_test) == len(edge_data_labels_test)
assert (num_sampled_positives - num_sampled_negatives) == 0
assert len(g_test.edges()) < len(self.g.edges())
assert nx.is_connected(g_test)
with pytest.raises(ValueError):
# This should raise ValueError because it is asking for more positive samples that are available
# without breaking graph connectivity
g_test, edge_data_ids_test, edge_data_labels_test = self.es_obj.train_test_split(
p=0.8, method="global", keep_connected=True
)
def test_split_data_local(self):
p = 0.1
# using default sampling probabilities
g_test, edge_data_ids_test, edge_data_labels_test = self.es_obj.train_test_split(
p=p, method="local", keep_connected=True
)
# if all goes well, what are the expected return values?
num_sampled_positives = np.sum(edge_data_labels_test == 1)
num_sampled_negatives = np.sum(edge_data_labels_test == 0)
assert num_sampled_positives > 0
assert num_sampled_negatives > 0
assert len(edge_data_ids_test) == len(edge_data_labels_test)
assert (num_sampled_positives - num_sampled_negatives) == 0
assert len(g_test.edges()) < len(self.g.edges())
assert nx.is_connected(g_test)
sampling_probs = [0.0, 0.0, 0.1, 0.2, 0.5, 0.2]
g_test, edge_data_ids_test, edge_data_labels_test = self.es_obj.train_test_split(
p=p, method="local", probs=sampling_probs, keep_connected=True
)
num_sampled_positives = np.sum(edge_data_labels_test == 1)
num_sampled_negatives = np.sum(edge_data_labels_test == 0)
assert num_sampled_positives > 0
assert num_sampled_negatives > 0
assert len(edge_data_ids_test) == len(edge_data_labels_test)
assert (num_sampled_positives - num_sampled_negatives) == 0
assert len(g_test.edges()) < len(self.g.edges())
assert nx.is_connected(g_test)
with pytest.raises(ValueError):
# This should raise ValueError because it is asking for more positive samples that are available
# without breaking graph connectivity
self.es_obj.train_test_split(
p=0.8, method="local", probs=sampling_probs, keep_connected=True
)
sampling_probs = [0.2, 0.1, 0.2, 0.5, 0.2] # values don't sum to 1
with pytest.raises(ValueError):
self.es_obj.train_test_split(p=p, method="local", probs=sampling_probs)
class TestEdgeSplitterHeterogeneous(object):
g = create_heterogeneous_graph()
es_obj = EdgeSplitter(g)
def test_split_data_by_edge_type_and_attribute(self):
# test global method for negative edge sampling
self._test_split_data_by_edge_type_and_attribute(method="global")
# test local method for positive edge sampling
self._test_split_data_by_edge_type_and_attribute(method="local")
def _test_split_data_by_edge_type_and_attribute(self, method):
p = 0.1
res = self.es_obj.train_test_split(
p=p,
method=method,
keep_connected=True,
edge_label="friend",
edge_attribute_label="date",
attribute_is_datetime=True,
edge_attribute_threshold="01/01/2008",
)
g_test, edge_data_ids_test, edge_data_labels_test = res
# if all goes well, what are the expected return values?
num_sampled_positives = np.sum(edge_data_labels_test == 1)
num_sampled_negatives = np.sum(edge_data_labels_test == 0)
assert num_sampled_positives > 0
assert num_sampled_negatives > 0
assert len(edge_data_ids_test) == len(edge_data_labels_test)
assert (num_sampled_positives - num_sampled_negatives) == 0
assert len(g_test.edges()) < len(self.g.edges())
assert nx.is_connected(g_test)
p = 0.8
with pytest.raises(ValueError):
# This will raise ValueError because it cannot sample enough positive edges while maintaining graph
# connectivity
self.es_obj.train_test_split(
p=p,
method=method,
keep_connected=True,
edge_label="friend",
edge_attribute_label="date",
attribute_is_datetime=True,
edge_attribute_threshold="01/01/2008",
)
with pytest.raises(ValueError):
# This will raise ValueError because it cannot sample enough negative edges of the given edge_label.
self.es_obj.train_test_split(
p=p,
method=method,
keep_connected=False,
edge_label="friend",
edge_attribute_label="date",
attribute_is_datetime=True,
edge_attribute_threshold="01/01/2008",
)
p = 0.1
with pytest.raises(KeyError):
# This call will raise an exception because the edges of type friend don't have attribute of type 'Any'
self.es_obj.train_test_split(
p=p,
method=method,
keep_connected=True,
edge_label="friend",
edge_attribute_label="Any",
attribute_is_datetime=True,
edge_attribute_threshold="01/01/2008",
)
with pytest.raises(KeyError):
# This call will raise and exception because edges of type 'towards' don't have a 'date' attribute
self.es_obj.train_test_split(
p=p,
method=method,
keep_connected=True,
edge_label="published-at",
edge_attribute_label="date",
attribute_is_datetime=True,
edge_attribute_threshold="01/01/2008",
)
with pytest.raises(ValueError):
# This call will raise an exception because the edge attribute must be specified as datetime
self.es_obj.train_test_split(
p=p,
method=method,
keep_connected=True,
edge_label="friend",
edge_attribute_label="date",
attribute_is_datetime=False,
edge_attribute_threshold="01/01/2008",
)
# Th below call will raise an exception because the threshold value does not have the correct format dd/mm/yyyy
with pytest.raises(ValueError):
self.es_obj.train_test_split(
p=p,
method=method,
keep_connected=True,
edge_label="friend",
edge_attribute_label="date",
attribute_is_datetime=True,
edge_attribute_threshold="01/2008",
)
with pytest.raises(ValueError):
self.es_obj.train_test_split(
p=p,
method=method,
keep_connected=True,
edge_label="friend",
edge_attribute_label="date",
attribute_is_datetime=True,
edge_attribute_threshold="Jan 2005",
)
with pytest.raises(ValueError):
self.es_obj.train_test_split(
p=p,
method=method,
keep_connected=True,
edge_label="friend",
edge_attribute_label="date",
attribute_is_datetime=True,
edge_attribute_threshold="01-01-2000",
)
with pytest.raises(ValueError):
# month is out of range; no such thing as a 14th month in a year
self.es_obj.train_test_split(
p=p,
method=method,
keep_connected=True,
edge_label="friend",
edge_attribute_label="date",
attribute_is_datetime=True,
edge_attribute_threshold="01/14/2008",
)
with pytest.raises(ValueError):
# day is out of range; no such thing as a 32nd day in October
self.es_obj.train_test_split(
p=p,
method=method,
keep_connected=True,
edge_label="friend",
edge_attribute_label="date",
attribute_is_datetime=True,
edge_attribute_threshold="32/10/2008",
)
with pytest.raises(Exception):
# This call to train_test_split will raise an exception because all the edges of type 'writes' are
# on the minimum spanning tree and cannot be removed.
self.es_obj.train_test_split(
p=p,
method=method,
keep_connected=True,
edge_label="writes",
edge_attribute_label="date",
attribute_is_datetime=True,
edge_attribute_threshold="01/01/2008",
)
def test_split_data_by_edge_type(self):
# test global method for negative edge sampling
self._test_split_data_by_edge_type(method="global")
# test local method for positive edge sampling
self._test_split_data_by_edge_type(method="local")
def _test_split_data_by_edge_type(self, method):
p = 0.1
g_test, edge_data_ids_test, edge_data_labels_test = self.es_obj.train_test_split(
p=p, method=method, edge_label="friend", keep_connected=True
)
# if all goes well, what are the expected return values?
num_sampled_positives = np.sum(edge_data_labels_test == 1)
num_sampled_negatives = np.sum(edge_data_labels_test == 0)
assert len(edge_data_ids_test) == len(edge_data_labels_test)
assert (num_sampled_positives - num_sampled_negatives) == 0
assert len(g_test.edges()) < len(self.g.edges())
assert nx.is_connected(g_test)
with pytest.raises(Exception):
# This call will raise an exception because the graph has no edges of type 'Non Label'
self.es_obj.train_test_split(
p=p, method=method, keep_connected=True, edge_label="No Label"
)
p = 0.8
with pytest.raises(ValueError):
self.es_obj.train_test_split(
p=p, method=method, edge_label="friend", keep_connected=True
)
p = 0.8
with pytest.raises(ValueError):
self.es_obj.train_test_split(
p=p, method=method, edge_label="friend", keep_connected=False
)
def test_split_data_global(self):
p = 0.1
g_test, edge_data_ids_test, edge_data_labels_test = self.es_obj.train_test_split(
p=p, method="global", keep_connected=True
)
# if all goes well, what are the expected return values?
num_sampled_positives = np.sum(edge_data_labels_test == 1)
num_sampled_negatives = np.sum(edge_data_labels_test == 0)
assert num_sampled_positives > 0
assert num_sampled_negatives > 0
assert len(edge_data_ids_test) == len(edge_data_labels_test)
assert (num_sampled_positives - num_sampled_negatives) == 0
assert len(g_test.edges()) < len(self.g.edges())
assert nx.is_connected(g_test)
def test_split_data_local(self):
p = 0.1
# using default sampling probabilities
g_test, edge_data_ids_test, edge_data_labels_test = self.es_obj.train_test_split(
p=p, method="local", keep_connected=True
)
# if all goes well, what are the expected return values?
num_sampled_positives = np.sum(edge_data_labels_test == 1)
num_sampled_negatives = np.sum(edge_data_labels_test == 0)
assert num_sampled_positives > 0
assert num_sampled_negatives > 0
assert len(edge_data_ids_test) == len(edge_data_labels_test)
assert (num_sampled_positives - num_sampled_negatives) == 0
assert len(g_test.edges()) < len(self.g.edges())
assert nx.is_connected(g_test)
class TestEdgeSplitterCommon(object):
g = create_heterogeneous_graph()
es_obj = EdgeSplitter(g)
def test_split_data_keep_connected_parameter(self):
# keep_connected must be bool type.
with pytest.raises(ValueError):
self.es_obj.train_test_split(keep_connected="Yes")
with pytest.raises(ValueError):
self.es_obj.train_test_split(keep_connected=0)
with pytest.raises(ValueError):
self.es_obj.train_test_split(keep_connected=None)
def test_split_data_p_parameter(self):
# Test some edge cases for the value of p, e.g., < 0, = 0, > 1, =1
p = 0
with pytest.raises(ValueError):
self.es_obj.train_test_split(p=p, method="global")
p = -0.1
with pytest.raises(ValueError):
self.es_obj.train_test_split(p=p, method="global")
p = 1.001
with pytest.raises(ValueError):
self.es_obj.train_test_split(p=p, method="global")
p = 1
with pytest.raises(ValueError):
self.es_obj.train_test_split(p=p, method="global")
def test_split_data_method_parameter(self):
p = 0.5 # any value in the interval (0, 1) should do
sampling_method = "other" # correct values are global and local only
with pytest.raises(ValueError):
self.es_obj.train_test_split(p=p, method=sampling_method)