Skip to content
Open
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
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ Each release links to full notes on the
is now a fractional expected count (a float) instead of an integer that
varied between runs
([#601](https://github.com/awslabs/stickler/issues/601)).
- **The HTML report's static non-matches table renders a missing, `None`,
NaN or infinite value as an empty cell.** It printed `None`, `nan` or
`inf`, while the per-document view showed the same value as an empty cell,
so the text changed when you picked a document and changed back on return.
Other values are unchanged and still cut at 100 characters
([#578](https://github.com/awslabs/stickler/issues/578)).

### Performance

Expand Down
23 changes: 20 additions & 3 deletions src/stickler/reporting/html/section_generator.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,28 @@
import html
from typing import Dict, Union
import math
from typing import Any, Dict, Union

from stickler.reporting.html.report_config import ReportConfig
from stickler.reporting.html.utils import ColorUtils, DataExtractor
from stickler.reporting.html.visualization_engine import VisualizationEngine
from stickler.utils.process_evaluation import ProcessEvaluation

# Values longer than this are cut in the static non-matches table.
_MAX_CELL_VALUE_LENGTH = 100


def _non_match_cell_value(value: Any) -> str:
"""Render a non-match value for the static non-matches table.

A missing value, None, NaN and infinity render as an empty cell, as the
per-document view does: its payload carries non-finite numbers as null,
and the browser renders null as an empty cell (#578). The text is cut at
``_MAX_CELL_VALUE_LENGTH`` characters, then escaped.
"""
if value is None or (isinstance(value, float) and not math.isfinite(value)):
return ""
return html.escape(str(value)[:_MAX_CELL_VALUE_LENGTH])


class SectionGenerator:
def __init__(self, results, viz_engine):
Expand Down Expand Up @@ -141,8 +158,8 @@ def generate_non_matches(self, config: ReportConfig) -> str:
doc_id = html.escape(str(nm.get('doc_id', 'N/A')))
field_path = html.escape(str(nm.get('field_path', 'N/A')))
non_match_type = html.escape(str(nm.get('non_match_type', 'N/A')).replace('NonMatchType.',''))
ground_truth_value = html.escape(str(nm.get('ground_truth_value', 'None'))[:100]) # Truncate long values
prediction_value = html.escape(str(nm.get('prediction_value', 'None'))[:100])
ground_truth_value = _non_match_cell_value(nm.get('ground_truth_value'))
prediction_value = _non_match_cell_value(nm.get('prediction_value'))

html_string += f'''
<tr>
Expand Down
62 changes: 62 additions & 0 deletions tests/reporting/html/test_section_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,68 @@ def test_generate_non_matches(self, mock_extract_non_matches):

mock_extract_non_matches.assert_called_once_with(self.mock_results)

@staticmethod
def _value_cells(result):
"""The Ground Truth and Prediction cells of each non-matches row."""
body = result.split('<tbody>', 1)[1].split('</tbody>', 1)[0]
rows = []
for row in body.split('<tr>')[1:]:
cells = [cell.split('</td>', 1)[0] for cell in row.split('<td>')[1:]]
rows.append((cells[3], cells[4]))
return rows

@patch('stickler.reporting.html.utils.data_extractors.DataExtractor.extract_non_matches')
def test_generate_non_matches_missing_and_non_finite_values_are_empty(
self, mock_extract_non_matches
):
"""None, NaN, infinity and a missing key render as empty cells, as in
the per-document view (#578)."""
mock_extract_non_matches.return_value = [
{'doc_id': 'd1', 'field_path': 'a', 'non_match_type': 'FN',
'ground_truth_value': 'x', 'prediction_value': None},
{'doc_id': 'd2', 'field_path': 'b', 'non_match_type': 'FD',
'ground_truth_value': float('nan'), 'prediction_value': float('inf')},
{'doc_id': 'd3', 'field_path': 'c', 'non_match_type': 'FA',
'ground_truth_value': np.float64('-inf'), 'prediction_value': 2.5},
{'doc_id': 'd4', 'field_path': 'd', 'non_match_type': 'FN'},
]

result = self.section_generator.generate_non_matches(ReportConfig())

assert self._value_cells(result) == [
('x', ''),
('', ''),
('', '2.5'),
('', ''),
]
assert 'None' not in result
assert 'nan' not in result
assert 'inf' not in result

@patch('stickler.reporting.html.utils.data_extractors.DataExtractor.extract_non_matches')
def test_generate_non_matches_keeps_falsy_values_and_truncation(
self, mock_extract_non_matches
):
"""Only missing and non-finite values are blanked: 0, False and an
empty string keep their text, and long values are still cut at 100
characters before escaping."""
mock_extract_non_matches.return_value = [
{'doc_id': 'd1', 'field_path': 'a', 'non_match_type': 'FD',
'ground_truth_value': 0, 'prediction_value': False},
{'doc_id': 'd2', 'field_path': 'b', 'non_match_type': 'FD',
'ground_truth_value': 0.0, 'prediction_value': ''},
{'doc_id': 'd3', 'field_path': 'c', 'non_match_type': 'FD',
'ground_truth_value': 'a' * 150, 'prediction_value': '<' * 150},
]

result = self.section_generator.generate_non_matches(ReportConfig())

assert self._value_cells(result) == [
('0', 'False'),
('0.0', ''),
('a' * 100, '&lt;' * 100),
]

@patch('stickler.reporting.html.utils.data_extractors.DataExtractor.extract_non_matches')
def test_generate_non_matches_no_data(self, mock_extract_non_matches):
"""Records were collected and there were none: that is a success."""
Expand Down