示例#1
0
def query_graph_f4():
    """query f4"""
    G = Graph(directed=True)

    v0 = G.add_vertex()
    v1 = G.add_vertex()
    v2 = G.add_vertex()
    v3 = G.add_vertex()
    v4 = G.add_vertex()
    v5 = G.add_vertex()
    v6 = G.add_vertex()
    v7 = G.add_vertex()
    v8 = G.add_vertex()
    v9 = G.add_vertex()

    G.add_edge(v0, v1)  # e0, foaf:homepage
    G.add_edge(v2, v0)  # e1, gr:includes
    G.add_edge(v0, v3)  # e2, og:title
    G.add_edge(v0, v4)  # e3, sorg:description
    G.add_edge(v0, v8)  # e4, sorg:contentSize
    G.add_edge(v1, v5)  # e5, sorg:url
    G.add_edge(v1, v6)  # e6, wsdbm:hits
    G.add_edge(v7, v1)  # e7, wsdbm:likes
    G.add_edge(v1, v9)  # e8, sorg:language

    return G
def gen_cascade(g, p, source=None, stop_fraction=0.5):
    if source is None:
        source = random.choice(np.arange(g.num_vertices()))
    infected = {source}
    infection_times = np.ones(g.num_vertices()) * -1
    infection_times[source] = 0
    time = 0
    edges = []
    while np.count_nonzero(
            infection_times != -1) / g.num_vertices() <= stop_fraction:
        infected_nodes_until_t = copy(infected)
        time += 1
        for i in infected_nodes_until_t:
            for j in g.vertex(i).all_neighbours():
                j = int(j)
                if j not in infected and random.random() <= p:
                    infected.add(j)
                    infection_times[j] = time
                    edges.append((i, j))

    tree = Graph(directed=True)
    for _ in range(g.num_vertices()):
        tree.add_vertex()
    for u, v in edges:
        tree.add_edge(u, v)
    return source, infection_times, tree
示例#3
0
def line_g():
    g = Graph(directed=True)
    g.add_edge(0, 1)
    g.add_edge(1, 0)
    g.add_edge(1, 2)
    g.add_edge(2, 1)
    return g
def build_closure(g, terminals, debug=False, verbose=False):
    terminals = list(terminals)
    # build closure
    gc = Graph(directed=False)
    gc.add_vertex(g.num_vertices())

    edges_with_weight = set()
    r2pred = {}

    for r in terminals:
        if debug:
            print('root {}'.format(r))
        vis = init_visitor(g, r)
        pbfs_search(g, source=r, terminals=terminals, visitor=vis)
        new_edges = set(get_edges(vis.dist, r, terminals))
        if debug:
            print('new edges {}'.format(new_edges))
        edges_with_weight |= new_edges
        r2pred[r] = vis.pred

    for u, v, c in edges_with_weight:
        gc.add_edge(u, v)

    eweight = gc.new_edge_property('int')
    weights = np.array([c for _, _, c in edges_with_weight])
    eweight.set_2d_array(weights)

    vfilt = gc.new_vertex_property('bool')
    vfilt.a = False
    for v in terminals:
        vfilt[v] = True
    gc.set_vertex_filter(vfilt)
    return gc, eweight, r2pred
示例#5
0
def transform_npz_nx(g, penalize=20):
    from numpy import int32
    ids = g['ids']
    lat = g['lat']
    long = g['long']
    id2edges = g['id2edges']
    edges_weight = g['edges_weight']
    edges_neighbor = g['edges_neighbor']
    edges_air = g['edges_type']
    rescale = edges_weight.dtype == int32
    from networkx import Graph
    h = Graph()
    index2node = {}
    for index in range(len(ids)):
        node = (int(ids[index]), lat[index] / (10000000. if rescale else 1),
                long[index] / (10000000. if rescale else 1))
        index2node[index] = node
        h.add_node(node)
    for index in range(len(ids)):
        left = id2edges[index]
        right = id2edges[index + 1]
        me = index2node[index]
        for i in range(left, right):
            neighbor = edges_neighbor[i]
            air = edges_air[i]
            w = edges_weight[i] / (
                (1000. * (penalize if air < 0 else 1)) if rescale else 1)
            h.add_edge(me, index2node[neighbor], weight=w, type=air)
    return h
示例#6
0
def balancedBinaryTree(h, drawGraph=False):
    '''
    h - the height of the tree
    '''
    g = Graph(directed=False)
    g.add_vertex(2**h - 1)

    for i in xrange(1, 2**(h - 1)):
        lc = 2 * i - 1
        rc = lc + 1
        g.add_edge(i - 1, lc)
        g.add_edge(i - 1, rc)

    hIndex = g.new_vertex_property(
        "int")  #associate with each node the height at which it lies
    k = 2
    m = 1
    for i in xrange(1, len(hIndex.a)):
        hIndex.a[i] = m
        k -= 1
        if k == 0:
            m += 1
            k = 2**m
    g.vp['height'] = hIndex

    if drawGraph == True:
        draw.graph_draw(g,
                        vertex_text=g.vertex_index,
                        edge_color="black",
                        output="binaryTree_h_" + str(h) + "_.pdf")

    return g
示例#7
0
def golang_graph_to_graphtool_graph(edge_data):
    g = golang_graph_to_graph(edge_data)
    G = Graph(directed=False)
    handles = [G.add_vertex() for _ in g.nodes()]
    for u, v in g.edges():
        G.add_edge(handles[u], handles[v])
    return G
示例#8
0
def find_cycles2(args, wires):
    # implemented with graph-tools
    g = Graph()
    t_a = datetime.now()
    lst = []
    for i in range(0, len(wires)):
        lst.append(g.add_vertex())

    for i in range(0, len(wires)):
        if wires[i].type != "inp":
            for j in range(len(wires[i].operands)):
                g.add_edge(lst[wires[i].operands[j].index],
                           lst[wires[i].index])

    cycles = []
    for c in all_circuits(g):
        if len(cycles) > 100000:
            logging.info("number of cycles is limited.")
            break
        cycles.append(c.tolist())

    t_b = datetime.now()
    logging.info("time of finding cycles: " + diff(t_a, t_b))
    logging.info("there are" + str(len(cycles)) + "cycles")
    if args.p:
        logging.info("list of cycles:")
        for cycle in cycles:
            tmp = ""
            for i in range(len(cycle)):
                tmp += wires[cycle[i]].name + " "
            logging.info(tmp)
        print()
    return cycles
def gen_cascade(g, p, source=None, stop_fraction=0.5):
    if source is None:
        source = random.choice(np.arange(g.num_vertices()))
    infected = {source}
    infection_times = np.ones(g.num_vertices()) * -1
    infection_times[source] = 0
    time = 0
    edges = []
    while np.count_nonzero(infection_times != -1) / g.num_vertices() <= stop_fraction:
        infected_nodes_until_t = copy(infected)
        time += 1
        for i in infected_nodes_until_t:
            for j in g.vertex(i).all_neighbours():
                j = int(j)
                if j not in infected and random.random() <= p:
                    infected.add(j)
                    infection_times[j] = time
                    edges.append((i, j))

    tree = Graph(directed=True)
    for _ in range(g.num_vertices()):
        tree.add_vertex()
    for u, v in edges:
        tree.add_edge(u, v)
    return source, infection_times, tree
def ring(num_vtx=100, k=2, p=0.0):
    g = Graph(directed=False)
    vtx = list(g.add_vertex(num_vtx))

    # connect neighbors
    for i in vtx:
        for j in xrange(1, k + 1):
            dest = g.vertex((g.vertex_index[i] - j) % num_vtx)
            if g.edge(i, dest) is None:
                g.add_edge(i, dest)

    # redirect edges
    # old_edges = list(g.edges())
    old_edges = [(x.source(), x.target()) for x in g.edges()]
    for i in old_edges:
        n = random.random()
        if n < p:  # redirect edge; choose random vertex as new destination
            vtx_tmp = vtx[:]
            vtx_tmp.remove(i[1])
            if i[0] in vtx_tmp:
                vtx_tmp.remove(i[0])
            dest = random.choice(vtx_tmp)
            while g.edge(i[0], dest) is not None:
                vtx_tmp.remove(dest)
                dest = random.choice(vtx_tmp)

            g.remove_edge(g.edge(i[0], i[1]))
            g.add_edge(i[0], dest)

    return g
def build_graph_from_edges(edges):
    """returns Graph (a new one)
    """
    g = Graph()
    for u, v in edges:
        g.add_edge(u, v)
    return g
示例#12
0
def diagram_to_graph(diagram):
    '''Convert specified chord diagram to the equivalent graph.
       Construct the graph with a vertex for each diagram chord, and an edge
       from the vertex for each node to the next greater node as though manually
       drawing the planar diagram from the chord diagram, such that edges go
       from nodes (1 -> 2), (2 -> 3),..., (n-1 -> n), (n -> 1).

       Arguments:
           diagram: A list of chords, where each chord is a node tuple.
       Returns:
           Returns the graph corresponding to the input diagram.
       Raises:
           None
    '''
    graph = Graph()

    # Create a vertex for each chord - same vertex stored for each node of chord
    vertex_by_node = {}
    for chord in diagram:
        vertex = graph.add_vertex()
        vertex_by_node[chord[0]] = vertex
        vertex_by_node[chord[1]] = vertex

    # As though drawing the planar diagram from the chord diagram by hand,
    # add an edge from 1st node to 2nd, 2nd to 3rd, and so on, until closing
    # the loop back to the 1st node.
    nodes = vertex_by_node.keys()
    n_nodes = len(nodes)
    for i in range(0, len(nodes)):
        j = (i + 1) % n_nodes
        graph.add_edge(vertex_by_node[nodes[i]], vertex_by_node[nodes[j]])

    return graph
示例#13
0
def query_graph_s1():
    """query s1"""
    G = Graph(directed=True)

    v0 = G.add_vertex()
    v1 = G.add_vertex()
    v2 = G.add_vertex()
    v3 = G.add_vertex()
    v4 = G.add_vertex()
    v5 = G.add_vertex()
    v6 = G.add_vertex()
    v7 = G.add_vertex()
    v8 = G.add_vertex()
    v9 = G.add_vertex()

    G.add_edge(v0, v1)  # e0, gr:includes
    G.add_edge(v2, v0)  # e1, gr:offers
    G.add_edge(v0, v3)  # e2, gr:price
    G.add_edge(v0, v4)  # e3, gr:serial_number
    G.add_edge(v0, v5)  # e4, gr:validFrom
    G.add_edge(v0, v6)  # e5, gr:validThrough
    G.add_edge(v0, v7)  # e6, sorg:eligible_Region
    G.add_edge(v0, v8)  # e7, sorg:eligible_Region
    G.add_edge(v0, v9)  # e8, gr:priceValidUntil

    return G
def build_minimum_tree(g, root, terminals, edges, directed=True):
    """remove redundant edges from `edges` so that root can reach each node in terminals
    """
    # build the tree
    t = Graph(directed=directed)

    for _ in range(g.num_vertices()):
        t.add_vertex()

    for (u, v) in edges:
        t.add_edge(u, v)

    # mask out redundant edges
    vis = init_visitor(t, root)
    pbfs_search(t, source=root, terminals=list(terminals), visitor=vis)

    minimum_edges = {e
                     for u in terminals
                     for e in extract_edges_from_pred(t, root, u, vis.pred)}
    # print(minimum_edges)
    efilt = t.new_edge_property('bool')
    efilt.a = False
    for u, v in minimum_edges:
        efilt[u, v] = True
    t.set_edge_filter(efilt)

    return filter_nodes_by_edges(t, minimum_edges)
示例#15
0
def build_minimum_tree(g, root, terminals, edges, directed=True):
    """remove redundant edges from `edges` so that root can reach each node in terminals
    """
    # build the tree
    t = Graph(directed=directed)

    for _ in range(g.num_vertices()):
        t.add_vertex()

    for (u, v) in edges:
        t.add_edge(u, v)

    # mask out redundant edges
    vis = init_visitor(t, root)
    pbfs_search(t, source=root, terminals=list(terminals), visitor=vis)

    minimum_edges = {
        e
        for u in terminals
        for e in extract_edges_from_pred(t, root, u, vis.pred)
    }
    # print(minimum_edges)
    efilt = t.new_edge_property('bool')
    efilt.a = False
    for u, v in minimum_edges:
        efilt[u, v] = True
    t.set_edge_filter(efilt)

    return filter_nodes_by_edges(t, minimum_edges)
示例#16
0
    def _filter_short_branch(self, filter=False, short=30):
        """
        filter out very short branches: do this maybe not right for some models, for models with flat part, it is right
        I will test how this effect the final matching results
        need to delete nodes, switch with the last one then delete last
        """
        if filter == False:
            self.verts = self.verts_init
            self.edges = self.edges_init
        else:
            init_graph = Graph(directed=False)
            init_graph.add_vertex(len(self.verts_init))
            for edge in self.edges_init:
                init_graph.add_edge(init_graph.vertex(edge[0]), init_graph.vertex(edge[1]))

            terminal_node = []
            for v in init_graph.vertices():
                if v.out_degree() == 1:
                    terminal_node.append(v)

            visitor = DepthVisitor()
            short_nodes = []
            for tn in terminal_node:
                search.dfs_search(init_graph, tn, visitor)
                tmp_node = visitor.get_short_branch(min_length=short)
                visitor.reset()
                for n in tmp_node:
                    short_nodes.append(n)

            ## get edges on the short paths
            short_nodes = list(set(short_nodes))
            short_edges = []
            temp_verts = self.verts_init[:]
            v_num = len(self.verts_init)
            if len(short_nodes):
                for v in reversed(sorted(short_nodes)):
                    for ve in init_graph.vertex(v).out_edges():
                        short_edges.append(ve)

                ## delete edges first, then vertex
                short_edges = list(set(short_edges))
                for e in short_edges:
                    init_graph.remove_edge(e)

                print 'deleting vertex',
                for v in reversed(sorted(short_nodes)):
                    print v,
                    temp_verts[int(v)] = temp_verts[v_num-1]
                    init_graph.remove_vertex(v, fast=True)
                    v_num -= 1
                print '\ndeleting related edges' # already done above, just info user
            else:
                print 'no short branches'

            ######## new vertices and edges ########
            self.verts = temp_verts[:v_num]
            self.edges = []
            for e in init_graph.edges():
                self.edges.append([int(e.source()), int(e.target())])
示例#17
0
def edges2graph(g, edges):
    tree = Graph(directed=True)
    for _ in range(g.num_vertices()):
        tree.add_vertex()
    for u, v in edges:
        tree.add_edge(int(u), int(v))

    return filter_nodes_by_edges(tree, edges)
def edges2graph(g, edges):
    tree = Graph(directed=True)
    for _ in range(g.num_vertices()):
        tree.add_vertex()
    for u, v in edges:
        tree.add_edge(int(u), int(v))

    return filter_nodes_by_edges(tree, edges)
def steiner_tree_greedy(
        g, root, infection_times, source, obs_nodes,
        debug=False,
        verbose=True):
    # root = min(obs_nodes, key=infection_times.__getitem__)
    sorted_obs = list(sorted(obs_nodes, key=infection_times.__getitem__))[1:]
    tree_nodes = {root}
    tree_edges = set()
    for u in sorted_obs:
        # connect u to the tree
        vis = init_visitor(g, u)
        if debug:
            print('connect {} to tree'.format(u))
            print('nodes connectable: {}'.format(tree_nodes))
        forbidden_nodes = list(set(obs_nodes) - tree_nodes)
        cpbfs_search(g, u, visitor=vis,
                     terminals=list(tree_nodes),
                     forbidden_nodes=forbidden_nodes,
                     count_threshold=1)

        # add edge
        reachable_nodes = set(np.nonzero(vis.dist > 0)[0]).intersection(tree_nodes)

        if debug:
            print('reachable_nodes: {}'.format(reachable_nodes))

        assert len(reachable_nodes) > 0
        sorted_ancestors = sorted(reachable_nodes, key=vis.dist.__getitem__)
        ancestor = sorted_ancestors[0]

        if debug:
            print('ancestor: {}'.format(ancestor))
            print('dist to reachable: {}'.format(vis.dist[sorted_ancestors]))

        new_edges = extract_edges_from_pred(g, u, ancestor, vis.pred)
        new_edges = {(v, u) for u, v in new_edges}  # needs to reverse the order

        if debug:
            print('new_edges: {}'.format(new_edges))

        tree_edges |= set(new_edges)
        tree_nodes |= {v for e in new_edges for v in e}

    t = Graph(directed=True)
    for _ in range(g.num_vertices()):
        t.add_vertex()

    vfilt = t.new_vertex_property('bool')
    vfilt.a = False
    for v in tree_nodes:
        vfilt[t.vertex(v)] = True

    for u, v in tree_edges:
        t.add_edge(t.vertex(u), t.vertex(v))

    t.set_vertex_filter(vfilt)

    return t
示例#20
0
def input_data_gt():
    g_nx = nx.karate_club_graph()
    g = Graph(directed=True)
    g.add_vertex(g_nx.number_of_nodes())
    for u, v in g_nx.edges():
        g.add_edge(u, v)
        g.add_edge(v, u)  # the other direction

    return g, from_gt(g, None), g.num_vertices()
def build_closure(g, terminals, p=None, debug=False, verbose=False):
    """build the transitive closure on terminals"""
    def get_edges(dist, root, terminals):
        """get adjacent edges to root with weight"""
        return {(root, t, dist[t])
                for t in terminals if dist[t] != -1 and t != root}

    terminals = list(terminals)
    gc = Graph(directed=False)

    gc.add_vertex(g.num_vertices())

    edges_with_weight = set()
    r2pred = {}  # root to predecessor map (from bfs)

    # shortest path to all other nodes
    for r in terminals:
        if debug:
            print('root {}'.format(r))

        targets = list(set(terminals) - {r})
        dist_map, pred_map = shortest_distance(g,
                                               source=r,
                                               target=targets,
                                               weights=p,
                                               pred_map=True)
        dist_map = dict(zip(targets, dist_map))
        # print(dist_map)
        # print(pred_map)
        new_edges = get_edges(dist_map, r, targets)
        # if p is None:
        #     vis = init_visitor(g, r)
        #     bfs_search(g, source=r, visitor=vis)
        #     new_edges = set(get_edges(vis.dist, r, terminals))
        # else:
        #     print('weighted graph')

        if debug:
            print('new edges {}'.format(new_edges))
        edges_with_weight |= new_edges
        # r2pred[r] = vis.pred
        r2pred[r] = pred_map

    for u, v, c in edges_with_weight:
        gc.add_edge(u, v)

    # edge weights
    eweight = gc.new_edge_property('int')
    weights = np.array([c for _, _, c in edges_with_weight])
    eweight.set_2d_array(weights)

    vfilt = gc.new_vertex_property('bool')
    vfilt.a = False
    for v in terminals:
        vfilt[v] = True
    gc.set_vertex_filter(vfilt)
    return gc, eweight, r2pred
示例#22
0
def starGraph(numOfVertices):
    '''
    numOfVertices = Number of vertices including the center node of the star.
    dependencies: graph_tool
    '''
    g = Graph(directed=False)
    centerNode = g.add_vertex()
    for i in xrange(2, numOfVertices + 1):
        node = g.add_vertex()
        g.add_edge(centerNode, node)
    return g
示例#23
0
def query_graph_l4():
    G = Graph(directed=True)

    v0 = G.add_vertex()
    v1 = G.add_vertex()
    v2 = G.add_vertex()

    G.add_edge(v0, v1)  # e0, og:tag
    G.add_edge(v0, v2)  # e1, sorg:caption

    return G
示例#24
0
def query_graph_l3():
    G = Graph(directed=True)

    v0 = G.add_vertex()
    v1 = G.add_vertex()
    v2 = G.add_vertex()

    G.add_edge(v0, v1)  # e0, wsdbm:likes
    G.add_edge(v0, v2)  # e1, wsdbm:subscribes

    return G
示例#25
0
    def draw_game_flow(self, model: GameModel):
        self.log.debug('drawing game flow for model {0}'.format(model.name))
        TMPUTILS.clear_container(self.game_flow_panel)

        all_phases = [
            phase for pt in [model.table_type] + model.player_types
            for phase in pt.phases
        ]
        phase_phase = {}
        for phase in all_phases:
            phase_phase[phase] = list(TMPUTILS.end_phases(phase))

        graph = Graph()
        graph.vp.name = graph.new_vertex_property('string')
        graph.vp.color = graph.new_vertex_property('string')
        graph.vp.text_pos = graph.new_vertex_property('float')
        graph.vp.text_off = graph.new_vertex_property('vector<float>')
        phase_vertex = {}
        for phase in all_phases:
            vertex = graph.add_vertex()
            phase_vertex[phase] = vertex
            text = phase.name
            graph.vp.name[vertex] = text
            if phase is model.start_phase:
                color = self.config.start_game_color()
            elif phase is model.end_phase:
                color = self.config.end_game_color()
            elif phase in model.table_type.phases:
                color = self.config.table_color()
            else:
                number = model.player_types.index(
                    model.get_player_type_for_phase(phase))
                color = self.config.player_color(number)
            graph.vp.color[vertex] = color
            graph.vp.text_pos[vertex] = 0
            graph.vp.text_off[vertex] = [0, 0]

        for phase in all_phases:
            for other in phase_phase[phase]:
                graph.add_edge(phase_vertex[phase], phase_vertex[other])

        pos = sfdp_layout(graph)

        vprops = {
            'text': graph.vp.name,
            'fill_color': graph.vp.color,
            'text_position': graph.vp.text_pos,
            'text_offset': graph.vp.text_off
        }

        graph_widget = GraphWidget(graph, pos, vprops=vprops, vertex_size=50)
        self.game_flow_panel.pack_start(Gtk.Label('gameflow'), False, False, 0)
        self.game_flow_panel.pack_start(graph_widget, True, True, 0)
        self.show_all()
示例#26
0
def query_graph_l5():
    G = Graph(directed=True)

    v0 = G.add_vertex()
    v1 = G.add_vertex()
    v2 = G.add_vertex()
    v3 = G.add_vertex()

    G.add_edge(v0, v1)  # e0, sorg:jobTitle
    G.add_edge(v0, v3)  # e1, sorg:nationality
    G.add_edge(v2, v3)  # e2, gn:parentCountry

    return G
示例#27
0
def query_graph_l1():
    G = Graph(directed=True)

    v0 = G.add_vertex()
    v1 = G.add_vertex()
    v2 = G.add_vertex()
    v3 = G.add_vertex()

    G.add_edge(v0, v1)  # e0
    G.add_edge(v0, v2)  # e1
    G.add_edge(v2, v3)  # e2

    return G
示例#28
0
def query_graph_l2():
    G = Graph(directed=True)

    v0 = G.add_vertex()
    v1 = G.add_vertex()
    v2 = G.add_vertex()
    v3 = G.add_vertex()

    G.add_edge(v2, v3)  # e0, wsdbm:likes
    G.add_edge(v2, v1)  # e1, sorg:nationality
    G.add_edge(v0, v1)  # e2, gn:parentCountry, switched directions

    return G
示例#29
0
def query_graph_s7():
    """query s7"""
    G = Graph(directed=True)

    v0 = G.add_vertex()
    v1 = G.add_vertex()
    v2 = G.add_vertex()
    v3 = G.add_vertex()

    G.add_edge(v0, v1)  # e0, rdf:type
    G.add_edge(v0, v2)  # e1, sorg:text
    G.add_edge(v3, v0)  # e2, wsdbm:likes

    return G
示例#30
0
def query_graph_s6():
    """query s6"""
    G = Graph(directed=True)

    v0 = G.add_vertex()
    v1 = G.add_vertex()
    v2 = G.add_vertex()
    v3 = G.add_vertex()

    G.add_edge(v0, v1)  # e0, mo:conductor
    G.add_edge(v0, v2)  # e1, rdf:type
    G.add_edge(v0, v3)  # e2, wsdbm:hasGenre

    return G
def to_directed(g, t, root):
    new_t = Graph(directed=True)
    all_edges = set()
    leaves = [v for v in t.vertices()
              if (v.out_degree() + v.in_degree()) == 1 and t != root]
    for target in leaves:
        path = shortest_path(t, source=root, target=target)[0]
        edges = set(zip(path[:-1], path[1:]))
        all_edges |= edges

    for _ in range(g.num_vertices()):
        new_t.add_vertex()
    for u, v in all_edges:
        new_t.add_edge(int(u), int(v))
    return new_t
def convert_to_gt(G):
    """
    Takes a NetworkX-formatted network and returns an equivalent graphtool-formatted network.

    Parameters
    ----------
    G : NetworkX-formatted network
    """
    n = nx.number_of_nodes(G)
    Gto = Graph(directed=False)
    Gto.add_vertex(n)
    edges = [edge for edge in G.edges()]
    for edge in edges:
        Gto.add_edge(Gto.vertex(edge[0]), Gto.vertex(edge[1]))
    return (Gto)
示例#33
0
def graph():
    """    
       0 (root)
      / \
     1   2
      \ /
       3 (X)
    """
    g = Graph()
    g.add_vertex(4)
    g.add_edge(0, 1)
    g.add_edge(0, 2)
    g.add_edge(1, 3)
    g.add_edge(2, 3)
    return g
示例#34
0
    def create_graph(cls, edges, is_directed=True):
        """Create a graph-tool type graph from a list of edges"""
        g = Graph()
        g.set_directed(is_directed)
        label2index = dict()
        label = g.new_vertex_property('int32_t')
        g.vertex_properties['label'] = label

        for v1_label, v2_label in edges:
            cls.add_vertex(v1_label, label2index, g)
            cls.add_vertex(v2_label, label2index, g)

            v1, v2 = label2index[v1_label], label2index[v2_label]
            g.add_edge(v1, v2)

        return g, label2index
示例#35
0
def insert_clique_subgraph(a: gt.Graph, n_nodes, att_dist, att_name,
                           target_solutions):
    q_nodes = np.random.choice(a.get_vertices(), n_nodes, replace=False)
    a_to_q = {}
    q_to_a = {}
    nodes_list = []
    edges_list = []

    for i, n in enumerate(q_nodes):
        a_to_q[n] = i  # Maps from archive node to T.
        q_to_a[i] = n  # Maps from T to archive node
        nodes_list.append(n)
    for i in range(n_nodes):
        for j in range(i + 1, n_nodes):
            n1 = q_to_a[i]
            n2 = q_to_a[j]
            # e = a.edge(n1, n2)
            # if e:
            #     edges_list.append(e)
            #     continue
            e = a.add_edge(n1, n2)
            edges_list.append(e)
            add_single_edge_attribute(a, e, att_dist, att_name)

    new_target = {'nodes': nodes_list, 'edges': edges_list, 'isClutter': False}
    target_solutions.append(new_target)
    return create_q_graph(a, q_nodes)
示例#36
0
def query_graph_s3():
    """query s3"""
    G = Graph(directed=True)

    v0 = G.add_vertex()
    v1 = G.add_vertex()
    v2 = G.add_vertex()
    v3 = G.add_vertex()
    v4 = G.add_vertex()

    G.add_edge(v0, v1)  # e0, rdf:type
    G.add_edge(v0, v2)  # e1, sorg:caption
    G.add_edge(v0, v3)  # e2, wsdbm:hasGenre
    G.add_edge(v0, v4)  # e3, sorg:publisher

    return G
示例#37
0
def query_graph_s4():
    """query s4"""
    G = Graph(directed=True)

    v0 = G.add_vertex()
    v1 = G.add_vertex()
    v2 = G.add_vertex()
    v3 = G.add_vertex()
    v4 = G.add_vertex()

    G.add_edge(v0, v1)  # e0, foaf:age
    G.add_edge(v0, v2)  # e1, foaf:familyName
    G.add_edge(v3, v0)  # e2, mo:artist
    G.add_edge(v0, v4)  # e3, sorg:nationality

    return G
示例#38
0
def query_graph_s5():
    """query s5"""
    G = Graph(directed=True)

    v0 = G.add_vertex()
    v1 = G.add_vertex()
    v2 = G.add_vertex()
    v3 = G.add_vertex()
    v4 = G.add_vertex()

    G.add_edge(v0, v1)  # e0, dc:Location
    G.add_edge(v0, v2)  # e1, sorg:nationality
    G.add_edge(v0, v3)  # e2, wsdbm:gender
    G.add_edge(v0, v4)  # e3, rdf:type

    return G
示例#39
0
class RoadMap(object):
    def __init__(self, mapfile):
        self._mapfile = mapfile
        self.DIRECTION_index = 6
        self.PATHCLASS_index = 20
        self.g = Graph()
        self.g.edge_properties["length"] = self.g.new_edge_property("double")
        self.g.edge_properties["level"] = self.g.new_edge_property("int")
        self.g.vertex_properties["pos"] = self.g.new_vertex_property("vector<double>")
        self.cross_pos_index = {}

    def load(self):
        if self._mapfile[-3:] != 'shp':
            self.g = load_graph(self._mapfile)
            return

        try:
            sf = shapefile.Reader(self._mapfile)
        except Exception as e:
            print(str(e))
            return False
        roads_records = sf.shapeRecords()  # 获取路段信息'
        for road_record in roads_records:
            cross_s_index = self.add_cross(road_record.shape.points[0])
            cross_e_index = self.add_cross(road_record.shape.points[-1])
            self.add_road_edge(cross_s_index, cross_e_index, road_record)
            if int(road_record.record[self.DIRECTION_index]) == 0:  # 若路段是双向车道
                self.add_road_edge(cross_e_index, cross_s_index, road_record)
        return True

    def has_edge(self, s_vertex, e_vertex):
        if self.g.num_vertices() >= max(s_vertex, e_vertex):
            return self.g.edge(s_vertex, e_vertex)
        else:
            return None

    def add_cross(self, cross_pos):
        if cross_pos in self.cross_pos_index:
            return self.cross_pos_index.get(cross_pos)
        else:
            cross_index = self.g.add_vertex()
            self.g.vp.pos[cross_index] = cross_pos
            self.cross_pos_index[cross_pos] = cross_index
            return cross_index

    def add_road_edge(self, s_vertex, e_vertex, road):
        if self.has_edge(s_vertex, e_vertex):
            return self.g.edge(s_vertex, e_vertex)
        else:
            edge = self.g.add_edge(s_vertex, e_vertex)
            self.g.ep.level[edge] = int(road.record[self.PATHCLASS_index])
            self.g.ep.length[edge] = self.road_length(road)
            return edge

    @staticmethod
    def road_length(road):
        length = 0
        for sub_road in zip(road.shape.points[:-1], road.shape.points[1:]):
            length += distance.euclidean(sub_road[0], sub_road[1])
        return length
def edges_to_directed_tree(g, root, edges):
    t = Graph(directed=False)
    for _ in range(g.num_vertices()):
        t.add_vertex()

    for u, v in edges:
        t.add_edge(u, v)

    vis = EdgeCollectorVisitor()
    bfs_search(t, source=root, visitor=vis)

    t.clear_edges()
    t.set_directed(True)
    for u, v in vis.edges:
        t.add_edge(u, v)

    return filter_nodes_by_edges(t, edges)
示例#41
0
def compose_graph(uid_pid_pairs):

    # set up graph
    g = Graph()
    g.vp['pid'] = v_pid_p = g.new_vertex_property('string')
    g.vp['count'] = v_count_p = g.new_vertex_property('int')
    g.ep['count'] = e_count_p = g.new_edge_property('int')

    pid_v_map = {}
    uid_last_v_map = {}
    vv_e_map = {}

    for uid, pid in uid_pid_pairs:

        # vertex

        v = pid_v_map.get(pid)
        if v is None:
            v = g.add_vertex()
            v_pid_p[v] = pid
            v_count_p[v] = 0
            pid_v_map[pid] = v
        v_count_p[v] += 1

        # edge

        last_v = uid_last_v_map.get(uid)
        uid_last_v_map[uid] = v
        if last_v is None:
            continue

        vv = (last_v, v)
        e = vv_e_map.get(vv)
        if e is None:
            e = g.add_edge(*vv)
            e_count_p[e] = 0
            vv_e_map[vv] = e
        e_count_p[e] += 1

    # calculate closeness
    g.vp['closeness'] = v_closeness_p = g.new_vertex_property('float')
    e_inverse_count_p = g.new_edge_property('int')
    e_inverse_count_p.a = e_count_p.a.max()-e_count_p.a
    debug('e_inverse_count_p.a: {}', e_inverse_count_p.a)
    closeness(g, weight=e_inverse_count_p, vprop=v_closeness_p)
    debug('v_closeness_p.a    : {}', v_closeness_p.a)
    v_closeness_p.a = nan_to_num(v_closeness_p.a)
    debug('v_closeness_p.a    : {}', v_closeness_p.a)

    # fillter
    g.vp['picked'] = v_picked_p = g.new_vertex_property('bool')
    debug('v_count_p.a.mean() : {}', v_count_p.a.mean())
    v_picked_p.a = v_count_p.a > v_count_p.a.mean()
    debug('v_picked_p.a       : {}', v_picked_p.a)
    g.set_vertex_filter(v_picked_p)
    g.set_vertex_filter(None)

    return g
示例#42
0
def graph_from_dataframes(vertex_df, edge_df):
    '''Re-creates a Graph object with PropertyMaps taken
    from the vertex_df and edge_df DataFrames

    Paramters:
    ==========
    verex_df: a DataFrame with an index named 'vertex_index'
    edge_df: a DataFrame with a multi-index named ('source', 'target')

    Returns:
    ========
    graph: a grah-tool Graph with PropertyMaps copied
        from the columns of the input DataFrames
    '''

    graph = Graph(directed=True)

    vertex_index = vertex_df.index.get_level_values(level='vertex_index')
    vertices = graph.add_vertex(n=vertex_index.shape[0])
    for col in vertex_df.columns:
        in_type = vertex_df[col].dtype.name
        try:
            dtype = ALIASES[in_type]
        except KeyError:
            log.info('Data type {} not supported'.format(in_type))
            continue
        prop = graph.new_vertex_property(dtype)
        prop.fa = vertex_df[col]
        graph.vertex_properties[col] = prop

    src = edge_df.index.names.index('source')
    trgt = edge_df.index.names.index('target')
    ### TODO: use the list edge creation
    for tup in edge_df.index:
        source, target = tup[src], tup[trgt]
        try:
            edge = graph.add_edge(source, target)
        except ValueError:
            log.info('Invalid vertex in (source: {}, target: {})'.format(source, target))
    for col in edge_df.columns:
        in_type = edge_df[col].dtype.name
        try:
            dtype = ALIASES[in_type]
        except KeyError:
            log.info('Data type {} not supported'.format(in_type))
            continue
        prop = graph.new_edge_property(dtype)
        prop.fa = edge_df[col]
        graph.edge_properties[col] = prop

    return graph
def build_closure(g, terminals,
                  debug=False,
                  verbose=False):
    terminals = list(terminals)
    # build closure
    gc = Graph(directed=False)

    for _ in range(g.num_vertices()):
        gc.add_vertex()

    edges_with_weight = set()
    r2pred = {}

    for r in terminals:
        if debug:
            print('root {}'.format(r))
        vis = init_visitor(g, r)
        pbfs_search(g, source=r, terminals=terminals, visitor=vis)
        new_edges = set(get_edges(vis.dist, r, terminals))
        if debug:
            print('new edges {}'.format(new_edges))
        edges_with_weight |= new_edges
        r2pred[r] = vis.pred
    
    for u, v, c in edges_with_weight:
        gc.add_edge(u, v)
        
    eweight = gc.new_edge_property('int')
    weights = np.array([c for _, _, c in edges_with_weight])
    eweight.set_2d_array(weights)

    vfilt = gc.new_vertex_property('bool')
    vfilt.a = False
    for v in terminals:
        vfilt[v] = True
    gc.set_vertex_filter(vfilt)
    return gc, eweight, r2pred
示例#44
0
def load_graph(infile):
    inmatrix = np.loadtxt(infile, dtype=np.dtype('uint32'), delimiter=" ")
    numv = np.amax(inmatrix[:,0:2])

    #print numv, inmatrix[:,0:2]

    g = Graph(directed=False)
    edge_weights = g.new_edge_property("double")
    g.edge_properties["weights"] = edge_weights
    vlist = list(g.add_vertex(numv))

    for i in inmatrix:
        edge = g.add_edge(vlist[i[0]-1], vlist[i[1]-1]) # need to convert from 1-based index in file to 0-based
        edge_weights[edge] = i[2]

    remove_parallel_edges(g)
    return g
示例#45
0
def user_network(storage, track, session):
    g = Graph()
    users = defaultdict(g.add_vertex)

    g.graph_properties["track"] = g.new_graph_property("string", track)
    g.graph_properties["session"] = g.new_graph_property("string", session)

    g.edge_properties["created_at"] = g.new_edge_property("int64_t")

    for tweet in storage:
        tweeter_id = tweet["user__id_str"]
        origin_id = tweet["retweeted_status__user__id_str"]

        created_at = arrow.get(tweet["created_at"], DATE_FORMAT).timestamp

        if origin_id:
            edge = g.add_edge(users[tweeter_id], users[origin_id])
            g.edge_properties["created_at"][edge] = created_at

    return g
 def test_sredni_wspolczynnik_klasteryzacji_na_sztywno_graf_pelny(self):
     # self.assertEqual(7. / 15, self.stat.sredni_wspolczynnik_klasteryzacji_moj())
     # print self.stat.sredni_wspolczynnik_klasteryzacji_moj()
     g = Graph(directed=False)
     v0 = g.add_vertex()
     v1 = g.add_vertex()
     v2 = g.add_vertex()
     v3 = g.add_vertex()
     g.add_edge(v0, v1)
     g.add_edge(v0, v2)
     g.add_edge(v0, v3)
     g.add_edge(v1, v2)
     g.add_edge(v1, v3)
     g.add_edge(v2, v3)
     lc = local_clustering(g, undirected=True)
     self.assertEqual(1.0, vertex_average(g, lc)[0])
    def parse_graph_from_string(self, graphML_string):
        dom = minidom.parseString(graphML_string)
        root = dom.getElementsByTagName("graphml")[0]
        graph = root.getElementsByTagName("graph")[0]
        name = graph.getAttribute('id')

        g = Graph(directed=False)

        vpos=g.new_vertex_property("vector<double>")
        for node in graph.getElementsByTagName("node"):
            id=node.getAttribute('id')
            n = g.add_vertex()
            g.vertex_index[id]

            #right now only the positions are available
            for attr in node.getElementsByTagName("data"):
                if attr.firstChild:
                    key=attr.getAttribute("key")
                    #n[key] = attr.firstChild.data
                    if(key=="x"):
                        x=attr.firstChild.data
                    elif(key=="y"):
                        y=attr.firstChild.data

            vpos[id]=(x,y)

        g.vertex_properties["pos"]=vpos

        #have to workaround the directed graph written by the server
        for edge in graph.getElementsByTagName("edge"):
            source = edge.getAttribute('source')
            dest = edge.getAttribute('target')

            edge=g.edge(dest,source)
            if(edge==None):
                e = g.add_edge(source, dest)

	return g
示例#48
0
文件: hdfgraph.py 项目: glyg/hdfgraph
def graph_from_dataframes(vertex_df, edge_df):
    '''Re-creates a Graph object with PropertyMaps taken
    from the vertex_df and edge_df DataFrames

    Paramters:
    ==========
    verex_df: a DataFrame with an index named 'vertex_index'
    edge_df: a DataFrame with a multi-index named ('source', 'target')

    Returns:
    ========
    graph: a grah-tool Graph with PropertyMaps copied
        from the columns of the input DataFrames
    '''

    graph = Graph(directed=True)

    vertex_index = vertex_df.index.get_level_values(level='vertex_index')
    vertices = graph.add_vertex(n=vertex_index.shape[0])
    for col in vertex_df.columns:
        dtype = ALIASES[vertex_df[col].dtype.name]
        prop = graph.new_vertex_property(dtype)
        prop.a = vertex_df[col]
        graph.vertex_properties[col] = prop

    src = edge_df.index.names.index('source')
    trgt = edge_df.index.names.index('target')
    ### TODO: use the list edge creation
    for tup in edge_df.index:
        source, target = tup[src], tup[trgt]
        edge = graph.add_edge(source, target)

    for col in edge_df.columns:
        dtype = ALIASES[edge_df[col].dtype.name]
        prop = graph.new_edge_property(dtype)
        prop.a = edge_df[col]
        graph.edge_properties[col] = prop
    return graph
示例#49
0
class SkeletonData(object):
    """
    class to store and process skeleton data, like generated from starlab mean curvature skeleton
    """
    def __init__(self, fname=None, mesh_name=None, filter_sb=False):
        """
        @param filter_sb: if filter out Short Branch
        """
        if fname != None:
            self.skel_name = fname
            self.read_skel_file(fname)
            self._filter_short_branch(filter=filter_sb, short=10)
            self._parse_data()
            self.mesh_name = mesh_name
            self.vert_radius = None

    def calc_skel_properties(self):
        """
        calc all properties needed for matching
        """
        self.calc_node_centricity()
        self.calc_skel_radius()
        self.calc_path_length_ratio()
        self.calc_path_radius_ratio()
        self.normalize_skeleton()


    def read_skel_file(self, fname, dim=3):
        if fname == None:
            print 'please input skeleton file name'
            sys.exit(0)
        elif os.path.isfile(fname):
            self.verts_init = []
            self.edges_init = []
            with open(fname) as sf:
                for line in sf:
                    line = line.strip('\n')
                    line = line.split(' ')
                    if line[0] == '#':
                        continue
                    elif line[0] == 'v':
                        self.verts_init.append([x for x in line[1:(dim+1)]])
                    #### attention!! verts of edge start from 1 in files ####
                    elif line[0] == 'e':
                        self.edges_init.append([int(x)-1 for x in line[1:3]])
                    else:
                        print 'not support this format'
                        sys.exit(0)
        else:
            print 'no such flie', fname
            sys.exit(0)


    def _filter_short_branch(self, filter=False, short=30):
        """
        filter out very short branches: do this maybe not right for some models, for models with flat part, it is right
        I will test how this effect the final matching results
        need to delete nodes, switch with the last one then delete last
        """
        if filter == False:
            self.verts = self.verts_init
            self.edges = self.edges_init
        else:
            init_graph = Graph(directed=False)
            init_graph.add_vertex(len(self.verts_init))
            for edge in self.edges_init:
                init_graph.add_edge(init_graph.vertex(edge[0]), init_graph.vertex(edge[1]))

            terminal_node = []
            for v in init_graph.vertices():
                if v.out_degree() == 1:
                    terminal_node.append(v)

            visitor = DepthVisitor()
            short_nodes = []
            for tn in terminal_node:
                search.dfs_search(init_graph, tn, visitor)
                tmp_node = visitor.get_short_branch(min_length=short)
                visitor.reset()
                for n in tmp_node:
                    short_nodes.append(n)

            ## get edges on the short paths
            short_nodes = list(set(short_nodes))
            short_edges = []
            temp_verts = self.verts_init[:]
            v_num = len(self.verts_init)
            if len(short_nodes):
                for v in reversed(sorted(short_nodes)):
                    for ve in init_graph.vertex(v).out_edges():
                        short_edges.append(ve)

                ## delete edges first, then vertex
                short_edges = list(set(short_edges))
                for e in short_edges:
                    init_graph.remove_edge(e)

                print 'deleting vertex',
                for v in reversed(sorted(short_nodes)):
                    print v,
                    temp_verts[int(v)] = temp_verts[v_num-1]
                    init_graph.remove_vertex(v, fast=True)
                    v_num -= 1
                print '\ndeleting related edges' # already done above, just info user
            else:
                print 'no short branches'

            ######## new vertices and edges ########
            self.verts = temp_verts[:v_num]
            self.edges = []
            for e in init_graph.edges():
                self.edges.append([int(e.source()), int(e.target())])


    def create_virtual_node(self):
        """
        I am planning use this function to make virtual nodes for those feature nodes
        """
        pass


    def _parse_data(self):
        """
        extract interal points(degree>2) and endpoints(degree=1)
        extract segments
        """
        if self.verts == None or self.edges == None:
            print 'please first call read_skel_file function'
        else:
            self.verts = np.array(self.verts, dtype=np.float)
            self.edges = np.array(self.edges, dtype=np.int)
            terminal_index = []
            junction_index = []
            self.skel_graph = Graph(directed=False)
            self.skel_graph.add_vertex(len(self.verts))
            for edge in self.edges :
                self.skel_graph.add_edge(self.skel_graph.vertex(edge[0]), self.skel_graph.vertex(edge[1]))

            for v in self.skel_graph.vertices():
                if v.out_degree() == 2 :
                    continue
                elif v.out_degree() == 1 :
                    terminal_index.append(int(v))
                elif v.out_degree() > 2 :
                    junction_index.append(int(v))

            self.terminal = self.verts[terminal_index]
            self.junction = self.verts[junction_index]
            self.terminal_index = terminal_index
            self.junction_index = junction_index
            self.feature_node_index = junction_index + terminal_index 
            self.feature_node = self.verts[self.feature_node_index]

            """
            edge_vert_index = self.edges.flatten()
            print 'edge vertex index dtype', edge_vert_index.dtype
            if 0 in edge_vert_index:
                print 'vertex start from 0'
            else:
                print 'vertex start from 1'
            print 'skeleton vertex num', self.skel_graph.num_vertices()
            print 'skeleton edge num', self.skel_graph.num_edges()
            """
    
    def _calc_edge_length(self):
        """
        calc edge length and make it edge property map in graph-tool
        """
        vec = self.verts[self.edges[:,0]] - self.verts[self.edges[:,1]]
        edge_length = np.sqrt(np.sum(vec**2, axis=-1))
        self.edge_length_map = self.skel_graph.new_edge_property("double")
        self.edge_length_map.a = edge_length
    

    def calc_node_centricity(self):
        """
        calc node centricity of feature nodes(terminal and junction nodes)
        T1 in Oscar's EG 2010 paper
        """
        self._calc_edge_length()
        node_centricity = []
        for n_idx in self.feature_node_index:
            dist = topology.shortest_distance(self.skel_graph, source=self.skel_graph.vertex(n_idx), weights=self.edge_length_map)
            node_centricity.append(dist.a.mean())

        node_centricity = np.array(node_centricity)
        self.node_centricity = node_centricity / np.max(node_centricity)


    def calc_skel_radius(self, mesh_name=None, dim=3):
        """
        calc nearest mesh vertex of skeleton vertex
        """
        if mesh_name != None:
            self.mesh_name = mesh_name

        if self.mesh_name == None:
            print 'please set mesh_name before calc_skel_radius'
        elif os.path.isfile(self.mesh_name):
            mesh = om.TriMesh()
            assert om.read_mesh(mesh, self.mesh_name)
            mesh_vertices = np.zeros((mesh.n_vertices(), dim), dtype=float)
            for n, vh in enumerate(mesh.vertices()):
                for i in xrange(3):
                    mesh_vertices[n, i] = mesh.point(vh)[i]

            nbrs = NearestNeighbors(n_neighbors=1, algorithm='ball_tree').fit(mesh_vertices)
            self.vert_radius, indices = nbrs.kneighbors(self.verts)
        else:
            print 'cannot find mesh file', self.mesh_name                
            sys.exit(0)


    def calc_path_radius(self, start, end):
        """
        utile function for other function
        calc skeleton **mean** vertex radius along some segment
        """
        if self.vert_radius == None:
            print 'please call calc_skel_radius function first'
            return None
        elif start in self.feature_node_index and end in self.feature_node_index:
            v_list, e_list = topology.shortest_path(self.skel_graph, self.skel_graph.vertex(start), self.skel_graph.vertex(end), weights=self.edge_length_map)
            v_idx_list = []
            for v in v_list:
                v_idx_list.append(int(v))
            v_radius = self.vert_radius[v_idx_list]
            return v_radius.mean()
        else:
            print 'input vertex index is not feature node index'
            return None
    

    def calc_path_length_ratio(self):
        """
        for each feature node pair segment, calculate path length ratio
        normalized, to make it scale invariant
        """
        path_length = np.zeros((len(self.feature_node_index), len(self.feature_node_index)), dtype=float)
        for i, n_idx in enumerate(self.feature_node_index):
            for j, m_idx in enumerate(self.feature_node_index[i+1:], start=i+1):
                length = topology.shortest_distance(self.skel_graph, self.skel_graph.vertex(n_idx), self.skel_graph.vertex(m_idx), weights=self.edge_length_map)
                if length != None :
                    path_length[i,j] = path_length[j,i] = length
                else:
                    print 'compute path length ratio error'
                    return None

        ### extract path length from each feature node to junction nodes ###
        ### Careful!! path_length MUST start from junction node
        self.path_to_junction = path_length[:,:len(self.junction_index)]

        self.path_length_ratio = path_length / path_length.max()
        return self.path_length_ratio
    

    def calc_path_radius_ratio(self):
        """
        for each feature node pair segment, calculate path radius ratio   
        normalized, to make it scale invariant
        """
        path_radius = np.zeros((len(self.feature_node_index), len(self.feature_node_index)), dtype=float)
        for i, n_idx in enumerate(self.feature_node_index):
            for j, m_idx in enumerate(self.feature_node_index[i+1:], start=i+1):
                radius = self.calc_path_radius(n_idx, m_idx)
                if radius != None :
                    path_radius[i, j] = path_radius[j, i] = radius
                else:
                    print 'comptue path radius error'
                    return None

        self.path_radius_ratio = path_radius / path_radius.max()
        return self.path_radius_ratio


    def normalize_skeleton(self):
        """
        calc the pose-normalized skeleton to distinguish symmetric nodes
        using multidimensional scaling method(MDS)
        """
        v_num = len(self.verts)
        geodesic_dist = np.zeros((v_num, v_num))
        geodesic_dist_map = topology.shortest_distance(self.skel_graph, weights=self.edge_length_map)
        for i in xrange(v_num):
            geodesic_dist[i] = geodesic_dist_map[self.skel_graph.vertex(i)].a

        mds = manifold.MDS(n_components=3, max_iter=500, eps=1e-10, dissimilarity="precomputed", n_jobs=-2, n_init=1)
        verts_mean = self.verts - self.verts.mean(axis=0)
        normalized_verts = mds.fit(geodesic_dist, init=verts_mean).embedding_
        #scale = np.sqrt((verts_mean ** 2).sum()) / np.sqrt((normalized_verts ** 2).sum())
        #normalized_verts *= scale
        self.normalized_verts = normalized_verts
        self.normalized_feature_verts = normalized_verts[self.feature_node_index]
        return self.normalized_verts


    def write_file(self, file_path='./'):
        """
        maybe need to save file after filter
        same as starlab mean curvature skeleton
        """
        file_name = os.path.basename(self.skel_name)
        full_name = file_path + file_name
        v_num = len(self.verts)
        e_num = len(self.edges)
        first_line = '# D:3 ' + 'NV:' + str(v_num) + ' NE:' + str(e_num) + '\n'
        with open(full_name, 'w') as f:
            f.write(first_line)
            for v in self.verts:
                line = 'v ' + str(v[0]) + ' ' + str(v[1]) + ' ' + str(v[2]) + '\n'
                f.write(line)

            for e in self.edges:
                line = 'e ' + str(e[0]+1) + ' ' + str(e[1]+1) + '\n'
                f.write(line)
示例#50
0
    def makeGraphFast(self,img,dia,xScale,yScale):
        print('Building Graph Data Structure'),
        start=time.time()
        G = Graph(directed=False)
        sumAddVertices=0
        
        vprop=G.new_vertex_property('object')
        eprop=G.new_edge_property('object')
        epropW=G.new_edge_property("float")
        h, w = np.shape(img)
        if xScale>0 and yScale>0: avgScale=(xScale+yScale)/2
        else: 
            avgScale=1.
            xScale=1.
            yScale=1.
        addedVerticesLine2=[]
        vListLine2=[]
        percentOld=0
        counter=0
        '''
        Sweep over each line in the image except the last line
        '''
        for idx,i in enumerate(img[:len(img)-2]):
            '''
            Get foreground indices in the current line of the image and make vertices
            '''
            counter+=1
            percent=(float(counter)/float(h))*100
            if percentOld+10< percent: 
                print (str(np.round(percent,1))+'% '),
                percentOld=percent

            line1=np.where(i==True)
            if len(line1[0])>0:
                line1=set(line1[0]).difference(set(addedVerticesLine2))
                vL=G.add_vertex(len(list(line1)))
                
                
                if len(line1)>1 : 
                    vList=vListLine2+list(vL)
                else: vList=vListLine2+[vL]
                line1=addedVerticesLine2+list(line1)
                for jdx,j in enumerate(line1):
                    vprop[vList[jdx]]={'imgIdx':(j,idx),'coord': (float(j)*xScale,float(idx)*yScale), 'nrOfPaths':0, 'diameter':float(dia[idx][j])*avgScale}
                '''
                keep order of the inserted vertices
                '''
                sumAddVertices+=len(line1)
                
                addedVerticesLine2=[]
                vListLine2=[]
                '''
                Connect foreground indices to neighbours in the next line
                '''
                for v1 in line1:
                    va=vList[line1.index(v1)]
                    diagonalLeft = diagonalRight = True
                    try:
                        if img[idx][v1-1]==True:
                            diagonalLeft=False
                            vb=vList[line1.index(v1-1)]
                            e=G.add_edge(va,vb)
                            eprop[e]={'coord1':vprop[va]['coord'], 'coord2':vprop[vb]['coord'],'weight':((vprop[va]['diameter']+vprop[vb]['diameter'])/2),'RTP':False}
                            epropW[e]=2./(eprop[e]['weight']**2)
                    except:
                        print 'Boundary vertex at: '+str([v1,idx-1])+' image size: '+ str([w,h])
                        pass
                    
                    try:
                        if img[idx][v1+1]==True:
                            diagonalRight=False
                            vb=vList[line1.index(v1+1)]
                            e=G.add_edge(va,vb)
                            eprop[e]={'coord1':vprop[va]['coord'], 'coord2':vprop[vb]['coord'],'weight':((vprop[va]['diameter']+vprop[vb]['diameter'])/2),'RTP':False}
                            epropW[e]=2./(eprop[e]['weight']**2)
                    except:
                        print 'Boundary vertex at: '+str([v1+1,idx])+' image size: '+ str([w,h])
                        pass # just if we are out of bounds
                    
                    try:
                        if img[idx+1][v1]==True:
                            diagonalRight=False
                            diagonalLeft=False
                            vNew=G.add_vertex()
                            vprop[vNew]={'imgIdx':(v1,idx+1),'coord': (float(v1)*xScale,float(idx+1)*yScale), 'nrOfPaths':0, 'diameter':float(dia[idx+1][v1])*avgScale}
                            vListLine2.append(vNew)
                            e=G.add_edge(vList[line1.index(v1)],vNew)
                            eprop[e]={'coord1':vprop[va]['coord'], 'coord2':vprop[vNew]['coord'],'weight':((vprop[va]['diameter']+vprop[vNew]['diameter'])/2),'RTP':False}
                            epropW[e]=1./(eprop[e]['weight']**2)
                            if v1 not in addedVerticesLine2: addedVerticesLine2.append(v1)
                    except:
                        print 'Boundary vertex at: '+str([v1,idx+1])+' image size: '+ str([w,h])
                        pass
                    
                    try:    
                        if diagonalRight == True and img[idx+1][v1+1]==True:
                            vNew=G.add_vertex()
                            vprop[vNew]={'imgIdx':(v1+1,idx+1),'coord': (float(v1+1)*xScale,float(idx+1)*yScale), 'nrOfPaths':0, 'diameter':float(dia[idx+1][v1+1])*avgScale}
                            vListLine2.append(vNew)
                            e=G.add_edge(vList[line1.index(v1)],vNew)
                            eprop[e]={'coord1':vprop[va]['coord'], 'coord2':vprop[vNew]['coord'],'weight':((vprop[va]['diameter']+vprop[vNew]['diameter'])/2),'RTP':False}
                            epropW[e]=1.41/(eprop[e]['weight']**2)
                            if v1+1 not in addedVerticesLine2: addedVerticesLine2.append(v1+1)
                    except:
                        print 'Boundary vertex at: '+str([v1+1,idx+1])+' image size: '+ str([w,h])
                        pass
                    
                    try:
                        if diagonalLeft  == True and img[idx+1][v1-1]==True:
                            vNew=G.add_vertex()
                            vprop[vNew]={'imgIdx':(v1-1,idx+1),'coord': (float(v1-1)*xScale,float(idx+1)*yScale), 'nrOfPaths':0, 'diameter':float(dia[idx+1][v1-1])*avgScale}
                            vListLine2.append(vNew)
                            e=G.add_edge(vList[line1.index(v1)],vNew)
                            eprop[e]={'coord1':vprop[va]['coord'], 'coord2':vprop[vNew]['coord'],'weight':((vprop[va]['diameter']+vprop[vNew]['diameter'])/2),'RTP':False}
                            epropW[e]=1.41/(eprop[e]['weight']**2)
                            if v1-1 not in addedVerticesLine2: addedVerticesLine2.append(v1-1)
                    except:
                        print 'Boundary vertex at: '+str([v1-1,idx+1])+' image size: '+ str([w,h])
                        pass
                    try:
                        if img[idx][v1+1]==False and img[idx][v1-1]==False and img[idx+1][v1]==False and diagonalLeft==False and diagonalRight==False:
                            print 'tip detected'
                            if img[idx-1][v1-1]==False and img[idx-1][v1+1]==False and img[idx-1][v1]==False:
                                print 'floating pixel'
                    except:
                        pass
        
        print'done!'                               
        G.edge_properties["ep"] = eprop
        G.edge_properties["w"] = epropW
        G.vertex_properties["vp"] = vprop            
        print 'graph build in '+str(time.time()-start)
        l = gt.label_largest_component(G)
        u = gt.GraphView(G, vfilt=l)
        print '# vertices'
        print(u.num_vertices())
        print(G.num_vertices())
        if u.num_vertices()!=G.num_vertices(): self.__fail=float((G.num_vertices()-u.num_vertices()))/float(G.num_vertices())
        return u,u.num_vertices()
示例#51
0
def build_graph(df_list, sens='ST', top=410, min_sens=0.01,
                edge_cutoff=0.0):
    """
    Initializes and constructs a graph where vertices are the parameters
    selected from the first dataframe in 'df_list', subject to the
    constraints set by 'sens', 'top', and 'min_sens'.  Edges are the second
    order sensitivities of the interactions between those vertices,
    with sensitivities greater than 'edge_cutoff'.

    Parameters
    -----------
    df_list     : list
                  A list of two dataframes.  The first dataframe should be
                  the first/total order sensitivities collected by the
                  function data_processing.get_sa_data().
    sens        : str, optional
                  A string with the name of the sensitivity that you would
                  like to use for the vertices ('ST' or 'S1').
    top         : int, optional
                  An integer specifying the number of vertices to display (
                  the top sensitivity values).
    min_sens    : float, optional
                  A float with the minimum sensitivity to allow in the graph.
    edge_cutoff : float, optional
                  A float specifying the minimum second order sensitivity to
                  show as an edge in the graph.

    Returns
    --------
    g : graph-tool object
        a graph-tool graph object of the network described above.  Each
        vertex has properties 'param', 'sensitivity', and 'confidence'
        corresponding to the name of the parameter, value of the sensitivity
        index, and it's confidence interval.  The only edge property is
        'second_sens', the second order sensitivity index for the
        interaction between the two vertices it connects.
    """

    # get the first/total index dataframe and second order dataframe
    df = df_list[0]
    df2 = df_list[1]

    # Make sure sens is ST or S1
    if sens not in set(['ST', 'S1']):
        raise ValueError('sens must be ST or S1')
    # Make sure that there is a second order index dataframe
    try:
        if not df2:
            raise Exception('Missing second order dataframe!')
    except:
        pass

    # slice the dataframes so the resulting graph will only include the top
    # 'top' values of 'sens' greater than 'min_sens'.
    df = df.sort_values(sens, ascending=False)
    df = df.ix[df[sens] > min_sens, :].head(top)
    df = df.reset_index()

    # initialize a graph
    g = Graph()

    vprop_sens = g.new_vertex_property('double')
    vprop_conf = g.new_vertex_property('double')
    vprop_name = g.new_vertex_property('string')
    eprop_sens = g.new_edge_property('double')

    g.vertex_properties['param'] = vprop_name
    g.vertex_properties['sensitivity'] = vprop_sens
    g.vertex_properties['confidence'] = vprop_conf
    g.edge_properties['second_sens'] = eprop_sens

    # keep a list of all the vertices
    v_list = []

    # Add the vertices to the graph
    for i, param in enumerate(df['Parameter']):
        v = g.add_vertex()
        vprop_sens[v] = df.ix[i, sens]
        vprop_conf[v] = 1 + df.ix[i, '%s_conf' % sens] / df.ix[i, sens]
        vprop_name[v] = param
        v_list.append(v)

    # Make two new columns in second order dataframe that point to the vertices
    # connected on each row.
    df2['vertex1'] = -999
    df2['vertex2'] = -999
    for vertex in v_list:
        param = g.vp.param[vertex]
        df2.ix[df2['Parameter_1'] == param, 'vertex1'] = vertex
        df2.ix[df2['Parameter_2'] == param, 'vertex2'] = vertex

    # Only allow edges for vertices that we've defined
    df_edges = df2[(df2['vertex1'] != -999) & (df2['vertex2'] != -999)]
    # eliminate edges below a certain cutoff value
    pruned = df_edges[df_edges['S2'] > edge_cutoff]
    pruned.reset_index(inplace=True)
    # Add the edges for the graph
    for i, sensitivity in enumerate(pruned['S2']):
        v1 = pruned.ix[i, 'vertex1']
        v2 = pruned.ix[i, 'vertex2']
        e = g.add_edge(v1, v2)
        # multiply by a number to make the lines visible on the plot
        eprop_sens[e] = sensitivity * 150

    # These are ways you can reference properties of vertices or edges
    # g.vp.param[g.vertex(77)]
    # g.vp.param[v_list[0]]

    print ('Created a graph with %s vertices and %s edges.\nVertices are the '
           'top %s %s values greater than %s.\nOnly S2 values (edges) '
           'greater than %s are included.' %
           (g.num_vertices(), g.num_edges(), top, sens, min_sens, edge_cutoff))

    return g
from random import randint

from Stack import Stack
from AllConstants import *

all_child_graphs = []
output = open("Blockings_log_BK.txt","a")

if simulated_on_network == 1:
    ## Network is USIP ##

    ########### Creating as many layered graphs as there are channels ###########
    for i in range(number_frequency_bands):
        child_graph = Graph();
        vertices_set = child_graph.add_vertex(24)
        e01 = child_graph.add_edge(child_graph.vertex_index[0], child_graph.vertex_index[1])
        e05 = child_graph.add_edge(child_graph.vertex_index[0], child_graph.vertex_index[5])
        e12 = child_graph.add_edge(child_graph.vertex_index[1], child_graph.vertex_index[2])
        e15 = child_graph.add_edge(child_graph.vertex_index[1], child_graph.vertex_index[5])
        e23 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[3])
        e24 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[4])
        e26 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[6])
        e36 = child_graph.add_edge(child_graph.vertex_index[3], child_graph.vertex_index[6])
        e34 = child_graph.add_edge(child_graph.vertex_index[3], child_graph.vertex_index[4])
        e47 = child_graph.add_edge(child_graph.vertex_index[4], child_graph.vertex_index[7])
        e56 = child_graph.add_edge(child_graph.vertex_index[5], child_graph.vertex_index[6])
        e58 = child_graph.add_edge(child_graph.vertex_index[5], child_graph.vertex_index[8])
        e510 = child_graph.add_edge(child_graph.vertex_index[5], child_graph.vertex_index[10])
        e67 = child_graph.add_edge(child_graph.vertex_index[6], child_graph.vertex_index[7])
        e68 = child_graph.add_edge(child_graph.vertex_index[6], child_graph.vertex_index[8])
        e79 = child_graph.add_edge(child_graph.vertex_index[7], child_graph.vertex_index[9])
class BoardGraphGraphtool(BoardGraphBase):

    def __init__(self, number_of_vertices, graph_type):
        super().__init__(number_of_vertices, graph_type)
        # Graph tool creates directed multigraph by default.
        self._graph = Graph()
        self._graph.add_vertex(number_of_vertices)
        self._graph.vertex_properties["cell"] = self._graph.new_vertex_property(
            "object", number_of_vertices * [BoardCell()]
        )
        self._graph.edge_properties["direction"
                                   ] = self._graph.new_edge_property("object")
        self._graph.edge_properties["weight"
                                   ] = self._graph.new_edge_property("int")

    def __getitem__(self, position):
        return self._graph.vp.cell[self._graph.vertex(position)]

    def __setitem__(self, position, board_cell):
        self._graph.vp.cell[self._graph.vertex(position)] = board_cell

    def __contains__(self, position):
        return position in range(0, self.vertices_count())

    def vertices_count(self):
        return self._graph.num_vertices()

    def edges_count(self):
        return self._graph.num_edges()

    def has_edge(self, source_vertice, target_vertice, direction):
        for e in self._graph.vertex(source_vertice).out_edges():
            if (
                int(e.target()) == target_vertice and
                self._graph.ep.direction[e] == direction
            ):
                return True
        return False

    def out_edges_count(self, source_vertice, target_vertice):
        return len([
            1 for e in self._graph.vertex(source_vertice).out_edges()
            if int(e.target()) == target_vertice
        ])

    def reconfigure_edges(self, width, height, tessellation):
        """
        Uses tessellation object to create all edges in graph.
        """
        self._graph.clear_edges()
        for source_vertice in self._graph.vertices():
            for direction in tessellation.legal_directions:
                neighbor_vertice = tessellation.neighbor_position(
                    int(source_vertice),
                    direction,
                    board_width=width,
                    board_height=height
                )
                if neighbor_vertice is not None:
                    e = self._graph.add_edge(
                        source_vertice, neighbor_vertice, add_missing=False
                    )
                    self._graph.ep.direction[e] = direction

    # TODO: Faster version?
    # def reconfigure_edges(self, width, height, tessellation):
    #     """
    #     Uses tessellation object to create all edges in graph.
    #     """
    #     self._graph.clear_edges()
    #     edges_to_add = []
    #     directions_to_add = dict()
    #     for source_vertice in self._graph.vertices():
    #         for direction in tessellation.legal_directions:
    #             neighbor_vertice = tessellation.neighbor_position(
    #                 int(source_vertice), direction,
    #                 board_width=width, board_height=height
    #             )
    #             if neighbor_vertice is not None:
    #                 edge = (int(source_vertice), neighbor_vertice,)

    #                 edges_to_add.append(edge)

    #                 if edge not in directions_to_add:
    #                     directions_to_add[edge] = deque()

    #                 directions_to_add[edge].append(direction)

    #     self._graph.add_edge_list(edges_to_add) if edges_to_add else None

    #     for e in edges_to_add:
    #         e_descriptors = self._graph.edge(
    #             s = self._graph.vertex(e[0]),
    #             t = self._graph.vertex(e[1]),
    #             all_edges = True
    #         )

    #         for e_descriptor in e_descriptors:
    #             if len(directions_to_add[e]) > 0:
    #                 self._graph.ep.direction[e_descriptor] = directions_to_add[e][0]
    #                 directions_to_add[e].popleft()

    def calculate_edge_weights(self):
        for e in self._graph.edges():
            self._graph.ep.weight[e] = self.out_edge_weight(int(e.target()))

    def neighbor(self, from_position, direction):
        try:
            for e in self._graph.vertex(from_position).out_edges():
                if self._graph.ep.direction[e] == direction:
                    return int(e.target())
        except ValueError as e:
            raise IndexError(e.args)

        return None

    def wall_neighbors(self, from_position):
        return [
            int(n) for n in self._graph.vertex(from_position).out_neighbours()
            if self[int(n)].is_wall
        ]

    def all_neighbors(self, from_position):
        return [
            int(n) for n in self._graph.vertex(from_position).out_neighbours()
        ]

    def shortest_path(self, start_position, end_position):
        try:
            return [
                int(v)
                for v in shortest_path(
                    g=self._graph,
                    source=self._graph.vertex(start_position),
                    target=self._graph.vertex(end_position),
                )[0]
            ]
        except ValueError:
            return []

    def dijkstra_path(self, start_position, end_position):
        try:
            self.calculate_edge_weights()
            return [
                int(v)
                for v in shortest_path(
                    g=self._graph,
                    source=self._graph.vertex(start_position),
                    target=self._graph.vertex(end_position),
                    weights=self._graph.ep.weight,
                )[0]
            ]
        except ValueError:
            return []

    def position_path_to_direction_path(self, position_path):
        retv = []
        src_vertice_index = 0
        for target_vertice in position_path[1:]:
            source_vertice = position_path[src_vertice_index]
            src_vertice_index += 1

            for out_edge in self._graph.vertex(source_vertice).out_edges():
                if int(out_edge.target()) == target_vertice:
                    retv.append(self._graph.ep.direction[out_edge])

        return {
            'source_position': position_path[0] if position_path else None,
            'path': retv
        }
示例#54
0
class SkeletonMatch(object):
    """
    implement Oscar's skeleton matching alogrithm in this class
    """
    def __init__(self, skel1, skel2, centricity=.5, length=.5, distorted=20.):
        if skel1 is not None and skel2 is not None :
            self.skel1 = skel1
            self.skel2 = skel2
            self.centricity_threhold = centricity
            self.length_threhold = length
            self.distorted_threhold = distorted
            self.skel1.calc_skel_properties()
            self.skel2.calc_skel_properties()
            # use index instead of real value
            skel1_index = np.arange(len(self.skel1.feature_node_index))
            skel2_index = np.arange(len(self.skel2.feature_node_index))
            junc1_num = len(skel1.junction_index)
            junc2_num = len(skel2.junction_index)

            #print 'skel1 normalized_verts\n', skel1.normalized_feature_verts
            #print 'skel2 normalized_verts\n', skel2.normalized_feature_verts

            #candidate matched pairs
            junction_pairs = []
            junc_term_pairs = []
            terminal_pairs = []
            for i, j in itertools.product(skel1_index, skel2_index):
                if self.test_node_centricity(c1=i, c2=j):
                    if i < junc1_num and j < junc2_num: # only junction nodes
                        junction_pairs.append([i,j])
                    elif i >= junc1_num and j >= junc2_num: # only terminal nodes
                        terminal_pairs.append([i,j])
                    else:
                        junc_term_pairs.append([i,j])

            self.junction_pairs = np.array(junction_pairs)
            self.terminal_pairs = np.array(terminal_pairs)
            self.junc_term_pairs = np.array(junc_term_pairs)
            #self.all_junc_pairs = np.vstack((self.junction_pairs, self.junc_term_pairs))

            self.vote_tree = Graph(directed=False)
            self.node_pair = self.vote_tree.new_vertex_property("vector<short>")

            self._construct_voting_tree()
        else:
            print 'need input two skeleton to match'


    def _construct_voting_tree(self, prev_pairs=np.array([]), mix_junc_term=True):
        """
        recursively consturct voting tree
        @param prev_pairs record that already on the path
        @param junc_pairs record that left part junction pairs (on current tree level)
        @param term_pairs record that left terminal pairs on current tree level

        now limits: at least one junction pair
        """
        # root of the tree
        if len(prev_pairs) == 0:
            v1 = self.vote_tree.add_vertex()
            #first level, only junction pairs (both are junction)
            for n, pair in enumerate(self.junction_pairs):
                new_prev = pair.reshape(-1,2)  # to use len(for level one), need to change shape
                print 'adding subtree', n+1, '/', len(self.junction_pairs)
                v2 = self._construct_voting_tree(prev_pairs=new_prev)
                self.vote_tree.add_edge(v1, v2)
            return v1                    # return the root

        elif len(prev_pairs) == 1:  # first level
            v1 = self.vote_tree.add_vertex()
            self.node_pair[v1] = prev_pairs.flatten()
            """
            priority order: junction pairs, terminal pairs, junc-term pairs
            """

            check_junc = True
            #prepare for next(second) level
            for n, pair in enumerate(self.junction_pairs):
                if pair[0] not in prev_pairs[:,0] and pair[1] not in prev_pairs[:,1]:
                    new_prev = np.vstack((prev_pairs, pair))
                    v2 = self._construct_voting_tree(prev_pairs=new_prev)
                    if v2 is not None: 
                        check_junc = False
                        self.vote_tree.add_edge(v1, v2)

            # it is sure that that should be some terminal_pairs
            # but in case
            check_term = mix_junc_term # if True allow mix junc and term 
            if check_junc:
                for n, pair in enumerate(self.terminal_pairs):
                    new_prev = np.vstack((prev_pairs, pair))
                    v2 = self._construct_voting_tree(prev_pairs=new_prev, mix_junc_term=True)
                    if v2 is not None:
                        check_term = False
                        self.vote_tree.add_edge(v1, v2)

            if check_junc and check_term:
                for n, pair in enumerate(self.junc_term_pairs):
                    new_prev = np.vstack((prev_pairs, pair))
                    v2 = self._construct_voting_tree(prev_pairs=new_prev)
                    if v2 is not None:
                        self.vote_tree.add_edge(v1, v2)

            return v1                   # return the first level of the tree

        elif 4 > len(prev_pairs) > 1:  # above level two
            #if satisfy T2 (length and radius prune)
            if self.test_length_radius(n1=prev_pairs[-1,0], n2=prev_pairs[-1,1], matched_pairs=prev_pairs[:-1]) and self.test_topology_consistency(n1=prev_pairs[-1,0], n2=prev_pairs[-1,1], matched_pairs=prev_pairs[:-1]):
                v1 = self.vote_tree.add_vertex()
                self.node_pair[v1] = prev_pairs.flatten()

                check_junc = True
                for pair in self.junction_pairs:
                    if pair[0] not in prev_pairs[:,0] and pair[1] not in prev_pairs[:,1]:
                        new_prev = np.vstack((prev_pairs, pair))
                        v2 = self._construct_voting_tree(prev_pairs=new_prev)
                        if v2 is not None:
                            check_junc = False
                            self.vote_tree.add_edge(v1, v2)
                
                check_term = mix_junc_term   # if allow mix junc and term
                if check_junc:
                    for pair in self.terminal_pairs:
                        if pair[0] not in prev_pairs[:,0] and pair[1] not in prev_pairs[:,1]:
                            new_prev = np.vstack((prev_pairs, pair))
                            v2 = self._construct_voting_tree(prev_pairs=new_prev, mix_junc_term=True)
                            if v2 is not None:
                                check_term = False
                                self.vote_tree.add_edge(v1, v2)

                if check_junc and check_term:
                    for pair in self.junc_term_pairs:
                        if pair[0] not in prev_pairs[:,0] and pair[1] not in prev_pairs[:,1]:
                            new_prev = np.vstack((prev_pairs, pair))
                            v2 = self._construct_voting_tree(prev_pairs=new_prev)
                            if v2 is not None:
                                self.vote_tree.add_edge(v1, v2)

                return v1             # return second and above level tree
            else:
                return None             # fail to match

        elif len(prev_pairs) >= 4:
            if self.test_length_radius(n1=prev_pairs[-1,0], n2=prev_pairs[-1,1], matched_pairs=prev_pairs[:-1]) and self.test_topology_consistency(n1=prev_pairs[-1,0], n2=prev_pairs[-1,1], matched_pairs=prev_pairs[:-1]):
                #print 'len(prev_pairs) >= 4',
                #print 'current pairs\n', prev_pairs, 
                if self.test_spatial_configuration(n1=prev_pairs[-1,0], n2=prev_pairs[-1,1], matched_pairs=prev_pairs[:-1]):
                    v1 = self.vote_tree.add_vertex()
                    self.node_pair[v1] = prev_pairs.flatten()
                    #print 'succeed testing spatial', '[', prev_pairs[-1,0], prev_pairs[-1,1], ']',
                    #print 'from\n',  prev_pairs[:-1]
                    #print 'current matched pairs\n', prev_pairs

                    check_junc = True
                    for pair in self.junction_pairs:
                        if pair[0] not in prev_pairs[:,0] and pair[1] not in prev_pairs[:,1]:
                            new_prev = np.vstack((prev_pairs, pair))
                            v2 = self._construct_voting_tree(prev_pairs=new_prev)
                            if v2 is not None:
                                check_junc = False
                                self.vote_tree.add_edge(v1, v2)

                    check_term = mix_junc_term # if True allow mix junction and terminal
                    if check_junc:
                        for pair in self.terminal_pairs:
                            if pair[0] not in prev_pairs[:,0] and pair[1] not in prev_pairs[:,1]:
                                new_prev = np.vstack((prev_pairs, pair))
                                v2 = self._construct_voting_tree(prev_pairs=new_prev)
                                if v2 is not None:
                                    check_term = False
                                    self.vote_tree.add_edge(v1, v2)

                    if check_junc and check_term:
                        for pair in self.junc_term_pairs:
                            if pair[0] not in prev_pairs[:,0] and pair[1] not in prev_pairs[:,1]:
                                new_prev = np.vstack((prev_pairs, pair))
                                v2 = self._construct_voting_tree(prev_pairs=new_prev)
                                if v2 is not None:
                                    self.vote_tree.add_edge(v1, v2)
                    return v1
                else:
                    return None
            else:
                return None
    

    def test_node_centricity(self, c1, c2):
        """
        match node centricity of the graph
        """
        node_cent1 = self.skel1.node_centricity[c1]
        node_cent2 = self.skel2.node_centricity[c2]
        match_result = abs(node_cent1 - node_cent2) / (node_cent1 + node_cent2)
        threhold = self.centricity_threhold * .5
        return match_result < threhold


    def test_length_radius(self, n1, n2, matched_pairs):
        """
        match path length and radius from n1/n2 to nodes that already in matched_pairs
        """
        path_len1 = self.skel1.path_length_ratio[n1, matched_pairs[:,0]]
        path_len2 = self.skel2.path_length_ratio[n2, matched_pairs[:,1]]
        length_match = abs(path_len1 - path_len2) / (path_len1 + path_len2)
        threhold = self.length_threhold * 0.5
        #if all satisfied
        if np.all(length_match < threhold):
            path_rad1 = self.skel1.path_radius_ratio[n1, matched_pairs[:,0]]
            path_rad2 = self.skel2.path_radius_ratio[n2, matched_pairs[:,1]]
            radius_match = abs(path_rad1 - path_rad2) / (path_rad1 + path_rad2)
            if np.all(radius_match < threhold):
                return True
            else:
                return False
        else:
            return False


    def test_topology_consistency(self, n1, n2, matched_pairs):
        """
        match skeleton topology consistency
        """
        if len(matched_pairs) > 0:
            junct1 = matched_pairs[matched_pairs[:,0] < len(self.skel1.junction_index), 0]
            junct2 = matched_pairs[matched_pairs[:,1] < len(self.skel2.junction_index), 1]
            if len(junct1) < 1 or len(junct2) < 1:
                print 'no junction node in already matched pairs'
                return False
            else:
                idx1 = np.argmin(self.skel1.path_to_junction[n1, junct1])
                idx2 = np.argmin(self.skel2.path_to_junction[n2, junct2])

                """
                print '\n[',n1,',',n2,']', 
                print 'nearest pair[',junct1[idx1],',', junct2[idx2],']',
                if [junct1[idx1], junct2[idx2]] in matched_pairs.tolist():
                    print ' IN ',
                else:
                    print ' NOT in ',

                for pair in matched_pairs:
                    print pair,
                print '\n'
                """

                return [junct1[idx1], junct2[idx2]] in matched_pairs.tolist()
        else:
            print 'none in matched_pairs'
            return False


    def test_spatial_configuration(self, n1, n2, matched_pairs):
        """
        match spatial configuration
        """
        threhold = self.distorted_threhold
        #need test if can be inverse
        skel1_vectors = self.skel1.normalized_feature_verts[matched_pairs[-3:,0]] - self.skel1.normalized_feature_verts[n1]
        skel2_vectors = self.skel2.normalized_feature_verts[matched_pairs[-3:,1]] - self.skel2.normalized_feature_verts[n2]
        #skel1_vectors = self.skel1.feature_node[matched_pairs[-3:,0]] - self.skel1.feature_node[n1]
        #skel2_vectors = self.skel2.feature_node[matched_pairs[-3:,1]] - self.skel2.feature_node[n2]
        """
        for i in xrange(3):
            skel1_vectors[i] *= ( 1. / np.linalg.norm(skel1_vectors[i]) )
            skel2_vectors[i] *= ( 1. / np.linalg.norm(skel2_vectors[i]) )
        """

        a = np.dot(skel2_vectors, np.linalg.inv(skel1_vectors))
        u, s, v = np.linalg.svd(a)
        r = np.dot(u, v)
        if np.linalg.det(r) < 0:
            r *= -1.0

        res1 = np.linalg.norm(a-r)
        #print 'res1', res1,
        if res1 > threhold:
            return False
        else:
            a = np.dot(skel1_vectors, np.linalg.inv(skel2_vectors))
            u, s, v = np.linalg.svd(a)
            r = np.dot(u, v)
            if np.linalg.det(r) < 0:
                r *= -1.0

            res2 = np.linalg.norm(a-r)
            if res2 > threhold:
                return False
            #print 'res2', res2

        #return max(res1, res2) <= threhold
        return True


    def elector_vote(self):
        """
        use elector vote to find better correspondence
        """
        vote_matrix = np.zeros((len(self.skel1.feature_node_index), len(self.skel2.feature_node_index)), dtype=int)
        for v in self.vote_tree.vertices():
            if v.out_degree() < 2:
                pairs = self.node_pair[v]
                if len(pairs) >= 8:
                    temp_pairs = pairs.a.reshape(-1,2)
                    for pair in temp_pairs:
                        vote_matrix[pair[0], pair[1]] += 1

        node_num_skel1 = len(self.skel1.feature_node_index)
        node_num_skel2 = len(self.skel2.feature_node_index)
        self.vote_matrix = vote_matrix.copy()

        
        #print 'original vote_matrix\n', vote_matrix
        if np.max(vote_matrix) == 0:
            final_corres = np.array([])
        else:
            node_pair = np.unravel_index(vote_matrix.argmax(), vote_matrix.shape)
            vote_matrix[node_pair[0], :] = vote_matrix[:, node_pair[1]] = 0
            final_corres = np.array(node_pair)
            final_corres.shape = (-1,2)
            #print 'correspondence\n', final_corres
            #print 'vote_matrix\n', vote_matrix
            while len(final_corres) < min(node_num_skel1, node_num_skel2):
                junct1 = final_corres[final_corres[:,0] < len(self.skel1.junction_index), 0]
                junct2 = final_corres[final_corres[:,1] < len(self.skel2.junction_index), 1]
                node_pair = np.unravel_index(vote_matrix.argmax(), vote_matrix.shape)
                if node_pair[0] not in final_corres[:,0] and node_pair[1] not in final_corres[:,1]:
                    if len(junct1) < 3 or len(junct2) < 3:
                        print 'less than 3 junction matched'
                        final_corres = np.vstack((final_corres, node_pair))
                        vote_matrix[node_pair[0], :] = vote_matrix[:, node_pair[1]] = 0
                    else:
                        idx1 = np.argmin(self.skel1.path_to_junction[node_pair[0], junct1])
                        idx2 = np.argmin(self.skel2.path_to_junction[node_pair[1], junct2])
                        if [junct1[idx1], junct2[idx2]] in final_corres.tolist():
                            final_corres = np.vstack((final_corres, node_pair))
                            vote_matrix[node_pair[0], :] = vote_matrix[:, node_pair[1]] = 0
                            #print 'added correspondence\n', final_corres
                            #print 'vote_matrix\n', vote_matrix
                        else:
                            vote_matrix[node_pair[0], node_pair[1]] = 0
                else:
                    vote_matrix[node_pair[0], node_pair[1]] = 0

                if np.all(vote_matrix == 0):
                    break

        self.final_corres = final_corres
示例#55
0
    def makeGraph(self,img,dia,xScale,yScale):
        print 'Building Graph Data Structure'
        start=time.time()
        G = Graph(directed=False)
        vprop=G.new_vertex_property('object')
        eprop=G.new_edge_property('object')
        epropW=G.new_edge_property("int32_t")
        avgScale=(xScale+yScale)/2

        test=np.where(img==True)
        ss = np.shape(test)
        cccc=0
        percentOld=0.0
        print str(np.round(percentOld,1))+'%'
        for (i,j) in zip(test[1],test[0]):
                cccc+=1
                percent=(float(cccc)/float(ss[1]))*100
                if percentOld+10< percent: 
                    print str(np.round(percent,1))+'%'
                    percentOld=percent
                nodeNumber1 = (float(i)*yScale,float(j)*xScale)
                if gu.find_vertex(G, vprop, {'imgIdx':(j,i),'coord':nodeNumber1, 'nrOfPaths':0, 'diameter':float(dia[j][i])*avgScale}):
                            v1=gu.find_vertex(G, vprop, {'imgIdx':(j,i),'coord':nodeNumber1, 'nrOfPaths':0, 'diameter':float(dia[j][i])*avgScale})[0]
                else:
                    v1=G.add_vertex()
                    vprop[G.vertex(v1)]={'imgIdx':(j,i),'coord':nodeNumber1, 'nrOfPaths':0, 'diameter':float(dia[j][i])*avgScale}
                try:
                    
                    if img[j,i+1] == True:
                        nodeNumber2 = (float(i+1)*yScale,float(j)*xScale)
                        if gu.find_vertex(G, vprop, {'imgIdx':(j,i+1),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j][i+1])*avgScale}):
                            v2=gu.find_vertex(G, vprop, {'imgIdx':(j,i+1),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j][i+1])*avgScale})[0]
                            if gu.find_edge(G, eprop, {'coord1':vprop[v2]['coord'], 'coord2':vprop[v1]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}):
                                pass
                            else:
                                e = G.add_edge(v1, v2)
                                epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4
                                eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}
                        else:
                            v2=G.add_vertex()
                            vprop[G.vertex(v2)]={'imgIdx':(j,i+1),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j][i+1])*avgScale}
                            e = G.add_edge(v1, v2)
                            epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4
                            eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}
                except:
                    pass
                try:
                    if img[j,i-1] == True:
                        nodeNumber2 = (float(i-1)*yScale,float(j)*xScale)
                        if gu.find_vertex(G, vprop, {'imgIdx':(j,i-1),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j][i-1])*avgScale}):
                            v2=gu.find_vertex(G, vprop, {'imgIdx':(j,i-1),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j][i-1])*avgScale})[0]
                            if gu.find_edge(G, eprop, {'coord1':vprop[v2]['coord'], 'coord2':vprop[v1]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}):
                                pass
                            else:
                                e = G.add_edge(v1, v2)
                                epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4
                                eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}
                        else:
                            v2=G.add_vertex()
                            vprop[G.vertex(v2)]={'imgIdx':(j,i-1),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j][i-1])*avgScale}
                            e = G.add_edge(v1, v2)
                            epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4
                            eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}
                except:pass
                try:
                    if img[j + 1,i] == True:
                        nodeNumber2 = (float(i)*yScale,float(j+1)*xScale)
                        if gu.find_vertex(G, vprop, {'imgIdx':(j+1,i),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j+1][i])*avgScale}):
                            v2=gu.find_vertex(G, vprop, {'imgIdx':(j+1,i),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j+1][i])*avgScale})[0]
                            if gu.find_edge(G, eprop, {'coord1':vprop[v2]['coord'], 'coord2':vprop[v1]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}):
                                pass
                            else:
                                e = G.add_edge(v1, v2)
                                epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4
                                eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}
                        else:
                            v2=G.add_vertex()
                            vprop[G.vertex(v2)]={'imgIdx':(j+1,i),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j+1][i])*avgScale}
                            e = G.add_edge(v1, v2)
                            epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4
                            eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}
                except:pass
                try:
                    if img[j - 1,i] == True:
                        nodeNumber2 = (float(i)*yScale,float(j-1)*xScale)
                        if gu.find_vertex(G, vprop, {'imgIdx':(j-1,i),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j-1][i])*avgScale}):
                            v2=gu.find_vertex(G, vprop, {'imgIdx':(j-1,i),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j-1][i])*avgScale})[0]
                            if gu.find_edge(G, eprop, {'coord1':vprop[v2]['coord'], 'coord2':vprop[v1]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}):
                                pass
                            else:
                                e = G.add_edge(v1, v2)
                                epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4
                                eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}
                        else:
                            v2=G.add_vertex()
                            vprop[G.vertex(v2)]={'imgIdx':(j-1,i),'coord':nodeNumber2, 'nrOfPaths':0, 'diameter':float(dia[j-1][i])*avgScale}
                            e = G.add_edge(v1, v2)
                            epropW[e]=(((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)/avgScale)**4
                            eprop[e]={'coord1':vprop[v1]['coord'], 'coord2':vprop[v2]['coord'],'weight':((vprop[v1]['diameter']+vprop[v2]['diameter'])/2)**4,'RTP':False}
                except: pass
#                    
        print '100.0%'
        print 'selecting largest connected component'
        G.edge_properties["ep"] = eprop
        G.edge_properties["w"] = epropW
        G.vertex_properties["vp"] = vprop
        l = gt.label_largest_component(G)
        print(l.a)
        u = gt.GraphView(G, vfilt=l)
        print '# vertices'
        print(u.num_vertices())
        print(G.num_vertices())
        print '# edges'
        print(u.num_edges())
        print 'building graph finished in: '+str(time.time()-start)+'s'
        return u 
    ## Network is USIP ##

    ## Creating the USIP network ##
    g = Graph(directed=False)
    vertices_set = g.add_vertex(24)
    '''
    e01 = g.add_edge(g.vertex_index[0], g.vertex_index[1])
    e02 = g.add_edge(g.vertex_index[0], g.vertex_index[2])
    e12 = g.add_edge(g.vertex_index[1], g.vertex_index[2])
    e13 = g.add_edge(g.vertex_index[1], g.vertex_index[3])
    e24 = g.add_edge(g.vertex_index[2], g.vertex_index[4])
    e34 = g.add_edge(g.vertex_index[3], g.vertex_index[4])
    e35 = g.add_edge(g.vertex_index[3], g.vertex_index[5])
    e45 = g.add_edge(g.vertex_index[4], g.vertex_index[5])
    '''
    e01 = g.add_edge(g.vertex_index[0], g.vertex_index[1])
    e05 = g.add_edge(g.vertex_index[0], g.vertex_index[5])
    e12 = g.add_edge(g.vertex_index[1], g.vertex_index[2])
    e15 = g.add_edge(g.vertex_index[1], g.vertex_index[5])
    e23 = g.add_edge(g.vertex_index[2], g.vertex_index[3])
    e24 = g.add_edge(g.vertex_index[2], g.vertex_index[4])
    e26 = g.add_edge(g.vertex_index[2], g.vertex_index[6])
    e36 = g.add_edge(g.vertex_index[3], g.vertex_index[6])
    e34 = g.add_edge(g.vertex_index[3], g.vertex_index[4])
    e47 = g.add_edge(g.vertex_index[4], g.vertex_index[7])
    e56 = g.add_edge(g.vertex_index[5], g.vertex_index[6])
    e58 = g.add_edge(g.vertex_index[5], g.vertex_index[8])
    e510 = g.add_edge(g.vertex_index[5], g.vertex_index[10])
    e67 = g.add_edge(g.vertex_index[6], g.vertex_index[7])
    e68 = g.add_edge(g.vertex_index[6], g.vertex_index[8])
    e79 = g.add_edge(g.vertex_index[7], g.vertex_index[9])
示例#57
0
文件: main.py 项目: crazy2be/cs452
def main():
	conn = serial_interface.connect()

	cur_track = track.init_tracka()
	g = Graph()
	g.add_vertex(len(cur_track))
	for (vi, node) in enumerate(cur_track): node.i = vi

	n_title = g.new_vertex_property("string")
	n_color = g.new_vertex_property("string")
	n_pos = g.new_vertex_property("vector<double>")
	e_title = g.new_edge_property("string")
	e_dist = g.new_edge_property("double")

	for node in cur_track:
		v = g.vertex(node.i)
		n_title[v] = node.name
		if node.typ == track.NODE_EXIT:
			# Invert points to match our ASCII display.
			n_pos[v] = (-node.reverse.coord_x, -node.reverse.coord_y)
		else:
			n_pos[v] = (-node.coord_x, -node.coord_y)
		e = g.add_edge(g.vertex(node.i), g.vertex(node.reverse.i))
		if node.typ == track.NODE_SENSOR: n_color[v] = "blue"
		elif node.typ == track.NODE_BRANCH: n_color[v] = "orange"
		elif node.typ == track.NODE_MERGE: n_color[v] = "yellow"
		elif node.typ == track.NODE_ENTER: n_color[v] = "green"
		elif node.typ == track.NODE_EXIT: n_color[v] = "red"
		else: n_color[v] = "white"
		for edge in node.edge:
			if edge.src is None: continue
			e = g.add_edge(g.vertex(edge.src.i), g.vertex(edge.dest.i))
			e_dist[e] = edge.dist
			e_title[e] = "%.2f" % (edge.dist)

	win = graph_tool.draw.GraphWindow(g, n_pos, (640, 480), edge_text=e_title, vertex_fill_color=n_color, vertex_text=n_title)
	win.show_all()
	def destroy_callback(*args, **kwargs):
		win.destroy()
		Gtk.main_quit()

	def set_switch(sw, d):
		for node in cur_track:
			if node.typ == track.NODE_BRANCH and node.num == sw:
				node.switch_direction = d
				return
		print "WARN: Could not find switch %d" % sw

	class Train():
		num = -1
		vel = 0.
		speed = 0.
		edge = cur_track[0].edge[0]
		edge_dist = 0
		SPEEDX = 1.

		def __init__(self, num):
			self.num = num

		def update(self):
			# Super shitty deacceleration model
			self.vel = self.vel + (0.018/self.SPEEDX)*(self.speed - self.vel)
			self.edge_dist += self.vel
			while True:
				e = self.e()
				if self.edge_dist < e_dist[e]: break
				if self.edge.dest.typ == track.NODE_SENSOR:
					conn.set_sensor_tripped(self.edge.dest.num)
				self.edge = self.edge.dest.edge[self.edge.dest.switch_direction]
				self.edge_dist -= e_dist[e]

		def draw(self, n_pos, da, cr):
			e = self.e()
			start, end = np.array(n_pos[e.source()]), np.array(n_pos[e.target()])
			alpha = self.edge_dist / e_dist[e]
			pos = start + alpha*(end - start)
			dp = win.graph.pos_to_device(pos) # dp: device position
			cr.rectangle(dp[0]-10, dp[1]-10, 20, 20)
			cr.set_source_rgb(102. / 256, 102. / 256, 102. / 256)
			cr.fill()
			cr.move_to(dp[0]-10, dp[1] + 10 - 12./2)
			cr.set_source_rgb(1., 1., 1.)
			cr.set_font_size(12)
			cr.show_text("%d" % self.num)
			cr.fill()
		def e(self): return g.edge(self.edge.src.i, self.edge.dest.i)
		def set_speed(self, speed): self.speed = speed/self.SPEEDX
		def toggle_reverse(self):
			self.edge = self.edge.reverse
			self.edge_dist = e_dist[self.e()] - self.edge_dist

	def find_train(train_number):
		for train in trains:
			if train.num == train_number:
				return train
		train = Train(train_number)
		trains.append(train)
		return train

	trains = [Train(12)]
	startup_time = time.time()
	accumulated_error = [0.]
	last_time = [time.time()]
	last_sensor_poll = [0]
	FPS = 30.
	def my_draw(da, cr):
		(typ, a1, a2) = conn.next_cmd()
		if typ is None: pass
		elif typ == 'set_speed': find_train(a1).set_speed(a2)
		elif typ == 'toggle_reverse': find_train(a1).toggle_reverse()
		elif typ == 'switch': set_switch(a1, a2)
		elif typ == 'sensor': last_sensor_poll[0] = round((time.time() - startup_time) * 1000)/1000
		else: print "Ignoring command %s" % typ
		cur_time = time.time()
		dt = cur_time - last_time[0] + accumulated_error[0]
		num_steps = int(dt*FPS)
		accumulated_error[0] = dt - num_steps/FPS
		for train in trains:
			for _ in range(0, num_steps): train.update()
			train.draw(n_pos, da, cr)
			cr.move_to(10., 10.)
			cr.set_source_rgb(0., 0., 0.)
			cr.set_font_size(12)
			cr.show_text("Last polled at %.3f" % last_sensor_poll[0])
		da.queue_draw()
		last_time[0] = cur_time

	win.connect("delete_event", destroy_callback)
	win.graph.connect("draw", my_draw)
	Gtk.main()
示例#58
0
# graph-tool.skewed.de/
from graph_tool import Graph
from graph_tool import draw

g = Graph(directed=True)

for i in range(5):
    g.add_vertex()

v1 = g.add_vertex()
v2 = g.add_vertex()

v3 = g.vertex(2)

e1 = g.add_edge(v1, v2)
g.add_edge(v1, v3)

draw.graph_draw(g)

def build_truncated_closure(g, cand_source, terminals, infection_times,
                            k=-1,
                            debug=False,
                            verbose=False,
                            **kawrgs):
    """
    build a clojure graph in which cand_source + terminals are all connected to each other.
    the number of neighbors of each node is determined by k

    the larger the k, the denser the graph"""
    r2pred = {}
    edges = {}
    terminals = list(terminals)

    # from cand_source to terminals
    vis = init_visitor(g, cand_source)

    cpbfs_search(g, source=cand_source, visitor=vis, terminals=terminals,
                 forbidden_nodes=terminals,
                 count_threshold=-1)  # k=-1 here because root connects to all other nodes
    r2pred[cand_source] = vis.pred
    for u, v, c in get_edges(vis.dist, cand_source, terminals):
        edges[(u, v)] = c
    if debug:
        print('cand_source: {}'.format(cand_source))
        print('#terminals: {}'.format(len(terminals)))
        print('edges from cand_source: {}'.format(edges))

    if verbose:
        terminals_iter = tqdm(terminals)
        print('building closure graph')
    else:
        terminals_iter = terminals

    # from terminal to other terminals
    # every temrinal should connetct to at least one earlier terminal
    # in this way, connectivity is ensured
    for root in terminals_iter:
        if root == cand_source:
            continue            
        # connect from some earlier node to root

        # if it's earliest, can only connect to peers
        early_terminals = [t for t in terminals
                           if infection_times[t] < infection_times[root]]
        same_time_terminals = [t for t in terminals
                               if infection_times[t] == infection_times[root] if t != root]
        late_time_terminals = [t for t in terminals
                               if infection_times[t] > infection_times[root]]
        if debug:
            print('root: {}'.format(root))
            print('early_terminals: {}'.format(early_terminals))
            print('same_time_terminals: {}'.format(same_time_terminals))
            print('late_time_terminals: {}'.format(late_time_terminals))

        if infection_times[root] == infection_times[terminals].min():
            targets = early_terminals + same_time_terminals
        else:
            targets = early_terminals

        targets = list(set(targets) - {cand_source})  # no one can connect to cand_source

        if debug:
            print('targets: {}'.format(targets))
            
        vis = init_visitor(g, root)
        cpbfs_search(g, source=root, visitor=vis,
                     terminals=targets,
                     forbidden_nodes=late_time_terminals,
                     count_threshold=k)
        r2pred[root] = vis.pred
        for root, v, c in get_edges(vis.dist, root, early_terminals):
            if debug:
                print('edge ({}, {})'.format(v, root))
            edges[(v, root)] = c  # from earlier node to root

    if verbose:
        print('returning closure graph')

    gc = Graph(directed=True)

    for _ in range(g.num_vertices()):
        gc.add_vertex()

    for (u, v) in edges:
        gc.add_edge(u, v)

    eweight = gc.new_edge_property('int')
    eweight.set_2d_array(np.array(list(edges.values())))

    return gc, eweight, r2pred
        e13 = child_graph.add_edge(child_graph.vertex_index[1], child_graph.vertex_index[3])
        e24 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[4])
        e34 = child_graph.add_edge(child_graph.vertex_index[3], child_graph.vertex_index[4])
        e35 = child_graph.add_edge(child_graph.vertex_index[3], child_graph.vertex_index[5])
        e45 = child_graph.add_edge(child_graph.vertex_index[4], child_graph.vertex_index[5])

        e10 = child_graph.add_edge(child_graph.vertex_index[1], child_graph.vertex_index[0])
        e20 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[0])
        e21 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[1])
        e31 = child_graph.add_edge(child_graph.vertex_index[3], child_graph.vertex_index[1])
        e42 = child_graph.add_edge(child_graph.vertex_index[4], child_graph.vertex_index[2])
        e43 = child_graph.add_edge(child_graph.vertex_index[4], child_graph.vertex_index[3])
        e53 = child_graph.add_edge(child_graph.vertex_index[5], child_graph.vertex_index[3])
        e54 = child_graph.add_edge(child_graph.vertex_index[5], child_graph.vertex_index[4])
        '''
        e01 = child_graph.add_edge(child_graph.vertex_index[0], child_graph.vertex_index[1])
        e05 = child_graph.add_edge(child_graph.vertex_index[0], child_graph.vertex_index[5])
        e12 = child_graph.add_edge(child_graph.vertex_index[1], child_graph.vertex_index[2])
        e15 = child_graph.add_edge(child_graph.vertex_index[1], child_graph.vertex_index[5])
        e23 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[3])
        e24 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[4])
        e26 = child_graph.add_edge(child_graph.vertex_index[2], child_graph.vertex_index[6])
        e36 = child_graph.add_edge(child_graph.vertex_index[3], child_graph.vertex_index[6])
        e34 = child_graph.add_edge(child_graph.vertex_index[3], child_graph.vertex_index[4])
        e47 = child_graph.add_edge(child_graph.vertex_index[4], child_graph.vertex_index[7])
        e56 = child_graph.add_edge(child_graph.vertex_index[5], child_graph.vertex_index[6])
        e58 = child_graph.add_edge(child_graph.vertex_index[5], child_graph.vertex_index[8])
        e510 = child_graph.add_edge(child_graph.vertex_index[5], child_graph.vertex_index[10])
        e67 = child_graph.add_edge(child_graph.vertex_index[6], child_graph.vertex_index[7])
        e68 = child_graph.add_edge(child_graph.vertex_index[6], child_graph.vertex_index[8])
        e79 = child_graph.add_edge(child_graph.vertex_index[7], child_graph.vertex_index[9])