Python networkx.set_edge_attributes() Examples
The following are 30
code examples of networkx.set_edge_attributes().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
networkx
, or try the search function
.
Example #1
Source File: test_function.py From Carnets with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_get_edge_attributes(): graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()] for G in graphs: G = nx.path_graph(3, create_using=G) attr = 'hello' vals = 100 nx.set_edge_attributes(G, vals, attr) attrs = nx.get_edge_attributes(G, attr) assert_equal(len(attrs), 2) if G.is_multigraph(): keys = [(0, 1, 0), (1, 2, 0)] for u, v, k in keys: try: assert_equal(attrs[(u, v, k)], 100) except KeyError: assert_equal(attrs[(v, u, k)], 100) else: keys = [(0, 1), (1, 2)] for u, v in keys: try: assert_equal(attrs[(u, v)], 100) except KeyError: assert_equal(attrs[(v, u)], 100)
Example #2
Source File: test_function.py From aws-kube-codesuite with Apache License 2.0 | 6 votes |
def test_get_edge_attributes(): graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()] for G in graphs: G = nx.path_graph(3, create_using=G) attr = 'hello' vals = 100 nx.set_edge_attributes(G, vals, attr) attrs = nx.get_edge_attributes(G, attr) assert_equal(len(attrs), 2) if G.is_multigraph(): keys = [(0, 1, 0), (1, 2, 0)] for u, v, k in keys: try: assert_equal(attrs[(u, v, k)], 100) except KeyError: assert_equal(attrs[(v, u, k)], 100) else: keys = [(0, 1), (1, 2)] for u, v in keys: try: assert_equal(attrs[(u, v)], 100) except KeyError: assert_equal(attrs[(v, u)], 100)
Example #3
Source File: test_plotter.py From pygraphistry with BSD 3-Clause "New" or "Revised" License | 6 votes |
def test_networkx2igraph(self): import networkx as nx ng = nx.complete_graph(3) [x, y] = [int(x) for x in nx.__version__.split('.')] if x == 1: nx.set_node_attributes(ng, 'vattrib', 0) nx.set_edge_attributes(ng, 'eattrib', 1) else: nx.set_node_attributes(ng, 0, 'vattrib') nx.set_edge_attributes(ng, 1, 'eattrib') (e, n) = graphistry.bind(source='src', destination='dst').networkx2pandas(ng) edges = pd.DataFrame({ 'dst': {0: 1, 1: 2, 2: 2}, 'src': {0: 0, 1: 0, 2: 1}, 'eattrib': {0: 1, 1: 1, 2: 1} }) nodes = pd.DataFrame({ '__nodeid__': {0: 0, 1: 1, 2: 2}, 'vattrib': {0: 0, 1: 0, 2: 0} }) assertFrameEqual(e, edges) assertFrameEqual(n, nodes)
Example #4
Source File: rpp_star.py From postman_problems with MIT License | 6 votes |
def create_star_graph(n_nodes=10, ring=True): """ Create a star graph with the points connected by Args: n_nodes (int): number of nodes in graph (max 26) ring (Boolean): add ring around the border with low (distance=2) weights Returns: networkx MultiGraoh in the shape of a star """ graph = nx.MultiGraph() node_names = list(string.ascii_lowercase)[:n_nodes] graph.add_star(node_names) nx.set_edge_attributes(graph, 10, 'distance') nx.set_edge_attributes(graph, 1, 'required') nx.set_edge_attributes(graph, 'solid', 'style') if ring: for e in list(zip(node_names[1:-1] + [node_names[1]], node_names[2:] + [node_names[-1]])): graph.add_edge(e[0], e[1], distance=2, required=0, style='dashed') return graph
Example #5
Source File: test_compartment.py From ndlib with BSD 2-Clause "Simplified" License | 6 votes |
def test_edge_attribute(self): g = nx.karate_club_graph() attr = {(u, v): {"even": int((u+v) % 2)} for (u, v) in g.edges()} nx.set_edge_attributes(g, attr) model = gc.CompositeModel(g) model.add_status("Susceptible") model.add_status("Infected") c = cpm.EdgeCategoricalAttribute("even", "0", probability=0.6) model.add_rule("Susceptible", "Infected", c) config = mc.Configuration() config.add_model_parameter('fraction_infected', 0.1) model.set_initial_status(config) iterations = model.iteration_bunch(10) self.assertEqual(len(iterations), 10)
Example #6
Source File: safe_io.py From safepy with GNU General Public License v3.0 | 6 votes |
def calculate_edge_lengths(G, verbose=True): # Calculate the lengths of the edges if verbose: print('Calculating edge lengths...') x = np.matrix(G.nodes.data('x'))[:, 1] y = np.matrix(G.nodes.data('y'))[:, 1] node_coordinates = np.concatenate([x, y], axis=1) node_distances = squareform(pdist(node_coordinates, 'euclidean')) adjacency_matrix = np.array(nx.adjacency_matrix(G).todense()) adjacency_matrix = adjacency_matrix.astype('float') adjacency_matrix[adjacency_matrix == 0] = np.nan edge_lengths = np.multiply(node_distances, adjacency_matrix) edge_attr_dict = {index: v for index, v in np.ndenumerate(edge_lengths) if ~np.isnan(v)} nx.set_edge_attributes(G, edge_attr_dict, 'length') return G
Example #7
Source File: topology.py From YAFS with MIT License | 6 votes |
def load_graphml(self,filename): warnings.warn("The load_graphml function is deprecated and " "will be removed in version 2.0.0. " "Use NX.READ_GRAPHML function instead.", FutureWarning, stacklevel=8 ) self.G = nx.read_graphml(filename) attEdges = {} for k in self.G.edges(): attEdges[k] = {"BW": 1, "PR": 1} nx.set_edge_attributes(self.G, values=attEdges) attNodes = {} for k in self.G.nodes(): attNodes[k] = {"IPT": 1} nx.set_node_attributes(self.G, values=attNodes) for k in self.G.nodes(): self.nodeAttributes[k] = self.G.node[k] #it has "id" att. TODO IMPROVE
Example #8
Source File: test_max_cut.py From dwave_networkx with Apache License 2.0 | 6 votes |
def test_typical_cases(self): G = nx.complete_graph(10) S = dnx.maximum_cut(G, ExactSolver()) self.assertTrue(len(S) == 5) # half of the nodes with self.assertRaises(dnx.DWaveNetworkXException): S = dnx.weighted_maximum_cut(G, ExactSolver()) nx.set_edge_attributes(G, 1, 'weight') S = dnx.weighted_maximum_cut(G, ExactSolver()) self.assertTrue(len(S) == 5) # half of the nodes G = nx.Graph() G.add_edges_from([(0, 1), (0, 2), (1, 2), (1, 3), (3, 4), (2, 4)]) S = dnx.maximum_cut(G, ExactSolver()) self.assertTrue(len(S) in (2, 3)) # this needs another one for weight
Example #9
Source File: test_distance.py From netrd with MIT License | 6 votes |
def test_weighted_input(): G1 = nx.karate_club_graph() G2 = nx.karate_club_graph() rand = np.random.RandomState(seed=42) edge_weights = {e: rand.randint(0, 1000) for e in G2.edges} nx.set_edge_attributes(G2, edge_weights, "weight") assert nx.is_isomorphic(G1, G2) for label, obj in distance.__dict__.items(): with warnings.catch_warnings(record=True) as w: warnings.simplefilter("always") if isinstance(obj, type) and BaseDistance in obj.__bases__: dist = obj().dist(G1, G2) warning_triggered = False for warning in w: if "weighted" in str(warning.message): warning_triggered = True if not warning_triggered: assert not np.isclose(dist, 0.0) else: assert np.isclose(dist, 0.0)
Example #10
Source File: test_function.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_set_edge_attributes(): graphs = [nx.Graph(), nx.DiGraph()] for G in graphs: G = nx.path_graph(3, create_using=G) # Test single value attr = 'hello' vals = 3 nx.set_edge_attributes(G, attr, vals) assert_equal(G[0][1][attr], vals) assert_equal(G[1][2][attr], vals) # Test multiple values attr = 'hi' edges = [(0,1), (1,2)] vals = dict(zip(edges, range(len(edges)))) nx.set_edge_attributes(G, attr, vals) assert_equal(G[0][1][attr], 0) assert_equal(G[1][2][attr], 1)
Example #11
Source File: test_function.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_set_edge_attributes_multi(): graphs = [nx.MultiGraph(), nx.MultiDiGraph()] for G in graphs: G = nx.path_graph(3, create_using=G) # Test single value attr = 'hello' vals = 3 nx.set_edge_attributes(G, attr, vals) assert_equal(G[0][1][0][attr], vals) assert_equal(G[1][2][0][attr], vals) # Test multiple values attr = 'hi' edges = [(0,1,0), (1,2,0)] vals = dict(zip(edges, range(len(edges)))) nx.set_edge_attributes(G, attr, vals) assert_equal(G[0][1][0][attr], 0) assert_equal(G[1][2][0][attr], 1)
Example #12
Source File: test_function.py From qgisSpaceSyntaxToolkit with GNU General Public License v3.0 | 6 votes |
def test_get_edge_attributes(): graphs = [nx.Graph(), nx.DiGraph(), nx.MultiGraph(), nx.MultiDiGraph()] for G in graphs: G = nx.path_graph(3, create_using=G) attr = 'hello' vals = 100 nx.set_edge_attributes(G, attr, vals) attrs = nx.get_edge_attributes(G, attr) assert_equal(len(attrs), 2) if G.is_multigraph(): keys = [(0,1,0), (1,2,0)] else: keys = [(0,1), (1,2)] for key in keys: assert_equal(attrs[key], 100)
Example #13
Source File: test_simple_paths.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_weighted_shortest_simple_path(): def cost_func(path): return sum(G.adj[u][v]['weight'] for (u, v) in zip(path, path[1:])) G = nx.complete_graph(5) weight = {(u, v): random.randint(1, 100) for (u, v) in G.edges()} nx.set_edge_attributes(G, weight, 'weight') cost = 0 for path in nx.shortest_simple_paths(G, 0, 3, weight='weight'): this_cost = cost_func(path) assert_true(cost <= this_cost) cost = this_cost
Example #14
Source File: test_function.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_set_edge_attributes_multi(): graphs = [nx.MultiGraph(), nx.MultiDiGraph()] for G in graphs: # Test single value G = nx.path_graph(3, create_using=G) attr = 'hello' vals = 3 nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][0][attr], vals) assert_equal(G[1][2][0][attr], vals) # Test multiple values G = nx.path_graph(3, create_using=G) attr = 'hi' edges = [(0, 1, 0), (1, 2, 0)] vals = dict(zip(edges, range(len(edges)))) nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][0][attr], 0) assert_equal(G[1][2][0][attr], 1) # Test dictionary of dictionaries G = nx.path_graph(3, create_using=G) d = {'hi': 0, 'hello': 200} edges = [(0, 1, 0)] vals = dict.fromkeys(edges, d) nx.set_edge_attributes(G, vals) assert_equal(G[0][1][0]['hi'], 0) assert_equal(G[0][1][0]['hello'], 200) assert_equal(G[1][2][0], {})
Example #15
Source File: test_structuralholes.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_effective_size_weighted_undirected(self): G = self.G.copy() nx.set_edge_attributes(G, self.G_weights, 'weight') effective_size = nx.effective_size(G, weight='weight') assert_almost_equal(round(effective_size['G'], 2), 5.47) assert_almost_equal(round(effective_size['A'], 2), 2.47) assert_almost_equal(round(effective_size['C'], 2), 1)
Example #16
Source File: test_function.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_set_edge_attributes(): graphs = [nx.Graph(), nx.DiGraph()] for G in graphs: # Test single value G = nx.path_graph(3, create_using=G) attr = 'hello' vals = 3 nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][attr], vals) assert_equal(G[1][2][attr], vals) # Test multiple values G = nx.path_graph(3, create_using=G) attr = 'hi' edges = [(0, 1), (1, 2)] vals = dict(zip(edges, range(len(edges)))) nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][attr], 0) assert_equal(G[1][2][attr], 1) # Test dictionary of dictionaries G = nx.path_graph(3, create_using=G) d = {'hi': 0, 'hello': 200} edges = [(0, 1)] vals = dict.fromkeys(edges, d) nx.set_edge_attributes(G, vals) assert_equal(G[0][1]['hi'], 0) assert_equal(G[0][1]['hello'], 200) assert_equal(G[1][2], {})
Example #17
Source File: test_gomory_hu.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_florentine_families_graph(self): G = nx.florentine_families_graph() nx.set_edge_attributes(G, 1, 'capacity') for flow_func in flow_funcs: T = nx.gomory_hu_tree(G, flow_func=flow_func) assert_true(nx.is_tree(T)) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert_equal(nx.minimum_cut_value(G, u, v), cut_value)
Example #18
Source File: test_maxflow.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_complete_graph_cutoff(self): G = nx.complete_graph(5) nx.set_edge_attributes(G, dict(((u, v), 1) for u, v in G.edges()), 'capacity') for flow_func in [shortest_augmenting_path, edmonds_karp]: for cutoff in [3, 2, 1]: result = nx.maximum_flow_value(G, 0, 4, flow_func=flow_func, cutoff=cutoff) assert_equal(cutoff, result, msg="cutoff error in {0}".format(flow_func.__name__))
Example #19
Source File: test_gomory_hu.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_davis_southern_women_graph(self): G = nx.davis_southern_women_graph() nx.set_edge_attributes(G, 1, 'capacity') for flow_func in flow_funcs: T = nx.gomory_hu_tree(G, flow_func=flow_func) assert_true(nx.is_tree(T)) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert_equal(nx.minimum_cut_value(G, u, v), cut_value)
Example #20
Source File: test_gomory_hu.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_karate_club_graph(self): G = nx.karate_club_graph() nx.set_edge_attributes(G, 1, 'capacity') for flow_func in flow_funcs: T = nx.gomory_hu_tree(G, flow_func=flow_func) assert_true(nx.is_tree(T)) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert_equal(nx.minimum_cut_value(G, u, v), cut_value)
Example #21
Source File: test_gomory_hu.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_default_flow_function_karate_club_graph(self): G = nx.karate_club_graph() nx.set_edge_attributes(G, 1, 'capacity') T = nx.gomory_hu_tree(G) assert_true(nx.is_tree(T)) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert_equal(nx.minimum_cut_value(G, u, v), cut_value)
Example #22
Source File: test_gomory_hu.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_karate_club_graph(self): G = nx.karate_club_graph() nx.set_edge_attributes(G, 1, 'capacity') for flow_func in flow_funcs: T = nx.gomory_hu_tree(G, flow_func=flow_func) assert_true(nx.is_tree(T)) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert_equal(nx.minimum_cut_value(G, u, v), cut_value)
Example #23
Source File: test_gomory_hu.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_davis_southern_women_graph(self): G = nx.davis_southern_women_graph() nx.set_edge_attributes(G, 1, 'capacity') for flow_func in flow_funcs: T = nx.gomory_hu_tree(G, flow_func=flow_func) assert_true(nx.is_tree(T)) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert_equal(nx.minimum_cut_value(G, u, v), cut_value)
Example #24
Source File: test_gomory_hu.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_florentine_families_graph(self): G = nx.florentine_families_graph() nx.set_edge_attributes(G, 1, 'capacity') for flow_func in flow_funcs: T = nx.gomory_hu_tree(G, flow_func=flow_func) assert_true(nx.is_tree(T)) for u, v in combinations(G, 2): cut_value, edge = self.minimum_edge_weight(T, u, v) assert_equal(nx.minimum_cut_value(G, u, v), cut_value)
Example #25
Source File: test_maxflow.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_complete_graph_cutoff(self): G = nx.complete_graph(5) nx.set_edge_attributes(G, {(u, v): 1 for u, v in G.edges()}, 'capacity') for flow_func in [shortest_augmenting_path, edmonds_karp]: for cutoff in [3, 2, 1]: result = nx.maximum_flow_value(G, 0, 4, flow_func=flow_func, cutoff=cutoff) assert_equal(cutoff, result, msg="cutoff error in {0}".format(flow_func.__name__))
Example #26
Source File: test_gomory_hu.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_karate_club_graph_cutset(self): G = nx.karate_club_graph() nx.set_edge_attributes(G, 1, 'capacity') T = nx.gomory_hu_tree(G) assert_true(nx.is_tree(T)) u, v = 0, 33 cut_value, edge = self.minimum_edge_weight(T, u, v) cutset = self.compute_cutset(G, T, edge) assert_equal(cut_value, len(cutset))
Example #27
Source File: test_function.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_set_edge_attributes_multi(): graphs = [nx.MultiGraph(), nx.MultiDiGraph()] for G in graphs: # Test single value G = nx.path_graph(3, create_using=G) attr = 'hello' vals = 3 nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][0][attr], vals) assert_equal(G[1][2][0][attr], vals) # Test multiple values G = nx.path_graph(3, create_using=G) attr = 'hi' edges = [(0, 1, 0), (1, 2, 0)] vals = dict(zip(edges, range(len(edges)))) nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][0][attr], 0) assert_equal(G[1][2][0][attr], 1) # Test dictionary of dictionaries G = nx.path_graph(3, create_using=G) d = {'hi': 0, 'hello': 200} edges = [(0, 1, 0)] vals = dict.fromkeys(edges, d) nx.set_edge_attributes(G, vals) assert_equal(G[0][1][0]['hi'], 0) assert_equal(G[0][1][0]['hello'], 200) assert_equal(G[1][2][0], {})
Example #28
Source File: test_function.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_set_edge_attributes(): graphs = [nx.Graph(), nx.DiGraph()] for G in graphs: # Test single value G = nx.path_graph(3, create_using=G) attr = 'hello' vals = 3 nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][attr], vals) assert_equal(G[1][2][attr], vals) # Test multiple values G = nx.path_graph(3, create_using=G) attr = 'hi' edges = [(0, 1), (1, 2)] vals = dict(zip(edges, range(len(edges)))) nx.set_edge_attributes(G, vals, attr) assert_equal(G[0][1][attr], 0) assert_equal(G[1][2][attr], 1) # Test dictionary of dictionaries G = nx.path_graph(3, create_using=G) d = {'hi': 0, 'hello': 200} edges = [(0, 1)] vals = dict.fromkeys(edges, d) nx.set_edge_attributes(G, vals) assert_equal(G[0][1]['hi'], 0) assert_equal(G[0][1]['hello'], 200) assert_equal(G[1][2], {})
Example #29
Source File: test_structuralholes.py From aws-kube-codesuite with Apache License 2.0 | 5 votes |
def test_effective_size_weighted_directed(self): D = self.D.copy() nx.set_edge_attributes(D, self.D_weights, 'weight') effective_size = nx.effective_size(D, weight='weight') assert_almost_equal(round(effective_size[0], 3), 1.567) assert_almost_equal(round(effective_size[1], 3), 1.083) assert_almost_equal(round(effective_size[2], 3), 1)
Example #30
Source File: test_structuralholes.py From Carnets with BSD 3-Clause "New" or "Revised" License | 5 votes |
def test_constraint_weighted_directed(self): D = self.D.copy() nx.set_edge_attributes(D, self.D_weights, 'weight') constraint = nx.constraint(D, weight='weight') assert_almost_equal(round(constraint[0], 3), 0.840) assert_almost_equal(round(constraint[1], 3), 1.143) assert_almost_equal(round(constraint[2], 3), 1.378)