Skip to content

Commit

Permalink
Python C glue for B1 index
Browse files Browse the repository at this point in the history
Closes #2255
  • Loading branch information
jeromekelleher authored and mergify[bot] committed Jun 20, 2022
1 parent 1a8a614 commit 8feedac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 10 deletions.
25 changes: 25 additions & 0 deletions python/_tskitmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -10802,6 +10802,27 @@ Tree_get_colless_index(Tree *self)
return ret;
}

static PyObject *
Tree_get_b1_index(Tree *self)
{
PyObject *ret = NULL;
int err;
double result;

if (Tree_check_state(self) != 0) {
goto out;
}

err = tsk_tree_b1_index(self->tree, &result);
if (err != 0) {
handle_library_error(err);
goto out;
}
ret = Py_BuildValue("d", result);
out:
return ret;
}

static PyObject *
Tree_get_root_threshold(Tree *self)
{
Expand Down Expand Up @@ -11221,6 +11242,10 @@ static PyMethodDef Tree_methods[] = {
.ml_meth = (PyCFunction) Tree_get_colless_index,
.ml_flags = METH_NOARGS,
.ml_doc = "Returns the Colless index for this tree." },
{ .ml_name = "get_b1_index",
.ml_meth = (PyCFunction) Tree_get_b1_index,
.ml_flags = METH_NOARGS,
.ml_doc = "Returns the B1 index for this tree." },
{ NULL } /* Sentinel */
};

Expand Down
11 changes: 1 addition & 10 deletions python/tskit/trees.py
Original file line number Diff line number Diff line change
Expand Up @@ -2793,16 +2793,7 @@ def b1_index(self):
:return: The B1 balance index.
:rtype: float
"""
# TODO implement in C
max_path_length = np.zeros(self.tree_sequence.num_nodes, dtype=int)
total = 0.0
for u in self.postorder():
if self.parent(u) != tskit.NULL and self.is_internal(u):
max_path_length[u] = 1 + max(
max_path_length[v] for v in self.children(u)
)
total += 1 / max_path_length[u]
return total
return self._ll_tree.get_b1_index()

def colless_index(self):
"""
Expand Down

0 comments on commit 8feedac

Please sign in to comment.