heudiconv
heudiconv copied to clipboard
dire need: BIDSFile helper
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 ;)
@pvelasco -- ha, I started to RF to address this issue and I see that you have implemented BIDSFile helper already in https://github.com/cbinyu/heudiconv/commit/2886525ccff3c0592f18490b9cface731a148661 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.