diff options
Diffstat (limited to 'networkx/algorithms')
28 files changed, 74 insertions, 135 deletions
diff --git a/networkx/algorithms/assortativity/correlation.py b/networkx/algorithms/assortativity/correlation.py index 3036ff8b..39ac2fab 100644 --- a/networkx/algorithms/assortativity/correlation.py +++ b/networkx/algorithms/assortativity/correlation.py @@ -80,18 +80,18 @@ def degree_assortativity_coefficient(G, x="out", y="in", weight=None, nodes=None if G.is_directed(): indeg = ( - set([d for _, d in G.in_degree(nodes, weight=weight)]) + {d for _, d in G.in_degree(nodes, weight=weight)} if "in" in (x, y) else set() ) outdeg = ( - set([d for _, d in G.out_degree(nodes, weight=weight)]) + {d for _, d in G.out_degree(nodes, weight=weight)} if "out" in (x, y) else set() ) degrees = set.union(indeg, outdeg) else: - degrees = set([d for _, d in G.degree(nodes, weight=weight)]) + degrees = {d for _, d in G.degree(nodes, weight=weight)} mapping = {d: i for i, d, in enumerate(degrees)} M = degree_mixing_matrix(G, x=x, y=y, nodes=nodes, weight=weight, mapping=mapping) @@ -247,7 +247,7 @@ def numeric_assortativity_coefficient(G, attribute, nodes=None): """ if nodes is None: nodes = G.nodes - vals = set(G.nodes[n][attribute] for n in nodes) + vals = {G.nodes[n][attribute] for n in nodes} mapping = {d: i for i, d, in enumerate(vals)} M = attribute_mixing_matrix(G, attribute, nodes, mapping) return numeric_ac(M, mapping) diff --git a/networkx/algorithms/centrality/katz.py b/networkx/algorithms/centrality/katz.py index 87c2df07..33fcf096 100644 --- a/networkx/algorithms/centrality/katz.py +++ b/networkx/algorithms/centrality/katz.py @@ -176,7 +176,7 @@ def katz_centrality( x[n] = alpha * x[n] + b[n] # check convergence - err = sum([abs(x[n] - xlast[n]) for n in x]) + err = sum(abs(x[n] - xlast[n]) for n in x) if err < nnodes * tol: if normalized: # normalize vector diff --git a/networkx/algorithms/clique.py b/networkx/algorithms/clique.py index 803507b8..d636a842 100644 --- a/networkx/algorithms/clique.py +++ b/networkx/algorithms/clique.py @@ -485,11 +485,11 @@ def node_clique_number(G, nodes=None, cliques=None): if not isinstance(nodes, list): # check for a list v = nodes # assume it is a single value - d = max([len(c) for c in cliques if v in c]) + d = max(len(c) for c in cliques if v in c) else: d = {} for v in nodes: - d[v] = max([len(c) for c in cliques if v in c]) + d[v] = max(len(c) for c in cliques if v in c) return d # if nodes is None: # none, use entire graph @@ -554,7 +554,7 @@ def cliques_containing_node(G, nodes=None, cliques=None): return vcliques -class MaxWeightClique(object): +class MaxWeightClique: """A class for the maximum weight clique algorithm. This class is a helper for the `max_weight_clique` function. The class diff --git a/networkx/algorithms/coloring/equitable_coloring.py b/networkx/algorithms/coloring/equitable_coloring.py index f07cc541..1a2c99fe 100644 --- a/networkx/algorithms/coloring/equitable_coloring.py +++ b/networkx/algorithms/coloring/equitable_coloring.py @@ -447,7 +447,7 @@ def equitable_color(G, num_colors): # Basic graph statistics and sanity check. if len(G.nodes) > 0: - r_ = max([G.degree(node) for node in G.nodes]) + r_ = max(G.degree(node) for node in G.nodes) else: r_ = 0 diff --git a/networkx/algorithms/coloring/tests/test_coloring.py b/networkx/algorithms/coloring/tests/test_coloring.py index 5d2cb82f..274abff5 100644 --- a/networkx/algorithms/coloring/tests/test_coloring.py +++ b/networkx/algorithms/coloring/tests/test_coloring.py @@ -775,7 +775,7 @@ def check_state(L, N, H, F, C): def max_degree(G): """Get the maximum degree of any node in G.""" - return max([G.degree(node) for node in G.nodes]) if len(G.nodes) > 0 else 0 + return max(G.degree(node) for node in G.nodes) if len(G.nodes) > 0 else 0 def make_params_from_graph(G, F): diff --git a/networkx/algorithms/community/lukes.py b/networkx/algorithms/community/lukes.py index 29ade800..6fe1d0de 100644 --- a/networkx/algorithms/community/lukes.py +++ b/networkx/algorithms/community/lukes.py @@ -132,14 +132,14 @@ def lukes_partitioning(G, max_size: int, node_weight=None, edge_weight=None) -> @lru_cache(CLUSTER_EVAL_CACHE_SIZE) def _value_of_cluster(cluster: frozenset): valid_edges = [e for e in safe_G.edges if e[0] in cluster and e[1] in cluster] - return sum([safe_G.edges[e][edge_weight] for e in valid_edges]) + return sum(safe_G.edges[e][edge_weight] for e in valid_edges) def _value_of_partition(partition: list): - return sum([_value_of_cluster(frozenset(c)) for c in partition]) + return sum(_value_of_cluster(frozenset(c)) for c in partition) @lru_cache(CLUSTER_EVAL_CACHE_SIZE) def _weight_of_cluster(cluster: frozenset): - return sum([safe_G.nodes[n][node_weight] for n in cluster]) + return sum(safe_G.nodes[n][node_weight] for n in cluster) def _pivot(partition: list, node): ccx = [c for c in partition if node in c] diff --git a/networkx/algorithms/community/modularity_max.py b/networkx/algorithms/community/modularity_max.py index 10d42e7f..19810531 100644 --- a/networkx/algorithms/community/modularity_max.py +++ b/networkx/algorithms/community/modularity_max.py @@ -282,7 +282,7 @@ def naive_greedy_modularity_communities(G, resolution=1): modularity """ # First create one community for each node - communities = list([frozenset([u]) for u in G.nodes()]) + communities = list(frozenset([u]) for u in G.nodes()) # Track merges merges = [] # Greedily merge communities until no improvement is possible diff --git a/networkx/algorithms/components/tests/test_biconnected.py b/networkx/algorithms/components/tests/test_biconnected.py index 80faea0e..6c7574dc 100644 --- a/networkx/algorithms/components/tests/test_biconnected.py +++ b/networkx/algorithms/components/tests/test_biconnected.py @@ -4,8 +4,8 @@ from networkx import NetworkXNotImplemented def assert_components_edges_equal(x, y): - sx = {frozenset([frozenset(e) for e in c]) for c in x} - sy = {frozenset([frozenset(e) for e in c]) for c in y} + sx = {frozenset(frozenset(e) for e in c) for c in x} + sy = {frozenset(frozenset(e) for e in c) for c in y} assert sx == sy diff --git a/networkx/algorithms/connectivity/tests/test_edge_augmentation.py b/networkx/algorithms/connectivity/tests/test_edge_augmentation.py index 600ab13e..a1bbf5a5 100644 --- a/networkx/algorithms/connectivity/tests/test_edge_augmentation.py +++ b/networkx/algorithms/connectivity/tests/test_edge_augmentation.py @@ -351,7 +351,7 @@ def _augment_and_check( # Find the weight of the augmentation num_edges = len(aug_edges) if avail is not None: - total_weight = sum([avail_dict[e] for e in aug_edges]) + total_weight = sum(avail_dict[e] for e in aug_edges) else: total_weight = num_edges diff --git a/networkx/algorithms/dag.py b/networkx/algorithms/dag.py index e6a408e0..5a4881ff 100644 --- a/networkx/algorithms/dag.py +++ b/networkx/algorithms/dag.py @@ -681,15 +681,13 @@ def transitive_closure(G, reflexive=False): for v in G: if reflexive is None: - TC.add_edges_from(((v, u) for u in nx.descendants(G, v) if u not in TC[v])) + TC.add_edges_from((v, u) for u in nx.descendants(G, v) if u not in TC[v]) elif reflexive is True: TC.add_edges_from( - ((v, u) for u in nx.descendants(G, v) | {v} if u not in TC[v]) + (v, u) for u in nx.descendants(G, v) | {v} if u not in TC[v] ) elif reflexive is False: - TC.add_edges_from( - ((v, e[1]) for e in nx.edge_bfs(G, v) if e[1] not in TC[v]) - ) + TC.add_edges_from((v, e[1]) for e in nx.edge_bfs(G, v) if e[1] not in TC[v]) return TC diff --git a/networkx/algorithms/distance_regular.py b/networkx/algorithms/distance_regular.py index cd2ab793..3813aa73 100644 --- a/networkx/algorithms/distance_regular.py +++ b/networkx/algorithms/distance_regular.py @@ -151,7 +151,7 @@ def intersection_array(G): raise nx.NetworkXError("Graph is not distance regular.") k = knext path_length = dict(nx.all_pairs_shortest_path_length(G)) - diameter = max([max(path_length[n].values()) for n in path_length]) + diameter = max(max(path_length[n].values()) for n in path_length) bint = {} # 'b' intersection array cint = {} # 'c' intersection array for u in G: diff --git a/networkx/algorithms/euler.py b/networkx/algorithms/euler.py index 5fd8dce4..6d433c0a 100644 --- a/networkx/algorithms/euler.py +++ b/networkx/algorithms/euler.py @@ -74,7 +74,7 @@ def _find_path_start(G): return arbitrary_element(G) if G.is_directed(): - v1, v2 = [v for v in G if G.in_degree(v) != G.out_degree(v)] + v1, v2 = (v for v in G if G.in_degree(v) != G.out_degree(v)) # Determines which is the 'start' node (as opposed to the 'end') if G.out_degree(v1) > G.in_degree(v1): return v1 diff --git a/networkx/algorithms/isomorphism/matchhelpers.py b/networkx/algorithms/isomorphism/matchhelpers.py index 5daaff69..c2f02778 100644 --- a/networkx/algorithms/isomorphism/matchhelpers.py +++ b/networkx/algorithms/isomorphism/matchhelpers.py @@ -198,8 +198,8 @@ def numerical_multiedge_match(attr, default, rtol=1.0000000000000001e-05, atol=1 if isinstance(attr, str): def match(datasets1, datasets2): - values1 = sorted([data.get(attr, default) for data in datasets1.values()]) - values2 = sorted([data.get(attr, default) for data in datasets2.values()]) + values1 = sorted(data.get(attr, default) for data in datasets1.values()) + values2 = sorted(data.get(attr, default) for data in datasets2.values()) return allclose(values1, values2, rtol=rtol, atol=atol) else: diff --git a/networkx/algorithms/isomorphism/tree_isomorphism.py b/networkx/algorithms/isomorphism/tree_isomorphism.py index 94ef6cd4..7e13d027 100644 --- a/networkx/algorithms/isomorphism/tree_isomorphism.py +++ b/networkx/algorithms/isomorphism/tree_isomorphism.py @@ -175,7 +175,7 @@ def rooted_tree_isomorphism(t1, root1, t2, root2): if dT.out_degree(v) > 0: # get all the pairs of labels and nodes of children # and sort by labels - s = sorted([(label[u], u) for u in dT.successors(v)]) + s = sorted((label[u], u) for u in dT.successors(v)) # invert to give a list of two tuples # the sorted labels, and the corresponding children @@ -183,7 +183,7 @@ def rooted_tree_isomorphism(t1, root1, t2, root2): # now collect and sort the sorted ordered_labels # for all nodes in L[i], carrying along the node - forlabel = sorted([(ordered_labels[v], v) for v in L[i]]) + forlabel = sorted((ordered_labels[v], v) for v in L[i]) # now assign labels to these nodes, according to the sorted order # starting from 0, where idential ordered_labels get the same label @@ -249,8 +249,8 @@ def tree_isomorphism(t1, t2): return [] # Another shortcut is that the sorted degree sequences need to be the same. - degree_sequence1 = sorted([d for (n, d) in t1.degree()]) - degree_sequence2 = sorted([d for (n, d) in t2.degree()]) + degree_sequence1 = sorted(d for (n, d) in t1.degree()) + degree_sequence2 = sorted(d for (n, d) in t2.degree()) if degree_sequence1 != degree_sequence2: return [] diff --git a/networkx/algorithms/link_analysis/hits_alg.py b/networkx/algorithms/link_analysis/hits_alg.py index 281ebad2..491214d9 100644 --- a/networkx/algorithms/link_analysis/hits_alg.py +++ b/networkx/algorithms/link_analysis/hits_alg.py @@ -129,7 +129,7 @@ def _hits_python(G, max_iter=100, tol=1.0e-8, nstart=None, normalized=True): for n in a: a[n] *= s # check convergence, l1 norm - err = sum([abs(h[n] - hlast[n]) for n in h]) + err = sum(abs(h[n] - hlast[n]) for n in h) if err < tol: break else: diff --git a/networkx/algorithms/link_analysis/pagerank_alg.py b/networkx/algorithms/link_analysis/pagerank_alg.py index f564b910..718ddb51 100644 --- a/networkx/algorithms/link_analysis/pagerank_alg.py +++ b/networkx/algorithms/link_analysis/pagerank_alg.py @@ -166,7 +166,7 @@ def _pagerank_python( x[nbr] += alpha * xlast[n] * wt x[n] += danglesum * dangling_weights.get(n, 0) + (1.0 - alpha) * p.get(n, 0) # check convergence, l1 norm - err = sum([abs(x[n] - xlast[n]) for n in x]) + err = sum(abs(x[n] - xlast[n]) for n in x) if err < N * tol: return x raise nx.PowerIterationFailedConvergence(max_iter) diff --git a/networkx/algorithms/matching.py b/networkx/algorithms/matching.py index cbafe8fa..0c874f40 100644 --- a/networkx/algorithms/matching.py +++ b/networkx/algorithms/matching.py @@ -217,7 +217,7 @@ def min_weight_matching(G, maxcardinality=False, weight="weight"): if len(G.edges) == 0: return max_weight_matching(G, maxcardinality, weight) G_edges = G.edges(data=weight, default=1) - min_weight = min([w for _, _, w in G_edges]) + min_weight = min(w for _, _, w in G_edges) InvG = nx.Graph() edges = ((u, v, 1 / (1 + w - min_weight)) for u, v, w in G_edges) InvG.add_weighted_edges_from(edges, weight=weight) diff --git a/networkx/algorithms/operators/all.py b/networkx/algorithms/operators/all.py index 92df582d..0ff17094 100644 --- a/networkx/algorithms/operators/all.py +++ b/networkx/algorithms/operators/all.py @@ -71,7 +71,7 @@ def union_all(graphs, rename=(None,)): graphs = [add_prefix(G, name) for G, name in zip_longest(graphs, rename)] - if sum([len(G) for G in graphs]) != len(set().union(*graphs)): + if sum(len(G) for G in graphs) != len(set().union(*graphs)): raise nx.NetworkXError( "The node sets of the graphs are not disjoint.", "Use appropriate rename" diff --git a/networkx/algorithms/shortest_paths/generic.py b/networkx/algorithms/shortest_paths/generic.py index e5a691e2..2c11eb5d 100644 --- a/networkx/algorithms/shortest_paths/generic.py +++ b/networkx/algorithms/shortest_paths/generic.py @@ -403,7 +403,7 @@ def average_shortest_path_length(G, weight=None, method=None): else: if method == "floyd-warshall": all_pairs = nx.floyd_warshall(G, weight=weight) - s = sum([sum(t.values()) for t in all_pairs.values()]) + s = sum(sum(t.values()) for t in all_pairs.values()) elif method == "floyd-warshall-numpy": s = nx.floyd_warshall_numpy(G, weight=weight).sum() return s / (n * (n - 1)) diff --git a/networkx/algorithms/similarity.py b/networkx/algorithms/similarity.py index c56eb55e..9966d76a 100644 --- a/networkx/algorithms/similarity.py +++ b/networkx/algorithms/similarity.py @@ -1515,48 +1515,6 @@ def simrank_similarity_numpy( ) -# TODO replace w/ math.comb(n, k) for Python 3.8+ -def _n_choose_k(n, k): - """Pure Python implementation of the binomial coefficient - - The general equation is n! / (k! * (n - k)!). The below - implementation is a more efficient version. - - Note: this will be removed in favor of Python 3.8's ``math.comb(n, k)``. - - Parameters - ---------- - n : int - Set of ``n`` elements - k : int - Unordered chosen subset of length ``k`` - - Returns - ------- - binomial_coeff : int - The number of ways (disregarding order) that k objects - can be chosen from among n objects. - - Examples - -------- - >>> _n_choose_k(5, 2) - 10 - >>> _n_choose_k(5, 4) - 5 - >>> _n_choose_k(100, 100) - 1 - - """ - if k > n: - return 0 - if n == k: - return 1 - elif k < n - k: - return reduce(mul, range(n - k + 1, n + 1)) // math.factorial(k) - else: - return reduce(mul, range(k + 1, n + 1)) // math.factorial(n - k) - - def panther_similarity(G, source, k=5, path_length=5, c=0.5, delta=0.1, eps=None): r"""Returns the Panther similarity of nodes in the graph `G` to node ``v``. @@ -1623,7 +1581,7 @@ def panther_similarity(G, source, k=5, path_length=5, c=0.5, delta=0.1, eps=None # Calculate the sample size ``R`` for how many paths # to randomly generate - t_choose_2 = _n_choose_k(path_length, 2) + t_choose_2 = math.comb(path_length, 2) sample_size = int((c / eps ** 2) * (np.log2(t_choose_2) + 1 + np.log(1 / delta))) index_map = {} _ = list( diff --git a/networkx/algorithms/smetric.py b/networkx/algorithms/smetric.py index e33f6702..b851e1ee 100644 --- a/networkx/algorithms/smetric.py +++ b/networkx/algorithms/smetric.py @@ -35,4 +35,4 @@ def s_metric(G, normalized=True): # Gmax = li_smax_graph(list(G.degree().values())) # return s_metric(G,normalized=False)/s_metric(Gmax,normalized=False) # else: - return float(sum([G.degree(u) * G.degree(v) for (u, v) in G.edges()])) + return float(sum(G.degree(u) * G.degree(v) for (u, v) in G.edges())) diff --git a/networkx/algorithms/summarization.py b/networkx/algorithms/summarization.py index b26639d7..a572a832 100644 --- a/networkx/algorithms/summarization.py +++ b/networkx/algorithms/summarization.py @@ -174,7 +174,7 @@ def dedensify(G, threshold, prefix=None, copy=True): degrees = G.in_degree if G.is_directed() else G.degree # Group nodes based on degree threshold - high_degree_nodes = set([n for n, d in degrees if d > threshold]) + high_degree_nodes = {n for n, d in degrees if d > threshold} low_degree_nodes = G.nodes() - high_degree_nodes auxillary = {} @@ -262,7 +262,7 @@ def _snap_build_graph( node_label_lookup = dict() for index, group_id in enumerate(groups): group_set = groups[group_id] - supernode = "%s%s" % (prefix, index) + supernode = f"{prefix}{index}" node_label_lookup[group_id] = supernode supernode_attributes = { attr: G.nodes[next(iter(group_set))][attr] for attr in node_attributes diff --git a/networkx/algorithms/tests/test_core.py b/networkx/algorithms/tests/test_core.py index 9caabfc1..ae546a38 100644 --- a/networkx/algorithms/tests/test_core.py +++ b/networkx/algorithms/tests/test_core.py @@ -54,9 +54,7 @@ class TestCore: def test_find_cores(self): core = nx.find_cores(self.G) - nodes_by_core = [ - sorted([n for n in core if core[n] == val]) for val in range(4) - ] + nodes_by_core = [sorted(n for n in core if core[n] == val) for val in range(4)] assert nodes_equal(nodes_by_core[0], [21]) assert nodes_equal(nodes_by_core[1], [17, 18, 19, 20]) assert nodes_equal(nodes_by_core[2], [9, 10, 11, 12, 13, 14, 15, 16]) @@ -68,9 +66,7 @@ class TestCore: def test_find_cores2(self): core = nx.find_cores(self.H) - nodes_by_core = [ - sorted([n for n in core if core[n] == val]) for val in range(3) - ] + nodes_by_core = [sorted(n for n in core if core[n] == val) for val in range(3)] assert nodes_equal(nodes_by_core[0], [0]) assert nodes_equal(nodes_by_core[1], [1, 3]) assert nodes_equal(nodes_by_core[2], [2, 4, 5, 6]) @@ -169,7 +165,7 @@ class TestCore: def test_onion_layers(self): layers = nx.onion_layers(self.G) nodes_by_layer = [ - sorted([n for n in layers if layers[n] == val]) for val in range(1, 7) + sorted(n for n in layers if layers[n] == val) for val in range(1, 7) ] assert nodes_equal(nodes_by_layer[0], [21]) assert nodes_equal(nodes_by_layer[1], [17, 18, 19, 20]) diff --git a/networkx/algorithms/tests/test_similarity.py b/networkx/algorithms/tests/test_similarity.py index 8c9b5422..c4fd17f1 100644 --- a/networkx/algorithms/tests/test_similarity.py +++ b/networkx/algorithms/tests/test_similarity.py @@ -5,7 +5,6 @@ from networkx.algorithms.similarity import ( graph_edit_distance, optimal_edit_paths, optimize_graph_edit_distance, - _n_choose_k, ) from networkx.generators.classic import ( circular_ladder_graph, @@ -791,18 +790,6 @@ class TestSimilarity: actual = nx.similarity._simrank_similarity_numpy(G, source=0, target=0) np.testing.assert_allclose(expected, actual, atol=1e-7) - def test_n_choose_k_small_k(self): - assert _n_choose_k(10, 4) == 210 - - def test_n_choose_k_big_k(self): - assert _n_choose_k(10, 8) == 45 - - def test_n_choose_k_same(self): - assert _n_choose_k(10, 10) == 1 - - def test_n_choose_k_k_bigger_than_n(self): - assert _n_choose_k(5, 10) == 0 - def test_panther_similarity_unweighted(self): np.random.seed(42) diff --git a/networkx/algorithms/tests/test_simple_paths.py b/networkx/algorithms/tests/test_simple_paths.py index aaf9a051..3e83c5d0 100644 --- a/networkx/algorithms/tests/test_simple_paths.py +++ b/networkx/algorithms/tests/test_simple_paths.py @@ -449,7 +449,7 @@ def test_shortest_simple_paths(): assert next(paths) == [1, 2, 3, 4, 8, 12] assert next(paths) == [1, 5, 6, 7, 8, 12] assert [len(path) for path in nx.shortest_simple_paths(G, 1, 12)] == sorted( - [len(path) for path in nx.all_simple_paths(G, 1, 12)] + len(path) for path in nx.all_simple_paths(G, 1, 12) ) @@ -469,7 +469,7 @@ def test_shortest_simple_paths_directed_with_weight_fucntion(): assert next(paths) == [1, 5, 6, 7, 8, 12] assert [ len(path) for path in nx.shortest_simple_paths(G, 1, 12, weight=cost) - ] == sorted([len(path) for path in nx.all_simple_paths(G, 1, 12)]) + ] == sorted(len(path) for path in nx.all_simple_paths(G, 1, 12)) def test_shortest_simple_paths_with_weight_fucntion(): diff --git a/networkx/algorithms/tests/test_summarization.py b/networkx/algorithms/tests/test_summarization.py index d633f825..2106f615 100644 --- a/networkx/algorithms/tests/test_summarization.py +++ b/networkx/algorithms/tests/test_summarization.py @@ -340,12 +340,12 @@ class TestSNAPNoEdgeTypes(AbstractSNAP): G.add_edge(source, target) supernodes = { - "Supernode-0": set(["A", "B"]), - "Supernode-1": set(["C", "D"]), - "Supernode-2": set(["E", "F"]), - "Supernode-3": set(["G", "H"]), - "Supernode-4": set(["I", "J"]), - "Supernode-5": set(["K", "L"]), + "Supernode-0": {"A", "B"}, + "Supernode-1": {"C", "D"}, + "Supernode-2": {"E", "F"}, + "Supernode-3": {"G", "H"}, + "Supernode-4": {"I", "J"}, + "Supernode-5": {"K", "L"}, } nx.set_node_attributes(G, supernodes, "group") return G @@ -418,12 +418,12 @@ class TestSNAPUndirected(AbstractSNAP): G.add_edge(source, target, types=[dict(type=type)]) supernodes = { - "Supernode-0": set(["A", "B"]), - "Supernode-1": set(["C", "D"]), - "Supernode-2": set(["E", "F"]), - "Supernode-3": set(["G", "H"]), - "Supernode-4": set(["I", "J"]), - "Supernode-5": set(["K", "L"]), + "Supernode-0": {"A", "B"}, + "Supernode-1": {"C", "D"}, + "Supernode-2": {"E", "F"}, + "Supernode-3": {"G", "H"}, + "Supernode-4": {"I", "J"}, + "Supernode-5": {"K", "L"}, } nx.set_node_attributes(G, supernodes, "group") return G @@ -488,12 +488,12 @@ class TestSNAPDirected(AbstractSNAP): G.add_edge(source, target, types=types) supernodes = { - "Supernode-0": set(["A", "B"]), - "Supernode-1": set(["C", "D"]), - "Supernode-2": set(["E", "F"]), - "Supernode-3": set(["G", "H"]), - "Supernode-4": set(["I", "J"]), - "Supernode-5": set(["K", "L"]), + "Supernode-0": {"A", "B"}, + "Supernode-1": {"C", "D"}, + "Supernode-2": {"E", "F"}, + "Supernode-3": {"G", "H"}, + "Supernode-4": {"I", "J"}, + "Supernode-5": {"K", "L"}, } nx.set_node_attributes(G, supernodes, "group") return G @@ -559,12 +559,12 @@ class TestSNAPUndirectedMulti(AbstractSNAP): G.add_edge(source, target, type=type) supernodes = { - "Supernode-0": set(["A", "B"]), - "Supernode-1": set(["C", "D"]), - "Supernode-2": set(["E", "F"]), - "Supernode-3": set(["G", "H"]), - "Supernode-4": set(["I", "J"]), - "Supernode-5": set(["K", "L"]), + "Supernode-0": {"A", "B"}, + "Supernode-1": {"C", "D"}, + "Supernode-2": {"E", "F"}, + "Supernode-3": {"G", "H"}, + "Supernode-4": {"I", "J"}, + "Supernode-5": {"K", "L"}, } nx.set_node_attributes(G, supernodes, "group") return G @@ -631,10 +631,10 @@ class TestSNAPDirectedMulti(AbstractSNAP): G.add_edge(source, target, type=type) supernodes = { - "Supernode-0": set(["A", "B"]), - "Supernode-1": set(["C", "D"]), - "Supernode-2": set(["E", "F"]), - "Supernode-3": set(["G", "H"]), + "Supernode-0": {"A", "B"}, + "Supernode-1": {"C", "D"}, + "Supernode-2": {"E", "F"}, + "Supernode-3": {"G", "H"}, } nx.set_node_attributes(G, supernodes, "group") return G diff --git a/networkx/algorithms/tests/test_threshold.py b/networkx/algorithms/tests/test_threshold.py index fdd03c19..9467e8de 100644 --- a/networkx/algorithms/tests/test_threshold.py +++ b/networkx/algorithms/tests/test_threshold.py @@ -190,7 +190,7 @@ class TestGeneratorThreshold: wseq = nxt.creation_sequence_to_weights("ddidiiidididid") ws = [s / float(12) for s in [6, 6, 5, 7, 4, 4, 4, 8, 3, 9, 2, 10, 1, 11]] - assert sum([abs(c - d) for c, d in zip(wseq, ws)]) < 1e-14 + assert sum(abs(c - d) for c, d in zip(wseq, ws)) < 1e-14 def test_finding_routines(self): G = nx.Graph({1: [2], 2: [3], 3: [4], 4: [5], 5: [6]}) @@ -223,11 +223,11 @@ class TestGeneratorThreshold: c1 = nxt.cluster_sequence(cs) c2 = list(nx.clustering(G).values()) - assert sum([abs(c - d) for c, d in zip(c1, c2)]) == pytest.approx(0, abs=1e-7) + assert sum(abs(c - d) for c, d in zip(c1, c2)) == pytest.approx(0, abs=1e-7) b1 = nx.betweenness_centrality(G).values() b2 = nxt.betweenness_sequence(cs) - assert sum([abs(c - d) for c, d in zip(b1, b2)]) < 1e-14 + assert sum(abs(c - d) for c, d in zip(b1, b2)) < 1e-14 assert nxt.eigenvalues(cs) == [0, 1, 3, 3, 5, 7, 7, 8] diff --git a/networkx/algorithms/threshold.py b/networkx/algorithms/threshold.py index 57677725..aa9b13db 100644 --- a/networkx/algorithms/threshold.py +++ b/networkx/algorithms/threshold.py @@ -772,7 +772,7 @@ def spectral_projection(u, eigenpairs): coeff = [] evect = eigenpairs[1] for ev in evect: - c = sum([evv * uv for (evv, uv) in zip(ev, u)]) + c = sum(evv * uv for (evv, uv) in zip(ev, u)) coeff.append(c) return coeff |