Add validation to _data_setter for non-standard data node names#847
Open
drbenvincent wants to merge 2 commits into
Open
Add validation to _data_setter for non-standard data node names#847drbenvincent wants to merge 2 commits into
drbenvincent wants to merge 2 commits into
Conversation
Add _predictor_data_name and _target_data_name class attributes to PyMCModel so subclasses using non-default data node names can customize without re-implementing _data_setter. Validate at predict time and raise a clear ValueError if expected nodes are missing. Also fix pre-existing mypy type: ignore codes in panel_regression.py (attr-defined -> union-attr). Made-with: Cursor
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #847 +/- ##
==========================================
- Coverage 94.44% 94.44% -0.01%
==========================================
Files 80 80
Lines 12707 12754 +47
Branches 770 773 +3
==========================================
+ Hits 12001 12045 +44
- Misses 498 500 +2
- Partials 208 209 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Remove _predictor_data_name / _target_data_name class attributes (added complexity for a case no existing subclass needs). Keep the validation that raises a clear ValueError when expected data nodes are missing. Revert undeclared y_dtype behavioral change. Improve test coverage with separate X-missing and y-missing error paths. Made-with: Cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
PyMCModel._data_setter()hard-codes"X"and"y"as data node names. If a subclass uses different names, it silently misbehaves or throws a cryptic PyMC error. This PR adds validation that raises a clearValueErrorwith actionable guidance when expected data nodes are missing.Fixes #663
Changes
causalpy/pymc_models.py:_data_setter()that checks"X"and"y"exist inself.named_varsbefore proceedingValueErrorwith guidance to override_data_setter()when nodes are missingpm.set_datacall is unchangedcausalpy/experiments/panel_regression.py:type: ignorecodes (attr-defined→union-attr)causalpy/tests/test_pymc_models.py:NonStandardDataModeltest subclass using renamed data nodesTestDataSetterValidationwith 2 tests:test_mismatched_X_raises: verifies clear error when"X"is renamedtest_missing_y_raises: verifies clear error when only"y"is renamedDesign rationale
The issue proposed 5 possible directions. After examining all existing subclasses, I found that:
LinearRegression,WeightedSumFitter,SoftmaxWeightedSumFitter) use"X"and"y"— they work as-isBayesianBasisExpansionTimeSeries) already override_data_setterentirelyStateSpaceTimeSeries,InstrumentalVariableRegression,PropensityScore) overridepredictorfitentirelySo the override contract already works for all existing subclasses. The missing piece was validation: a subclass that forgets to override
_data_setterpreviously got a cryptic error. Now it gets a clearValueErrorpointing to the solution.I deliberately kept this minimal — no new class attributes or API surface — because the name-remapping-only case doesn't arise in any existing subclass and would add complexity for a speculative benefit.
Testing
test_pymc_models.pypassprek run --all-filescleanChecklist