From ffa8e7152c52f3177966de25adedc5392a8ce732 Mon Sep 17 00:00:00 2001 From: maestroque Date: Wed, 21 Aug 2024 16:26:36 +0200 Subject: [PATCH] [test] BIDS structure creation utility function fix --- physutils/tests/test_io.py | 20 +++++++++++++++++++- physutils/tests/utils.py | 30 ++++++++++++++++++------------ 2 files changed, 37 insertions(+), 13 deletions(-) diff --git a/physutils/tests/test_io.py b/physutils/tests/test_io.py index bd4fd24..b64078a 100644 --- a/physutils/tests/test_io.py +++ b/physutils/tests/test_io.py @@ -51,7 +51,7 @@ def test_load_physio(caplog): def test_load_from_bids(): - create_random_bids_structure("physutils/tests/data") + create_random_bids_structure("physutils/tests/data", recording_id="cardiac") phys_array = io.load_from_bids( "physutils/tests/data/bids-dir", subject="01", @@ -69,6 +69,24 @@ def test_load_from_bids(): assert phys_array[col].history[0][0] == "physutils.io.load_from_bids" +def test_load_from_bids_no_rec(): + create_random_bids_structure("physutils/tests/data") + phys_array = io.load_from_bids( + "physutils/tests/data/bids-dir", + subject="01", + session="01", + task="rest", + run="01", + ) + + for col in phys_array.keys(): + assert isinstance(phys_array[col], physio.Physio) + # The data saved are the ones after t_0 = -3s + assert phys_array[col].data.size == 80000 + assert phys_array[col].fs == 10000.0 + assert phys_array[col].history[0][0] == "physutils.io.load_from_bids" + + def test_save_physio(tmpdir): pckl = io.load_physio(get_test_data_path("ECG.phys"), allow_pickle=True) out = io.save_physio(tmpdir.join("tmp").purebasename, pckl) diff --git a/physutils/tests/utils.py b/physutils/tests/utils.py index 68dee50..fe9c30f 100644 --- a/physutils/tests/utils.py +++ b/physutils/tests/utils.py @@ -82,7 +82,7 @@ def filter_physio(data, cutoffs, method, *, order=3): return filtered -def create_random_bids_structure(data_dir): +def create_random_bids_structure(data_dir, recording_id=None): dataset_description = { "Name": "Example BIDS Dataset", @@ -114,7 +114,7 @@ def create_random_bids_structure(data_dir): session_id = "01" task_id = "rest" run_id = "01" - recording_id = "cardiac" + recording_id = recording_id bids_dir = pjoin( data_dir, "bids-dir", f"sub-{subject_id}", f"ses-{session_id}", "func" @@ -125,11 +125,16 @@ def create_random_bids_structure(data_dir): with open(pjoin(data_dir, "bids-dir", "dataset_description.json"), "w") as f: json.dump(dataset_description, f, indent=4) + if recording_id is not None: + filename_body = f"sub-{subject_id}_ses-{session_id}_task-{task_id}_run-{run_id}_recording-{recording_id}" + else: + filename_body = f"sub-{subject_id}_ses-{session_id}_task-{task_id}_run-{run_id}" + # Create physio.json with open( pjoin( bids_dir, - f"sub-{subject_id}_ses-{session_id}_task-{task_id}_run-{run_id}_recording-{recording_id}_physio.json", + f"{filename_body}_physio.json", ), "w", ) as f: @@ -146,19 +151,20 @@ def create_random_bids_structure(data_dir): ) data = np.column_stack((time, np.random.rand(num_rows, num_cols - 1).round(8))) df = pd.DataFrame(data) - tsv_file = pjoin( - bids_dir, - f"sub-{subject_id}_ses-{session_id}_task-{task_id}_run-{run_id}_recording-{recording_id}_physio.tsv", - ) - df.to_csv(tsv_file, sep="\t", index=False, header=False, float_format="%.8e") - # Compress tsv file into tsv.gz + # Compress dataframe into tsv.gz tsv_gz_file = pjoin( bids_dir, - f"sub-{subject_id}_ses-{session_id}_task-{task_id}_run-{run_id}_recording-{recording_id}_physio.tsv.gz", + f"{filename_body}_physio.tsv.gz", ) - pd.read_csv(tsv_file, sep="\t").to_csv( - tsv_gz_file, sep="\t", index=False, compression="gzip" + + df.to_csv( + tsv_gz_file, + sep="\t", + index=False, + header=False, + float_format="%.8e", + compression="gzip", ) return bids_dir