Skip to content

Add SAPS nonconformity score ("saps" score type) - #1376

Open
DarylOkeke wants to merge 1 commit into
sunlabuiuc:masterfrom
DarylOkeke:feat/saps
Open

DarylOkeke wants to merge 1 commit into
sunlabuiuc:masterfrom
DarylOkeke:feat/saps

Conversation

@DarylOkeke

@DarylOkeke DarylOkeke commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

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" to SUPPORTED_SCORE_TYPES with a clean-room implementation of Eq. 10. Rank-1 classes score u * pi_max; lower ranks score pi_max + (rank - 2 + u) * lambda, where u ~ Uniform(0,1) is drawn once per row (or 1 when randomize=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 optional saps_lambda argument (default 0.1, validated non-negative and finite) on all four public score functions; it is ignored by the other score types.
  • saps_lambda constructor 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: 21 new tests in tests/core/test_scores.py covering 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 in test_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_lambda is a convenience default, not a tuned value; the paper tunes lambda on a validation split.

@github-actions

github-actions Bot commented Nov 3, 2026 •

Copy link
Copy Markdown

Test Coverage

Total: 84.54% (37995 / 44944 lines)

Modified file Coverage Lines
pyhealth/calib/predictionset/base_conformal/__init__.py 73.47% 72 / 98
pyhealth/calib/predictionset/cluster/cluster_label.py 79.03% 98 / 124
pyhealth/calib/predictionset/cluster/neighborhood_label.py 95.76% 113 / 118
pyhealth/calib/predictionset/covariate/covariate_label.py 47.42% 92 / 194
pyhealth/calib/predictionset/label.py 73.33% 44 / 60
pyhealth/calib/predictionset/label_shift/label_shift.py 96.39% 80 / 83
pyhealth/calib/predictionset/rc3p.py 97.27% 107 / 110
pyhealth/calib/predictionset/scores.py 100.00% 57 / 57

Workflow run · bd9cb36

@DarylOkeke

Copy link
Copy Markdown
Contributor Author

/review

4 similar comments
@DarylOkeke

Copy link
Copy Markdown
Contributor Author

/review

@DarylOkeke

Copy link
Copy Markdown
Contributor Author

/review

@DarylOkeke

Copy link
Copy Markdown
Contributor Author

/review

@DarylOkeke

Copy link
Copy Markdown
Contributor Author

/review

@solarsys solarsys added the status: automatic The PR will go through automatic review process. label Nov 16, 2026
@solarsys

Copy link
Copy Markdown
Collaborator

/review

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous review

Reviewed commit d866a36

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

  1. 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.
  2. 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 to status: manual.

Reviewed commit d866a36

@DarylOkeke

Copy link
Copy Markdown
Contributor Author

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed commit bd9cb36

>>> 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]])

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[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]]).

@github-actions github-actions Bot added status: manual Pending maintainer's review and removed status: automatic The PR will go through automatic review process. labels Nov 16, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.py is at 100%.
  • Several modified files are below 80%: covariate_label.py (47%), base_conformal/__init__.py (73%), label.py (73%) and cluster_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_lambda is only validated when scores are computed. Validating it in each constructor, when score_type="saps", would make a bad value fail earlier.
  • The doctest in all_class_nc_scores expects array([[0.7, 0.8, 0.9]]). NumPy's default 8-digit printing should render that correctly, but please confirm it passes.
  • Some random_state docstrings (for example in NeighborhoodLabel and CovariateLabel) still mention only score_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

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: manual Pending maintainer's review

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants