diff options
author | Ross Barnowski <rossbar@berkeley.edu> | 2021-03-25 13:49:02 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-25 21:49:02 +0100 |
commit | ae7eb5319d89ae58049c3ca573299b80eb9c0b71 (patch) | |
tree | d7970623c752ba4610e0abedc9e0f88059436626 | |
parent | 5cee6a56a63cb25bc1e109727792ad6a91be93b3 (diff) | |
download | networkx-ae7eb5319d89ae58049c3ca573299b80eb9c0b71.tar.gz |
Remove instances of random.sample from sets (deprecated in Python 3.9) (#4602)
* Fix random.sample warnings in maxcut tests.
* Fix random.sample warnings in betweenness.
* Add sorted to sampling in triads + tests.
* Add sorted to random_uniform_k_out_graph gen.
* add sorted to random.sample in internet_as_graphs.
* No sorted on nodes of graphs in general.
* No sorted in random_uniform graph generator.
-rw-r--r-- | networkx/algorithms/approximation/tests/test_maxcut.py | 4 | ||||
-rw-r--r-- | networkx/algorithms/centrality/betweenness.py | 2 | ||||
-rw-r--r-- | networkx/algorithms/tests/test_triads.py | 2 | ||||
-rw-r--r-- | networkx/algorithms/triads.py | 2 | ||||
-rw-r--r-- | networkx/generators/directed.py | 2 | ||||
-rw-r--r-- | networkx/generators/internet_as_graphs.py | 2 |
6 files changed, 7 insertions, 7 deletions
diff --git a/networkx/algorithms/approximation/tests/test_maxcut.py b/networkx/algorithms/approximation/tests/test_maxcut.py index 897d2559..ec75b59f 100644 --- a/networkx/algorithms/approximation/tests/test_maxcut.py +++ b/networkx/algorithms/approximation/tests/test_maxcut.py @@ -39,7 +39,7 @@ def test_one_exchange_basic(): for (u, v, w) in G.edges(data=True): w["weight"] = random.randrange(-100, 100, 1) / 10 - initial_cut = set(random.sample(G.nodes(), k=5)) + initial_cut = set(random.sample(sorted(G.nodes()), k=5)) cut_size, (set1, set2) = maxcut.one_exchange( G, initial_cut, weight="weight", seed=5 ) @@ -71,7 +71,7 @@ def test_negative_weights(): for (u, v, w) in G.edges(data=True): w["weight"] = -1 * random.random() - initial_cut = set(random.sample(G.nodes(), k=5)) + initial_cut = set(random.sample(sorted(G.nodes()), k=5)) cut_size, (set1, set2) = maxcut.one_exchange(G, initial_cut, weight="weight") # make sure it is a valid cut diff --git a/networkx/algorithms/centrality/betweenness.py b/networkx/algorithms/centrality/betweenness.py index c78585cf..4e183877 100644 --- a/networkx/algorithms/centrality/betweenness.py +++ b/networkx/algorithms/centrality/betweenness.py @@ -122,7 +122,7 @@ def betweenness_centrality( if k is None: nodes = G else: - nodes = seed.sample(G.nodes(), k) + nodes = seed.sample(list(G.nodes()), k) for s in nodes: # single source shortest paths if weight is None: # use BFS diff --git a/networkx/algorithms/tests/test_triads.py b/networkx/algorithms/tests/test_triads.py index e0e45dfe..a57b11af 100644 --- a/networkx/algorithms/tests/test_triads.py +++ b/networkx/algorithms/tests/test_triads.py @@ -36,7 +36,7 @@ def test_is_triad(): G = nx.karate_club_graph() G = G.to_directed() for i in range(100): - nodes = sample(G.nodes(), 3) + nodes = sample(sorted(G.nodes()), 3) G2 = G.subgraph(nodes) assert nx.is_triad(G2) diff --git a/networkx/algorithms/triads.py b/networkx/algorithms/triads.py index 8269b3f7..f31b9bdf 100644 --- a/networkx/algorithms/triads.py +++ b/networkx/algorithms/triads.py @@ -395,7 +395,7 @@ def random_triad(G): G2 : subgraph A randomly selected triad (order-3 NetworkX DiGraph) """ - nodes = sample(G.nodes(), 3) + nodes = sample(list(G.nodes()), 3) G2 = G.subgraph(nodes) return G2 diff --git a/networkx/generators/directed.py b/networkx/generators/directed.py index 0e6009b5..0b8e94ab 100644 --- a/networkx/generators/directed.py +++ b/networkx/generators/directed.py @@ -367,7 +367,7 @@ def random_uniform_k_out_graph(n, k, self_loops=True, with_replacement=True, see def sample(v, nodes): if not self_loops: nodes = nodes - {v} - return seed.sample(nodes, k) + return seed.sample(list(nodes), k) G = nx.empty_graph(n, create_using) nodes = set(G) diff --git a/networkx/generators/internet_as_graphs.py b/networkx/generators/internet_as_graphs.py index 2c93ab0e..a2bb16e8 100644 --- a/networkx/generators/internet_as_graphs.py +++ b/networkx/generators/internet_as_graphs.py @@ -308,7 +308,7 @@ class AS_graph_generator: node_options.remove(j) if len(node_options) > 0: - j = self.seed.sample(node_options, 1)[0] + j = self.seed.sample(list(node_options), 1)[0] self.add_edge(cp, j, "peer") self.G.nodes[cp]["peers"] += 1 self.G.nodes[j]["peers"] += 1 |