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

Convert ndarray to scalar to avoid deprecation warning #7375

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

Prtm2110
Copy link

@Prtm2110 Prtm2110 commented Jun 19, 2024

Description

This PR fixes a Numpy deprecation warning by converting ndarray values to scalars in the record method.

Changes-
Updated record method to convert ndarray to scalar using val.item() before assignment.
Reason-
Prevents future errors due to deprecated implicit array-to-scalar conversions in Numpy.

Related Issue

Checklist

Type of change

  • New feature / enhancement
  • Bug fix
  • Documentation
  • Maintenance
  • Other (please specify):

📚 Documentation preview 📚: https://pymc--7375.org.readthedocs.build/en/7375/

Copy link

welcome bot commented Jun 19, 2024

Thank You Banner]
💖 Thanks for opening this pull request! 💖 The PyMC community really appreciates your time and effort to contribute to the project. Please make sure you have read our Contributing Guidelines and filled in our pull request template to the best of your ability.

@@ -115,6 +115,8 @@ def record(self, point, sampler_stats=None) -> None:
if sampler_stats is not None:
for data, vars in zip(self._stats, sampler_stats):
for key, val in vars.items():
if isinstance(val, np.ndarray) and val.ndim > 0:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the val always a single number?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, currently val is converted to a scalar only when it is a single-element ndarray
I think the correct implementation would be

if isinstance(val, np.ndarray):
    # Extract the first element using indexing (preserves order)
    val = val.flat[0]  
data[key][self.draw_idx] = val

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure these records are supposed to always be single elements. If not, discarding the rest doesn't make sense

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.

Numpy deprecation warning issued from within test_metropolis.py
2 participants