Changeset 571


Ignore:
Timestamp:
08/08/16 16:10:18 (8 years ago)
Author:
konrad
Message:

Additional options in tree-genealogy.py, dots and jitter

File:
1 edited

Legend:

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

    r566 r571  
    66import argparse
    77
    8 TIME = "GENERATIONAL"
    9 BALANCE = "MIN"
     8TIME = "" # BIRTHS / GENERATIONAL / REAL
     9BALANCE = "" # MIN / DENSITY
     10
     11DOT_STYLE = "" # NONE / NORMAL / CLEAR
     12
     13JITTER = "" #
    1014
    1115# ------SVG---------
     
    1418svg_line_style = 'stroke="rgb(90%,10%,16%)" stroke-width="1" stroke-opacity="0.8"'
    1519svg_dot_style = 'r="2" stroke="black" stroke-width="0.2" fill="red"'
     20svg_clear_dot_style = 'r="2" stroke="black" stroke-width="0.4" fill="none"'
     21
    1622svg_spine_line_style = 'stroke="rgb(0%,0%,80%)" stroke-width="2" stroke-opacity="1"'
    17 #svg_spine_dot_style = 'r="3" stroke="black" stroke-width="0.4" fill="rgb(50%,50%,100%)"'
    1823svg_spine_dot_style = 'r="1" stroke="black" stroke-width="0.2" fill="rgb(50%,50%,100%)"'
    1924
     
    2631def svg_generate_line_style(percent):
    2732    # hotdog
     33    from_col = [100, 70, 0]
     34    to_col = [60, 0, 0]
     35    # lava
     36    # from_col = [100, 80, 0]
     37    # to_col = [100, 0, 0]
     38    # neon
     39    # from_col = [30, 200, 255]
     40    # to_col = [240, 0, 220]
     41
     42    from_opa = 0.2
     43    to_opa = 1.0
     44    from_stroke = 1
     45    to_stroke = 3
     46
     47    opa = from_opa*(1-percent) + to_opa*percent
     48    stroke = from_stroke*(1-percent) + to_stroke*percent
     49
     50    percent = 1 - ((1-percent)**20)
     51
     52    return 'stroke="rgb(' + str(from_col[0]*(1-percent) + to_col[0]*percent) + '%,' \
     53           + str(from_col[1]*(1-percent) + to_col[1]*percent) + '%,' \
     54           + str(from_col[2]*(1-percent) + to_col[2]*percent) + '%)" stroke-width="' + str(stroke) + '" stroke-opacity="' + str(opa) + '"'
     55
     56def svg_generate_dot_style(percent):
    2857    from_col = [100, 70, 0]
    2958    to_col = [60, 0, 0]
     
    3463    from_opa = 0.2
    3564    to_opa = 1.0
    36     from_stroke = 1
    37     to_stroke = 3
    38 
    39     opa = from_opa*(1-percent) + to_opa*percent
    40     stroke = from_stroke*(1-percent) + to_stroke*percent
    41 
    42     percent = 1 - ((1-percent)**20)
    43 
    44     return 'stroke="rgb(' + str(from_col[0]*(1-percent) + to_col[0]*percent) + '%,' \
    45            + str(from_col[1]*(1-percent) + to_col[1]*percent) + '%,' \
    46            + str(from_col[2]*(1-percent) + to_col[2]*percent) + '%)" stroke-width="' + str(stroke) + '" stroke-opacity="' + str(opa) + '"'
    47 
    48 def svg_generate_dot_style(percent):
    49     from_col = [100, 90, 0]
    50     to_col = [60, 0, 0]
    51     # neon
    52     # from_col = [30, 200, 255]
    53     # to_col = [240, 0, 220]
    54 
    55     from_opa = 0.2
    56     to_opa = 1.0
    5765
    5866    opa = from_opa*(1-percent) + to_opa*percent
     
    6876
    6977def load_data(dir):
    70     global firstnode, nodes, inv_nodes
     78    global firstnode, nodes, inv_nodes, time
    7179    f = open(dir)
    7280    for line in f:
    73         sline = line.split(' ', 2)
    74         if len(sline) == 3:
    75             if sline[1] == "[OFFSPRING]":
    76                 creature = json.loads(sline[2])
     81        sline = line.split(' ', 1)
     82        if len(sline) == 2:
     83            if sline[0] == "[OFFSPRING]":
     84                creature = json.loads(sline[1])
    7785                #print("B" +str(creature))
    7886                if "FromIDs" in creature:
    79                     assert(len(creature["FromIDs"]) == 1)
     87                    #assert(len(creature["FromIDs"]) == 1)
    8088                    nodes[creature["ID"]] = creature["FromIDs"][0]
    8189                    if not creature["FromIDs"][0] in nodes:
     
    151159    for c in inv_nodes[node]:
    152160        #print(node + "->" + c)
    153         dissimilarity = 0.5 #random.gauss(0,0.3)
     161        if JITTER == True:
     162            dissimilarity = random.gauss(0,1)
     163        else:
     164            dissimilarity = 1
     165            #TODO take this info from proper fields
     166
    154167        if TIME == "BIRTHS":
    155168            id = ""
     
    193206
    194207        line_style = (svg_line_style if args.mono_tree else svg_generate_line_style(depth[c]/max_depth))
    195         dot_style = (svg_dot_style if args.mono_tree else svg_generate_dot_style(depth[c]/max_depth))
    196 
    197208        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),
    198209            (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)
     210
     211        if DOT_STYLE == "NONE":
     212            continue
     213        elif DOT_STYLE == "CLEAR":
     214            dot_style = svg_clear_dot_style
     215        else: # NORMAL, default
     216            dot_style = svg_generate_dot_style(depth[c]/max_depth)
    199217        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)
    200218def draw_children():
     
    203221            max_depth = max(max_depth, v)
    204222    draw_children_recurrent(firstnode, max_depth)
    205     dot_style = (svg_dot_style if args.mono_tree else svg_generate_dot_style(depth[firstnode]/max_depth))
     223
     224    if DOT_STYLE == "NONE":
     225        return
     226    elif DOT_STYLE == "CLEAR":
     227        dot_style = svg_clear_dot_style
     228    else: # NORMAL, default
     229        dot_style = svg_generate_dot_style(depth[firstnode]/max_depth)
    206230    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)
    207231
     
    269293
    270294def main():
    271     global svg_file, min_skeleton_depth, args, TIME, BALANCE
     295    global svg_file, min_skeleton_depth, args, TIME, BALANCE, DOT_STYLE, JITTER
    272296
    273297    parser = argparse.ArgumentParser(description='Process some integers.')
     
    287311
    288312    #TODO: better names for those parameters
    289     parser.add_argument('--time', default='BIRTHS', dest='time', help='values on vertical axis (REAL/GENERATIONAL)')
    290     parser.add_argument('--balance', default='MIN',dest='balance', help='method of placing node in the tree (RANDOM/MIN/DENSITY)')
     313    parser.add_argument('--time', default='BIRTHS', dest='time', help='values on vertical axis (BIRTHS/GENERATIONAL/REAL); '
     314                                                                      'BIRTHS: time measured as the number of births since the beggining; '
     315                                                                      'GENERATIONAL: time measured as number of ancestors; '
     316                                                                      'REAL: real time of the simulation')
     317    parser.add_argument('--balance', default='MIN', dest='balance', help='method of placing node in the tree (RANDOM/MIN/DENSITY)')
     318
     319    parser.add_argument('--dots', default='NORMAL', dest='dots', help='method of drawing dots (individuals) (NONE/NORMAL/CLEAR)')
     320
     321    parser.add_argument('-j', '--jitter', dest="jitter", action='store_true', help='draw horizontal positions of children from the normal distribution')
    291322
    292323    mono_tree_parser = parser.add_mutually_exclusive_group(required=False)
     
    310341    TIME = args.time
    311342    BALANCE = args.balance
     343    DOT_STYLE = args.dots
     344    JITTER = args.jitter
    312345
    313346    dir = args.input
Note: See TracChangeset for help on using the changeset viewer.