Beispiel #1
0
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)
Beispiel #2
0
    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
Beispiel #3
0
    def load_dependency_graph(self):
        dep_path = Config.get("dependency_graph")
        self.log.info('Loading model dependency graph', path = dep_path)

        try:
            dep_graph_str = open(dep_path).read()

            # joint_dependencies is of the form { Model1 -> [(Model2, src_port, dst_port), ...] }
            # src_port is the field that accesses Model2 from Model1
            # dst_port is the field that accesses Model1 from Model2
            joint_dependencies = json.loads(dep_graph_str)

            model_dependency_graph = DiGraph()
            for src_model, deps in joint_dependencies.items():
                for dep in deps:
                    dst_model, src_accessor, dst_accessor = dep
                    if src_model != dst_model:
                        edge_label = {'src_accessor': src_accessor,
                                      'dst_accessor': dst_accessor}
                        model_dependency_graph.add_edge(
                            src_model, dst_model, edge_label)

            model_dependency_graph_rev = model_dependency_graph.reverse(
                copy=True)
            self.model_dependency_graph = {
                # deletion
                True: model_dependency_graph_rev,
                False: model_dependency_graph
            }
            self.log.info("Loaded dependencies", edges = model_dependency_graph.edges())
        except Exception as e:
            self.log.exception("Error loading dependency graph", e = e)
            raise e
def build_network_from_db():
    """
    Creates a new graph with data inserted from the database,
    overwrites the current graph. This function will extract all
    producers from the database and iterate through their source_ratings
    to build the global network. Therefore, the time to complete running this
    function depends on the number of producers in the database
    and the number of ratings they have set on each other.

    Returns: the global network (type NetworkX DiGraph)

    """
    
    global graph
    # Users not included in graph.
    producers = Producer.objects()
    graph = DiGraph()
    tmp = []
    for p1 in producers:    
        try:
            tmp.append(extractor.get_producer(p1.name))
        except Exception:
            pass

    for p2 in tmp:
        for k,v in p2.source_ratings.iteritems():
            graph.add_edge(p2.name, k, v)  
    
    return graph
    
    """
Beispiel #5
0
    def to_networkx(self):
        """Return a NetworkX DiGraph object representing the single linkage tree.

        Edge weights in the graph are the distance values at which child nodes
        merge to form the parent cluster.

        Nodes have a `size` attribute attached giving the number of points
        that are in the cluster.
        """
        try:
            from networkx import DiGraph, set_node_attributes
        except ImportError:
            raise ImportError('You must have networkx installed to export networkx graphs')

        max_node = 2 * self._linkage.shape[0]
        num_points = max_node - (self._linkage.shape[0] - 1)

        result = DiGraph()
        for parent, row in enumerate(self._linkage, num_points):
            result.add_edge(parent, row[0], weight=row[2])
            result.add_edge(parent, row[1], weight=row[2])

        size_dict = {parent : row[3] for parent, row in enumerate(self._linkage, num_points)}
        set_node_attributes(result, 'size', size_dict)

        return result
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
Beispiel #7
0
def filter_non_dependencies(nodes, get_deps_func):
    node_set = set(nodes)
    G = DiGraph()
    G.add_nodes_from(nodes)

    # process the edges based on the dependency function
    for n in G:
        deps = get_deps_func(n)
        logging.info('%s depends on %s' % (n, deps))
        for d in deps:
            if d in G:
                G.add_edge(n, d)



    # now filter the nodes and return them
    filtered_pkgs = {node for node, in_degree in G.in_degree_iter() if in_degree == 0}

    # now find any strongly connected components with size greater than 1
    # these will all have in degree > 0, but should still be included
    glist = [g for g in strongly_connected_component_subgraphs(G, copy=False) if g.number_of_nodes() > 1]

    for g in glist:
        # only counts if it was the original list
        nodes = [n for n in g.nodes() if n in node_set]
        if len(nodes) > 0:
            logging.debug('Strongly connected component: %s' % repr(nodes))
            for n in nodes:
                filtered_pkgs.add(n)

    return filtered_pkgs
Beispiel #8
0
def sample(n):
  T = DiGraph()
  alive = dict()
  heights = list()
  total = 0.0
  for i in range(n):
    alive[i] = 0.0

  k = n
  while k > 1:
    event = exponential(1.0/binom(k, 2))
    total += event
    heights.append(total)
    for c in alive.keys():
      alive[c] += event

    [a, b] = subset(alive.keys(), 2)
    c = new_node(k)
    alive[a]
    alive[b]
    T.add_edge(a, c, length = alive[a])
    T.add_edge(b, c, length = alive[b])

    del alive[a]
    del alive[b]
    alive[c] = 0.0

    k -= 1

  T.below = collapse(T)
  T.heights = heights

  return T
Beispiel #9
0
def _graph(formula):
  """Build the implication graph"""
  G = DiGraph()
  for (a,b) in formula.iterclause():
		G.add_edge(-a,b)
		G.add_edge(-b,a)
		
  return G
Beispiel #10
0
def test_new_attributes_are_better():
    mock_g = DiGraph()
    mock_g.add_edge('A', 'B', PDC=5, TP=['C', 'D', 'E'])
    method = MapGraph._new_attributes_are_better.im_func
    nt.assert_true(method(mock_g, 'A', 'B', 18, []))
    nt.assert_true(method(mock_g, 'A', 'B', 2, ['C', 'D', 'E']))
    nt.assert_false(method(mock_g, 'A', 'B', 2, ['C', 'D', 'E', 'F']))
    nt.assert_false(method(mock_g, 'A', 'B', 5, ['C', 'D', 'E']))
Beispiel #11
0
def test_translate_attr_modified():
    mock_conn = DiGraph()
    mock_conn.add_edge('B-1', 'B-2', Connection='Present')
    translate = EndGraph._translate_attr_modified.im_func
    # PDCs that get averaged are 3 (RCs), 6 (existent conn edge), and
    # 18 (non-existent conn edge).
    nt.assert_equal(translate(EndGraph(), ('A-1', ['B-1', 'B-3']),
                              ('A-2', ['B-2']), None, mock_conn),
                    {'Connection': 'Present', 'PDC': 6.6})
Beispiel #12
0
 def add_edge(self,  u,  v = None):
     temp = self.copy()
     DiGraph.add_edge(temp,  u,  v = v)
     if not temp._is_directed_acyclic_graph():
         raise ValueError("Edge (%s, %s) creates a cycle" %(u, v) )
     elif not temp._is_connected():
         raise ValueError("Edge (%s, %s) creates disconnected graph" %(u, v) )
     else:
         DiGraph.add_edge(self,  u,  v = v)
Beispiel #13
0
    def test_bidirectional_edges(self):
        """A tournament must not have any pair of nodes with greater
        than one edge joining the pair.

        """
        G = DiGraph()
        G.add_edges_from([(0, 1), (1, 2), (2, 3), (3, 0), (1, 3), (0, 2)])
        G.add_edge(1, 0)
        assert_false(is_tournament(G))
Beispiel #14
0
def test_new_attributes_are_better():
    mock_endg = DiGraph()
    mock_endg.add_edge('A', 'B', {'PDC': 5, 'Connection': 'Unknown'})
    method = EndGraph._new_attributes_are_better.im_func
    nt.assert_false(method(mock_endg, 'A', 'B', {'Connection': 'Unknown'}))
    nt.assert_true(method(mock_endg, 'A', 'B', {'Connection': 'Present',
                                                'PDC': 2}))
    nt.assert_false(method(mock_endg, 'A', 'B', {'EC_Source': 'U',
                                                 'EC_Target': 'X'}))
Beispiel #15
0
def test_translate_attr_original():
    mock_conn = DiGraph()
    mock_conn.add_edge('B-1', 'B-2', {'EC_Source': 'N', 'EC_Target': 'Nc'})
    translate = EndGraph._translate_attr_original.im_func
    # PDCs that get averaged are 3 (RCs), 6 (existent conn edge), and
    # 18 (non-existent conn edge).
    nt.assert_equal(translate(EndGraph(), ('A-1', ['B-1', 'B-3']),
                              ('A-2', ['B-2']), None, mock_conn),
                    {'EC_Source': 'N', 'EC_Target': 'Nc', 'PDC': 6.6})
Beispiel #16
0
	def init_graph(self):
		#Represent the graph using adjacency lists
		g = DiGraph()
		g.add_nodes_from(self.vocabulary) #The keys (terms) become nodes in the graph
		for x in self.vocabulary:
			for y in self.vocabulary:
				if self.conditional_probability(x,y) >= 0.8 and self.conditional_probability(y,x) < 0.8:
					g.add_edge(x,y)
		return g 
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')
Beispiel #18
0
 def pagerank(self):
     g = DiGraph()
     valid_links = self.links.find({'status': 200})
     outlinks = self.outlinks
     for i in valid_links:
         g.add_edge(i['parent'], i['page'])
     for i in outlinks.find(): #TODO remove stupid hack
         g.add_edge(i['parent'], i['page'])
     return pr(g)
Beispiel #19
0
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
Beispiel #20
0
def build_digraph(fasta_dict, k):
    """Build a digraph from the data given by the fasta dict, using k and our
    is_edge predicate.
    """
    d = DiGraph()
    d.add_nodes_from(fasta_dict.items())
    for (s, t) in permutations(fasta_dict.items(), 2):
        if is_edge(s, t, k):
            d.add_edge(s, t)
    return d
Beispiel #21
0
    def _buildGraph(self, jobs):
        """ Builds the workflow graph

        Uses the dependencies specified in the configuration
        file to build a directed acyclic graph.  This graph
        is used to direct which process are run and when.

        @param jobs: A list of job attributes and dependencies
        from the conf file
        @type jobs: C{list}
        """
        h = {}

        # Set up my graph and the dummy start noe
        g = DiGraph()
        self.start = Job("start", "nothin", logDir=self.jobLogRoot)
        self.start.done = True
        self.start.succeeded = True

        # Make a dict with the job id as the key
        # Put the instantiated jobs and dependencies
        # in as values
        for node in list(jobs):
            id = node["id"]
            del node["id"]
            h[id] = {}

            if "homedir" not in node:
                node["homedir"] = WorkFlow.root
            env = os.environ
            if "env" in node:
                env.update(node["env"])

            job = Job(id, node["script"], homeDir=node["homedir"], logDir=self.jobLogRoot, env=env)
            h[id]["job"] = job
            if "depends_on" in node:

                # Quack: depends_on must be a list, may be passed
                # as a string.  This bit of duck typing makes sure
                # it's a list
                if not hasattr(node["depends_on"], "sort"):
                    node["depends_on"] = [node["depends_on"]]

                h[id]["depends_on"] = node["depends_on"]

        # Use the dict to build the DAG
        for jobId, v in h.items():
            if "depends_on" in v:
                for j in v["depends_on"]:
                    g.add_edge(h[j]["job"], v["job"])
            else:
                g.add_edge(self.start, v["job"])

        return g
Beispiel #22
0
 def add_edge(self, source_mac, target_mac, signal=None,
         traffic_byt=None, speed_mbps=None, d_speed_mbps=None,
         residual_bw=None, weight=None, confirmed=True, wired=False):
     if weight is None:
         weight = self.DEFAULT_WEIGHT
     DiGraph.add_edge(self, source_mac, target_mac, signal=signal,
             traffic_byt=traffic_byt, speed_mbps=speed_mbps,
             d_speed_mbps=d_speed_mbps, weight=weight, confirmed=confirmed,
             residual_bw=residual_bw, last_update=time(), wired=wired)
     # update time stamp
     self.time_stamp += 1
    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
Beispiel #24
0
 def build_distances(self, session, limit, directed):
     if directed:
         graph = DiGraph()
     else:
         graph = Graph_()
     for node in session.query(LastFmArtist).all():
         for edge in node.graph_out[0:self.limit]:
             graph.add_edge(edge.from_.id, edge.to_.id)
     self.__distances = all_pairs_shortest_path_length(graph)
     self.max_distance = max(max(self.__distances[key]) for key in self.__distances)
     LOG.debug('Max distance: {0}'.format(self.max_distance))
Beispiel #25
0
def test_relate_node_to_others():
    mock_mapp = DiGraph()
    relate = MapGraph._relate_node_to_others.im_func
    nt.assert_equal(relate(mock_mapp, 'A-1', ['A-2', 'A-3', 'A-4']),
                    ([], 'D'))
    mock_mapp.add_edge('A-1', 'A-3', RC='I')
    nt.assert_equal(relate(mock_mapp, 'A-1', ['A-2', 'A-3', 'A-4']),
                    ('A-3', 'I'))
    mock_mapp.add_edges_from([('A-1', 'A-3', {'RC': 'L'}),
                              ('A-1', 'A-4', {'RC': 'L'})])
    nt.assert_equal(relate(mock_mapp, 'A-1', ['A-2', 'A-3', 'A-4']),
                    (['A-3', 'A-4'], 'L'))
Beispiel #26
0
def generate_tgen_perf_clients(servers="server1:8888,server2:8888", size="50 KiB", name="conf/tgen.perf50kclient.graphml.xml"):
    G = DiGraph()

    G.add_node("start", socksproxy="localhost:9000", serverport="8888", peers=servers)
    G.add_node("transfer", type="get", protocol="tcp", size=size)
    G.add_node("pause", time="60")

    G.add_edge("start", "transfer")
    G.add_edge("transfer", "pause")
    G.add_edge("pause", "start")

    write_graphml(G, name)
Beispiel #27
0
    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 createRandomGraph(n, balanced=None, randGenerator=None, seed=None):

        """
        Parameters
        ----------
        :rtype : graph
        n : int
            The number of nodes.
        balanced : bool
            False: The tree is not balanced
            True: The tree is balanced
        seed : int, optional
            Seed for random number generator (default=None).
        """
        G = DiGraph()

        if not randGenerator is None:
            randGen = randGenerator
        else:
            randGen=random
            if not seed is None:
                randGen.seed(seed)
        parents = range(0, n - 1)
        nodes = []
        visited = [0] * (n - 1)
        #each node can have only one child and maximum of two parents
        root = parents[randGen.randint(0,len(parents) - 1)]
        nodes.append(root)
        del parents[parents.index(root)]
        G.add_edge(root, n - 1)
        while len(parents) > 0:
            if balanced:
                node = 0
            else:
                if len(nodes) == 1:
                    node = 0
                else:
                    node = randGen.randint(0,len(nodes) - 1)
            node = nodes[node]
            if len(parents) == 1:
                parent = 0
            else:
                parent = randGen.randint(0,len(parents) - 1)
            parent = parents[parent]
            G.add_edge(parent, node)
            del parents[parents.index(parent)]
            nodes.append(parent)
            visited[node] += 1
            if visited[node] == 2:
                del nodes[nodes.index(node)]

        return G
Beispiel #29
0
def call_graph(ast):
    """Return a call graph (networkx.Diagraph) caller ---(callsite)---> callee.

    All values are represented as AST nodes. You can straightforwardly pull
    line and column numbers out and apply them to the original code to get
    excerpts.

    """
    graph = DiGraph()
    for call_site in call_sites(ast):
        graph.add_edge(call_site.nearest_scope_holder(), lookup(call_site),
                       call_site=call_site)
    return graph
Beispiel #30
0
def test_get_rcs():
    nt.assert_equal(EndGraph._get_rcs.im_func(None, ('B-1', ['B-1']), None,
                                              [3]),
                    (['I'], [3, 0]))
    mock_mapp = DiGraph()
    mock_mapp.add_edge('A-1', 'B-1', RC='S', PDC=4)
    mock_mapp.add_edge('A-2', 'B-1', RC='O', PDC=6)
    nt.assert_equal(EndGraph._get_rcs.im_func(None, ('B-1', ['A-1', 'A-2']),
                                              mock_mapp, []),
                    (['S', 'O'], [4, 6]))
    mock_mapp.add_edge('A-3', 'B-1', RC='I', PDC=3)
    nt.assert_raises(EndGraphError, EndGraph._get_rcs.im_func, None,
                     ('B-1', ['A-1', 'A-2', 'A-3']), mock_mapp, [])
Beispiel #31
0
def read_digraph_in_dimacs_format(filename):
    with open(filename) as f:
        line = f.readline()
        while line.startswith('c '):
            line = f.readline()
        tokens = line.split()
        num_nodes = int(tokens[2])
        num_edges = int(tokens[3])
        G = DiGraph()
        G.add_nodes_from(range(1, num_nodes + 1))
        for i in range(num_edges):
            tokens = f.readline().split()
            n1 = int(tokens[1])
            n2 = int(tokens[2])
            G.add_edge(n1, n2)
        return G
Beispiel #32
0
def make_edge(g: nx.DiGraph, question_id, to_id, answer_id=None) -> bool:
    if not to_id or not question_id:
        return False
    if question_id not in g.nodes:
        raise Exception(f'Not found question_id = {question_id}')
    if to_id not in g.nodes:
        raise Exception(f'Not found to_id = {to_id}')

    if answer_id and find_answer(g, question_id, answer_id):
        g.add_edge(question_id, to_id, answer_id=answer_id)
        return True
    else:
        g.add_edge(question_id, to_id)
        return True

    raise Exception(f'Not found answer_id = {answer_id}')
Beispiel #33
0
    def _abstract_cyclic_region(graph: networkx.DiGraph, loop_nodes, head,
                                normal_entries, abnormal_entries,
                                normal_exit_node, abnormal_exit_nodes):
        region = GraphRegion(head, None, None, None, True)

        subgraph = networkx.DiGraph()
        region_outedges = []

        graph.add_node(region)
        for node in loop_nodes:
            subgraph.add_node(node)
            in_edges = list(graph.in_edges(node, data=True))
            out_edges = list(graph.out_edges(node, data=True))

            for src, dst, data in in_edges:
                if src in normal_entries:
                    graph.add_edge(src, region, **data)
                elif src in abnormal_entries:
                    data['region_dst_node'] = dst
                    graph.add_edge(src, region, **data)
                elif src in loop_nodes:
                    subgraph.add_edge(src, dst, **data)
                elif src is region:
                    subgraph.add_edge(head, dst, **data)
                else:
                    assert 0

            for src, dst, data in out_edges:
                if dst in loop_nodes:
                    subgraph.add_edge(src, dst, **data)
                elif dst is region:
                    subgraph.add_edge(src, head, **data)
                elif dst is normal_exit_node:
                    region_outedges.append((node, dst))
                    graph.add_edge(region, dst, **data)
                elif dst in abnormal_exit_nodes:
                    region_outedges.append((node, dst))
                    # data['region_src_node'] = src
                    graph.add_edge(region, dst, **data)
                else:
                    assert 0

        subgraph_with_exits = networkx.DiGraph(subgraph)
        for src, dst in region_outedges:
            subgraph_with_exits.add_edge(src, dst)
        region.graph = subgraph
        region.graph_with_successors = subgraph_with_exits
        if normal_exit_node is not None:
            region.successors = [normal_exit_node]
        else:
            region.successors = []
        region.successors += list(abnormal_exit_nodes)

        for node in loop_nodes:
            graph.remove_node(node)

        return region
Beispiel #34
0
    def load_dependency_graph(self):

        try:
            if Config.get("dependency_graph"):
                self.log.debug(
                    "Loading model dependency graph",
                    path=Config.get("dependency_graph"),
                )
                dep_graph_str = open(Config.get("dependency_graph")).read()
            else:
                self.log.debug("Using default model dependency graph",
                               graph={})
                dep_graph_str = "{}"

            # joint_dependencies is of the form { Model1 -> [(Model2, src_port, dst_port), ...] }
            # src_port is the field that accesses Model2 from Model1
            # dst_port is the field that accesses Model1 from Model2
            static_dependencies = json.loads(dep_graph_str)
            dynamic_dependencies = self.compute_service_dependencies()

            joint_dependencies = dict(static_dependencies.items() +
                                      dynamic_dependencies)

            model_dependency_graph = DiGraph()
            for src_model, deps in joint_dependencies.items():
                for dep in deps:
                    dst_model, src_accessor, dst_accessor = dep
                    if src_model != dst_model:
                        edge_label = {
                            "src_accessor": src_accessor,
                            "dst_accessor": dst_accessor,
                        }
                        model_dependency_graph.add_edge(
                            src_model, dst_model, edge_label)

            model_dependency_graph_rev = model_dependency_graph.reverse(
                copy=True)
            self.model_dependency_graph = {
                # deletion
                True: model_dependency_graph_rev,
                False: model_dependency_graph,
            }
            self.log.debug("Loaded dependencies",
                           edges=model_dependency_graph.edges())
        except Exception as e:
            self.log.exception("Error loading dependency graph", e=e)
            raise e
Beispiel #35
0
def create_entry_and_exit_nodes(graph: nx.DiGraph, items):
    graph.add_nodes_from([START, END])
    items = sorted(items)
    graph.add_edge(START, items[0])
    for node in (set(graph.nodes()) - {START}) - nx.descendants(graph, START):
        graph.add_edge(START, node)
    graph.add_edge(items[-1], END)
    for node in (set(graph.nodes()) - {END}) - nx.ancestors(graph, END):
        graph.add_edge(node, END)
Beispiel #36
0
 def _append_task(graph: nx.DiGraph, task: TaskMixin) -> str:
     if isinstance(task, Input):
         graph.add_node(task.__name__,
                        label=task.__loader__.__name__,
                        time=task.__time__,
                        ntype='input')
     elif isinstance(task, Task):
         graph.add_node(task.__name__,
                        label=task.__fn__.__name__,
                        time=task.__time__,
                        ntype='internal')
         for dependency in task.dependencies:
             child = _append_task(graph, dependency)
             graph.add_edge(child, task.__name__)
     else:
         raise NotImplementedError(f"Unrecognized Node Type: {type(task)}")
     return task.__name__
Beispiel #37
0
def min_spanning_arborescence_nx(arcs, sink):
    """
    Wrapper for the networkX min_spanning_tree to follow the original API
    :param arcs: list of Arc tuples
    :param sink: unused argument. We assume that 0 is the only possible root over the set of edges given to
     the algorithm.
    """
    G = DiGraph()
    for arc in arcs:
        G.add_edge(arc.head, arc.tail, weight=arc.weight)
    ARB = minimum_spanning_arborescence(G)
    result = {}
    headtail2arc = {(a.head, a.tail): a for a in arcs}
    for edge in ARB.edges:
        tail = edge[1]
        result[tail] = headtail2arc[(edge[0], edge[1])]
    return result
Beispiel #38
0
def process_bel_graph(bel_graph: BELGraph) -> DiGraph:
    """Convert BEL Graph to a directed graph ready for drug2ways.

    :param bel_graph: BELGraph
    :return: directed graph
    """
    # Get only causal edges
    bel_graph = get_subgraph_by_edge_filter(bel_graph, is_causal_relation)

    directed_graph = DiGraph()
    for source, target, data in bel_graph.edges(data=True):
        if data[RELATION] not in RELATION_MAPPING_BEL:
            logger.warning(f"Unknown relation {data[RELATION]}")
            continue
        directed_graph.add_edge(source.as_bel(), target.as_bel(), relation=RELATION_MAPPING_BEL[data[RELATION]])

    return directed_graph
Beispiel #39
0
    def _add_node(self, g: nx.DiGraph, node: Node) -> None:
        """
        Adds a node to the internal graph representation.

        .. warning:: Not to be used for adding nodes to the DAG, this is only used to build an internal
           representation of the graph.

        Args:
            g: Graph object
            node: Node to add

        """
        g.add_node(node)

        for child in node.children:
            self._add_node(g, child)
            g.add_edge(node, child)
Beispiel #40
0
    def test_assemble(self):
        seqs = [
            Sequence("a", "DriverSaves"),
            Sequence("b", "EachDay"),
            Sequence("c", "LivesEach"),
            Sequence("d", "SavesLives")
        ]

        path = list('adcb')

        graph = DiGraph()
        graph.add_edge('a', 'd', overlap=5)
        graph.add_edge('d', 'c', overlap=5)
        graph.add_edge('c', 'b', overlap=4)

        self.assertEqual(assemble(seqs, graph, path),
                         "DriverSavesLivesEachDay")
Beispiel #41
0
 def _convert_initial_routes_to_digraphs(self):
     """Converts list of initial routes to list of Digraphs."""
     route_id = 0
     self._routes = []
     for r in self._initial_routes:
         total_cost = 0
         route_id += 1
         G = DiGraph(name=route_id)
         edges = list(zip(r[:-1], r[1:]))
         for (i, j) in edges:
             edge_cost = self.G.edges[i, j]["cost"]
             G.add_edge(i, j, cost=edge_cost)
             total_cost += edge_cost
         G.graph["cost"] = total_cost
         self._routes.append(G)
         for v in r:
             self._routes_with_node[v] = [G]
Beispiel #42
0
    def _build_symbol_dag(
        self,
        symbol: Object,
        graph: nx.DiGraph,
        resolution_stack: ResolutionStack,
        check_dag: bool = True,
    ) -> nx.DiGraph:

        syms = [(symbol, ())]
        added = set()

        while syms:
            sym, downstreams = syms.pop(0)
            resolution_stack.push(sym._impl.id)
            try:
                processed = False
                if sym._impl.id in graph.nodes:
                    processed = True
                else:
                    graph.add_node(sym._impl.id, symbol=sym)
                    added.add(sym._impl.id)

                for symbol_id in downstreams:
                    if (sym._impl.id, symbol_id) not in graph.edges:
                        graph.add_edge(sym._impl.id, symbol_id)
                        added.add(symbol_id)
                        added.add(sym._impl.id)

                if processed:
                    continue

                for upstream in sym._inst.depends_on(self):
                    syms.append((upstream, (sym._impl.id,)))
            except Exception as err:
                _, _, tb = sys.exc_info()
                raise exc.ResolutionError(resolution_stack, err, tb) from err
            except BaseException:
                raise
            else:
                resolution_stack.pop()

        if check_dag and added:
            # Check that this is in fact a DAG and has no cycles
            self._raise_not_dag(graph, list(added))

        return added
Beispiel #43
0
def parse_owl_rdf_resolver(iri):
    path = os.path.join(owl_dir_path, get_uri_name(iri))
    o = Ontospy(path)

    g = DiGraph(IRI=iri)

    for cls in o.classes:
        g.add_node(cls.locale, type='Class')

        for parent in cls.parents():
            g.add_edge(cls.locale, parent.locale, type='SubClassOf')

        for instance in cls.instances():
            _, frag = urldefrag(instance)
            g.add_edge(frag, cls.locale, type='ClassAssertion')

    return g
Beispiel #44
0
def balance_graph(graph: nx.DiGraph) -> (object, object):
    head = None
    tail = None
    marked_nodes = set()
    for node in graph:
        balance = in_out_balance(graph, node)
        if abs(balance) > 1:
            raise ValueError("Cannot balance graph: {0}".format(graph))
        elif balance == 1:
            tail = node
        elif balance == -1:
            head = node

    edge = (tail, head)
    if head is not None:
        graph.add_edge(*edge)
    return edge
Beispiel #45
0
 def replace_input_edges(graph: nx.DiGraph, input_edges_match: dict):
     """
     Replacing existing input/output edges with a new ones to a new sub-graph.
     :param graph: networkX graph to operate on.
     :param input_edges_match: match of input edges between old and new sub-graph.
     :return: None
     """
     for old_name_port, new_name_port in input_edges_match.items():
         old_node_name, old_in_port = __class__.extract_port(old_name_port)
         new_node_name, new_in_port = __class__.extract_port(new_name_port)
         old_node = Node(graph, old_node_name)
         src_node_name = get_sorted_inputs(old_node)[old_in_port][0]
         edge_attrs = graph[src_node_name][old_node_name][0].copy()
         edge_attrs['in'] = new_in_port
         graph.add_edge(src_node_name, new_node_name, **edge_attrs)
         log.debug("Created edge from {} to {} with attrs: {}".format(
             src_node_name, new_node_name, edge_attrs))
Beispiel #46
0
def network_from_json(json_data):
    # NOTE|dutc: we could use the following, but it would tie our data format
    #            too closely to the graph library
    # from networkx import node_link_graph
    g = DiGraph()

    nodes = {}
    for el in json_data['elements']:
        el = getattr(elements, el['type'])(el['id'], **el['metadata'])
        g.add_node(el)
        nodes[el.id] = el

    for cx in json_data['connections']:
        from_node, to_node = nodes[cx['from_node']], nodes[cx['to_node']]
        g.add_edge(from_node, to_node)

    return g
Beispiel #47
0
 def create(self,root:TreeNode,G:nx.DiGraph,pos,x=0,y=0,layer=1,bias=0):
     if root is not None:
         pos[root.val]=(x,y)
         if root.left:
             G.add_node(root.val)
             G.add_edge(root.val,root.left.val)
             l_x, l_y = x -bias, y - 1
             layer += 1
             self.create(root.left,G,pos,x=l_x,y=l_y,layer=layer,bias=bias//2)
         if root.right:
             G.add_edge(root.val,root.right.val)
             r_x, r_y = x +bias, y - 1
             layer += 1
             self.create(root.right,G,pos,x=r_x,y=r_y,layer=layer,bias=bias//2)
         return G,pos
     else:
         return
Beispiel #48
0
    def to_nx(
        self,
        g: nx.DiGraph,
        attrs: t.Optional[t.MutableMapping[str, t.Callable[[Edge],
                                                           t.Any]]] = None,
    ) -> None:
        """Submethod used to export Graph object g into NX Graph format."""

        if attrs is None:
            attrs = {}

        g.add_edge(
            self.source.id,
            self.target.id,
            **{key: func(self)
               for key, func in attrs.items()},
        )
Beispiel #49
0
def test_graph_pruning():
    participant1 = make_address()
    participant2 = make_address()
    participant3 = make_address()

    graph = DiGraph()
    graph.add_edge(participant1, participant2, view=12)
    graph.add_edge(participant2, participant1, view=21)
    graph.add_edge(participant2, participant3, view=23)
    graph.add_edge(participant3, participant2, view=32)

    all_reachable = SimpleReachabilityContainer({
        p: AddressReachability.REACHABLE
        for p in (participant1, participant2, participant3)
    })
    pruned_all_reachable = prune_graph(graph=graph,
                                       reachability_state=all_reachable)
    assert len(pruned_all_reachable.edges) == len(graph.edges)

    p1_not_reachable = SimpleReachabilityContainer(
        all_reachable.reachabilities.copy())
    p1_not_reachable.reachabilities[
        participant1] = AddressReachability.UNREACHABLE
    pruned_p1_unreachbale = prune_graph(graph=graph,
                                        reachability_state=p1_not_reachable)
    assert len(pruned_p1_unreachbale.edges
               ) == 2  # just the two edges between 2 and 3 left

    p2_not_reachable = SimpleReachabilityContainer(
        all_reachable.reachabilities.copy())
    p2_not_reachable.reachabilities[
        participant1] = AddressReachability.UNREACHABLE
    p2_not_reachable.reachabilities[
        participant2] = AddressReachability.UNREACHABLE
    pruned_p2_unreachbale = prune_graph(graph=graph,
                                        reachability_state=p2_not_reachable)
    assert len(pruned_p2_unreachbale.edges) == 0  # 2 is part of all channels

    # test handling of unknown nodes
    p1_not_in_reachble_map = SimpleReachabilityContainer(
        all_reachable.reachabilities.copy())
    del p1_not_in_reachble_map.reachabilities[participant1]
    pruned_p1_not_in_reachable_map = prune_graph(
        graph=graph, reachability_state=p1_not_in_reachble_map)
    assert (len(pruned_p1_not_in_reachable_map.edges) == 2
            )  # just the two edges between 2 and 3 left
Beispiel #50
0
def make_clothing_graph(root=ROOT):
    """Create a mock hierarchy of clothing items."""
    G = DiGraph()
    G.add_edge(root, "Mens")
    G.add_edge("Mens", "Shirts")
    G.add_edge("Mens", "Bottoms")
    G.add_edge("Mens", "Jackets")
    G.add_edge("Mens", "Swim")

    return G
        def _solve(
            samples: List[Union[str, int]],
            tree: nx.DiGraph,
            unique_character_matrix: pd.DataFrame,
            priors: Dict[int, Dict[int, float]],
            weights: Dict[int, Dict[int, float]],
            missing_state_indicator: int,
        ):

            if len(samples) == 1:
                return samples[0]
            # Partitions the set of samples by percolating a similarity graph
            clades = list(
                self.percolate(
                    unique_character_matrix,
                    samples,
                    priors,
                    weights,
                    missing_state_indicator,
                ))
            # Generates a root for this subtree with a unique int identifier
            root = next(node_name_generator)
            tree.add_node(root)

            for clade in clades:
                if len(clade) == 0:
                    clades.remove(clade)

            # If unable to return a split, generate a polytomy and return
            if len(clades) == 1:
                for clade in clades[0]:
                    tree.add_edge(root, clade)
                return root
            # Recursively generate the subtrees for each daughter clade
            for clade in clades:
                child = _solve(
                    clade,
                    tree,
                    unique_character_matrix,
                    priors,
                    weights,
                    missing_state_indicator,
                )
                tree.add_edge(root, child)
            return root
Beispiel #52
0
def generate_graph(edges):
    """
    Converts edges into a networkx digraph object.

    Param edges (list of tuples)
        ... like [('A', 'B'), ('B', 'C'), ('A', 'C'), ('E', 'D'), ('D', 'E')]
        ... where 'B' follows 'A', 'C' follows 'B', etc.

    Returns graph (networkx.classes.digraph.DiGraph)
    """
    graph = DiGraph()
    for edge in edges:
        source = edge[0]  # source, a.k.a friend, a.k.a followed
        recipient = edge[1]  # recipient, a.k.a user, a.k.a follower
        graph.add_node(source)
        graph.add_node(recipient)
        graph.add_edge(source, recipient)
    return graph
Beispiel #53
0
def compile_mock_rt_graph(edge_list=mock_rt_graph_edge_list,
                          weight_attr="retweet_count"):
    """
    Param
        edge_list (list of dict) like: [
            {"user_screen_name": "user1", "retweet_user_screen_name": "leader1", "weight": 4},
            {"user_screen_name": "user2", "retweet_user_screen_name": "leader1", "weight": 6},
            {"user_screen_name": "user3", "retweet_user_screen_name": "leader2", "weight": 4},
        ]

        weight_attr (str) the name of the weight attribute for each edge in the edge list
    """
    graph = DiGraph()
    for row in edge_list:
        graph.add_edge(row["user_screen_name"],
                       row["retweet_user_screen_name"],
                       rt_count=float(row[weight_attr]))
    return graph
Beispiel #54
0
def check_loops(gltf: GLTF):
    n = DiGraph()
    all_nodes = list(range(len(gltf.model.nodes)))
    for i in all_nodes:
        n.add_node(i)

    for node_index, node in enumerate(gltf.model.nodes):
        if node.children:
            for c in node.children:
                n.add_edge(node_index, c)

    try:
        c = find_cycle(n)
    except NetworkXNoCycle:
        pass
    else:
        logger.info(c=c)
        raise ZValueError("cycle found", c=c)
Beispiel #55
0
def generate_graph_from_task_dependencies(task_dependencies_dot_file: str):
    if task_dependencies_dot_file is not None:
        print(
            f"Generate Task Dependency Graph to {task_dependencies_dot_file}")
        print()
        dependencies = collect_dependencies()
        g = DiGraph()
        for dependency in dependencies:
            g.add_node(dependency.source,
                       label=dependency.source.representation)
            g.add_node(dependency.target,
                       label=dependency.target.representation)
            g.add_edge(
                dependency.source,
                dependency.target,
                dependency=dependency,
                label=f"\"type={dependency.type}, index={dependency.index}\"")
        networkx.nx_pydot.write_dot(g, task_dependencies_dot_file)
Beispiel #56
0
def parse_trusts():
    """ Parses trust relations.

      Args:
          None.

      Returns:
          A dictionary with user ids as indexes and a list of user ids as values.
      This means that the user represented by the index trusts the corresponding
      list of users.
  """
    f = open('data/trustnetwork.txt', 'r')
    trust = DiGraph()
    for l in f:
        l = l.strip().split('::::')
        trust.add_edge(l[0], l[1])
    f.close()
    return trust
Beispiel #57
0
def load_input():
    answers = set()
    pattern = re.compile("(\w+) (\w+) bags contain (.+)")
    grph = DiGraph()
    with open('input.txt') as fd:
        for line in fd:
            match = pattern.search(line.strip())
            source_bag = "{0} {1}".format(match.group(1), match.group(2))
            dests = match.group(3)
            dests = dests.split(',')
            dests = list(map(lambda x: x.strip(), dests))
            for dest in dests:
                spl_dest = dest.split()
                if spl_dest[0].isnumeric():
                    number = int(dest[0])
                    dest_bag = "{0} {1}".format(spl_dest[1], spl_dest[2])
                    grph.add_edge(source_bag, dest_bag, number=number)
    return grph
Beispiel #58
0
def _get_disregulated_triplets_helper(graph, relation_set):
    """
    :param pybel.BELGraph graph: A BEL graph
    :param set[str] relation_set: A set of relations to keep 
    :rtype: iter[tuple]
    """
    result = DiGraph()

    for u, v, d in graph.edges_iter(data=True):
        if d[RELATION] in relation_set:
            result.add_edge(u, v)

    update_node_helper(graph, result)

    for a, b, c in get_triangles(result):
        if a == b == c:
            continue
        yield a, b, c
Beispiel #59
0
    def _add_from_to_edge(
        self,
        g: nx.DiGraph,
        s: Tuple[float, float, int, int],
        t: Tuple[float, float, int, int],
    ):
        def ae(x, y):
            return abs(x - y) < 0.001

        s_x, s_z, s_rot, s_hor = s
        t_x, t_z, t_rot, t_hor = t

        dist = round(math.sqrt((s_x - t_x) ** 2 + (s_z - t_z) ** 2), 2)
        angle_dist = (round_to_factor(t_rot - s_rot, 90) % 360) // 90
        horz_dist = (round_to_factor(t_hor - s_hor, 30) % 360) // 30

        # If source and target differ by more than one action, continue
        if sum(x != 0 for x in [dist, angle_dist, horz_dist]) != 1:
            return

        grid_size = self._grid_size
        action = None
        if angle_dist != 0:
            if angle_dist == 1:
                action = "RotateRight"
            elif angle_dist == 3:
                action = "RotateLeft"

        elif horz_dist != 0:
            if horz_dist == 11:
                action = "LookUp"
            elif horz_dist == 1:
                action = "LookDown"
        elif ae(dist, grid_size):
            if (
                (s_rot == 0 and ae(t_z - s_z, grid_size))
                or (s_rot == 90 and ae(t_x - s_x, grid_size))
                or (s_rot == 180 and ae(t_z - s_z, -grid_size))
                or (s_rot == 270 and ae(t_x - s_x, -grid_size))
            ):
                g.add_edge(s, t, action="MoveAhead")

        if action is not None:
            g.add_edge(s, t, action=action)
Beispiel #60
0
class OrtTestCase(TestCase):
    def setUp(self):
        self.m = DiGraph()
        self.c = DiGraph()
        self.e = EndGraph()

    def test_unknown_input_does_not_pollute(self):
        self.m.add_edges_from([('A-1', 'B-1', {
            'RC': 'S',
            'PDC': 0
        }), ('B-1', 'A-1', {
            'RC': 'L',
            'PDC': 0
        }), ('A-2', 'B-1', {
            'RC': 'S',
            'PDC': 0
        }), ('B-1', 'A-2', {
            'RC': 'L',
            'PDC': 0
        }), ('A-3', 'B-2', {
            'RC': 'S',
            'PDC': 0
        }), ('B-2', 'A-3', {
            'RC': 'L',
            'PDC': 0
        }), ('A-4', 'B-2', {
            'RC': 'S',
            'PDC': 0
        }), ('B-2', 'A-4', {
            'RC': 'L',
            'PDC': 0
        })])
        self.c.add_edge('A-2',
                        'A-3',
                        EC_Source='P',
                        EC_Target='P',
                        PDC_EC_Source=0,
                        PDC_EC_Target=0,
                        PDC_Site_Source=0,
                        PDC_Site_Target=0)
        self.e.add_translated_edges(self.m, self.c, 'B', 'original')
        self.assertEqual(self.e.number_of_edges(), 1)
        self.assertEqual(self.e['1']['2']['EC_Source'], 'P')
        self.assertEqual(self.e['1']['2']['EC_Target'], 'P')