def opt_simple_model_para(graph: nx.DiGraph, alloc_map, config: Config) -> None: graph.add_node(PNO_GRAPH_HEAD_ID) graph.add_edge(PNO_GRAPH_HEAD_ID, 0) graph.nodes[PNO_GRAPH_HEAD_ID]["node"] = Node(-1, ops.O2P_GRAPH_HEAD, {}, {}, {}, 0) cuda_devices = get_valid_cuda_devices() num_cuda = len(cuda_devices) total_nodes = graph.number_of_nodes() # HACK split_len = total_nodes // 5 for gnode in graph.nodes: node = graph.nodes[gnode]["node"] if node.device_type == "gpu": node.device_id = node.node_id // split_len # HACKY SAFETY if node.device_id > num_cuda - 1: node.device_id = num_cuda - 1 return
def _transitive_closure(def_: Definition, graph: networkx.DiGraph, result: networkx.DiGraph, visited: Optional[Set[Definition]] = None): """ Returns a joint graph that comprises the transitive closure of all defs that `def_` depends on and the current graph `result`. `result` is updated. """ if def_ in self._transitive_closures.keys(): closure = self._transitive_closures[def_] # merge closure into result result.add_edges_from(closure.edges()) return result predecessors = list(graph.predecessors(def_)) result.add_node(def_) result.add_edges_from( list( map(lambda e: (*e, graph.get_edge_data(*e)), map(lambda p: (p, def_), predecessors)))) visited = visited or set() visited.add(def_) predecessors_to_visit = set(predecessors) - set(visited) closure = reduce( lambda acc, def0: _transitive_closure( def0, graph, acc, visited), predecessors_to_visit, result) self._transitive_closures[def_] = closure return closure
def graph(self) -> DiGraph: """ Get a graph of the job indicating the inputs to the job. Returns ------- DiGraph The graph showing the connectivity of the jobs. """ from networkx import DiGraph edges = [] for uuid, refs in self.input_references_grouped.items(): properties: list[str] | str = [ ref.attributes_formatted[-1] .replace("[", "") .replace("]", "") .replace(".", "") for ref in refs if ref.attributes ] properties = properties[0] if len(properties) == 1 else properties properties = properties if len(properties) > 0 else "output" edges.append((uuid, self.uuid, {"properties": properties})) graph = DiGraph() graph.add_node(self.uuid, job=self, label=self.name) graph.add_edges_from(edges) return graph
def extend_refinements_graph( g: networkx.DiGraph, stmt: Statement, less_specifics: List[int], matches_fun: Optional[Callable[[Statement], str]] = None, ) -> networkx.DiGraph: """Extend refinements graph with a new statement and its refinements. Parameters ---------- g : A refinements graph to be extended. stmt : The statement to be added to the refinements graph. less_specifics : A list of statement hashes of statements that are refined by this statement (i.e., are less specific versions of it). matches_fun : An optional function to calculate the matches key and hash of a given statement. Default: None """ sh = stmt.get_hash(matches_fun=matches_fun) g.add_node(sh, stmt=stmt) for less_spec in less_specifics: g.add_edge(less_spec, sh) return g
def get_node_by_uuid_web(self, uuid, json_out=True): """ Returns a networkx graph containing matching node. :param uuid: The node name. :return: Graph containing node or none. """ graph = DiGraph() node_query = "match (i)-[r:STATE]->(s) where i.name='{}' and r.to>{}" \ " return i, s".format(uuid, str(time.time())) query_result = self.graph_db.run(node_query) result = list(query_result) if result: records = result[0] node = dict(records[0]) state_attrs = dict(records[1]) if 'geo' in state_attrs: state_attrs['geo'] = json.loads(state_attrs['geo']) state_attributes = self._unique_attribute_names( IDEN_PROPS, state_attrs, node["type"]) node.update(state_attributes) graph.add_node(uuid, node) if json_out: graph = json.dumps(json_graph.node_link_data(graph)) return graph return None
def get_node_by_properties_web(self, properties, start=None, timeframe=0): """ Return a list of nodes which match the given properties. :param properties: A tuple with the key, value. Example: (k, v) :return: A graph of matching nodes """ start = start or time.time() if not properties: return None start = int(float(start)) timeframe = int(float(timeframe)) end = start + timeframe # Build conditional query conditional_query = "" property_key = properties[0] property_value = properties[1] propery_operator = "=" condition = '(n.{0}{1}"{2}" OR s.{0}{1}"{2}")'.format( property_key, propery_operator, property_value) conditional_query += condition query = 'match (n)-[r:STATE]->(s) where {0} ' \ 'AND (r.from <= {1} AND r.to > {2}) return n, s'\ .format(conditional_query, start, end) graph = DiGraph() for id_node, state_node in self.graph_db.run(query): node = dict(id_node) state_attributes = self._unique_attribute_names( IDEN_PROPS, dict(state_node), node["type"]) node.update(state_attributes) graph.add_node(node["name"], node) graph_json = json.dumps(json_graph.node_link_data(graph)) return graph_json
def build_component_dependency_graph( pipeline_definition: Dict[str, Any], component_definitions: Dict[str, Any]) -> DiGraph: """ Builds a dependency graph between components. Dependencies are: - referenced components during component build time (e.g. init params) - predecessor components in the pipeline that produce the needed input This enables sorting the components in a working and meaningful order for instantiation using topological sorting. :param pipeline_definition: the definition of the pipeline (e.g. use get_pipeline_definition() to obtain it) :param component_definitions: the definition of the pipeline components (e.g. use get_component_definitions() to obtain it) """ graph = DiGraph() for node in pipeline_definition["nodes"]: node_name = node["name"] graph.add_node(node_name) for input in node["inputs"]: if input in component_definitions: graph.add_edge(input, node_name) for component_name, component_definition in component_definitions.items(): params = component_definition.get("params", {}) referenced_components: List[str] = list() for param_value in params.values(): # Currently we don't do any additional type validation here. if param_value in component_definitions: referenced_components.append(param_value) for referenced_component in referenced_components: graph.add_edge(referenced_component, component_name) return graph
def compute_dependent_cohorts(self, objects, deletion): model_map = defaultdict(list) n = len(objects) r = range(n) indexed_objects = zip(r, objects) mG = self.model_dependency_graph[deletion] oG = DiGraph() for i in r: oG.add_node(i) for v0, v1 in mG.edges(): try: for i0 in range(n): for i1 in range(n): if i0 != i1: if not deletion and self.concrete_path_exists( objects[i0], objects[i1]): oG.add_edge(i0, i1) elif deletion and self.concrete_path_exists( objects[i1], objects[i0]): oG.add_edge(i0, i1) except KeyError: pass components = weakly_connected_component_subgraphs(oG) cohort_indexes = [reversed(topological_sort(g)) for g in components] cohorts = [[objects[i] for i in cohort_index] for cohort_index in cohort_indexes] return cohorts
def find_connected(): for stemmer in [ 'Lancaster', 'WordNetLemmatizer', 'PorterStemmer', 'SnowballStemmer' ]: with open('output_files/window50/%s_dice_filtered.txt' % stemmer, 'r') as sin, \ open('output_files/window50/%s_dice_connected.txt' % stemmer, 'w') as out, \ open('output_files/window50/%s_dice_sconnected.txt' % stemmer, 'w') as out2: print('building clusters for %s' % stemmer) g = DiGraph() g2 = Graph() nodes = set() edges = set() for line in sin: stemC1C2, dice = line.rstrip().split(' ') stem, c1, c2 = stemC1C2.split(',') nodes.add(stem) nodes.add(c1) nodes.add(c2) edges.add((stem, c1, float(dice))) edges.add((stem, c2, float(dice))) edges.add((c1, stem, float(dice))) edges.add((c2, stem, float(dice))) for n in nodes: g.add_node(n) g2.add_node(n) for stem, term, dice in edges: g.add_edge(stem, term, weight=dice) g2.add_edge(stem, term, weight=dice) for connected in sorted(nx.connected_components(g2), key=lambda s: list(s)[0]): out.write('%s\n' % ' '.join(connected)) for connected in sorted(nx.strongly_connected_components(g), key=lambda s: list(s)[0]): out2.write('%s\n' % ' '.join(connected))
def build_graph(alternatives, outranking, credibility=False): """There are some conventions to follow in this function: 1. labels (i.e. alternatives' ids) are kept in graph's dictionary (see: graph.graph) 2. aggregated nodes (only numbers, as list) are kept under 'aggr' key in node's dict (see: graph.nodes(data=True)) 3. weights on the edges are kept under 'weight' key in edge's dict - similarly as with nodes (see: graph.edges(data=True)) """ graph = DiGraph() # we need directed graph for this # creating nodes... for i, alt in enumerate(alternatives): graph.add_node(i) graph.graph.update({i: alt}) # creating edges... for i, alt in enumerate(alternatives): relations = outranking.get(alt) if not relations: # if graph is built from intersectionDistillation continue for relation in relations.items(): if relation[1] == 1.0: weight = credibility[alt][relation[0]] if credibility else None graph.add_edge(i, alternatives.index(relation[0]), weight=weight) return graph
def save(self, filename: str): """ Save graph to GEXF file. :param filename: The filename """ graph = DiGraph(mode="dynamic") for doc_id, doc in self.documents.items(): graph.add_node( doc_id, label=doc_id[:7], start=doc[0], end=doc[1], cluster=doc[3], # [(value, start, end), ...] title=doc[4], viz={"position": { "x": doc[2][0], "y": doc[2][1], "z": 0 }}) # Add edges (customer assignments in dd-CRP) for linked_doc_id, spells in doc[5].items(): graph.add_edge( doc_id, linked_doc_id, spells=spells # [(start, end), ...] ) write_gexf(graph, filename)
def add_proposals_and_relationships_to_network( n: nx.DiGraph, proposals: int, funding_pool: float, token_supply: float) -> nx.DiGraph: participant_count = len(n) for i in range(proposals): j = participant_count + i n.add_node(j, type="proposal", conviction=0, status="candidate", age=0) r_rv = gamma.rvs(3, loc=0.001, scale=10000) n.nodes[j]['funds_requested'] = r_rv n.nodes[j]['trigger'] = trigger_threshold(r_rv, funding_pool, token_supply) for i in range(participant_count): n.add_edge(i, j) rv = np.random.rand() a_rv = 1 - 4 * (1 - rv) * rv #polarized distribution n.edges[(i, j)]['affinity'] = a_rv n.edges[(i, j)]['tokens'] = 0 n.edges[(i, j)]['conviction'] = 0 n.edges[(i, j)]['type'] = 'support' # Conflict Rate is a potential variable to optimize # Relative Influence is a potential variable to optimize n = initial_conflict_network(n, rate=.25) n = initial_social_network(n, scale=1) return n
def _abstract_acyclic_region(self, graph: networkx.DiGraph, region, frontier, dummy_endnode=None, secondary_graph=None): in_edges = self._region_in_edges(graph, region, data=True) out_edges = self._region_out_edges(graph, region, data=True) nodes_set = set() for node_ in list(region.graph.nodes()): nodes_set.add(node_) if node_ is not dummy_endnode: graph.remove_node(node_) graph.add_node(region) for src, _, data in in_edges: if src not in nodes_set: graph.add_edge(src, region, **data) for _, dst, data in out_edges: if dst not in nodes_set: graph.add_edge(region, dst, **data) if frontier: for frontier_node in frontier: if frontier_node is not dummy_endnode: graph.add_edge(region, frontier_node) if secondary_graph is not None: self._abstract_acyclic_region(secondary_graph, region, {})
def test_format_attributes() -> None: """ Testing attribute annotation in xgmml. Returns: None Raises: AssertionError if an expected attribute type is not within the node attributes. """ graph = DiGraph() graph.add_node('A', str_value='A', int_value=1, float_value=1.0) format_attribute_system_str = XGMML(graph, "graph").to_string() expected_attribute_type = ['integer', 'double', 'string'] xmldoc_actual = minidom.parseString(format_attribute_system_str) actual_node_attribute_types = [ _get_node_att_type(xmldoc_actual.getElementsByTagName('node')[0], attribute_name=attribute) for attribute in ['int_value', 'float_value', 'str_value'] ] while expected_attribute_type: attribute_type = expected_attribute_type.pop() assert attribute_type in actual_node_attribute_types
def test_graph_schema_verification_throws_error_when_attributes_missing_from_stops(): g = DiGraph() g.add_node('1', x=1, y=2) with pytest.raises(ScheduleElementGraphSchemaError) as e: verify_graph_schema(g) assert 'missing the following attributes' in str(e.value)
def test_remove_node(): mock_mapp = DiGraph() mock_mapp.add_node('X') mock_mapp.add_edges_from([('A', 'B', {'TP': ['X']}), ('B', 'C', {'TP': ['Y']})]) MapGraph.remove_node.im_func(mock_mapp, 'X') nt.assert_equal(mock_mapp.edges(), [('B', 'C')])
def select_binary_groups(groups, root=desc.root): def get_bin_group(grps, bin_grps, rt, graph): level = grps.successors(rt) if not level: bin_grps.append(graph) return bin_grps elif len(level) == 2: graph.add_nodes_from(level) graph.add_edge(rt, level[0], grps.get_edge_data(rt, level[0])) graph.add_edge(rt, level[1], grps.get_edge_data(rt, level[1])) if grps.successors(level[0]): get_bin_group(grps, bin_grps, rt=level[0], graph=graph) else: get_bin_group(grps, bin_grps, rt=level[1], graph=graph) else: level.sort() for i in xrange(0, len(level), 2): g = DiGraph(graph) g.add_nodes_from([level[i], level[i + 1]]) g.add_edge(rt, level[i], grps.get_edge_data(rt, level[i])) g.add_edge(rt, level[i + 1], grps.get_edge_data(rt, level[i + 1])) if grps.successors(level[i]): get_bin_group(grps, bin_grps, rt=level[i], graph=g) else: get_bin_group(grps, bin_grps, rt=level[i + 1], graph=g) dg = DiGraph() dg.add_node(root) bin_groups = [] get_bin_group(groups, bin_grps=bin_groups, rt=root, graph=dg) return bin_groups
def add_node(self, hwaddr, ip=None, dpid=None, conn=None, ports=None, fdb=None, t_stamp=0, latency=0): if ports is None: ports = [] if fdb is None: fdb = {} ### added timestamp and latency variables #DiGraph.add_node(self, hwaddr, ip=ip, dpid=dpid, conn=conn, # ports=ports, fdb=fdb, name=hwaddr[12:] ) ### added timestamp and latency variables DiGraph.add_node(self, hwaddr, ip=ip, dpid=dpid, conn=conn, ports=ports, fdb=fdb, name=hwaddr[12:], t_stamp=t_stamp, latency=latency) # update time stamp self.time_stamp += 1
def _analyze(rules: List[Rule]) -> List[List[Rule]]: # build rule dependency graph occ: Dict[Atom, Set[RuleIndex]] = {} dep_graph = DiGraph() for u, rule in enumerate(rules): dep_graph.add_node(u) for lit in rule.body: occ.setdefault(abs(lit), set()).add(u) for u, rule in enumerate(rules): atm, = rule.head for v in occ.get(atm, []): dep_graph.add_edge(u, v) sccs = list(strongly_connected_components(dep_graph)) # build scc dependency graph # (this part only exists because the networkx library does not document the # order of components; in principle, the tarjan algorithm guarentees a # topological order) scc_rule: Dict[RuleIndex, RuleIndex] = {} scc_graph = DiGraph() for i, scc in enumerate(sccs): scc_graph.add_node(i) for u in scc: scc_rule[u] = i for i, scc in enumerate(sccs): for u in scc: for v in dep_graph[u]: j = scc_rule[u] if i != j: scc_graph.add_edge(i, j) return [[rules[j] for j in sccs[i]] for i in topological_sort(scc_graph)]
def duplicate_nodes(graph: nx.DiGraph, nodes: Set[str]) -> Dict: """ Replicates nodes of a graph. :param graph: graph used to replicate and attach new nodes. :type graph: networkX DiGraph :param nodes: nodes to be replicated. :type nodes: Set[str]. :return: the new nodes replicated. :rtype: Dict[str]. """ new_nodes = {} for node in nodes: new_node = f"{node}_{uuid4()}" graph.add_node(new_node, **graph.nodes[node]) nx.set_node_attributes(graph, {new_node: node}, "duplicate_of") new_nodes[node] = new_node for node, new_node in new_nodes.items(): for parent, _ in graph.in_edges(node): if parent in new_nodes: graph.add_edge(new_nodes[parent], new_node) else: graph.add_edge(parent, new_node) for _, child in graph.out_edges(node): if child in new_nodes: graph.add_edge(new_node, new_nodes[child]) else: graph.add_edge(new_node, child) return new_nodes
def test_graph_schema_verification_throws_error_when_routes_missing(): g = DiGraph() g.add_node('1', x=1, y=2, id='1', epsg='epsg:27700') with pytest.raises(ScheduleElementGraphSchemaError) as e: verify_graph_schema(g) assert 'Graph is missing `routes` attribute' in str(e.value)
def main(): # Create an example Boolean circuit. # # This circuit has a ∧ at the output and two ∨s at the next layer. # The third layer has a variable x that appears in the left ∨, a # variable y that appears in both the left and right ∨s, and a # negation for the variable z that appears as the sole node in the # fourth layer. circuit = DiGraph() # Layer 0 circuit.add_node(0, label='∧') # Layer 1 circuit.add_node(1, label='∨') circuit.add_node(2, label='∨') circuit.add_edge(0, 1) circuit.add_edge(0, 2) # Layer 2 circuit.add_node(3, label='x') circuit.add_node(4, label='y') circuit.add_node(5, label='¬') circuit.add_edge(1, 3) circuit.add_edge(1, 4) circuit.add_edge(2, 4) circuit.add_edge(2, 5) # Layer 3 circuit.add_node(6, label='z') circuit.add_edge(5, 6) # Convert the circuit to an equivalent formula. formula = circuit_to_formula(circuit) print(formula_to_string(formula))
def lazy_load_trees(skeleton_ids, node_properties): """ Return a lazy collection of pairs of (long, DiGraph) representing (skeleton_id, tree). The node_properties is a list of strings, each being a name of a column in the django model of the Treenode table that is not the treenode id, parent_id or skeleton_id. """ values_list = ('id', 'parent_id', 'skeleton_id') props = tuple(set(node_properties) - set(values_list)) values_list += props ts = Treenode.objects.filter(skeleton__in=skeleton_ids) \ .order_by('skeleton') \ .values_list(*values_list) skid = None tree = None for t in ts: if t[2] != skid: if tree: yield (skid, tree) # Prepare for the next one skid = t[2] tree = DiGraph() fields = {k: v for k, v in izip(props, islice(t, 3, 3 + len(props)))} tree.add_node(t[0], fields) if t[1]: # From child to parent tree.add_edge(t[0], t[1]) if tree: yield (skid, tree)
def lazy_load_trees(skeleton_ids, node_properties): """ Return a lazy collection of pairs of (long, DiGraph) representing (skeleton_id, tree). The node_properties is a list of strings, each being a name of a column in the django model of the Treenode table that is not the treenode id, parent_id or skeleton_id. """ values_list = ('id', 'parent_id', 'skeleton_id') props = tuple(set(node_properties) - set(values_list)) values_list += props ts = Treenode.objects.filter(skeleton__in=skeleton_ids) \ .order_by('skeleton') \ .values_list(*values_list) skid = None tree = None for t in ts: if t[2] != skid: if tree: yield (skid, tree) # Prepare for the next one skid = t[2] tree = DiGraph() fields = {k: v for k,v in izip(props, islice(t, 3, 3 + len(props)))} tree.add_node(t[0], fields) if t[1]: # From child to parent tree.add_edge(t[0], t[1]) if tree: yield (skid, tree)
def test_get_paths_self_edge(self): expected = [[1]] graph = DiGraph() graph.add_node(1) graph.add_edge(1, 1) actual = get_paths(graph) self.assertEqual(expected, actual)
def z_to_rz(gate: Gate) -> Optional[DiGraph]: if gate._gate != 'Z': return None else: graph = DiGraph() graph.add_node(Gate('RZ', gate._qubits, 'pi')) return graph
def compute_dependent_cohorts(self, objects, deletion): model_map = defaultdict(list) n = len(objects) r = range(n) indexed_objects = zip(r, objects) mG = self.model_dependency_graph[deletion] oG = DiGraph() for i in r: oG.add_node(i) for v0, v1 in mG.edges(): try: for i0 in range(n): for i1 in range(n): if i0 != i1: if not deletion and self.concrete_path_exists( objects[i0], objects[i1]): oG.add_edge(i0, i1) elif deletion and self.concrete_path_exists(objects[i1], objects[i0]): oG.add_edge(i0, i1) except KeyError: pass components = weakly_connected_component_subgraphs(oG) cohort_indexes = [reversed(topological_sort(g)) for g in components] cohorts = [[objects[i] for i in cohort_index] for cohort_index in cohort_indexes] return cohorts
class KTN(): _topology = None _system = None network = DiGraph() def __init__(self, topology, system): self._topology = topology self._system = system def reset(self): self.network = DiGraph() def add_transition(self, origin=None, end=None): if origin not in self.network: self.network.add_node(origin) if end in self.network[origin]: self.network[origin][end]['weight'] += 1 else: self.network.add_edge(origin, end, weight=1)
def _add_node(self, g: nx.DiGraph, node: Task) -> None: # type: ignore[override] """ Recursively add a node and all of its children to a given graph. This function is used when creating the nx.DiGraph representation of the pipeline to add all the tasks to the graph. As additional information, a "fillcolor" attribute is set according to the task's status (see :py:attr:`~Pipeline.colormap`). This is used when creating a PNG image of the pipeline via :py:meth:`~DAG.plot_graph`. Args: g: Graph to add the node to node: Node to add Returns: """ child: Task g.add_node( node, style="filled", fillcolor=self.colormap[node.status], label=node.name ) for child in node.children: # type: ignore if not g.has_node(child): self._add_node(g, child) g.add_edge(node, child)
def _merge_nodes(self, graph: networkx.DiGraph, node_a, node_b, force_multinode=False): # pylint:disable=no-self-use in_edges = list(graph.in_edges(node_a, data=True)) out_edges = list(graph.out_edges(node_b, data=True)) if not force_multinode and len(in_edges) <= 1 and len(out_edges) <= 1: # it forms a region by itself :-) new_node = None else: new_node = MultiNode([node_a, node_b]) graph.remove_node(node_a) graph.remove_node(node_b) if new_node is not None: graph.add_node(new_node) for src, _, data in in_edges: if src is node_b: src = new_node graph.add_edge(src, new_node, **data) for _, dst, data in out_edges: if dst is node_a: dst = new_node graph.add_edge(new_node, dst, **data) assert not node_a in graph assert not node_b in graph
def _dict_to_graph(data: Dict[str, Any]) -> Tuple[DiGraph, Dict[str, int]]: """Convert dictionary representation of the graph to a directed graph. :param data: graph as a dictionary :return: directed graph """ graph = DiGraph() node2id = {} for node, properties in data['node_list'].items(): node2id[node] = properties['id'] graph.add_node(int(properties['id']), name=node, isTarget=bool(properties['isTarget'])) for node, adj in data['adj_list'].items(): source = int(node) increases = adj.get('increases', []) decreases = adj.get('decreases', []) for n in increases: graph.add_edge(source, n, polarity=1) for n in decreases: graph.add_edge(source, n, polarity=-1) return graph, node2id
def dump(nodes, weighted_edges, white, black, flatten_colours, flatten_weights): graph = DiGraph() for node in nodes: graph.add_node(node.id, label=node.name) (min_weight, max_weight) = log_range('Weight', map(lambda we: we[0], weighted_edges)) if flatten_colours or flatten_weights: flattened_weights = flatten( dict((weight, weight) for (weight, edge) in weighted_edges)) for (weight, edge) in weighted_edges: colour = flattened_weights[weight] if flatten_colours else ( weight - min_weight) / (max_weight - min_weight) weight = flattened_weights[weight] if flatten_weights else weight graph.add_edge(edge.from_.id, edge.to_.id, weight=weight, viz=rgb(colour, white, black)) #write_graphml(graph, 'uykfe.graphml') write_gexf(graph, 'uykfe.gexf') # fix namespace bug with open('uykfe.gexf') as input: xml = input.read() xml = xml.replace('xmlns:viz="http://www.gexf.net/1.1draft/viz" ', '', 1) with open('uykfe.gexf', 'w') as output: xml = output.write(xml)
def generate(self): g = DiGraph() g.add_node("start", serverport=self.tgen_port, loglevel="info", heartbeat="1 minute") return g
def src_snk(pathway: nx.DiGraph,labels: pd.DataFrame,verbose=True) -> nx.DiGraph: """ :pathway graph :labels dataframe of labels :returns graph with source and sink added """ pathway.add_node('SRC') pathway.add_node('SNK') #get source nodes try: sources = list(labels[labels['Node type'] == 'source']['#Node']) except: sources = list(labels[labels['node_symbol'] == 'receptor']['#node']) if verbose: print('sources: {}'.format(sources)) for s in sources: pathway.add_edge('SRC',s) #get sink nodes try: sinks = list(labels[labels['Node type'] == 'target']['#Node']) except: sinks = list(labels[labels['node_symbol'] == 'tf']['#node']) if verbose: print('sinks: {}'.format(sinks)) for s in sinks: pathway.add_edge(s,'SNK') return pathway
def addCell(self, cell, gateName=None): mod = self.__nl.mods[self.__nl.topMod] yaml = self.__nl.yaml self.__cells.add(cell) name = gateName if gateName else mod.cells[cell].submodname if "clocks" in yaml[name]: self.__flops.add(cell) digraph.add_node(self, cell)
def vytvořím_networkx_graf(): from networkx import DiGraph, write_graphml graf = DiGraph() graf.add_node(1, time='5pm') graf.add_node(2) graf.add_edge(1,2, weight=25) write_graphml(graf, './data/networkx.graphml')
def get_connection_graph(names, connections): G = DiGraph() # add names to check if it is connected for n in names: G.add_node(n) for c in connections: dp1 = c.dp1 dp2 = c.dp2 G.add_edge(dp1, dp2) return G
def getStageGraph(self, phase): graph = DiGraph() for stage in self.structureInfo[phase][consts.STAGES_KEY].keys(): graph.add_node(stage) graph.node[stage]["name"] = str(stage) for edge in self.structureInfo[phase][consts.STAGE_TO_STAGE_KEY]: graph.add_edge(edge[consts.SRC_KEY], edge[consts.DEST_KEY]) return graph
def subgroup_identification(sample, mode, a_logrank=desc.a_logrank, cov_at_level=desc.cov_at_level, min_sub_size=desc.min_sub_size): subgroups = DiGraph() cov_used = {key: val for key in sample.keys() for val in [False]} subgroups.add_node(desc.root) global global_counter global_counter = 1 sub_ident(sample, subgroups, cov_used, a_logrank, cov_at_level, min_sub_size, mode, parent=desc.root, recurs_level=1) return subgroups
def createDiGraphCopy(self, geneTuples): copyGraph = DiGraph() for node in self: if len(self.getGenesByNode(node).intersection(geneTuples)) > 0 or len(self.getPropagatedGenesByNode(node).intersection(geneTuples)) > 0: copyGraph.add_node(node, {'gene':set(self.getGenesByNode(node).intersection(geneTuples)), 'propGene':set(self.getPropagatedGenesByNode(node).intersection(geneTuples)), 'pmid': set(self.getPubMedByNode(node)), 'mergeGene': set(), 'mergePMID': set(), 'mergeCount': 0, 'infoLoss': 0}) for i in copyGraph: for edge in self.edge[i]: if edge in copyGraph: copyGraph.add_edge(i, edge, self.edge[i][edge]) return copyGraph
def build_allele_graph(self, num_alleles=None): """ Method that builds segregation graph from the given pedigree. See the constructor for descriptions of parameters. Returns ------- G : networkx.DiGraph NetworkX DiGraph representation of the segregation network defined by the given pedigree and allele assignments. Nodes of the graph are named using the following conventions: allele node = id_parent_locus ex.: 1_0_0 -> paternal allele of locus 0 for ID = 1 ex.: 2_1_3 -> maternal allele of locus 3 for ID = 2 segregation node = S_parent_child_locus ex.: S_1_6_1 -> segrgation indicator of parent ID = 1 to child ID = 6 for locus 1 """ num_alleles = num_alleles if num_alleles else self.len G = DiGraph() # for each allele for i in range(num_alleles): # for each k = parent, v = [children, sex] for k,v in self.ped.pedigree.items(): #for k in self.ped.ped_graph.nodes(): # make parent nodes, one for each allele, at locus i G.add_node(self.mk_nd(k, 1, i)) G.add_node(self.mk_nd(k, 0, i)) # for each k = parent, v = [children, sex] for k,v in self.ped.pedigree.items(): # for each child of k for c in v['cs']: # determine if parent is male or female type = 0 if v['sex'] == 'm' else 1 # add edges from parent alleles to children alleles G.add_edge(self.mk_nd(k, 1, i), self.mk_nd(c, type, i)) G.add_edge(self.mk_nd(k, 0, i), self.mk_nd(c, type, i)) # add other allele of child, just in case it's not in the graph G.add_node(self.mk_nd(c, int(not type), i)) # add segregation node edge G.add_edge(self.mk_nd('S', k, c, i), self.mk_nd(c, type, i)) # if applicable, add edge between segregation indicators if i > 0: G.add_edge(self.mk_nd('S', k, c, i - 1), self.mk_nd('S', k, c, i)) # for UAI: vars are indexed by topological sort self.vars = topological_sort(G) self.var_idxs = dict([(v,i) for i,v in enumerate(self.vars)]) self.allele_graph = G return G
def __init__(self, debug=0): digraph.__init__(self) self.__debug = debug self.__inputs = set() self.__outputs = set() self.__cells = set() self.__flops = set() self.__virtual = dict() # maps name --> gate self.__pins = dict() self.__flopsIn = dict() # create input, output nodes digraph.add_node(self, "__INPUTS__") digraph.add_node(self, "__OUTPUTS__")
def _rename_nodes(self, graph): new_graph = DiGraph() # print len(self._node_names) for node in graph.nodes(): # Add all the nodes with the names given in the data set new_node_name = self._node_names[node] new_graph.add_node(new_node_name) for edge in graph.edges(): # Add all the with the names given in the data set new_source_node = self._node_names[ edge[0] ] new_destination_node = self._node_names[ edge[1] ] new_graph.add_edge(new_source_node, new_destination_node) new_graph.name = self._network_name return new_graph
def load_cliques(self,cliques): self.cliques=cliques for k in range(len(cliques)): DiGraph.add_node(self,k,keywords=cliques[k]) for i in range(len(cliques)): for j in range(i): a=set(cliques[i]) b=set(cliques[j]) common_words=len(a.intersection(b)) dis_i2j=common_words/len(a) dis_j2i=common_words/len(b) if dis_i2j>0: DiGraph.add_edge(self,i,j,weight=dis_i2j) if dis_j2i>0: DiGraph.add_edge(self,j,i,weight=dis_j2i)
def add_node(self, hwaddr, ip=None, dpid=None, conn=None, ports=None, fdb=None, t_stamp=0, latency=0 ): if ports is None: ports = [] if fdb is None: fdb = {} ### added timestamp and latency variables #DiGraph.add_node(self, hwaddr, ip=ip, dpid=dpid, conn=conn, # ports=ports, fdb=fdb, name=hwaddr[12:] ) ### added timestamp and latency variables DiGraph.add_node(self, hwaddr, ip=ip, dpid=dpid, conn=conn, ports=ports, fdb=fdb, name=hwaddr[12:], t_stamp=t_stamp, latency=latency ) # update time stamp self.time_stamp += 1
def get_tasks(do_tasks, dep_graph): """Given a list of tasks to perform and a dependency graph, return the tasks that must be performed, in the correct order""" #XXX: Is it important that if a task has "foo" before "bar" as a dep, # that foo executes before bar? Why? ATM this may not happen. #Each task that the user has specified gets its own execution graph task_graphs = [] for task in do_tasks: exgraph = DiGraph() exgraph.add_node(task) _get_deps(task, exgraph, dep_graph) task_graphs.append(exgraph) return flatten(reversed(topological_sort(g)) for g in task_graphs)
def dump(nodes, weighted_edges, white, black, flatten_colours, flatten_weights): graph = DiGraph() for node in nodes: graph.add_node(node.id, label=node.name) (min_weight, max_weight) = log_range('Weight', map(lambda we: we[0], weighted_edges)) if flatten_colours or flatten_weights: flattened_weights = flatten(dict((weight, weight) for (weight, edge) in weighted_edges)) for (weight, edge) in weighted_edges: colour = flattened_weights[weight] if flatten_colours else (weight - min_weight) / (max_weight - min_weight) weight = flattened_weights[weight] if flatten_weights else weight graph.add_edge(edge.from_.id, edge.to_.id, weight=weight, viz=rgb(colour, white, black)) #write_graphml(graph, 'uykfe.graphml') write_gexf(graph, 'uykfe.gexf') # fix namespace bug with open('uykfe.gexf') as input: xml = input.read() xml = xml.replace('xmlns:viz="http://www.gexf.net/1.1draft/viz" ', '', 1) with open('uykfe.gexf', 'w') as output: xml = output.write(xml)
def build_graph(alternatives, outranking, cut_threshold): # There are some conventions to follow here: # 1. labels (i.e. alternatives' ids) are kept in graph's dictionary (see: graph.graph) # 2. aggregated nodes (only numbers, as list) are kept under 'aggr' key in node's # dict (see: graph.nodes(data=True)) # 3. weights on the edges are kept under 'weight' key in edge's dict - similarly as # with nodes (see: graph.edges(data=True)) graph = DiGraph() # creating nodes... for i, alternative in enumerate(alternatives): graph.add_node(i) graph.graph.update({i: alternative}) # creating edges... for i, alternative in enumerate(alternatives): relations = outranking.get(alternative) if not relations: # if graph is built from intersectionDistillation continue for relation in relations.items(): if relation[1] >= cut_threshold: graph.add_edge(i, alternatives.index(relation[0]), weight=relation[1]) return graph
def make_dependency_graph(tasks): dep_graph = DiGraph() #build the dependency graph in two steps. First add the nodes for task in tasks: assert not dep_graph.has_node(task), "Cannot add duplicate task: %s" % task dep_graph.add_node(task) #then add the edges. taskdeps = [(name, task.__pub_dependencies__) for name, task in tasks.iteritems() if hasattr(task, "__pub_dependencies__")] for task, deps in taskdeps: for dep in deps: dep_graph.add_edge(task, dep) cycles = list(simple_cycles(dep_graph)) if cycles: raise DependencyCycle("Cycle in the dependency graph: %s %s" % (dep_graph, cycles)) return dep_graph
def resolve_versions(self, dependencies): # type: (ProjectIdentifier, Revision) -> [ProjectIdentifier, Tag] """Given an array of project identifier/version pairs work out the build order""" graph = DiGraph() versions_for_identifier = dict(dependencies) for identifier, version in dependencies: parent = Node(identifier, version) graph.add_node(parent) assert isinstance(version, Revision) dependencies_for_node = self._dependencies_for_node(Node(identifier, version)) for dependency, _ in dependencies_for_node: version = versions_for_identifier[dependency] child = Node(dependency, version) graph.add_edge(parent, child) build_order = topological_sort(graph, reverse=True) return build_order
def _get_category_graph(skos_categories_path): print("Computing category graph...", end=" ") category_labels = _get_labels(skos_categories_path) g = DiGraph() with open(skos_categories_path) as f: for chunks in csv.reader(f, delimiter=" "): if chunks[1] == BROADER: source, target = chunks[0], chunks[2] source, target = ( source[source.rfind(":") + 1:-1].decode("utf-8"), target[target.rfind(":") + 1:-1].decode("utf-8") ) # The original relation is "broader", but for our purposes # "wider" is more appropriate. g.add_edge(category_labels[target], category_labels[source]) elif chunks[1] == TYPE: source = chunks[0] source = source[source.rfind(":") + 1:-1].decode("utf-8") g.add_node(category_labels[source]) print("done") return g
def spanning_tree(tree, preserve): """ Return a new DiGraph with the spanning tree including the desired nodes. preserve: the set of nodes that delimit the spanning tree. """ spanning = DiGraph() preserve = set(preserve) # duplicate, will be altered if 1 == len(preserve): spanning.add_node(iter(preserve).next()) return spanning if len(tree.successors(find_root(tree))) > 1: tree = tree.copy() # First end node found endNode = (node for node in tree if not next(tree.successors_iter(node), None)).next() reroot(tree, endNode) n_seen = 0 # Start from shortest sequence for seq in sorted(partition(tree), key=len): path = [] for node in seq: if node in preserve: path.append(node) if node not in spanning: n_seen += 1 if len(preserve) == n_seen: break elif path: path.append(node) if path: spanning.add_path(path) if seq[-1] == path[-1]: preserve.add(path[-1]) if len(preserve) == n_seen: break return spanning
def example_invalid_subtrees(): is1 = DiGraph() is1.add_node('NP-2') is1.add_edges_from([('D', 'the'), ('N-2', 'apple')]) is2 = DiGraph() is2.add_node('D') is3 = DiGraph() is3.add_nodes_from(['D', 'N-2']) is4 = DiGraph() is4.add_edges_from([('NP-2', 'D'), ('D', 'the')]) is5 = DiGraph() is5.add_nodes_from(['the']) is6 = DiGraph() is6.add_nodes_from(['the', 'apple']) is7 = DiGraph() is7.add_edges_from([('the', 'apple')]) return [is1, is2, is3, is4, is5, is6, is7]
class PipelineFramework(object): """A PipelineFramework is a set Tasks that may have data dependencies.""" def __init__(self, tasks_reqs): """Construct a PipelineFramework based on the given Tasks and their requirements. A PipelineFramework is the structure of the pipeline, it contains no patient data. :param tasks_reqs: the Tasks and their requirements :type tasks_reqs: iterable of tuples, each with a Task and its list of required UIDs :raises: ValueError """ self.dag = DiGraph() task_dict = {} for task, _ in tasks_reqs: if task_dict.get(task._uid) is not None: raise ValueError("Pipeline contains duplicate Task {}".format(task._uid)) self.dag.add_node(task, done=False) task_dict[task._uid] = task for task, reqs in tasks_reqs: for req_uid in reqs: uid = task_dict.get(req_uid) if uid is None: raise KeyError("Unknown UID {} set as requirement for {}".format(req_uid, task._uid)) self.dag.add_edge(uid, task) if not is_directed_acyclic_graph(self.dag): raise ValueError("Pipeline contains a cycle.") def __len__(self): """Determine the length of the Pipeline. :returns: the number of Tasks in this Pipeline :rtype: {int} """ return self.dag.__len__()
def compute_dependent_cohorts(self, objects, deletion): n = len(objects) r = list(range(n)) oG = DiGraph() for i in r: oG.add_node(i) try: for i0 in range(n): for i1 in range(n): if i0 != i1: if deletion: path_args = (objects[i1], objects[i0]) else: path_args = (objects[i0], objects[i1]) is_connected, edge_type = self.concrete_path_exists(*path_args) if is_connected: try: edge_type = oG[i1][i0]["type"] if edge_type == PROXY_EDGE: oG.remove_edge(i1, i0) oG.add_edge(i0, i1, type=edge_type) except KeyError: oG.add_edge(i0, i1, type=edge_type) except KeyError: pass components = weakly_connected_component_subgraphs(oG) cohort_indexes = [reversed(list(topological_sort(g))) for g in components] cohorts = [ [objects[i] for i in cohort_index] for cohort_index in cohort_indexes ] return cohorts
def lazy_load_trees(skeleton_ids, node_properties): """ Return a lazy collection of pairs of (long, DiGraph) representing (skeleton_id, tree). The node_properties is a list of strings, each being a name of a column in the django model of the Treenode table that is not the treenode id, parent_id or skeleton_id. """ values_list = ("id", "parent_id", "skeleton_id") props = tuple(set(node_properties) - set(values_list)) values_list += props ts = Treenode.objects.filter(skeleton__in=skeleton_ids).order_by("skeleton").values_list(*values_list) skid = None tree = None for t in ts: if t[2] != skid: if tree: yield (skid, tree) # Prepare for the next one skid = t[2] tree = DiGraph() props = {k: v for k, v in izip(props, islice(t, 3, 3 + len(props)))} # Hack: why doesn't django parse well the location? loc = props.get("location") if loc: props["location"] = Double3D(*(imap(float, loc[1:-1].split(",")))) tree.add_node(t[0], props) if t[1]: # From child to parent tree.add_edge(t[0], t[1]) if tree: yield (skid, tree)
class NetworkXKeyedGraph(AbstractKeyedGraph): def __init__(self): self.__nx_graph = DiGraph() self.__nodes = {} def source(self): return self.__nx_graph def add_node(self, key, **kw): if self.__nodes.has_key(key): node = self.__nodes[key] else: node = NetworkXNode(key, self.__nx_graph) self.__nodes[key] = node self.__nx_graph.add_node(node, **kw) return node def find_node_by_key(self, key): return self.__nodes.get(key, None) def iterator(self): class Iterator(AbstractGraphIterator): def __init__(self, nx_graph): self.__iterator = nx_graph.nodes_iter() def next(self): return self.__iterator.next() return Iterator(self.__nx_graph) def size(self): return len(self.__nx_graph) def __repr__(self): return str(dict(id=hash(self), size=self.size()))
def generate_tgen_filetransfer_clients(servers): # webclients G = DiGraph() G.add_node("start", socksproxy="localhost:9000", serverport="8888", peers=servers) G.add_node("transfer", type="get", protocol="tcp", size="320 KiB") G.add_node("pause", time="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59,60") G.add_edge("start", "transfer") G.add_edge("transfer", "pause") G.add_edge("pause", "start") write_graphml(G, "conf/tgen.torwebclient.graphml.xml") # bulkclients G = DiGraph() G.add_node("start", socksproxy="localhost:9000", serverport="8888", peers=servers) G.add_node("transfer", type="get", protocol="tcp", size="5 MiB") G.add_edge("start", "transfer") G.add_edge("transfer", "start") write_graphml(G, "conf/tgen.torbulkclient.graphml.xml")