Exemple #1
0
    def draw(self, file_name, output_size=(1980, 1980)):
        # for straight edges, use only one line below instead of the next 5 lines before gt.graph_draw
        # tpos = pos = gt.radial_tree_layout(self.g, self.root)

        state = gt.minimize_nested_blockmodel_dl(self.g, deg_corr=True)
        t = gt.get_hierarchy_tree(state)[0]
        tpos = pos = gt.radial_tree_layout(self.g, self.root)
        cts = gt.get_hierarchy_control_points(self.g, t, tpos, beta=0.2)
        pos = self.g.own_property(tpos)

        gt.graph_draw(self.g, bg_color=[1,1,1,1], vertex_text=self.vprop_label, vertex_text_position=self.text_position, \
                      vertex_text_rotation=self.text_rotation, vertex_fill_color=self.fill_color, \
                      output=file_name, output_size=output_size, inline=True, vertex_font_size=self.font_size, \
                      edge_marker_size=self.marker_size, vertex_text_offset=self.text_offset, \
                      vertex_size=self.vertex_size, vertex_anchor = 0, pos=pos, edge_control_points=cts, fit_view=0.9)
Exemple #2
0
 def make_radial_graph(self, **args):
     if self.random_state is not None:
         np.random.seed(self.random_state)
     state = gt.minimize_nested_blockmodel_dl(self.g, deg_corr=True)
     t = gt.get_hierarchy_tree(state)[0]
     tpos = gt.radial_tree_layout(t,
                                  t.vertex(t.num_vertices() - 1),
                                  weighted=True)
     cts = self.g.new_edge_property("double")
     pos = self.g.new_edge_property("double")
     self.g.edge_properties['cts'] = gt.get_hierarchy_control_points(
         self.g, t, tpos)
     self.g.vertex_properties['pos'] = self.g.own_property(tpos)
     self.membership = list(state.get_bs()[0])
     if self.node_color.sum() == 0:
         self.node_color = self.convert_str_to_color(self.membership)
         self.add_color_nodes()
Exemple #3
0
def get_hierarchy_gt(graph_json, state):
    # Get the partition from blocks
    levels = state.get_levels()
    level_num = len(levels)
    level_block_count = []
    for level in range(0, level_num):
        level_block_count.append(levels[level].B)

    level = 0
    tree, label, order = gt.get_hierarchy_tree(state)
    for v_idx in tree.get_vertices():
        v = tree.vertex(v_idx)
        label_v = label[v]
        # A new level
        if v_idx != 0 and label_v == 0:
            level += 1

        assert v_idx == len(graph_json['nodes'])
        node = {
            'label': 'node_' + str(v_idx),
            'idx': int(v_idx),
            'height': level,
            'ancIdx': None,
            'childIdx': []
        }
        # Virtual Node
        if level == 0:
            node['virtualNode'] = False
        else:
            node['virtualNode'] = True
        # Hierarchy
        if level != 0:
            for vertex in v.out_neighbors():
                node['childIdx'].append(int(vertex))
                graph_json['nodes'][int(vertex)]['ancIdx'] = int(v_idx)

        graph_json['nodes'].append(node)

    graph_json['rootIdx'] = len(graph_json['nodes']) - 1
Exemple #4
0
def Stochastic():

    import pandas as pd
    import numpy as np
    import pprint as pp
    import locale
    import matplotlib.pyplot as plt
    import matplotlib.ticker as tkr
    import graph_tool.all as gt
    import math

    # Need to drag this out into the real world
    from GAC_Graph_Builder import findEdges

    t = gt.Graph(directed=True)

    tprop_label = t.new_vertex_property("string")
    tprop_instType = t.new_vertex_property("string")

    linkDict, instSet = findEdges()

    # ingest our university checking lists [this is sloppy, TBI]

    foreignUniTxt = open('Workaround txts/Foreign Unis.txt', 'r')
    UKUniTxt = open('Workaround txts/UK Unis.txt', 'r')

    forerignUniVals = foreignUniTxt.read().splitlines()
    UKUniVals = UKUniTxt.read().splitlines()

    # add vertices and label them based on their names.

    ######## FILTERING BASED ON CORDIS RESIDENCY ##########

    dfCordisNames = pd.read_pickle('Pickles/CORDIS_Countries.pickle')

    eligiblenames = dfCordisNames.name.values.tolist()

    veryDirtyWorkaround = ['FOCUS', 'FLUOR', 'GE', 'NI', 'OTE', 'ROKE']

    for inst in instSet:

        nameCheck = inst.upper()
        firstFound = next((x for x in eligiblenames if nameCheck in x), None)
        if inst in forerignUniVals:
            del (linkDict[inst])
        elif nameCheck in veryDirtyWorkaround:
            del (linkDict[inst])
        elif firstFound is None:
            del (linkDict[inst])
        else:
            vert = t.add_vertex()
            tprop_label[vert] = str(inst)

    del (linkDict[''])

    # internalise property map
    t.vertex_properties["label"] = tprop_label

    # explicitly declare the hierarchy defining vertices and edges, the sequencing here matters.
    for_uni = t.add_vertex()
    UK_uni = t.add_vertex()
    other = t.add_vertex()
    root = t.add_vertex()

    edgeList = [(root, for_uni), (root, UK_uni), (root, other)]
    t.add_edge_list(edgeList)

    # use label name to add edges to hierarchy
    for i in range(t.num_vertices())[:-4]:
        if tprop_label[i] in forerignUniVals:
            t.add_edge(for_uni, t.vertex(i))
            tprop_instType[i] = "Foreign Uni"
        elif tprop_label[i] in UKUniVals:
            t.add_edge(UK_uni, t.vertex(i))
            tprop_instType[i] = "UK Uni"
        else:
            t.add_edge(other, t.vertex(i))
            tprop_instType[i] = "Other Institution"

    t.vertex_properties["instType"] = tprop_instType
    tpos = gt.radial_tree_layout(t,
                                 t.vertex(t.num_vertices() - 1),
                                 rel_order_leaf=True)

    ######### MAIN GRAPH DRAWING ################

    g = gt.Graph(directed=False)
    # creates graph g, using the same nodes (with the same index!)

    for v in t.vertices():
        gv = g.add_vertex()

    # we remove: root, for_uni, uk_uni or 'other' vertices

    lower = g.num_vertices() - 5
    current = g.num_vertices() - 1

    while current > lower:
        g.remove_vertex(current)
        current -= 1

    # Pull vertex properties from t

    labelDict = t.vertex_properties["label"]
    instTypeDict = t.vertex_properties["instType"]

    # create properties for g vertices

    gprop_label = g.new_vertex_property("string")
    gprop_instType = g.new_vertex_property("string")

    # match labels between g and t

    for v in g.vertices():
        gprop_label[v] = labelDict[v]
        gprop_instType[v] = instTypeDict[v]

    # make property map internal to graph g
    g.vertex_properties["label"] = gprop_label
    g.vertex_properties["instType"] = gprop_instType

    ###### COLOUR VERTICES #########

    # Reclaim variable names because lazy

    gprop_vcolour = g.new_vertex_property("string")

    for v in g.vertices():

        if gprop_instType[v] == "Foreign Uni":
            gprop_vcolour[v] = "red"
        elif gprop_instType[v] == "UK Uni":
            gprop_vcolour[v] = "blue"
        else:
            gprop_vcolour[v] = "white"

    g.vertex_properties["vcolour"] = gprop_vcolour

    # create numLinks edge property for g edges

    eprop_numLinks = g.new_edge_property("int")

    # creates the edges between nodes

    for i in linkDict:
        for n in linkDict[i]:
            #print(i)
            vertex_i = gt.find_vertex(g, gprop_label, i)[0]
            #print(n)
            try:
                vertex_n = gt.find_vertex(g, gprop_label, n)[0]
                e = g.add_edge(vertex_i, vertex_n)
                eprop_numLinks[e] = linkDict[i][n]
            except:
                IndexError

    ##### EXPERIMENTAL SIZE THINGS ######

    #gvprop_size = g.new_vertex_property('float')

    deleteList = []

    for v in g.vertices():

        # sum the num edges and the number of links they correspond to
        # use this to find a ratio and scale size off of this.

        numEdges = sum(1 for _ in v.all_edges())
        numLinks = 0

        for e in v.all_edges():

            numLinks += eprop_numLinks[e]

        #print(gprop_label[v])
        print("NumEdges = " + str(numEdges) + " NumLinks = " + str(numLinks))
        # create a delete list

        try:
            ratio = (numLinks / numEdges) * 5 * 2
        except:
            ZeroDivisionError
            deleteList.append(v)

        #gvprop_size[v] = ratio

    #g.vertex_properties['size'] = gvprop_size

    #### Delete linkless vertices #######

    for v in reversed(sorted(deleteList)):
        g.remove_vertex(v)

    for v in reversed(sorted(deleteList)):
        t.remove_vertex(v)

    tpos = gt.radial_tree_layout(t,
                                 t.vertex(t.num_vertices() - 1),
                                 rel_order_leaf=True)

    #######

    ############ stochastic BLOCK MODEL ####################

    state = gt.minimize_nested_blockmodel_dl(g, deg_corr=True, verbose=True)
    t = gt.get_hierarchy_tree(state)[0]
    tpos = pos = gt.radial_tree_layout(t,
                                       t.vertex(t.num_vertices() - 1),
                                       weighted=True)

    # in order to make sure labels fit in the image we have to manually adjust the
    # co-ordinates of each vertex.

    x, y = gt.ungroup_vector_property(tpos, [0, 1])
    x.a = (x.a - x.a.min()) / (x.a.max() - x.a.min()) * 1400 + 400
    y.a = (y.a - y.a.min()) / (y.a.max() - y.a.min()) * 1400 + 400
    tpos = gt.group_vector_property([x, y])

    # This draws the 'Bezier spline control points' for edges
    # it draws the edges directed in graph g, but uses the hierarchy / positioning of graph t.
    cts = gt.get_hierarchy_control_points(g, t, tpos)

    pos = g.own_property(tpos)

    gt.graph_draw(
        g,
        vertex_text_position="centered",
        vertex_text=g.vertex_properties["label"],
        vertex_font_size=14,
        vertex_anchor=0,
        vertex_aspect=1,
        vertex_shape="square",
        vertex_fill_color=g.vertex_properties["vcolour"],
        vertex_size=10,
        fit_view=False,
        # edge_color=g.edge_properties["colour"],
        # edge_pen_width=g.edge_properties["thickness"],
        edge_end_marker="none",
        edge_pen_width=0.2,
        edge_color="white",
        bg_color=[0, 0, 0, 1],
        output_size=[2000, 2000],
        output='UK_ONLY_RELATIONSHIPS_stochastic.png',
        pos=pos,
        edge_control_points=cts)

    if __name__ == '__main__':
        pyjd.setup("Hello.html")
Exemple #5
0
def draw_graph(
    adata: AnnData,
    layout: _Layout = 'sfdp',
    #    init_pos: Union[str, bool, None] = None,
    #    root: Optional[int] = None,
    use_tree: bool = False,
    random_seed: Optional[int] = None,
    adjacency: Optional[spmatrix] = None,
    key_added_ext: Optional[str] = None,
    key: Optional[str] = 'schist',
    copy: bool = False,
    **kwds,
):
    """\
    Extends scanpy.tools.draw_graph function using some layouts available in 
    graph-tool library. Three layouts are available here:
    
    - SFDP spring-block layout.
    - ARF spring-block layout.
    - Fruchterman-Reingold spring-block layout.
    
    Fruchterman-Reingold is already available in scanpy, but here can be used
    to render the nested model tree. 
    
    In order to use these plotting function, the NestedBlockState needs to be
    saved when building the model, so `save_state=True` needs to be set.
    
    Parameters
    ----------
    adata
        Annotated data matrix. A NestedBlockState object needs to be saved
    layout
        A layout among 'sfdp', 'fr' or 'arf'. Other graph-tool layouts haven't been
        implemented.
    use_tree
        When this is set, the tree of the nested model is used to generate layout, 
        otherwise the layout only accounts for the neighborhood graph.    
    random_seed
        Random number to be used as seed for graph-tool
    adjacency
        Sparse adjacency matrix of the graph, defaults to
        `adata.uns['neighbors']['connectivities']`.
    key_added_ext
        By default, append `layout`.
    key
        The slot in `AnnData.uns` containing the state. Default is 'nsbm'
    copy
        Return a copy instead of writing to adata.
    **kwds
        Parameters of chosen igraph layout. See e.g. `fruchterman-reingold`_
        [Fruchterman91]_. One of the most important ones is `maxiter`.

        .. _fruchterman-reingold: http://igraph.org/python/doc/igraph.Graph-class.html#layout_fruchterman_reingold

    Returns
    -------
    Depending on `copy`, returns or updates `adata` with the following field.

    **X_draw_graph_layout** : `adata.obsm`
        Coordinates of graph layout. E.g. for layout='fa' (the default),
        the field is called 'X_draw_graph_fa'
    """
    if random_seed:
        np.random.seed(random_seed)
        gt.seed_rng(random_seed)

    n_cells = adata.shape[0]
    start = logg.info(f'drawing single-cell graph using layout {layout!r}')
    if layout not in _LAYOUTS:
        raise ValueError(f'Provide a valid layout, one of {_LAYOUTS}.')
    adata = adata.copy() if copy else adata
    if adjacency is None and 'neighbors' not in adata.uns:
        raise ValueError('You need to run `pp.neighbors` first '
                         'to compute a neighborhood graph.')
    if not key in adata.uns:
        raise ValueError(
            'You need to run `nested_model` before trying to run this function '
        )

    if use_tree and 'state' not in adata.uns[key]:
        raise ValueError(
            'When `use_tree` is set to `True`, a state should be saved'
            'running  `nested_model(adata, save_state=True)`.')
    if adjacency is None:
        adjacency = adata.uns['neighbors']['connectivities']

    g = get_graph_tool_from_adjacency(adjacency)
    weights = g.ep['weight']
    if use_tree:
        state = state_from_blocks(adata)
        g, _, _ = gt.get_hierarchy_tree(state, empty_branches=False)
        weights = None

    # actual drawing
    positions = np.zeros((n_cells, 2))
    if layout == 'fr':
        positions = gt.fruchterman_reingold_layout(g, weight=weights)
        positions = np.array([x for x in positions][:n_cells])
    elif layout == 'sfdp':
        positions = gt.sfdp_layout(g)
        positions = np.array([x for x in positions][:n_cells])
    elif layout == 'arf':
        positions = gt.arf_layout(g)
        positions = np.array([x for x in positions][:n_cells])

    adata.uns['draw_graph'] = {}
    adata.uns['draw_graph']['params'] = dict(layout=layout,
                                             random_seed=random_seed)
    key_added = f'X_draw_graph_{layout}'
    adata.obsm[key_added] = positions
    logg.info(
        '    finished',
        time=start,
        deep=('added\n'
              f'    {key_added!r}, graph_drawing coordinates (adata.obsm)'),
    )
    return adata if copy else None
    if plot_color[e.source()] != plot_color[e.target()]:
        if plot_color[e.source()] == (0, 0, 1, 1):
            #orange on dem -> rep
            edge_color[e] = (255.0 / 255.0, 102 / 255.0, 0 / 255.0, alpha)
        else:
            edge_color[e] = (102.0 / 255.0, 51 / 255.0, 153 / 255.0, alpha)
    #red on rep-rep edges
    elif plot_color[e.source()] == (1, 0, 0, 1):
        edge_color[e] = (1, 0, 0, alpha)
    #blue on dem-dem edges
    else:
        edge_color[e] = (0, 0, 1, alpha)

state = gt.minimize_nested_blockmodel_dl(g, deg_corr=True)
bstack = state.get_bstack()
t = gt.get_hierarchy_tree(bstack)[0]
tpos = pos = gt.radial_tree_layout(t,
                                   t.vertex(t.num_vertices() - 1),
                                   weighted=True)
cts = gt.get_hierarchy_control_points(g, t, tpos)
pos = g.own_property(tpos)
b = bstack[0].vp["b"]

#labels
text_rot = g.new_vertex_property('double')
g.vertex_properties['text_rot'] = text_rot
for v in g.vertices():
    if pos[v][0] > 0:
        text_rot[v] = math.atan(pos[v][1] / pos[v][0])
    else:
        text_rot[v] = math.pi + math.atan(pos[v][1] / pos[v][0])
Exemple #7
0
def codon_circos(cmap='tab20', filetype="pdf", reverse=False):
    cm = plt.cm.get_cmap(cmap)
    cmappable = ScalarMappable(norm=Normalize(vmin=0, vmax=20), cmap=cm)

    g_codons = gt.Graph(directed=False)
    g_codons.vp.codon = g_codons.new_vertex_property("string")
    g_codons.vp.aa = g_codons.new_vertex_property("string")
    g_codons.vp.aa_index = g_codons.new_vertex_property("int")
    g_codons.vp.aa_color = g_codons.new_vertex_property("vector<float>")
    g_codons.vp.codon_index = g_codons.new_vertex_property("int")
    g_codons.ep.syn = g_codons.new_edge_property("bool")
    g_codons.ep.grad = g_codons.new_edge_property("vector<float>")

    for aa_index, aa in enumerate(aa_order):
        if aa == "X": continue
        for codon_index, c in enumerate(sorted([k for k, v in codontable.items() if v == aa])):
            v = g_codons.add_vertex()
            g_codons.vp.codon[v] = c
            g_codons.vp.aa[v] = aa
            g_codons.vp.codon_index[v] = codon_index
            g_codons.vp.aa_index[v] = aa_index
            g_codons.vp.aa_color[v] = cmappable.to_rgba(aa_index)

    for ref in g_codons.vertices():
        for alt in g_codons.vertices():
            if alt <= ref: continue
            codon_ref, codon_alt = g_codons.vp.codon[ref], g_codons.vp.codon[alt]
            if distance_str(codon_ref, codon_alt) != 1: continue
            if codontable[codon_ref] != codontable[codon_alt]:
                e_c = g_codons.add_edge(ref, alt)
                g_codons.ep.syn[e_c] = False
                x = cmappable.to_rgba(g_codons.vp.aa_index[ref])[:3]
                y = cmappable.to_rgba(g_codons.vp.aa_index[alt])[:3]
                if reverse: x, y = y, x
                g_codons.ep.grad[e_c] = [0.0, *x, 0.75, 1.0, *y, 0.75]

    for ref in g_codons.vertices():
        for alt in g_codons.vertices():
            if alt >= ref: continue
            codon_ref, codon_alt = g_codons.vp.codon[ref], g_codons.vp.codon[alt]
            if distance_str(codon_ref, codon_alt) != 1: continue
            if codontable[codon_ref] == codontable[codon_alt]:
                e_c = g_codons.add_edge(ref, alt)
                g_codons.ep.syn[e_c] = True
                syn_color = 0.0, 0.0, 0.0, 1.0
                g_codons.ep.grad[e_c] = [0.0, *syn_color, 1.0, *syn_color]

    assert g_codons.num_vertices() == 61
    dist = gt.shortest_distance(g_codons)
    r = max([max(dist[g_codons.vertex(i)].a) for i in g_codons.vertices()])
    print('Codons graph radius : {0}'.format(r))
    print('Codons : {0} transitions out of {1} possibles.'.format(g_codons.num_edges(), 61 * 60 / 2))
    syn_array = g_codons.ep.syn.get_array()
    print('Codons : {0} are synonymous and {1} are non-synonymous.'.format(sum(syn_array),
                                                                           len(syn_array) - sum(syn_array)))
    state = gt.NestedBlockState(g_codons, bs=[g_codons.vp.aa_index, g_codons.vp.codon_index], sampling=False)
    t = gt.get_hierarchy_tree(state)[0]
    tpos = gt.radial_tree_layout(t, t.vertex(t.num_vertices() - 1), weighted=True)
    cts = gt.get_hierarchy_control_points(g_codons, t, tpos)
    pos = g_codons.own_property(tpos)
    gt.graph_draw(g_codons, pos=pos, edge_control_points=cts, edge_gradient=g_codons.ep.grad, edge_pen_width=2.5,
                  vertex_text=g_codons.vp.codon, vertex_anchor=0, vertex_font_size=9, vertex_pen_width=1.6,
                  vertex_color=(0.65, 0.65, 0.65, 1), vertex_fill_color=g_codons.vp.aa_color, vertex_size=25.0,
                  output="gt-codon-{0}.{1}".format(cmap, filetype))
Exemple #8
0
def amino_acid_circos(cmap='tab20', filetype="pdf", reverse=False):
    cm = plt.cm.get_cmap(cmap)
    cmappable = ScalarMappable(norm=Normalize(vmin=0, vmax=20), cmap=cm)

    g_aa = gt.Graph(directed=False)
    g_aa.vp.aa = g_aa.new_vertex_property("string")
    g_aa.vp.aa_color = g_aa.new_vertex_property("vector<float>")
    g_aa.vp.count = g_aa.new_vertex_property("float")
    g_aa.ep.count = g_aa.new_edge_property("float")
    g_aa.ep.grad = g_aa.new_edge_property("vector<float>")

    for aa_index, aa in enumerate(aa_order):
        if aa == "X": continue
        v = g_aa.add_vertex()
        g_aa.vp.aa[v] = aa
        g_aa.vp.aa_color[v] = cmappable.to_rgba(aa_index)
        g_aa.vp.count[v] = np.sqrt(len([k for k, v in codontable.items() if v == aa])) * 28

    adj = np.zeros((g_aa.num_vertices(), g_aa.num_vertices()))
    for ref_index, ref in enumerate(g_aa.vertices()):
        for alt_index, alt in enumerate(g_aa.vertices()):
            if alt <= ref: continue
            aa_ref, aa_alt = g_aa.vp.aa[ref], g_aa.vp.aa[alt]
            c_ref = [k for k, v in codontable.items() if v == aa_ref]
            c_alt = [k for k, v in codontable.items() if v == aa_alt]
            nei = [(r, a) for r, a in product(c_ref, c_alt) if distance_str(r, a) == 1]
            if len(nei) > 0:
                e_aa = g_aa.add_edge(ref, alt)
                x = cmappable.to_rgba(ref_index)[:3]
                y = cmappable.to_rgba(alt_index)[:3]
                if reverse: x, y = y, x
                g_aa.ep.grad[e_aa] = [0.0, *x, 0.75, 1.0, *y, 0.75]
                g_aa.ep.count[e_aa] = len(nei) * 2.0
                adj[ref_index, alt_index] = len(nei)

    table = open("aa-adjacency.tex", "w")
    table.writelines("\\begin{table}[H]\n\\centering\n")
    table.writelines("\\begin{tabular}{|c||" + "c|" * g_aa.num_vertices() + "}\n")
    table.writelines("\\hline & ")
    table.writelines(" & ".join(map(lambda x: "\\textbf{" + x + "}", g_aa.vp.aa)) + "\\\\\n")
    table.writelines("\\hline\n\\hline ")
    for i in range(adj.shape[0]):
        elts = ["\\textbf{" + g_aa.vp.aa[i] + "}"]
        for j in range(adj.shape[1]):
            if i < j:
                elts.append("{:d}".format(int(adj[i][j])))
            else:
                elts.append("-")
        table.writelines(" & ".join(elts) + "\\\\\n\\hline ")
    table.writelines("\\end{tabular}\n")
    table.writelines("\\caption[]{}\n")
    table.writelines("\\end{table}\n")
    table.close()

    assert g_aa.num_vertices() == 20
    dist = gt.shortest_distance(g_aa)
    r = max([max(dist[g_aa.vertex(i)].a) for i in g_aa.vertices()])
    print('Amino acids graph radius : {0}'.format(r))
    dict_distance = {1: [], 2: [], 3: []}
    for source in g_aa.vertices():
        for target in g_aa.vertices():
            if source <= target: continue
            dict_distance[int(gt.shortest_distance(g_aa, source, target))].append(
                "{0}-{1}".format(g_aa.vp.aa[source], g_aa.vp.aa[target]))

    for k, v in dict_distance.items():
        print("d={0}: {1} pairs".format(k, len(v)))
        print(", ".join(v))

    print('Amino acids : {0} transitions out of {1} possibles '.format(g_aa.num_edges(), 20 * 19 / 2))
    state = gt.minimize_nested_blockmodel_dl(g_aa, deg_corr=True)
    t = gt.get_hierarchy_tree(state)[0]
    tpos = gt.radial_tree_layout(t, t.vertex(t.num_vertices() - 1), weighted=True)
    cts = gt.get_hierarchy_control_points(g_aa, t, tpos)
    pos = g_aa.own_property(tpos)
    gt.graph_draw(g_aa, pos=pos, edge_control_points=cts, vertex_anchor=0, vertex_text=g_aa.vp.aa,
                  vertex_fill_color=g_aa.vp.aa_color, vertex_size=g_aa.vp.count, vertex_font_size=16,
                  vertex_pen_width=3.2, vertex_color=(0.65, 0.65, 0.65, 1),
                  edge_gradient=g_aa.ep.grad, edge_pen_width=g_aa.ep.count,
                  output="gt-aa-{0}.{1}".format(cmap, filetype))
Exemple #9
0
def getTreeTop(state,centrality=gt.pagerank,directed=True,fulltree=False):
    g = state.g
    visited = []

    g.set_directed(directed)
    label = g.vertex_properties['label']
    nodeid = g.vertex_properties['nodeid']

    tree, inlevelblocklabels, order = gt.get_hierarchy_tree(state)

    if fulltree:
        treetop = tree
        treetop_label = treetop.new_vertex_property('string')
        for v in treetop.vertices():
            if treetop.vertex_index[v] < g.num_vertices():
                treetop_label[v] = label[v]
    else:
        treetop = gt.GraphView(tree,vfilt=lambda v: tree.vertex_index[v] >= g.num_vertices())
        treetop_label = treetop.new_vertex_property('string')

    dangling = treetop.new_vertex_property('bool')
    treecentrality = treetop.new_vertex_property('double')
    
    numlevels = len(state.levels)
    rootid = tree.num_vertices() - 1
    j = rootid
    count = 0
    for i in range(numlevels-1,-1,-1):
        s = state.project_level(i)
        blocks = set(s.get_blocks().get_array())
        propmap = s.get_blocks()
        for b in range(len(blocks)-1,-1,-1):
            assert inlevelblocklabels[tree.vertex(j)] == b, "b %s and inlevelblocklabels %s do not match up" % (b, inlevelblocklabels[tree.vertex(j)])
            if tree.vertex(j).out_degree() == 1:
                treetop_label[treetop.vertex(j)] = '__PRUNEME__'
                dangling[treetop.vertex(j)] = False
                print i,b,gv.num_vertices(),'__PRUNEME__'
            else:
                gv = gt.GraphView(g, vfilt= lambda v: (propmap[v] == b) and (label[v] not in visited))
                if gv.num_vertices() > 0:
                    try:
                        centrality_prop = centrality(gv)
                        name, mynodeid, v, maxcentrality = getMaxCentrality(gv,centrality_prop,label,nodeid)
                        visited.append(name)
                        treetop_label[treetop.vertex(j)] = name
                        dangling[treetop.vertex(j)] = False
                        treecentrality[treetop.vertex(j)] = maxcentrality
                        print i,b,gv.num_vertices(),name,mynodeid
                    except:
                        pass
                else:
                    treetop_label[treetop.vertex(j)] = '__DANGLING__'
                    dangling[treetop.vertex(j)] = True
                    print i,b,'DANGLING'
                    count = count + 1
            j=j-1
            sys.stdout.flush()
    treetop.vertex_properties['label'] = treetop_label
    treetop.vertex_properties['dangling'] = dangling
    treetop.vertex_properties['centrality'] = treecentrality
    print count,"dangling vertices\n"
    sys.stdout.flush()
    return treetop,rootid
Exemple #10
0
import argparse
parser = argparse.ArgumentParser(description='Select purge threshold.')
parser.add_argument('threshold', metavar='N', type=str, nargs='+',
                   help='option number')

args = parser.parse_args()
myfile = args.threshold[0]

# nested block model
#state = utils.pickleLoad('graph_pagerank_purge_%s_nested.pk1' % threshold)
state = utils.pickleLoad(myfile)

# underlying graph
g = state.g

t = gt.get_hierarchy_tree(state)[0]


tpos = pos = gt.radial_tree_layout(t, t.vertex(t.num_vertices() - 1), weighted=True)

pos = g.own_property(tpos)
text_rot = g.new_vertex_property('double')
g.vertex_properties['text_rot'] = text_rot
cts = gt.get_hierarchy_control_points(g, t, tpos)



for v in g.vertices():
    if pos[v][0] > 0:
        text_rot[v] = math.atan(pos[v][1]/pos[v][0])
    else:
Exemple #11
0
        plot_color[v] = g_green  #red_blue_map[g.vertex_properties['value'][v]]
    else:
        plot_color[v] = (0, 0, 0, 1)
#edge colors
alpha = 0.15
edge_color = g.new_edge_property('vector<double>')
g.edge_properties['edge_color'] = edge_color
for e in g.edges():
    if g.vertex_properties['vertex_name'][e.source()] == "sci-biology/samri":
        edge_color[e] = g_green
    else:
        edge_color[e] = g_purple

state = gt.minimize_nested_blockmodel_dl(g, deg_corr=True)
bstack = state.get_bstack()
t = gt.get_hierarchy_tree(state)[0]
tpos = pos = gt.radial_tree_layout(t,
                                   t.vertex(t.num_vertices() - 1),
                                   weighted=True)
cts = gt.get_hierarchy_control_points(g, t, tpos)
pos = g.own_property(tpos)
b = bstack[0].vp["b"]

#labels
text_rot = g.new_vertex_property('double')
g.vertex_properties['text_rot'] = text_rot
for v in g.vertices():
    if pos[v][0] > 0:
        text_rot[v] = math.atan(pos[v][1] / pos[v][0])
    else:
        text_rot[v] = math.pi + math.atan(pos[v][1] / pos[v][0])
Exemple #12
0
def circular_depgraph(g,
	plot_type="graph",
	save_as="~/depgraph.png",
	):

	save_as = os.path.abspath(os.path.expanduser(save_as))

	state = gt.minimize_nested_blockmodel_dl(g, deg_corr=True)
	t = gt.get_hierarchy_tree(state)[0]
	tpos = pos = gt.radial_tree_layout(t, t.vertex(t.num_vertices() - 1), weighted=True)
	cts = gt.get_hierarchy_control_points(g, t, tpos)
	pos = g.own_property(tpos)

	vtext_rotation = g.new_vertex_property('double')
	g.vertex_properties['vtext_rotation'] = vtext_rotation

	for v in g.vertices():
		#set vtext_rotation
		if pos[v][0] >= 0:
			try:
				vtext_rotation[v] = math.atan(pos[v][1]/pos[v][0])
			except ZeroDivisionError:
				vtext_rotation[v] = 0
		else:
			vtext_rotation[v] = math.pi + math.atan(pos[v][1]/pos[v][0])

	#here we do black magic to get proper output size (controls vertex spacing) and scaling
	vertex_number = g.num_vertices()
	view_zoom = (vertex_number*36.0485)**(-10.068/vertex_number)+0.017037
	output_size = int(vertex_number*5.9+400)
	dpi=300
	if output_size >= 18000:
		print("WARNING: You are exceding the maximal printable size - 150cm in one dimension at 300dpi")
	print("Plotting dependency graph containing {0} packages, at a resolution of {1} pixels by {1} pixels".format(vertex_number, output_size))

	if plot_type == "graph":
		gt.graph_draw(g, pos=pos,
				edge_control_points=cts,
				vertex_anchor=0,
				vertex_color=g.vertex_properties['vcolor'],
				vertex_fill_color=g.vertex_properties['vcolor'],
				vertex_font_size=14,
				vertex_text=g.vertex_properties['vlabel'],
				vertex_text_position=6.2,
				vertex_text_rotation=g.vertex_properties['vtext_rotation'],
				vertex_text_color=g.vertex_properties['vtext_color'],
				vertex_size=16,
				edge_start_marker="none",
				edge_mid_marker="none",
				edge_end_marker="none",
				edge_gradient=g.edge_properties["egradient"],
				eorder=g.edge_properties["eorder"],
				bg_color=[1,1,1,1],
				output_size=[output_size,output_size],
				output=save_as,
				fit_view=view_zoom,
				)
	elif plot_type == "state":
		gt.draw_hierarchy(state,
			vertex_text_position=1,
			vertex_font_size=12,
			vertex_text=g.vertex_properties['label'],
			vertex_text_rotation=g.vertex_properties['text_rotation'],
			vertex_anchor=0,
			bg_color=[1,1,1,1],
			output_size=[output_size,output_size],
			output=save_as,
			)
    if plot_color[e.source()] != plot_color[e.target()]:
        if plot_color[e.source()] == (0,0,1,1):
            #orange on dem -> rep
            edge_color[e] = (255.0/255.0, 102/255.0, 0/255.0, alpha)
        else:
            edge_color[e] = (102.0/255.0, 51/255.0, 153/255.0, alpha)            
    #red on rep-rep edges
    elif plot_color[e.source()] == (1,0,0,1):
        edge_color[e] = (1,0,0, alpha)
    #blue on dem-dem edges
    else:
        edge_color[e] = (0,0,1, alpha)

state = gt.minimize_nested_blockmodel_dl(g, deg_corr=True)
bstack = state.get_bstack()
t = gt.get_hierarchy_tree(bstack)[0]
tpos = pos = gt.radial_tree_layout(t, t.vertex(t.num_vertices() - 1), weighted=True)
cts = gt.get_hierarchy_control_points(g, t, tpos)
pos = g.own_property(tpos)
b = bstack[0].vp["b"]

#labels
text_rot = g.new_vertex_property('double')
g.vertex_properties['text_rot'] = text_rot
for v in g.vertices():
    if pos[v][0] >0:
        text_rot[v] = math.atan(pos[v][1]/pos[v][0])
    else:
        text_rot[v] = math.pi + math.atan(pos[v][1]/pos[v][0])

gt.graph_draw(g, pos=pos, vertex_fill_color=g.vertex_properties['plot_color'],