comparison env/lib/python3.9/site-packages/networkx/algorithms/assortativity/tests/test_correlation.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 import pytest
2
3 np = pytest.importorskip("numpy")
4 npt = pytest.importorskip("numpy.testing")
5 scipy = pytest.importorskip("scipy")
6
7
8 import networkx as nx
9 from .base_test import BaseTestAttributeMixing, BaseTestDegreeMixing
10 from networkx.algorithms.assortativity.correlation import attribute_ac
11
12
13 class TestDegreeMixingCorrelation(BaseTestDegreeMixing):
14 def test_degree_assortativity_undirected(self):
15 r = nx.degree_assortativity_coefficient(self.P4)
16 npt.assert_almost_equal(r, -1.0 / 2, decimal=4)
17
18 def test_degree_assortativity_directed(self):
19 r = nx.degree_assortativity_coefficient(self.D)
20 npt.assert_almost_equal(r, -0.57735, decimal=4)
21
22 def test_degree_assortativity_multigraph(self):
23 r = nx.degree_assortativity_coefficient(self.M)
24 npt.assert_almost_equal(r, -1.0 / 7.0, decimal=4)
25
26 def test_degree_pearson_assortativity_undirected(self):
27 r = nx.degree_pearson_correlation_coefficient(self.P4)
28 npt.assert_almost_equal(r, -1.0 / 2, decimal=4)
29
30 def test_degree_pearson_assortativity_directed(self):
31 r = nx.degree_pearson_correlation_coefficient(self.D)
32 npt.assert_almost_equal(r, -0.57735, decimal=4)
33
34 def test_degree_pearson_assortativity_multigraph(self):
35 r = nx.degree_pearson_correlation_coefficient(self.M)
36 npt.assert_almost_equal(r, -1.0 / 7.0, decimal=4)
37
38
39 class TestAttributeMixingCorrelation(BaseTestAttributeMixing):
40 def test_attribute_assortativity_undirected(self):
41 r = nx.attribute_assortativity_coefficient(self.G, "fish")
42 assert r == 6.0 / 22.0
43
44 def test_attribute_assortativity_directed(self):
45 r = nx.attribute_assortativity_coefficient(self.D, "fish")
46 assert r == 1.0 / 3.0
47
48 def test_attribute_assortativity_multigraph(self):
49 r = nx.attribute_assortativity_coefficient(self.M, "fish")
50 assert r == 1.0
51
52 def test_attribute_assortativity_coefficient(self):
53 # from "Mixing patterns in networks"
54 # fmt: off
55 a = np.array([[0.258, 0.016, 0.035, 0.013],
56 [0.012, 0.157, 0.058, 0.019],
57 [0.013, 0.023, 0.306, 0.035],
58 [0.005, 0.007, 0.024, 0.016]])
59 # fmt: on
60 r = attribute_ac(a)
61 npt.assert_almost_equal(r, 0.623, decimal=3)
62
63 def test_attribute_assortativity_coefficient2(self):
64 # fmt: off
65 a = np.array([[0.18, 0.02, 0.01, 0.03],
66 [0.02, 0.20, 0.03, 0.02],
67 [0.01, 0.03, 0.16, 0.01],
68 [0.03, 0.02, 0.01, 0.22]])
69 # fmt: on
70 r = attribute_ac(a)
71 npt.assert_almost_equal(r, 0.68, decimal=2)
72
73 def test_attribute_assortativity(self):
74 a = np.array([[50, 50, 0], [50, 50, 0], [0, 0, 2]])
75 r = attribute_ac(a)
76 npt.assert_almost_equal(r, 0.029, decimal=3)