-
Notifications
You must be signed in to change notification settings - Fork 430
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
Address numpy deprecation warning #506
Conversation
for more information, see https://pre-commit.ci
Thanks! Can you share a code snippet and all the relevant versions where you're seeing this? I can't reproduce this warning. |
I think the default is to not show DeprecationWarning for users. Try setting import warnings
warnings.simplefilter('always') I see it on anything with blobs. haven't checked multiple versions, I am on python 3.10, numpy 1.26.4 and emcee 3.1.4. Some example snippets: # single blob
sampler = emcee.EnsembleSampler(6, 1, lambda x: (-(x**2), 3))
sampler.run_mcmc(np.random.normal(size=(6, 1)), 20);
# multiple blobs
sampler = emcee.EnsembleSampler(6, 1, lambda x: (-(x**2), (np.random.normal(x), 3)))
sampler.run_mcmc(np.random.normal(size=(6, 1)), 20); |
The problem here is really that there is a bug in the log probability that you're specifying. The log probability should always be a scalar, but in this case it has size - lambda x: (-(x**2), 3)
+ lambda x: (-np.sum(x**2), 3) I'd actually probably rather raise an exception for non-scalar probability, rather than trying to coerce it into a scalar. I expect that this is a fairly small corner case (log probs that have shape That being said, relying on numpy's deprecation for this doesn't seem like a good approach either! Would you be willing to update this PR to explicitly check that the log prob is a scalar? |
Sounds good, I thought (1,) and scalars were exchangeable, I'll fix my code too. Do you have a preferred way to check something is a scalar? |
I guess they used to be interchangeable, but if numpy is deprecating this behavior, it might be best for emcee to follow suit!
This seems sensible to me. Thank you!! |
In scipy we come across this rather more than I'd like, so much jumping through hoops. Anyway for a multivariate function returning a scalar we deal with it as:
It should deal with |
I understood the goal wasn't to deal with |
My comment was from the sidelines, what other projects do in a similar situation, not necessarily what should be done here. |
Thanks @andyfaff!! This is very useful context. It's interesting that scipy has decided to handle the deprecation this way, and it definitely makes me less certain about how we want to navigate this. The original motivation for emcee was to be roughly compatible with scipy's Another more radical approach would be to explicitly sum the log prob result down to a scalar, but that would be a bigger change to the expected interface. In the end I guess I don't have a strong opinion so I'm happy with either raising the error or using the scipy approach that @andyfaff shared above. What do you think @OriolAbril? Thanks both!! |
closing this then |
Thanks, @OriolAbril! And thanks again for tracking this down. I'm happy with where this landed in the end. |
I am seeing:
from these lines, I think this would fix them. Not really sure how to add a test for that or if you'd prefer a diferent approach.