Spaces:
Running
Running
File size: 8,708 Bytes
b200bda |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 |
import pytest
np = pytest.importorskip("numpy")
pytest.importorskip("scipy")
import networkx as nx
from networkx.exception import NetworkXError
from networkx.generators.degree_seq import havel_hakimi_graph
def test_incidence_matrix_simple():
deg = [3, 2, 2, 1, 0]
G = havel_hakimi_graph(deg)
deg = [(1, 0), (1, 0), (1, 0), (2, 0), (1, 0), (2, 1), (0, 1), (0, 1)]
MG = nx.random_clustered_graph(deg, seed=42)
I = nx.incidence_matrix(G, dtype=int).todense()
# fmt: off
expected = np.array(
[[1, 1, 1, 0],
[0, 1, 0, 1],
[1, 0, 0, 1],
[0, 0, 1, 0],
[0, 0, 0, 0]]
)
# fmt: on
np.testing.assert_equal(I, expected)
I = nx.incidence_matrix(MG, dtype=int).todense()
# fmt: off
expected = np.array(
[[1, 0, 0, 0, 0, 0, 0],
[1, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0],
[0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 1, 1, 0],
[0, 0, 0, 0, 0, 1, 1],
[0, 0, 0, 0, 1, 0, 1]]
)
# fmt: on
np.testing.assert_equal(I, expected)
with pytest.raises(NetworkXError):
nx.incidence_matrix(G, nodelist=[0, 1])
class TestGraphMatrix:
@classmethod
def setup_class(cls):
deg = [3, 2, 2, 1, 0]
cls.G = havel_hakimi_graph(deg)
# fmt: off
cls.OI = np.array(
[[-1, -1, -1, 0],
[1, 0, 0, -1],
[0, 1, 0, 1],
[0, 0, 1, 0],
[0, 0, 0, 0]]
)
cls.A = np.array(
[[0, 1, 1, 1, 0],
[1, 0, 1, 0, 0],
[1, 1, 0, 0, 0],
[1, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]
)
# fmt: on
cls.WG = havel_hakimi_graph(deg)
cls.WG.add_edges_from(
(u, v, {"weight": 0.5, "other": 0.3}) for (u, v) in cls.G.edges()
)
# fmt: off
cls.WA = np.array(
[[0, 0.5, 0.5, 0.5, 0],
[0.5, 0, 0.5, 0, 0],
[0.5, 0.5, 0, 0, 0],
[0.5, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]
)
# fmt: on
cls.MG = nx.MultiGraph(cls.G)
cls.MG2 = cls.MG.copy()
cls.MG2.add_edge(0, 1)
# fmt: off
cls.MG2A = np.array(
[[0, 2, 1, 1, 0],
[2, 0, 1, 0, 0],
[1, 1, 0, 0, 0],
[1, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]
)
cls.MGOI = np.array(
[[-1, -1, -1, -1, 0],
[1, 1, 0, 0, -1],
[0, 0, 1, 0, 1],
[0, 0, 0, 1, 0],
[0, 0, 0, 0, 0]]
)
# fmt: on
cls.no_edges_G = nx.Graph([(1, 2), (3, 2, {"weight": 8})])
cls.no_edges_A = np.array([[0, 0], [0, 0]])
def test_incidence_matrix(self):
"Conversion to incidence matrix"
I = nx.incidence_matrix(
self.G,
nodelist=sorted(self.G),
edgelist=sorted(self.G.edges()),
oriented=True,
dtype=int,
).todense()
np.testing.assert_equal(I, self.OI)
I = nx.incidence_matrix(
self.G,
nodelist=sorted(self.G),
edgelist=sorted(self.G.edges()),
oriented=False,
dtype=int,
).todense()
np.testing.assert_equal(I, np.abs(self.OI))
I = nx.incidence_matrix(
self.MG,
nodelist=sorted(self.MG),
edgelist=sorted(self.MG.edges()),
oriented=True,
dtype=int,
).todense()
np.testing.assert_equal(I, self.OI)
I = nx.incidence_matrix(
self.MG,
nodelist=sorted(self.MG),
edgelist=sorted(self.MG.edges()),
oriented=False,
dtype=int,
).todense()
np.testing.assert_equal(I, np.abs(self.OI))
I = nx.incidence_matrix(
self.MG2,
nodelist=sorted(self.MG2),
edgelist=sorted(self.MG2.edges()),
oriented=True,
dtype=int,
).todense()
np.testing.assert_equal(I, self.MGOI)
I = nx.incidence_matrix(
self.MG2,
nodelist=sorted(self.MG),
edgelist=sorted(self.MG2.edges()),
oriented=False,
dtype=int,
).todense()
np.testing.assert_equal(I, np.abs(self.MGOI))
I = nx.incidence_matrix(self.G, dtype=np.uint8)
assert I.dtype == np.uint8
def test_weighted_incidence_matrix(self):
I = nx.incidence_matrix(
self.WG,
nodelist=sorted(self.WG),
edgelist=sorted(self.WG.edges()),
oriented=True,
dtype=int,
).todense()
np.testing.assert_equal(I, self.OI)
I = nx.incidence_matrix(
self.WG,
nodelist=sorted(self.WG),
edgelist=sorted(self.WG.edges()),
oriented=False,
dtype=int,
).todense()
np.testing.assert_equal(I, np.abs(self.OI))
# np.testing.assert_equal(nx.incidence_matrix(self.WG,oriented=True,
# weight='weight').todense(),0.5*self.OI)
# np.testing.assert_equal(nx.incidence_matrix(self.WG,weight='weight').todense(),
# np.abs(0.5*self.OI))
# np.testing.assert_equal(nx.incidence_matrix(self.WG,oriented=True,weight='other').todense(),
# 0.3*self.OI)
I = nx.incidence_matrix(
self.WG,
nodelist=sorted(self.WG),
edgelist=sorted(self.WG.edges()),
oriented=True,
weight="weight",
).todense()
np.testing.assert_equal(I, 0.5 * self.OI)
I = nx.incidence_matrix(
self.WG,
nodelist=sorted(self.WG),
edgelist=sorted(self.WG.edges()),
oriented=False,
weight="weight",
).todense()
np.testing.assert_equal(I, np.abs(0.5 * self.OI))
I = nx.incidence_matrix(
self.WG,
nodelist=sorted(self.WG),
edgelist=sorted(self.WG.edges()),
oriented=True,
weight="other",
).todense()
np.testing.assert_equal(I, 0.3 * self.OI)
# WMG=nx.MultiGraph(self.WG)
# WMG.add_edge(0,1,weight=0.5,other=0.3)
# np.testing.assert_equal(nx.incidence_matrix(WMG,weight='weight').todense(),
# np.abs(0.5*self.MGOI))
# np.testing.assert_equal(nx.incidence_matrix(WMG,weight='weight',oriented=True).todense(),
# 0.5*self.MGOI)
# np.testing.assert_equal(nx.incidence_matrix(WMG,weight='other',oriented=True).todense(),
# 0.3*self.MGOI)
WMG = nx.MultiGraph(self.WG)
WMG.add_edge(0, 1, weight=0.5, other=0.3)
I = nx.incidence_matrix(
WMG,
nodelist=sorted(WMG),
edgelist=sorted(WMG.edges(keys=True)),
oriented=True,
weight="weight",
).todense()
np.testing.assert_equal(I, 0.5 * self.MGOI)
I = nx.incidence_matrix(
WMG,
nodelist=sorted(WMG),
edgelist=sorted(WMG.edges(keys=True)),
oriented=False,
weight="weight",
).todense()
np.testing.assert_equal(I, np.abs(0.5 * self.MGOI))
I = nx.incidence_matrix(
WMG,
nodelist=sorted(WMG),
edgelist=sorted(WMG.edges(keys=True)),
oriented=True,
weight="other",
).todense()
np.testing.assert_equal(I, 0.3 * self.MGOI)
def test_adjacency_matrix(self):
"Conversion to adjacency matrix"
np.testing.assert_equal(nx.adjacency_matrix(self.G).todense(), self.A)
np.testing.assert_equal(nx.adjacency_matrix(self.MG).todense(), self.A)
np.testing.assert_equal(nx.adjacency_matrix(self.MG2).todense(), self.MG2A)
np.testing.assert_equal(
nx.adjacency_matrix(self.G, nodelist=[0, 1]).todense(), self.A[:2, :2]
)
np.testing.assert_equal(nx.adjacency_matrix(self.WG).todense(), self.WA)
np.testing.assert_equal(
nx.adjacency_matrix(self.WG, weight=None).todense(), self.A
)
np.testing.assert_equal(
nx.adjacency_matrix(self.MG2, weight=None).todense(), self.MG2A
)
np.testing.assert_equal(
nx.adjacency_matrix(self.WG, weight="other").todense(), 0.6 * self.WA
)
np.testing.assert_equal(
nx.adjacency_matrix(self.no_edges_G, nodelist=[1, 3]).todense(),
self.no_edges_A,
)
|