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

Conversation

adelavega
Copy link
Collaborator

Fixes #940

The issue is that BIDSFile.get_entities is somewhat slow, because it has to do a SQL query. A single query is fine, but in this instance its doing it times the number of files in the result (which with no other argument, is the total number of files indexed).

Instead, I use BIDSFile.entities, which is much faster, because its an association_proxy. Essentially, instead of doing a complex query that filters, it just pulls all the entities for every file.

This results in 36x speedup for get_subjects in my example dataset (660ms -> 18ms)

The crazy thing is this can be sped up even further, by doing a direct SQL query such as:

layout.session.query(models.Tag).filter_by(entity_name='subject').distinct().with_entities(models.Tag._value).all()

This only takes 2.14ms, which is over 300x faster.

The only issue is the latter query bypasses a lot of the Python logic built into pybids classes, which do somewhat important things such as formatting run ids as PaddedInt and ensuring the are returned in the correct type, so I was not able to use this implementation.

On the whole, this makes me think that a lot of things that are "slow with pybids" need not be so...

@adelavega adelavega requested review from effigies and rwblair February 8, 2023 00:02
@codecov
Copy link

codecov bot commented Feb 8, 2023

Codecov Report

Base: 86.32% // Head: 86.35% // Increases project coverage by +0.02% 🎉

Coverage data is based on head (39b9f8a) compared to base (1ba4f66).
Patch coverage: 100.00% of modified lines in pull request are covered.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #942      +/-   ##
==========================================
+ Coverage   86.32%   86.35%   +0.02%     
==========================================
  Files          35       35              
  Lines        4022     4023       +1     
  Branches      973      974       +1     
==========================================
+ Hits         3472     3474       +2     
  Misses        355      355              
+ Partials      195      194       -1     
Impacted Files Coverage Δ
bids/layout/layout.py 88.10% <100.00%> (+0.18%) ⬆️
bids/layout/models.py 92.08% <100.00%> (+0.05%) ⬆️

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

Copy link
Collaborator

@effigies effigies left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks entirely sensible. Coverage looks reasonable. I assume you've tested it with non-entities like get_RepetitionTimes()? Or, even better, we have tests that cover that case?

bids/layout/layout.py Outdated Show resolved Hide resolved
@adelavega
Copy link
Collaborator Author

I just tested it and it works, but I added an additional get_RepetitionTimes test to make sure (couldn't find an equivalent test elsewhere).

Will merge once tests pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Poor performance of get_subjects() even when using indexing without metadata
2 participants