summaryrefslogtreecommitdiff
path: root/networkx/generators
diff options
context:
space:
mode:
Diffstat (limited to 'networkx/generators')
-rw-r--r--networkx/generators/directed.py6
-rw-r--r--networkx/generators/geometric.py15
-rw-r--r--networkx/generators/tests/test_geometric.py54
-rw-r--r--networkx/generators/tests/test_random_graphs.py4
4 files changed, 33 insertions, 46 deletions
diff --git a/networkx/generators/directed.py b/networkx/generators/directed.py
index 9382e47b..d76e79ef 100644
--- a/networkx/generators/directed.py
+++ b/networkx/generators/directed.py
@@ -271,8 +271,8 @@ def scale_free_graph(
raise ValueError("delta_out must be >= 0.")
# pre-populate degree states
- vs = sum([count * [idx] for idx, count in G.out_degree()], [])
- ws = sum([count * [idx] for idx, count in G.in_degree()], [])
+ vs = sum((count * [idx] for idx, count in G.out_degree()), [])
+ ws = sum((count * [idx] for idx, count in G.in_degree()), [])
# pre-populate node state
node_list = list(G.nodes())
@@ -281,7 +281,7 @@ def scale_free_graph(
numeric_nodes = [n for n in node_list if isinstance(n, numbers.Number)]
if len(numeric_nodes) > 0:
# set cursor for new nodes appropriately
- cursor = max([int(n.real) for n in numeric_nodes]) + 1
+ cursor = max(int(n.real) for n in numeric_nodes) + 1
else:
# or start at zero
cursor = 0
diff --git a/networkx/generators/geometric.py b/networkx/generators/geometric.py
index d843a9df..9888a6ae 100644
--- a/networkx/generators/geometric.py
+++ b/networkx/generators/geometric.py
@@ -3,7 +3,6 @@
from bisect import bisect_left
from itertools import accumulate, combinations, product
-from math import sqrt
import math
import networkx as nx
@@ -26,8 +25,16 @@ def euclidean(x, y):
Each of ``x`` and ``y`` can be any iterable of numbers. The
iterables must be of the same length.
+ .. deprecated:: 2.7
"""
- return sqrt(sum((a - b) ** 2 for a, b in zip(x, y)))
+ import warnings
+
+ msg = (
+ "euclidean is deprecated and will be removed in 3.0."
+ "Use math.dist(x, y) instead."
+ )
+ warnings.warn(msg, DeprecationWarning, stacklevel=2)
+ return math.dist(x, y)
def geometric_edges(G, radius, p):
@@ -450,7 +457,7 @@ def geographical_threshold_graph(
pos = {v: [seed.random() for i in range(dim)] for v in nodes}
# If no distance metric is provided, use Euclidean distance.
if metric is None:
- metric = euclidean
+ metric = math.dist
nx.set_node_attributes(G, weight, "weight")
nx.set_node_attributes(G, pos, "pos")
@@ -571,7 +578,7 @@ def waxman_graph(
nx.set_node_attributes(G, pos, "pos")
# If no distance metric is provided, use Euclidean distance.
if metric is None:
- metric = euclidean
+ metric = math.dist
# If the maximum distance L is not specified (that is, we are in the
# Waxman-1 model), then find the maximum distance between any pair
# of nodes.
diff --git a/networkx/generators/tests/test_geometric.py b/networkx/generators/tests/test_geometric.py
index 87dadafe..ea83d6f1 100644
--- a/networkx/generators/tests/test_geometric.py
+++ b/networkx/generators/tests/test_geometric.py
@@ -1,10 +1,8 @@
from itertools import combinations
-from math import sqrt
+import math
import random
-
import networkx as nx
-from networkx.generators.geometric import euclidean
def l1dist(x, y):
@@ -30,15 +28,14 @@ class TestRandomGeometricGraph:
"""
# Use the Euclidean metric, the default according to the
# documentation.
- dist = euclidean
G = nx.random_geometric_graph(50, 0.25)
for u, v in combinations(G, 2):
# Adjacent vertices must be within the given distance.
if v in G[u]:
- assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
+ assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
# Nonadjacent vertices must be at greater distance.
else:
- assert not dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
+ assert not math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
def test_p(self):
"""Tests for providing an alternate distance metric to the
@@ -46,15 +43,14 @@ class TestRandomGeometricGraph:
"""
# Use the L1 metric.
- dist = l1dist
G = nx.random_geometric_graph(50, 0.25, p=1)
for u, v in combinations(G, 2):
# Adjacent vertices must be within the given distance.
if v in G[u]:
- assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
+ assert l1dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
# Nonadjacent vertices must be at greater distance.
else:
- assert not dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
+ assert not l1dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
def test_node_names(self):
"""Tests using values other than sequential numbers as node IDs."""
@@ -64,14 +60,13 @@ class TestRandomGeometricGraph:
G = nx.random_geometric_graph(nodes, 0.25)
assert len(G) == len(nodes)
- dist = euclidean
for u, v in combinations(G, 2):
# Adjacent vertices must be within the given distance.
if v in G[u]:
- assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
+ assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
# Nonadjacent vertices must be at greater distance.
else:
- assert not dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
+ assert not math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
class TestSoftRandomGeometricGraph:
@@ -93,14 +88,11 @@ class TestSoftRandomGeometricGraph:
"""
# Use the Euclidean metric, the default according to the
# documentation.
- def dist(x, y):
- return sqrt(sum((a - b) ** 2 for a, b in zip(x, y)))
-
G = nx.soft_random_geometric_graph(50, 0.25)
for u, v in combinations(G, 2):
# Adjacent vertices must be within the given distance.
if v in G[u]:
- assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
+ assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
def test_p(self):
"""Tests for providing an alternate distance metric to the
@@ -125,13 +117,10 @@ class TestSoftRandomGeometricGraph:
G = nx.soft_random_geometric_graph(nodes, 0.25)
assert len(G) == len(nodes)
- def dist(x, y):
- return sqrt(sum((a - b) ** 2 for a, b in zip(x, y)))
-
for u, v in combinations(G, 2):
# Adjacent vertices must be within the given distance.
if v in G[u]:
- assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
+ assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
def test_p_dist_default(self):
"""Tests default p_dict = 0.5 returns graph with edge count <= RGG with
@@ -192,15 +181,14 @@ class TestGeographicalThresholdGraph:
"""
# Use the Euclidean metric and alpha = -2
# the default according to the documentation.
- dist = euclidean
G = nx.geographical_threshold_graph(50, 10)
for u, v in combinations(G, 2):
# Adjacent vertices must exceed the threshold.
if v in G[u]:
- assert join(G, u, v, 10, -2, dist)
+ assert join(G, u, v, 10, -2, math.dist)
# Nonadjacent vertices must not exceed the threshold.
else:
- assert not join(G, u, v, 10, -2, dist)
+ assert not join(G, u, v, 10, -2, math.dist)
def test_metric(self):
"""Tests for providing an alternate distance metric to the
@@ -208,15 +196,14 @@ class TestGeographicalThresholdGraph:
"""
# Use the L1 metric.
- dist = l1dist
- G = nx.geographical_threshold_graph(50, 10, metric=dist)
+ G = nx.geographical_threshold_graph(50, 10, metric=l1dist)
for u, v in combinations(G, 2):
# Adjacent vertices must exceed the threshold.
if v in G[u]:
- assert join(G, u, v, 10, -2, dist)
+ assert join(G, u, v, 10, -2, l1dist)
# Nonadjacent vertices must not exceed the threshold.
else:
- assert not join(G, u, v, 10, -2, dist)
+ assert not join(G, u, v, 10, -2, l1dist)
def test_p_dist_zero(self):
"""Tests if p_dict = 0 returns disconencted graph with 0 edges"""
@@ -249,8 +236,7 @@ class TestWaxmanGraph:
"""
# Use the L1 metric.
- dist = l1dist
- G = nx.waxman_graph(50, 0.5, 0.1, metric=dist)
+ G = nx.waxman_graph(50, 0.5, 0.1, metric=l1dist)
assert len(G) == 50
@@ -288,14 +274,11 @@ class TestThresholdedRandomGeometricGraph:
"""
# Use the Euclidean metric, the default according to the
# documentation.
- def dist(x, y):
- return sqrt(sum((a - b) ** 2 for a, b in zip(x, y)))
-
G = nx.thresholded_random_geometric_graph(50, 0.25, 0.1)
for u, v in combinations(G, 2):
# Adjacent vertices must be within the given distance.
if v in G[u]:
- assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
+ assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
def test_p(self):
"""Tests for providing an alternate distance metric to the
@@ -320,13 +303,10 @@ class TestThresholdedRandomGeometricGraph:
G = nx.thresholded_random_geometric_graph(nodes, 0.25, 0.1)
assert len(G) == len(nodes)
- def dist(x, y):
- return sqrt(sum((a - b) ** 2 for a, b in zip(x, y)))
-
for u, v in combinations(G, 2):
# Adjacent vertices must be within the given distance.
if v in G[u]:
- assert dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
+ assert math.dist(G.nodes[u]["pos"], G.nodes[v]["pos"]) <= 0.25
def test_theta(self):
"""Tests that pairs of vertices adjacent if and only if their sum
diff --git a/networkx/generators/tests/test_random_graphs.py b/networkx/generators/tests/test_random_graphs.py
index 8d47b960..bcc8aa2b 100644
--- a/networkx/generators/tests/test_random_graphs.py
+++ b/networkx/generators/tests/test_random_graphs.py
@@ -50,7 +50,7 @@ def test_gnp_generators_for_p_close_to_1(generator):
"""If the edge probability `p` is close to 1, the resulting graph should have all edges."""
runs = 100
edges = sum(
- [generator(10, 0.99999, directed=True).number_of_edges() for _ in range(runs)]
+ generator(10, 0.99999, directed=True).number_of_edges() for _ in range(runs)
)
assert abs(edges / float(runs) - 90) <= runs * 2.0 / 100
@@ -180,7 +180,7 @@ class TestGeneratorsRandom:
return is_caterpillar(g.subgraph(non_leafs))
G = nx.random_lobster(10, 0.1, 0.5, seed)
- assert max([G.degree(n) for n in G.nodes()]) > 3
+ assert max(G.degree(n) for n in G.nodes()) > 3
assert is_lobster(G)
pytest.raises(nx.NetworkXError, nx.random_lobster, 10, 0.1, 1, seed)
pytest.raises(nx.NetworkXError, nx.random_lobster, 10, 1, 1, seed)