Ejemplo n.º 1
0
 def asim_jac_nodes_graded(self, graph_premise, graph_hypothesis):
     """
     Asymmetric Jaccard similarity between the nodes of the definition graphs
     :param graph_premise: the definition graph of the premise
     :param graph_hypothesis: the definition graph of the hypothesis
     :return: the ratio of overlapping nodes per the length of the hypothesis definition
     """
     prem = [(node, self.clear_node(node))
             for node in graph_premise.G.nodes]
     hyp = [(node, self.clear_node(node))
            for node in graph_hypothesis.G.nodes]
     sim = 0
     for hyp_node in hyp:
         if hyp_node[1] in [p[1] for p in prem]:
             try:
                 shortest_path = algorithms.shortest_path(
                     graph_premise.G, graph_premise.root,
                     prem[[p[1] for p in prem].index(hyp_node[1])][0])
                 print("ok")
                 hypothesis_path = algorithms.shortest_path(
                     graph_hypothesis.G, graph_hypothesis.root, hyp_node[0])
                 if shortest_path in [0, 1]:
                     sim += max(hypothesis_path, 2)
                 elif hypothesis_path == 0:
                     sim += 1
                 else:
                     sim += hypothesis_path / (shortest_path - 1)
             except Exception as e:
                 sim += 0.5
     if sim == 0 or len(hyp) == 0:
         print(sim)
         return 0
     else:
         return float(sim) / float(len(hyp))
Ejemplo n.º 2
0
    def get_subgroup(self):
        """
        Generate the subgroup from the graph as pi_1^X(G).
        """
        G_pos = self.G_pos
        T = maximum_spanning_tree(G_pos.to_undirected())
        # Because in list(T.edges), src and dst may be messed up,
        # we have to check each one
        T_edges = []
        for src, dst, letter in T.edges(data='label'):
            for key, eattr in self.G[src][dst].items():
                if eattr['label'] == letter:
                    T_edges.append((src, dst, letter))
                    break
                elif eattr['label'] == -letter:
                    T_edges.append((dst, src, letter))
                    break

        G_pos_edges = G_pos.edges(data='label')

        gens = []

        edges_to_add = [edge for edge in G_pos_edges if edge not in T_edges]

        origin = (0, )
        for src, dst, letter in edges_to_add:
            gen = self.F(1)
            gen *= self.read_path(shortest_path(T, origin, src), T_edges)
            gen *= self.F([letter])
            gen *= self.read_path(shortest_path(T, dst, origin), T_edges)
            gens.append(gen)

        assert len(gens) == self.rank(), "Something is wrong."
        return gens
Ejemplo n.º 3
0
def _find_shortest_path(G, source, target):
    try:
        shortest_p = shortest_path(G, source, target)
    except NetworkXNoPath as e:
        return e
    else:
        return shortest_p
Ejemplo n.º 4
0
def calculate_move_human(human, cat, net):
    try:
        matches = shortest_path(net, source=human.current_station, target=cat.current_station)
    except nx.NetworkXNoPath:
        # no path (ei. all destination station are closed)
        human.can_move = False
        return
    if len(matches) > 1:
        if net.node[matches[1]]['closed']:
            matches = list(all_shortest_paths(net, source=human.current_station, target=cat.current_station)
                           )
            paths = []
            for path_id, sub_list in enumerate(matches):
                for c in sub_list:
                    if net.node[c]['closed']:
                        paths.append(False)
                        break
                if len(paths) < path_id:
                    paths.append(True)
            if True in paths:
                human.count_move += 1
                human.current_station = matches[path_id][1]
            else:
                human.can_move = False
        else:
            human.count_move += 1
            human.current_station = matches[1]
Ejemplo n.º 5
0
    def subgraph_to_json(self, adj_list):
        output = []

        for src in adj_list:
            for dest in adj_list:
                if src in self.nx_graph and dest in self.nx_graph:
                    try:
                        path = shortest_path(self.nx_graph, src, dest)
                        for i in range(0, len(path) - 1):
                            v0 = path[i]
                            v1 = path[i + 1]
                            edges = self.nx_graph[v0][v1]
                            adverbs = []
                            for edge_dict in edges.itervalues():
                                adverb = edge_dict['adv']
                                adverbs.append(adverb)
                            output.append({
                                'source':
                                v0,
                                'target':
                                v1,
                                'relationshipWord':
                                '; '.join(adverbs)
                            })
                    except NetworkXNoPath as e:
                        print str(e)

        return json.dumps(output)
Ejemplo n.º 6
0
def getLeavesFromGraphImposingDistanceFromOrigin(G: nx.DiGraph, path_lenght_from_main:int=ORIGIN_TO_LEAVES_DIST, origin_name:str=ORIGIN_NAME) -> list:
  leaves = []
  for n in G.nodes:
    #print(f'verify - ({G.in_degree(n)}, {len(nx.algorithms.shortest_paths.generic.shortest_path(G, n, "culture"))}) ({n})')
    if G.in_degree(n) == 0 and len(shortest_path(G, n, origin_name)) == path_lenght_from_main:
      leaves.append(n)
  return leaves
Ejemplo n.º 7
0
    def reset_gripper (self, lr, transforms, ar, hydra=None):
        """
        Resets gripper with new lr value and a list of transforms
        to populate transform graph. Input also contains ar marker and hydra
        Each transform in list is dict with 'parent', 'child' and 'tfm'
        The resulting graph edges will contain transform between every pair of nodes in the same connected subgraph
        """
        self.lr = lr
        self.ar_marker = ar
        self.hydra_marker = hydra
        
        self.transform_graph = nx.DiGraph()
        for tfm in transforms:
            self.transform_graph.add_edge(tfm['parent'], tfm['child'])
            self.transform_graph.edge[tfm['parent']][tfm['child']] = tfm['tfm']
            self.transform_graph.edge[tfm['child']][tfm['parent']] = nlg.inv(tfm['tfm'])

        
        for i,j in itertools.combinations(sorted(self.transform_graph.nodes()), 2):
            if not self.transform_graph.has_edge(i,j):
                ij_path = nxa.shortest_path(self.transform_graph, i, j)
                Tij = self.transform_graph.edge[ij_path[0]][ij_path[1]]
                for p in xrange(2,len(ij_path)):
                    Tij = Tij.dot(self.transform_graph.edge[ij_path[p-1]][ij_path[p]])
                Tji = nlg.inv(Tij)
                
                self.transform_graph.add_edge(i,j)
                self.transform_graph.add_edge(j,i)
                self.transform_graph.edge[i][j] = Tij
                self.transform_graph.edge[j][i] = Tji
        
        for node in self.transform_graph.nodes_iter():
            self.transform_graph.edge[node][node] = np.eye(4)
Ejemplo n.º 8
0
    def find_path(self,
                  downstream: Module, upstream: Module,
                  ignore_paths: Optional[List[ImportPath]] = None) -> List[Module]:
        """
        Find a list of module names showing the dependency path between the downstream and the
        upstream module, or None if there is no dependency.

        For example, given an upstream module a and a downstream module d:

                - [d, a] will be returned if d directly imports a.
                - [d, c, b, a] will be returned if d imports c, which imports b, which imports a.
                - None will be returned if d does not import a (even indirectly).
        """
        logger.debug("Finding path from '{}' up to '{}'.".format(downstream, upstream))
        ignore_paths = ignore_paths if ignore_paths else []

        removed_paths = self._remove_paths_from_networkx_graph(ignore_paths)

        try:
            path = shortest_path(self._networkx_graph, downstream, upstream)
        except (networkx.NetworkXNoPath, networkx.exception.NodeNotFound):
            # Either there is no path, or one of the modules doesn't even exist.
            path = None
        else:
            path = tuple(path)

        self._restore_paths_to_networkx_graph(removed_paths)

        return path
Ejemplo n.º 9
0
    def handle(self, p_ipv4, msg, in_port, eth, callback):
        datapath = msg.datapath
        ofproto = datapath.ofproto

        LOG.debug("--- ICMP Packet!: \nIP Address src:%s\nIP Address Dest:%s\n",
                  p_ipv4.src, p_ipv4.dst)

        if self.networkMap.findActiveHostByMac(eth.dst):
            LOG.debug("This adress has been found!")
            if self.networkMap.isInactiveHost(eth.src):
                LOG.debug("Activate Host...")
                self.networkMap.addActiveHost(
                    datapath, msg.match['in_port'], host.host(eth.src, p_ipv4.src))
            out_port = self.networkMap.findPortByHostMac(eth.dst).port_no
        else:
            out_port = ofproto.OFPP_FLOOD

        actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]
        #LOG.debug("out_port to the destination host is ", out_port)
        # install a flow to avoid packet_in next time
        if out_port != ofproto.OFPP_FLOOD:
            if (self.networkMap.findSwitchByDatapath(datapath) != self.networkMap.findSwitchByHostMac(eth.dst)):

                LOG.debug("###More than one Switch detected###")
                path = nx.shortest_path(self.networkMap.networkMap, self.networkMap.findSwitchByDatapath(
                    datapath), self.networkMap.findSwitchByHostMac(eth.dst))
                print("---- Way to go ", str(path))
                for item in range(1, (len(path) - 1)):
                    if isinstance(path[item], Port) and isinstance(path[item - 1], Switch):
                        datapath = path[item - 1].dp
                        port_no = path[item].port_no
                        match = datapath.ofproto_parser.OFPMatch(
                            in_port=in_port, ipv4_dst=p_ipv4.dst, ipv4_src=p_ipv4.src, eth_type=0x0800)
                        actions = [
                            datapath.ofproto_parser.OFPActionOutput(port_no)]
                        #LOG.debug("out_port to the next hope is", str(port_no))
                        #self.add_flow(datapath, port_no, actions, match)
                        callback(datapath, port_no, actions, match)

                    else:
                        LOG.debug("---- Error in establishing multiflow.")

                LOG.debug("###TO BE IMPLEMENTED###")
            else:
                match = datapath.ofproto_parser.OFPMatch(
                    in_port=in_port, ipv4_dst=p_ipv4.dst, ipv4_src=p_ipv4.src, eth_type=0x0800)

                callback(datapath, out_port, actions, match)


        data = None

        if msg.buffer_id == ofproto.OFP_NO_BUFFER:
            data = msg.data

        out = datapath.ofproto_parser.OFPPacketOut(
            datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port,
            actions=actions, data=data)
        datapath.send_msg(out)
Ejemplo n.º 10
0
def get_path(graph, sender, receiver) -> List[int]:
    while not (receiver != sender
               and has_path(graph, source=sender, target=receiver)):
        if receiver == sender:
            receiver = random.choice(list(graph))
        if not has_path(graph, source=sender, target=receiver):
            sender = random.choice(list(graph))
            receiver = random.choice(list(graph))
    return shortest_path(graph,
                         source=sender,
                         target=receiver,
                         weight='base_fee')
Ejemplo n.º 11
0
def get_contigs(G, start_node_list, end_node_list):
    """Get all paths from input to output nodes : returns a list of contigs and their size"""
    contigs = []
    for source in start_node_list:
        for target in end_node_list:
            if algorithms.has_path(G, source, target) == True:
                path = algorithms.shortest_path(G, source, target)
                contig = path[0]
                for i in range(len(path) - 1):
                    contig += path[i + 1][-1]
                contigs.append((contig, len(contig)))
    return contigs
Ejemplo n.º 12
0
def build_path(env, cur, target):
    start = env.point_ll_to_ft(cur)
    end = env.point_ll_to_ft(target)
    start = (round_to_granularity(env, start[0]),
             round_to_granularity(env, start[1]))
    end = (round_to_granularity(env,
                                end[0]), round_to_granularity(env, end[1]))
    path = shortest_path(env.graph.base_graph, start, end, weight='weight')
    path = trim_path(path)
    path = np.array([[p[1] for p in path], [p[0] for p in path]]) * float(
        env.granularity)
    path = env.ft_to_ll(path)
    return path
    def find_SideOfEdge(self, ConnectDf, Source_Verts, Target_Verts,
                        Edge_Verts):
        edge_set = set(Edge_Verts)
        # build a net only contain the edge
        onEdge_N = self.counts_contain(Verts_Array=ConnectDf.values,
                                       VertsNeeded_set=edge_set)
        g_edge = nx.from_pandas_edgelist(ConnectDf[onEdge_N == 2], 'source',
                                         'target', ['Weight']).to_undirected()
        # edge on the one side of the max length path
        ShortestPath_OnEdge1 = shortest_path(G=g_edge,
                                             source=Source_Verts,
                                             target=Target_Verts,
                                             weight='Weight')

        # build a edge net without the Verts on the ShortestPath_OnEdge1
        edgeSet_shortestRm = (edge_set - set(ShortestPath_OnEdge1)).union(
            set([Source_Verts, Target_Verts]))
        onEdge_N1 = self.counts_contain(Verts_Array=ConnectDf.values,
                                        VertsNeeded_set=edgeSet_shortestRm)
        g_edge1 = nx.from_pandas_edgelist(ConnectDf[onEdge_N1 == 2], 'source',
                                          'target',
                                          ['Weight']).to_undirected()
        # edge on the other side of the max length path
        try:
            ShortestPath_OnEdge2 = shortest_path(G=g_edge1,
                                                 source=Source_Verts,
                                                 target=Target_Verts,
                                                 weight='Weight')
        except:
            ShortestPath_OnEdge2 = list(edgeSet_shortestRm)
            print('Edge incomplete, maybe not a closed circle!')

        ShortestPath_OnEdge1 = np.array(ShortestPath_OnEdge1, dtype=np.int32)
        ShortestPath_OnEdge2 = np.array(ShortestPath_OnEdge2, dtype=np.int32)
        return {
            'OneSideOfEdge_Verts': ShortestPath_OnEdge1,
            'OtherSiderOfEdge_Verts': ShortestPath_OnEdge2
        }
Ejemplo n.º 14
0
def get_contigs(graph, list_start_node, list_end_node):
    '''takes a graph, a list of entry nodes and a list of exit nodes
    and returns a list of tuple (contig, contig_size)
    '''
    contigs = []
    for source in list_start_node:
        for target in list_end_node:
            if algorithms.has_path(graph, source, target) == True:
                path = algorithms.shortest_path(graph, source, target)
                contig = path[0]
                for i in range(len(path) - 1):
                    contig += path[i + 1][-1]
                contigs.append((contig, len(contig)))
        return contigs
 def find_MaxLengthPath(self, ConnectDf, Source_Verts, Target_Verts):
     g = nx.from_pandas_edgelist(ConnectDf, 'source', 'target', ['Weight'])
     g_use = g.to_undirected()
     # max length path on the surface,
     # note:
     # the length of the path based on shortest path only is longer
     # than the distance generated by gdist.compute_gdist
     # maybe a bug!
     ShortestPath_Verts = shortest_path(G=g_use,
                                        source=Source_Verts,
                                        target=Target_Verts,
                                        weight='Weight')
     ShortestPath_Verts = np.array(ShortestPath_Verts, dtype=np.int32)
     return ShortestPath_Verts
Ejemplo n.º 16
0
    def simple_edge_paths(self) -> Tuple[Tuple[Transition]]:
        """Get all paths without loops that are ending with an isolated state or with the initial node.
        """
        paths = list()

        for transition in self.transitions:
            path = [
                self.get_transition(*p) for p in pairwise(
                    shortest_path(self.graph, self.initial_state,
                                  transition.from_state))
            ]
            path.append(transition)
            paths.append(path)

        return tuple(paths)
Ejemplo n.º 17
0
def get_path():
    """
    Get the shortest path
    """
    database = Database()
    graphs = database.get_graphs()

    print('The first word:')
    word1 = input()

    print('The second word:')
    word2 = input()

    if len(word2) != len(word1):
        raise AttributeError('The words are not equal!')

    print('Result:')
    try:
        print(shortest_path(graphs.get_graph(len(word1)), word1, word2))
    except NetworkXException:
        print('The words are not found!')
Ejemplo n.º 18
0
    def find_path(self, downstream, upstream, ignore_paths=None):
        """
        Args:
            downstream (string):                 Absolute name of module.
            upstream (string)                    Absolute name of module.
            ignore_paths
                (list of ImportPaths, optional): List of the paths that should not be considered.

        Returns:
            List of module names showing the dependency path between
            the downstream and the upstream module, or None if there
            is no dependency.

            For example, given an upstream module 'alpha' and a downstream
            module 'delta':

                - ['alpha'] will be returned if delta directly imports alpha.
                - ['delta', 'gamma', 'beta', 'alpha'] will be returned if delta imports
                  gamma, which imports beta, which imports alpha.
        """
        logger.debug("Finding path from '{}' up to '{}'.".format(downstream, upstream))
        ignore_paths = ignore_paths if ignore_paths else []

        removed_paths = self._remove_paths_from_networkx_graph(ignore_paths)

        try:
            path = shortest_path(self._networkx_graph, downstream, upstream)
        except (networkx.NetworkXNoPath, networkx.exception.NodeNotFound):
            # Either there is no path, or one of the modules doesn't even exist.
            path = None
        else:
            path = tuple(path)

        self._restore_paths_to_networkx_graph(removed_paths)

        return path
def soma_through_projection_to_leaf(source, skeleton_id=None, neuron=None):
    if not skeleton_id and not neuron:
        raise Exception("Must provide skeleton_id or neuron")
    if skeleton_id and not neuron:
        neuron = source.get_neuron(skeleton_id)
    try:
        soma = neuron.tags['soma']
    except KeyError:
        print("Skipping skeleton %s. No soma found" % neuron.skeleton_id)
        return None
    if any([('axon' in tag) for tag in neuron.tags]):
        projection = neuron.tags['axon']
        projection_graph = neuron.axons[projection[0]]['tree']
    else:
        try:
            projection = neuron.tags['projection']
            projection_graph = neuron.projection[projection[0]]['tree']
        except:
            return None
    longest_path = dag_longest_path(projection_graph)
    soma_to_projection = shortest_path(neuron.graph, soma[0], projection[0])
    soma_to_projection.remove(projection[0])
    projection_path = soma_to_projection + longest_path
    return projection_path
Ejemplo n.º 20
0
def get_graph_d3(old1, new1, csim, cstars, cenr, chours):
    """
    determines optimal path (shortest path)

    Parameters
    ----------

    old1 : int
    index of old topic

    new1 : int
    index of new topic

    csim : int or float
    weight for course similarity

    cstars : int or float
    weight for course rating

    cenr : int or float
    weight for course enrollment

    chours : int or float
    weight for course length

    Returns
    -------

    shortpath : array
    shortest path in the course graph
    """

    # load Graph
    file = open('networkx_graph.pkl', 'rb')
    G = pickle.load(file)
    file.close()

    # load positions
    file = open('networkx_pos.pkl', 'rb')
    pos = pickle.load(file)
    file.close()

    # load node values
    file = open('networkx_values.pkl', 'rb')
    values = pickle.load(file)
    file.close()

    # load titles
    file = open('course_titles.pkl', 'rb')
    titles = pickle.load(file)
    file.close()

    # topic scores
    mat = loadmat('scoremat.mat')
    scoremat = mat['scoremat']
    scorecorrs = cos_sim(scoremat)
    for d in range(len(scorecorrs)):
        scorecorrs[d, d] = 0
    print('corr test 1:', scorecorrs[old1, new1])

    # numeric course info
    mat = loadmat('course_numeric_info.mat')
    stars = mat['stars']
    hours = mat['hours']
    enrollment = mat['enrollment']

    Gdir = nx.DiGraph(G)
    list_edges = list(Gdir.edges)

    # add weighted costs to edges

    stars_norm = normalize_cost(stars, 1)
    enrollment_norm = normalize_cost(np.log10(enrollment), 1)
    hours_norm = normalize_cost(hours)

    weighted_costs = cstars * stars_norm + cenr * enrollment_norm + chours * hours_norm
    if np.shape(weighted_costs)[0] == 1:
        weighted_costs = weighted_costs.T

    list_weighted_costs = []
    list_weights = []
    for edge in Gdir.edges:
        sim = scorecorrs[edge[0], edge[1]]
        dissim = 1 - sim
        edge_cost = weighted_costs[edge[1]] + csim * dissim
        if edge_cost < 0:
            print(edge)
        Gdir.edges[edge[0], edge[1]]['weighted_cost'] = edge_cost
        Gdir.edges[edge[0], edge[1]]['weight'] = 1 - edge_cost
        list_weighted_costs.append(edge_cost)
        list_weights.append(1 - edge_cost)
    print(np.min(np.array(list_weighted_costs)))
    print('corr:', scorecorrs[old1, new1])
    #edge_weights = [Gdir[u][v]['weight']-.4 for u,v in G.edges()] # min is .5; -.4 so that min is .1

    # shortest path
    shortpath = shortest_path(Gdir, old1, new1, weight='weighted_cost')
    print('shortpath:', shortpath)
    mytuples = []
    mytuples_directed = []
    for i in range(len(shortpath) - 1):
        newlink = (shortpath[i], shortpath[i + 1])
        if shortpath[i] < shortpath[i + 1]:
            newlink = (shortpath[i], shortpath[i + 1])
        else:
            newlink = (shortpath[i + 1], shortpath[i])
        mytuples.append(newlink)
        newlink = (shortpath[i], shortpath[i + 1])
        mytuples_directed.append(newlink)

    # write nodes_output.csv
    # write nodes not in shortpath first so large nodes are drawn on top
    with open('static/nodes_output.csv', mode='w') as fp:
        fwriter = csv.writer(fp,
                             delimiter=',',
                             quotechar='"',
                             quoting=csv.QUOTE_MINIMAL)
        fwriter.writerow(['x', 'y', 'strength', 'radius', 'title'])
        for i in range(len(pos)):
            if i in shortpath:
                fwriter.writerow(
                    [pos[i][0], pos[i][1],
                     int(values[i]), 4, titles[i]])
            else:
                pass

    # write edges_output.csv
    with open('static/edges_output.csv', mode='w') as fp:
        fwriter = csv.writer(fp,
                             delimiter=',',
                             quotechar='"',
                             quoting=csv.QUOTE_MINIMAL)
        fwriter.writerow(['x1', 'x2', 'y1', 'y2', 'width', 'color'])
        for i in range(len(list_edges)):
            if list_edges[i] in mytuples:
                if list_edges[i] in mytuples_directed:
                    x1 = pos[list_edges[i][0]][0]
                    x2 = pos[list_edges[i][1]][0]
                    y1 = pos[list_edges[i][0]][1]
                    y2 = pos[list_edges[i][1]][1]
                else:
                    x1 = pos[list_edges[i][1]][0]
                    x2 = pos[list_edges[i][0]][0]
                    y1 = pos[list_edges[i][1]][1]
                    y2 = pos[list_edges[i][0]][1]
                fwriter.writerow([x1, x2, y1, y2, 2, '#ff0000'])

    return shortpath
Ejemplo n.º 21
0
def getNodesFromOriginDistance(G: nx.DiGraph, distance: int = 4, origin_name:str=ORIGIN_NAME) -> list:
  return [ n for n in G.nodes if len(shortest_path(G, n, origin_name)) == distance ]
Ejemplo n.º 22
0
from networkx import algorithms
import networkx
from networkx import *
import itertools
g = networkx.generators.krackhardt_kite_graph()
print(algorithms.shortest_path(g, 5, 1))
print(algorithms.dijkstra_path(g, 5, 1))
#print(dict(algorithms.all_pairs_shortest_path(g)))
#print(dict(algorithms.all_pairs_shortest_path(g))[5])
#并排显示
#lst1=[1,2]
#lst2=[5,6]
#print(lst1,lst2)
#print(*lst1)
nodes = list(g.nodes())[:8]
node_pairs = list(itertools.combinations(nodes, 2))
#print(node_pairs)
for pair in node_pairs:
    print(algorithms.shortest_path(g, *pair),
          algorithms.dijkstra_path(g, *pair))
print(node_pairs)
Ejemplo n.º 23
0
import networkx.generators.small
import networkx as net
import matplotlib.pyplot as plot
from networkx.algorithms import traversal
from networkx import algorithms


g = networkx.generators.small.krackhardt_kite_graph()
g.number_of_edges()
g.number_of_nodes()
g.adjacency_list()
dict((x, g.neighbors(x)) for x in g.nodes())
# networkx.generators.small.krackhardt_kite_graph?
net.draw(g)
plot.show()

edges=traversal.dfs_edges(g,0)
list(edges)

edges = traversal.bfs_edges(g, 0)
bfs = list(edges)

net.draw(traversal.bfs_tree(g,0))
plot.show()

algorithms.shortest_path(g,0,7)
shorts = algorithms.all_pairs_shortest_path(g)
print "all shortest lengths", shorts
    def getShortestPath(self, nodA, nodB):
        ''' Get the shortest path between two nodes. '''

        return alg.shortest_path(self.graph, nodA, nodB)
Ejemplo n.º 25
0
def answer_quest(q, talker):
    '''
  given question q, interacts with talker and returns
  its best answers
  '''
    max_answers = talker.params.max_answers
    db = talker.db
    sent_data, l2occ = db

    unknowns = []
    q_lemmas = []
    if talker.params.with_answerer:
        answerer = Talker(from_text=q)
        q_sent_data, _ = answerer.db
        for j, q_lemma in enumerate(q_sent_data[0][LEMMA]):
            q_sent_data, q_l2occ = answerer.db
            q_tag = q_sent_data[0][TAG][j]
            if q_tag[0] not in "NVJ": continue  # ppp(q_lemma,q_tag)
            q_lemmas.append((q_lemma, wn_tag(q_tag)))
    else:
        answerer = None
        from nltk.tokenize import word_tokenize
        from nltk.stem import WordNetLemmatizer
        wnl = WordNetLemmatizer()
        toks = word_tokenize(q)
        tag = None
        for t in toks:
            tag = 'n'
            l = wnl.lemmatize(t, tag)
            if l == t:
                tag = 'v'
                l = wnl.lemmatize(t, tag)
            if l == t:
                tag = 'a'
                l = wnl.lemmatize(t, tag)
            l = l.lower()
            q_lemmas.append((l, tag))

    matches = []
    nears = []
    sharesDict = defaultdict(set)
    count = defaultdict(int)

    for q_lemma, wn_q_tag in q_lemmas:
        if not good_word(q_lemma) or q_lemma in ".?": continue

        #  actual QA starts here
        ys = l2occ.get(q_lemma)

        if ys:
            matches.append(q_lemma)
            for sent, _pos in ys:
                sharesDict[sent].add(q_lemma)
                count[q_lemma] += 1
        else:
            if talker.params.expand_query > 0:
                related = wn_all(talker.params.expand_query, 3, q_lemma,
                                 wn_q_tag)
                for r_lemma in related:
                    if not good_word(q_lemma): continue
                    zs = l2occ.get(r_lemma)
                    if not zs:
                        tprint("UNKNOWNS:", q_lemma, '\n')
                        continue
                    nears.append(r_lemma)
                    tprint('EXPANDED:', q_lemma, '-->', r_lemma)
                    sharesDict[sent].add(r_lemma)
                    count[r_lemma] += 1

    print('count:', count)

    ignored = []
    for lemma in count:
        if (count[lemma] > 3):
            ignored.append(lemma)

    print('ignored:', ignored)

    lavg = talker.avg_len

    best = []
    for id in sharesDict:
        sent = sent_data[id][SENT]
        lsent = len(sent)
        if lsent > 2 * lavg:
            sharedNum = len(sharesDict[id])
            if sharedNum == 1:
                shares = list(sharesDict[id])
                if shares[0] in ignored:
                    continue
        r = 0
        for key in matches:
            if (key in ignored):
                if (key in sharesDict[id]):
                    r += 1.0
                continue

            if (nxAlg.has_path(talker.g, key, id)):
                nodes = nxAlg.shortest_path(talker.g, key, id)
                if (len(nodes) < 6):
                    n = math.pow(2, len(nodes) - 1)
                    r += 16.0 / n

        for key in nears:
            if (key in ignored):
                if (key in sharesDict[id]):
                    r += 0.5
                continue

            if (nxAlg.has_path(talker.g, key, id)):
                nodes = nxAlg.shortest_path(talker.g, key, id)
                print('****************nears, key:id=', key, ':', id,
                      ', get nodes, length:', len(nodes), 'nodes:', nodes)
                if (len(nodes) < 6):
                    n = math.pow(2, len(nodes) - 1)
                    r += 8.0 / n
        best.append((r, id, sharesDict[id], sent))

    best.sort(reverse=True)

    answers = []
    last_rank = 0
    for i, b in enumerate(best):
        if i >= max_answers: break
        #ppp(i,b)
        rank, id, shared, sent = b
        if last_rank != 0:
            if rank / last_rank < 0.70: break
        last_rank = rank
        answers.append((id, sent, round(rank, 4), shared))
    return answers, answerer
Ejemplo n.º 26
0
# Breadth First Search:
#     visit all of the immediate neighbors first and 
#     only then proceeds to their neighbors.
print ("Breadth First Search")
print (list(algorithms.traversal.bfs_edges(G, 0)))
print ()

# A walk is an alternating sequence of nodes and edges that connect them.
#    A walk is simple if no node is crossed twice.
#    A walk is open if the starting and ending nodes are different.
#    A walk is closed if the starting and ending nodes are the same.
#    The length of the walk is the number of edges.
#    A path is an open simple walk. A path can have lenght 0.
#    A cycle is a closed simple walk. A cycle can not have lenght 0.
print ("Shortest Path")
print (algorithms.shortest_path(G,0,3))
print ()

print ("Average Shortest Path")
print (algorithms.all_pairs_shortest_path(G))
print (algorithms.average_shortest_path_length(G))
print ()

# Graph Distance:
#    Unweighted distance - number of edges between two nodes
#    Weighted distance - sum of weights between two nodes. 
#        so shortest path is least combined weight, not necessarily the fewest nodes
#    Euclidean distance -  between the two nodes in the adjacency matrix: which is
#        proportional to the number of common neighbors shared between the nodes.
#
Ejemplo n.º 27
0
#!/usr/bin/env python3
import json

from networkx import DiGraph
from networkx.algorithms import shortest_path

p = json.load(open('data.json'))
p = p[1][1]
start_id = p[0][0]
g = DiGraph()
for x in p:
    g.add_node(x[0])
for i, x in enumerate(p):
    if x[3] == 8:
        g.add_edge(x[0], p[i + 1][0])
    if len(x) > 4 and x[4] is not None and x[4][0][1] is not None:
        for y in x[4][0][1]:
            g.add_edge(x[0], y[2], label=y[0])
q = shortest_path(g, 938169490, 751651474)
ll = ''
for i in range(len(q) - 1):
    d = g.get_edge_data(q[i], q[i + 1])
    l = d.get('label')
    if l:
        ll += l
ll = f'pbctf{{{ll}_s3cuR3_p1n_id_2_3v3ry0ne}}'
print(ll)
Ejemplo n.º 28
0
def short_distance():
    from networkx import algorithms
    g=generate_graph()
    print algorithms.shortest_path(g,0,5)

    print algorithms.dijkstra_path(g,0,5)
Ejemplo n.º 29
0
    def handle(self, p_ipv4, msg, in_port, eth, callback):
        datapath = msg.datapath
        ofproto = datapath.ofproto

        LOG.debug("--- ICMP Packet!: \nIP Address src:%s\nIP Address Dest:%s\n",
                  p_ipv4.src, p_ipv4.dst)

        if not self.networkMap.findActiveHostByIP(p_ipv4.src):
            self.networkMap.addActiveHost(
                datapath, msg.match['in_port'], host.host(eth.src, p_ipv4.src))

        if self.networkMap.findActiveHostByMac(eth.dst):
            LOG.debug("This adress has been found!")
            if self.networkMap.isInactiveHost(eth.src):
                LOG.debug("Activate Host...")
                self.networkMap.addActiveHost(
                    datapath, msg.match['in_port'], host.host(eth.src, p_ipv4.src))
            out_port = self.networkMap.findPortByHostMac(eth.dst).port_no
        # else:
        #     out_port = ofproto.OFPP_FLOOD

        actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]

        if out_port != ofproto.OFPP_FLOOD:
            if (self.networkMap.findSwitchByDatapath(datapath) != self.networkMap.findSwitchByHostMac(eth.dst)):

                LOG.debug("###More than one Switch detected###")
                path = nx.shortest_path(self.networkMap.networkMap, self.networkMap.findSwitchByDatapath(
                    datapath), self.networkMap.findSwitchByHostMac(eth.dst))
                print("---- Way to go ", str(path))

                print("--- IP Packet from ", str(p_ipv4.src), 
                    "to", str(p_ipv4.dst),"--from Switch", str(msg.datapath.id))
                for item in range(1, (len(path))):
                    if isinstance(path[item], Port) and isinstance(path[item - 1], Switch):
                        datapath = path[item - 1].dp
                        port_no = path[item].port_no
                        match = datapath.ofproto_parser.OFPMatch(
                            in_port=in_port, eth_src=eth.src,
                            eth_dst=eth.dst)
                        actions = [
                            datapath.ofproto_parser.OFPActionOutput(port_no)]

                        callback(datapath, port_no, actions, match)

                    else:
                        LOG.debug("---- Error in establishing multiflow.")
            else:
                match = datapath.ofproto_parser.OFPMatch(
                    in_port=in_port, eth_src=eth.src,
                    eth_dst=eth.dst)

                callback(datapath, out_port, actions, match)

        data = None

        if msg.buffer_id == ofproto.OFP_NO_BUFFER:
            data = msg.data

        out = datapath.ofproto_parser.OFPPacketOut(
            datapath=datapath, buffer_id=msg.buffer_id, in_port=in_port,
            actions=actions, data=data)
        datapath.send_msg(out)
Ejemplo n.º 30
0
    def __parse_function(self, func_node):
        try:
            # ignore standard library functions
            if func_node.displayname.startswith('__'):
                return

            # detect header only function duplicates
            file_name = func_node.location.file.name
            source_mark = (file_name, func_node.extent.start.line)
            if file_name.endswith('.h') and func_node.is_definition:
                # print('Header only function: {0}'.format(func_node.displayname))
                if source_mark in self.header_only_functions:
                    # print('Duplicate')
                    return
                else:
                    self.header_only_functions.add(source_mark)

            key = tokenize(func_node.spelling, self.max_subtokens_num)
            g = ast_to_graph(func_node, self.max_ast_depth)

            # debug_save_graph(func_node, g)

            terminal_nodes = [node for (node, degree) in g.degree() if degree == 1]
            random.shuffle(terminal_nodes)

            contexts = set()
            ends = combinations(terminal_nodes, 2)

            for start, end in ends:
                path = shortest_path(g, start, end)
                if path:
                    if self.max_path_len != 0 and len(path) > self.max_path_len:
                        continue  # skip too long paths
                    path = path[1:-1]
                    start_node = g.nodes[start]['label']
                    tokenize_start_node = not g.nodes[start]['is_reserved']
                    end_node = g.nodes[end]['label']
                    tokenize_end_node = not g.nodes[end]['is_reserved']

                    path_tokens = []
                    for path_item in path:
                        path_node = g.nodes[path_item]['label']
                        path_tokens.append(path_node)

                    context = Context(
                        tokenize(start_node, self.max_subtokens_num) if tokenize_start_node else [start_node],
                        tokenize(end_node, self.max_subtokens_num) if tokenize_end_node else [end_node],
                        Path(path_tokens, self.validate), self.validate)
                    contexts.add(context)
                if len(contexts) > self.max_contexts_num:
                    break

            if len(contexts) > 0:
                sample = Sample(key, contexts, source_mark, self.validate)
                self.samples.add(sample)
        except Exception as e:
            # skip unknown cursor exceptions
            if 'Unknown template argument kind' not in str(e):
                print('Failed to parse function : ')
                print('Filename : ' + func_node.location.file.name)
                print('Start {0}:{1}'.format(func_node.extent.start.line, func_node.extent.start.column))
                print('End {0}:{1}'.format(func_node.extent.end.line, func_node.extent.end.column))
                print(e)
Ejemplo n.º 31
0
#!/usr/bin/env python

from argparse import ArgumentParser
import pathway_tools.convert
from networkx.algorithms import shortest_path

if __name__ == "__main__":
    parser = ArgumentParser(description="Find shortest directed path between two genes")    
    parser.add_argument("--xgmml", default=None)
    parser.add_argument("gene1")
    parser.add_argument("gene2")

    args = parser.parse_args()

    handle = open(args.xgmml)
    net = pathway_tools.convert.read_xgmml(handle)

    out = []
    path = shortest_path(net, args.gene1, args.gene2)
    for i in range(len(path)-1):
        emap = net[path[i]][path[i+1]]
        ea = []
        for e in emap:
            ea.append(emap[e]['interaction'])
        estr = ",".join(ea)
        out.append( "%s\t%s\t" % (path[i], estr) )
    print "%s%s" % ("".join(out), path[-1])
    def shortest_path(self, source, target):
        """Get the shortest path between one type/function name and another."""
        # Trigger computation
        _ = self[source]

        return shortest_path(self.graph, source=source, target=target)
Ejemplo n.º 33
0
# Breadth First Search:
#     visit all of the immediate neighbors first and
#     only then proceeds to their neighbors.
print("Breadth First Search")
print(list(algorithms.traversal.bfs_edges(G, 0)))
print()

# A walk is an alternating sequence of nodes and edges that connect them.
#    A walk is simple if no node is crossed twice.
#    A walk is open if the starting and ending nodes are different.
#    A walk is closed if the starting and ending nodes are the same.
#    The length of the walk is the number of edges.
#    A path is an open simple walk. A path can have lenght 0.
#    A cycle is a closed simple walk. A cycle can not have lenght 0.
print("Shortest Path")
print(algorithms.shortest_path(G, 0, 3))
print()

print("Average Shortest Path")
print(algorithms.all_pairs_shortest_path(G))
print(algorithms.average_shortest_path_length(G))
print()

# Graph Distance:
#    Unweighted distance - number of edges between two nodes
#    Weighted distance - sum of weights between two nodes.
#        so shortest path is least combined weight, not necessarily the fewest nodes
#    Euclidean distance -  between the two nodes in the adjacency matrix: which is
#        proportional to the number of common neighbors shared between the nodes.
#
Ejemplo n.º 34
0
    def handle(self, p_ipv4, msg, in_port, eth, dst_mac, callback):
        datapath = msg.datapath
        ofproto = datapath.ofproto
        parser = datapath.ofproto_parser

        LOG.debug(
            "--- moved host ipv4 Packet!: \nIP Address src:%s\nIP Address Dest:%s\n",
            p_ipv4.src, p_ipv4.dst)

        # crafting fake arp
        src_mac = eth.src
        LOG.debug("crafting a fake arp for moved host")
        e = ethernet.ethernet(src_mac, dst_mac, ether.ETH_TYPE_ARP)
        a = arp.arp(hwtype=1,
                    proto=ether.ETH_TYPE_IP,
                    hlen=6,
                    plen=4,
                    opcode=arp.ARP_REPLY,
                    src_mac=dst_mac,
                    src_ip=p_ipv4.dst,
                    dst_mac=src_mac,
                    dst_ip=p_ipv4.src)
        p = packet.Packet()
        p.add_protocol(e)
        p.add_protocol(a)
        actions = [parser.OFPActionOutput(port=msg.match['in_port'])]
        self._send_packet(datapath, actions, p, ofproto.OFPP_CONTROLLER)

        if self.networkMap.findActiveHostByMac(eth.dst):
            LOG.debug("This adress has been found!")
            if self.networkMap.isInactiveHost(eth.src):
                LOG.debug("Activate Host...")
                self.networkMap.addActiveHost(datapath, msg.match['in_port'],
                                              host.host(eth.src, p_ipv4.src))
            out_port = self.networkMap.findPortByHostMac(eth.dst).port_no
        else:
            out_port = ofproto.OFPP_FLOOD
        if self.networkMap.findActiveHostByMac(eth.dst):
            dst_Real_IP = self.networkMap.findActiveHostByMac(eth.dst).ip
            actions = [datapath.ofproto_parser.OFPActionOutput(out_port)]

            if out_port != ofproto.OFPP_FLOOD:
                if (self.networkMap.findSwitchByDatapath(datapath) !=
                        self.networkMap.findSwitchByHostMac(eth.dst)):

                    LOG.debug("###More than one Switch detected###")

                    path1 = nx.shortest_path(
                        self.networkMap.networkMap,
                        self.networkMap.findSwitchByDatapath(datapath),
                        self.networkMap.findSwitchByHostMac(eth.dst))

                    for item in range(1, (len(path1) - 1)):
                        if isinstance(path1[item], Port) and isinstance(
                                path1[item - 1], Switch):
                            datapath = path1[item - 1].dp
                            port_no = path1[item].port_no

                            match = datapath.ofproto_parser.OFPMatch(
                                ipv4_src=p_ipv4.src,
                                ipv4_dst=p_ipv4.dst,
                                eth_type=0x0800)
                            actions = [
                                datapath.ofproto_parser.OFPActionSetField(
                                    ipv4_dst=dst_Real_IP),
                                datapath.ofproto_parser.OFPActionOutput(
                                    port_no)
                            ]

                            callback(datapath, port_no, actions, match)

                            port_no = self.networkMap.findPortByHostMac(
                                eth.src).port_no
                            match_back = datapath.ofproto_parser.OFPMatch(
                                ipv4_src=dst_Real_IP,
                                ipv4_dst=p_ipv4.src,
                                eth_type=0x0800)
                            actions = [
                                datapath.ofproto_parser.OFPActionSetField(
                                    ipv4_src=p_ipv4.dst),
                                datapath.ofproto_parser.OFPActionOutput(
                                    port_no)
                            ]

                            callback(datapath, port_no, actions, match_back)

                    # fake flow impel for the switch that has the moved host
                    datapath2 = self.networkMap.findSwitchByHostMac(eth.dst).dp
                    port_no = self.networkMap.findPortByHostMac(
                        eth.dst).port_no
                    match = datapath.ofproto_parser.OFPMatch(
                        ipv4_src=p_ipv4.src,
                        ipv4_dst=dst_Real_IP,
                        eth_type=0x0800)
                    actions = [
                        datapath.ofproto_parser.OFPActionOutput(port_no)
                    ]

                    callback(datapath2, port_no, actions, match)

                    path2 = nx.shortest_path(
                        self.networkMap.networkMap,
                        self.networkMap.findSwitchByHostMac(eth.dst),
                        self.networkMap.findSwitchByDatapath(datapath))

                    for item in range(1, (len(path2) - 1)):
                        if isinstance(path2[item], Port) and isinstance(
                                path2[item - 1], Switch):
                            datapath = path2[item - 1].dp
                            port_no2 = path2[item].port_no

                            match_back = datapath.ofproto_parser.OFPMatch(
                                ipv4_src=dst_Real_IP,
                                ipv4_dst=p_ipv4.src,
                                eth_type=0x0800)
                            actions = [
                                datapath.ofproto_parser.OFPActionOutput(
                                    port_no2)
                            ]

                            callback(datapath, port_no2, actions, match_back)

                        else:
                            LOG.debug("---- Error in establishing multiflow.")

                    LOG.debug("###TO BE IMPLEMENTED###")
                else:
                    # TBC
                    return
                    # match = datapath.ofproto_parser.OFPMatch(
                    #     in_port=in_port, ipv4_dst=p_ipv4.dst, ipv4_src=p_ipv4.src, eth_type=0x0800)

                    # callback(datapath, out_port, actions, match)

            data = None

            if msg.buffer_id == ofproto.OFP_NO_BUFFER:
                data = msg.data
def optimize_transforms (G):
    """
    Optimize for transforms in G. Assumes G is_ready.
    Returns a clique with relative transforms between all objects.
    """

    if G.number_of_edges == 0:
        return G.to_directed()

    # Index maps from nodes and edges in the optimizer variable X.
    # Also calculate the reverse map if needed.
    node_map, edge_map = {}, {}
    rev_map = {}
    idx = 0

    for obj in G.nodes_iter():
        node_map[obj] = idx
        rev_map[idx] = obj
        idx += 1
    for i,j in G.edges_iter():
        if i < j:
            edge_map[i,j] = idx
            rev_map[idx] = i,j
        else:
            edge_map[j,i] = idx
            rev_map[idx] = j,i
        idx += 1
        
    # Some predefined variables
    I3 = np.eye(3)
        
    def get_mat_from_x(X, i, j):
        """
        X is a vertical stack of variables for transformation matrices (12 variables each).         
        Returns 3x4 matrix Ti or Tij by looking into the index maps.
        """

        if j is None:
            offset = node_map[i]*12
        else:
            if i < j:
                offset = edge_map[i,j]*12
            else:
                offset = edge_map[j,i]*12
        
        Xij = X[offset:offset+12]
        return Xij.reshape([3,4], order='F')


    def f_objective (X):
        """
        Objective function to make transforms close to average transforms.
        Sum of the norms of matrix differences between each Tij and 
        """        
        obj = 0
        for i,j in edge_map:
            obj += nlg.norm(get_mat_from_x(X, i, j) - G[i][j]['avg_tfm'][0:3,:])
            
        return obj
    
    def f_constraints (X):
        """
        Constraint function to force matrices to be valid transforms (R.T*R = I_3).
        It also forces Tj = Ti*Tij.
        Also, T0 = identity transform.
        """
        con = []
        Tis, Tijs = {},{}
        for node in node_map:
            Tis[node] = get_mat_from_x(X, node, None)
        for i,j in edge_map:
            Tijs[i,j] = get_mat_from_x(X, i, j)
        
        # T0 --> identity transform.
        con.append(nlg.norm(Tis[node_map.keys()[0]] - np.eye(4)[0:3,:]))
        
        # Constrain Ris to be valid rotations
        for node in node_map.keys()[1:]:
            Ri = Tis[node][0:3,0:3]
            con.append(nlg.norm(Ri.T.dot(Ri) - I3))
        
        # Tj = Ti*Tij
        # Rij.T*Rij = I
        for i,j in edge_map:
            Ti = np.r_[Tis[i],np.array([[0,0,0,1]])]
            Tj = np.r_[Tis[j],np.array([[0,0,0,1]])]
            Tij = np.r_[Tijs[i,j],np.array([[0,0,0,1]])]
            Rij = Tij[0:3,0:3]
              
            con.append(nlg.norm(Tj - Ti.dot(Tij)))
            con.append(nlg.norm(Rij.T.dot(Rij) - I3))
            
        return np.asarray(con)
    
    ### Setting initial values by walking through maximal spanning tree.
    G_init = G.copy()
    for i,j in G_init.edges_iter():
        G_init[i][j]['weight'] = -1*G_init[i][j]['n']
    G_init_tree = nxa.minimum_spanning_tree(G_init)
    
    x_init = np.zeros(12*(G.number_of_nodes() + G.number_of_edges()))
    
    node0 = G_init_tree.nodes()[0]
    offset0 = node_map[node0]
    x_init[offset0:offset0+9] = I3.reshape(9)
    
    fringe = [node0]
    seen = []
    while len(fringe)>0:
        node = fringe.pop(0)
        if node in seen: continue
        seen.append(node)
        for n_node in G_init_tree.neighbors(node):
            if n_node in seen: continue
            fringe.append(n_node)
            offset = node_map[n_node]*12
            tfm = G.edge[node][n_node]['avg_tfm']
            if node > n_node:
                tfm = nlg.inv(tfm)

            x_init[offset:offset+12] = tfm[0:3,:].reshape(12, order='F')
            
    for i,j in edge_map:
        offset = edge_map[i,j]*12
        x_init[offset:offset+12] = G.edge[i][j]['avg_tfm'][0:3,:].reshape(12,order='F')
    ### ###
    
    print "Initial x: ", x_init
    (X, fx, _, _, _) = sco.fmin_slsqp(func=f_objective, x0=x_init, f_eqcons=f_constraints, iter=100, full_output=1)
    
    # Create output optimal graph and copy edge transforms
    G_opt = G.to_directed()
    for i,j in edge_map:
        Tij = np.r_[get_mat_from_x(X, i, j),np.array([[0,0,0,1]])]
        Tji = nlg.inv(Tij)
        
        G_opt.edge[i][j] = {'tfm':Tij}
        G_opt.edge[j][i] = {'tfm':Tji}
        
    # Add all edges to make clique
    # Follow shortest path to get transform for edge
    for i,j in itertools.combinations(sorted(G_opt.nodes()), 2):
        if not G_opt.has_edge(i,j):
            ij_path = nxa.shortest_path(G_opt, i, j)
            Tij = G_opt.edge[ij_path[0]][ij_path[1]]['tfm']
            for p in xrange(2,len(ij_path)):
                Tij = Tij.dot(G_opt.edge[ij_path[p-1]][ij_path[p]]['tfm'])
            Tji = nlg.inv(Tij)
            
            G_opt.add_edge(i,j)
            G_opt.add_edge(j,i)
            G_opt.edge[i][j] = {'tfm':Tij}
            G_opt.edge[j][i] = {'tfm':Tji}
    
    for i in G_opt.nodes_iter():
        G_opt.add_edge(i,i)
        G_opt[i][i]['tfm'] = np.eye(4)
    
    n = G_opt.number_of_nodes()
    try:
        assert G_opt.number_of_edges() == n**2
    except AssertionError:
        print "Not all edges found...? Fix this"

    return G_opt
Ejemplo n.º 36
0
 def getShortestPath(self,nodA,nodB):
     
     ''' Get the shortest path between two nodes. '''
     
     return alg.shortest_path(self.graph,nodA,nodB)