Beispiel #1
0
def to_graph(evt):
    G = nx.DiGraph()
    in_counter = 0
    out_counter = 0
    for v in evt.vertices():
        G.add_node(v.barcode())
    for p in evt.particles():
        start = p.production_vertex()
        end = p.end_vertex()
        if not start:
            s = 'In: %d' % in_counter
            e = end.barcode()
            in_counter += 1
        elif not end:
            s = start.barcode()
            e = 'Out: %d' % out_counter
            out_counter += 1
        else:
            s = start.barcode()
            e = end.barcode()
        G.add_edge(s, e, barcode=p.barcode(),
                         status=p.status(),
                         pdg=p.pdg_id(),
                         obj=p)
    # color signal particles
    for p1 in G.edges(data=True):
        par = p1[1]
        pid = p1[2]['obj'].pdg_id()
        if pid in {-5, 5}:
            for p2 in dfs_edges(G, par):
                G[p2[0]][p2[1]]['signal'] = True
    return G
Beispiel #2
0
def retrieve_etymology(word, lang="eng"):
    try:
        word_etym = [edge[1] for edge in dfs_edges(etymology, lang+':'+word)]
    except KeyError:
        #print("Warning! Could not retrieve etymology for word '%s' in language '%s'" %(word, lang))
        word_etym = []
    return word_etym
Beispiel #3
0
def t1() :
    g=digraph().rand()
    ns= list(algs.dfs_edges(g,source=0))
    print("NetworkX DFS Edges: ", ns)
    print("My DFS Edges:       ", list(df_edges(g,0)))
    ds = [0] + list(map(lambda b: b[1], algbfs.bfs_edges(g,source=0)))
    print("NetworkX BFS Edges: ", ds)
    print("My BFS Edges:       ", list(bf_nodes(g,0)))
def retrieve_etymology(word, lang="eng"):
    global G
    loadEtym()
    try:
        etymology = [edge[1] for edge in dfs_edges(G, lang+':'+word)]
    except KeyError:
        print("Warning! Could not retrieve etymology for word '%s' in language '%s'" %(word, lang))
        etymology = []
    return etymology
def search_dependencies(to_search, G, verbose):
    """
    Print dependencies
    """
    for edge in dfs_edges(G, to_search):
        print(edge)
        if verbose == 1:
            print(G.node[edge[0]])
            print(G.node[edge[1]])
            print('----------')
Beispiel #6
0
    def walk_graph_and_get_coords(self, sgraph: StructureGraph) -> np.array:
        vectors = self.get_distance_vectors(sgraph)
        node_coords = {0: sgraph.structure[0].coords}
        for u, v in dfs_edges(nx.Graph(sgraph.graph), source=0):
            node_coords[v] = node_coords[u] + vectors[(u, v)]

        final_coords = np.array(
            [node_coords[k] for k in sorted(node_coords.keys())])

        return final_coords
Beispiel #7
0
 def retrieve_etymology(self, word, lang="eng"):
     if not self.etymwn:
         self.etymwn = resources.init_etymwn()
     try:
         word_etym = [edge[1] for edge in dfs_edges(self.etymwn, lang + ':' + word)]
     except KeyError:
         # print("Warning! Could not retrieve etymology for word '%s' in
         # language '%s'" %(word, lang))
         word_etym = []
     return word_etym
Beispiel #8
0
 def test_randomgraph_files(self):
     G = nx.gn_graph(20)
     targets = defaultdict(dict)
     depends = defaultdict(dict)
     allfiles = list()
     for a, b in G.edges():
         f = os.path.join(self.workdir, "{}_{}.txt".format(a, b))
         allfiles.append(f)
         targets[a][b] = depends[b][a] = f
     shall_fail = set(
         [random.choice(G.nodes()) for _ in range(int(len(G)**.5))])
     nodes = nx.algorithms.dag.topological_sort(G)
     task_nos = [None for _ in range(len(nodes))]
     for n in nodes:
         cmd = "touch /dev/null " + " ".join(targets[n].values())
         if n in shall_fail:
             cmd += " ;exit 1"
         t = self.ctx.add_task(cmd,
                               name=cmd,
                               targets=list(targets[n].values()),
                               depends=list(depends[n].values()))
         task_nos[n] = t.task_no
     # self.ctx.fail_idx = task_nos[G.successors(list(shall_fail)[0])[-1]]
     self.assertFalse(any(map(os.path.exists, allfiles)))
     with capture(stderr=StringIO()):
         import anadama2.reporters
         with self.assertRaises(anadama2.workflow.RunFailed):
             rep = anadama2.reporters.LoggerReporter("debug", "/tmp/analog")
             self.ctx.go(reporter=rep)
     child_fail = set()
     for n in shall_fail:
         task_no = task_nos[n]
         self.assertIn(task_no, self.ctx.failed_tasks,
                       ("tasks that raise exceptions should be marked"
                        " as failed"))
         self.assertTrue(bool(self.ctx.task_results[task_no].error),
                         "Failed tasks should have errors in task_results")
         for _, succ in dfs_edges(G, n):
             s_no = task_nos[succ]
             child_fail.add(succ)
             self.assertIn(s_no, self.ctx.failed_tasks,
                           "all children of failed tasks should fail")
             self.assertIn("parent task", self.ctx.task_results[s_no].error,
                           ("children of failed tasks should have errors"
                            " in task_results"))
     for n in set(nodes).difference(shall_fail.union(child_fail)):
         task_no = task_nos[n]
         self.assertIn(task_no, self.ctx.completed_tasks)
         self.assertFalse(bool(self.ctx.task_results[task_no].error))
Beispiel #9
0
 def test_randomgraph_files(self):
     G = nx.gn_graph(20)
     targets = defaultdict(dict)
     depends = defaultdict(dict)
     allfiles = list()
     for a, b in G.edges():
         f = os.path.join(self.workdir, "{}_{}.txt".format(a, b))
         allfiles.append(f)
         targets[a][b] = depends[b][a] = f
     shall_fail = set(
         [random.choice(G.nodes()) for _ in range(int(len(G)**.5))])
     nodes = nx.algorithms.dag.topological_sort(G)
     task_nos = [None for _ in range(len(nodes))]
     for n in nodes:
         cmd = "touch /dev/null " + " ".join(targets[n].values())
         if n in shall_fail:
             cmd += " ;exit 1"
         slurm_add_task = lambda *a, **kw: self.ctx.add_task_gridable(
             mem=50, time=5, cores=1, *a, **kw)
         add_task = self.ctx.add_task if bern(0.5) else slurm_add_task
         t = add_task(cmd,
                      name=cmd,
                      targets=list(targets[n].values()),
                      depends=list(depends[n].values()))
         task_nos[n] = t.task_no
     self.assertFalse(any(map(os.path.exists, allfiles)))
     with capture(stderr=StringIO()):
         with self.assertRaises(anadama2.workflow.RunFailed):
             self.ctx.go(grid_jobs=2)
     child_fail = set()
     for n in shall_fail:
         task_no = task_nos[n]
         self.assertIn(task_no, self.ctx.failed_tasks,
                       ("tasks that raise exceptions should be marked"
                        " as failed"))
         self.assertTrue(bool(self.ctx.task_results[task_no].error),
                         "Failed tasks should have errors in task_results")
         for _, succ in dfs_edges(G, n):
             s_no = task_nos[succ]
             child_fail.add(succ)
             self.assertIn(s_no, self.ctx.failed_tasks,
                           "all children of failed tasks should fail")
             self.assertIn("parent task", self.ctx.task_results[s_no].error,
                           ("children of failed tasks should have errors"
                            " in task_results"))
     for n in set(nodes).difference(shall_fail.union(child_fail)):
         task_no = task_nos[n]
         self.assertIn(task_no, self.ctx.completed_tasks)
         self.assertFalse(bool(self.ctx.task_results[task_no].error))
Beispiel #10
0
    def test_randomgraph_tasks(self):
        G = nx.gn_graph(20)
        targets = defaultdict(dict)
        depends = defaultdict(dict)
        shall_fail = set(
            [random.choice(G.nodes()) for _ in range(int(len(G)**.5))])
        nodes = nx.algorithms.dag.topological_sort(G)
        task_nos = [None for _ in range(len(nodes))]
        for n in nodes:
            cmd = "touch /dev/null "
            name = None
            if n in shall_fail:
                cmd += " ;exit 1"
                name = "should fail"
            slurm_add_task = lambda *a, **kw: self.ctx.add_task_gridable(
                mem=50, time=5, cores=1, *a, **kw)
            add_task = self.ctx.add_task if bern(0.5) else slurm_add_task
            t = add_task(cmd,
                         name=name,
                         depends=[
                             self.ctx.tasks[task_nos[a]]
                             for a in G.predecessors(n)
                         ])
            task_nos[n] = t.task_no

        with capture(stderr=StringIO()):
            with self.assertRaises(anadama2.workflow.RunFailed):
                self.ctx.go(grid_jobs=2)
        child_fail = set()
        for n in shall_fail:
            task_no = task_nos[n]
            self.assertIn(task_no, self.ctx.failed_tasks,
                          ("tasks that raise exceptions should be marked"
                           " as failed"))
            self.assertTrue(bool(self.ctx.task_results[task_no].error),
                            "Failed tasks should have errors in task_results")
            for _, succ in dfs_edges(G, n):
                s_no = task_nos[succ]
                child_fail.add(succ)
                self.assertIn(s_no, self.ctx.failed_tasks,
                              "all children of failed tasks should fail")
                self.assertIn("parent task", self.ctx.task_results[s_no].error,
                              ("children of failed tasks should have errors"
                               " in task_results"))
        for n in set(nodes).difference(shall_fail.union(child_fail)):
            task_no = task_nos[n]
            self.assertIn(task_no, self.ctx.completed_tasks)
            self.assertFalse(bool(self.ctx.task_results[task_no].error))
Beispiel #11
0
	def _reduce_to_forest(G):
		"""Removes edges from G so that each connected component is a tree."""
		from networkx.algorithms.traversal.depth_first_search import dfs_edges
		unseen_nodes = set(G.nodes_iter())
		unvisisted_edges = set(G.edges_iter())
		roots = []
		
		while unseen_nodes:
			root = unseen_nodes.pop()
			roots.append(root)
			for edge in dfs_edges(G, root):
				unvisisted_edges.remove(edge)
				_, target = edge
				unseen_nodes.remove(target)
		
		to_remove = [e for e in G.edges_iter() if e in unvisisted_edges]
		G.remove_edges_from(to_remove)
		return roots
Beispiel #12
0
 def modify(self): 
   list_of_models = []
   names = []
   tmpbool = True   
   for node in self._tree.get_nodes():
     m = node.get_model()
     names.append(node.get_name())
     kmodel = knapsack_model.KnapsackModel(m)
     kmodel.mp_model_to_knapsack(m)
     new_model = mp_model_builder.MPModelBuilder().build_from_scratch(kmodel.get_profits(), [kmodel.get_weights()], ['L'], [kmodel.get_max_weight()], ["new_model"], [0 for j in range(m.get_num_columns())], [1 for j in range(m.get_num_columns())], m.columns_names)
     new_model.set_objective(kmodel.get_profits())
     new_model.set_name(m.get_name())
     list_of_models.append(new_model)
             
   G = nx.DiGraph(self._tree)
   for edge in dfs_edges(G, self._tree.get_root()):   
     fnode = edge[0]
     snode = edge[1]
     for i in range(len(names)):
       if names[i] == fnode:
         fmodel = list_of_models[i]
       elif names[i] == snode:
         smodel = list_of_models[i]
     if tmpbool:
       new_tree = DecompositionTree(self._tree._model)
       root = Node(fmodel)
       new_tree.set_root(root)
       tmpbool = False
     fbool = False
     if not fmodel in new_tree.get_models():
       new_tree.add_node(fmodel)
       fbool = True
     if not smodel in new_tree.get_models():
       new_tree.add_node(smodel)
       fbool = True
     if fbool: 
       shared_vars_names = []
       fvars = fmodel.columns_names
       svars = smodel.columns_names
       for p in fvars:
         if p in svars:
           shared_vars_names.append(p)
       new_tree.add_edge(fmodel, smodel, shared_vars_names)
   return new_tree
Beispiel #13
0
 def get_n_neighbors(self,node,depth):
     node_set = set()
     for edge in dfs_edges(self.graph,source=node, depth_limit=depth):
         node_set.add(edge[0])
         node_set.add(edge[1])
     return list(node_set)   
Beispiel #14
0
def allchildren(dag, task_no):
    kids = map(second, dfs_edges(dag, task_no))
    return itertools.chain([task_no], kids)
Beispiel #15
0
def dfs_beam_edges(G, source, value, width=None):
    """Iterates over edges in a beam search.

    The beam search is a generalized breadth-first search in which only
    the "best" *w* neighbors of the current node are enqueued, where *w*
    is the beam width and "best" is an application-specific
    heuristic. In general, a beam search with a small beam width might
    not visit each node in the graph.

    Parameters
    ----------
    G : NetworkX graph

    source : node
        Starting node for the breadth-first search; this function
        iterates over only those edges in the component reachable from
        this node.

    value : function
        A function that takes a node of the graph as input and returns a
        real number indicating how "good" it is. A higher value means it
        is more likely to be visited sooner during the search. When
        visiting a new node, only the `width` neighbors with the highest
        `value` are enqueued (in decreasing order of `value`).

    width : int (default = None)
        The beam width for the search. This is the number of neighbors
        (ordered by `value`) to enqueue when visiting each new node.

    Yields
    ------
    edge
        Edges in the beam search starting from `source`, given as a pair
        of nodes.

    Examples
    --------
    To give nodes with, for example, a higher centrality precedence
    during the search, set the `value` function to return the centrality
    value of the node::

        >>> G = nx.karate_club_graph()
        >>> centrality = nx.eigenvector_centrality(G)
        >>> source = 0
        >>> width = 5
        >>> for u, v in nx.bfs_beam_edges(G, source, centrality.get, width):
        ...     print((u, v))  # doctest: +SKIP

    """

    if width is None:
        width = len(G)

    def successors(v):
        """Returns a list of the best neighbors of a node.

        `v` is a node in the graph `G`.

        The "best" neighbors are chosen according to the `value`
        function (higher is better). Only the `width` best neighbors of
        `v` are returned.

        The list returned by this function is in decreasing value as
        measured by the `value` function.

        """
        # TODO The Python documentation states that for small values, it
        # is better to use `heapq.nlargest`. We should determine the
        # threshold at which its better to use `heapq.nlargest()`
        # instead of `sorted()[:]` and apply that optimization here.
        #
        # If `width` is greater than the number of neighbors of `v`, all
        # neighbors are returned by the semantics of slicing in
        # Python. This occurs in the special case that the user did not
        # specify a `width`: in this case all neighbors are always
        # returned, so this is just a (slower) implementation of
        # `bfs_edges(G, source)` but with a sorted enqueue step.
        return iter(sorted(G.neighbors(v), key=value, reverse=True)[:width])

    # TODO In Python 3.3+, this should be `yield from ...`
    for e in dfs_edges(G, source):
        yield e
Beispiel #16
0
def _dfs_with_networkx(vert_num, edges):
    graph = nx.Graph()
    graph.add_nodes_from(range(vert_num))
    for u, v in edges:
        graph.add_edge(u, v)
    return np.array(list(depth_first_search.dfs_edges(graph, source=0)))