Skip to content

Commit

Permalink
Added few tests for /generators/duplication.py and /generators/geomet… (
Browse files Browse the repository at this point in the history
networkx#6976)

* Added few tests for /generators/duplication.py and /generators/geometric.py. Corrected an exception statement in geometric.py (r must be >=0 instead of r must be >= 1)

* Added kargs to make it more readable. And added few more tests.

* STY: Minor signature cleanup.

* Update test for r in range [0, 1].

* Add test for general edge scaling with increasing r.

---------

Co-authored-by: Ross Barnowski <rossbar@berkeley.edu>
  • Loading branch information
2 people authored and cvanelteren committed Apr 22, 2024
1 parent f959810 commit 5f5e349
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
2 changes: 1 addition & 1 deletion networkx/generators/geometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,7 @@ def navigable_small_world_graph(n, p=1, q=1, r=2, dim=2, seed=None):
if q < 0:
raise nx.NetworkXException("q must be >= 0")
if r < 0:
raise nx.NetworkXException("r must be >= 1")
raise nx.NetworkXException("r must be >= 0")

G = nx.DiGraph()
nodes = list(product(range(n), repeat=dim))
Expand Down
19 changes: 15 additions & 4 deletions networkx/generators/tests/test_duplication.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,29 @@ class TestDuplicationDivergenceGraph:
"""

def test_final_size(self):
G = duplication_divergence_graph(3, 1)
G = duplication_divergence_graph(3, p=1)
assert len(G) == 3
G = duplication_divergence_graph(3, 1, seed=42)
G = duplication_divergence_graph(3, p=1, seed=42)
assert len(G) == 3

def test_probability_too_large(self):
with pytest.raises(NetworkXError):
duplication_divergence_graph(3, 2)
duplication_divergence_graph(3, p=2)

def test_probability_too_small(self):
with pytest.raises(NetworkXError):
duplication_divergence_graph(3, -1)
duplication_divergence_graph(3, p=-1)

def test_non_extreme_probability_value(self):
G = duplication_divergence_graph(6, p=0.3, seed=42)
assert len(G) == 6
assert list(G.degree()) == [(0, 2), (1, 3), (2, 2), (3, 3), (4, 1), (5, 1)]

def test_minimum_desired_nodes(self):
with pytest.raises(
NetworkXError, match=".*n must be greater than or equal to 2"
):
duplication_divergence_graph(1, p=1)


class TestPartialDuplicationGraph:
Expand Down
33 changes: 33 additions & 0 deletions networkx/generators/tests/test_geometric.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,39 @@ def test_navigable_small_world(self):
gg = nx.grid_graph([5]).to_directed()
assert nx.is_isomorphic(G, gg)

def test_invalid_diameter_value(self):
with pytest.raises(nx.NetworkXException, match=".*p must be >= 1"):
nx.navigable_small_world_graph(5, p=0, q=0, dim=1)

def test_invalid_long_range_connections_value(self):
with pytest.raises(nx.NetworkXException, match=".*q must be >= 0"):
nx.navigable_small_world_graph(5, p=1, q=-1, dim=1)

def test_invalid_exponent_for_decaying_probability_value(self):
with pytest.raises(nx.NetworkXException, match=".*r must be >= 0"):
nx.navigable_small_world_graph(5, p=1, q=0, r=-1, dim=1)

def test_r_between_0_and_1(self):
"""Smoke test for radius in range [0, 1]"""
# q=0 means no long-range connections
G = nx.navigable_small_world_graph(3, p=1, q=0, r=0.5, dim=2, seed=42)
expected = nx.grid_2d_graph(3, 3, create_using=nx.DiGraph)
assert nx.utils.graphs_equal(G, expected)

@pytest.mark.parametrize("seed", range(2478, 2578, 10))
def test_r_general_scaling(self, seed):
"""The probability of adding a long-range edge scales with `1 / dist**r`,
so a navigable_small_world graph created with r < 1 should generally
result in more edges than a navigable_small_world graph with r >= 1
(for 0 < q << n).
N.B. this is probabilistic, so this test may not hold for all seeds."""
G1 = nx.navigable_small_world_graph(7, q=3, r=0.5, seed=seed)
G2 = nx.navigable_small_world_graph(7, q=3, r=1, seed=seed)
G3 = nx.navigable_small_world_graph(7, q=3, r=2, seed=seed)
assert G1.number_of_edges() > G2.number_of_edges()
assert G2.number_of_edges() > G3.number_of_edges()


class TestThresholdedRandomGeometricGraph:
"""Unit tests for :func:`~networkx.thresholded_random_geometric_graph`"""
Expand Down

0 comments on commit 5f5e349

Please sign in to comment.