test_non_randomness.py 782 B

12345678910111213141516171819202122232425262728293031323334353637
  1. import pytest
  2. import networkx as nx
  3. np = pytest.importorskip("numpy")
  4. @pytest.mark.parametrize(
  5. "k, weight, expected",
  6. [
  7. (None, None, 7.21), # infers 3 communities
  8. (2, None, 11.7),
  9. (None, "weight", 25.45),
  10. (2, "weight", 38.8),
  11. ],
  12. )
  13. def test_non_randomness(k, weight, expected):
  14. G = nx.karate_club_graph()
  15. np.testing.assert_almost_equal(
  16. nx.non_randomness(G, k, weight)[0], expected, decimal=2
  17. )
  18. def test_non_connected():
  19. G = nx.Graph()
  20. G.add_edge(1, 2)
  21. G.add_node(3)
  22. with pytest.raises(nx.NetworkXException):
  23. nx.non_randomness(G)
  24. def test_self_loops():
  25. G = nx.Graph()
  26. G.add_edge(1, 2)
  27. G.add_edge(1, 1)
  28. with pytest.raises(nx.NetworkXError):
  29. nx.non_randomness(G)