Skip to content

Commit 53fb7ba

Browse files
committed
add get_dataset_description method
1 parent f73379d commit 53fb7ba

File tree

2 files changed

+40
-7
lines changed

2 files changed

+40
-7
lines changed

bids/layout/layout.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -417,8 +417,8 @@ def get_entities(self, scope='all', metadata=None):
417417
418418
Args:
419419
scope (str): The scope of the search space. Indicates which
420-
BIDSLayouts' entities to extract. See BIDSLayout docstring
421-
for valid values.
420+
BIDSLayouts' entities to extract. See BIDSLayout.get()
421+
docstring for valid values.
422422
metadata (bool, None): By default (None), all available entities
423423
are returned. If True, only entities found in metadata files
424424
(and not defined for filenames) are returned. If False, only
@@ -444,8 +444,8 @@ def get_files(self, scope='all'):
444444
445445
Args:
446446
scope (str): The scope of the search space. Indicates which
447-
BIDSLayouts' entities to extract. See BIDSLayout docstring
448-
for valid values.
447+
BIDSLayouts' entities to extract. See BIDSLayout.get()
448+
docstring for valid values.
449449
450450
Returns: A dict, where keys are file paths and values are BIDSFile
451451
instances.
@@ -469,8 +469,8 @@ def parse_file_entities(self, filename, scope='all', entities=None,
469469
Args:
470470
filename (str): The filename to parse for entity values
471471
scope (str, list): The scope of the search space. Indicates which
472-
BIDSLayouts' entities to extract. See BIDSLayout docstring
473-
for valid values. By default, extracts all entities.
472+
BIDSLayouts' entities to extract. See BIDSLayout.get()
473+
docstring for valid values. By default, extracts all entities.
474474
entities (list): An optional list of Entity instances to use in
475475
extraction. If passed, the scope and config arguments are
476476
ignored, and only the Entities in this list are used.
@@ -763,7 +763,7 @@ def get_file(self, filename, scope='all'):
763763
an absolute path, or relative to the root of this BIDSLayout.
764764
scope (str, list): Scope of the search space. If passed, only
765765
BIDSLayouts that match the specified scope will be
766-
searched. See BIDSLayout docstring for valid values.
766+
searched. See BIDSLayout.get() docstring for valid values.
767767
768768
Returns: A BIDSFile, or None if no match was found.
769769
'''
@@ -877,6 +877,27 @@ def get_metadata(self, path, include_entities=False, scope='all'):
877877

878878
return {}
879879

880+
def get_dataset_description(self, scope='self', all_=False):
881+
''' Return contents of dataset_description.json.
882+
883+
Args:
884+
scope (str): The scope of the search space. Only descriptions of
885+
BIDSLayouts that match the specified scope will be returned.
886+
See BIDSLayout.get() docstring for valid values. Defaults to
887+
'self'--i.e., returns the dataset_description.json file for
888+
only the directly-called BIDSLayout.
889+
all_ (bool): If True, returns a list containing descriptions for
890+
all matching layouts. If False (default), returns for only the
891+
first matching layout.
892+
893+
Returns: a dictionary or list of dictionaries (depending on all_).
894+
'''
895+
layouts = self._get_layouts_in_scope(scope)
896+
if not all_:
897+
return layouts[0].get_file('dataset_description.json').get_dict()
898+
return [l.get_file('dataset_description.json').get_dict()
899+
for l in layouts]
900+
880901
def get_nearest(self, path, return_type='filename', strict=True,
881902
all_=False, ignore_strict_entities='extension',
882903
full_search=False, **filters):

bids/layout/tests/test_layout.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,18 @@ def test_get_layouts_in_scope(layout_ds005_multi_derivs):
495495
assert self_scope == [l]
496496

497497

498+
def test_get_dataset_description(layout_ds005_multi_derivs):
499+
l = layout_ds005_multi_derivs
500+
dd = l.get_dataset_description()
501+
assert isinstance(dd, dict)
502+
assert dd['Name'] == 'Mixed-gambles task'
503+
dd = l.get_dataset_description('all', True)
504+
assert isinstance(dd, list)
505+
assert len(dd) == 3
506+
names = {'Mixed-gambles task', 'Mixed-gambles task -- dummy derivative'}
507+
assert set([d['Name'] for d in dd]) == names
508+
509+
498510
def test_indexed_file_associations(layout_7t_trt):
499511
img = layout_7t_trt.get(subject='01', run=1, suffix='bold', session='1',
500512
acquisition='fullbrain', extension='nii.gz')[0]

0 commit comments

Comments
 (0)