Skip to content

Commit c1f670a

Browse files
committed
aliases and netcdf4
1 parent 97a6b0b commit c1f670a

File tree

5 files changed

+215
-4
lines changed

5 files changed

+215
-4
lines changed

lib/bald/__init__.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,10 +108,14 @@ def validate_netcdf(afilepath):
108108

109109
with load(afilepath) as fhandle:
110110
sval = bv.StoredValidation()
111-
prefix_group = fhandle[fhandle.bald__prefixes] if hasattr(fhandle, 'bald__prefixes') else {}
111+
prefix_group = fhandle[fhandle.bald__isPrefixedBy] if hasattr(fhandle, 'bald__isPrefixedBy') else {}
112112
prefixes = {}
113113
if prefix_group:
114114
prefixes = dict([(prefix, getattr(prefix_group, prefix)) for prefix in prefix_group.ncattrs()])
115+
else:
116+
for k in fhandle.ncattrs():
117+
if k.endswith('__'):
118+
prefixes[k] = getattr(fhandle, k)
115119
attrs = {}
116120
for k in fhandle.ncattrs():
117121
attrs[k] = getattr(fhandle, k)
@@ -139,7 +143,7 @@ def validate_hdf5(afilepath):
139143
with load(afilepath) as fhandle:
140144
sval = bv.StoredValidation()
141145
cache = {}
142-
prefix_group = fhandle.attrs.get('bald__prefixes')
146+
prefix_group = fhandle.attrs.get('bald__isPrefixedBy')
143147
prefixes = {}
144148
if prefix_group:
145149
prefixes = fhandle[prefix_group].attrs
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
import unittest
2+
3+
import h5py
4+
import numpy as np
5+
6+
import bald
7+
from bald.tests import BaldTestCase
8+
9+
def _fattrs(f):
10+
f.attrs['rdf__type'] = 'bald__Container'
11+
group_alias = f.create_group('bald__alias_list')
12+
group_pref.attrs['bald__'] = 'http://binary-array-ld.net/latest/'
13+
group_pref.attrs['rdf__'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
14+
f.attrs['bald__aliases'] = group_alias.ref
15+
return f
16+
17+
def _create_parent_child(f, pshape, cshape):
18+
dsetp = f.create_dataset("parent_dataset", pshape, dtype='i')
19+
dsetc = f.create_dataset("child_dataset", cshape, dtype='i')
20+
dsetp.attrs['rdf__type'] = 'bald__Array'
21+
dsetp.attrs['bald__references'] = dsetc.ref
22+
dsetc.attrs['rdf__type'] = 'bald__Array'
23+
dsetc.attrs['rdf__type'] = 'bald__Reference'
24+
dsetc.attrs['bald__array'] = dsetc.ref
25+
return f
26+
27+
28+
class Test(BaldTestCase):
29+
30+
def test_valid_uri(self):
31+
with self.temp_filename('.hdf') as tfile:
32+
f = h5py.File(tfile, "w")
33+
f = _fattrs(f)
34+
f = _create_parent_child(f, (11, 17), (11, 17))
35+
f.close()
36+
validation = bald.validate_hdf5(tfile)
37+
self.assertTrue(validation.is_valid())
38+
39+
def test_invalid_uri(self):
40+
with self.temp_filename('.hdf') as tfile:
41+
f = h5py.File(tfile, "w")
42+
f = _fattrs(f)
43+
f = _create_parent_child(f, (11, 17), (11, 17))
44+
f.attrs['bald__turtle'] = 'bald__walnut'
45+
f.close()
46+
validation = bald.validate_hdf5(tfile)
47+
self.assertFalse(validation.is_valid())
48+
49+
class TestArrayReference(BaldTestCase):
50+
def test_match(self):
51+
with self.temp_filename('.hdf') as tfile:
52+
f = h5py.File(tfile, "w")
53+
f = _fattrs(f)
54+
f = _create_parent_child(f, (11, 17), (11, 17))
55+
f.close()
56+
validation = bald.validate_hdf5(tfile)
57+
self.assertTrue(validation.is_valid())
58+
59+
def test_mismatch_zeroth(self):
60+
with self.temp_filename('.hdf') as tfile:
61+
f = h5py.File(tfile, "w")
62+
f = _fattrs(f)
63+
f = _create_parent_child(f, (11, 17), (11, 13))
64+
f.close()
65+
validation = bald.validate_hdf5(tfile)
66+
self.assertFalse(validation.is_valid())
67+
68+
def test_mismatch_oneth(self):
69+
with self.temp_filename('.hdf') as tfile:
70+
f = h5py.File(tfile, "w")
71+
f = _fattrs(f)
72+
f = _create_parent_child(f, (11, 17), (13, 17))
73+
f.close()
74+
validation = bald.validate_hdf5(tfile)
75+
self.assertFalse(validation.is_valid())
76+
77+
def test_match_plead_dim(self):
78+
with self.temp_filename('.hdf') as tfile:
79+
f = h5py.File(tfile, "w")
80+
f = _fattrs(f)
81+
# parent has leading dimension wrt child
82+
f = _create_parent_child(f, (4, 13, 17), (13, 17))
83+
f.close()
84+
validation = bald.validate_hdf5(tfile)
85+
self.assertTrue(validation.is_valid())
86+
87+
def test_match_clead_dim(self):
88+
with self.temp_filename('.hdf') as tfile:
89+
f = h5py.File(tfile, "w")
90+
f = _fattrs(f)
91+
# child has leading dimension wrt parent
92+
f = _create_parent_child(f, (13, 17), (7, 13, 17))
93+
f.close()
94+
validation = bald.validate_hdf5(tfile)
95+
self.assertTrue(validation.is_valid())
96+
97+
def test_mismatch_pdisjc_lead_dim(self):
98+
with self.temp_filename('.hdf') as tfile:
99+
f = h5py.File(tfile, "w")
100+
f = _fattrs(f)
101+
# child and parent have disjoint leading dimensions
102+
f = _create_parent_child(f, (4, 13, 17), (7, 13, 17))
103+
104+
f.close()
105+
validation = bald.validate_hdf5(tfile)
106+
self.assertFalse(validation.is_valid())
107+
108+
def test_mismatch_pdisjc_trail_dim(self):
109+
with self.temp_filename('.hdf') as tfile:
110+
f = h5py.File(tfile, "w")
111+
f = _fattrs(f)
112+
# child and parent have disjoint trailing dimensions
113+
f = _create_parent_child(f, (13, 17, 2), (13, 17, 9))
114+
f.close()
115+
validation = bald.validate_hdf5(tfile)
116+
self.assertFalse(validation.is_valid())
117+
118+
119+
# def test_match_(self):
120+
# with self.temp_filename('.hdf') as tfile:
121+
# f = h5py.File(tfile, "w")
122+
# f = _fattrs(f)
123+
# #
124+
# f = _create_parent_child(f, (), ())
125+
# f.close()
126+
# self.assert(bald.validate_hdf5(tfile).is_valid())
127+
128+
if __name__ == '__main__':
129+
unittest.main()

lib/bald/tests/integration/test_netcdf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def _fattrs(f):
1212
group_pref = f.createGroup('bald__prefix_list')
1313
group_pref.bald__ = 'http://binary-array-ld.net/latest/'
1414
group_pref.rdf__ = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
15-
f.bald__prefixes = 'bald__prefix_list'
15+
f.bald__isPrefixedBy = 'bald__prefix_list'
1616
return f
1717

1818
def _create_parent_child(f, pshape, cshape):
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
import unittest
2+
3+
import h5py
4+
import netCDF4
5+
import numpy as np
6+
7+
import bald
8+
from bald.tests import BaldTestCase
9+
10+
def _fattrs(f):
11+
f.bald__ = 'http://binary-array-ld.net/latest/'
12+
f.rdf__ = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
13+
f.rdf__type = 'bald__Container'
14+
return f
15+
16+
def _create_parent_child(f, pshape, cshape):
17+
for i, pdimsize in enumerate(pshape):
18+
f.createDimension("pdim{}".format(str(i)), pdimsize)
19+
for i, cdimsize in enumerate(cshape):
20+
f.createDimension("cdim{}".format(str(i)), cdimsize)
21+
varp = f.createVariable("parent_variable", 'i4', tuple(["pdim{}".format(str(i)) for i, _ in enumerate(pshape)]))
22+
varc = f.createVariable("child_variable", 'i4', tuple(["cdim{}".format(str(i)) for i, _ in enumerate(cshape)]))
23+
varp.rdf__type = 'bald__Array'
24+
varp.bald__references = "child_variable"
25+
varc.rdf__type = 'bald__Array'
26+
varc.rdf__type = 'bald__Reference'
27+
varc.bald__array = "child_variable"
28+
return f
29+
30+
31+
class Test(BaldTestCase):
32+
33+
def test_valid_uri(self):
34+
with self.temp_filename('.nc') as tfile:
35+
f = netCDF4.Dataset(tfile, "w", format="NETCDF4_CLASSIC")
36+
37+
f = _fattrs(f)
38+
f.close()
39+
validation = bald.validate_netcdf(tfile)
40+
self.assertTrue(validation.is_valid())
41+
42+
def test_invalid_uri(self):
43+
with self.temp_filename('.nc') as tfile:
44+
f = netCDF4.Dataset(tfile, "w", format="NETCDF4_CLASSIC")
45+
46+
f = _fattrs(f)
47+
setattr(f, 'bald__turtle', 'bald__walnut')
48+
f.close()
49+
validation = bald.validate_netcdf(tfile)
50+
self.assertFalse(validation.is_valid())
51+
52+
53+
class TestArrayReference(BaldTestCase):
54+
def test_match(self):
55+
with self.temp_filename('.nc') as tfile:
56+
f = netCDF4.Dataset(tfile, "w", format="NETCDF4_CLASSIC")
57+
f = _fattrs(f)
58+
f = _create_parent_child(f, (11, 17), (11, 17))
59+
f.close()
60+
validation = bald.validate_netcdf(tfile)
61+
self.assertTrue(validation.is_valid())
62+
63+
def test_mismatch_zeroth(self):
64+
with self.temp_filename('.nc') as tfile:
65+
f = netCDF4.Dataset(tfile, "w", format="NETCDF4_CLASSIC")
66+
f = _fattrs(f)
67+
f = _create_parent_child(f, (11, 17), (11, 13))
68+
f.close()
69+
validation = bald.validate_netcdf(tfile)
70+
self.assertFalse(validation.is_valid())
71+
72+
73+
if __name__ == '__main__':
74+
unittest.main()
75+
76+
77+
78+

lib/bald/tests/integration/test_validation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ def _fattrs(f):
1111
group_pref = f.create_group('bald__prefix_list')
1212
group_pref.attrs['bald__'] = 'http://binary-array-ld.net/latest/'
1313
group_pref.attrs['rdf__'] = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'
14-
f.attrs['bald__prefixes'] = group_pref.ref
14+
f.attrs['bald__isPrefixedBy'] = group_pref.ref
1515
return f
1616

1717
def _create_parent_child(f, pshape, cshape):

0 commit comments

Comments
 (0)