Fixing position of root vertex

Hi,

I'm using the code below to fix the position of a root vertex. I'm
developing an application and I wanna make sure that the root vertex is
always located in the same place.

g = Graph()
v_pin = g.new_vertex_property("bool")
v = g.add_vertex()
v_pin[v] = True #root vertex
...
pos = sfdp_layout(g, pin=v_pin)
graph_draw(g, ...)

I suppose it's working, but how I can confirm which one is the root vertex
in my result?

<http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/file/n4025773/mxt.png&gt;

There are many ways: You can change the shape, size, and color of the
vertices to your liking. It is all described in the documentation.

For instance, to change the color of the root you would do:

    c = g.new_vertex_property("int")
    c[root] = 1
    graph_draw(g, vertex_fill_color=c)

Best,
Tiago

Thanks Tiago.
Just one more question. Is there a way to fix the position of the root node?The pin parameter is not working like I expect.

attachment.html (1.7 KB)

Thanks Tiago.

Just one more question. Is there a way to fix the position of the root node?
The pin parameter is not working like I expect.

Please provide a short self-contained example which shows the problem
you are experiencing.

Best,
Tiago

Ok Tiago,

Here is the problem, the root node is located in a different place each time
I run the code. See the image (root node is in red).

<http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/file/n4025780/example.png&gt;

I wanna fix the place for the root node. For example, it should always be
located at the bottom of the result.

The "pin" parameter does exactly that, it allows you to fix the absolute
position of selected nodes. What it does not do is fix the relative
position to other nodes, which seems to be what you want. Unfortunately
there is no easy way to do that, since the nodes which are unpinned are
allowed to be placed in any way.

Perhaps what you should do is to do an interactive layout with
graph_draw(), and position/rotate the layout by hand.

Best,
Tiago

Hi Tiago,

Let me get this straight, when I use the "pin" parameter I fix the position
of the root node, but when I use the layout algorithm the position of other
nodes end up interfering with the position of the root node at the end. Is
that what is happening? I just need to fix the position of the root node and
it needs to be done im an automatic way.

Thanks for your help. I really appreciate it.

Let me get this straight, when I use the "pin" parameter I fix the position
of the root node, but when I use the layout algorithm the position of other
nodes end up interfering with the position of the root node at the end. Is
that what is happening?

No. The actual (absolute) position of the root node remains fixed (you
can check it by inspecting the position value before and after the
layout). But since the other nodes can be placed in any way, their
relative position to the root node (above it, below it, etc) is
arbitrary.

I wanna fix the place for the root node. For example, it should always
be located at the bottom of the result.

Again, your are thinking on their relative positions. The layout
algorithm has no idea which side is up, down, etc. The graph_draw()
function just frames and re-scales the nodes using their positions, but
the layout algorithm itself is fully rotational and translational
invariant.

If you want to have some control over their relative positions, you can
pin the root node, and place the other ones initially more or less in
the region you would like them to stay at the end, relative to the root
node. There is no guarantee that they will not move during the layout,
but the probability is higher.

Best,
Tiago

Thanks Tiago.

Hi,

I'm using this post as a doubt arose related to this problem.
I generated positions for the main paths of my graph and let them fixed by
pin property. But after using the function sfdp_layout all the positions
have changed. Please, let me know if this is actually the expected behavior.
Following image showing the generated positions and the positions after
using sfdp_layout.

<http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/file/n4025880/graph.png&gt;

Please,

Could anyone help me with this question?

Thanks.

Please can you post the relevant sample of code?

Cheers

Charlie

Ok.

After generating a long graph, I use linspace function to generate positions
for the main paths of the graph. So I fixed these positions using the pin
property. But after calling the function sfdp_layout all positions are
changed. Is this the expected behavior? I presented the results in the
previous post. Here is my relevant code:

    pin = g.new_vertex_property("bool")
    pos = g.new_vertex_property("vector<double>")
    v_c = g.new_vertex_property("double")
    v_s = g.new_vertex_property("int")
       
    # I created a graph with 11000 nodes.
        
    # setting positions for the main path
    position = np.linspace(0.0, 12.0, main_path_size)
    for i,idx in enumerate(main_path_i):
        pin[g.vertex(idx)] = True
        pos[g.vertex(idx)] = (0, position[i])
        v_c[g.vertex(idx)] = 50 # color
        v_s[g.vertex(idx)] = 10 # size
    
    # setting positions for the second main path
    position = np.linspace(0.0, 12.0, sec_path_size)
    for i,idx in enumerate(sec_path_i):
        pin[g.vertex(idx)] = True
        pos[g.vertex(idx)] = (position[i], 12.0)
        v_c[g.vertex(idx)] = 250 # color
        v_s[g.vertex(idx)] = 10 # size
        
    # setting positions for the thirmain path
    position = np.linspace(0.0, 12.0, thi_path_size)
    for i,idx in enumerate(thi_path_i):
        pin[g.vertex(idx)] = True
        pos[g.vertex(idx)] = (-position[i], 12.0)
        v_c[g.vertex(idx)] = 350 # color
        v_s[g.vertex(idx)] = 10 # size
       
    # calling the layout function
    pos = gt.sfdp_layout(g, pin=pin, pos=pos)
    
    gt.graph_draw(g, pos, output_size=(1000, 1000), vertex_fill_color=v_c,
vertex_size=v_s, vcmap=mat.cm.jet, output='mxt.png', fmt='png')

Try setting the pin properties of the vertices you want moved to False in your pin map.

Cheers,

Charlie

Hi Charlie,

I did what you suggested, I set to False the pin properties of the vertices
I want to move.
But after calling the layout function the positions of all vertices are
still changing.

Anyway, thanks for your help.

The pin values of the vertices for which you want the positions to stay
the same must be set to True, and the others to False. After you create
a new property map, all its values are per default False (zero). If you
just set the ones you want to move to 'False' you haven't accomplished
anything. You must also set the ones you want fixed to 'True'.

When you have problems like this it is very difficult to help if you
don't post a small self-contained example which shows the problem. I
don't mean a fragment of code, I mean something I can run myself,
together with the graph, etc.

Best,
Tiago

Hi Tiago,

Thanks for your attention.
When I use a small example it works fine. But the problem occurs when I use
a big graph with 11000 vertices as I showed in the image in the previous
post. Could I send a link containing all my edges and a the code here?

I based on this small example that is working fine:

    import graph_tool.all as gt
    import matplotlib as mat

    g = gt.Graph()
    g.add_vertex(7)
    g.add_edge(0,1)
    g.add_edge(1,2)
    g.add_edge(2,3)
    g.add_edge(2,4)
    g.add_edge(3,5)
    g.add_edge(2,6)
    
    pos = g.new_vertex_property("vector<double>")
    pin = g.new_vertex_property("bool")
        
    pos[g.vertex(0)] = (0, 0)
    pos[g.vertex(1)] = (2, 2)
    pos[g.vertex(2)] = (4, 2)
    pos[g.vertex(3)] = (6,6)
    pos[g.vertex(4)] = (0,4)
    pos[g.vertex(5)] = (9,8)
    pos[g.vertex(6)] = (0,8)
    
    pin[g.vertex(0)] = True
    pin[g.vertex(1)] = True
    pin[g.vertex(2)] = True
    pin[g.vertex(3)] = True
    pin[g.vertex(4)] = False
    pin[g.vertex(5)] = False
    pin[g.vertex(6)] = False
    
    gt.graph_draw(g, pos, output_size=(1000, 1000), edge_pen_width=1.3,
vertex_size=10, vcmap=mat.cm.jet, output='fixed_positions.png', fmt='png')
    
    pos = gt.sfdp_layout(g, pin=pin, pos=pos)

    gt.graph_draw(g, pos, output_size=(1000, 1000), edge_pen_width=1.3,
vertex_size=10, vcmap=mat.cm.jet, output='final_positions.png', fmt='png')

Try using the option "multilevel=False" in sfdp_layout(). Does that
solve the problem?

Best,
Tiago

Hi Thiago,

Using this parameter actually positions are fixed, but my layout is messy.

<http://main-discussion-list-for-the-graph-tool-project.982480.n3.nabble.com/file/n4025892/graph-fp.png&gt;