higra / Higra

Hierarchical Graph Analysis

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Simplify tree with leaves error

fguiotte opened this issue · comments

From time to time, depending on the criterion used, simplify_tree operating on leaves will trigger an assertion in tree constructor :

t = hg.Tree((7, 7, 8, 8, 8, 9, 9, 11, 10, 10, 11, 11))
criterion = np.zeros(t.num_vertices(), dtype=np.bool)
criterion[[5,6]] = True

new_tree, node_map = hg.simplify_tree(t, criterion, True)
RuntimeError: tree in file /tmp/pip-req-build-_gvhjvcf/include/higra/algo/../structure/tree_graph.hpp(line:135): leaves nodes are not before internal nodes

It may come from the tree remapping but I can't find any mistakes.

for (auto i: leaves_to_root_iterator(t, leaves_it::include, root_it::exclude)) {
if (!criterion(i) || (!process_leaves && is_leaf(i, t))) {
auto par = copy_parent(i);
auto new_par = par - deleted_map(par);
node_map(count) = i;
new_parent(count) = new_par;
count++;
}
}
node_map(node_map.size() - 1) = root(t);
return make_remapped_tree(tree(new_parent, t.category()), std::move(node_map));

OK, I see what is happening: the parent relation which is created by simplify_tree is valid but it does not satisfy a precondition of the Tree constructor (leaves must come first). This means that some reordering may append when some leaves are deleted.

I'll work on a patch asap. Thx for the clear bug report.

@Karamaz0V1 If you need this urgently I can make a new release. Otherwise, I will wait a bit to add more things before creating version 0.4.3

No problem, I build the latest version myself. Thank you very much for the quick fix!

Sorry to report again!
Some cases are corrected by the last fix but there are still some problems. I was able to produce two RuntimeError:

t = hg.Tree((7, 7, 8, 8, 8, 9, 9, 11, 10, 10, 11, 11))
criterion = np.zeros(t.num_vertices(), dtype=np.bool)
criterion[:8] = True
new_tree, node_map = hg.simplify_tree(t, criterion, process_leaves=True)
RuntimeError: tree in file /tmp/pip-req-build-ag8hxpfh/include/higra/algo/../structure/tree_graph.hpp(line:126): several root nodes detected

and

criterion[:9] = True
new_tree, node_map = hg.simplify_tree(t, criterion, process_leaves=True)
RuntimeError: tree in file /tmp/pip-req-build-ag8hxpfh/include/higra/algo/../structure/tree_graph.hpp(line:122): nodes are not in a topological order (last node is not a root)

On the bright side, there is no problem when we first simplify the tree without the leaves, then remove the leaves as a second step.

Sorry, you are right, leaves removal are tricky and I missed a case !