matsengrp / gctree

GCtree: phylogenetic inference of genotype-collapsed trees
https://matsengrp.github.io/gctree
GNU General Public License v3.0
16 stars 2 forks source link

Fix ufunc attribute error from numpy.log #125

Closed willdumm closed 7 months ago

willdumm commented 7 months ago

When there are very many MP trees, it's possible to get the following error during inference:

AttributeError: 'int' object has no attribute 'log'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/wdumm/Downloads/gctree/gctree/branching_processes.py", line 1124, in mle
    self.parameters = _mle_helper(self.ll, **kwargs)
  File "/Users/wdumm/Downloads/gctree/gctree/branching_processes.py", line 1631, in _mle_helper
    grad_check = sco.check_grad(lambda x: f(x)[0], lambda x: f(x)[1], x_0)
  File "/Users/wdumm/Downloads/gctreetestenv/lib/python3.9/site-packages/scipy/optimize/_optimize.py", line 1178, in check_grad
    analytical_grad = grad(x0, *args)
  File "/Users/wdumm/Downloads/gctree/gctree/branching_processes.py", line 1631, in <lambda>
    grad_check = sco.check_grad(lambda x: f(x)[0], lambda x: f(x)[1], x_0)
  File "/Users/wdumm/Downloads/gctree/gctree/branching_processes.py", line 1629, in f
    return tuple(-y for y in ll(*x, **kwargs))
  File "/usr/local/Cellar/python@3.9/3.9.18_2/Frameworks/Python.framework/Versions/3.9/lib/python3.9/contextlib.py", line 79, in inner
    return func(*args, **kwds)
  File "/Users/wdumm/Downloads/gctree/gctree/branching_processes.py", line 1104, in ll
    return (-np.log(count_ls.sum()) + scs.logsumexp(ls, b=count_ls)), np.array(
TypeError: loop of ufunc does not support argument 0 of type int which has no callable log method

This is because numpy.log doesn't work on integer arguments greater than 2**64. Luckily in this case we're always just calling log on an integer (not arrays) so the fix is simply to use math.log.