Skip to content

Commit dd37994

Browse files
authored
Merge pull request #529 from propcgamer20-png/fix/report-negative-zero-pp
fix: stop rendering "-0.0 pp" for tiny negative share changes in compare
2 parents 7dafe0f + fb7970b commit dd37994

2 files changed

Lines changed: 37 additions & 1 deletion

File tree

faircode/report.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ def _delta(n: int | None) -> str:
118118
return "not available" if n is None else f"{n:+d}"
119119

120120

121+
def _strip_neg_zero(val: float, dp: int = 1) -> float:
122+
"""Return 0.0 when ``val`` rounds to zero at ``dp`` decimals, so a tiny
123+
negative value does not render as a misleading ``-0.0``."""
124+
return 0.0 if round(val, dp) == 0 else val
125+
126+
121127
def _dataset_score_line(dataset: dict) -> str:
122128
if dataset["overall_score"] is None:
123129
return f"{dataset['n_rows']:,} rows · score not measured"
@@ -156,7 +162,8 @@ def compare_to_terminal(cmp: dict) -> str:
156162
tag = {"appeared": " (appeared)", "disappeared": " (disappeared)",
157163
"shifted": ""}[g["status"]]
158164
add(f" {g['label'][:18]:<18} {g['share_a'] * 100:5.1f}% → "
159-
f"{g['share_b'] * 100:5.1f}% ({g['share_delta'] * 100:+5.1f} pp){tag}")
165+
f"{g['share_b'] * 100:5.1f}% "
166+
f"({_strip_neg_zero(g['share_delta'] * 100):+5.1f} pp){tag}")
160167
if len(cd["groups"]) > DISPLAY_GROUPS:
161168
add(f" … and {len(cd['groups']) - DISPLAY_GROUPS} more groups")
162169
add("")
@@ -345,6 +352,7 @@ def esc(s) -> str:
345352
return html.escape(str(s))
346353

347354
def signed(val: float | int, dp: int = 1) -> str:
355+
val = _strip_neg_zero(val, dp)
348356
prefix = "+" if val > 0 else ""
349357
return f"{prefix}{val:.{dp}f}"
350358

tests/test_report.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,31 @@ def test_compare_reports_unmeasured_score_without_formatting_none():
361361
assert "Overall score change: not available" in terminal_out
362362
assert "score change not available" in html_out
363363
assert "None" not in html_out
364+
365+
366+
def test_compare_does_not_render_negative_zero_share_delta():
367+
"""A tiny negative share_delta that rounds to 0.0 pp must not print as
368+
'-0.0 pp' in either the terminal or the HTML compare report."""
369+
result = {
370+
"score_delta": 0,
371+
"a": {"name": "A", "overall_score": 100, "n_rows": 10000, "grade": "A"},
372+
"b": {"name": "B", "overall_score": 100, "n_rows": 10000, "grade": "A"},
373+
"added_dimensions": [], "removed_dimensions": [], "flags": [],
374+
"dimensions": [
375+
{
376+
"name": "Gender", "kind": "Demographic", "drift_level": "none",
377+
"psi": 0.0, "tvd": 0.0,
378+
"dimension_score_a": 100, "dimension_score_b": 100,
379+
"dimension_score_delta": 0,
380+
"groups": [
381+
{"label": "Female", "status": "shifted",
382+
"share_a": 0.5, "share_b": 0.4999, "share_delta": -0.0001},
383+
{"label": "Male", "status": "shifted",
384+
"share_a": 0.5, "share_b": 0.5001, "share_delta": 0.0001},
385+
],
386+
}
387+
],
388+
}
389+
390+
assert "-0.0 pp" not in compare_to_terminal(result)
391+
assert "-0.0 pp" not in compare_to_html(result)

0 commit comments

Comments
 (0)