Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up BIDSLayout.get(return_type='id', ...) #942

Merged
merged 5 commits into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions bids/layout/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,7 @@ def to_df(self, metadata=False, **filters):
data.loc[o] = pd.Series(dtype=float)

return data.reset_index()

adelavega marked this conversation as resolved.
Show resolved Hide resolved
def get(self, return_type='object', target=None, scope='all',
regex_search=False, absolute_paths=None, invalid_filters='error',
**filters):
Expand Down Expand Up @@ -673,10 +673,8 @@ def get(self, return_type='object', target=None, scope='all',
raise TargetError('If return_type is "id" or "dir", a valid '
'target entity must also be specified.')

metadata = target not in self.get_entities(metadata=False)

if return_type == 'id':
ent_iter = (x.get_entities(metadata=metadata) for x in results)
ent_iter = (x.entities for x in results)
results = list({
ents[target] for ents in ent_iter if target in ents
})
Expand Down
3 changes: 3 additions & 0 deletions bids/layout/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,9 @@ def get_entities(self, metadata=False, values='tags'):
A dict, where keys are entity names and values are Entity
instances.
"""
if metadata is None and values == 'tags':
return self.entities

session = object_session(self)
query = (session.query(Tag)
.filter_by(file_path=self.path)
Expand Down