def get_annotation_branch_lengths(annotation):
"""
Fragments an annotation into pieces at every branch point - remaining
lonely nodes are deleted. Branch nodes are simply deleted. The lengths of
the resulting fragments are then returned.
WARNING: THIS FUNCTION IS REALLY SLOW FOR SOME REASONS I DO NOT FULLY
UNDERSTAND AT THE MOMENT, PROBABLY OBJECT COPY RELATED
:param annotation:
:return: list of branch lengths in um
"""
# this is necessary to avoid trouble because of in place modifications
anno = copy.deepcopy(annotation)
nx_graph = su.annotation_to_nx_graph(anno)
branches = list({k for k, v in nx_graph.degree().iteritems() if v > 2})
nx_graph.remove_nodes_from(branches)
lonely = list({k for k, v in nx_graph.degree().iteritems() if v == 0})
nx_graph.remove_nodes_from(lonely)
ccs = list(nx.connected_component_subgraphs(nx_graph))
lengths = [cc.size(weight='weight') / 1000. for cc in ccs]
return lengths
评论列表
文章目录