Skip to content

Commit

Permalink
Merge pull request #1034 from tsalo/test-false-metadata
Browse files Browse the repository at this point in the history
FIX: Ensure values of boolean metadata fields are preserved
  • Loading branch information
effigies authored Nov 30, 2023
2 parents 8fb92fa + db47dbd commit a7c8554
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bids/layout/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -698,6 +698,9 @@ def _init_on_load(self):
if self._dtype == 'json':
self.value = json.loads(self._value)
self.dtype = 'json'
elif self._dtype == 'bool':
self.value = self._value == 'True'
self.dtype = bool
else:
self.dtype = type_map[self._dtype]
self.value = self.dtype(self._value)
Expand Down
42 changes: 42 additions & 0 deletions bids/layout/tests/test_layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ def test_derivative_datasets_load_with_no_validation(self, dataset_path):
assert len(layout.get()) == 4
assert len(layout.get(desc="preproc")) == 3


def test_get_metadata(layout_7t_trt):
target = 'sub-03/ses-2/func/sub-03_ses-2_task-' \
'rest_acq-fullbrain_run-2_bold.nii.gz'
Expand Down Expand Up @@ -244,6 +245,21 @@ def test_get_metadata4(layout_ds005):
assert result == {}


def test_get_metadata_error2(layout_7t_trt):
"""Ensure that False boolean fields are retained as False."""
target = 'sub-03/ses-2/func/sub-03_ses-2_task-' \
'rest_acq-fullbrain_run-2_bold.nii.gz'
target = target.split('/')
# Check get_metadata from the BIDSLayout
result = layout_7t_trt.get_metadata(join(layout_7t_trt.root, *target))
assert result['MTState'] is False

# Check get_metadata from the BIDSFile
files = layout_7t_trt.get(task="rest", acquisition="fullbrain", suffix="bold", extension=".nii.gz")
result = files[0].get_metadata()
assert result['MTState'] is False


def test_get_metadata_meg(layout_ds117):
funcs = ['get_subjects', 'get_sessions', 'get_tasks', 'get_runs',
'get_acquisitions', 'get_procs']
Expand Down Expand Up @@ -278,6 +294,32 @@ def test_get_metadata_via_bidsfile(layout_7t_trt):
assert 'subject' not in result


def test_metadata_equivalence(layout_7t_trt):
"""Ensure that JSON metadata is not corrupted by the layout."""
bold = layout_7t_trt.get(
subject='02', acquisition='fullbrain', suffix='bold', extension='.nii.gz'
)[0]
root_metadata_file = layout_7t_trt.get(
subject=None, acquisition='fullbrain', suffix='bold', extension='.json'
)[0]

assert bold.get_metadata() == root_metadata_file.get_dict()

physio = layout_7t_trt.get(
subject='02', acquisition='fullbrain', suffix='physio', extension='.tsv.gz'
)[0]
root_metadata_file = layout_7t_trt.get(
subject=None, acquisition='fullbrain', suffix='physio', extension='.json'
)[0]

assert physio.get_metadata() == root_metadata_file.get_dict()

# Verify that we cover all common metadata types
types = {type(val) for val in bold.get_metadata().values()}
assert types == {str, float, bool, list}
assert isinstance(physio.get_metadata()['StartTime'], int)


def test_get_metadata_error(layout_7t_trt):
''' Same as test_get_metadata5, but called through BIDSFile. '''
target = 'sub-01/ses-1/func/sub-01_ses-1_task-rest_acq-fullbrain_run-1_bold.nii.gz'
Expand Down
1 change: 1 addition & 0 deletions bids/tests/data/7t_trt/task-rest_acq-fullbrain_bold.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"CogAtlasID": "trm_4c8a834779883",
"EchoTime": 0.017,
"EffectiveEchoSpacing": 0.0003333262223739227,
"MTState": false,
"PhaseEncodingDirection": "j-",
"RepetitionTime": 3.0,
"SliceEncodingDirection": "k",
Expand Down

0 comments on commit a7c8554

Please sign in to comment.