Changeset 621 for mds-and-trees


Ignore:
Timestamp:
10/10/16 17:18:52 (7 years ago)
Author:
konrad
Message:

Rewritten prepos_children to the iterative form + Turned on some debugging

File:
1 edited

Legend:

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

    r620 r621  
    66import math
    77import argparse
     8import time as ttime
    89
    910TIME = "" # BIRTHS / GENERATIONAL / REAL
     
    117118                        quit()
    118119
    119                     if not creature["FromIDs"][0] in nodes:
     120                    if not creature["FromIDs"][0] in nodes and firstnode == None:
    120121                        firstnode = creature["FromIDs"][0]
    121122
     
    130131            inv_nodes[val] = inv_nodes.get(val, [])
    131132            inv_nodes[val].append(k)
     133
     134    print(len(nodes))
    132135
    133136
     
    191194# ------------------------------------
    192195
    193 def prepos_children_reccurent(node):
    194     global visited
    195     for c in inv_nodes[node]:
    196 
    197         # we want to visit the node just once, after all of its parents
    198         if not all_parents_visited(c):
    199             continue
    200         else:
    201             visited[c] = True
    202 
    203         cy = 0
    204         if TIME == "BIRTHS":
    205             if c[0] == "c":
    206                 cy = int(c[1:])
    207             else:
    208                 cy = int(c)
    209         elif TIME == "GENERATIONAL":
    210             cy = positions[node][1]+1
    211         elif TIME == "REAL":
    212             cy = time[c]
    213 
    214         if len(nodes[c]) == 1:
    215             dissimilarity = 0
    216             if JITTER == True:
    217                 dissimilarity = random.gauss(0,1)
    218             else:
    219                 dissimilarity = 1
    220             positions[c] = [xmin_crowd(positions[node][0]-dissimilarity, positions[node][0]+dissimilarity, cy), cy]
    221         else:
    222             vsum = sum([v for k, v in nodes[c].items()])
    223             cx = sum([positions[k][0]*v/vsum for k, v in nodes[c].items()])
    224 
    225             if JITTER == True:
    226                 positions[c] = [cx + random.gauss(0, 0.1), cy]
    227             else:
    228                 positions[c] = [cx, cy]
    229 
    230 
    231         if c in inv_nodes:
    232             prepos_children_reccurent(c)
     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)
    233236
    234237def prepos_children():
    235     global max_height, max_width, min_width, visited
     238    global max_height, max_width, min_width, visited, TIME
     239
     240    print("firstnode " + firstnode)
    236241
    237242    if not bool(time):
     
    241246    positions[firstnode] = [0, 0]
    242247
    243     visited = {}
    244     visited[firstnode] = True
    245     prepos_children_reccurent(firstnode)
     248    #visited = {}
     249    #visited[firstnode] = True
     250
     251
     252    nodes_to_visit = [firstnode]
     253
     254    ccc = 0
     255    timet = ttime.time()
     256
     257    while True:
     258
     259        ccc += 1
     260        if ccc%1000 == 0 :
     261            print(str(ccc) + " "  + str(ttime.time()-timet))
     262            timet = ttime.time()
     263
     264        current_node = nodes_to_visit[0]
     265
     266        if current_node in inv_nodes:
     267            for c in inv_nodes[current_node]:
     268                # we want to visit the node just once, after all of its parents
     269                if c not in nodes_to_visit:
     270                    nodes_to_visit.append(c)
     271
     272                    cy = 0
     273                    if TIME == "BIRTHS":
     274                        if c[0] == "c":
     275                            cy = int(c[1:])
     276                        else:
     277                            cy = int(c)
     278                    elif TIME == "GENERATIONAL":
     279                        cy = positions[current_node][1]+1
     280                    elif TIME == "REAL":
     281                        cy = time[c]
     282
     283                    if len(nodes[c]) == 1:
     284                        dissimilarity = 0
     285                        if JITTER == True:
     286                            dissimilarity = random.gauss(0,1)
     287                        else:
     288                            dissimilarity = 1
     289                        positions[c] = [xmin_crowd(positions[current_node][0]-dissimilarity, positions[current_node][0]+dissimilarity, cy), cy]
     290                    else:
     291                        vsum = sum([v for k, v in nodes[c].items()])
     292                        cx = sum([positions[k][0]*v/vsum for k, v in nodes[c].items()])
     293
     294                        if JITTER == True:
     295                            positions[c] = [cx + random.gauss(0, 0.1), cy]
     296                        else:
     297                            positions[c] = [cx, cy]
     298
     299
     300        #if c in inv_nodes:
     301        #    prepos_children_reccurent(c)
     302
     303        nodes_to_visit = nodes_to_visit[1:]
     304        # if none left, we can stop
     305        if len(nodes_to_visit) == 0:
     306            break
     307
     308
     309   # prepos_children_reccurent(firstnode)
    246310
    247311    for pos in positions:
     
    388452min_skeleton_depth = 0
    389453
    390 firstnode = ""
     454firstnode = None
    391455nodes = {}
    392456inv_nodes = {}
Note: See TracChangeset for help on using the changeset viewer.