Changeset 572 for mds-and-trees
- Timestamp:
- 08/08/16 20:13:29 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
mds-and-trees/tree-genealogy.py
r571 r572 85 85 #print("B" +str(creature)) 86 86 if "FromIDs" in creature: 87 #assert(len(creature["FromIDs"]) == 1) 88 nodes[creature["ID"]] = creature["FromIDs"][0] 87 if not creature["ID"] in nodes: 88 nodes[creature["ID"]] = {} 89 # we assign to each parent its contribution to the genotype of the child 90 for i in range(0, len(creature["FromIDs"])): 91 inherited = 1 #(creature["Inherited"][i] if 'Inherited' in creature else 1) #ONLY FOR NOW 92 nodes[creature["ID"]][creature["FromIDs"][i]] = inherited 93 else: 94 print("Doubled entry for " + creature["ID"]) 95 quit() 96 89 97 if not creature["FromIDs"][0] in nodes: 90 98 firstnode = creature["FromIDs"][0] 99 91 100 if "Time" in creature: 92 101 time[creature["ID"]] = creature["Time"] 93 102 94 103 for k, v in sorted(nodes.items()): 95 inv_nodes[v] = inv_nodes.get(v, []) 96 inv_nodes[v].append(k) 104 for val in sorted(v): 105 inv_nodes[val] = inv_nodes.get(val, []) 106 inv_nodes[val].append(k) 97 107 98 108 … … 157 167 158 168 def prepos_children_reccurent(node): 169 global visited 159 170 for c in inv_nodes[node]: 160 #print(node + "->" + c) 161 if JITTER == True: 162 dissimilarity = random.gauss(0,1) 171 172 # we want to visit the node just once, after all of its parents 173 if not all_parents_visited(c): 174 continue 163 175 else: 164 dissimilarity = 1 165 #TODO take this info from proper fields 166 176 visited[c] = True 177 178 # if JITTER == True: 179 # dissimilarity = random.gauss(0,1) 180 # else: 181 # dissimilarity = 1 182 # #TODO take this info from proper fields 183 184 cy = 0 167 185 if TIME == "BIRTHS": 168 id = ""169 186 if c[0] == "c": 170 id= int(c[1:])187 cy = int(c[1:]) 171 188 else: 172 id = int(c) 173 positions[c] = [xmin_crowd(positions[node][0]-dissimilarity, positions[node][0]+dissimilarity, id), id] 189 cy = int(c) 174 190 elif TIME == "GENERATIONAL": 175 positions[c] = [xmin_crowd(positions[node][0]-dissimilarity, positions[node][0]+dissimilarity, positions[node][1]+1), positions[node][1]+1]191 cy = positions[node][1]+1 176 192 elif TIME == "REAL": 177 positions[c] = [xmin_crowd(positions[node][0]-dissimilarity, positions[node][0]+dissimilarity, time[c]), time[c]] 178 179 for c in inv_nodes[node]: 193 cy = time[c] 194 195 if len(nodes[c]) == 1: 196 dissimilarity = 0 197 if JITTER == True: 198 dissimilarity = random.gauss(0,1) 199 else: 200 dissimilarity = 1 201 positions[c] = [xmin_crowd(positions[node][0]-dissimilarity, positions[node][0]+dissimilarity, cy), cy] 202 else: 203 vsum = sum([v for k, v in nodes[c].items()]) 204 cx = sum([positions[k][0]*v/vsum for k, v in nodes[c].items()]) 205 206 if JITTER == True: 207 positions[c] = [cx + random.gauss(0, 0.1), cy] 208 else: 209 positions[c] = [cx, cy] 210 211 180 212 if c in inv_nodes: 181 213 prepos_children_reccurent(c) 182 214 183 215 def prepos_children(): 184 global max_height, max_width, min_width 216 global max_height, max_width, min_width, visited 185 217 186 218 if not bool(time): … … 190 222 positions[firstnode] = [0, 0] 191 223 224 visited = {} 225 visited[firstnode] = True 192 226 prepos_children_reccurent(firstnode) 193 227 … … 199 233 # ------------------------------------ 200 234 235 def all_parents_visited(node): 236 apv = True 237 for k, v in sorted(nodes[node].items()): 238 if not k in visited: 239 apv = False 240 break 241 return apv 242 # ------------------------------------ 243 201 244 def draw_children_recurrent(node, max_depth): 202 global max_height, max_width, min_width 245 global visited 246 203 247 for c in inv_nodes[node]: 248 249 # we want to draw the node just once 250 if not all_parents_visited(c): 251 continue 252 else: 253 visited[c] = True 254 204 255 if c in inv_nodes: 205 256 draw_children_recurrent(c, max_depth) 206 257 207 258 line_style = (svg_line_style if args.mono_tree else svg_generate_line_style(depth[c]/max_depth)) 208 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), 209 (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) 259 for k, v in sorted(nodes[c].items()): 260 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), 261 (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 262 211 263 if DOT_STYLE == "NONE": … … 217 269 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) 218 270 def draw_children(): 271 global visited 272 visited = {} 273 visited[firstnode] = True 274 219 275 max_depth = 0 220 276 for k, v in depth.items(): … … 231 287 232 288 def draw_spine_recurrent(node): 233 global max_height, max_width, min_width234 289 for c in inv_nodes[node]: 235 290 if depth[c] == depth[node] - 1: … … 246 301 247 302 def draw_skeleton_reccurent(node, max_depth): 248 global max_height, max_width, min_width249 303 for c in inv_nodes[node]: 250 304 if depth[c] >= min_skeleton_depth or depth[c] == max([depth[q] for q in inv_nodes[node]]): … … 289 343 inv_nodes = {} 290 344 positions = {} 345 visited= {} 291 346 depth = {} 292 347 time = {}
Note: See TracChangeset
for help on using the changeset viewer.