Skip to content

dire need: BIDSFile helper  #452

@yarikoptic

Description

@yarikoptic

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 ;)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions