def setUp(self): # Create digraph with negative resource costs with unreachable node 'E' self.G = DiGraph(directed=True, n_res=2) self.G.add_edge('Source', 'A', res_cost=array([1, 2]), weight=0) self.G.add_edge('A', 'C', res_cost=array([-1, 0.3]), weight=0) self.G.add_edge('A', 'B', res_cost=array([-1, 3]), weight=0) self.G.add_edge('B', 'D', res_cost=array([-1, 2]), weight=0) # Unreachable node E self.G.add_edge('B', 'E', res_cost=array([10, 1]), weight=0) self.G.add_edge('C', 'D', res_cost=array([1, 0.1]), weight=0) self.G.add_edge('D', 'Sink', res_cost=array([1, 0.1]), weight=0) # Create digraph with a resource infeasible minimum cost path self.H = MultiGraph(directed=True) self.H.add_edge('Source', 'A', res_cost=array([1, 1]), weight=-1) self.H.add_edge('A', 'B', res_cost=array([1, 1]), weight=-1) self.H.add_edge('B', 'Sink', res_cost=array([1, 1]), weight=-1) self.H.add_edge('Sink', 'Source', weight=-1) self.max_res, self.min_res = ["foo"], ["bar"] # Create digraph to test issue 72 self.F = DiGraph(n_res=1) self.F.add_edge('Source', 'A', res_cost=array([1]), weight=1) self.F.add_edge('A', 'B', res_cost=array([10]), weight=1) self.F.add_edge('A', 'Sink', res_cost=array([1]), weight=1)
def test_auto_neb(self): # Test AutoNEB procedure graph = MultiGraph() for idx, minimum in enumerate(self.minima): graph.add_node(idx + 1, **minimum) # Set up AutoNEB schedule spring_constant = float("inf") eval_config = EvalConfig(128) optim_config_1 = OptimConfig(10, SGD, {"lr": 0.1}, None, None, eval_config) optim_config_2 = OptimConfig(10, SGD, {"lr": 0.01}, None, None, eval_config) weight_decay = 0 subsample_pivot_count = 1 neb_configs = [ NEBConfig(spring_constant, weight_decay, equal, {"count": 2}, subsample_pivot_count, optim_config_1), NEBConfig(spring_constant, weight_decay, highest, {"count": 3, "key": "dense_train_loss"}, subsample_pivot_count, optim_config_1), NEBConfig(spring_constant, weight_decay, highest, {"count": 3, "key": "dense_train_loss"}, subsample_pivot_count, optim_config_2), NEBConfig(spring_constant, weight_decay, highest, {"count": 3, "key": "dense_train_loss"}, subsample_pivot_count, optim_config_2), ] auto_neb_config = AutoNEBConfig(neb_configs) self.assertEqual(auto_neb_config.cycle_count, len(neb_configs)) # Run AutoNEB auto_neb(1, 2, graph, self.model, auto_neb_config) self.assertEqual(len(graph.edges), auto_neb_config.cycle_count)
def multiple_edges(self, new): """ Get/set whether or not self allows multiple edges. INPUT: new: boolean or None DOCTEST: sage: G = sage.graphs.base.graph_backends.NetworkXGraphBackend() sage: G.multiple_edges(True) sage: G.multiple_edges(None) True """ try: assert(not isinstance(self._nxg, (NetworkXGraphDeprecated, NetworkXDiGraphDeprecated))) except AssertionError: self._nxg = self._nxg.mutate() from networkx import Graph,MultiGraph,DiGraph,MultiDiGraph if new is None: return self._nxg.is_multigraph() if new == self._nxg.is_multigraph(): return if new: if self._nxg.is_directed(): self._nxg = MultiDiGraph(self._nxg) else: self._nxg = MultiGraph(self._nxg) else: if self._nxg.is_directed(): self._nxg = DiGraph(self._nxg) else: self._nxg = Graph(self._nxg)
def __init__(self, symbols=None, positions=None, numbers=None, tags=None, momenta=None, masses=None, magmoms=None, charges=None, scaled_positions=None, cell=None, pbc=None, celldisp=None, constraint=None, calculator=None, info=None, edges=None): super().__init__(symbols, positions, numbers, tags, momenta, masses, magmoms, charges, scaled_positions, cell, pbc, celldisp, constraint, calculator, info) if self.pbc.any(): self._graph = MultiGraph() else: self._graph = Graph() nodes = [[i, { 'number': n }] for i, n in enumerate(self.arrays['numbers'])] self._graph.add_nodes_from(nodes) if edges: self._graph.add_edges_from(edges, bonds=1) self._surface_atoms = None
def test_sequence(self): graph = MultiGraph() graph.add_node(1, value=1) # Global minimum graph.add_node(2, value=2) graph.add_node(3, value=3) graph.add_node(4, value=4) def weight(id_pair): return sum(node ** 2 for node in id_pair) unfinished_edge = (1, 3) config = LandscapeExplorationConfig("value", "weight", [], None, AutoNEBConfig([None, None])) # Disconnect suggest correct_order = [ (1, 2), (1, 3), (1, 4), ] config.suggest_methods = [disconnected] while True: pair = suggest_pair(graph, config) if pair[0] is None: break self.assertGreater(len(correct_order), 0, "disconnected_suggest gives more pairs than necessary") assert pair == correct_order.pop(0) graph.add_edge(*pair, key=1, weight=weight(pair)) if pair != unfinished_edge: # Skip the second edge for this pair, to test unfinished suggest graph.add_edge(*pair, key=2, weight=weight(pair)) self.assertEqual(len(correct_order), 0, "disconnected_suggest missing suggestions!") # Unfinished suggest correct_order = [ unfinished_edge ] config.suggest_methods = [unfinished] while True: pair = suggest_pair(graph, config) if pair[0] is None: break self.assertGreater(len(correct_order), 0, "unfinished_suggest gives more pairs than necessary") assert pair == correct_order.pop(0) graph.add_edge(*pair, key=2, weight=weight(pair)) self.assertEqual(len(correct_order), 0, "unfinished_suggest missing suggestions!") # Core: MST suggest correct_order = [ (2, 4), (3, 4), # Replace (1, 4) (2, 3), # Replace (1, 3) ] config.suggest_methods = [mst] while True: pair = suggest_pair(graph, config) if pair[0] is None: break self.assertGreater(len(correct_order), 0, "mst_suggest gives more pairs than necessary") assert pair == correct_order.pop(0) graph.add_edge(*pair, key=1, weight=weight(pair)) graph.add_edge(*pair, key=2, weight=weight(pair)) self.assertEqual(len(correct_order), 0, "mst_suggest missing suggestions!")
def test_relabel_nodes_multigraph(): """failed after switching to dg.relabel_nodes""" G = MultiGraph([('a', 'b'), ('a', 'b')]) mapping = {'a': 'aardvark', 'b': 'bear'} G = relabel_nodes(G, mapping, copy=False) assert sorted(G.nodes()) == ['aardvark', 'bear'] assert_edges_equal(sorted(G.edges()), [('aardvark', 'bear'), ('aardvark', 'bear')])
def initGraph(self): self.shuttlingGraph = MultiGraph() for edge in self: self.shuttlingGraph.add_node(edge.startName) self.shuttlingGraph.add_node(edge.stopName) self.shuttlingGraph.add_edge(edge.startName, edge.stopName, key=hash(edge), edge=edge, weight=abs(edge.stopLine-edge.startLine)) self.nodeLookup[edge.startLine] = edge.startName self.nodeLookup[edge.stopLine] = edge.stopName
def __make_route_graph(routes): graph = MultiGraph() for route in routes: # assume each segment has weight 1 - we could estimate edge weight via # distance bewteen stops (we have location information), but we have # no information to tell us how fast any particular service is. make_route_segment = lambda src, dst: (src, dst, {'route': route}) segments = zip_with(make_route_segment, route.stops(), tail(route.stops())) graph.add_edges_from(segments) return graph
def get_fbvs(graph): if is_acyclic(graph): return set() if type(graph) is not MultiGraph: graph = MultiGraph(graph) for i in range(1, graph.number_of_nodes()): result = get_fbvs_max_size(graph, i) if result is not None: return result # in the worst case, result is n-2 nodes
def create_grid_network(domain_extents, row_spacing, col_spacing): """ Create a Manhattan network with horizontal / vertical edges. :param domain_extents: (xmin, ymin, xmax, ymax) of the domain :param row_spacing: Distance between horizontal edges :param col_spacing: Distance between vertical edges :return: Streetnet object """ xmin, ymin, xmax, ymax = domain_extents # compute edge coords y = np.arange(ymin + row_spacing / 2., ymax, row_spacing) x = np.arange(xmin + col_spacing / 2., xmax, col_spacing) g = MultiGraph() letters = [] aint = ord('a') for i in range(26): for j in range(26): for k in range(26): letters.append(chr(aint + i) + chr(aint + j) + chr(aint + k)) def add_edge(x0, y0, x1, y1): if x0 < 0 or y0 < 0 or x1 >= len(x) or y1 >= len(y): # no link needed return k0 = x0 * x.size + y0 k1 = x1 * x.size + y1 idx_x0 = letters[k0] idx_x1 = letters[k1] label0 = idx_x0 + str(y0) label1 = idx_x1 + str(y1) ls = LineString([ (x[x0], y[y0]), (x[x1], y[y1]), ]) atts = { 'fid': "%s-%s" % (label0, label1), 'linestring': ls, 'length': ls.length, 'orientation_neg': label0, 'orientation_pos': label1 } g.add_edge(label0, label1, key=atts['fid'], attr_dict=atts) g.node[label0]['loc'] = (x[x0], y[y0]) g.node[label1]['loc'] = (x[x1], y[y1]) for i in range(x.size): for j in range(y.size): add_edge(i, j - 1, i, j) add_edge(i - 1, j, i, j) add_edge(i, j, i, j + 1) add_edge(i, j, i + 1, j) return itn.ITNStreetNet.from_multigraph(g)
def __init__(self, nodes=None, edges=None): self.graph = MultiGraph() # Nodes nodes = nodes or {} for node_id, (lon, lat) in nodes.items(): self.add_node(node_id, lon, lat) # Edges edges = edges or {} for (n1, n2), (edge_type, dict_attrs) in edges.items(): self.add_edge(n1, n2, edge_type, **dict_attrs)
def create_graph(self, _type: str): if _type == 'di': graph = DiGraph() elif _type == 'mx': graph = MultiGraph() else: # _type == 'g' graph = Graph() graph.add_edges_from(self.edges) graph.add_nodes_from(self.nodes) return graph
def main(): parser = ArgumentParser() parser.add_argument("project_directory", nargs=1) parser.add_argument("config_file", nargs=1) parser.add_argument("--no-backup", default=False, action="store_true") args = parser.parse_args() project_directory = args.project_directory[0] config_file = args.config_file[0] graph_path, project_config_path = setup_project(config_file, project_directory) model, minima_count, min_config, lex_config = read_config_file( project_config_path) # Setup Logger root_logger = getLogger() root_logger.setLevel(INFO) root_logger.addHandler(StreamHandler(sys.stdout)) root_logger.addHandler( FileHandler(join(project_directory, "exploration.log"))) # === Create/load graph === if isfile(graph_path): if not args.no_backup: root_logger.info("Copying current 'graph.p' to backup file.") copyfile( graph_path, graph_path.replace(".p", f"_bak{strftime('%Y%m%d-%H%M')}.p")) else: root_logger.info( "Not creating a backup of 'graph.p' because of user request.") graph = repair_graph(load_pickle_graph(graph_path), model) else: graph = MultiGraph() # Call this after every optmisiation def save_callback(): store_pickle_graph(graph, graph_path) # === Ensure the specified number of minima === for _ in pbar(range(len(graph.nodes), minima_count), "Finding minima"): minimum_data = find_minimum(model, min_config) graph.add_node( max(graph.nodes) + 1 if len(graph.nodes) > 0 else 1, **move_to(minimum_data, "cpu")) save_callback() # === Connect minima === landscape_exploration(graph, model, lex_config, callback=save_callback)
def test_run_walks(): mg = MultiGraph() mg.add_edge('a', 'b') mg.add_edge('b', 'c') dw = DeepWalk(mg, 10, 100) dw.get_walks(workers=1) # Number of neighbors for all nodes together is 4, times niter: 400 assert len(dw.walks) == 400, len(dw.walks) assert len([w for w in dw.walks if w[0] == 'a']) == 100 assert len([w for w in dw.walks if w[0] == 'b']) == 200 dw.get_walks(workers=2) # Number of neighbors for all nodes together is 4, times niter: 400 assert len(dw.walks) == 400, len(dw.walks) assert len([w for w in dw.walks if w[0] == 'a']) == 100 assert len([w for w in dw.walks if w[0] == 'b']) == 200
def get_fbvs(self, graph: Graph): if is_acyclic(graph): return set() # Save the original node set for later use since we'll mutate the graph nodes = set(graph.nodes()) if type(graph) is not MultiGraph: graph = MultiGraph(graph) mif_set = self.preprocess_1(graph, set(), None) if mif_set is not None: fbvs = nodes.difference(mif_set) return fbvs return None
def to_networkx(network: Network) -> Any: from networkx import Graph, DiGraph, MultiDiGraph, MultiGraph if network.directed and network.multiedges: G = MultiDiGraph() elif not network.directed and network.multiedges: G = MultiGraph() elif network.directed and not network.multiedges: G = DiGraph() else: G = Graph() G.add_nodes_from([(v, network.nodes[v].attributes) for v in network.nodes.uids]) G.add_edges_from([(network.edges[e].v.uid, network.edges[e].w.uid, network.edges[e].attributes) for e in network.edges.uids]) return G
def main(): g = MultiGraph() analyzer = TraceAnalyzer() analyzer.parse("../pintool.log") # Get all the chunks used by the application chunks = analyzer.getChunks() for chunk in chunks: g.add_node("%x-%x" % (chunk.chunk_addr, chunk.timestamp)) writes = analyzer.getMemoryWrites() for write in writes: b = "C:%x-%x" % (write.chunk_addr, write.timestamp) a = "W:%x-%x" % (write.write_addr, write.content) g.add_edge(a, b) draw_shell(g) plt.show()
def build_graph(self, inside_percent=0.4, multiprocessing=True, crs='EPSG:4326'): """ Compute visibility graph for a set of polygons and polylines. :param float inside_percent: (from 0 to 1) - controls the number of inner polygon edges :param bool multiprocessing: speed up computation for dense areas using multiprocessing :param str crs: coordinate reference system :rtype: networkx.MultiGraph """ if inside_percent < 0 or inside_percent > 1: raise ValueError("inside_percent should be from 1 to 0") graph = MultiGraph(crs=crs) self.__process_points_of_objects(True, graph, inside_percent, multiprocessing) self.__process_points_of_objects(False, graph, inside_percent, multiprocessing) return graph
def multiple_edges(self, new=None): """ Get/set whether or not self allows multiple edges. INPUT: - ``new`` -- can be a boolean (in which case it sets the value) or ``None``, in which case the current value is returned. It is set to ``None`` by default. TESTS:: sage: G = sage.graphs.base.graph_backends.NetworkXGraphBackend() sage: G.multiple_edges(True) sage: G.multiple_edges(None) True """ try: assert (not isinstance( self._nxg, (NetworkXGraphDeprecated, NetworkXDiGraphDeprecated))) except AssertionError: self._nxg = self._nxg.mutate() from networkx import Graph, MultiGraph, DiGraph, MultiDiGraph if new is None: return self._nxg.is_multigraph() if new == self._nxg.is_multigraph(): return if new: if self._nxg.is_directed(): self._nxg = MultiDiGraph(self._nxg) else: self._nxg = MultiGraph(self._nxg) else: if self._nxg.is_directed(): self._nxg = DiGraph(self._nxg) else: self._nxg = Graph(self._nxg)
basename = splitext(basename(sys.argv[1]))[0] random.seed(1) print "Reading {}, writing {}".format(sys.argv[1], outname) graph = read_gml(sys.argv[1]) hsubspec = "flowsize=exponential(1/10000.0) flowstart=exponential(100) ipproto=randomchoice(6) sport=randomchoice(22,80,443) dport=randomunifint(1025,65535) lossrate=randomchoice(0.001)" mnodes = add_measurement(graph) newgraph = MultiGraph(name=basename, counterexportfile=basename + "_counters", flowexport="text", flowsampling=1.0, pktsampling=1.0, exportcycle=60, counterexport=True, counterexportinterval=1, longflowtmo=60, flowinactivetmo=60, harpoonsubspec=hsubspec, measurementnodes=mnodes) def get_cap(label): if 'OC192/STM64' in label: return '10Gb' elif 'OC3' in label: return '155Mb' elif 'OC12' in label: return '622Mb' elif 'OC48' in label:
def graph_to_multigraph(g: Graph) -> MultiGraph: gx = MultiGraph(g) return gx
def graph_from_dataframe( dataframe, threshold_by_percent_unique=0.1, threshold_by_count_unique=None, node_id_columns=[], node_property_columns=[], edge_property_columns=[], node_type_key="type", edge_type_key="type", collapse_edges=True, edge_agg_key="weight", ): """ Build an undirected graph from a pandas dataframe. This function attempts to infer which cells should become nodes based on either: a. what percentage of the column are unique values (defaults to 10%) b. an explicit count of unique values (i.e. any column with 7 unique values or less) c. an explicit list of column keys (i.e. ['employee_id', 'location_code']) Column headers are preserved as node and edge 'types'. By default, this is stored using the key 'type' which is used by some graph import processes but can be reconfigured. This function uses a MultiGraph structure during the build phase so that it is possible to make multiple connections between nodes. By default, at the end of the build phase, the MultiGraph is converted to a Graph and the count of edges between each node-pair is written as a 'weight' property. :param pandas.DataFrame dataframe: A pandas dataframe containing the data to be converted into a graph. :param float threshold_by_percent_unique: A percent value used to determine whether a column should be used to generate nodes based on its cardinality (i.e. in a dataframe with 100 rows, treat any column with 10 or less unique values as a node) :param int threshold_by_count_unique: A numeric value used to determine whether a column should be used to generate nodes based on its cardinality (i.e. if 7 is supplied, treat any column with 7 or less unique values as a node) - supplying a value will take priority over percent_unique :param list node_id_columns: A list of column headers to use for generating nodes. Suppyling any value will take precedence over threshold_by_percent_unique or threshold_by_count_unique. Note: this can cause the size of the graph to expand significantly since every unique value in a column will become a node. :param list node_property_columns: A list of column headers to use for generating properties of nodes. These can include the same column headers used for the node id. :param list edge_property_columns: A list of column headers to use for generating properties of edges. :param str node_type_key: A string that sets the key will be used to preserve the column name as node property (this is useful for importing networkx graphs to databases that distinguish between node 'types' or for visually encoding those types in plots). :param str edge_type_key: A string that sets the key will be used to keep track of edge relationships an 'types' (this is useful for importing networkx graphs to databases that distinguish between edge'types' or for visually encoding those types in plots). Edge type values are automatically set to <node_a_id>_<node_b_id>. :param bool collapse_edges: Graphs are instantiated as a 'MultiGraph' (allow multiple edges between nodes) and then collapsed into a 'Graph' which only has a single edge between any two nodes. Information is preserved by aggregating the count of those edges as a 'weight' value. Set this value to False to return the MultiGraph. Note: this can cause the size of the graph to expand significantly since each row can potentially have n! edges where n is the number of columns in the dataframe. :param str edge_agg_key: A string that sets the key the edge count will be assigned to when edges are aggregated. :returns: A networkx Graph (or MultiGraph if collapse_edges is set to False). """ assert isinstance( dataframe, pd.DataFrame), "{} is not a pandas DataFrame".format(dataframe) M = MultiGraph() # if explicit specification of node_id_columns is provided, use those if len(node_id_columns) > 0: node_columns = node_id_columns else: # otherwise, compute with thresholds based on the dataframe if threshold_by_count_unique: node_columns = sorted([ col for col in dataframe.columns if dataframe[col].nunique() <= threshold_by_count_unique ]) else: node_columns = sorted([ col for col in dataframe.columns if dataframe[col].nunique() / dataframe.shape[0] <= threshold_by_percent_unique # NOQA to preserve meaningful variable names ]) # use the unique values for each node column as node types for node_type in node_columns: M.add_nodes_from([(node, { node_type_key: node_type }) for node in dataframe[node_type].unique()]) # iterate over the rows and generate an edge for each pair of node columns for i, row in dataframe.iterrows(): # assemble the edge properties as a dictionary edge_properties = {k: row[k] for k in edge_property_columns} # iterate over the node_ids in each node_column of the dataframe row node_buffer = [] for node_type in node_columns: node_id = row[node_type] # get a reference to the node and assign any specified properties node = M.nodes[node_id] for k in node_property_columns: # if values are not identical, append with a pipe delimiter if k not in node: node[k] = row[k] elif isinstance(node[k], str) and str(row[k]) not in node[k]: node[k] += "|{}".format(str(row[k])) elif str(row[k]) not in str(node[k]): node[k] = str(node[k]) + "|{}".format(str(row[k])) # build edges using precomputed edge properties for other_node_id, other_node_type in node_buffer: # sort node_type so undirected edges all share the same type ordered_name = "_".join(sorted([node_type, other_node_type])) edge_properties[edge_type_key] = ordered_name M.add_edge(node_id, other_node_id, **edge_properties) # store the node from this column in the buffer for future edges node_buffer.append((node_id, node_type)) if collapse_edges: # convert the MultiGraph to a Graph G = Graph(M) k = edge_agg_key # preserve the edge count as a sum of the weight values for u, v, data in M.edges(data=True): w = data[k] if k in data else 1.0 edge = G[u][v] edge[k] = (w + edge[k]) if k in edge else w return G return M
import pandas as pd from networkx import draw, Graph, MultiGraph from matplotlib.pyplot import show graph = MultiGraph() #Reading the excel file #Setting index_col = 0, which takes first column as index with open('excel_files', 'r') as ef: for each_excel_file in ef: each_excel_file = each_excel_file.strip() excel_file_object = pd.read_excel( r'C:\\Network Programmability and Automation\network_diagram\\' + each_excel_file) #print(excel_file_object) #filter specific columns from excel sheet remote_device_id = pd.DataFrame(excel_file_object, columns=['remote device-Id']) Local_device_id = pd.DataFrame(excel_file_object, columns=['Local device-Id']) #print(list(remote_device_id)) #print('\n\n') #print(remote_device_id) #print('\n\n\n') #print(Local_device_id) #print('test') #print() local_device_name = Local_device_id.values[0][0]