cairo_draw.py 95.9 KB
Newer Older
2001
        t_orig.copy_property(pos, tpos, g=u)
2002
2003
2004
    else:
        pos = (g.own_property(pos[0]),
               g.own_property(pos[1]))
2005
2006
        t_orig.copy_property(pos[0], tpos, g=u)
    return pos, t_orig, tpos
2007

2008

2009
def get_bip_hierachy_pos(state, aspect=1., node_weight=None):
2010

2011
    if state.levels[0].overlap:
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
        g = state.g
        ostate = state.levels[0]
        bv, bcin, bcout, bc = ostate.get_overlap_blocks()
        be = ostate.get_edge_blocks()

        n_r = zeros(ostate.B)
        b = g.new_vertex_property("int")
        for v in g.vertices():
            i = bc[v].a.argmax()
            b[v] = bv[v][i]
            n_r[b[v]] += 1

        orphans = [r for r in range(ostate.B) if n_r[r] == 0]

        for v in g.vertices():
            for r in orphans:
                b[v] = r

        orig_state = state
        state = state.copy()
        state.levels[0] = BlockState(g, b=b)

    g = state.g

    deg = g.degree_property_map("total")

    t, tb, order = get_hierarchy_tree(state)

2040
    root = t.vertex(t.num_vertices(True) - 1)
2041
2042
    if root.out_degree() > 2:
        clabel = is_bipartite(g, partition=True)[1].copy("int")
2043
        if state.levels[0].overlap:
2044
2045
            ostate = OverlapBlockState(g, b=clabel)
            ostate = orig_state.copy(clabel=clabel)
2046
            bc = ostate.propagate_clabel(len(state.levels) - 2)
2047
2048
        else:
            state = state.copy(clabel=clabel)
2049
            bc = state.propagate_clabel(len(state.levels) - 2)
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069

        ps = list(root.out_neighbours())
        t.clear_vertex(root)

        p1 = t.add_vertex()
        p2 = t.add_vertex()

        t.add_edge(root, p1)
        t.add_edge(root, p2)
        for p in ps:
            if bc.a[tb[p]] == 0:
                t.add_edge(p2, p)
            else:
                t.add_edge(p1, p)

    w = t.new_vertex_property("double")
    for v in t.vertices():
        if v.in_degree() == 0:
            break
        if v.out_degree() == 0:
2070
            w[v] = 1 if node_weight is None else node_weight[v]
2071
2072
2073
2074
2075
2076
2077
2078
2079
        parent, = v.in_neighbours()
        w[parent] += w[v]

    pos = t.new_vertex_property("vector<double>")

    pos[root] = (0., 0.)

    p1, p2 = root.out_neighbours()

2080
2081
    if ((w[p1] == w[p2] and p1.out_degree() > p2.out_degree()) or
        w[p1] > w[p2]):
2082
2083
2084
        p1, p2 = p2, p1

    L = len(state.levels)
2085
2086
    pos[p1] = (-1 / L * .5 * aspect, 0)
    pos[p2] = (+1 / L * .5 * aspect, 0)
2087
2088
2089
2090
2091
2092

    for i, p in enumerate([p1, p2]):
        roots = [p]
        while len(roots) > 0:
            nroots = []
            for r in roots:
2093
                cw = pos[r][1] - w[r] / (2. * w[p])
2094
2095
2096
                for v in sorted(r.out_neighbours(), key=lambda a: order[a]):
                    pos[v] = (0, 0)
                    if i == 0:
2097
                        pos[v][0] = pos[r][0] - 1 / L * .5 * aspect
2098
                    else:
2099
                        pos[v][0] = pos[r][0] + 1 / L * .5 * aspect
2100
                    pos[v][1] = cw + w[v] / (2. * w[p])
2101
2102
2103
2104
2105
2106
                    cw += w[v] / w[p]
                    nroots.append(v)
            roots = nroots
    return pos


2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
# Handle cairo contexts from cairocffi

try:
    import cairocffi
    import ctypes
    pycairo = ctypes.PyDLL(cairo._cairo.__file__)
    pycairo.PycairoContext_FromContext.restype = ctypes.c_void_p
    pycairo.PycairoContext_FromContext.argtypes = 3 * [ctypes.c_void_p]
    ctypes.pythonapi.PyList_Append.argtypes = 2 * [ctypes.c_void_p]
except ImportError:
    pass

def _UNSAFE_cairocffi_context_to_pycairo(cairocffi_context):
    # Sanity check. Continuing with another type would probably segfault.
    if not isinstance(cairocffi_context, cairocffi.Context):
        raise TypeError('Expected a cairocffi.Context, got %r'
                        % cairocffi_context)

    # Create a reference for PycairoContext_FromContext to take ownership of.
    cairocffi.cairo.cairo_reference(cairocffi_context._pointer)
    # Casting the pointer to uintptr_t (the integer type as wide as a pointer)
    # gets the context’s integer address.
    # On CPython id(cairo.Context) gives the address to the Context type,
    # as expected by PycairoContext_FromContext.
    address = pycairo.PycairoContext_FromContext(
        int(cairocffi.ffi.cast('uintptr_t', cairocffi_context._pointer)),
        id(cairo.Context),
        None)
    assert address
    # This trick uses Python’s C API
    # to get a reference to a Python object from its address.
    temp_list = []
    assert ctypes.pythonapi.PyList_Append(id(temp_list), address) == 0
    return temp_list[0]
2141

2142
# Bottom imports to avoid circular dependency issues
2143
2144
from .. inference import get_hierarchy_tree, NestedBlockState, BlockState, \
    OverlapBlockState