diff --git a/bidsmreye/visualize.py b/bidsmreye/visualize.py index c84a6eb..d4f9305 100644 --- a/bidsmreye/visualize.py +++ b/bidsmreye/visualize.py @@ -70,6 +70,8 @@ def collect_group_qc_data(cfg: Config) -> pd.DataFrame | None: with open(file) as f: data = json.loads(f.read()) + print(data) + df = pd.json_normalize(data) df["filename"] = Path(file).name df["subject"] = entities["subject"] @@ -87,7 +89,14 @@ def collect_group_qc_data(cfg: Config) -> pd.DataFrame | None: "eye1XVar", "eye1YVar", ] - qc_data = qc_data[cols] + try: + qc_data = qc_data[cols] + except KeyError: + log.error( + f"""Sidecar files seem to be missing the keys: {cols}. + To fix try to run the qc at the participant level first.""" + ) + return None return qc_data diff --git a/tests/test_visualize.py b/tests/test_visualize.py index acec136..ab97ba1 100644 --- a/tests/test_visualize.py +++ b/tests/test_visualize.py @@ -1,5 +1,6 @@ from __future__ import annotations +import json import shutil import pandas as pd @@ -32,6 +33,31 @@ def test_group_report(tmp_path, data_dir): assert (target_dir / "group_eyetrack.tsv").exists() +def test_group_report_missing_qc(tmp_path, data_dir): + """Regression test for https://github.com/cpp-lln-lab/bidsMReye/issues/171 .""" + src_dir = data_dir / "derivatives" / "bidsmreye" + target_dir = tmp_path / "bidsmreye" + target_dir.mkdir() + shutil.copytree(src_dir, target_dir, dirs_exist_ok=True) + + file_to_modify = ( + target_dir + / "sub-9001" + / "ses-1" + / "func" + / "sub-9001_ses-1_task-rest_desc-bidsmreye_eyetrack.json" + ) + + with open(file_to_modify, "w") as f: + json.dump({"SamplingFrequency": 0.14285714285714285}, f) + + cfg = Config(input_dir=target_dir, output_dir=target_dir.parent, subjects=["9001"]) + + group_report(cfg) + + assert not (target_dir / "group_eyetrack.html").exists() + + def test_group_report_cli(tmp_path, data_dir): src_dir = data_dir / "derivatives" / "bidsmreye"