Esempio n. 1
0
    def run(self, G, O, mi, sigma2):
        """
        Main
        :param G: graph
        :param O: list of observers <<<< ACTIVE observers !
        :param mi: mean
        :param sigma2: variance
        :return:
        """
        # TODO : consider only active observers !
        first_node = O[0]

        # Compute the delay vector d relative to first_node
        d = self.observed_delay(G, O)

        # calculates F for the first node: fulfills max
        max = self.main_function(first_node, O, d, nx.bfs_tree(G, source=first_node), mi, sigma2)

        source = first_node  # SEE HOW WE CAN DO IT
        # calculates the maximum F
        for s in G:  # FIXME is this G_a ?
            # Compute the spanning tree rooted at s
            T = nx.bfs_tree(G, source=s)
            F = self.main_function(s, O, d, T, mi, sigma2)

            if F > max:
                max = F
                source = s
        return source
Esempio n. 2
0
 def relations(self, sty1, sty2, rela, source_vocab=[]):
     """Return set of relations between provided semantic types"""
     # collect descendant/child types for each semantic type
     network = self.semantic_network.graph("isa")
     sty1 = [node for node in nx.bfs_tree(network, sty1)]
     sty2 = [node for node in nx.bfs_tree(network, sty2)]
     sty1 = " OR ".join(map(lambda x:"STY='%s'" % x, sty1))
     sty2 = " OR ".join(map(lambda x:"STY='%s'" % x, sty2))
     
     # override object default source vocabulary?
     if source_vocab:
         sab = self._source_vocab_sql(source_vocab)
     else:
         sab = self._source_vocab_sql(self.source_vocab)
     sab = "" if not sab else sab + " AND"
     
     sql = """
     SELECT DISTINCT CUI2,CUI1 FROM 
     (SELECT * FROM MRREL WHERE RELA='%s') AS R,
     (SELECT L.CUI FROM MRCONSO AS L, MRSTY AS LS WHERE (%s) AND L.CUI=LS.CUI) AS LARG,
     (SELECT R.CUI FROM MRCONSO AS R, MRSTY AS RS WHERE (%s) AND R.CUI=RS.CUI) AS RARG
     WHERE %s ((LARG.CUI=CUI2) AND (RARG.CUI=CUI1));"""
    
     sql = sql % (rela,sty1,sty2,sab)
     results = self.conn.query(sql)
     
     return results
Esempio n. 3
0
def family_tree(digraph, target):
    """
    Subsets graph to return predecessors and successors with a blood relation
    to the target node
    """
    all_successors = nx.bfs_tree(digraph, target, reverse=False)
    all_predecessors = nx.bfs_tree(digraph, target, reverse=True)
    subdig = digraph.subgraph(itertools.chain(
            [target], all_successors, all_predecessors)).copy()
    return subdig
Esempio n. 4
0
 def Algorithm(self, G, O, mi, sigma2):
     d = self.observed_delay(G, O)
     first_node = O[0]
     # calculates F for the first node: fulfills max
     MAX = self.main_function(first_node, O, d, nx.bfs_tree(G, source=first_node), mi, sigma2)
     source = first_node  # SEE HOW WE CAN DO IT
     # calculates the maximum F
     for s in G:
         T = nx.bfs_tree(G, source=s)
         F = self.main_function(s, d, T, mi)
         if F > MAX:
             MAX = F
             source = s
     return source
Esempio n. 5
0
def bfs_heur(graph, query_nodes, a = 1, include_solution = False):
	""" Approximately maximize discrepancy on graph. """
	
	best_root = None
	best_d    = -1
	for root in query_nodes:
		sys.stderr.write('.')
		tree = nx.bfs_tree(graph, root)
		d, solution_graph = tree_offline(tree, query_nodes, root, a, False)
		if d > best_d:
			best_d = d
			best_root = root
	tree = nx.bfs_tree(graph, best_root)
	sys.stderr.write('\n')
	return tree_offline(tree, query_nodes, best_root, a, include_solution)
Esempio n. 6
0
 def test_bfs_tree_isolates(self):
     G = nx.Graph()
     G.add_node(1)
     G.add_node(2)
     T = nx.bfs_tree(G, source=1)
     assert_equal(sorted(T.nodes()), [1])
     assert_equal(sorted(T.edges()), [])
Esempio n. 7
0
def main(args):

    # read and validate the rate matrix info from the sqlite3 database file
    conn = sqlite3.connect(args.rates)
    cursor = conn.cursor()
    states, distn, Q = get_rate_matrix_info(cursor)
    conn.close()

    # Get a more convenient form of the rate matrix for forward simulation.
    rates, P = cmedbutil.decompose_rates(Q)

    # extract the unrooted tree from the tree db file
    conn = sqlite3.connect(args.tree)
    cursor = conn.cursor()
    G = get_unrooted_tree(cursor)
    conn.close()

    # Pick the smallest vertex of G as an arbitrary root for sampling.
    # This choice can be made arbitrarily
    # because the rate matrix is currently defined to be time-reversible.
    root = min(G.nodes())

    # Build a directed breadth first tree starting at the distinguished vertex.
    # Note that the tree built by nx.bfs_tree and the edges yielded
    # by nx.bfs_edges do not retain the edge attributes.
    G_dag = nx.bfs_tree(G, root)
    for a, b in G_dag.edges():
        G_dag[a][b]['blen'] = G[a][b]['blen']

    # sample the unconditional columns of the alignment
    conn = sqlite3.connect(args.outfile)
    build_alignment_table(
            args.length, args.only_leaves,
            conn, root, G_dag, distn, states, rates, P)
    conn.close()
Esempio n. 8
0
def chiral_order(atoms, chiral_atom, depth=6):
  # Create a list of ordered atoms to be passed back
  ordered = []
  # Do a quick check whether there are multiple hydrogens
  neighbors = atoms.neighbors(chiral_atom)
  hydrogens = [atom for atom in neighbors if atom.element == "H"]
  if len(hydrogens) < 2:
    tree = nx.bfs_tree(atoms, chiral_atom)
    # Generate the list of shortest paths in the molecule, neglecting the trivial path [chiral_atom]
    paths = sorted(nx.single_source_shortest_path(tree, chiral_atom, depth).values(), reverse = True)[:-1]
    while paths:
      # Pop the first element (highest priority path) from the list of paths and remove any duplicates.
      path = paths.pop(0)
      paths_no_dups = [unpruned for unpruned in paths if unpruned != path]
      # If there are any duplicates, the paths list will be smaller and we can't resolve a highest priority yet.
      if len(paths_no_dups) != len(paths):
        paths = paths_no_dups
      # Otherwise, the path is higher priority than all the other paths, so its second atom is the neighbour with
      # highest priority.
      else:
        ranked_atom = path[1]
        ordered.append(ranked_atom)
        # Drop all the paths containing our ranked atom.
        paths = [unpruned for unpruned in paths if unpruned[1] is not ranked_atom]
  return ordered
Esempio n. 9
0
 def get_spanning_tree(self, node, use_infectors = False):
     ''' Returns a networkx spanning tree of the adjacency matrix
     rooted at node'''
     G = nx.bfs_tree(self.graph, node).to_undirected()
     if not nx.is_connected(G):
         return None
     return G
Esempio n. 10
0
    def keep(self, areas=['all'], sexes=['male', 'female', 'total'], start_year=-pl.inf, end_year=pl.inf):
        """ Modify model to feature only desired area/sex/year(s)

        :Parameters:
          - `areas` : list of str, optional
          - `sexes` : list of str, optional
          - `start_year` : int, optional
          - `end_year` : int, optional

        """
        if 'all' not in areas:
            self.hierarchy.remove_node('all')
            for area in areas:
                self.hierarchy.add_edge('all', area)
            self.hierarchy = nx.bfs_tree(self.hierarchy, 'all')

            def relevant_row(i):
                area = self.input_data['area'][i]
                return (area in self.hierarchy) or (area == 'all')

            self.input_data = self.input_data.select(relevant_row)
            self.nodes_to_fit = set(self.hierarchy.nodes()) & set(self.nodes_to_fit)

        self.input_data = self.input_data.select(lambda i: self.input_data['sex'][i] in sexes)

        self.input_data = self.input_data.select(lambda i: self.input_data['year_end'][i] >= start_year)
        self.input_data = self.input_data.select(lambda i: self.input_data['year_start'][i] <= end_year)

        print 'kept %d rows of data' % len(self.input_data.index)
Esempio n. 11
0
def _get_node_to_pset_same_transition_matrix(T, root, P,
        node_to_allowed_states=None):
    T_bfs = nx.bfs_tree(T, root)
    preorder_nodes = list(nx.dfs_preorder_nodes(T, root))
    sorted_states = sorted(P)

    # Put the tree into sparse boolean csr form.
    tree_csr_indices, tree_csr_indptr = _digraph_to_bool_csr(
            T_bfs, preorder_nodes)

    # Put the transition matrix into sparse boolean csr form.
    trans_csr_indices, trans_csr_indptr = _digraph_to_bool_csr(
            P, sorted_states)

    # Define the state mask.
    state_mask = _define_state_mask(
            node_to_allowed_states, preorder_nodes, sorted_states)

    # Update the state mask.
    pyfelscore.mcy_get_node_to_pset(
            tree_csr_indices,
            tree_csr_indptr,
            trans_csr_indices,
            trans_csr_indptr,
            state_mask)

    # Convert the updated state mask into a node_to_pset dict.
    node_to_pset = _state_mask_to_dict(
            state_mask, preorder_nodes, sorted_states)

    # Return the node_to_pset dict.
    return node_to_pset
def graph(ttl):
    T = nx.bfs_tree(G,ttl)


    edgeAccuracy = 0
    years = []
    for e in T.edges():
        t1 = e[0]
        t2 = e[1]
        y1 = 0
        y2 = 0
        for d in Data:
            if(d['title'] == t1):
                y1 = d['year']
            if(d['title'] == t2):
                y2 = d['year']
        #print e
        #print str(y1) + " | " + str(y2)
        if(y1 > y2):
            edgeAccuracy += 1
        if(years == []):
            years.append(y1)
            years.append(y2)
        else:
            years.append(y2)

    yearsAccuracy = 0
    for y in range(1,len(years)):
        if( years[y] > years[y-1]):
            yearsAccuracy +=1

    print(T.edges())
    #print years
    #print "edge Accuracy = " + str(edgeAccuracy)
    print ttl + " = " + str(yearsAccuracy)
Esempio n. 13
0
File: data.py Progetto: aflaxman/gbd
    def keep(self, areas=["all"], sexes=["male", "female", "total"], start_year=-pl.inf, end_year=pl.inf):
        """ Modify model to feature only desired area/sex/year(s)

        :Parameters:
          - `areas` : list of str, optional
          - `sexes` : list of str, optional
          - `start_year` : int, optional
          - `end_year` : int, optional

        """
        if "all" not in areas:
            self.hierarchy.remove_node("all")
            for area in areas:
                self.hierarchy.add_edge("all", area)
            self.hierarchy = nx.bfs_tree(self.hierarchy, "all")

            def relevant_row(i):
                area = self.input_data["area"][i]
                return (area in self.hierarchy) or (area == "all")

            self.input_data = self.input_data.select(relevant_row)
            self.nodes_to_fit = set(self.hierarchy.nodes()) & set(self.nodes_to_fit)

        self.input_data = self.input_data.select(lambda i: self.input_data["sex"][i] in sexes)

        self.input_data = self.input_data.select(lambda i: self.input_data["year_end"][i] >= start_year)
        self.input_data = self.input_data.select(lambda i: self.input_data["year_start"][i] <= end_year)

        print "kept %d rows of data" % len(self.input_data.index)
Esempio n. 14
0
def objecttree_get_all_skeletons(request, project_id=None, node_id=None):
    """ Retrieve all skeleton ids for a given node in the object tree. """
    g = get_annotation_graph( project_id )
    potential_skeletons = nx.bfs_tree(g, int(node_id)).nodes()
    result = tuple(nid for nid in potential_skeletons if 'skeleton' == g.node[nid]['class'])
    json_return = json.dumps({'skeletons': result}, sort_keys=True, indent=4)
    return HttpResponse(json_return, content_type='text/json')
Esempio n. 15
0
def get_graph_compressed(graph_data):
    """
    Getting the Compressed Graph. A Compressed Graph is a DAG, after removing
    unreachable graph nodes, and getting bfs tree.
    """
    # Creating the directed graphs, for graph1.
    dgraph = nx.DiGraph(graph_data)
    if not dgraph.has_node(0):  # adding root node, on one node case.
        dgraph.add_node(0)
    # First, remove non reachable nodes, from the root.
    # assuming node 0 is the function root node.
    bfsy = nx.bfs_tree(dgraph, 0).nodes()
    if 0 not in bfsy:
        bfsy.append(0)
    for i in dgraph.nodes():
        if i not in bfsy:
            dgraph.remove_node(i)

    # Second, _collapse some vertices together...
    dgraph = _collapse(dgraph)

    # create DAG's (computing scc) from digraph before.
    compressed_graph = nx.condensation(dgraph)

    return compressed_graph.edges()
Esempio n. 16
0
def all_dag_covers(_graph, condenseg, final_sccs, tree_type):
    initial_scc = _graph.node[_graph.graph["initial"]]["scc_index"]
    if tree_type=="bfs":
        condense_tree = networkx.bfs_tree(condenseg, initial_scc)
    elif tree_type=="dfs":
        condense_tree = networkx.dfs_tree(condenseg, initial_scc)
    rest_edges = [edge for edge in condenseg.edges() if edge not in condense_tree.edges()]

    all_tree_branch(_graph, condenseg, final_sccs, tree_type, condense_tree)
    dag_paths = condenseg.graph["condense_paths"]
    for rest_edge in rest_edges:
        path = networkx.shortest_path(condense_tree, initial_scc, rest_edge[0])
        _node = rest_edge[1]
        while True:
            if condense_tree.out_degree(_node)==0 and condense_tree.in_degree(_node)==1:
                if "_final" in str(_node):
                    path.append(_node)
                else:
                    path = path + condense_tree.node[_node]["continue_path"]
                break
            else:
                path.append(_node)
                _node = condense_tree.edge[_node].keys()[0]

        dag_paths.append(path)

    condenseg.graph["condense_paths"] = dag_paths
    return dag_paths
Esempio n. 17
0
 def generate_graph(self):
     #nodes
     n = int(self.lineEdit_2.text())
     self.graph = nx.balanced_tree(2, n)
     self.graph = nx.bfs_tree(self.graph, 0)
     #self.pos = nx.circular_layout(self.graph)
     self.pos = nx.graphviz_layout(self.graph, prog='dot')
     self.draw_graph()
Esempio n. 18
0
def get_state_segmentation(G_in):
    """
    Segment the tree according to state.
    This does not use the branch lengths or the layout.
    @param G_in: undirected graph with state annotation on edges
    @return: segment_isostate_list, isostate_to_parity
    """

    # get leaf vertices
    # pick an arbitrary leaf as a distinguished (root) vertex
    vertices = list(G_in)
    leaves = sorted(v for v in vertices if len(G_in.neighbors(v)) == 1)
    root = leaves[0]

    # Build a directed breadth first tree starting at the distinguished vertex.
    # Note that the tree built by nx.bfs_tree and the edges yielded
    # by nx.bfs_edges do not retain the edge attributes.
    G_dag = nx.bfs_tree(G_in, root)

    # initialize the tree of isostate adjacencies
    G_isostate = nx.Graph()

    # Each contig is defined by a set of edges.
    root_state = G_in[root][G_dag.successors(root)[0]]['state']
    root_edge_list = []
    root_contig_index = 0
    contig_states = [root_state]
    contig_edge_lists = [root_edge_list]
    vertex_to_contig_index = {root : root_contig_index}
    for node in nx.topological_sort(G_dag):
        ci = vertex_to_contig_index[node]
        ci_state = contig_states[ci]
        successors = G_dag.successors(node)
        for v in successors:
            state = G_in[node][v]['state']
            if state == ci_state:
                contig_edge_lists[ci].append((node, v))
                vertex_to_contig_index[v] = ci
            else:
                ci_next = len(contig_states)
                G_isostate.add_edge(ci, ci_next)
                vertex_to_contig_index[v] = ci_next
                contig_states.append(state)
                contig_edge_lists.append([(node, v)])

    # Convert the G_isostate graph into a map from
    # isostate labels to parities.
    isostate_to_parity = {0 : 0}
    for va, vb in nx.bfs_edges(G_isostate, 0):
        isostate_to_parity[vb] = 1 - isostate_to_parity[va]
    
    # Get the isostate label associated with each edge.
    va_vb_isostate_list = []
    for isostate_label, edge_list in enumerate(contig_edge_lists):
        for va, vb in edge_list:
            va_vb_isostate_list.append((va, vb, isostate_label))
    return va_vb_isostate_list, isostate_to_parity
Esempio n. 19
0
def simply_bfs(_graph, condenseg, final_sccs):
    initial_scc = _graph.node[_graph.graph["initial"]]["scc_index"]
    condense_tree = networkx.bfs_tree(condenseg, initial_scc)
    dag_paths = []
    for final_scc in final_sccs:
        path = networkx.shortest_path(condense_tree, initial_scc, final_scc)
        dag_paths.append(path)

    condenseg.graph["condense_paths"] = dag_paths
    return dag_paths
Esempio n. 20
0
def BFS_levels_comm(G, s):
  if G:
    T = nx.bfs_tree(G,s)
    nextlevel = set()
    succ = T.successors(s)
    prevlevel = set()
    prevlevel.add(s)
    level = 0
    
    children = [set() for proc in range(gnumprocs)] # ALL children
    failedchildren = [0 for proc in range(gnumprocs)]
    peers = [set() for proc in range(gnumprocs)]
    failed_parents = [set() for proc in range(gnumprocs)]

    while (len(succ) > 0):
      level += 1
      comm = 0
      hidegedges = 0
      for node in succ:
        node_succ = set(T.successors(node))
        nodeproc = proc_of_node(node)
        nodeneighbs = set(G.neighbors(node))
        # all children
        children[nodeproc] = children[nodeproc].union(nodeneighbs.intersection(node_succ))      
        # number of failed children
        failedchildren[nodeproc] += len(children[nodeproc].intersection(nodeneighbs.intersection(node_succ))) 
        # peer
        peers[nodeproc] = peers[nodeproc].union(nodeneighbs.intersection(succ))
        # failed parent
        failed_parents[nodeproc] = failed_parents[nodeproc].union(nodeneighbs.intersection(prevlevel)) 
        for targnode in nodeneighbs:
          if (G.degree(targnode) > ghideg):
            hidegedges += 1
        for targnode in node_succ:
          if (share_same_proc(node, targnode) != 1) :
            comm += 1
        nextlevel = nextlevel.union(node_succ)
      prevlevel = succ
      succ = list(nextlevel)
      nextlevel.clear()
      if (level < gmaxlevels):
        #vec_children[level] += len(succ)
        vec_hideg[level] += hidegedges
        for i in range(gnumprocs):
          vec_failedchildren[level] += failedchildren[i]
          vec_children[level] += len(children[i])
          vec_peers[level] += len(peers[i])
          vec_parents[level] += len(failed_parents[i])

          children[i].clear()
          peers[i].clear()
          failed_parents[i].clear()
          failedchildren[i] = 0
          hidegedges = 0
      yield comm
Esempio n. 21
0
    def _get_scaffold_hierarchy(self,
                                scaffold_smiles,
                                data=False,
                                default=None,
                                max_levels=-1,
                                traversal='parent'):
        """Private: Return a list of parent/child scaffolds for a query scaffold.

        Parameters
        ----------
        scaffold_smiles : str
            SMILES of query scaffold.
        data : str, bool, optional
            The scaffold node attribute returned in 2-tuple (n, ddict[data]).
            If True, return entire node attribute dict as (n, ddict).
            If False, return just the nodes n. The default is False.
        default : value, bool, optional
            Value used for nodes that don't have the requested attribute.
            Only relevant if data is not True or False.
        max_levels : int, optional
            If > 0 only return scaffolds with a hierarchy difference to the
            query scaffold of `max_levels`.
        traversal : {'parent', 'child'}, optional
            Direction of traversal.

        Returns
        -------
        list
            A list of scaffold parent/child nodes.

        """
        assert traversal in {'parent', 'child'}
        reverse = traversal == 'parent'
        next_hiers = []
        if scaffold_smiles not in self:
            scaffold_smiles = canonize_smiles(scaffold_smiles, failsafe=True)
            if scaffold_smiles not in self:
                return next_hiers
        level = self.nodes[scaffold_smiles].get('hierarchy', float('inf'))
        bfs = iter(nx.bfs_tree(self, scaffold_smiles, reverse=reverse).nodes)
        next(bfs)  # first entry is the query node
        for succ in bfs:
            d = self.nodes[succ]
            if d.get('type') == 'scaffold' and (
                    max_levels < 0
                    or level - d.get('hierarchy', 0) <= max_levels):
                if data is False:
                    next_hiers.append(succ)
                elif data is True:
                    next_hiers.append((succ, self.nodes[succ]))
                else:
                    next_hiers.append(
                        (succ, self.nodes[succ].get(data, default)))
        return next_hiers
Esempio n. 22
0
def test_exhaustive_stepwise(wf_run, res, mfl, task_names_ref):
    base_model = DummyModel('run1', ofv=res[0], parameter_estimates=res[1])
    mfl = ModelFeatures(mfl)
    wf_search = exhaustive_stepwise(base_model, mfl, wf_run)
    start_node = wf_search.get_input()[0]
    start_node_successors = list(wf_search.tasks.successors(start_node))
    assert len(start_node_successors) == 1
    bfs_node_names = [
        task.name for task in nx.bfs_tree(wf_search.tasks, start_node)
    ]
    assert bfs_node_names[:len(task_names_ref)] == task_names_ref
Esempio n. 23
0
 def reduce(self, records):
   bfs_path = self.bfs_path
   bfs_count = self.bfs_count
   for item in records:
     graph_j = json.loads(item['json_data'])
     graph = nx.Graph()
     for tup in graph_j['links']:
        graph.add_edge(tup['source'], tup['target'])
     for node in graph_j['nodes']:
        bfs = list(nx.bfs_tree(graph, node['id']))
        yield { bfs_path: bfs, bfs_count: len(bfs) }
Esempio n. 24
0
def BFS_levels(G, s):
    if G:
        T = nx.bfs_tree(G, s)
        nextlevel = set()
        succ = T.successors(s)
        while (len(succ) > 0):
            for node in succ:
                nextlevel = nextlevel.union(T.successors(node))
            succ = list(nextlevel)
            nextlevel.clear()
            yield succ
Esempio n. 25
0
def part1(filename):
    with open(filename) as f:
        lines = f.readlines()

    G = nx.Graph()
    for line in lines:
        srcVertex, dstVertices = parse(line)
        for dstVertex in dstVertices:
            G.add_edge(srcVertex, dstVertex)

    print(len(nx.bfs_tree(G, 0).nodes()))
Esempio n. 26
0
def _cut_networkx(x, cut_node, ret):
    """Uses networkX graph to cut a neuron."""

    # Get subgraphs consisting of nodes distal to cut node
    dist_graph = nx.bfs_tree(x.graph, cut_node, reverse=True)

    if ret == 'distal' or ret == 'both':
        # bfs_tree does not preserve 'weight'
        # -> need to subset original graph by those nodes
        dist_graph = x.graph.subgraph(dist_graph.nodes)

        # Generate new neurons
        # This is the actual bottleneck of the function: ~70% of time
        dist = subset_neuron(x, dist_graph, clear_temp=False)

        # Change new root for dist
        dist.nodes.loc[dist.nodes.treenode_id == cut_node, 'parent_id'] = None
        dist.nodes.loc[dist.nodes.treenode_id == cut_node, 'type'] = 'root'

        # Reassign graphs
        dist.graph = dist_graph

        # Clear other temporary attributes
        dist._clear_temp_attr(exclude=['graph', 'type', 'classify_nodes'])

    if ret == 'proximal' or ret == 'both':
        # bfs_tree does not preserve 'weight'
        # need to subset original graph by those nodes
        prox_graph = x.graph.subgraph(
            [n
             for n in x.graph.nodes if n not in dist_graph.nodes] + [cut_node])

        # Generate new neurons
        # This is the actual bottleneck of the function: ~70% of time
        prox = subset_neuron(x, prox_graph, clear_temp=False)

        # Change cut node to end node for prox
        prox.nodes.loc[prox.nodes.treenode_id == cut_node, 'type'] = 'end'

        # Reassign graphs
        prox.graph = prox_graph

        # Clear other temporary attributes
        prox._clear_temp_attr(exclude=['graph', 'type', 'classify_nodes'])

    # ATTENTION: prox/dist_graph contain pointers to the original graph
    # -> changes to attributes will propagate back

    if ret == 'both':
        return dist, prox
    elif ret == 'distal':
        return dist
    elif ret == 'proximal':
        return prox
Esempio n. 27
0
def main():
    import pprint
    droid = Droid("../assets/day15-input")
    g, m = spread(droid, ' ')

    oxygen_location = next(coords for coords, t in m.items() if t == 'O')
    path = nx.shortest_path(g, source=(0, 0), target=oxygen_location)
    print("part 1:", len(path) - 1)
    t = nx.bfs_tree(g, source=oxygen_location)
    assert nx.is_tree(t)
    print("part 2:", tree_height(t, oxygen_location) - 1)
Esempio n. 28
0
def objecttree_get_all_skeletons(request, project_id=None, node_id=None):
    """ Retrieve all skeleton ids for a given node in the object tree
    """
    g = get_annotation_graph( project_id )
    potential_skeletons = nx.bfs_tree(g, int(node_id)).nodes()
    result = []
    for node_id in potential_skeletons:
        if g.node[node_id]['class'] == 'skeleton':
            result.append( node_id )
    json_return = json.dumps({'skeletons': result}, sort_keys=True, indent=4)
    return HttpResponse(json_return, mimetype='text/json')
Esempio n. 29
0
def BFS_levels(G, s):
  if G:
    T = nx.bfs_tree(G,s)
    nextlevel = set()
    succ = T.successors(s)
    while (len(succ) > 0):
      for node in succ:
        nextlevel = nextlevel.union(T.successors(node))
      succ = list(nextlevel)
      nextlevel.clear()
      yield succ
Esempio n. 30
0
def get_subgraphs(graph):
    """Get independent subgraphs."""
    node_list = list(graph.nodes)
    subgraphs = []
    while len(node_list) > 0:
        node = node_list[-1]
        subgraph = nx.bfs_tree(to_undirected(graph), node)
        for n in subgraph.nodes:
            node_list.remove(n)
        subgraphs.append(graph.subgraph(subgraph.nodes))
    return subgraphs
def noTimingComponentSizes(vanillaDiGraph):
    distributionOfSizes = []

    for guy in vanillaDiGraph.nodes():
        tree = nx.bfs_tree(vanillaDiGraph, guy)
        distributionOfSizes.append(len(tree.nodes()))
        
        if len(tree.nodes())>10:
            print guy
    
    return distributionOfSizes
Esempio n. 32
0
def opt_beta(g):
    k1s = 0
    k2s = 0
    for node in g.nodes():
        k1s += len(list(g.neighbors(node)))
        k2s += len(list(nx.bfs_tree(g, source=node, depth_limit=2).edges()))
    if k2s > k1s:
        beta = k1s / (k2s - k1s)
    else:
        beta = 0.1
    return beta
Esempio n. 33
0
    def generate(seed, number_of_nodes=20, max_connections_per_node=4):
        """
        >>> edges = [(0, 1), (0, 2), (0, 4), (1, 3), (2, 0), (2, 3), (3, 4)]
        >>> fitness = mlrose.MaxKColor(edges)
        >>> state = np.array([0, 1, 0, 1, 1])
        >>> fitness.evaluate(state)
        """
        np.random.seed(seed)
        # all nodes have to be connected, somehow.
        node_connection_counts = 1 + np.random.randint(
            max_connections_per_node, size=number_of_nodes)

        node_connections = {}
        nodes = range(number_of_nodes)
        for n in nodes:
            all_other_valid_nodes = [
                o for o in nodes if (o != n and (
                    o not in node_connections or n not in node_connections[o]))
            ]
            count = min(node_connection_counts[n], len(all_other_valid_nodes))
            other_nodes = sorted(
                np.random.choice(all_other_valid_nodes, count, replace=False))
            node_connections[n] = [(n, o) for o in other_nodes]

        # check connectivity
        g = nx.Graph()
        g.add_edges_from([x for y in node_connections.values() for x in y])

        for n in nodes:
            cannot_reach = [(n, o) if n < o else (o, n) for o in nodes
                            if o not in nx.bfs_tree(g, n).nodes()]
            for s, f in cannot_reach:
                g.add_edge(s, f)
                check_reach = len(
                    [_ for _ in nodes if o not in nx.bfs_tree(g, n).nodes()])
                if check_reach == 0:
                    break

        edges = [(s, f) for (s, f) in g.edges()]
        problem = MaxKColorOpt(edges=edges, length=number_of_nodes)
        return problem
Esempio n. 34
0
def mindist_from_source(G, source):
    dag = nx.bfs_tree(G, source)
    dist = {}  # stores [node, distance] pair
    for node in nx.topological_sort(dag):
        # pairs of dist,node for all incoming edges
        pairs = [(dist[v][0] + 1, v) for v in dag.pred[node]]
        if pairs:
            dist[node] = min(pairs)
        else:
            dist[node] = (0, node)

    return dist
Esempio n. 35
0
    def print_tree(self):
        """trying to print tree using netwrkx I guess if graphviz was installed it would be much prettier"""
        g=self._get_nx_graph()        
        g=nx.bfs_tree(g,round(self.root.node_prob_value,3))
        pos=nx.spring_layout(g)       

        for i in pos:
            pos[i][0] = pos[i][0] /100. # x coordinate
            pos[i][1] = pos[i][1] /100.# y coordinate
        nx.draw_networkx(g,pos,node_color='w',node_size=1000,with_labels=False)        
        nx.draw_networkx_labels(g,pos,font_size=10)                                        
        plt.show()
Esempio n. 36
0
    def _generate_bayes_net(self):
        # Pseudo Code
        # 1. Start at any node(probably 0 since that will be the easiest for
        # indexing)
        # 2. At each node figure out the conditional probability
        # 3. Add it to the new graph (which should probably be directed)
        # 4. Find unprocessed adjacent nodes
        # 5. If any go to 2
        #    Else return the bayes net'

        # Will it be possible that zero is not the root? If so, we need to pick
        # one
        root = 0

        samples = np.asarray(self.samples)
        self.bayes_net = nx.bfs_tree(self.spanning_graph, root)

        for parent, child in self.bayes_net.edges():
            parent_array = samples[:, parent]

            # if node is not root, get probability of each gene appearing in parent
            if not self.bayes_net.predecessors(parent):
                freqs = np.histogram(parent_array,
                                     len(np.unique(parent_array)))[0]
                parent_probs = dict(
                    zip(np.unique(parent_array), freqs / (sum(freqs) * 1.0)))

                self.bayes_net.node[parent]["probabilities"] = {
                    x: 0
                    for x in range(len(self.samples))
                }
                self.bayes_net.node[parent]["probabilities"].update(
                    parent_probs)

            child_array = samples[:, child]

            unique_parents = np.unique(parent_array)
            for parent_val in unique_parents:
                parent_inds = np.argwhere(parent_array == parent_val)
                sub_child = child_array[parent_inds]

                freqs = np.histogram(sub_child, len(np.unique(sub_child)))[0]
                child_probs = dict(
                    zip(np.unique(sub_child), freqs / (sum(freqs) * 1.0)))

                self.bayes_net.node[child][parent_val] = {
                    x: 0
                    for x in range(len(self.samples))
                }
                self.bayes_net.node[child][parent_val].update(child_probs)

            self.bayes_net.node[child] = dict(
                probabilities=self.bayes_net.node[child])
Esempio n. 37
0
def _sum_attrs_in_tree(G):
    ordered_nodes = list(reversed(list(nx.bfs_tree(G, "root").nodes)))

    for attr in ["chars_n", "chars_nowhites", "tokens_n", "tokens_unique"]:
        weights = defaultdict(int)
        for node in ordered_nodes:
            if G.out_degree(node) == 0:
                weights[node] = G.nodes[node][attr]
            G.nodes[node][attr] = weights[node]
            parents = list(G.predecessors(node))
            if parents:
                weights[parents[0]] += weights[node]
Esempio n. 38
0
def construct_bfs_flat_tree(adata,dict_win_ncells,dict_edge_shift_dist,max_width,cell_labels_ordered,root_node=None):
    flat_tree = adata.uns['flat_tree']
    if(root_node is None):
        root_node = list(flat_tree.nodes())[0]
    dict_path_len = nx.shortest_path_length(flat_tree,source=root_node,weight='len')    
    list_bfs_edges = list(nx.bfs_edges(flat_tree,root_node))  
    bfs_flat_tree = nx.bfs_tree(flat_tree,root_node)
    
    dict_edges_ncells = dict()
    dict_edges_ncells_cumsum = dict()
    max_ncells_cumsum = 0
    for edge_i in list_bfs_edges:
        br_id = flat_tree.edges[edge_i]['id']
        if(br_id == edge_i):
            df_ncells_edge_i = dict_win_ncells[br_id] 
        else:
            df_ncells_edge_i = dict_win_ncells[br_id].copy()
            df_ncells_edge_i = df_ncells_edge_i.iloc[::-1]
            df_ncells_edge_i['pt_lam'] = df_ncells_edge_i['pt_lam'].max() - df_ncells_edge_i['pt_lam']
            df_ncells_edge_i.index = range(df_ncells_edge_i.shape[0])
        df_ncells_edge_i = df_ncells_edge_i[['pt_lam'] + cell_labels_ordered]
        dict_edges_ncells[edge_i] = df_ncells_edge_i
        #sum up cell count
        df_ncells_edge_i_cumsum = df_ncells_edge_i.copy()
        df_ncells_edge_i_cumsum.iloc[:,1:] = df_ncells_edge_i.iloc[:,1:].cumsum(axis=1) 
        dict_edges_ncells_cumsum[edge_i] = df_ncells_edge_i_cumsum
        max_ncells_cumsum = max(max_ncells_cumsum,df_ncells_edge_i_cumsum.iloc[:,1:].values.max()) 
    nx.set_edge_attributes(bfs_flat_tree,values=dict_edges_ncells,name='ncells')
    nx.set_edge_attributes(bfs_flat_tree,values=dict_edges_ncells_cumsum,name='ncells_cumsum')
    
    dict_edges_ncells_cumsum_norm = dict()
    dict_edges_x = dict()
    dict_edges_y_up = dict()
    dict_edges_y_down = dict()
    for edge_i in list_bfs_edges:
        df_ncells_edge_i_cumsum = bfs_flat_tree.edges[edge_i]['ncells_cumsum']
        #normalize cell count  
        df_ncells_edge_i_cumsum_norm = df_ncells_edge_i_cumsum.copy()
        df_ncells_edge_i_cumsum_norm.iloc[:,1:] = max_width*df_ncells_edge_i_cumsum.iloc[:,1:]/np.float(max_ncells_cumsum)
        dict_edges_ncells_cumsum_norm[edge_i] = df_ncells_edge_i_cumsum_norm
        node_pos_st = np.array([dict_path_len[edge_i[0]],dict_edge_shift_dist[edge_i]]) 
        dict_edges_x[edge_i] = dict_path_len[edge_i[0]] + df_ncells_edge_i_cumsum_norm['pt_lam']
        df_coord_y_up = df_ncells_edge_i_cumsum_norm.iloc[:,1:].subtract(df_ncells_edge_i_cumsum_norm.iloc[:,-1]/2.0,axis=0)
        dict_edges_y_up[edge_i] = dict_edge_shift_dist[edge_i] + df_coord_y_up
        df_coord_y_down = df_coord_y_up.copy()
        df_coord_y_down.iloc[:,1:] =  df_coord_y_up.iloc[:,:-1].values
        df_coord_y_down.iloc[:,0] = 0 - df_ncells_edge_i_cumsum_norm.iloc[:,-1]/2.0
        dict_edges_y_down[edge_i] = dict_edge_shift_dist[edge_i] + df_coord_y_down
    nx.set_edge_attributes(bfs_flat_tree,values=dict_edges_ncells_cumsum_norm,name='ncells_cumsum_norm')
    nx.set_edge_attributes(bfs_flat_tree,values=dict_edges_x,name='x')
    nx.set_edge_attributes(bfs_flat_tree,values=dict_edges_y_up,name='y_up')
    nx.set_edge_attributes(bfs_flat_tree,values=dict_edges_y_down,name='y_down')    
    return bfs_flat_tree
Esempio n. 39
0
    def stream(self, records):
        self.logger.debug('nxbfs: %s', self)
        child = self.child
        parent = self.parent
        graph = nx.Graph()

        for item in records:
            graph.add_edge(item[child], item[parent])
            bfs_path = list(nx.bfs_tree(graph, item[parent]))
            item[self.bfs_path] = bfs_path
            item[self.bfs_count] = len(bfs_path)
            yield item
Esempio n. 40
0
    def get_origin_key(self, key):
        """Trace the record at 'key' back to its origin through its
        transformation trace and return the origin's key.

        :param key: the key of the record
        :type key: str
        :return: the original record key
        :rtype: str
        """
        tree = nx.bfs_tree(self.graph, key, reverse=True)
        roots = [n for n, d in dict(tree.out_degree()).items() if d == 0]
        return roots[0]
Esempio n. 41
0
 def select_decoy_cands(self, graph, hops):
     cands = {}
     for h in hops:
         cands_h = set(nx.bfs_tree(
             G=graph,
             source=self.node,
             depth_limit=h).nodes())
         cands_h_1 = set().union(*cands.values())
         cands[h] = cands_h.difference(cands_h_1)
         for node in self.link:
             cands[h].remove(node)
     return cands
Esempio n. 42
0
 def generate_tables(self, schema):
     g = schema.inheritance_graph
     hierarchy = list(bfs_tree(g, "__root__"))
     hierarchy.remove("__root__")
     log.info(f"Creating map prefixes.")
     for c in hierarchy:
         c.class_.compile_map(c.nsmap)
     # ToDo: create_all is quite slow, maybe this can be sped up. Currently low priority.
     log.info(f"Creating table metadata.")
     for child in g["__root__"]:
         child.class_.metadata.create_all(self.engine)
     log.info(f"Backend model ready.")
    def _generate_bayes_net(self):
        # Pseudo Code
        # 1. Start at any node(probably 0 since that will be the easiest for
        # indexing)
        # 2. At each node figure out the conditional probability
        # 3. Add it to the new graph (which should probably be directed)
        # 4. Find unprocessed adjacent nodes
        # 5. If any go to 2
        #    Else return the bayes net'

        # Will it be possible that zero is not the root? If so, we need to pick
        # one
        root = 0

        samples = np.asarray(self.samples)

        self.bayes_net = nx.bfs_tree(self.spanning_graph, root)

        for parent, child in self.bayes_net.edges():

            parent_array = samples[:, parent]

            # Check if node is root
            if not self.bayes_net.predecessors(parent):
                parent_probs = np.histogram(
                    parent_array,
                    (np.max(parent_array) + 1),
                )[0] / float(parent_array.shape[0])

                self.bayes_net.node[parent]["probabilities"] = dict(
                    enumerate(parent_probs))

            child_array = samples[:, child]

            unique_parents = np.unique(parent_array)
            for parent_val in unique_parents:
                parent_inds = np.argwhere(parent_array == parent_val)
                sub_child = child_array[parent_inds]

                child_probs = np.histogram(
                    sub_child,
                    (np.max(sub_child) + 1),
                )[0] / float(sub_child.shape[0])

                # If P(0) = 1 then child_probs = [1.]
                # must append zeros to ensure output consistency
                while child_probs.shape[0] < unique_parents.shape[0]:
                    child_probs = np.append(child_probs, 0.)
                self.bayes_net.node[child][parent_val] = dict(
                    enumerate(child_probs))

            self.bayes_net.node[child] = dict(
                probabilities=self.bayes_net.node[child])
Esempio n. 44
0
    def _get_degree_of_centrality_weight(self, wd_id):
        predecessors = nx.bfs_tree(self._graph, wd_id, reverse=True)
        connected_leafs = 0

        for p in predecessors:  # determine the number of leaf event nodes connected
            if p not in self.nodes:  # if redundant nodes are removed
                continue

            if self.nodes[p]["node_type"] == "leaf":
                connected_leafs += 1

        return 1 - (connected_leafs - 1) / self.num_leafs  # Eq. (5) of the paper
Esempio n. 45
0
def Generate_zone(clusterAssment, dataSet):
    print("step 5: Generating demand...")
    for i in range(0, size(g_micro_node_list)):
        index = int(clusterAssment[i, 0])
        temp_x = dataSet[i, 0]
        temp_y = dataSet[i, 1]
        g_micro_node_list[i].zone_id = index
        g_node_zone_map[i] = index

    # Zone generation
    for zone_no in range(0, cluster_zone):
        g_zone_list.append(zone_no)
        g_zone_list[zone_no] = macroZone()
        g_zone_list[zone_no].zone_id = zone_no
        x_temp = 0
        y_temp = 0
        for i in g_node_zone_map:
            if (i == zone_no):
                g_zone_list[zone_no].micro_node_list.append(g_node_zone_map[i])
                x_temp += g_micro_node_list[i].x_coord
                y_temp += g_micro_node_list[i].y_coord

        g_zone_list[zone_no].x_coord = 1 / len(
            g_zone_list[zone_no].micro_node_list) * x_temp
        g_zone_list[zone_no].y_coord = 1 / len(
            g_zone_list[zone_no].micro_node_list) * y_temp

    # Demand generation
    G1 = nx.DiGraph()
    for link in g_micro_link_list:
        G1.add_edges_from([(link.from_node_id, link.to_node_id)])
    G2 = nx.DiGraph()
    for link in g_micro_link_list:
        G2.add_edges_from([(link.to_node_id, link.from_node_id)])

    for node in g_micro_node_list:
        node.production = len(nx.bfs_tree(G1, node.node_id))
        node.attraction = len(nx.bfs_tree(G2, node.node_id))
        g_zone_list[node.zone_id].production += node.production
        g_zone_list[node.zone_id].attraction += node.attraction
Esempio n. 46
0
def get_different_paths(g, source, min_disjoint, rand):
    # First get a BFS Tree
    stree = nx.bfs_tree(g, source)
    max_level = set_levels(stree, source, 0)

    # Then connect all nodes in the BFS Tree to all nodes
    # the on a level lower than itself (if the edge exists on the original graph)
    for slevel in range(0, max_level):
        snodes = [
            node for node in stree.nodes()
            if stree.node[node]['level'] == slevel
        ]
        for dlevel in range(slevel + 1, max_level + 1):
            dnodes = [
                node for node in stree.nodes()
                if stree.node[node]['level'] == dlevel
            ]
            for src in snodes:
                for dst in dnodes:
                    if g.has_edge(src, dst):
                        stree.add_edge(src, dst)

    # Connect leave nodes to each other if they don't create cycles
    leaves = [node for node in stree.nodes() if stree.out_degree(node) == 0]
    while len(leaves) > 0:
        node = rand.choice(leaves)
        for neighbor, _ in g.in_edges(node):
            if not stree.has_edge(node, neighbor):
                stree.add_edge(neighbor, node)
        # New leaves
        new_leaves = [
            node for node in stree.nodes() if stree.out_degree(node) == 1
        ]
        if set(new_leaves) == set(leaves):
            # We hit a stable point and we cannot connect new leaves
            # Without creating cycles
            break
        else:
            leaves = new_leaves

    all_paths = []
    for node in g.nodes():
        if node == source:
            continue
        paths = list(nx.all_simple_paths(stree, source, node))
        unique = []
        for path in paths:
            if path not in unique:
                unique.append(path)
        if len(paths) >= min_disjoint:
            all_paths.append(paths)
    return all_paths
Esempio n. 47
0
def create_node_ordered_graph(G, node_order='DFS'):
    node_degree_list = [(n, d) for n, d in G.degree()]
    adj_0 = np.array(nx.to_numpy_matrix(G))

    ### Degree descent ranking
    # N.B.: largest-degree node may not be unique
    degree_sequence = sorted(node_degree_list,
                             key=lambda tt: tt[1],
                             reverse=True)
    adj_1 = np.array(
        nx.to_numpy_matrix(G, nodelist=[dd[0] for dd in degree_sequence]))

    ### Degree ascent ranking
    degree_sequence = sorted(node_degree_list, key=lambda tt: tt[1])
    adj_2 = np.array(
        nx.to_numpy_matrix(G, nodelist=[dd[0] for dd in degree_sequence]))

    ### BFS & DFS from largest-degree node
    CGs = [G.subgraph(c) for c in nx.connected_components(G)]

    # rank connected componets from large to small size
    CGs = sorted(CGs, key=lambda x: x.number_of_nodes(), reverse=True)

    node_list_bfs = []
    node_list_dfs = []
    for ii in range(len(CGs)):
        node_degree_list = [(n, d) for n, d in CGs[ii].degree()]
        degree_sequence = sorted(node_degree_list,
                                 key=lambda tt: tt[1],
                                 reverse=True)

        bfs_tree = nx.bfs_tree(CGs[ii], source=degree_sequence[0][0])
        dfs_tree = nx.dfs_tree(CGs[ii], source=degree_sequence[0][0])

        node_list_bfs += list(bfs_tree.nodes())
        node_list_dfs += list(dfs_tree.nodes())

        adj_3 = np.array(nx.to_numpy_matrix(G, nodelist=node_list_bfs))
        adj_4 = np.array(nx.to_numpy_matrix(G, nodelist=node_list_dfs))

        if node_order == 'degree_decent':
            adj_list = [adj_1]
        elif node_order == 'degree_accent':
            adj_list = [adj_2]
        elif node_order == 'BFS':
            adj_list = [adj_3]
        elif node_order == 'DFS':
            adj_list = [adj_4]
        else:
            adj_list = [adj_0]

    return adj_list
Esempio n. 48
0
def tree_interpolation(G, v, weight='weight'):
    """
    Interpolation on a tree domain.

    A purely networkx based interface and implementation.

    Parameters
    ----------
    G : networkx Graph
        Undirected weighted acyclic networkx graph.
    v : dict
        Sparse map from vertex to value.
        Keys not in G will be ignored.
    weight : string or None, optional
        The edge attribute that hold the numerical value used as a weight.

    Returns
    -------
    v_interp : dict
        Sparse map from vertex to value.
        All nodes in G should be represented as keys in this dict.

    """
    if weight is None:
        raise NotImplementedError(
                'interpolation is implemented only for weighted trees')
    nnodes = nx.number_of_nodes(G)
    if not nnodes:
        return {}
    if not nx.is_connected(G):
        raise ValueError('expected a tree, but the graph is not connected')
    edge_count = 0
    for na, nb in G.edges():
        if G[na][nb][weight] <= 0:
            raise ValueError('edge weights must be positive')
        edge_count += 1
    if edge_count != nnodes - 1:
        raise ValueError('expected a tree')
    observed_nodes = set(v) & set(G)
    # if no value is known on the tree then set all values to zero
    if not observed_nodes:
        return dict((node, 0) for node in G)
    vlocal = dict((node, v[node]) for node in observed_nodes)
    # If all values are known then no more work is needed.
    # Note that this will include the case of the single-node tree.
    if observed_nodes == set(G):
        return vlocal

    # construct the arborescence rooted at an observed node
    root = _get_first(observed_nodes)
    A = nx.bfs_tree(G, root)
    return arborescence_interpolation(A, root, vlocal, weight=weight)
Esempio n. 49
0
def get_connected_scales(scale: str, graph: Optional[MultiDiGraph] = None):
    """Return list of connected scales for given scale.

    Arguments:
        scale: The scale to start.
        graph: The graph to look up. Defaults to ExpansionScheme graph.
    """
    graph = graph or construct_scale_graph()
    return (
        list(node for node in bfs_tree(graph, scale).nodes if node != scale)
        if scale in graph
        else list()
    )
Esempio n. 50
0
def main():
    data = get_dataset()
    G = nx.Graph()
    nodes = data.columns
    for i in range(len(nodes)):
        for j in range(i + 1, len(nodes)):
            G.add_edge(nodes[i], nodes[j], weight=mi(nodes[i], nodes[j], data))

    T = nx.maximum_spanning_tree(G)
    terminal_nodes = [x for x in T.nodes() if T.degree(x) == 1]
    root = random.choice(terminal_nodes)
    print(root)
    T = nx.bfs_tree(T, root)
Esempio n. 51
0
 def common_parent_distances(self, u):
     dists = defaultdict(list)
     for v in self.network.N.predecessors_iter(u):
         self.assign_distances_tree(
             networkx.bfs_tree(self.network.N, v, reverse=True), v, dists)
     # get a list of nodes who have distances from all predecessors
     # finally, filter non-pareto minimal items (which are not lowest ancestors)
     print("filtered: %s" % str(
         filter(lambda x: len(x) == self.network.N.in_degree(u),
                dists.values())))
     return pareto_mins(
         filter(lambda x: len(x) == self.network.N.in_degree(u),
                dists.values()))
Esempio n. 52
0
def isthereapath(G,s,t):
    bfs = nx.bfs_tree(G,s)
    path = []
    pred = bfs.predecessors(t)[0] if t in bfs.pred else None
    while pred and (pred != 0):
        path.append(pred)
        pred = bfs.predecessors(pred)[0] if pred in bfs.pred else None


    if len(path) > 0:
        return list(reversed(list(zip(path+[s],[t]+path))))
    else:
        return path
Esempio n. 53
0
def runRecursiveDP(G, k):
    #makeMatrix(G, G.number_of_nodes())

    storePayoff = [ [ [None] * (k+1) for _ in range(G.number_of_nodes())] for _ in range(2)]

    witness = [ [ [None] * (k+1) for _ in range(G.number_of_nodes())] for _ in range(2)]
    nodes_tup = sorted(G.degree, key=lambda x: x[1], reverse=True) #sort by highest degree node
    #print("root is", nodes_tup[0][0]) #take top degree node as root
    root = nodes_tup[0][0]
    tree = nx.bfs_tree(G, root)
    recursiveDP(G, tree, k, root, storePayoff, witness)
    cc.clearVisitedNodesAndDictionaries(G)
    return storePayoff[0][root][k], storePayoff[1][root][k]
Esempio n. 54
0
def create_embeddings(filename, rootnode, target_filename, lambda_factor=0.6):
    df = pd.read_csv(filename)
    # Create the Directed Graph
    try:
        G = nx.from_pandas_edgelist(df,
                                    source='parent',
                                    target='child',
                                    create_using=nx.DiGraph())
    except KeyError:
        G = nx.from_pandas_edgelist(df,
                                    source='object',
                                    target='subject',
                                    create_using=nx.DiGraph())
    # create tree by specifying root node
    tree = nx.bfs_tree(G, rootnode)  #
    # find level of node(shortest path from root to current node)
    optional_attrs = nx.shortest_path_length(tree, rootnode)
    nx.set_node_attributes(tree, optional_attrs, 'node_level')

    ls_leafnodes = [node for node in tree.nodes()]
    pairs = list(itertools.product(ls_leafnodes,
                                   repeat=2))  # create pair of all nodes
    all_ancestors = nx.algorithms.all_pairs_lowest_common_ancestor(
        tree,
        pairs=pairs)  # get lowest common ancestors of alll pairs of nodes

    # replace ancestor node with its level in the hierarchy
    ls_ancestors_levels = {}
    for i in all_ancestors:
        ls_ancestors_levels[i[0]] = tree.node[i[1]]['node_level']

    chunked_data = [[k[0], k[1], v] for k, v in ls_ancestors_levels.items()]
    df_nodes = pd.DataFrame(chunked_data)
    df_nodes = df_nodes.rename(columns={0: 'node1', 1: 'node2', 2: 'weight'})
    depth = df_nodes.weight.max()  # find the maximum levels in the hierarchy

    # create adjancey matrix
    vals = np.unique(df_nodes[['node1', 'node2']])
    df_nodes = df_nodes.pivot(index='node1', columns='node2',
                              values='weight').reindex(columns=vals,
                                                       index=vals,
                                                       fill_value=0)

    df_adjacency = df_nodes.apply(lambda x: np.power(lambda_factor, depth - x))

    # set diagnoal to 1
    pd.DataFrame.set_diag = set_diag
    df_adjacency.set_diag(1)
    df_adjacency.fillna(0, inplace=True)

    df_adjacency.to_csv(target_filename)
Esempio n. 55
0
    def generate_aht(
        self,
        aht_graph_creation_fn: Callable,
        filter_relation_fn: Callable = None,
        aht_graph_weight_name: str = "pagerank",
        metric_for_aspect_with_max_weight="pagerank",
        aspects_to_skip=None,
        with_aspect_filtering: bool = False,
    ):
        logging.info(f"Experiments for:  {self.paths.experiment_path}")

        discourse_trees_df = (self.extract_discourse_trees().pipe(
            self.extract_discourse_trees_ids_only).pipe(
                self.extract_sentiment).pipe(self.extract_aspects).pipe(
                    self.extract_edu_rhetorical_rules))

        if with_aspect_filtering:
            discourse_trees_df = self.filter_rules_based_on_aspects_freq(
                discourse_trees_df)

        mlflow.log_metric("discourse_tree_df_len", len(discourse_trees_df))

        graph = self.build_aspect_to_aspect_graph(discourse_trees_df,
                                                  filter_relation_fn,
                                                  aspects_to_skip)
        mlflow.log_metric("aspect_2_aspect_graph_edges",
                          graph.number_of_edges())
        mlflow.log_metric("aspect_2_aspect_graph_nodes",
                          graph.number_of_nodes())
        graph, _ = self.add_sentiments_and_weights_to_nodes(
            graph, discourse_trees_df)
        aht_graph = aht_graph_creation_fn(
            graph,
            max_number_of_nodes=self.aht_max_number_of_nodes,
            weight=aht_graph_weight_name,
        )

        serializer.save(aht_graph, self.paths.aspect_hierarchical_tree)

        aspect_with_max_weight = sort_networkx_attributes(
            nx.get_node_attributes(aht_graph,
                                   metric_for_aspect_with_max_weight))[0]

        aht_graph_directed = nx.bfs_tree(aht_graph,
                                         aspect_with_max_weight,
                                         reverse=True)
        draw_tree(
            aht_graph_directed,
            self.paths.experiment_path /
            f"aht_for_{self.paths.experiment_name}",
        )
Esempio n. 56
0
    def compute_node_graph(self):
        gr_undirected = self.to_undirected()
        num_nodes = gr_undirected.number_of_nodes()

        # Remove disconnected nodes from the graph
        component_nodes = nx.node_connected_component(gr_undirected,
                                                      self.root_node)
        for node in list(gr_undirected.nodes()):
            if node not in component_nodes:
                gr_undirected.remove_node(node)

        # Find bfs tree of the connected components
        bfs_tree = nx.bfs_tree(gr_undirected, self.root_node)

        # Position the nodes in a circular fashion according to the bfs tree
        pos = gpos.hierarchy_pos(bfs_tree,
                                 root=self.root_node,
                                 width=2 * math.pi,
                                 xcenter=0.5)

        graph_nodes = []
        graph_edges = []
        index_mapper = {}

        node_id = 0
        max_x = max_y = 0.0001
        for _id, (theta, r) in pos.items():
            index_mapper[_id] = node_id
            node = get_nx_node(gr_undirected, _id)
            node['id'] = node_id
            node_id += 1

            # convert from polar coordinates to cartesian coordinates
            x = r * math.sin(theta) * num_nodes
            y = r * math.cos(theta) * num_nodes
            node['pos'] = [x, y]
            graph_nodes.append(node)

            # max values to be used for normalization
            max_x = max(abs(x), max_x)
            max_y = max(abs(y), max_y)

        # Normalize the positions
        for node in graph_nodes:
            node['pos'][0] /= max_x
            node['pos'][1] /= max_y

        for edge in gr_undirected.edges():
            graph_edges.append((index_mapper[edge[0]], index_mapper[edge[1]]))

        return {'node': graph_nodes, 'edge': graph_edges}
def main(args):

    # define the number of histories to sample
    nsamples = args.nsamples

    # validate table name
    if not args.table.isalnum():
        raise Exception('table name must be alphanumeric')

    # read and validate the rate matrix info from the sqlite3 database file
    conn = sqlite3.connect(args.rates)
    cursor = conn.cursor()
    states, distn, Q = get_rate_matrix_info(cursor)
    conn.close()

    # open the tree db file
    conn = sqlite3.connect(args.tree)
    cursor = conn.cursor()
    G = get_unrooted_tree(cursor)
    conn.close()

    # define the root node index for sampling;
    # the root is either user supplied,
    # or is taken to be the smallest node index otherwise
    vertices = sorted(G)
    if args.root is None:
        root = vertices[0]
    elif args.root in vertices:
        root = args.root
    else:
        raise Exception('the specified root should be a node index in the tree')

    # Build a directed breadth first tree starting at the distinguished vertex.
    # Note that the tree built by nx.bfs_tree and the edges yielded
    # by nx.bfs_edges do not retain the edge attributes.
    G_dag = nx.bfs_tree(G, root)
    for a, b in G_dag.edges():
        G_dag[a][b]['blen'] = G[a][b]['blen']

    # sample the unconditional history or histories
    rates, P = cmedbutil.decompose_rates(Q)
    conn = sqlite3.connect(args.outfile)
    if args.nsamples == 1:
        build_single_history_table(
                conn, args.table, root, G_dag, distn, states, rates, P)
    else:
        build_multiple_histories_table(
                nsamples,
                conn, args.table, root, G_dag, distn, states, rates, P)
    conn.close()
Esempio n. 58
0
 def enforceMaxParallel(self):
     if self.maxParallelSubtrees is None or len(self.depTree.nodes()) <= 2:
         return
     assert self.maxParallelSubtrees > 0
     tree = self.depTree.copy()
     # remove followOn edges
     for edge in list(tree.edges()):
         if self.isFollowOn(edge[0], edge[1]):
             tree.remove_edge(edge[0], edge[1])
     # get roots
     roots = []
     for node in tree.nodes():
         if len(tree.in_edges(node)) == 0:
             roots.append(node)
     # for each root, independently reduce the number of leaves
     for root in roots:
         leaves = []
         for node in NX.bfs_tree(tree, root):
             if (len(tree.out_edges(node)) == 0 and
                 not self.isVirtual(node)):
                 leaves.append(node)
         numLeaves = len(leaves)
         # sort leaves by chain length (lowest at end)
         leafChains = [self.getChainParent(tree, x) for x in leaves]
         leafChains.sort()
         leaves = [x[1] for x in leafChains]
         leaves.reverse()
         while numLeaves > self.maxParallelSubtrees:
             # try to insert the last leaf of the list above
             # its sibling
             leaf = leaves[-1]
             chainParent = self.getChainParent(tree, leaf)[2]
             parents = [x for x in tree.predecessors(chainParent)]
             assert len(parents) == 1
             parent = parents[0]
             children = [x for x in tree.successors(parent)]
             for child in children:
                 if child != chainParent and chainParent != root:
                     tree.add_edge(leaf, child)
                     self.depTree.add_edge(leaf, child)
                     tree.remove_edge(parent, child)
                     self.depTree.remove_edge(parent, child)
                     numLeaves -= 1
                     break
             del leaves[-1]
         assert numLeaves <= self.maxParallelSubtrees
def test_arborescence_interpolation():
    root = 2
    G = nx.Graph()
    G.add_weighted_edges_from([
        (root, 0, 1.0),
        (root, 1, 2.0),
        ])
    A = nx.bfs_tree(G, root)
    for na, nb in A.edges():
        A[na][nb]['weight'] = G[na][nb]['weight']
    v_in = {0 : 0.5, 1 : 0.6}
    v_out = arborescence_interpolation(A, root, v_in)
    print(v_out)

    # interpolation
    v_out_linalg = _linalg_tree_interpolation(G, (0, 1), (0.5, 0.6))
    print(v_out_linalg)
Esempio n. 60
0
    def _generate_bayes_net(self):
        # Pseudo Code
        # 1. Start at any node(probably 0 since that will be the easiest for
        # indexing)
        # 2. At each node figure out the conditional probability
        # 3. Add it to the new graph (which should probably be directed)
        # 4. Find unprocessed adjacent nodes
        # 5. If any go to 2
        #    Else return the bayes net'

        # Will it be possible that zero is not the root? If so, we need to pick
        # one
        root = 0

        samples = np.asarray(self.samples)

        self.bayes_net = nx.bfs_tree(self.spanning_graph, root)

        for parent, child in self.bayes_net.edges():

            parent_array = samples[:, parent]
            # Check if node is root
            if not self.bayes_net.predecessors(parent):
                bins = np.max(parent_array)+1
                hist = np.histogram(parent_array, bins=bins)
                parent_probs = hist[0] / float(parent_array.shape[0])
                self.bayes_net.node[parent]["probabilities"] = dict(enumerate(parent_probs))

            child_array = samples[:, child]

            unique_parents = np.unique(parent_array)
            for parent_val in unique_parents:
                parent_inds = np.argwhere(parent_array == parent_val)
                sub_child = child_array[parent_inds]
                bins = np.max(sub_child)+1
                hist = np.histogram(sub_child, bins=bins)
                child_probs = hist[0] / float(sub_child.shape[0])

                # If P(0) = 1 then child_probs = [1.]
                # must append zeros to ensure output consistency
                while child_probs.shape[0] < unique_parents.shape[0]:
                    child_probs = np.append(child_probs, 0.)
                self.bayes_net.node[child][parent_val] = dict(enumerate(child_probs))

            self.bayes_net.node[child] = dict(probabilities=self.bayes_net.node[child])