question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`build_path` chooses the first pattern that matches, even when a better match exists

See original GitHub issue

It looks like BIDSLayout.build_path() goes with the first path pattern it finds for which all required entities are given. I ran into a situation where this was blocking access to a better pattern later in the list. A minimal example:

from bids.layout import BIDSLayout
data=BIDSLayout('/path/to/my/BIDS/data')
entities = {'subject':'001', 
    'suffix':'T1w',
    'extension':'.nii.gz',
    'space':'MNI' }
patterns = ['sub-{subject}[/ses-{session}]/anat/sub-{subject}[_ses-{session}][_acq-{acquisition}][_ce-{ceagent}][_rec-{reconstruction}]_{suffix<T1w|T2w|T1rho|T1map|T2map|T2star|FLAIR|FLASH|PDmap|PD|PDT2|inplaneT[12]|angio>}.{extension<nii|nii.gz|json>|nii.gz}',
            'sub-{subject}[/ses-{session}]/anat/sub-{subject}[_ses-{session}][_acq-{acquisition}][_ce-{ceagent}][_rec-{reconstruction}][_space-{space}][_desc-{desc}]_{suffix<T1w|T2w|T1rho|T1map|T2map|T2star|FLAIR|FLASH|PDmap|PD|PDT2|inplaneT[12]|angio>}.{extension<nii|nii.gz|json>|nii.gz}']
print(data.build_path(entities, patterns, validate=False))
print(data.build_path(entities, patterns[1:], validate=False))

outputs the following:

sub-001/anat/sub-001_T1w.nii.gz
sub-001/anat/sub-001_space-MNI_T1w.nii.gz

Including the first pattern causes the 2nd to never be reached, even though it is better: patterns[1] makes use of the ‘space’ entity which is absent in patterns[0]. Users can work around this by giving the exact patterns they want and no others, but it would be nice if pybids tried to find a pattern which matched all given entities, or failing that, as many as possible.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12

github_iconTop GitHub Comments

1reaction
effigiescommented, Oct 1, 2019

The idea is that if extension isn’t present, nii.gz would be the default.

1reaction
effigiescommented, Oct 1, 2019

Okay. Could be that the handling of defaults and/or choices needs fixing up. I haven’t looked into it very much at this point, but it definitely seems like something that would be easy to get wrong. We have an open issue for really stress testing the path patterns: #478.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Regular expression to stop at first match - regex
You need to make your regular expression lazy/non-greedy, because by default, "(.*)" will match all of "file path/level1/level2" xxx ...
Read more >
4. Pattern Matching with Regular Expressions - Java ...
Returns the characters matched by group i of the current match, if i is greater than or equal to zero and less than...
Read more >
Best Practices for Regular Expressions in .NET
This article outlines some of the best practices that developers can adopt to ensure ... Text that matches the regular expression pattern.
Read more >
Pattern Matching
Choose what to do when multiple matches exist for the pattern: First match only: returns the first value that matches the pattern.
Read more >
re — Regular expression operations — Python 3.11.1 ...
When one pattern completely matches, that branch is accepted. This means that once A matches, B will not be tested further, even if...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found