Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
docs: show that numerically equal values can score below 1.0
The text-comparison warning only showed one direction, wrong values scoring
high. The opposite is likelier to bite:

    5                  vs 5.0                -> 0.0000
    1000               vs 1000.0             -> 0.6667
    Decimal("10.50")   vs Decimal("10.5")    -> 0.8000

A ground truth loaded from a database as an integer, against a prediction parsed
from JSON as a float, is a perfect extraction scored as a miss. Two identical
numbers scoring 0.0 is the kind of thing a reader should not have to derive from
"compared character by character".

Review raised this as a blocker and it is being answered as documented behaviour
rather than fixed, so the documentation has to actually document it, and the
remedy has to be named: declare the field with NumericComparator.

Five tests, including one asserting the named remedy works, so the docs cannot
recommend something untrue.

2105 passed, ruff clean, mkdocs warnings unchanged.
  • Loading branch information
sromoam committed Sep 3, 2026
commit e55270d35ab80a5d7cade0dcced0e9096a9f3310
15 changes: 15 additions & 0 deletions docs/docs/Guides/Comparators/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,21 @@ incidental character overlap.
A wrong account number, a date off by a day and a **2x-wrong amount** all score
above the one row that is a genuine near-miss.

It fails in the other direction too. Values that are *numerically equal* score
below 1.0, or not at all, because their text differs:

| ground truth | prediction | score |
|---|---|---|
| `5` | `5.0` | 0.0000 |
| `1000` | `1000.0` | 0.6667 |
| `Decimal("10.50")` | `Decimal("10.5")` | 0.8000 |

This is easy to hit by accident: a ground truth loaded from a database as an
integer, against a prediction parsed from JSON as a float, is a perfect
extraction scored as a miss. Declaring the field with
[`NumericComparator`](#numericcomparator) compares the numbers instead of their
spelling.

Lowering `leaf_threshold` will not separate these: a cutoff high enough to
reject the account number also removes the partial credit you wanted. **If a
value's correctness matters, declare that field** so it gets a comparator
Expand Down
32 changes: 32 additions & 0 deletions tests/comparators/test_anls_leaf_semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,3 +272,35 @@ def test_a_json_type_does_not_warn(self):
assert ANLSStarComparator().compare(
{"v": Decimal("1.5")}, {"v": Decimal("1.5")}
) == pytest.approx(1.0)


class TestNumericallyEqualValuesCanScoreBelowOne:
"""The other direction of uniform text leaves, and the likelier one to bite.

A ground truth loaded from a database as an integer against a prediction
parsed from JSON as a float is a perfect extraction, and text comparison
scores it as a miss. Pinned as documented behaviour so nobody "fixes" it into
the type-aware leaves that were deliberately reverted; see the FUTURE note in
`ANLSLeaf.nls_list`.
"""

@pytest.mark.parametrize(
"gt,pred,expected",
[
(5, 5.0, 0.0),
(0, 0.0, 0.0),
(1000, 1000.0, 2 / 3),
(Decimal("10.50"), Decimal("10.5"), 0.8),
],
)
def test_equal_numbers_of_different_spelling(self, gt, pred, expected):
assert ANLSStarComparator().compare({"v": gt}, {"v": pred}) == pytest.approx(
expected, abs=1e-4
)

def test_the_documented_remedy_actually_works(self):
"""The docs point at NumericComparator, so check that it does the job."""
from stickler.comparators.numeric import NumericComparator

assert NumericComparator().compare(5, 5.0) == pytest.approx(1.0)
assert NumericComparator().compare(1000, 1000.0) == pytest.approx(1.0)