From afc5180ee4063e0b771e66419dc7a4d3dc0f20e2 Mon Sep 17 00:00:00 2001 From: marqh Date: Wed, 9 Mar 2016 15:07:32 +0000 Subject: [PATCH 01/11] bald --- lib/bald/__init__.py | 27 +++++++++++++ lib/bald/__init__.pyc | Bin 0 -> 152 bytes lib/bald/__init__.py~ | 0 lib/bald/tests/__init__.py | 16 ++++++++ lib/bald/tests/__init__.pyc | Bin 0 -> 894 bytes lib/bald/tests/__init__.py~ | 0 lib/bald/tests/integration/__init__.py | 0 lib/bald/tests/test_array_reference.py | 32 ++++++++++++++++ lib/bald/tests/test_array_reference.py~ | 31 +++++++++++++++ lib/bald/tests/test_simple.py | 36 +++++++++++++++++ lib/bald/tests/test_simple.py~ | 49 ++++++++++++++++++++++++ lib/bald/tests/unit/__init__.py | 0 lib/bald/validation.py | 15 ++++++++ lib/bald/validation.py~ | 15 ++++++++ setup.py | 0 15 files changed, 221 insertions(+) create mode 100644 lib/bald/__init__.py create mode 100644 lib/bald/__init__.pyc create mode 100644 lib/bald/__init__.py~ create mode 100644 lib/bald/tests/__init__.py create mode 100644 lib/bald/tests/__init__.pyc create mode 100644 lib/bald/tests/__init__.py~ create mode 100644 lib/bald/tests/integration/__init__.py create mode 100644 lib/bald/tests/test_array_reference.py create mode 100644 lib/bald/tests/test_array_reference.py~ create mode 100644 lib/bald/tests/test_simple.py create mode 100644 lib/bald/tests/test_simple.py~ create mode 100644 lib/bald/tests/unit/__init__.py create mode 100644 lib/bald/validation.py create mode 100644 lib/bald/validation.py~ create mode 100644 setup.py diff --git a/lib/bald/__init__.py b/lib/bald/__init__.py new file mode 100644 index 0000000..46f46fa --- /dev/null +++ b/lib/bald/__init__.py @@ -0,0 +1,27 @@ +import h5py + +import bald.validation + + +@contextlib.contextmanager +def load(afilepath): + if afilepath.endswith('.hdf5'): + loader = h5py.File + else: + raise ValueError('filepath suffix not supported') + try: + f = loader(afilepath, "r") + yield f + finally: + f.close() + return f + +def validate(afilepath): + """ + Validate a file with respect ot binarry-array-linked-data. + Returns a :class:`bald.validation.Validation` + """ + + with load(afilepath) as f: + + diff --git a/lib/bald/__init__.pyc b/lib/bald/__init__.pyc new file mode 100644 index 0000000000000000000000000000000000000000..14fab03e3a8a331e77c4998d9c6d7fa927efbdd9 GIT binary patch literal 152 zcmZSn%*&-Ca5^lR0SXv_v;z;Ydu&7f*~RqUM)w!}G1DKyI1meRl9qtZ zt=tK^MF%4)XN5#X!lDIx48FqwR{RKqt)-w%K5!!`Wt0v26?6a$!LFo(8%5tIv{7^_ zzJP;;L!pB)=QLx}8 pcT#ey%-4j+TVZ-uB7eK>#mndaV}F_K&Anc4`T9g|Wl!W+{Q;4q#k~Lk literal 0 HcmV?d00001 diff --git a/lib/bald/tests/__init__.py~ b/lib/bald/tests/__init__.py~ new file mode 100644 index 0000000..e69de29 diff --git a/lib/bald/tests/integration/__init__.py b/lib/bald/tests/integration/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/bald/tests/test_array_reference.py b/lib/bald/tests/test_array_reference.py new file mode 100644 index 0000000..b3b8aab --- /dev/null +++ b/lib/bald/tests/test_array_reference.py @@ -0,0 +1,32 @@ +import unittest + +import h5py +import numpy as np + +from bald.tests import BaldTestCase + +def _fattrs(f): + f.attrs['bald_._'] = 'http://binary_array_ld.net/experimental' + f.attrs['bald_._type'] = 'bald_._Container' + return f + +def _create_parent_child(f, pshape, cshape): + dsetp = f.create_dataset("parent_dataset", pshape, dtype='i') + dsetc = f.create_dataset("child_dataset", cshape, dtype='i') + dsetp.attrs['bald_._type'] = 'bald_._Dataset' + dsetp.attrs['bald_._reference'] = dsetc.ref + dsetc.attrs['bald_._type'] = 'bald_._Dataset' + return f + + +class TestArrayReference(BaldTestCase): + def test_match_array_reference(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + f = _create_parent_child(f, (11, 17), (11, 17)) + f.close() + + +if __name__ == '__main__': + unittest.main() diff --git a/lib/bald/tests/test_array_reference.py~ b/lib/bald/tests/test_array_reference.py~ new file mode 100644 index 0000000..f5f7ae9 --- /dev/null +++ b/lib/bald/tests/test_array_reference.py~ @@ -0,0 +1,31 @@ +import unittest + +import h5py +import numpy as np + +from bald.tests import BaldTestCase + +def _fattrs(f): + f.attrs.create('bald_._', 'http://binary_array_ld.net/experimental') + f.attrs.create('bald_._type', 'bald_._Container') + # f.attrs['bald_._'] = 'http://binary_array_ld.net/experimental' + # f.attrs['bald_._type'] = 'bald_._Container' + return f + + +class TestArrayReference(BaldTestCase): + def test_match_array_reference(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + dsetp = f.create_dataset("mydataset", (11, 17), dtype='i') + dsetc = f.create_dataset("mydataset", (11, 17), dtype='i') + dsetp.create('bald_._reference', dsetc.ref) + f.close() + newf = h5py.File(tfile, "r") + import pdb; pdb.set_trace() + nset = newf.get('mydatasetp') + + +if __name__ == '__main__': + unittest.main() diff --git a/lib/bald/tests/test_simple.py b/lib/bald/tests/test_simple.py new file mode 100644 index 0000000..6de425d --- /dev/null +++ b/lib/bald/tests/test_simple.py @@ -0,0 +1,36 @@ +import unittest + +import h5py +import numpy as np + +from bald.tests import BaldTestCase + + +class ATest(BaldTestCase): + def setUp(self): + self.this = 'this' + + def test_this(self): + self.assertTrue(self.this, 'this') + + def test_make_file(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + dset = f.create_dataset("mydataset", (100,), dtype='i') + + def test_make_load_file(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + dset = f.create_dataset("mydataset", (100,), dtype='i') + dset.attrs['bald_._'] = 'http://binary_array_ld.net/alpha' + f.close() + newf = h5py.File(tfile, "r") + nset = newf.get('mydataset') + self.assertEqual(nset.shape, (100,)) + self.assertEqual(nset.dtype, 'int32') + self.assertEqual(nset.attrs.get('bald_._'), + 'http://binary_array_ld.net/alpha') + + +if __name__ == '__main__': + unittest.main() diff --git a/lib/bald/tests/test_simple.py~ b/lib/bald/tests/test_simple.py~ new file mode 100644 index 0000000..9dd8733 --- /dev/null +++ b/lib/bald/tests/test_simple.py~ @@ -0,0 +1,49 @@ +import contextlib +import os +import tempfile +import unittest + +import h5py +import numpy as np + + +class BATestCase(unittest.TestCase): + @contextlib.contextmanager + def temp_filename(self, suffix=''): + temp_file = tempfile.mkstemp(suffix) + os.close(temp_file[0]) + filename = temp_file[1] + try: + yield filename + finally: + os.remove(filename) + + +class ATest(BATestCase): + def setUp(self): + self.this = 'this' + + def test_this(self): + self.assertTrue(self.this, 'this') + + def test_make_file(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + dset = f.create_dataset("mydataset", (100,), dtype='i') + + def test_make_load_file(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + dset = f.create_dataset("mydataset", (100,), dtype='i') + dset.attrs['bald_._'] = 'http://binary_array_ld.net/alpha' + f.close() + newf = h5py.File(tfile, "r") + nset = newf.get('mydataset') + self.assertEqual(nset.shape, (100,)) + self.assertEqual(nset.dtype, 'int32') + self.assertEqual(nset.attrs.get('bald_._'), + 'http://binary_array_ld.net/alpha') + + +if __name__ == '__main__': + unittest.main() diff --git a/lib/bald/tests/unit/__init__.py b/lib/bald/tests/unit/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/lib/bald/validation.py b/lib/bald/validation.py new file mode 100644 index 0000000..7b03841 --- /dev/null +++ b/lib/bald/validation.py @@ -0,0 +1,15 @@ + + + + +class Validation(object): + pass + +class ValidationSet(Validation): + pass + +class ValidationList(Validation): + pass + + + diff --git a/lib/bald/validation.py~ b/lib/bald/validation.py~ new file mode 100644 index 0000000..db8f213 --- /dev/null +++ b/lib/bald/validation.py~ @@ -0,0 +1,15 @@ + + + + +class Validation(object): + pass + +class ValidationSet(object): + pass + +class ValidationList(object): + pass + + + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..e69de29 From b720fcb287ff5c3d8477a5c3c275c308985cd8e8 Mon Sep 17 00:00:00 2001 From: marqh Date: Wed, 9 Mar 2016 15:09:03 +0000 Subject: [PATCH 02/11] ~ --- lib/bald/__init__.py~ | 0 lib/bald/tests/__init__.py~ | 0 lib/bald/tests/test_array_reference.py~ | 31 ---------------- lib/bald/tests/test_simple.py~ | 49 ------------------------- lib/bald/validation.py~ | 15 -------- 5 files changed, 95 deletions(-) delete mode 100644 lib/bald/__init__.py~ delete mode 100644 lib/bald/tests/__init__.py~ delete mode 100644 lib/bald/tests/test_array_reference.py~ delete mode 100644 lib/bald/tests/test_simple.py~ delete mode 100644 lib/bald/validation.py~ diff --git a/lib/bald/__init__.py~ b/lib/bald/__init__.py~ deleted file mode 100644 index e69de29..0000000 diff --git a/lib/bald/tests/__init__.py~ b/lib/bald/tests/__init__.py~ deleted file mode 100644 index e69de29..0000000 diff --git a/lib/bald/tests/test_array_reference.py~ b/lib/bald/tests/test_array_reference.py~ deleted file mode 100644 index f5f7ae9..0000000 --- a/lib/bald/tests/test_array_reference.py~ +++ /dev/null @@ -1,31 +0,0 @@ -import unittest - -import h5py -import numpy as np - -from bald.tests import BaldTestCase - -def _fattrs(f): - f.attrs.create('bald_._', 'http://binary_array_ld.net/experimental') - f.attrs.create('bald_._type', 'bald_._Container') - # f.attrs['bald_._'] = 'http://binary_array_ld.net/experimental' - # f.attrs['bald_._type'] = 'bald_._Container' - return f - - -class TestArrayReference(BaldTestCase): - def test_match_array_reference(self): - with self.temp_filename('.hdf') as tfile: - f = h5py.File(tfile, "w") - f = _fattrs(f) - dsetp = f.create_dataset("mydataset", (11, 17), dtype='i') - dsetc = f.create_dataset("mydataset", (11, 17), dtype='i') - dsetp.create('bald_._reference', dsetc.ref) - f.close() - newf = h5py.File(tfile, "r") - import pdb; pdb.set_trace() - nset = newf.get('mydatasetp') - - -if __name__ == '__main__': - unittest.main() diff --git a/lib/bald/tests/test_simple.py~ b/lib/bald/tests/test_simple.py~ deleted file mode 100644 index 9dd8733..0000000 --- a/lib/bald/tests/test_simple.py~ +++ /dev/null @@ -1,49 +0,0 @@ -import contextlib -import os -import tempfile -import unittest - -import h5py -import numpy as np - - -class BATestCase(unittest.TestCase): - @contextlib.contextmanager - def temp_filename(self, suffix=''): - temp_file = tempfile.mkstemp(suffix) - os.close(temp_file[0]) - filename = temp_file[1] - try: - yield filename - finally: - os.remove(filename) - - -class ATest(BATestCase): - def setUp(self): - self.this = 'this' - - def test_this(self): - self.assertTrue(self.this, 'this') - - def test_make_file(self): - with self.temp_filename('.hdf') as tfile: - f = h5py.File(tfile, "w") - dset = f.create_dataset("mydataset", (100,), dtype='i') - - def test_make_load_file(self): - with self.temp_filename('.hdf') as tfile: - f = h5py.File(tfile, "w") - dset = f.create_dataset("mydataset", (100,), dtype='i') - dset.attrs['bald_._'] = 'http://binary_array_ld.net/alpha' - f.close() - newf = h5py.File(tfile, "r") - nset = newf.get('mydataset') - self.assertEqual(nset.shape, (100,)) - self.assertEqual(nset.dtype, 'int32') - self.assertEqual(nset.attrs.get('bald_._'), - 'http://binary_array_ld.net/alpha') - - -if __name__ == '__main__': - unittest.main() diff --git a/lib/bald/validation.py~ b/lib/bald/validation.py~ deleted file mode 100644 index db8f213..0000000 --- a/lib/bald/validation.py~ +++ /dev/null @@ -1,15 +0,0 @@ - - - - -class Validation(object): - pass - -class ValidationSet(object): - pass - -class ValidationList(object): - pass - - - From ed0c5c9c2e6a278e2268e92ae017f33526d13a72 Mon Sep 17 00:00:00 2001 From: marqh Date: Wed, 9 Mar 2016 19:27:58 +0000 Subject: [PATCH 03/11] onon --- lib/bald/__init__.py | 52 ++++++++++++++-- lib/bald/__init__.pyc | Bin 152 -> 0 bytes lib/bald/tests/integration/test_validation.py | 53 +++++++++++++++++ lib/bald/tests/test_simple.py | 2 +- lib/bald/tests/unit/test_validation.py | 20 +++++++ lib/bald/validation.py | 56 +++++++++++++++++- 6 files changed, 176 insertions(+), 7 deletions(-) delete mode 100644 lib/bald/__init__.pyc create mode 100644 lib/bald/tests/integration/test_validation.py create mode 100644 lib/bald/tests/unit/test_validation.py diff --git a/lib/bald/__init__.py b/lib/bald/__init__.py index 46f46fa..bb90674 100644 --- a/lib/bald/__init__.py +++ b/lib/bald/__init__.py @@ -1,11 +1,48 @@ +import contextlib +import re + import h5py -import bald.validation +from bald.validation import ContainerValidation + +class Container(object): + def __init__(self, attrs=None): + """ + attrs: an dictionary of key value pair attributes + """ + if attrs is None: + attrs = [] + self.attrs = attrs + self._prefix_suffix = re.compile('(^(?:(?!_._).)*)_._((?!.*_._).*$)') + _http_p = 'http[s]?://.*' + self._http_uri = re.compile('{}'.format(_http_p)) + self._http_uri_prefix = re.compile('{}/|#'.format(_http_p)) + + + def prefixes(self): + prefixes = {} + for key, value in self.attrs.iteritems(): + if key.endswith('_._') and self._http_uri_prefix.match(value): + pref = key.rstrip('_._') + if prefixes.has_key(pref): + raise ValueError('This container has conflicting prefix definitions') + prefixes[pref] = value + return prefixes + + def unpack_uri(self, astring): + result = astring + if self._prefix_suffix.match(astring): + prefix, suffix = self._prefix_suffix.match(astring).groups() + + if self.prefixes().has_key(prefix): + if self._http_uri.match(self.prefixes()[prefix]): + result = astring.replace('{}_._'.format(prefix), self.prefixes()[prefix]) + return result @contextlib.contextmanager def load(afilepath): - if afilepath.endswith('.hdf5'): + if afilepath.endswith('.hdf'): loader = h5py.File else: raise ValueError('filepath suffix not supported') @@ -14,14 +51,19 @@ def load(afilepath): yield f finally: f.close() - return f -def validate(afilepath): + +def validate_hdf5(afilepath): """ Validate a file with respect ot binarry-array-linked-data. Returns a :class:`bald.validation.Validation` """ - with load(afilepath) as f: + with load(afilepath) as fhandle: + root_container = Container(fhandle.attrs) + root_val = ContainerValidation(root_container) + return root_val.is_valid() + + diff --git a/lib/bald/__init__.pyc b/lib/bald/__init__.pyc deleted file mode 100644 index 14fab03e3a8a331e77c4998d9c6d7fa927efbdd9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 152 zcmZSn%*&-Ca5^lR0SXv_v;z Date: Wed, 9 Mar 2016 19:28:31 +0000 Subject: [PATCH 04/11] ignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.gitignore b/.gitignore index 1dbc687..9b86f6f 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,6 @@ target/ #Ipython Notebook .ipynb_checkpoints + +*~ + From 29a8a193c4ce6a027f88e3195aae6e7f8f085bad Mon Sep 17 00:00:00 2001 From: marqh Date: Sat, 9 Apr 2016 06:52:24 +0000 Subject: [PATCH 05/11] broadcast --- lib/bald/__init__.py | 18 ++- lib/bald/tests/integration/test_validation.py | 30 +++-- lib/bald/validation.py | 103 ++++++++++++++++-- 3 files changed, 129 insertions(+), 22 deletions(-) diff --git a/lib/bald/__init__.py b/lib/bald/__init__.py index bb90674..d77acc1 100644 --- a/lib/bald/__init__.py +++ b/lib/bald/__init__.py @@ -3,9 +3,9 @@ import h5py -from bald.validation import ContainerValidation +from bald.validation import ContainerValidation, DatasetValidation -class Container(object): +class Subject(object): def __init__(self, attrs=None): """ attrs: an dictionary of key value pair attributes @@ -60,9 +60,19 @@ def validate_hdf5(afilepath): """ with load(afilepath) as fhandle: - root_container = Container(fhandle.attrs) + valid = True + root_container = Subject(fhandle.attrs) root_val = ContainerValidation(root_container) - return root_val.is_valid() + if not root_val.is_valid(): + valid = False + # iterate through the datasets + for name, dataset in fhandle.items(): + dset = Subject(dataset.attrs) + dset_val = DatasetValidation(name, dataset, dset, fhandle) + if not dset_val.is_valid(): + valid = False + + return valid diff --git a/lib/bald/tests/integration/test_validation.py b/lib/bald/tests/integration/test_validation.py index 0606044..13e4c33 100644 --- a/lib/bald/tests/integration/test_validation.py +++ b/lib/bald/tests/integration/test_validation.py @@ -39,14 +39,30 @@ def test_invalid_uri(self): f.close() self.assertFalse(bald.validate_hdf5(tfile)) -# class TestArrayReference(BaldTestCase): -# def test_match_array_reference(self): -# with self.temp_filename('.hdf') as tfile: -# f = h5py.File(tfile, "w") -# f = _fattrs(f) -# f = _create_parent_child(f, (11, 17), (11, 17)) -# f.close() +class TestArrayReference(BaldTestCase): + def test_match_array_reference(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + f = _create_parent_child(f, (11, 17), (11, 17)) + f.close() + self.assertTrue(bald.validate_hdf5(tfile)) + def test_misatch_zeroth_array_reference(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + f = _create_parent_child(f, (11, 17), (11, 13)) + f.close() + self.assertFalse(bald.validate_hdf5(tfile)) + + def test_misatch_oneth_array_reference(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + f = _create_parent_child(f, (11, 17), (13, 17)) + f.close() + self.assertFalse(bald.validate_hdf5(tfile)) if __name__ == '__main__': diff --git a/lib/bald/validation.py b/lib/bald/validation.py index 40183aa..3577880 100644 --- a/lib/bald/validation.py +++ b/lib/bald/validation.py @@ -1,4 +1,5 @@ +import numpy as np import requests import bald @@ -14,7 +15,30 @@ def check_uri(uri): result = True return result +def valid_array_reference(parray, carray): + """ + Returns boolean. + Validates bald array broadcasting rules between a parent array and a child array. + Args: + * parray - a numpy array: the parent of a bald array reference relation + + * carray - a numpy array: the child of a bald array reference relation + """ + # https://github.com/SciTools/courses/blob/master/course_content/notebooks/numpy_intro.ipynb + # https://cs231n.github.io/python-numpy-tutorial/#numpy-broadcasting + # http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html + # http://scipy.github.io/old-wiki/pages/EricsBroadcastingDoc + + result = True + # try numpy broadcast + try: + _ = np.broadcast(parray, carray) + except ValueError: + result = False + + return result + class Validation(object): @@ -25,9 +49,10 @@ def exceptions(self): exceptions = [] return exceptions -class ContainerValidation(Validation): - def __init__(self, container): - self.container = container +class SubjectValidation(Validation): + + def __init__(self, subject): + self.subject = subject def is_valid(self): return not self.exceptions() @@ -40,23 +65,79 @@ def exceptions(self): def check_attr_uris(self, exceptions): def _check_uri(uri, exceptions): if not check_uri(uri): + check_uri(uri) msg = '{} is not resolving as a resource (404).' msg = msg.format(uri) exceptions.append(ValueError(msg)) return exceptions - for pref, uri in self.container.prefixes().iteritems(): - exceptions = _check_uri(self.container.unpack_uri(uri), - exceptions) - for attr, value in self.container.attrs.iteritems(): - exceptions = _check_uri(self.container.unpack_uri(attr), + for pref, uri in self.subject.prefixes().iteritems(): + exceptions = _check_uri(self.subject.unpack_uri(uri), exceptions) - exceptions = _check_uri(self.container.unpack_uri(value), + for attr, value in self.subject.attrs.iteritems(): + exceptions = _check_uri(self.subject.unpack_uri(attr), exceptions) - + if isinstance(value, str): + exceptions = _check_uri(self.subject.unpack_uri(value), + exceptions) return exceptions - + +class ContainerValidation(SubjectValidation): + + def __init__(self, container): + self.container = container + self.subject = container + + def exceptions(self): + exceptions = [] + exceptions = self.check_attr_uris(exceptions) + return exceptions + +class DatasetValidation(SubjectValidation): + + + def __init__(self, name, dataset, subject, fhandle): + self.dataset = dataset + self.subject = subject + self.name = name + self.fhandle = fhandle + + def exceptions(self): + exceptions = [] + exceptions = self.check_attr_uris(exceptions) + exceptions = self.check_array_references(exceptions) + return exceptions + + def check_array_references(self, exceptions): + def _check_ref(pdataset, parray, cdataset, carray): + if not valid_array_reference(parray, carray): + msg = ('{} declares a child of {} but the arrays' + 'do not conform to the bald array reference' + 'rules') + msg = msg.format(pdataset, cdataset) + exceptions.append(ValueError(msg)) + return exceptions + + # for dataset in ????? + # for parent child relation in dataset + # check the broadcasting relationship + # parray = np.array((1,2)) + # carray = np.array((3,4)) + + for attr, value in self.subject.attrs.iteritems(): + # should support subtypes + #import pdb; pdb.set_trace() + if attr == 'bald_._reference': + # check if it's this type, otherwise exception) + # if isinstance(value, + child_dset = self.fhandle[value] + parray = np.zeros(self.dataset.shape) + carray = np.zeros(child_dset.shape) + + exceptions = _check_ref('p', parray, 'c', carray) + return exceptions + class ValidationSet(Validation): pass From 00b739920b3066812df6ffb01479318e941676ff Mon Sep 17 00:00:00 2001 From: marqh Date: Sat, 9 Apr 2016 08:15:39 +0000 Subject: [PATCH 06/11] caching --- lib/bald/__init__.py | 43 +++++++++++++-- lib/bald/tests/integration/test_validation.py | 52 +++++++++++++++++-- lib/bald/validation.py | 31 +++++------ 3 files changed, 101 insertions(+), 25 deletions(-) diff --git a/lib/bald/__init__.py b/lib/bald/__init__.py index d77acc1..b75d402 100644 --- a/lib/bald/__init__.py +++ b/lib/bald/__init__.py @@ -2,9 +2,41 @@ import re import h5py +import requests from bald.validation import ContainerValidation, DatasetValidation + +class HttpStatusCache(object): + def __init__(self): + self.cache = {} + + def __getitem__(self, item): + uri = item + if item not in self.cache: + if not uri.startswith('http://') or uri.startswith('https://'): + raise ValueError('{} is not a HTTP URI.'.format(item)) + if item in self.cache: + result = self.cache[item] + else: + r = requests.get(uri) + if r.status_code == 200: + headers={'Accept':'text/turtle'} + rraw = requests.get(uri, headers=headers) + self.cache[item] = rraw.status_code + else: + self.cache[item] = r.status_code + + result = self.cache[item] + return result + + + def check_uri(self, uri): + result = False + if self[uri] == 200: + result = True + return result + class Subject(object): def __init__(self, attrs=None): """ @@ -61,14 +93,19 @@ def validate_hdf5(afilepath): with load(afilepath) as fhandle: valid = True + cache = {} root_container = Subject(fhandle.attrs) - root_val = ContainerValidation(root_container) + root_val = ContainerValidation(subject=root_container) if not root_val.is_valid(): valid = False # iterate through the datasets for name, dataset in fhandle.items(): - dset = Subject(dataset.attrs) - dset_val = DatasetValidation(name, dataset, dset, fhandle) + # a dataset's attribute collection inherits from and specialises it's + # container's attrbiute collection + sattrs = dict(fhandle.attrs).copy() + sattrs.update(dataset.attrs) + dset = Subject(sattrs) + dset_val = DatasetValidation(name, dataset, fhandle, subject=dset) if not dset_val.is_valid(): valid = False diff --git a/lib/bald/tests/integration/test_validation.py b/lib/bald/tests/integration/test_validation.py index 13e4c33..e85643c 100644 --- a/lib/bald/tests/integration/test_validation.py +++ b/lib/bald/tests/integration/test_validation.py @@ -40,7 +40,7 @@ def test_invalid_uri(self): self.assertFalse(bald.validate_hdf5(tfile)) class TestArrayReference(BaldTestCase): - def test_match_array_reference(self): + def test_match(self): with self.temp_filename('.hdf') as tfile: f = h5py.File(tfile, "w") f = _fattrs(f) @@ -48,7 +48,7 @@ def test_match_array_reference(self): f.close() self.assertTrue(bald.validate_hdf5(tfile)) - def test_misatch_zeroth_array_reference(self): + def test_mismatch_zeroth(self): with self.temp_filename('.hdf') as tfile: f = h5py.File(tfile, "w") f = _fattrs(f) @@ -56,7 +56,7 @@ def test_misatch_zeroth_array_reference(self): f.close() self.assertFalse(bald.validate_hdf5(tfile)) - def test_misatch_oneth_array_reference(self): + def test_mismatch_oneth(self): with self.temp_filename('.hdf') as tfile: f = h5py.File(tfile, "w") f = _fattrs(f) @@ -64,6 +64,52 @@ def test_misatch_oneth_array_reference(self): f.close() self.assertFalse(bald.validate_hdf5(tfile)) + def test_match_plead_dim(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + # parent has leading dimension wrt child + f = _create_parent_child(f, (4, 13, 17), (13, 17)) + f.close() + self.assertTrue(bald.validate_hdf5(tfile)) + + def test_match_clead_dim(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + # child has leading dimension wrt parent + f = _create_parent_child(f, (13, 17), (7, 13, 17)) + f.close() + self.assertTrue(bald.validate_hdf5(tfile)) + + def test_mismatch_pdisjc_lead_dim(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + # child and parent have disjoint leading dimensions + f = _create_parent_child(f, (4, 13, 17), (7, 13, 17)) + f.close() + self.assertFalse(bald.validate_hdf5(tfile)) + + def test_mismatch_pdisjc_trail_dim(self): + with self.temp_filename('.hdf') as tfile: + f = h5py.File(tfile, "w") + f = _fattrs(f) + # child and parent have disjoint trailing dimensions + f = _create_parent_child(f, (13, 17, 2), (13, 17, 9)) + f.close() + self.assertFalse(bald.validate_hdf5(tfile)) + + + + # def test_match_(self): + # with self.temp_filename('.hdf') as tfile: + # f = h5py.File(tfile, "w") + # f = _fattrs(f) + # # + # f = _create_parent_child(f, (), ()) + # f.close() + # self.assert(bald.validate_hdf5(tfile)) if __name__ == '__main__': unittest.main() diff --git a/lib/bald/validation.py b/lib/bald/validation.py index 3577880..11b9a23 100644 --- a/lib/bald/validation.py +++ b/lib/bald/validation.py @@ -1,19 +1,8 @@ import numpy as np -import requests import bald -def check_uri(uri): - result = False - if uri.startswith('http://') or uri.startswith('https://'): - r = requests.get(uri) - if r.status_code == 200: - headers={'Accept':'text/turtle'} - rraw = requests.get(uri, headers=headers) - if rraw.status_code == 200: - result = True - return result def valid_array_reference(parray, carray): """ @@ -51,8 +40,13 @@ def exceptions(self): class SubjectValidation(Validation): - def __init__(self, subject): + def __init__(self, subject, httpcache=None): self.subject = subject + if isinstance(httpcache, bald.HttpStatusCache): + self.cache = httpcache + else: + self.cache = bald.HttpStatusCache() + def is_valid(self): return not self.exceptions() @@ -64,8 +58,7 @@ def exceptions(self): def check_attr_uris(self, exceptions): def _check_uri(uri, exceptions): - if not check_uri(uri): - check_uri(uri) + if not self.cache.check_uri(uri): msg = '{} is not resolving as a resource (404).' msg = msg.format(uri) exceptions.append(ValueError(msg)) @@ -85,9 +78,8 @@ def _check_uri(uri, exceptions): class ContainerValidation(SubjectValidation): - def __init__(self, container): - self.container = container - self.subject = container + def __init__(self, **kwargs): + super(ContainerValidation, self).__init__(**kwargs) def exceptions(self): exceptions = [] @@ -97,11 +89,12 @@ def exceptions(self): class DatasetValidation(SubjectValidation): - def __init__(self, name, dataset, subject, fhandle): + def __init__(self, name, dataset, fhandle, **kwargs): + self.dataset = dataset - self.subject = subject self.name = name self.fhandle = fhandle + super(DatasetValidation, self).__init__(**kwargs) def exceptions(self): exceptions = [] From b4b31ff553621d33bf81c0c4b338b55cf22db8cb Mon Sep 17 00:00:00 2001 From: marqh Date: Sat, 9 Apr 2016 08:39:02 +0000 Subject: [PATCH 07/11] caching --- lib/bald/validation.py | 13 ++++--------- 1 file changed, 4 insertions(+), 9 deletions(-) diff --git a/lib/bald/validation.py b/lib/bald/validation.py index 11b9a23..b159d16 100644 --- a/lib/bald/validation.py +++ b/lib/bald/validation.py @@ -54,6 +54,7 @@ def is_valid(self): def exceptions(self): exceptions = [] exceptions = self.check_attr_uris(exceptions) + exceptions = self.check_attr_domain_range(exceptions) return exceptions def check_attr_uris(self, exceptions): @@ -74,6 +75,9 @@ def _check_uri(uri, exceptions): exceptions = _check_uri(self.subject.unpack_uri(value), exceptions) return exceptions + + def check_attr_domain_range(self, exceptions): + return exceptions class ContainerValidation(SubjectValidation): @@ -112,15 +116,8 @@ def _check_ref(pdataset, parray, cdataset, carray): exceptions.append(ValueError(msg)) return exceptions - # for dataset in ????? - # for parent child relation in dataset - # check the broadcasting relationship - # parray = np.array((1,2)) - # carray = np.array((3,4)) - for attr, value in self.subject.attrs.iteritems(): # should support subtypes - #import pdb; pdb.set_trace() if attr == 'bald_._reference': # check if it's this type, otherwise exception) # if isinstance(value, @@ -139,5 +136,3 @@ class ValidationList(Validation): pass - -#if __name__ == '__main__': From c9bd9938078719d5fd7102d79a6e70e795825860 Mon Sep 17 00:00:00 2001 From: marqh Date: Sat, 9 Apr 2016 10:30:26 +0000 Subject: [PATCH 08/11] domain range pass --- lib/bald/__init__.py | 29 ++++++++--------------- lib/bald/validation.py | 54 ++++++++++++++++++++++++++++++++++-------- 2 files changed, 54 insertions(+), 29 deletions(-) diff --git a/lib/bald/__init__.py b/lib/bald/__init__.py index b75d402..be86014 100644 --- a/lib/bald/__init__.py +++ b/lib/bald/__init__.py @@ -12,28 +12,19 @@ def __init__(self): self.cache = {} def __getitem__(self, item): - uri = item + + if not item.startswith('http://') or item.startswith('https://'): + raise ValueError('{} is not a HTTP URI.'.format(item)) if item not in self.cache: - if not uri.startswith('http://') or uri.startswith('https://'): - raise ValueError('{} is not a HTTP URI.'.format(item)) - if item in self.cache: - result = self.cache[item] - else: - r = requests.get(uri) - if r.status_code == 200: - headers={'Accept':'text/turtle'} - rraw = requests.get(uri, headers=headers) - self.cache[item] = rraw.status_code - else: - self.cache[item] = r.status_code - - result = self.cache[item] - return result + headers={'Accept':'text/turtle'} + self.cache[item] = requests.get(item, headers=headers) + + return self.cache[item] def check_uri(self, uri): result = False - if self[uri] == 200: + if self[uri].status_code == 200: result = True return result @@ -95,7 +86,7 @@ def validate_hdf5(afilepath): valid = True cache = {} root_container = Subject(fhandle.attrs) - root_val = ContainerValidation(subject=root_container) + root_val = ContainerValidation(subject=root_container, fhandle=fhandle) if not root_val.is_valid(): valid = False # iterate through the datasets @@ -105,7 +96,7 @@ def validate_hdf5(afilepath): sattrs = dict(fhandle.attrs).copy() sattrs.update(dataset.attrs) dset = Subject(sattrs) - dset_val = DatasetValidation(name, dataset, fhandle, subject=dset) + dset_val = DatasetValidation(name, dataset, fhandle=fhandle, subject=dset) if not dset_val.is_valid(): valid = False diff --git a/lib/bald/validation.py b/lib/bald/validation.py index b159d16..50a2f2b 100644 --- a/lib/bald/validation.py +++ b/lib/bald/validation.py @@ -1,5 +1,6 @@ import numpy as np +import rdflib import bald @@ -40,8 +41,9 @@ def exceptions(self): class SubjectValidation(Validation): - def __init__(self, subject, httpcache=None): + def __init__(self, subject, fhandle, httpcache=None): self.subject = subject + self.fhandle = fhandle if isinstance(httpcache, bald.HttpStatusCache): self.cache = httpcache else: @@ -55,6 +57,10 @@ def exceptions(self): exceptions = [] exceptions = self.check_attr_uris(exceptions) exceptions = self.check_attr_domain_range(exceptions) + exceptions = self._extra_exceptions(exceptions) + return exceptions + + def _extra_exceptions(self, exceptions): return exceptions def check_attr_uris(self, exceptions): @@ -77,6 +83,39 @@ def _check_uri(uri, exceptions): return exceptions def check_attr_domain_range(self, exceptions): + for attr, value in self.subject.attrs.iteritems(): + uri = self.subject.unpack_uri(attr) + if self.cache.check_uri(uri): + #thus we have a payload + # go rdf + g = rdflib.Graph() + g.parse(data=self.cache[uri].text, format="n3") + query = ('SELECT ?s \n' + '(GROUP_CONCAT(?domain; SEPARATOR=" | ") AS ?domains) \n' + '(GROUP_CONCAT(?type; SEPARATOR=" | ") AS ?types) \n' + 'WHERE {{ \n' + '?s a ?type . \n' + 'OPTIONAL{{ ?s rdfs:domain ?domain . }} \n' + 'FILTER(?s = <{uria}> || ?s = <{urib}>) \n' + '}} \n' + 'GROUP BY ?s \n'.format(uria=uri, urib= uri.rstrip('/'))) + qres = list(g.query(query)) + if len(qres) != 1: + raise ValueError('{} does not define one and only one \n' + 'rdfs:domain'.format(uri)) + qres, = qres + # implement recursive inheritance check + # we need to check if the value that the attr points to + # has an rdf:type which is the same as the one required by + # the object property constraint + # The value may be a URI or it may be a reference to another + # subject within the file. + # therefore subjects in the file have to be typed?!? + + # import pdb; pdb.set_trace() + # if qres.domains != rdflib.term.URIRef(value): + # msg = ('The attribute {} references ') + # exceptions.appen(ValueError(msg)) return exceptions @@ -85,24 +124,19 @@ class ContainerValidation(SubjectValidation): def __init__(self, **kwargs): super(ContainerValidation, self).__init__(**kwargs) - def exceptions(self): - exceptions = [] - exceptions = self.check_attr_uris(exceptions) - return exceptions + + class DatasetValidation(SubjectValidation): - def __init__(self, name, dataset, fhandle, **kwargs): + def __init__(self, name, dataset, **kwargs): self.dataset = dataset self.name = name - self.fhandle = fhandle super(DatasetValidation, self).__init__(**kwargs) - def exceptions(self): - exceptions = [] - exceptions = self.check_attr_uris(exceptions) + def _extra_exceptions(self, exceptions): exceptions = self.check_array_references(exceptions) return exceptions From 821ae80b46b962a4b41ddee46fa1d8487b738e9b Mon Sep 17 00:00:00 2001 From: marqh Date: Sun, 10 Apr 2016 21:04:59 +0000 Subject: [PATCH 09/11] movement --- lib/bald/__init__.py | 7 ++++++- lib/bald/tests/integration/test_validation.py | 1 + lib/bald/validation.py | 10 +++++++--- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/bald/__init__.py b/lib/bald/__init__.py index be86014..5accea0 100644 --- a/lib/bald/__init__.py +++ b/lib/bald/__init__.py @@ -7,7 +7,10 @@ from bald.validation import ContainerValidation, DatasetValidation -class HttpStatusCache(object): +class HttpCache(object): + """ + Requests cache. + """ def __init__(self): self.cache = {} @@ -31,6 +34,8 @@ def check_uri(self, uri): class Subject(object): def __init__(self, attrs=None): """ + A subject of metadata statements. + attrs: an dictionary of key value pair attributes """ if attrs is None: diff --git a/lib/bald/tests/integration/test_validation.py b/lib/bald/tests/integration/test_validation.py index e85643c..ff50920 100644 --- a/lib/bald/tests/integration/test_validation.py +++ b/lib/bald/tests/integration/test_validation.py @@ -88,6 +88,7 @@ def test_mismatch_pdisjc_lead_dim(self): f = _fattrs(f) # child and parent have disjoint leading dimensions f = _create_parent_child(f, (4, 13, 17), (7, 13, 17)) + f.close() self.assertFalse(bald.validate_hdf5(tfile)) diff --git a/lib/bald/validation.py b/lib/bald/validation.py index 50a2f2b..273edaa 100644 --- a/lib/bald/validation.py +++ b/lib/bald/validation.py @@ -5,7 +5,7 @@ import bald -def valid_array_reference(parray, carray): +def valid_array_reference(parray, carray, broadcast_shape=None): """ Returns boolean. Validates bald array broadcasting rules between a parent array and a child array. @@ -14,6 +14,10 @@ def valid_array_reference(parray, carray): * parray - a numpy array: the parent of a bald array reference relation * carray - a numpy array: the child of a bald array reference relation + + * broadcast_shape - a string defining the ordered list of dimensions and sizes + for the resulting array from broadcasting parry and carray; + e.g. (1,1, """ # https://github.com/SciTools/courses/blob/master/course_content/notebooks/numpy_intro.ipynb # https://cs231n.github.io/python-numpy-tutorial/#numpy-broadcasting @@ -44,10 +48,10 @@ class SubjectValidation(Validation): def __init__(self, subject, fhandle, httpcache=None): self.subject = subject self.fhandle = fhandle - if isinstance(httpcache, bald.HttpStatusCache): + if isinstance(httpcache, bald.HttpCache): self.cache = httpcache else: - self.cache = bald.HttpStatusCache() + self.cache = bald.HttpCache() def is_valid(self): From 41ba52426ebcd8d27d21dee7f1391dcbeaa1bae5 Mon Sep 17 00:00:00 2001 From: marqh Date: Wed, 4 May 2016 21:33:28 +0000 Subject: [PATCH 10/11] interesting --- lib/bald/__init__.py | 8 ++++---- lib/bald/tests/integration/test_validation.py | 15 +++++++++------ lib/bald/tests/test_array_reference.py | 10 +++++----- lib/bald/tests/test_simple.py | 6 +++--- lib/bald/validation.py | 2 +- 5 files changed, 22 insertions(+), 19 deletions(-) diff --git a/lib/bald/__init__.py b/lib/bald/__init__.py index 5accea0..2fd4df4 100644 --- a/lib/bald/__init__.py +++ b/lib/bald/__init__.py @@ -41,7 +41,7 @@ def __init__(self, attrs=None): if attrs is None: attrs = [] self.attrs = attrs - self._prefix_suffix = re.compile('(^(?:(?!_._).)*)_._((?!.*_._).*$)') + self._prefix_suffix = re.compile('(^(?:(?!__).)*)__((?!.*__).*$)') _http_p = 'http[s]?://.*' self._http_uri = re.compile('{}'.format(_http_p)) self._http_uri_prefix = re.compile('{}/|#'.format(_http_p)) @@ -50,8 +50,8 @@ def __init__(self, attrs=None): def prefixes(self): prefixes = {} for key, value in self.attrs.iteritems(): - if key.endswith('_._') and self._http_uri_prefix.match(value): - pref = key.rstrip('_._') + if key.endswith('__') and self._http_uri_prefix.match(value): + pref = key.rstrip('__') if prefixes.has_key(pref): raise ValueError('This container has conflicting prefix definitions') prefixes[pref] = value @@ -65,7 +65,7 @@ def unpack_uri(self, astring): if self.prefixes().has_key(prefix): if self._http_uri.match(self.prefixes()[prefix]): - result = astring.replace('{}_._'.format(prefix), self.prefixes()[prefix]) + result = astring.replace('{}__'.format(prefix), self.prefixes()[prefix]) return result @contextlib.contextmanager diff --git a/lib/bald/tests/integration/test_validation.py b/lib/bald/tests/integration/test_validation.py index ff50920..00b7fd8 100644 --- a/lib/bald/tests/integration/test_validation.py +++ b/lib/bald/tests/integration/test_validation.py @@ -7,16 +7,19 @@ from bald.tests import BaldTestCase def _fattrs(f): - f.attrs['bald_._'] = 'http://binary-array-ld.net/experimental/' - f.attrs['bald_._type'] = 'bald_._Container' + f.attrs['bald__'] = 'http://binary-array-ld.net/experimental/' + f.attrs['rdf__'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' + f.attrs['rdf__type'] = 'bald__Container' return f def _create_parent_child(f, pshape, cshape): dsetp = f.create_dataset("parent_dataset", pshape, dtype='i') dsetc = f.create_dataset("child_dataset", cshape, dtype='i') - dsetp.attrs['bald_._type'] = 'bald_._Dataset' - dsetp.attrs['bald_._reference'] = dsetc.ref - dsetc.attrs['bald_._type'] = 'bald_._Dataset' + dsetp.attrs['rdf__type'] = 'bald__Dataset' + dsetp.attrs['bald__references'] = dsetc.ref + dsetc.attrs['rdf__type'] = 'bald__Dataset' + dsetc.attrs['rdf__type'] = 'bald__Reference' + dsetc.attrs['bald__dataset'] = dsetc.ref return f @@ -35,7 +38,7 @@ def test_invalid_uri(self): f = h5py.File(tfile, "w") f = _fattrs(f) f = _create_parent_child(f, (11, 17), (11, 17)) - f.attrs['bald_._turtle'] = 'bald_._walnut' + f.attrs['bald__turtle'] = 'bald__walnut' f.close() self.assertFalse(bald.validate_hdf5(tfile)) diff --git a/lib/bald/tests/test_array_reference.py b/lib/bald/tests/test_array_reference.py index b3b8aab..b182b70 100644 --- a/lib/bald/tests/test_array_reference.py +++ b/lib/bald/tests/test_array_reference.py @@ -6,16 +6,16 @@ from bald.tests import BaldTestCase def _fattrs(f): - f.attrs['bald_._'] = 'http://binary_array_ld.net/experimental' - f.attrs['bald_._type'] = 'bald_._Container' + f.attrs['bald__'] = 'http://binary_array_ld.net/experimental' + f.attrs['bald__type'] = 'bald__Container' return f def _create_parent_child(f, pshape, cshape): dsetp = f.create_dataset("parent_dataset", pshape, dtype='i') dsetc = f.create_dataset("child_dataset", cshape, dtype='i') - dsetp.attrs['bald_._type'] = 'bald_._Dataset' - dsetp.attrs['bald_._reference'] = dsetc.ref - dsetc.attrs['bald_._type'] = 'bald_._Dataset' + dsetp.attrs['bald__type'] = 'bald__Dataset' + dsetp.attrs['bald__reference'] = dsetc.ref + dsetc.attrs['bald__type'] = 'bald__Dataset' return f diff --git a/lib/bald/tests/test_simple.py b/lib/bald/tests/test_simple.py index 231b2ee..780130b 100644 --- a/lib/bald/tests/test_simple.py +++ b/lib/bald/tests/test_simple.py @@ -22,14 +22,14 @@ def test_make_load_file(self): with self.temp_filename('.hdf') as tfile: f = h5py.File(tfile, "w") dset = f.create_dataset("mydataset", (100,), dtype='i') - dset.attrs['bald_._'] = 'http://binary_array_ld.net/alpha' + dset.attrs['bald__'] = 'http://binary_array_ld.net/experimental' f.close() newf = h5py.File(tfile, "r") nset = newf.get('mydataset') self.assertEqual(nset.shape, (100,)) self.assertEqual(nset.dtype, 'int32') - self.assertEqual(nset.attrs.get('bald_._'), - 'http://binary_array_ld.net/alpha') + self.assertEqual(nset.attrs.get('bald__'), + 'http://binary_array_ld.net/experimental') if __name__ == '__main__': diff --git a/lib/bald/validation.py b/lib/bald/validation.py index 273edaa..fd300ec 100644 --- a/lib/bald/validation.py +++ b/lib/bald/validation.py @@ -156,7 +156,7 @@ def _check_ref(pdataset, parray, cdataset, carray): for attr, value in self.subject.attrs.iteritems(): # should support subtypes - if attr == 'bald_._reference': + if attr == 'bald__references': # check if it's this type, otherwise exception) # if isinstance(value, child_dset = self.fhandle[value] From e76e0532005285db4b5c78b49c074e7309bc2f56 Mon Sep 17 00:00:00 2001 From: marqh Date: Thu, 5 May 2016 14:19:31 +0000 Subject: [PATCH 11/11] use get --- lib/bald/tests/__init__.pyc | Bin 894 -> 879 bytes lib/bald/validation.py | 19 +++++++++++++------ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/bald/tests/__init__.pyc b/lib/bald/tests/__init__.pyc index 666a0386fddc282dfa3caddc40ce4c8ea88b1550..2e81140fe86a2bd6476fe7c2c9941198e22b7392 100644 GIT binary patch delta 70 zcmeyz_MVM{`7E8#xv;GP+MLWK!cSOU%hkNi4}Pnmn6Hdh$ocdMx4vSj40! I2Qte603zrey8r+H diff --git a/lib/bald/validation.py b/lib/bald/validation.py index fd300ec..cfaffaa 100644 --- a/lib/bald/validation.py +++ b/lib/bald/validation.py @@ -154,16 +154,23 @@ def _check_ref(pdataset, parray, cdataset, carray): exceptions.append(ValueError(msg)) return exceptions - for attr, value in self.subject.attrs.iteritems(): - # should support subtypes - if attr == 'bald__references': - # check if it's this type, otherwise exception) - # if isinstance(value, - child_dset = self.fhandle[value] + # if this Dataset has a bald__references + # not implemented yet: and it's a singleton + if self.subject.attrs.get('bald__references', ''): + # then it must have a bald_dataset + ref_dset = self.fhandle[self.subject.attrs.get('bald__references')] + if not ref_dset.attrs.get('bald__dataset', ''): + exceptions.append(ValueError('A bald__Reference must reference' + ' one and only one bald__Dataset') + ) + else: + # and we impose bald broadcasting rules on it + child_dset = self.fhandle[ref_dset.attrs.get('bald__dataset')] parray = np.zeros(self.dataset.shape) carray = np.zeros(child_dset.shape) exceptions = _check_ref('p', parray, 'c', carray) + return exceptions