Skip to content

[bug] layout.get_collections('run', merge=True) extra subject 0; missing others #1275

Description

@WillForan

Using merge=True with get_collections introduces rows where subject='0'.
Without subject/session filters, get_collections('run',merge=True) also drops whole subjects.
Likely related to #604 (issues with to_df())

merge on just the first subject and session yields rows with subject as 0

layout = bids.BIDSLayout('tests/data/synthetic')
r = runc.variables['respiratory'].to_df()

runc_bug = layout.get_collections("run", subject="01", session="01", merge=True) 
runc_bug.variables['respiratory'].to_df().subject.unique()
# BUG: expect '01' get '01' and 0
# array(['01', 0], dtype=object)

No filter merge also drops subjs 02 to 04

# without subject= and session= filters, missing entire subjects
runc_all = layout.get_collections("run", merge=True)
runc_all.variables['respiratory'].to_df().subject.unique()
# BUG: expect 01-05. missing middle
# array(['01', '05', 0], dtype=object)

Without merge (no internal to_df()?), there's no subject 0 and all subject sessions are in the dat.

# as expected without merge=False
runc_nomerge = layout.get_collections("run", subject="01", session="01")
runc_nomerge[0].variables['respiratory'].to_df().subject.unique()
# array(['01'], dtype=object)

# and without filters
runc_nomerge_all = layout.get_collections("run")
len(runc_nomerge_all) # 30, subj * ses * task
[x.variables['respiratory'].to_df().subject.unique() for x in runc_nomerge_all ]
# [['01'], ['01'], ['01'], ['01'], ['01'], ['01'], ['02'], ['02'], ['02'], ['02'], ['02'], ['02'], ['03'], ['03'], ['03'], ['03'], ['03'], ['03'], ['04'], ['04'], ['04'], ['04'], ['04'], ['04'], ['05'], ['05'], ['05'], ['05'], ['05'], ['05']]

Initial debugging shows rest row (without run in entities) as introducing the subject==0 row

r = runc_bug.variables['respiratory'].to_df()
r.groupby(["subject",  "session",  "task",   "TaskName",   "run",
           "suffix", "EchoTime",  "RepetitionTime",
           "datatype", "extension",  "condition",  "duration", ]).agg(
    {k: (np.mean, np.max, lambda x: len(pd.unique(x))) 
       for k in ["amplitude", "onset"]}
)


                                                                                                          amplitude                        onset                  
                                                                                                               mean       max <lambda_0>    mean    max <lambda_0>
subject session task  TaskName run suffix EchoTime RepetitionTime datatype extension condition   duration                                                         
0       0       0     0        0   0      0.00     0.0            0        0         respiratory 0.1       0.000537  0.999607       1363  399.95  479.9       1600
01      01      nback N-Back   1   bold   0.03     2.5            func     .nii      respiratory 0.1      -0.000794  0.999792       1150   79.95  159.9       1600
                rest  Rest     2   bold   0.03     2.5            func     .nii      respiratory 0.1      -0.014537  0.999752       1145  239.95  319.9       1600

Source of the bug might be that the rest df does not have a run column

dfs = [x.variables['respiratory'].to_df() for x in runc_nomerge]
print("\n".join([str(df.head(n=2)) for df in dfs]))


   amplitude    condition EchoTime RepetitionTime TaskName datatype extension run session subject suffix   task  onset  duration
0        0.0  respiratory     0.03            2.5   N-Back     func      .nii   1      01      01   bold  nback    0.0       0.1
1        0.0  respiratory     0.03            2.5   N-Back     func      .nii   1      01      01   bold  nback    0.1       0.1
   amplitude    condition EchoTime RepetitionTime TaskName datatype extension run session subject suffix   task  onset  duration
0        0.0  respiratory     0.03            2.5   N-Back     func      .nii   2      01      01   bold  nback    0.0       0.1
1        0.0  respiratory     0.03            2.5   N-Back     func      .nii   2      01      01   bold  nback    0.1       0.1
   amplitude    condition EchoTime RepetitionTime TaskName datatype extension session subject suffix  task  onset  duration
0  -0.825171  respiratory     0.03            2.5     Rest     func      .nii      01      01   bold  rest    0.0       0.1
1  -0.788074  respiratory     0.03            2.5     Rest     func      .nii      01      01   bold  rest    0.1       0.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions