Search code examples
pythonmatplotlibnetworkxgraph-tool

Align the node labels radially in draw_hierarchy


How is it possible to align the labels of nodes to oriente radially toward center using "draw_hierarchy"? The text file which contains my dataset about my network, in each row has the labels of two connected nodes (data).

This is my code:

for i in range(len(events["e"])):
    g = Graph(directed=False)
    vprop = g.new_vertex_property("string") 
    eprop = g.new_edge_property("int") 
    added_nodes=[]
    n=0
    for ii, row in data.iterrows():
        #if the node does not already exist in the node list labels
        if data['i'][ii] not in added_nodes:
           added_nodes.append(data['i'][ii])
           exec("vertex_%d=g.add_vertex()" %n)
           exec("vprop[vertex_%d] =%d"%(n  , data['i'][ii]))
           vertix_out=n
           n+=1
        else:
           indx= added_nodes.index(data['i'][ii])
           vertix_out=indx
   
        if data['j'][ii] not in added_nodes: 
           added_nodes.append(data['j'][ii]) 
           exec("vertex_%d=g.add_vertex()" %n)
           exec("vprop[vertex_%d] =%d"%(n  , data['j'][ii]))
           vertix_in=n
           n+=1
        else:
           indx= added_nodes.index(data['j'][ii]) 
           vertix_in=indx
        exec("e=g.add_edge(vertex_%d,vertex_%d)"%(vertix_out,vertix_in))
        eprop[e] =data['t_sp'][ii]
    g.vertex_properties["name"]=vprop 
    g.edge_properties["Timestamp"]=eprop
    state = minimize_nested_blockmodel_dl(g, deg_corr=True,  mcmc_args={'parallel':True},mcmc_equilibrate_args={'verbose':False, 'epsilon':1e-4}, verbose=True)
    L = len(state.levels)
    # ## block membership of each node on each level
    eventName = events["e"][i]
    for l in range(L):
         with open(os.path.join('/home/','nodes_blocks_level_%s_%s'%(l,eventName)),'w') as f:
              state_proj_l = state.project_level(l)
              blocks_proj_l = state_proj_l.get_blocks()
              for i_b,b in enumerate(blocks_proj_l.a):
                  f.write('%s \t %s \n'%(i_b,b))     
    print state.entropy()    
            
    t = get_hierarchy_tree(state)[0]
    tpos = pos = radial_tree_layout(t, t.vertex(t.num_vertices() - 1), weighted=True)
    cts = get_hierarchy_control_points(g, t, tpos,beta=.87)
    pos = g.own_property(tpos)
    #text rotation
    vtext_rotation = g.new_vertex_property('double')
    for v in g.vertices():
        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])
    g.vertex_properties['text_rotation'] = vtext_rotation
    """
    graph_draw(g, pos=pos, 
               edge_control_points=cts,
               vertex_size=20,
               vertex_text=g.vertex_properties["name"], 
               vertex_text_position=1, 
               vertex_text_rotation=g.vertex_properties['text_rotation'], 
               vertex_font_family='mono',
               vertex_font_size=5,
               vertex_anchor=0, 
               output_size=[1024*2,1024*2],
               output="ScioPattern_%s.png"%(eventName))
    """           
    draw_hierarchy(state, pos=pos, 
               deg_order=True,    
               edge_control_points=cts,
               vertex_size=20,
               vertex_text=g.vertex_properties["name"], 
               vertex_text_position=1, 
               vertex_text_rotation=g.vertex_properties['text_rotation'], 
               vertex_font_family='mono',
               vertex_font_size=15,
               vertex_anchor=0, 
               output_size=[1024*2,1024*2],
               output="hierarchy_%s.png"%(eventName))

The labels get align radially when I use "graph_draw" but they become randomly orientated when I use "draw_hierarchy". Why is this happening?
enter image description here

I am also wondering how I could build a layered blockmodel? Should I use LayeredBlockState class? Is it possible that someone provides an example of how it works?


Solution

  • Just pass the option vertex_text_position="centered" to draw_hierarchy().

    There is an example of using layered SBMs in the HOWTO: https://graph-tool.skewed.de/static/doc/demos/inference/inference.html#layered-networks