# http://pyrocko.org - GPLv3 # # The Pyrocko Developers, 21st Century # ---|P------/S----------~Lg----------
num.dtype('float64'): '<f8', num.dtype('float32'): '<f4', num.dtype('int64'): '<i8', num.dtype('int32'): '<i4', num.dtype('int16'): '<i2', num.dtype('int8'): '<i1'}
(v, k) for (k, v) in restricted_dtype_map.items())
return a.dtype == b.dtype \ and a.shape == b.shape \ and num.all(a == b)
self, shape=None, dtype=None, serialize_as='table', serialize_dtype=None, *args, **kwargs):
'table', 'base64', 'list', 'npy', 'base64+meta', 'base64-compat')
elif val is None: return False else: return array_equal(self._default_cmp, val)
BytesIO(val.encode('utf-8')), dtype=self.dtype, ndmin=ndim)
data, dtype=self.serialize_dtype).astype(self.dtype)
data, dtype=self.serialize_dtype).astype(self.dtype) except binascii.Error: val = num.loadtxt( BytesIO(val.encode('utf-8')), dtype=self.dtype, ndmin=ndim)
elif self.serialize_as == 'npy': data = b64decode(val) try: val = num.load(BytesIO(data), allow_pickle=False) except TypeError: # allow_pickle only available in newer NumPy val = num.load(BytesIO(data))
if self.serialize_as == 'base64+meta': if not sorted(val.keys()) == ['data', 'dtype', 'shape']: raise ValidationError( 'array in format "base64+meta" must have keys ' '"data", "dtype", and "shape"')
shape = val['shape'] if not isinstance(shape, list): raise ValidationError('invalid shape definition')
for n in shape: if not isinstance(n, int): raise ValidationError('invalid shape definition')
serialize_dtype = val['dtype'] allowed = list(restricted_dtype_map_rev.keys()) if self.serialize_dtype is not None: allowed.append(self.serialize_dtype)
if serialize_dtype not in allowed: raise ValidationError( 'only the following dtypes are allowed: %s' % ', '.join(sorted(allowed)))
data = val['data'] if not isinstance(data, (str, newstr)): raise ValidationError( 'data must be given as a base64 encoded string')
data = b64decode(data)
dtype = self.dtype or \ restricted_dtype_map_rev[serialize_dtype]
val = num.fromstring( data, dtype=serialize_dtype).astype(dtype)
if val.size != num.product(shape): raise ValidationError('size/shape mismatch')
val = val.reshape(shape)
else:
raise ValidationError( 'object is not of type numpy.ndarray: %s' % type(val)) raise ValidationError( 'array not of required type: need %s, got %s' % ( self.dtype, val.dtype))
raise ValidationError( 'array dimension mismatch: need %i, got %i' % ( la, lb))
if a != b: raise ValidationError( 'array shape mismatch: need %s, got: %s' % ( self.shape, val.shape))
or self.serialize_as == 'base64-compat': elif self.serialize_as == 'list': if self.dtype == num.complex: return [repr(x) for x in val] else: return val.tolist() elif self.serialize_as == 'npy': out = BytesIO() try: num.save(out, val, allow_pickle=False) except TypeError: # allow_pickle only available in newer NumPy num.save(out, val)
return literal(b64encode(out.getvalue()).decode('utf-8'))
elif self.serialize_as == 'base64+meta': serialize_dtype = self.serialize_dtype or \ restricted_dtype_map[val.dtype]
data = val.astype(serialize_dtype).tostring()
return dict( dtype=serialize_dtype, shape=val.shape, data=literal(b64encode(data).decode('utf-8')))
|