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
fix: integrate dev and align NaN field reports
  • Loading branch information
hugosmoreira committed Oct 13, 2026
commit 20d13e747315693084e6d04585d19928b32fa76d
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@ Each release links to full notes on the
false-discovery records. Primitive-list reports avoid extra comparator calls
and preserve scoring's threshold tolerance. Standard structured-child reports
reuse scored child results; custom comparison overrides retain their fallback.
Field-comparison rows treat float `NaN` as missing, consistently with scoring,
both for top-level fields and fields in accepted structured-list pairs.

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.

Please add one sentence here: nested list and nested object rows in field_comparisons now report match=False when scoring recorded any non-match beneath them, even if their score meets the threshold. Users who filter on match will see those rows change.


- **A float `NaN` is now absent, the same as `None`.** Two identical `NaN`s
scored `0.0` and counted as a false discovery, because `NaN` equals nothing
under IEEE 754. `NaN` vs `NaN` or `None` is now a true negative scoring `1.0`,
`NaN` vs a number is a false alarm or false negative, and comparators apply
the same rule. This changes confusion counts for any field holding `NaN`
([#367](https://github.com/awslabs/stickler/issues/367))

- **An unrecognized key in a model config is now reported instead of dropped.**
`model_from_json` read a closed set of keys and ignored everything else, so
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from typing import TYPE_CHECKING, Any, Dict, List, Optional

from .field_comparison_helper import FieldComparisonHelper
from .null_helper import NullHelper
from .reporting_context import ReportingContext

if TYPE_CHECKING:
Expand Down Expand Up @@ -151,13 +152,15 @@ def collect_field_comparisons(
else:
reason = f"below threshold ({raw_score:.3f} < {info.threshold})"

# Handle missing fields
if pred_val is None and gt_val is not None:
# Use scoring's float-NaN-as-missing rule without changing the displayed values.
gt_missing = gt_val is None or NullHelper.is_nan(gt_val)
pred_missing = pred_val is None or NullHelper.is_nan(pred_val)
if pred_missing and not gt_missing:
reason = "false negative (unmatched ground truth)"
is_match = False
raw_score = 0.0
weighted_score = 0.0
elif gt_val is None and pred_val is not None:
elif gt_missing and not pred_missing:
reason = "false alarm (unmatched prediction)"
is_match = False
raw_score = 0.0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from typing import Any, Dict, List, Optional

from .comparison_helper_base import ComparisonHelperBase
from .null_helper import NullHelper
from .reporting_context import ReportingContext


Expand Down Expand Up @@ -162,12 +163,14 @@ def _extract_entries_from_objects(
if field_result is not None and "fields" in field_result and not field_is_match:
field_reason = "non-matching or unmatched items"

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.

This reason replaces the score and threshold, and it also fires for a single nested object below its threshold. In the repro, addr goes from "below threshold (0.000 < 0.5)" to "non-matching or unmatched items", though it's one object. Could this override apply only when the verdict disagrees with score >= threshold, and keep the score in the text? For example: "score 0.667 >= 0.5, but 1 item did not match".


# Handle missing fields
if pred_nested_val is None and gt_nested_val is not None:
# Keep the scored NaN-as-missing decision when formatting child fields.
gt_missing = gt_nested_val is None or NullHelper.is_nan(gt_nested_val)
pred_missing = pred_nested_val is None or NullHelper.is_nan(pred_nested_val)
if pred_missing and not gt_missing:
field_reason = "false negative (unmatched ground truth)"
field_is_match = False
field_score = 0.0
elif gt_nested_val is None and pred_nested_val is not None:
elif gt_missing and not pred_missing:
field_reason = "false alarm (unmatched prediction)"
field_is_match = False
field_score = 0.0
Expand Down
46 changes: 46 additions & 0 deletions tests/structured_object_evaluator/test_nan_as_absent.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,49 @@ def test_null_predicates_agree_on_nan():
assert not NullHelper.is_nan("nan")
assert NullHelper.is_effectively_null_for_primitives(NAN)
assert FieldHelper.is_null_value(NAN)


@pytest.mark.parametrize("nested", [False, True])
@pytest.mark.parametrize(
"gt, pred, score, count, reason",
[
(NAN, NAN, 1.0, "tn", "exact match"),
(NAN, None, 1.0, "tn", "exact match"),
(None, NAN, 1.0, "tn", "exact match"),
(NAN, 5.0, 0.0, "fa", "false alarm (unmatched prediction)"),
(5.0, NAN, 0.0, "fn", "false negative (unmatched ground truth)"),
(0.0, 0.0, 1.0, "tp", "exact match"),
(0.0, 5.0, 0.0, "fd", "below threshold (0.000 < 0.5)"),
],
)
def test_field_reports_agree_with_missing_value_counts(
nested, gt, pred, score, count, reason
):
class ReportItem(Item):
# The matching sku must keep the pair accepted when value is missing.
match_threshold = 0.4

class ReportOrder(StructuredModel):
items: List[ReportItem] = ComparableField()

if nested:
expected = ReportOrder(items=[ReportItem(value=gt)])
actual = ReportOrder(items=[ReportItem(value=pred)])
path = "items[0].value"
else:
expected, actual = Reading(value=gt), Reading(value=pred)
path = "value"

result = expected.compare_with(
actual, include_confusion_matrix=True, document_field_comparisons=True
)
fields = result["confusion_matrix"]["fields"]
if nested:
assert fields["items"]["overall"]["tp"] == 1
fields = fields["items"]["fields"]
assert fields["value"]["overall"][count] == 1
row = next(r for r in result["field_comparisons"] if r["expected_key"] == path)
assert row["actual_key"] == path
assert row["match"] is (count in ("tp", "tn"))
assert row["score"] == score
assert row["reason"] == reason
Loading
You are viewing a condensed version of this merge commit. You can view the full changes here.