Repository: bids-standard/pybids

Issue:
Read in "Name" and "BIDSVersion" with a Layout object
"Name" and "BIDSVersion" are both required fields for the specification, so I think we could rely on them being there when trying to load in a BIDS dataset. I think that upon init for `Layout` we should:

1. Check if `dataset_description.json` exists in the root of the folder. (and if not raise an error)
2. Check if `Name` and `BIDSVersion` are there (and if not raise an error)
3. Read them in as attributes of the resulting instance.

WDYT @tyarkoni ?


Discussion:
I think you are 100% correct, which earns you the privilege of submitting a PR!
Oh, except I think we should read everything in the file, and probably store it in a `.description` or `.metadata` dictionary rather than extracting just `Name` and `BIDSVersion`.
hehe - ping me in a week if I haven't gotten to this yet :-)  I need to write the bids validator code for the iEEG extension, which I foresee sucking me into a regex hole
(agree w/r/t just using a `description` attribute)
Cool... let me know if you want to talk through how we should handle validation of extensions within the context of the existing `bids_validator` module.

Resolving patch:
diff --git a/bids/grabbids/bids_layout.py b/bids/grabbids/bids_layout.py
--- a/bids/grabbids/bids_layout.py
+++ b/bids/grabbids/bids_layout.py
@@ -3,8 +3,7 @@
 import warnings
 from io import open
 
-from os.path import dirname
-from os.path import abspath
+from os.path import dirname, abspath, exists
 from os.path import join as pathjoin
 
 from .bids_validator import BIDSValidator
@@ -62,6 +61,19 @@ class BIDSLayout(Layout):
 
     def __init__(self, path, config=None, validate=False,
                  index_associated=True, include=None, exclude=None, **kwargs):
+
+        # Load and validate information in dataset_description.json
+        target = pathjoin(path, 'dataset_description.json')
+        if not exists(target):
+            raise ValueError("Mandatory 'dataset_description.json' file is "
+                             "missing from project root!")
+        self.description = json.load(open(target, 'r'))
+
+        for k in ['Name', 'BIDSVersion']:
+            if k not in self.description:
+                raise ValueError("Mandatory '%s' field missing from "
+                                 "dataset_description.json." % k)
+
         self.validator = BIDSValidator(index_associated=index_associated)
         self.validate = validate
 


Test patch:
diff --git a/bids/grabbids/tests/test_grabbids.py b/bids/grabbids/tests/test_grabbids.py
--- a/bids/grabbids/tests/test_grabbids.py
+++ b/bids/grabbids/tests/test_grabbids.py
@@ -30,6 +30,16 @@ def test_layout_init(testlayout1):
     assert isinstance(testlayout1.files, dict)
 
 
+def test_load_description(testlayout1):
+    with pytest.raises(ValueError) as e:
+        data_dir = join(get_test_data_path(), 'images')
+        layout = BIDSLayout(data_dir)
+        assert e.value.message.startswith("Mandatory 'dataset_description'")
+    assert hasattr(testlayout1, 'description')
+    assert testlayout1.description['Name'] == '7t_trt'
+    assert testlayout1.description['BIDSVersion'] == "1.0.0rc3"
+
+
 def test_get_metadata(testlayout1):
     target = 'sub-03/ses-2/func/sub-03_ses-2_task-' \
              'rest_acq-fullbrain_run-2_bold.nii.gz'