Conversation
Moves `MicrocalibrateAdapter` / `MicrocalibrateAdapterConfig` up from
`microplex-us` so every country package (microplex-us, a future
microplex-uk, etc.) shares one identity-preserving gradient-descent
chi-squared calibrator instead of duplicating the glue. The adapter is
genuinely country-agnostic — it wraps `microcalibrate.Calibration` with
the legacy `Calibrator.fit_transform` surface and uses only
`microplex.calibration.LinearConstraint` (no US tax logic).
Structure:
- `src/microplex/calibration.py` becomes a package directory
(`src/microplex/calibration/__init__.py`) so the existing public
namespace (`LinearConstraint`, `Calibrator`, `SparseCalibrator`,
`HardConcreteCalibrator`) continues to import identically. The
adapter slots in as a sibling module
`src/microplex/calibration/microcalibrate_adapter.py`.
- `microplex.calibration.__init__` appends a `try/except ImportError`
re-export of `MicrocalibrateAdapter` and `MicrocalibrateAdapterConfig`.
Callers without the `calibrate` extra get `None` for those names,
which is what `pytest.importorskip` and typical feature-gate code
expect. Callers with the extra get the real classes.
- `pyproject.toml` exposes `calibrate = ["microcalibrate>=0.22; python_version >= '3.13'"]`
as an optional extra so microcalibrate's ~1.5 GB torch/optuna/l0
footprint is opt-in, and microplex keeps working on Python 3.11/3.12
(which microcalibrate does not support yet). The `all` extra now
includes `calibrate`.
Default `batch_size = 100_000` on `MicrocalibrateAdapterConfig` keeps
peak autograd activation under ~200 MB at ~500 constraints (PR #99
gradient-accumulation path), so the adapter defaults to scale-safe.
TDD: `tests/test_microcalibrate_adapter.py` pins
- import path (`from microplex.calibration import MicrocalibrateAdapter`);
- default `batch_size` is set;
- convergence on a 3-age-band problem within 0.1 max relative error.
Skips cleanly via `pytest.importorskip("microcalibrate")` when the extra
isn't installed.
Downstream microplex-us re-exports from here so existing
`from microplex_us.calibration import MicrocalibrateAdapter` keeps
working (see companion commit on microplex-us).
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
MaxGhenis
added a commit
to CosilicoAI/microplex-us
that referenced
this pull request
Apr 22, 2026
The adapter moved to upstream microplex (see CosilicoAI/microplex#6) so every country package shares one identity-preserving calibrator instead of duplicating the glue. This commit: - Swaps pyproject dependency `microcalibrate>=0.22` for `microplex[calibrate]`, picking up the torch/optuna/l0 stack transitively via the extra. - Deletes `src/microplex_us/calibration/microcalibrate_adapter.py`; the source of truth is now `microplex.calibration.microcalibrate_adapter`. - Rewrites `src/microplex_us/calibration/__init__.py` to re-export the adapter classes from upstream so existing `from microplex_us.calibration import MicrocalibrateAdapter` imports keep working — bit-for-bit backward-compatible for downstream pipelines. All 13 microplex-us calibration tests pass against the re-exported adapter (identical behavior, upstream-hosted implementation). Next: once microplex#6 merges, this PR can merge too; pipelines using MicrocalibrateAdapter get the batched calibration transparently. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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
Moves
MicrocalibrateAdapter/MicrocalibrateAdapterConfigup frommicroplex-usso every country package (microplex-us, a future microplex-uk, etc.) shares one identity-preserving gradient-descent chi-squared calibrator instead of duplicating the glue. The adapter is genuinely country-agnostic — it wrapsmicrocalibrate.Calibrationwith the legacyCalibrator.fit_transformsurface and uses onlymicroplex.calibration.LinearConstraint, no US tax logic.Structure
src/microplex/calibration.py→ package directory (src/microplex/calibration/__init__.py). Existing public surface (LinearConstraint,Calibrator,SparseCalibrator,HardConcreteCalibrator) imports identically.src/microplex/calibration/microcalibrate_adapter.pycarries the adapter.calibration/__init__.pyappends atry/except ImportErrorre-export so callers without thecalibrateextra getNonefor the adapter names and callers with the extra get real classes.pyproject.tomlgainscalibrate = ["microcalibrate>=0.22; python_version >= '3.13'"]— opt-in because microcalibrate pulls ~1.5 GB of torch/optuna/l0 and pins Python 3.13+ while microplex supports 3.11+.batch_size = 100_000keeps autograd activation under ~200 MB at ~500 constraints (see Add batch_size gradient accumulation; release pandas matrix after init PolicyEngine/microcalibrate#99).Test plan
pytest tests/test_calibration.py tests/test_microcalibrate_adapter.py— 36 passed.tests/test_microcalibrate_adapter.pypins: importability from canonical path; defaultbatch_sizeset; 3-age-band convergence within 0.1 max relative error. Skips cleanly viapytest.importorskipwhenmicrocalibrateextra not installed.microplex-uscompanion PR re-exports from here. Verified locally (pytest tests/calibration/→ 13 pass).Rationale
Max asked: "but won't all countries use microcalibrate?" Yes. Relocating now keeps the architecture properly modular at every step and avoids a later second-country duplication.
🤖 Generated with Claude Code