|
| 1 | +"""Tests for physutils.tasks and their integration.""" |
| 2 | + |
| 3 | +import os |
| 4 | + |
| 5 | +import physutils.tasks as tasks |
| 6 | +from physutils import physio |
| 7 | +from physutils.tests.utils import create_random_bids_structure |
| 8 | + |
| 9 | + |
| 10 | +def test_generate_physio_phys_file(): |
| 11 | + """Test generate_physio task.""" |
| 12 | + physio_file = os.path.abspath("physutils/tests/data/ECG.phys") |
| 13 | + task = tasks.generate_physio(input_file=physio_file, mode="physio") |
| 14 | + assert task.inputs.input_file == physio_file |
| 15 | + assert task.inputs.mode == "physio" |
| 16 | + assert task.inputs.fs is None |
| 17 | + |
| 18 | + task() |
| 19 | + |
| 20 | + physio_obj = task.result().output.out |
| 21 | + assert isinstance(physio_obj, physio.Physio) |
| 22 | + assert physio_obj.fs == 1000 |
| 23 | + assert physio_obj.data.shape == (44611,) |
| 24 | + |
| 25 | + |
| 26 | +def test_generate_physio_bids_file(): |
| 27 | + """Test generate_physio task.""" |
| 28 | + create_random_bids_structure("physutils/tests/data", recording_id="cardiac") |
| 29 | + bids_parameters = { |
| 30 | + "subject": "01", |
| 31 | + "session": "01", |
| 32 | + "task": "rest", |
| 33 | + "run": "01", |
| 34 | + "recording": "cardiac", |
| 35 | + } |
| 36 | + bids_dir = os.path.abspath("physutils/tests/data/bids-dir") |
| 37 | + task = tasks.generate_physio( |
| 38 | + input_file=bids_dir, |
| 39 | + mode="bids", |
| 40 | + bids_parameters=bids_parameters, |
| 41 | + col_physio_type="cardiac", |
| 42 | + ) |
| 43 | + |
| 44 | + assert task.inputs.input_file == bids_dir |
| 45 | + assert task.inputs.mode == "bids" |
| 46 | + assert task.inputs.fs is None |
| 47 | + assert task.inputs.bids_parameters == bids_parameters |
| 48 | + assert task.inputs.col_physio_type == "cardiac" |
| 49 | + |
| 50 | + task() |
| 51 | + |
| 52 | + physio_obj = task.result().output.out |
| 53 | + assert isinstance(physio_obj, physio.Physio) |
| 54 | + |
| 55 | + |
| 56 | +def test_generate_physio_auto(): |
| 57 | + create_random_bids_structure("physutils/tests/data", recording_id="cardiac") |
| 58 | + bids_parameters = { |
| 59 | + "subject": "01", |
| 60 | + "session": "01", |
| 61 | + "task": "rest", |
| 62 | + "run": "01", |
| 63 | + "recording": "cardiac", |
| 64 | + } |
| 65 | + bids_dir = os.path.abspath("physutils/tests/data/bids-dir") |
| 66 | + task = tasks.generate_physio( |
| 67 | + input_file=bids_dir, |
| 68 | + mode="auto", |
| 69 | + bids_parameters=bids_parameters, |
| 70 | + col_physio_type="cardiac", |
| 71 | + ) |
| 72 | + |
| 73 | + assert task.inputs.input_file == bids_dir |
| 74 | + assert task.inputs.mode == "auto" |
| 75 | + assert task.inputs.fs is None |
| 76 | + assert task.inputs.bids_parameters == bids_parameters |
| 77 | + assert task.inputs.col_physio_type == "cardiac" |
| 78 | + |
| 79 | + task() |
| 80 | + |
| 81 | + physio_obj = task.result().output.out |
| 82 | + assert isinstance(physio_obj, physio.Physio) |
| 83 | + |
| 84 | + |
| 85 | +def test_generate_physio_auto_error(caplog): |
| 86 | + bids_dir = os.path.abspath("physutils/tests/data/non-bids-dir") |
| 87 | + task = tasks.generate_physio( |
| 88 | + input_file=bids_dir, |
| 89 | + mode="auto", |
| 90 | + col_physio_type="cardiac", |
| 91 | + ) |
| 92 | + |
| 93 | + assert task.inputs.input_file == bids_dir |
| 94 | + assert task.inputs.mode == "auto" |
| 95 | + assert task.inputs.fs is None |
| 96 | + assert task.inputs.col_physio_type == "cardiac" |
| 97 | + |
| 98 | + try: |
| 99 | + task() |
| 100 | + except Exception: |
| 101 | + assert caplog.text.count("ERROR") == 1 |
| 102 | + assert ( |
| 103 | + caplog.text.count( |
| 104 | + "dataset_description.json' is missing from project root. Every valid BIDS dataset must have this file." |
| 105 | + ) |
| 106 | + == 1 |
| 107 | + ) |
0 commit comments