def mean_f(M): nodes = listify_nodes(M) avg = 0 for n in nodes: avg += f_(n) return avg / len(nodes)
def median_f(M): nodes = listify_nodes(M) nodes.sort(key=f_) num = len(nodes) n1 = nodes[math.ceil((num-1)/2)] n2 = nodes[math.floor((num-1)/2)] med = (f_(n1)+f_(n2))/2 return med
def find_root(T): nodes = listify_nodes(T) max_ = f_(nodes[0]) max_node = nodes[0] for n in nodes: if(f_(n) > max_): max_ = f_(n) max_node = n return max_node['name']
def shift_f(M, center = "median"): if center == "median": center = median_f(M) elif center == "mean": center = mean_f(M) else: print("Invalid center parameter. Valid choices are 'median' and 'mean'. Using median for shifting.") center = median_f(M) nodes = listify_nodes(M) for n in nodes: n['value'] = n['value'] - center
def calc_values_height_reorient(G, pos, angle=None): #Get the list of node objects n = listify_nodes(G) if(angle!=None): reorient(pos, angle) #Set all of the function values by height for i in range(0, len(n)): n[i]['value'] = height(pos[n[i]['name']], math.pi / 2) else: for i in range(0, len(n)): n[i]['value'] = pos[n[i]['name']][1]
def calc_values_height(G, pos, angle): direction = [math.cos(angle), math.sin(angle)] x = direction[0] y = direction[1] if(len(direction) > 2 or (x==0 and y==0)): print("Faulty input direction!") return #Get the list of node objects n = listify_nodes(G) #Set all of the function values by height for i in range(0, len(n)): n[i]['value'] = height(pos[n[i]['name']], angle)
def merge_tree(G, shift=True): preprocess(G) #Get the nodes from the networkx graph nodes = listify_nodes(G) nodes.sort(key=f_) #The legendary Merge Tree M = nx.Graph() for i in range(0, len(nodes)): add_node(nodes[i]['name'], G, M) reduce(M) if(shift): shift_f(M) return M