Add MCNNMPanelSolver fit method#31
Conversation
TianyiPeng
left a comment
There was a problem hiding this comment.
Clean, focused PR. The dispatch pattern in fit() is well-designed and the tests are thorough.
What works well
- Clear dispatch logic:
suggest_r > l > cross-validation— intuitive and documented in the docstring. - Smart test strategy: monkeypatching the underlying solve methods verifies dispatch routing without running expensive solvers. Fast and isolated.
- All dispatch paths covered: suggest_r priority, l priority, CV default (K=2), CV with custom K, and missing O error.
- Newline at EOF fixed for MCNNM.py.
Issues
1. Test file placement is inconsistent
The new test is at tests/test_mcnnm_fit.py (repo root), but existing tests live under src/causaltensor/tests/. Either move the new test to match the existing location, or note the intentional separation (e.g., unit tests vs integration tests). As-is, poetry run pytest from root may or may not find both locations depending on pytest config.
2. No return type in fit() docstring
The method returns a Result (from whichever underlying solver is dispatched), but neither the docstring nor type annotations mention the return value. Adding Returns: Result would help discoverability, especially since fit() is meant to be the primary user-facing entry point.
3. Tutorial notebook diffs are large (+4k/-2k lines) and hard to review from patches
The PR description says they were executed via nbclient — I'll trust that. But notebook output cells in the diff make it hard to spot code changes. Consider clearing outputs before committing if the reviewers need to focus on the code changes, and re-running in CI.
Smaller notes
fit()raisesValueError("O must be provided.")whenO is None— good. But other MCNNM methods (e.g.,solve_with_regularizer) also takeOand silently use it. Worth asking whetherOshould be passed at construction time instead (likeSDIDPanelSolver(Z, O)) for API consistency, though that's a bigger design discussion outside this PR's scope.- The
fit()dispatch means passing bothsuggest_randlsilently ignoresl. The docstring says "dispatch order is suggest_r > l > CV" which is correct, but consider logging a warning if both are passed — easy mistake to make.
Overall this is ready with the test-placement question resolved. Nice work.
Generated by Claude Code
Summary
MCNNMPanelSolver.fit(...)as a convenience dispatcher for suggested rank, fixed regularizer, and cross-validation workflows.Closes #19.
Why
The MC-NNM solver already exposed the underlying solve methods, but did not match the
fitstyle used by the other panel solvers. The tutorials also needed to show the new workflow while still making the older API discoverable.Validation
env PYTHONPATH=src poetry run pytest tests/test_mcnnm_fit.pyenv PYTHONPATH=src poetry run pytest src/causaltensor/tests/test_synthetic_class.py::TestSyntheticClass::test_mc src/causaltensor/tests/test_real_class.py::TestRealClass::test_mctutorials/datasets.ipynb,tutorials/Panel Data Example.ipynb, andtutorials/Panel_Data_Example.ipynbvianbclientfrom both repo root andtutorials/working directories, withoutPYTHONPATH.git diff --check