Mercurial > repos > shellac > sam_consensus_v3
comparison env/lib/python3.9/site-packages/networkx/algorithms/tests/test_triads.py @ 0:4f3585e2f14b draft default tip
"planemo upload commit 60cee0fc7c0cda8592644e1aad72851dec82c959"
| author | shellac |
|---|---|
| date | Mon, 22 Mar 2021 18:12:50 +0000 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| -1:000000000000 | 0:4f3585e2f14b |
|---|---|
| 1 """Unit tests for the :mod:`networkx.algorithms.triads` module.""" | |
| 2 | |
| 3 import networkx as nx | |
| 4 from collections import defaultdict | |
| 5 from random import sample | |
| 6 | |
| 7 | |
| 8 def test_triadic_census(): | |
| 9 """Tests the triadic_census function.""" | |
| 10 G = nx.DiGraph() | |
| 11 G.add_edges_from(["01", "02", "03", "04", "05", "12", "16", "51", "56", "65"]) | |
| 12 expected = { | |
| 13 "030T": 2, | |
| 14 "120C": 1, | |
| 15 "210": 0, | |
| 16 "120U": 0, | |
| 17 "012": 9, | |
| 18 "102": 3, | |
| 19 "021U": 0, | |
| 20 "111U": 0, | |
| 21 "003": 8, | |
| 22 "030C": 0, | |
| 23 "021D": 9, | |
| 24 "201": 0, | |
| 25 "111D": 1, | |
| 26 "300": 0, | |
| 27 "120D": 0, | |
| 28 "021C": 2, | |
| 29 } | |
| 30 actual = nx.triadic_census(G) | |
| 31 assert expected == actual | |
| 32 | |
| 33 | |
| 34 def test_is_triad(): | |
| 35 """Tests the is_triad function""" | |
| 36 G = nx.karate_club_graph() | |
| 37 G = G.to_directed() | |
| 38 for i in range(100): | |
| 39 nodes = sample(G.nodes(), 3) | |
| 40 G2 = G.subgraph(nodes) | |
| 41 assert nx.is_triad(G2) | |
| 42 | |
| 43 | |
| 44 def test_all_triplets(): | |
| 45 """Tests the all_triplets function.""" | |
| 46 G = nx.DiGraph() | |
| 47 G.add_edges_from(["01", "02", "03", "04", "05", "12", "16", "51", "56", "65"]) | |
| 48 expected = [ | |
| 49 f"{i},{j},{k}" | |
| 50 for i in range(7) | |
| 51 for j in range(i + 1, 7) | |
| 52 for k in range(j + 1, 7) | |
| 53 ] | |
| 54 expected = [set(x.split(",")) for x in expected] | |
| 55 actual = list(set(x) for x in nx.all_triplets(G)) | |
| 56 assert all([any([s1 == s2 for s1 in expected]) for s2 in actual]) | |
| 57 | |
| 58 | |
| 59 def test_all_triads(): | |
| 60 """Tests the all_triplets function.""" | |
| 61 G = nx.DiGraph() | |
| 62 G.add_edges_from(["01", "02", "03", "04", "05", "12", "16", "51", "56", "65"]) | |
| 63 expected = [ | |
| 64 f"{i},{j},{k}" | |
| 65 for i in range(7) | |
| 66 for j in range(i + 1, 7) | |
| 67 for k in range(j + 1, 7) | |
| 68 ] | |
| 69 expected = [G.subgraph(x.split(",")) for x in expected] | |
| 70 actual = list(nx.all_triads(G)) | |
| 71 assert all(any([nx.is_isomorphic(G1, G2) for G1 in expected]) for G2 in actual) | |
| 72 | |
| 73 | |
| 74 def test_triad_type(): | |
| 75 """Tests the triad_type function.""" | |
| 76 # 0 edges (1 type) | |
| 77 G = nx.DiGraph({0: [], 1: [], 2: []}) | |
| 78 assert nx.triad_type(G) == "003" | |
| 79 # 1 edge (1 type) | |
| 80 G = nx.DiGraph({0: [1], 1: [], 2: []}) | |
| 81 assert nx.triad_type(G) == "012" | |
| 82 # 2 edges (4 types) | |
| 83 G = nx.DiGraph([(0, 1), (0, 2)]) | |
| 84 assert nx.triad_type(G) == "021D" | |
| 85 G = nx.DiGraph({0: [1], 1: [0], 2: []}) | |
| 86 assert nx.triad_type(G) == "102" | |
| 87 G = nx.DiGraph([(0, 1), (2, 1)]) | |
| 88 assert nx.triad_type(G) == "021U" | |
| 89 G = nx.DiGraph([(0, 1), (1, 2)]) | |
| 90 assert nx.triad_type(G) == "021C" | |
| 91 # 3 edges (4 types) | |
| 92 G = nx.DiGraph([(0, 1), (1, 0), (2, 1)]) | |
| 93 assert nx.triad_type(G) == "111D" | |
| 94 G = nx.DiGraph([(0, 1), (1, 0), (1, 2)]) | |
| 95 assert nx.triad_type(G) == "111U" | |
| 96 G = nx.DiGraph([(0, 1), (1, 2), (0, 2)]) | |
| 97 assert nx.triad_type(G) == "030T" | |
| 98 G = nx.DiGraph([(0, 1), (1, 2), (2, 0)]) | |
| 99 assert nx.triad_type(G) == "030C" | |
| 100 # 4 edges (4 types) | |
| 101 G = nx.DiGraph([(0, 1), (1, 0), (2, 0), (0, 2)]) | |
| 102 assert nx.triad_type(G) == "201" | |
| 103 G = nx.DiGraph([(0, 1), (1, 0), (2, 0), (2, 1)]) | |
| 104 assert nx.triad_type(G) == "120D" | |
| 105 G = nx.DiGraph([(0, 1), (1, 0), (0, 2), (1, 2)]) | |
| 106 assert nx.triad_type(G) == "120U" | |
| 107 G = nx.DiGraph([(0, 1), (1, 0), (0, 2), (2, 1)]) | |
| 108 assert nx.triad_type(G) == "120C" | |
| 109 # 5 edges (1 type) | |
| 110 G = nx.DiGraph([(0, 1), (1, 0), (2, 1), (1, 2), (0, 2)]) | |
| 111 assert nx.triad_type(G) == "210" | |
| 112 # 6 edges (1 type) | |
| 113 G = nx.DiGraph([(0, 1), (1, 0), (1, 2), (2, 1), (0, 2), (2, 0)]) | |
| 114 assert nx.triad_type(G) == "300" | |
| 115 | |
| 116 | |
| 117 def test_triads_by_type(): | |
| 118 """Tests the all_triplets function.""" | |
| 119 G = nx.DiGraph() | |
| 120 G.add_edges_from(["01", "02", "03", "04", "05", "12", "16", "51", "56", "65"]) | |
| 121 all_triads = nx.all_triads(G) | |
| 122 expected = defaultdict(list) | |
| 123 for triad in all_triads: | |
| 124 name = nx.triad_type(triad) | |
| 125 expected[name].append(triad) | |
| 126 actual = nx.triads_by_type(G) | |
| 127 assert set(actual.keys()) == set(expected.keys()) | |
| 128 for tri_type, actual_Gs in actual.items(): | |
| 129 expected_Gs = expected[tri_type] | |
| 130 for a in actual_Gs: | |
| 131 assert any(nx.is_isomorphic(a, e) for e in expected_Gs) | |
| 132 | |
| 133 | |
| 134 def test_random_triad(): | |
| 135 """Tests the random_triad function""" | |
| 136 G = nx.karate_club_graph() | |
| 137 G = G.to_directed() | |
| 138 for i in range(100): | |
| 139 assert nx.is_triad(nx.random_triad(G)) |
