Changeset 577


Ignore:
Timestamp:
08/09/16 19:38:35 (8 years ago)
Author:
konrad
Message:

Supporting coloring edges and nodes by type, scalable dots

File:
1 edited

Legend:

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

    r576 r577  
    1616svg_file = 0
    1717
    18 svg_line_style = 'stroke="rgb(90%,10%,16%)" stroke-width="1" stroke-opacity="0.8"'
     18svg_line_style = 'stroke="rgb(90%,10%,16%)" stroke-width="1" stroke-opacity="0.7"'
     19svg_mutation_line_style = 'stroke="black" stroke-width="1" stroke-opacity="0.7"'
     20svg_crossover_line_style = 'stroke="darkviolet" stroke-width="1" stroke-opacity="0.7"'
     21svg_spine_line_style = 'stroke="rgb(0%,90%,40%)" stroke-width="2" stroke-opacity="1"'
     22svg_scale_line_style = 'stroke="black" stroke-width="0.5" stroke-opacity="1" stroke-dasharray="5, 5"'
     23
    1924svg_dot_style = 'r="2" stroke="black" stroke-width="0.2" fill="red"'
    2025svg_clear_dot_style = 'r="2" stroke="black" stroke-width="0.4" fill="none"'
    21 
    22 svg_spine_line_style = 'stroke="rgb(0%,0%,80%)" stroke-width="2" stroke-opacity="1"'
    2326svg_spine_dot_style = 'r="1" stroke="black" stroke-width="0.2" fill="rgb(50%,50%,100%)"'
    2427
    25 svg_scale_line_style = 'stroke="black" stroke-width="0.5" stroke-opacity="1" stroke-dasharray="5, 5"'
    2628svg_scale_text_style = 'style="font-family: Arial; font-size: 12; fill: #000000;"'
    2729
     
    6062           + str(from_col[2]*(1-percent) + to_col[2]*percent) + '%)" stroke-width="' + str(stroke) + '" stroke-opacity="' + str(opa) + '"'
    6163
    62 def svg_generate_dot_style(percent):
    63     from_col = [100, 70, 0]
    64     to_col = [60, 0, 0]
    65     # neon
    66     # from_col = [30, 200, 255]
    67     # to_col = [240, 0, 220]
    68 
    69     from_opa = 0.2
    70     to_opa = 1.0
    71 
    72     opa = from_opa*(1-percent) + to_opa*percent
    73 
    74     percent = 1 - ((1-percent)**20)
    75 
    76     return 'fill="rgb(' + str(from_col[0]*(1-percent) + to_col[0]*percent) + '%,' \
    77            + str(from_col[1]*(1-percent) + to_col[1]*percent) + '%,' \
    78            + str(from_col[2]*(1-percent) + to_col[2]*percent) + '%)" r="1.5" stroke="black" stroke-width="0.2" fill-opacity="' + str(opa) + '" ' \
    79            'stroke-opacity="' + str(opa) + '"'
     64def svg_generate_dot_style(kind):
     65    kinds = ["red", "lawngreen", "royalblue", "magenta", "yellow", "cyan", "white", "black"]
     66
     67    r = min(2500/len(nodes), 10)
     68
     69    return 'fill="' + kinds[kind] + '" r="' + str(r) + '" stroke="black" stroke-width="' + str(r/10) + '" fill-opacity="1.0" ' \
     70           'stroke-opacity="1.0"'
    8071
    8172# -------------------
     
    10697                if "Time" in creature:
    10798                    time[creature["ID"]] = creature["Time"]
     99
     100                if "Kind" in creature:
     101                    kind[creature["ID"]] = creature["Kind"]
    108102
    109103    for k, v in sorted(nodes.items()):
     
    182176            visited[c] = True
    183177
    184         # if JITTER == True:
    185         #     dissimilarity = random.gauss(0,1)
    186         # else:
    187         #     dissimilarity = 1
    188         #     #TODO take this info from proper fields
    189 
    190178        cy = 0
    191179        if TIME == "BIRTHS":
     
    262250            draw_children_recurrent(c, max_depth)
    263251
    264         line_style = (svg_line_style if args.mono_tree else svg_generate_line_style(depth[c]/max_depth))
     252        line_style = ""
     253        if COLORING == "NONE":
     254            line_style = svg_line_style
     255        elif COLORING == "TYPE":
     256            line_style = (svg_mutation_line_style if len(nodes[c]) == 1 else svg_crossover_line_style)
     257        else: # IMPORTANCE, default
     258            line_style = svg_generate_line_style(depth[c]/max_depth)
     259
    265260        for k, v in sorted(nodes[c].items()):
    266261            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),
     
    269264        if DOT_STYLE == "NONE":
    270265            continue
    271         elif DOT_STYLE == "CLEAR":
    272             dot_style = svg_clear_dot_style
     266        elif DOT_STYLE == "KIND":
     267            dot_style = svg_generate_dot_style(kind[c] if c in kind else 0) #type
    273268        else: # NORMAL, default
    274             dot_style = svg_generate_dot_style(depth[c]/max_depth)
     269            dot_style = svg_clear_dot_style #svg_generate_dot_style(depth[c]/max_depth)
    275270        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)
    276271def draw_children():
     
    286281    if DOT_STYLE == "NONE":
    287282        return
    288     elif DOT_STYLE == "CLEAR":
    289         dot_style = svg_clear_dot_style
     283    elif DOT_STYLE == "KIND":
     284        dot_style = svg_generate_dot_style(kind[firstnode] if firstnode in kind else 0)
    290285    else: # NORMAL, default
    291         dot_style = svg_generate_dot_style(depth[firstnode]/max_depth)
     286        dot_style = svg_clear_dot_style #svg_generate_dot_style(depth[c]/max_depth)
    292287    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)
    293288
     
    350345    if TIME == "GENERATIONAL":
    351346       end_text = "Depth " + str(max([v for k, v in depth.items()]))
    352     svg_add_text( end_text, (w, h-h_margin - 5), "end")
     347    svg_add_text( end_text, (w, h-h_margin + 15), "end")
    353348
    354349
     
    377372depth = {}
    378373time = {}
     374kind = {}
    379375
    380376def main():
    381     global svg_file, min_skeleton_depth, args, TIME, BALANCE, DOT_STYLE, JITTER
     377    global svg_file, min_skeleton_depth, args, TIME, BALANCE, DOT_STYLE, COLORING, JITTER
    382378
    383379    parser = argparse.ArgumentParser(description='Process some integers.')
     
    402398                                                                      'REAL: real time of the simulation')
    403399    parser.add_argument('-b', '--balance', default='MIN', dest='balance', help='method of placing node in the tree (RANDOM/MIN/DENSITY)')
    404 
    405     parser.add_argument('-s', '--scale', default='NONE', dest='scale', help='type of timescale added to the tree (NONE/SIMPLE/FULL)')
    406 
    407     parser.add_argument('-d', '--dots', default='NORMAL', dest='dots', help='method of drawing dots (individuals) (NONE/NORMAL/CLEAR)')
    408 
     400    parser.add_argument('-s', '--scale', default='NONE', dest='scale', help='type of timescale added to the tree (NONE/SIMPLE)')
     401    parser.add_argument('-c', '--coloring', default='IMPORTANCE', dest="coloring", help='method of coloring the tree (NONE/IMPORTANCE/TYPE)')
     402    parser.add_argument('-d', '--dots', default='NORMAL', dest='dots', help='method of drawing dots (individuals) (NONE/NORMAL/TYPE)')
    409403    parser.add_argument('-j', '--jitter', dest="jitter", action='store_true', help='draw horizontal positions of children from the normal distribution')
    410 
    411     mono_tree_parser = parser.add_mutually_exclusive_group(required=False)
    412     mono_tree_parser.add_argument('--mono-tree', dest='mono_tree', action='store_true', help='whether the tree should be drawn with a single color')
    413     mono_tree_parser.add_argument('--no-mono-tree', dest='mono_tree', action='store_false')
    414404
    415405    parser.add_argument('--min-skeleton-depth', type=int, default=2, dest='min_skeleton_depth', help='minimal distance from the leafs for the nodes in the skeleton')
     
    418408    parser.add_argument('--simple-data', type=bool, dest='simple_data', help='input data are given in a simple format (#child #parent)')
    419409
    420     parser.set_defaults(mono_tree=False)
    421410    parser.set_defaults(draw_tree=True)
    422411    parser.set_defaults(draw_skeleton=False)
     
    430419    BALANCE = args.balance
    431420    DOT_STYLE = args.dots
     421    COLORING = args.coloring
    432422    JITTER = args.jitter
    433423
Note: See TracChangeset for help on using the changeset viewer.