ZTWHHH commited on
Commit
30a252b
·
verified ·
1 Parent(s): 9c74977

Add files using upload-large-folder tool

Browse files
This view is limited to 50 files because it contains too many changes.   See raw diff
Files changed (50) hide show
  1. minigpt2/lib/python3.10/site-packages/networkx/__pycache__/__init__.cpython-310.pyc +0 -0
  2. minigpt2/lib/python3.10/site-packages/networkx/__pycache__/conftest.cpython-310.pyc +0 -0
  3. minigpt2/lib/python3.10/site-packages/networkx/__pycache__/convert.cpython-310.pyc +0 -0
  4. minigpt2/lib/python3.10/site-packages/networkx/__pycache__/convert_matrix.cpython-310.pyc +0 -0
  5. minigpt2/lib/python3.10/site-packages/networkx/__pycache__/exception.cpython-310.pyc +0 -0
  6. minigpt2/lib/python3.10/site-packages/networkx/__pycache__/lazy_imports.cpython-310.pyc +0 -0
  7. minigpt2/lib/python3.10/site-packages/networkx/__pycache__/relabel.cpython-310.pyc +0 -0
  8. minigpt2/lib/python3.10/site-packages/networkx/classes/coreviews.py +431 -0
  9. minigpt2/lib/python3.10/site-packages/networkx/classes/digraph.py +1352 -0
  10. minigpt2/lib/python3.10/site-packages/networkx/classes/multigraph.py +1283 -0
  11. minigpt2/lib/python3.10/site-packages/networkx/classes/reportviews.py +1447 -0
  12. minigpt2/lib/python3.10/site-packages/networkx/classes/tests/__pycache__/test_filters.cpython-310.pyc +0 -0
  13. minigpt2/lib/python3.10/site-packages/networkx/classes/tests/__pycache__/test_function.cpython-310.pyc +0 -0
  14. minigpt2/lib/python3.10/site-packages/networkx/classes/tests/__pycache__/test_graphviews.cpython-310.pyc +0 -0
  15. minigpt2/lib/python3.10/site-packages/networkx/classes/tests/__pycache__/test_multigraph.cpython-310.pyc +0 -0
  16. minigpt2/lib/python3.10/site-packages/networkx/classes/tests/dispatch_interface.py +185 -0
  17. minigpt2/lib/python3.10/site-packages/networkx/classes/tests/historical_tests.py +475 -0
  18. minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_digraph.py +331 -0
  19. minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_function.py +1035 -0
  20. minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_graph.py +920 -0
  21. minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_graphviews.py +350 -0
  22. minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_multigraph.py +528 -0
  23. minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_reportviews.py +1435 -0
  24. minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_special.py +131 -0
  25. minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_subgraphviews.py +362 -0
  26. minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/__init__.cpython-310.pyc +0 -0
  27. minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/edgelist.cpython-310.pyc +0 -0
  28. minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/gexf.cpython-310.pyc +0 -0
  29. minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/gml.cpython-310.pyc +0 -0
  30. minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/graph6.cpython-310.pyc +0 -0
  31. minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/graphml.cpython-310.pyc +0 -0
  32. minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/multiline_adjlist.cpython-310.pyc +0 -0
  33. minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/pajek.cpython-310.pyc +0 -0
  34. minigpt2/lib/python3.10/site-packages/networkx/readwrite/json_graph/__pycache__/__init__.cpython-310.pyc +0 -0
  35. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_efficient_attention_backward_ops.h +28 -0
  36. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_foobar_compositeexplicitautograd_dispatch.h +24 -0
  37. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_foreach_abs_ops.h +50 -0
  38. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_foreach_erf_cuda_dispatch.h +24 -0
  39. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_fw_primal_copy_ops.h +39 -0
  40. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_pad_circular_ops.h +28 -0
  41. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_slow_conv2d_backward.h +135 -0
  42. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_weight_norm_interface_backward_cuda_dispatch.h +23 -0
  43. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/bincount_ops.h +39 -0
  44. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/convolution_backward_overrideable_native.h +22 -0
  45. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/detach_copy_compositeexplicitautogradnonfunctional_dispatch.h +23 -0
  46. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/embedding_bag_ops.h +39 -0
  47. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/fractional_max_pool2d_cpu_dispatch.h +25 -0
  48. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/hinge_embedding_loss_ops.h +28 -0
  49. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/index_select_native.h +30 -0
  50. parrot/lib/python3.10/site-packages/torch/include/ATen/ops/leaky_relu_meta.h +27 -0
minigpt2/lib/python3.10/site-packages/networkx/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (1.29 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/__pycache__/conftest.cpython-310.pyc ADDED
Binary file (6.03 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/__pycache__/convert.cpython-310.pyc ADDED
Binary file (13.1 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/__pycache__/convert_matrix.cpython-310.pyc ADDED
Binary file (41.2 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/__pycache__/exception.cpython-310.pyc ADDED
Binary file (4.76 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/__pycache__/lazy_imports.cpython-310.pyc ADDED
Binary file (5.85 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/__pycache__/relabel.cpython-310.pyc ADDED
Binary file (10.1 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/classes/coreviews.py ADDED
@@ -0,0 +1,431 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Views of core data structures such as nested Mappings (e.g. dict-of-dicts).
2
+ These ``Views`` often restrict element access, with either the entire view or
3
+ layers of nested mappings being read-only.
4
+ """
5
+
6
+ from collections.abc import Mapping
7
+
8
+ __all__ = [
9
+ "AtlasView",
10
+ "AdjacencyView",
11
+ "MultiAdjacencyView",
12
+ "UnionAtlas",
13
+ "UnionAdjacency",
14
+ "UnionMultiInner",
15
+ "UnionMultiAdjacency",
16
+ "FilterAtlas",
17
+ "FilterAdjacency",
18
+ "FilterMultiInner",
19
+ "FilterMultiAdjacency",
20
+ ]
21
+
22
+
23
+ class AtlasView(Mapping):
24
+ """An AtlasView is a Read-only Mapping of Mappings.
25
+
26
+ It is a View into a dict-of-dict data structure.
27
+ The inner level of dict is read-write. But the
28
+ outer level is read-only.
29
+
30
+ See Also
31
+ ========
32
+ AdjacencyView: View into dict-of-dict-of-dict
33
+ MultiAdjacencyView: View into dict-of-dict-of-dict-of-dict
34
+ """
35
+
36
+ __slots__ = ("_atlas",)
37
+
38
+ def __getstate__(self):
39
+ return {"_atlas": self._atlas}
40
+
41
+ def __setstate__(self, state):
42
+ self._atlas = state["_atlas"]
43
+
44
+ def __init__(self, d):
45
+ self._atlas = d
46
+
47
+ def __len__(self):
48
+ return len(self._atlas)
49
+
50
+ def __iter__(self):
51
+ return iter(self._atlas)
52
+
53
+ def __getitem__(self, key):
54
+ return self._atlas[key]
55
+
56
+ def copy(self):
57
+ return {n: self[n].copy() for n in self._atlas}
58
+
59
+ def __str__(self):
60
+ return str(self._atlas) # {nbr: self[nbr] for nbr in self})
61
+
62
+ def __repr__(self):
63
+ return f"{self.__class__.__name__}({self._atlas!r})"
64
+
65
+
66
+ class AdjacencyView(AtlasView):
67
+ """An AdjacencyView is a Read-only Map of Maps of Maps.
68
+
69
+ It is a View into a dict-of-dict-of-dict data structure.
70
+ The inner level of dict is read-write. But the
71
+ outer levels are read-only.
72
+
73
+ See Also
74
+ ========
75
+ AtlasView: View into dict-of-dict
76
+ MultiAdjacencyView: View into dict-of-dict-of-dict-of-dict
77
+ """
78
+
79
+ __slots__ = () # Still uses AtlasView slots names _atlas
80
+
81
+ def __getitem__(self, name):
82
+ return AtlasView(self._atlas[name])
83
+
84
+ def copy(self):
85
+ return {n: self[n].copy() for n in self._atlas}
86
+
87
+
88
+ class MultiAdjacencyView(AdjacencyView):
89
+ """An MultiAdjacencyView is a Read-only Map of Maps of Maps of Maps.
90
+
91
+ It is a View into a dict-of-dict-of-dict-of-dict data structure.
92
+ The inner level of dict is read-write. But the
93
+ outer levels are read-only.
94
+
95
+ See Also
96
+ ========
97
+ AtlasView: View into dict-of-dict
98
+ AdjacencyView: View into dict-of-dict-of-dict
99
+ """
100
+
101
+ __slots__ = () # Still uses AtlasView slots names _atlas
102
+
103
+ def __getitem__(self, name):
104
+ return AdjacencyView(self._atlas[name])
105
+
106
+ def copy(self):
107
+ return {n: self[n].copy() for n in self._atlas}
108
+
109
+
110
+ class UnionAtlas(Mapping):
111
+ """A read-only union of two atlases (dict-of-dict).
112
+
113
+ The two dict-of-dicts represent the inner dict of
114
+ an Adjacency: `G.succ[node]` and `G.pred[node]`.
115
+ The inner level of dict of both hold attribute key:value
116
+ pairs and is read-write. But the outer level is read-only.
117
+
118
+ See Also
119
+ ========
120
+ UnionAdjacency: View into dict-of-dict-of-dict
121
+ UnionMultiAdjacency: View into dict-of-dict-of-dict-of-dict
122
+ """
123
+
124
+ __slots__ = ("_succ", "_pred")
125
+
126
+ def __getstate__(self):
127
+ return {"_succ": self._succ, "_pred": self._pred}
128
+
129
+ def __setstate__(self, state):
130
+ self._succ = state["_succ"]
131
+ self._pred = state["_pred"]
132
+
133
+ def __init__(self, succ, pred):
134
+ self._succ = succ
135
+ self._pred = pred
136
+
137
+ def __len__(self):
138
+ return len(self._succ.keys() | self._pred.keys())
139
+
140
+ def __iter__(self):
141
+ return iter(set(self._succ.keys()) | set(self._pred.keys()))
142
+
143
+ def __getitem__(self, key):
144
+ try:
145
+ return self._succ[key]
146
+ except KeyError:
147
+ return self._pred[key]
148
+
149
+ def copy(self):
150
+ result = {nbr: dd.copy() for nbr, dd in self._succ.items()}
151
+ for nbr, dd in self._pred.items():
152
+ if nbr in result:
153
+ result[nbr].update(dd)
154
+ else:
155
+ result[nbr] = dd.copy()
156
+ return result
157
+
158
+ def __str__(self):
159
+ return str({nbr: self[nbr] for nbr in self})
160
+
161
+ def __repr__(self):
162
+ return f"{self.__class__.__name__}({self._succ!r}, {self._pred!r})"
163
+
164
+
165
+ class UnionAdjacency(Mapping):
166
+ """A read-only union of dict Adjacencies as a Map of Maps of Maps.
167
+
168
+ The two input dict-of-dict-of-dicts represent the union of
169
+ `G.succ` and `G.pred`. Return values are UnionAtlas
170
+ The inner level of dict is read-write. But the
171
+ middle and outer levels are read-only.
172
+
173
+ succ : a dict-of-dict-of-dict {node: nbrdict}
174
+ pred : a dict-of-dict-of-dict {node: nbrdict}
175
+ The keys for the two dicts should be the same
176
+
177
+ See Also
178
+ ========
179
+ UnionAtlas: View into dict-of-dict
180
+ UnionMultiAdjacency: View into dict-of-dict-of-dict-of-dict
181
+ """
182
+
183
+ __slots__ = ("_succ", "_pred")
184
+
185
+ def __getstate__(self):
186
+ return {"_succ": self._succ, "_pred": self._pred}
187
+
188
+ def __setstate__(self, state):
189
+ self._succ = state["_succ"]
190
+ self._pred = state["_pred"]
191
+
192
+ def __init__(self, succ, pred):
193
+ # keys must be the same for two input dicts
194
+ assert len(set(succ.keys()) ^ set(pred.keys())) == 0
195
+ self._succ = succ
196
+ self._pred = pred
197
+
198
+ def __len__(self):
199
+ return len(self._succ) # length of each dict should be the same
200
+
201
+ def __iter__(self):
202
+ return iter(self._succ)
203
+
204
+ def __getitem__(self, nbr):
205
+ return UnionAtlas(self._succ[nbr], self._pred[nbr])
206
+
207
+ def copy(self):
208
+ return {n: self[n].copy() for n in self._succ}
209
+
210
+ def __str__(self):
211
+ return str({nbr: self[nbr] for nbr in self})
212
+
213
+ def __repr__(self):
214
+ return f"{self.__class__.__name__}({self._succ!r}, {self._pred!r})"
215
+
216
+
217
+ class UnionMultiInner(UnionAtlas):
218
+ """A read-only union of two inner dicts of MultiAdjacencies.
219
+
220
+ The two input dict-of-dict-of-dicts represent the union of
221
+ `G.succ[node]` and `G.pred[node]` for MultiDiGraphs.
222
+ Return values are UnionAtlas.
223
+ The inner level of dict is read-write. But the outer levels are read-only.
224
+
225
+ See Also
226
+ ========
227
+ UnionAtlas: View into dict-of-dict
228
+ UnionAdjacency: View into dict-of-dict-of-dict
229
+ UnionMultiAdjacency: View into dict-of-dict-of-dict-of-dict
230
+ """
231
+
232
+ __slots__ = () # Still uses UnionAtlas slots names _succ, _pred
233
+
234
+ def __getitem__(self, node):
235
+ in_succ = node in self._succ
236
+ in_pred = node in self._pred
237
+ if in_succ:
238
+ if in_pred:
239
+ return UnionAtlas(self._succ[node], self._pred[node])
240
+ return UnionAtlas(self._succ[node], {})
241
+ return UnionAtlas({}, self._pred[node])
242
+
243
+ def copy(self):
244
+ nodes = set(self._succ.keys()) | set(self._pred.keys())
245
+ return {n: self[n].copy() for n in nodes}
246
+
247
+
248
+ class UnionMultiAdjacency(UnionAdjacency):
249
+ """A read-only union of two dict MultiAdjacencies.
250
+
251
+ The two input dict-of-dict-of-dict-of-dicts represent the union of
252
+ `G.succ` and `G.pred` for MultiDiGraphs. Return values are UnionAdjacency.
253
+ The inner level of dict is read-write. But the outer levels are read-only.
254
+
255
+ See Also
256
+ ========
257
+ UnionAtlas: View into dict-of-dict
258
+ UnionMultiInner: View into dict-of-dict-of-dict
259
+ """
260
+
261
+ __slots__ = () # Still uses UnionAdjacency slots names _succ, _pred
262
+
263
+ def __getitem__(self, node):
264
+ return UnionMultiInner(self._succ[node], self._pred[node])
265
+
266
+
267
+ class FilterAtlas(Mapping): # nodedict, nbrdict, keydict
268
+ """A read-only Mapping of Mappings with filtering criteria for nodes.
269
+
270
+ It is a view into a dict-of-dict data structure, and it selects only
271
+ nodes that meet the criteria defined by ``NODE_OK``.
272
+
273
+ See Also
274
+ ========
275
+ FilterAdjacency
276
+ FilterMultiInner
277
+ FilterMultiAdjacency
278
+ """
279
+
280
+ def __init__(self, d, NODE_OK):
281
+ self._atlas = d
282
+ self.NODE_OK = NODE_OK
283
+
284
+ def __len__(self):
285
+ # check whether NODE_OK stores the number of nodes as `length`
286
+ # or the nodes themselves as a set `nodes`. If not, count the nodes.
287
+ if hasattr(self.NODE_OK, "length"):
288
+ return self.NODE_OK.length
289
+ if hasattr(self.NODE_OK, "nodes"):
290
+ return len(self.NODE_OK.nodes & self._atlas.keys())
291
+ return sum(1 for n in self._atlas if self.NODE_OK(n))
292
+
293
+ def __iter__(self):
294
+ try: # check that NODE_OK has attr 'nodes'
295
+ node_ok_shorter = 2 * len(self.NODE_OK.nodes) < len(self._atlas)
296
+ except AttributeError:
297
+ node_ok_shorter = False
298
+ if node_ok_shorter:
299
+ return (n for n in self.NODE_OK.nodes if n in self._atlas)
300
+ return (n for n in self._atlas if self.NODE_OK(n))
301
+
302
+ def __getitem__(self, key):
303
+ if key in self._atlas and self.NODE_OK(key):
304
+ return self._atlas[key]
305
+ raise KeyError(f"Key {key} not found")
306
+
307
+ def __str__(self):
308
+ return str({nbr: self[nbr] for nbr in self})
309
+
310
+ def __repr__(self):
311
+ return f"{self.__class__.__name__}({self._atlas!r}, {self.NODE_OK!r})"
312
+
313
+
314
+ class FilterAdjacency(Mapping): # edgedict
315
+ """A read-only Mapping of Mappings with filtering criteria for nodes and edges.
316
+
317
+ It is a view into a dict-of-dict-of-dict data structure, and it selects nodes
318
+ and edges that satisfy specific criteria defined by ``NODE_OK`` and ``EDGE_OK``,
319
+ respectively.
320
+
321
+ See Also
322
+ ========
323
+ FilterAtlas
324
+ FilterMultiInner
325
+ FilterMultiAdjacency
326
+ """
327
+
328
+ def __init__(self, d, NODE_OK, EDGE_OK):
329
+ self._atlas = d
330
+ self.NODE_OK = NODE_OK
331
+ self.EDGE_OK = EDGE_OK
332
+
333
+ def __len__(self):
334
+ # check whether NODE_OK stores the number of nodes as `length`
335
+ # or the nodes themselves as a set `nodes`. If not, count the nodes.
336
+ if hasattr(self.NODE_OK, "length"):
337
+ return self.NODE_OK.length
338
+ if hasattr(self.NODE_OK, "nodes"):
339
+ return len(self.NODE_OK.nodes & self._atlas.keys())
340
+ return sum(1 for n in self._atlas if self.NODE_OK(n))
341
+
342
+ def __iter__(self):
343
+ try: # check that NODE_OK has attr 'nodes'
344
+ node_ok_shorter = 2 * len(self.NODE_OK.nodes) < len(self._atlas)
345
+ except AttributeError:
346
+ node_ok_shorter = False
347
+ if node_ok_shorter:
348
+ return (n for n in self.NODE_OK.nodes if n in self._atlas)
349
+ return (n for n in self._atlas if self.NODE_OK(n))
350
+
351
+ def __getitem__(self, node):
352
+ if node in self._atlas and self.NODE_OK(node):
353
+
354
+ def new_node_ok(nbr):
355
+ return self.NODE_OK(nbr) and self.EDGE_OK(node, nbr)
356
+
357
+ return FilterAtlas(self._atlas[node], new_node_ok)
358
+ raise KeyError(f"Key {node} not found")
359
+
360
+ def __str__(self):
361
+ return str({nbr: self[nbr] for nbr in self})
362
+
363
+ def __repr__(self):
364
+ name = self.__class__.__name__
365
+ return f"{name}({self._atlas!r}, {self.NODE_OK!r}, {self.EDGE_OK!r})"
366
+
367
+
368
+ class FilterMultiInner(FilterAdjacency): # muliedge_seconddict
369
+ """A read-only Mapping of Mappings with filtering criteria for nodes and edges.
370
+
371
+ It is a view into a dict-of-dict-of-dict-of-dict data structure, and it selects nodes
372
+ and edges that meet specific criteria defined by ``NODE_OK`` and ``EDGE_OK``.
373
+
374
+ See Also
375
+ ========
376
+ FilterAtlas
377
+ FilterAdjacency
378
+ FilterMultiAdjacency
379
+ """
380
+
381
+ def __iter__(self):
382
+ try: # check that NODE_OK has attr 'nodes'
383
+ node_ok_shorter = 2 * len(self.NODE_OK.nodes) < len(self._atlas)
384
+ except AttributeError:
385
+ node_ok_shorter = False
386
+ if node_ok_shorter:
387
+ my_nodes = (n for n in self.NODE_OK.nodes if n in self._atlas)
388
+ else:
389
+ my_nodes = (n for n in self._atlas if self.NODE_OK(n))
390
+ for n in my_nodes:
391
+ some_keys_ok = False
392
+ for key in self._atlas[n]:
393
+ if self.EDGE_OK(n, key):
394
+ some_keys_ok = True
395
+ break
396
+ if some_keys_ok is True:
397
+ yield n
398
+
399
+ def __getitem__(self, nbr):
400
+ if nbr in self._atlas and self.NODE_OK(nbr):
401
+
402
+ def new_node_ok(key):
403
+ return self.EDGE_OK(nbr, key)
404
+
405
+ return FilterAtlas(self._atlas[nbr], new_node_ok)
406
+ raise KeyError(f"Key {nbr} not found")
407
+
408
+
409
+ class FilterMultiAdjacency(FilterAdjacency): # multiedgedict
410
+ """A read-only Mapping of Mappings with filtering criteria
411
+ for nodes and edges.
412
+
413
+ It is a view into a dict-of-dict-of-dict-of-dict data structure,
414
+ and it selects nodes and edges that satisfy specific criteria
415
+ defined by ``NODE_OK`` and ``EDGE_OK``, respectively.
416
+
417
+ See Also
418
+ ========
419
+ FilterAtlas
420
+ FilterAdjacency
421
+ FilterMultiInner
422
+ """
423
+
424
+ def __getitem__(self, node):
425
+ if node in self._atlas and self.NODE_OK(node):
426
+
427
+ def edge_ok(nbr, key):
428
+ return self.NODE_OK(nbr) and self.EDGE_OK(node, nbr, key)
429
+
430
+ return FilterMultiInner(self._atlas[node], self.NODE_OK, edge_ok)
431
+ raise KeyError(f"Key {node} not found")
minigpt2/lib/python3.10/site-packages/networkx/classes/digraph.py ADDED
@@ -0,0 +1,1352 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Base class for directed graphs."""
2
+
3
+ from copy import deepcopy
4
+ from functools import cached_property
5
+
6
+ import networkx as nx
7
+ from networkx import convert
8
+ from networkx.classes.coreviews import AdjacencyView
9
+ from networkx.classes.graph import Graph
10
+ from networkx.classes.reportviews import (
11
+ DiDegreeView,
12
+ InDegreeView,
13
+ InEdgeView,
14
+ OutDegreeView,
15
+ OutEdgeView,
16
+ )
17
+ from networkx.exception import NetworkXError
18
+
19
+ __all__ = ["DiGraph"]
20
+
21
+
22
+ class _CachedPropertyResetterAdjAndSucc:
23
+ """Data Descriptor class that syncs and resets cached properties adj and succ
24
+
25
+ The cached properties `adj` and `succ` are reset whenever `_adj` or `_succ`
26
+ are set to new objects. In addition, the attributes `_succ` and `_adj`
27
+ are synced so these two names point to the same object.
28
+
29
+ Warning: most of the time, when ``G._adj`` is set, ``G._pred`` should also
30
+ be set to maintain a valid data structure. They share datadicts.
31
+
32
+ This object sits on a class and ensures that any instance of that
33
+ class clears its cached properties "succ" and "adj" whenever the
34
+ underlying instance attributes "_succ" or "_adj" are set to a new object.
35
+ It only affects the set process of the obj._adj and obj._succ attribute.
36
+ All get/del operations act as they normally would.
37
+
38
+ For info on Data Descriptors see: https://docs.python.org/3/howto/descriptor.html
39
+ """
40
+
41
+ def __set__(self, obj, value):
42
+ od = obj.__dict__
43
+ od["_adj"] = value
44
+ od["_succ"] = value
45
+ # reset cached properties
46
+ props = [
47
+ "adj",
48
+ "succ",
49
+ "edges",
50
+ "out_edges",
51
+ "degree",
52
+ "out_degree",
53
+ "in_degree",
54
+ ]
55
+ for prop in props:
56
+ if prop in od:
57
+ del od[prop]
58
+
59
+
60
+ class _CachedPropertyResetterPred:
61
+ """Data Descriptor class for _pred that resets ``pred`` cached_property when needed
62
+
63
+ This assumes that the ``cached_property`` ``G.pred`` should be reset whenever
64
+ ``G._pred`` is set to a new value.
65
+
66
+ Warning: most of the time, when ``G._pred`` is set, ``G._adj`` should also
67
+ be set to maintain a valid data structure. They share datadicts.
68
+
69
+ This object sits on a class and ensures that any instance of that
70
+ class clears its cached property "pred" whenever the underlying
71
+ instance attribute "_pred" is set to a new object. It only affects
72
+ the set process of the obj._pred attribute. All get/del operations
73
+ act as they normally would.
74
+
75
+ For info on Data Descriptors see: https://docs.python.org/3/howto/descriptor.html
76
+ """
77
+
78
+ def __set__(self, obj, value):
79
+ od = obj.__dict__
80
+ od["_pred"] = value
81
+ # reset cached properties
82
+ props = ["pred", "in_edges", "degree", "out_degree", "in_degree"]
83
+ for prop in props:
84
+ if prop in od:
85
+ del od[prop]
86
+
87
+
88
+ class DiGraph(Graph):
89
+ """
90
+ Base class for directed graphs.
91
+
92
+ A DiGraph stores nodes and edges with optional data, or attributes.
93
+
94
+ DiGraphs hold directed edges. Self loops are allowed but multiple
95
+ (parallel) edges are not.
96
+
97
+ Nodes can be arbitrary (hashable) Python objects with optional
98
+ key/value attributes. By convention `None` is not used as a node.
99
+
100
+ Edges are represented as links between nodes with optional
101
+ key/value attributes.
102
+
103
+ Parameters
104
+ ----------
105
+ incoming_graph_data : input graph (optional, default: None)
106
+ Data to initialize graph. If None (default) an empty
107
+ graph is created. The data can be any format that is supported
108
+ by the to_networkx_graph() function, currently including edge list,
109
+ dict of dicts, dict of lists, NetworkX graph, 2D NumPy array, SciPy
110
+ sparse matrix, or PyGraphviz graph.
111
+
112
+ attr : keyword arguments, optional (default= no attributes)
113
+ Attributes to add to graph as key=value pairs.
114
+
115
+ See Also
116
+ --------
117
+ Graph
118
+ MultiGraph
119
+ MultiDiGraph
120
+
121
+ Examples
122
+ --------
123
+ Create an empty graph structure (a "null graph") with no nodes and
124
+ no edges.
125
+
126
+ >>> G = nx.DiGraph()
127
+
128
+ G can be grown in several ways.
129
+
130
+ **Nodes:**
131
+
132
+ Add one node at a time:
133
+
134
+ >>> G.add_node(1)
135
+
136
+ Add the nodes from any container (a list, dict, set or
137
+ even the lines from a file or the nodes from another graph).
138
+
139
+ >>> G.add_nodes_from([2, 3])
140
+ >>> G.add_nodes_from(range(100, 110))
141
+ >>> H = nx.path_graph(10)
142
+ >>> G.add_nodes_from(H)
143
+
144
+ In addition to strings and integers any hashable Python object
145
+ (except None) can represent a node, e.g. a customized node object,
146
+ or even another Graph.
147
+
148
+ >>> G.add_node(H)
149
+
150
+ **Edges:**
151
+
152
+ G can also be grown by adding edges.
153
+
154
+ Add one edge,
155
+
156
+ >>> G.add_edge(1, 2)
157
+
158
+ a list of edges,
159
+
160
+ >>> G.add_edges_from([(1, 2), (1, 3)])
161
+
162
+ or a collection of edges,
163
+
164
+ >>> G.add_edges_from(H.edges)
165
+
166
+ If some edges connect nodes not yet in the graph, the nodes
167
+ are added automatically. There are no errors when adding
168
+ nodes or edges that already exist.
169
+
170
+ **Attributes:**
171
+
172
+ Each graph, node, and edge can hold key/value attribute pairs
173
+ in an associated attribute dictionary (the keys must be hashable).
174
+ By default these are empty, but can be added or changed using
175
+ add_edge, add_node or direct manipulation of the attribute
176
+ dictionaries named graph, node and edge respectively.
177
+
178
+ >>> G = nx.DiGraph(day="Friday")
179
+ >>> G.graph
180
+ {'day': 'Friday'}
181
+
182
+ Add node attributes using add_node(), add_nodes_from() or G.nodes
183
+
184
+ >>> G.add_node(1, time="5pm")
185
+ >>> G.add_nodes_from([3], time="2pm")
186
+ >>> G.nodes[1]
187
+ {'time': '5pm'}
188
+ >>> G.nodes[1]["room"] = 714
189
+ >>> del G.nodes[1]["room"] # remove attribute
190
+ >>> list(G.nodes(data=True))
191
+ [(1, {'time': '5pm'}), (3, {'time': '2pm'})]
192
+
193
+ Add edge attributes using add_edge(), add_edges_from(), subscript
194
+ notation, or G.edges.
195
+
196
+ >>> G.add_edge(1, 2, weight=4.7)
197
+ >>> G.add_edges_from([(3, 4), (4, 5)], color="red")
198
+ >>> G.add_edges_from([(1, 2, {"color": "blue"}), (2, 3, {"weight": 8})])
199
+ >>> G[1][2]["weight"] = 4.7
200
+ >>> G.edges[1, 2]["weight"] = 4
201
+
202
+ Warning: we protect the graph data structure by making `G.edges[1, 2]` a
203
+ read-only dict-like structure. However, you can assign to attributes
204
+ in e.g. `G.edges[1, 2]`. Thus, use 2 sets of brackets to add/change
205
+ data attributes: `G.edges[1, 2]['weight'] = 4`
206
+ (For multigraphs: `MG.edges[u, v, key][name] = value`).
207
+
208
+ **Shortcuts:**
209
+
210
+ Many common graph features allow python syntax to speed reporting.
211
+
212
+ >>> 1 in G # check if node in graph
213
+ True
214
+ >>> [n for n in G if n < 3] # iterate through nodes
215
+ [1, 2]
216
+ >>> len(G) # number of nodes in graph
217
+ 5
218
+
219
+ Often the best way to traverse all edges of a graph is via the neighbors.
220
+ The neighbors are reported as an adjacency-dict `G.adj` or `G.adjacency()`
221
+
222
+ >>> for n, nbrsdict in G.adjacency():
223
+ ... for nbr, eattr in nbrsdict.items():
224
+ ... if "weight" in eattr:
225
+ ... # Do something useful with the edges
226
+ ... pass
227
+
228
+ But the edges reporting object is often more convenient:
229
+
230
+ >>> for u, v, weight in G.edges(data="weight"):
231
+ ... if weight is not None:
232
+ ... # Do something useful with the edges
233
+ ... pass
234
+
235
+ **Reporting:**
236
+
237
+ Simple graph information is obtained using object-attributes and methods.
238
+ Reporting usually provides views instead of containers to reduce memory
239
+ usage. The views update as the graph is updated similarly to dict-views.
240
+ The objects `nodes`, `edges` and `adj` provide access to data attributes
241
+ via lookup (e.g. `nodes[n]`, `edges[u, v]`, `adj[u][v]`) and iteration
242
+ (e.g. `nodes.items()`, `nodes.data('color')`,
243
+ `nodes.data('color', default='blue')` and similarly for `edges`)
244
+ Views exist for `nodes`, `edges`, `neighbors()`/`adj` and `degree`.
245
+
246
+ For details on these and other miscellaneous methods, see below.
247
+
248
+ **Subclasses (Advanced):**
249
+
250
+ The Graph class uses a dict-of-dict-of-dict data structure.
251
+ The outer dict (node_dict) holds adjacency information keyed by node.
252
+ The next dict (adjlist_dict) represents the adjacency information and holds
253
+ edge data keyed by neighbor. The inner dict (edge_attr_dict) represents
254
+ the edge data and holds edge attribute values keyed by attribute names.
255
+
256
+ Each of these three dicts can be replaced in a subclass by a user defined
257
+ dict-like object. In general, the dict-like features should be
258
+ maintained but extra features can be added. To replace one of the
259
+ dicts create a new graph class by changing the class(!) variable
260
+ holding the factory for that dict-like structure. The variable names are
261
+ node_dict_factory, node_attr_dict_factory, adjlist_inner_dict_factory,
262
+ adjlist_outer_dict_factory, edge_attr_dict_factory and graph_attr_dict_factory.
263
+
264
+ node_dict_factory : function, (default: dict)
265
+ Factory function to be used to create the dict containing node
266
+ attributes, keyed by node id.
267
+ It should require no arguments and return a dict-like object
268
+
269
+ node_attr_dict_factory: function, (default: dict)
270
+ Factory function to be used to create the node attribute
271
+ dict which holds attribute values keyed by attribute name.
272
+ It should require no arguments and return a dict-like object
273
+
274
+ adjlist_outer_dict_factory : function, (default: dict)
275
+ Factory function to be used to create the outer-most dict
276
+ in the data structure that holds adjacency info keyed by node.
277
+ It should require no arguments and return a dict-like object.
278
+
279
+ adjlist_inner_dict_factory : function, optional (default: dict)
280
+ Factory function to be used to create the adjacency list
281
+ dict which holds edge data keyed by neighbor.
282
+ It should require no arguments and return a dict-like object
283
+
284
+ edge_attr_dict_factory : function, optional (default: dict)
285
+ Factory function to be used to create the edge attribute
286
+ dict which holds attribute values keyed by attribute name.
287
+ It should require no arguments and return a dict-like object.
288
+
289
+ graph_attr_dict_factory : function, (default: dict)
290
+ Factory function to be used to create the graph attribute
291
+ dict which holds attribute values keyed by attribute name.
292
+ It should require no arguments and return a dict-like object.
293
+
294
+ Typically, if your extension doesn't impact the data structure all
295
+ methods will inherited without issue except: `to_directed/to_undirected`.
296
+ By default these methods create a DiGraph/Graph class and you probably
297
+ want them to create your extension of a DiGraph/Graph. To facilitate
298
+ this we define two class variables that you can set in your subclass.
299
+
300
+ to_directed_class : callable, (default: DiGraph or MultiDiGraph)
301
+ Class to create a new graph structure in the `to_directed` method.
302
+ If `None`, a NetworkX class (DiGraph or MultiDiGraph) is used.
303
+
304
+ to_undirected_class : callable, (default: Graph or MultiGraph)
305
+ Class to create a new graph structure in the `to_undirected` method.
306
+ If `None`, a NetworkX class (Graph or MultiGraph) is used.
307
+
308
+ **Subclassing Example**
309
+
310
+ Create a low memory graph class that effectively disallows edge
311
+ attributes by using a single attribute dict for all edges.
312
+ This reduces the memory used, but you lose edge attributes.
313
+
314
+ >>> class ThinGraph(nx.Graph):
315
+ ... all_edge_dict = {"weight": 1}
316
+ ...
317
+ ... def single_edge_dict(self):
318
+ ... return self.all_edge_dict
319
+ ...
320
+ ... edge_attr_dict_factory = single_edge_dict
321
+ >>> G = ThinGraph()
322
+ >>> G.add_edge(2, 1)
323
+ >>> G[2][1]
324
+ {'weight': 1}
325
+ >>> G.add_edge(2, 2)
326
+ >>> G[2][1] is G[2][2]
327
+ True
328
+ """
329
+
330
+ _adj = _CachedPropertyResetterAdjAndSucc() # type: ignore[assignment]
331
+ _succ = _adj # type: ignore[has-type]
332
+ _pred = _CachedPropertyResetterPred()
333
+
334
+ def __init__(self, incoming_graph_data=None, **attr):
335
+ """Initialize a graph with edges, name, or graph attributes.
336
+
337
+ Parameters
338
+ ----------
339
+ incoming_graph_data : input graph (optional, default: None)
340
+ Data to initialize graph. If None (default) an empty
341
+ graph is created. The data can be an edge list, or any
342
+ NetworkX graph object. If the corresponding optional Python
343
+ packages are installed the data can also be a 2D NumPy array, a
344
+ SciPy sparse array, or a PyGraphviz graph.
345
+
346
+ attr : keyword arguments, optional (default= no attributes)
347
+ Attributes to add to graph as key=value pairs.
348
+
349
+ See Also
350
+ --------
351
+ convert
352
+
353
+ Examples
354
+ --------
355
+ >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
356
+ >>> G = nx.Graph(name="my graph")
357
+ >>> e = [(1, 2), (2, 3), (3, 4)] # list of edges
358
+ >>> G = nx.Graph(e)
359
+
360
+ Arbitrary graph attribute pairs (key=value) may be assigned
361
+
362
+ >>> G = nx.Graph(e, day="Friday")
363
+ >>> G.graph
364
+ {'day': 'Friday'}
365
+
366
+ """
367
+ self.graph = self.graph_attr_dict_factory() # dictionary for graph attributes
368
+ self._node = self.node_dict_factory() # dictionary for node attr
369
+ # We store two adjacency lists:
370
+ # the predecessors of node n are stored in the dict self._pred
371
+ # the successors of node n are stored in the dict self._succ=self._adj
372
+ self._adj = self.adjlist_outer_dict_factory() # empty adjacency dict successor
373
+ self._pred = self.adjlist_outer_dict_factory() # predecessor
374
+ # Note: self._succ = self._adj # successor
375
+
376
+ self.__networkx_cache__ = {}
377
+ # attempt to load graph with data
378
+ if incoming_graph_data is not None:
379
+ convert.to_networkx_graph(incoming_graph_data, create_using=self)
380
+ # load graph attributes (must be after convert)
381
+ self.graph.update(attr)
382
+
383
+ @cached_property
384
+ def adj(self):
385
+ """Graph adjacency object holding the neighbors of each node.
386
+
387
+ This object is a read-only dict-like structure with node keys
388
+ and neighbor-dict values. The neighbor-dict is keyed by neighbor
389
+ to the edge-data-dict. So `G.adj[3][2]['color'] = 'blue'` sets
390
+ the color of the edge `(3, 2)` to `"blue"`.
391
+
392
+ Iterating over G.adj behaves like a dict. Useful idioms include
393
+ `for nbr, datadict in G.adj[n].items():`.
394
+
395
+ The neighbor information is also provided by subscripting the graph.
396
+ So `for nbr, foovalue in G[node].data('foo', default=1):` works.
397
+
398
+ For directed graphs, `G.adj` holds outgoing (successor) info.
399
+ """
400
+ return AdjacencyView(self._succ)
401
+
402
+ @cached_property
403
+ def succ(self):
404
+ """Graph adjacency object holding the successors of each node.
405
+
406
+ This object is a read-only dict-like structure with node keys
407
+ and neighbor-dict values. The neighbor-dict is keyed by neighbor
408
+ to the edge-data-dict. So `G.succ[3][2]['color'] = 'blue'` sets
409
+ the color of the edge `(3, 2)` to `"blue"`.
410
+
411
+ Iterating over G.succ behaves like a dict. Useful idioms include
412
+ `for nbr, datadict in G.succ[n].items():`. A data-view not provided
413
+ by dicts also exists: `for nbr, foovalue in G.succ[node].data('foo'):`
414
+ and a default can be set via a `default` argument to the `data` method.
415
+
416
+ The neighbor information is also provided by subscripting the graph.
417
+ So `for nbr, foovalue in G[node].data('foo', default=1):` works.
418
+
419
+ For directed graphs, `G.adj` is identical to `G.succ`.
420
+ """
421
+ return AdjacencyView(self._succ)
422
+
423
+ @cached_property
424
+ def pred(self):
425
+ """Graph adjacency object holding the predecessors of each node.
426
+
427
+ This object is a read-only dict-like structure with node keys
428
+ and neighbor-dict values. The neighbor-dict is keyed by neighbor
429
+ to the edge-data-dict. So `G.pred[2][3]['color'] = 'blue'` sets
430
+ the color of the edge `(3, 2)` to `"blue"`.
431
+
432
+ Iterating over G.pred behaves like a dict. Useful idioms include
433
+ `for nbr, datadict in G.pred[n].items():`. A data-view not provided
434
+ by dicts also exists: `for nbr, foovalue in G.pred[node].data('foo'):`
435
+ A default can be set via a `default` argument to the `data` method.
436
+ """
437
+ return AdjacencyView(self._pred)
438
+
439
+ def add_node(self, node_for_adding, **attr):
440
+ """Add a single node `node_for_adding` and update node attributes.
441
+
442
+ Parameters
443
+ ----------
444
+ node_for_adding : node
445
+ A node can be any hashable Python object except None.
446
+ attr : keyword arguments, optional
447
+ Set or change node attributes using key=value.
448
+
449
+ See Also
450
+ --------
451
+ add_nodes_from
452
+
453
+ Examples
454
+ --------
455
+ >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
456
+ >>> G.add_node(1)
457
+ >>> G.add_node("Hello")
458
+ >>> K3 = nx.Graph([(0, 1), (1, 2), (2, 0)])
459
+ >>> G.add_node(K3)
460
+ >>> G.number_of_nodes()
461
+ 3
462
+
463
+ Use keywords set/change node attributes:
464
+
465
+ >>> G.add_node(1, size=10)
466
+ >>> G.add_node(3, weight=0.4, UTM=("13S", 382871, 3972649))
467
+
468
+ Notes
469
+ -----
470
+ A hashable object is one that can be used as a key in a Python
471
+ dictionary. This includes strings, numbers, tuples of strings
472
+ and numbers, etc.
473
+
474
+ On many platforms hashable items also include mutables such as
475
+ NetworkX Graphs, though one should be careful that the hash
476
+ doesn't change on mutables.
477
+ """
478
+ if node_for_adding not in self._succ:
479
+ if node_for_adding is None:
480
+ raise ValueError("None cannot be a node")
481
+ self._succ[node_for_adding] = self.adjlist_inner_dict_factory()
482
+ self._pred[node_for_adding] = self.adjlist_inner_dict_factory()
483
+ attr_dict = self._node[node_for_adding] = self.node_attr_dict_factory()
484
+ attr_dict.update(attr)
485
+ else: # update attr even if node already exists
486
+ self._node[node_for_adding].update(attr)
487
+ nx._clear_cache(self)
488
+
489
+ def add_nodes_from(self, nodes_for_adding, **attr):
490
+ """Add multiple nodes.
491
+
492
+ Parameters
493
+ ----------
494
+ nodes_for_adding : iterable container
495
+ A container of nodes (list, dict, set, etc.).
496
+ OR
497
+ A container of (node, attribute dict) tuples.
498
+ Node attributes are updated using the attribute dict.
499
+ attr : keyword arguments, optional (default= no attributes)
500
+ Update attributes for all nodes in nodes.
501
+ Node attributes specified in nodes as a tuple take
502
+ precedence over attributes specified via keyword arguments.
503
+
504
+ See Also
505
+ --------
506
+ add_node
507
+
508
+ Notes
509
+ -----
510
+ When adding nodes from an iterator over the graph you are changing,
511
+ a `RuntimeError` can be raised with message:
512
+ `RuntimeError: dictionary changed size during iteration`. This
513
+ happens when the graph's underlying dictionary is modified during
514
+ iteration. To avoid this error, evaluate the iterator into a separate
515
+ object, e.g. by using `list(iterator_of_nodes)`, and pass this
516
+ object to `G.add_nodes_from`.
517
+
518
+ Examples
519
+ --------
520
+ >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
521
+ >>> G.add_nodes_from("Hello")
522
+ >>> K3 = nx.Graph([(0, 1), (1, 2), (2, 0)])
523
+ >>> G.add_nodes_from(K3)
524
+ >>> sorted(G.nodes(), key=str)
525
+ [0, 1, 2, 'H', 'e', 'l', 'o']
526
+
527
+ Use keywords to update specific node attributes for every node.
528
+
529
+ >>> G.add_nodes_from([1, 2], size=10)
530
+ >>> G.add_nodes_from([3, 4], weight=0.4)
531
+
532
+ Use (node, attrdict) tuples to update attributes for specific nodes.
533
+
534
+ >>> G.add_nodes_from([(1, dict(size=11)), (2, {"color": "blue"})])
535
+ >>> G.nodes[1]["size"]
536
+ 11
537
+ >>> H = nx.Graph()
538
+ >>> H.add_nodes_from(G.nodes(data=True))
539
+ >>> H.nodes[1]["size"]
540
+ 11
541
+
542
+ Evaluate an iterator over a graph if using it to modify the same graph
543
+
544
+ >>> G = nx.DiGraph([(0, 1), (1, 2), (3, 4)])
545
+ >>> # wrong way - will raise RuntimeError
546
+ >>> # G.add_nodes_from(n + 1 for n in G.nodes)
547
+ >>> # correct way
548
+ >>> G.add_nodes_from(list(n + 1 for n in G.nodes))
549
+ """
550
+ for n in nodes_for_adding:
551
+ try:
552
+ newnode = n not in self._node
553
+ newdict = attr
554
+ except TypeError:
555
+ n, ndict = n
556
+ newnode = n not in self._node
557
+ newdict = attr.copy()
558
+ newdict.update(ndict)
559
+ if newnode:
560
+ if n is None:
561
+ raise ValueError("None cannot be a node")
562
+ self._succ[n] = self.adjlist_inner_dict_factory()
563
+ self._pred[n] = self.adjlist_inner_dict_factory()
564
+ self._node[n] = self.node_attr_dict_factory()
565
+ self._node[n].update(newdict)
566
+ nx._clear_cache(self)
567
+
568
+ def remove_node(self, n):
569
+ """Remove node n.
570
+
571
+ Removes the node n and all adjacent edges.
572
+ Attempting to remove a nonexistent node will raise an exception.
573
+
574
+ Parameters
575
+ ----------
576
+ n : node
577
+ A node in the graph
578
+
579
+ Raises
580
+ ------
581
+ NetworkXError
582
+ If n is not in the graph.
583
+
584
+ See Also
585
+ --------
586
+ remove_nodes_from
587
+
588
+ Examples
589
+ --------
590
+ >>> G = nx.path_graph(3) # or DiGraph, MultiGraph, MultiDiGraph, etc
591
+ >>> list(G.edges)
592
+ [(0, 1), (1, 2)]
593
+ >>> G.remove_node(1)
594
+ >>> list(G.edges)
595
+ []
596
+
597
+ """
598
+ try:
599
+ nbrs = self._succ[n]
600
+ del self._node[n]
601
+ except KeyError as err: # NetworkXError if n not in self
602
+ raise NetworkXError(f"The node {n} is not in the digraph.") from err
603
+ for u in nbrs:
604
+ del self._pred[u][n] # remove all edges n-u in digraph
605
+ del self._succ[n] # remove node from succ
606
+ for u in self._pred[n]:
607
+ del self._succ[u][n] # remove all edges n-u in digraph
608
+ del self._pred[n] # remove node from pred
609
+ nx._clear_cache(self)
610
+
611
+ def remove_nodes_from(self, nodes):
612
+ """Remove multiple nodes.
613
+
614
+ Parameters
615
+ ----------
616
+ nodes : iterable container
617
+ A container of nodes (list, dict, set, etc.). If a node
618
+ in the container is not in the graph it is silently ignored.
619
+
620
+ See Also
621
+ --------
622
+ remove_node
623
+
624
+ Notes
625
+ -----
626
+ When removing nodes from an iterator over the graph you are changing,
627
+ a `RuntimeError` will be raised with message:
628
+ `RuntimeError: dictionary changed size during iteration`. This
629
+ happens when the graph's underlying dictionary is modified during
630
+ iteration. To avoid this error, evaluate the iterator into a separate
631
+ object, e.g. by using `list(iterator_of_nodes)`, and pass this
632
+ object to `G.remove_nodes_from`.
633
+
634
+ Examples
635
+ --------
636
+ >>> G = nx.path_graph(3) # or DiGraph, MultiGraph, MultiDiGraph, etc
637
+ >>> e = list(G.nodes)
638
+ >>> e
639
+ [0, 1, 2]
640
+ >>> G.remove_nodes_from(e)
641
+ >>> list(G.nodes)
642
+ []
643
+
644
+ Evaluate an iterator over a graph if using it to modify the same graph
645
+
646
+ >>> G = nx.DiGraph([(0, 1), (1, 2), (3, 4)])
647
+ >>> # this command will fail, as the graph's dict is modified during iteration
648
+ >>> # G.remove_nodes_from(n for n in G.nodes if n < 2)
649
+ >>> # this command will work, since the dictionary underlying graph is not modified
650
+ >>> G.remove_nodes_from(list(n for n in G.nodes if n < 2))
651
+ """
652
+ for n in nodes:
653
+ try:
654
+ succs = self._succ[n]
655
+ del self._node[n]
656
+ for u in succs:
657
+ del self._pred[u][n] # remove all edges n-u in digraph
658
+ del self._succ[n] # now remove node
659
+ for u in self._pred[n]:
660
+ del self._succ[u][n] # remove all edges n-u in digraph
661
+ del self._pred[n] # now remove node
662
+ except KeyError:
663
+ pass # silent failure on remove
664
+ nx._clear_cache(self)
665
+
666
+ def add_edge(self, u_of_edge, v_of_edge, **attr):
667
+ """Add an edge between u and v.
668
+
669
+ The nodes u and v will be automatically added if they are
670
+ not already in the graph.
671
+
672
+ Edge attributes can be specified with keywords or by directly
673
+ accessing the edge's attribute dictionary. See examples below.
674
+
675
+ Parameters
676
+ ----------
677
+ u_of_edge, v_of_edge : nodes
678
+ Nodes can be, for example, strings or numbers.
679
+ Nodes must be hashable (and not None) Python objects.
680
+ attr : keyword arguments, optional
681
+ Edge data (or labels or objects) can be assigned using
682
+ keyword arguments.
683
+
684
+ See Also
685
+ --------
686
+ add_edges_from : add a collection of edges
687
+
688
+ Notes
689
+ -----
690
+ Adding an edge that already exists updates the edge data.
691
+
692
+ Many NetworkX algorithms designed for weighted graphs use
693
+ an edge attribute (by default `weight`) to hold a numerical value.
694
+
695
+ Examples
696
+ --------
697
+ The following all add the edge e=(1, 2) to graph G:
698
+
699
+ >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
700
+ >>> e = (1, 2)
701
+ >>> G.add_edge(1, 2) # explicit two-node form
702
+ >>> G.add_edge(*e) # single edge as tuple of two nodes
703
+ >>> G.add_edges_from([(1, 2)]) # add edges from iterable container
704
+
705
+ Associate data to edges using keywords:
706
+
707
+ >>> G.add_edge(1, 2, weight=3)
708
+ >>> G.add_edge(1, 3, weight=7, capacity=15, length=342.7)
709
+
710
+ For non-string attribute keys, use subscript notation.
711
+
712
+ >>> G.add_edge(1, 2)
713
+ >>> G[1][2].update({0: 5})
714
+ >>> G.edges[1, 2].update({0: 5})
715
+ """
716
+ u, v = u_of_edge, v_of_edge
717
+ # add nodes
718
+ if u not in self._succ:
719
+ if u is None:
720
+ raise ValueError("None cannot be a node")
721
+ self._succ[u] = self.adjlist_inner_dict_factory()
722
+ self._pred[u] = self.adjlist_inner_dict_factory()
723
+ self._node[u] = self.node_attr_dict_factory()
724
+ if v not in self._succ:
725
+ if v is None:
726
+ raise ValueError("None cannot be a node")
727
+ self._succ[v] = self.adjlist_inner_dict_factory()
728
+ self._pred[v] = self.adjlist_inner_dict_factory()
729
+ self._node[v] = self.node_attr_dict_factory()
730
+ # add the edge
731
+ datadict = self._adj[u].get(v, self.edge_attr_dict_factory())
732
+ datadict.update(attr)
733
+ self._succ[u][v] = datadict
734
+ self._pred[v][u] = datadict
735
+ nx._clear_cache(self)
736
+
737
+ def add_edges_from(self, ebunch_to_add, **attr):
738
+ """Add all the edges in ebunch_to_add.
739
+
740
+ Parameters
741
+ ----------
742
+ ebunch_to_add : container of edges
743
+ Each edge given in the container will be added to the
744
+ graph. The edges must be given as 2-tuples (u, v) or
745
+ 3-tuples (u, v, d) where d is a dictionary containing edge data.
746
+ attr : keyword arguments, optional
747
+ Edge data (or labels or objects) can be assigned using
748
+ keyword arguments.
749
+
750
+ See Also
751
+ --------
752
+ add_edge : add a single edge
753
+ add_weighted_edges_from : convenient way to add weighted edges
754
+
755
+ Notes
756
+ -----
757
+ Adding the same edge twice has no effect but any edge data
758
+ will be updated when each duplicate edge is added.
759
+
760
+ Edge attributes specified in an ebunch take precedence over
761
+ attributes specified via keyword arguments.
762
+
763
+ When adding edges from an iterator over the graph you are changing,
764
+ a `RuntimeError` can be raised with message:
765
+ `RuntimeError: dictionary changed size during iteration`. This
766
+ happens when the graph's underlying dictionary is modified during
767
+ iteration. To avoid this error, evaluate the iterator into a separate
768
+ object, e.g. by using `list(iterator_of_edges)`, and pass this
769
+ object to `G.add_edges_from`.
770
+
771
+ Examples
772
+ --------
773
+ >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
774
+ >>> G.add_edges_from([(0, 1), (1, 2)]) # using a list of edge tuples
775
+ >>> e = zip(range(0, 3), range(1, 4))
776
+ >>> G.add_edges_from(e) # Add the path graph 0-1-2-3
777
+
778
+ Associate data to edges
779
+
780
+ >>> G.add_edges_from([(1, 2), (2, 3)], weight=3)
781
+ >>> G.add_edges_from([(3, 4), (1, 4)], label="WN2898")
782
+
783
+ Evaluate an iterator over a graph if using it to modify the same graph
784
+
785
+ >>> G = nx.DiGraph([(1, 2), (2, 3), (3, 4)])
786
+ >>> # Grow graph by one new node, adding edges to all existing nodes.
787
+ >>> # wrong way - will raise RuntimeError
788
+ >>> # G.add_edges_from(((5, n) for n in G.nodes))
789
+ >>> # right way - note that there will be no self-edge for node 5
790
+ >>> G.add_edges_from(list((5, n) for n in G.nodes))
791
+ """
792
+ for e in ebunch_to_add:
793
+ ne = len(e)
794
+ if ne == 3:
795
+ u, v, dd = e
796
+ elif ne == 2:
797
+ u, v = e
798
+ dd = {}
799
+ else:
800
+ raise NetworkXError(f"Edge tuple {e} must be a 2-tuple or 3-tuple.")
801
+ if u not in self._succ:
802
+ if u is None:
803
+ raise ValueError("None cannot be a node")
804
+ self._succ[u] = self.adjlist_inner_dict_factory()
805
+ self._pred[u] = self.adjlist_inner_dict_factory()
806
+ self._node[u] = self.node_attr_dict_factory()
807
+ if v not in self._succ:
808
+ if v is None:
809
+ raise ValueError("None cannot be a node")
810
+ self._succ[v] = self.adjlist_inner_dict_factory()
811
+ self._pred[v] = self.adjlist_inner_dict_factory()
812
+ self._node[v] = self.node_attr_dict_factory()
813
+ datadict = self._adj[u].get(v, self.edge_attr_dict_factory())
814
+ datadict.update(attr)
815
+ datadict.update(dd)
816
+ self._succ[u][v] = datadict
817
+ self._pred[v][u] = datadict
818
+ nx._clear_cache(self)
819
+
820
+ def remove_edge(self, u, v):
821
+ """Remove the edge between u and v.
822
+
823
+ Parameters
824
+ ----------
825
+ u, v : nodes
826
+ Remove the edge between nodes u and v.
827
+
828
+ Raises
829
+ ------
830
+ NetworkXError
831
+ If there is not an edge between u and v.
832
+
833
+ See Also
834
+ --------
835
+ remove_edges_from : remove a collection of edges
836
+
837
+ Examples
838
+ --------
839
+ >>> G = nx.Graph() # or DiGraph, etc
840
+ >>> nx.add_path(G, [0, 1, 2, 3])
841
+ >>> G.remove_edge(0, 1)
842
+ >>> e = (1, 2)
843
+ >>> G.remove_edge(*e) # unpacks e from an edge tuple
844
+ >>> e = (2, 3, {"weight": 7}) # an edge with attribute data
845
+ >>> G.remove_edge(*e[:2]) # select first part of edge tuple
846
+ """
847
+ try:
848
+ del self._succ[u][v]
849
+ del self._pred[v][u]
850
+ except KeyError as err:
851
+ raise NetworkXError(f"The edge {u}-{v} not in graph.") from err
852
+ nx._clear_cache(self)
853
+
854
+ def remove_edges_from(self, ebunch):
855
+ """Remove all edges specified in ebunch.
856
+
857
+ Parameters
858
+ ----------
859
+ ebunch: list or container of edge tuples
860
+ Each edge given in the list or container will be removed
861
+ from the graph. The edges can be:
862
+
863
+ - 2-tuples (u, v) edge between u and v.
864
+ - 3-tuples (u, v, k) where k is ignored.
865
+
866
+ See Also
867
+ --------
868
+ remove_edge : remove a single edge
869
+
870
+ Notes
871
+ -----
872
+ Will fail silently if an edge in ebunch is not in the graph.
873
+
874
+ Examples
875
+ --------
876
+ >>> G = nx.path_graph(4) # or DiGraph, MultiGraph, MultiDiGraph, etc
877
+ >>> ebunch = [(1, 2), (2, 3)]
878
+ >>> G.remove_edges_from(ebunch)
879
+ """
880
+ for e in ebunch:
881
+ u, v = e[:2] # ignore edge data
882
+ if u in self._succ and v in self._succ[u]:
883
+ del self._succ[u][v]
884
+ del self._pred[v][u]
885
+ nx._clear_cache(self)
886
+
887
+ def has_successor(self, u, v):
888
+ """Returns True if node u has successor v.
889
+
890
+ This is true if graph has the edge u->v.
891
+ """
892
+ return u in self._succ and v in self._succ[u]
893
+
894
+ def has_predecessor(self, u, v):
895
+ """Returns True if node u has predecessor v.
896
+
897
+ This is true if graph has the edge u<-v.
898
+ """
899
+ return u in self._pred and v in self._pred[u]
900
+
901
+ def successors(self, n):
902
+ """Returns an iterator over successor nodes of n.
903
+
904
+ A successor of n is a node m such that there exists a directed
905
+ edge from n to m.
906
+
907
+ Parameters
908
+ ----------
909
+ n : node
910
+ A node in the graph
911
+
912
+ Raises
913
+ ------
914
+ NetworkXError
915
+ If n is not in the graph.
916
+
917
+ See Also
918
+ --------
919
+ predecessors
920
+
921
+ Notes
922
+ -----
923
+ neighbors() and successors() are the same.
924
+ """
925
+ try:
926
+ return iter(self._succ[n])
927
+ except KeyError as err:
928
+ raise NetworkXError(f"The node {n} is not in the digraph.") from err
929
+
930
+ # digraph definitions
931
+ neighbors = successors
932
+
933
+ def predecessors(self, n):
934
+ """Returns an iterator over predecessor nodes of n.
935
+
936
+ A predecessor of n is a node m such that there exists a directed
937
+ edge from m to n.
938
+
939
+ Parameters
940
+ ----------
941
+ n : node
942
+ A node in the graph
943
+
944
+ Raises
945
+ ------
946
+ NetworkXError
947
+ If n is not in the graph.
948
+
949
+ See Also
950
+ --------
951
+ successors
952
+ """
953
+ try:
954
+ return iter(self._pred[n])
955
+ except KeyError as err:
956
+ raise NetworkXError(f"The node {n} is not in the digraph.") from err
957
+
958
+ @cached_property
959
+ def edges(self):
960
+ """An OutEdgeView of the DiGraph as G.edges or G.edges().
961
+
962
+ edges(self, nbunch=None, data=False, default=None)
963
+
964
+ The OutEdgeView provides set-like operations on the edge-tuples
965
+ as well as edge attribute lookup. When called, it also provides
966
+ an EdgeDataView object which allows control of access to edge
967
+ attributes (but does not provide set-like operations).
968
+ Hence, `G.edges[u, v]['color']` provides the value of the color
969
+ attribute for edge `(u, v)` while
970
+ `for (u, v, c) in G.edges.data('color', default='red'):`
971
+ iterates through all the edges yielding the color attribute
972
+ with default `'red'` if no color attribute exists.
973
+
974
+ Parameters
975
+ ----------
976
+ nbunch : single node, container, or all nodes (default= all nodes)
977
+ The view will only report edges from these nodes.
978
+ data : string or bool, optional (default=False)
979
+ The edge attribute returned in 3-tuple (u, v, ddict[data]).
980
+ If True, return edge attribute dict in 3-tuple (u, v, ddict).
981
+ If False, return 2-tuple (u, v).
982
+ default : value, optional (default=None)
983
+ Value used for edges that don't have the requested attribute.
984
+ Only relevant if data is not True or False.
985
+
986
+ Returns
987
+ -------
988
+ edges : OutEdgeView
989
+ A view of edge attributes, usually it iterates over (u, v)
990
+ or (u, v, d) tuples of edges, but can also be used for
991
+ attribute lookup as `edges[u, v]['foo']`.
992
+
993
+ See Also
994
+ --------
995
+ in_edges, out_edges
996
+
997
+ Notes
998
+ -----
999
+ Nodes in nbunch that are not in the graph will be (quietly) ignored.
1000
+ For directed graphs this returns the out-edges.
1001
+
1002
+ Examples
1003
+ --------
1004
+ >>> G = nx.DiGraph() # or MultiDiGraph, etc
1005
+ >>> nx.add_path(G, [0, 1, 2])
1006
+ >>> G.add_edge(2, 3, weight=5)
1007
+ >>> [e for e in G.edges]
1008
+ [(0, 1), (1, 2), (2, 3)]
1009
+ >>> G.edges.data() # default data is {} (empty dict)
1010
+ OutEdgeDataView([(0, 1, {}), (1, 2, {}), (2, 3, {'weight': 5})])
1011
+ >>> G.edges.data("weight", default=1)
1012
+ OutEdgeDataView([(0, 1, 1), (1, 2, 1), (2, 3, 5)])
1013
+ >>> G.edges([0, 2]) # only edges originating from these nodes
1014
+ OutEdgeDataView([(0, 1), (2, 3)])
1015
+ >>> G.edges(0) # only edges from node 0
1016
+ OutEdgeDataView([(0, 1)])
1017
+
1018
+ """
1019
+ return OutEdgeView(self)
1020
+
1021
+ # alias out_edges to edges
1022
+ @cached_property
1023
+ def out_edges(self):
1024
+ return OutEdgeView(self)
1025
+
1026
+ out_edges.__doc__ = edges.__doc__
1027
+
1028
+ @cached_property
1029
+ def in_edges(self):
1030
+ """A view of the in edges of the graph as G.in_edges or G.in_edges().
1031
+
1032
+ in_edges(self, nbunch=None, data=False, default=None):
1033
+
1034
+ Parameters
1035
+ ----------
1036
+ nbunch : single node, container, or all nodes (default= all nodes)
1037
+ The view will only report edges incident to these nodes.
1038
+ data : string or bool, optional (default=False)
1039
+ The edge attribute returned in 3-tuple (u, v, ddict[data]).
1040
+ If True, return edge attribute dict in 3-tuple (u, v, ddict).
1041
+ If False, return 2-tuple (u, v).
1042
+ default : value, optional (default=None)
1043
+ Value used for edges that don't have the requested attribute.
1044
+ Only relevant if data is not True or False.
1045
+
1046
+ Returns
1047
+ -------
1048
+ in_edges : InEdgeView or InEdgeDataView
1049
+ A view of edge attributes, usually it iterates over (u, v)
1050
+ or (u, v, d) tuples of edges, but can also be used for
1051
+ attribute lookup as `edges[u, v]['foo']`.
1052
+
1053
+ Examples
1054
+ --------
1055
+ >>> G = nx.DiGraph()
1056
+ >>> G.add_edge(1, 2, color="blue")
1057
+ >>> G.in_edges()
1058
+ InEdgeView([(1, 2)])
1059
+ >>> G.in_edges(nbunch=2)
1060
+ InEdgeDataView([(1, 2)])
1061
+
1062
+ See Also
1063
+ --------
1064
+ edges
1065
+ """
1066
+ return InEdgeView(self)
1067
+
1068
+ @cached_property
1069
+ def degree(self):
1070
+ """A DegreeView for the Graph as G.degree or G.degree().
1071
+
1072
+ The node degree is the number of edges adjacent to the node.
1073
+ The weighted node degree is the sum of the edge weights for
1074
+ edges incident to that node.
1075
+
1076
+ This object provides an iterator for (node, degree) as well as
1077
+ lookup for the degree for a single node.
1078
+
1079
+ Parameters
1080
+ ----------
1081
+ nbunch : single node, container, or all nodes (default= all nodes)
1082
+ The view will only report edges incident to these nodes.
1083
+
1084
+ weight : string or None, optional (default=None)
1085
+ The name of an edge attribute that holds the numerical value used
1086
+ as a weight. If None, then each edge has weight 1.
1087
+ The degree is the sum of the edge weights adjacent to the node.
1088
+
1089
+ Returns
1090
+ -------
1091
+ DiDegreeView or int
1092
+ If multiple nodes are requested (the default), returns a `DiDegreeView`
1093
+ mapping nodes to their degree.
1094
+ If a single node is requested, returns the degree of the node as an integer.
1095
+
1096
+ See Also
1097
+ --------
1098
+ in_degree, out_degree
1099
+
1100
+ Examples
1101
+ --------
1102
+ >>> G = nx.DiGraph() # or MultiDiGraph
1103
+ >>> nx.add_path(G, [0, 1, 2, 3])
1104
+ >>> G.degree(0) # node 0 with degree 1
1105
+ 1
1106
+ >>> list(G.degree([0, 1, 2]))
1107
+ [(0, 1), (1, 2), (2, 2)]
1108
+
1109
+ """
1110
+ return DiDegreeView(self)
1111
+
1112
+ @cached_property
1113
+ def in_degree(self):
1114
+ """An InDegreeView for (node, in_degree) or in_degree for single node.
1115
+
1116
+ The node in_degree is the number of edges pointing to the node.
1117
+ The weighted node degree is the sum of the edge weights for
1118
+ edges incident to that node.
1119
+
1120
+ This object provides an iteration over (node, in_degree) as well as
1121
+ lookup for the degree for a single node.
1122
+
1123
+ Parameters
1124
+ ----------
1125
+ nbunch : single node, container, or all nodes (default= all nodes)
1126
+ The view will only report edges incident to these nodes.
1127
+
1128
+ weight : string or None, optional (default=None)
1129
+ The name of an edge attribute that holds the numerical value used
1130
+ as a weight. If None, then each edge has weight 1.
1131
+ The degree is the sum of the edge weights adjacent to the node.
1132
+
1133
+ Returns
1134
+ -------
1135
+ If a single node is requested
1136
+ deg : int
1137
+ In-degree of the node
1138
+
1139
+ OR if multiple nodes are requested
1140
+ nd_iter : iterator
1141
+ The iterator returns two-tuples of (node, in-degree).
1142
+
1143
+ See Also
1144
+ --------
1145
+ degree, out_degree
1146
+
1147
+ Examples
1148
+ --------
1149
+ >>> G = nx.DiGraph()
1150
+ >>> nx.add_path(G, [0, 1, 2, 3])
1151
+ >>> G.in_degree(0) # node 0 with degree 0
1152
+ 0
1153
+ >>> list(G.in_degree([0, 1, 2]))
1154
+ [(0, 0), (1, 1), (2, 1)]
1155
+
1156
+ """
1157
+ return InDegreeView(self)
1158
+
1159
+ @cached_property
1160
+ def out_degree(self):
1161
+ """An OutDegreeView for (node, out_degree)
1162
+
1163
+ The node out_degree is the number of edges pointing out of the node.
1164
+ The weighted node degree is the sum of the edge weights for
1165
+ edges incident to that node.
1166
+
1167
+ This object provides an iterator over (node, out_degree) as well as
1168
+ lookup for the degree for a single node.
1169
+
1170
+ Parameters
1171
+ ----------
1172
+ nbunch : single node, container, or all nodes (default= all nodes)
1173
+ The view will only report edges incident to these nodes.
1174
+
1175
+ weight : string or None, optional (default=None)
1176
+ The name of an edge attribute that holds the numerical value used
1177
+ as a weight. If None, then each edge has weight 1.
1178
+ The degree is the sum of the edge weights adjacent to the node.
1179
+
1180
+ Returns
1181
+ -------
1182
+ If a single node is requested
1183
+ deg : int
1184
+ Out-degree of the node
1185
+
1186
+ OR if multiple nodes are requested
1187
+ nd_iter : iterator
1188
+ The iterator returns two-tuples of (node, out-degree).
1189
+
1190
+ See Also
1191
+ --------
1192
+ degree, in_degree
1193
+
1194
+ Examples
1195
+ --------
1196
+ >>> G = nx.DiGraph()
1197
+ >>> nx.add_path(G, [0, 1, 2, 3])
1198
+ >>> G.out_degree(0) # node 0 with degree 1
1199
+ 1
1200
+ >>> list(G.out_degree([0, 1, 2]))
1201
+ [(0, 1), (1, 1), (2, 1)]
1202
+
1203
+ """
1204
+ return OutDegreeView(self)
1205
+
1206
+ def clear(self):
1207
+ """Remove all nodes and edges from the graph.
1208
+
1209
+ This also removes the name, and all graph, node, and edge attributes.
1210
+
1211
+ Examples
1212
+ --------
1213
+ >>> G = nx.path_graph(4) # or DiGraph, MultiGraph, MultiDiGraph, etc
1214
+ >>> G.clear()
1215
+ >>> list(G.nodes)
1216
+ []
1217
+ >>> list(G.edges)
1218
+ []
1219
+
1220
+ """
1221
+ self._succ.clear()
1222
+ self._pred.clear()
1223
+ self._node.clear()
1224
+ self.graph.clear()
1225
+ nx._clear_cache(self)
1226
+
1227
+ def clear_edges(self):
1228
+ """Remove all edges from the graph without altering nodes.
1229
+
1230
+ Examples
1231
+ --------
1232
+ >>> G = nx.path_graph(4) # or DiGraph, MultiGraph, MultiDiGraph, etc
1233
+ >>> G.clear_edges()
1234
+ >>> list(G.nodes)
1235
+ [0, 1, 2, 3]
1236
+ >>> list(G.edges)
1237
+ []
1238
+
1239
+ """
1240
+ for predecessor_dict in self._pred.values():
1241
+ predecessor_dict.clear()
1242
+ for successor_dict in self._succ.values():
1243
+ successor_dict.clear()
1244
+ nx._clear_cache(self)
1245
+
1246
+ def is_multigraph(self):
1247
+ """Returns True if graph is a multigraph, False otherwise."""
1248
+ return False
1249
+
1250
+ def is_directed(self):
1251
+ """Returns True if graph is directed, False otherwise."""
1252
+ return True
1253
+
1254
+ def to_undirected(self, reciprocal=False, as_view=False):
1255
+ """Returns an undirected representation of the digraph.
1256
+
1257
+ Parameters
1258
+ ----------
1259
+ reciprocal : bool (optional)
1260
+ If True only keep edges that appear in both directions
1261
+ in the original digraph.
1262
+ as_view : bool (optional, default=False)
1263
+ If True return an undirected view of the original directed graph.
1264
+
1265
+ Returns
1266
+ -------
1267
+ G : Graph
1268
+ An undirected graph with the same name and nodes and
1269
+ with edge (u, v, data) if either (u, v, data) or (v, u, data)
1270
+ is in the digraph. If both edges exist in digraph and
1271
+ their edge data is different, only one edge is created
1272
+ with an arbitrary choice of which edge data to use.
1273
+ You must check and correct for this manually if desired.
1274
+
1275
+ See Also
1276
+ --------
1277
+ Graph, copy, add_edge, add_edges_from
1278
+
1279
+ Notes
1280
+ -----
1281
+ If edges in both directions (u, v) and (v, u) exist in the
1282
+ graph, attributes for the new undirected edge will be a combination of
1283
+ the attributes of the directed edges. The edge data is updated
1284
+ in the (arbitrary) order that the edges are encountered. For
1285
+ more customized control of the edge attributes use add_edge().
1286
+
1287
+ This returns a "deepcopy" of the edge, node, and
1288
+ graph attributes which attempts to completely copy
1289
+ all of the data and references.
1290
+
1291
+ This is in contrast to the similar G=DiGraph(D) which returns a
1292
+ shallow copy of the data.
1293
+
1294
+ See the Python copy module for more information on shallow
1295
+ and deep copies, https://docs.python.org/3/library/copy.html.
1296
+
1297
+ Warning: If you have subclassed DiGraph to use dict-like objects
1298
+ in the data structure, those changes do not transfer to the
1299
+ Graph created by this method.
1300
+
1301
+ Examples
1302
+ --------
1303
+ >>> G = nx.path_graph(2) # or MultiGraph, etc
1304
+ >>> H = G.to_directed()
1305
+ >>> list(H.edges)
1306
+ [(0, 1), (1, 0)]
1307
+ >>> G2 = H.to_undirected()
1308
+ >>> list(G2.edges)
1309
+ [(0, 1)]
1310
+ """
1311
+ graph_class = self.to_undirected_class()
1312
+ if as_view is True:
1313
+ return nx.graphviews.generic_graph_view(self, graph_class)
1314
+ # deepcopy when not a view
1315
+ G = graph_class()
1316
+ G.graph.update(deepcopy(self.graph))
1317
+ G.add_nodes_from((n, deepcopy(d)) for n, d in self._node.items())
1318
+ if reciprocal is True:
1319
+ G.add_edges_from(
1320
+ (u, v, deepcopy(d))
1321
+ for u, nbrs in self._adj.items()
1322
+ for v, d in nbrs.items()
1323
+ if v in self._pred[u]
1324
+ )
1325
+ else:
1326
+ G.add_edges_from(
1327
+ (u, v, deepcopy(d))
1328
+ for u, nbrs in self._adj.items()
1329
+ for v, d in nbrs.items()
1330
+ )
1331
+ return G
1332
+
1333
+ def reverse(self, copy=True):
1334
+ """Returns the reverse of the graph.
1335
+
1336
+ The reverse is a graph with the same nodes and edges
1337
+ but with the directions of the edges reversed.
1338
+
1339
+ Parameters
1340
+ ----------
1341
+ copy : bool optional (default=True)
1342
+ If True, return a new DiGraph holding the reversed edges.
1343
+ If False, the reverse graph is created using a view of
1344
+ the original graph.
1345
+ """
1346
+ if copy:
1347
+ H = self.__class__()
1348
+ H.graph.update(deepcopy(self.graph))
1349
+ H.add_nodes_from((n, deepcopy(d)) for n, d in self.nodes.items())
1350
+ H.add_edges_from((v, u, deepcopy(d)) for u, v, d in self.edges(data=True))
1351
+ return H
1352
+ return nx.reverse_view(self)
minigpt2/lib/python3.10/site-packages/networkx/classes/multigraph.py ADDED
@@ -0,0 +1,1283 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Base class for MultiGraph."""
2
+
3
+ from copy import deepcopy
4
+ from functools import cached_property
5
+
6
+ import networkx as nx
7
+ from networkx import NetworkXError, convert
8
+ from networkx.classes.coreviews import MultiAdjacencyView
9
+ from networkx.classes.graph import Graph
10
+ from networkx.classes.reportviews import MultiDegreeView, MultiEdgeView
11
+
12
+ __all__ = ["MultiGraph"]
13
+
14
+
15
+ class MultiGraph(Graph):
16
+ """
17
+ An undirected graph class that can store multiedges.
18
+
19
+ Multiedges are multiple edges between two nodes. Each edge
20
+ can hold optional data or attributes.
21
+
22
+ A MultiGraph holds undirected edges. Self loops are allowed.
23
+
24
+ Nodes can be arbitrary (hashable) Python objects with optional
25
+ key/value attributes. By convention `None` is not used as a node.
26
+
27
+ Edges are represented as links between nodes with optional
28
+ key/value attributes, in a MultiGraph each edge has a key to
29
+ distinguish between multiple edges that have the same source and
30
+ destination nodes.
31
+
32
+ Parameters
33
+ ----------
34
+ incoming_graph_data : input graph (optional, default: None)
35
+ Data to initialize graph. If None (default) an empty
36
+ graph is created. The data can be any format that is supported
37
+ by the to_networkx_graph() function, currently including edge list,
38
+ dict of dicts, dict of lists, NetworkX graph, 2D NumPy array,
39
+ SciPy sparse array, or PyGraphviz graph.
40
+
41
+ multigraph_input : bool or None (default None)
42
+ Note: Only used when `incoming_graph_data` is a dict.
43
+ If True, `incoming_graph_data` is assumed to be a
44
+ dict-of-dict-of-dict-of-dict structure keyed by
45
+ node to neighbor to edge keys to edge data for multi-edges.
46
+ A NetworkXError is raised if this is not the case.
47
+ If False, :func:`to_networkx_graph` is used to try to determine
48
+ the dict's graph data structure as either a dict-of-dict-of-dict
49
+ keyed by node to neighbor to edge data, or a dict-of-iterable
50
+ keyed by node to neighbors.
51
+ If None, the treatment for True is tried, but if it fails,
52
+ the treatment for False is tried.
53
+
54
+ attr : keyword arguments, optional (default= no attributes)
55
+ Attributes to add to graph as key=value pairs.
56
+
57
+ See Also
58
+ --------
59
+ Graph
60
+ DiGraph
61
+ MultiDiGraph
62
+
63
+ Examples
64
+ --------
65
+ Create an empty graph structure (a "null graph") with no nodes and
66
+ no edges.
67
+
68
+ >>> G = nx.MultiGraph()
69
+
70
+ G can be grown in several ways.
71
+
72
+ **Nodes:**
73
+
74
+ Add one node at a time:
75
+
76
+ >>> G.add_node(1)
77
+
78
+ Add the nodes from any container (a list, dict, set or
79
+ even the lines from a file or the nodes from another graph).
80
+
81
+ >>> G.add_nodes_from([2, 3])
82
+ >>> G.add_nodes_from(range(100, 110))
83
+ >>> H = nx.path_graph(10)
84
+ >>> G.add_nodes_from(H)
85
+
86
+ In addition to strings and integers any hashable Python object
87
+ (except None) can represent a node, e.g. a customized node object,
88
+ or even another Graph.
89
+
90
+ >>> G.add_node(H)
91
+
92
+ **Edges:**
93
+
94
+ G can also be grown by adding edges.
95
+
96
+ Add one edge,
97
+
98
+ >>> key = G.add_edge(1, 2)
99
+
100
+ a list of edges,
101
+
102
+ >>> keys = G.add_edges_from([(1, 2), (1, 3)])
103
+
104
+ or a collection of edges,
105
+
106
+ >>> keys = G.add_edges_from(H.edges)
107
+
108
+ If some edges connect nodes not yet in the graph, the nodes
109
+ are added automatically. If an edge already exists, an additional
110
+ edge is created and stored using a key to identify the edge.
111
+ By default the key is the lowest unused integer.
112
+
113
+ >>> keys = G.add_edges_from([(4, 5, {"route": 28}), (4, 5, {"route": 37})])
114
+ >>> G[4]
115
+ AdjacencyView({3: {0: {}}, 5: {0: {}, 1: {'route': 28}, 2: {'route': 37}}})
116
+
117
+ **Attributes:**
118
+
119
+ Each graph, node, and edge can hold key/value attribute pairs
120
+ in an associated attribute dictionary (the keys must be hashable).
121
+ By default these are empty, but can be added or changed using
122
+ add_edge, add_node or direct manipulation of the attribute
123
+ dictionaries named graph, node and edge respectively.
124
+
125
+ >>> G = nx.MultiGraph(day="Friday")
126
+ >>> G.graph
127
+ {'day': 'Friday'}
128
+
129
+ Add node attributes using add_node(), add_nodes_from() or G.nodes
130
+
131
+ >>> G.add_node(1, time="5pm")
132
+ >>> G.add_nodes_from([3], time="2pm")
133
+ >>> G.nodes[1]
134
+ {'time': '5pm'}
135
+ >>> G.nodes[1]["room"] = 714
136
+ >>> del G.nodes[1]["room"] # remove attribute
137
+ >>> list(G.nodes(data=True))
138
+ [(1, {'time': '5pm'}), (3, {'time': '2pm'})]
139
+
140
+ Add edge attributes using add_edge(), add_edges_from(), subscript
141
+ notation, or G.edges.
142
+
143
+ >>> key = G.add_edge(1, 2, weight=4.7)
144
+ >>> keys = G.add_edges_from([(3, 4), (4, 5)], color="red")
145
+ >>> keys = G.add_edges_from([(1, 2, {"color": "blue"}), (2, 3, {"weight": 8})])
146
+ >>> G[1][2][0]["weight"] = 4.7
147
+ >>> G.edges[1, 2, 0]["weight"] = 4
148
+
149
+ Warning: we protect the graph data structure by making `G.edges[1,
150
+ 2, 0]` a read-only dict-like structure. However, you can assign to
151
+ attributes in e.g. `G.edges[1, 2, 0]`. Thus, use 2 sets of brackets
152
+ to add/change data attributes: `G.edges[1, 2, 0]['weight'] = 4`.
153
+
154
+ **Shortcuts:**
155
+
156
+ Many common graph features allow python syntax to speed reporting.
157
+
158
+ >>> 1 in G # check if node in graph
159
+ True
160
+ >>> [n for n in G if n < 3] # iterate through nodes
161
+ [1, 2]
162
+ >>> len(G) # number of nodes in graph
163
+ 5
164
+ >>> G[1] # adjacency dict-like view mapping neighbor -> edge key -> edge attributes
165
+ AdjacencyView({2: {0: {'weight': 4}, 1: {'color': 'blue'}}})
166
+
167
+ Often the best way to traverse all edges of a graph is via the neighbors.
168
+ The neighbors are reported as an adjacency-dict `G.adj` or `G.adjacency()`.
169
+
170
+ >>> for n, nbrsdict in G.adjacency():
171
+ ... for nbr, keydict in nbrsdict.items():
172
+ ... for key, eattr in keydict.items():
173
+ ... if "weight" in eattr:
174
+ ... # Do something useful with the edges
175
+ ... pass
176
+
177
+ But the edges() method is often more convenient:
178
+
179
+ >>> for u, v, keys, weight in G.edges(data="weight", keys=True):
180
+ ... if weight is not None:
181
+ ... # Do something useful with the edges
182
+ ... pass
183
+
184
+ **Reporting:**
185
+
186
+ Simple graph information is obtained using methods and object-attributes.
187
+ Reporting usually provides views instead of containers to reduce memory
188
+ usage. The views update as the graph is updated similarly to dict-views.
189
+ The objects `nodes`, `edges` and `adj` provide access to data attributes
190
+ via lookup (e.g. `nodes[n]`, `edges[u, v, k]`, `adj[u][v]`) and iteration
191
+ (e.g. `nodes.items()`, `nodes.data('color')`,
192
+ `nodes.data('color', default='blue')` and similarly for `edges`)
193
+ Views exist for `nodes`, `edges`, `neighbors()`/`adj` and `degree`.
194
+
195
+ For details on these and other miscellaneous methods, see below.
196
+
197
+ **Subclasses (Advanced):**
198
+
199
+ The MultiGraph class uses a dict-of-dict-of-dict-of-dict data structure.
200
+ The outer dict (node_dict) holds adjacency information keyed by node.
201
+ The next dict (adjlist_dict) represents the adjacency information
202
+ and holds edge_key dicts keyed by neighbor. The edge_key dict holds
203
+ each edge_attr dict keyed by edge key. The inner dict
204
+ (edge_attr_dict) represents the edge data and holds edge attribute
205
+ values keyed by attribute names.
206
+
207
+ Each of these four dicts in the dict-of-dict-of-dict-of-dict
208
+ structure can be replaced by a user defined dict-like object.
209
+ In general, the dict-like features should be maintained but
210
+ extra features can be added. To replace one of the dicts create
211
+ a new graph class by changing the class(!) variable holding the
212
+ factory for that dict-like structure. The variable names are
213
+ node_dict_factory, node_attr_dict_factory, adjlist_inner_dict_factory,
214
+ adjlist_outer_dict_factory, edge_key_dict_factory, edge_attr_dict_factory
215
+ and graph_attr_dict_factory.
216
+
217
+ node_dict_factory : function, (default: dict)
218
+ Factory function to be used to create the dict containing node
219
+ attributes, keyed by node id.
220
+ It should require no arguments and return a dict-like object
221
+
222
+ node_attr_dict_factory: function, (default: dict)
223
+ Factory function to be used to create the node attribute
224
+ dict which holds attribute values keyed by attribute name.
225
+ It should require no arguments and return a dict-like object
226
+
227
+ adjlist_outer_dict_factory : function, (default: dict)
228
+ Factory function to be used to create the outer-most dict
229
+ in the data structure that holds adjacency info keyed by node.
230
+ It should require no arguments and return a dict-like object.
231
+
232
+ adjlist_inner_dict_factory : function, (default: dict)
233
+ Factory function to be used to create the adjacency list
234
+ dict which holds multiedge key dicts keyed by neighbor.
235
+ It should require no arguments and return a dict-like object.
236
+
237
+ edge_key_dict_factory : function, (default: dict)
238
+ Factory function to be used to create the edge key dict
239
+ which holds edge data keyed by edge key.
240
+ It should require no arguments and return a dict-like object.
241
+
242
+ edge_attr_dict_factory : function, (default: dict)
243
+ Factory function to be used to create the edge attribute
244
+ dict which holds attribute values keyed by attribute name.
245
+ It should require no arguments and return a dict-like object.
246
+
247
+ graph_attr_dict_factory : function, (default: dict)
248
+ Factory function to be used to create the graph attribute
249
+ dict which holds attribute values keyed by attribute name.
250
+ It should require no arguments and return a dict-like object.
251
+
252
+ Typically, if your extension doesn't impact the data structure all
253
+ methods will inherited without issue except: `to_directed/to_undirected`.
254
+ By default these methods create a DiGraph/Graph class and you probably
255
+ want them to create your extension of a DiGraph/Graph. To facilitate
256
+ this we define two class variables that you can set in your subclass.
257
+
258
+ to_directed_class : callable, (default: DiGraph or MultiDiGraph)
259
+ Class to create a new graph structure in the `to_directed` method.
260
+ If `None`, a NetworkX class (DiGraph or MultiDiGraph) is used.
261
+
262
+ to_undirected_class : callable, (default: Graph or MultiGraph)
263
+ Class to create a new graph structure in the `to_undirected` method.
264
+ If `None`, a NetworkX class (Graph or MultiGraph) is used.
265
+
266
+ **Subclassing Example**
267
+
268
+ Create a low memory graph class that effectively disallows edge
269
+ attributes by using a single attribute dict for all edges.
270
+ This reduces the memory used, but you lose edge attributes.
271
+
272
+ >>> class ThinGraph(nx.Graph):
273
+ ... all_edge_dict = {"weight": 1}
274
+ ...
275
+ ... def single_edge_dict(self):
276
+ ... return self.all_edge_dict
277
+ ...
278
+ ... edge_attr_dict_factory = single_edge_dict
279
+ >>> G = ThinGraph()
280
+ >>> G.add_edge(2, 1)
281
+ >>> G[2][1]
282
+ {'weight': 1}
283
+ >>> G.add_edge(2, 2)
284
+ >>> G[2][1] is G[2][2]
285
+ True
286
+ """
287
+
288
+ # node_dict_factory = dict # already assigned in Graph
289
+ # adjlist_outer_dict_factory = dict
290
+ # adjlist_inner_dict_factory = dict
291
+ edge_key_dict_factory = dict
292
+ # edge_attr_dict_factory = dict
293
+
294
+ def to_directed_class(self):
295
+ """Returns the class to use for empty directed copies.
296
+
297
+ If you subclass the base classes, use this to designate
298
+ what directed class to use for `to_directed()` copies.
299
+ """
300
+ return nx.MultiDiGraph
301
+
302
+ def to_undirected_class(self):
303
+ """Returns the class to use for empty undirected copies.
304
+
305
+ If you subclass the base classes, use this to designate
306
+ what directed class to use for `to_directed()` copies.
307
+ """
308
+ return MultiGraph
309
+
310
+ def __init__(self, incoming_graph_data=None, multigraph_input=None, **attr):
311
+ """Initialize a graph with edges, name, or graph attributes.
312
+
313
+ Parameters
314
+ ----------
315
+ incoming_graph_data : input graph
316
+ Data to initialize graph. If incoming_graph_data=None (default)
317
+ an empty graph is created. The data can be an edge list, or any
318
+ NetworkX graph object. If the corresponding optional Python
319
+ packages are installed the data can also be a 2D NumPy array, a
320
+ SciPy sparse array, or a PyGraphviz graph.
321
+
322
+ multigraph_input : bool or None (default None)
323
+ Note: Only used when `incoming_graph_data` is a dict.
324
+ If True, `incoming_graph_data` is assumed to be a
325
+ dict-of-dict-of-dict-of-dict structure keyed by
326
+ node to neighbor to edge keys to edge data for multi-edges.
327
+ A NetworkXError is raised if this is not the case.
328
+ If False, :func:`to_networkx_graph` is used to try to determine
329
+ the dict's graph data structure as either a dict-of-dict-of-dict
330
+ keyed by node to neighbor to edge data, or a dict-of-iterable
331
+ keyed by node to neighbors.
332
+ If None, the treatment for True is tried, but if it fails,
333
+ the treatment for False is tried.
334
+
335
+ attr : keyword arguments, optional (default= no attributes)
336
+ Attributes to add to graph as key=value pairs.
337
+
338
+ See Also
339
+ --------
340
+ convert
341
+
342
+ Examples
343
+ --------
344
+ >>> G = nx.MultiGraph()
345
+ >>> G = nx.MultiGraph(name="my graph")
346
+ >>> e = [(1, 2), (1, 2), (2, 3), (3, 4)] # list of edges
347
+ >>> G = nx.MultiGraph(e)
348
+
349
+ Arbitrary graph attribute pairs (key=value) may be assigned
350
+
351
+ >>> G = nx.MultiGraph(e, day="Friday")
352
+ >>> G.graph
353
+ {'day': 'Friday'}
354
+
355
+ """
356
+ # multigraph_input can be None/True/False. So check "is not False"
357
+ if isinstance(incoming_graph_data, dict) and multigraph_input is not False:
358
+ Graph.__init__(self)
359
+ try:
360
+ convert.from_dict_of_dicts(
361
+ incoming_graph_data, create_using=self, multigraph_input=True
362
+ )
363
+ self.graph.update(attr)
364
+ except Exception as err:
365
+ if multigraph_input is True:
366
+ raise nx.NetworkXError(
367
+ f"converting multigraph_input raised:\n{type(err)}: {err}"
368
+ )
369
+ Graph.__init__(self, incoming_graph_data, **attr)
370
+ else:
371
+ Graph.__init__(self, incoming_graph_data, **attr)
372
+
373
+ @cached_property
374
+ def adj(self):
375
+ """Graph adjacency object holding the neighbors of each node.
376
+
377
+ This object is a read-only dict-like structure with node keys
378
+ and neighbor-dict values. The neighbor-dict is keyed by neighbor
379
+ to the edgekey-data-dict. So `G.adj[3][2][0]['color'] = 'blue'` sets
380
+ the color of the edge `(3, 2, 0)` to `"blue"`.
381
+
382
+ Iterating over G.adj behaves like a dict. Useful idioms include
383
+ `for nbr, edgesdict in G.adj[n].items():`.
384
+
385
+ The neighbor information is also provided by subscripting the graph.
386
+
387
+ Examples
388
+ --------
389
+ >>> e = [(1, 2), (1, 2), (1, 3), (3, 4)] # list of edges
390
+ >>> G = nx.MultiGraph(e)
391
+ >>> G.edges[1, 2, 0]["weight"] = 3
392
+ >>> result = set()
393
+ >>> for edgekey, data in G[1][2].items():
394
+ ... result.add(data.get("weight", 1))
395
+ >>> result
396
+ {1, 3}
397
+
398
+ For directed graphs, `G.adj` holds outgoing (successor) info.
399
+ """
400
+ return MultiAdjacencyView(self._adj)
401
+
402
+ def new_edge_key(self, u, v):
403
+ """Returns an unused key for edges between nodes `u` and `v`.
404
+
405
+ The nodes `u` and `v` do not need to be already in the graph.
406
+
407
+ Notes
408
+ -----
409
+ In the standard MultiGraph class the new key is the number of existing
410
+ edges between `u` and `v` (increased if necessary to ensure unused).
411
+ The first edge will have key 0, then 1, etc. If an edge is removed
412
+ further new_edge_keys may not be in this order.
413
+
414
+ Parameters
415
+ ----------
416
+ u, v : nodes
417
+
418
+ Returns
419
+ -------
420
+ key : int
421
+ """
422
+ try:
423
+ keydict = self._adj[u][v]
424
+ except KeyError:
425
+ return 0
426
+ key = len(keydict)
427
+ while key in keydict:
428
+ key += 1
429
+ return key
430
+
431
+ def add_edge(self, u_for_edge, v_for_edge, key=None, **attr):
432
+ """Add an edge between u and v.
433
+
434
+ The nodes u and v will be automatically added if they are
435
+ not already in the graph.
436
+
437
+ Edge attributes can be specified with keywords or by directly
438
+ accessing the edge's attribute dictionary. See examples below.
439
+
440
+ Parameters
441
+ ----------
442
+ u_for_edge, v_for_edge : nodes
443
+ Nodes can be, for example, strings or numbers.
444
+ Nodes must be hashable (and not None) Python objects.
445
+ key : hashable identifier, optional (default=lowest unused integer)
446
+ Used to distinguish multiedges between a pair of nodes.
447
+ attr : keyword arguments, optional
448
+ Edge data (or labels or objects) can be assigned using
449
+ keyword arguments.
450
+
451
+ Returns
452
+ -------
453
+ The edge key assigned to the edge.
454
+
455
+ See Also
456
+ --------
457
+ add_edges_from : add a collection of edges
458
+
459
+ Notes
460
+ -----
461
+ To replace/update edge data, use the optional key argument
462
+ to identify a unique edge. Otherwise a new edge will be created.
463
+
464
+ NetworkX algorithms designed for weighted graphs cannot use
465
+ multigraphs directly because it is not clear how to handle
466
+ multiedge weights. Convert to Graph using edge attribute
467
+ 'weight' to enable weighted graph algorithms.
468
+
469
+ Default keys are generated using the method `new_edge_key()`.
470
+ This method can be overridden by subclassing the base class and
471
+ providing a custom `new_edge_key()` method.
472
+
473
+ Examples
474
+ --------
475
+ The following each add an additional edge e=(1, 2) to graph G:
476
+
477
+ >>> G = nx.MultiGraph()
478
+ >>> e = (1, 2)
479
+ >>> ekey = G.add_edge(1, 2) # explicit two-node form
480
+ >>> G.add_edge(*e) # single edge as tuple of two nodes
481
+ 1
482
+ >>> G.add_edges_from([(1, 2)]) # add edges from iterable container
483
+ [2]
484
+
485
+ Associate data to edges using keywords:
486
+
487
+ >>> ekey = G.add_edge(1, 2, weight=3)
488
+ >>> ekey = G.add_edge(1, 2, key=0, weight=4) # update data for key=0
489
+ >>> ekey = G.add_edge(1, 3, weight=7, capacity=15, length=342.7)
490
+
491
+ For non-string attribute keys, use subscript notation.
492
+
493
+ >>> ekey = G.add_edge(1, 2)
494
+ >>> G[1][2][0].update({0: 5})
495
+ >>> G.edges[1, 2, 0].update({0: 5})
496
+ """
497
+ u, v = u_for_edge, v_for_edge
498
+ # add nodes
499
+ if u not in self._adj:
500
+ if u is None:
501
+ raise ValueError("None cannot be a node")
502
+ self._adj[u] = self.adjlist_inner_dict_factory()
503
+ self._node[u] = self.node_attr_dict_factory()
504
+ if v not in self._adj:
505
+ if v is None:
506
+ raise ValueError("None cannot be a node")
507
+ self._adj[v] = self.adjlist_inner_dict_factory()
508
+ self._node[v] = self.node_attr_dict_factory()
509
+ if key is None:
510
+ key = self.new_edge_key(u, v)
511
+ if v in self._adj[u]:
512
+ keydict = self._adj[u][v]
513
+ datadict = keydict.get(key, self.edge_attr_dict_factory())
514
+ datadict.update(attr)
515
+ keydict[key] = datadict
516
+ else:
517
+ # selfloops work this way without special treatment
518
+ datadict = self.edge_attr_dict_factory()
519
+ datadict.update(attr)
520
+ keydict = self.edge_key_dict_factory()
521
+ keydict[key] = datadict
522
+ self._adj[u][v] = keydict
523
+ self._adj[v][u] = keydict
524
+ nx._clear_cache(self)
525
+ return key
526
+
527
+ def add_edges_from(self, ebunch_to_add, **attr):
528
+ """Add all the edges in ebunch_to_add.
529
+
530
+ Parameters
531
+ ----------
532
+ ebunch_to_add : container of edges
533
+ Each edge given in the container will be added to the
534
+ graph. The edges can be:
535
+
536
+ - 2-tuples (u, v) or
537
+ - 3-tuples (u, v, d) for an edge data dict d, or
538
+ - 3-tuples (u, v, k) for not iterable key k, or
539
+ - 4-tuples (u, v, k, d) for an edge with data and key k
540
+
541
+ attr : keyword arguments, optional
542
+ Edge data (or labels or objects) can be assigned using
543
+ keyword arguments.
544
+
545
+ Returns
546
+ -------
547
+ A list of edge keys assigned to the edges in `ebunch`.
548
+
549
+ See Also
550
+ --------
551
+ add_edge : add a single edge
552
+ add_weighted_edges_from : convenient way to add weighted edges
553
+
554
+ Notes
555
+ -----
556
+ Adding the same edge twice has no effect but any edge data
557
+ will be updated when each duplicate edge is added.
558
+
559
+ Edge attributes specified in an ebunch take precedence over
560
+ attributes specified via keyword arguments.
561
+
562
+ Default keys are generated using the method ``new_edge_key()``.
563
+ This method can be overridden by subclassing the base class and
564
+ providing a custom ``new_edge_key()`` method.
565
+
566
+ When adding edges from an iterator over the graph you are changing,
567
+ a `RuntimeError` can be raised with message:
568
+ `RuntimeError: dictionary changed size during iteration`. This
569
+ happens when the graph's underlying dictionary is modified during
570
+ iteration. To avoid this error, evaluate the iterator into a separate
571
+ object, e.g. by using `list(iterator_of_edges)`, and pass this
572
+ object to `G.add_edges_from`.
573
+
574
+ Examples
575
+ --------
576
+ >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
577
+ >>> G.add_edges_from([(0, 1), (1, 2)]) # using a list of edge tuples
578
+ >>> e = zip(range(0, 3), range(1, 4))
579
+ >>> G.add_edges_from(e) # Add the path graph 0-1-2-3
580
+
581
+ Associate data to edges
582
+
583
+ >>> G.add_edges_from([(1, 2), (2, 3)], weight=3)
584
+ >>> G.add_edges_from([(3, 4), (1, 4)], label="WN2898")
585
+
586
+ Evaluate an iterator over a graph if using it to modify the same graph
587
+
588
+ >>> G = nx.MultiGraph([(1, 2), (2, 3), (3, 4)])
589
+ >>> # Grow graph by one new node, adding edges to all existing nodes.
590
+ >>> # wrong way - will raise RuntimeError
591
+ >>> # G.add_edges_from(((5, n) for n in G.nodes))
592
+ >>> # right way - note that there will be no self-edge for node 5
593
+ >>> assigned_keys = G.add_edges_from(list((5, n) for n in G.nodes))
594
+ """
595
+ keylist = []
596
+ for e in ebunch_to_add:
597
+ ne = len(e)
598
+ if ne == 4:
599
+ u, v, key, dd = e
600
+ elif ne == 3:
601
+ u, v, dd = e
602
+ key = None
603
+ elif ne == 2:
604
+ u, v = e
605
+ dd = {}
606
+ key = None
607
+ else:
608
+ msg = f"Edge tuple {e} must be a 2-tuple, 3-tuple or 4-tuple."
609
+ raise NetworkXError(msg)
610
+ ddd = {}
611
+ ddd.update(attr)
612
+ try:
613
+ ddd.update(dd)
614
+ except (TypeError, ValueError):
615
+ if ne != 3:
616
+ raise
617
+ key = dd # ne == 3 with 3rd value not dict, must be a key
618
+ key = self.add_edge(u, v, key)
619
+ self[u][v][key].update(ddd)
620
+ keylist.append(key)
621
+ nx._clear_cache(self)
622
+ return keylist
623
+
624
+ def remove_edge(self, u, v, key=None):
625
+ """Remove an edge between u and v.
626
+
627
+ Parameters
628
+ ----------
629
+ u, v : nodes
630
+ Remove an edge between nodes u and v.
631
+ key : hashable identifier, optional (default=None)
632
+ Used to distinguish multiple edges between a pair of nodes.
633
+ If None, remove a single edge between u and v. If there are
634
+ multiple edges, removes the last edge added in terms of
635
+ insertion order.
636
+
637
+ Raises
638
+ ------
639
+ NetworkXError
640
+ If there is not an edge between u and v, or
641
+ if there is no edge with the specified key.
642
+
643
+ See Also
644
+ --------
645
+ remove_edges_from : remove a collection of edges
646
+
647
+ Examples
648
+ --------
649
+ >>> G = nx.MultiGraph()
650
+ >>> nx.add_path(G, [0, 1, 2, 3])
651
+ >>> G.remove_edge(0, 1)
652
+ >>> e = (1, 2)
653
+ >>> G.remove_edge(*e) # unpacks e from an edge tuple
654
+
655
+ For multiple edges
656
+
657
+ >>> G = nx.MultiGraph() # or MultiDiGraph, etc
658
+ >>> G.add_edges_from([(1, 2), (1, 2), (1, 2)]) # key_list returned
659
+ [0, 1, 2]
660
+
661
+ When ``key=None`` (the default), edges are removed in the opposite
662
+ order that they were added:
663
+
664
+ >>> G.remove_edge(1, 2)
665
+ >>> G.edges(keys=True)
666
+ MultiEdgeView([(1, 2, 0), (1, 2, 1)])
667
+ >>> G.remove_edge(2, 1) # edges are not directed
668
+ >>> G.edges(keys=True)
669
+ MultiEdgeView([(1, 2, 0)])
670
+
671
+ For edges with keys
672
+
673
+ >>> G = nx.MultiGraph()
674
+ >>> G.add_edge(1, 2, key="first")
675
+ 'first'
676
+ >>> G.add_edge(1, 2, key="second")
677
+ 'second'
678
+ >>> G.remove_edge(1, 2, key="first")
679
+ >>> G.edges(keys=True)
680
+ MultiEdgeView([(1, 2, 'second')])
681
+
682
+ """
683
+ try:
684
+ d = self._adj[u][v]
685
+ except KeyError as err:
686
+ raise NetworkXError(f"The edge {u}-{v} is not in the graph.") from err
687
+ # remove the edge with specified data
688
+ if key is None:
689
+ d.popitem()
690
+ else:
691
+ try:
692
+ del d[key]
693
+ except KeyError as err:
694
+ msg = f"The edge {u}-{v} with key {key} is not in the graph."
695
+ raise NetworkXError(msg) from err
696
+ if len(d) == 0:
697
+ # remove the key entries if last edge
698
+ del self._adj[u][v]
699
+ if u != v: # check for selfloop
700
+ del self._adj[v][u]
701
+ nx._clear_cache(self)
702
+
703
+ def remove_edges_from(self, ebunch):
704
+ """Remove all edges specified in ebunch.
705
+
706
+ Parameters
707
+ ----------
708
+ ebunch: list or container of edge tuples
709
+ Each edge given in the list or container will be removed
710
+ from the graph. The edges can be:
711
+
712
+ - 2-tuples (u, v) A single edge between u and v is removed.
713
+ - 3-tuples (u, v, key) The edge identified by key is removed.
714
+ - 4-tuples (u, v, key, data) where data is ignored.
715
+
716
+ See Also
717
+ --------
718
+ remove_edge : remove a single edge
719
+
720
+ Notes
721
+ -----
722
+ Will fail silently if an edge in ebunch is not in the graph.
723
+
724
+ Examples
725
+ --------
726
+ >>> G = nx.path_graph(4) # or DiGraph, MultiGraph, MultiDiGraph, etc
727
+ >>> ebunch = [(1, 2), (2, 3)]
728
+ >>> G.remove_edges_from(ebunch)
729
+
730
+ Removing multiple copies of edges
731
+
732
+ >>> G = nx.MultiGraph()
733
+ >>> keys = G.add_edges_from([(1, 2), (1, 2), (1, 2)])
734
+ >>> G.remove_edges_from([(1, 2), (2, 1)]) # edges aren't directed
735
+ >>> list(G.edges())
736
+ [(1, 2)]
737
+ >>> G.remove_edges_from([(1, 2), (1, 2)]) # silently ignore extra copy
738
+ >>> list(G.edges) # now empty graph
739
+ []
740
+
741
+ When the edge is a 2-tuple ``(u, v)`` but there are multiple edges between
742
+ u and v in the graph, the most recent edge (in terms of insertion
743
+ order) is removed.
744
+
745
+ >>> G = nx.MultiGraph()
746
+ >>> for key in ("x", "y", "a"):
747
+ ... k = G.add_edge(0, 1, key=key)
748
+ >>> G.edges(keys=True)
749
+ MultiEdgeView([(0, 1, 'x'), (0, 1, 'y'), (0, 1, 'a')])
750
+ >>> G.remove_edges_from([(0, 1)])
751
+ >>> G.edges(keys=True)
752
+ MultiEdgeView([(0, 1, 'x'), (0, 1, 'y')])
753
+
754
+ """
755
+ for e in ebunch:
756
+ try:
757
+ self.remove_edge(*e[:3])
758
+ except NetworkXError:
759
+ pass
760
+ nx._clear_cache(self)
761
+
762
+ def has_edge(self, u, v, key=None):
763
+ """Returns True if the graph has an edge between nodes u and v.
764
+
765
+ This is the same as `v in G[u] or key in G[u][v]`
766
+ without KeyError exceptions.
767
+
768
+ Parameters
769
+ ----------
770
+ u, v : nodes
771
+ Nodes can be, for example, strings or numbers.
772
+
773
+ key : hashable identifier, optional (default=None)
774
+ If specified return True only if the edge with
775
+ key is found.
776
+
777
+ Returns
778
+ -------
779
+ edge_ind : bool
780
+ True if edge is in the graph, False otherwise.
781
+
782
+ Examples
783
+ --------
784
+ Can be called either using two nodes u, v, an edge tuple (u, v),
785
+ or an edge tuple (u, v, key).
786
+
787
+ >>> G = nx.MultiGraph() # or MultiDiGraph
788
+ >>> nx.add_path(G, [0, 1, 2, 3])
789
+ >>> G.has_edge(0, 1) # using two nodes
790
+ True
791
+ >>> e = (0, 1)
792
+ >>> G.has_edge(*e) # e is a 2-tuple (u, v)
793
+ True
794
+ >>> G.add_edge(0, 1, key="a")
795
+ 'a'
796
+ >>> G.has_edge(0, 1, key="a") # specify key
797
+ True
798
+ >>> G.has_edge(1, 0, key="a") # edges aren't directed
799
+ True
800
+ >>> e = (0, 1, "a")
801
+ >>> G.has_edge(*e) # e is a 3-tuple (u, v, 'a')
802
+ True
803
+
804
+ The following syntax are equivalent:
805
+
806
+ >>> G.has_edge(0, 1)
807
+ True
808
+ >>> 1 in G[0] # though this gives :exc:`KeyError` if 0 not in G
809
+ True
810
+ >>> 0 in G[1] # other order; also gives :exc:`KeyError` if 0 not in G
811
+ True
812
+
813
+ """
814
+ try:
815
+ if key is None:
816
+ return v in self._adj[u]
817
+ else:
818
+ return key in self._adj[u][v]
819
+ except KeyError:
820
+ return False
821
+
822
+ @cached_property
823
+ def edges(self):
824
+ """Returns an iterator over the edges.
825
+
826
+ edges(self, nbunch=None, data=False, keys=False, default=None)
827
+
828
+ The MultiEdgeView provides set-like operations on the edge-tuples
829
+ as well as edge attribute lookup. When called, it also provides
830
+ an EdgeDataView object which allows control of access to edge
831
+ attributes (but does not provide set-like operations).
832
+ Hence, ``G.edges[u, v, k]['color']`` provides the value of the color
833
+ attribute for the edge from ``u`` to ``v`` with key ``k`` while
834
+ ``for (u, v, k, c) in G.edges(data='color', keys=True, default="red"):``
835
+ iterates through all the edges yielding the color attribute with
836
+ default `'red'` if no color attribute exists.
837
+
838
+ Edges are returned as tuples with optional data and keys
839
+ in the order (node, neighbor, key, data). If ``keys=True`` is not
840
+ provided, the tuples will just be (node, neighbor, data), but
841
+ multiple tuples with the same node and neighbor will be generated
842
+ when multiple edges exist between two nodes.
843
+
844
+ Parameters
845
+ ----------
846
+ nbunch : single node, container, or all nodes (default= all nodes)
847
+ The view will only report edges from these nodes.
848
+ data : string or bool, optional (default=False)
849
+ The edge attribute returned in 3-tuple (u, v, ddict[data]).
850
+ If True, return edge attribute dict in 3-tuple (u, v, ddict).
851
+ If False, return 2-tuple (u, v).
852
+ keys : bool, optional (default=False)
853
+ If True, return edge keys with each edge, creating (u, v, k)
854
+ tuples or (u, v, k, d) tuples if data is also requested.
855
+ default : value, optional (default=None)
856
+ Value used for edges that don't have the requested attribute.
857
+ Only relevant if data is not True or False.
858
+
859
+ Returns
860
+ -------
861
+ edges : MultiEdgeView
862
+ A view of edge attributes, usually it iterates over (u, v)
863
+ (u, v, k) or (u, v, k, d) tuples of edges, but can also be
864
+ used for attribute lookup as ``edges[u, v, k]['foo']``.
865
+
866
+ Notes
867
+ -----
868
+ Nodes in nbunch that are not in the graph will be (quietly) ignored.
869
+ For directed graphs this returns the out-edges.
870
+
871
+ Examples
872
+ --------
873
+ >>> G = nx.MultiGraph()
874
+ >>> nx.add_path(G, [0, 1, 2])
875
+ >>> key = G.add_edge(2, 3, weight=5)
876
+ >>> key2 = G.add_edge(2, 1, weight=2) # multi-edge
877
+ >>> [e for e in G.edges()]
878
+ [(0, 1), (1, 2), (1, 2), (2, 3)]
879
+ >>> G.edges.data() # default data is {} (empty dict)
880
+ MultiEdgeDataView([(0, 1, {}), (1, 2, {}), (1, 2, {'weight': 2}), (2, 3, {'weight': 5})])
881
+ >>> G.edges.data("weight", default=1)
882
+ MultiEdgeDataView([(0, 1, 1), (1, 2, 1), (1, 2, 2), (2, 3, 5)])
883
+ >>> G.edges(keys=True) # default keys are integers
884
+ MultiEdgeView([(0, 1, 0), (1, 2, 0), (1, 2, 1), (2, 3, 0)])
885
+ >>> G.edges.data(keys=True)
886
+ MultiEdgeDataView([(0, 1, 0, {}), (1, 2, 0, {}), (1, 2, 1, {'weight': 2}), (2, 3, 0, {'weight': 5})])
887
+ >>> G.edges.data("weight", default=1, keys=True)
888
+ MultiEdgeDataView([(0, 1, 0, 1), (1, 2, 0, 1), (1, 2, 1, 2), (2, 3, 0, 5)])
889
+ >>> G.edges([0, 3]) # Note ordering of tuples from listed sources
890
+ MultiEdgeDataView([(0, 1), (3, 2)])
891
+ >>> G.edges([0, 3, 2, 1]) # Note ordering of tuples
892
+ MultiEdgeDataView([(0, 1), (3, 2), (2, 1), (2, 1)])
893
+ >>> G.edges(0)
894
+ MultiEdgeDataView([(0, 1)])
895
+ """
896
+ return MultiEdgeView(self)
897
+
898
+ def get_edge_data(self, u, v, key=None, default=None):
899
+ """Returns the attribute dictionary associated with edge (u, v,
900
+ key).
901
+
902
+ If a key is not provided, returns a dictionary mapping edge keys
903
+ to attribute dictionaries for each edge between u and v.
904
+
905
+ This is identical to `G[u][v][key]` except the default is returned
906
+ instead of an exception is the edge doesn't exist.
907
+
908
+ Parameters
909
+ ----------
910
+ u, v : nodes
911
+
912
+ default : any Python object (default=None)
913
+ Value to return if the specific edge (u, v, key) is not
914
+ found, OR if there are no edges between u and v and no key
915
+ is specified.
916
+
917
+ key : hashable identifier, optional (default=None)
918
+ Return data only for the edge with specified key, as an
919
+ attribute dictionary (rather than a dictionary mapping keys
920
+ to attribute dictionaries).
921
+
922
+ Returns
923
+ -------
924
+ edge_dict : dictionary
925
+ The edge attribute dictionary, OR a dictionary mapping edge
926
+ keys to attribute dictionaries for each of those edges if no
927
+ specific key is provided (even if there's only one edge
928
+ between u and v).
929
+
930
+ Examples
931
+ --------
932
+ >>> G = nx.MultiGraph() # or MultiDiGraph
933
+ >>> key = G.add_edge(0, 1, key="a", weight=7)
934
+ >>> G[0][1]["a"] # key='a'
935
+ {'weight': 7}
936
+ >>> G.edges[0, 1, "a"] # key='a'
937
+ {'weight': 7}
938
+
939
+ Warning: we protect the graph data structure by making
940
+ `G.edges` and `G[1][2]` read-only dict-like structures.
941
+ However, you can assign values to attributes in e.g.
942
+ `G.edges[1, 2, 'a']` or `G[1][2]['a']` using an additional
943
+ bracket as shown next. You need to specify all edge info
944
+ to assign to the edge data associated with an edge.
945
+
946
+ >>> G[0][1]["a"]["weight"] = 10
947
+ >>> G.edges[0, 1, "a"]["weight"] = 10
948
+ >>> G[0][1]["a"]["weight"]
949
+ 10
950
+ >>> G.edges[1, 0, "a"]["weight"]
951
+ 10
952
+
953
+ >>> G = nx.MultiGraph() # or MultiDiGraph
954
+ >>> nx.add_path(G, [0, 1, 2, 3])
955
+ >>> G.edges[0, 1, 0]["weight"] = 5
956
+ >>> G.get_edge_data(0, 1)
957
+ {0: {'weight': 5}}
958
+ >>> e = (0, 1)
959
+ >>> G.get_edge_data(*e) # tuple form
960
+ {0: {'weight': 5}}
961
+ >>> G.get_edge_data(3, 0) # edge not in graph, returns None
962
+ >>> G.get_edge_data(3, 0, default=0) # edge not in graph, return default
963
+ 0
964
+ >>> G.get_edge_data(1, 0, 0) # specific key gives back
965
+ {'weight': 5}
966
+ """
967
+ try:
968
+ if key is None:
969
+ return self._adj[u][v]
970
+ else:
971
+ return self._adj[u][v][key]
972
+ except KeyError:
973
+ return default
974
+
975
+ @cached_property
976
+ def degree(self):
977
+ """A DegreeView for the Graph as G.degree or G.degree().
978
+
979
+ The node degree is the number of edges adjacent to the node.
980
+ The weighted node degree is the sum of the edge weights for
981
+ edges incident to that node.
982
+
983
+ This object provides an iterator for (node, degree) as well as
984
+ lookup for the degree for a single node.
985
+
986
+ Parameters
987
+ ----------
988
+ nbunch : single node, container, or all nodes (default= all nodes)
989
+ The view will only report edges incident to these nodes.
990
+
991
+ weight : string or None, optional (default=None)
992
+ The name of an edge attribute that holds the numerical value used
993
+ as a weight. If None, then each edge has weight 1.
994
+ The degree is the sum of the edge weights adjacent to the node.
995
+
996
+ Returns
997
+ -------
998
+ MultiDegreeView or int
999
+ If multiple nodes are requested (the default), returns a `MultiDegreeView`
1000
+ mapping nodes to their degree.
1001
+ If a single node is requested, returns the degree of the node as an integer.
1002
+
1003
+ Examples
1004
+ --------
1005
+ >>> G = nx.Graph() # or DiGraph, MultiGraph, MultiDiGraph, etc
1006
+ >>> nx.add_path(G, [0, 1, 2, 3])
1007
+ >>> G.degree(0) # node 0 with degree 1
1008
+ 1
1009
+ >>> list(G.degree([0, 1]))
1010
+ [(0, 1), (1, 2)]
1011
+
1012
+ """
1013
+ return MultiDegreeView(self)
1014
+
1015
+ def is_multigraph(self):
1016
+ """Returns True if graph is a multigraph, False otherwise."""
1017
+ return True
1018
+
1019
+ def is_directed(self):
1020
+ """Returns True if graph is directed, False otherwise."""
1021
+ return False
1022
+
1023
+ def copy(self, as_view=False):
1024
+ """Returns a copy of the graph.
1025
+
1026
+ The copy method by default returns an independent shallow copy
1027
+ of the graph and attributes. That is, if an attribute is a
1028
+ container, that container is shared by the original an the copy.
1029
+ Use Python's `copy.deepcopy` for new containers.
1030
+
1031
+ If `as_view` is True then a view is returned instead of a copy.
1032
+
1033
+ Notes
1034
+ -----
1035
+ All copies reproduce the graph structure, but data attributes
1036
+ may be handled in different ways. There are four types of copies
1037
+ of a graph that people might want.
1038
+
1039
+ Deepcopy -- A "deepcopy" copies the graph structure as well as
1040
+ all data attributes and any objects they might contain.
1041
+ The entire graph object is new so that changes in the copy
1042
+ do not affect the original object. (see Python's copy.deepcopy)
1043
+
1044
+ Data Reference (Shallow) -- For a shallow copy the graph structure
1045
+ is copied but the edge, node and graph attribute dicts are
1046
+ references to those in the original graph. This saves
1047
+ time and memory but could cause confusion if you change an attribute
1048
+ in one graph and it changes the attribute in the other.
1049
+ NetworkX does not provide this level of shallow copy.
1050
+
1051
+ Independent Shallow -- This copy creates new independent attribute
1052
+ dicts and then does a shallow copy of the attributes. That is, any
1053
+ attributes that are containers are shared between the new graph
1054
+ and the original. This is exactly what `dict.copy()` provides.
1055
+ You can obtain this style copy using:
1056
+
1057
+ >>> G = nx.path_graph(5)
1058
+ >>> H = G.copy()
1059
+ >>> H = G.copy(as_view=False)
1060
+ >>> H = nx.Graph(G)
1061
+ >>> H = G.__class__(G)
1062
+
1063
+ Fresh Data -- For fresh data, the graph structure is copied while
1064
+ new empty data attribute dicts are created. The resulting graph
1065
+ is independent of the original and it has no edge, node or graph
1066
+ attributes. Fresh copies are not enabled. Instead use:
1067
+
1068
+ >>> H = G.__class__()
1069
+ >>> H.add_nodes_from(G)
1070
+ >>> H.add_edges_from(G.edges)
1071
+
1072
+ View -- Inspired by dict-views, graph-views act like read-only
1073
+ versions of the original graph, providing a copy of the original
1074
+ structure without requiring any memory for copying the information.
1075
+
1076
+ See the Python copy module for more information on shallow
1077
+ and deep copies, https://docs.python.org/3/library/copy.html.
1078
+
1079
+ Parameters
1080
+ ----------
1081
+ as_view : bool, optional (default=False)
1082
+ If True, the returned graph-view provides a read-only view
1083
+ of the original graph without actually copying any data.
1084
+
1085
+ Returns
1086
+ -------
1087
+ G : Graph
1088
+ A copy of the graph.
1089
+
1090
+ See Also
1091
+ --------
1092
+ to_directed: return a directed copy of the graph.
1093
+
1094
+ Examples
1095
+ --------
1096
+ >>> G = nx.path_graph(4) # or DiGraph, MultiGraph, MultiDiGraph, etc
1097
+ >>> H = G.copy()
1098
+
1099
+ """
1100
+ if as_view is True:
1101
+ return nx.graphviews.generic_graph_view(self)
1102
+ G = self.__class__()
1103
+ G.graph.update(self.graph)
1104
+ G.add_nodes_from((n, d.copy()) for n, d in self._node.items())
1105
+ G.add_edges_from(
1106
+ (u, v, key, datadict.copy())
1107
+ for u, nbrs in self._adj.items()
1108
+ for v, keydict in nbrs.items()
1109
+ for key, datadict in keydict.items()
1110
+ )
1111
+ return G
1112
+
1113
+ def to_directed(self, as_view=False):
1114
+ """Returns a directed representation of the graph.
1115
+
1116
+ Returns
1117
+ -------
1118
+ G : MultiDiGraph
1119
+ A directed graph with the same name, same nodes, and with
1120
+ each edge (u, v, k, data) replaced by two directed edges
1121
+ (u, v, k, data) and (v, u, k, data).
1122
+
1123
+ Notes
1124
+ -----
1125
+ This returns a "deepcopy" of the edge, node, and
1126
+ graph attributes which attempts to completely copy
1127
+ all of the data and references.
1128
+
1129
+ This is in contrast to the similar D=MultiDiGraph(G) which
1130
+ returns a shallow copy of the data.
1131
+
1132
+ See the Python copy module for more information on shallow
1133
+ and deep copies, https://docs.python.org/3/library/copy.html.
1134
+
1135
+ Warning: If you have subclassed MultiGraph to use dict-like objects
1136
+ in the data structure, those changes do not transfer to the
1137
+ MultiDiGraph created by this method.
1138
+
1139
+ Examples
1140
+ --------
1141
+ >>> G = nx.MultiGraph()
1142
+ >>> G.add_edge(0, 1)
1143
+ 0
1144
+ >>> G.add_edge(0, 1)
1145
+ 1
1146
+ >>> H = G.to_directed()
1147
+ >>> list(H.edges)
1148
+ [(0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1)]
1149
+
1150
+ If already directed, return a (deep) copy
1151
+
1152
+ >>> G = nx.MultiDiGraph()
1153
+ >>> G.add_edge(0, 1)
1154
+ 0
1155
+ >>> H = G.to_directed()
1156
+ >>> list(H.edges)
1157
+ [(0, 1, 0)]
1158
+ """
1159
+ graph_class = self.to_directed_class()
1160
+ if as_view is True:
1161
+ return nx.graphviews.generic_graph_view(self, graph_class)
1162
+ # deepcopy when not a view
1163
+ G = graph_class()
1164
+ G.graph.update(deepcopy(self.graph))
1165
+ G.add_nodes_from((n, deepcopy(d)) for n, d in self._node.items())
1166
+ G.add_edges_from(
1167
+ (u, v, key, deepcopy(datadict))
1168
+ for u, nbrs in self.adj.items()
1169
+ for v, keydict in nbrs.items()
1170
+ for key, datadict in keydict.items()
1171
+ )
1172
+ return G
1173
+
1174
+ def to_undirected(self, as_view=False):
1175
+ """Returns an undirected copy of the graph.
1176
+
1177
+ Returns
1178
+ -------
1179
+ G : Graph/MultiGraph
1180
+ A deepcopy of the graph.
1181
+
1182
+ See Also
1183
+ --------
1184
+ copy, add_edge, add_edges_from
1185
+
1186
+ Notes
1187
+ -----
1188
+ This returns a "deepcopy" of the edge, node, and
1189
+ graph attributes which attempts to completely copy
1190
+ all of the data and references.
1191
+
1192
+ This is in contrast to the similar `G = nx.MultiGraph(D)`
1193
+ which returns a shallow copy of the data.
1194
+
1195
+ See the Python copy module for more information on shallow
1196
+ and deep copies, https://docs.python.org/3/library/copy.html.
1197
+
1198
+ Warning: If you have subclassed MultiGraph to use dict-like
1199
+ objects in the data structure, those changes do not transfer
1200
+ to the MultiGraph created by this method.
1201
+
1202
+ Examples
1203
+ --------
1204
+ >>> G = nx.MultiGraph([(0, 1), (0, 1), (1, 2)])
1205
+ >>> H = G.to_directed()
1206
+ >>> list(H.edges)
1207
+ [(0, 1, 0), (0, 1, 1), (1, 0, 0), (1, 0, 1), (1, 2, 0), (2, 1, 0)]
1208
+ >>> G2 = H.to_undirected()
1209
+ >>> list(G2.edges)
1210
+ [(0, 1, 0), (0, 1, 1), (1, 2, 0)]
1211
+ """
1212
+ graph_class = self.to_undirected_class()
1213
+ if as_view is True:
1214
+ return nx.graphviews.generic_graph_view(self, graph_class)
1215
+ # deepcopy when not a view
1216
+ G = graph_class()
1217
+ G.graph.update(deepcopy(self.graph))
1218
+ G.add_nodes_from((n, deepcopy(d)) for n, d in self._node.items())
1219
+ G.add_edges_from(
1220
+ (u, v, key, deepcopy(datadict))
1221
+ for u, nbrs in self._adj.items()
1222
+ for v, keydict in nbrs.items()
1223
+ for key, datadict in keydict.items()
1224
+ )
1225
+ return G
1226
+
1227
+ def number_of_edges(self, u=None, v=None):
1228
+ """Returns the number of edges between two nodes.
1229
+
1230
+ Parameters
1231
+ ----------
1232
+ u, v : nodes, optional (Default=all edges)
1233
+ If u and v are specified, return the number of edges between
1234
+ u and v. Otherwise return the total number of all edges.
1235
+
1236
+ Returns
1237
+ -------
1238
+ nedges : int
1239
+ The number of edges in the graph. If nodes `u` and `v` are
1240
+ specified return the number of edges between those nodes. If
1241
+ the graph is directed, this only returns the number of edges
1242
+ from `u` to `v`.
1243
+
1244
+ See Also
1245
+ --------
1246
+ size
1247
+
1248
+ Examples
1249
+ --------
1250
+ For undirected multigraphs, this method counts the total number
1251
+ of edges in the graph::
1252
+
1253
+ >>> G = nx.MultiGraph()
1254
+ >>> G.add_edges_from([(0, 1), (0, 1), (1, 2)])
1255
+ [0, 1, 0]
1256
+ >>> G.number_of_edges()
1257
+ 3
1258
+
1259
+ If you specify two nodes, this counts the total number of edges
1260
+ joining the two nodes::
1261
+
1262
+ >>> G.number_of_edges(0, 1)
1263
+ 2
1264
+
1265
+ For directed multigraphs, this method can count the total number
1266
+ of directed edges from `u` to `v`::
1267
+
1268
+ >>> G = nx.MultiDiGraph()
1269
+ >>> G.add_edges_from([(0, 1), (0, 1), (1, 0)])
1270
+ [0, 1, 0]
1271
+ >>> G.number_of_edges(0, 1)
1272
+ 2
1273
+ >>> G.number_of_edges(1, 0)
1274
+ 1
1275
+
1276
+ """
1277
+ if u is None:
1278
+ return self.size()
1279
+ try:
1280
+ edgedata = self._adj[u][v]
1281
+ except KeyError:
1282
+ return 0 # no such edge
1283
+ return len(edgedata)
minigpt2/lib/python3.10/site-packages/networkx/classes/reportviews.py ADDED
@@ -0,0 +1,1447 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ View Classes provide node, edge and degree "views" of a graph.
3
+
4
+ Views for nodes, edges and degree are provided for all base graph classes.
5
+ A view means a read-only object that is quick to create, automatically
6
+ updated when the graph changes, and provides basic access like `n in V`,
7
+ `for n in V`, `V[n]` and sometimes set operations.
8
+
9
+ The views are read-only iterable containers that are updated as the
10
+ graph is updated. As with dicts, the graph should not be updated
11
+ while iterating through the view. Views can be iterated multiple times.
12
+
13
+ Edge and Node views also allow data attribute lookup.
14
+ The resulting attribute dict is writable as `G.edges[3, 4]['color']='red'`
15
+ Degree views allow lookup of degree values for single nodes.
16
+ Weighted degree is supported with the `weight` argument.
17
+
18
+ NodeView
19
+ ========
20
+
21
+ `V = G.nodes` (or `V = G.nodes()`) allows `len(V)`, `n in V`, set
22
+ operations e.g. "G.nodes & H.nodes", and `dd = G.nodes[n]`, where
23
+ `dd` is the node data dict. Iteration is over the nodes by default.
24
+
25
+ NodeDataView
26
+ ============
27
+
28
+ To iterate over (node, data) pairs, use arguments to `G.nodes()`
29
+ to create a DataView e.g. `DV = G.nodes(data='color', default='red')`.
30
+ The DataView iterates as `for n, color in DV` and allows
31
+ `(n, 'red') in DV`. Using `DV = G.nodes(data=True)`, the DataViews
32
+ use the full datadict in writeable form also allowing contain testing as
33
+ `(n, {'color': 'red'}) in VD`. DataViews allow set operations when
34
+ data attributes are hashable.
35
+
36
+ DegreeView
37
+ ==========
38
+
39
+ `V = G.degree` allows iteration over (node, degree) pairs as well
40
+ as lookup: `deg=V[n]`. There are many flavors of DegreeView
41
+ for In/Out/Directed/Multi. For Directed Graphs, `G.degree`
42
+ counts both in and out going edges. `G.out_degree` and
43
+ `G.in_degree` count only specific directions.
44
+ Weighted degree using edge data attributes is provide via
45
+ `V = G.degree(weight='attr_name')` where any string with the
46
+ attribute name can be used. `weight=None` is the default.
47
+ No set operations are implemented for degrees, use NodeView.
48
+
49
+ The argument `nbunch` restricts iteration to nodes in nbunch.
50
+ The DegreeView can still lookup any node even if nbunch is specified.
51
+
52
+ EdgeView
53
+ ========
54
+
55
+ `V = G.edges` or `V = G.edges()` allows iteration over edges as well as
56
+ `e in V`, set operations and edge data lookup `dd = G.edges[2, 3]`.
57
+ Iteration is over 2-tuples `(u, v)` for Graph/DiGraph. For multigraphs
58
+ edges 3-tuples `(u, v, key)` are the default but 2-tuples can be obtained
59
+ via `V = G.edges(keys=False)`.
60
+
61
+ Set operations for directed graphs treat the edges as a set of 2-tuples.
62
+ For undirected graphs, 2-tuples are not a unique representation of edges.
63
+ So long as the set being compared to contains unique representations
64
+ of its edges, the set operations will act as expected. If the other
65
+ set contains both `(0, 1)` and `(1, 0)` however, the result of set
66
+ operations may contain both representations of the same edge.
67
+
68
+ EdgeDataView
69
+ ============
70
+
71
+ Edge data can be reported using an EdgeDataView typically created
72
+ by calling an EdgeView: `DV = G.edges(data='weight', default=1)`.
73
+ The EdgeDataView allows iteration over edge tuples, membership checking
74
+ but no set operations.
75
+
76
+ Iteration depends on `data` and `default` and for multigraph `keys`
77
+ If `data is False` (the default) then iterate over 2-tuples `(u, v)`.
78
+ If `data is True` iterate over 3-tuples `(u, v, datadict)`.
79
+ Otherwise iterate over `(u, v, datadict.get(data, default))`.
80
+ For Multigraphs, if `keys is True`, replace `u, v` with `u, v, key`
81
+ to create 3-tuples and 4-tuples.
82
+
83
+ The argument `nbunch` restricts edges to those incident to nodes in nbunch.
84
+ """
85
+
86
+ from abc import ABC
87
+ from collections.abc import Mapping, Set
88
+
89
+ import networkx as nx
90
+
91
+ __all__ = [
92
+ "NodeView",
93
+ "NodeDataView",
94
+ "EdgeView",
95
+ "OutEdgeView",
96
+ "InEdgeView",
97
+ "EdgeDataView",
98
+ "OutEdgeDataView",
99
+ "InEdgeDataView",
100
+ "MultiEdgeView",
101
+ "OutMultiEdgeView",
102
+ "InMultiEdgeView",
103
+ "MultiEdgeDataView",
104
+ "OutMultiEdgeDataView",
105
+ "InMultiEdgeDataView",
106
+ "DegreeView",
107
+ "DiDegreeView",
108
+ "InDegreeView",
109
+ "OutDegreeView",
110
+ "MultiDegreeView",
111
+ "DiMultiDegreeView",
112
+ "InMultiDegreeView",
113
+ "OutMultiDegreeView",
114
+ ]
115
+
116
+
117
+ # NodeViews
118
+ class NodeView(Mapping, Set):
119
+ """A NodeView class to act as G.nodes for a NetworkX Graph
120
+
121
+ Set operations act on the nodes without considering data.
122
+ Iteration is over nodes. Node data can be looked up like a dict.
123
+ Use NodeDataView to iterate over node data or to specify a data
124
+ attribute for lookup. NodeDataView is created by calling the NodeView.
125
+
126
+ Parameters
127
+ ----------
128
+ graph : NetworkX graph-like class
129
+
130
+ Examples
131
+ --------
132
+ >>> G = nx.path_graph(3)
133
+ >>> NV = G.nodes()
134
+ >>> 2 in NV
135
+ True
136
+ >>> for n in NV:
137
+ ... print(n)
138
+ 0
139
+ 1
140
+ 2
141
+ >>> assert NV & {1, 2, 3} == {1, 2}
142
+
143
+ >>> G.add_node(2, color="blue")
144
+ >>> NV[2]
145
+ {'color': 'blue'}
146
+ >>> G.add_node(8, color="red")
147
+ >>> NDV = G.nodes(data=True)
148
+ >>> (2, NV[2]) in NDV
149
+ True
150
+ >>> for n, dd in NDV:
151
+ ... print((n, dd.get("color", "aqua")))
152
+ (0, 'aqua')
153
+ (1, 'aqua')
154
+ (2, 'blue')
155
+ (8, 'red')
156
+ >>> NDV[2] == NV[2]
157
+ True
158
+
159
+ >>> NVdata = G.nodes(data="color", default="aqua")
160
+ >>> (2, NVdata[2]) in NVdata
161
+ True
162
+ >>> for n, dd in NVdata:
163
+ ... print((n, dd))
164
+ (0, 'aqua')
165
+ (1, 'aqua')
166
+ (2, 'blue')
167
+ (8, 'red')
168
+ >>> NVdata[2] == NV[2] # NVdata gets 'color', NV gets datadict
169
+ False
170
+ """
171
+
172
+ __slots__ = ("_nodes",)
173
+
174
+ def __getstate__(self):
175
+ return {"_nodes": self._nodes}
176
+
177
+ def __setstate__(self, state):
178
+ self._nodes = state["_nodes"]
179
+
180
+ def __init__(self, graph):
181
+ self._nodes = graph._node
182
+
183
+ # Mapping methods
184
+ def __len__(self):
185
+ return len(self._nodes)
186
+
187
+ def __iter__(self):
188
+ return iter(self._nodes)
189
+
190
+ def __getitem__(self, n):
191
+ if isinstance(n, slice):
192
+ raise nx.NetworkXError(
193
+ f"{type(self).__name__} does not support slicing, "
194
+ f"try list(G.nodes)[{n.start}:{n.stop}:{n.step}]"
195
+ )
196
+ return self._nodes[n]
197
+
198
+ # Set methods
199
+ def __contains__(self, n):
200
+ return n in self._nodes
201
+
202
+ @classmethod
203
+ def _from_iterable(cls, it):
204
+ return set(it)
205
+
206
+ # DataView method
207
+ def __call__(self, data=False, default=None):
208
+ if data is False:
209
+ return self
210
+ return NodeDataView(self._nodes, data, default)
211
+
212
+ def data(self, data=True, default=None):
213
+ """
214
+ Return a read-only view of node data.
215
+
216
+ Parameters
217
+ ----------
218
+ data : bool or node data key, default=True
219
+ If ``data=True`` (the default), return a `NodeDataView` object that
220
+ maps each node to *all* of its attributes. `data` may also be an
221
+ arbitrary key, in which case the `NodeDataView` maps each node to
222
+ the value for the keyed attribute. In this case, if a node does
223
+ not have the `data` attribute, the `default` value is used.
224
+ default : object, default=None
225
+ The value used when a node does not have a specific attribute.
226
+
227
+ Returns
228
+ -------
229
+ NodeDataView
230
+ The layout of the returned NodeDataView depends on the value of the
231
+ `data` parameter.
232
+
233
+ Notes
234
+ -----
235
+ If ``data=False``, returns a `NodeView` object without data.
236
+
237
+ See Also
238
+ --------
239
+ NodeDataView
240
+
241
+ Examples
242
+ --------
243
+ >>> G = nx.Graph()
244
+ >>> G.add_nodes_from(
245
+ ... [
246
+ ... (0, {"color": "red", "weight": 10}),
247
+ ... (1, {"color": "blue"}),
248
+ ... (2, {"color": "yellow", "weight": 2}),
249
+ ... ]
250
+ ... )
251
+
252
+ Accessing node data with ``data=True`` (the default) returns a
253
+ NodeDataView mapping each node to all of its attributes:
254
+
255
+ >>> G.nodes.data()
256
+ NodeDataView({0: {'color': 'red', 'weight': 10}, 1: {'color': 'blue'}, 2: {'color': 'yellow', 'weight': 2}})
257
+
258
+ If `data` represents a key in the node attribute dict, a NodeDataView mapping
259
+ the nodes to the value for that specific key is returned:
260
+
261
+ >>> G.nodes.data("color")
262
+ NodeDataView({0: 'red', 1: 'blue', 2: 'yellow'}, data='color')
263
+
264
+ If a specific key is not found in an attribute dict, the value specified
265
+ by `default` is returned:
266
+
267
+ >>> G.nodes.data("weight", default=-999)
268
+ NodeDataView({0: 10, 1: -999, 2: 2}, data='weight')
269
+
270
+ Note that there is no check that the `data` key is in any of the
271
+ node attribute dictionaries:
272
+
273
+ >>> G.nodes.data("height")
274
+ NodeDataView({0: None, 1: None, 2: None}, data='height')
275
+ """
276
+ if data is False:
277
+ return self
278
+ return NodeDataView(self._nodes, data, default)
279
+
280
+ def __str__(self):
281
+ return str(list(self))
282
+
283
+ def __repr__(self):
284
+ return f"{self.__class__.__name__}({tuple(self)})"
285
+
286
+
287
+ class NodeDataView(Set):
288
+ """A DataView class for nodes of a NetworkX Graph
289
+
290
+ The main use for this class is to iterate through node-data pairs.
291
+ The data can be the entire data-dictionary for each node, or it
292
+ can be a specific attribute (with default) for each node.
293
+ Set operations are enabled with NodeDataView, but don't work in
294
+ cases where the data is not hashable. Use with caution.
295
+ Typically, set operations on nodes use NodeView, not NodeDataView.
296
+ That is, they use `G.nodes` instead of `G.nodes(data='foo')`.
297
+
298
+ Parameters
299
+ ==========
300
+ graph : NetworkX graph-like class
301
+ data : bool or string (default=False)
302
+ default : object (default=None)
303
+ """
304
+
305
+ __slots__ = ("_nodes", "_data", "_default")
306
+
307
+ def __getstate__(self):
308
+ return {"_nodes": self._nodes, "_data": self._data, "_default": self._default}
309
+
310
+ def __setstate__(self, state):
311
+ self._nodes = state["_nodes"]
312
+ self._data = state["_data"]
313
+ self._default = state["_default"]
314
+
315
+ def __init__(self, nodedict, data=False, default=None):
316
+ self._nodes = nodedict
317
+ self._data = data
318
+ self._default = default
319
+
320
+ @classmethod
321
+ def _from_iterable(cls, it):
322
+ try:
323
+ return set(it)
324
+ except TypeError as err:
325
+ if "unhashable" in str(err):
326
+ msg = " : Could be b/c data=True or your values are unhashable"
327
+ raise TypeError(str(err) + msg) from err
328
+ raise
329
+
330
+ def __len__(self):
331
+ return len(self._nodes)
332
+
333
+ def __iter__(self):
334
+ data = self._data
335
+ if data is False:
336
+ return iter(self._nodes)
337
+ if data is True:
338
+ return iter(self._nodes.items())
339
+ return (
340
+ (n, dd[data] if data in dd else self._default)
341
+ for n, dd in self._nodes.items()
342
+ )
343
+
344
+ def __contains__(self, n):
345
+ try:
346
+ node_in = n in self._nodes
347
+ except TypeError:
348
+ n, d = n
349
+ return n in self._nodes and self[n] == d
350
+ if node_in is True:
351
+ return node_in
352
+ try:
353
+ n, d = n
354
+ except (TypeError, ValueError):
355
+ return False
356
+ return n in self._nodes and self[n] == d
357
+
358
+ def __getitem__(self, n):
359
+ if isinstance(n, slice):
360
+ raise nx.NetworkXError(
361
+ f"{type(self).__name__} does not support slicing, "
362
+ f"try list(G.nodes.data())[{n.start}:{n.stop}:{n.step}]"
363
+ )
364
+ ddict = self._nodes[n]
365
+ data = self._data
366
+ if data is False or data is True:
367
+ return ddict
368
+ return ddict[data] if data in ddict else self._default
369
+
370
+ def __str__(self):
371
+ return str(list(self))
372
+
373
+ def __repr__(self):
374
+ name = self.__class__.__name__
375
+ if self._data is False:
376
+ return f"{name}({tuple(self)})"
377
+ if self._data is True:
378
+ return f"{name}({dict(self)})"
379
+ return f"{name}({dict(self)}, data={self._data!r})"
380
+
381
+
382
+ # DegreeViews
383
+ class DiDegreeView:
384
+ """A View class for degree of nodes in a NetworkX Graph
385
+
386
+ The functionality is like dict.items() with (node, degree) pairs.
387
+ Additional functionality includes read-only lookup of node degree,
388
+ and calling with optional features nbunch (for only a subset of nodes)
389
+ and weight (use edge weights to compute degree).
390
+
391
+ Parameters
392
+ ==========
393
+ graph : NetworkX graph-like class
394
+ nbunch : node, container of nodes, or None meaning all nodes (default=None)
395
+ weight : bool or string (default=None)
396
+
397
+ Notes
398
+ -----
399
+ DegreeView can still lookup any node even if nbunch is specified.
400
+
401
+ Examples
402
+ --------
403
+ >>> G = nx.path_graph(3)
404
+ >>> DV = G.degree()
405
+ >>> assert DV[2] == 1
406
+ >>> assert sum(deg for n, deg in DV) == 4
407
+
408
+ >>> DVweight = G.degree(weight="span")
409
+ >>> G.add_edge(1, 2, span=34)
410
+ >>> DVweight[2]
411
+ 34
412
+ >>> DVweight[0] # default edge weight is 1
413
+ 1
414
+ >>> sum(span for n, span in DVweight) # sum weighted degrees
415
+ 70
416
+
417
+ >>> DVnbunch = G.degree(nbunch=(1, 2))
418
+ >>> assert len(list(DVnbunch)) == 2 # iteration over nbunch only
419
+ """
420
+
421
+ def __init__(self, G, nbunch=None, weight=None):
422
+ self._graph = G
423
+ self._succ = G._succ if hasattr(G, "_succ") else G._adj
424
+ self._pred = G._pred if hasattr(G, "_pred") else G._adj
425
+ self._nodes = self._succ if nbunch is None else list(G.nbunch_iter(nbunch))
426
+ self._weight = weight
427
+
428
+ def __call__(self, nbunch=None, weight=None):
429
+ if nbunch is None:
430
+ if weight == self._weight:
431
+ return self
432
+ return self.__class__(self._graph, None, weight)
433
+ try:
434
+ if nbunch in self._nodes:
435
+ if weight == self._weight:
436
+ return self[nbunch]
437
+ return self.__class__(self._graph, None, weight)[nbunch]
438
+ except TypeError:
439
+ pass
440
+ return self.__class__(self._graph, nbunch, weight)
441
+
442
+ def __getitem__(self, n):
443
+ weight = self._weight
444
+ succs = self._succ[n]
445
+ preds = self._pred[n]
446
+ if weight is None:
447
+ return len(succs) + len(preds)
448
+ return sum(dd.get(weight, 1) for dd in succs.values()) + sum(
449
+ dd.get(weight, 1) for dd in preds.values()
450
+ )
451
+
452
+ def __iter__(self):
453
+ weight = self._weight
454
+ if weight is None:
455
+ for n in self._nodes:
456
+ succs = self._succ[n]
457
+ preds = self._pred[n]
458
+ yield (n, len(succs) + len(preds))
459
+ else:
460
+ for n in self._nodes:
461
+ succs = self._succ[n]
462
+ preds = self._pred[n]
463
+ deg = sum(dd.get(weight, 1) for dd in succs.values()) + sum(
464
+ dd.get(weight, 1) for dd in preds.values()
465
+ )
466
+ yield (n, deg)
467
+
468
+ def __len__(self):
469
+ return len(self._nodes)
470
+
471
+ def __str__(self):
472
+ return str(list(self))
473
+
474
+ def __repr__(self):
475
+ return f"{self.__class__.__name__}({dict(self)})"
476
+
477
+
478
+ class DegreeView(DiDegreeView):
479
+ """A DegreeView class to act as G.degree for a NetworkX Graph
480
+
481
+ Typical usage focuses on iteration over `(node, degree)` pairs.
482
+ The degree is by default the number of edges incident to the node.
483
+ Optional argument `weight` enables weighted degree using the edge
484
+ attribute named in the `weight` argument. Reporting and iteration
485
+ can also be restricted to a subset of nodes using `nbunch`.
486
+
487
+ Additional functionality include node lookup so that `G.degree[n]`
488
+ reported the (possibly weighted) degree of node `n`. Calling the
489
+ view creates a view with different arguments `nbunch` or `weight`.
490
+
491
+ Parameters
492
+ ==========
493
+ graph : NetworkX graph-like class
494
+ nbunch : node, container of nodes, or None meaning all nodes (default=None)
495
+ weight : string or None (default=None)
496
+
497
+ Notes
498
+ -----
499
+ DegreeView can still lookup any node even if nbunch is specified.
500
+
501
+ Examples
502
+ --------
503
+ >>> G = nx.path_graph(3)
504
+ >>> DV = G.degree()
505
+ >>> assert DV[2] == 1
506
+ >>> assert G.degree[2] == 1
507
+ >>> assert sum(deg for n, deg in DV) == 4
508
+
509
+ >>> DVweight = G.degree(weight="span")
510
+ >>> G.add_edge(1, 2, span=34)
511
+ >>> DVweight[2]
512
+ 34
513
+ >>> DVweight[0] # default edge weight is 1
514
+ 1
515
+ >>> sum(span for n, span in DVweight) # sum weighted degrees
516
+ 70
517
+
518
+ >>> DVnbunch = G.degree(nbunch=(1, 2))
519
+ >>> assert len(list(DVnbunch)) == 2 # iteration over nbunch only
520
+ """
521
+
522
+ def __getitem__(self, n):
523
+ weight = self._weight
524
+ nbrs = self._succ[n]
525
+ if weight is None:
526
+ return len(nbrs) + (n in nbrs)
527
+ return sum(dd.get(weight, 1) for dd in nbrs.values()) + (
528
+ n in nbrs and nbrs[n].get(weight, 1)
529
+ )
530
+
531
+ def __iter__(self):
532
+ weight = self._weight
533
+ if weight is None:
534
+ for n in self._nodes:
535
+ nbrs = self._succ[n]
536
+ yield (n, len(nbrs) + (n in nbrs))
537
+ else:
538
+ for n in self._nodes:
539
+ nbrs = self._succ[n]
540
+ deg = sum(dd.get(weight, 1) for dd in nbrs.values()) + (
541
+ n in nbrs and nbrs[n].get(weight, 1)
542
+ )
543
+ yield (n, deg)
544
+
545
+
546
+ class OutDegreeView(DiDegreeView):
547
+ """A DegreeView class to report out_degree for a DiGraph; See DegreeView"""
548
+
549
+ def __getitem__(self, n):
550
+ weight = self._weight
551
+ nbrs = self._succ[n]
552
+ if self._weight is None:
553
+ return len(nbrs)
554
+ return sum(dd.get(self._weight, 1) for dd in nbrs.values())
555
+
556
+ def __iter__(self):
557
+ weight = self._weight
558
+ if weight is None:
559
+ for n in self._nodes:
560
+ succs = self._succ[n]
561
+ yield (n, len(succs))
562
+ else:
563
+ for n in self._nodes:
564
+ succs = self._succ[n]
565
+ deg = sum(dd.get(weight, 1) for dd in succs.values())
566
+ yield (n, deg)
567
+
568
+
569
+ class InDegreeView(DiDegreeView):
570
+ """A DegreeView class to report in_degree for a DiGraph; See DegreeView"""
571
+
572
+ def __getitem__(self, n):
573
+ weight = self._weight
574
+ nbrs = self._pred[n]
575
+ if weight is None:
576
+ return len(nbrs)
577
+ return sum(dd.get(weight, 1) for dd in nbrs.values())
578
+
579
+ def __iter__(self):
580
+ weight = self._weight
581
+ if weight is None:
582
+ for n in self._nodes:
583
+ preds = self._pred[n]
584
+ yield (n, len(preds))
585
+ else:
586
+ for n in self._nodes:
587
+ preds = self._pred[n]
588
+ deg = sum(dd.get(weight, 1) for dd in preds.values())
589
+ yield (n, deg)
590
+
591
+
592
+ class MultiDegreeView(DiDegreeView):
593
+ """A DegreeView class for undirected multigraphs; See DegreeView"""
594
+
595
+ def __getitem__(self, n):
596
+ weight = self._weight
597
+ nbrs = self._succ[n]
598
+ if weight is None:
599
+ return sum(len(keys) for keys in nbrs.values()) + (
600
+ n in nbrs and len(nbrs[n])
601
+ )
602
+ # edge weighted graph - degree is sum of nbr edge weights
603
+ deg = sum(
604
+ d.get(weight, 1) for key_dict in nbrs.values() for d in key_dict.values()
605
+ )
606
+ if n in nbrs:
607
+ deg += sum(d.get(weight, 1) for d in nbrs[n].values())
608
+ return deg
609
+
610
+ def __iter__(self):
611
+ weight = self._weight
612
+ if weight is None:
613
+ for n in self._nodes:
614
+ nbrs = self._succ[n]
615
+ deg = sum(len(keys) for keys in nbrs.values()) + (
616
+ n in nbrs and len(nbrs[n])
617
+ )
618
+ yield (n, deg)
619
+ else:
620
+ for n in self._nodes:
621
+ nbrs = self._succ[n]
622
+ deg = sum(
623
+ d.get(weight, 1)
624
+ for key_dict in nbrs.values()
625
+ for d in key_dict.values()
626
+ )
627
+ if n in nbrs:
628
+ deg += sum(d.get(weight, 1) for d in nbrs[n].values())
629
+ yield (n, deg)
630
+
631
+
632
+ class DiMultiDegreeView(DiDegreeView):
633
+ """A DegreeView class for MultiDiGraph; See DegreeView"""
634
+
635
+ def __getitem__(self, n):
636
+ weight = self._weight
637
+ succs = self._succ[n]
638
+ preds = self._pred[n]
639
+ if weight is None:
640
+ return sum(len(keys) for keys in succs.values()) + sum(
641
+ len(keys) for keys in preds.values()
642
+ )
643
+ # edge weighted graph - degree is sum of nbr edge weights
644
+ deg = sum(
645
+ d.get(weight, 1) for key_dict in succs.values() for d in key_dict.values()
646
+ ) + sum(
647
+ d.get(weight, 1) for key_dict in preds.values() for d in key_dict.values()
648
+ )
649
+ return deg
650
+
651
+ def __iter__(self):
652
+ weight = self._weight
653
+ if weight is None:
654
+ for n in self._nodes:
655
+ succs = self._succ[n]
656
+ preds = self._pred[n]
657
+ deg = sum(len(keys) for keys in succs.values()) + sum(
658
+ len(keys) for keys in preds.values()
659
+ )
660
+ yield (n, deg)
661
+ else:
662
+ for n in self._nodes:
663
+ succs = self._succ[n]
664
+ preds = self._pred[n]
665
+ deg = sum(
666
+ d.get(weight, 1)
667
+ for key_dict in succs.values()
668
+ for d in key_dict.values()
669
+ ) + sum(
670
+ d.get(weight, 1)
671
+ for key_dict in preds.values()
672
+ for d in key_dict.values()
673
+ )
674
+ yield (n, deg)
675
+
676
+
677
+ class InMultiDegreeView(DiDegreeView):
678
+ """A DegreeView class for inward degree of MultiDiGraph; See DegreeView"""
679
+
680
+ def __getitem__(self, n):
681
+ weight = self._weight
682
+ nbrs = self._pred[n]
683
+ if weight is None:
684
+ return sum(len(data) for data in nbrs.values())
685
+ # edge weighted graph - degree is sum of nbr edge weights
686
+ return sum(
687
+ d.get(weight, 1) for key_dict in nbrs.values() for d in key_dict.values()
688
+ )
689
+
690
+ def __iter__(self):
691
+ weight = self._weight
692
+ if weight is None:
693
+ for n in self._nodes:
694
+ nbrs = self._pred[n]
695
+ deg = sum(len(data) for data in nbrs.values())
696
+ yield (n, deg)
697
+ else:
698
+ for n in self._nodes:
699
+ nbrs = self._pred[n]
700
+ deg = sum(
701
+ d.get(weight, 1)
702
+ for key_dict in nbrs.values()
703
+ for d in key_dict.values()
704
+ )
705
+ yield (n, deg)
706
+
707
+
708
+ class OutMultiDegreeView(DiDegreeView):
709
+ """A DegreeView class for outward degree of MultiDiGraph; See DegreeView"""
710
+
711
+ def __getitem__(self, n):
712
+ weight = self._weight
713
+ nbrs = self._succ[n]
714
+ if weight is None:
715
+ return sum(len(data) for data in nbrs.values())
716
+ # edge weighted graph - degree is sum of nbr edge weights
717
+ return sum(
718
+ d.get(weight, 1) for key_dict in nbrs.values() for d in key_dict.values()
719
+ )
720
+
721
+ def __iter__(self):
722
+ weight = self._weight
723
+ if weight is None:
724
+ for n in self._nodes:
725
+ nbrs = self._succ[n]
726
+ deg = sum(len(data) for data in nbrs.values())
727
+ yield (n, deg)
728
+ else:
729
+ for n in self._nodes:
730
+ nbrs = self._succ[n]
731
+ deg = sum(
732
+ d.get(weight, 1)
733
+ for key_dict in nbrs.values()
734
+ for d in key_dict.values()
735
+ )
736
+ yield (n, deg)
737
+
738
+
739
+ # A base class for all edge views. Ensures all edge view and edge data view
740
+ # objects/classes are captured by `isinstance(obj, EdgeViewABC)` and
741
+ # `issubclass(cls, EdgeViewABC)` respectively
742
+ class EdgeViewABC(ABC):
743
+ pass
744
+
745
+
746
+ # EdgeDataViews
747
+ class OutEdgeDataView(EdgeViewABC):
748
+ """EdgeDataView for outward edges of DiGraph; See EdgeDataView"""
749
+
750
+ __slots__ = (
751
+ "_viewer",
752
+ "_nbunch",
753
+ "_data",
754
+ "_default",
755
+ "_adjdict",
756
+ "_nodes_nbrs",
757
+ "_report",
758
+ )
759
+
760
+ def __getstate__(self):
761
+ return {
762
+ "viewer": self._viewer,
763
+ "nbunch": self._nbunch,
764
+ "data": self._data,
765
+ "default": self._default,
766
+ }
767
+
768
+ def __setstate__(self, state):
769
+ self.__init__(**state)
770
+
771
+ def __init__(self, viewer, nbunch=None, data=False, *, default=None):
772
+ self._viewer = viewer
773
+ adjdict = self._adjdict = viewer._adjdict
774
+ if nbunch is None:
775
+ self._nodes_nbrs = adjdict.items
776
+ else:
777
+ # dict retains order of nodes but acts like a set
778
+ nbunch = dict.fromkeys(viewer._graph.nbunch_iter(nbunch))
779
+ self._nodes_nbrs = lambda: [(n, adjdict[n]) for n in nbunch]
780
+ self._nbunch = nbunch
781
+ self._data = data
782
+ self._default = default
783
+ # Set _report based on data and default
784
+ if data is True:
785
+ self._report = lambda n, nbr, dd: (n, nbr, dd)
786
+ elif data is False:
787
+ self._report = lambda n, nbr, dd: (n, nbr)
788
+ else: # data is attribute name
789
+ self._report = (
790
+ lambda n, nbr, dd: (n, nbr, dd[data])
791
+ if data in dd
792
+ else (n, nbr, default)
793
+ )
794
+
795
+ def __len__(self):
796
+ return sum(len(nbrs) for n, nbrs in self._nodes_nbrs())
797
+
798
+ def __iter__(self):
799
+ return (
800
+ self._report(n, nbr, dd)
801
+ for n, nbrs in self._nodes_nbrs()
802
+ for nbr, dd in nbrs.items()
803
+ )
804
+
805
+ def __contains__(self, e):
806
+ u, v = e[:2]
807
+ if self._nbunch is not None and u not in self._nbunch:
808
+ return False # this edge doesn't start in nbunch
809
+ try:
810
+ ddict = self._adjdict[u][v]
811
+ except KeyError:
812
+ return False
813
+ return e == self._report(u, v, ddict)
814
+
815
+ def __str__(self):
816
+ return str(list(self))
817
+
818
+ def __repr__(self):
819
+ return f"{self.__class__.__name__}({list(self)})"
820
+
821
+
822
+ class EdgeDataView(OutEdgeDataView):
823
+ """A EdgeDataView class for edges of Graph
824
+
825
+ This view is primarily used to iterate over the edges reporting
826
+ edges as node-tuples with edge data optionally reported. The
827
+ argument `nbunch` allows restriction to edges incident to nodes
828
+ in that container/singleton. The default (nbunch=None)
829
+ reports all edges. The arguments `data` and `default` control
830
+ what edge data is reported. The default `data is False` reports
831
+ only node-tuples for each edge. If `data is True` the entire edge
832
+ data dict is returned. Otherwise `data` is assumed to hold the name
833
+ of the edge attribute to report with default `default` if that
834
+ edge attribute is not present.
835
+
836
+ Parameters
837
+ ----------
838
+ nbunch : container of nodes, node or None (default None)
839
+ data : False, True or string (default False)
840
+ default : default value (default None)
841
+
842
+ Examples
843
+ --------
844
+ >>> G = nx.path_graph(3)
845
+ >>> G.add_edge(1, 2, foo="bar")
846
+ >>> list(G.edges(data="foo", default="biz"))
847
+ [(0, 1, 'biz'), (1, 2, 'bar')]
848
+ >>> assert (0, 1, "biz") in G.edges(data="foo", default="biz")
849
+ """
850
+
851
+ __slots__ = ()
852
+
853
+ def __len__(self):
854
+ return sum(1 for e in self)
855
+
856
+ def __iter__(self):
857
+ seen = {}
858
+ for n, nbrs in self._nodes_nbrs():
859
+ for nbr, dd in nbrs.items():
860
+ if nbr not in seen:
861
+ yield self._report(n, nbr, dd)
862
+ seen[n] = 1
863
+ del seen
864
+
865
+ def __contains__(self, e):
866
+ u, v = e[:2]
867
+ if self._nbunch is not None and u not in self._nbunch and v not in self._nbunch:
868
+ return False # this edge doesn't start and it doesn't end in nbunch
869
+ try:
870
+ ddict = self._adjdict[u][v]
871
+ except KeyError:
872
+ return False
873
+ return e == self._report(u, v, ddict)
874
+
875
+
876
+ class InEdgeDataView(OutEdgeDataView):
877
+ """An EdgeDataView class for outward edges of DiGraph; See EdgeDataView"""
878
+
879
+ __slots__ = ()
880
+
881
+ def __iter__(self):
882
+ return (
883
+ self._report(nbr, n, dd)
884
+ for n, nbrs in self._nodes_nbrs()
885
+ for nbr, dd in nbrs.items()
886
+ )
887
+
888
+ def __contains__(self, e):
889
+ u, v = e[:2]
890
+ if self._nbunch is not None and v not in self._nbunch:
891
+ return False # this edge doesn't end in nbunch
892
+ try:
893
+ ddict = self._adjdict[v][u]
894
+ except KeyError:
895
+ return False
896
+ return e == self._report(u, v, ddict)
897
+
898
+
899
+ class OutMultiEdgeDataView(OutEdgeDataView):
900
+ """An EdgeDataView for outward edges of MultiDiGraph; See EdgeDataView"""
901
+
902
+ __slots__ = ("keys",)
903
+
904
+ def __getstate__(self):
905
+ return {
906
+ "viewer": self._viewer,
907
+ "nbunch": self._nbunch,
908
+ "keys": self.keys,
909
+ "data": self._data,
910
+ "default": self._default,
911
+ }
912
+
913
+ def __setstate__(self, state):
914
+ self.__init__(**state)
915
+
916
+ def __init__(self, viewer, nbunch=None, data=False, *, default=None, keys=False):
917
+ self._viewer = viewer
918
+ adjdict = self._adjdict = viewer._adjdict
919
+ self.keys = keys
920
+ if nbunch is None:
921
+ self._nodes_nbrs = adjdict.items
922
+ else:
923
+ # dict retains order of nodes but acts like a set
924
+ nbunch = dict.fromkeys(viewer._graph.nbunch_iter(nbunch))
925
+ self._nodes_nbrs = lambda: [(n, adjdict[n]) for n in nbunch]
926
+ self._nbunch = nbunch
927
+ self._data = data
928
+ self._default = default
929
+ # Set _report based on data and default
930
+ if data is True:
931
+ if keys is True:
932
+ self._report = lambda n, nbr, k, dd: (n, nbr, k, dd)
933
+ else:
934
+ self._report = lambda n, nbr, k, dd: (n, nbr, dd)
935
+ elif data is False:
936
+ if keys is True:
937
+ self._report = lambda n, nbr, k, dd: (n, nbr, k)
938
+ else:
939
+ self._report = lambda n, nbr, k, dd: (n, nbr)
940
+ else: # data is attribute name
941
+ if keys is True:
942
+ self._report = (
943
+ lambda n, nbr, k, dd: (n, nbr, k, dd[data])
944
+ if data in dd
945
+ else (n, nbr, k, default)
946
+ )
947
+ else:
948
+ self._report = (
949
+ lambda n, nbr, k, dd: (n, nbr, dd[data])
950
+ if data in dd
951
+ else (n, nbr, default)
952
+ )
953
+
954
+ def __len__(self):
955
+ return sum(1 for e in self)
956
+
957
+ def __iter__(self):
958
+ return (
959
+ self._report(n, nbr, k, dd)
960
+ for n, nbrs in self._nodes_nbrs()
961
+ for nbr, kd in nbrs.items()
962
+ for k, dd in kd.items()
963
+ )
964
+
965
+ def __contains__(self, e):
966
+ u, v = e[:2]
967
+ if self._nbunch is not None and u not in self._nbunch:
968
+ return False # this edge doesn't start in nbunch
969
+ try:
970
+ kdict = self._adjdict[u][v]
971
+ except KeyError:
972
+ return False
973
+ if self.keys is True:
974
+ k = e[2]
975
+ try:
976
+ dd = kdict[k]
977
+ except KeyError:
978
+ return False
979
+ return e == self._report(u, v, k, dd)
980
+ return any(e == self._report(u, v, k, dd) for k, dd in kdict.items())
981
+
982
+
983
+ class MultiEdgeDataView(OutMultiEdgeDataView):
984
+ """An EdgeDataView class for edges of MultiGraph; See EdgeDataView"""
985
+
986
+ __slots__ = ()
987
+
988
+ def __iter__(self):
989
+ seen = {}
990
+ for n, nbrs in self._nodes_nbrs():
991
+ for nbr, kd in nbrs.items():
992
+ if nbr not in seen:
993
+ for k, dd in kd.items():
994
+ yield self._report(n, nbr, k, dd)
995
+ seen[n] = 1
996
+ del seen
997
+
998
+ def __contains__(self, e):
999
+ u, v = e[:2]
1000
+ if self._nbunch is not None and u not in self._nbunch and v not in self._nbunch:
1001
+ return False # this edge doesn't start and doesn't end in nbunch
1002
+ try:
1003
+ kdict = self._adjdict[u][v]
1004
+ except KeyError:
1005
+ try:
1006
+ kdict = self._adjdict[v][u]
1007
+ except KeyError:
1008
+ return False
1009
+ if self.keys is True:
1010
+ k = e[2]
1011
+ try:
1012
+ dd = kdict[k]
1013
+ except KeyError:
1014
+ return False
1015
+ return e == self._report(u, v, k, dd)
1016
+ return any(e == self._report(u, v, k, dd) for k, dd in kdict.items())
1017
+
1018
+
1019
+ class InMultiEdgeDataView(OutMultiEdgeDataView):
1020
+ """An EdgeDataView for inward edges of MultiDiGraph; See EdgeDataView"""
1021
+
1022
+ __slots__ = ()
1023
+
1024
+ def __iter__(self):
1025
+ return (
1026
+ self._report(nbr, n, k, dd)
1027
+ for n, nbrs in self._nodes_nbrs()
1028
+ for nbr, kd in nbrs.items()
1029
+ for k, dd in kd.items()
1030
+ )
1031
+
1032
+ def __contains__(self, e):
1033
+ u, v = e[:2]
1034
+ if self._nbunch is not None and v not in self._nbunch:
1035
+ return False # this edge doesn't end in nbunch
1036
+ try:
1037
+ kdict = self._adjdict[v][u]
1038
+ except KeyError:
1039
+ return False
1040
+ if self.keys is True:
1041
+ k = e[2]
1042
+ dd = kdict[k]
1043
+ return e == self._report(u, v, k, dd)
1044
+ return any(e == self._report(u, v, k, dd) for k, dd in kdict.items())
1045
+
1046
+
1047
+ # EdgeViews have set operations and no data reported
1048
+ class OutEdgeView(Set, Mapping, EdgeViewABC):
1049
+ """A EdgeView class for outward edges of a DiGraph"""
1050
+
1051
+ __slots__ = ("_adjdict", "_graph", "_nodes_nbrs")
1052
+
1053
+ def __getstate__(self):
1054
+ return {"_graph": self._graph, "_adjdict": self._adjdict}
1055
+
1056
+ def __setstate__(self, state):
1057
+ self._graph = state["_graph"]
1058
+ self._adjdict = state["_adjdict"]
1059
+ self._nodes_nbrs = self._adjdict.items
1060
+
1061
+ @classmethod
1062
+ def _from_iterable(cls, it):
1063
+ return set(it)
1064
+
1065
+ dataview = OutEdgeDataView
1066
+
1067
+ def __init__(self, G):
1068
+ self._graph = G
1069
+ self._adjdict = G._succ if hasattr(G, "succ") else G._adj
1070
+ self._nodes_nbrs = self._adjdict.items
1071
+
1072
+ # Set methods
1073
+ def __len__(self):
1074
+ return sum(len(nbrs) for n, nbrs in self._nodes_nbrs())
1075
+
1076
+ def __iter__(self):
1077
+ for n, nbrs in self._nodes_nbrs():
1078
+ for nbr in nbrs:
1079
+ yield (n, nbr)
1080
+
1081
+ def __contains__(self, e):
1082
+ try:
1083
+ u, v = e
1084
+ return v in self._adjdict[u]
1085
+ except KeyError:
1086
+ return False
1087
+
1088
+ # Mapping Methods
1089
+ def __getitem__(self, e):
1090
+ if isinstance(e, slice):
1091
+ raise nx.NetworkXError(
1092
+ f"{type(self).__name__} does not support slicing, "
1093
+ f"try list(G.edges)[{e.start}:{e.stop}:{e.step}]"
1094
+ )
1095
+ u, v = e
1096
+ try:
1097
+ return self._adjdict[u][v]
1098
+ except KeyError as ex: # Customize msg to indicate exception origin
1099
+ raise KeyError(f"The edge {e} is not in the graph.")
1100
+
1101
+ # EdgeDataView methods
1102
+ def __call__(self, nbunch=None, data=False, *, default=None):
1103
+ if nbunch is None and data is False:
1104
+ return self
1105
+ return self.dataview(self, nbunch, data, default=default)
1106
+
1107
+ def data(self, data=True, default=None, nbunch=None):
1108
+ """
1109
+ Return a read-only view of edge data.
1110
+
1111
+ Parameters
1112
+ ----------
1113
+ data : bool or edge attribute key
1114
+ If ``data=True``, then the data view maps each edge to a dictionary
1115
+ containing all of its attributes. If `data` is a key in the edge
1116
+ dictionary, then the data view maps each edge to its value for
1117
+ the keyed attribute. In this case, if the edge doesn't have the
1118
+ attribute, the `default` value is returned.
1119
+ default : object, default=None
1120
+ The value used when an edge does not have a specific attribute
1121
+ nbunch : container of nodes, optional (default=None)
1122
+ Allows restriction to edges only involving certain nodes. All edges
1123
+ are considered by default.
1124
+
1125
+ Returns
1126
+ -------
1127
+ dataview
1128
+ Returns an `EdgeDataView` for undirected Graphs, `OutEdgeDataView`
1129
+ for DiGraphs, `MultiEdgeDataView` for MultiGraphs and
1130
+ `OutMultiEdgeDataView` for MultiDiGraphs.
1131
+
1132
+ Notes
1133
+ -----
1134
+ If ``data=False``, returns an `EdgeView` without any edge data.
1135
+
1136
+ See Also
1137
+ --------
1138
+ EdgeDataView
1139
+ OutEdgeDataView
1140
+ MultiEdgeDataView
1141
+ OutMultiEdgeDataView
1142
+
1143
+ Examples
1144
+ --------
1145
+ >>> G = nx.Graph()
1146
+ >>> G.add_edges_from(
1147
+ ... [
1148
+ ... (0, 1, {"dist": 3, "capacity": 20}),
1149
+ ... (1, 2, {"dist": 4}),
1150
+ ... (2, 0, {"dist": 5}),
1151
+ ... ]
1152
+ ... )
1153
+
1154
+ Accessing edge data with ``data=True`` (the default) returns an
1155
+ edge data view object listing each edge with all of its attributes:
1156
+
1157
+ >>> G.edges.data()
1158
+ EdgeDataView([(0, 1, {'dist': 3, 'capacity': 20}), (0, 2, {'dist': 5}), (1, 2, {'dist': 4})])
1159
+
1160
+ If `data` represents a key in the edge attribute dict, a dataview listing
1161
+ each edge with its value for that specific key is returned:
1162
+
1163
+ >>> G.edges.data("dist")
1164
+ EdgeDataView([(0, 1, 3), (0, 2, 5), (1, 2, 4)])
1165
+
1166
+ `nbunch` can be used to limit the edges:
1167
+
1168
+ >>> G.edges.data("dist", nbunch=[0])
1169
+ EdgeDataView([(0, 1, 3), (0, 2, 5)])
1170
+
1171
+ If a specific key is not found in an edge attribute dict, the value
1172
+ specified by `default` is used:
1173
+
1174
+ >>> G.edges.data("capacity")
1175
+ EdgeDataView([(0, 1, 20), (0, 2, None), (1, 2, None)])
1176
+
1177
+ Note that there is no check that the `data` key is present in any of
1178
+ the edge attribute dictionaries:
1179
+
1180
+ >>> G.edges.data("speed")
1181
+ EdgeDataView([(0, 1, None), (0, 2, None), (1, 2, None)])
1182
+ """
1183
+ if nbunch is None and data is False:
1184
+ return self
1185
+ return self.dataview(self, nbunch, data, default=default)
1186
+
1187
+ # String Methods
1188
+ def __str__(self):
1189
+ return str(list(self))
1190
+
1191
+ def __repr__(self):
1192
+ return f"{self.__class__.__name__}({list(self)})"
1193
+
1194
+
1195
+ class EdgeView(OutEdgeView):
1196
+ """A EdgeView class for edges of a Graph
1197
+
1198
+ This densely packed View allows iteration over edges, data lookup
1199
+ like a dict and set operations on edges represented by node-tuples.
1200
+ In addition, edge data can be controlled by calling this object
1201
+ possibly creating an EdgeDataView. Typically edges are iterated over
1202
+ and reported as `(u, v)` node tuples or `(u, v, key)` node/key tuples
1203
+ for multigraphs. Those edge representations can also be using to
1204
+ lookup the data dict for any edge. Set operations also are available
1205
+ where those tuples are the elements of the set.
1206
+ Calling this object with optional arguments `data`, `default` and `keys`
1207
+ controls the form of the tuple (see EdgeDataView). Optional argument
1208
+ `nbunch` allows restriction to edges only involving certain nodes.
1209
+
1210
+ If `data is False` (the default) then iterate over 2-tuples `(u, v)`.
1211
+ If `data is True` iterate over 3-tuples `(u, v, datadict)`.
1212
+ Otherwise iterate over `(u, v, datadict.get(data, default))`.
1213
+ For Multigraphs, if `keys is True`, replace `u, v` with `u, v, key` above.
1214
+
1215
+ Parameters
1216
+ ==========
1217
+ graph : NetworkX graph-like class
1218
+ nbunch : (default= all nodes in graph) only report edges with these nodes
1219
+ keys : (only for MultiGraph. default=False) report edge key in tuple
1220
+ data : bool or string (default=False) see above
1221
+ default : object (default=None)
1222
+
1223
+ Examples
1224
+ ========
1225
+ >>> G = nx.path_graph(4)
1226
+ >>> EV = G.edges()
1227
+ >>> (2, 3) in EV
1228
+ True
1229
+ >>> for u, v in EV:
1230
+ ... print((u, v))
1231
+ (0, 1)
1232
+ (1, 2)
1233
+ (2, 3)
1234
+ >>> assert EV & {(1, 2), (3, 4)} == {(1, 2)}
1235
+
1236
+ >>> EVdata = G.edges(data="color", default="aqua")
1237
+ >>> G.add_edge(2, 3, color="blue")
1238
+ >>> assert (2, 3, "blue") in EVdata
1239
+ >>> for u, v, c in EVdata:
1240
+ ... print(f"({u}, {v}) has color: {c}")
1241
+ (0, 1) has color: aqua
1242
+ (1, 2) has color: aqua
1243
+ (2, 3) has color: blue
1244
+
1245
+ >>> EVnbunch = G.edges(nbunch=2)
1246
+ >>> assert (2, 3) in EVnbunch
1247
+ >>> assert (0, 1) not in EVnbunch
1248
+ >>> for u, v in EVnbunch:
1249
+ ... assert u == 2 or v == 2
1250
+
1251
+ >>> MG = nx.path_graph(4, create_using=nx.MultiGraph)
1252
+ >>> EVmulti = MG.edges(keys=True)
1253
+ >>> (2, 3, 0) in EVmulti
1254
+ True
1255
+ >>> (2, 3) in EVmulti # 2-tuples work even when keys is True
1256
+ True
1257
+ >>> key = MG.add_edge(2, 3)
1258
+ >>> for u, v, k in EVmulti:
1259
+ ... print((u, v, k))
1260
+ (0, 1, 0)
1261
+ (1, 2, 0)
1262
+ (2, 3, 0)
1263
+ (2, 3, 1)
1264
+ """
1265
+
1266
+ __slots__ = ()
1267
+
1268
+ dataview = EdgeDataView
1269
+
1270
+ def __len__(self):
1271
+ num_nbrs = (len(nbrs) + (n in nbrs) for n, nbrs in self._nodes_nbrs())
1272
+ return sum(num_nbrs) // 2
1273
+
1274
+ def __iter__(self):
1275
+ seen = {}
1276
+ for n, nbrs in self._nodes_nbrs():
1277
+ for nbr in list(nbrs):
1278
+ if nbr not in seen:
1279
+ yield (n, nbr)
1280
+ seen[n] = 1
1281
+ del seen
1282
+
1283
+ def __contains__(self, e):
1284
+ try:
1285
+ u, v = e[:2]
1286
+ return v in self._adjdict[u] or u in self._adjdict[v]
1287
+ except (KeyError, ValueError):
1288
+ return False
1289
+
1290
+
1291
+ class InEdgeView(OutEdgeView):
1292
+ """A EdgeView class for inward edges of a DiGraph"""
1293
+
1294
+ __slots__ = ()
1295
+
1296
+ def __setstate__(self, state):
1297
+ self._graph = state["_graph"]
1298
+ self._adjdict = state["_adjdict"]
1299
+ self._nodes_nbrs = self._adjdict.items
1300
+
1301
+ dataview = InEdgeDataView
1302
+
1303
+ def __init__(self, G):
1304
+ self._graph = G
1305
+ self._adjdict = G._pred if hasattr(G, "pred") else G._adj
1306
+ self._nodes_nbrs = self._adjdict.items
1307
+
1308
+ def __iter__(self):
1309
+ for n, nbrs in self._nodes_nbrs():
1310
+ for nbr in nbrs:
1311
+ yield (nbr, n)
1312
+
1313
+ def __contains__(self, e):
1314
+ try:
1315
+ u, v = e
1316
+ return u in self._adjdict[v]
1317
+ except KeyError:
1318
+ return False
1319
+
1320
+ def __getitem__(self, e):
1321
+ if isinstance(e, slice):
1322
+ raise nx.NetworkXError(
1323
+ f"{type(self).__name__} does not support slicing, "
1324
+ f"try list(G.in_edges)[{e.start}:{e.stop}:{e.step}]"
1325
+ )
1326
+ u, v = e
1327
+ return self._adjdict[v][u]
1328
+
1329
+
1330
+ class OutMultiEdgeView(OutEdgeView):
1331
+ """A EdgeView class for outward edges of a MultiDiGraph"""
1332
+
1333
+ __slots__ = ()
1334
+
1335
+ dataview = OutMultiEdgeDataView
1336
+
1337
+ def __len__(self):
1338
+ return sum(
1339
+ len(kdict) for n, nbrs in self._nodes_nbrs() for nbr, kdict in nbrs.items()
1340
+ )
1341
+
1342
+ def __iter__(self):
1343
+ for n, nbrs in self._nodes_nbrs():
1344
+ for nbr, kdict in nbrs.items():
1345
+ for key in kdict:
1346
+ yield (n, nbr, key)
1347
+
1348
+ def __contains__(self, e):
1349
+ N = len(e)
1350
+ if N == 3:
1351
+ u, v, k = e
1352
+ elif N == 2:
1353
+ u, v = e
1354
+ k = 0
1355
+ else:
1356
+ raise ValueError("MultiEdge must have length 2 or 3")
1357
+ try:
1358
+ return k in self._adjdict[u][v]
1359
+ except KeyError:
1360
+ return False
1361
+
1362
+ def __getitem__(self, e):
1363
+ if isinstance(e, slice):
1364
+ raise nx.NetworkXError(
1365
+ f"{type(self).__name__} does not support slicing, "
1366
+ f"try list(G.edges)[{e.start}:{e.stop}:{e.step}]"
1367
+ )
1368
+ u, v, k = e
1369
+ return self._adjdict[u][v][k]
1370
+
1371
+ def __call__(self, nbunch=None, data=False, *, default=None, keys=False):
1372
+ if nbunch is None and data is False and keys is True:
1373
+ return self
1374
+ return self.dataview(self, nbunch, data, default=default, keys=keys)
1375
+
1376
+ def data(self, data=True, default=None, nbunch=None, keys=False):
1377
+ if nbunch is None and data is False and keys is True:
1378
+ return self
1379
+ return self.dataview(self, nbunch, data, default=default, keys=keys)
1380
+
1381
+
1382
+ class MultiEdgeView(OutMultiEdgeView):
1383
+ """A EdgeView class for edges of a MultiGraph"""
1384
+
1385
+ __slots__ = ()
1386
+
1387
+ dataview = MultiEdgeDataView
1388
+
1389
+ def __len__(self):
1390
+ return sum(1 for e in self)
1391
+
1392
+ def __iter__(self):
1393
+ seen = {}
1394
+ for n, nbrs in self._nodes_nbrs():
1395
+ for nbr, kd in nbrs.items():
1396
+ if nbr not in seen:
1397
+ for k, dd in kd.items():
1398
+ yield (n, nbr, k)
1399
+ seen[n] = 1
1400
+ del seen
1401
+
1402
+
1403
+ class InMultiEdgeView(OutMultiEdgeView):
1404
+ """A EdgeView class for inward edges of a MultiDiGraph"""
1405
+
1406
+ __slots__ = ()
1407
+
1408
+ def __setstate__(self, state):
1409
+ self._graph = state["_graph"]
1410
+ self._adjdict = state["_adjdict"]
1411
+ self._nodes_nbrs = self._adjdict.items
1412
+
1413
+ dataview = InMultiEdgeDataView
1414
+
1415
+ def __init__(self, G):
1416
+ self._graph = G
1417
+ self._adjdict = G._pred if hasattr(G, "pred") else G._adj
1418
+ self._nodes_nbrs = self._adjdict.items
1419
+
1420
+ def __iter__(self):
1421
+ for n, nbrs in self._nodes_nbrs():
1422
+ for nbr, kdict in nbrs.items():
1423
+ for key in kdict:
1424
+ yield (nbr, n, key)
1425
+
1426
+ def __contains__(self, e):
1427
+ N = len(e)
1428
+ if N == 3:
1429
+ u, v, k = e
1430
+ elif N == 2:
1431
+ u, v = e
1432
+ k = 0
1433
+ else:
1434
+ raise ValueError("MultiEdge must have length 2 or 3")
1435
+ try:
1436
+ return k in self._adjdict[v][u]
1437
+ except KeyError:
1438
+ return False
1439
+
1440
+ def __getitem__(self, e):
1441
+ if isinstance(e, slice):
1442
+ raise nx.NetworkXError(
1443
+ f"{type(self).__name__} does not support slicing, "
1444
+ f"try list(G.in_edges)[{e.start}:{e.stop}:{e.step}]"
1445
+ )
1446
+ u, v, k = e
1447
+ return self._adjdict[v][u][k]
minigpt2/lib/python3.10/site-packages/networkx/classes/tests/__pycache__/test_filters.cpython-310.pyc ADDED
Binary file (5.03 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/classes/tests/__pycache__/test_function.cpython-310.pyc ADDED
Binary file (27.5 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/classes/tests/__pycache__/test_graphviews.cpython-310.pyc ADDED
Binary file (13.5 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/classes/tests/__pycache__/test_multigraph.cpython-310.pyc ADDED
Binary file (17.6 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/classes/tests/dispatch_interface.py ADDED
@@ -0,0 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # This file contains utilities for testing the dispatching feature
2
+
3
+ # A full test of all dispatchable algorithms is performed by
4
+ # modifying the pytest invocation and setting an environment variable
5
+ # NETWORKX_TEST_BACKEND=nx_loopback pytest
6
+ # This is comprehensive, but only tests the `test_override_dispatch`
7
+ # function in networkx.classes.backends.
8
+
9
+ # To test the `_dispatchable` function directly, several tests scattered throughout
10
+ # NetworkX have been augmented to test normal and dispatch mode.
11
+ # Searching for `dispatch_interface` should locate the specific tests.
12
+
13
+ import networkx as nx
14
+ from networkx import DiGraph, Graph, MultiDiGraph, MultiGraph, PlanarEmbedding
15
+ from networkx.classes.reportviews import NodeView
16
+
17
+
18
+ class LoopbackGraph(Graph):
19
+ __networkx_backend__ = "nx_loopback"
20
+
21
+
22
+ class LoopbackDiGraph(DiGraph):
23
+ __networkx_backend__ = "nx_loopback"
24
+
25
+
26
+ class LoopbackMultiGraph(MultiGraph):
27
+ __networkx_backend__ = "nx_loopback"
28
+
29
+
30
+ class LoopbackMultiDiGraph(MultiDiGraph):
31
+ __networkx_backend__ = "nx_loopback"
32
+
33
+
34
+ class LoopbackPlanarEmbedding(PlanarEmbedding):
35
+ __networkx_backend__ = "nx_loopback"
36
+
37
+
38
+ def convert(graph):
39
+ if isinstance(graph, PlanarEmbedding):
40
+ return LoopbackPlanarEmbedding(graph)
41
+ if isinstance(graph, MultiDiGraph):
42
+ return LoopbackMultiDiGraph(graph)
43
+ if isinstance(graph, MultiGraph):
44
+ return LoopbackMultiGraph(graph)
45
+ if isinstance(graph, DiGraph):
46
+ return LoopbackDiGraph(graph)
47
+ if isinstance(graph, Graph):
48
+ return LoopbackGraph(graph)
49
+ raise TypeError(f"Unsupported type of graph: {type(graph)}")
50
+
51
+
52
+ class LoopbackBackendInterface:
53
+ def __getattr__(self, item):
54
+ try:
55
+ return nx.utils.backends._registered_algorithms[item].orig_func
56
+ except KeyError:
57
+ raise AttributeError(item) from None
58
+
59
+ @staticmethod
60
+ def convert_from_nx(
61
+ graph,
62
+ *,
63
+ edge_attrs=None,
64
+ node_attrs=None,
65
+ preserve_edge_attrs=None,
66
+ preserve_node_attrs=None,
67
+ preserve_graph_attrs=None,
68
+ name=None,
69
+ graph_name=None,
70
+ ):
71
+ if name in {
72
+ # Raise if input graph changes. See test_dag.py::test_topological_sort6
73
+ "lexicographical_topological_sort",
74
+ "topological_generations",
75
+ "topological_sort",
76
+ # Would be nice to some day avoid these cutoffs of full testing
77
+ }:
78
+ return graph
79
+ if isinstance(graph, NodeView):
80
+ # Convert to a Graph with only nodes (no edges)
81
+ new_graph = Graph()
82
+ new_graph.add_nodes_from(graph.items())
83
+ graph = new_graph
84
+ G = LoopbackGraph()
85
+ elif not isinstance(graph, Graph):
86
+ raise TypeError(
87
+ f"Bad type for graph argument {graph_name} in {name}: {type(graph)}"
88
+ )
89
+ elif graph.__class__ in {Graph, LoopbackGraph}:
90
+ G = LoopbackGraph()
91
+ elif graph.__class__ in {DiGraph, LoopbackDiGraph}:
92
+ G = LoopbackDiGraph()
93
+ elif graph.__class__ in {MultiGraph, LoopbackMultiGraph}:
94
+ G = LoopbackMultiGraph()
95
+ elif graph.__class__ in {MultiDiGraph, LoopbackMultiDiGraph}:
96
+ G = LoopbackMultiDiGraph()
97
+ elif graph.__class__ in {PlanarEmbedding, LoopbackPlanarEmbedding}:
98
+ G = LoopbackDiGraph() # or LoopbackPlanarEmbedding
99
+ else:
100
+ # Would be nice to handle these better some day
101
+ # nx.algorithms.approximation.kcomponents._AntiGraph
102
+ # nx.classes.tests.test_multidigraph.MultiDiGraphSubClass
103
+ # nx.classes.tests.test_multigraph.MultiGraphSubClass
104
+ G = graph.__class__()
105
+
106
+ if preserve_graph_attrs:
107
+ G.graph.update(graph.graph)
108
+
109
+ # add nodes
110
+ G.add_nodes_from(graph)
111
+ if preserve_node_attrs:
112
+ for n, dd in G._node.items():
113
+ dd.update(graph.nodes[n])
114
+ elif node_attrs:
115
+ for n, dd in G._node.items():
116
+ dd.update(
117
+ (attr, graph._node[n].get(attr, default))
118
+ for attr, default in node_attrs.items()
119
+ if default is not None or attr in graph._node[n]
120
+ )
121
+
122
+ # tools to build datadict and keydict
123
+ if preserve_edge_attrs:
124
+
125
+ def G_new_datadict(old_dd):
126
+ return G.edge_attr_dict_factory(old_dd)
127
+ elif edge_attrs:
128
+
129
+ def G_new_datadict(old_dd):
130
+ return G.edge_attr_dict_factory(
131
+ (attr, old_dd.get(attr, default))
132
+ for attr, default in edge_attrs.items()
133
+ if default is not None or attr in old_dd
134
+ )
135
+ else:
136
+
137
+ def G_new_datadict(old_dd):
138
+ return G.edge_attr_dict_factory()
139
+
140
+ if G.is_multigraph():
141
+
142
+ def G_new_inner(keydict):
143
+ kd = G.adjlist_inner_dict_factory(
144
+ (k, G_new_datadict(dd)) for k, dd in keydict.items()
145
+ )
146
+ return kd
147
+ else:
148
+ G_new_inner = G_new_datadict
149
+
150
+ # add edges keeping the same order in _adj and _pred
151
+ G_adj = G._adj
152
+ if G.is_directed():
153
+ for n, nbrs in graph._adj.items():
154
+ G_adj[n].update((nbr, G_new_inner(dd)) for nbr, dd in nbrs.items())
155
+ # ensure same datadict for pred and adj; and pred order of graph._pred
156
+ G_pred = G._pred
157
+ for n, nbrs in graph._pred.items():
158
+ G_pred[n].update((nbr, G_adj[nbr][n]) for nbr in nbrs)
159
+ else: # undirected
160
+ for n, nbrs in graph._adj.items():
161
+ # ensure same datadict for both ways; and adj order of graph._adj
162
+ G_adj[n].update(
163
+ (nbr, G_adj[nbr][n] if n in G_adj[nbr] else G_new_inner(dd))
164
+ for nbr, dd in nbrs.items()
165
+ )
166
+
167
+ return G
168
+
169
+ @staticmethod
170
+ def convert_to_nx(obj, *, name=None):
171
+ return obj
172
+
173
+ @staticmethod
174
+ def on_start_tests(items):
175
+ # Verify that items can be xfailed
176
+ for item in items:
177
+ assert hasattr(item, "add_marker")
178
+
179
+ def can_run(self, name, args, kwargs):
180
+ # It is unnecessary to define this function if algorithms are fully supported.
181
+ # We include it for illustration purposes.
182
+ return hasattr(self, name)
183
+
184
+
185
+ backend_interface = LoopbackBackendInterface()
minigpt2/lib/python3.10/site-packages/networkx/classes/tests/historical_tests.py ADDED
@@ -0,0 +1,475 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Original NetworkX graph tests"""
2
+
3
+ import pytest
4
+
5
+ import networkx as nx
6
+ from networkx import convert_node_labels_to_integers as cnlti
7
+ from networkx.utils import edges_equal, nodes_equal
8
+
9
+
10
+ class HistoricalTests:
11
+ @classmethod
12
+ def setup_class(cls):
13
+ cls.null = nx.null_graph()
14
+ cls.P1 = cnlti(nx.path_graph(1), first_label=1)
15
+ cls.P3 = cnlti(nx.path_graph(3), first_label=1)
16
+ cls.P10 = cnlti(nx.path_graph(10), first_label=1)
17
+ cls.K1 = cnlti(nx.complete_graph(1), first_label=1)
18
+ cls.K3 = cnlti(nx.complete_graph(3), first_label=1)
19
+ cls.K4 = cnlti(nx.complete_graph(4), first_label=1)
20
+ cls.K5 = cnlti(nx.complete_graph(5), first_label=1)
21
+ cls.K10 = cnlti(nx.complete_graph(10), first_label=1)
22
+ cls.G = nx.Graph
23
+
24
+ def test_name(self):
25
+ G = self.G(name="test")
26
+ assert G.name == "test"
27
+ H = self.G()
28
+ assert H.name == ""
29
+
30
+ # Nodes
31
+
32
+ def test_add_remove_node(self):
33
+ G = self.G()
34
+ G.add_node("A")
35
+ assert G.has_node("A")
36
+ G.remove_node("A")
37
+ assert not G.has_node("A")
38
+
39
+ def test_nonhashable_node(self):
40
+ # Test if a non-hashable object is in the Graph. A python dict will
41
+ # raise a TypeError, but for a Graph class a simple False should be
42
+ # returned (see Graph __contains__). If it cannot be a node then it is
43
+ # not a node.
44
+ G = self.G()
45
+ assert not G.has_node(["A"])
46
+ assert not G.has_node({"A": 1})
47
+
48
+ def test_add_nodes_from(self):
49
+ G = self.G()
50
+ G.add_nodes_from(list("ABCDEFGHIJKL"))
51
+ assert G.has_node("L")
52
+ G.remove_nodes_from(["H", "I", "J", "K", "L"])
53
+ G.add_nodes_from([1, 2, 3, 4])
54
+ assert sorted(G.nodes(), key=str) == [
55
+ 1,
56
+ 2,
57
+ 3,
58
+ 4,
59
+ "A",
60
+ "B",
61
+ "C",
62
+ "D",
63
+ "E",
64
+ "F",
65
+ "G",
66
+ ]
67
+ # test __iter__
68
+ assert sorted(G, key=str) == [1, 2, 3, 4, "A", "B", "C", "D", "E", "F", "G"]
69
+
70
+ def test_contains(self):
71
+ G = self.G()
72
+ G.add_node("A")
73
+ assert "A" in G
74
+ assert [] not in G # never raise a Key or TypeError in this test
75
+ assert {1: 1} not in G
76
+
77
+ def test_add_remove(self):
78
+ # Test add_node and remove_node acting for various nbunch
79
+ G = self.G()
80
+ G.add_node("m")
81
+ assert G.has_node("m")
82
+ G.add_node("m") # no complaints
83
+ pytest.raises(nx.NetworkXError, G.remove_node, "j")
84
+ G.remove_node("m")
85
+ assert list(G) == []
86
+
87
+ def test_nbunch_is_list(self):
88
+ G = self.G()
89
+ G.add_nodes_from(list("ABCD"))
90
+ G.add_nodes_from(self.P3) # add nbunch of nodes (nbunch=Graph)
91
+ assert sorted(G.nodes(), key=str) == [1, 2, 3, "A", "B", "C", "D"]
92
+ G.remove_nodes_from(self.P3) # remove nbunch of nodes (nbunch=Graph)
93
+ assert sorted(G.nodes(), key=str) == ["A", "B", "C", "D"]
94
+
95
+ def test_nbunch_is_set(self):
96
+ G = self.G()
97
+ nbunch = set("ABCDEFGHIJKL")
98
+ G.add_nodes_from(nbunch)
99
+ assert G.has_node("L")
100
+
101
+ def test_nbunch_dict(self):
102
+ # nbunch is a dict with nodes as keys
103
+ G = self.G()
104
+ nbunch = set("ABCDEFGHIJKL")
105
+ G.add_nodes_from(nbunch)
106
+ nbunch = {"I": "foo", "J": 2, "K": True, "L": "spam"}
107
+ G.remove_nodes_from(nbunch)
108
+ assert sorted(G.nodes(), key=str), ["A", "B", "C", "D", "E", "F", "G", "H"]
109
+
110
+ def test_nbunch_iterator(self):
111
+ G = self.G()
112
+ G.add_nodes_from(["A", "B", "C", "D", "E", "F", "G", "H"])
113
+ n_iter = self.P3.nodes()
114
+ G.add_nodes_from(n_iter)
115
+ assert sorted(G.nodes(), key=str) == [
116
+ 1,
117
+ 2,
118
+ 3,
119
+ "A",
120
+ "B",
121
+ "C",
122
+ "D",
123
+ "E",
124
+ "F",
125
+ "G",
126
+ "H",
127
+ ]
128
+ n_iter = self.P3.nodes() # rebuild same iterator
129
+ G.remove_nodes_from(n_iter) # remove nbunch of nodes (nbunch=iterator)
130
+ assert sorted(G.nodes(), key=str) == ["A", "B", "C", "D", "E", "F", "G", "H"]
131
+
132
+ def test_nbunch_graph(self):
133
+ G = self.G()
134
+ G.add_nodes_from(["A", "B", "C", "D", "E", "F", "G", "H"])
135
+ nbunch = self.K3
136
+ G.add_nodes_from(nbunch)
137
+ assert sorted(G.nodes(), key=str), [
138
+ 1,
139
+ 2,
140
+ 3,
141
+ "A",
142
+ "B",
143
+ "C",
144
+ "D",
145
+ "E",
146
+ "F",
147
+ "G",
148
+ "H",
149
+ ]
150
+
151
+ # Edges
152
+
153
+ def test_add_edge(self):
154
+ G = self.G()
155
+ pytest.raises(TypeError, G.add_edge, "A")
156
+
157
+ G.add_edge("A", "B") # testing add_edge()
158
+ G.add_edge("A", "B") # should fail silently
159
+ assert G.has_edge("A", "B")
160
+ assert not G.has_edge("A", "C")
161
+ assert G.has_edge(*("A", "B"))
162
+ if G.is_directed():
163
+ assert not G.has_edge("B", "A")
164
+ else:
165
+ # G is undirected, so B->A is an edge
166
+ assert G.has_edge("B", "A")
167
+
168
+ G.add_edge("A", "C") # test directedness
169
+ G.add_edge("C", "A")
170
+ G.remove_edge("C", "A")
171
+ if G.is_directed():
172
+ assert G.has_edge("A", "C")
173
+ else:
174
+ assert not G.has_edge("A", "C")
175
+ assert not G.has_edge("C", "A")
176
+
177
+ def test_self_loop(self):
178
+ G = self.G()
179
+ G.add_edge("A", "A") # test self loops
180
+ assert G.has_edge("A", "A")
181
+ G.remove_edge("A", "A")
182
+ G.add_edge("X", "X")
183
+ assert G.has_node("X")
184
+ G.remove_node("X")
185
+ G.add_edge("A", "Z") # should add the node silently
186
+ assert G.has_node("Z")
187
+
188
+ def test_add_edges_from(self):
189
+ G = self.G()
190
+ G.add_edges_from([("B", "C")]) # test add_edges_from()
191
+ assert G.has_edge("B", "C")
192
+ if G.is_directed():
193
+ assert not G.has_edge("C", "B")
194
+ else:
195
+ assert G.has_edge("C", "B") # undirected
196
+
197
+ G.add_edges_from([("D", "F"), ("B", "D")])
198
+ assert G.has_edge("D", "F")
199
+ assert G.has_edge("B", "D")
200
+
201
+ if G.is_directed():
202
+ assert not G.has_edge("D", "B")
203
+ else:
204
+ assert G.has_edge("D", "B") # undirected
205
+
206
+ def test_add_edges_from2(self):
207
+ G = self.G()
208
+ # after failing silently, should add 2nd edge
209
+ G.add_edges_from([tuple("IJ"), list("KK"), tuple("JK")])
210
+ assert G.has_edge(*("I", "J"))
211
+ assert G.has_edge(*("K", "K"))
212
+ assert G.has_edge(*("J", "K"))
213
+ if G.is_directed():
214
+ assert not G.has_edge(*("K", "J"))
215
+ else:
216
+ assert G.has_edge(*("K", "J"))
217
+
218
+ def test_add_edges_from3(self):
219
+ G = self.G()
220
+ G.add_edges_from(zip(list("ACD"), list("CDE")))
221
+ assert G.has_edge("D", "E")
222
+ assert not G.has_edge("E", "C")
223
+
224
+ def test_remove_edge(self):
225
+ G = self.G()
226
+ G.add_nodes_from([1, 2, 3, "A", "B", "C", "D", "E", "F", "G", "H"])
227
+
228
+ G.add_edges_from(zip(list("MNOP"), list("NOPM")))
229
+ assert G.has_edge("O", "P")
230
+ assert G.has_edge("P", "M")
231
+ G.remove_node("P") # tests remove_node()'s handling of edges.
232
+ assert not G.has_edge("P", "M")
233
+ pytest.raises(TypeError, G.remove_edge, "M")
234
+
235
+ G.add_edge("N", "M")
236
+ assert G.has_edge("M", "N")
237
+ G.remove_edge("M", "N")
238
+ assert not G.has_edge("M", "N")
239
+
240
+ # self loop fails silently
241
+ G.remove_edges_from([list("HI"), list("DF"), tuple("KK"), tuple("JK")])
242
+ assert not G.has_edge("H", "I")
243
+ assert not G.has_edge("J", "K")
244
+ G.remove_edges_from([list("IJ"), list("KK"), list("JK")])
245
+ assert not G.has_edge("I", "J")
246
+ G.remove_nodes_from(set("ZEFHIMNO"))
247
+ G.add_edge("J", "K")
248
+
249
+ def test_edges_nbunch(self):
250
+ # Test G.edges(nbunch) with various forms of nbunch
251
+ G = self.G()
252
+ G.add_edges_from([("A", "B"), ("A", "C"), ("B", "D"), ("C", "B"), ("C", "D")])
253
+ # node not in nbunch should be quietly ignored
254
+ pytest.raises(nx.NetworkXError, G.edges, 6)
255
+ assert list(G.edges("Z")) == [] # iterable non-node
256
+ # nbunch can be an empty list
257
+ assert list(G.edges([])) == []
258
+ if G.is_directed():
259
+ elist = [("A", "B"), ("A", "C"), ("B", "D")]
260
+ else:
261
+ elist = [("A", "B"), ("A", "C"), ("B", "C"), ("B", "D")]
262
+ # nbunch can be a list
263
+ assert edges_equal(list(G.edges(["A", "B"])), elist)
264
+ # nbunch can be a set
265
+ assert edges_equal(G.edges({"A", "B"}), elist)
266
+ # nbunch can be a graph
267
+ G1 = self.G()
268
+ G1.add_nodes_from("AB")
269
+ assert edges_equal(G.edges(G1), elist)
270
+ # nbunch can be a dict with nodes as keys
271
+ ndict = {"A": "thing1", "B": "thing2"}
272
+ assert edges_equal(G.edges(ndict), elist)
273
+ # nbunch can be a single node
274
+ assert edges_equal(list(G.edges("A")), [("A", "B"), ("A", "C")])
275
+ assert nodes_equal(sorted(G), ["A", "B", "C", "D"])
276
+
277
+ # nbunch can be nothing (whole graph)
278
+ assert edges_equal(
279
+ list(G.edges()),
280
+ [("A", "B"), ("A", "C"), ("B", "D"), ("C", "B"), ("C", "D")],
281
+ )
282
+
283
+ def test_degree(self):
284
+ G = self.G()
285
+ G.add_edges_from([("A", "B"), ("A", "C"), ("B", "D"), ("C", "B"), ("C", "D")])
286
+ assert G.degree("A") == 2
287
+
288
+ # degree of single node in iterable container must return dict
289
+ assert list(G.degree(["A"])) == [("A", 2)]
290
+ assert sorted(d for n, d in G.degree(["A", "B"])) == [2, 3]
291
+ assert sorted(d for n, d in G.degree()) == [2, 2, 3, 3]
292
+
293
+ def test_degree2(self):
294
+ H = self.G()
295
+ H.add_edges_from([(1, 24), (1, 2)])
296
+ assert sorted(d for n, d in H.degree([1, 24])) == [1, 2]
297
+
298
+ def test_degree_graph(self):
299
+ P3 = nx.path_graph(3)
300
+ P5 = nx.path_graph(5)
301
+ # silently ignore nodes not in P3
302
+ assert dict(d for n, d in P3.degree(["A", "B"])) == {}
303
+ # nbunch can be a graph
304
+ assert sorted(d for n, d in P5.degree(P3)) == [1, 2, 2]
305
+ # nbunch can be a graph that's way too big
306
+ assert sorted(d for n, d in P3.degree(P5)) == [1, 1, 2]
307
+ assert list(P5.degree([])) == []
308
+ assert dict(P5.degree([])) == {}
309
+
310
+ def test_null(self):
311
+ null = nx.null_graph()
312
+ assert list(null.degree()) == []
313
+ assert dict(null.degree()) == {}
314
+
315
+ def test_order_size(self):
316
+ G = self.G()
317
+ G.add_edges_from([("A", "B"), ("A", "C"), ("B", "D"), ("C", "B"), ("C", "D")])
318
+ assert G.order() == 4
319
+ assert G.size() == 5
320
+ assert G.number_of_edges() == 5
321
+ assert G.number_of_edges("A", "B") == 1
322
+ assert G.number_of_edges("A", "D") == 0
323
+
324
+ def test_copy(self):
325
+ G = self.G()
326
+ H = G.copy() # copy
327
+ assert H.adj == G.adj
328
+ assert H.name == G.name
329
+ assert H is not G
330
+
331
+ def test_subgraph(self):
332
+ G = self.G()
333
+ G.add_edges_from([("A", "B"), ("A", "C"), ("B", "D"), ("C", "B"), ("C", "D")])
334
+ SG = G.subgraph(["A", "B", "D"])
335
+ assert nodes_equal(list(SG), ["A", "B", "D"])
336
+ assert edges_equal(list(SG.edges()), [("A", "B"), ("B", "D")])
337
+
338
+ def test_to_directed(self):
339
+ G = self.G()
340
+ if not G.is_directed():
341
+ G.add_edges_from(
342
+ [("A", "B"), ("A", "C"), ("B", "D"), ("C", "B"), ("C", "D")]
343
+ )
344
+
345
+ DG = G.to_directed()
346
+ assert DG is not G # directed copy or copy
347
+
348
+ assert DG.is_directed()
349
+ assert DG.name == G.name
350
+ assert DG.adj == G.adj
351
+ assert sorted(DG.out_edges(list("AB"))) == [
352
+ ("A", "B"),
353
+ ("A", "C"),
354
+ ("B", "A"),
355
+ ("B", "C"),
356
+ ("B", "D"),
357
+ ]
358
+ DG.remove_edge("A", "B")
359
+ assert DG.has_edge("B", "A") # this removes B-A but not A-B
360
+ assert not DG.has_edge("A", "B")
361
+
362
+ def test_to_undirected(self):
363
+ G = self.G()
364
+ if G.is_directed():
365
+ G.add_edges_from(
366
+ [("A", "B"), ("A", "C"), ("B", "D"), ("C", "B"), ("C", "D")]
367
+ )
368
+ UG = G.to_undirected() # to_undirected
369
+ assert UG is not G
370
+ assert not UG.is_directed()
371
+ assert G.is_directed()
372
+ assert UG.name == G.name
373
+ assert UG.adj != G.adj
374
+ assert sorted(UG.edges(list("AB"))) == [
375
+ ("A", "B"),
376
+ ("A", "C"),
377
+ ("B", "C"),
378
+ ("B", "D"),
379
+ ]
380
+ assert sorted(UG.edges(["A", "B"])) == [
381
+ ("A", "B"),
382
+ ("A", "C"),
383
+ ("B", "C"),
384
+ ("B", "D"),
385
+ ]
386
+ UG.remove_edge("A", "B")
387
+ assert not UG.has_edge("B", "A")
388
+ assert not UG.has_edge("A", "B")
389
+
390
+ def test_neighbors(self):
391
+ G = self.G()
392
+ G.add_edges_from([("A", "B"), ("A", "C"), ("B", "D"), ("C", "B"), ("C", "D")])
393
+ G.add_nodes_from("GJK")
394
+ assert sorted(G["A"]) == ["B", "C"]
395
+ assert sorted(G.neighbors("A")) == ["B", "C"]
396
+ assert sorted(G.neighbors("A")) == ["B", "C"]
397
+ assert sorted(G.neighbors("G")) == []
398
+ pytest.raises(nx.NetworkXError, G.neighbors, "j")
399
+
400
+ def test_iterators(self):
401
+ G = self.G()
402
+ G.add_edges_from([("A", "B"), ("A", "C"), ("B", "D"), ("C", "B"), ("C", "D")])
403
+ G.add_nodes_from("GJK")
404
+ assert sorted(G.nodes()) == ["A", "B", "C", "D", "G", "J", "K"]
405
+ assert edges_equal(
406
+ G.edges(), [("A", "B"), ("A", "C"), ("B", "D"), ("C", "B"), ("C", "D")]
407
+ )
408
+
409
+ assert sorted(v for k, v in G.degree()) == [0, 0, 0, 2, 2, 3, 3]
410
+ assert sorted(G.degree(), key=str) == [
411
+ ("A", 2),
412
+ ("B", 3),
413
+ ("C", 3),
414
+ ("D", 2),
415
+ ("G", 0),
416
+ ("J", 0),
417
+ ("K", 0),
418
+ ]
419
+ assert sorted(G.neighbors("A")) == ["B", "C"]
420
+ pytest.raises(nx.NetworkXError, G.neighbors, "X")
421
+ G.clear()
422
+ assert nx.number_of_nodes(G) == 0
423
+ assert nx.number_of_edges(G) == 0
424
+
425
+ def test_null_subgraph(self):
426
+ # Subgraph of a null graph is a null graph
427
+ nullgraph = nx.null_graph()
428
+ G = nx.null_graph()
429
+ H = G.subgraph([])
430
+ assert nx.is_isomorphic(H, nullgraph)
431
+
432
+ def test_empty_subgraph(self):
433
+ # Subgraph of an empty graph is an empty graph. test 1
434
+ nullgraph = nx.null_graph()
435
+ E5 = nx.empty_graph(5)
436
+ E10 = nx.empty_graph(10)
437
+ H = E10.subgraph([])
438
+ assert nx.is_isomorphic(H, nullgraph)
439
+ H = E10.subgraph([1, 2, 3, 4, 5])
440
+ assert nx.is_isomorphic(H, E5)
441
+
442
+ def test_complete_subgraph(self):
443
+ # Subgraph of a complete graph is a complete graph
444
+ K1 = nx.complete_graph(1)
445
+ K3 = nx.complete_graph(3)
446
+ K5 = nx.complete_graph(5)
447
+ H = K5.subgraph([1, 2, 3])
448
+ assert nx.is_isomorphic(H, K3)
449
+
450
+ def test_subgraph_nbunch(self):
451
+ nullgraph = nx.null_graph()
452
+ K1 = nx.complete_graph(1)
453
+ K3 = nx.complete_graph(3)
454
+ K5 = nx.complete_graph(5)
455
+ # Test G.subgraph(nbunch), where nbunch is a single node
456
+ H = K5.subgraph(1)
457
+ assert nx.is_isomorphic(H, K1)
458
+ # Test G.subgraph(nbunch), where nbunch is a set
459
+ H = K5.subgraph({1})
460
+ assert nx.is_isomorphic(H, K1)
461
+ # Test G.subgraph(nbunch), where nbunch is an iterator
462
+ H = K5.subgraph(iter(K3))
463
+ assert nx.is_isomorphic(H, K3)
464
+ # Test G.subgraph(nbunch), where nbunch is another graph
465
+ H = K5.subgraph(K3)
466
+ assert nx.is_isomorphic(H, K3)
467
+ H = K5.subgraph([9])
468
+ assert nx.is_isomorphic(H, nullgraph)
469
+
470
+ def test_node_tuple_issue(self):
471
+ H = self.G()
472
+ # Test error handling of tuple as a node
473
+ pytest.raises(nx.NetworkXError, H.remove_node, (1, 2))
474
+ H.remove_nodes_from([(1, 2)]) # no error
475
+ pytest.raises(nx.NetworkXError, H.neighbors, (1, 2))
minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_digraph.py ADDED
@@ -0,0 +1,331 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+ from networkx.utils import nodes_equal
5
+
6
+ from .test_graph import BaseAttrGraphTester, BaseGraphTester
7
+ from .test_graph import TestEdgeSubgraph as _TestGraphEdgeSubgraph
8
+ from .test_graph import TestGraph as _TestGraph
9
+
10
+
11
+ class BaseDiGraphTester(BaseGraphTester):
12
+ def test_has_successor(self):
13
+ G = self.K3
14
+ assert G.has_successor(0, 1)
15
+ assert not G.has_successor(0, -1)
16
+
17
+ def test_successors(self):
18
+ G = self.K3
19
+ assert sorted(G.successors(0)) == [1, 2]
20
+ with pytest.raises(nx.NetworkXError):
21
+ G.successors(-1)
22
+
23
+ def test_has_predecessor(self):
24
+ G = self.K3
25
+ assert G.has_predecessor(0, 1)
26
+ assert not G.has_predecessor(0, -1)
27
+
28
+ def test_predecessors(self):
29
+ G = self.K3
30
+ assert sorted(G.predecessors(0)) == [1, 2]
31
+ with pytest.raises(nx.NetworkXError):
32
+ G.predecessors(-1)
33
+
34
+ def test_edges(self):
35
+ G = self.K3
36
+ assert sorted(G.edges()) == [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
37
+ assert sorted(G.edges(0)) == [(0, 1), (0, 2)]
38
+ assert sorted(G.edges([0, 1])) == [(0, 1), (0, 2), (1, 0), (1, 2)]
39
+ with pytest.raises(nx.NetworkXError):
40
+ G.edges(-1)
41
+
42
+ def test_out_edges(self):
43
+ G = self.K3
44
+ assert sorted(G.out_edges()) == [(0, 1), (0, 2), (1, 0), (1, 2), (2, 0), (2, 1)]
45
+ assert sorted(G.out_edges(0)) == [(0, 1), (0, 2)]
46
+ with pytest.raises(nx.NetworkXError):
47
+ G.out_edges(-1)
48
+
49
+ def test_out_edges_dir(self):
50
+ G = self.P3
51
+ assert sorted(G.out_edges()) == [(0, 1), (1, 2)]
52
+ assert sorted(G.out_edges(0)) == [(0, 1)]
53
+ assert sorted(G.out_edges(2)) == []
54
+
55
+ def test_out_edges_data(self):
56
+ G = nx.DiGraph([(0, 1, {"data": 0}), (1, 0, {})])
57
+ assert sorted(G.out_edges(data=True)) == [(0, 1, {"data": 0}), (1, 0, {})]
58
+ assert sorted(G.out_edges(0, data=True)) == [(0, 1, {"data": 0})]
59
+ assert sorted(G.out_edges(data="data")) == [(0, 1, 0), (1, 0, None)]
60
+ assert sorted(G.out_edges(0, data="data")) == [(0, 1, 0)]
61
+
62
+ def test_in_edges_dir(self):
63
+ G = self.P3
64
+ assert sorted(G.in_edges()) == [(0, 1), (1, 2)]
65
+ assert sorted(G.in_edges(0)) == []
66
+ assert sorted(G.in_edges(2)) == [(1, 2)]
67
+
68
+ def test_in_edges_data(self):
69
+ G = nx.DiGraph([(0, 1, {"data": 0}), (1, 0, {})])
70
+ assert sorted(G.in_edges(data=True)) == [(0, 1, {"data": 0}), (1, 0, {})]
71
+ assert sorted(G.in_edges(1, data=True)) == [(0, 1, {"data": 0})]
72
+ assert sorted(G.in_edges(data="data")) == [(0, 1, 0), (1, 0, None)]
73
+ assert sorted(G.in_edges(1, data="data")) == [(0, 1, 0)]
74
+
75
+ def test_degree(self):
76
+ G = self.K3
77
+ assert sorted(G.degree()) == [(0, 4), (1, 4), (2, 4)]
78
+ assert dict(G.degree()) == {0: 4, 1: 4, 2: 4}
79
+ assert G.degree(0) == 4
80
+ assert list(G.degree(iter([0]))) == [(0, 4)] # run through iterator
81
+
82
+ def test_in_degree(self):
83
+ G = self.K3
84
+ assert sorted(G.in_degree()) == [(0, 2), (1, 2), (2, 2)]
85
+ assert dict(G.in_degree()) == {0: 2, 1: 2, 2: 2}
86
+ assert G.in_degree(0) == 2
87
+ assert list(G.in_degree(iter([0]))) == [(0, 2)] # run through iterator
88
+
89
+ def test_out_degree(self):
90
+ G = self.K3
91
+ assert sorted(G.out_degree()) == [(0, 2), (1, 2), (2, 2)]
92
+ assert dict(G.out_degree()) == {0: 2, 1: 2, 2: 2}
93
+ assert G.out_degree(0) == 2
94
+ assert list(G.out_degree(iter([0]))) == [(0, 2)]
95
+
96
+ def test_size(self):
97
+ G = self.K3
98
+ assert G.size() == 6
99
+ assert G.number_of_edges() == 6
100
+
101
+ def test_to_undirected_reciprocal(self):
102
+ G = self.Graph()
103
+ G.add_edge(1, 2)
104
+ assert G.to_undirected().has_edge(1, 2)
105
+ assert not G.to_undirected(reciprocal=True).has_edge(1, 2)
106
+ G.add_edge(2, 1)
107
+ assert G.to_undirected(reciprocal=True).has_edge(1, 2)
108
+
109
+ def test_reverse_copy(self):
110
+ G = nx.DiGraph([(0, 1), (1, 2)])
111
+ R = G.reverse()
112
+ assert sorted(R.edges()) == [(1, 0), (2, 1)]
113
+ R.remove_edge(1, 0)
114
+ assert sorted(R.edges()) == [(2, 1)]
115
+ assert sorted(G.edges()) == [(0, 1), (1, 2)]
116
+
117
+ def test_reverse_nocopy(self):
118
+ G = nx.DiGraph([(0, 1), (1, 2)])
119
+ R = G.reverse(copy=False)
120
+ assert sorted(R.edges()) == [(1, 0), (2, 1)]
121
+ with pytest.raises(nx.NetworkXError):
122
+ R.remove_edge(1, 0)
123
+
124
+ def test_reverse_hashable(self):
125
+ class Foo:
126
+ pass
127
+
128
+ x = Foo()
129
+ y = Foo()
130
+ G = nx.DiGraph()
131
+ G.add_edge(x, y)
132
+ assert nodes_equal(G.nodes(), G.reverse().nodes())
133
+ assert [(y, x)] == list(G.reverse().edges())
134
+
135
+ def test_di_cache_reset(self):
136
+ G = self.K3.copy()
137
+ old_succ = G.succ
138
+ assert id(G.succ) == id(old_succ)
139
+ old_adj = G.adj
140
+ assert id(G.adj) == id(old_adj)
141
+
142
+ G._succ = {}
143
+ assert id(G.succ) != id(old_succ)
144
+ assert id(G.adj) != id(old_adj)
145
+
146
+ old_pred = G.pred
147
+ assert id(G.pred) == id(old_pred)
148
+ G._pred = {}
149
+ assert id(G.pred) != id(old_pred)
150
+
151
+ def test_di_attributes_cached(self):
152
+ G = self.K3.copy()
153
+ assert id(G.in_edges) == id(G.in_edges)
154
+ assert id(G.out_edges) == id(G.out_edges)
155
+ assert id(G.in_degree) == id(G.in_degree)
156
+ assert id(G.out_degree) == id(G.out_degree)
157
+ assert id(G.succ) == id(G.succ)
158
+ assert id(G.pred) == id(G.pred)
159
+
160
+
161
+ class BaseAttrDiGraphTester(BaseDiGraphTester, BaseAttrGraphTester):
162
+ def test_edges_data(self):
163
+ G = self.K3
164
+ all_edges = [
165
+ (0, 1, {}),
166
+ (0, 2, {}),
167
+ (1, 0, {}),
168
+ (1, 2, {}),
169
+ (2, 0, {}),
170
+ (2, 1, {}),
171
+ ]
172
+ assert sorted(G.edges(data=True)) == all_edges
173
+ assert sorted(G.edges(0, data=True)) == all_edges[:2]
174
+ assert sorted(G.edges([0, 1], data=True)) == all_edges[:4]
175
+ with pytest.raises(nx.NetworkXError):
176
+ G.edges(-1, True)
177
+
178
+ def test_in_degree_weighted(self):
179
+ G = self.K3.copy()
180
+ G.add_edge(0, 1, weight=0.3, other=1.2)
181
+ assert sorted(G.in_degree(weight="weight")) == [(0, 2), (1, 1.3), (2, 2)]
182
+ assert dict(G.in_degree(weight="weight")) == {0: 2, 1: 1.3, 2: 2}
183
+ assert G.in_degree(1, weight="weight") == 1.3
184
+ assert sorted(G.in_degree(weight="other")) == [(0, 2), (1, 2.2), (2, 2)]
185
+ assert dict(G.in_degree(weight="other")) == {0: 2, 1: 2.2, 2: 2}
186
+ assert G.in_degree(1, weight="other") == 2.2
187
+ assert list(G.in_degree(iter([1]), weight="other")) == [(1, 2.2)]
188
+
189
+ def test_out_degree_weighted(self):
190
+ G = self.K3.copy()
191
+ G.add_edge(0, 1, weight=0.3, other=1.2)
192
+ assert sorted(G.out_degree(weight="weight")) == [(0, 1.3), (1, 2), (2, 2)]
193
+ assert dict(G.out_degree(weight="weight")) == {0: 1.3, 1: 2, 2: 2}
194
+ assert G.out_degree(0, weight="weight") == 1.3
195
+ assert sorted(G.out_degree(weight="other")) == [(0, 2.2), (1, 2), (2, 2)]
196
+ assert dict(G.out_degree(weight="other")) == {0: 2.2, 1: 2, 2: 2}
197
+ assert G.out_degree(0, weight="other") == 2.2
198
+ assert list(G.out_degree(iter([0]), weight="other")) == [(0, 2.2)]
199
+
200
+
201
+ class TestDiGraph(BaseAttrDiGraphTester, _TestGraph):
202
+ """Tests specific to dict-of-dict-of-dict digraph data structure"""
203
+
204
+ def setup_method(self):
205
+ self.Graph = nx.DiGraph
206
+ # build dict-of-dict-of-dict K3
207
+ ed1, ed2, ed3, ed4, ed5, ed6 = ({}, {}, {}, {}, {}, {})
208
+ self.k3adj = {0: {1: ed1, 2: ed2}, 1: {0: ed3, 2: ed4}, 2: {0: ed5, 1: ed6}}
209
+ self.k3edges = [(0, 1), (0, 2), (1, 2)]
210
+ self.k3nodes = [0, 1, 2]
211
+ self.K3 = self.Graph()
212
+ self.K3._succ = self.k3adj # K3._adj is synced with K3._succ
213
+ self.K3._pred = {0: {1: ed3, 2: ed5}, 1: {0: ed1, 2: ed6}, 2: {0: ed2, 1: ed4}}
214
+ self.K3._node = {}
215
+ self.K3._node[0] = {}
216
+ self.K3._node[1] = {}
217
+ self.K3._node[2] = {}
218
+
219
+ ed1, ed2 = ({}, {})
220
+ self.P3 = self.Graph()
221
+ self.P3._succ = {0: {1: ed1}, 1: {2: ed2}, 2: {}}
222
+ self.P3._pred = {0: {}, 1: {0: ed1}, 2: {1: ed2}}
223
+ # P3._adj is synced with P3._succ
224
+ self.P3._node = {}
225
+ self.P3._node[0] = {}
226
+ self.P3._node[1] = {}
227
+ self.P3._node[2] = {}
228
+
229
+ def test_data_input(self):
230
+ G = self.Graph({1: [2], 2: [1]}, name="test")
231
+ assert G.name == "test"
232
+ assert sorted(G.adj.items()) == [(1, {2: {}}), (2, {1: {}})]
233
+ assert sorted(G.succ.items()) == [(1, {2: {}}), (2, {1: {}})]
234
+ assert sorted(G.pred.items()) == [(1, {2: {}}), (2, {1: {}})]
235
+
236
+ def test_add_edge(self):
237
+ G = self.Graph()
238
+ G.add_edge(0, 1)
239
+ assert G.adj == {0: {1: {}}, 1: {}}
240
+ assert G.succ == {0: {1: {}}, 1: {}}
241
+ assert G.pred == {0: {}, 1: {0: {}}}
242
+ G = self.Graph()
243
+ G.add_edge(*(0, 1))
244
+ assert G.adj == {0: {1: {}}, 1: {}}
245
+ assert G.succ == {0: {1: {}}, 1: {}}
246
+ assert G.pred == {0: {}, 1: {0: {}}}
247
+ with pytest.raises(ValueError, match="None cannot be a node"):
248
+ G.add_edge(None, 3)
249
+
250
+ def test_add_edges_from(self):
251
+ G = self.Graph()
252
+ G.add_edges_from([(0, 1), (0, 2, {"data": 3})], data=2)
253
+ assert G.adj == {0: {1: {"data": 2}, 2: {"data": 3}}, 1: {}, 2: {}}
254
+ assert G.succ == {0: {1: {"data": 2}, 2: {"data": 3}}, 1: {}, 2: {}}
255
+ assert G.pred == {0: {}, 1: {0: {"data": 2}}, 2: {0: {"data": 3}}}
256
+
257
+ with pytest.raises(nx.NetworkXError):
258
+ G.add_edges_from([(0,)]) # too few in tuple
259
+ with pytest.raises(nx.NetworkXError):
260
+ G.add_edges_from([(0, 1, 2, 3)]) # too many in tuple
261
+ with pytest.raises(TypeError):
262
+ G.add_edges_from([0]) # not a tuple
263
+ with pytest.raises(ValueError, match="None cannot be a node"):
264
+ G.add_edges_from([(None, 3), (3, 2)])
265
+
266
+ def test_remove_edge(self):
267
+ G = self.K3.copy()
268
+ G.remove_edge(0, 1)
269
+ assert G.succ == {0: {2: {}}, 1: {0: {}, 2: {}}, 2: {0: {}, 1: {}}}
270
+ assert G.pred == {0: {1: {}, 2: {}}, 1: {2: {}}, 2: {0: {}, 1: {}}}
271
+ with pytest.raises(nx.NetworkXError):
272
+ G.remove_edge(-1, 0)
273
+
274
+ def test_remove_edges_from(self):
275
+ G = self.K3.copy()
276
+ G.remove_edges_from([(0, 1)])
277
+ assert G.succ == {0: {2: {}}, 1: {0: {}, 2: {}}, 2: {0: {}, 1: {}}}
278
+ assert G.pred == {0: {1: {}, 2: {}}, 1: {2: {}}, 2: {0: {}, 1: {}}}
279
+ G.remove_edges_from([(0, 0)]) # silent fail
280
+
281
+ def test_clear(self):
282
+ G = self.K3
283
+ G.graph["name"] = "K3"
284
+ G.clear()
285
+ assert list(G.nodes) == []
286
+ assert G.succ == {}
287
+ assert G.pred == {}
288
+ assert G.graph == {}
289
+
290
+ def test_clear_edges(self):
291
+ G = self.K3
292
+ G.graph["name"] = "K3"
293
+ nodes = list(G.nodes)
294
+ G.clear_edges()
295
+ assert list(G.nodes) == nodes
296
+ expected = {0: {}, 1: {}, 2: {}}
297
+ assert G.succ == expected
298
+ assert G.pred == expected
299
+ assert list(G.edges) == []
300
+ assert G.graph["name"] == "K3"
301
+
302
+
303
+ class TestEdgeSubgraph(_TestGraphEdgeSubgraph):
304
+ """Unit tests for the :meth:`DiGraph.edge_subgraph` method."""
305
+
306
+ def setup_method(self):
307
+ # Create a doubly-linked path graph on five nodes.
308
+ G = nx.DiGraph(nx.path_graph(5))
309
+ # Add some node, edge, and graph attributes.
310
+ for i in range(5):
311
+ G.nodes[i]["name"] = f"node{i}"
312
+ G.edges[0, 1]["name"] = "edge01"
313
+ G.edges[3, 4]["name"] = "edge34"
314
+ G.graph["name"] = "graph"
315
+ # Get the subgraph induced by the first and last edges.
316
+ self.G = G
317
+ self.H = G.edge_subgraph([(0, 1), (3, 4)])
318
+
319
+ def test_pred_succ(self):
320
+ """Test that nodes are added to predecessors and successors.
321
+
322
+ For more information, see GitHub issue #2370.
323
+
324
+ """
325
+ G = nx.DiGraph()
326
+ G.add_edge(0, 1)
327
+ H = G.edge_subgraph([(0, 1)])
328
+ assert list(H.predecessors(0)) == []
329
+ assert list(H.successors(0)) == [1]
330
+ assert list(H.predecessors(1)) == [0]
331
+ assert list(H.successors(1)) == []
minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_function.py ADDED
@@ -0,0 +1,1035 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import random
2
+
3
+ import pytest
4
+
5
+ import networkx as nx
6
+ from networkx.utils import edges_equal, nodes_equal
7
+
8
+
9
+ def test_degree_histogram_empty():
10
+ G = nx.Graph()
11
+ assert nx.degree_histogram(G) == []
12
+
13
+
14
+ class TestFunction:
15
+ def setup_method(self):
16
+ self.G = nx.Graph({0: [1, 2, 3], 1: [1, 2, 0], 4: []}, name="Test")
17
+ self.Gdegree = {0: 3, 1: 2, 2: 2, 3: 1, 4: 0}
18
+ self.Gnodes = list(range(5))
19
+ self.Gedges = [(0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2)]
20
+ self.DG = nx.DiGraph({0: [1, 2, 3], 1: [1, 2, 0], 4: []})
21
+ self.DGin_degree = {0: 1, 1: 2, 2: 2, 3: 1, 4: 0}
22
+ self.DGout_degree = {0: 3, 1: 3, 2: 0, 3: 0, 4: 0}
23
+ self.DGnodes = list(range(5))
24
+ self.DGedges = [(0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2)]
25
+
26
+ def test_nodes(self):
27
+ assert nodes_equal(self.G.nodes(), list(nx.nodes(self.G)))
28
+ assert nodes_equal(self.DG.nodes(), list(nx.nodes(self.DG)))
29
+
30
+ def test_edges(self):
31
+ assert edges_equal(self.G.edges(), list(nx.edges(self.G)))
32
+ assert sorted(self.DG.edges()) == sorted(nx.edges(self.DG))
33
+ assert edges_equal(
34
+ self.G.edges(nbunch=[0, 1, 3]), list(nx.edges(self.G, nbunch=[0, 1, 3]))
35
+ )
36
+ assert sorted(self.DG.edges(nbunch=[0, 1, 3])) == sorted(
37
+ nx.edges(self.DG, nbunch=[0, 1, 3])
38
+ )
39
+
40
+ def test_degree(self):
41
+ assert edges_equal(self.G.degree(), list(nx.degree(self.G)))
42
+ assert sorted(self.DG.degree()) == sorted(nx.degree(self.DG))
43
+ assert edges_equal(
44
+ self.G.degree(nbunch=[0, 1]), list(nx.degree(self.G, nbunch=[0, 1]))
45
+ )
46
+ assert sorted(self.DG.degree(nbunch=[0, 1])) == sorted(
47
+ nx.degree(self.DG, nbunch=[0, 1])
48
+ )
49
+ assert edges_equal(
50
+ self.G.degree(weight="weight"), list(nx.degree(self.G, weight="weight"))
51
+ )
52
+ assert sorted(self.DG.degree(weight="weight")) == sorted(
53
+ nx.degree(self.DG, weight="weight")
54
+ )
55
+
56
+ def test_neighbors(self):
57
+ assert list(self.G.neighbors(1)) == list(nx.neighbors(self.G, 1))
58
+ assert list(self.DG.neighbors(1)) == list(nx.neighbors(self.DG, 1))
59
+
60
+ def test_number_of_nodes(self):
61
+ assert self.G.number_of_nodes() == nx.number_of_nodes(self.G)
62
+ assert self.DG.number_of_nodes() == nx.number_of_nodes(self.DG)
63
+
64
+ def test_number_of_edges(self):
65
+ assert self.G.number_of_edges() == nx.number_of_edges(self.G)
66
+ assert self.DG.number_of_edges() == nx.number_of_edges(self.DG)
67
+
68
+ def test_is_directed(self):
69
+ assert self.G.is_directed() == nx.is_directed(self.G)
70
+ assert self.DG.is_directed() == nx.is_directed(self.DG)
71
+
72
+ def test_add_star(self):
73
+ G = self.G.copy()
74
+ nlist = [12, 13, 14, 15]
75
+ nx.add_star(G, nlist)
76
+ assert edges_equal(G.edges(nlist), [(12, 13), (12, 14), (12, 15)])
77
+
78
+ G = self.G.copy()
79
+ nx.add_star(G, nlist, weight=2.0)
80
+ assert edges_equal(
81
+ G.edges(nlist, data=True),
82
+ [
83
+ (12, 13, {"weight": 2.0}),
84
+ (12, 14, {"weight": 2.0}),
85
+ (12, 15, {"weight": 2.0}),
86
+ ],
87
+ )
88
+
89
+ G = self.G.copy()
90
+ nlist = [12]
91
+ nx.add_star(G, nlist)
92
+ assert nodes_equal(G, list(self.G) + nlist)
93
+
94
+ G = self.G.copy()
95
+ nlist = []
96
+ nx.add_star(G, nlist)
97
+ assert nodes_equal(G.nodes, self.Gnodes)
98
+ assert edges_equal(G.edges, self.G.edges)
99
+
100
+ def test_add_path(self):
101
+ G = self.G.copy()
102
+ nlist = [12, 13, 14, 15]
103
+ nx.add_path(G, nlist)
104
+ assert edges_equal(G.edges(nlist), [(12, 13), (13, 14), (14, 15)])
105
+ G = self.G.copy()
106
+ nx.add_path(G, nlist, weight=2.0)
107
+ assert edges_equal(
108
+ G.edges(nlist, data=True),
109
+ [
110
+ (12, 13, {"weight": 2.0}),
111
+ (13, 14, {"weight": 2.0}),
112
+ (14, 15, {"weight": 2.0}),
113
+ ],
114
+ )
115
+
116
+ G = self.G.copy()
117
+ nlist = ["node"]
118
+ nx.add_path(G, nlist)
119
+ assert edges_equal(G.edges(nlist), [])
120
+ assert nodes_equal(G, list(self.G) + ["node"])
121
+
122
+ G = self.G.copy()
123
+ nlist = iter(["node"])
124
+ nx.add_path(G, nlist)
125
+ assert edges_equal(G.edges(["node"]), [])
126
+ assert nodes_equal(G, list(self.G) + ["node"])
127
+
128
+ G = self.G.copy()
129
+ nlist = [12]
130
+ nx.add_path(G, nlist)
131
+ assert edges_equal(G.edges(nlist), [])
132
+ assert nodes_equal(G, list(self.G) + [12])
133
+
134
+ G = self.G.copy()
135
+ nlist = iter([12])
136
+ nx.add_path(G, nlist)
137
+ assert edges_equal(G.edges([12]), [])
138
+ assert nodes_equal(G, list(self.G) + [12])
139
+
140
+ G = self.G.copy()
141
+ nlist = []
142
+ nx.add_path(G, nlist)
143
+ assert edges_equal(G.edges, self.G.edges)
144
+ assert nodes_equal(G, list(self.G))
145
+
146
+ G = self.G.copy()
147
+ nlist = iter([])
148
+ nx.add_path(G, nlist)
149
+ assert edges_equal(G.edges, self.G.edges)
150
+ assert nodes_equal(G, list(self.G))
151
+
152
+ def test_add_cycle(self):
153
+ G = self.G.copy()
154
+ nlist = [12, 13, 14, 15]
155
+ oklists = [
156
+ [(12, 13), (12, 15), (13, 14), (14, 15)],
157
+ [(12, 13), (13, 14), (14, 15), (15, 12)],
158
+ ]
159
+ nx.add_cycle(G, nlist)
160
+ assert sorted(G.edges(nlist)) in oklists
161
+ G = self.G.copy()
162
+ oklists = [
163
+ [
164
+ (12, 13, {"weight": 1.0}),
165
+ (12, 15, {"weight": 1.0}),
166
+ (13, 14, {"weight": 1.0}),
167
+ (14, 15, {"weight": 1.0}),
168
+ ],
169
+ [
170
+ (12, 13, {"weight": 1.0}),
171
+ (13, 14, {"weight": 1.0}),
172
+ (14, 15, {"weight": 1.0}),
173
+ (15, 12, {"weight": 1.0}),
174
+ ],
175
+ ]
176
+ nx.add_cycle(G, nlist, weight=1.0)
177
+ assert sorted(G.edges(nlist, data=True)) in oklists
178
+
179
+ G = self.G.copy()
180
+ nlist = [12]
181
+ nx.add_cycle(G, nlist)
182
+ assert nodes_equal(G, list(self.G) + nlist)
183
+
184
+ G = self.G.copy()
185
+ nlist = []
186
+ nx.add_cycle(G, nlist)
187
+ assert nodes_equal(G.nodes, self.Gnodes)
188
+ assert edges_equal(G.edges, self.G.edges)
189
+
190
+ def test_subgraph(self):
191
+ assert (
192
+ self.G.subgraph([0, 1, 2, 4]).adj == nx.subgraph(self.G, [0, 1, 2, 4]).adj
193
+ )
194
+ assert (
195
+ self.DG.subgraph([0, 1, 2, 4]).adj == nx.subgraph(self.DG, [0, 1, 2, 4]).adj
196
+ )
197
+ assert (
198
+ self.G.subgraph([0, 1, 2, 4]).adj
199
+ == nx.induced_subgraph(self.G, [0, 1, 2, 4]).adj
200
+ )
201
+ assert (
202
+ self.DG.subgraph([0, 1, 2, 4]).adj
203
+ == nx.induced_subgraph(self.DG, [0, 1, 2, 4]).adj
204
+ )
205
+ # subgraph-subgraph chain is allowed in function interface
206
+ H = nx.induced_subgraph(self.G.subgraph([0, 1, 2, 4]), [0, 1, 4])
207
+ assert H._graph is not self.G
208
+ assert H.adj == self.G.subgraph([0, 1, 4]).adj
209
+
210
+ def test_edge_subgraph(self):
211
+ assert (
212
+ self.G.edge_subgraph([(1, 2), (0, 3)]).adj
213
+ == nx.edge_subgraph(self.G, [(1, 2), (0, 3)]).adj
214
+ )
215
+ assert (
216
+ self.DG.edge_subgraph([(1, 2), (0, 3)]).adj
217
+ == nx.edge_subgraph(self.DG, [(1, 2), (0, 3)]).adj
218
+ )
219
+
220
+ def test_create_empty_copy(self):
221
+ G = nx.create_empty_copy(self.G, with_data=False)
222
+ assert nodes_equal(G, list(self.G))
223
+ assert G.graph == {}
224
+ assert G._node == {}.fromkeys(self.G.nodes(), {})
225
+ assert G._adj == {}.fromkeys(self.G.nodes(), {})
226
+ G = nx.create_empty_copy(self.G)
227
+ assert nodes_equal(G, list(self.G))
228
+ assert G.graph == self.G.graph
229
+ assert G._node == self.G._node
230
+ assert G._adj == {}.fromkeys(self.G.nodes(), {})
231
+
232
+ def test_degree_histogram(self):
233
+ assert nx.degree_histogram(self.G) == [1, 1, 1, 1, 1]
234
+
235
+ def test_density(self):
236
+ assert nx.density(self.G) == 0.5
237
+ assert nx.density(self.DG) == 0.3
238
+ G = nx.Graph()
239
+ G.add_node(1)
240
+ assert nx.density(G) == 0.0
241
+
242
+ def test_density_selfloop(self):
243
+ G = nx.Graph()
244
+ G.add_edge(1, 1)
245
+ assert nx.density(G) == 0.0
246
+ G.add_edge(1, 2)
247
+ assert nx.density(G) == 2.0
248
+
249
+ def test_freeze(self):
250
+ G = nx.freeze(self.G)
251
+ assert G.frozen
252
+ pytest.raises(nx.NetworkXError, G.add_node, 1)
253
+ pytest.raises(nx.NetworkXError, G.add_nodes_from, [1])
254
+ pytest.raises(nx.NetworkXError, G.remove_node, 1)
255
+ pytest.raises(nx.NetworkXError, G.remove_nodes_from, [1])
256
+ pytest.raises(nx.NetworkXError, G.add_edge, 1, 2)
257
+ pytest.raises(nx.NetworkXError, G.add_edges_from, [(1, 2)])
258
+ pytest.raises(nx.NetworkXError, G.remove_edge, 1, 2)
259
+ pytest.raises(nx.NetworkXError, G.remove_edges_from, [(1, 2)])
260
+ pytest.raises(nx.NetworkXError, G.clear_edges)
261
+ pytest.raises(nx.NetworkXError, G.clear)
262
+
263
+ def test_is_frozen(self):
264
+ assert not nx.is_frozen(self.G)
265
+ G = nx.freeze(self.G)
266
+ assert G.frozen == nx.is_frozen(self.G)
267
+ assert G.frozen
268
+
269
+ def test_node_attributes_are_still_mutable_on_frozen_graph(self):
270
+ G = nx.freeze(nx.path_graph(3))
271
+ node = G.nodes[0]
272
+ node["node_attribute"] = True
273
+ assert node["node_attribute"] == True
274
+
275
+ def test_edge_attributes_are_still_mutable_on_frozen_graph(self):
276
+ G = nx.freeze(nx.path_graph(3))
277
+ edge = G.edges[(0, 1)]
278
+ edge["edge_attribute"] = True
279
+ assert edge["edge_attribute"] == True
280
+
281
+ def test_neighbors_complete_graph(self):
282
+ graph = nx.complete_graph(100)
283
+ pop = random.sample(list(graph), 1)
284
+ nbors = list(nx.neighbors(graph, pop[0]))
285
+ # should be all the other vertices in the graph
286
+ assert len(nbors) == len(graph) - 1
287
+
288
+ graph = nx.path_graph(100)
289
+ node = random.sample(list(graph), 1)[0]
290
+ nbors = list(nx.neighbors(graph, node))
291
+ # should be all the other vertices in the graph
292
+ if node != 0 and node != 99:
293
+ assert len(nbors) == 2
294
+ else:
295
+ assert len(nbors) == 1
296
+
297
+ # create a star graph with 99 outer nodes
298
+ graph = nx.star_graph(99)
299
+ nbors = list(nx.neighbors(graph, 0))
300
+ assert len(nbors) == 99
301
+
302
+ def test_non_neighbors(self):
303
+ graph = nx.complete_graph(100)
304
+ pop = random.sample(list(graph), 1)
305
+ nbors = nx.non_neighbors(graph, pop[0])
306
+ # should be all the other vertices in the graph
307
+ assert len(nbors) == 0
308
+
309
+ graph = nx.path_graph(100)
310
+ node = random.sample(list(graph), 1)[0]
311
+ nbors = nx.non_neighbors(graph, node)
312
+ # should be all the other vertices in the graph
313
+ if node != 0 and node != 99:
314
+ assert len(nbors) == 97
315
+ else:
316
+ assert len(nbors) == 98
317
+
318
+ # create a star graph with 99 outer nodes
319
+ graph = nx.star_graph(99)
320
+ nbors = nx.non_neighbors(graph, 0)
321
+ assert len(nbors) == 0
322
+
323
+ # disconnected graph
324
+ graph = nx.Graph()
325
+ graph.add_nodes_from(range(10))
326
+ nbors = nx.non_neighbors(graph, 0)
327
+ assert len(nbors) == 9
328
+
329
+ def test_non_edges(self):
330
+ # All possible edges exist
331
+ graph = nx.complete_graph(5)
332
+ nedges = list(nx.non_edges(graph))
333
+ assert len(nedges) == 0
334
+
335
+ graph = nx.path_graph(4)
336
+ expected = [(0, 2), (0, 3), (1, 3)]
337
+ nedges = list(nx.non_edges(graph))
338
+ for u, v in expected:
339
+ assert (u, v) in nedges or (v, u) in nedges
340
+
341
+ graph = nx.star_graph(4)
342
+ expected = [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]
343
+ nedges = list(nx.non_edges(graph))
344
+ for u, v in expected:
345
+ assert (u, v) in nedges or (v, u) in nedges
346
+
347
+ # Directed graphs
348
+ graph = nx.DiGraph()
349
+ graph.add_edges_from([(0, 2), (2, 0), (2, 1)])
350
+ expected = [(0, 1), (1, 0), (1, 2)]
351
+ nedges = list(nx.non_edges(graph))
352
+ for e in expected:
353
+ assert e in nedges
354
+
355
+ def test_is_weighted(self):
356
+ G = nx.Graph()
357
+ assert not nx.is_weighted(G)
358
+
359
+ G = nx.path_graph(4)
360
+ assert not nx.is_weighted(G)
361
+ assert not nx.is_weighted(G, (2, 3))
362
+
363
+ G.add_node(4)
364
+ G.add_edge(3, 4, weight=4)
365
+ assert not nx.is_weighted(G)
366
+ assert nx.is_weighted(G, (3, 4))
367
+
368
+ G = nx.DiGraph()
369
+ G.add_weighted_edges_from(
370
+ [
371
+ ("0", "3", 3),
372
+ ("0", "1", -5),
373
+ ("1", "0", -5),
374
+ ("0", "2", 2),
375
+ ("1", "2", 4),
376
+ ("2", "3", 1),
377
+ ]
378
+ )
379
+ assert nx.is_weighted(G)
380
+ assert nx.is_weighted(G, ("1", "0"))
381
+
382
+ G = G.to_undirected()
383
+ assert nx.is_weighted(G)
384
+ assert nx.is_weighted(G, ("1", "0"))
385
+
386
+ pytest.raises(nx.NetworkXError, nx.is_weighted, G, (1, 2))
387
+
388
+ def test_is_negatively_weighted(self):
389
+ G = nx.Graph()
390
+ assert not nx.is_negatively_weighted(G)
391
+
392
+ G.add_node(1)
393
+ G.add_nodes_from([2, 3, 4, 5])
394
+ assert not nx.is_negatively_weighted(G)
395
+
396
+ G.add_edge(1, 2, weight=4)
397
+ assert not nx.is_negatively_weighted(G, (1, 2))
398
+
399
+ G.add_edges_from([(1, 3), (2, 4), (2, 6)])
400
+ G[1][3]["color"] = "blue"
401
+ assert not nx.is_negatively_weighted(G)
402
+ assert not nx.is_negatively_weighted(G, (1, 3))
403
+
404
+ G[2][4]["weight"] = -2
405
+ assert nx.is_negatively_weighted(G, (2, 4))
406
+ assert nx.is_negatively_weighted(G)
407
+
408
+ G = nx.DiGraph()
409
+ G.add_weighted_edges_from(
410
+ [
411
+ ("0", "3", 3),
412
+ ("0", "1", -5),
413
+ ("1", "0", -2),
414
+ ("0", "2", 2),
415
+ ("1", "2", -3),
416
+ ("2", "3", 1),
417
+ ]
418
+ )
419
+ assert nx.is_negatively_weighted(G)
420
+ assert not nx.is_negatively_weighted(G, ("0", "3"))
421
+ assert nx.is_negatively_weighted(G, ("1", "0"))
422
+
423
+ pytest.raises(nx.NetworkXError, nx.is_negatively_weighted, G, (1, 4))
424
+
425
+
426
+ class TestCommonNeighbors:
427
+ @classmethod
428
+ def setup_class(cls):
429
+ cls.func = staticmethod(nx.common_neighbors)
430
+
431
+ def test_func(G, u, v, expected):
432
+ result = sorted(cls.func(G, u, v))
433
+ assert result == expected
434
+
435
+ cls.test = staticmethod(test_func)
436
+
437
+ def test_K5(self):
438
+ G = nx.complete_graph(5)
439
+ self.test(G, 0, 1, [2, 3, 4])
440
+
441
+ def test_P3(self):
442
+ G = nx.path_graph(3)
443
+ self.test(G, 0, 2, [1])
444
+
445
+ def test_S4(self):
446
+ G = nx.star_graph(4)
447
+ self.test(G, 1, 2, [0])
448
+
449
+ def test_digraph(self):
450
+ with pytest.raises(nx.NetworkXNotImplemented):
451
+ G = nx.DiGraph()
452
+ G.add_edges_from([(0, 1), (1, 2)])
453
+ self.func(G, 0, 2)
454
+
455
+ def test_nonexistent_nodes(self):
456
+ G = nx.complete_graph(5)
457
+ pytest.raises(nx.NetworkXError, nx.common_neighbors, G, 5, 4)
458
+ pytest.raises(nx.NetworkXError, nx.common_neighbors, G, 4, 5)
459
+ pytest.raises(nx.NetworkXError, nx.common_neighbors, G, 5, 6)
460
+
461
+ def test_custom1(self):
462
+ """Case of no common neighbors."""
463
+ G = nx.Graph()
464
+ G.add_nodes_from([0, 1])
465
+ self.test(G, 0, 1, [])
466
+
467
+ def test_custom2(self):
468
+ """Case of equal nodes."""
469
+ G = nx.complete_graph(4)
470
+ self.test(G, 0, 0, [1, 2, 3])
471
+
472
+
473
+ @pytest.mark.parametrize(
474
+ "graph_type", (nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph)
475
+ )
476
+ def test_set_node_attributes(graph_type):
477
+ # Test single value
478
+ G = nx.path_graph(3, create_using=graph_type)
479
+ vals = 100
480
+ attr = "hello"
481
+ nx.set_node_attributes(G, vals, attr)
482
+ assert G.nodes[0][attr] == vals
483
+ assert G.nodes[1][attr] == vals
484
+ assert G.nodes[2][attr] == vals
485
+
486
+ # Test dictionary
487
+ G = nx.path_graph(3, create_using=graph_type)
488
+ vals = dict(zip(sorted(G.nodes()), range(len(G))))
489
+ attr = "hi"
490
+ nx.set_node_attributes(G, vals, attr)
491
+ assert G.nodes[0][attr] == 0
492
+ assert G.nodes[1][attr] == 1
493
+ assert G.nodes[2][attr] == 2
494
+
495
+ # Test dictionary of dictionaries
496
+ G = nx.path_graph(3, create_using=graph_type)
497
+ d = {"hi": 0, "hello": 200}
498
+ vals = dict.fromkeys(G.nodes(), d)
499
+ vals.pop(0)
500
+ nx.set_node_attributes(G, vals)
501
+ assert G.nodes[0] == {}
502
+ assert G.nodes[1]["hi"] == 0
503
+ assert G.nodes[2]["hello"] == 200
504
+
505
+
506
+ @pytest.mark.parametrize(
507
+ ("values", "name"),
508
+ (
509
+ ({0: "red", 1: "blue"}, "color"), # values dictionary
510
+ ({0: {"color": "red"}, 1: {"color": "blue"}}, None), # dict-of-dict
511
+ ),
512
+ )
513
+ def test_set_node_attributes_ignores_extra_nodes(values, name):
514
+ """
515
+ When `values` is a dict or dict-of-dict keyed by nodes, ensure that keys
516
+ that correspond to nodes not in G are ignored.
517
+ """
518
+ G = nx.Graph()
519
+ G.add_node(0)
520
+ nx.set_node_attributes(G, values, name)
521
+ assert G.nodes[0]["color"] == "red"
522
+ assert 1 not in G.nodes
523
+
524
+
525
+ @pytest.mark.parametrize("graph_type", (nx.Graph, nx.DiGraph))
526
+ def test_set_edge_attributes(graph_type):
527
+ # Test single value
528
+ G = nx.path_graph(3, create_using=graph_type)
529
+ attr = "hello"
530
+ vals = 3
531
+ nx.set_edge_attributes(G, vals, attr)
532
+ assert G[0][1][attr] == vals
533
+ assert G[1][2][attr] == vals
534
+
535
+ # Test multiple values
536
+ G = nx.path_graph(3, create_using=graph_type)
537
+ attr = "hi"
538
+ edges = [(0, 1), (1, 2)]
539
+ vals = dict(zip(edges, range(len(edges))))
540
+ nx.set_edge_attributes(G, vals, attr)
541
+ assert G[0][1][attr] == 0
542
+ assert G[1][2][attr] == 1
543
+
544
+ # Test dictionary of dictionaries
545
+ G = nx.path_graph(3, create_using=graph_type)
546
+ d = {"hi": 0, "hello": 200}
547
+ edges = [(0, 1)]
548
+ vals = dict.fromkeys(edges, d)
549
+ nx.set_edge_attributes(G, vals)
550
+ assert G[0][1]["hi"] == 0
551
+ assert G[0][1]["hello"] == 200
552
+ assert G[1][2] == {}
553
+
554
+
555
+ @pytest.mark.parametrize(
556
+ ("values", "name"),
557
+ (
558
+ ({(0, 1): 1.0, (0, 2): 2.0}, "weight"), # values dict
559
+ ({(0, 1): {"weight": 1.0}, (0, 2): {"weight": 2.0}}, None), # values dod
560
+ ),
561
+ )
562
+ def test_set_edge_attributes_ignores_extra_edges(values, name):
563
+ """If `values` is a dict or dict-of-dicts containing edges that are not in
564
+ G, data associate with these edges should be ignored.
565
+ """
566
+ G = nx.Graph([(0, 1)])
567
+ nx.set_edge_attributes(G, values, name)
568
+ assert G[0][1]["weight"] == 1.0
569
+ assert (0, 2) not in G.edges
570
+
571
+
572
+ @pytest.mark.parametrize("graph_type", (nx.MultiGraph, nx.MultiDiGraph))
573
+ def test_set_edge_attributes_multi(graph_type):
574
+ # Test single value
575
+ G = nx.path_graph(3, create_using=graph_type)
576
+ attr = "hello"
577
+ vals = 3
578
+ nx.set_edge_attributes(G, vals, attr)
579
+ assert G[0][1][0][attr] == vals
580
+ assert G[1][2][0][attr] == vals
581
+
582
+ # Test multiple values
583
+ G = nx.path_graph(3, create_using=graph_type)
584
+ attr = "hi"
585
+ edges = [(0, 1, 0), (1, 2, 0)]
586
+ vals = dict(zip(edges, range(len(edges))))
587
+ nx.set_edge_attributes(G, vals, attr)
588
+ assert G[0][1][0][attr] == 0
589
+ assert G[1][2][0][attr] == 1
590
+
591
+ # Test dictionary of dictionaries
592
+ G = nx.path_graph(3, create_using=graph_type)
593
+ d = {"hi": 0, "hello": 200}
594
+ edges = [(0, 1, 0)]
595
+ vals = dict.fromkeys(edges, d)
596
+ nx.set_edge_attributes(G, vals)
597
+ assert G[0][1][0]["hi"] == 0
598
+ assert G[0][1][0]["hello"] == 200
599
+ assert G[1][2][0] == {}
600
+
601
+
602
+ @pytest.mark.parametrize(
603
+ ("values", "name"),
604
+ (
605
+ ({(0, 1, 0): 1.0, (0, 2, 0): 2.0}, "weight"), # values dict
606
+ ({(0, 1, 0): {"weight": 1.0}, (0, 2, 0): {"weight": 2.0}}, None), # values dod
607
+ ),
608
+ )
609
+ def test_set_edge_attributes_multi_ignores_extra_edges(values, name):
610
+ """If `values` is a dict or dict-of-dicts containing edges that are not in
611
+ G, data associate with these edges should be ignored.
612
+ """
613
+ G = nx.MultiGraph([(0, 1, 0), (0, 1, 1)])
614
+ nx.set_edge_attributes(G, values, name)
615
+ assert G[0][1][0]["weight"] == 1.0
616
+ assert G[0][1][1] == {}
617
+ assert (0, 2) not in G.edges()
618
+
619
+
620
+ def test_get_node_attributes():
621
+ graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()]
622
+ for G in graphs:
623
+ G = nx.path_graph(3, create_using=G)
624
+ attr = "hello"
625
+ vals = 100
626
+ nx.set_node_attributes(G, vals, attr)
627
+ attrs = nx.get_node_attributes(G, attr)
628
+ assert attrs[0] == vals
629
+ assert attrs[1] == vals
630
+ assert attrs[2] == vals
631
+ default_val = 1
632
+ G.add_node(4)
633
+ attrs = nx.get_node_attributes(G, attr, default=default_val)
634
+ assert attrs[4] == default_val
635
+
636
+
637
+ def test_get_edge_attributes():
638
+ graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()]
639
+ for G in graphs:
640
+ G = nx.path_graph(3, create_using=G)
641
+ attr = "hello"
642
+ vals = 100
643
+ nx.set_edge_attributes(G, vals, attr)
644
+ attrs = nx.get_edge_attributes(G, attr)
645
+ assert len(attrs) == 2
646
+
647
+ for edge in G.edges:
648
+ assert attrs[edge] == vals
649
+
650
+ default_val = vals
651
+ G.add_edge(4, 5)
652
+ deafult_attrs = nx.get_edge_attributes(G, attr, default=default_val)
653
+ assert len(deafult_attrs) == 3
654
+
655
+ for edge in G.edges:
656
+ assert deafult_attrs[edge] == vals
657
+
658
+
659
+ @pytest.mark.parametrize(
660
+ "graph_type", (nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph)
661
+ )
662
+ def test_remove_node_attributes(graph_type):
663
+ # Test removing single attribute
664
+ G = nx.path_graph(3, create_using=graph_type)
665
+ vals = 100
666
+ attr = "hello"
667
+ nx.set_node_attributes(G, vals, attr)
668
+ nx.remove_node_attributes(G, attr)
669
+ assert attr not in G.nodes[0]
670
+ assert attr not in G.nodes[1]
671
+ assert attr not in G.nodes[2]
672
+
673
+ # Test removing single attribute when multiple present
674
+ G = nx.path_graph(3, create_using=graph_type)
675
+ other_vals = 200
676
+ other_attr = "other"
677
+ nx.set_node_attributes(G, vals, attr)
678
+ nx.set_node_attributes(G, other_vals, other_attr)
679
+ nx.remove_node_attributes(G, attr)
680
+ assert attr not in G.nodes[0]
681
+ assert G.nodes[0][other_attr] == other_vals
682
+ assert attr not in G.nodes[1]
683
+ assert G.nodes[1][other_attr] == other_vals
684
+ assert attr not in G.nodes[2]
685
+ assert G.nodes[2][other_attr] == other_vals
686
+
687
+ # Test removing multiple attributes
688
+ G = nx.path_graph(3, create_using=graph_type)
689
+ nx.set_node_attributes(G, vals, attr)
690
+ nx.set_node_attributes(G, other_vals, other_attr)
691
+ nx.remove_node_attributes(G, attr, other_attr)
692
+ assert attr not in G.nodes[0] and other_attr not in G.nodes[0]
693
+ assert attr not in G.nodes[1] and other_attr not in G.nodes[1]
694
+ assert attr not in G.nodes[2] and other_attr not in G.nodes[2]
695
+
696
+ # Test removing multiple (but not all) attributes
697
+ G = nx.path_graph(3, create_using=graph_type)
698
+ third_vals = 300
699
+ third_attr = "three"
700
+ nx.set_node_attributes(
701
+ G,
702
+ {
703
+ n: {attr: vals, other_attr: other_vals, third_attr: third_vals}
704
+ for n in G.nodes()
705
+ },
706
+ )
707
+ nx.remove_node_attributes(G, other_attr, third_attr)
708
+ assert other_attr not in G.nodes[0] and third_attr not in G.nodes[0]
709
+ assert other_attr not in G.nodes[1] and third_attr not in G.nodes[1]
710
+ assert other_attr not in G.nodes[2] and third_attr not in G.nodes[2]
711
+ assert G.nodes[0][attr] == vals
712
+ assert G.nodes[1][attr] == vals
713
+ assert G.nodes[2][attr] == vals
714
+
715
+ # Test incomplete node attributes
716
+ G = nx.path_graph(3, create_using=graph_type)
717
+ nx.set_node_attributes(
718
+ G,
719
+ {
720
+ 1: {attr: vals, other_attr: other_vals},
721
+ 2: {attr: vals, other_attr: other_vals},
722
+ },
723
+ )
724
+ nx.remove_node_attributes(G, attr)
725
+ assert attr not in G.nodes[0]
726
+ assert attr not in G.nodes[1]
727
+ assert attr not in G.nodes[2]
728
+ assert G.nodes[1][other_attr] == other_vals
729
+ assert G.nodes[2][other_attr] == other_vals
730
+
731
+ # Test removing on a subset of nodes
732
+ G = nx.path_graph(3, create_using=graph_type)
733
+ nx.set_node_attributes(
734
+ G,
735
+ {
736
+ n: {attr: vals, other_attr: other_vals, third_attr: third_vals}
737
+ for n in G.nodes()
738
+ },
739
+ )
740
+ nx.remove_node_attributes(G, attr, other_attr, nbunch=[0, 1])
741
+ assert attr not in G.nodes[0] and other_attr not in G.nodes[0]
742
+ assert attr not in G.nodes[1] and other_attr not in G.nodes[1]
743
+ assert attr in G.nodes[2] and other_attr in G.nodes[2]
744
+ assert third_attr in G.nodes[0] and G.nodes[0][third_attr] == third_vals
745
+ assert third_attr in G.nodes[1] and G.nodes[1][third_attr] == third_vals
746
+
747
+
748
+ @pytest.mark.parametrize("graph_type", (nx.Graph, nx.DiGraph))
749
+ def test_remove_edge_attributes(graph_type):
750
+ # Test removing single attribute
751
+ G = nx.path_graph(3, create_using=graph_type)
752
+ attr = "hello"
753
+ vals = 100
754
+ nx.set_edge_attributes(G, vals, attr)
755
+ nx.remove_edge_attributes(G, attr)
756
+ assert len(nx.get_edge_attributes(G, attr)) == 0
757
+
758
+ # Test removing only some attributes
759
+ G = nx.path_graph(3, create_using=graph_type)
760
+ other_attr = "other"
761
+ other_vals = 200
762
+ nx.set_edge_attributes(G, vals, attr)
763
+ nx.set_edge_attributes(G, other_vals, other_attr)
764
+ nx.remove_edge_attributes(G, attr)
765
+
766
+ assert attr not in G[0][1]
767
+ assert attr not in G[1][2]
768
+ assert G[0][1][other_attr] == 200
769
+ assert G[1][2][other_attr] == 200
770
+
771
+ # Test removing multiple attributes
772
+ G = nx.path_graph(3, create_using=graph_type)
773
+ nx.set_edge_attributes(G, vals, attr)
774
+ nx.set_edge_attributes(G, other_vals, other_attr)
775
+ nx.remove_edge_attributes(G, attr, other_attr)
776
+ assert attr not in G[0][1] and other_attr not in G[0][1]
777
+ assert attr not in G[1][2] and other_attr not in G[1][2]
778
+
779
+ # Test removing multiple (not all) attributes
780
+ G = nx.path_graph(3, create_using=graph_type)
781
+ third_attr = "third"
782
+ third_vals = 300
783
+ nx.set_edge_attributes(
784
+ G,
785
+ {
786
+ (u, v): {attr: vals, other_attr: other_vals, third_attr: third_vals}
787
+ for u, v in G.edges()
788
+ },
789
+ )
790
+ nx.remove_edge_attributes(G, other_attr, third_attr)
791
+ assert other_attr not in G[0][1] and third_attr not in G[0][1]
792
+ assert other_attr not in G[1][2] and third_attr not in G[1][2]
793
+ assert G[0][1][attr] == vals
794
+ assert G[1][2][attr] == vals
795
+
796
+ # Test removing incomplete edge attributes
797
+ G = nx.path_graph(3, create_using=graph_type)
798
+ nx.set_edge_attributes(G, {(0, 1): {attr: vals, other_attr: other_vals}})
799
+ nx.remove_edge_attributes(G, other_attr)
800
+ assert other_attr not in G[0][1] and G[0][1][attr] == vals
801
+ assert other_attr not in G[1][2]
802
+
803
+ # Test removing subset of edge attributes
804
+ G = nx.path_graph(3, create_using=graph_type)
805
+ nx.set_edge_attributes(
806
+ G,
807
+ {
808
+ (u, v): {attr: vals, other_attr: other_vals, third_attr: third_vals}
809
+ for u, v in G.edges()
810
+ },
811
+ )
812
+ nx.remove_edge_attributes(G, other_attr, third_attr, ebunch=[(0, 1)])
813
+ assert other_attr not in G[0][1] and third_attr not in G[0][1]
814
+ assert other_attr in G[1][2] and third_attr in G[1][2]
815
+
816
+
817
+ @pytest.mark.parametrize("graph_type", (nx.MultiGraph, nx.MultiDiGraph))
818
+ def test_remove_multi_edge_attributes(graph_type):
819
+ # Test removing single attribute
820
+ G = nx.path_graph(3, create_using=graph_type)
821
+ G.add_edge(1, 2)
822
+ attr = "hello"
823
+ vals = 100
824
+ nx.set_edge_attributes(G, vals, attr)
825
+ nx.remove_edge_attributes(G, attr)
826
+ assert attr not in G[0][1][0]
827
+ assert attr not in G[1][2][0]
828
+ assert attr not in G[1][2][1]
829
+
830
+ # Test removing only some attributes
831
+ G = nx.path_graph(3, create_using=graph_type)
832
+ G.add_edge(1, 2)
833
+ other_attr = "other"
834
+ other_vals = 200
835
+ nx.set_edge_attributes(G, vals, attr)
836
+ nx.set_edge_attributes(G, other_vals, other_attr)
837
+ nx.remove_edge_attributes(G, attr)
838
+ assert attr not in G[0][1][0]
839
+ assert attr not in G[1][2][0]
840
+ assert attr not in G[1][2][1]
841
+ assert G[0][1][0][other_attr] == other_vals
842
+ assert G[1][2][0][other_attr] == other_vals
843
+ assert G[1][2][1][other_attr] == other_vals
844
+
845
+ # Test removing multiple attributes
846
+ G = nx.path_graph(3, create_using=graph_type)
847
+ G.add_edge(1, 2)
848
+ nx.set_edge_attributes(G, vals, attr)
849
+ nx.set_edge_attributes(G, other_vals, other_attr)
850
+ nx.remove_edge_attributes(G, attr, other_attr)
851
+ assert attr not in G[0][1][0] and other_attr not in G[0][1][0]
852
+ assert attr not in G[1][2][0] and other_attr not in G[1][2][0]
853
+ assert attr not in G[1][2][1] and other_attr not in G[1][2][1]
854
+
855
+ # Test removing multiple (not all) attributes
856
+ G = nx.path_graph(3, create_using=graph_type)
857
+ G.add_edge(1, 2)
858
+ third_attr = "third"
859
+ third_vals = 300
860
+ nx.set_edge_attributes(
861
+ G,
862
+ {
863
+ (u, v, k): {attr: vals, other_attr: other_vals, third_attr: third_vals}
864
+ for u, v, k in G.edges(keys=True)
865
+ },
866
+ )
867
+ nx.remove_edge_attributes(G, other_attr, third_attr)
868
+ assert other_attr not in G[0][1][0] and third_attr not in G[0][1][0]
869
+ assert other_attr not in G[1][2][0] and other_attr not in G[1][2][0]
870
+ assert other_attr not in G[1][2][1] and other_attr not in G[1][2][1]
871
+ assert G[0][1][0][attr] == vals
872
+ assert G[1][2][0][attr] == vals
873
+ assert G[1][2][1][attr] == vals
874
+
875
+ # Test removing incomplete edge attributes
876
+ G = nx.path_graph(3, create_using=graph_type)
877
+ G.add_edge(1, 2)
878
+ nx.set_edge_attributes(
879
+ G,
880
+ {
881
+ (0, 1, 0): {attr: vals, other_attr: other_vals},
882
+ (1, 2, 1): {attr: vals, other_attr: other_vals},
883
+ },
884
+ )
885
+ nx.remove_edge_attributes(G, other_attr)
886
+ assert other_attr not in G[0][1][0] and G[0][1][0][attr] == vals
887
+ assert other_attr not in G[1][2][0]
888
+ assert other_attr not in G[1][2][1]
889
+
890
+ # Test removing subset of edge attributes
891
+ G = nx.path_graph(3, create_using=graph_type)
892
+ G.add_edge(1, 2)
893
+ nx.set_edge_attributes(
894
+ G,
895
+ {
896
+ (0, 1, 0): {attr: vals, other_attr: other_vals},
897
+ (1, 2, 0): {attr: vals, other_attr: other_vals},
898
+ (1, 2, 1): {attr: vals, other_attr: other_vals},
899
+ },
900
+ )
901
+ nx.remove_edge_attributes(G, attr, ebunch=[(0, 1, 0), (1, 2, 0)])
902
+ assert attr not in G[0][1][0] and other_attr in G[0][1][0]
903
+ assert attr not in G[1][2][0] and other_attr in G[1][2][0]
904
+ assert attr in G[1][2][1] and other_attr in G[1][2][1]
905
+
906
+
907
+ def test_is_empty():
908
+ graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()]
909
+ for G in graphs:
910
+ assert nx.is_empty(G)
911
+ G.add_nodes_from(range(5))
912
+ assert nx.is_empty(G)
913
+ G.add_edges_from([(1, 2), (3, 4)])
914
+ assert not nx.is_empty(G)
915
+
916
+
917
+ @pytest.mark.parametrize(
918
+ "graph_type", [nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph]
919
+ )
920
+ def test_selfloops(graph_type):
921
+ G = nx.complete_graph(3, create_using=graph_type)
922
+ G.add_edge(0, 0)
923
+ assert nodes_equal(nx.nodes_with_selfloops(G), [0])
924
+ assert edges_equal(nx.selfloop_edges(G), [(0, 0)])
925
+ assert edges_equal(nx.selfloop_edges(G, data=True), [(0, 0, {})])
926
+ assert nx.number_of_selfloops(G) == 1
927
+
928
+
929
+ @pytest.mark.parametrize(
930
+ "graph_type", [nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph]
931
+ )
932
+ def test_selfloop_edges_attr(graph_type):
933
+ G = nx.complete_graph(3, create_using=graph_type)
934
+ G.add_edge(0, 0)
935
+ G.add_edge(1, 1, weight=2)
936
+ assert edges_equal(
937
+ nx.selfloop_edges(G, data=True), [(0, 0, {}), (1, 1, {"weight": 2})]
938
+ )
939
+ assert edges_equal(nx.selfloop_edges(G, data="weight"), [(0, 0, None), (1, 1, 2)])
940
+
941
+
942
+ def test_selfloop_edges_multi_with_data_and_keys():
943
+ G = nx.complete_graph(3, create_using=nx.MultiGraph)
944
+ G.add_edge(0, 0, weight=10)
945
+ G.add_edge(0, 0, weight=100)
946
+ assert edges_equal(
947
+ nx.selfloop_edges(G, data="weight", keys=True), [(0, 0, 0, 10), (0, 0, 1, 100)]
948
+ )
949
+
950
+
951
+ @pytest.mark.parametrize("graph_type", [nx.Graph, nx.DiGraph])
952
+ def test_selfloops_removal(graph_type):
953
+ G = nx.complete_graph(3, create_using=graph_type)
954
+ G.add_edge(0, 0)
955
+ G.remove_edges_from(nx.selfloop_edges(G, keys=True))
956
+ G.add_edge(0, 0)
957
+ G.remove_edges_from(nx.selfloop_edges(G, data=True))
958
+ G.add_edge(0, 0)
959
+ G.remove_edges_from(nx.selfloop_edges(G, keys=True, data=True))
960
+
961
+
962
+ @pytest.mark.parametrize("graph_type", [nx.MultiGraph, nx.MultiDiGraph])
963
+ def test_selfloops_removal_multi(graph_type):
964
+ """test removing selfloops behavior vis-a-vis altering a dict while iterating.
965
+ cf. gh-4068"""
966
+ G = nx.complete_graph(3, create_using=graph_type)
967
+ # Defaults - see gh-4080
968
+ G.add_edge(0, 0)
969
+ G.add_edge(0, 0)
970
+ G.remove_edges_from(nx.selfloop_edges(G))
971
+ assert (0, 0) not in G.edges()
972
+ # With keys
973
+ G.add_edge(0, 0)
974
+ G.add_edge(0, 0)
975
+ with pytest.raises(RuntimeError):
976
+ G.remove_edges_from(nx.selfloop_edges(G, keys=True))
977
+ # With data
978
+ G.add_edge(0, 0)
979
+ G.add_edge(0, 0)
980
+ with pytest.raises(TypeError):
981
+ G.remove_edges_from(nx.selfloop_edges(G, data=True))
982
+ # With keys and data
983
+ G.add_edge(0, 0)
984
+ G.add_edge(0, 0)
985
+ with pytest.raises(RuntimeError):
986
+ G.remove_edges_from(nx.selfloop_edges(G, data=True, keys=True))
987
+
988
+
989
+ def test_pathweight():
990
+ valid_path = [1, 2, 3]
991
+ invalid_path = [1, 3, 2]
992
+ graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()]
993
+ edges = [
994
+ (1, 2, {"cost": 5, "dist": 6}),
995
+ (2, 3, {"cost": 3, "dist": 4}),
996
+ (1, 2, {"cost": 1, "dist": 2}),
997
+ ]
998
+ for graph in graphs:
999
+ graph.add_edges_from(edges)
1000
+ assert nx.path_weight(graph, valid_path, "cost") == 4
1001
+ assert nx.path_weight(graph, valid_path, "dist") == 6
1002
+ pytest.raises(nx.NetworkXNoPath, nx.path_weight, graph, invalid_path, "cost")
1003
+
1004
+
1005
+ @pytest.mark.parametrize(
1006
+ "G", (nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph())
1007
+ )
1008
+ def test_ispath(G):
1009
+ G.add_edges_from([(1, 2), (2, 3), (1, 2), (3, 4)])
1010
+ valid_path = [1, 2, 3, 4]
1011
+ invalid_path = [1, 2, 4, 3] # wrong node order
1012
+ another_invalid_path = [1, 2, 3, 4, 5] # contains node not in G
1013
+ assert nx.is_path(G, valid_path)
1014
+ assert not nx.is_path(G, invalid_path)
1015
+ assert not nx.is_path(G, another_invalid_path)
1016
+
1017
+
1018
+ @pytest.mark.parametrize("G", (nx.Graph(), nx.DiGraph()))
1019
+ def test_restricted_view(G):
1020
+ G.add_edges_from([(0, 1), (0, 2), (0, 3), (1, 0), (1, 1), (1, 2)])
1021
+ G.add_node(4)
1022
+ H = nx.restricted_view(G, [0, 2, 5], [(1, 2), (3, 4)])
1023
+ assert set(H.nodes()) == {1, 3, 4}
1024
+ assert set(H.edges()) == {(1, 1)}
1025
+
1026
+
1027
+ @pytest.mark.parametrize("G", (nx.MultiGraph(), nx.MultiDiGraph()))
1028
+ def test_restricted_view_multi(G):
1029
+ G.add_edges_from(
1030
+ [(0, 1, 0), (0, 2, 0), (0, 3, 0), (0, 1, 1), (1, 0, 0), (1, 1, 0), (1, 2, 0)]
1031
+ )
1032
+ G.add_node(4)
1033
+ H = nx.restricted_view(G, [0, 2, 5], [(1, 2, 0), (3, 4, 0)])
1034
+ assert set(H.nodes()) == {1, 3, 4}
1035
+ assert set(H.edges()) == {(1, 1)}
minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_graph.py ADDED
@@ -0,0 +1,920 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gc
2
+ import pickle
3
+ import platform
4
+ import weakref
5
+
6
+ import pytest
7
+
8
+ import networkx as nx
9
+ from networkx.utils import edges_equal, graphs_equal, nodes_equal
10
+
11
+
12
+ class BaseGraphTester:
13
+ """Tests for data-structure independent graph class features."""
14
+
15
+ def test_contains(self):
16
+ G = self.K3
17
+ assert 1 in G
18
+ assert 4 not in G
19
+ assert "b" not in G
20
+ assert [] not in G # no exception for nonhashable
21
+ assert {1: 1} not in G # no exception for nonhashable
22
+
23
+ def test_order(self):
24
+ G = self.K3
25
+ assert len(G) == 3
26
+ assert G.order() == 3
27
+ assert G.number_of_nodes() == 3
28
+
29
+ def test_nodes(self):
30
+ G = self.K3
31
+ assert isinstance(G._node, G.node_dict_factory)
32
+ assert isinstance(G._adj, G.adjlist_outer_dict_factory)
33
+ assert all(
34
+ isinstance(adj, G.adjlist_inner_dict_factory) for adj in G._adj.values()
35
+ )
36
+ assert sorted(G.nodes()) == self.k3nodes
37
+ assert sorted(G.nodes(data=True)) == [(0, {}), (1, {}), (2, {})]
38
+
39
+ def test_none_node(self):
40
+ G = self.Graph()
41
+ with pytest.raises(ValueError):
42
+ G.add_node(None)
43
+ with pytest.raises(ValueError):
44
+ G.add_nodes_from([None])
45
+ with pytest.raises(ValueError):
46
+ G.add_edge(0, None)
47
+ with pytest.raises(ValueError):
48
+ G.add_edges_from([(0, None)])
49
+
50
+ def test_has_node(self):
51
+ G = self.K3
52
+ assert G.has_node(1)
53
+ assert not G.has_node(4)
54
+ assert not G.has_node([]) # no exception for nonhashable
55
+ assert not G.has_node({1: 1}) # no exception for nonhashable
56
+
57
+ def test_has_edge(self):
58
+ G = self.K3
59
+ assert G.has_edge(0, 1)
60
+ assert not G.has_edge(0, -1)
61
+
62
+ def test_neighbors(self):
63
+ G = self.K3
64
+ assert sorted(G.neighbors(0)) == [1, 2]
65
+ with pytest.raises(nx.NetworkXError):
66
+ G.neighbors(-1)
67
+
68
+ @pytest.mark.skipif(
69
+ platform.python_implementation() == "PyPy", reason="PyPy gc is different"
70
+ )
71
+ def test_memory_leak(self):
72
+ G = self.Graph()
73
+
74
+ def count_objects_of_type(_type):
75
+ # Iterating over all objects tracked by gc can include weak references
76
+ # whose weakly-referenced objects may no longer exist. Calling `isinstance`
77
+ # on such a weak reference will raise ReferenceError. There are at least
78
+ # three workarounds for this: one is to compare type names instead of using
79
+ # `isinstance` such as `type(obj).__name__ == typename`, another is to use
80
+ # `type(obj) == _type`, and the last is to ignore ProxyTypes as we do below.
81
+ # NOTE: even if this safeguard is deemed unnecessary to pass NetworkX tests,
82
+ # we should still keep it for maximum safety for other NetworkX backends.
83
+ return sum(
84
+ 1
85
+ for obj in gc.get_objects()
86
+ if not isinstance(obj, weakref.ProxyTypes) and isinstance(obj, _type)
87
+ )
88
+
89
+ gc.collect()
90
+ before = count_objects_of_type(self.Graph)
91
+ G.copy()
92
+ gc.collect()
93
+ after = count_objects_of_type(self.Graph)
94
+ assert before == after
95
+
96
+ # test a subgraph of the base class
97
+ class MyGraph(self.Graph):
98
+ pass
99
+
100
+ gc.collect()
101
+ G = MyGraph()
102
+ before = count_objects_of_type(MyGraph)
103
+ G.copy()
104
+ gc.collect()
105
+ after = count_objects_of_type(MyGraph)
106
+ assert before == after
107
+
108
+ def test_edges(self):
109
+ G = self.K3
110
+ assert isinstance(G._adj, G.adjlist_outer_dict_factory)
111
+ assert edges_equal(G.edges(), [(0, 1), (0, 2), (1, 2)])
112
+ assert edges_equal(G.edges(0), [(0, 1), (0, 2)])
113
+ assert edges_equal(G.edges([0, 1]), [(0, 1), (0, 2), (1, 2)])
114
+ with pytest.raises(nx.NetworkXError):
115
+ G.edges(-1)
116
+
117
+ def test_degree(self):
118
+ G = self.K3
119
+ assert sorted(G.degree()) == [(0, 2), (1, 2), (2, 2)]
120
+ assert dict(G.degree()) == {0: 2, 1: 2, 2: 2}
121
+ assert G.degree(0) == 2
122
+ with pytest.raises(nx.NetworkXError):
123
+ G.degree(-1) # node not in graph
124
+
125
+ def test_size(self):
126
+ G = self.K3
127
+ assert G.size() == 3
128
+ assert G.number_of_edges() == 3
129
+
130
+ def test_nbunch_iter(self):
131
+ G = self.K3
132
+ assert nodes_equal(G.nbunch_iter(), self.k3nodes) # all nodes
133
+ assert nodes_equal(G.nbunch_iter(0), [0]) # single node
134
+ assert nodes_equal(G.nbunch_iter([0, 1]), [0, 1]) # sequence
135
+ # sequence with none in graph
136
+ assert nodes_equal(G.nbunch_iter([-1]), [])
137
+ # string sequence with none in graph
138
+ assert nodes_equal(G.nbunch_iter("foo"), [])
139
+ # node not in graph doesn't get caught upon creation of iterator
140
+ bunch = G.nbunch_iter(-1)
141
+ # but gets caught when iterator used
142
+ with pytest.raises(nx.NetworkXError, match="is not a node or a sequence"):
143
+ list(bunch)
144
+ # unhashable doesn't get caught upon creation of iterator
145
+ bunch = G.nbunch_iter([0, 1, 2, {}])
146
+ # but gets caught when iterator hits the unhashable
147
+ with pytest.raises(
148
+ nx.NetworkXError, match="in sequence nbunch is not a valid node"
149
+ ):
150
+ list(bunch)
151
+
152
+ def test_nbunch_iter_node_format_raise(self):
153
+ # Tests that a node that would have failed string formatting
154
+ # doesn't cause an error when attempting to raise a
155
+ # :exc:`nx.NetworkXError`.
156
+
157
+ # For more information, see pull request #1813.
158
+ G = self.Graph()
159
+ nbunch = [("x", set())]
160
+ with pytest.raises(nx.NetworkXError):
161
+ list(G.nbunch_iter(nbunch))
162
+
163
+ def test_selfloop_degree(self):
164
+ G = self.Graph()
165
+ G.add_edge(1, 1)
166
+ assert sorted(G.degree()) == [(1, 2)]
167
+ assert dict(G.degree()) == {1: 2}
168
+ assert G.degree(1) == 2
169
+ assert sorted(G.degree([1])) == [(1, 2)]
170
+ assert G.degree(1, weight="weight") == 2
171
+
172
+ def test_selfloops(self):
173
+ G = self.K3.copy()
174
+ G.add_edge(0, 0)
175
+ assert nodes_equal(nx.nodes_with_selfloops(G), [0])
176
+ assert edges_equal(nx.selfloop_edges(G), [(0, 0)])
177
+ assert nx.number_of_selfloops(G) == 1
178
+ G.remove_edge(0, 0)
179
+ G.add_edge(0, 0)
180
+ G.remove_edges_from([(0, 0)])
181
+ G.add_edge(1, 1)
182
+ G.remove_node(1)
183
+ G.add_edge(0, 0)
184
+ G.add_edge(1, 1)
185
+ G.remove_nodes_from([0, 1])
186
+
187
+ def test_cache_reset(self):
188
+ G = self.K3.copy()
189
+ old_adj = G.adj
190
+ assert id(G.adj) == id(old_adj)
191
+ G._adj = {}
192
+ assert id(G.adj) != id(old_adj)
193
+
194
+ old_nodes = G.nodes
195
+ assert id(G.nodes) == id(old_nodes)
196
+ G._node = {}
197
+ assert id(G.nodes) != id(old_nodes)
198
+
199
+ def test_attributes_cached(self):
200
+ G = self.K3.copy()
201
+ assert id(G.nodes) == id(G.nodes)
202
+ assert id(G.edges) == id(G.edges)
203
+ assert id(G.degree) == id(G.degree)
204
+ assert id(G.adj) == id(G.adj)
205
+
206
+
207
+ class BaseAttrGraphTester(BaseGraphTester):
208
+ """Tests of graph class attribute features."""
209
+
210
+ def test_weighted_degree(self):
211
+ G = self.Graph()
212
+ G.add_edge(1, 2, weight=2, other=3)
213
+ G.add_edge(2, 3, weight=3, other=4)
214
+ assert sorted(d for n, d in G.degree(weight="weight")) == [2, 3, 5]
215
+ assert dict(G.degree(weight="weight")) == {1: 2, 2: 5, 3: 3}
216
+ assert G.degree(1, weight="weight") == 2
217
+ assert nodes_equal((G.degree([1], weight="weight")), [(1, 2)])
218
+
219
+ assert nodes_equal((d for n, d in G.degree(weight="other")), [3, 7, 4])
220
+ assert dict(G.degree(weight="other")) == {1: 3, 2: 7, 3: 4}
221
+ assert G.degree(1, weight="other") == 3
222
+ assert edges_equal((G.degree([1], weight="other")), [(1, 3)])
223
+
224
+ def add_attributes(self, G):
225
+ G.graph["foo"] = []
226
+ G.nodes[0]["foo"] = []
227
+ G.remove_edge(1, 2)
228
+ ll = []
229
+ G.add_edge(1, 2, foo=ll)
230
+ G.add_edge(2, 1, foo=ll)
231
+
232
+ def test_name(self):
233
+ G = self.Graph(name="")
234
+ assert G.name == ""
235
+ G = self.Graph(name="test")
236
+ assert G.name == "test"
237
+
238
+ def test_str_unnamed(self):
239
+ G = self.Graph()
240
+ G.add_edges_from([(1, 2), (2, 3)])
241
+ assert str(G) == f"{type(G).__name__} with 3 nodes and 2 edges"
242
+
243
+ def test_str_named(self):
244
+ G = self.Graph(name="foo")
245
+ G.add_edges_from([(1, 2), (2, 3)])
246
+ assert str(G) == f"{type(G).__name__} named 'foo' with 3 nodes and 2 edges"
247
+
248
+ def test_graph_chain(self):
249
+ G = self.Graph([(0, 1), (1, 2)])
250
+ DG = G.to_directed(as_view=True)
251
+ SDG = DG.subgraph([0, 1])
252
+ RSDG = SDG.reverse(copy=False)
253
+ assert G is DG._graph
254
+ assert DG is SDG._graph
255
+ assert SDG is RSDG._graph
256
+
257
+ def test_copy(self):
258
+ G = self.Graph()
259
+ G.add_node(0)
260
+ G.add_edge(1, 2)
261
+ self.add_attributes(G)
262
+ # copy edge datadict but any container attr are same
263
+ H = G.copy()
264
+ self.graphs_equal(H, G)
265
+ self.different_attrdict(H, G)
266
+ self.shallow_copy_attrdict(H, G)
267
+
268
+ def test_class_copy(self):
269
+ G = self.Graph()
270
+ G.add_node(0)
271
+ G.add_edge(1, 2)
272
+ self.add_attributes(G)
273
+ # copy edge datadict but any container attr are same
274
+ H = G.__class__(G)
275
+ self.graphs_equal(H, G)
276
+ self.different_attrdict(H, G)
277
+ self.shallow_copy_attrdict(H, G)
278
+
279
+ def test_fresh_copy(self):
280
+ G = self.Graph()
281
+ G.add_node(0)
282
+ G.add_edge(1, 2)
283
+ self.add_attributes(G)
284
+ # copy graph structure but use fresh datadict
285
+ H = G.__class__()
286
+ H.add_nodes_from(G)
287
+ H.add_edges_from(G.edges())
288
+ assert len(G.nodes[0]) == 1
289
+ ddict = G.adj[1][2][0] if G.is_multigraph() else G.adj[1][2]
290
+ assert len(ddict) == 1
291
+ assert len(H.nodes[0]) == 0
292
+ ddict = H.adj[1][2][0] if H.is_multigraph() else H.adj[1][2]
293
+ assert len(ddict) == 0
294
+
295
+ def is_deepcopy(self, H, G):
296
+ self.graphs_equal(H, G)
297
+ self.different_attrdict(H, G)
298
+ self.deep_copy_attrdict(H, G)
299
+
300
+ def deep_copy_attrdict(self, H, G):
301
+ self.deepcopy_graph_attr(H, G)
302
+ self.deepcopy_node_attr(H, G)
303
+ self.deepcopy_edge_attr(H, G)
304
+
305
+ def deepcopy_graph_attr(self, H, G):
306
+ assert G.graph["foo"] == H.graph["foo"]
307
+ G.graph["foo"].append(1)
308
+ assert G.graph["foo"] != H.graph["foo"]
309
+
310
+ def deepcopy_node_attr(self, H, G):
311
+ assert G.nodes[0]["foo"] == H.nodes[0]["foo"]
312
+ G.nodes[0]["foo"].append(1)
313
+ assert G.nodes[0]["foo"] != H.nodes[0]["foo"]
314
+
315
+ def deepcopy_edge_attr(self, H, G):
316
+ assert G[1][2]["foo"] == H[1][2]["foo"]
317
+ G[1][2]["foo"].append(1)
318
+ assert G[1][2]["foo"] != H[1][2]["foo"]
319
+
320
+ def is_shallow_copy(self, H, G):
321
+ self.graphs_equal(H, G)
322
+ self.shallow_copy_attrdict(H, G)
323
+
324
+ def shallow_copy_attrdict(self, H, G):
325
+ self.shallow_copy_graph_attr(H, G)
326
+ self.shallow_copy_node_attr(H, G)
327
+ self.shallow_copy_edge_attr(H, G)
328
+
329
+ def shallow_copy_graph_attr(self, H, G):
330
+ assert G.graph["foo"] == H.graph["foo"]
331
+ G.graph["foo"].append(1)
332
+ assert G.graph["foo"] == H.graph["foo"]
333
+
334
+ def shallow_copy_node_attr(self, H, G):
335
+ assert G.nodes[0]["foo"] == H.nodes[0]["foo"]
336
+ G.nodes[0]["foo"].append(1)
337
+ assert G.nodes[0]["foo"] == H.nodes[0]["foo"]
338
+
339
+ def shallow_copy_edge_attr(self, H, G):
340
+ assert G[1][2]["foo"] == H[1][2]["foo"]
341
+ G[1][2]["foo"].append(1)
342
+ assert G[1][2]["foo"] == H[1][2]["foo"]
343
+
344
+ def same_attrdict(self, H, G):
345
+ old_foo = H[1][2]["foo"]
346
+ H.adj[1][2]["foo"] = "baz"
347
+ assert G.edges == H.edges
348
+ H.adj[1][2]["foo"] = old_foo
349
+ assert G.edges == H.edges
350
+
351
+ old_foo = H.nodes[0]["foo"]
352
+ H.nodes[0]["foo"] = "baz"
353
+ assert G.nodes == H.nodes
354
+ H.nodes[0]["foo"] = old_foo
355
+ assert G.nodes == H.nodes
356
+
357
+ def different_attrdict(self, H, G):
358
+ old_foo = H[1][2]["foo"]
359
+ H.adj[1][2]["foo"] = "baz"
360
+ assert G._adj != H._adj
361
+ H.adj[1][2]["foo"] = old_foo
362
+ assert G._adj == H._adj
363
+
364
+ old_foo = H.nodes[0]["foo"]
365
+ H.nodes[0]["foo"] = "baz"
366
+ assert G._node != H._node
367
+ H.nodes[0]["foo"] = old_foo
368
+ assert G._node == H._node
369
+
370
+ def graphs_equal(self, H, G):
371
+ assert G._adj == H._adj
372
+ assert G._node == H._node
373
+ assert G.graph == H.graph
374
+ assert G.name == H.name
375
+ if not G.is_directed() and not H.is_directed():
376
+ assert H._adj[1][2] is H._adj[2][1]
377
+ assert G._adj[1][2] is G._adj[2][1]
378
+ else: # at least one is directed
379
+ if not G.is_directed():
380
+ G._pred = G._adj
381
+ G._succ = G._adj
382
+ if not H.is_directed():
383
+ H._pred = H._adj
384
+ H._succ = H._adj
385
+ assert G._pred == H._pred
386
+ assert G._succ == H._succ
387
+ assert H._succ[1][2] is H._pred[2][1]
388
+ assert G._succ[1][2] is G._pred[2][1]
389
+
390
+ def test_graph_attr(self):
391
+ G = self.K3.copy()
392
+ G.graph["foo"] = "bar"
393
+ assert isinstance(G.graph, G.graph_attr_dict_factory)
394
+ assert G.graph["foo"] == "bar"
395
+ del G.graph["foo"]
396
+ assert G.graph == {}
397
+ H = self.Graph(foo="bar")
398
+ assert H.graph["foo"] == "bar"
399
+
400
+ def test_node_attr(self):
401
+ G = self.K3.copy()
402
+ G.add_node(1, foo="bar")
403
+ assert all(
404
+ isinstance(d, G.node_attr_dict_factory) for u, d in G.nodes(data=True)
405
+ )
406
+ assert nodes_equal(G.nodes(), [0, 1, 2])
407
+ assert nodes_equal(G.nodes(data=True), [(0, {}), (1, {"foo": "bar"}), (2, {})])
408
+ G.nodes[1]["foo"] = "baz"
409
+ assert nodes_equal(G.nodes(data=True), [(0, {}), (1, {"foo": "baz"}), (2, {})])
410
+ assert nodes_equal(G.nodes(data="foo"), [(0, None), (1, "baz"), (2, None)])
411
+ assert nodes_equal(
412
+ G.nodes(data="foo", default="bar"), [(0, "bar"), (1, "baz"), (2, "bar")]
413
+ )
414
+
415
+ def test_node_attr2(self):
416
+ G = self.K3.copy()
417
+ a = {"foo": "bar"}
418
+ G.add_node(3, **a)
419
+ assert nodes_equal(G.nodes(), [0, 1, 2, 3])
420
+ assert nodes_equal(
421
+ G.nodes(data=True), [(0, {}), (1, {}), (2, {}), (3, {"foo": "bar"})]
422
+ )
423
+
424
+ def test_edge_lookup(self):
425
+ G = self.Graph()
426
+ G.add_edge(1, 2, foo="bar")
427
+ assert edges_equal(G.edges[1, 2], {"foo": "bar"})
428
+
429
+ def test_edge_attr(self):
430
+ G = self.Graph()
431
+ G.add_edge(1, 2, foo="bar")
432
+ assert all(
433
+ isinstance(d, G.edge_attr_dict_factory) for u, v, d in G.edges(data=True)
434
+ )
435
+ assert edges_equal(G.edges(data=True), [(1, 2, {"foo": "bar"})])
436
+ assert edges_equal(G.edges(data="foo"), [(1, 2, "bar")])
437
+
438
+ def test_edge_attr2(self):
439
+ G = self.Graph()
440
+ G.add_edges_from([(1, 2), (3, 4)], foo="foo")
441
+ assert edges_equal(
442
+ G.edges(data=True), [(1, 2, {"foo": "foo"}), (3, 4, {"foo": "foo"})]
443
+ )
444
+ assert edges_equal(G.edges(data="foo"), [(1, 2, "foo"), (3, 4, "foo")])
445
+
446
+ def test_edge_attr3(self):
447
+ G = self.Graph()
448
+ G.add_edges_from([(1, 2, {"weight": 32}), (3, 4, {"weight": 64})], foo="foo")
449
+ assert edges_equal(
450
+ G.edges(data=True),
451
+ [
452
+ (1, 2, {"foo": "foo", "weight": 32}),
453
+ (3, 4, {"foo": "foo", "weight": 64}),
454
+ ],
455
+ )
456
+
457
+ G.remove_edges_from([(1, 2), (3, 4)])
458
+ G.add_edge(1, 2, data=7, spam="bar", bar="foo")
459
+ assert edges_equal(
460
+ G.edges(data=True), [(1, 2, {"data": 7, "spam": "bar", "bar": "foo"})]
461
+ )
462
+
463
+ def test_edge_attr4(self):
464
+ G = self.Graph()
465
+ G.add_edge(1, 2, data=7, spam="bar", bar="foo")
466
+ assert edges_equal(
467
+ G.edges(data=True), [(1, 2, {"data": 7, "spam": "bar", "bar": "foo"})]
468
+ )
469
+ G[1][2]["data"] = 10 # OK to set data like this
470
+ assert edges_equal(
471
+ G.edges(data=True), [(1, 2, {"data": 10, "spam": "bar", "bar": "foo"})]
472
+ )
473
+
474
+ G.adj[1][2]["data"] = 20
475
+ assert edges_equal(
476
+ G.edges(data=True), [(1, 2, {"data": 20, "spam": "bar", "bar": "foo"})]
477
+ )
478
+ G.edges[1, 2]["data"] = 21 # another spelling, "edge"
479
+ assert edges_equal(
480
+ G.edges(data=True), [(1, 2, {"data": 21, "spam": "bar", "bar": "foo"})]
481
+ )
482
+ G.adj[1][2]["listdata"] = [20, 200]
483
+ G.adj[1][2]["weight"] = 20
484
+ dd = {
485
+ "data": 21,
486
+ "spam": "bar",
487
+ "bar": "foo",
488
+ "listdata": [20, 200],
489
+ "weight": 20,
490
+ }
491
+ assert edges_equal(G.edges(data=True), [(1, 2, dd)])
492
+
493
+ def test_to_undirected(self):
494
+ G = self.K3
495
+ self.add_attributes(G)
496
+ H = nx.Graph(G)
497
+ self.is_shallow_copy(H, G)
498
+ self.different_attrdict(H, G)
499
+ H = G.to_undirected()
500
+ self.is_deepcopy(H, G)
501
+
502
+ def test_to_directed_as_view(self):
503
+ H = nx.path_graph(2, create_using=self.Graph)
504
+ H2 = H.to_directed(as_view=True)
505
+ assert H is H2._graph
506
+ assert H2.has_edge(0, 1)
507
+ assert H2.has_edge(1, 0) or H.is_directed()
508
+ pytest.raises(nx.NetworkXError, H2.add_node, -1)
509
+ pytest.raises(nx.NetworkXError, H2.add_edge, 1, 2)
510
+ H.add_edge(1, 2)
511
+ assert H2.has_edge(1, 2)
512
+ assert H2.has_edge(2, 1) or H.is_directed()
513
+
514
+ def test_to_undirected_as_view(self):
515
+ H = nx.path_graph(2, create_using=self.Graph)
516
+ H2 = H.to_undirected(as_view=True)
517
+ assert H is H2._graph
518
+ assert H2.has_edge(0, 1)
519
+ assert H2.has_edge(1, 0)
520
+ pytest.raises(nx.NetworkXError, H2.add_node, -1)
521
+ pytest.raises(nx.NetworkXError, H2.add_edge, 1, 2)
522
+ H.add_edge(1, 2)
523
+ assert H2.has_edge(1, 2)
524
+ assert H2.has_edge(2, 1)
525
+
526
+ def test_directed_class(self):
527
+ G = self.Graph()
528
+
529
+ class newGraph(G.to_undirected_class()):
530
+ def to_directed_class(self):
531
+ return newDiGraph
532
+
533
+ def to_undirected_class(self):
534
+ return newGraph
535
+
536
+ class newDiGraph(G.to_directed_class()):
537
+ def to_directed_class(self):
538
+ return newDiGraph
539
+
540
+ def to_undirected_class(self):
541
+ return newGraph
542
+
543
+ G = newDiGraph() if G.is_directed() else newGraph()
544
+ H = G.to_directed()
545
+ assert isinstance(H, newDiGraph)
546
+ H = G.to_undirected()
547
+ assert isinstance(H, newGraph)
548
+
549
+ def test_to_directed(self):
550
+ G = self.K3
551
+ self.add_attributes(G)
552
+ H = nx.DiGraph(G)
553
+ self.is_shallow_copy(H, G)
554
+ self.different_attrdict(H, G)
555
+ H = G.to_directed()
556
+ self.is_deepcopy(H, G)
557
+
558
+ def test_subgraph(self):
559
+ G = self.K3
560
+ self.add_attributes(G)
561
+ H = G.subgraph([0, 1, 2, 5])
562
+ self.graphs_equal(H, G)
563
+ self.same_attrdict(H, G)
564
+ self.shallow_copy_attrdict(H, G)
565
+
566
+ H = G.subgraph(0)
567
+ assert H.adj == {0: {}}
568
+ H = G.subgraph([])
569
+ assert H.adj == {}
570
+ assert G.adj != {}
571
+
572
+ def test_selfloops_attr(self):
573
+ G = self.K3.copy()
574
+ G.add_edge(0, 0)
575
+ G.add_edge(1, 1, weight=2)
576
+ assert edges_equal(
577
+ nx.selfloop_edges(G, data=True), [(0, 0, {}), (1, 1, {"weight": 2})]
578
+ )
579
+ assert edges_equal(
580
+ nx.selfloop_edges(G, data="weight"), [(0, 0, None), (1, 1, 2)]
581
+ )
582
+
583
+
584
+ class TestGraph(BaseAttrGraphTester):
585
+ """Tests specific to dict-of-dict-of-dict graph data structure"""
586
+
587
+ def setup_method(self):
588
+ self.Graph = nx.Graph
589
+ # build dict-of-dict-of-dict K3
590
+ ed1, ed2, ed3 = ({}, {}, {})
591
+ self.k3adj = {0: {1: ed1, 2: ed2}, 1: {0: ed1, 2: ed3}, 2: {0: ed2, 1: ed3}}
592
+ self.k3edges = [(0, 1), (0, 2), (1, 2)]
593
+ self.k3nodes = [0, 1, 2]
594
+ self.K3 = self.Graph()
595
+ self.K3._adj = self.k3adj
596
+ self.K3._node = {}
597
+ self.K3._node[0] = {}
598
+ self.K3._node[1] = {}
599
+ self.K3._node[2] = {}
600
+
601
+ def test_pickle(self):
602
+ G = self.K3
603
+ pg = pickle.loads(pickle.dumps(G, -1))
604
+ self.graphs_equal(pg, G)
605
+ pg = pickle.loads(pickle.dumps(G))
606
+ self.graphs_equal(pg, G)
607
+
608
+ def test_data_input(self):
609
+ G = self.Graph({1: [2], 2: [1]}, name="test")
610
+ assert G.name == "test"
611
+ assert sorted(G.adj.items()) == [(1, {2: {}}), (2, {1: {}})]
612
+
613
+ def test_adjacency(self):
614
+ G = self.K3
615
+ assert dict(G.adjacency()) == {
616
+ 0: {1: {}, 2: {}},
617
+ 1: {0: {}, 2: {}},
618
+ 2: {0: {}, 1: {}},
619
+ }
620
+
621
+ def test_getitem(self):
622
+ G = self.K3
623
+ assert G.adj[0] == {1: {}, 2: {}}
624
+ assert G[0] == {1: {}, 2: {}}
625
+ with pytest.raises(KeyError):
626
+ G.__getitem__("j")
627
+ with pytest.raises(TypeError):
628
+ G.__getitem__(["A"])
629
+
630
+ def test_add_node(self):
631
+ G = self.Graph()
632
+ G.add_node(0)
633
+ assert G.adj == {0: {}}
634
+ # test add attributes
635
+ G.add_node(1, c="red")
636
+ G.add_node(2, c="blue")
637
+ G.add_node(3, c="red")
638
+ assert G.nodes[1]["c"] == "red"
639
+ assert G.nodes[2]["c"] == "blue"
640
+ assert G.nodes[3]["c"] == "red"
641
+ # test updating attributes
642
+ G.add_node(1, c="blue")
643
+ G.add_node(2, c="red")
644
+ G.add_node(3, c="blue")
645
+ assert G.nodes[1]["c"] == "blue"
646
+ assert G.nodes[2]["c"] == "red"
647
+ assert G.nodes[3]["c"] == "blue"
648
+
649
+ def test_add_nodes_from(self):
650
+ G = self.Graph()
651
+ G.add_nodes_from([0, 1, 2])
652
+ assert G.adj == {0: {}, 1: {}, 2: {}}
653
+ # test add attributes
654
+ G.add_nodes_from([0, 1, 2], c="red")
655
+ assert G.nodes[0]["c"] == "red"
656
+ assert G.nodes[2]["c"] == "red"
657
+ # test that attribute dicts are not the same
658
+ assert G.nodes[0] is not G.nodes[1]
659
+ # test updating attributes
660
+ G.add_nodes_from([0, 1, 2], c="blue")
661
+ assert G.nodes[0]["c"] == "blue"
662
+ assert G.nodes[2]["c"] == "blue"
663
+ assert G.nodes[0] is not G.nodes[1]
664
+ # test tuple input
665
+ H = self.Graph()
666
+ H.add_nodes_from(G.nodes(data=True))
667
+ assert H.nodes[0]["c"] == "blue"
668
+ assert H.nodes[2]["c"] == "blue"
669
+ assert H.nodes[0] is not H.nodes[1]
670
+ # specific overrides general
671
+ H.add_nodes_from([0, (1, {"c": "green"}), (3, {"c": "cyan"})], c="red")
672
+ assert H.nodes[0]["c"] == "red"
673
+ assert H.nodes[1]["c"] == "green"
674
+ assert H.nodes[2]["c"] == "blue"
675
+ assert H.nodes[3]["c"] == "cyan"
676
+
677
+ def test_remove_node(self):
678
+ G = self.K3.copy()
679
+ G.remove_node(0)
680
+ assert G.adj == {1: {2: {}}, 2: {1: {}}}
681
+ with pytest.raises(nx.NetworkXError):
682
+ G.remove_node(-1)
683
+
684
+ # generator here to implement list,set,string...
685
+
686
+ def test_remove_nodes_from(self):
687
+ G = self.K3.copy()
688
+ G.remove_nodes_from([0, 1])
689
+ assert G.adj == {2: {}}
690
+ G.remove_nodes_from([-1]) # silent fail
691
+
692
+ def test_add_edge(self):
693
+ G = self.Graph()
694
+ G.add_edge(0, 1)
695
+ assert G.adj == {0: {1: {}}, 1: {0: {}}}
696
+ G = self.Graph()
697
+ G.add_edge(*(0, 1))
698
+ assert G.adj == {0: {1: {}}, 1: {0: {}}}
699
+ G = self.Graph()
700
+ with pytest.raises(ValueError):
701
+ G.add_edge(None, "anything")
702
+
703
+ def test_add_edges_from(self):
704
+ G = self.Graph()
705
+ G.add_edges_from([(0, 1), (0, 2, {"weight": 3})])
706
+ assert G.adj == {
707
+ 0: {1: {}, 2: {"weight": 3}},
708
+ 1: {0: {}},
709
+ 2: {0: {"weight": 3}},
710
+ }
711
+ G = self.Graph()
712
+ G.add_edges_from([(0, 1), (0, 2, {"weight": 3}), (1, 2, {"data": 4})], data=2)
713
+ assert G.adj == {
714
+ 0: {1: {"data": 2}, 2: {"weight": 3, "data": 2}},
715
+ 1: {0: {"data": 2}, 2: {"data": 4}},
716
+ 2: {0: {"weight": 3, "data": 2}, 1: {"data": 4}},
717
+ }
718
+
719
+ with pytest.raises(nx.NetworkXError):
720
+ G.add_edges_from([(0,)]) # too few in tuple
721
+ with pytest.raises(nx.NetworkXError):
722
+ G.add_edges_from([(0, 1, 2, 3)]) # too many in tuple
723
+ with pytest.raises(TypeError):
724
+ G.add_edges_from([0]) # not a tuple
725
+ with pytest.raises(ValueError):
726
+ G.add_edges_from([(None, 3), (3, 2)]) # None cannot be a node
727
+
728
+ def test_remove_edge(self):
729
+ G = self.K3.copy()
730
+ G.remove_edge(0, 1)
731
+ assert G.adj == {0: {2: {}}, 1: {2: {}}, 2: {0: {}, 1: {}}}
732
+ with pytest.raises(nx.NetworkXError):
733
+ G.remove_edge(-1, 0)
734
+
735
+ def test_remove_edges_from(self):
736
+ G = self.K3.copy()
737
+ G.remove_edges_from([(0, 1)])
738
+ assert G.adj == {0: {2: {}}, 1: {2: {}}, 2: {0: {}, 1: {}}}
739
+ G.remove_edges_from([(0, 0)]) # silent fail
740
+
741
+ def test_clear(self):
742
+ G = self.K3.copy()
743
+ G.graph["name"] = "K3"
744
+ G.clear()
745
+ assert list(G.nodes) == []
746
+ assert G.adj == {}
747
+ assert G.graph == {}
748
+
749
+ def test_clear_edges(self):
750
+ G = self.K3.copy()
751
+ G.graph["name"] = "K3"
752
+ nodes = list(G.nodes)
753
+ G.clear_edges()
754
+ assert list(G.nodes) == nodes
755
+ assert G.adj == {0: {}, 1: {}, 2: {}}
756
+ assert list(G.edges) == []
757
+ assert G.graph["name"] == "K3"
758
+
759
+ def test_edges_data(self):
760
+ G = self.K3
761
+ all_edges = [(0, 1, {}), (0, 2, {}), (1, 2, {})]
762
+ assert edges_equal(G.edges(data=True), all_edges)
763
+ assert edges_equal(G.edges(0, data=True), [(0, 1, {}), (0, 2, {})])
764
+ assert edges_equal(G.edges([0, 1], data=True), all_edges)
765
+ with pytest.raises(nx.NetworkXError):
766
+ G.edges(-1, True)
767
+
768
+ def test_get_edge_data(self):
769
+ G = self.K3.copy()
770
+ assert G.get_edge_data(0, 1) == {}
771
+ assert G[0][1] == {}
772
+ assert G.get_edge_data(10, 20) is None
773
+ assert G.get_edge_data(-1, 0) is None
774
+ assert G.get_edge_data(-1, 0, default=1) == 1
775
+
776
+ def test_update(self):
777
+ # specify both edges and nodes
778
+ G = self.K3.copy()
779
+ G.update(nodes=[3, (4, {"size": 2})], edges=[(4, 5), (6, 7, {"weight": 2})])
780
+ nlist = [
781
+ (0, {}),
782
+ (1, {}),
783
+ (2, {}),
784
+ (3, {}),
785
+ (4, {"size": 2}),
786
+ (5, {}),
787
+ (6, {}),
788
+ (7, {}),
789
+ ]
790
+ assert sorted(G.nodes.data()) == nlist
791
+ if G.is_directed():
792
+ elist = [
793
+ (0, 1, {}),
794
+ (0, 2, {}),
795
+ (1, 0, {}),
796
+ (1, 2, {}),
797
+ (2, 0, {}),
798
+ (2, 1, {}),
799
+ (4, 5, {}),
800
+ (6, 7, {"weight": 2}),
801
+ ]
802
+ else:
803
+ elist = [
804
+ (0, 1, {}),
805
+ (0, 2, {}),
806
+ (1, 2, {}),
807
+ (4, 5, {}),
808
+ (6, 7, {"weight": 2}),
809
+ ]
810
+ assert sorted(G.edges.data()) == elist
811
+ assert G.graph == {}
812
+
813
+ # no keywords -- order is edges, nodes
814
+ G = self.K3.copy()
815
+ G.update([(4, 5), (6, 7, {"weight": 2})], [3, (4, {"size": 2})])
816
+ assert sorted(G.nodes.data()) == nlist
817
+ assert sorted(G.edges.data()) == elist
818
+ assert G.graph == {}
819
+
820
+ # update using only a graph
821
+ G = self.Graph()
822
+ G.graph["foo"] = "bar"
823
+ G.add_node(2, data=4)
824
+ G.add_edge(0, 1, weight=0.5)
825
+ GG = G.copy()
826
+ H = self.Graph()
827
+ GG.update(H)
828
+ assert graphs_equal(G, GG)
829
+ H.update(G)
830
+ assert graphs_equal(H, G)
831
+
832
+ # update nodes only
833
+ H = self.Graph()
834
+ H.update(nodes=[3, 4])
835
+ assert H.nodes ^ {3, 4} == set()
836
+ assert H.size() == 0
837
+
838
+ # update edges only
839
+ H = self.Graph()
840
+ H.update(edges=[(3, 4)])
841
+ assert sorted(H.edges.data()) == [(3, 4, {})]
842
+ assert H.size() == 1
843
+
844
+ # No inputs -> exception
845
+ with pytest.raises(nx.NetworkXError):
846
+ nx.Graph().update()
847
+
848
+
849
+ class TestEdgeSubgraph:
850
+ """Unit tests for the :meth:`Graph.edge_subgraph` method."""
851
+
852
+ def setup_method(self):
853
+ # Create a path graph on five nodes.
854
+ G = nx.path_graph(5)
855
+ # Add some node, edge, and graph attributes.
856
+ for i in range(5):
857
+ G.nodes[i]["name"] = f"node{i}"
858
+ G.edges[0, 1]["name"] = "edge01"
859
+ G.edges[3, 4]["name"] = "edge34"
860
+ G.graph["name"] = "graph"
861
+ # Get the subgraph induced by the first and last edges.
862
+ self.G = G
863
+ self.H = G.edge_subgraph([(0, 1), (3, 4)])
864
+
865
+ def test_correct_nodes(self):
866
+ """Tests that the subgraph has the correct nodes."""
867
+ assert [0, 1, 3, 4] == sorted(self.H.nodes())
868
+
869
+ def test_correct_edges(self):
870
+ """Tests that the subgraph has the correct edges."""
871
+ assert [(0, 1, "edge01"), (3, 4, "edge34")] == sorted(self.H.edges(data="name"))
872
+
873
+ def test_add_node(self):
874
+ """Tests that adding a node to the original graph does not
875
+ affect the nodes of the subgraph.
876
+
877
+ """
878
+ self.G.add_node(5)
879
+ assert [0, 1, 3, 4] == sorted(self.H.nodes())
880
+
881
+ def test_remove_node(self):
882
+ """Tests that removing a node in the original graph does
883
+ affect the nodes of the subgraph.
884
+
885
+ """
886
+ self.G.remove_node(0)
887
+ assert [1, 3, 4] == sorted(self.H.nodes())
888
+
889
+ def test_node_attr_dict(self):
890
+ """Tests that the node attribute dictionary of the two graphs is
891
+ the same object.
892
+
893
+ """
894
+ for v in self.H:
895
+ assert self.G.nodes[v] == self.H.nodes[v]
896
+ # Making a change to G should make a change in H and vice versa.
897
+ self.G.nodes[0]["name"] = "foo"
898
+ assert self.G.nodes[0] == self.H.nodes[0]
899
+ self.H.nodes[1]["name"] = "bar"
900
+ assert self.G.nodes[1] == self.H.nodes[1]
901
+
902
+ def test_edge_attr_dict(self):
903
+ """Tests that the edge attribute dictionary of the two graphs is
904
+ the same object.
905
+
906
+ """
907
+ for u, v in self.H.edges():
908
+ assert self.G.edges[u, v] == self.H.edges[u, v]
909
+ # Making a change to G should make a change in H and vice versa.
910
+ self.G.edges[0, 1]["name"] = "foo"
911
+ assert self.G.edges[0, 1]["name"] == self.H.edges[0, 1]["name"]
912
+ self.H.edges[3, 4]["name"] = "bar"
913
+ assert self.G.edges[3, 4]["name"] == self.H.edges[3, 4]["name"]
914
+
915
+ def test_graph_attr_dict(self):
916
+ """Tests that the graph attribute dictionary of the two graphs
917
+ is the same object.
918
+
919
+ """
920
+ assert self.G.graph is self.H.graph
minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_graphviews.py ADDED
@@ -0,0 +1,350 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+ from networkx.utils import edges_equal, nodes_equal
5
+
6
+ # Note: SubGraph views are not tested here. They have their own testing file
7
+
8
+
9
+ class TestReverseView:
10
+ def setup_method(self):
11
+ self.G = nx.path_graph(9, create_using=nx.DiGraph())
12
+ self.rv = nx.reverse_view(self.G)
13
+
14
+ def test_pickle(self):
15
+ import pickle
16
+
17
+ rv = self.rv
18
+ prv = pickle.loads(pickle.dumps(rv, -1))
19
+ assert rv._node == prv._node
20
+ assert rv._adj == prv._adj
21
+ assert rv.graph == prv.graph
22
+
23
+ def test_contains(self):
24
+ assert (2, 3) in self.G.edges
25
+ assert (3, 2) not in self.G.edges
26
+ assert (2, 3) not in self.rv.edges
27
+ assert (3, 2) in self.rv.edges
28
+
29
+ def test_iter(self):
30
+ expected = sorted(tuple(reversed(e)) for e in self.G.edges)
31
+ assert sorted(self.rv.edges) == expected
32
+
33
+ def test_exceptions(self):
34
+ G = nx.Graph()
35
+ pytest.raises(nx.NetworkXNotImplemented, nx.reverse_view, G)
36
+
37
+ def test_subclass(self):
38
+ class MyGraph(nx.DiGraph):
39
+ def my_method(self):
40
+ return "me"
41
+
42
+ def to_directed_class(self):
43
+ return MyGraph()
44
+
45
+ M = MyGraph()
46
+ M.add_edge(1, 2)
47
+ RM = nx.reverse_view(M)
48
+ print("RM class", RM.__class__)
49
+ RMC = RM.copy()
50
+ print("RMC class", RMC.__class__)
51
+ print(RMC.edges)
52
+ assert RMC.has_edge(2, 1)
53
+ assert RMC.my_method() == "me"
54
+
55
+
56
+ class TestMultiReverseView:
57
+ def setup_method(self):
58
+ self.G = nx.path_graph(9, create_using=nx.MultiDiGraph())
59
+ self.G.add_edge(4, 5)
60
+ self.rv = nx.reverse_view(self.G)
61
+
62
+ def test_pickle(self):
63
+ import pickle
64
+
65
+ rv = self.rv
66
+ prv = pickle.loads(pickle.dumps(rv, -1))
67
+ assert rv._node == prv._node
68
+ assert rv._adj == prv._adj
69
+ assert rv.graph == prv.graph
70
+
71
+ def test_contains(self):
72
+ assert (2, 3, 0) in self.G.edges
73
+ assert (3, 2, 0) not in self.G.edges
74
+ assert (2, 3, 0) not in self.rv.edges
75
+ assert (3, 2, 0) in self.rv.edges
76
+ assert (5, 4, 1) in self.rv.edges
77
+ assert (4, 5, 1) not in self.rv.edges
78
+
79
+ def test_iter(self):
80
+ expected = sorted((v, u, k) for u, v, k in self.G.edges)
81
+ assert sorted(self.rv.edges) == expected
82
+
83
+ def test_exceptions(self):
84
+ MG = nx.MultiGraph(self.G)
85
+ pytest.raises(nx.NetworkXNotImplemented, nx.reverse_view, MG)
86
+
87
+
88
+ def test_generic_multitype():
89
+ nxg = nx.graphviews
90
+ G = nx.DiGraph([(1, 2)])
91
+ with pytest.raises(nx.NetworkXError):
92
+ nxg.generic_graph_view(G, create_using=nx.MultiGraph)
93
+ G = nx.MultiDiGraph([(1, 2)])
94
+ with pytest.raises(nx.NetworkXError):
95
+ nxg.generic_graph_view(G, create_using=nx.DiGraph)
96
+
97
+
98
+ class TestToDirected:
99
+ def setup_method(self):
100
+ self.G = nx.path_graph(9)
101
+ self.dv = nx.to_directed(self.G)
102
+ self.MG = nx.path_graph(9, create_using=nx.MultiGraph())
103
+ self.Mdv = nx.to_directed(self.MG)
104
+
105
+ def test_directed(self):
106
+ assert not self.G.is_directed()
107
+ assert self.dv.is_directed()
108
+
109
+ def test_already_directed(self):
110
+ dd = nx.to_directed(self.dv)
111
+ Mdd = nx.to_directed(self.Mdv)
112
+ assert edges_equal(dd.edges, self.dv.edges)
113
+ assert edges_equal(Mdd.edges, self.Mdv.edges)
114
+
115
+ def test_pickle(self):
116
+ import pickle
117
+
118
+ dv = self.dv
119
+ pdv = pickle.loads(pickle.dumps(dv, -1))
120
+ assert dv._node == pdv._node
121
+ assert dv._succ == pdv._succ
122
+ assert dv._pred == pdv._pred
123
+ assert dv.graph == pdv.graph
124
+
125
+ def test_contains(self):
126
+ assert (2, 3) in self.G.edges
127
+ assert (3, 2) in self.G.edges
128
+ assert (2, 3) in self.dv.edges
129
+ assert (3, 2) in self.dv.edges
130
+
131
+ def test_iter(self):
132
+ revd = [tuple(reversed(e)) for e in self.G.edges]
133
+ expected = sorted(list(self.G.edges) + revd)
134
+ assert sorted(self.dv.edges) == expected
135
+
136
+
137
+ class TestToUndirected:
138
+ def setup_method(self):
139
+ self.DG = nx.path_graph(9, create_using=nx.DiGraph())
140
+ self.uv = nx.to_undirected(self.DG)
141
+ self.MDG = nx.path_graph(9, create_using=nx.MultiDiGraph())
142
+ self.Muv = nx.to_undirected(self.MDG)
143
+
144
+ def test_directed(self):
145
+ assert self.DG.is_directed()
146
+ assert not self.uv.is_directed()
147
+
148
+ def test_already_directed(self):
149
+ uu = nx.to_undirected(self.uv)
150
+ Muu = nx.to_undirected(self.Muv)
151
+ assert edges_equal(uu.edges, self.uv.edges)
152
+ assert edges_equal(Muu.edges, self.Muv.edges)
153
+
154
+ def test_pickle(self):
155
+ import pickle
156
+
157
+ uv = self.uv
158
+ puv = pickle.loads(pickle.dumps(uv, -1))
159
+ assert uv._node == puv._node
160
+ assert uv._adj == puv._adj
161
+ assert uv.graph == puv.graph
162
+ assert hasattr(uv, "_graph")
163
+
164
+ def test_contains(self):
165
+ assert (2, 3) in self.DG.edges
166
+ assert (3, 2) not in self.DG.edges
167
+ assert (2, 3) in self.uv.edges
168
+ assert (3, 2) in self.uv.edges
169
+
170
+ def test_iter(self):
171
+ expected = sorted(self.DG.edges)
172
+ assert sorted(self.uv.edges) == expected
173
+
174
+
175
+ class TestChainsOfViews:
176
+ @classmethod
177
+ def setup_class(cls):
178
+ cls.G = nx.path_graph(9)
179
+ cls.DG = nx.path_graph(9, create_using=nx.DiGraph())
180
+ cls.MG = nx.path_graph(9, create_using=nx.MultiGraph())
181
+ cls.MDG = nx.path_graph(9, create_using=nx.MultiDiGraph())
182
+ cls.Gv = nx.to_undirected(cls.DG)
183
+ cls.DGv = nx.to_directed(cls.G)
184
+ cls.MGv = nx.to_undirected(cls.MDG)
185
+ cls.MDGv = nx.to_directed(cls.MG)
186
+ cls.Rv = cls.DG.reverse()
187
+ cls.MRv = cls.MDG.reverse()
188
+ cls.graphs = [
189
+ cls.G,
190
+ cls.DG,
191
+ cls.MG,
192
+ cls.MDG,
193
+ cls.Gv,
194
+ cls.DGv,
195
+ cls.MGv,
196
+ cls.MDGv,
197
+ cls.Rv,
198
+ cls.MRv,
199
+ ]
200
+ for G in cls.graphs:
201
+ G.edges, G.nodes, G.degree
202
+
203
+ def test_pickle(self):
204
+ import pickle
205
+
206
+ for G in self.graphs:
207
+ H = pickle.loads(pickle.dumps(G, -1))
208
+ assert edges_equal(H.edges, G.edges)
209
+ assert nodes_equal(H.nodes, G.nodes)
210
+
211
+ def test_subgraph_of_subgraph(self):
212
+ SGv = nx.subgraph(self.G, range(3, 7))
213
+ SDGv = nx.subgraph(self.DG, range(3, 7))
214
+ SMGv = nx.subgraph(self.MG, range(3, 7))
215
+ SMDGv = nx.subgraph(self.MDG, range(3, 7))
216
+ for G in self.graphs + [SGv, SDGv, SMGv, SMDGv]:
217
+ SG = nx.induced_subgraph(G, [4, 5, 6])
218
+ assert list(SG) == [4, 5, 6]
219
+ SSG = SG.subgraph([6, 7])
220
+ assert list(SSG) == [6]
221
+ # subgraph-subgraph chain is short-cut in base class method
222
+ assert SSG._graph is G
223
+
224
+ def test_restricted_induced_subgraph_chains(self):
225
+ """Test subgraph chains that both restrict and show nodes/edges.
226
+
227
+ A restricted_view subgraph should allow induced subgraphs using
228
+ G.subgraph that automagically without a chain (meaning the result
229
+ is a subgraph view of the original graph not a subgraph-of-subgraph.
230
+ """
231
+ hide_nodes = [3, 4, 5]
232
+ hide_edges = [(6, 7)]
233
+ RG = nx.restricted_view(self.G, hide_nodes, hide_edges)
234
+ nodes = [4, 5, 6, 7, 8]
235
+ SG = nx.induced_subgraph(RG, nodes)
236
+ SSG = RG.subgraph(nodes)
237
+ assert RG._graph is self.G
238
+ assert SSG._graph is self.G
239
+ assert SG._graph is RG
240
+ assert edges_equal(SG.edges, SSG.edges)
241
+ # should be same as morphing the graph
242
+ CG = self.G.copy()
243
+ CG.remove_nodes_from(hide_nodes)
244
+ CG.remove_edges_from(hide_edges)
245
+ assert edges_equal(CG.edges(nodes), SSG.edges)
246
+ CG.remove_nodes_from([0, 1, 2, 3])
247
+ assert edges_equal(CG.edges, SSG.edges)
248
+ # switch order: subgraph first, then restricted view
249
+ SSSG = self.G.subgraph(nodes)
250
+ RSG = nx.restricted_view(SSSG, hide_nodes, hide_edges)
251
+ assert RSG._graph is not self.G
252
+ assert edges_equal(RSG.edges, CG.edges)
253
+
254
+ def test_subgraph_copy(self):
255
+ for origG in self.graphs:
256
+ G = nx.Graph(origG)
257
+ SG = G.subgraph([4, 5, 6])
258
+ H = SG.copy()
259
+ assert type(G) == type(H)
260
+
261
+ def test_subgraph_todirected(self):
262
+ SG = nx.induced_subgraph(self.G, [4, 5, 6])
263
+ SSG = SG.to_directed()
264
+ assert sorted(SSG) == [4, 5, 6]
265
+ assert sorted(SSG.edges) == [(4, 5), (5, 4), (5, 6), (6, 5)]
266
+
267
+ def test_subgraph_toundirected(self):
268
+ SG = nx.induced_subgraph(self.G, [4, 5, 6])
269
+ SSG = SG.to_undirected()
270
+ assert list(SSG) == [4, 5, 6]
271
+ assert sorted(SSG.edges) == [(4, 5), (5, 6)]
272
+
273
+ def test_reverse_subgraph_toundirected(self):
274
+ G = self.DG.reverse(copy=False)
275
+ SG = G.subgraph([4, 5, 6])
276
+ SSG = SG.to_undirected()
277
+ assert list(SSG) == [4, 5, 6]
278
+ assert sorted(SSG.edges) == [(4, 5), (5, 6)]
279
+
280
+ def test_reverse_reverse_copy(self):
281
+ G = self.DG.reverse(copy=False)
282
+ H = G.reverse(copy=True)
283
+ assert H.nodes == self.DG.nodes
284
+ assert H.edges == self.DG.edges
285
+ G = self.MDG.reverse(copy=False)
286
+ H = G.reverse(copy=True)
287
+ assert H.nodes == self.MDG.nodes
288
+ assert H.edges == self.MDG.edges
289
+
290
+ def test_subgraph_edgesubgraph_toundirected(self):
291
+ G = self.G.copy()
292
+ SG = G.subgraph([4, 5, 6])
293
+ SSG = SG.edge_subgraph([(4, 5), (5, 4)])
294
+ USSG = SSG.to_undirected()
295
+ assert list(USSG) == [4, 5]
296
+ assert sorted(USSG.edges) == [(4, 5)]
297
+
298
+ def test_copy_subgraph(self):
299
+ G = self.G.copy()
300
+ SG = G.subgraph([4, 5, 6])
301
+ CSG = SG.copy(as_view=True)
302
+ DCSG = SG.copy(as_view=False)
303
+ assert hasattr(CSG, "_graph") # is a view
304
+ assert not hasattr(DCSG, "_graph") # not a view
305
+
306
+ def test_copy_disubgraph(self):
307
+ G = self.DG.copy()
308
+ SG = G.subgraph([4, 5, 6])
309
+ CSG = SG.copy(as_view=True)
310
+ DCSG = SG.copy(as_view=False)
311
+ assert hasattr(CSG, "_graph") # is a view
312
+ assert not hasattr(DCSG, "_graph") # not a view
313
+
314
+ def test_copy_multidisubgraph(self):
315
+ G = self.MDG.copy()
316
+ SG = G.subgraph([4, 5, 6])
317
+ CSG = SG.copy(as_view=True)
318
+ DCSG = SG.copy(as_view=False)
319
+ assert hasattr(CSG, "_graph") # is a view
320
+ assert not hasattr(DCSG, "_graph") # not a view
321
+
322
+ def test_copy_multisubgraph(self):
323
+ G = self.MG.copy()
324
+ SG = G.subgraph([4, 5, 6])
325
+ CSG = SG.copy(as_view=True)
326
+ DCSG = SG.copy(as_view=False)
327
+ assert hasattr(CSG, "_graph") # is a view
328
+ assert not hasattr(DCSG, "_graph") # not a view
329
+
330
+ def test_copy_of_view(self):
331
+ G = nx.MultiGraph(self.MGv)
332
+ assert G.__class__.__name__ == "MultiGraph"
333
+ G = G.copy(as_view=True)
334
+ assert G.__class__.__name__ == "MultiGraph"
335
+
336
+ def test_subclass(self):
337
+ class MyGraph(nx.DiGraph):
338
+ def my_method(self):
339
+ return "me"
340
+
341
+ def to_directed_class(self):
342
+ return MyGraph()
343
+
344
+ for origG in self.graphs:
345
+ G = MyGraph(origG)
346
+ SG = G.subgraph([4, 5, 6])
347
+ H = SG.copy()
348
+ assert SG.my_method() == "me"
349
+ assert H.my_method() == "me"
350
+ assert 3 not in H or 3 in SG
minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_multigraph.py ADDED
@@ -0,0 +1,528 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from collections import UserDict
2
+
3
+ import pytest
4
+
5
+ import networkx as nx
6
+ from networkx.utils import edges_equal
7
+
8
+ from .test_graph import BaseAttrGraphTester
9
+ from .test_graph import TestGraph as _TestGraph
10
+
11
+
12
+ class BaseMultiGraphTester(BaseAttrGraphTester):
13
+ def test_has_edge(self):
14
+ G = self.K3
15
+ assert G.has_edge(0, 1)
16
+ assert not G.has_edge(0, -1)
17
+ assert G.has_edge(0, 1, 0)
18
+ assert not G.has_edge(0, 1, 1)
19
+
20
+ def test_get_edge_data(self):
21
+ G = self.K3
22
+ assert G.get_edge_data(0, 1) == {0: {}}
23
+ assert G[0][1] == {0: {}}
24
+ assert G[0][1][0] == {}
25
+ assert G.get_edge_data(10, 20) is None
26
+ assert G.get_edge_data(0, 1, 0) == {}
27
+
28
+ def test_adjacency(self):
29
+ G = self.K3
30
+ assert dict(G.adjacency()) == {
31
+ 0: {1: {0: {}}, 2: {0: {}}},
32
+ 1: {0: {0: {}}, 2: {0: {}}},
33
+ 2: {0: {0: {}}, 1: {0: {}}},
34
+ }
35
+
36
+ def deepcopy_edge_attr(self, H, G):
37
+ assert G[1][2][0]["foo"] == H[1][2][0]["foo"]
38
+ G[1][2][0]["foo"].append(1)
39
+ assert G[1][2][0]["foo"] != H[1][2][0]["foo"]
40
+
41
+ def shallow_copy_edge_attr(self, H, G):
42
+ assert G[1][2][0]["foo"] == H[1][2][0]["foo"]
43
+ G[1][2][0]["foo"].append(1)
44
+ assert G[1][2][0]["foo"] == H[1][2][0]["foo"]
45
+
46
+ def graphs_equal(self, H, G):
47
+ assert G._adj == H._adj
48
+ assert G._node == H._node
49
+ assert G.graph == H.graph
50
+ assert G.name == H.name
51
+ if not G.is_directed() and not H.is_directed():
52
+ assert H._adj[1][2][0] is H._adj[2][1][0]
53
+ assert G._adj[1][2][0] is G._adj[2][1][0]
54
+ else: # at least one is directed
55
+ if not G.is_directed():
56
+ G._pred = G._adj
57
+ G._succ = G._adj
58
+ if not H.is_directed():
59
+ H._pred = H._adj
60
+ H._succ = H._adj
61
+ assert G._pred == H._pred
62
+ assert G._succ == H._succ
63
+ assert H._succ[1][2][0] is H._pred[2][1][0]
64
+ assert G._succ[1][2][0] is G._pred[2][1][0]
65
+
66
+ def same_attrdict(self, H, G):
67
+ # same attrdict in the edgedata
68
+ old_foo = H[1][2][0]["foo"]
69
+ H.adj[1][2][0]["foo"] = "baz"
70
+ assert G._adj == H._adj
71
+ H.adj[1][2][0]["foo"] = old_foo
72
+ assert G._adj == H._adj
73
+
74
+ old_foo = H.nodes[0]["foo"]
75
+ H.nodes[0]["foo"] = "baz"
76
+ assert G._node == H._node
77
+ H.nodes[0]["foo"] = old_foo
78
+ assert G._node == H._node
79
+
80
+ def different_attrdict(self, H, G):
81
+ # used by graph_equal_but_different
82
+ old_foo = H[1][2][0]["foo"]
83
+ H.adj[1][2][0]["foo"] = "baz"
84
+ assert G._adj != H._adj
85
+ H.adj[1][2][0]["foo"] = old_foo
86
+ assert G._adj == H._adj
87
+
88
+ old_foo = H.nodes[0]["foo"]
89
+ H.nodes[0]["foo"] = "baz"
90
+ assert G._node != H._node
91
+ H.nodes[0]["foo"] = old_foo
92
+ assert G._node == H._node
93
+
94
+ def test_to_undirected(self):
95
+ G = self.K3
96
+ self.add_attributes(G)
97
+ H = nx.MultiGraph(G)
98
+ self.is_shallow_copy(H, G)
99
+ H = G.to_undirected()
100
+ self.is_deepcopy(H, G)
101
+
102
+ def test_to_directed(self):
103
+ G = self.K3
104
+ self.add_attributes(G)
105
+ H = nx.MultiDiGraph(G)
106
+ self.is_shallow_copy(H, G)
107
+ H = G.to_directed()
108
+ self.is_deepcopy(H, G)
109
+
110
+ def test_number_of_edges_selfloops(self):
111
+ G = self.K3
112
+ G.add_edge(0, 0)
113
+ G.add_edge(0, 0)
114
+ G.add_edge(0, 0, key="parallel edge")
115
+ G.remove_edge(0, 0, key="parallel edge")
116
+ assert G.number_of_edges(0, 0) == 2
117
+ G.remove_edge(0, 0)
118
+ assert G.number_of_edges(0, 0) == 1
119
+
120
+ def test_edge_lookup(self):
121
+ G = self.Graph()
122
+ G.add_edge(1, 2, foo="bar")
123
+ G.add_edge(1, 2, "key", foo="biz")
124
+ assert edges_equal(G.edges[1, 2, 0], {"foo": "bar"})
125
+ assert edges_equal(G.edges[1, 2, "key"], {"foo": "biz"})
126
+
127
+ def test_edge_attr(self):
128
+ G = self.Graph()
129
+ G.add_edge(1, 2, key="k1", foo="bar")
130
+ G.add_edge(1, 2, key="k2", foo="baz")
131
+ assert isinstance(G.get_edge_data(1, 2), G.edge_key_dict_factory)
132
+ assert all(
133
+ isinstance(d, G.edge_attr_dict_factory) for u, v, d in G.edges(data=True)
134
+ )
135
+ assert edges_equal(
136
+ G.edges(keys=True, data=True),
137
+ [(1, 2, "k1", {"foo": "bar"}), (1, 2, "k2", {"foo": "baz"})],
138
+ )
139
+ assert edges_equal(
140
+ G.edges(keys=True, data="foo"), [(1, 2, "k1", "bar"), (1, 2, "k2", "baz")]
141
+ )
142
+
143
+ def test_edge_attr4(self):
144
+ G = self.Graph()
145
+ G.add_edge(1, 2, key=0, data=7, spam="bar", bar="foo")
146
+ assert edges_equal(
147
+ G.edges(data=True), [(1, 2, {"data": 7, "spam": "bar", "bar": "foo"})]
148
+ )
149
+ G[1][2][0]["data"] = 10 # OK to set data like this
150
+ assert edges_equal(
151
+ G.edges(data=True), [(1, 2, {"data": 10, "spam": "bar", "bar": "foo"})]
152
+ )
153
+
154
+ G.adj[1][2][0]["data"] = 20
155
+ assert edges_equal(
156
+ G.edges(data=True), [(1, 2, {"data": 20, "spam": "bar", "bar": "foo"})]
157
+ )
158
+ G.edges[1, 2, 0]["data"] = 21 # another spelling, "edge"
159
+ assert edges_equal(
160
+ G.edges(data=True), [(1, 2, {"data": 21, "spam": "bar", "bar": "foo"})]
161
+ )
162
+ G.adj[1][2][0]["listdata"] = [20, 200]
163
+ G.adj[1][2][0]["weight"] = 20
164
+ assert edges_equal(
165
+ G.edges(data=True),
166
+ [
167
+ (
168
+ 1,
169
+ 2,
170
+ {
171
+ "data": 21,
172
+ "spam": "bar",
173
+ "bar": "foo",
174
+ "listdata": [20, 200],
175
+ "weight": 20,
176
+ },
177
+ )
178
+ ],
179
+ )
180
+
181
+
182
+ class TestMultiGraph(BaseMultiGraphTester, _TestGraph):
183
+ def setup_method(self):
184
+ self.Graph = nx.MultiGraph
185
+ # build K3
186
+ ed1, ed2, ed3 = ({0: {}}, {0: {}}, {0: {}})
187
+ self.k3adj = {0: {1: ed1, 2: ed2}, 1: {0: ed1, 2: ed3}, 2: {0: ed2, 1: ed3}}
188
+ self.k3edges = [(0, 1), (0, 2), (1, 2)]
189
+ self.k3nodes = [0, 1, 2]
190
+ self.K3 = self.Graph()
191
+ self.K3._adj = self.k3adj
192
+ self.K3._node = {}
193
+ self.K3._node[0] = {}
194
+ self.K3._node[1] = {}
195
+ self.K3._node[2] = {}
196
+
197
+ def test_data_input(self):
198
+ G = self.Graph({1: [2], 2: [1]}, name="test")
199
+ assert G.name == "test"
200
+ expected = [(1, {2: {0: {}}}), (2, {1: {0: {}}})]
201
+ assert sorted(G.adj.items()) == expected
202
+
203
+ def test_data_multigraph_input(self):
204
+ # standard case with edge keys and edge data
205
+ edata0 = {"w": 200, "s": "foo"}
206
+ edata1 = {"w": 201, "s": "bar"}
207
+ keydict = {0: edata0, 1: edata1}
208
+ dododod = {"a": {"b": keydict}}
209
+
210
+ multiple_edge = [("a", "b", 0, edata0), ("a", "b", 1, edata1)]
211
+ single_edge = [("a", "b", 0, keydict)]
212
+
213
+ G = self.Graph(dododod, multigraph_input=True)
214
+ assert list(G.edges(keys=True, data=True)) == multiple_edge
215
+ G = self.Graph(dododod, multigraph_input=None)
216
+ assert list(G.edges(keys=True, data=True)) == multiple_edge
217
+ G = self.Graph(dododod, multigraph_input=False)
218
+ assert list(G.edges(keys=True, data=True)) == single_edge
219
+
220
+ # test round-trip to_dict_of_dict and MultiGraph constructor
221
+ G = self.Graph(dododod, multigraph_input=True)
222
+ H = self.Graph(nx.to_dict_of_dicts(G))
223
+ assert nx.is_isomorphic(G, H) is True # test that default is True
224
+ for mgi in [True, False]:
225
+ H = self.Graph(nx.to_dict_of_dicts(G), multigraph_input=mgi)
226
+ assert nx.is_isomorphic(G, H) == mgi
227
+
228
+ # Set up cases for when incoming_graph_data is not multigraph_input
229
+ etraits = {"w": 200, "s": "foo"}
230
+ egraphics = {"color": "blue", "shape": "box"}
231
+ edata = {"traits": etraits, "graphics": egraphics}
232
+ dodod1 = {"a": {"b": edata}}
233
+ dodod2 = {"a": {"b": etraits}}
234
+ dodod3 = {"a": {"b": {"traits": etraits, "s": "foo"}}}
235
+ dol = {"a": ["b"]}
236
+
237
+ multiple_edge = [("a", "b", "traits", etraits), ("a", "b", "graphics", egraphics)]
238
+ single_edge = [("a", "b", 0, {})] # type: ignore[var-annotated]
239
+ single_edge1 = [("a", "b", 0, edata)]
240
+ single_edge2 = [("a", "b", 0, etraits)]
241
+ single_edge3 = [("a", "b", 0, {"traits": etraits, "s": "foo"})]
242
+
243
+ cases = [ # (dod, mgi, edges)
244
+ (dodod1, True, multiple_edge),
245
+ (dodod1, False, single_edge1),
246
+ (dodod2, False, single_edge2),
247
+ (dodod3, False, single_edge3),
248
+ (dol, False, single_edge),
249
+ ]
250
+
251
+ @pytest.mark.parametrize("dod, mgi, edges", cases)
252
+ def test_non_multigraph_input(self, dod, mgi, edges):
253
+ G = self.Graph(dod, multigraph_input=mgi)
254
+ assert list(G.edges(keys=True, data=True)) == edges
255
+ G = nx.to_networkx_graph(dod, create_using=self.Graph, multigraph_input=mgi)
256
+ assert list(G.edges(keys=True, data=True)) == edges
257
+
258
+ mgi_none_cases = [
259
+ (dodod1, multiple_edge),
260
+ (dodod2, single_edge2),
261
+ (dodod3, single_edge3),
262
+ ]
263
+
264
+ @pytest.mark.parametrize("dod, edges", mgi_none_cases)
265
+ def test_non_multigraph_input_mgi_none(self, dod, edges):
266
+ # test constructor without to_networkx_graph for mgi=None
267
+ G = self.Graph(dod)
268
+ assert list(G.edges(keys=True, data=True)) == edges
269
+
270
+ raise_cases = [dodod2, dodod3, dol]
271
+
272
+ @pytest.mark.parametrize("dod", raise_cases)
273
+ def test_non_multigraph_input_raise(self, dod):
274
+ # cases where NetworkXError is raised
275
+ pytest.raises(nx.NetworkXError, self.Graph, dod, multigraph_input=True)
276
+ pytest.raises(
277
+ nx.NetworkXError,
278
+ nx.to_networkx_graph,
279
+ dod,
280
+ create_using=self.Graph,
281
+ multigraph_input=True,
282
+ )
283
+
284
+ def test_getitem(self):
285
+ G = self.K3
286
+ assert G[0] == {1: {0: {}}, 2: {0: {}}}
287
+ with pytest.raises(KeyError):
288
+ G.__getitem__("j")
289
+ with pytest.raises(TypeError):
290
+ G.__getitem__(["A"])
291
+
292
+ def test_remove_node(self):
293
+ G = self.K3
294
+ G.remove_node(0)
295
+ assert G.adj == {1: {2: {0: {}}}, 2: {1: {0: {}}}}
296
+ with pytest.raises(nx.NetworkXError):
297
+ G.remove_node(-1)
298
+
299
+ def test_add_edge(self):
300
+ G = self.Graph()
301
+ G.add_edge(0, 1)
302
+ assert G.adj == {0: {1: {0: {}}}, 1: {0: {0: {}}}}
303
+ G = self.Graph()
304
+ G.add_edge(*(0, 1))
305
+ assert G.adj == {0: {1: {0: {}}}, 1: {0: {0: {}}}}
306
+ G = self.Graph()
307
+ with pytest.raises(ValueError):
308
+ G.add_edge(None, "anything")
309
+
310
+ def test_add_edge_conflicting_key(self):
311
+ G = self.Graph()
312
+ G.add_edge(0, 1, key=1)
313
+ G.add_edge(0, 1)
314
+ assert G.number_of_edges() == 2
315
+ G = self.Graph()
316
+ G.add_edges_from([(0, 1, 1, {})])
317
+ G.add_edges_from([(0, 1)])
318
+ assert G.number_of_edges() == 2
319
+
320
+ def test_add_edges_from(self):
321
+ G = self.Graph()
322
+ G.add_edges_from([(0, 1), (0, 1, {"weight": 3})])
323
+ assert G.adj == {
324
+ 0: {1: {0: {}, 1: {"weight": 3}}},
325
+ 1: {0: {0: {}, 1: {"weight": 3}}},
326
+ }
327
+ G.add_edges_from([(0, 1), (0, 1, {"weight": 3})], weight=2)
328
+ assert G.adj == {
329
+ 0: {1: {0: {}, 1: {"weight": 3}, 2: {"weight": 2}, 3: {"weight": 3}}},
330
+ 1: {0: {0: {}, 1: {"weight": 3}, 2: {"weight": 2}, 3: {"weight": 3}}},
331
+ }
332
+ G = self.Graph()
333
+ edges = [
334
+ (0, 1, {"weight": 3}),
335
+ (0, 1, (("weight", 2),)),
336
+ (0, 1, 5),
337
+ (0, 1, "s"),
338
+ ]
339
+ G.add_edges_from(edges)
340
+ keydict = {0: {"weight": 3}, 1: {"weight": 2}, 5: {}, "s": {}}
341
+ assert G._adj == {0: {1: keydict}, 1: {0: keydict}}
342
+
343
+ # too few in tuple
344
+ with pytest.raises(nx.NetworkXError):
345
+ G.add_edges_from([(0,)])
346
+ # too many in tuple
347
+ with pytest.raises(nx.NetworkXError):
348
+ G.add_edges_from([(0, 1, 2, 3, 4)])
349
+ # not a tuple
350
+ with pytest.raises(TypeError):
351
+ G.add_edges_from([0])
352
+
353
+ def test_multigraph_add_edges_from_four_tuple_misordered(self):
354
+ """add_edges_from expects 4-tuples of the format (u, v, key, data_dict).
355
+
356
+ Ensure 4-tuples of form (u, v, data_dict, key) raise exception.
357
+ """
358
+ G = nx.MultiGraph()
359
+ with pytest.raises(TypeError):
360
+ # key/data values flipped in 4-tuple
361
+ G.add_edges_from([(0, 1, {"color": "red"}, 0)])
362
+
363
+ def test_remove_edge(self):
364
+ G = self.K3
365
+ G.remove_edge(0, 1)
366
+ assert G.adj == {0: {2: {0: {}}}, 1: {2: {0: {}}}, 2: {0: {0: {}}, 1: {0: {}}}}
367
+
368
+ with pytest.raises(nx.NetworkXError):
369
+ G.remove_edge(-1, 0)
370
+ with pytest.raises(nx.NetworkXError):
371
+ G.remove_edge(0, 2, key=1)
372
+
373
+ def test_remove_edges_from(self):
374
+ G = self.K3.copy()
375
+ G.remove_edges_from([(0, 1)])
376
+ kd = {0: {}}
377
+ assert G.adj == {0: {2: kd}, 1: {2: kd}, 2: {0: kd, 1: kd}}
378
+ G.remove_edges_from([(0, 0)]) # silent fail
379
+ self.K3.add_edge(0, 1)
380
+ G = self.K3.copy()
381
+ G.remove_edges_from(list(G.edges(data=True, keys=True)))
382
+ assert G.adj == {0: {}, 1: {}, 2: {}}
383
+ G = self.K3.copy()
384
+ G.remove_edges_from(list(G.edges(data=False, keys=True)))
385
+ assert G.adj == {0: {}, 1: {}, 2: {}}
386
+ G = self.K3.copy()
387
+ G.remove_edges_from(list(G.edges(data=False, keys=False)))
388
+ assert G.adj == {0: {}, 1: {}, 2: {}}
389
+ G = self.K3.copy()
390
+ G.remove_edges_from([(0, 1, 0), (0, 2, 0, {}), (1, 2)])
391
+ assert G.adj == {0: {1: {1: {}}}, 1: {0: {1: {}}}, 2: {}}
392
+
393
+ def test_remove_multiedge(self):
394
+ G = self.K3
395
+ G.add_edge(0, 1, key="parallel edge")
396
+ G.remove_edge(0, 1, key="parallel edge")
397
+ assert G.adj == {
398
+ 0: {1: {0: {}}, 2: {0: {}}},
399
+ 1: {0: {0: {}}, 2: {0: {}}},
400
+ 2: {0: {0: {}}, 1: {0: {}}},
401
+ }
402
+ G.remove_edge(0, 1)
403
+ kd = {0: {}}
404
+ assert G.adj == {0: {2: kd}, 1: {2: kd}, 2: {0: kd, 1: kd}}
405
+ with pytest.raises(nx.NetworkXError):
406
+ G.remove_edge(-1, 0)
407
+
408
+
409
+ class TestEdgeSubgraph:
410
+ """Unit tests for the :meth:`MultiGraph.edge_subgraph` method."""
411
+
412
+ def setup_method(self):
413
+ # Create a doubly-linked path graph on five nodes.
414
+ G = nx.MultiGraph()
415
+ nx.add_path(G, range(5))
416
+ nx.add_path(G, range(5))
417
+ # Add some node, edge, and graph attributes.
418
+ for i in range(5):
419
+ G.nodes[i]["name"] = f"node{i}"
420
+ G.adj[0][1][0]["name"] = "edge010"
421
+ G.adj[0][1][1]["name"] = "edge011"
422
+ G.adj[3][4][0]["name"] = "edge340"
423
+ G.adj[3][4][1]["name"] = "edge341"
424
+ G.graph["name"] = "graph"
425
+ # Get the subgraph induced by one of the first edges and one of
426
+ # the last edges.
427
+ self.G = G
428
+ self.H = G.edge_subgraph([(0, 1, 0), (3, 4, 1)])
429
+
430
+ def test_correct_nodes(self):
431
+ """Tests that the subgraph has the correct nodes."""
432
+ assert [0, 1, 3, 4] == sorted(self.H.nodes())
433
+
434
+ def test_correct_edges(self):
435
+ """Tests that the subgraph has the correct edges."""
436
+ assert [(0, 1, 0, "edge010"), (3, 4, 1, "edge341")] == sorted(
437
+ self.H.edges(keys=True, data="name")
438
+ )
439
+
440
+ def test_add_node(self):
441
+ """Tests that adding a node to the original graph does not
442
+ affect the nodes of the subgraph.
443
+
444
+ """
445
+ self.G.add_node(5)
446
+ assert [0, 1, 3, 4] == sorted(self.H.nodes())
447
+
448
+ def test_remove_node(self):
449
+ """Tests that removing a node in the original graph does
450
+ affect the nodes of the subgraph.
451
+
452
+ """
453
+ self.G.remove_node(0)
454
+ assert [1, 3, 4] == sorted(self.H.nodes())
455
+
456
+ def test_node_attr_dict(self):
457
+ """Tests that the node attribute dictionary of the two graphs is
458
+ the same object.
459
+
460
+ """
461
+ for v in self.H:
462
+ assert self.G.nodes[v] == self.H.nodes[v]
463
+ # Making a change to G should make a change in H and vice versa.
464
+ self.G.nodes[0]["name"] = "foo"
465
+ assert self.G.nodes[0] == self.H.nodes[0]
466
+ self.H.nodes[1]["name"] = "bar"
467
+ assert self.G.nodes[1] == self.H.nodes[1]
468
+
469
+ def test_edge_attr_dict(self):
470
+ """Tests that the edge attribute dictionary of the two graphs is
471
+ the same object.
472
+
473
+ """
474
+ for u, v, k in self.H.edges(keys=True):
475
+ assert self.G._adj[u][v][k] == self.H._adj[u][v][k]
476
+ # Making a change to G should make a change in H and vice versa.
477
+ self.G._adj[0][1][0]["name"] = "foo"
478
+ assert self.G._adj[0][1][0]["name"] == self.H._adj[0][1][0]["name"]
479
+ self.H._adj[3][4][1]["name"] = "bar"
480
+ assert self.G._adj[3][4][1]["name"] == self.H._adj[3][4][1]["name"]
481
+
482
+ def test_graph_attr_dict(self):
483
+ """Tests that the graph attribute dictionary of the two graphs
484
+ is the same object.
485
+
486
+ """
487
+ assert self.G.graph is self.H.graph
488
+
489
+
490
+ class CustomDictClass(UserDict):
491
+ pass
492
+
493
+
494
+ class MultiGraphSubClass(nx.MultiGraph):
495
+ node_dict_factory = CustomDictClass # type: ignore[assignment]
496
+ node_attr_dict_factory = CustomDictClass # type: ignore[assignment]
497
+ adjlist_outer_dict_factory = CustomDictClass # type: ignore[assignment]
498
+ adjlist_inner_dict_factory = CustomDictClass # type: ignore[assignment]
499
+ edge_key_dict_factory = CustomDictClass # type: ignore[assignment]
500
+ edge_attr_dict_factory = CustomDictClass # type: ignore[assignment]
501
+ graph_attr_dict_factory = CustomDictClass # type: ignore[assignment]
502
+
503
+
504
+ class TestMultiGraphSubclass(TestMultiGraph):
505
+ def setup_method(self):
506
+ self.Graph = MultiGraphSubClass
507
+ # build K3
508
+ self.k3edges = [(0, 1), (0, 2), (1, 2)]
509
+ self.k3nodes = [0, 1, 2]
510
+ self.K3 = self.Graph()
511
+ self.K3._adj = self.K3.adjlist_outer_dict_factory(
512
+ {
513
+ 0: self.K3.adjlist_inner_dict_factory(),
514
+ 1: self.K3.adjlist_inner_dict_factory(),
515
+ 2: self.K3.adjlist_inner_dict_factory(),
516
+ }
517
+ )
518
+ self.K3._pred = {0: {}, 1: {}, 2: {}}
519
+ for u in self.k3nodes:
520
+ for v in self.k3nodes:
521
+ if u != v:
522
+ d = {0: {}}
523
+ self.K3._adj[u][v] = d
524
+ self.K3._adj[v][u] = d
525
+ self.K3._node = self.K3.node_dict_factory()
526
+ self.K3._node[0] = self.K3.node_attr_dict_factory()
527
+ self.K3._node[1] = self.K3.node_attr_dict_factory()
528
+ self.K3._node[2] = self.K3.node_attr_dict_factory()
minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_reportviews.py ADDED
@@ -0,0 +1,1435 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pickle
2
+ from copy import deepcopy
3
+
4
+ import pytest
5
+
6
+ import networkx as nx
7
+ from networkx.classes import reportviews as rv
8
+ from networkx.classes.reportviews import NodeDataView
9
+
10
+
11
+ # Nodes
12
+ class TestNodeView:
13
+ @classmethod
14
+ def setup_class(cls):
15
+ cls.G = nx.path_graph(9)
16
+ cls.nv = cls.G.nodes # NodeView(G)
17
+
18
+ def test_pickle(self):
19
+ import pickle
20
+
21
+ nv = self.nv
22
+ pnv = pickle.loads(pickle.dumps(nv, -1))
23
+ assert nv == pnv
24
+ assert nv.__slots__ == pnv.__slots__
25
+
26
+ def test_str(self):
27
+ assert str(self.nv) == "[0, 1, 2, 3, 4, 5, 6, 7, 8]"
28
+
29
+ def test_repr(self):
30
+ assert repr(self.nv) == "NodeView((0, 1, 2, 3, 4, 5, 6, 7, 8))"
31
+
32
+ def test_contains(self):
33
+ G = self.G.copy()
34
+ nv = G.nodes
35
+ assert 7 in nv
36
+ assert 9 not in nv
37
+ G.remove_node(7)
38
+ G.add_node(9)
39
+ assert 7 not in nv
40
+ assert 9 in nv
41
+
42
+ def test_getitem(self):
43
+ G = self.G.copy()
44
+ nv = G.nodes
45
+ G.nodes[3]["foo"] = "bar"
46
+ assert nv[7] == {}
47
+ assert nv[3] == {"foo": "bar"}
48
+ # slicing
49
+ with pytest.raises(nx.NetworkXError):
50
+ G.nodes[0:5]
51
+
52
+ def test_iter(self):
53
+ nv = self.nv
54
+ for i, n in enumerate(nv):
55
+ assert i == n
56
+ inv = iter(nv)
57
+ assert next(inv) == 0
58
+ assert iter(nv) != nv
59
+ assert iter(inv) == inv
60
+ inv2 = iter(nv)
61
+ next(inv2)
62
+ assert list(inv) == list(inv2)
63
+ # odd case where NodeView calls NodeDataView with data=False
64
+ nnv = nv(data=False)
65
+ for i, n in enumerate(nnv):
66
+ assert i == n
67
+
68
+ def test_call(self):
69
+ nodes = self.nv
70
+ assert nodes is nodes()
71
+ assert nodes is not nodes(data=True)
72
+ assert nodes is not nodes(data="weight")
73
+
74
+
75
+ class TestNodeDataView:
76
+ @classmethod
77
+ def setup_class(cls):
78
+ cls.G = nx.path_graph(9)
79
+ cls.nv = NodeDataView(cls.G)
80
+ cls.ndv = cls.G.nodes.data(True)
81
+ cls.nwv = cls.G.nodes.data("foo")
82
+
83
+ def test_viewtype(self):
84
+ nv = self.G.nodes
85
+ ndvfalse = nv.data(False)
86
+ assert nv is ndvfalse
87
+ assert nv is not self.ndv
88
+
89
+ def test_pickle(self):
90
+ import pickle
91
+
92
+ nv = self.nv
93
+ pnv = pickle.loads(pickle.dumps(nv, -1))
94
+ assert nv == pnv
95
+ assert nv.__slots__ == pnv.__slots__
96
+
97
+ def test_str(self):
98
+ msg = str([(n, {}) for n in range(9)])
99
+ assert str(self.ndv) == msg
100
+
101
+ def test_repr(self):
102
+ expected = "NodeDataView((0, 1, 2, 3, 4, 5, 6, 7, 8))"
103
+ assert repr(self.nv) == expected
104
+ expected = (
105
+ "NodeDataView({0: {}, 1: {}, 2: {}, 3: {}, "
106
+ + "4: {}, 5: {}, 6: {}, 7: {}, 8: {}})"
107
+ )
108
+ assert repr(self.ndv) == expected
109
+ expected = (
110
+ "NodeDataView({0: None, 1: None, 2: None, 3: None, 4: None, "
111
+ + "5: None, 6: None, 7: None, 8: None}, data='foo')"
112
+ )
113
+ assert repr(self.nwv) == expected
114
+
115
+ def test_contains(self):
116
+ G = self.G.copy()
117
+ nv = G.nodes.data()
118
+ nwv = G.nodes.data("foo")
119
+ G.nodes[3]["foo"] = "bar"
120
+ assert (7, {}) in nv
121
+ assert (3, {"foo": "bar"}) in nv
122
+ assert (3, "bar") in nwv
123
+ assert (7, None) in nwv
124
+ # default
125
+ nwv_def = G.nodes(data="foo", default="biz")
126
+ assert (7, "biz") in nwv_def
127
+ assert (3, "bar") in nwv_def
128
+
129
+ def test_getitem(self):
130
+ G = self.G.copy()
131
+ nv = G.nodes
132
+ G.nodes[3]["foo"] = "bar"
133
+ assert nv[3] == {"foo": "bar"}
134
+ # default
135
+ nwv_def = G.nodes(data="foo", default="biz")
136
+ assert nwv_def[7], "biz"
137
+ assert nwv_def[3] == "bar"
138
+ # slicing
139
+ with pytest.raises(nx.NetworkXError):
140
+ G.nodes.data()[0:5]
141
+
142
+ def test_iter(self):
143
+ G = self.G.copy()
144
+ nv = G.nodes.data()
145
+ ndv = G.nodes.data(True)
146
+ nwv = G.nodes.data("foo")
147
+ for i, (n, d) in enumerate(nv):
148
+ assert i == n
149
+ assert d == {}
150
+ inv = iter(nv)
151
+ assert next(inv) == (0, {})
152
+ G.nodes[3]["foo"] = "bar"
153
+ # default
154
+ for n, d in nv:
155
+ if n == 3:
156
+ assert d == {"foo": "bar"}
157
+ else:
158
+ assert d == {}
159
+ # data=True
160
+ for n, d in ndv:
161
+ if n == 3:
162
+ assert d == {"foo": "bar"}
163
+ else:
164
+ assert d == {}
165
+ # data='foo'
166
+ for n, d in nwv:
167
+ if n == 3:
168
+ assert d == "bar"
169
+ else:
170
+ assert d is None
171
+ # data='foo', default=1
172
+ for n, d in G.nodes.data("foo", default=1):
173
+ if n == 3:
174
+ assert d == "bar"
175
+ else:
176
+ assert d == 1
177
+
178
+
179
+ def test_nodedataview_unhashable():
180
+ G = nx.path_graph(9)
181
+ G.nodes[3]["foo"] = "bar"
182
+ nvs = [G.nodes.data()]
183
+ nvs.append(G.nodes.data(True))
184
+ H = G.copy()
185
+ H.nodes[4]["foo"] = {1, 2, 3}
186
+ nvs.append(H.nodes.data(True))
187
+ # raise unhashable
188
+ for nv in nvs:
189
+ pytest.raises(TypeError, set, nv)
190
+ pytest.raises(TypeError, eval, "nv | nv", locals())
191
+ # no raise... hashable
192
+ Gn = G.nodes.data(False)
193
+ set(Gn)
194
+ Gn | Gn
195
+ Gn = G.nodes.data("foo")
196
+ set(Gn)
197
+ Gn | Gn
198
+
199
+
200
+ class TestNodeViewSetOps:
201
+ @classmethod
202
+ def setup_class(cls):
203
+ cls.G = nx.path_graph(9)
204
+ cls.G.nodes[3]["foo"] = "bar"
205
+ cls.nv = cls.G.nodes
206
+
207
+ def n_its(self, nodes):
208
+ return set(nodes)
209
+
210
+ def test_len(self):
211
+ G = self.G.copy()
212
+ nv = G.nodes
213
+ assert len(nv) == 9
214
+ G.remove_node(7)
215
+ assert len(nv) == 8
216
+ G.add_node(9)
217
+ assert len(nv) == 9
218
+
219
+ def test_and(self):
220
+ # print("G & H nodes:", gnv & hnv)
221
+ nv = self.nv
222
+ some_nodes = self.n_its(range(5, 12))
223
+ assert nv & some_nodes == self.n_its(range(5, 9))
224
+ assert some_nodes & nv == self.n_its(range(5, 9))
225
+
226
+ def test_or(self):
227
+ # print("G | H nodes:", gnv | hnv)
228
+ nv = self.nv
229
+ some_nodes = self.n_its(range(5, 12))
230
+ assert nv | some_nodes == self.n_its(range(12))
231
+ assert some_nodes | nv == self.n_its(range(12))
232
+
233
+ def test_xor(self):
234
+ # print("G ^ H nodes:", gnv ^ hnv)
235
+ nv = self.nv
236
+ some_nodes = self.n_its(range(5, 12))
237
+ nodes = {0, 1, 2, 3, 4, 9, 10, 11}
238
+ assert nv ^ some_nodes == self.n_its(nodes)
239
+ assert some_nodes ^ nv == self.n_its(nodes)
240
+
241
+ def test_sub(self):
242
+ # print("G - H nodes:", gnv - hnv)
243
+ nv = self.nv
244
+ some_nodes = self.n_its(range(5, 12))
245
+ assert nv - some_nodes == self.n_its(range(5))
246
+ assert some_nodes - nv == self.n_its(range(9, 12))
247
+
248
+
249
+ class TestNodeDataViewSetOps(TestNodeViewSetOps):
250
+ @classmethod
251
+ def setup_class(cls):
252
+ cls.G = nx.path_graph(9)
253
+ cls.G.nodes[3]["foo"] = "bar"
254
+ cls.nv = cls.G.nodes.data("foo")
255
+
256
+ def n_its(self, nodes):
257
+ return {(node, "bar" if node == 3 else None) for node in nodes}
258
+
259
+
260
+ class TestNodeDataViewDefaultSetOps(TestNodeDataViewSetOps):
261
+ @classmethod
262
+ def setup_class(cls):
263
+ cls.G = nx.path_graph(9)
264
+ cls.G.nodes[3]["foo"] = "bar"
265
+ cls.nv = cls.G.nodes.data("foo", default=1)
266
+
267
+ def n_its(self, nodes):
268
+ return {(node, "bar" if node == 3 else 1) for node in nodes}
269
+
270
+
271
+ # Edges Data View
272
+ class TestEdgeDataView:
273
+ @classmethod
274
+ def setup_class(cls):
275
+ cls.G = nx.path_graph(9)
276
+ cls.eview = nx.reportviews.EdgeView
277
+
278
+ def test_pickle(self):
279
+ import pickle
280
+
281
+ ev = self.eview(self.G)(data=True)
282
+ pev = pickle.loads(pickle.dumps(ev, -1))
283
+ assert list(ev) == list(pev)
284
+ assert ev.__slots__ == pev.__slots__
285
+
286
+ def modify_edge(self, G, e, **kwds):
287
+ G._adj[e[0]][e[1]].update(kwds)
288
+
289
+ def test_str(self):
290
+ ev = self.eview(self.G)(data=True)
291
+ rep = str([(n, n + 1, {}) for n in range(8)])
292
+ assert str(ev) == rep
293
+
294
+ def test_repr(self):
295
+ ev = self.eview(self.G)(data=True)
296
+ rep = (
297
+ "EdgeDataView([(0, 1, {}), (1, 2, {}), "
298
+ + "(2, 3, {}), (3, 4, {}), "
299
+ + "(4, 5, {}), (5, 6, {}), "
300
+ + "(6, 7, {}), (7, 8, {})])"
301
+ )
302
+ assert repr(ev) == rep
303
+
304
+ def test_iterdata(self):
305
+ G = self.G.copy()
306
+ evr = self.eview(G)
307
+ ev = evr(data=True)
308
+ ev_def = evr(data="foo", default=1)
309
+
310
+ for u, v, d in ev:
311
+ pass
312
+ assert d == {}
313
+
314
+ for u, v, wt in ev_def:
315
+ pass
316
+ assert wt == 1
317
+
318
+ self.modify_edge(G, (2, 3), foo="bar")
319
+ for e in ev:
320
+ assert len(e) == 3
321
+ if set(e[:2]) == {2, 3}:
322
+ assert e[2] == {"foo": "bar"}
323
+ checked = True
324
+ else:
325
+ assert e[2] == {}
326
+ assert checked
327
+
328
+ for e in ev_def:
329
+ assert len(e) == 3
330
+ if set(e[:2]) == {2, 3}:
331
+ assert e[2] == "bar"
332
+ checked_wt = True
333
+ else:
334
+ assert e[2] == 1
335
+ assert checked_wt
336
+
337
+ def test_iter(self):
338
+ evr = self.eview(self.G)
339
+ ev = evr()
340
+ for u, v in ev:
341
+ pass
342
+ iev = iter(ev)
343
+ assert next(iev) == (0, 1)
344
+ assert iter(ev) != ev
345
+ assert iter(iev) == iev
346
+
347
+ def test_contains(self):
348
+ evr = self.eview(self.G)
349
+ ev = evr()
350
+ if self.G.is_directed():
351
+ assert (1, 2) in ev and (2, 1) not in ev
352
+ else:
353
+ assert (1, 2) in ev and (2, 1) in ev
354
+ assert (1, 4) not in ev
355
+ assert (1, 90) not in ev
356
+ assert (90, 1) not in ev
357
+
358
+ def test_contains_with_nbunch(self):
359
+ evr = self.eview(self.G)
360
+ ev = evr(nbunch=[0, 2])
361
+ if self.G.is_directed():
362
+ assert (0, 1) in ev
363
+ assert (1, 2) not in ev
364
+ assert (2, 3) in ev
365
+ else:
366
+ assert (0, 1) in ev
367
+ assert (1, 2) in ev
368
+ assert (2, 3) in ev
369
+ assert (3, 4) not in ev
370
+ assert (4, 5) not in ev
371
+ assert (5, 6) not in ev
372
+ assert (7, 8) not in ev
373
+ assert (8, 9) not in ev
374
+
375
+ def test_len(self):
376
+ evr = self.eview(self.G)
377
+ ev = evr(data="foo")
378
+ assert len(ev) == 8
379
+ assert len(evr(1)) == 2
380
+ assert len(evr([1, 2, 3])) == 4
381
+
382
+ assert len(self.G.edges(1)) == 2
383
+ assert len(self.G.edges()) == 8
384
+ assert len(self.G.edges) == 8
385
+
386
+ H = self.G.copy()
387
+ H.add_edge(1, 1)
388
+ assert len(H.edges(1)) == 3
389
+ assert len(H.edges()) == 9
390
+ assert len(H.edges) == 9
391
+
392
+
393
+ class TestOutEdgeDataView(TestEdgeDataView):
394
+ @classmethod
395
+ def setup_class(cls):
396
+ cls.G = nx.path_graph(9, create_using=nx.DiGraph())
397
+ cls.eview = nx.reportviews.OutEdgeView
398
+
399
+ def test_repr(self):
400
+ ev = self.eview(self.G)(data=True)
401
+ rep = (
402
+ "OutEdgeDataView([(0, 1, {}), (1, 2, {}), "
403
+ + "(2, 3, {}), (3, 4, {}), "
404
+ + "(4, 5, {}), (5, 6, {}), "
405
+ + "(6, 7, {}), (7, 8, {})])"
406
+ )
407
+ assert repr(ev) == rep
408
+
409
+ def test_len(self):
410
+ evr = self.eview(self.G)
411
+ ev = evr(data="foo")
412
+ assert len(ev) == 8
413
+ assert len(evr(1)) == 1
414
+ assert len(evr([1, 2, 3])) == 3
415
+
416
+ assert len(self.G.edges(1)) == 1
417
+ assert len(self.G.edges()) == 8
418
+ assert len(self.G.edges) == 8
419
+
420
+ H = self.G.copy()
421
+ H.add_edge(1, 1)
422
+ assert len(H.edges(1)) == 2
423
+ assert len(H.edges()) == 9
424
+ assert len(H.edges) == 9
425
+
426
+ def test_contains_with_nbunch(self):
427
+ evr = self.eview(self.G)
428
+ ev = evr(nbunch=[0, 2])
429
+ assert (0, 1) in ev
430
+ assert (1, 2) not in ev
431
+ assert (2, 3) in ev
432
+ assert (3, 4) not in ev
433
+ assert (4, 5) not in ev
434
+ assert (5, 6) not in ev
435
+ assert (7, 8) not in ev
436
+ assert (8, 9) not in ev
437
+
438
+
439
+ class TestInEdgeDataView(TestOutEdgeDataView):
440
+ @classmethod
441
+ def setup_class(cls):
442
+ cls.G = nx.path_graph(9, create_using=nx.DiGraph())
443
+ cls.eview = nx.reportviews.InEdgeView
444
+
445
+ def test_repr(self):
446
+ ev = self.eview(self.G)(data=True)
447
+ rep = (
448
+ "InEdgeDataView([(0, 1, {}), (1, 2, {}), "
449
+ + "(2, 3, {}), (3, 4, {}), "
450
+ + "(4, 5, {}), (5, 6, {}), "
451
+ + "(6, 7, {}), (7, 8, {})])"
452
+ )
453
+ assert repr(ev) == rep
454
+
455
+ def test_contains_with_nbunch(self):
456
+ evr = self.eview(self.G)
457
+ ev = evr(nbunch=[0, 2])
458
+ assert (0, 1) not in ev
459
+ assert (1, 2) in ev
460
+ assert (2, 3) not in ev
461
+ assert (3, 4) not in ev
462
+ assert (4, 5) not in ev
463
+ assert (5, 6) not in ev
464
+ assert (7, 8) not in ev
465
+ assert (8, 9) not in ev
466
+
467
+
468
+ class TestMultiEdgeDataView(TestEdgeDataView):
469
+ @classmethod
470
+ def setup_class(cls):
471
+ cls.G = nx.path_graph(9, create_using=nx.MultiGraph())
472
+ cls.eview = nx.reportviews.MultiEdgeView
473
+
474
+ def modify_edge(self, G, e, **kwds):
475
+ G._adj[e[0]][e[1]][0].update(kwds)
476
+
477
+ def test_repr(self):
478
+ ev = self.eview(self.G)(data=True)
479
+ rep = (
480
+ "MultiEdgeDataView([(0, 1, {}), (1, 2, {}), "
481
+ + "(2, 3, {}), (3, 4, {}), "
482
+ + "(4, 5, {}), (5, 6, {}), "
483
+ + "(6, 7, {}), (7, 8, {})])"
484
+ )
485
+ assert repr(ev) == rep
486
+
487
+ def test_contains_with_nbunch(self):
488
+ evr = self.eview(self.G)
489
+ ev = evr(nbunch=[0, 2])
490
+ assert (0, 1) in ev
491
+ assert (1, 2) in ev
492
+ assert (2, 3) in ev
493
+ assert (3, 4) not in ev
494
+ assert (4, 5) not in ev
495
+ assert (5, 6) not in ev
496
+ assert (7, 8) not in ev
497
+ assert (8, 9) not in ev
498
+
499
+
500
+ class TestOutMultiEdgeDataView(TestOutEdgeDataView):
501
+ @classmethod
502
+ def setup_class(cls):
503
+ cls.G = nx.path_graph(9, create_using=nx.MultiDiGraph())
504
+ cls.eview = nx.reportviews.OutMultiEdgeView
505
+
506
+ def modify_edge(self, G, e, **kwds):
507
+ G._adj[e[0]][e[1]][0].update(kwds)
508
+
509
+ def test_repr(self):
510
+ ev = self.eview(self.G)(data=True)
511
+ rep = (
512
+ "OutMultiEdgeDataView([(0, 1, {}), (1, 2, {}), "
513
+ + "(2, 3, {}), (3, 4, {}), "
514
+ + "(4, 5, {}), (5, 6, {}), "
515
+ + "(6, 7, {}), (7, 8, {})])"
516
+ )
517
+ assert repr(ev) == rep
518
+
519
+ def test_contains_with_nbunch(self):
520
+ evr = self.eview(self.G)
521
+ ev = evr(nbunch=[0, 2])
522
+ assert (0, 1) in ev
523
+ assert (1, 2) not in ev
524
+ assert (2, 3) in ev
525
+ assert (3, 4) not in ev
526
+ assert (4, 5) not in ev
527
+ assert (5, 6) not in ev
528
+ assert (7, 8) not in ev
529
+ assert (8, 9) not in ev
530
+
531
+
532
+ class TestInMultiEdgeDataView(TestOutMultiEdgeDataView):
533
+ @classmethod
534
+ def setup_class(cls):
535
+ cls.G = nx.path_graph(9, create_using=nx.MultiDiGraph())
536
+ cls.eview = nx.reportviews.InMultiEdgeView
537
+
538
+ def test_repr(self):
539
+ ev = self.eview(self.G)(data=True)
540
+ rep = (
541
+ "InMultiEdgeDataView([(0, 1, {}), (1, 2, {}), "
542
+ + "(2, 3, {}), (3, 4, {}), "
543
+ + "(4, 5, {}), (5, 6, {}), "
544
+ + "(6, 7, {}), (7, 8, {})])"
545
+ )
546
+ assert repr(ev) == rep
547
+
548
+ def test_contains_with_nbunch(self):
549
+ evr = self.eview(self.G)
550
+ ev = evr(nbunch=[0, 2])
551
+ assert (0, 1) not in ev
552
+ assert (1, 2) in ev
553
+ assert (2, 3) not in ev
554
+ assert (3, 4) not in ev
555
+ assert (4, 5) not in ev
556
+ assert (5, 6) not in ev
557
+ assert (7, 8) not in ev
558
+ assert (8, 9) not in ev
559
+
560
+
561
+ # Edge Views
562
+ class TestEdgeView:
563
+ @classmethod
564
+ def setup_class(cls):
565
+ cls.G = nx.path_graph(9)
566
+ cls.eview = nx.reportviews.EdgeView
567
+
568
+ def test_pickle(self):
569
+ import pickle
570
+
571
+ ev = self.eview(self.G)
572
+ pev = pickle.loads(pickle.dumps(ev, -1))
573
+ assert ev == pev
574
+ assert ev.__slots__ == pev.__slots__
575
+
576
+ def modify_edge(self, G, e, **kwds):
577
+ G._adj[e[0]][e[1]].update(kwds)
578
+
579
+ def test_str(self):
580
+ ev = self.eview(self.G)
581
+ rep = str([(n, n + 1) for n in range(8)])
582
+ assert str(ev) == rep
583
+
584
+ def test_repr(self):
585
+ ev = self.eview(self.G)
586
+ rep = (
587
+ "EdgeView([(0, 1), (1, 2), (2, 3), (3, 4), "
588
+ + "(4, 5), (5, 6), (6, 7), (7, 8)])"
589
+ )
590
+ assert repr(ev) == rep
591
+
592
+ def test_getitem(self):
593
+ G = self.G.copy()
594
+ ev = G.edges
595
+ G.edges[0, 1]["foo"] = "bar"
596
+ assert ev[0, 1] == {"foo": "bar"}
597
+
598
+ # slicing
599
+ with pytest.raises(nx.NetworkXError, match=".*does not support slicing"):
600
+ G.edges[0:5]
601
+
602
+ # Invalid edge
603
+ with pytest.raises(KeyError, match=r".*edge.*is not in the graph."):
604
+ G.edges[0, 9]
605
+
606
+ def test_call(self):
607
+ ev = self.eview(self.G)
608
+ assert id(ev) == id(ev())
609
+ assert id(ev) == id(ev(data=False))
610
+ assert id(ev) != id(ev(data=True))
611
+ assert id(ev) != id(ev(nbunch=1))
612
+
613
+ def test_data(self):
614
+ ev = self.eview(self.G)
615
+ assert id(ev) != id(ev.data())
616
+ assert id(ev) == id(ev.data(data=False))
617
+ assert id(ev) != id(ev.data(data=True))
618
+ assert id(ev) != id(ev.data(nbunch=1))
619
+
620
+ def test_iter(self):
621
+ ev = self.eview(self.G)
622
+ for u, v in ev:
623
+ pass
624
+ iev = iter(ev)
625
+ assert next(iev) == (0, 1)
626
+ assert iter(ev) != ev
627
+ assert iter(iev) == iev
628
+
629
+ def test_contains(self):
630
+ ev = self.eview(self.G)
631
+ edv = ev()
632
+ if self.G.is_directed():
633
+ assert (1, 2) in ev and (2, 1) not in ev
634
+ assert (1, 2) in edv and (2, 1) not in edv
635
+ else:
636
+ assert (1, 2) in ev and (2, 1) in ev
637
+ assert (1, 2) in edv and (2, 1) in edv
638
+ assert (1, 4) not in ev
639
+ assert (1, 4) not in edv
640
+ # edge not in graph
641
+ assert (1, 90) not in ev
642
+ assert (90, 1) not in ev
643
+ assert (1, 90) not in edv
644
+ assert (90, 1) not in edv
645
+
646
+ def test_contains_with_nbunch(self):
647
+ ev = self.eview(self.G)
648
+ evn = ev(nbunch=[0, 2])
649
+ assert (0, 1) in evn
650
+ assert (1, 2) in evn
651
+ assert (2, 3) in evn
652
+ assert (3, 4) not in evn
653
+ assert (4, 5) not in evn
654
+ assert (5, 6) not in evn
655
+ assert (7, 8) not in evn
656
+ assert (8, 9) not in evn
657
+
658
+ def test_len(self):
659
+ ev = self.eview(self.G)
660
+ num_ed = 9 if self.G.is_multigraph() else 8
661
+ assert len(ev) == num_ed
662
+
663
+ H = self.G.copy()
664
+ H.add_edge(1, 1)
665
+ assert len(H.edges(1)) == 3 + H.is_multigraph() - H.is_directed()
666
+ assert len(H.edges()) == num_ed + 1
667
+ assert len(H.edges) == num_ed + 1
668
+
669
+ def test_and(self):
670
+ # print("G & H edges:", gnv & hnv)
671
+ ev = self.eview(self.G)
672
+ some_edges = {(0, 1), (1, 0), (0, 2)}
673
+ if self.G.is_directed():
674
+ assert some_edges & ev, {(0, 1)}
675
+ assert ev & some_edges, {(0, 1)}
676
+ else:
677
+ assert ev & some_edges == {(0, 1), (1, 0)}
678
+ assert some_edges & ev == {(0, 1), (1, 0)}
679
+ return
680
+
681
+ def test_or(self):
682
+ # print("G | H edges:", gnv | hnv)
683
+ ev = self.eview(self.G)
684
+ some_edges = {(0, 1), (1, 0), (0, 2)}
685
+ result1 = {(n, n + 1) for n in range(8)}
686
+ result1.update(some_edges)
687
+ result2 = {(n + 1, n) for n in range(8)}
688
+ result2.update(some_edges)
689
+ assert (ev | some_edges) in (result1, result2)
690
+ assert (some_edges | ev) in (result1, result2)
691
+
692
+ def test_xor(self):
693
+ # print("G ^ H edges:", gnv ^ hnv)
694
+ ev = self.eview(self.G)
695
+ some_edges = {(0, 1), (1, 0), (0, 2)}
696
+ if self.G.is_directed():
697
+ result = {(n, n + 1) for n in range(1, 8)}
698
+ result.update({(1, 0), (0, 2)})
699
+ assert ev ^ some_edges == result
700
+ else:
701
+ result = {(n, n + 1) for n in range(1, 8)}
702
+ result.update({(0, 2)})
703
+ assert ev ^ some_edges == result
704
+ return
705
+
706
+ def test_sub(self):
707
+ # print("G - H edges:", gnv - hnv)
708
+ ev = self.eview(self.G)
709
+ some_edges = {(0, 1), (1, 0), (0, 2)}
710
+ result = {(n, n + 1) for n in range(8)}
711
+ result.remove((0, 1))
712
+ assert ev - some_edges, result
713
+
714
+
715
+ class TestOutEdgeView(TestEdgeView):
716
+ @classmethod
717
+ def setup_class(cls):
718
+ cls.G = nx.path_graph(9, nx.DiGraph())
719
+ cls.eview = nx.reportviews.OutEdgeView
720
+
721
+ def test_repr(self):
722
+ ev = self.eview(self.G)
723
+ rep = (
724
+ "OutEdgeView([(0, 1), (1, 2), (2, 3), (3, 4), "
725
+ + "(4, 5), (5, 6), (6, 7), (7, 8)])"
726
+ )
727
+ assert repr(ev) == rep
728
+
729
+ def test_contains_with_nbunch(self):
730
+ ev = self.eview(self.G)
731
+ evn = ev(nbunch=[0, 2])
732
+ assert (0, 1) in evn
733
+ assert (1, 2) not in evn
734
+ assert (2, 3) in evn
735
+ assert (3, 4) not in evn
736
+ assert (4, 5) not in evn
737
+ assert (5, 6) not in evn
738
+ assert (7, 8) not in evn
739
+ assert (8, 9) not in evn
740
+
741
+
742
+ class TestInEdgeView(TestEdgeView):
743
+ @classmethod
744
+ def setup_class(cls):
745
+ cls.G = nx.path_graph(9, nx.DiGraph())
746
+ cls.eview = nx.reportviews.InEdgeView
747
+
748
+ def test_repr(self):
749
+ ev = self.eview(self.G)
750
+ rep = (
751
+ "InEdgeView([(0, 1), (1, 2), (2, 3), (3, 4), "
752
+ + "(4, 5), (5, 6), (6, 7), (7, 8)])"
753
+ )
754
+ assert repr(ev) == rep
755
+
756
+ def test_contains_with_nbunch(self):
757
+ ev = self.eview(self.G)
758
+ evn = ev(nbunch=[0, 2])
759
+ assert (0, 1) not in evn
760
+ assert (1, 2) in evn
761
+ assert (2, 3) not in evn
762
+ assert (3, 4) not in evn
763
+ assert (4, 5) not in evn
764
+ assert (5, 6) not in evn
765
+ assert (7, 8) not in evn
766
+ assert (8, 9) not in evn
767
+
768
+
769
+ class TestMultiEdgeView(TestEdgeView):
770
+ @classmethod
771
+ def setup_class(cls):
772
+ cls.G = nx.path_graph(9, nx.MultiGraph())
773
+ cls.G.add_edge(1, 2, key=3, foo="bar")
774
+ cls.eview = nx.reportviews.MultiEdgeView
775
+
776
+ def modify_edge(self, G, e, **kwds):
777
+ if len(e) == 2:
778
+ e = e + (0,)
779
+ G._adj[e[0]][e[1]][e[2]].update(kwds)
780
+
781
+ def test_str(self):
782
+ ev = self.eview(self.G)
783
+ replist = [(n, n + 1, 0) for n in range(8)]
784
+ replist.insert(2, (1, 2, 3))
785
+ rep = str(replist)
786
+ assert str(ev) == rep
787
+
788
+ def test_getitem(self):
789
+ G = self.G.copy()
790
+ ev = G.edges
791
+ G.edges[0, 1, 0]["foo"] = "bar"
792
+ assert ev[0, 1, 0] == {"foo": "bar"}
793
+
794
+ # slicing
795
+ with pytest.raises(nx.NetworkXError):
796
+ G.edges[0:5]
797
+
798
+ def test_repr(self):
799
+ ev = self.eview(self.G)
800
+ rep = (
801
+ "MultiEdgeView([(0, 1, 0), (1, 2, 0), (1, 2, 3), (2, 3, 0), "
802
+ + "(3, 4, 0), (4, 5, 0), (5, 6, 0), (6, 7, 0), (7, 8, 0)])"
803
+ )
804
+ assert repr(ev) == rep
805
+
806
+ def test_call(self):
807
+ ev = self.eview(self.G)
808
+ assert id(ev) == id(ev(keys=True))
809
+ assert id(ev) == id(ev(data=False, keys=True))
810
+ assert id(ev) != id(ev(keys=False))
811
+ assert id(ev) != id(ev(data=True))
812
+ assert id(ev) != id(ev(nbunch=1))
813
+
814
+ def test_data(self):
815
+ ev = self.eview(self.G)
816
+ assert id(ev) != id(ev.data())
817
+ assert id(ev) == id(ev.data(data=False, keys=True))
818
+ assert id(ev) != id(ev.data(keys=False))
819
+ assert id(ev) != id(ev.data(data=True))
820
+ assert id(ev) != id(ev.data(nbunch=1))
821
+
822
+ def test_iter(self):
823
+ ev = self.eview(self.G)
824
+ for u, v, k in ev:
825
+ pass
826
+ iev = iter(ev)
827
+ assert next(iev) == (0, 1, 0)
828
+ assert iter(ev) != ev
829
+ assert iter(iev) == iev
830
+
831
+ def test_iterkeys(self):
832
+ G = self.G
833
+ evr = self.eview(G)
834
+ ev = evr(keys=True)
835
+ for u, v, k in ev:
836
+ pass
837
+ assert k == 0
838
+ ev = evr(keys=True, data="foo", default=1)
839
+ for u, v, k, wt in ev:
840
+ pass
841
+ assert wt == 1
842
+
843
+ self.modify_edge(G, (2, 3, 0), foo="bar")
844
+ ev = evr(keys=True, data=True)
845
+ for e in ev:
846
+ assert len(e) == 4
847
+ print("edge:", e)
848
+ if set(e[:2]) == {2, 3}:
849
+ print(self.G._adj[2][3])
850
+ assert e[2] == 0
851
+ assert e[3] == {"foo": "bar"}
852
+ checked = True
853
+ elif set(e[:3]) == {1, 2, 3}:
854
+ assert e[2] == 3
855
+ assert e[3] == {"foo": "bar"}
856
+ checked_multi = True
857
+ else:
858
+ assert e[2] == 0
859
+ assert e[3] == {}
860
+ assert checked
861
+ assert checked_multi
862
+ ev = evr(keys=True, data="foo", default=1)
863
+ for e in ev:
864
+ if set(e[:2]) == {1, 2} and e[2] == 3:
865
+ assert e[3] == "bar"
866
+ if set(e[:2]) == {1, 2} and e[2] == 0:
867
+ assert e[3] == 1
868
+ if set(e[:2]) == {2, 3}:
869
+ assert e[2] == 0
870
+ assert e[3] == "bar"
871
+ assert len(e) == 4
872
+ checked_wt = True
873
+ assert checked_wt
874
+ ev = evr(keys=True)
875
+ for e in ev:
876
+ assert len(e) == 3
877
+ elist = sorted([(i, i + 1, 0) for i in range(8)] + [(1, 2, 3)])
878
+ assert sorted(ev) == elist
879
+ # test that the keyword arguments are passed correctly
880
+ ev = evr((1, 2), "foo", keys=True, default=1)
881
+ with pytest.raises(TypeError):
882
+ evr((1, 2), "foo", True, 1)
883
+ with pytest.raises(TypeError):
884
+ evr((1, 2), "foo", True, default=1)
885
+ for e in ev:
886
+ if set(e[:2]) == {1, 2}:
887
+ assert e[2] in {0, 3}
888
+ if e[2] == 3:
889
+ assert e[3] == "bar"
890
+ else: # e[2] == 0
891
+ assert e[3] == 1
892
+ if G.is_directed():
893
+ assert len(list(ev)) == 3
894
+ else:
895
+ assert len(list(ev)) == 4
896
+
897
+ def test_or(self):
898
+ # print("G | H edges:", gnv | hnv)
899
+ ev = self.eview(self.G)
900
+ some_edges = {(0, 1, 0), (1, 0, 0), (0, 2, 0)}
901
+ result = {(n, n + 1, 0) for n in range(8)}
902
+ result.update(some_edges)
903
+ result.update({(1, 2, 3)})
904
+ assert ev | some_edges == result
905
+ assert some_edges | ev == result
906
+
907
+ def test_sub(self):
908
+ # print("G - H edges:", gnv - hnv)
909
+ ev = self.eview(self.G)
910
+ some_edges = {(0, 1, 0), (1, 0, 0), (0, 2, 0)}
911
+ result = {(n, n + 1, 0) for n in range(8)}
912
+ result.remove((0, 1, 0))
913
+ result.update({(1, 2, 3)})
914
+ assert ev - some_edges, result
915
+ assert some_edges - ev, result
916
+
917
+ def test_xor(self):
918
+ # print("G ^ H edges:", gnv ^ hnv)
919
+ ev = self.eview(self.G)
920
+ some_edges = {(0, 1, 0), (1, 0, 0), (0, 2, 0)}
921
+ if self.G.is_directed():
922
+ result = {(n, n + 1, 0) for n in range(1, 8)}
923
+ result.update({(1, 0, 0), (0, 2, 0), (1, 2, 3)})
924
+ assert ev ^ some_edges == result
925
+ assert some_edges ^ ev == result
926
+ else:
927
+ result = {(n, n + 1, 0) for n in range(1, 8)}
928
+ result.update({(0, 2, 0), (1, 2, 3)})
929
+ assert ev ^ some_edges == result
930
+ assert some_edges ^ ev == result
931
+
932
+ def test_and(self):
933
+ # print("G & H edges:", gnv & hnv)
934
+ ev = self.eview(self.G)
935
+ some_edges = {(0, 1, 0), (1, 0, 0), (0, 2, 0)}
936
+ if self.G.is_directed():
937
+ assert ev & some_edges == {(0, 1, 0)}
938
+ assert some_edges & ev == {(0, 1, 0)}
939
+ else:
940
+ assert ev & some_edges == {(0, 1, 0), (1, 0, 0)}
941
+ assert some_edges & ev == {(0, 1, 0), (1, 0, 0)}
942
+
943
+ def test_contains_with_nbunch(self):
944
+ ev = self.eview(self.G)
945
+ evn = ev(nbunch=[0, 2])
946
+ assert (0, 1) in evn
947
+ assert (1, 2) in evn
948
+ assert (2, 3) in evn
949
+ assert (3, 4) not in evn
950
+ assert (4, 5) not in evn
951
+ assert (5, 6) not in evn
952
+ assert (7, 8) not in evn
953
+ assert (8, 9) not in evn
954
+
955
+
956
+ class TestOutMultiEdgeView(TestMultiEdgeView):
957
+ @classmethod
958
+ def setup_class(cls):
959
+ cls.G = nx.path_graph(9, nx.MultiDiGraph())
960
+ cls.G.add_edge(1, 2, key=3, foo="bar")
961
+ cls.eview = nx.reportviews.OutMultiEdgeView
962
+
963
+ def modify_edge(self, G, e, **kwds):
964
+ if len(e) == 2:
965
+ e = e + (0,)
966
+ G._adj[e[0]][e[1]][e[2]].update(kwds)
967
+
968
+ def test_repr(self):
969
+ ev = self.eview(self.G)
970
+ rep = (
971
+ "OutMultiEdgeView([(0, 1, 0), (1, 2, 0), (1, 2, 3), (2, 3, 0),"
972
+ + " (3, 4, 0), (4, 5, 0), (5, 6, 0), (6, 7, 0), (7, 8, 0)])"
973
+ )
974
+ assert repr(ev) == rep
975
+
976
+ def test_contains_with_nbunch(self):
977
+ ev = self.eview(self.G)
978
+ evn = ev(nbunch=[0, 2])
979
+ assert (0, 1) in evn
980
+ assert (1, 2) not in evn
981
+ assert (2, 3) in evn
982
+ assert (3, 4) not in evn
983
+ assert (4, 5) not in evn
984
+ assert (5, 6) not in evn
985
+ assert (7, 8) not in evn
986
+ assert (8, 9) not in evn
987
+
988
+
989
+ class TestInMultiEdgeView(TestMultiEdgeView):
990
+ @classmethod
991
+ def setup_class(cls):
992
+ cls.G = nx.path_graph(9, nx.MultiDiGraph())
993
+ cls.G.add_edge(1, 2, key=3, foo="bar")
994
+ cls.eview = nx.reportviews.InMultiEdgeView
995
+
996
+ def modify_edge(self, G, e, **kwds):
997
+ if len(e) == 2:
998
+ e = e + (0,)
999
+ G._adj[e[0]][e[1]][e[2]].update(kwds)
1000
+
1001
+ def test_repr(self):
1002
+ ev = self.eview(self.G)
1003
+ rep = (
1004
+ "InMultiEdgeView([(0, 1, 0), (1, 2, 0), (1, 2, 3), (2, 3, 0), "
1005
+ + "(3, 4, 0), (4, 5, 0), (5, 6, 0), (6, 7, 0), (7, 8, 0)])"
1006
+ )
1007
+ assert repr(ev) == rep
1008
+
1009
+ def test_contains_with_nbunch(self):
1010
+ ev = self.eview(self.G)
1011
+ evn = ev(nbunch=[0, 2])
1012
+ assert (0, 1) not in evn
1013
+ assert (1, 2) in evn
1014
+ assert (2, 3) not in evn
1015
+ assert (3, 4) not in evn
1016
+ assert (4, 5) not in evn
1017
+ assert (5, 6) not in evn
1018
+ assert (7, 8) not in evn
1019
+ assert (8, 9) not in evn
1020
+
1021
+
1022
+ # Degrees
1023
+ class TestDegreeView:
1024
+ GRAPH = nx.Graph
1025
+ dview = nx.reportviews.DegreeView
1026
+
1027
+ @classmethod
1028
+ def setup_class(cls):
1029
+ cls.G = nx.path_graph(6, cls.GRAPH())
1030
+ cls.G.add_edge(1, 3, foo=2)
1031
+ cls.G.add_edge(1, 3, foo=3)
1032
+
1033
+ def test_pickle(self):
1034
+ import pickle
1035
+
1036
+ deg = self.G.degree
1037
+ pdeg = pickle.loads(pickle.dumps(deg, -1))
1038
+ assert dict(deg) == dict(pdeg)
1039
+
1040
+ def test_str(self):
1041
+ dv = self.dview(self.G)
1042
+ rep = str([(0, 1), (1, 3), (2, 2), (3, 3), (4, 2), (5, 1)])
1043
+ assert str(dv) == rep
1044
+ dv = self.G.degree()
1045
+ assert str(dv) == rep
1046
+
1047
+ def test_repr(self):
1048
+ dv = self.dview(self.G)
1049
+ rep = "DegreeView({0: 1, 1: 3, 2: 2, 3: 3, 4: 2, 5: 1})"
1050
+ assert repr(dv) == rep
1051
+
1052
+ def test_iter(self):
1053
+ dv = self.dview(self.G)
1054
+ for n, d in dv:
1055
+ pass
1056
+ idv = iter(dv)
1057
+ assert iter(dv) != dv
1058
+ assert iter(idv) == idv
1059
+ assert next(idv) == (0, dv[0])
1060
+ assert next(idv) == (1, dv[1])
1061
+ # weighted
1062
+ dv = self.dview(self.G, weight="foo")
1063
+ for n, d in dv:
1064
+ pass
1065
+ idv = iter(dv)
1066
+ assert iter(dv) != dv
1067
+ assert iter(idv) == idv
1068
+ assert next(idv) == (0, dv[0])
1069
+ assert next(idv) == (1, dv[1])
1070
+
1071
+ def test_nbunch(self):
1072
+ dv = self.dview(self.G)
1073
+ dvn = dv(0)
1074
+ assert dvn == 1
1075
+ dvn = dv([2, 3])
1076
+ assert sorted(dvn) == [(2, 2), (3, 3)]
1077
+
1078
+ def test_getitem(self):
1079
+ dv = self.dview(self.G)
1080
+ assert dv[0] == 1
1081
+ assert dv[1] == 3
1082
+ assert dv[2] == 2
1083
+ assert dv[3] == 3
1084
+ dv = self.dview(self.G, weight="foo")
1085
+ assert dv[0] == 1
1086
+ assert dv[1] == 5
1087
+ assert dv[2] == 2
1088
+ assert dv[3] == 5
1089
+
1090
+ def test_weight(self):
1091
+ dv = self.dview(self.G)
1092
+ dvw = dv(0, weight="foo")
1093
+ assert dvw == 1
1094
+ dvw = dv(1, weight="foo")
1095
+ assert dvw == 5
1096
+ dvw = dv([2, 3], weight="foo")
1097
+ assert sorted(dvw) == [(2, 2), (3, 5)]
1098
+ dvd = dict(dv(weight="foo"))
1099
+ assert dvd[0] == 1
1100
+ assert dvd[1] == 5
1101
+ assert dvd[2] == 2
1102
+ assert dvd[3] == 5
1103
+
1104
+ def test_len(self):
1105
+ dv = self.dview(self.G)
1106
+ assert len(dv) == 6
1107
+
1108
+
1109
+ class TestDiDegreeView(TestDegreeView):
1110
+ GRAPH = nx.DiGraph
1111
+ dview = nx.reportviews.DiDegreeView
1112
+
1113
+ def test_repr(self):
1114
+ dv = self.G.degree()
1115
+ rep = "DiDegreeView({0: 1, 1: 3, 2: 2, 3: 3, 4: 2, 5: 1})"
1116
+ assert repr(dv) == rep
1117
+
1118
+
1119
+ class TestOutDegreeView(TestDegreeView):
1120
+ GRAPH = nx.DiGraph
1121
+ dview = nx.reportviews.OutDegreeView
1122
+
1123
+ def test_str(self):
1124
+ dv = self.dview(self.G)
1125
+ rep = str([(0, 1), (1, 2), (2, 1), (3, 1), (4, 1), (5, 0)])
1126
+ assert str(dv) == rep
1127
+ dv = self.G.out_degree()
1128
+ assert str(dv) == rep
1129
+
1130
+ def test_repr(self):
1131
+ dv = self.G.out_degree()
1132
+ rep = "OutDegreeView({0: 1, 1: 2, 2: 1, 3: 1, 4: 1, 5: 0})"
1133
+ assert repr(dv) == rep
1134
+
1135
+ def test_nbunch(self):
1136
+ dv = self.dview(self.G)
1137
+ dvn = dv(0)
1138
+ assert dvn == 1
1139
+ dvn = dv([2, 3])
1140
+ assert sorted(dvn) == [(2, 1), (3, 1)]
1141
+
1142
+ def test_getitem(self):
1143
+ dv = self.dview(self.G)
1144
+ assert dv[0] == 1
1145
+ assert dv[1] == 2
1146
+ assert dv[2] == 1
1147
+ assert dv[3] == 1
1148
+ dv = self.dview(self.G, weight="foo")
1149
+ assert dv[0] == 1
1150
+ assert dv[1] == 4
1151
+ assert dv[2] == 1
1152
+ assert dv[3] == 1
1153
+
1154
+ def test_weight(self):
1155
+ dv = self.dview(self.G)
1156
+ dvw = dv(0, weight="foo")
1157
+ assert dvw == 1
1158
+ dvw = dv(1, weight="foo")
1159
+ assert dvw == 4
1160
+ dvw = dv([2, 3], weight="foo")
1161
+ assert sorted(dvw) == [(2, 1), (3, 1)]
1162
+ dvd = dict(dv(weight="foo"))
1163
+ assert dvd[0] == 1
1164
+ assert dvd[1] == 4
1165
+ assert dvd[2] == 1
1166
+ assert dvd[3] == 1
1167
+
1168
+
1169
+ class TestInDegreeView(TestDegreeView):
1170
+ GRAPH = nx.DiGraph
1171
+ dview = nx.reportviews.InDegreeView
1172
+
1173
+ def test_str(self):
1174
+ dv = self.dview(self.G)
1175
+ rep = str([(0, 0), (1, 1), (2, 1), (3, 2), (4, 1), (5, 1)])
1176
+ assert str(dv) == rep
1177
+ dv = self.G.in_degree()
1178
+ assert str(dv) == rep
1179
+
1180
+ def test_repr(self):
1181
+ dv = self.G.in_degree()
1182
+ rep = "InDegreeView({0: 0, 1: 1, 2: 1, 3: 2, 4: 1, 5: 1})"
1183
+ assert repr(dv) == rep
1184
+
1185
+ def test_nbunch(self):
1186
+ dv = self.dview(self.G)
1187
+ dvn = dv(0)
1188
+ assert dvn == 0
1189
+ dvn = dv([2, 3])
1190
+ assert sorted(dvn) == [(2, 1), (3, 2)]
1191
+
1192
+ def test_getitem(self):
1193
+ dv = self.dview(self.G)
1194
+ assert dv[0] == 0
1195
+ assert dv[1] == 1
1196
+ assert dv[2] == 1
1197
+ assert dv[3] == 2
1198
+ dv = self.dview(self.G, weight="foo")
1199
+ assert dv[0] == 0
1200
+ assert dv[1] == 1
1201
+ assert dv[2] == 1
1202
+ assert dv[3] == 4
1203
+
1204
+ def test_weight(self):
1205
+ dv = self.dview(self.G)
1206
+ dvw = dv(0, weight="foo")
1207
+ assert dvw == 0
1208
+ dvw = dv(1, weight="foo")
1209
+ assert dvw == 1
1210
+ dvw = dv([2, 3], weight="foo")
1211
+ assert sorted(dvw) == [(2, 1), (3, 4)]
1212
+ dvd = dict(dv(weight="foo"))
1213
+ assert dvd[0] == 0
1214
+ assert dvd[1] == 1
1215
+ assert dvd[2] == 1
1216
+ assert dvd[3] == 4
1217
+
1218
+
1219
+ class TestMultiDegreeView(TestDegreeView):
1220
+ GRAPH = nx.MultiGraph
1221
+ dview = nx.reportviews.MultiDegreeView
1222
+
1223
+ def test_str(self):
1224
+ dv = self.dview(self.G)
1225
+ rep = str([(0, 1), (1, 4), (2, 2), (3, 4), (4, 2), (5, 1)])
1226
+ assert str(dv) == rep
1227
+ dv = self.G.degree()
1228
+ assert str(dv) == rep
1229
+
1230
+ def test_repr(self):
1231
+ dv = self.G.degree()
1232
+ rep = "MultiDegreeView({0: 1, 1: 4, 2: 2, 3: 4, 4: 2, 5: 1})"
1233
+ assert repr(dv) == rep
1234
+
1235
+ def test_nbunch(self):
1236
+ dv = self.dview(self.G)
1237
+ dvn = dv(0)
1238
+ assert dvn == 1
1239
+ dvn = dv([2, 3])
1240
+ assert sorted(dvn) == [(2, 2), (3, 4)]
1241
+
1242
+ def test_getitem(self):
1243
+ dv = self.dview(self.G)
1244
+ assert dv[0] == 1
1245
+ assert dv[1] == 4
1246
+ assert dv[2] == 2
1247
+ assert dv[3] == 4
1248
+ dv = self.dview(self.G, weight="foo")
1249
+ assert dv[0] == 1
1250
+ assert dv[1] == 7
1251
+ assert dv[2] == 2
1252
+ assert dv[3] == 7
1253
+
1254
+ def test_weight(self):
1255
+ dv = self.dview(self.G)
1256
+ dvw = dv(0, weight="foo")
1257
+ assert dvw == 1
1258
+ dvw = dv(1, weight="foo")
1259
+ assert dvw == 7
1260
+ dvw = dv([2, 3], weight="foo")
1261
+ assert sorted(dvw) == [(2, 2), (3, 7)]
1262
+ dvd = dict(dv(weight="foo"))
1263
+ assert dvd[0] == 1
1264
+ assert dvd[1] == 7
1265
+ assert dvd[2] == 2
1266
+ assert dvd[3] == 7
1267
+
1268
+
1269
+ class TestDiMultiDegreeView(TestMultiDegreeView):
1270
+ GRAPH = nx.MultiDiGraph
1271
+ dview = nx.reportviews.DiMultiDegreeView
1272
+
1273
+ def test_repr(self):
1274
+ dv = self.G.degree()
1275
+ rep = "DiMultiDegreeView({0: 1, 1: 4, 2: 2, 3: 4, 4: 2, 5: 1})"
1276
+ assert repr(dv) == rep
1277
+
1278
+
1279
+ class TestOutMultiDegreeView(TestDegreeView):
1280
+ GRAPH = nx.MultiDiGraph
1281
+ dview = nx.reportviews.OutMultiDegreeView
1282
+
1283
+ def test_str(self):
1284
+ dv = self.dview(self.G)
1285
+ rep = str([(0, 1), (1, 3), (2, 1), (3, 1), (4, 1), (5, 0)])
1286
+ assert str(dv) == rep
1287
+ dv = self.G.out_degree()
1288
+ assert str(dv) == rep
1289
+
1290
+ def test_repr(self):
1291
+ dv = self.G.out_degree()
1292
+ rep = "OutMultiDegreeView({0: 1, 1: 3, 2: 1, 3: 1, 4: 1, 5: 0})"
1293
+ assert repr(dv) == rep
1294
+
1295
+ def test_nbunch(self):
1296
+ dv = self.dview(self.G)
1297
+ dvn = dv(0)
1298
+ assert dvn == 1
1299
+ dvn = dv([2, 3])
1300
+ assert sorted(dvn) == [(2, 1), (3, 1)]
1301
+
1302
+ def test_getitem(self):
1303
+ dv = self.dview(self.G)
1304
+ assert dv[0] == 1
1305
+ assert dv[1] == 3
1306
+ assert dv[2] == 1
1307
+ assert dv[3] == 1
1308
+ dv = self.dview(self.G, weight="foo")
1309
+ assert dv[0] == 1
1310
+ assert dv[1] == 6
1311
+ assert dv[2] == 1
1312
+ assert dv[3] == 1
1313
+
1314
+ def test_weight(self):
1315
+ dv = self.dview(self.G)
1316
+ dvw = dv(0, weight="foo")
1317
+ assert dvw == 1
1318
+ dvw = dv(1, weight="foo")
1319
+ assert dvw == 6
1320
+ dvw = dv([2, 3], weight="foo")
1321
+ assert sorted(dvw) == [(2, 1), (3, 1)]
1322
+ dvd = dict(dv(weight="foo"))
1323
+ assert dvd[0] == 1
1324
+ assert dvd[1] == 6
1325
+ assert dvd[2] == 1
1326
+ assert dvd[3] == 1
1327
+
1328
+
1329
+ class TestInMultiDegreeView(TestDegreeView):
1330
+ GRAPH = nx.MultiDiGraph
1331
+ dview = nx.reportviews.InMultiDegreeView
1332
+
1333
+ def test_str(self):
1334
+ dv = self.dview(self.G)
1335
+ rep = str([(0, 0), (1, 1), (2, 1), (3, 3), (4, 1), (5, 1)])
1336
+ assert str(dv) == rep
1337
+ dv = self.G.in_degree()
1338
+ assert str(dv) == rep
1339
+
1340
+ def test_repr(self):
1341
+ dv = self.G.in_degree()
1342
+ rep = "InMultiDegreeView({0: 0, 1: 1, 2: 1, 3: 3, 4: 1, 5: 1})"
1343
+ assert repr(dv) == rep
1344
+
1345
+ def test_nbunch(self):
1346
+ dv = self.dview(self.G)
1347
+ dvn = dv(0)
1348
+ assert dvn == 0
1349
+ dvn = dv([2, 3])
1350
+ assert sorted(dvn) == [(2, 1), (3, 3)]
1351
+
1352
+ def test_getitem(self):
1353
+ dv = self.dview(self.G)
1354
+ assert dv[0] == 0
1355
+ assert dv[1] == 1
1356
+ assert dv[2] == 1
1357
+ assert dv[3] == 3
1358
+ dv = self.dview(self.G, weight="foo")
1359
+ assert dv[0] == 0
1360
+ assert dv[1] == 1
1361
+ assert dv[2] == 1
1362
+ assert dv[3] == 6
1363
+
1364
+ def test_weight(self):
1365
+ dv = self.dview(self.G)
1366
+ dvw = dv(0, weight="foo")
1367
+ assert dvw == 0
1368
+ dvw = dv(1, weight="foo")
1369
+ assert dvw == 1
1370
+ dvw = dv([2, 3], weight="foo")
1371
+ assert sorted(dvw) == [(2, 1), (3, 6)]
1372
+ dvd = dict(dv(weight="foo"))
1373
+ assert dvd[0] == 0
1374
+ assert dvd[1] == 1
1375
+ assert dvd[2] == 1
1376
+ assert dvd[3] == 6
1377
+
1378
+
1379
+ @pytest.mark.parametrize(
1380
+ ("reportview", "err_msg_terms"),
1381
+ (
1382
+ (rv.NodeView, "list(G.nodes"),
1383
+ (rv.NodeDataView, "list(G.nodes.data"),
1384
+ (rv.EdgeView, "list(G.edges"),
1385
+ # Directed EdgeViews
1386
+ (rv.InEdgeView, "list(G.in_edges"),
1387
+ (rv.OutEdgeView, "list(G.edges"),
1388
+ # Multi EdgeViews
1389
+ (rv.MultiEdgeView, "list(G.edges"),
1390
+ (rv.InMultiEdgeView, "list(G.in_edges"),
1391
+ (rv.OutMultiEdgeView, "list(G.edges"),
1392
+ ),
1393
+ )
1394
+ def test_slicing_reportviews(reportview, err_msg_terms):
1395
+ G = nx.complete_graph(3)
1396
+ view = reportview(G)
1397
+ with pytest.raises(nx.NetworkXError) as exc:
1398
+ view[0:2]
1399
+ errmsg = str(exc.value)
1400
+ assert type(view).__name__ in errmsg
1401
+ assert err_msg_terms in errmsg
1402
+
1403
+
1404
+ @pytest.mark.parametrize(
1405
+ "graph", [nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph]
1406
+ )
1407
+ def test_cache_dict_get_set_state(graph):
1408
+ G = nx.path_graph(5, graph())
1409
+ G.nodes, G.edges, G.adj, G.degree
1410
+ if G.is_directed():
1411
+ G.pred, G.succ, G.in_edges, G.out_edges, G.in_degree, G.out_degree
1412
+ cached_dict = G.__dict__
1413
+ assert "nodes" in cached_dict
1414
+ assert "edges" in cached_dict
1415
+ assert "adj" in cached_dict
1416
+ assert "degree" in cached_dict
1417
+ if G.is_directed():
1418
+ assert "pred" in cached_dict
1419
+ assert "succ" in cached_dict
1420
+ assert "in_edges" in cached_dict
1421
+ assert "out_edges" in cached_dict
1422
+ assert "in_degree" in cached_dict
1423
+ assert "out_degree" in cached_dict
1424
+
1425
+ # Raises error if the cached properties and views do not work
1426
+ pickle.loads(pickle.dumps(G, -1))
1427
+ deepcopy(G)
1428
+
1429
+
1430
+ def test_edge_views_inherit_from_EdgeViewABC():
1431
+ all_edge_view_classes = (v for v in dir(nx.reportviews) if "Edge" in v)
1432
+ for eview_class in all_edge_view_classes:
1433
+ assert issubclass(
1434
+ getattr(nx.reportviews, eview_class), nx.reportviews.EdgeViewABC
1435
+ )
minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_special.py ADDED
@@ -0,0 +1,131 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import networkx as nx
2
+
3
+ from .test_digraph import BaseDiGraphTester
4
+ from .test_digraph import TestDiGraph as _TestDiGraph
5
+ from .test_graph import BaseGraphTester
6
+ from .test_graph import TestGraph as _TestGraph
7
+ from .test_multidigraph import TestMultiDiGraph as _TestMultiDiGraph
8
+ from .test_multigraph import TestMultiGraph as _TestMultiGraph
9
+
10
+
11
+ def test_factories():
12
+ class mydict1(dict):
13
+ pass
14
+
15
+ class mydict2(dict):
16
+ pass
17
+
18
+ class mydict3(dict):
19
+ pass
20
+
21
+ class mydict4(dict):
22
+ pass
23
+
24
+ class mydict5(dict):
25
+ pass
26
+
27
+ for Graph in (nx.Graph, nx.DiGraph, nx.MultiGraph, nx.MultiDiGraph):
28
+ # print("testing class: ", Graph.__name__)
29
+ class MyGraph(Graph):
30
+ node_dict_factory = mydict1
31
+ adjlist_outer_dict_factory = mydict2
32
+ adjlist_inner_dict_factory = mydict3
33
+ edge_key_dict_factory = mydict4
34
+ edge_attr_dict_factory = mydict5
35
+
36
+ G = MyGraph()
37
+ assert isinstance(G._node, mydict1)
38
+ assert isinstance(G._adj, mydict2)
39
+ G.add_node(1)
40
+ assert isinstance(G._adj[1], mydict3)
41
+ if G.is_directed():
42
+ assert isinstance(G._pred, mydict2)
43
+ assert isinstance(G._succ, mydict2)
44
+ assert isinstance(G._pred[1], mydict3)
45
+ G.add_edge(1, 2)
46
+ if G.is_multigraph():
47
+ assert isinstance(G._adj[1][2], mydict4)
48
+ assert isinstance(G._adj[1][2][0], mydict5)
49
+ else:
50
+ assert isinstance(G._adj[1][2], mydict5)
51
+
52
+
53
+ class TestSpecialGraph(_TestGraph):
54
+ def setup_method(self):
55
+ _TestGraph.setup_method(self)
56
+ self.Graph = nx.Graph
57
+
58
+
59
+ class TestThinGraph(BaseGraphTester):
60
+ def setup_method(self):
61
+ all_edge_dict = {"weight": 1}
62
+
63
+ class MyGraph(nx.Graph):
64
+ def edge_attr_dict_factory(self):
65
+ return all_edge_dict
66
+
67
+ self.Graph = MyGraph
68
+ # build dict-of-dict-of-dict K3
69
+ ed1, ed2, ed3 = (all_edge_dict, all_edge_dict, all_edge_dict)
70
+ self.k3adj = {0: {1: ed1, 2: ed2}, 1: {0: ed1, 2: ed3}, 2: {0: ed2, 1: ed3}}
71
+ self.k3edges = [(0, 1), (0, 2), (1, 2)]
72
+ self.k3nodes = [0, 1, 2]
73
+ self.K3 = self.Graph()
74
+ self.K3._adj = self.k3adj
75
+ self.K3._node = {}
76
+ self.K3._node[0] = {}
77
+ self.K3._node[1] = {}
78
+ self.K3._node[2] = {}
79
+
80
+
81
+ class TestSpecialDiGraph(_TestDiGraph):
82
+ def setup_method(self):
83
+ _TestDiGraph.setup_method(self)
84
+ self.Graph = nx.DiGraph
85
+
86
+
87
+ class TestThinDiGraph(BaseDiGraphTester):
88
+ def setup_method(self):
89
+ all_edge_dict = {"weight": 1}
90
+
91
+ class MyGraph(nx.DiGraph):
92
+ def edge_attr_dict_factory(self):
93
+ return all_edge_dict
94
+
95
+ self.Graph = MyGraph
96
+ # build dict-of-dict-of-dict K3
97
+ ed1, ed2, ed3 = (all_edge_dict, all_edge_dict, all_edge_dict)
98
+ ed4, ed5, ed6 = (all_edge_dict, all_edge_dict, all_edge_dict)
99
+ self.k3adj = {0: {1: ed1, 2: ed2}, 1: {0: ed3, 2: ed4}, 2: {0: ed5, 1: ed6}}
100
+ self.k3edges = [(0, 1), (0, 2), (1, 2)]
101
+ self.k3nodes = [0, 1, 2]
102
+ self.K3 = self.Graph()
103
+ self.K3._succ = self.k3adj
104
+ # K3._adj is synced with K3._succ
105
+ self.K3._pred = {0: {1: ed3, 2: ed5}, 1: {0: ed1, 2: ed6}, 2: {0: ed2, 1: ed4}}
106
+ self.K3._node = {}
107
+ self.K3._node[0] = {}
108
+ self.K3._node[1] = {}
109
+ self.K3._node[2] = {}
110
+
111
+ ed1, ed2 = (all_edge_dict, all_edge_dict)
112
+ self.P3 = self.Graph()
113
+ self.P3._succ = {0: {1: ed1}, 1: {2: ed2}, 2: {}}
114
+ # P3._adj is synced with P3._succ
115
+ self.P3._pred = {0: {}, 1: {0: ed1}, 2: {1: ed2}}
116
+ self.P3._node = {}
117
+ self.P3._node[0] = {}
118
+ self.P3._node[1] = {}
119
+ self.P3._node[2] = {}
120
+
121
+
122
+ class TestSpecialMultiGraph(_TestMultiGraph):
123
+ def setup_method(self):
124
+ _TestMultiGraph.setup_method(self)
125
+ self.Graph = nx.MultiGraph
126
+
127
+
128
+ class TestSpecialMultiDiGraph(_TestMultiDiGraph):
129
+ def setup_method(self):
130
+ _TestMultiDiGraph.setup_method(self)
131
+ self.Graph = nx.MultiDiGraph
minigpt2/lib/python3.10/site-packages/networkx/classes/tests/test_subgraphviews.py ADDED
@@ -0,0 +1,362 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import pytest
2
+
3
+ import networkx as nx
4
+ from networkx.utils import edges_equal
5
+
6
+
7
+ class TestSubGraphView:
8
+ gview = staticmethod(nx.subgraph_view)
9
+ graph = nx.Graph
10
+ hide_edges_filter = staticmethod(nx.filters.hide_edges)
11
+ show_edges_filter = staticmethod(nx.filters.show_edges)
12
+
13
+ @classmethod
14
+ def setup_class(cls):
15
+ cls.G = nx.path_graph(9, create_using=cls.graph())
16
+ cls.hide_edges_w_hide_nodes = {(3, 4), (4, 5), (5, 6)}
17
+
18
+ def test_hidden_nodes(self):
19
+ hide_nodes = [4, 5, 111]
20
+ nodes_gone = nx.filters.hide_nodes(hide_nodes)
21
+ gview = self.gview
22
+ G = gview(self.G, filter_node=nodes_gone)
23
+ assert self.G.nodes - G.nodes == {4, 5}
24
+ assert self.G.edges - G.edges == self.hide_edges_w_hide_nodes
25
+ if G.is_directed():
26
+ assert list(G[3]) == []
27
+ assert list(G[2]) == [3]
28
+ else:
29
+ assert list(G[3]) == [2]
30
+ assert set(G[2]) == {1, 3}
31
+ pytest.raises(KeyError, G.__getitem__, 4)
32
+ pytest.raises(KeyError, G.__getitem__, 112)
33
+ pytest.raises(KeyError, G.__getitem__, 111)
34
+ assert G.degree(3) == (3 if G.is_multigraph() else 1)
35
+ assert G.size() == (7 if G.is_multigraph() else 5)
36
+
37
+ def test_hidden_edges(self):
38
+ hide_edges = [(2, 3), (8, 7), (222, 223)]
39
+ edges_gone = self.hide_edges_filter(hide_edges)
40
+ gview = self.gview
41
+ G = gview(self.G, filter_edge=edges_gone)
42
+ assert self.G.nodes == G.nodes
43
+ if G.is_directed():
44
+ assert self.G.edges - G.edges == {(2, 3)}
45
+ assert list(G[2]) == []
46
+ assert list(G.pred[3]) == []
47
+ assert list(G.pred[2]) == [1]
48
+ assert G.size() == 7
49
+ else:
50
+ assert self.G.edges - G.edges == {(2, 3), (7, 8)}
51
+ assert list(G[2]) == [1]
52
+ assert G.size() == 6
53
+ assert list(G[3]) == [4]
54
+ pytest.raises(KeyError, G.__getitem__, 221)
55
+ pytest.raises(KeyError, G.__getitem__, 222)
56
+ assert G.degree(3) == 1
57
+
58
+ def test_shown_node(self):
59
+ induced_subgraph = nx.filters.show_nodes([2, 3, 111])
60
+ gview = self.gview
61
+ G = gview(self.G, filter_node=induced_subgraph)
62
+ assert set(G.nodes) == {2, 3}
63
+ if G.is_directed():
64
+ assert list(G[3]) == []
65
+ else:
66
+ assert list(G[3]) == [2]
67
+ assert list(G[2]) == [3]
68
+ pytest.raises(KeyError, G.__getitem__, 4)
69
+ pytest.raises(KeyError, G.__getitem__, 112)
70
+ pytest.raises(KeyError, G.__getitem__, 111)
71
+ assert G.degree(3) == (3 if G.is_multigraph() else 1)
72
+ assert G.size() == (3 if G.is_multigraph() else 1)
73
+
74
+ def test_shown_edges(self):
75
+ show_edges = [(2, 3), (8, 7), (222, 223)]
76
+ edge_subgraph = self.show_edges_filter(show_edges)
77
+ G = self.gview(self.G, filter_edge=edge_subgraph)
78
+ assert self.G.nodes == G.nodes
79
+ if G.is_directed():
80
+ assert G.edges == {(2, 3)}
81
+ assert list(G[3]) == []
82
+ assert list(G[2]) == [3]
83
+ assert list(G.pred[3]) == [2]
84
+ assert list(G.pred[2]) == []
85
+ assert G.size() == 1
86
+ else:
87
+ assert G.edges == {(2, 3), (7, 8)}
88
+ assert list(G[3]) == [2]
89
+ assert list(G[2]) == [3]
90
+ assert G.size() == 2
91
+ pytest.raises(KeyError, G.__getitem__, 221)
92
+ pytest.raises(KeyError, G.__getitem__, 222)
93
+ assert G.degree(3) == 1
94
+
95
+
96
+ class TestSubDiGraphView(TestSubGraphView):
97
+ gview = staticmethod(nx.subgraph_view)
98
+ graph = nx.DiGraph
99
+ hide_edges_filter = staticmethod(nx.filters.hide_diedges)
100
+ show_edges_filter = staticmethod(nx.filters.show_diedges)
101
+ hide_edges = [(2, 3), (8, 7), (222, 223)]
102
+ excluded = {(2, 3), (3, 4), (4, 5), (5, 6)}
103
+
104
+ def test_inoutedges(self):
105
+ edges_gone = self.hide_edges_filter(self.hide_edges)
106
+ hide_nodes = [4, 5, 111]
107
+ nodes_gone = nx.filters.hide_nodes(hide_nodes)
108
+ G = self.gview(self.G, filter_node=nodes_gone, filter_edge=edges_gone)
109
+
110
+ assert self.G.in_edges - G.in_edges == self.excluded
111
+ assert self.G.out_edges - G.out_edges == self.excluded
112
+
113
+ def test_pred(self):
114
+ edges_gone = self.hide_edges_filter(self.hide_edges)
115
+ hide_nodes = [4, 5, 111]
116
+ nodes_gone = nx.filters.hide_nodes(hide_nodes)
117
+ G = self.gview(self.G, filter_node=nodes_gone, filter_edge=edges_gone)
118
+
119
+ assert list(G.pred[2]) == [1]
120
+ assert list(G.pred[6]) == []
121
+
122
+ def test_inout_degree(self):
123
+ edges_gone = self.hide_edges_filter(self.hide_edges)
124
+ hide_nodes = [4, 5, 111]
125
+ nodes_gone = nx.filters.hide_nodes(hide_nodes)
126
+ G = self.gview(self.G, filter_node=nodes_gone, filter_edge=edges_gone)
127
+
128
+ assert G.degree(2) == 1
129
+ assert G.out_degree(2) == 0
130
+ assert G.in_degree(2) == 1
131
+ assert G.size() == 4
132
+
133
+
134
+ # multigraph
135
+ class TestMultiGraphView(TestSubGraphView):
136
+ gview = staticmethod(nx.subgraph_view)
137
+ graph = nx.MultiGraph
138
+ hide_edges_filter = staticmethod(nx.filters.hide_multiedges)
139
+ show_edges_filter = staticmethod(nx.filters.show_multiedges)
140
+
141
+ @classmethod
142
+ def setup_class(cls):
143
+ cls.G = nx.path_graph(9, create_using=cls.graph())
144
+ multiedges = {(2, 3, 4), (2, 3, 5)}
145
+ cls.G.add_edges_from(multiedges)
146
+ cls.hide_edges_w_hide_nodes = {(3, 4, 0), (4, 5, 0), (5, 6, 0)}
147
+
148
+ def test_hidden_edges(self):
149
+ hide_edges = [(2, 3, 4), (2, 3, 3), (8, 7, 0), (222, 223, 0)]
150
+ edges_gone = self.hide_edges_filter(hide_edges)
151
+ G = self.gview(self.G, filter_edge=edges_gone)
152
+ assert self.G.nodes == G.nodes
153
+ if G.is_directed():
154
+ assert self.G.edges - G.edges == {(2, 3, 4)}
155
+ assert list(G[3]) == [4]
156
+ assert list(G[2]) == [3]
157
+ assert list(G.pred[3]) == [2] # only one 2 but two edges
158
+ assert list(G.pred[2]) == [1]
159
+ assert G.size() == 9
160
+ else:
161
+ assert self.G.edges - G.edges == {(2, 3, 4), (7, 8, 0)}
162
+ assert list(G[3]) == [2, 4]
163
+ assert list(G[2]) == [1, 3]
164
+ assert G.size() == 8
165
+ assert G.degree(3) == 3
166
+ pytest.raises(KeyError, G.__getitem__, 221)
167
+ pytest.raises(KeyError, G.__getitem__, 222)
168
+
169
+ def test_shown_edges(self):
170
+ show_edges = [(2, 3, 4), (2, 3, 3), (8, 7, 0), (222, 223, 0)]
171
+ edge_subgraph = self.show_edges_filter(show_edges)
172
+ G = self.gview(self.G, filter_edge=edge_subgraph)
173
+ assert self.G.nodes == G.nodes
174
+ if G.is_directed():
175
+ assert G.edges == {(2, 3, 4)}
176
+ assert list(G[3]) == []
177
+ assert list(G.pred[3]) == [2]
178
+ assert list(G.pred[2]) == []
179
+ assert G.size() == 1
180
+ else:
181
+ assert G.edges == {(2, 3, 4), (7, 8, 0)}
182
+ assert G.size() == 2
183
+ assert list(G[3]) == [2]
184
+ assert G.degree(3) == 1
185
+ assert list(G[2]) == [3]
186
+ pytest.raises(KeyError, G.__getitem__, 221)
187
+ pytest.raises(KeyError, G.__getitem__, 222)
188
+
189
+
190
+ # multidigraph
191
+ class TestMultiDiGraphView(TestMultiGraphView, TestSubDiGraphView):
192
+ gview = staticmethod(nx.subgraph_view)
193
+ graph = nx.MultiDiGraph
194
+ hide_edges_filter = staticmethod(nx.filters.hide_multidiedges)
195
+ show_edges_filter = staticmethod(nx.filters.show_multidiedges)
196
+ hide_edges = [(2, 3, 0), (8, 7, 0), (222, 223, 0)]
197
+ excluded = {(2, 3, 0), (3, 4, 0), (4, 5, 0), (5, 6, 0)}
198
+
199
+ def test_inout_degree(self):
200
+ edges_gone = self.hide_edges_filter(self.hide_edges)
201
+ hide_nodes = [4, 5, 111]
202
+ nodes_gone = nx.filters.hide_nodes(hide_nodes)
203
+ G = self.gview(self.G, filter_node=nodes_gone, filter_edge=edges_gone)
204
+
205
+ assert G.degree(2) == 3
206
+ assert G.out_degree(2) == 2
207
+ assert G.in_degree(2) == 1
208
+ assert G.size() == 6
209
+
210
+
211
+ # induced_subgraph
212
+ class TestInducedSubGraph:
213
+ @classmethod
214
+ def setup_class(cls):
215
+ cls.K3 = G = nx.complete_graph(3)
216
+ G.graph["foo"] = []
217
+ G.nodes[0]["foo"] = []
218
+ G.remove_edge(1, 2)
219
+ ll = []
220
+ G.add_edge(1, 2, foo=ll)
221
+ G.add_edge(2, 1, foo=ll)
222
+
223
+ def test_full_graph(self):
224
+ G = self.K3
225
+ H = nx.induced_subgraph(G, [0, 1, 2, 5])
226
+ assert H.name == G.name
227
+ self.graphs_equal(H, G)
228
+ self.same_attrdict(H, G)
229
+
230
+ def test_partial_subgraph(self):
231
+ G = self.K3
232
+ H = nx.induced_subgraph(G, 0)
233
+ assert dict(H.adj) == {0: {}}
234
+ assert dict(G.adj) != {0: {}}
235
+
236
+ H = nx.induced_subgraph(G, [0, 1])
237
+ assert dict(H.adj) == {0: {1: {}}, 1: {0: {}}}
238
+
239
+ def same_attrdict(self, H, G):
240
+ old_foo = H[1][2]["foo"]
241
+ H.edges[1, 2]["foo"] = "baz"
242
+ assert G.edges == H.edges
243
+ H.edges[1, 2]["foo"] = old_foo
244
+ assert G.edges == H.edges
245
+ old_foo = H.nodes[0]["foo"]
246
+ H.nodes[0]["foo"] = "baz"
247
+ assert G.nodes == H.nodes
248
+ H.nodes[0]["foo"] = old_foo
249
+ assert G.nodes == H.nodes
250
+
251
+ def graphs_equal(self, H, G):
252
+ assert G._adj == H._adj
253
+ assert G._node == H._node
254
+ assert G.graph == H.graph
255
+ assert G.name == H.name
256
+ if not G.is_directed() and not H.is_directed():
257
+ assert H._adj[1][2] is H._adj[2][1]
258
+ assert G._adj[1][2] is G._adj[2][1]
259
+ else: # at least one is directed
260
+ if not G.is_directed():
261
+ G._pred = G._adj
262
+ G._succ = G._adj
263
+ if not H.is_directed():
264
+ H._pred = H._adj
265
+ H._succ = H._adj
266
+ assert G._pred == H._pred
267
+ assert G._succ == H._succ
268
+ assert H._succ[1][2] is H._pred[2][1]
269
+ assert G._succ[1][2] is G._pred[2][1]
270
+
271
+
272
+ # edge_subgraph
273
+ class TestEdgeSubGraph:
274
+ @classmethod
275
+ def setup_class(cls):
276
+ # Create a path graph on five nodes.
277
+ cls.G = G = nx.path_graph(5)
278
+ # Add some node, edge, and graph attributes.
279
+ for i in range(5):
280
+ G.nodes[i]["name"] = f"node{i}"
281
+ G.edges[0, 1]["name"] = "edge01"
282
+ G.edges[3, 4]["name"] = "edge34"
283
+ G.graph["name"] = "graph"
284
+ # Get the subgraph induced by the first and last edges.
285
+ cls.H = nx.edge_subgraph(G, [(0, 1), (3, 4)])
286
+
287
+ def test_correct_nodes(self):
288
+ """Tests that the subgraph has the correct nodes."""
289
+ assert [(0, "node0"), (1, "node1"), (3, "node3"), (4, "node4")] == sorted(
290
+ self.H.nodes.data("name")
291
+ )
292
+
293
+ def test_correct_edges(self):
294
+ """Tests that the subgraph has the correct edges."""
295
+ assert edges_equal(
296
+ [(0, 1, "edge01"), (3, 4, "edge34")], self.H.edges.data("name")
297
+ )
298
+
299
+ def test_add_node(self):
300
+ """Tests that adding a node to the original graph does not
301
+ affect the nodes of the subgraph.
302
+
303
+ """
304
+ self.G.add_node(5)
305
+ assert [0, 1, 3, 4] == sorted(self.H.nodes)
306
+ self.G.remove_node(5)
307
+
308
+ def test_remove_node(self):
309
+ """Tests that removing a node in the original graph
310
+ removes the nodes of the subgraph.
311
+
312
+ """
313
+ self.G.remove_node(0)
314
+ assert [1, 3, 4] == sorted(self.H.nodes)
315
+ self.G.add_node(0, name="node0")
316
+ self.G.add_edge(0, 1, name="edge01")
317
+
318
+ def test_node_attr_dict(self):
319
+ """Tests that the node attribute dictionary of the two graphs is
320
+ the same object.
321
+
322
+ """
323
+ for v in self.H:
324
+ assert self.G.nodes[v] == self.H.nodes[v]
325
+ # Making a change to G should make a change in H and vice versa.
326
+ self.G.nodes[0]["name"] = "foo"
327
+ assert self.G.nodes[0] == self.H.nodes[0]
328
+ self.H.nodes[1]["name"] = "bar"
329
+ assert self.G.nodes[1] == self.H.nodes[1]
330
+ # Revert the change, so tests pass with pytest-randomly
331
+ self.G.nodes[0]["name"] = "node0"
332
+ self.H.nodes[1]["name"] = "node1"
333
+
334
+ def test_edge_attr_dict(self):
335
+ """Tests that the edge attribute dictionary of the two graphs is
336
+ the same object.
337
+
338
+ """
339
+ for u, v in self.H.edges():
340
+ assert self.G.edges[u, v] == self.H.edges[u, v]
341
+ # Making a change to G should make a change in H and vice versa.
342
+ self.G.edges[0, 1]["name"] = "foo"
343
+ assert self.G.edges[0, 1]["name"] == self.H.edges[0, 1]["name"]
344
+ self.H.edges[3, 4]["name"] = "bar"
345
+ assert self.G.edges[3, 4]["name"] == self.H.edges[3, 4]["name"]
346
+ # Revert the change, so tests pass with pytest-randomly
347
+ self.G.edges[0, 1]["name"] = "edge01"
348
+ self.H.edges[3, 4]["name"] = "edge34"
349
+
350
+ def test_graph_attr_dict(self):
351
+ """Tests that the graph attribute dictionary of the two graphs
352
+ is the same object.
353
+
354
+ """
355
+ assert self.G.graph is self.H.graph
356
+
357
+ def test_readonly(self):
358
+ """Tests that the subgraph cannot change the graph structure"""
359
+ pytest.raises(nx.NetworkXError, self.H.add_node, 5)
360
+ pytest.raises(nx.NetworkXError, self.H.remove_node, 0)
361
+ pytest.raises(nx.NetworkXError, self.H.add_edge, 5, 6)
362
+ pytest.raises(nx.NetworkXError, self.H.remove_edge, 0, 1)
minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (711 Bytes). View file
 
minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/edgelist.cpython-310.pyc ADDED
Binary file (13.2 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/gexf.cpython-310.pyc ADDED
Binary file (25.1 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/gml.cpython-310.pyc ADDED
Binary file (24.4 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/graph6.cpython-310.pyc ADDED
Binary file (11.7 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/graphml.cpython-310.pyc ADDED
Binary file (28.5 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/multiline_adjlist.cpython-310.pyc ADDED
Binary file (9.53 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/readwrite/__pycache__/pajek.cpython-310.pyc ADDED
Binary file (6.75 kB). View file
 
minigpt2/lib/python3.10/site-packages/networkx/readwrite/json_graph/__pycache__/__init__.cpython-310.pyc ADDED
Binary file (865 Bytes). View file
 
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_efficient_attention_backward_ops.h ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // @generated by torchgen/gen.py from Operator.h
4
+
5
+ #include <tuple>
6
+ #include <vector>
7
+
8
+ // Forward declarations of any types needed in the operator signatures.
9
+ // We can't directly include these classes because it will cause circular include dependencies.
10
+ // This file is included by TensorBody.h, which defines the Tensor class.
11
+ #include <ATen/core/ATen_fwd.h>
12
+
13
+ namespace at {
14
+ namespace _ops {
15
+
16
+
17
+ struct TORCH_API _efficient_attention_backward {
18
+ using schema = ::std::tuple<at::Tensor,at::Tensor,at::Tensor,at::Tensor> (const at::Tensor &, const at::Tensor &, const at::Tensor &, const at::Tensor &, const ::std::optional<at::Tensor> &, const at::Tensor &, const ::std::optional<at::Tensor> &, const ::std::optional<at::Tensor> &, c10::SymInt, c10::SymInt, const at::Tensor &, double, const at::Tensor &, const at::Tensor &, int64_t, bool, ::std::optional<double>, ::std::optional<int64_t>, ::std::optional<int64_t>, bool);
19
+ using ptr_schema = schema*;
20
+ // See Note [static constexpr char* members for windows NVCC]
21
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_efficient_attention_backward")
22
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
23
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_efficient_attention_backward(Tensor grad_out_, Tensor query, Tensor key, Tensor value, Tensor? bias, Tensor out, Tensor? cu_seqlens_q, Tensor? cu_seqlens_k, SymInt max_seqlen_q, SymInt max_seqlen_k, Tensor logsumexp, float dropout_p, Tensor philox_seed, Tensor philox_offset, int custom_mask_type, bool bias_requires_grad, *, float? scale=None, int? num_splits_key=None, int? window_size=None, bool shared_storage_dqdkdv=False) -> (Tensor, Tensor, Tensor, Tensor)")
24
+ static ::std::tuple<at::Tensor,at::Tensor,at::Tensor,at::Tensor> call(const at::Tensor & grad_out_, const at::Tensor & query, const at::Tensor & key, const at::Tensor & value, const ::std::optional<at::Tensor> & bias, const at::Tensor & out, const ::std::optional<at::Tensor> & cu_seqlens_q, const ::std::optional<at::Tensor> & cu_seqlens_k, c10::SymInt max_seqlen_q, c10::SymInt max_seqlen_k, const at::Tensor & logsumexp, double dropout_p, const at::Tensor & philox_seed, const at::Tensor & philox_offset, int64_t custom_mask_type, bool bias_requires_grad, ::std::optional<double> scale, ::std::optional<int64_t> num_splits_key, ::std::optional<int64_t> window_size, bool shared_storage_dqdkdv);
25
+ static ::std::tuple<at::Tensor,at::Tensor,at::Tensor,at::Tensor> redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & grad_out_, const at::Tensor & query, const at::Tensor & key, const at::Tensor & value, const ::std::optional<at::Tensor> & bias, const at::Tensor & out, const ::std::optional<at::Tensor> & cu_seqlens_q, const ::std::optional<at::Tensor> & cu_seqlens_k, c10::SymInt max_seqlen_q, c10::SymInt max_seqlen_k, const at::Tensor & logsumexp, double dropout_p, const at::Tensor & philox_seed, const at::Tensor & philox_offset, int64_t custom_mask_type, bool bias_requires_grad, ::std::optional<double> scale, ::std::optional<int64_t> num_splits_key, ::std::optional<int64_t> window_size, bool shared_storage_dqdkdv);
26
+ };
27
+
28
+ }} // namespace at::_ops
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_foobar_compositeexplicitautograd_dispatch.h ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ // @generated by torchgen/gen.py from DispatchKeyFunction.h
3
+
4
+ // NB: The implementing C++ file is RegisterDispatchKey.cpp
5
+
6
+ // The only #includes we need are for custom classes that have defaults in the C++ API
7
+ #include <c10/core/MemoryFormat.h>
8
+ #include <c10/core/Scalar.h>
9
+ #include <ATen/core/Reduction.h>
10
+
11
+ // Forward declarations of any types needed in the operator signatures.
12
+ // We can't directly include these classes because it will cause circular include dependencies.
13
+ // This file is included by TensorBody.h, which defines the Tensor class.
14
+ #include <ATen/core/ATen_fwd.h>
15
+
16
+ namespace at {
17
+
18
+ namespace compositeexplicitautograd {
19
+
20
+ TORCH_API at::Tensor & _foobar_out(at::Tensor & out, const at::Tensor & self, bool arg1=true, bool arg2=true, bool arg3=true);
21
+ TORCH_API at::Tensor & _foobar_outf(const at::Tensor & self, bool arg1, bool arg2, bool arg3, at::Tensor & out);
22
+
23
+ } // namespace compositeexplicitautograd
24
+ } // namespace at
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_foreach_abs_ops.h ADDED
@@ -0,0 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // @generated by torchgen/gen.py from Operator.h
4
+
5
+ #include <tuple>
6
+ #include <vector>
7
+
8
+ // Forward declarations of any types needed in the operator signatures.
9
+ // We can't directly include these classes because it will cause circular include dependencies.
10
+ // This file is included by TensorBody.h, which defines the Tensor class.
11
+ #include <ATen/core/ATen_fwd.h>
12
+
13
+ namespace at {
14
+ namespace _ops {
15
+
16
+
17
+ struct TORCH_API _foreach_abs {
18
+ using schema = ::std::vector<at::Tensor> (at::TensorList);
19
+ using ptr_schema = schema*;
20
+ // See Note [static constexpr char* members for windows NVCC]
21
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_foreach_abs")
22
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
23
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_foreach_abs(Tensor[] self) -> Tensor[]")
24
+ static ::std::vector<at::Tensor> call(at::TensorList self);
25
+ static ::std::vector<at::Tensor> redispatch(c10::DispatchKeySet dispatchKeySet, at::TensorList self);
26
+ };
27
+
28
+ struct TORCH_API _foreach_abs_ {
29
+ using schema = void (at::TensorList);
30
+ using ptr_schema = schema*;
31
+ // See Note [static constexpr char* members for windows NVCC]
32
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_foreach_abs_")
33
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
34
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_foreach_abs_(Tensor(a!)[] self) -> ()")
35
+ static void call(at::TensorList self);
36
+ static void redispatch(c10::DispatchKeySet dispatchKeySet, at::TensorList self);
37
+ };
38
+
39
+ struct TORCH_API _foreach_abs_out {
40
+ using schema = void (at::TensorList, at::TensorList);
41
+ using ptr_schema = schema*;
42
+ // See Note [static constexpr char* members for windows NVCC]
43
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_foreach_abs")
44
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "out")
45
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_foreach_abs.out(Tensor[] self, *, Tensor(a!)[] out) -> ()")
46
+ static void call(at::TensorList self, at::TensorList out);
47
+ static void redispatch(c10::DispatchKeySet dispatchKeySet, at::TensorList self, at::TensorList out);
48
+ };
49
+
50
+ }} // namespace at::_ops
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_foreach_erf_cuda_dispatch.h ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ // @generated by torchgen/gen.py from DispatchKeyFunction.h
3
+
4
+ // NB: The implementing C++ file is RegisterDispatchKey.cpp
5
+
6
+ // The only #includes we need are for custom classes that have defaults in the C++ API
7
+ #include <c10/core/MemoryFormat.h>
8
+ #include <c10/core/Scalar.h>
9
+ #include <ATen/core/Reduction.h>
10
+
11
+ // Forward declarations of any types needed in the operator signatures.
12
+ // We can't directly include these classes because it will cause circular include dependencies.
13
+ // This file is included by TensorBody.h, which defines the Tensor class.
14
+ #include <ATen/core/ATen_fwd.h>
15
+
16
+ namespace at {
17
+
18
+ namespace cuda {
19
+
20
+ TORCH_API ::std::vector<at::Tensor> _foreach_erf(at::TensorList self);
21
+ TORCH_API void _foreach_erf_(at::TensorList self);
22
+
23
+ } // namespace cuda
24
+ } // namespace at
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_fw_primal_copy_ops.h ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // @generated by torchgen/gen.py from Operator.h
4
+
5
+ #include <tuple>
6
+ #include <vector>
7
+
8
+ // Forward declarations of any types needed in the operator signatures.
9
+ // We can't directly include these classes because it will cause circular include dependencies.
10
+ // This file is included by TensorBody.h, which defines the Tensor class.
11
+ #include <ATen/core/ATen_fwd.h>
12
+
13
+ namespace at {
14
+ namespace _ops {
15
+
16
+
17
+ struct TORCH_API _fw_primal_copy {
18
+ using schema = at::Tensor (const at::Tensor &, int64_t);
19
+ using ptr_schema = schema*;
20
+ // See Note [static constexpr char* members for windows NVCC]
21
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_fw_primal_copy")
22
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
23
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_fw_primal_copy(Tensor self, int level) -> Tensor")
24
+ static at::Tensor call(const at::Tensor & self, int64_t level);
25
+ static at::Tensor redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, int64_t level);
26
+ };
27
+
28
+ struct TORCH_API _fw_primal_copy_out {
29
+ using schema = at::Tensor & (const at::Tensor &, int64_t, at::Tensor &);
30
+ using ptr_schema = schema*;
31
+ // See Note [static constexpr char* members for windows NVCC]
32
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_fw_primal_copy")
33
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "out")
34
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_fw_primal_copy.out(Tensor self, int level, *, Tensor(a!) out) -> Tensor(a!)")
35
+ static at::Tensor & call(const at::Tensor & self, int64_t level, at::Tensor & out);
36
+ static at::Tensor & redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, int64_t level, at::Tensor & out);
37
+ };
38
+
39
+ }} // namespace at::_ops
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_pad_circular_ops.h ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // @generated by torchgen/gen.py from Operator.h
4
+
5
+ #include <tuple>
6
+ #include <vector>
7
+
8
+ // Forward declarations of any types needed in the operator signatures.
9
+ // We can't directly include these classes because it will cause circular include dependencies.
10
+ // This file is included by TensorBody.h, which defines the Tensor class.
11
+ #include <ATen/core/ATen_fwd.h>
12
+
13
+ namespace at {
14
+ namespace _ops {
15
+
16
+
17
+ struct TORCH_API _pad_circular {
18
+ using schema = at::Tensor (const at::Tensor &, c10::SymIntArrayRef);
19
+ using ptr_schema = schema*;
20
+ // See Note [static constexpr char* members for windows NVCC]
21
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::_pad_circular")
22
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
23
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "_pad_circular(Tensor self, SymInt[] pad) -> Tensor")
24
+ static at::Tensor call(const at::Tensor & self, c10::SymIntArrayRef pad);
25
+ static at::Tensor redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, c10::SymIntArrayRef pad);
26
+ };
27
+
28
+ }} // namespace at::_ops
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_slow_conv2d_backward.h ADDED
@@ -0,0 +1,135 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // @generated by torchgen/gen.py from Function.h
4
+
5
+ #include <ATen/Context.h>
6
+ #include <ATen/DeviceGuard.h>
7
+ #include <ATen/TensorUtils.h>
8
+ #include <ATen/TracerMode.h>
9
+ #include <ATen/core/Generator.h>
10
+ #include <ATen/core/Reduction.h>
11
+ #include <ATen/core/Tensor.h>
12
+ #include <c10/core/Scalar.h>
13
+ #include <c10/core/Storage.h>
14
+ #include <c10/core/TensorOptions.h>
15
+ #include <c10/util/Deprecated.h>
16
+ #include <c10/util/Optional.h>
17
+
18
+
19
+
20
+ #include <ATen/ops/_slow_conv2d_backward_ops.h>
21
+
22
+ namespace at {
23
+
24
+
25
+ // aten::_slow_conv2d_backward.grad_input(Tensor grad_output, Tensor self, Tensor weight, SymInt[2] kernel_size, SymInt[2] stride, SymInt[2] padding, *, Tensor(a!) grad_input, Tensor(b!) grad_weight, Tensor(c!) grad_bias) -> (Tensor(a!), Tensor(b!), Tensor(c!))
26
+ inline ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_out(at::Tensor & grad_input, at::Tensor & grad_weight, at::Tensor & grad_bias, const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, at::IntArrayRef kernel_size, at::IntArrayRef stride, at::IntArrayRef padding) {
27
+ return at::_ops::_slow_conv2d_backward_grad_input::call(grad_output, self, weight, c10::fromIntArrayRefSlow(kernel_size), c10::fromIntArrayRefSlow(stride), c10::fromIntArrayRefSlow(padding), grad_input, grad_weight, grad_bias);
28
+ }
29
+ namespace symint {
30
+ template <typename T, typename = std::enable_if_t<std::is_same<T, int64_t>::value>>
31
+ ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_out(at::Tensor & grad_input, at::Tensor & grad_weight, at::Tensor & grad_bias, const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, at::IntArrayRef kernel_size, at::IntArrayRef stride, at::IntArrayRef padding) {
32
+ return at::_ops::_slow_conv2d_backward_grad_input::call(grad_output, self, weight, c10::fromIntArrayRefSlow(kernel_size), c10::fromIntArrayRefSlow(stride), c10::fromIntArrayRefSlow(padding), grad_input, grad_weight, grad_bias);
33
+ }
34
+ }
35
+
36
+ // aten::_slow_conv2d_backward.grad_input(Tensor grad_output, Tensor self, Tensor weight, SymInt[2] kernel_size, SymInt[2] stride, SymInt[2] padding, *, Tensor(a!) grad_input, Tensor(b!) grad_weight, Tensor(c!) grad_bias) -> (Tensor(a!), Tensor(b!), Tensor(c!))
37
+ inline ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_outf(const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, at::IntArrayRef kernel_size, at::IntArrayRef stride, at::IntArrayRef padding, at::Tensor & grad_input, at::Tensor & grad_weight, at::Tensor & grad_bias) {
38
+ return at::_ops::_slow_conv2d_backward_grad_input::call(grad_output, self, weight, c10::fromIntArrayRefSlow(kernel_size), c10::fromIntArrayRefSlow(stride), c10::fromIntArrayRefSlow(padding), grad_input, grad_weight, grad_bias);
39
+ }
40
+ namespace symint {
41
+ template <typename T, typename = std::enable_if_t<std::is_same<T, int64_t>::value>>
42
+ ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_outf(const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, at::IntArrayRef kernel_size, at::IntArrayRef stride, at::IntArrayRef padding, at::Tensor & grad_input, at::Tensor & grad_weight, at::Tensor & grad_bias) {
43
+ return at::_ops::_slow_conv2d_backward_grad_input::call(grad_output, self, weight, c10::fromIntArrayRefSlow(kernel_size), c10::fromIntArrayRefSlow(stride), c10::fromIntArrayRefSlow(padding), grad_input, grad_weight, grad_bias);
44
+ }
45
+ }
46
+
47
+ // aten::_slow_conv2d_backward.grad_input(Tensor grad_output, Tensor self, Tensor weight, SymInt[2] kernel_size, SymInt[2] stride, SymInt[2] padding, *, Tensor(a!) grad_input, Tensor(b!) grad_weight, Tensor(c!) grad_bias) -> (Tensor(a!), Tensor(b!), Tensor(c!))
48
+ inline ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_symint_out(at::Tensor & grad_input, at::Tensor & grad_weight, at::Tensor & grad_bias, const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding) {
49
+ return at::_ops::_slow_conv2d_backward_grad_input::call(grad_output, self, weight, kernel_size, stride, padding, grad_input, grad_weight, grad_bias);
50
+ }
51
+ namespace symint {
52
+ template <typename T, typename = std::enable_if_t<std::is_same<T, c10::SymInt>::value>>
53
+ ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_out(at::Tensor & grad_input, at::Tensor & grad_weight, at::Tensor & grad_bias, const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding) {
54
+ return at::_ops::_slow_conv2d_backward_grad_input::call(grad_output, self, weight, kernel_size, stride, padding, grad_input, grad_weight, grad_bias);
55
+ }
56
+ }
57
+
58
+ // aten::_slow_conv2d_backward.grad_input(Tensor grad_output, Tensor self, Tensor weight, SymInt[2] kernel_size, SymInt[2] stride, SymInt[2] padding, *, Tensor(a!) grad_input, Tensor(b!) grad_weight, Tensor(c!) grad_bias) -> (Tensor(a!), Tensor(b!), Tensor(c!))
59
+ inline ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_symint_outf(const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding, at::Tensor & grad_input, at::Tensor & grad_weight, at::Tensor & grad_bias) {
60
+ return at::_ops::_slow_conv2d_backward_grad_input::call(grad_output, self, weight, kernel_size, stride, padding, grad_input, grad_weight, grad_bias);
61
+ }
62
+ namespace symint {
63
+ template <typename T, typename = std::enable_if_t<std::is_same<T, c10::SymInt>::value>>
64
+ ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_outf(const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding, at::Tensor & grad_input, at::Tensor & grad_weight, at::Tensor & grad_bias) {
65
+ return at::_ops::_slow_conv2d_backward_grad_input::call(grad_output, self, weight, kernel_size, stride, padding, grad_input, grad_weight, grad_bias);
66
+ }
67
+ }
68
+
69
+ // aten::_slow_conv2d_backward.output_mask(Tensor grad_output, Tensor self, Tensor weight, SymInt[2] kernel_size, SymInt[2] stride, SymInt[2] padding, bool[3] output_mask) -> (Tensor grad_input, Tensor grad_weight, Tensor grad_bias)
70
+ inline ::std::tuple<at::Tensor,at::Tensor,at::Tensor> _slow_conv2d_backward(const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, at::IntArrayRef kernel_size, at::IntArrayRef stride, at::IntArrayRef padding, ::std::array<bool,3> output_mask) {
71
+ return at::_ops::_slow_conv2d_backward_output_mask::call(grad_output, self, weight, c10::fromIntArrayRefSlow(kernel_size), c10::fromIntArrayRefSlow(stride), c10::fromIntArrayRefSlow(padding), output_mask);
72
+ }
73
+ namespace symint {
74
+ template <typename T, typename = std::enable_if_t<std::is_same<T, int64_t>::value>>
75
+ ::std::tuple<at::Tensor,at::Tensor,at::Tensor> _slow_conv2d_backward(const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, at::IntArrayRef kernel_size, at::IntArrayRef stride, at::IntArrayRef padding, ::std::array<bool,3> output_mask) {
76
+ return at::_ops::_slow_conv2d_backward_output_mask::call(grad_output, self, weight, c10::fromIntArrayRefSlow(kernel_size), c10::fromIntArrayRefSlow(stride), c10::fromIntArrayRefSlow(padding), output_mask);
77
+ }
78
+ }
79
+
80
+ // aten::_slow_conv2d_backward.output_mask(Tensor grad_output, Tensor self, Tensor weight, SymInt[2] kernel_size, SymInt[2] stride, SymInt[2] padding, bool[3] output_mask) -> (Tensor grad_input, Tensor grad_weight, Tensor grad_bias)
81
+ inline ::std::tuple<at::Tensor,at::Tensor,at::Tensor> _slow_conv2d_backward_symint(const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding, ::std::array<bool,3> output_mask) {
82
+ return at::_ops::_slow_conv2d_backward_output_mask::call(grad_output, self, weight, kernel_size, stride, padding, output_mask);
83
+ }
84
+ namespace symint {
85
+ template <typename T, typename = std::enable_if_t<std::is_same<T, c10::SymInt>::value>>
86
+ ::std::tuple<at::Tensor,at::Tensor,at::Tensor> _slow_conv2d_backward(const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding, ::std::array<bool,3> output_mask) {
87
+ return at::_ops::_slow_conv2d_backward_output_mask::call(grad_output, self, weight, kernel_size, stride, padding, output_mask);
88
+ }
89
+ }
90
+
91
+ // aten::_slow_conv2d_backward.output_mask_out(Tensor grad_output, Tensor self, Tensor weight, SymInt[2] kernel_size, SymInt[2] stride, SymInt[2] padding, bool[3] output_mask, *, Tensor(a!) out0, Tensor(b!) out1, Tensor(c!) out2) -> (Tensor(a!), Tensor(b!), Tensor(c!))
92
+ inline ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_out(at::Tensor & out0, at::Tensor & out1, at::Tensor & out2, const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, at::IntArrayRef kernel_size, at::IntArrayRef stride, at::IntArrayRef padding, ::std::array<bool,3> output_mask) {
93
+ return at::_ops::_slow_conv2d_backward_output_mask_out::call(grad_output, self, weight, c10::fromIntArrayRefSlow(kernel_size), c10::fromIntArrayRefSlow(stride), c10::fromIntArrayRefSlow(padding), output_mask, out0, out1, out2);
94
+ }
95
+ namespace symint {
96
+ template <typename T, typename = std::enable_if_t<std::is_same<T, int64_t>::value>>
97
+ ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_out(at::Tensor & out0, at::Tensor & out1, at::Tensor & out2, const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, at::IntArrayRef kernel_size, at::IntArrayRef stride, at::IntArrayRef padding, ::std::array<bool,3> output_mask) {
98
+ return at::_ops::_slow_conv2d_backward_output_mask_out::call(grad_output, self, weight, c10::fromIntArrayRefSlow(kernel_size), c10::fromIntArrayRefSlow(stride), c10::fromIntArrayRefSlow(padding), output_mask, out0, out1, out2);
99
+ }
100
+ }
101
+
102
+ // aten::_slow_conv2d_backward.output_mask_out(Tensor grad_output, Tensor self, Tensor weight, SymInt[2] kernel_size, SymInt[2] stride, SymInt[2] padding, bool[3] output_mask, *, Tensor(a!) out0, Tensor(b!) out1, Tensor(c!) out2) -> (Tensor(a!), Tensor(b!), Tensor(c!))
103
+ inline ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_outf(const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, at::IntArrayRef kernel_size, at::IntArrayRef stride, at::IntArrayRef padding, ::std::array<bool,3> output_mask, at::Tensor & out0, at::Tensor & out1, at::Tensor & out2) {
104
+ return at::_ops::_slow_conv2d_backward_output_mask_out::call(grad_output, self, weight, c10::fromIntArrayRefSlow(kernel_size), c10::fromIntArrayRefSlow(stride), c10::fromIntArrayRefSlow(padding), output_mask, out0, out1, out2);
105
+ }
106
+ namespace symint {
107
+ template <typename T, typename = std::enable_if_t<std::is_same<T, int64_t>::value>>
108
+ ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_outf(const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, at::IntArrayRef kernel_size, at::IntArrayRef stride, at::IntArrayRef padding, ::std::array<bool,3> output_mask, at::Tensor & out0, at::Tensor & out1, at::Tensor & out2) {
109
+ return at::_ops::_slow_conv2d_backward_output_mask_out::call(grad_output, self, weight, c10::fromIntArrayRefSlow(kernel_size), c10::fromIntArrayRefSlow(stride), c10::fromIntArrayRefSlow(padding), output_mask, out0, out1, out2);
110
+ }
111
+ }
112
+
113
+ // aten::_slow_conv2d_backward.output_mask_out(Tensor grad_output, Tensor self, Tensor weight, SymInt[2] kernel_size, SymInt[2] stride, SymInt[2] padding, bool[3] output_mask, *, Tensor(a!) out0, Tensor(b!) out1, Tensor(c!) out2) -> (Tensor(a!), Tensor(b!), Tensor(c!))
114
+ inline ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_symint_out(at::Tensor & out0, at::Tensor & out1, at::Tensor & out2, const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding, ::std::array<bool,3> output_mask) {
115
+ return at::_ops::_slow_conv2d_backward_output_mask_out::call(grad_output, self, weight, kernel_size, stride, padding, output_mask, out0, out1, out2);
116
+ }
117
+ namespace symint {
118
+ template <typename T, typename = std::enable_if_t<std::is_same<T, c10::SymInt>::value>>
119
+ ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_out(at::Tensor & out0, at::Tensor & out1, at::Tensor & out2, const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding, ::std::array<bool,3> output_mask) {
120
+ return at::_ops::_slow_conv2d_backward_output_mask_out::call(grad_output, self, weight, kernel_size, stride, padding, output_mask, out0, out1, out2);
121
+ }
122
+ }
123
+
124
+ // aten::_slow_conv2d_backward.output_mask_out(Tensor grad_output, Tensor self, Tensor weight, SymInt[2] kernel_size, SymInt[2] stride, SymInt[2] padding, bool[3] output_mask, *, Tensor(a!) out0, Tensor(b!) out1, Tensor(c!) out2) -> (Tensor(a!), Tensor(b!), Tensor(c!))
125
+ inline ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_symint_outf(const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding, ::std::array<bool,3> output_mask, at::Tensor & out0, at::Tensor & out1, at::Tensor & out2) {
126
+ return at::_ops::_slow_conv2d_backward_output_mask_out::call(grad_output, self, weight, kernel_size, stride, padding, output_mask, out0, out1, out2);
127
+ }
128
+ namespace symint {
129
+ template <typename T, typename = std::enable_if_t<std::is_same<T, c10::SymInt>::value>>
130
+ ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> _slow_conv2d_backward_outf(const at::Tensor & grad_output, const at::Tensor & self, const at::Tensor & weight, c10::SymIntArrayRef kernel_size, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding, ::std::array<bool,3> output_mask, at::Tensor & out0, at::Tensor & out1, at::Tensor & out2) {
131
+ return at::_ops::_slow_conv2d_backward_output_mask_out::call(grad_output, self, weight, kernel_size, stride, padding, output_mask, out0, out1, out2);
132
+ }
133
+ }
134
+
135
+ }
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/_weight_norm_interface_backward_cuda_dispatch.h ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ // @generated by torchgen/gen.py from DispatchKeyFunction.h
3
+
4
+ // NB: The implementing C++ file is RegisterDispatchKey.cpp
5
+
6
+ // The only #includes we need are for custom classes that have defaults in the C++ API
7
+ #include <c10/core/MemoryFormat.h>
8
+ #include <c10/core/Scalar.h>
9
+ #include <ATen/core/Reduction.h>
10
+
11
+ // Forward declarations of any types needed in the operator signatures.
12
+ // We can't directly include these classes because it will cause circular include dependencies.
13
+ // This file is included by TensorBody.h, which defines the Tensor class.
14
+ #include <ATen/core/ATen_fwd.h>
15
+
16
+ namespace at {
17
+
18
+ namespace cuda {
19
+
20
+ TORCH_API ::std::tuple<at::Tensor,at::Tensor> _weight_norm_interface_backward(const at::Tensor & grad_w, const at::Tensor & saved_v, const at::Tensor & saved_g, const at::Tensor & saved_norms, int64_t dim);
21
+
22
+ } // namespace cuda
23
+ } // namespace at
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/bincount_ops.h ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // @generated by torchgen/gen.py from Operator.h
4
+
5
+ #include <tuple>
6
+ #include <vector>
7
+
8
+ // Forward declarations of any types needed in the operator signatures.
9
+ // We can't directly include these classes because it will cause circular include dependencies.
10
+ // This file is included by TensorBody.h, which defines the Tensor class.
11
+ #include <ATen/core/ATen_fwd.h>
12
+
13
+ namespace at {
14
+ namespace _ops {
15
+
16
+
17
+ struct TORCH_API bincount {
18
+ using schema = at::Tensor (const at::Tensor &, const ::std::optional<at::Tensor> &, int64_t);
19
+ using ptr_schema = schema*;
20
+ // See Note [static constexpr char* members for windows NVCC]
21
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::bincount")
22
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
23
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "bincount(Tensor self, Tensor? weights=None, int minlength=0) -> Tensor")
24
+ static at::Tensor call(const at::Tensor & self, const ::std::optional<at::Tensor> & weights, int64_t minlength);
25
+ static at::Tensor redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, const ::std::optional<at::Tensor> & weights, int64_t minlength);
26
+ };
27
+
28
+ struct TORCH_API bincount_out {
29
+ using schema = at::Tensor & (const at::Tensor &, const ::std::optional<at::Tensor> &, int64_t, at::Tensor &);
30
+ using ptr_schema = schema*;
31
+ // See Note [static constexpr char* members for windows NVCC]
32
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::bincount")
33
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "out")
34
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "bincount.out(Tensor self, Tensor? weights=None, int minlength=0, *, Tensor(a!) out) -> Tensor(a!)")
35
+ static at::Tensor & call(const at::Tensor & self, const ::std::optional<at::Tensor> & weights, int64_t minlength, at::Tensor & out);
36
+ static at::Tensor & redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, const ::std::optional<at::Tensor> & weights, int64_t minlength, at::Tensor & out);
37
+ };
38
+
39
+ }} // namespace at::_ops
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/convolution_backward_overrideable_native.h ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // @generated by torchgen/gen.py from NativeFunction.h
4
+
5
+ #include <c10/core/Scalar.h>
6
+ #include <c10/core/Storage.h>
7
+ #include <c10/core/TensorOptions.h>
8
+ #include <c10/util/Deprecated.h>
9
+ #include <c10/util/Optional.h>
10
+ #include <c10/core/QScheme.h>
11
+ #include <ATen/core/Reduction.h>
12
+ #include <ATen/core/Tensor.h>
13
+ #include <tuple>
14
+ #include <vector>
15
+
16
+
17
+ namespace at {
18
+ namespace native {
19
+ TORCH_API ::std::tuple<at::Tensor,at::Tensor,at::Tensor> convolution_backward_overrideable(const at::Tensor & grad_output, const at::Tensor & input, const at::Tensor & weight, at::IntArrayRef stride, at::IntArrayRef padding, at::IntArrayRef dilation, bool transposed, at::IntArrayRef output_padding, int64_t groups, ::std::array<bool,3> output_mask);
20
+ TORCH_API ::std::tuple<at::Tensor &,at::Tensor &,at::Tensor &> convolution_backward_overrideable_out_symint(const at::Tensor & grad_output, const at::Tensor & input, const at::Tensor & weight, c10::SymIntArrayRef stride, c10::SymIntArrayRef padding, c10::SymIntArrayRef dilation, bool transposed, c10::SymIntArrayRef output_padding, c10::SymInt groups, ::std::array<bool,3> output_mask, at::Tensor & out0, at::Tensor & out1, at::Tensor & out2);
21
+ } // namespace native
22
+ } // namespace at
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/detach_copy_compositeexplicitautogradnonfunctional_dispatch.h ADDED
@@ -0,0 +1,23 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ // @generated by torchgen/gen.py from DispatchKeyFunction.h
3
+
4
+ // NB: The implementing C++ file is RegisterDispatchKey.cpp
5
+
6
+ // The only #includes we need are for custom classes that have defaults in the C++ API
7
+ #include <c10/core/MemoryFormat.h>
8
+ #include <c10/core/Scalar.h>
9
+ #include <ATen/core/Reduction.h>
10
+
11
+ // Forward declarations of any types needed in the operator signatures.
12
+ // We can't directly include these classes because it will cause circular include dependencies.
13
+ // This file is included by TensorBody.h, which defines the Tensor class.
14
+ #include <ATen/core/ATen_fwd.h>
15
+
16
+ namespace at {
17
+
18
+ namespace compositeexplicitautogradnonfunctional {
19
+
20
+ TORCH_API at::Tensor detach_copy(const at::Tensor & self);
21
+
22
+ } // namespace compositeexplicitautogradnonfunctional
23
+ } // namespace at
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/embedding_bag_ops.h ADDED
@@ -0,0 +1,39 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // @generated by torchgen/gen.py from Operator.h
4
+
5
+ #include <tuple>
6
+ #include <vector>
7
+
8
+ // Forward declarations of any types needed in the operator signatures.
9
+ // We can't directly include these classes because it will cause circular include dependencies.
10
+ // This file is included by TensorBody.h, which defines the Tensor class.
11
+ #include <ATen/core/ATen_fwd.h>
12
+
13
+ namespace at {
14
+ namespace _ops {
15
+
16
+
17
+ struct TORCH_API embedding_bag {
18
+ using schema = ::std::tuple<at::Tensor,at::Tensor,at::Tensor,at::Tensor> (const at::Tensor &, const at::Tensor &, const at::Tensor &, bool, int64_t, bool, const ::std::optional<at::Tensor> &, bool);
19
+ using ptr_schema = schema*;
20
+ // See Note [static constexpr char* members for windows NVCC]
21
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::embedding_bag")
22
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
23
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "embedding_bag(Tensor weight, Tensor indices, Tensor offsets, bool scale_grad_by_freq=False, int mode=0, bool sparse=False, Tensor? per_sample_weights=None, bool include_last_offset=False) -> (Tensor, Tensor, Tensor, Tensor)")
24
+ static ::std::tuple<at::Tensor,at::Tensor,at::Tensor,at::Tensor> call(const at::Tensor & weight, const at::Tensor & indices, const at::Tensor & offsets, bool scale_grad_by_freq, int64_t mode, bool sparse, const ::std::optional<at::Tensor> & per_sample_weights, bool include_last_offset);
25
+ static ::std::tuple<at::Tensor,at::Tensor,at::Tensor,at::Tensor> redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & weight, const at::Tensor & indices, const at::Tensor & offsets, bool scale_grad_by_freq, int64_t mode, bool sparse, const ::std::optional<at::Tensor> & per_sample_weights, bool include_last_offset);
26
+ };
27
+
28
+ struct TORCH_API embedding_bag_padding_idx {
29
+ using schema = ::std::tuple<at::Tensor,at::Tensor,at::Tensor,at::Tensor> (const at::Tensor &, const at::Tensor &, const at::Tensor &, bool, int64_t, bool, const ::std::optional<at::Tensor> &, bool, ::std::optional<int64_t>);
30
+ using ptr_schema = schema*;
31
+ // See Note [static constexpr char* members for windows NVCC]
32
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::embedding_bag")
33
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "padding_idx")
34
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "embedding_bag.padding_idx(Tensor weight, Tensor indices, Tensor offsets, bool scale_grad_by_freq, int mode, bool sparse, Tensor? per_sample_weights, bool include_last_offset, int? padding_idx) -> (Tensor, Tensor, Tensor, Tensor)")
35
+ static ::std::tuple<at::Tensor,at::Tensor,at::Tensor,at::Tensor> call(const at::Tensor & weight, const at::Tensor & indices, const at::Tensor & offsets, bool scale_grad_by_freq, int64_t mode, bool sparse, const ::std::optional<at::Tensor> & per_sample_weights, bool include_last_offset, ::std::optional<int64_t> padding_idx);
36
+ static ::std::tuple<at::Tensor,at::Tensor,at::Tensor,at::Tensor> redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & weight, const at::Tensor & indices, const at::Tensor & offsets, bool scale_grad_by_freq, int64_t mode, bool sparse, const ::std::optional<at::Tensor> & per_sample_weights, bool include_last_offset, ::std::optional<int64_t> padding_idx);
37
+ };
38
+
39
+ }} // namespace at::_ops
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/fractional_max_pool2d_cpu_dispatch.h ADDED
@@ -0,0 +1,25 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+ // @generated by torchgen/gen.py from DispatchKeyFunction.h
3
+
4
+ // NB: The implementing C++ file is RegisterDispatchKey.cpp
5
+
6
+ // The only #includes we need are for custom classes that have defaults in the C++ API
7
+ #include <c10/core/MemoryFormat.h>
8
+ #include <c10/core/Scalar.h>
9
+ #include <ATen/core/Reduction.h>
10
+
11
+ // Forward declarations of any types needed in the operator signatures.
12
+ // We can't directly include these classes because it will cause circular include dependencies.
13
+ // This file is included by TensorBody.h, which defines the Tensor class.
14
+ #include <ATen/core/ATen_fwd.h>
15
+
16
+ namespace at {
17
+
18
+ namespace cpu {
19
+
20
+ TORCH_API ::std::tuple<at::Tensor,at::Tensor> fractional_max_pool2d(const at::Tensor & self, at::IntArrayRef kernel_size, at::IntArrayRef output_size, const at::Tensor & random_samples);
21
+ TORCH_API ::std::tuple<at::Tensor &,at::Tensor &> fractional_max_pool2d_out(at::Tensor & output, at::Tensor & indices, const at::Tensor & self, at::IntArrayRef kernel_size, at::IntArrayRef output_size, const at::Tensor & random_samples);
22
+ TORCH_API ::std::tuple<at::Tensor &,at::Tensor &> fractional_max_pool2d_outf(const at::Tensor & self, at::IntArrayRef kernel_size, at::IntArrayRef output_size, const at::Tensor & random_samples, at::Tensor & output, at::Tensor & indices);
23
+
24
+ } // namespace cpu
25
+ } // namespace at
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/hinge_embedding_loss_ops.h ADDED
@@ -0,0 +1,28 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // @generated by torchgen/gen.py from Operator.h
4
+
5
+ #include <tuple>
6
+ #include <vector>
7
+
8
+ // Forward declarations of any types needed in the operator signatures.
9
+ // We can't directly include these classes because it will cause circular include dependencies.
10
+ // This file is included by TensorBody.h, which defines the Tensor class.
11
+ #include <ATen/core/ATen_fwd.h>
12
+
13
+ namespace at {
14
+ namespace _ops {
15
+
16
+
17
+ struct TORCH_API hinge_embedding_loss {
18
+ using schema = at::Tensor (const at::Tensor &, const at::Tensor &, double, int64_t);
19
+ using ptr_schema = schema*;
20
+ // See Note [static constexpr char* members for windows NVCC]
21
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(name, "aten::hinge_embedding_loss")
22
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(overload_name, "")
23
+ STATIC_CONSTEXPR_STR_INL_EXCEPT_WIN_CUDA(schema_str, "hinge_embedding_loss(Tensor self, Tensor target, float margin=1.0, int reduction=Mean) -> Tensor")
24
+ static at::Tensor call(const at::Tensor & self, const at::Tensor & target, double margin, int64_t reduction);
25
+ static at::Tensor redispatch(c10::DispatchKeySet dispatchKeySet, const at::Tensor & self, const at::Tensor & target, double margin, int64_t reduction);
26
+ };
27
+
28
+ }} // namespace at::_ops
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/index_select_native.h ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // @generated by torchgen/gen.py from NativeFunction.h
4
+
5
+ #include <c10/core/Scalar.h>
6
+ #include <c10/core/Storage.h>
7
+ #include <c10/core/TensorOptions.h>
8
+ #include <c10/util/Deprecated.h>
9
+ #include <c10/util/Optional.h>
10
+ #include <c10/core/QScheme.h>
11
+ #include <ATen/core/Reduction.h>
12
+ #include <ATen/core/Tensor.h>
13
+ #include <tuple>
14
+ #include <vector>
15
+
16
+
17
+ namespace at {
18
+ namespace native {
19
+ TORCH_API at::Tensor index_select_cpu_(const at::Tensor & self, int64_t dim, const at::Tensor & index);
20
+ TORCH_API at::Tensor & index_select_out_cpu_(const at::Tensor & self, int64_t dim, const at::Tensor & index, at::Tensor & out);
21
+ TORCH_API at::Tensor index_select_cuda(const at::Tensor & self, int64_t dim, const at::Tensor & index);
22
+ TORCH_API at::Tensor & index_select_out_cuda(const at::Tensor & self, int64_t dim, const at::Tensor & index, at::Tensor & out);
23
+ TORCH_API at::Tensor index_select_sparse_cpu(const at::Tensor & self, int64_t dim, const at::Tensor & index);
24
+ TORCH_API at::Tensor index_select_sparse_cuda(const at::Tensor & self, int64_t dim, const at::Tensor & index);
25
+ TORCH_API at::Tensor index_select_quantized_cpu_(const at::Tensor & self, int64_t dim, const at::Tensor & index);
26
+ TORCH_API at::Tensor index_select_quantized_cuda(const at::Tensor & self, int64_t dim, const at::Tensor & index);
27
+ TORCH_API at::Tensor index_select(const at::Tensor & self, at::Dimname dim, const at::Tensor & index);
28
+ TORCH_API at::Tensor & index_select_out(const at::Tensor & self, at::Dimname dim, const at::Tensor & index, at::Tensor & out);
29
+ } // namespace native
30
+ } // namespace at
parrot/lib/python3.10/site-packages/torch/include/ATen/ops/leaky_relu_meta.h ADDED
@@ -0,0 +1,27 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #pragma once
2
+
3
+ // @generated by torchgen/gen.py from NativeMetaFunction.h
4
+
5
+ #include <c10/core/Scalar.h>
6
+ #include <c10/core/Storage.h>
7
+ #include <c10/core/TensorOptions.h>
8
+ #include <c10/util/Deprecated.h>
9
+ #include <c10/util/Optional.h>
10
+ #include <c10/core/QScheme.h>
11
+ #include <ATen/core/Reduction.h>
12
+ #include <ATen/TensorIterator.h>
13
+ #include <ATen/TensorMeta.h>
14
+ #include <tuple>
15
+ #include <vector>
16
+
17
+ namespace at {
18
+ namespace meta {
19
+
20
+ struct TORCH_API structured_leaky_relu : public TensorIteratorBase {
21
+
22
+
23
+ void meta(const at::Tensor & self, const at::Scalar & negative_slope);
24
+ };
25
+
26
+ } // namespace native
27
+ } // namespace at