Skip to content
Merged
Show file tree
Hide file tree
Changes from 9 commits
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
136 changes: 86 additions & 50 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,27 +141,6 @@ Each release links to full notes on the
`match_threshold=` explicitly still overrides the declaration, and a class that
declares nothing is unaffected.

- JSON Schema import now delegates standard types, local references, combiners,
and constraint parsing to `json-schema-to-pydantic`. Stickler retains a narrow
adapter for comparison metadata and nested `StructuredModel` creation. Parsed
types still choose comparison behavior, but schema constraints do not reject
imperfect predictions before scoring: malformed enum, date, and
constraint-violating values remain constructible and reach their comparator.

Unconfigured enum fields now use `ExactComparator` at threshold `1.0`, and
`date` / `date-time` formats use `DateComparator` at threshold `1.0`; both used
`LevenshteinComparator` at threshold `0.5` before this change, so default scores
can move for those fields.
This adds support for valid Draft 7 multi-type unions and multi-arm `allOf`,
`anyOf`, and `oneOf` schemas; recursive models and `patternProperties` remain
explicit unsupported boundaries ([#212](https://github.com/awslabs/stickler/issues/212)).

- Confidence AUROC and document-splitting statistics now use NumPy
implementations with randomized scikit-learn and SciPy equivalence tests.
`scikit-learn` is no longer a core dependency, and the `docsplit` extra now
adds only pandas; SciPy remains isolated to the `semantic` extra
([#216](https://github.com/awslabs/stickler/issues/216)).

### Fixed

- **Breaking:** `HungarianMatcher.calculate_metrics` no longer reports a paired
Expand Down Expand Up @@ -312,45 +291,48 @@ Each release links to full notes on the
Replacements, all of which already existed: `overall_score` for the scalar
summary, `EvalResult.matched` for a single object-level verdict, and
`field_comparisons` for the individual failures. To ask whether anything failed
at all, both rollup nodes have to be read, because a list item that scores
below `match_threshold` is recorded as one `fd` on `confusion_matrix.overall`
and is not descended into, so its leaves never appear under
`confusion_matrix.aggregate`:
at all, both rollup nodes have to be read, because each is blind to the other's
failures. A list item scoring below `match_threshold` is recorded as one `fd` on
`confusion_matrix.overall` and is not descended into, so its leaves never appear
under `confusion_matrix.aggregate`; conversely a value invented where the ground
truth is null is `fa` at that leaf and rolls into `confusion_matrix.aggregate`,
while `overall` stays clean because the item still paired:

```python
clean = (
cm['aggregate']['fd'] + cm['aggregate']['fn'] == 0
and cm['overall']['fd'] + cm['overall']['fn'] + cm['overall']['fa'] == 0
cm['aggregate']['fp'] + cm['aggregate']['fn'] == 0
and cm['overall']['fp'] + cm['overall']['fn'] == 0
)
```

A below-threshold object is a spurious non-match, so not descending into it is
deliberate: `overall` carries the object verdict and `aggregate` carries leaf
detail for the objects that were comparable. A caller wanting leaf detail for a
marginal object lowers `match_threshold` until it qualifies. See
[#288](https://github.com/awslabs/stickler/issues/288) for the naming.
A below-threshold **list item** is a spurious non-match, so not descending into it
is deliberate: `overall` carries the object verdict and `aggregate` carries leaf
detail for the items that were comparable. A caller wanting leaf detail for a
marginal list item lowers `match_threshold` until it qualifies.

That gating is a property of `List[StructuredModel]` pairing, not of nested
objects generally, and the pages now say so. `StructuredListComparator` pairs
items and then accepts or rejects; a single nested `StructuredModel` field goes
through `FieldComparator`, which has no such stage. So for a nested object field
the leaves are **always** reported on `aggregate`, and the verdict comes from the
field's own `threshold` -- `match_threshold` is never consulted, which made the
published "lower `match_threshold`" remedy a no-op for that shape. Measured on a
three-leaf nested object with one leaf wrong:

Replacements, all of which already existed: `overall_score` for the scalar
summary, `EvalResult.matched` for a single object-level verdict, and
`field_comparisons` for the individual failures. To ask whether anything failed
at all, both rollup nodes have to be read, because a list item that scores
below `match_threshold` is recorded as one `fd` on `confusion_matrix.overall`
and is not descended into, so its leaves never appear under
`confusion_matrix.aggregate`:

```python
clean = (
cm['aggregate']['fd'] + cm['aggregate']['fn'] == 0
and cm['overall']['fd'] + cm['overall']['fn'] + cm['overall']['fa'] == 0
)
```
field threshold=0.9 overall tp=1 fd=1 aggregate tp=3 fd=1
field threshold=0.5 overall tp=2 fd=0 aggregate tp=3 fd=1
match_threshold 0.9 / 0.7 / 0.5 / 0.1 -> identical at every value
```

A below-threshold object is a spurious non-match, so not descending into it is
deliberate: `overall` carries the object verdict and `aggregate` carries leaf
detail for the objects that were comparable. A caller wanting leaf detail for a
marginal object lowers `match_threshold` until it qualifies. See
[#288](https://github.com/awslabs/stickler/issues/288) for the naming.
Also documented: on a document where **every** subtree was rejected, `aggregate`
stops counting leaves. `AggregateMetricsCalculator` falls back to summing
children's `overall` when the recursive leaf sum is all-zero, so the unit of the
count changes with the data -- two rejected items of six fields report
`aggregate tp=0 fd=2`, two object rows where twelve leaves exist. An `aggregate`
count is therefore not a safe denominator for a leaf total on such a document.

See [#288](https://github.com/awslabs/stickler/issues/288) for the naming.

- An unrecognized `x-aws-stickler-*` extension key in a JSON Schema now raises
instead of being silently dropped. The sharpest shape was a typo beside a
Expand Down Expand Up @@ -523,6 +505,60 @@ Each release links to full notes on the
A test pins the superset property so adding a case to either rule without
widening the guard fails loudly rather than silently skipping the check.

### Documentation

- Documented what the two confusion-matrix rollup nodes answer. `overall`
classifies a node's direct children, so on a list field it reads as a per-item
verdict (was this pairing genuine or spurious) while at the root it classifies
the root's own fields; `aggregate` gives leaf detail for the objects that were
comparable. `match_threshold` is the line between them for a **list item**: an
item below it is a single FD, a spurious non-match, and is not descended into,
so a caller wanting leaf detail for a marginal item lowers `match_threshold`
until the item qualifies as comparable. A single nested `StructuredModel` field
is not gated this way; its leaves are always reported, and the field's own
`threshold` decides its verdict.

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 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.

Older sections of the same pages were brought into line, since a page that
disagrees with itself about this is the defect being fixed. `Calculation Logic`
and `Key Features` in `aggregate-metrics.md` stated the parent-node rule as an
unconditional sum of child `aggregate` values, which is the model the new
warning corrects, and `Field-Level Aggregate Metrics` in
`understanding-results.md` repeated it 350 lines below the corrected bullet --
with a worked snippet that ranked sections by `aggregate` error counts, mixing
leaf rows and object rows in one ranking. That snippet now reports which unit it
got. The "lower `match_threshold` to get a marginal object's leaves" remedy also
survived in `threshold-gated-evaluation.md` and in two headings, where
`test_match_threshold_is_inert_for_this_shape` pins it as a no-op; those now say
**list item**.

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
FP and FN so mAP recall falls, while a below-threshold object is `fd` only, so
`overall` recall reads `1.0` on a document with a spurious pairing unless
`recall_with_fd=True` is passed.

Also documents that `EvalResult.precision`, `.recall`, `.f1` and `.accuracy`
are the object-level metrics, read from `confusion_matrix.overall.derived`, so
`precision` of `1.0` beside an `overall_score` of `0.9667` is two correct
answers to two different questions rather than a contradiction. The naming is
under review for 1.0 in
[#288](https://github.com/awslabs/stickler/issues/288).

## [0.7.0] - 2026-08-18

### Added
Expand Down
118 changes: 114 additions & 4 deletions docs/docs/Advanced/aggregate-metrics.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Stickler automatically includes an `aggregate` field at every node in the confus
## Key Features

- **Automatic** -- Every node gets an `aggregate` field, with no per-field configuration.
- **Hierarchical** -- Parent nodes sum metrics from all child primitive fields.
- **Hierarchical** -- Parent nodes sum metrics from all child primitive fields, except where a list's items were all rejected and the node reports object rows instead ([why](#aggregate-counts-objects-for-an-all-rejected-list)).
- **Consistent** -- The same access pattern works at every level: `result['confusion_matrix']['aggregate']` or `result['confusion_matrix']['fields']['contact']['aggregate']`.
- **Derived metrics included** -- Each aggregate contains precision, recall, F1, and accuracy.

Expand Down Expand Up @@ -79,14 +79,124 @@ 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, 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.

The gating applies to **`List[StructuredModel]` items only**. It is a property of `StructuredListComparator`, which pairs items and then decides which pairs are the same object. A single nested `StructuredModel` field goes through `FieldComparator`, which has no such stage, so:

- its leaves are **always** reported on `aggregate`, whether or not the object was rejected;
- its `overall` verdict comes from the **field's own `threshold`**, not from `match_threshold`, which is never consulted for that shape.

The worked output above is that case, and shows it: `contact` scores 0.5 and is `fd=1` on `overall`, and its leaves are still counted (`aggregate tp=1 fd=1`) and still roll into the root. If your schema is nested objects rather than lists of them, `aggregate` is showing you your failing leaves, not hiding them.

This is the same two-stage structure as mean Average Precision, which Stickler also implements for bounding boxes: an IoU threshold decides whether a detection matched, and only matched pairs are evaluated further. See [Bounding Box mAP Metrics](bbox-map-metrics.md#iou-calculation), where a below-threshold detection is likewise a failure at the matching stage rather than a source of per-attribute errors. Nobody expects an unmatched detection to contribute attribute-level accuracy, and the reasoning for objects is the same.

The two paths differ on recall, though. A below-threshold detection counts as both FP and FN, so mAP recall falls; a below-threshold object is an `fd` only, so `overall` recall still reads `1.0` on a document with a spurious pairing. Pass `recall_with_fd=True` to `compare_with()` for the mAP convention.

Five line items of six fields each, one field of one item wrong. That item scores 5/6, clears a 0.7 threshold, and is comparable:

```
overall_score 0.9667

cm['overall'] tp=5 fd=0 P=1.0000 R=1.0000 F1=1.0000
cm['aggregate'] tp=29 fd=1 P=0.9667 R=1.0000 F1=0.9831
```

Five comparable objects and no spurious pairings, which is what `overall` measures. The wrong field is one of 30 leaves, which is what `aggregate` measures. Both numbers are correct for their own question.

Drop the same item below the threshold (two fields wrong, so 4/6) and it becomes a rejected object. The nodes do not converge, they diverge further:

```
overall_score 0.9333

cm['overall'] tp=4 fd=1 P=0.8000 R=1.0000 F1=0.8889
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 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`.

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.
{: #aggregate-counts-objects-for-an-all-rejected-list }

!!! 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
```

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

`match_threshold` controls how much leaf detail you get for a **list item**. It does nothing for a single nested `StructuredModel` field, whose leaves are always reported. Lower it so the object qualifies as comparable, and its leaves are scored individually. Same five items, with the third still at 4/6:

```
match_threshold comparable? overall aggregate
0.70 no tp=4 fd=1 tp=24 fd=0
0.66 yes tp=5 fd=0 tp=28 fd=2
```

At `0.66` the marginal item's six leaves join the other 24, and the two wrong ones finally appear as `fd`.

#### Asking whether anything failed

Because the nodes scope different things, a complete check reads both:

```python
clean = (
cm['aggregate']['fp'] + cm['aggregate']['fn'] == 0
and cm['overall']['fp'] + cm['overall']['fn'] == 0
)
```
Comment on lines +211 to +220

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.

Same snippet and same fa claim as understanding-results.md:149-156 — see the detail there. Both copies need the fix, and this one states it as "overall is also the only node carrying fa", which is the stronger and equally untrue form. A leaf with null ground truth and a populated prediction is fa=1 at the leaf and rolls into aggregate; the root overall shows fa=0.


Sum `fp` rather than `fa + fd`. `FP = FA + FD` by construction, so the two are equivalent today, and reading `fp` cannot go stale if a class is ever added.

Both nodes carry `fa`. A value invented where the ground truth is null is `fa` at that leaf and rolls into `aggregate`, leaving `overall` clean because the item still paired; an item invented wholesale is `fa` on `overall`. A check that names only one node reports clean on a hallucinated value:

```
one item, ground truth total=None, prediction total="INVENTED", five other leaves exact

overall tp=1 fp=0 fn=0 fa=0 fd=0
aggregate tp=5 fp=1 fn=0 fa=1 fd=0
```

The `overall` name predates the aggregate rollup and reads as "the whole document" when it means "a classification of this node's direct children". Renaming is breaking, so it is under consideration for 1.0 in [#288](https://github.com/awslabs/stickler/issues/288).

## Calculation Logic

1. **Leaf nodes** (primitive fields): `aggregate` equals `overall`.
2. **Parent nodes**: `aggregate` is the sum of all child `aggregate` values.
3. **Derived metrics**: Precision, recall, F1, and accuracy are recomputed at each level from the summed counts.
2. **Parent nodes**: `aggregate` is the sum of all child `aggregate` values, unless every one of them is zero, in which case the children's `overall` values are summed instead and the unit of the count becomes the object rather than the leaf ([why](#aggregate-counts-objects-for-an-all-rejected-list)).
3. **Derived metrics**: Precision, recall, F1, and accuracy are recomputed at each level from the summed counts. They inherit whichever unit step 2 produced, so they are not a leaf rate on a node whose list items were all rejected.

## Hierarchical Reporting Example

Expand Down
Loading
Loading