1
1
2
+ import numpy as np
2
3
import requests
3
4
4
5
import bald
@@ -14,7 +15,30 @@ def check_uri(uri):
14
15
result = True
15
16
return result
16
17
18
+ def valid_array_reference (parray , carray ):
19
+ """
20
+ Returns boolean.
21
+ Validates bald array broadcasting rules between a parent array and a child array.
17
22
23
+ Args:
24
+ * parray - a numpy array: the parent of a bald array reference relation
25
+
26
+ * carray - a numpy array: the child of a bald array reference relation
27
+ """
28
+ # https://github.com/SciTools/courses/blob/master/course_content/notebooks/numpy_intro.ipynb
29
+ # https://cs231n.github.io/python-numpy-tutorial/#numpy-broadcasting
30
+ # http://docs.scipy.org/doc/numpy/user/basics.broadcasting.html
31
+ # http://scipy.github.io/old-wiki/pages/EricsBroadcastingDoc
32
+
33
+ result = True
34
+ # try numpy broadcast
35
+ try :
36
+ _ = np .broadcast (parray , carray )
37
+ except ValueError :
38
+ result = False
39
+
40
+ return result
41
+
18
42
19
43
class Validation (object ):
20
44
@@ -25,9 +49,10 @@ def exceptions(self):
25
49
exceptions = []
26
50
return exceptions
27
51
28
- class ContainerValidation (Validation ):
29
- def __init__ (self , container ):
30
- self .container = container
52
+ class SubjectValidation (Validation ):
53
+
54
+ def __init__ (self , subject ):
55
+ self .subject = subject
31
56
32
57
def is_valid (self ):
33
58
return not self .exceptions ()
@@ -40,23 +65,79 @@ def exceptions(self):
40
65
def check_attr_uris (self , exceptions ):
41
66
def _check_uri (uri , exceptions ):
42
67
if not check_uri (uri ):
68
+ check_uri (uri )
43
69
msg = '{} is not resolving as a resource (404).'
44
70
msg = msg .format (uri )
45
71
exceptions .append (ValueError (msg ))
46
72
return exceptions
47
73
48
- for pref , uri in self .container .prefixes ().iteritems ():
49
- exceptions = _check_uri (self .container .unpack_uri (uri ),
50
- exceptions )
51
- for attr , value in self .container .attrs .iteritems ():
52
- exceptions = _check_uri (self .container .unpack_uri (attr ),
74
+ for pref , uri in self .subject .prefixes ().iteritems ():
75
+ exceptions = _check_uri (self .subject .unpack_uri (uri ),
53
76
exceptions )
54
- exceptions = _check_uri (self .container .unpack_uri (value ),
77
+ for attr , value in self .subject .attrs .iteritems ():
78
+ exceptions = _check_uri (self .subject .unpack_uri (attr ),
55
79
exceptions )
56
-
80
+ if isinstance (value , str ):
81
+ exceptions = _check_uri (self .subject .unpack_uri (value ),
82
+ exceptions )
57
83
return exceptions
58
84
59
-
85
+
86
+ class ContainerValidation (SubjectValidation ):
87
+
88
+ def __init__ (self , container ):
89
+ self .container = container
90
+ self .subject = container
91
+
92
+ def exceptions (self ):
93
+ exceptions = []
94
+ exceptions = self .check_attr_uris (exceptions )
95
+ return exceptions
96
+
97
+ class DatasetValidation (SubjectValidation ):
98
+
99
+
100
+ def __init__ (self , name , dataset , subject , fhandle ):
101
+ self .dataset = dataset
102
+ self .subject = subject
103
+ self .name = name
104
+ self .fhandle = fhandle
105
+
106
+ def exceptions (self ):
107
+ exceptions = []
108
+ exceptions = self .check_attr_uris (exceptions )
109
+ exceptions = self .check_array_references (exceptions )
110
+ return exceptions
111
+
112
+ def check_array_references (self , exceptions ):
113
+ def _check_ref (pdataset , parray , cdataset , carray ):
114
+ if not valid_array_reference (parray , carray ):
115
+ msg = ('{} declares a child of {} but the arrays'
116
+ 'do not conform to the bald array reference'
117
+ 'rules' )
118
+ msg = msg .format (pdataset , cdataset )
119
+ exceptions .append (ValueError (msg ))
120
+ return exceptions
121
+
122
+ # for dataset in ?????
123
+ # for parent child relation in dataset
124
+ # check the broadcasting relationship
125
+ # parray = np.array((1,2))
126
+ # carray = np.array((3,4))
127
+
128
+ for attr , value in self .subject .attrs .iteritems ():
129
+ # should support subtypes
130
+ #import pdb; pdb.set_trace()
131
+ if attr == 'bald_._reference' :
132
+ # check if it's this type, otherwise exception)
133
+ # if isinstance(value,
134
+ child_dset = self .fhandle [value ]
135
+ parray = np .zeros (self .dataset .shape )
136
+ carray = np .zeros (child_dset .shape )
137
+
138
+ exceptions = _check_ref ('p' , parray , 'c' , carray )
139
+ return exceptions
140
+
60
141
61
142
class ValidationSet (Validation ):
62
143
pass
0 commit comments