Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ufunc attribute error from numpy.log #125

Merged
merged 5 commits into from
Mar 6, 2024
Merged

Fix ufunc attribute error from numpy.log #125

merged 5 commits into from
Mar 6, 2024

Conversation

willdumm
Copy link
Contributor

@willdumm willdumm commented Mar 6, 2024

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/[email protected]/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.

@willdumm willdumm merged commit ff0890c into main Mar 6, 2024
9 checks passed
@willdumm willdumm deleted the wd-duncan-fix branch March 6, 2024 21:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant