Skip to content

Commit dc90cca

Browse files
committed
too many references
1 parent b25b2ec commit dc90cca

14 files changed

+191
-136
lines changed

lib/bald/__init__.py

Lines changed: 40 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -476,6 +476,8 @@ def graph_elems(self):
476476

477477
if hasattr(self, 'bald__array'):
478478
for aref in self.bald__array:
479+
if isinstance(aref, str):
480+
raise TypeError('unexpected string: {}'.format(aref))
479481
alink = "link({var}, {target}, 'bald__array', 'bottom');"
480482
alink = alink.format(var=self.identity, target=aref.identity)
481483
links.append(alink)
@@ -511,19 +513,6 @@ def graph_elems(self):
511513
return instances, links
512514

513515

514-
@contextlib.contextmanager
515-
def load(afilepath):
516-
if afilepath.endswith('.hdf'):
517-
loader = h5py.File
518-
else:
519-
raise ValueError('filepath suffix not supported')
520-
try:
521-
f = loader(afilepath, "r")
522-
yield f
523-
finally:
524-
f.close()
525-
526-
527516
@contextlib.contextmanager
528517
def load(afilepath):
529518
if afilepath.endswith('.hdf'):
@@ -540,11 +529,13 @@ def load(afilepath):
540529

541530
def load_netcdf(afilepath, baseuri=None):
542531
"""
543-
Validate a file with respect to binary-array-linked-data.
544-
Returns a :class:`bald.validation.Validation`
532+
Load a file with respect to binary-array-linked-data.
533+
Returns a :class:`bald.Collection`
545534
"""
546535

547536
with load(afilepath) as fhandle:
537+
if baseuri is None:
538+
baseuri = 'file://{}'.format(afilepath)
548539
prefix_var_name = None
549540
if hasattr(fhandle, 'bald__isPrefixedBy'):
550541
prefix_var_name = fhandle.bald__isPrefixedBy
@@ -609,8 +600,7 @@ def load_netcdf(afilepath, baseuri=None):
609600
# netCDF coordinate variable special case
610601
if (len(fhandle.variables[name].dimensions) == 1 and
611602
fhandle.variables[name].dimensions[0] == name):
612-
#sattrs['bald__array'] = name
613-
sattrs['bald__array'] = identity
603+
sattrs['bald__array'] = name
614604
sattrs['rdf__type'] = 'bald__Reference'
615605

616606
if fhandle.variables[name].shape:
@@ -629,18 +619,42 @@ def load_netcdf(afilepath, baseuri=None):
629619
continue
630620

631621
var = file_variables[name]
632-
# reverse lookup based on type to be added
633-
lookups = ['bald__references', 'bald__array']
634-
for lookup in lookups:
635-
if lookup in var.attrs:
636-
child_dset_set = var.attrs[lookup].split(' ')
637-
var.attrs[lookup] = set()
638-
for child_dset_name in child_dset_set:
639-
carray = file_variables.get(child_dset_name)
640-
var.attrs[lookup].add(carray)
622+
sattrs = fhandle.variables[name].__dict__.copy()
623+
# netCDF coordinate variable special case
624+
if (len(fhandle.variables[name].dimensions) == 1 and
625+
fhandle.variables[name].dimensions[0] == name):
626+
sattrs['bald__array'] = name
627+
628+
for sattr in sattrs:
629+
if (isinstance(sattrs[sattr], str) and
630+
file_variables.get(sattrs[sattr])):
631+
# next: remove all use of set, everything is dict or orderedDict
632+
var.attrs[sattr] = set((file_variables.get(sattrs[sattr]),))
633+
elif isinstance(sattrs[sattr], str):
634+
potrefs_list = sattrs[sattr].split(',')
635+
potrefs_set = sattrs[sattr].split(' ')
636+
if len(potrefs_list) > 1:
637+
refs = np.array([file_variables.get(pref) is not None
638+
for pref in potrefs_list])
639+
if np.all(refs):
640+
var.attrs[sattr] = [file_variables.get(pref)
641+
for pref in potrefs_list]
642+
643+
elif len(potrefs_set) > 1:
644+
refs = np.array([file_variables.get(pref) is not None
645+
for pref in potrefs_set])
646+
if np.all(refs):
647+
var.attrs[sattr] = set([file_variables.get(pref)
648+
for pref in potrefs_set])
649+
650+
# if name == 'pdim0':
651+
# import pdb; pdb.set_trace()
652+
641653
# coordinate variables are bald__references except for
642654
# variables that already declare themselves as bald__Reference
643655
if 'bald__Reference' not in var.rdf__type:
656+
# if name == 'pdim0':
657+
# import pdb; pdb.set_trace()
644658
for dim in fhandle.variables[name].dimensions:
645659
if file_variables.get(dim):
646660
cv_shape = fhandle.variables[dim].shape

lib/bald/tests/integration/CDL/multi_array_reference.cdl

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,17 @@ variables:
77
prefix_list:bald__ = "http://binary-array-ld.net/latest/" ;
88
prefix_list:metce__ = "http://codes.wmo.int/common/observation-type/METCE/2013/" ;
99
prefix_list:rdf__ = "http://www.w3.org/1999/02/22-rdf-syntax-ns#" ;
10+
prefix_list:some_vocab__ = "http://binary-array-ld.net/latest/" ;
1011

11-
int variable1(pdim0, pdim1) ;
12-
variable1:bald__references = "location_variable" ;
13-
variable1:long_name = "Gerald";
14-
variable1:obtype = "metce__SamplingObservation";
12+
int data_variable1(pdim0, pdim1) ;
13+
data_variable1:bald__references = "location_variable" ;
14+
data_variable1:long_name = "Gerald";
15+
data_variable1:obtype = "metce__SamplingObservation";
1516

16-
int variable2(pdim0, pdim1) ;
17-
variable2:bald__references = "location_variable" ;
18-
variable2:long_name = "Imelda";
19-
variable2:obtype = "metce__SamplingObservation";
17+
int data_variable2(pdim0, pdim1) ;
18+
data_variable2:bald__references = "location_variable" ;
19+
data_variable2:long_name = "Imelda";
20+
data_variable2:obtype = "metce__SamplingObservation";
2021

2122
int pdim0(pdim0) ;
2223

@@ -32,6 +33,13 @@ variables:
3233
location_reference_system:bald__array = "location_reference_system";
3334
location_reference_system:pcode = "4897";
3435

36+
int set_collection ;
37+
set_collection:some_vocab__references = "data_variable1 data_variable2" ;
38+
39+
int list_collection ;
40+
list_collection:some_vocab__references = "data_variable1,data_variable2" ;
41+
42+
3543
// global attributes:
3644
:_NCProperties = "version=1|netcdflibversion=4.4.1|hdf5libversion=1.8.17" ;
3745
:bald__isPrefixedBy = "prefix_list" ;

lib/bald/tests/integration/HTML/array_reference.html

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -174,13 +174,13 @@
174174
return cell;
175175
};
176176

177-
var child_variable = instance('child_variable:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17'], '#878800');
178-
var parent_variable = instance('parent_variable:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17'], '#878800');
179-
var root = instance('root:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');
180-
link(child_variable, child_variable, 'bald__array', 'bottom');
181-
link(parent_variable, child_variable, 'bald__references');
182-
link(root, child_variable, 'bald__contains', 'top', true);
183-
link(root, parent_variable, 'bald__contains', 'top', true);
177+
var file://CDL/array_reference.cdl = instance('file://CDL/array_reference.cdl:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');
178+
var file://CDL/array_reference.cdl/child_variable = instance('file://CDL/array_reference.cdl/child_variable:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17'], '#878800');
179+
var file://CDL/array_reference.cdl/parent_variable = instance('file://CDL/array_reference.cdl/parent_variable:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17'], '#878800');
180+
link(file://CDL/array_reference.cdl, file://CDL/array_reference.cdl/child_variable, 'bald__contains', 'top', true);
181+
link(file://CDL/array_reference.cdl, file://CDL/array_reference.cdl/parent_variable, 'bald__contains', 'top', true);
182+
link(file://CDL/array_reference.cdl/child_variable, file://CDL/array_reference.cdl/child_variable, 'bald__array', 'bottom');
183+
link(file://CDL/array_reference.cdl/parent_variable, file://CDL/array_reference.cdl/child_variable, 'bald__references');
184184
joint.layout.DirectedGraph.layout(graph, { setLinkVertices: false,
185185
nodeSep: 150, rankSep: 100,
186186
marginX: 100, marginY: 100,

lib/bald/tests/integration/HTML/hdf_container_nest.html

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,14 +174,14 @@
174174
return cell;
175175
};
176176

177-
var alocation = instance('alocation:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17'], '#878800');
177+
var alocation = instance('alocation:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17'], '#878800');
178178
var anotherpair = instance('anotherpair:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>, <a xlink:href="http://binary-array-ld.net/latest/Reference" xlink:show=new text-decoration="underline">bald__Reference</a>', ['<a xlink:href="http://binary-array-ld.net/latest/array" xlink:show=new text-decoration="underline">bald__array</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 2'], '#878800');
179179
var apair = instance('apair:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 2'], '#878800');
180180
var data = instance('data:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/references" xlink:show=new text-decoration="underline">bald__references</a>: |', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: 11, 17'], '#878800');
181181
var discovery = instance('discovery:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');
182-
var institution = instance('institution:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://www.w3.org/2004/02/skos/core#prefLabel" xlink:show=new text-decoration="underline">skos__prefLabel</a>: a quality establishment', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: |'], '#878800');
183-
var locref = instance('locref:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://www.w3.org/2004/02/skos/core#prefLabel" xlink:show=new text-decoration="underline">skos__prefLabel</a>: for locational purposes', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: |'], '#878800');
184-
var locref2 = instance('locref2:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://www.w3.org/2004/02/skos/core#prefLabel" xlink:show=new text-decoration="underline">skos__prefLabel</a>: for more locational purposes', '<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: |'], '#878800');
182+
var institution = instance('institution:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: |', '<a xlink:href="http://www.w3.org/2004/02/skos/core#prefLabel" xlink:show=new text-decoration="underline">skos__prefLabel</a>: a quality establishment'], '#878800');
183+
var locref = instance('locref:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: |', '<a xlink:href="http://www.w3.org/2004/02/skos/core#prefLabel" xlink:show=new text-decoration="underline">skos__prefLabel</a>: for locational purposes'], '#878800');
184+
var locref2 = instance('locref2:<a xlink:href="http://binary-array-ld.net/latest/Array" xlink:show=new text-decoration="underline">bald__Array</a>', ['<a xlink:href="http://binary-array-ld.net/latest/shape" xlink:show=new text-decoration="underline">bald__shape</a>: |', '<a xlink:href="http://www.w3.org/2004/02/skos/core#prefLabel" xlink:show=new text-decoration="underline">skos__prefLabel</a>: for more locational purposes'], '#878800');
185185
var referencing = instance('referencing:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');
186186
var root = instance('root:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');
187187
var source = instance('source:<a xlink:href="http://binary-array-ld.net/latest/Container" xlink:show=new text-decoration="underline">bald__Container</a>', ['<a xlink:href="http://binary-array-ld.net/latest/contains" xlink:show=new text-decoration="underline">bald__contains</a>: |'], '#878800');

0 commit comments

Comments
 (0)