def save(self, file_name, fmt="auto"):
        """ overload Graph.save to make output dotfiles pretty.
            This is entirely cosmetic. """
        u = self

        # add some properties to prettify dot output
        if fmt is "dot" or fmt is "auto" and file_name.endswith(".dot"):
            u = GraphView(self)

            # add shape property according to vertex owners
            shape = u.new_vertex_property("string")
            for v in u.vertices():
                if u.vp.owner[v] == 1:
                    shape[v] = "box"
                else:
                    shape[v] = "diamond"
            u.vp.shape = shape

            # add label property according to priorities
            #u.vertex_properties['label'] = u.vertex_properties['priority']
            label = u.new_vertex_property("string")
            for v in u.vertices():
                prio = u.vertex_properties['priority'][v]
                name = u.vertex_index[v]
                label[v] = "%d (%d)" % (name, prio)
            u.vp.label = label

        Graph.save(u, file_name, fmt)
def test_fill_missing_time():
    """simple chain graph test
    """
    g = Graph(directed=False)
    g.add_vertex(4)
    g.add_edge_list([(0, 1), (1, 2), (2, 3)])

    t = GraphView(g, directed=True)
    efilt = t.new_edge_property('bool')
    efilt.a = True
    efilt[t.edge(2, 3)] = False
    t.set_edge_filter(efilt)
    vfilt = t.new_vertex_property('bool')
    vfilt.a = True
    vfilt[3] = False
    t.set_vertex_filter(vfilt)

    root = 0
    obs_nodes = {0, 2}
    infection_times = [0, 1.5, 3, -1]

    pt = fill_missing_time(g, t, root, obs_nodes, infection_times, debug=False)

    for i in range(4):
        assert pt[i] == infection_times[i]
def is_order_respected(tree, root, obs_nodes, infection_times):
    tree = GraphView(tree)
    obs_set = set(obs_nodes)
    vfilt = tree.new_vertex_property('bool')
    vfilt.a = True
    tree.set_vertex_filter(vfilt)

    leaves = [o for o in obs_nodes if tree.vertex(o).out_degree() == 0]
    vis = init_visitor(tree, root)
    pbfs_search(tree, root, terminals=leaves, visitor=vis, count_threshold=-1)
    for l in leaves:
        edges = extract_edges_from_pred(tree, root, l, vis.pred)
        edges = edges[::-1]
        path = list(edges[0]) + [u for _, u in edges[1:]]
        useful_nodes_on_path = [v for v in path if v in obs_set]

        for i in range(len(useful_nodes_on_path)-1):
            u, v = useful_nodes_on_path[i: i+2]
            if infection_times[u] > infection_times[v]:
                return False
    return True