Changeset 619


Ignore:
Timestamp:
09/23/16 15:06:58 (8 years ago)
Author:
konrad
Message:

Changed spine and skeleton drawing methods from recurrent to iterative ones.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mds-and-trees/tree-genealogy.py

    r618 r619  
    261261# ------------------------------------
    262262
    263 def draw_children_norec():
    264     global visited
    265     visited = {}
    266     visited[firstnode] = True
    267 
     263def draw_children():
    268264    max_depth = 0
    269265    for k, v in depth.items():
     
    308304            break
    309305
    310 def draw_spine_recurrent(node):
    311     global visited
    312     for c in inv_nodes[node]:
    313 
    314         # we want to draw the node just once
    315         if all_parents_visited(c):
    316             visited[c] = True
    317 
    318             if depth[c] == depth[node] - 1:
    319                 if c in inv_nodes:
    320                     draw_spine_recurrent(c)
    321 
    322         if depth[c] == depth[node] - 1:
    323             line_style = svg_spine_line_style
    324             svg_add_line( (w_margin+w_no_margs*(positions[node][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[node][1]/max_height),
    325                 (w_margin+w_no_margs*(positions[c][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[c][1]/max_height), line_style)
    326         #svg_add_dot( (w_margin+w_no_margs*(positions[c][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[c][1]/max_height), svg_spine_dot_style)
    327 
    328306def draw_spine():
    329     global visited
    330     visited = {}
    331     visited[firstnode] = True
    332 
    333     draw_spine_recurrent(firstnode)
    334     #svg_add_dot( (w_margin+w_no_margs*(positions[firstnode][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[firstnode][1]/max_height), svg_spine_dot_style)
    335 
    336 def draw_skeleton_reccurent(node):
    337     global visited
    338     for c in inv_nodes[node]:
    339 
    340         if all_parents_visited(c):
    341             visited[c] = True
    342 
    343             if depth[c] >= min_skeleton_depth: # or depth[c] == max([depth[q] for q in inv_nodes[node]]):
    344                 if c in inv_nodes:
    345                     draw_skeleton_reccurent(c)
    346 
    347         if depth[c] >= min_skeleton_depth: # or depth[c] == max([depth[q] for q in inv_nodes[node]]):
    348             #print([depth[q] for q in inv_nodes[node]])
    349             line_style = svg_spine_line_style
    350             svg_add_line( (w_margin+w_no_margs*(positions[node][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[node][1]/max_height),
    351                 (w_margin+w_no_margs*(positions[c][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[c][1]/max_height), line_style)
    352             #svg_add_dot( (w_margin+w_no_margs*(positions[c][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[c][1]/max_height), svg_spine_dot_style)
     307    nodes_to_visit = [firstnode]
     308    while True:
     309        current_node = nodes_to_visit[0]
     310
     311        if current_node in inv_nodes:
     312            for c in inv_nodes[current_node]: # inv_node => p->c
     313                if depth[c] == depth[current_node] - 1:
     314                    if not c in nodes_to_visit:
     315                        nodes_to_visit.append(c)
     316                    line_style = svg_spine_line_style
     317                    svg_add_line( (w_margin+w_no_margs*(positions[current_node][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[current_node][1]/max_height),
     318                        (w_margin+w_no_margs*(positions[c][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[c][1]/max_height), line_style)
     319
     320        # we remove the current node from the list
     321        nodes_to_visit = nodes_to_visit[1:]
     322        # if none left, we can stop
     323        if len(nodes_to_visit) == 0:
     324            break
    353325
    354326def draw_skeleton():
    355     global visited
    356     visited = {}
    357     visited[firstnode] = True
    358 
    359     draw_skeleton_reccurent(firstnode)
    360     #svg_add_dot( (w_margin+w_no_margs*(positions[firstnode][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[firstnode][1]/max_height), svg_spine_dot_style)
     327    nodes_to_visit = [firstnode]
     328    while True:
     329        current_node = nodes_to_visit[0]
     330
     331        if current_node in inv_nodes:
     332            for c in inv_nodes[current_node]: # inv_node => p->c
     333                if depth[c] >= min_skeleton_depth:
     334                    if not c in nodes_to_visit:
     335                        nodes_to_visit.append(c)
     336                    line_style = svg_spine_line_style
     337                    svg_add_line( (w_margin+w_no_margs*(positions[current_node][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[current_node][1]/max_height),
     338                        (w_margin+w_no_margs*(positions[c][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[c][1]/max_height), line_style)
     339
     340        # we remove the current node from the list
     341        nodes_to_visit = nodes_to_visit[1:]
     342        # if none left, we can stop
     343        if len(nodes_to_visit) == 0:
     344            break
    361345
    362346# ------------------------------------
     
    491475
    492476    if args.draw_tree:
    493         draw_children_norec()
     477        draw_children()
    494478    if args.draw_skeleton:
    495479        draw_skeleton()
Note: See TracChangeset for help on using the changeset viewer.