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 convert_nodes( cls, node_uris: List[URIRef], rdf_graph: RDFGraph, namespaces: Union[NamespaceManager, Dict[str, Union[str, Namespace, URIRef]]] = None, strict: bool = False, ) -> MultiDiGraph: """Converts a set of RDF nodes into a set of LPG nodes (networkx). :param node_uris: a list of node URIs to be converted :param rdf_graph: the rdflib.graph.Graph containing the raw data :param namespaces: the collection of namespaces used to simplify URIs :param strict: boolean for Literal conversion (True = supported types only) :return: the networkx MultiDiGraph containing all collected node/edge data """ if cls.initNs is None: if namespaces is None: namespaces = dict(rdf_graph.namespaces()) cls.initNs = dict(namespaces) nx_graph = MultiDiGraph() # TODO pass list of nodes to this function # TODO add edges for these nodes nodes = cls.transform_nodes(rdf_graph, node_iris=node_uris, strict=strict) for node_iri, node_attrs in nodes.items(): nx_graph.add_node(node_iri, **node_attrs) return nx_graph
def __init__(self, graph: MultiDiGraph = None, callbacks=None, label_max_length=DEFAULT_LABEL_MAX_LENGTH, group_by_property=LABEL_KEY, display_property=LABEL_KEY, edge_display_property=EDGE_TYPE_KEY, ignore_groups=False): if graph is None: graph = MultiDiGraph() if label_max_length < 3: self.label_max_length = 3 else: self.label_max_length = label_max_length try: self.group_by_property = json.loads(group_by_property) except ValueError: self.group_by_property = group_by_property try: self.display_property = self.convert_multiproperties_to_tuples( json.loads(display_property)) except ValueError: self.display_property = self.convert_multiproperties_to_tuples( display_property) try: self.edge_display_property = self.convert_multiproperties_to_tuples( json.loads(edge_display_property)) except ValueError: self.edge_display_property = self.convert_multiproperties_to_tuples( edge_display_property) self.ignore_groups = ignore_groups super().__init__(graph, callbacks)
def __init__(self, tree_name: str = None): self.graph = MultiDiGraph() self.name: str = tree_name self.ptn_to_an: Dict = {} self.an_to_ptn: Dict = {} self.an_to_sf: Dict = {} self.phylo: PantherTreePhylo = None
def EdmondsKarp(layered_network: LayeredGraph, network: Graph, s: int, t: int, capacity_label: str = 'c') -> int: # unite_sinks(network=layered_network, t=t) # united_sink = layered_network.max_node * layered_network.layers # cut_dead_ends(layered_network, s=s, t=t) source_net = MultiDiGraph() for src, dst, key, data in network.edges(data=True, keys=True): new_key = data[ArcAttrs.arc_type] source_net.add_edge(src, dst, key=new_key, **data) source_net[src][dst][new_key][ ArcAttrs.res_capacity] = data[capacity_label] flow_network = build_flow_network( layered_network) # create auxiliary network with reversed arcs while True: path = search_path(flow_network=flow_network, source_network=source_net, s=s, t=t) if path: augment(path, flow_network=flow_network, source_network=source_net) else: return gather_in_flows(node=t, flow_network=flow_network)
def calculate_dijkstra(source: int, layered_graph: LayeredGraph, ) -> Tuple[MultiDiGraph, MultiDiGraph]: node_info = {node: {COST: inf, ARC: None, VISITED: False, DEPTH: inf} for node in layered_graph} node_info[source][COST] = 0 node_info[source][DEPTH] = 0 tree = MultiDiGraph() to_visit = [] insert(to_visit, (0, source)) images = {node: [] for node in layered_graph.origin_nodes} insert(images[layered_graph.origin_node_index(source)], ((0, 0), source)) leafs = set() while len(to_visit) > 0: to_open = extract_minimum(to_visit)[1] if node_info[to_open][VISITED]: continue node_info[to_open][VISITED] = True for arc in layered_graph.out_edges(to_open, data=True): dest = arc[1] if not layered_graph.out_edges(dest): leafs.add(dest) new_cost = node_info[to_open][COST] + arc[2][WEIGHT] new_depth = node_info[to_open][DEPTH] + 1 if node_info[dest][COST] > new_cost: node_info[dest][COST] = new_cost node_info[dest][ARC] = arc node_info[dest][DEPTH] = new_depth insert(to_visit, (new_cost, dest)) insert(images[layered_graph.origin_node_index(dest)], ((new_cost, new_depth), dest)) if tree.has_edge(to_open, dest): # TODO change on link to arc tree.remove_edge(to_open, dest) tree.add_edge(to_open, dest, COST=new_cost) # node_info truncation - усечение дерева not_truncated_tree = MultiDiGraph(tree) while leafs: leaf = leafs.pop() path_cost = node_info[leaf][COST] path_depth = node_info[leaf][DEPTH] if nsmallest(1, images[layered_graph.origin_node_index(leaf)]) != ((path_cost, path_depth), leaf): parent = node_info[leaf][ARC][0] tree.remove_node(leaf) if not tree.out_edges(parent): leafs.add(parent) return tree, not_truncated_tree
def im_json_to_graph(im_json): """Return networkx graph from Kappy's influence map JSON. Parameters ---------- im_json : dict A JSON dict which contains an influence map generated by Kappy. Returns ------- graph : networkx.MultiDiGraph A graph representing the influence map. """ # This is for kappy compatibility: as of 4.1.2, im_json is a string, # whereas before it was a json object if isinstance(im_json, str): im_json = json.loads(im_json) imap_data = im_json['influence map']['map'] # Initialize the graph graph = MultiDiGraph() id_node_dict = {} # Add each node to the graph for node_dict in imap_data['nodes']: # There is always just one entry here with the node type e.g. "rule" # as key, and all the node data as the value node_type, node = list(node_dict.items())[0] # Add the node to the graph with its label and type attrs = { 'fillcolor': '#b7d2ff' if node_type == 'rule' else '#cdffc9', 'shape': 'box' if node_type == 'rule' else 'oval', 'style': 'filled' } graph.add_node(node['label'], node_type=node_type, **attrs) # Save the key of the node to refer to it later new_key = '%s%s' % (node_type, node['id']) id_node_dict[new_key] = node['label'] def add_edges(link_list, edge_sign): attrs = { 'sign': edge_sign, 'color': 'green' if edge_sign == 1 else 'red', 'arrowhead': 'normal' if edge_sign == 1 else 'tee' } for link_dict in link_list: source = link_dict['source'] for target_dict in link_dict['target map']: target = target_dict['target'] src_id = '%s%s' % list(source.items())[0] tgt_id = '%s%s' % list(target.items())[0] graph.add_edge(id_node_dict[src_id], id_node_dict[tgt_id], **attrs) # Add all the edges from the positive and negative influences add_edges(imap_data['wake-up map'], 1) add_edges(imap_data['inhibition map'], -1) return graph
def convert_grandalf_graph_to_networkx_graph(G): from networkx import MultiDiGraph nxg = MultiDiGraph() for v in G.V(): nxg.add_node(v.data) for e in G.E(): nxg.add_edge(e.v[0].data, e.v[1].data) return nxg
def get_meausurements_graph(po: PlacedObject) -> MultiDiGraph: G = MultiDiGraph() for name, sr in iterate_measurements_relations((), po): a = sr.a b = sr.b attr_dict = dict(sr=sr) G.add_edge(a, b, attr_dict=attr_dict) return G
def __init__(self, graph: MultiDiGraph = None, callbacks=None, label_max_length=DEFAULT_LABEL_MAX_LENGTH, group_by_property=T_LABEL, ignore_groups=False): if graph is None: graph = MultiDiGraph() self.label_max_length = label_max_length self.group_by_property = group_by_property self.ignore_groups=ignore_groups super().__init__(graph, callbacks)
def __init__(self, graph: MultiDiGraph = None, callbacks: dict = None): if callbacks is None: callbacks = defaultdict(list) self.callbacks = callbacks if graph is None: graph = MultiDiGraph() super().__init__(graph)
def decomposed(self): subclass = type(self) return [ subclass(self.ring, self.left_algebra, self.right_algebra, self.left_scalar_action, self.right_scalar_action, MultiDiGraph(self.graph.subgraph(component))) for component in nx.weakly_connected_components(self.graph) ]
def __init__(self, graph: MultiDiGraph = None, callbacks=None, label_max_length=DEFAULT_LABEL_MAX_LENGTH): if graph is None: graph = MultiDiGraph() self.label_max_length = label_max_length super().__init__(graph, callbacks)
def breadth_first( source: int, layered_graph: LayeredGraph) -> Tuple[MultiDiGraph, MultiDiGraph]: node_info = { node: { mark: not_processed, depth: 0 } for node in layered_graph.nodes } image = {origin_node: [] for origin_node in layered_graph.origin_nodes} tree = MultiDiGraph() to_process = Queue() leafs = Queue() node_info[source][mark] = viewed to_process.put(source) while not to_process.empty(): to_open = to_process.get() image[layered_graph.origin_node_index(to_open)].append(to_open) arcs = layered_graph.out_edges(to_open) if arcs: for arc in arcs: dest = arc[1] if node_info[dest][mark] == not_processed: node_info[dest][mark] = viewed node_info[dest][depth] = node_info[to_open][depth] + 1 to_process.put(dest) tree.add_edge(to_open, dest) else: leafs.put(to_open) node_info[to_open][mark] = processed not_truncated_tree = MultiDiGraph(tree) while not leafs.empty(): leaf = leafs.get() images = image[layered_graph.origin_node_index(leaf)] if images[0] != leaf: leaf_source = list(tree.in_edges(leaf))[0][0] tree.remove_node(leaf) if not tree.out_edges(leaf_source): leafs.put(leaf_source) return tree, not_truncated_tree
def read_cref_table( inmap, strip_paths=["/usr", "lib/STM32Cube_FW_F4_V1.16.0"], simplify_paths=["libgcc.a", "libc_nano.a", "libnosys.a"], hide_aeabi_symbols=True ): # Helper function to simplify filenames in order to increase graph clarity def process_filename(last_module): # Strip certain file paths for key in strip_paths: if last_module.find(key) > -1: s = last_module.split("/") print(last_module + " - " + s[len(s)-1]) last_module = s[len(s)-1] # Remove symbol names (in round brackets) from certain libraries for key in simplify_paths: if last_module.find(key) > -1: s = last_module.split("(") print(last_module + " - " + s[0]) last_module = s[0] return last_module # Create new graph modules = MultiDiGraph() # Parse all table lines redundant_associations = [] while True: l = inmap.readline() words = l.split() if len(words) == 2: # New symbol A, which is defined in file B last_symbol = words[0] last_module = process_filename(words[1]) elif len(words) == 1: # One more file uses the previous symbol filename = process_filename(words[0]) # References of a file to itself are not particularly interesting if filename != last_module: if last_symbol.find("__aeabi") > -1 and hide_aeabi_symbols: t = (filename, last_module) if not (t in redundant_associations): redundant_associations.append(t) modules.add_edge(filename, last_module); else: modules.add_edge(filename, last_module, label=last_symbol); elif len(l) == 0: # End of table break # Return the generated graph return modules
def get_graphs(): g1 = MultiDiGraph() g1.name = 'Graph 1' g1.add_node('HGNC:12345', id='HGNC:12345', name='Test Gene', category=['biolink:Gene']) g1.add_node('B', id='B', name='Node B', category=['biolink:NamedThing']) g1.add_node('C', id='C', name='Node C', category=['biolink:NamedThing']) g1.add_edge('C', 'B', key='C-biolink:subclass_of-B', edge_label='biolink:sub_class_of', relation='rdfs:subClassOf') g1.add_edge('B', 'A', key='B-biolink:subclass_of-A', edge_label='biolink:sub_class_of', relation='rdfs:subClassOf', provided_by='Graph 1') return [g1]
def get_graph(): graph = MultiDiGraph() graph.add_edge('B', 'A', edge_label='biolink:sub_class_of') graph.add_edge('C', 'B', edge_label='biolink:sub_class_of') graph.add_edge('D', 'C', edge_label='biolink:sub_class_of') graph.add_edge('D', 'A', edge_label='biolink:related_to') graph.add_edge('E', 'D', edge_label='biolink:sub_class_of') graph.add_edge('F', 'D', edge_label='biolink:sub_class_of') return graph
def multi_cloud_graph(replace_on: str) -> MultiDiGraph: g = MultiDiGraph() root = "root" def add_node(node_id: str) -> None: kind = re.sub("_.*$", "", node_id) reported = { "id": f"id_{node_id}", "name": f"name_{node_id}", "kind": kind, "some": {"deep": {"nested": node_id}}, } # for sake of testing: declare parent as phantom resource phantom = {"phantom": True} if kind == "parent" else {} g.add_node( node_id, id=node_id, replace=node_id.startswith(replace_on), reported=reported, metadata=phantom, kind=kind, kinds=[kind], kinds_set={kind}, ) def add_edge(from_node: str, to_node: str, edge_type: str = EdgeType.default) -> None: key = GraphAccess.edge_key(from_node, to_node, edge_type) g.add_edge(from_node, to_node, key, edge_type=edge_type) add_node(root) for cloud_d in ["aws", "gcp"]: cloud = f"cloud_{cloud_d}" add_node(cloud) add_edge(root, cloud) for account_d in range(0, 3): account = f"account_{cloud}_{account_d}" add_node(account) add_edge(cloud, account) add_edge(account, cloud, EdgeType.delete) for region_d in ["europe", "america", "asia", "africa", "antarctica", "australia"]: region = f"region_{account}_{region_d}" add_node(region) add_edge(account, region) add_edge(region, account, EdgeType.delete) for parent_d in range(0, 3): parent = f"parent_{region}_{parent_d}" add_node(parent) add_edge(region, parent) add_edge(parent, region, EdgeType.delete) for children_d in range(0, 3): children = f"child_{parent}_{children_d}" add_node(children) add_edge(parent, children) add_edge(children, parent, EdgeType.delete) return g
def test_content_hash() -> None: # the order of properties should not matter for the content hash g = MultiDiGraph() g.add_node("1", reported={"a": {"a": 1, "c": 2, "b": 3}, "c": 2, "b": 3, "d": "foo", "z": True, "kind": "a"}) g.add_node("2", reported={"z": True, "c": 2, "b": 3, "a": {"b": 3, "c": 2, "a": 1}, "d": "foo", "kind": "a"}) access = GraphAccess(g) sha1 = node(access, "1")["hash"] # type: ignore sha2 = node(access, "2")["hash"] # type: ignore assert sha1 == sha2
def construct_multi_di_graph(edges: List[Tuple[str, str, Any]]) -> MultiDiGraph: """Constructs a multi directed graph from a list of edges. Arguments: edges: A list of tuples with entries: start, end and identifier. """ g = MultiDiGraph() for start, end, key in edges: g.add_edge(start, end, key=key) return g
def __init__(self, g_input: Graph) -> None: log.info( f"FeatureConcatenator: Initiating with a graph of {g_input.number_of_nodes()} nodes " f"and {g_input.number_of_edges()} edges" ) self.g_input = g_input self.g = MultiDiGraph(deepcopy(g_input)) self._obtain_attrs() self._init_feat_attrs()
def create_multi_collector_graph(width: int = 3) -> MultiDiGraph: graph = MultiDiGraph() def add_edge(from_node: str, to_node: str, edge_type: str = EdgeType.default) -> None: key = GraphAccess.edge_key(from_node, to_node, edge_type) graph.add_edge(from_node, to_node, key, edge_type=edge_type) def add_node(node_id: str, kind: str, replace: bool = False) -> str: reported = { **to_json(Foo(node_id)), "id": node_id, "name": node_id, "kind": kind } graph.add_node( node_id, id=node_id, reported=reported, desired={}, metadata={}, hash="123", replace=replace, kind=kind, kinds=[kind], kinds_set={kind}, ) return node_id root = add_node("root", "graph_root") for cloud_num in range(0, 2): cloud = add_node(f"cloud_{cloud_num}", "cloud") add_edge(root, cloud) for account_num in range(0, 2): aid = f"{cloud_num}:{account_num}" account = add_node(f"account_{aid}", "account") add_edge(cloud, account) add_edge(account, cloud, EdgeType.delete) for region_num in range(0, 2): rid = f"{aid}:{region_num}" region = add_node(f"region_{rid}", "region", replace=True) add_edge(account, region) add_edge(region, account, EdgeType.delete) for parent_num in range(0, width): pid = f"{rid}:{parent_num}" parent = add_node(f"parent_{pid}", "parent") add_edge(region, parent) add_edge(parent, region, EdgeType.delete) for child_num in range(0, width): cid = f"{pid}:{child_num}" child = add_node(f"child_{cid}", "child") add_edge(parent, child) add_edge(child, parent, EdgeType.delete) return graph
def sst(n): S = MultiDiGraph() morphisms = [('X' + str(m + 1), 'X' + str(m), 'd' + str(m) + str(i)) for m in range(n) for i in range(m + 2)] relations = [] if n > 1: relations = [(('X'+str(m+2),'X'+str(m),'d'+str(m+1)+str(j)+'d'+str(m)+str(i)),\ ('X'+str(m+2),'X'+str(m),'d'+str(m+1)+str(i)+'d'+str(m)+str(j-1))) for m in range(n-1) for j in range(1,m+3) for i in range(j)] S.add_edges_from(morphisms) return fic(S, relations)
def combine_dots(dotlist, mathid): g1 = drawing.nx_agraph.read_dot(TMP + dotlist[-1]) g = MultiDiGraph() for d in dotlist: g2 = drawing.nx_agraph.read_dot(TMP + d) g1 = compose(g1, g2) g.add_nodes_from(g1.nodes(data=True)) g.add_edges_from(g1.edges(data=True)) g.to_directed() g = nx_agraph.to_agraph(g) g.write(TMP + '-'.join(mathid) + '.dot')
def convert( cls, rdf_graph: RDFGraph, namespaces: Union[NamespaceManager, Dict[str, Union[str, Namespace, URIRef]]] = None, strict: bool = False, ) -> MultiDiGraph: """The main method for converting an RDF graph to a networkx representation. :param rdf_graph: the rdflib.graph.Graph containing the raw data :param namespaces: the collection of namespaces used to simplify URIs :param strict: boolean for Literal conversion (True = supported types only) :return: the networkx MultiDiGraph containing all collected node/edge data """ if cls.initNs is None: if namespaces is None: namespaces = rdf_graph.namespaces cls.initNs = dict(namespaces) assert ( "base" in cls.initNs ), "For conversion, namespaces must include a base namespace." nx_graph = MultiDiGraph() nodes = cls.transform_nodes(rdf_graph, strict=strict) edges = cls.transform_edges(rdf_graph, strict=strict) for node_iri, node_attrs in nodes.items(): nx_graph.add_node(node_iri, **node_attrs) for edge_iri, edge_data in edges.items(): try: edge_source = edge_data.get("source") edge_target = edge_data.get("target") edge_attrs = edge_data.get("attrs") except AttributeError as err: logger.warning("Unable to get expected edge attributes.") raise err if edge_source in nx_graph.nodes and edge_target in nx_graph.nodes: edge_attrs["iri"] = edge_iri nx_graph.add_edge(edge_source, edge_target, **edge_attrs) else: if edge_source not in nx_graph.nodes: logger.info( f"Edge source '{edge_source}' missing in graph. Skipping..." ) elif edge_target not in nx_graph.nodes: logger.info( f"Edge target '{edge_target}' missing in graph. Skipping..." ) return nx_graph
def __init__(self, activation_fn=None): """Initialize the NetworkXKB.""" # parameters if activation_fn is None: activation_fn = (lambda graph, mem_id: None) self.activation_fn = activation_fn # variables self.graph = MultiDiGraph() self.inverted_index = defaultdict(set) self.query_results = None self.result_index = None self.clear()
def read_cref(inmap): modules = MultiDiGraph() while True: l = inmap.readline() words = l.split() if len(words) == 2: last_symbol = words[0] last_module = words[1] elif len(words) == 1: modules.add_edge(words[0], last_module, label=last_symbol) elif len(l) == 0: break return modules
def __init__(self, **kwargs): # type: (**Any) -> None """Initialize the NetworkXLTM. Parameters: **kwargs: Arbitrary keyword arguments. """ super().__init__(**kwargs) self.graph = MultiDiGraph() self.inverted_index = defaultdict(set) # type: Dict[Hashable, Set[Hashable]] self.query_results = [] # type: List[Hashable] self.result_index = -1 self.clear()
def cyclic_multi_graph(acyclic: bool) -> MultiDiGraph: g = MultiDiGraph() g.add_nodes_from([1, 2, 3]) g.add_edge(1, 2, EdgeKey(1, 2, "default")) g.add_edge(1, 3, EdgeKey(1, 3, "default")) g.add_edge(2, 3, EdgeKey(2, 3, "default")) g.add_edge(2, 1, EdgeKey(2, 1, "delete")) g.add_edge(3, 2, EdgeKey(3, 2, "delete")) g.add_edge(3, 1, EdgeKey(3, 1, "delete")) if not acyclic: g.add_edge(3, 1, EdgeKey(3, 1, "default")) g.add_edge(1, 3, EdgeKey(1, 3, "delete")) return g
def __init__(self, graph: MultiDiGraph = None, callbacks=None, label_max_length=DEFAULT_LABEL_MAX_LENGTH, edge_label_max_length=DEFAULT_LABEL_MAX_LENGTH, group_by_property=LABEL_KEY, display_property=LABEL_KEY, edge_display_property=EDGE_TYPE_KEY, tooltip_property=None, edge_tooltip_property=None, ignore_groups=False, group_by_depth=False, group_by_raw=False): if graph is None: graph = MultiDiGraph() if group_by_depth: group_by_property = DEPTH_GRP_KEY super().__init__(graph, callbacks, label_max_length, edge_label_max_length, group_by_property, display_property, edge_display_property, tooltip_property, edge_tooltip_property, ignore_groups, group_by_raw)