BIDSLayout.get_nearest_helper() does not anticipate absent 'type' entity
Happens with openfmri ds000002 due to contained derivatives:

```
  File "/home/mih/.local/lib/python2.7/site-packages/bids/grabbids/bids_layout.py", line 45, in get_metadata
    potentialJSONs = self.get_nearest_helper(path, '.json', **kwargs)
  File "/home/mih/.local/lib/python2.7/site-packages/bids/grabbids/bids_layout.py", line 37, in get_nearest_helper
    type = self.files[path].entities['type']
KeyError: 'type'
()
> /home/mih/.local/lib/python2.7/site-packages/bids/grabbids/bids_layout.py(37)get_nearest_helper()
-> type = self.files[path].entities['type']
(Pdb) self.files[path].entities
{u'subject': '01'}
(Pdb) path
u'/tmp/openfmri/ds000002/derivatives/mriqc/anatomical_sub-01.pdf'
```
diff --git a/bids/grabbids/bids_layout.py b/bids/grabbids/bids_layout.py
index b2b27730..64f0dc37 100644
--- a/bids/grabbids/bids_layout.py
+++ b/bids/grabbids/bids_layout.py
@@ -62,7 +62,11 @@ class BIDSLayout(Layout):
                 "BIDS project." % path)
 
         if not type:
-            # Constrain the search to .json files with the same type as target
+            if 'type' not in self.files[path].entities:
+                raise ValueError(
+                    "File '%s' does not have a valid type definition, most "
+                    "likely because it is not a valid BIDS file." % path
+                )
             type = self.files[path].entities['type']
 
         tmp = self.get_nearest(path, extensions=extension, all_=True,
['bids/grabbids/bids_layout.py']