Skip to content

Commit e6f2d22

Browse files
committed
ENH check that run_onsets does not give empty runs
1 parent 1ca3519 commit e6f2d22

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

voxelwise_tutorials/tests/test_mappers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Unit tests on the mappers.
22
3-
Requires the movies 4T dataset locally.
3+
Requires the movies 3T dataset locally.
44
"""
55

66
import os
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import numpy as np
2+
import pytest
3+
from voxelwise_tutorials.utils import generate_leave_one_run_out
4+
5+
6+
def test_generate_leave_one_run_out_disjoint():
7+
n_samples = 40
8+
run_onsets = [0, 10, 20, 30]
9+
10+
for train, val in generate_leave_one_run_out(n_samples, run_onsets):
11+
assert len(train) > 0
12+
assert len(val) > 0
13+
assert not np.any(np.isin(train, val))
14+
assert not np.any(np.isin(val, train))
15+
16+
17+
@pytest.mark.parametrize("run_onsets",
18+
[[0, 10, 20, 30, 40], [0, 10, 20, 20, 30]])
19+
def test_generate_leave_one_run_out_empty_runs(run_onsets):
20+
n_samples = 40
21+
with pytest.raises(ValueError):
22+
list(generate_leave_one_run_out(n_samples, run_onsets))

voxelwise_tutorials/utils.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ def generate_leave_one_run_out(n_samples, run_onsets, random_state=None,
3838

3939
all_samples = np.arange(n_samples)
4040
runs = np.split(all_samples, run_onsets[1:])
41+
if any(len(run) == 0 for run in runs):
42+
raise ValueError("Some runs have no samples. Check that run_onsets "
43+
"does not include any repeated index, nor the last "
44+
"index.")
4145

4246
for val_runs in all_val_runs.T:
4347
train = np.hstack(

0 commit comments

Comments
 (0)