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(288): read the node whose children you mean
Addresses Vincil's second review of #295. All six reproduced; every number
he published matched.

The findings share one root: every example in the section was a model whose
ONLY field is the list, and `overall` sums a node's DIRECT children -- which
this PR itself now states. Add the header fields a real invoice has and the
object-verdict framing stops holding at the root.

  5 items, everything correct
    root  overall                     tp=8      3 header leaves + 5 pairings
    cm['fields']['lines']['overall']  tp=5      the item count

So "How many list items did the model find? -> overall" overstates by the
number of header fields. Corrected in the lookup row, in the framing on both
pages, in classification-logic.md, and in both docstrings -- which matter
most, since they publish to the API reference where no surrounding
line-item context signals that the claim was list-scoped. Added a worked
example with header fields so the mixing is visible rather than described.

The all-rejected warning was scoped to the document and needed to be scoped
to the list. The fallback fires per node whenever THAT node's leaf sum is
all-zero, so with three correct header fields beside a fully rejected list
the document is plainly not all-rejected and the root still reads

    root  overall    tp=3 fd=2
    root  aggregate  tp=3 fd=2    cm_precision 0.6000

five rows where fifteen leaves exist. A reader checking the document
condition concluded correctly that it did not apply and divided by 15
anyway, and the closing advice to read `overall` did not help either since
it reports the same numbers. The condition that actually holds is
`tp == 0` on the list field's own `overall`, which is what the warning now
says.

That same shape falsifies "the two nodes coincide only where there is no
accepted subtree to expand". They coincide there WITH an accepted subtree
present. The rule was stated in three places -- the page, the CHANGELOG,
and this test file's own module docstring -- and all three are corrected,
with `TestCoincidingNodesAreNotEvidenceOfAgreement` pinning the
counter-example, since it is exactly the kind of claim that file exists to
keep honest.

Also folded in: the `aggregate` reference bullet now links to the caveat
rather than promising a unit it does not always have; aggregate-metrics.md's
older "reflects this node's own direct classification" line agreed with
neither the engine nor the new section four lines below it; and the
repo-wide clean-check scan read a fixed 8-line window past the closing
paren, so an ordinary assertion written after a corrected block would have
failed this test in another file for an unrelated reason. It now stops when
the parentheses balance, verified still to fail on the retired form.

9 new tests, 33 in the file. 2355 pass, ruff clean, mkdocs at the 9-warning
dev baseline.

Refs #288
  • Loading branch information
sromoam committed Sep 8, 2026
commit 3477a6901218d8850b25bdae38dc10f84243eeba
16 changes: 11 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,11 +517,17 @@ Each release links to full notes on the
Both nodes were previously described only mechanically ("this node's own direct
classification" / "sums all primitive-field classifications beneath"), which
said nothing about which to read for which question, or that they diverge on
any model with nesting. They coincide only where there is no accepted subtree
to expand: a flat model, or a document in which *every* subtree was rejected.
One rejected subtree among several makes them diverge further, not converge,
because `aggregate` then reports a flawless precision over the accepted items
only.
any model with nesting. They coincide wherever the node being read has no
accepted subtree left to expand: a flat model, or one whose every nested subtree
was rejected. One rejected subtree among several usually makes them diverge
further rather than converge, because `aggregate` then reports a flawless
precision over the accepted items only.

Coinciding is not evidence that nothing was hidden, and the pages say so. With
three header fields beside a list whose items were all rejected, the root reads
`overall tp=3 fd=2` and `aggregate tp=3 fd=2` -- equal, with an accepted subtree
present and 15 leaves in the document, because the list contributed object rows
to both.

Both pages carry the same two-stage framing as mean Average Precision, and now
also name where the analogy stops: a below-threshold bounding box counts as both
Expand Down
34 changes: 23 additions & 11 deletions docs/docs/Advanced/aggregate-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,15 +79,15 @@ print(cm['fields']['contact']['aggregate'])

Note the difference between `overall` and `aggregate`:

- **`overall`** reflects this node's own direct classification.
- **`overall`** classifies this node's direct children. Where the field is a list, those children are item pairings; at the root they are the root's own fields, so the two units can mix in one count.
- **`aggregate`** sums all primitive-field classifications beneath this node (including itself if it is a leaf).

### Which node answers which question

The two nodes are the two stages of the evaluation:

- **`overall` is detection.** The unit is the object. Did we find the right things? Five line items paired, none spurious.
- **`aggregate` is extraction.** The unit is the leaf. Among the objects established to be the same object, how many field values were correct? 29 of 30.
- **`aggregate` is extraction.** The unit is the leaf, except where noted in the warning below. Among the objects established to be the same object, how many field values were correct? 29 of 30.

`match_threshold` is the handoff, and it is really the definition of "the same object". Above it, the pair is the same thing, so grading its fields is meaningful. Below it, it is not the same thing, so grading its fields would be scoring the fields of a *different* object. Such an **item** is classified as a single false discovery and is not descended into.

Expand Down Expand Up @@ -124,24 +124,36 @@ cm['aggregate'] tp=24 fd=0 P=1.0000 R=1.0000 F1=1.0000

`aggregate` now reports a flawless `P=1.0000` precisely because the rejected item contributes no leaf rows: 24 leaves from the four accepted items, all correct. Reading `aggregate` alone here is the same trap as reading `overall` alone one level up.

The two nodes coincide only where there is no accepted subtree to expand at all: a model with no nesting, or a document in which *every* subtree was rejected. Reject all five items and both nodes read `tp=0 fd=5`.
The two nodes coincide wherever the node being read has no accepted subtree left to expand: a model with no nesting, or one whose every nested subtree was rejected. Reject all five items and both nodes read `tp=0 fd=5`.

!!! warning "On an all-rejected document, `aggregate` counts objects, not leaves"
Coinciding is not evidence that nothing was hidden. Put three header fields beside that list and reject every item, and the root reads `overall tp=3 fd=2` and `aggregate tp=3 fd=2` -- equal, with an accepted subtree present and 15 leaves in the document. They agree because the list contributed object rows to both, not because the leaf view confirmed the object view.

That convergence is not the leaf view agreeing with the object view. When the
recursive leaf sum comes out all-zero, `aggregate` falls back to summing its
children's `overall`, so the **unit of the count changes with the data**:
!!! warning "When a list's items are all rejected, `aggregate` counts objects there, not leaves"

When a node's recursive leaf sum comes out all-zero, `aggregate` falls back to
summing its children's `overall`, so the **unit of the count changes with the
data**. It fires per node, and needs only that one list's items to be rejected
-- not the whole document:

```
two items of six fields
1 of 2 rejected aggregate tp=6 fd=0 P=1.0000 6 leaf rows
2 of 2 rejected aggregate tp=0 fd=2 P=0.0000 2 OBJECT rows, though 12 leaves exist
```

So `aggregate` counts are not a reliable denominator for a leaf total, and a
`derived` block from an all-rejected document is not leaf-level precision. Read
`overall` to know how many objects were rejected before dividing anything by an
`aggregate` count.
That propagates upward. With three correct header fields beside such a list, the
document is plainly **not** all-rejected, and the root still reads:

```
root overall tp=3 fd=2
root aggregate tp=3 fd=2 derived.cm_precision = 0.6000
```

Five rows where fifteen leaves exist. So checking whether the document was
all-rejected does not protect you, and neither does reading the root `overall`,
which reports the same numbers. Before dividing an `aggregate` count by a leaf
total, check each list field's own `overall` for `tp == 0`: that is the condition
under which its `aggregate` stops being a leaf count.

#### Getting leaf detail for a marginal list item

Expand Down
2 changes: 1 addition & 1 deletion docs/docs/Advanced/classification-logic.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ neither term. The mixed-matching example above scores recall `1.000` even though
`0.667`. `HungarianMatcher.calculate_metrics` always uses that second formula.
See [FD and recall](hungarian-matching.md#fd-and-recall).

These are computed at every node of the result tree, from that node's own counts, and which counts those are depends on which node you read. `overall` gives object verdicts at that level: was the pairing genuine or spurious. `aggregate` gives leaf detail, and for a list of objects that means only the items that were comparable, since a list item below the element class's `match_threshold` is one FD and is not descended into. A single nested `StructuredModel` field is not gated this way: its leaves are always reported. See [Which node answers which question](aggregate-metrics.md#which-node-answers-which-question).
These are computed at every node of the result tree, from that node's own counts, and which counts those are depends on which node you read. `overall` classifies that node's direct children: for a list field, whether each pairing was genuine or spurious; at the root, its own fields, which means the two units can mix in a single count. `aggregate` gives leaf detail, and for a list of objects that means only the items that were comparable, since a list item below the element class's `match_threshold` is one FD and is not descended into. A single nested `StructuredModel` field is not gated this way: its leaves are always reported. See [Which node answers which question](aggregate-metrics.md#which-node-answers-which-question).

## Edge Cases

Expand Down
31 changes: 28 additions & 3 deletions docs/docs/Guides/Evaluation/understanding-results.md
Original file line number Diff line number Diff line change
Expand Up @@ -103,13 +103,13 @@ The `confusion_matrix` object has four keys:
- **`overall`** -- Metrics for this node's direct children. Where the field is a list, those children are item pairings rather than leaves.
- **`fields`** -- Field-by-field breakdown, with nested structure for objects and lists.
- **`non_matches`** -- Populated when `document_non_matches=True` (empty otherwise).
- **`aggregate`** -- Primitive field metrics summed recursively below this node, excluding list items rejected at their element class's `match_threshold`. A single nested `StructuredModel` field is not excluded; its leaves are always counted. See [`overall` vs `aggregate`](#overall-vs-aggregate).
- **`aggregate`** -- Primitive field metrics summed recursively below this node, excluding list items rejected at their element class's `match_threshold`. A single nested `StructuredModel` field is not excluded; its leaves are always counted. Where a list's items were *all* rejected the counts become object rows rather than primitive-field metrics, so check that before computing a leaf rate ([why](../../Advanced/aggregate-metrics.md#getting-leaf-detail-for-a-marginal-list-item)). See [`overall` vs `aggregate`](#overall-vs-aggregate).

### `overall` vs `aggregate`

The two nodes are the two stages of the evaluation, and you generally want both:

- **`overall` is detection.** The unit is the object. Did we find the right things? For a list of 5 line items that each paired above `match_threshold`, `tp = 5`.
- **`overall` is detection.** The unit is whatever this node's direct children are. On a list field whose 5 items each paired above `match_threshold`, that field's `overall` reads `tp = 5`. Read it on the list field, not at the root: the root's children are its own fields, so a document with 3 header fields beside that list reads `tp = 8` at the root -- 3 leaves plus 5 pairings, mixing the two units in one number.
- **`aggregate` is extraction.** The unit is the leaf. Among the objects established to be the same object, how many field values were correct? For those same 5 items with 6 fields each, `tp` counts up to 30.

`match_threshold` is the handoff, and it is really the definition of "the same object". Above it, the pair is the same thing, so grading its fields is meaningful. Below it, it is not the same thing, so grading its fields would be scoring the fields of a *different* object. Such an **item** is classified as a single **false discovery** and is not descended into. This gating is a property of `List[StructuredModel]` pairing, not of nested objects generally: a single nested `StructuredModel` field always reports its leaves, and is judged against the field's own `threshold` rather than `match_threshold`.
Expand All @@ -125,6 +125,31 @@ recall_with_fd=False overall tp=4 fd=1 fn=0 R=1.0000
recall_with_fd=True overall tp=4 fd=1 fn=0 R=0.8000
```

### Read the node whose children you mean

`overall` counts a node's direct children, so which node you read decides what the
number means. On a document with fields beside a list:

```python
class Doc(StructuredModel):
invoice_id: str = ComparableField(...)
vendor: str = ComparableField(...)
date: str = ComparableField(...)
lines: List[Line] = ComparableField(...) # Line has 6 leaves
```

Five items, everything correct:

```
cm['overall'] tp=8 3 header leaves + 5 item pairings
cm['fields']['lines']['overall'] tp=5 the item count
```

The root number is not wrong, it is a different question: it classifies the root's
own four fields. But it is not a count of line items, and reading it as one
overstates by the number of header fields. For "how many items did we find", read the
list field's own node.

Two examples make the split concrete.

**A wrong leaf inside a comparable object.** Five line items of six fields each, one field of one item wrong. That item scores 5/6, clears the 0.7 threshold, and is comparable:
Expand Down Expand Up @@ -165,7 +190,7 @@ At `0.66` the marginal item is comparable, so its six leaves join the first item
|---|---|
| Were the objects comparable, and how many were spurious? | `overall` |
| Among comparable objects, which leaves landed? | `aggregate` |
| How many list items did the model find? | `overall` |
| How many list items did the model find? | that list field's own `overall`, e.g. `cm['fields']['lines']['overall']` |
| Did anything at all fail? | both, see below |

Because the two nodes scope different things, a complete "did anything fail" check reads both:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ def compare_with(
Args:
other: Another instance of the same model to compare with
include_confusion_matrix: Whether to include confusion matrix
calculations. `overall` gives object verdicts at this level;
calculations. `overall` classifies this node's direct children,
which at the root are its own fields, so read a list field's own
`overall` for a count of items;
`aggregate` gives leaf detail, and for a LIST ITEM that means
only the items that were comparable, since an item below the
element class's `match_threshold` is a single FD and is not
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1547,8 +1547,11 @@ def compare_with(
other: Another instance of the same model to compare with
include_confusion_matrix: Whether to include confusion matrix
calculations. The result carries two rollup nodes answering
different questions: `overall` gives object verdicts (was the
pairing genuine or spurious), while `aggregate` gives leaf
different questions: `overall` classifies this node's direct
children (for a list field, whether each pairing was genuine or
spurious; at the root, its own fields, so the two units can mix
in one count -- read a list field's own `overall` for a count of
items), while `aggregate` gives leaf
detail for the objects that were comparable. A LIST ITEM below
the element class's `match_threshold` is one FD and is not
descended into, so lowering `match_threshold` is how you get
Expand Down
Loading
Loading