Repository: bids-standard/pybids

Issue:
BIDSlayout.get_sessions() fails on openneuro ds001541
here is code to reproduce:

```
import os
from bids import BIDSLayout

os.system('datalad clone git@github.com:OpenNeuroDatasets/ds001541.git')

layout = BIDSLayout('ds001541')
layout.get_sessions()
```

should raise the following error:

```
  File "pybids_ds001541.py", line 9, in <module>
    layout.get_sessions()
  File "/Users/poldrack/anaconda3/envs/py38/lib/python3.8/site-packages/bids/layout/layout.py", line 970, in get
    results = list(set([x.entities[target] for x in results]))
TypeError: unhashable type: 'dict'
```

I wonder if it's related to different numbers of sessions across subjects:

```
ls ds001541/sub*
ds001541/sub-562:
ses-1	ses-2	ses-3	ses-4	ses-5	ses-6

ds001541/sub-572:
ses-4	ses-5	ses-7	ses-8	ses-9

ds001541/sub-573:
ses-1	ses-2	ses-3

ds001541/sub-584:
ses-1

ds001541/sub-589:
ses-1

ds001541/sub-597:
ses-1	ses-2	ses-4

ds001541/sub-598:
ses-1	ses-2	ses-3	ses-4	ses-5	ses-6	ses-7

ds001541/sub-600:
ses-1

ds001541/sub-603:
ses-1	ses-2	ses-3	ses-4	ses-5	ses-6	ses-7

ds001541/sub-620:
ses-1	ses-2	ses-3	ses-4	ses-5	ses-6	ses-7

ds001541/sub-647:
ses-1

ds001541/sub-652:
ses-1	ses-2

ds001541/sub-662:
ses-1	ses-2	ses-3

ds001541/sub-663:
ses-2

ds001541/sub-666:
ses-1

ds001541/sub-667:
ses-1	ses-2
```



Discussion:
I think it's probably that there's metadata in sessions.tsv.
I am not seeing a sessions.tsv file in the tree...

On Fri, Jun 18, 2021 at 2:53 PM Chris Markiewicz ***@***.***>
wrote:

> I think it's probably that there's metadata in sessions.tsv.
>
> —
> You are receiving this because you authored the thread.
> Reply to this email directly, view it on GitHub
> <https://github.com/bids-standard/pybids/issues/747#issuecomment-864292005>,
> or unsubscribe
> <https://github.com/notifications/unsubscribe-auth/AAGUVEDZDIOVXBQDRJIDFTLTTO56TANCNFSM466KTKYA>
> .
>


-- 
Russell A. Poldrack
Albert Ray Lang Professor of Psychology
Associate Director, Stanford Data Science
Director, SDS Center for Open and Reproducible Science
Building 420
Stanford University
Stanford, CA 94305

***@***.***
http://www.poldracklab.org/

I'm having the same issue with `get_tasks` on `narratives`.

What is happening is the by default pybids has `index_metadata=True`. What this does is index the `.json` sidecar meta-data as entities for the corresponding files. This is fine for `.json` side cars for images, but for `.tsv` side cars, this doesn't make sense. `.tsv` sidecars are descriptions of columns, not additional "entities" for the corresponding files.

Since the columns in `.tsv` files are not protected, then one could use "sessions" or "tasks" and describe those columns in a .json side car with an object: `{"session": {"Description": "session  name"}}`. This object is then entered into the `set()` operation used in `get_sessions`.

A quick fix is to set `index_metadata=False`, but this is a bug that should be fixed. 

Resolving patch:
diff --git a/bids/layout/layout.py b/bids/layout/layout.py
--- a/bids/layout/layout.py
+++ b/bids/layout/layout.py
@@ -678,7 +678,9 @@ def get(self, return_type='object', target=None, scope='all',
             results = [x for x in results if target in x.entities]
 
             if return_type == 'id':
-                results = list(set([x.entities[target] for x in results]))
+                results = list(set(
+                    [x.entities[target] for x in results 
+                     if type(x.entities[target]) is not dict]))
                 results = natural_sort(results)
 
             elif return_type == 'dir':


Test patch: