Skip to content

Commit 1f15c01

Browse files
authored
Cdl (#12)
* test_cdl
1 parent 70f82d3 commit 1f15c01

File tree

12 files changed

+229
-65
lines changed

12 files changed

+229
-65
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
__pycache__/
33
*.py[cod]
44
*$py.class
5+
*.pyc
56

67
# C extensions
78
*.so

lib/bald/__init__.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,18 +124,23 @@ def validate_netcdf(afilepath):
124124
else:
125125
for k in fhandle.ncattrs():
126126
if k.endswith('__'):
127-
prefixes[k] = getattr(fhandle, k)
127+
prefixes[k] = getattr(fhandle, k)
128+
alias_group = fhandle[fhandle.bald__isAliasedBy] if hasattr(fhandle, 'bald__isAliasedBy') else {}
129+
aliases = {}
130+
if alias_group:
131+
aliases = dict([(alias, getattr(alias_group, alias)) for alias in alias_group.ncattrs()])
132+
128133
attrs = {}
129134
for k in fhandle.ncattrs():
130135
attrs[k] = getattr(fhandle, k)
131-
root_container = Subject(attrs, prefixes=prefixes)
136+
root_container = Subject(attrs, prefixes=prefixes, aliases=aliases)
132137
root_val = bv.ContainerValidation(subject=root_container,
133138
fhandle=fhandle)
134139
sval.stored_exceptions += root_val.exceptions()
135140
for name in fhandle.variables:
136141
sattrs = fhandle.__dict__.copy()
137142
sattrs.update(fhandle.variables[name].__dict__.copy())
138-
var = Subject(sattrs, prefixes=prefixes)
143+
var = Subject(sattrs, prefixes=prefixes, aliases=aliases)
139144
var_val = bv.ArrayValidation(name, fhandle.variables[name], fhandle=fhandle,
140145
subject=var)
141146
sval.stored_exceptions += var_val.exceptions()

lib/bald/tests/__init__.pyc

6 Bytes
Binary file not shown.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
netcdf tmpMwXy8U {
2+
dimensions:
3+
pdim0 = 11 ;
4+
pdim1 = 17 ;
5+
variables:
6+
int parent_variable(pdim0, pdim1) ;
7+
parent_variable:rdf__type = "bald__Array" ;
8+
parent_variable:SDN_ParameterDiscoveryCode = "BactTaxaAbundSed" ;
9+
parent_variable:submursible_name = "Nautilus" ;
10+
11+
// global attributes:
12+
:_NCProperties = "version=1|netcdflibversion=4.4.1|hdf5libversion=1.8.17" ;
13+
:rdf__type = "bald__Container" ;
14+
:bald__isPrefixedBy = "prefix_list" ;
15+
:bald__isAliasedBy = "alias_list" ;
16+
data:
17+
18+
parent_variable =
19+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
20+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
21+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
22+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
23+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
24+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
25+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
26+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
27+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
28+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
29+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ;
30+
31+
32+
group: prefix_list {
33+
34+
// group attributes:
35+
:bald__ = "http://binary-array-ld.net/latest/" ;
36+
:rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ;
37+
} // group bald__prefix_list
38+
39+
group: alias_list {
40+
41+
// group attributes:
42+
:SDN_ParameterDiscoveryCode = "http://vocab.nerc.ac.uk/isoCodelists/sdnCodelists/cdicsrCodeList.xml#SDN_ParameterDiscoveryCode" ;
43+
:BactTaxaAbundSed = "http://vocab.nerc.ac.uk/collection/P02/current/BAUC/" ;
44+
} // group bald__alias_list
45+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
netcdf tmpMwXy8U {
2+
dimensions:
3+
pdim0 = 11 ;
4+
pdim1 = 17 ;
5+
variables:
6+
int parent_variable(pdim0, pdim1) ;
7+
parent_variable:rdf__type = "bald__Array" ;
8+
parent_variable:bald__references = "child_variable" ;
9+
int child_variable(pdim0, pdim1) ;
10+
child_variable:rdf__type = "bald__Reference" ;
11+
child_variable:bald__array = "child_variable" ;
12+
13+
// global attributes:
14+
:_NCProperties = "version=1|netcdflibversion=4.4.1|hdf5libversion=1.8.17" ;
15+
:rdf__type = "bald__Container" ;
16+
:bald__isPrefixedBy = "prefix_list" ;
17+
data:
18+
19+
parent_variable =
20+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
21+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
22+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
23+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
24+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
25+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
26+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
27+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
28+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
29+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
30+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ;
31+
32+
child_variable =
33+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
34+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
35+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
36+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
37+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
38+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
39+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
40+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
41+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
42+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _,
43+
_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _ ;
44+
45+
group: prefix_list {
46+
47+
// group attributes:
48+
:bald__ = "http://binary-array-ld.net/latest/" ;
49+
:rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ;
50+
} // group bald__prefix_list
51+
}

lib/bald/tests/integration/test_aliases.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
def _fattrs(f):
1010
f.attrs['rdf__type'] = 'bald__Container'
11-
group_pref = f.create_group('bald__prefix_list')
11+
group_pref = f.create_group('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#'
1414
f.attrs['bald__isPrefixedBy'] = group_pref.ref
@@ -38,7 +38,8 @@ def test_valid_uri(self):
3838
dsetp.attrs['skosPrefLabel'] = 'alabel'
3939
f.close()
4040
validation = bald.validate_hdf5(tfile)
41-
self.assertTrue(validation.is_valid())
41+
exns = validation.exceptions()
42+
self.assertTrue(validation.is_valid(), msg='{} != []'.format(exns))
4243

4344
def test_invalid_uri(self):
4445
with self.temp_filename('.hdf') as tfile:
@@ -52,7 +53,17 @@ def test_invalid_uri(self):
5253
dsetp.attrs['skosPrefLabel'] = 'alabel'
5354
f.close()
5455
validation = bald.validate_hdf5(tfile)
55-
self.assertFalse(validation.is_valid())
56+
exns = validation.exceptions()
57+
expected = ['http://binary-array-ld.net/latest/turtle is not resolving as a resource (404).',
58+
'http://binary-array-ld.net/latest/walnut is not resolving as a resource (404).',
59+
'http://binary-array-ld.net/latest/turtle is not resolving as a resource (404).',
60+
'http://binary-array-ld.net/latest/walnut is not resolving as a resource (404).',
61+
'http://binary-array-ld.net/latest/turtle is not resolving as a resource (404).',
62+
'http://binary-array-ld.net/latest/walnut is not resolving as a resource (404).',
63+
'http://binary-array-ld.net/latest/turtle is not resolving as a resource (404).',
64+
'http://binary-array-ld.net/latest/walnut is not resolving as a resource (404).']
65+
self.assertTrue((not validation.is_valid()) and exns == expected,
66+
msg='{} != {}'.format(exns, expected))
5667

5768

5869
if __name__ == '__main__':
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import os
2+
import subprocess
3+
import unittest
4+
5+
import netCDF4
6+
import numpy as np
7+
8+
import bald
9+
from bald.tests import BaldTestCase
10+
11+
class Test(BaldTestCase):
12+
def setUp(self):
13+
self.cdl_path = os.path.join(os.path.dirname(__file__), 'CDL')
14+
print(self.cdl_path)
15+
16+
def test_array_reference(self):
17+
with self.temp_filename('.nc') as tfile:
18+
cdl_file = os.path.join(self.cdl_path, 'array_reference.cdl')
19+
subprocess.check_call(['ncgen', '-o', tfile, cdl_file])
20+
validation = bald.validate_netcdf(tfile)
21+
exns = validation.exceptions()
22+
self.assertTrue(validation.is_valid(), msg='{} != []'.format(exns))
23+
24+
def test_alias(self):
25+
with self.temp_filename('.nc') as tfile:
26+
cdl_file = os.path.join(self.cdl_path, 'array_alias.cdl')
27+
subprocess.check_call(['ncgen', '-o', tfile, cdl_file])
28+
validation = bald.validate_netcdf(tfile)
29+
exns = validation.exceptions()
30+
self.assertTrue(validation.is_valid(), msg='{} != []'.format(exns))

lib/bald/tests/integration/test_netcdf.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
def _fattrs(f):
1111
f.rdf__type = 'bald__Container'
12-
group_pref = f.createGroup('bald__prefix_list')
12+
group_pref = f.createGroup('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__isPrefixedBy = 'bald__prefix_list'
15+
f.bald__isPrefixedBy = 'prefix_list'
1616
return f
1717

1818
def _create_parent_child(f, pshape, cshape):
@@ -39,7 +39,8 @@ def test_valid_uri(self):
3939
f = _fattrs(f)
4040
f.close()
4141
validation = bald.validate_netcdf(tfile)
42-
self.assertTrue(validation.is_valid())
42+
exns = validation.exceptions()
43+
self.assertTrue(validation.is_valid(), msg='{} != []'.format(exns))
4344

4445
def test_invalid_uri(self):
4546
with self.temp_filename('.nc') as tfile:
@@ -49,7 +50,11 @@ def test_invalid_uri(self):
4950
setattr(f, 'bald__turtle', 'bald__walnut')
5051
f.close()
5152
validation = bald.validate_netcdf(tfile)
52-
self.assertFalse(validation.is_valid())
53+
exns = validation.exceptions()
54+
expected = ['http://binary-array-ld.net/latest/turtle is not resolving as a resource (404).',
55+
'http://binary-array-ld.net/latest/walnut is not resolving as a resource (404).']
56+
self.assertTrue((not validation.is_valid()) and exns == expected,
57+
msg='{} != {}'.format(exns, expected))
5358

5459

5560
class TestArrayReference(BaldTestCase):
@@ -60,7 +65,8 @@ def test_match(self):
6065
f = _create_parent_child(f, (11, 17), (11, 17))
6166
f.close()
6267
validation = bald.validate_netcdf(tfile)
63-
self.assertTrue(validation.is_valid())
68+
exns = validation.exceptions()
69+
self.assertTrue(validation.is_valid(), msg='{} != []'.format(exns))
6470

6571
def test_mismatch_zeroth(self):
6672
with self.temp_filename('.nc') as tfile:
@@ -69,7 +75,10 @@ def test_mismatch_zeroth(self):
6975
f = _create_parent_child(f, (11, 17), (11, 13))
7076
f.close()
7177
validation = bald.validate_netcdf(tfile)
72-
self.assertFalse(validation.is_valid())
78+
exns = validation.exceptions()
79+
expected = ['p declares a child of c but the arrays do not conform to the bald array reference rules']
80+
self.assertTrue((not validation.is_valid()) and exns == expected,
81+
msg='{} != {}'.format(exns, expected))
7382

7483

7584
if __name__ == '__main__':

lib/bald/tests/integration/test_netcdf4_classic.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ def test_valid_uri(self):
3737
f = _fattrs(f)
3838
f.close()
3939
validation = bald.validate_netcdf(tfile)
40-
self.assertTrue(validation.is_valid())
40+
exns = validation.exceptions()
41+
self.assertTrue(validation.is_valid(), msg='{} != []'.format(exns))
4142

4243
def test_invalid_uri(self):
4344
with self.temp_filename('.nc') as tfile:
@@ -47,7 +48,12 @@ def test_invalid_uri(self):
4748
setattr(f, 'bald__turtle', 'bald__walnut')
4849
f.close()
4950
validation = bald.validate_netcdf(tfile)
50-
self.assertFalse(validation.is_valid())
51+
exns = validation.exceptions()
52+
53+
expected = ['http://binary-array-ld.net/latest/turtle is not resolving as a resource (404).',
54+
'http://binary-array-ld.net/latest/walnut is not resolving as a resource (404).']
55+
self.assertTrue((not validation.is_valid()) and exns == expected,
56+
msg='{} != {}'.format(exns, expected))
5157

5258

5359
class TestArrayReference(BaldTestCase):
@@ -58,7 +64,8 @@ def test_match(self):
5864
f = _create_parent_child(f, (11, 17), (11, 17))
5965
f.close()
6066
validation = bald.validate_netcdf(tfile)
61-
self.assertTrue(validation.is_valid())
67+
exns = validation.exceptions()
68+
self.assertTrue(validation.is_valid(), msg='{} != []'.format(exns))
6269

6370
def test_mismatch_zeroth(self):
6471
with self.temp_filename('.nc') as tfile:
@@ -67,7 +74,10 @@ def test_mismatch_zeroth(self):
6774
f = _create_parent_child(f, (11, 17), (11, 13))
6875
f.close()
6976
validation = bald.validate_netcdf(tfile)
70-
self.assertFalse(validation.is_valid())
77+
exns = validation.exceptions()
78+
expected = ['p declares a child of c but the arrays do not conform to the bald array reference rules']
79+
self.assertTrue((not validation.is_valid()) and exns == expected,
80+
msg='{} != {}'.format(exns, expected))
7181

7282

7383
if __name__ == '__main__':

0 commit comments

Comments
 (0)