Changeset 618


Ignore:
Timestamp:
09/23/16 13:09:55 (8 years ago)
Author:
konrad
Message:

Removed the recurrent version of the tree-drawing method, fixed and tested the new, iterative one.

File:
1 edited

Legend:

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

    r617 r618  
    261261# ------------------------------------
    262262
    263 def draw_children_recurrent(node, max_depth):
    264     global visited
    265 
    266     for c in inv_nodes[node]:
    267 
    268         # we want to draw the node just once
    269         if not all_parents_visited(c):
    270             continue
    271         else:
    272             visited[c] = True
    273 
    274         if c in inv_nodes:
    275             draw_children_recurrent(c, max_depth)
    276 
    277         line_style = ""
    278         if COLORING == "NONE":
    279             line_style = svg_line_style
    280         elif COLORING == "TYPE":
    281             line_style = (svg_mutation_line_style if len(nodes[c]) == 1 else svg_crossover_line_style)
    282         else: # IMPORTANCE, default
    283             line_style = svg_generate_line_style(depth[c]/max_depth)
    284 
    285         for k, v in sorted(nodes[c].items()):
    286             svg_add_line( (w_margin+w_no_margs*(positions[k][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[k][1]/max_height),
    287                 (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)
    288 
    289         if DOT_STYLE == "NONE":
    290             continue
    291         elif DOT_STYLE == "TYPE":
    292             dot_style = svg_generate_dot_style(kind[c] if c in kind else 0) #type
    293         else: # NORMAL, default
    294             dot_style = svg_clear_dot_style #svg_generate_dot_style(depth[c]/max_depth)
    295         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), dot_style)
    296         #svg_add_text( str(depth[c]), (w_margin+w_no_margs*(positions[c][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[c][1]/max_height), "end")
    297 
    298 def draw_children():
    299     global visited
    300     visited = {}
    301     visited[firstnode] = True
    302 
    303     max_depth = 0
    304     for k, v in depth.items():
    305             max_depth = max(max_depth, v)
    306     draw_children_recurrent(firstnode, max_depth)
    307 
    308     if DOT_STYLE == "NONE":
    309         return
    310     elif DOT_STYLE == "TYPE":
    311         dot_style = svg_generate_dot_style(kind[firstnode] if firstnode in kind else 0)
    312     else: # NORMAL, default
    313         dot_style = svg_clear_dot_style #svg_generate_dot_style(depth[c]/max_depth)
    314     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), dot_style)
    315 
    316 
    317 
    318263def draw_children_norec():
    319264    global visited
     
    329274        current_node = nodes_to_visit[0]
    330275
    331         for c in inv_nodes[current_node]:
    332 
    333             # we want to draw the node just once
    334             if not all_parents_visited(c):
    335                 continue
    336             else:
    337                 visited[c] = True
    338 
    339             if c in inv_nodes:
    340                 nodes_to_visit.append(c)
    341 
    342             line_style = ""
    343             if COLORING == "NONE":
    344                 line_style = svg_line_style
    345             elif COLORING == "TYPE":
    346                 line_style = (svg_mutation_line_style if len(nodes[c]) == 1 else svg_crossover_line_style)
    347             else: # IMPORTANCE, default
    348                 line_style = svg_generate_line_style(depth[c]/max_depth)
    349 
    350             for k, v in sorted(nodes[c].items()):
    351                 svg_add_line( (w_margin+w_no_margs*(positions[k][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[k][1]/max_height),
    352                     (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)
    353 
    354             if DOT_STYLE == "NONE":
    355                 continue
    356             elif DOT_STYLE == "TYPE":
    357                 dot_style = svg_generate_dot_style(kind[c] if c in kind else 0) #type
    358             else: # NORMAL, default
    359                 dot_style = svg_clear_dot_style #svg_generate_dot_style(depth[c]/max_depth)
    360             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), dot_style)
    361             #svg_add_text( str(depth[c]), (w_margin+w_no_margs*(positions[c][0]-min_width)/(max_width-min_width), h_margin+h_no_margs*positions[c][1]/max_height), "end")
     276        if current_node in inv_nodes:
     277            for c in inv_nodes[current_node]: # inv_node => p->c
     278
     279                if not c in nodes_to_visit:
     280                    nodes_to_visit.append(c)
     281
     282                line_style = ""
     283                if COLORING == "NONE":
     284                    line_style = svg_line_style
     285                elif COLORING == "TYPE":
     286                    line_style = (svg_mutation_line_style if len(nodes[c]) == 1 else svg_crossover_line_style)
     287                else: # IMPORTANCE, default
     288                    line_style = svg_generate_line_style(depth[c]/max_depth)
     289
     290                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),
     291                        (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)
     292
     293        # we want to draw the node just once
     294        if DOT_STYLE == "NONE":
     295            continue
     296        elif DOT_STYLE == "TYPE":
     297            dot_style = svg_generate_dot_style(kind[current_node] if current_node in kind else 0) #type
     298        else: # NORMAL, default
     299            dot_style = svg_clear_dot_style #svg_generate_dot_style(depth[c]/max_depth)
     300        svg_add_dot( (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), dot_style)
     301        #svg_add_text( str(depth[current_node]), (w_margin+w_no_margs*(positions[current_node][0]-min_width)/(max_width-min_width),
     302        # h_margin+h_no_margs*positions[current_node][1]/max_height), "end")
    362303
    363304        # we remove the current node from the list
     
    366307        if len(nodes_to_visit) == 0:
    367308            break
    368 
    369     if DOT_STYLE == "NONE":
    370         return
    371     elif DOT_STYLE == "TYPE":
    372         dot_style = svg_generate_dot_style(kind[firstnode] if firstnode in kind else 0)
    373     else: # NORMAL, default
    374         dot_style = svg_clear_dot_style #svg_generate_dot_style(depth[c]/max_depth)
    375     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), dot_style)
    376309
    377310def draw_spine_recurrent(node):
     
    558491
    559492    if args.draw_tree:
    560         draw_children()
     493        draw_children_norec()
    561494    if args.draw_skeleton:
    562495        draw_skeleton()
Note: See TracChangeset for help on using the changeset viewer.