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

Add filtering functionality to bids2table #6

Closed
wants to merge 4 commits into from

Conversation

ReinderVosDeWael
Copy link
Contributor

This PR adds filtering functionality to bids2table. It can be invoked with lists of values (that are considered an OR), or single values (equivalent to a list of a single value). See example usage below.

import bids2table

bids2table.bids2table(BIDS_DIR, filters={'datatype': ['func', 'anat'], 'suffix': 'T1w'})

Specifically, this PR adds the following:

  • A _filter method to _bids2table.py
  • Unit tests for _filter, including a workflow to run them
  • Updates the example notebook to include the new functionality
  • A exceptions.py for custom exceptions.

@ReinderVosDeWael ReinderVosDeWael requested a review from clane9 July 10, 2023 16:31
@ReinderVosDeWael ReinderVosDeWael self-assigned this Jul 10, 2023
Copy link
Collaborator

@clane9 clane9 left a comment

Choose a reason for hiding this comment

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

Thanks for the PR @ReinderVosDeWael! Left a couple comments. And then I'll be glad to merge.

example/example.ipynb Outdated Show resolved Hide resolved
pyproject.toml Outdated Show resolved Hide resolved
try:
df = df[df["entities"][key].isin(value)]
except KeyError as exc_info:
raise exceptions.InvalidFilterError(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think I would catch and re-raise a custom exception. I would just let the KeyError be raised. Just as informative I think and less code.

Copy link
Contributor Author

@ReinderVosDeWael ReinderVosDeWael Jul 11, 2023

Choose a reason for hiding this comment

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

For a developer like you or myself you're right. However, my idea here is to raise an error that is informative to someone who has never opened the source code. Consider someone who calls bids2table(..., filters={...}) If they get thrown a KeyError at line 140 at df = df[df["entities"][key].isin(value)] then they'd have to go through the stack trace and figure out from there that this key variable refers to one of their filters. That's a commitment beyond many end-users. If we're lucky they launch an issue for it, if we're unlucky they'll move to a different package. If they get thrown a InvalidFilterError then they would barely have to glance at the error description to know that it's a user error.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Fair point. The default KeyError message is very minimal and would probably be confusing. How about we re-raise KeyError with a more informative message? In general, I prefer not to use custom exceptions unless there's no good fit among the built-in exceptions or I need to handle the exception specially.

.github/workflows/test.yaml Outdated Show resolved Hide resolved
]
}
],
"outputs": [],
Copy link
Collaborator

Choose a reason for hiding this comment

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

The table generation progress log is now missing. Not sure why. Perhaps a restart and run all would fix it?

@clane9
Copy link
Collaborator

clane9 commented Aug 7, 2023

Superseded by #17.

@clane9 clane9 closed this Aug 7, 2023
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.

2 participants