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)