Skip to content

Commit 48fb815

Browse files
committed
FIX: Allow grouping by run and session when entities are undefined
1 parent 8aba94a commit 48fb815

File tree

1 file changed

+8
-5
lines changed

1 file changed

+8
-5
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

0 commit comments

Comments
 (0)