Skip to content

Fix L1 distance to use Manhattan sum - #1998

Merged
janfb merged 2 commits into
sbi-dev:mainfrom
SahilKumar75:fix/l1-manhattan-distance
Sep 21, 2026
Merged

janfb merged 2 commits into
sbi-dev:mainfrom
SahilKumar75:fix/l1-manhattan-distance

Conversation

@SahilKumar75

@SahilKumar75 SahilKumar75 commented Aug 23, 2026 •

Copy link
Copy Markdown
Contributor

What does this PR do?

Changes l1() from the mean of absolute differences to their sum, implementing the standard Manhattan distance.

Updates the docstring and adds a regression test showing that a single differing coordinate contributes a distance of 1, including when additional identical coordinates are present.

Does this close any issues?

Related discussion: #2008

Anything else we should know?

The existing docstring explicitly describes averaging. This change therefore alters existing behavior: for an axis of length n, the new distance is n times the previous distance. The intended convention and compatibility approach need discussion before merging.

Previously reported validation:

  • ruff check sbi/utils/metrics.py tests/metrics_test.py
  • ruff format --check sbi/utils/metrics.py tests/metrics_test.py
  • A direct L1/L2 regression check passed.
  • The full metrics test module could not be collected in the reported environment because pytest_harvest and nflows were unavailable.

No tests were rerun for this description update. The broader checks below remain unverified.

Checklist

  • I have read the contributing guide.
  • uv run pytest -n auto -m "not slow and not gpu" passes.
  • uv run pre-commit run --all-files passes (ruff and formatting).
  • uv run pyright sbi passes.
  • I added or updated tests for the changed behavior.
  • I used Google-style docstrings for new or changed public functions.
  • (If applicable) I reported how long new tests run and marked slow ones with pytest.mark.slow.

@coderabbitai

coderabbitai Bot commented Aug 23, 2026 •

Copy link
Copy Markdown

Review Change StackReview Change Stack

Understand this PR’s impact

Explore downstream dependencies and potential security impact with Blast Radius.

View blast radius →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 1d17863c-d4a3-40b7-93b4-0ac43e308559

📥 Commits

Reviewing files that changed from the base of the PR and between 69df3b4 and 47c1ca7.

📒 Files selected for processing (2)
  • tests/abc_test.py
  • tests/metrics_test.py
🚧 Files skipped from review as they are similar to previous changes (1)
  • tests/metrics_test.py

Included review availability: Your plan provides up to 4 included reviews per hour; 0 remain after this review.


📝 Walkthrough

Walkthrough

The l1 metric now computes non-averaged Manhattan distance by summing absolute differences along the selected axis. Regression tests cover dimensional behavior, and ABC performance scaling accounts for the updated distance.

Changes

Metric distance behavior

Layer / File(s) Summary
Update L1 distance and regression coverage
sbi/utils/metrics.py, tests/metrics_test.py, tests/abc_test.py
l1 now sums absolute differences instead of averaging them. Tests verify the distance across dimensions and update ABC performance epsilon scaling for l1.

Priority: ⬇️ Low

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested reviewers: janfb

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely describes the main change: updating L1 distance to use the Manhattan sum.
Description check ✅ Passed The description includes all required sections, explains the behavior change and compatibility impact, references issue #2008, documents validation status, and includes the checklist. It clearly notes…
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 4 functions across 2 files.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create a new PR

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@SahilKumar75

Copy link
Copy Markdown
Contributor Author

The focused regression change is ready for maintainer review. The test suite coverage and formatting checks are complete.

@BHARATH0153

Copy link
Copy Markdown
Contributor

sum fix looks right but it's a breaking change for existing l1 users — distances will scale with dim now. Consider adding a deprecation warning or changelog note

@janfb

janfb commented Sep 1, 2026

Copy link
Copy Markdown
Contributor

Hi @SahilKumar75 Thanks for the pull request! Please so our guidelines for the usual contribution process: https://sbi.readthedocs.io/en/latest/contributing.html

The usual workflow would be first creating an issue stating the problem and discussing a solution.

Can you please provide more details (as intended in our Pull Request Template): What's the problem here? what is this PR implementing and why?

Thanks

@SahilKumar75

Copy link
Copy Markdown
Contributor Author

I opened #2008 to discuss the intended metric behavior and compatibility approach before changing the numerical convention.

@codecov

codecov Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.88%. Comparing base (900821d) to head (47c1ca7).
⚠️ Report is 16 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1998      +/-   ##
==========================================
- Coverage   89.43%   88.88%   -0.55%     
==========================================
  Files         140      141       +1     
  Lines       14312    16612    +2300     
==========================================
+ Hits        12800    14766    +1966     
- Misses       1512     1846     +334     
Flag Coverage Δ
fast 84.37% <100.00%> (?)

Flags with carried forward coverage won't be shown. Click here to find out more.

Files with missing lines Coverage Δ
sbi/utils/metrics.py 87.34% <100.00%> (ø)

... and 26 files with indirect coverage changes

l1 now sums over dimensions, so the acceptance threshold in
test_mcabc_performance needs one more factor of num_dim than the L2-norm
distance. Without it the two-dimensional l1 case accepts a quarter of the
simulations and the c2st check fails.

Drop the l2 assertion from the new metrics test, which covers unchanged
code, and check instead that differing coordinates add up.

@janfb janfb left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for fixing this @SahilKumar75 !

This is a breaking change in principle, but in practice TARP, SMCABC and MCABC are not affected in their default settings. Still, we will mention this in the changelog for the next release.

Added a small fix for the tests: the eps needs scaling with d now that it's not averaged out.

Otherwise good to do! 👏

@janfb
janfb merged commit 807d5ec into sbi-dev:main Sep 21, 2026
18 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants