diff --git a/.gitignore b/.gitignore index 1dbc687..9b86f6f 100644 --- a/.gitignore +++ b/.gitignore @@ -60,3 +60,6 @@ target/ #Ipython Notebook .ipynb_checkpoints + +*~ + diff --git a/lib/bald/__init__.py b/lib/bald/__init__.py new file mode 100644 index 0000000..2fd4df4 --- /dev/null +++ b/lib/bald/__init__.py @@ -0,0 +1,112 @@ +import contextlib +import re + +import h5py +import requests + +from bald.validation import ContainerValidation, DatasetValidation + + +class HttpCache(object): + """ + Requests cache. + """ + def __init__(self): + self.cache = {} + + def __getitem__(self, 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: + 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].status_code == 200: + result = True + return result + +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: + 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('.hdf'): + loader = h5py.File + else: + raise ValueError('filepath suffix not supported') + try: + f = loader(afilepath, "r") + yield f + finally: + f.close() + + +def validate_hdf5(afilepath): + """ + Validate a file with respect ot binarry-array-linked-data. + Returns a :class:`bald.validation.Validation` + """ + + with load(afilepath) as fhandle: + valid = True + cache = {} + root_container = Subject(fhandle.attrs) + root_val = ContainerValidation(subject=root_container, fhandle=fhandle) + if not root_val.is_valid(): + valid = False + # iterate through the datasets + for name, dataset in fhandle.items(): + # 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=fhandle, subject=dset) + if not dset_val.is_valid(): + valid = False + + return valid + + + + diff --git a/lib/bald/tests/__init__.py b/lib/bald/tests/__init__.py new file mode 100644 index 0000000..bc00261 --- /dev/null +++ b/lib/bald/tests/__init__.py @@ -0,0 +1,16 @@ +import contextlib +import os +import tempfile +import unittest + + +class BaldTestCase(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) diff --git a/lib/bald/tests/__init__.pyc b/lib/bald/tests/__init__.pyc new file mode 100644 index 0000000..2e81140 Binary files /dev/null and b/lib/bald/tests/__init__.pyc differ 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/integration/test_validation.py b/lib/bald/tests/integration/test_validation.py new file mode 100644 index 0000000..00b7fd8 --- /dev/null +++ b/lib/bald/tests/integration/test_validation.py @@ -0,0 +1,119 @@ +import unittest + +import h5py +import numpy as np + +import bald +from bald.tests import BaldTestCase + +def _fattrs(f): + 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['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 + + +class Test(BaldTestCase): + + def test_valid_uri(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_invalid_uri(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.attrs['bald__turtle'] = 'bald__walnut' + f.close() + self.assertFalse(bald.validate_hdf5(tfile)) + +class TestArrayReference(BaldTestCase): + def test_match(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_mismatch_zeroth(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_mismatch_oneth(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)) + + 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/tests/test_array_reference.py b/lib/bald/tests/test_array_reference.py new file mode 100644 index 0000000..b182b70 --- /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_simple.py b/lib/bald/tests/test_simple.py new file mode 100644 index 0000000..780130b --- /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 Test(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/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/experimental') + + +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/tests/unit/test_validation.py b/lib/bald/tests/unit/test_validation.py new file mode 100644 index 0000000..09a1815 --- /dev/null +++ b/lib/bald/tests/unit/test_validation.py @@ -0,0 +1,20 @@ +import unittest + +import h5py +import numpy as np + +from bald.tests import BaldTestCase +from bald import validation + +class Test(unittest.TestCase): + def test_check_uri_200(self): + auri = 'http://binary-array-ld.net/experimental' + self.assertTrue(validation.check_uri(auri)) + + def test_check_uri_404(self): + notauri = 'http://binary-array-ld.net/experimentalish' + self.assertFalse(validation.check_uri(notauri)) + + +if __name__ == '__main__': + unittest.main() diff --git a/lib/bald/validation.py b/lib/bald/validation.py new file mode 100644 index 0000000..cfaffaa --- /dev/null +++ b/lib/bald/validation.py @@ -0,0 +1,183 @@ + +import numpy as np +import rdflib + +import bald + + +def valid_array_reference(parray, carray, broadcast_shape=None): + """ + 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 + + * 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 + # 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): + + def is_valid(self): + return not self.exceptions() + + def exceptions(self): + exceptions = [] + return exceptions + +class SubjectValidation(Validation): + + def __init__(self, subject, fhandle, httpcache=None): + self.subject = subject + self.fhandle = fhandle + if isinstance(httpcache, bald.HttpCache): + self.cache = httpcache + else: + self.cache = bald.HttpCache() + + + def is_valid(self): + return not self.exceptions() + + 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): + def _check_uri(uri, exceptions): + if not self.cache.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.subject.prefixes().iteritems(): + exceptions = _check_uri(self.subject.unpack_uri(uri), + exceptions) + 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 + + 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 + + +class ContainerValidation(SubjectValidation): + + def __init__(self, **kwargs): + super(ContainerValidation, self).__init__(**kwargs) + + + + +class DatasetValidation(SubjectValidation): + + + def __init__(self, name, dataset, **kwargs): + + self.dataset = dataset + self.name = name + super(DatasetValidation, self).__init__(**kwargs) + + def _extra_exceptions(self, 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 + + # 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 + + +class ValidationSet(Validation): + pass + +class ValidationList(Validation): + pass + + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..e69de29