Skip to content

Commit

Permalink
Better fix for the bids_validator regression (https://github.com/bids…
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelzwiers committed Sep 3, 2024
1 parent f730c14 commit 30e43d0
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 16 deletions.
24 changes: 17 additions & 7 deletions bidscoin/bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,28 @@

# Read the BIDS schema data
with (schemafolder/'objects'/'datatypes.yaml').open('r') as _stream:
bidsdatatypesdef = yaml.load(_stream) # The valid BIDS datatypes, along with their full names and descriptions
bidsdatatypesdef = yaml.load(_stream)
"The valid BIDS datatypes, along with their full names and descriptions"
datatyperules = {}
"The entities that can/should be present for each BIDS data type"
for _datatypefile in (schemafolder/'rules'/'files'/'raw').glob('*.yaml'):
with _datatypefile.open('r') as _stream:
datatyperules[_datatypefile.stem] = yaml.load(_stream) # The entities that can/should be present for each BIDS data type
datatyperules[_datatypefile.stem] = yaml.load(_stream)
with (schemafolder/'objects'/'suffixes.yaml').open('r') as _stream:
suffixes = yaml.load(_stream) # The descriptions of the valid BIDS file suffixes
suffixes = yaml.load(_stream)
"The descriptions of the valid BIDS file suffixes"
with (schemafolder/'objects'/'entities.yaml').open('r') as _stream:
entities = yaml.load(_stream) # The descriptions of the entities present in BIDS filenames
entities = yaml.load(_stream)
"The descriptions of the entities present in BIDS filenames"
with (schemafolder/'objects'/'extensions.yaml').open('r') as _stream:
extensions = [val['value'] for key,val in yaml.load(_stream).items() if val['value'] not in ('.json','.tsv','.bval','.bvec')]
"The possible extensions of BIDS data files"
with (schemafolder/'rules'/'entities.yaml').open('r') as _stream:
entitiesorder = yaml.load(_stream) # The order in which the entities should appear within filenames
entitiesorder = yaml.load(_stream)
"The order in which the entities should appear within filenames"
with (schemafolder/'objects'/'metadata.yaml').open('r') as _stream:
metafields = yaml.load(_stream) # The descriptions of the valid BIDS metadata fields
metafields = yaml.load(_stream)
"The descriptions of the valid BIDS metadata fields"


class DataSource:
Expand Down Expand Up @@ -1093,7 +1102,8 @@ def validate_bidsmap(bidsmap: Bidsmap, level: int=1) -> bool:
ignore = check_ignore(datatype, bidsignore) or check_ignore(bidsname+'.json', bidsignore, 'file')
ignore_1 = datatype in ignoretypes or ignore
ignore_2 = datatype in ignoretypes
bidstest = bids_validator.BIDSValidator().is_bids(f"/sub-{sanitize(dataformat)}/{datatype}/{bidsname}.nii") # NB: This used to be '.json', which is more generic (but see https://github.com/bids-standard/bids-validator/issues/2113)
for ext in extensions: # NB: `ext` used to be '.json', which is more generic (but see https://github.com/bids-standard/bids-validator/issues/2113)
if bidstest := bids_validator.BIDSValidator().is_bids(f"/sub-{sanitize(dataformat)}/{datatype}/{bidsname}{ext}"): break
if level==3 or (abs(level)==2 and not ignore_2) or (-2<level<2 and not ignore_1):
valid = valid and bidstest
if (level==0 and not bidstest) or (level==1 and not ignore_1) or (level==2 and not ignore_2) or level==3:
Expand Down
12 changes: 6 additions & 6 deletions bidscoin/bidseditor.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
if find_spec('bidscoin') is None:
sys.path.append(str(Path(__file__).parents[1]))
from bidscoin import bcoin, bids, bidsversion, check_version, trackusage, bidsmap_template, __version__
from bidscoin.bids import Bidsmap, Plugin, Run
from bidscoin.bids import Bidsmap, Plugin, Run, extensions


ROW_HEIGHT = 22
Expand Down Expand Up @@ -1532,12 +1532,12 @@ def accept_run(self):
"""Save the changes to the target_bidsmap and send it back to the main window: Finished!"""

# Check if the bidsname is valid
bidsname = Path(self.bidsname_textbox.toPlainText())
validrun = False not in bids.check_run(self.target_datatype, self.target_run, checks=(False, False, False))[1:3]
bidsname = Path(self.bidsname_textbox.toPlainText())
validrun = False not in bids.check_run(self.target_datatype, self.target_run, checks=(False, False, False))[1:3]
bidsvalid = validrun
if not (bids.check_ignore(self.target_datatype,self.bidsignore) or bids.check_ignore(bidsname.name,self.bidsignore,'file') or self.target_datatype in self.ignoredatatypes):
bidsvalid = BIDSValidator().is_bids((Path('/')/self.subid/self.sesid/bidsname).with_suffix('.json').as_posix())
else:
bidsvalid = validrun
for ext in extensions: # NB: `ext` used to be '.json', which is more generic (but see https://github.com/bids-standard/bids-validator/issues/2113)
if bidsvalid := BIDSValidator().is_bids((Path('/')/self.subid/self.sesid/bidsname).with_suffix(ext).as_posix()): break

# If the bidsname is not valid, ask the user if that's OK
message = ''
Expand Down
2 changes: 1 addition & 1 deletion bidscoin/plugins/dcm2niix2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ def bidscoiner_plugin(session: Path, bidsmap: Bidsmap, bidsses: Path) -> Union[N
targets = set() # -> A store for all fullpath output targets (.nii/.tsv) for this bidsname

# Check if the bidsname is valid
bidstest = (Path('/')/subid/sesid/datasource.datatype/bidsname).with_suffix('.json').as_posix()
bidstest = (Path('/')/subid/sesid/datasource.datatype/bidsname).with_suffix('.nii').as_posix()
isbids = BIDSValidator().is_bids(bidstest)
if not isbids and not ignore:
LOGGER.warning(f"The '{bidstest}' output name did not pass the bids-validator test")
Expand Down
2 changes: 1 addition & 1 deletion bidscoin/plugins/nibabel2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ def bidscoiner_plugin(session: Path, bidsmap: Bidsmap, bidsses: Path) -> None:
target = (outfolder/bidsname).with_suffix(ext)

# Check if the bidsname is valid
bidstest = (Path('/')/subid/sesid/datasource.datatype/bidsname).with_suffix('.json').as_posix()
bidstest = (Path('/')/subid/sesid/datasource.datatype/bidsname).with_suffix('.nii').as_posix()
isbids = BIDSValidator().is_bids(bidstest)
if not isbids and not bidsignore:
LOGGER.warning(f"The '{bidstest}' output name did not pass the bids-validator test")
Expand Down
2 changes: 1 addition & 1 deletion bidscoin/plugins/spec2nii2bids.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ def bidscoiner_plugin(session: Path, bidsmap: Bidsmap, bidsses: Path) -> Union[N
target = (outfolder/bidsname).with_suffix('.nii.gz')

# Check if the bidsname is valid
bidstest = (Path('/')/subid/sesid/datasource.datatype/bidsname).with_suffix('.json').as_posix()
bidstest = (Path('/')/subid/sesid/datasource.datatype/bidsname).with_suffix('.nii').as_posix()
isbids = BIDSValidator().is_bids(bidstest)
if not isbids and not bidsignore:
LOGGER.warning(f"The '{bidstest}' output name did not pass the bids-validator test")
Expand Down

0 comments on commit 30e43d0

Please sign in to comment.