Repository navigation
Add SAPS nonconformity score ("saps" score type) - #1376
DarylOkeke wants to merge 1 commit into
Conversation
Test CoverageTotal: 84.54% (37995 / 44944 lines)
|
|
/review |
4 similar comments
|
/review |
|
/review |
|
/review |
|
/review |
|
/review |
There was a problem hiding this comment.
Previous review
AI Review
suggestion: need change
decision: need change
Thank you for this contribution. It adds the SAPS nonconformity score (Huang et al., 2024, Eq. 10) as a new score_type, plus a saps_lambda option on all seven predictors, and the scores module is well tested. The change is close.
Core change: This touches the shared core. BaseConformal, a base class, gets a new constructor parameter that is forwarded in calibrate and forward. The shared scores.py functions that every predictor calls also change. Your defaults keep the existing behavior, but a change like this can't be approved automatically, so a maintainer needs to review it.
The formula, tie-breaking, validation and the RC3P and BaseConformal forwarding all look correct to me. scores.py is at 100% coverage.
To get this merged:
- Per-file coverage is below the 80% bar for several modified files: label.py is at 23%, covariate_label.py at 45%, base_conformal/init.py at 73% and cluster_label.py at 79%. The new saps_lambda forwarding in LABEL, CovariateLabel, ClusterLabel and NeighborhoodLabel has no SAPS test, and LabelShift has none either. Please add a small SAPS test for each, similar to the existing aps tests, checking that the configured lambda reaches both the calibration and inference score calls. Add tests until each modified file reaches at least 80%, or explain why a file can't.
- The docs don't match the code. docs/api/calib/pyhealth.calib.predictionset.rst still describes score_type as only threshold or aps. Please update it to include saps and saps_lambda.
Optional suggestions:
-
Update the predictor docstrings that list score types (RC3P, CovariateLabel, ClusterLabel, NeighborhoodLabel) to mention saps.
-
test_marginal_coverage_saps overlaps with the existing coverage loop, which already includes saps. You could drop it.
-
The TestScoresSAPS docstring contains a process note ("Frozen… agreed with ChatGPT…"). It could be shortened.
-
You could validate saps_lambda in the constructors so bad values fail early. Today they only fail at calibrate or forward time.
-
Warning: this PR can't be approved automatically because it touches the core part of PyHealth (base classes or shared code). To stay on automatic review, back out that part, or ask a maintainer to review it, then comment
/review. If the next review finds this again, the PR switches tostatus: manual.
Reviewed commit d866a36
d866a36 to
bd9cb36
Compare
|
/review |
| >>> all_class_nc_scores( | ||
| ... np.array([[0.7, 0.2, 0.1]]), score_type="saps", | ||
| ... randomize=False, saps_lambda=0.1) | ||
| array([[0.7, 0.8, 0.9]]) |
There was a problem hiding this comment.
[medium] SAPS doctest expected output does not match what the code prints
The new example in all_class_nc_scores expects array([[0.7, 0.8, 0.9]]). The code computes pi_max + (rank - 2 + u) * lambda in float64, so the second and third entries are 0.7 + 0.1 and 0.7 + 0.2, which print as 0.7999999999999999 and 0.8999999999999999. The example therefore fails under doctest, and this repo uses +SKIP and doctest markers that suggest doctests run. Fix it by rounding like the APS example above it, e.g. np.round(all_class_nc_scores(...), 2), and update the expected output to array([[0.7, 0.8, 0.9]]).
There was a problem hiding this comment.
AI Review
suggestion: approve
decision: human review
Thank you for this contribution. It adds the SAPS nonconformity score (Huang et al., ICML 2024, Eq. 10) as a new score_type="saps", with a saps_lambda parameter forwarded through all seven predictors. The implementation matches the paper's formula and keeps the threshold and APS behavior unchanged. saps_lambda is placed after the existing parameters, so positional calls still work. The docs match the code, and the tests are thorough. They include hand-computed scores, validation, positional compatibility, and spy tests that check saps_lambda reaches both calibration and inference in each predictor.
Core part of the codebase: The change edits the BaseConformal constructor, _compute_nc_scores and forward. It also changes the shared score functions in pyhealth/calib/predictionset/scores.py, which LABEL, RC3P, LabelShift and the cluster and covariate predictors all call. Defaults are unchanged, so I see no behavioral risk. Because base-class and shared-utility code is edited, a maintainer needs to review it instead of it being approved automatically.
Coverage:
- The whole repository is at 84.5%, above the 60% bar.
scores.pyis at 100%.- Several modified files are below 80%:
covariate_label.py(47%),base_conformal/__init__.py(73%),label.py(73%) andcluster_label.py(79%). - That gap comes mostly from existing code outside this change. Your new forwarding lines in those files are exercised by the new tests.
Optional suggestions:
- The new
saps_lambdais only validated when scores are computed. Validating it in each constructor, whenscore_type="saps", would make a bad value fail earlier. - The doctest in
all_class_nc_scoresexpectsarray([[0.7, 0.8, 0.9]]). NumPy's default 8-digit printing should render that correctly, but please confirm it passes. - Some
random_statedocstrings (for example inNeighborhoodLabelandCovariateLabel) still mention onlyscore_type="aps". You could update them to include"saps".
The change looks ready to merge from my side.
- Switched to
status: manual: it touches the core part of PyHealth (base classes or shared code), again after an earlier warning, so a maintainer will review this PR.
Reviewed commit bd9cb36
Implements the SAPS (Sorted Adaptive Prediction Sets, Huang et al., ICML 2024, Eq. 10) nonconformity score as a new score_type in the shared score module.
What changed:
pyhealth/calib/predictionset/scores.py: added"saps"toSUPPORTED_SCORE_TYPESwith a clean-room implementation of Eq. 10. Rank-1 classes scoreu * pi_max; lower ranks scorepi_max + (rank - 2 + u) * lambda, whereu ~ Uniform(0,1)is drawn once per row (or 1 whenrandomize=False) and ties are broken with the same infinitesimal random perturbation as APS. Only the max probability is used, so tail probabilities are discarded. New optionalsaps_lambdaargument (default 0.1, validated non-negative and finite) on all four public score functions; it is ignored by the other score types.saps_lambdaconstructor parameter added to all seven predictors (BaseConformal, LABEL, RC3P, CovariateLabel, ClusterLabel, NeighborhoodLabel, LabelShift), placed after the pre-existing parameters so positional constructor calls keep working, and forwarded through calibration and inference.tests/core/test_scores.pycovering hand-computed scores, tail invariance, nested prefixes, the lambda=0 boundary, validation, coverage, reproducibility, APS isolation, predictor-level forwarding (BaseConformal threshold monotonicity, RC3P calibration+inference, plus per-predictor calibration+inference spy tests for LABEL, ClusterLabel, CovariateLabel, NeighborhoodLabel, and LabelShift), and positional-argument compatibility. 34 tests pass intest_scores.py; 128 tests pass across the six affected conformal test modules.Update (2026-11-16): rebased onto upstream master 0e691f8 and addressed the repo AI bot review (per-predictor forwarding tests, docs update, duplicate coverage test removed).
Notes: the 0.1 default for
saps_lambdais a convenience default, not a tuned value; the paper tunes lambda on a validation split.