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

dire need: BIDSFile helper #452

Open
yarikoptic opened this issue May 13, 2020 · 1 comment · May be fixed by #544
Open

dire need: BIDSFile helper #452

yarikoptic opened this issue May 13, 2020 · 1 comment · May be fixed by #544
Milestone

Comments

@yarikoptic
Copy link
Member

To be used in https://github.com/nipy/heudiconv/pull/441/files#diff-a48009f9efe6cc64ffdb74d06e831296R370 and in code introduced in #424 (number of locations like https://github.com/nipy/heudiconv/pull/424/files#diff-a48009f9efe6cc64ffdb74d06e831296R284). We need some lightweight (not yet full pybids) parsing of BIDS filenames, so we could

  • easily query present in them entities (_key-value pairs), _suffix, and possibly .extension (everything after '.`) tune up
  • easily check for any of the values being present
  • set and/or explicitly override (we might want to distinguish those two operations) an existing value for a key
  • reconstruct back string form accounting for entities order defined in https://bids-specification.readthedocs.io/en/stable/99-appendices/04-entity-table.html

So I envision a dict like interface (but probably not a sub-class of dict), alike

class BIDSFile(object):
 # as defined in https://bids-specification.readthedocs.io/en/stable/99-appendices/04-entity-table.html
 # which might soon become machine readable
 # order matters
 _known_entities = ['sub', 'ses', ...]
 def __init__(self, entities, suffix, extension):
   self._entities = entities
   self._suffix = suffix
   self._extension = extension

 @classmethod
 def parse(filename):
     # use re.findall to find all key pairs, and then leftover might start with _ which would be suffix
     return BIDSFile(entities, suffix, extension)
 def __str__(self):
     # here reconstitute in a legit BIDS filename using the order from entity table
     # using self._known_entities
     # puke if mandatory `sub` is not known!
 def __getitem__(self, entity):
     return self._entities[entity]
 def __setitem__(self, entity, value):  # would puke with some exception if already known
     return self.set(entity, value, overwrite=False)
 def set(self, entity, value, overwrite=True):
     ...
 @property  # as needed make them RW
 def suffix(self):
    return self._suffix 
 @property 
 def extension(self):
    return self.extension

d'oh -- got carried away and almost coded it all up... so - as you can see - it should be a simple thing , just a few lines missing + tests ;) but it would provide super-powers at many points in the code ;)

@yarikoptic yarikoptic added this to the 0.8.1 milestone May 13, 2020
@yarikoptic yarikoptic modified the milestones: 0.9.0, 0.10.0 Dec 23, 2020
pvelasco added a commit to cbinyu/heudiconv that referenced this issue Jun 21, 2021
... to help parse entities from BIDS filenames, as suggested in nipy#452.

It adds one use case (in `get_key_info_for_fmap_assignment`), and the corresponding unit tests.

TO DO: RF the code using BIDSFile whenever we need to parse a BIDS filename.
@yarikoptic
Copy link
Member Author

yarikoptic commented Feb 7, 2022

@pvelasco -- ha, I started to RF to address this issue and I see that you have implemented BIDSFile helper already in cbinyu@2886525 etc. So that I neither have to "steal" nor "duplicate" your work, could you please add your code (submit a PR from the top of that branch) to https://github.com/nipy/heudiconv/tree/enh-bids to replace my initial class https://github.com/nipy/heudiconv/blob/enh-bids/heudiconv/bids/schema.py#L18 -- just keep that loading of entities from files (could even make it into a lazy property which would load only after first asked for values).

If you have no time, no problem -- let me know. I will "steal" and tune your code then into a PR.

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 a pull request may close this issue.

1 participant