Skip to content

Commit

Permalink
[test] BIDS structure creation utility function fix
Browse files Browse the repository at this point in the history
  • Loading branch information
maestroque committed Aug 21, 2024
1 parent d82ef72 commit ffa8e71
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 13 deletions.
20 changes: 19 additions & 1 deletion physutils/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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)
Expand Down
30 changes: 18 additions & 12 deletions physutils/tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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"
Expand All @@ -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:
Expand All @@ -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

0 comments on commit ffa8e71

Please sign in to comment.