Changeset 622 for mds-and-trees


Ignore:
Timestamp:
10/11/16 16:48:23 (4 years ago)
Author:
konrad
Message:

Added an option to restrict the tree to first X nodes.

File:
1 edited

Legend:

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

    r621 r622  
    101101    global firstnode, nodes, inv_nodes, time
    102102    f = open(dir)
     103    loaded = 0
     104
    103105    for line in f:
    104106        sline = line.split(' ', 1)
     
    127129                    kind[creature["ID"]] = creature["Kind"]
    128130
     131                loaded += 1
     132        if loaded == max_nodes and max_nodes != 0:
     133            break
     134
    129135    for k, v in sorted(nodes.items()):
    130136        for val in sorted(v):
     
    138144    global firstnode, nodes, inv_nodes
    139145    f = open(dir)
     146    loaded = 0
     147
    140148    for line in f:
    141149        sline = line.split()
     
    148156        else:
    149157            firstnode = sline[0]
     158
     159        loaded += 1
     160        if loaded == max_nodes and max_nodes != 0:
     161            break
    150162
    151163    for k, v in sorted(nodes.items()):
     
    181193        x1_dist = 0
    182194        x2_dist = 0
     195        ymin = y-10
     196        ymax = y+10
    183197        for pos in positions:
    184198            pos = positions[pos]
    185             if pos[1] > y-10 or pos[1] < y+10:
    186                 dy = pos[1]-y
     199            if pos[1] > ymin or pos[1] < ymax:
     200                dysq = (pos[1]-y)**2
    187201                dx1 = pos[0]-x1
    188202                dx2 = pos[0]-x2
    189203
    190                 x1_dist += math.sqrt(dy**2 + dx1**2)
    191                 x2_dist += math.sqrt(dy**2 + dx2**2)
     204
     205                x1_dist += math.sqrt(dysq + dx1**2)
     206                x2_dist += math.sqrt(dysq + dx2**2)
    192207        return (x1 if x1_dist > x2_dist else x2)
    193208
    194209# ------------------------------------
    195 
    196 # def prepos_children_reccurent(node):
    197 #     global visited
    198 #     for c in inv_nodes[node]:
    199 #
    200 #         # we want to visit the node just once, after all of its parents
    201 #         if not all_parents_visited(c):
    202 #             continue
    203 #         else:
    204 #             visited[c] = True
    205 #
    206 #         cy = 0
    207 #         if TIME == "BIRTHS":
    208 #             if c[0] == "c":
    209 #                 cy = int(c[1:])
    210 #             else:
    211 #                 cy = int(c)
    212 #         elif TIME == "GENERATIONAL":
    213 #             cy = positions[node][1]+1
    214 #         elif TIME == "REAL":
    215 #             cy = time[c]
    216 #
    217 #         if len(nodes[c]) == 1:
    218 #             dissimilarity = 0
    219 #             if JITTER == True:
    220 #                 dissimilarity = random.gauss(0,1)
    221 #             else:
    222 #                 dissimilarity = 1
    223 #             positions[c] = [xmin_crowd(positions[node][0]-dissimilarity, positions[node][0]+dissimilarity, cy), cy]
    224 #         else:
    225 #             vsum = sum([v for k, v in nodes[c].items()])
    226 #             cx = sum([positions[k][0]*v/vsum for k, v in nodes[c].items()])
    227 #
    228 #             if JITTER == True:
    229 #                 positions[c] = [cx + random.gauss(0, 0.1), cy]
    230 #             else:
    231 #                 positions[c] = [cx, cy]
    232 #
    233 #
    234 #         if c in inv_nodes:
    235 #             prepos_children_reccurent(c)
    236210
    237211def prepos_children():
     
    451425
    452426min_skeleton_depth = 0
     427max_nodes = 0
    453428
    454429firstnode = None
     
    462437
    463438def main():
    464     global svg_file, min_skeleton_depth, args, \
     439    global svg_file, min_skeleton_depth, max_nodes, args, \
    465440        TIME, BALANCE, DOT_STYLE, COLORING, JITTER, \
    466441        svg_mutation_line_style, svg_crossover_line_style
     
    499474
    500475    parser.add_argument('--simple-data', type=bool, dest='simple_data', help='input data are given in a simple format (#child #parent)')
     476
     477
     478    parser.add_argument('-x', '--max-nodes', type=int, default=0, dest='max_nodes', help='maximum number of nodes drawn (starting from the first one)')
    501479
    502480    parser.set_defaults(draw_tree=True)
     
    528506    dir = args.input
    529507    min_skeleton_depth = args.min_skeleton_depth
     508    max_nodes = args.max_nodes
    530509    seed = args.seed
    531510    if seed == -1:
Note: See TracChangeset for help on using the changeset viewer.