pymc
pymc copied to clipboard
Add dims/coords consistency validation before sampling (issue #7891)
This commit implements validation of InferenceData dimensions and coordinates consistency before pm.sample() executes, preventing cryptic shape mismatch errors during sampling.
Changes:
-
Add pymc/model/validation.py with validation functions:
- validate_dims_coords_consistency(): Main validation entry point
- check_dims_exist(): Verify referenced dims exist in model.coords
- check_shape_dims_match(): Verify variable shapes match declared dims
- check_coord_lengths(): Verify coordinate lengths match dimension sizes
-
Integrate validation into pymc/sampling/mcmc.py:
- Added validation call early in sample() function, before sampling setup
- Provides clear, actionable error messages to guide users
-
Add comprehensive tests in tests/model/test_dims_coords_validation.py:
- Test missing coord detection
- Test shape-dims mismatch detection
- Test coordinate length validation
- Test MutableData, observed data, and Deterministic variables
- Test edge cases and complex models
Fixes #7891
Description
Related Issue
- [ ] Closes #
- [ ] Related to #
Checklist
- [ ] Checked that the pre-commit linting/style checks pass
- [ ] Included tests that prove the fix is effective or that the new feature works
- [ ] Added necessary documentation (docstrings and/or example notebooks)
- [ ] If you are a pro: each commit corresponds to a relevant logical change
Type of change
- [ ] New feature / enhancement
- [ ] Bug fix
- [ ] Documentation
- [ ] Maintenance
- [ ] Other (please specify):
📚 Documentation preview 📚: https://pymc--7943.org.readthedocs.build/en/7943/