From 07af8aa6bee9302693c8a25a025c3ed4573ee021 Mon Sep 17 00:00:00 2001 From: Spencer Romo Date: Thu, 15 Oct 2026 15:23:26 -0500 Subject: [PATCH 1/7] docs(249): stop promising mean_score for leaves under list parents The _accumulate_field_metrics docstring said mean_score is recorded at every node, "including leaves under list parents". It is not: those leaves carry CM counts only, which the user docs and tests already state. Reword the docstring to match. No behaviour change. Refs #249 (per-leaf scores for list children remain open, post-1.0). --- .../bulk_structured_model_evaluator.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py b/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py index 9d99fab3..d26676ce 100644 --- a/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py +++ b/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py @@ -457,9 +457,11 @@ def _accumulate_field_metrics( """Recursively accumulate field-level CM counts and threshold_applied_score. Walks both ``fields`` (object subtrees) and ``nested_fields`` - (list-of-StructuredModel) so per-field ``mean_score`` is recorded - at every node compare_with emits, including leaves under list - parents. + (list-of-StructuredModel) so CM counts are recorded at every node + compare_with emits. Score sums (for ``mean_score``) are recorded + only where a node carries ``threshold_applied_score``. Leaves under + a list parent carry CM counts only, so their paths get no + ``mean_score`` (see #249). """ for field_name, field_data in fields_dict.items(): if not isinstance(field_data, dict): From 3aa363c3d9f68d4f5751f5c5a08f2167a5e10154 Mon Sep 17 00:00:00 2001 From: Spencer Romo Date: Thu, 15 Oct 2026 15:26:02 -0500 Subject: [PATCH 2/7] docs(338): split the [Plain, NoteSM] row by annotation The element-gate table is headed with a bare Optional[List[Any]], but its [Plain, NoteSM] vs [Plain, Note2SM] row was measured with a declared comparator. Bare, the pair scores 0.0 under the list-wide gate and 0.5 now, because the bare annotation refuses the Plain pair on its own ([Plain] vs [Plain] scores 0.0), not because of the cross-class rule. Split the row into bare and declared cases in comparison_helper.py and the test docstring. The released 1.0.0 CHANGELOG section is left as is; an Unreleased entry corrects it. No behaviour change. Closes #338. --- CHANGELOG.md | 9 +++++++++ .../models/comparison_helper.py | 15 +++++++++------ .../test_nested_plain_basemodel.py | 11 +++++++---- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a37f0f54..8b4fb183 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -108,6 +108,15 @@ Each release links to full notes on the Keyed on the leaf name alone, the first model built in a process silenced every later one carrying the same typo. +- **Documentation:** the 1.0.0 entry "The element gate judges a PAIR, not a + list" overstates its last row. Under the bare `Optional[List[Any]]` heading, + `[Plain, NoteSM]` vs `[Plain, Note2SM]` scores `0.5`, not `1.0`, and scored + `0.0` (not `0.5`) under the list-wide gate. The published values (`1.0`, + `0.5`, `1.0`) hold only with a comparator declared on `f`. The bare `0.5` is + the refused `Plain` pair (`[Plain]` vs `[Plain]` scores `0.0` bare), not the + cross-class rule. Scoring is unchanged + ([#338](https://github.com/awslabs/stickler/issues/338)) + ## [1.0.0] - 2026-09-11 ### Added diff --git a/src/stickler/structured_object_evaluator/models/comparison_helper.py b/src/stickler/structured_object_evaluator/models/comparison_helper.py index 1ffb4e73..404768fc 100644 --- a/src/stickler/structured_object_evaluator/models/comparison_helper.py +++ b/src/stickler/structured_object_evaluator/models/comparison_helper.py @@ -50,16 +50,19 @@ class _ClassGatedComparator(BaseComparator): [{'a':1},{'b':2},{'c':3}] 1.0 1.0 [{'a':1},{'b':2},{'c':3}, Plain('p')] 1.0 0.0 <- fd on all four [NoteSM] vs [Note2SM] 1.0 1.0 - [Plain, NoteSM] vs [Plain, Note2SM] 1.0 0.5 + [Plain, NoteSM] vs [Plain, Note2SM] 1.0 0.0 + same, with a comparator declared on `f` 1.0 0.5 The dict row zeroed three elements that score 1.0 on their own, in the same list, on `dev`, and here the moment the plain model is removed -- and it drew a "holds a mapping" warning on a field whose annotation says nothing about - mappings. The `StructuredModel` row is worse than wrong-looking: it enforces - the cross-class rule that #327 is explicitly holding open, so a user got the - #327 answer or the documented one according to whether an unrelated plain - model shared the list. Both rules now apply to exactly the population the - wrapper was installed for. + mappings. The declared-comparator row is worse than wrong-looking: it + enforces the cross-class rule that #327 is explicitly holding open, so a user + got the #327 answer or the documented one according to whether an unrelated + plain model shared the list. Both rules now apply to exactly the population + the wrapper was installed for. That row now scores 1.0; the bare row scores + 0.5, because the bare annotation still refuses the `Plain` pair on its own + (`[Plain]` vs `[Plain]` scores 0.0), not because of the cross-class rule. A dict element does not reach the gate -- neither because `_holds_a_plain_model` does not look for one, which was the old and only diff --git a/tests/structured_object_evaluator/test_nested_plain_basemodel.py b/tests/structured_object_evaluator/test_nested_plain_basemodel.py index 519e2d76..c1afa618 100644 --- a/tests/structured_object_evaluator/test_nested_plain_basemodel.py +++ b/tests/structured_object_evaluator/test_nested_plain_basemodel.py @@ -1707,14 +1707,17 @@ class TestTheElementGateJudgesAPairNotAList: [{'a':1},{'b':2},{'c':3}, Plain('p')] 1.0 0.0 0.75 ['x','y','z', Plain('p')] 1.0 0.75 0.75 [NoteSM] vs [Note2SM] 1.0 1.0 1.0 - [Plain, NoteSM] vs [Plain, Note2SM] 1.0 0.5 1.0 + [Plain, NoteSM] vs [Plain, Note2SM] 1.0 0.0 0.5 + same, comparator declared on `f` 1.0 0.5 1.0 The string row is the shape the refusal is FOR: one element out of four cannot be scored, and the field loses a quarter. The dict row zeroed three elements that score 1.0 on their own, in the same list, on `dev`, and here the moment - the plain model is removed. The last row enforced the cross-class rule that - #327 is deliberately holding open, so the answer depended on whether an - unrelated plain model shared the list. + the plain model is removed. The declared row enforced the cross-class rule + that #327 is deliberately holding open, so the answer depended on whether an + unrelated plain model shared the list. The bare row's 0.5 is the refused + `Plain` pair (`[Plain]` vs `[Plain]` scores 0.0 bare), not that rule, which is + why `test_two_structured_model_classes_keep_the_declared_gap` uses `Declared`. """ class Plain(BaseModel): From 12135c65215db6379806f70d93680d02b6df2e62 Mon Sep 17 00:00:00 2001 From: Spencer Romo Date: Thu, 15 Oct 2026 16:33:10 -0500 Subject: [PATCH 3/7] docs(249,338): fix claims the first two commits left wrong - `_accumulate_field_metrics`: list-of-StructuredModel element fields arrive under `fields`, not `nested_fields`; compare_with no longer emits `nested_fields` (its emitter went in ad19ac8). Note that only a finite score is summed. - bulk-evaluation.md and understanding-results.md said compare_with "only emits the score at the list parent". An object node inside a list element is scored too (`items.addr` gets a mean_score); only the leaves are not. - CHANGELOG: move the #338 correction from `### Fixed` with a `**Documentation:**` prefix to a `### Documentation` section, the heading earlier releases use. Refs #249, #338 --- CHANGELOG.md | 14 ++++++++------ docs/docs/Guides/Evaluation/bulk-evaluation.md | 2 +- .../Guides/Evaluation/understanding-results.md | 2 +- .../bulk_structured_model_evaluator.py | 13 +++++++------ 4 files changed, 17 insertions(+), 14 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8b4fb183..4e0230b9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -108,13 +108,15 @@ Each release links to full notes on the Keyed on the leaf name alone, the first model built in a process silenced every later one carrying the same typo. -- **Documentation:** the 1.0.0 entry "The element gate judges a PAIR, not a - list" overstates its last row. Under the bare `Optional[List[Any]]` heading, +### Documentation + +- Corrected the last row of the 1.0.0 table under "The element gate judges a + PAIR, not a list". Under its bare `Optional[List[Any]]` heading, `[Plain, NoteSM]` vs `[Plain, Note2SM]` scores `0.5`, not `1.0`, and scored - `0.0` (not `0.5`) under the list-wide gate. The published values (`1.0`, - `0.5`, `1.0`) hold only with a comparator declared on `f`. The bare `0.5` is - the refused `Plain` pair (`[Plain]` vs `[Plain]` scores `0.0` bare), not the - cross-class rule. Scoring is unchanged + `0.0`, not `0.5`, under the list-wide gate. The published `1.0`, `0.5`, `1.0` + hold only with a comparator declared on `f`. The bare `0.5` is the refused + `Plain` pair (`[Plain]` vs `[Plain]` scores `0.0` bare), not the cross-class + rule. Scoring is unchanged ([#338](https://github.com/awslabs/stickler/issues/338)) ## [1.0.0] - 2026-09-11 diff --git a/docs/docs/Guides/Evaluation/bulk-evaluation.md b/docs/docs/Guides/Evaluation/bulk-evaluation.md index 68ee2c8a..6f0335f7 100644 --- a/docs/docs/Guides/Evaluation/bulk-evaluation.md +++ b/docs/docs/Guides/Evaluation/bulk-evaluation.md @@ -139,7 +139,7 @@ Because each document's `overall_score = Σ_f(score × weight) / Σ_f(weight)` a The denominator is the count of documents whose `overall_score` was a finite number: error docs (`update()` raised) and successful docs carrying a non-finite or missing `overall_score` are both excluded. With zero eligible documents the score is `0.0` — disambiguate via `document_count` when that matters. -Per-field `mean_score` is reported at every nested path (leaf and object/list aggregate) where `threshold_applied_score` was observed in at least one document, averaging over just those documents. Paths with confusion-matrix counts but no score data (e.g., leaves inside `List[StructuredModel]`, where `compare_with()` only emits the score at the list parent) are surfaced without a `mean_score` key rather than reported as `0.0`. +Per-field `mean_score` is reported at every nested path (leaf and object/list aggregate) where `threshold_applied_score` was observed in at least one document, averaging over just those documents. Paths with confusion-matrix counts but no score data (e.g., leaves inside `List[StructuredModel]`, which `compare_with()` emits without a score) are surfaced without a `mean_score` key rather than reported as `0.0`. --- diff --git a/docs/docs/Guides/Evaluation/understanding-results.md b/docs/docs/Guides/Evaluation/understanding-results.md index 1969e535..eb0f1157 100644 --- a/docs/docs/Guides/Evaluation/understanding-results.md +++ b/docs/docs/Guides/Evaluation/understanding-results.md @@ -402,7 +402,7 @@ This is useful for comprehensive auditing of all comparisons, not just failures. The per-document `overall_score` above is a weighted average of field scores. When you aggregate across many documents with `BulkStructuredModelEvaluator.compute()`, the weight information is preserved via two keys: - **`metrics["weighted_overall_score"]`** (float) -- Arithmetic mean of each document's `overall_score`. Prefer this over `cm_f1` whenever your schema uses non-uniform `ComparableField(weight=...)` values: `cm_f1` treats every field-match equally, while `weighted_overall_score` preserves the declared per-field weights across the dataset. -- **`field_metrics[path]["mean_score"]`** (float, when present) -- Arithmetic mean of each document's `threshold_applied_score` at that path, reported at every nested node that was actually scored. Paths with confusion-matrix counts but no score data (e.g., leaves inside `List[StructuredModel]`, where `compare_with()` only emits the score at the list parent) are surfaced without a `mean_score` key rather than as `0.0`. +- **`field_metrics[path]["mean_score"]`** (float, when present) -- Arithmetic mean of each document's `threshold_applied_score` at that path, reported at every nested node that was actually scored. Paths with confusion-matrix counts but no score data (e.g., leaves inside `List[StructuredModel]`, which `compare_with()` emits without a score) are surfaced without a `mean_score` key rather than as `0.0`. ```python from stickler.structured_object_evaluator.bulk_structured_model_evaluator import ( diff --git a/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py b/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py index d26676ce..b649f655 100644 --- a/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py +++ b/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py @@ -456,12 +456,13 @@ def _accumulate_field_metrics( ) -> None: """Recursively accumulate field-level CM counts and threshold_applied_score. - Walks both ``fields`` (object subtrees) and ``nested_fields`` - (list-of-StructuredModel) so CM counts are recorded at every node - compare_with emits. Score sums (for ``mean_score``) are recorded - only where a node carries ``threshold_applied_score``. Leaves under - a list parent carry CM counts only, so their paths get no - ``mean_score`` (see #249). + Walks ``fields``, which holds both object subtrees and the element + fields of a list of StructuredModel, so CM counts are recorded at + every node compare_with emits. Score sums (for ``mean_score``) are + recorded only where a node carries a finite + ``threshold_applied_score``. Leaves under a list parent carry CM + counts only, so their paths get no ``mean_score`` (see #249). + ``nested_fields`` is also walked, but compare_with no longer emits it. """ for field_name, field_data in fields_dict.items(): if not isinstance(field_data, dict): From 18c241dc18934cf54b1fe57c1ef5da4d15e58cea Mon Sep 17 00:00:00 2001 From: Spencer Romo Date: Mon, 19 Oct 2026 11:09:38 -0500 Subject: [PATCH 4/7] docs(249,338): address review on #403 - Guides: leaves and primitive lists anywhere inside a List[StructuredModel] element have no mean_score, and every path inside an element counts only pairs that met the match threshold (a pair below it emits no element fields). - _accumulate_field_metrics docstring: same scope, replacing "leaves under a list parent". - _ClassGatedComparator and test-class docstrings: both [Plain, NoteSM] rows enforced the cross-class rule under the list-wide gate (bare fd=2). - Pin the bare row: [Plain] vs [Plain] = 0.0, bare [Plain, NoteA] vs [Plain, NoteB] = 0.5 (tp=1, fd=1). - Fix the two surviving "emits only at the list parent" comments in test_bulk_structured_model_evaluator.py. - CHANGELOG: Unreleased Documentation entry for the mean_score guide fix. --- CHANGELOG.md | 9 ++++++ .../docs/Guides/Evaluation/bulk-evaluation.md | 2 +- .../Evaluation/understanding-results.md | 2 +- .../bulk_structured_model_evaluator.py | 7 +++-- .../models/comparison_helper.py | 14 ++++++---- .../test_bulk_structured_model_evaluator.py | 9 +++--- .../test_nested_plain_basemodel.py | 28 +++++++++++++++---- 7 files changed, 52 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 50672f84..9905b867 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -151,6 +151,15 @@ Each release links to full notes on the ### Documentation +- Corrected which paths in `BulkStructuredModelEvaluator` `field_metrics` get a + `mean_score`. The guides said `compare_with()` scores only the list parent of a + `List[StructuredModel]`. An object inside a list element (`items.addr`) is + scored too. Leaves and primitive lists anywhere inside an element + (`items.name`, `items.addr.city`, `items.tags`) carry confusion-matrix counts + but no `mean_score`, and only from element pairs that met the match + threshold: a pair below it emits no element fields at all. Scoring is + unchanged ([#249](https://github.com/awslabs/stickler/issues/249)) + - Corrected the last row of the 1.0.0 table under "The element gate judges a PAIR, not a list". Under its bare `Optional[List[Any]]` heading, `[Plain, NoteSM]` vs `[Plain, Note2SM]` scores `0.5`, not `1.0`, and scored diff --git a/docs/docs/Guides/Evaluation/bulk-evaluation.md b/docs/docs/Guides/Evaluation/bulk-evaluation.md index 6f0335f7..5abd0c13 100644 --- a/docs/docs/Guides/Evaluation/bulk-evaluation.md +++ b/docs/docs/Guides/Evaluation/bulk-evaluation.md @@ -139,7 +139,7 @@ Because each document's `overall_score = Σ_f(score × weight) / Σ_f(weight)` a The denominator is the count of documents whose `overall_score` was a finite number: error docs (`update()` raised) and successful docs carrying a non-finite or missing `overall_score` are both excluded. With zero eligible documents the score is `0.0` — disambiguate via `document_count` when that matters. -Per-field `mean_score` is reported at every nested path (leaf and object/list aggregate) where `threshold_applied_score` was observed in at least one document, averaging over just those documents. Paths with confusion-matrix counts but no score data (e.g., leaves inside `List[StructuredModel]`, which `compare_with()` emits without a score) are surfaced without a `mean_score` key rather than reported as `0.0`. +Per-field `mean_score` is reported at every nested path (leaf and object/list aggregate) where `threshold_applied_score` was observed in at least one document, averaging over just those documents. Paths with confusion-matrix counts but no score data (leaves and primitive lists anywhere inside a `List[StructuredModel]` element, such as `items.name`, `items.addr.city` or `items.tags`) are surfaced without a `mean_score` key rather than reported as `0.0`. Every path inside an element, scored or not, counts only element pairs that met the match threshold: `compare_with()` emits no element fields for a pair below it. --- diff --git a/docs/docs/Guides/Evaluation/understanding-results.md b/docs/docs/Guides/Evaluation/understanding-results.md index eb0f1157..5d1b82ce 100644 --- a/docs/docs/Guides/Evaluation/understanding-results.md +++ b/docs/docs/Guides/Evaluation/understanding-results.md @@ -402,7 +402,7 @@ This is useful for comprehensive auditing of all comparisons, not just failures. The per-document `overall_score` above is a weighted average of field scores. When you aggregate across many documents with `BulkStructuredModelEvaluator.compute()`, the weight information is preserved via two keys: - **`metrics["weighted_overall_score"]`** (float) -- Arithmetic mean of each document's `overall_score`. Prefer this over `cm_f1` whenever your schema uses non-uniform `ComparableField(weight=...)` values: `cm_f1` treats every field-match equally, while `weighted_overall_score` preserves the declared per-field weights across the dataset. -- **`field_metrics[path]["mean_score"]`** (float, when present) -- Arithmetic mean of each document's `threshold_applied_score` at that path, reported at every nested node that was actually scored. Paths with confusion-matrix counts but no score data (e.g., leaves inside `List[StructuredModel]`, which `compare_with()` emits without a score) are surfaced without a `mean_score` key rather than as `0.0`. +- **`field_metrics[path]["mean_score"]`** (float, when present) -- Arithmetic mean of each document's `threshold_applied_score` at that path, reported at every nested node that was actually scored. Paths with confusion-matrix counts but no score data (leaves and primitive lists anywhere inside a `List[StructuredModel]` element, such as `items.name`, `items.addr.city` or `items.tags`) are surfaced without a `mean_score` key rather than as `0.0`. Every path inside an element, scored or not, counts only element pairs that met the match threshold: `compare_with()` emits no element fields for a pair below it. ```python from stickler.structured_object_evaluator.bulk_structured_model_evaluator import ( diff --git a/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py b/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py index b649f655..52b6bc4c 100644 --- a/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py +++ b/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py @@ -460,8 +460,11 @@ def _accumulate_field_metrics( fields of a list of StructuredModel, so CM counts are recorded at every node compare_with emits. Score sums (for ``mean_score``) are recorded only where a node carries a finite - ``threshold_applied_score``. Leaves under a list parent carry CM - counts only, so their paths get no ``mean_score`` (see #249). + ``threshold_applied_score``. Leaves anywhere inside a list element, + and primitive lists inside a list element, carry CM counts only, so + their paths get no ``mean_score`` (see #249). An element pair below + the match threshold emits no element fields, so every path inside an + element counts matched pairs only. ``nested_fields`` is also walked, but compare_with no longer emits it. """ for field_name, field_data in fields_dict.items(): diff --git a/src/stickler/structured_object_evaluator/models/comparison_helper.py b/src/stickler/structured_object_evaluator/models/comparison_helper.py index 820f5db8..2f770a80 100644 --- a/src/stickler/structured_object_evaluator/models/comparison_helper.py +++ b/src/stickler/structured_object_evaluator/models/comparison_helper.py @@ -56,12 +56,14 @@ class _ClassGatedComparator(BaseComparator): The dict row zeroed three elements that score 1.0 on their own, in the same list, on `dev`, and here the moment the plain model is removed -- and it drew a "holds a mapping" warning on a field whose annotation says nothing about - mappings. The declared-comparator row is worse than wrong-looking: it - enforces the cross-class rule that #327 is explicitly holding open, so a user - got the #327 answer or the documented one according to whether an unrelated - plain model shared the list. Both rules now apply to exactly the population - the wrapper was installed for. That row now scores 1.0; the bare row scores - 0.5, because the bare annotation still refuses the `Plain` pair on its own + mappings. The two `[Plain, NoteSM]` rows are worse than wrong-looking: both + enforce the cross-class rule that #327 is explicitly holding open (the bare + row's 0.0 is fd=2, the refused `Plain` pair plus `NoteSM`/`Note2SM`; with + `[Plain, NoteSM]` on both sides it scores 0.5), so a user got the #327 answer + or the documented one according to whether an unrelated plain model shared + the list. Both rules now apply to exactly the population the wrapper was + installed for. The declared row now scores 1.0; the bare row scores 0.5, + because the bare annotation still refuses the `Plain` pair on its own (`[Plain]` vs `[Plain]` scores 0.0), not because of the cross-class rule. A dict element does not reach the gate -- neither because diff --git a/tests/structured_object_evaluator/test_bulk_structured_model_evaluator.py b/tests/structured_object_evaluator/test_bulk_structured_model_evaluator.py index c31373b6..f63c25c6 100644 --- a/tests/structured_object_evaluator/test_bulk_structured_model_evaluator.py +++ b/tests/structured_object_evaluator/test_bulk_structured_model_evaluator.py @@ -1164,9 +1164,10 @@ def test_score_only_path_surfaces_in_field_metrics(self): def test_list_of_model_leaf_omits_mean_score_key(self): """Leaves inside List[StructuredModel] get CM counts but no mean_score. - compare_with() only emits threshold_applied_score at the list parent, - so nested leaves never accumulate a score. The key is omitted rather - than reported as a misleading 0.0. + compare_with() emits element leaves without threshold_applied_score + (the list parent and object nodes inside an element are scored), so + these leaves never accumulate a score. The key is omitted rather than + reported as a misleading 0.0. """ from typing import List as _List @@ -1191,7 +1192,7 @@ class _Order(StructuredModel): # Nested leaves get CM counts bubbled up... assert "items.name" in result.field_metrics assert result.field_metrics["items.name"].get("tp", 0) > 0 - # ...but no mean_score because compare_with emits it only at the parent. + # ...but no mean_score, because compare_with emits element leaves unscored. assert "mean_score" not in result.field_metrics["items.name"] assert "mean_score" not in result.field_metrics["items.qty"] # List parent and sibling leaves do surface mean_score. diff --git a/tests/structured_object_evaluator/test_nested_plain_basemodel.py b/tests/structured_object_evaluator/test_nested_plain_basemodel.py index c1afa618..5a9cdf9f 100644 --- a/tests/structured_object_evaluator/test_nested_plain_basemodel.py +++ b/tests/structured_object_evaluator/test_nested_plain_basemodel.py @@ -1713,11 +1713,12 @@ class TestTheElementGateJudgesAPairNotAList: The string row is the shape the refusal is FOR: one element out of four cannot be scored, and the field loses a quarter. The dict row zeroed three elements that score 1.0 on their own, in the same list, on `dev`, and here the moment - the plain model is removed. The declared row enforced the cross-class rule - that #327 is deliberately holding open, so the answer depended on whether an - unrelated plain model shared the list. The bare row's 0.5 is the refused - `Plain` pair (`[Plain]` vs `[Plain]` scores 0.0 bare), not that rule, which is - why `test_two_structured_model_classes_keep_the_declared_gap` uses `Declared`. + the plain model is removed. Both `[Plain, NoteSM]` rows enforced the + cross-class rule that #327 is deliberately holding open (bare: fd=2 under the + list-wide gate), so the answer depended on whether an unrelated plain model + shared the list. The bare row's 0.5 now is the refused `Plain` pair + (`[Plain]` vs `[Plain]` scores 0.0 bare), not that rule, which is why + `test_two_structured_model_classes_keep_the_declared_gap` uses `Declared`. """ class Plain(BaseModel): @@ -1803,6 +1804,23 @@ def test_two_structured_model_classes_keep_the_declared_gap(self): assert alone == pytest.approx(1.0) assert beside_a_plain_model == pytest.approx(alone) + def test_the_bare_row_loses_the_refused_plain_pair(self): + """Pins the bare `[Plain, NoteSM]` row of the table above (#338). + + The row drifted from behaviour once because nothing asserted it. The bare + 0.5 is the refused `Plain` pair: `[Plain]` alone scores 0.0, and the + cross-class `NoteA`/`NoteB` pair still scores as a match (tp=1). + """ + Bare = self._bare() + assert self._score(Bare, (self.Plain(sku="p"),)) == pytest.approx(0.0) + result = Bare(f=[self.Plain(sku="p"), self.NoteA(sku="a")]).compare_with( + Bare(f=[self.Plain(sku="p"), self.NoteB(sku="a")]), + include_confusion_matrix=True, + ) + node = result["confusion_matrix"]["fields"]["f"]["overall"] + assert result["field_scores"]["f"] == pytest.approx(0.5) + assert (node["tp"], node["fd"]) == (1, 1) + def test_a_plain_model_against_a_structured_model_is_still_a_mismatch(self): """The pair `and` would have waved through, which is why the rule reads `or`. From e9bf36ddf011e57557964dc30173a89fda3ecac1 Mon Sep 17 00:00:00 2001 From: Spencer Romo Date: Thu, 29 Oct 2026 11:01:39 -0500 Subject: [PATCH 5/7] docs: list the paths inside a list element that get mean_score Inside a List[StructuredModel] element, only the element's own object and List[StructuredModel] fields (items.addr, items.parts) carry a mean_score, and each document's value is the first matched element pair's score. Every other path inside an element, including objects two or more levels down (items.addr.geo), has confusion-matrix counts only. Fix the CHANGELOG, both guides and the _accumulate_field_metrics docstring, and extend the pinning test to the depth-2 object node, a primitive list and a depth-2 leaf (#249). --- CHANGELOG.md | 12 ++-- .../docs/Guides/Evaluation/bulk-evaluation.md | 2 +- .../Evaluation/understanding-results.md | 2 +- .../bulk_structured_model_evaluator.py | 13 ++-- .../test_bulk_structured_model_evaluator.py | 60 +++++++++++++------ 5 files changed, 59 insertions(+), 30 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index e798eb2e..10181641 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -346,11 +346,13 @@ Each release links to full notes on the - Corrected which paths in `BulkStructuredModelEvaluator` `field_metrics` get a `mean_score`. The guides said `compare_with()` scores only the list parent of a - `List[StructuredModel]`. An object inside a list element (`items.addr`) is - scored too. Leaves and primitive lists anywhere inside an element - (`items.name`, `items.addr.city`, `items.tags`) carry confusion-matrix counts - but no `mean_score`, and only from element pairs that met the match - threshold: a pair below it emits no element fields at all. Scoring is + `List[StructuredModel]`. A list element's own object and `List[StructuredModel]` + fields (`items.addr`, `items.parts`) are scored too, but each document's value + is its first matched element pair's score, not a mean over pairs, so it + depends on element order. Every other path inside an element (`items.name`, + `items.tags`, `items.addr.city`, `items.addr.geo`) carries confusion-matrix + counts but no `mean_score`. All of them count only element pairs that met the + match threshold: a pair below it emits no element fields at all. Scoring is unchanged ([#249](https://github.com/awslabs/stickler/issues/249)) - Corrected the last row of the 1.0.0 table under "The element gate judges a diff --git a/docs/docs/Guides/Evaluation/bulk-evaluation.md b/docs/docs/Guides/Evaluation/bulk-evaluation.md index 5abd0c13..2c2192ed 100644 --- a/docs/docs/Guides/Evaluation/bulk-evaluation.md +++ b/docs/docs/Guides/Evaluation/bulk-evaluation.md @@ -139,7 +139,7 @@ Because each document's `overall_score = Σ_f(score × weight) / Σ_f(weight)` a The denominator is the count of documents whose `overall_score` was a finite number: error docs (`update()` raised) and successful docs carrying a non-finite or missing `overall_score` are both excluded. With zero eligible documents the score is `0.0` — disambiguate via `document_count` when that matters. -Per-field `mean_score` is reported at every nested path (leaf and object/list aggregate) where `threshold_applied_score` was observed in at least one document, averaging over just those documents. Paths with confusion-matrix counts but no score data (leaves and primitive lists anywhere inside a `List[StructuredModel]` element, such as `items.name`, `items.addr.city` or `items.tags`) are surfaced without a `mean_score` key rather than reported as `0.0`. Every path inside an element, scored or not, counts only element pairs that met the match threshold: `compare_with()` emits no element fields for a pair below it. +Per-field `mean_score` is reported at every nested path (leaf and object/list aggregate) where `threshold_applied_score` was observed in at least one document, averaging over just those documents. Paths with confusion-matrix counts but no score data are surfaced without a `mean_score` key rather than reported as `0.0`. Inside a `List[StructuredModel]` element, only the element's own object and `List[StructuredModel]` fields (`items.addr`, `items.parts`) are scored, and each document's value is its first matched element pair's score, not a mean over pairs, so it depends on element order. Every other path inside an element (`items.name`, `items.tags`, `items.addr.city`, `items.addr.geo`) has counts only. All of them count only element pairs that met the match threshold: `compare_with()` emits no element fields for a pair below it. --- diff --git a/docs/docs/Guides/Evaluation/understanding-results.md b/docs/docs/Guides/Evaluation/understanding-results.md index 8987afba..5152a5d2 100644 --- a/docs/docs/Guides/Evaluation/understanding-results.md +++ b/docs/docs/Guides/Evaluation/understanding-results.md @@ -402,7 +402,7 @@ This is useful for comprehensive auditing of all comparisons, not just failures. The per-document `overall_score` above is a weighted average of field scores. When you aggregate across many documents with `BulkStructuredModelEvaluator.compute()`, the weight information is preserved via two keys: - **`metrics["weighted_overall_score"]`** (float) -- Arithmetic mean of each document's `overall_score`. Prefer this over `cm_f1` whenever your schema uses non-uniform `ComparableField(weight=...)` values: `cm_f1` treats every field-match equally, while `weighted_overall_score` preserves the declared per-field weights across the dataset. -- **`field_metrics[path]["mean_score"]`** (float, when present) -- Arithmetic mean of each document's `threshold_applied_score` at that path, reported at every nested node that was actually scored. Paths with confusion-matrix counts but no score data (leaves and primitive lists anywhere inside a `List[StructuredModel]` element, such as `items.name`, `items.addr.city` or `items.tags`) are surfaced without a `mean_score` key rather than as `0.0`. Every path inside an element, scored or not, counts only element pairs that met the match threshold: `compare_with()` emits no element fields for a pair below it. +- **`field_metrics[path]["mean_score"]`** (float, when present) -- Arithmetic mean of each document's `threshold_applied_score` at that path, reported at every nested node that was actually scored. Paths with confusion-matrix counts but no score data are surfaced without a `mean_score` key rather than as `0.0`. Inside a `List[StructuredModel]` element, only the element's own object and `List[StructuredModel]` fields (`items.addr`, `items.parts`) are scored, and each document's value is its first matched element pair's score, not a mean over pairs, so it depends on element order. Every other path inside an element (`items.name`, `items.tags`, `items.addr.city`, `items.addr.geo`) has counts only. All of them count only element pairs that met the match threshold: `compare_with()` emits no element fields for a pair below it. ```python from stickler.structured_object_evaluator.bulk_structured_model_evaluator import ( diff --git a/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py b/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py index 52b6bc4c..7212263d 100644 --- a/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py +++ b/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py @@ -460,11 +460,14 @@ def _accumulate_field_metrics( fields of a list of StructuredModel, so CM counts are recorded at every node compare_with emits. Score sums (for ``mean_score``) are recorded only where a node carries a finite - ``threshold_applied_score``. Leaves anywhere inside a list element, - and primitive lists inside a list element, carry CM counts only, so - their paths get no ``mean_score`` (see #249). An element pair below - the match threshold emits no element fields, so every path inside an - element counts matched pairs only. + ``threshold_applied_score``. Inside a list element, only the + element's own object and list-of-StructuredModel fields + (``items.addr``, ``items.parts``) carry one, copied from the first + matched element pair. Every other path inside an element (leaves, + primitive lists, ``items.addr.geo``) carries CM counts only, so it + gets no ``mean_score`` (see #249). An element pair below the match + threshold emits no element fields, so every path inside an element + counts matched pairs only. ``nested_fields`` is also walked, but compare_with no longer emits it. """ for field_name, field_data in fields_dict.items(): diff --git a/tests/structured_object_evaluator/test_bulk_structured_model_evaluator.py b/tests/structured_object_evaluator/test_bulk_structured_model_evaluator.py index f63c25c6..1e458fd1 100644 --- a/tests/structured_object_evaluator/test_bulk_structured_model_evaluator.py +++ b/tests/structured_object_evaluator/test_bulk_structured_model_evaluator.py @@ -1162,18 +1162,28 @@ def test_score_only_path_surfaces_in_field_metrics(self): assert result.field_metrics["bar"].get("tp", 0) == 0 def test_list_of_model_leaf_omits_mean_score_key(self): - """Leaves inside List[StructuredModel] get CM counts but no mean_score. + """In a List[StructuredModel] element only its own objects get mean_score. - compare_with() emits element leaves without threshold_applied_score - (the list parent and object nodes inside an element are scored), so - these leaves never accumulate a score. The key is omitted rather than - reported as a misleading 0.0. + compare_with() scores the list parent and the element's own object + field (``items.addr``), but emits every other path inside an element + without threshold_applied_score: leaves, primitive lists, and objects + two levels down (``items.addr.geo``). Those get CM counts only, and the + key is omitted rather than reported as a misleading 0.0 (#249). """ from typing import List as _List + class _Geo(StructuredModel): + lat: str = ComparableField(comparator=LevenshteinComparator()) + + class _Addr(StructuredModel): + city: str = ComparableField(comparator=LevenshteinComparator()) + geo: _Geo = ComparableField() + class _Line(StructuredModel): name: str = ComparableField(comparator=LevenshteinComparator(), weight=2.0) qty: int = ComparableField(comparator=NumericComparator(), weight=1.0) + tags: _List[str] = ComparableField() + addr: _Addr = ComparableField() class _Order(StructuredModel): order_id: str = ComparableField( @@ -1181,23 +1191,37 @@ class _Order(StructuredModel): ) items: _List[_Line] = ComparableField(weight=3.0) - gt = _Order(order_id="O-1", items=[_Line(name="apple", qty=1)]) - pred = _Order(order_id="O-1", items=[_Line(name="apple", qty=1)]) + line = _Line( + name="apple", + qty=1, + tags=["red"], + addr=_Addr(city="Oslo", geo=_Geo(lat="59.9")), + ) + gt = _Order(order_id="O-1", items=[line]) + pred = _Order(order_id="O-1", items=[line]) evaluator = BulkStructuredModelEvaluator(target_schema=_Order) evaluator.update(gt, pred) evaluator.update(gt, pred) - result = evaluator.compute() - - # Nested leaves get CM counts bubbled up... - assert "items.name" in result.field_metrics - assert result.field_metrics["items.name"].get("tp", 0) > 0 - # ...but no mean_score, because compare_with emits element leaves unscored. - assert "mean_score" not in result.field_metrics["items.name"] - assert "mean_score" not in result.field_metrics["items.qty"] - # List parent and sibling leaves do surface mean_score. - assert "mean_score" in result.field_metrics["items"] - assert "mean_score" in result.field_metrics["order_id"] + fm = evaluator.compute().field_metrics + + unscored = ( + "items.name", + "items.qty", + "items.tags", + "items.addr.city", + "items.addr.geo", + "items.addr.geo.lat", + ) + for path in unscored: + # CM counts bubble up... + assert fm[path].get("tp", 0) > 0, path + # ...but compare_with emits these paths unscored. + assert "mean_score" not in fm[path], path + # The list parent, the element's own object field, and top-level + # leaves do surface mean_score. + for path in ("items", "items.addr", "order_id"): + assert "mean_score" in fm[path], path def test_invalid_overall_score_skipped(self): """Non-numeric, non-finite, and bool overall_score are silently dropped. From f498854152e6ae162c8794ff789a162cbb80209b Mon Sep 17 00:00:00 2001 From: Spencer Romo Date: Thu, 29 Oct 2026 11:02:45 -0500 Subject: [PATCH 6/7] docs: link the element-order dependence to #428 --- CHANGELOG.md | 2 +- docs/docs/Guides/Evaluation/bulk-evaluation.md | 2 +- docs/docs/Guides/Evaluation/understanding-results.md | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10181641..72741780 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -349,7 +349,7 @@ Each release links to full notes on the `List[StructuredModel]`. A list element's own object and `List[StructuredModel]` fields (`items.addr`, `items.parts`) are scored too, but each document's value is its first matched element pair's score, not a mean over pairs, so it - depends on element order. Every other path inside an element (`items.name`, + depends on element order ([#428](https://github.com/awslabs/stickler/issues/428)). Every other path inside an element (`items.name`, `items.tags`, `items.addr.city`, `items.addr.geo`) carries confusion-matrix counts but no `mean_score`. All of them count only element pairs that met the match threshold: a pair below it emits no element fields at all. Scoring is diff --git a/docs/docs/Guides/Evaluation/bulk-evaluation.md b/docs/docs/Guides/Evaluation/bulk-evaluation.md index 2c2192ed..882ef89e 100644 --- a/docs/docs/Guides/Evaluation/bulk-evaluation.md +++ b/docs/docs/Guides/Evaluation/bulk-evaluation.md @@ -139,7 +139,7 @@ Because each document's `overall_score = Σ_f(score × weight) / Σ_f(weight)` a The denominator is the count of documents whose `overall_score` was a finite number: error docs (`update()` raised) and successful docs carrying a non-finite or missing `overall_score` are both excluded. With zero eligible documents the score is `0.0` — disambiguate via `document_count` when that matters. -Per-field `mean_score` is reported at every nested path (leaf and object/list aggregate) where `threshold_applied_score` was observed in at least one document, averaging over just those documents. Paths with confusion-matrix counts but no score data are surfaced without a `mean_score` key rather than reported as `0.0`. Inside a `List[StructuredModel]` element, only the element's own object and `List[StructuredModel]` fields (`items.addr`, `items.parts`) are scored, and each document's value is its first matched element pair's score, not a mean over pairs, so it depends on element order. Every other path inside an element (`items.name`, `items.tags`, `items.addr.city`, `items.addr.geo`) has counts only. All of them count only element pairs that met the match threshold: `compare_with()` emits no element fields for a pair below it. +Per-field `mean_score` is reported at every nested path (leaf and object/list aggregate) where `threshold_applied_score` was observed in at least one document, averaging over just those documents. Paths with confusion-matrix counts but no score data are surfaced without a `mean_score` key rather than reported as `0.0`. Inside a `List[StructuredModel]` element, only the element's own object and `List[StructuredModel]` fields (`items.addr`, `items.parts`) are scored, and each document's value is its first matched element pair's score, not a mean over pairs, so it depends on element order ([#428](https://github.com/awslabs/stickler/issues/428)). Every other path inside an element (`items.name`, `items.tags`, `items.addr.city`, `items.addr.geo`) has counts only. All of them count only element pairs that met the match threshold: `compare_with()` emits no element fields for a pair below it. --- diff --git a/docs/docs/Guides/Evaluation/understanding-results.md b/docs/docs/Guides/Evaluation/understanding-results.md index 5152a5d2..68b8cf8d 100644 --- a/docs/docs/Guides/Evaluation/understanding-results.md +++ b/docs/docs/Guides/Evaluation/understanding-results.md @@ -402,7 +402,7 @@ This is useful for comprehensive auditing of all comparisons, not just failures. The per-document `overall_score` above is a weighted average of field scores. When you aggregate across many documents with `BulkStructuredModelEvaluator.compute()`, the weight information is preserved via two keys: - **`metrics["weighted_overall_score"]`** (float) -- Arithmetic mean of each document's `overall_score`. Prefer this over `cm_f1` whenever your schema uses non-uniform `ComparableField(weight=...)` values: `cm_f1` treats every field-match equally, while `weighted_overall_score` preserves the declared per-field weights across the dataset. -- **`field_metrics[path]["mean_score"]`** (float, when present) -- Arithmetic mean of each document's `threshold_applied_score` at that path, reported at every nested node that was actually scored. Paths with confusion-matrix counts but no score data are surfaced without a `mean_score` key rather than as `0.0`. Inside a `List[StructuredModel]` element, only the element's own object and `List[StructuredModel]` fields (`items.addr`, `items.parts`) are scored, and each document's value is its first matched element pair's score, not a mean over pairs, so it depends on element order. Every other path inside an element (`items.name`, `items.tags`, `items.addr.city`, `items.addr.geo`) has counts only. All of them count only element pairs that met the match threshold: `compare_with()` emits no element fields for a pair below it. +- **`field_metrics[path]["mean_score"]`** (float, when present) -- Arithmetic mean of each document's `threshold_applied_score` at that path, reported at every nested node that was actually scored. Paths with confusion-matrix counts but no score data are surfaced without a `mean_score` key rather than as `0.0`. Inside a `List[StructuredModel]` element, only the element's own object and `List[StructuredModel]` fields (`items.addr`, `items.parts`) are scored, and each document's value is its first matched element pair's score, not a mean over pairs, so it depends on element order ([#428](https://github.com/awslabs/stickler/issues/428)). Every other path inside an element (`items.name`, `items.tags`, `items.addr.city`, `items.addr.geo`) has counts only. All of them count only element pairs that met the match threshold: `compare_with()` emits no element fields for a pair below it. ```python from stickler.structured_object_evaluator.bulk_structured_model_evaluator import ( From 633ce69164bd1501bae80f83014ac953796cb70e Mon Sep 17 00:00:00 2001 From: Spencer Romo Date: Thu, 29 Oct 2026 11:49:07 -0500 Subject: [PATCH 7/7] docs: name StructuredModel fields as the scored ones in a list element, and pin items.parts and a plain field --- CHANGELOG.md | 14 ++++---- .../docs/Guides/Evaluation/bulk-evaluation.md | 2 +- .../Evaluation/understanding-results.md | 2 +- .../bulk_structured_model_evaluator.py | 2 +- .../test_bulk_structured_model_evaluator.py | 36 +++++++++++++------ 5 files changed, 37 insertions(+), 19 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 72741780..52509832 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -346,12 +346,14 @@ Each release links to full notes on the - Corrected which paths in `BulkStructuredModelEvaluator` `field_metrics` get a `mean_score`. The guides said `compare_with()` scores only the list parent of a - `List[StructuredModel]`. A list element's own object and `List[StructuredModel]` - fields (`items.addr`, `items.parts`) are scored too, but each document's value - is its first matched element pair's score, not a mean over pairs, so it - depends on element order ([#428](https://github.com/awslabs/stickler/issues/428)). Every other path inside an element (`items.name`, - `items.tags`, `items.addr.city`, `items.addr.geo`) carries confusion-matrix - counts but no `mean_score`. All of them count only element pairs that met the + `List[StructuredModel]`. A list element's own `StructuredModel` and + `List[StructuredModel]` fields (`items.addr`, `items.parts`) are scored too, + but each document's value is its first matched element pair's score, not a + mean over pairs, so it depends on element order + ([#428](https://github.com/awslabs/stickler/issues/428)). Every other path + inside an element (`items.name`, `items.tags`, `items.addr.city`, + `items.addr.geo`, a plain `BaseModel` field) carries confusion-matrix counts + but no `mean_score`. All of them count only element pairs that met the match threshold: a pair below it emits no element fields at all. Scoring is unchanged ([#249](https://github.com/awslabs/stickler/issues/249)) diff --git a/docs/docs/Guides/Evaluation/bulk-evaluation.md b/docs/docs/Guides/Evaluation/bulk-evaluation.md index 882ef89e..8ec3cf1a 100644 --- a/docs/docs/Guides/Evaluation/bulk-evaluation.md +++ b/docs/docs/Guides/Evaluation/bulk-evaluation.md @@ -139,7 +139,7 @@ Because each document's `overall_score = Σ_f(score × weight) / Σ_f(weight)` a The denominator is the count of documents whose `overall_score` was a finite number: error docs (`update()` raised) and successful docs carrying a non-finite or missing `overall_score` are both excluded. With zero eligible documents the score is `0.0` — disambiguate via `document_count` when that matters. -Per-field `mean_score` is reported at every nested path (leaf and object/list aggregate) where `threshold_applied_score` was observed in at least one document, averaging over just those documents. Paths with confusion-matrix counts but no score data are surfaced without a `mean_score` key rather than reported as `0.0`. Inside a `List[StructuredModel]` element, only the element's own object and `List[StructuredModel]` fields (`items.addr`, `items.parts`) are scored, and each document's value is its first matched element pair's score, not a mean over pairs, so it depends on element order ([#428](https://github.com/awslabs/stickler/issues/428)). Every other path inside an element (`items.name`, `items.tags`, `items.addr.city`, `items.addr.geo`) has counts only. All of them count only element pairs that met the match threshold: `compare_with()` emits no element fields for a pair below it. +Per-field `mean_score` is reported at every nested path (leaf and object/list aggregate) where `threshold_applied_score` was observed in at least one document, averaging over just those documents. Paths with confusion-matrix counts but no score data are surfaced without a `mean_score` key rather than reported as `0.0`. Inside a `List[StructuredModel]` element, only the element's own `StructuredModel` and `List[StructuredModel]` fields (`items.addr`, `items.parts`) are scored, and each document's value is its first matched element pair's score, not a mean over pairs, so it depends on element order ([#428](https://github.com/awslabs/stickler/issues/428)). Every other path inside an element (`items.name`, `items.tags`, `items.addr.city`, `items.addr.geo`) has counts only. All of them count only element pairs that met the match threshold: `compare_with()` emits no element fields for a pair below it. --- diff --git a/docs/docs/Guides/Evaluation/understanding-results.md b/docs/docs/Guides/Evaluation/understanding-results.md index 68b8cf8d..24fd500a 100644 --- a/docs/docs/Guides/Evaluation/understanding-results.md +++ b/docs/docs/Guides/Evaluation/understanding-results.md @@ -402,7 +402,7 @@ This is useful for comprehensive auditing of all comparisons, not just failures. The per-document `overall_score` above is a weighted average of field scores. When you aggregate across many documents with `BulkStructuredModelEvaluator.compute()`, the weight information is preserved via two keys: - **`metrics["weighted_overall_score"]`** (float) -- Arithmetic mean of each document's `overall_score`. Prefer this over `cm_f1` whenever your schema uses non-uniform `ComparableField(weight=...)` values: `cm_f1` treats every field-match equally, while `weighted_overall_score` preserves the declared per-field weights across the dataset. -- **`field_metrics[path]["mean_score"]`** (float, when present) -- Arithmetic mean of each document's `threshold_applied_score` at that path, reported at every nested node that was actually scored. Paths with confusion-matrix counts but no score data are surfaced without a `mean_score` key rather than as `0.0`. Inside a `List[StructuredModel]` element, only the element's own object and `List[StructuredModel]` fields (`items.addr`, `items.parts`) are scored, and each document's value is its first matched element pair's score, not a mean over pairs, so it depends on element order ([#428](https://github.com/awslabs/stickler/issues/428)). Every other path inside an element (`items.name`, `items.tags`, `items.addr.city`, `items.addr.geo`) has counts only. All of them count only element pairs that met the match threshold: `compare_with()` emits no element fields for a pair below it. +- **`field_metrics[path]["mean_score"]`** (float, when present) -- Arithmetic mean of each document's `threshold_applied_score` at that path, reported at every nested node that was actually scored. Paths with confusion-matrix counts but no score data are surfaced without a `mean_score` key rather than as `0.0`. Inside a `List[StructuredModel]` element, only the element's own `StructuredModel` and `List[StructuredModel]` fields (`items.addr`, `items.parts`) are scored, and each document's value is its first matched element pair's score, not a mean over pairs, so it depends on element order ([#428](https://github.com/awslabs/stickler/issues/428)). Every other path inside an element (`items.name`, `items.tags`, `items.addr.city`, `items.addr.geo`) has counts only. All of them count only element pairs that met the match threshold: `compare_with()` emits no element fields for a pair below it. ```python from stickler.structured_object_evaluator.bulk_structured_model_evaluator import ( diff --git a/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py b/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py index 7212263d..efc5cb80 100644 --- a/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py +++ b/src/stickler/structured_object_evaluator/bulk_structured_model_evaluator.py @@ -461,7 +461,7 @@ def _accumulate_field_metrics( every node compare_with emits. Score sums (for ``mean_score``) are recorded only where a node carries a finite ``threshold_applied_score``. Inside a list element, only the - element's own object and list-of-StructuredModel fields + element's own StructuredModel and list-of-StructuredModel fields (``items.addr``, ``items.parts``) carry one, copied from the first matched element pair. Every other path inside an element (leaves, primitive lists, ``items.addr.geo``) carries CM counts only, so it diff --git a/tests/structured_object_evaluator/test_bulk_structured_model_evaluator.py b/tests/structured_object_evaluator/test_bulk_structured_model_evaluator.py index 1e458fd1..aac5843f 100644 --- a/tests/structured_object_evaluator/test_bulk_structured_model_evaluator.py +++ b/tests/structured_object_evaluator/test_bulk_structured_model_evaluator.py @@ -1162,16 +1162,20 @@ def test_score_only_path_surfaces_in_field_metrics(self): assert result.field_metrics["bar"].get("tp", 0) == 0 def test_list_of_model_leaf_omits_mean_score_key(self): - """In a List[StructuredModel] element only its own objects get mean_score. - - compare_with() scores the list parent and the element's own object - field (``items.addr``), but emits every other path inside an element - without threshold_applied_score: leaves, primitive lists, and objects - two levels down (``items.addr.geo``). Those get CM counts only, and the - key is omitted rather than reported as a misleading 0.0 (#249). + """In a List[StructuredModel] element only its own StructuredModel and + List[StructuredModel] fields get mean_score. + + compare_with() scores the list parent and those fields (``items.addr``, + ``items.parts``), copied from the first matched pair (#428), but emits + every other path inside an element without threshold_applied_score: + leaves, primitive lists, a plain BaseModel field, and anything two levels + down (``items.addr.geo``, ``items.parts.sku``). Those get CM counts only, + and the key is omitted rather than reported as a misleading 0.0 (#249). """ from typing import List as _List + from pydantic import BaseModel + class _Geo(StructuredModel): lat: str = ComparableField(comparator=LevenshteinComparator()) @@ -1179,11 +1183,19 @@ class _Addr(StructuredModel): city: str = ComparableField(comparator=LevenshteinComparator()) geo: _Geo = ComparableField() + class _Part(StructuredModel): + sku: str = ComparableField(comparator=LevenshteinComparator()) + + class _Plain(BaseModel): + note: str + class _Line(StructuredModel): name: str = ComparableField(comparator=LevenshteinComparator(), weight=2.0) qty: int = ComparableField(comparator=NumericComparator(), weight=1.0) tags: _List[str] = ComparableField() addr: _Addr = ComparableField() + parts: _List[_Part] = ComparableField() + plain: _Plain = ComparableField() class _Order(StructuredModel): order_id: str = ComparableField( @@ -1196,6 +1208,8 @@ class _Order(StructuredModel): qty=1, tags=["red"], addr=_Addr(city="Oslo", geo=_Geo(lat="59.9")), + parts=[_Part(sku="A-1")], + plain=_Plain(note="n"), ) gt = _Order(order_id="O-1", items=[line]) pred = _Order(order_id="O-1", items=[line]) @@ -1212,15 +1226,17 @@ class _Order(StructuredModel): "items.addr.city", "items.addr.geo", "items.addr.geo.lat", + "items.plain", + "items.parts.sku", ) for path in unscored: # CM counts bubble up... assert fm[path].get("tp", 0) > 0, path # ...but compare_with emits these paths unscored. assert "mean_score" not in fm[path], path - # The list parent, the element's own object field, and top-level - # leaves do surface mean_score. - for path in ("items", "items.addr", "order_id"): + # The list parent, the element's own StructuredModel and + # List[StructuredModel] fields, and top-level leaves do surface mean_score. + for path in ("items", "items.addr", "items.parts", "order_id"): assert "mean_score" in fm[path], path def test_invalid_overall_score_skipped(self):