Skip to content

Commit 7af6c9e

Browse files
authored
Merge pull request #822 from effigies/fix/dummy_groups
FIX: Allow grouping by run and session when entities are undefined
2 parents 8aba94a + 1b22dd1 commit 7af6c9e

File tree

2 files changed

+11
-6
lines changed

2 files changed

+11
-6
lines changed

bids/modeling/statsmodels.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -337,21 +337,24 @@ def _build_groups(objects, group_by):
337337

338338
groups = defaultdict(list)
339339

340-
# sanitize grouping entities, otherwise weird things can happen
341-
group_by = list(set(group_by) & VALID_GROUPING_ENTITIES)
342-
343340
# Get unique values in each grouping variable and construct indexing DF
344341
entities = [obj.entities for obj in objects]
345342
df = pd.DataFrame.from_records(entities)
346343

344+
# Single-run tasks and single-session subjects may not have entities
345+
dummy_groups = {"run", "session"} - set(df.columns)
346+
347+
# sanitize grouping entities, otherwise weird things can happen
348+
group_by = set(group_by) & VALID_GROUPING_ENTITIES - dummy_groups
349+
347350
# Verify all columns in group_by exist and raise sensible error if not
348-
missing_vars = list(set(group_by) - set(df.columns))
351+
missing_vars = list(group_by - set(df.columns))
349352
if missing_vars:
350353
raise ValueError("group_by contains variable(s) {} that could not "
351354
"be found in the entity index.".format(missing_vars) )
352355

353356
# Restrict DF to only grouping columns
354-
df = df.loc[:, group_by]
357+
df = df.loc[:, list(group_by)]
355358

356359
unique_vals = {col: df[col].dropna().unique().tolist() for col in group_by}
357360

bids/modeling/tests/test_statsmodels.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,9 @@ def test_entire_graph_smoketest(graph):
105105
# At dataset level, do one-sample t-tests separately for each gender,
106106
# but also two-sample t-tests comparing males and females.
107107
# Note that there are only 2 subjects in the graph.
108-
outputs = graph["run"].run(group_by=['subject', 'run'])
108+
# Note also that there is only one session (with no session label), which
109+
# should have no effect as a grouping variable
110+
outputs = graph["run"].run(group_by=['subject', 'session', 'run'])
109111
# 2 subjects x 3 runs
110112
assert len(outputs) == 6
111113
cis = list(chain(*[op.contrasts for op in outputs]))

0 commit comments

Comments
 (0)