Skip to content

Commit

Permalink
ENH: a little pythonisation + ensure no bad entities provided
Browse files Browse the repository at this point in the history
  • Loading branch information
yarikoptic committed Mar 29, 2022
1 parent de6087c commit 40aeffc
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 9 deletions.
31 changes: 31 additions & 0 deletions heudiconv/bids/consts.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#emacs: -*- mode: python-mode; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
#ex: set sts=4 ts=4 sw=4 noet:
"""
COPYRIGHT: Yaroslav Halchenko 2014
LICENSE: MIT
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
"""

__author__ = 'yoh'
__license__ = 'MIT'

BIDS_VERSION = "1.7.0"
12 changes: 7 additions & 5 deletions heudiconv/bids/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

from heudiconv.utils import remove_prefix

from .consts import BIDS_VERSION

lgr = logging.getLogger(__name__)


Expand All @@ -31,21 +33,21 @@ class BIDSFile:
_known_entities = _load_entities_order()

def __init__(self, entities, suffix, extension):
unknown_entities = set(entities).difference(self._known_entities)
if unknown_entities:
raise ValueError(f"Unknown to BIDS {BIDS_VERSION} entities provided: {', '.join(unknown_entities)}")
self._entities = entities
self._suffix = suffix
self._extension = extension

def __eq__(self, other):
if not isinstance(other, self.__class__):
return False
if (
return (
all([other[k] == v for k, v in self._entities.items()])
and self.extension == other.extension
and self.suffix == other.suffix
):
return True
else:
return False
)

@classmethod
def parse(cls, filename):
Expand Down
4 changes: 4 additions & 0 deletions heudiconv/bids/tests/test_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ def test_BIDSFile_known_entries():
assert len(BIDSFile._known_entities) > 10 # we do have many
assert 'run' in BIDSFile._known_entities

good_entities = dict(sub='me')
BIDSFile(good_entities, None, None) # just to ensure we are good
with pytest.raises(ValueError):
BIDSFile(dict(good_entities, badentity="good"), None, None)

def test_BIDSFile():
""" Tests for the BIDSFile class """
Expand Down
4 changes: 1 addition & 3 deletions heudiconv/bids/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@
from collections import OrderedDict
from datetime import datetime
from glob import glob
from random import sample

import numpy as np

from .consts import BIDS_VERSION
from .. import __version__
from ..external.pydicom import dcm
from ..parser import find_files
Expand Down Expand Up @@ -55,8 +55,6 @@ class BIDSError(Exception):
pass


BIDS_VERSION = "1.7.0"

# List defining allowed parameter matching for fmap assignment:
SHIM_KEY = 'ShimSetting'
AllowedFmapParameterMatching = [
Expand Down
2 changes: 1 addition & 1 deletion heudiconv/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
tuneup_bids_json_files,
add_participant_record,
BIDSError,
BIDS_VERSION,
)
from .bids.consts import BIDS_VERSION
from .dicoms import (
group_dicoms_into_seqinfos,
embed_metadata_from_dicoms,
Expand Down

0 comments on commit 40aeffc

Please sign in to comment.