def bell_reweighting(tree, root, sublinear=False):
# convert the hierarchy to a tree if make_bfs_tree is true
distance_by_target = nx.shortest_path_length(tree, source=root)
level_count = defaultdict(int)
for val in distance_by_target.values():
level_count[val] += 1
for edge in tree.edges():
parent, child = edge
if sublinear:
# use smoothed logarithm
tree[parent][child]['weight'] = 1.0 / log(1 + level_count[distance_by_target[child]], 10)
else:
tree[parent][child]['weight'] = 1.0 / level_count[distance_by_target[child]]
return tree
评论列表
文章目录