Skip to content

Commit 40aeffc

Browse files
committed
ENH: a little pythonisation + ensure no bad entities provided
1 parent de6087c commit 40aeffc

File tree

5 files changed

+44
-9
lines changed

5 files changed

+44
-9
lines changed

heudiconv/bids/consts.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#emacs: -*- mode: python-mode; py-indent-offset: 4; tab-width: 4; indent-tabs-mode: nil -*-
2+
#ex: set sts=4 ts=4 sw=4 noet:
3+
"""
4+
5+
COPYRIGHT: Yaroslav Halchenko 2014
6+
7+
LICENSE: MIT
8+
9+
Permission is hereby granted, free of charge, to any person obtaining a copy
10+
of this software and associated documentation files (the "Software"), to deal
11+
in the Software without restriction, including without limitation the rights
12+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
copies of the Software, and to permit persons to whom the Software is
14+
furnished to do so, subject to the following conditions:
15+
16+
The above copyright notice and this permission notice shall be included in
17+
all copies or substantial portions of the Software.
18+
19+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
THE SOFTWARE.
26+
"""
27+
28+
__author__ = 'yoh'
29+
__license__ = 'MIT'
30+
31+
BIDS_VERSION = "1.7.0"

heudiconv/bids/schema.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
from heudiconv.utils import remove_prefix
88

9+
from .consts import BIDS_VERSION
10+
911
lgr = logging.getLogger(__name__)
1012

1113

@@ -31,21 +33,21 @@ class BIDSFile:
3133
_known_entities = _load_entities_order()
3234

3335
def __init__(self, entities, suffix, extension):
36+
unknown_entities = set(entities).difference(self._known_entities)
37+
if unknown_entities:
38+
raise ValueError(f"Unknown to BIDS {BIDS_VERSION} entities provided: {', '.join(unknown_entities)}")
3439
self._entities = entities
3540
self._suffix = suffix
3641
self._extension = extension
3742

3843
def __eq__(self, other):
3944
if not isinstance(other, self.__class__):
4045
return False
41-
if (
46+
return (
4247
all([other[k] == v for k, v in self._entities.items()])
4348
and self.extension == other.extension
4449
and self.suffix == other.suffix
45-
):
46-
return True
47-
else:
48-
return False
50+
)
4951

5052
@classmethod
5153
def parse(cls, filename):

heudiconv/bids/tests/test_schema.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ def test_BIDSFile_known_entries():
1111
assert len(BIDSFile._known_entities) > 10 # we do have many
1212
assert 'run' in BIDSFile._known_entities
1313

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

1519
def test_BIDSFile():
1620
""" Tests for the BIDSFile class """

heudiconv/bids/utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
from collections import OrderedDict
1111
from datetime import datetime
1212
from glob import glob
13-
from random import sample
1413

1514
import numpy as np
1615

16+
from .consts import BIDS_VERSION
1717
from .. import __version__
1818
from ..external.pydicom import dcm
1919
from ..parser import find_files
@@ -55,8 +55,6 @@ class BIDSError(Exception):
5555
pass
5656

5757

58-
BIDS_VERSION = "1.7.0"
59-
6058
# List defining allowed parameter matching for fmap assignment:
6159
SHIM_KEY = 'ShimSetting'
6260
AllowedFmapParameterMatching = [

heudiconv/convert.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
tuneup_bids_json_files,
3232
add_participant_record,
3333
BIDSError,
34-
BIDS_VERSION,
3534
)
35+
from .bids.consts import BIDS_VERSION
3636
from .dicoms import (
3737
group_dicoms_into_seqinfos,
3838
embed_metadata_from_dicoms,

0 commit comments

Comments
 (0)