comparison env/lib/python3.7/site-packages/networkx/testing/utils.py @ 0:26e78fe6e8c4 draft

"planemo upload commit c699937486c35866861690329de38ec1a5d9f783"
author shellac
date Sat, 02 May 2020 07:14:21 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:26e78fe6e8c4
1 __all__ = ['assert_nodes_equal', 'assert_edges_equal', 'assert_graphs_equal',
2 'almost_equal']
3
4
5 def almost_equal(x, y, places=7):
6 return round(abs(x - y), places) == 0
7
8
9 def assert_nodes_equal(nodes1, nodes2):
10 # Assumes iterables of nodes, or (node,datadict) tuples
11 nlist1 = list(nodes1)
12 nlist2 = list(nodes2)
13 try:
14 d1 = dict(nlist1)
15 d2 = dict(nlist2)
16 except (ValueError, TypeError):
17 d1 = dict.fromkeys(nlist1)
18 d2 = dict.fromkeys(nlist2)
19 assert d1 == d2
20
21
22 def assert_edges_equal(edges1, edges2):
23 # Assumes iterables with u,v nodes as
24 # edge tuples (u,v), or
25 # edge tuples with data dicts (u,v,d), or
26 # edge tuples with keys and data dicts (u,v,k, d)
27 from collections import defaultdict
28 d1 = defaultdict(dict)
29 d2 = defaultdict(dict)
30 c1 = 0
31 for c1, e in enumerate(edges1):
32 u, v = e[0], e[1]
33 data = [e[2:]]
34 if v in d1[u]:
35 data = d1[u][v] + data
36 d1[u][v] = data
37 d1[v][u] = data
38 c2 = 0
39 for c2, e in enumerate(edges2):
40 u, v = e[0], e[1]
41 data = [e[2:]]
42 if v in d2[u]:
43 data = d2[u][v] + data
44 d2[u][v] = data
45 d2[v][u] = data
46 assert c1 == c2
47 # can check one direction because lengths are the same.
48 for n, nbrdict in d1.items():
49 for nbr, datalist in nbrdict.items():
50 assert n in d2
51 assert nbr in d2[n]
52 d2datalist = d2[n][nbr]
53 for data in datalist:
54 assert datalist.count(data) == d2datalist.count(data)
55
56
57 def assert_graphs_equal(graph1, graph2):
58 assert graph1.adj == graph2.adj
59 assert graph1.nodes == graph2.nodes
60 assert graph1.graph == graph2.graph