From e10c9b2a04b8b4be68dd8613e4b02a78f0a903fc Mon Sep 17 00:00:00 2001 From: maestroque Date: Tue, 27 Aug 2024 20:13:10 +0300 Subject: [PATCH] Verify transform_to_physio for BIDS files --- physutils/tasks.py | 10 ++++++++-- physutils/tests/test_tasks.py | 31 +++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 2 deletions(-) diff --git a/physutils/tasks.py b/physutils/tasks.py index 587ca73..084d942 100644 --- a/physutils/tasks.py +++ b/physutils/tasks.py @@ -10,7 +10,9 @@ @pydra.mark.task -def transform_to_physio(input_file: str, mode="physio", fs=None) -> Physio: +def transform_to_physio( + input_file: str, mode="physio", fs=None, bids_parameters=dict(), bids_channel=None +) -> Physio: LGR.debug(f"Loading physio object from {input_file}") if not fs: fs = None @@ -22,7 +24,11 @@ def transform_to_physio(input_file: str, mode="physio", fs=None) -> Physio: physio_obj = load_physio(input_file, allow_pickle=True) elif mode == "bids": - physio_obj = load_from_bids(input_file) + if bids_parameters is {}: + raise ValueError("BIDS parameters must be provided when loading from BIDS") + else: + physio_array = load_from_bids(input_file, **bids_parameters) + physio_obj = physio_array[bids_channel] else: raise ValueError(f"Invalid transform_to_physio mode: {mode}") return physio_obj diff --git a/physutils/tests/test_tasks.py b/physutils/tests/test_tasks.py index 6d648d7..8198a91 100644 --- a/physutils/tests/test_tasks.py +++ b/physutils/tests/test_tasks.py @@ -4,6 +4,7 @@ import physutils.tasks as tasks from physutils import physio +from physutils.tests.utils import create_random_bids_structure def test_transform_to_physio_phys_file(): @@ -20,3 +21,33 @@ def test_transform_to_physio_phys_file(): assert isinstance(physio_obj, physio.Physio) assert physio_obj.fs == 1000 assert physio_obj.data.shape == (44611,) + + +def test_transform_to_physio_bids_file(): + """Test transform_to_physio task.""" + create_random_bids_structure("physutils/tests/data", recording_id="cardiac") + bids_parameters = { + "subject": "01", + "session": "01", + "task": "rest", + "run": "01", + "recording": "cardiac", + } + bids_dir = os.path.abspath("physutils/tests/data/bids-dir") + task = tasks.transform_to_physio( + input_file=bids_dir, + mode="bids", + bids_parameters=bids_parameters, + bids_channel="cardiac", + ) + + assert task.inputs.input_file == bids_dir + assert task.inputs.mode == "bids" + assert task.inputs.fs is None + assert task.inputs.bids_parameters == bids_parameters + assert task.inputs.bids_channel == "cardiac" + + task() + + physio_obj = task.result().output.out + assert isinstance(physio_obj, physio.Physio)