mirror of
https://github.com/K-Dense-AI/scientific-agent-skills.git
synced 2026-12-04 01:24:00 +08:00
fix(analytical-method-validation): normalize JSON records like CSV
Fixes #295. Strip JSON keys and string values, preserve zero and null handling, and compare CLI results across padded JSON and CSV inputs.
This commit is contained in:
1 parent
a0aea743b3
commit
32960cda7b
3 files changed
+33
-2
No files matched your search
@@ -5,7 +5,7 @@ license: MIT
|
||||
compatibility: Requires Python 3.11+. Scripts use only the standard library - no numpy, scipy, or network access. Statistical distributions are computed from first principles so results are reproducible in any conforming interpreter.
|
||||
allowed-tools: Read Write Edit Bash
|
||||
metadata:
|
||||
version: "1.3"
|
||||
version: "1.4"
|
||||
skill-author: K-Dense Inc.
|
||||
last-reviewed: "2026-07-27"
|
||||
---
|
||||
|
||||
@@ -829,7 +829,11 @@ def parse_rows(text: str, path_hint: str | None = None) -> list[dict[str, str]]:
|
||||
for item in payload:
|
||||
if not isinstance(item, dict):
|
||||
raise InputError("JSON rows must be objects")
|
||||
rows.append({str(k): "" if v is None else str(v) for k, v in item.items()})
|
||||
# Match CSV/TSV whitespace handling while retaining null as empty.
|
||||
rows.append({
|
||||
str(k).strip(): "" if v is None else str(v).strip()
|
||||
for k, v in item.items()
|
||||
})
|
||||
if not rows:
|
||||
raise InputError("no data rows found")
|
||||
return rows
|
||||
|
||||
@@ -893,6 +893,33 @@ class TestCompareMethods(unittest.TestCase):
|
||||
|
||||
|
||||
class TestInputHandling(unittest.TestCase):
|
||||
def test_json_csv_and_tsv_normalize_the_same_record(self):
|
||||
record = {" level ": 0, " response ": " 10100 ", " sample ": " sample A ", " note ": None}
|
||||
expected = [{"level": "0", "response": "10100", "sample": "sample A", "note": ""}]
|
||||
for payload in ([record], {"rows": [record]}, {"data": [record]}):
|
||||
with self.subTest(payload=payload):
|
||||
rows = common.parse_rows(json.dumps(payload))
|
||||
self.assertEqual(rows, expected)
|
||||
common.require_columns(rows, ["level", "response"])
|
||||
for delimiter in (",", "\t"):
|
||||
with self.subTest(delimiter=delimiter):
|
||||
text = delimiter.join(record) + "\n"
|
||||
text += delimiter.join(("0", " 10100 ", " sample A ", "")) + "\n"
|
||||
self.assertEqual(common.parse_rows(text), expected)
|
||||
|
||||
def test_padded_json_produces_the_same_analysis_as_csv(self):
|
||||
rows = common.parse_rows((FIXTURES / "calibration_good.csv").read_text())
|
||||
padded = [{f" {key} ": f" {value} " for key, value in row.items()} for row in rows]
|
||||
result = subprocess.run(
|
||||
[sys.executable, str(SCRIPTS_DIR / "check_response.py"), "-i", "-", "--format", "json"],
|
||||
input=json.dumps(padded), capture_output=True, text=True, check=False,
|
||||
)
|
||||
baseline = run_script("check_response", "-i", str(FIXTURES / "calibration_good.csv"),
|
||||
"--format", "json")
|
||||
self.assertEqual(baseline.returncode, 0, baseline.stderr)
|
||||
self.assertEqual(result.returncode, 0, result.stderr)
|
||||
self.assertEqual(json.loads(result.stdout), json.loads(baseline.stdout))
|
||||
|
||||
def test_json_input_accepted(self):
|
||||
res = run_script("check_response", "-i", str(FIXTURES / "calibration_good.json"),
|
||||
"--format", "json")
|
||||
|
||||
Reference in new issue
Block a user