Skip to content

Commit c78ed91

Browse files
committed
nc array ref
1 parent ff5c639 commit c78ed91

File tree

3 files changed

+53
-36
lines changed

3 files changed

+53
-36
lines changed

lib/bald/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,13 +104,23 @@ def validate_netcdf(afilepath):
104104
"""
105105

106106
with load(afilepath) as fhandle:
107+
sval = bv.StoredValidation()
107108
attrs = {}
108109
for k in fhandle.ncattrs():
109110
attrs[k] = getattr(fhandle, k)
110111
root_container = Subject(attrs)
111112
root_val = bv.ContainerValidation(subject=root_container,
112113
fhandle=fhandle)
113-
return root_val
114+
sval.stored_exceptions += root_val.exceptions()
115+
for name in fhandle.variables:
116+
sattrs = fhandle.__dict__.copy()
117+
sattrs.update(fhandle.variables[name].__dict__.copy())
118+
var = Subject(sattrs)
119+
var_val = bv.ArrayValidation(name, fhandle.variables[name], fhandle=fhandle,
120+
subject=var)
121+
sval.stored_exceptions += var_val.exceptions()
122+
123+
return sval
114124

115125

116126
def validate_hdf5(afilepath):
@@ -130,8 +140,8 @@ def validate_hdf5(afilepath):
130140
for name, dataset in fhandle.items():
131141
# a dataset's attribute collection inherits from and
132142
# specialises it's container's attrbiute collection
143+
# this only helps with prefixes, afaik, hence:
133144
# #
134-
# this only helps with prefixes, afaik
135145
sattrs = dict(fhandle.attrs).copy()
136146
sattrs.update(dataset.attrs)
137147
dset = Subject(sattrs)

lib/bald/tests/integration/test_netcdf.py

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ def _fattrs(f):
1313
f.rdf__type = 'bald__Container'
1414
return f
1515

16-
# def _create_parent_child(f, pshape, cshape):
17-
# dsetp = f.create_dataset("parent_dataset", pshape, dtype='i')
18-
# dsetc = f.create_dataset("child_dataset", cshape, dtype='i')
19-
# dsetp.attrs['rdf__type'] = 'bald__Dataset'
20-
# dsetp.attrs['bald__references'] = dsetc.ref
21-
# dsetc.attrs['rdf__type'] = 'bald__Dataset'
22-
# dsetc.attrs['rdf__type'] = 'bald__Reference'
23-
# dsetc.attrs['bald__dataset'] = dsetc.ref
24-
# return f
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
2529

2630

2731
class Test(BaldTestCase):
@@ -45,25 +49,25 @@ def test_invalid_uri(self):
4549
validation = bald.validate_netcdf(tfile)
4650
self.assertFalse(validation.is_valid())
4751

48-
# class TestArrayReference(BaldTestCase):
49-
# def test_match(self):
50-
# with self.temp_filename('.nc') as tfile:
51-
# f = netCDF4.Dataset(tfile, "w", format="NETCDF4")
52-
# f = _fattrs(f)
53-
# f = _create_parent_child(f, (11, 17), (11, 17))
54-
# f.close()
55-
# validation = bald.validate_netcdf(tfile)
56-
# self.assertTrue(validation.is_valid())
57-
58-
# def test_mismatch_zeroth(self):
59-
# with self.temp_filename('.nc') as tfile:
60-
# f = netCDF4.Dataset(tfile, "w", format="NETCDF4")
61-
# f = _fattrs(f)
62-
# f = _create_parent_child(f, (11, 17), (11, 13))
63-
# f.close()
64-
# validation = bald.validate_netcdf(tfile)
65-
# self.assertFalse(validation.is_valid())
6652

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")
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")
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())
6771

6872

6973
if __name__ == '__main__':

lib/bald/validation.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -170,16 +170,19 @@ def _check_ref(parraysubj, parray, carraysubj, carray):
170170
if self.subject.attrs.get('bald__references', ''):
171171
# then it must have a bald_array
172172
ref_dset = self.fhandle[self.subject.attrs.get('bald__references')]
173-
if not ref_dset.attrs.get('bald__array', ''):
173+
child_dset = None
174+
if (hasattr(ref_dset, 'attrs')):
175+
child_dset = self.fhandle[ref_dset.attrs.get('bald__array', None)]
176+
elif 'bald__array' in ref_dset.ncattrs():
177+
child_dset = self.fhandle[ref_dset.bald__array]
178+
else:
174179
exceptions.append(ValueError('A bald__Reference must link to '
175180
'one and only one bald__Array'))
176-
else:
177-
# and we impose bald broadcasting rules on it
178-
child_dset = self.fhandle[ref_dset.attrs.get('bald__array')]
179-
parray = np.zeros(self.array.shape)
180-
carray = np.zeros(child_dset.shape)
181+
# and we impose bald broadcasting rules on it
182+
parray = np.zeros(self.array.shape)
183+
carray = np.zeros(child_dset.shape)
181184

182-
exceptions = _check_ref('p', parray, 'c', carray)
185+
exceptions = _check_ref('p', parray, 'c', carray)
183186

184187
return exceptions
185188

0 commit comments

Comments
 (0)