Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
12 changes: 11 additions & 1 deletion adapters/mhs.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"""

import json
import math
from collections import Counter, OrderedDict


Expand Down Expand Up @@ -68,7 +69,16 @@ def _label(value, index):
raise MHSInputError(
"record {} hatespeech must be null or one of 0, 1, 2".format(index)
)
normalized = str(value)
if isinstance(value, float):
if not math.isfinite(value) or not value.is_integer():
raise MHSInputError(
"record {} hatespeech must be null or one of 0, 1, 2".format(
index
)
)
normalized = str(int(value))
else:
normalized = str(value)
if normalized not in LABELS:
raise MHSInputError(
"record {} hatespeech must be null or one of 0, 1, 2".format(index)
Expand Down
36 changes: 36 additions & 0 deletions test_claims.py
Original file line number Diff line number Diff line change
Expand Up @@ -1411,6 +1411,42 @@ def test_frozen_eligibility_cohorts_primary_filter_and_halt(self):
self.assertEqual(halted["counts"]["primary_items"], 49)
self.assertIn("below the frozen minimum 50", halted["halt_reason"])

def test_label_normalizes_only_finite_integral_parquet_floats(self):
self.assertIsNone(mhs_adapter._label(None, 1))
for value, expected in (
(0.0, "0"),
(1.0, "1"),
(2.0, "2"),
(0, "0"),
(1, "1"),
(2, "2"),
("0", "0"),
("1", "1"),
("2", "2"),
):
self.assertEqual(mhs_adapter._label(value, 1), expected)

for value in (
0.5,
3.0,
-1.0,
math.nan,
math.inf,
-math.inf,
True,
False,
3,
-1,
"0.0",
"3",
"-1",
):
with self.subTest(value=value):
with self.assertRaisesRegex(
mhs_adapter.MHSInputError, "one of 0, 1, 2"
):
mhs_adapter._label(value, 1)

def test_synthetic_tool_wiring_and_frozen_metric_results(self):
with tempfile.TemporaryDirectory(prefix="groundless-mhs-") as temp:
temp_path = Path(temp)
Expand Down
Loading