Skip to content

Unify compare() and compare_with() on one scoring implementation #392

Open

Description

@sromoam

compare() and compare_with() are two separate implementations of "score this field", and neither calls the other:

  • compare_with() goes through ComparisonDispatcher.dispatch_field_comparison, which builds the full result: counts, thresholds and reports.
  • compare() goes through StructuredModel.compare and ComparisonHelper.compare_field_raw, a hand-written second copy. It exists for speed: it is the cost function for every cell of a Hungarian cost matrix, so it skips building a result.

Every scoring rule therefore has to be written twice, and the copies drift whenever a fix lands in only one. Each time, the two readers disagree about the same pair, and because compare() feeds list matching, a list can pair items on one rule and classify them on the other. That's the contradiction #233 rules out.

Fixes that had to land in both copies so far:

Each of those code paths carries a comment saying it must agree with the dispatcher.

Disagreements still open:

Proposal. Have one implementation. compare() should return the dispatcher's raw score for the pair, without thresholds or reports, rather than recomputing it. The speed advantage should come from not building reports (skipping the confusion matrix, non-matches and field comparisons), not from a second copy of the rules. Once that's done, a new rule has exactly one place to go, and a test that runs every field shape through both readers and asserts they agree would keep it that way.

What to measure first:

  • Cost: compare() runs once per cell (60x60 objects of 20 fields is 72,000 field calls), so the shared path needs a benchmark against today's compare() before and after.
  • Score changes: the unified behaviour has to pick one rule where the two readers differ today. Converge compare() and compare_with() on absent-on-both (1.1.0) #357 is the known case, so this changes some compare() scores and some list pairings, and needs a CHANGELOG entry marked as breaking.

This would subsume #357 and close the reader-disagreement half of #391.

Activity

  1. added this to the 1.1.0 milestone on Oct 15, 2026
  2. added
    enhancement
    on Oct 15, 2026
  3. modified the milestones: 1.1.0, 1.2.0 on Nov 23, 2026
  4. sromoam commented on Nov 24, 2026

    @sromoam
    ContributorAuthor

    Folded in from #357, which is now closed. It is the known case where the unified scorer has to pick one rule.

    • compare() excludes fields absent on both sides from its mean, and compare_with() credits them at 1.0. A nested model's score in field_scores comes from compare(), so both rules show up in one result. This is still true on dev at 57f9f3b: in Converge compare() and compare_with() on absent-on-both (1.1.0) #357's Doc/Addr example, addr is 0.0 in the parent's field_scores and 0.6667 on its own.
    • Applying the convergence to comparison_engine's percolation loop failed 23 tests, in test_simple_list_in_structured_list.py and tests/auto/test_risk_surface.py. Several of them are the guards that assert compare_with() credits absent-on-both. Either direction changes reported scores on sparse data, so it is breaking.
    • Recommended direction: make compare_with() exclude, matching compare(), precision, recall and F1, and leave accuracy as the metric that counts correct absence. A perfect extraction on a sparse document still scores 1.0; what drops is a wrong extraction on a sparse document. The alternative, making compare() credit, is cheaper in tests but makes the Hungarian cost matrix less discriminating.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancement

    Type

    No type

    Projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions