def unify_simple(a, b): """Unify two blaze types""" if isinstance(a, _strtypes): a = dshape(a) if isinstance(b, _strtypes): b = dshape(b) [res], _ = unify([(a, b)], [True]) return res
def test_parsing2(self): t = dshape('Int[32]') self.assertEqual(len(t.parameters), 1) self.assertIsInstance(t.parameters[0], T.Fixed) t = dshape('Int[Float[32] -> Complex[64]]') self.assertEqual(len(t.parameters), 1) self.assertIsInstance(t.parameters[0], T.Function)
def kernel(signature, impl='python', **metadata): """ Define an blaze python-level kernel. Further implementations may be associated with this overloaded kernel using the 'implement' method. Parameters ---------- signature: string or Type Optional function signature Usage ----- @kernel def add(a, b): return a + b or @kernel('a -> a -> a') # All types are unified def add(a, b): return a + b """ def decorator(f): func = lookup_previous(f) if isinstance(func, BlazeFunc): func = func.dispatcher elif isinstance(func, types.FunctionType): raise TypeError( "Function %s in current scope is not overloadable" % (func,)) else: func = Dispatcher() dispatcher = overload(signature, func=func)(f) if isinstance(f, types.FunctionType): kernel = BlazeFunc(dispatcher) else: assert isinstance(f, BlazeFunc), f kernel = f metadata.setdefault('elementwise', True) kernel.add_metadata(metadata) if impl != 'python': kernel.implement(f, signature, impl, f) return kernel signature = dshape(signature) if not isinstance(signature, T.Mono): # @kernel # def f(...): ... f = signature signature = None return decorator(f) else: # @kernel('a -> a -> b') # def f(...): ... return decorator
def test_var_dim(self): a = nd.array([[1,2,3], [4,5], [6]]) dd = DyNDDataDescriptor(a) self.assertEqual(dd.dshape, datashape.dshape('3, var, int32')) self.assertEqual(dd_as_py(dd), [[1,2,3], [4,5], [6]]) self.assertEqual(dd_as_py(dd[0]), [1,2,3]) self.assertEqual(dd_as_py(dd[1]), [4,5]) self.assertEqual(dd_as_py(dd[2]), [6])
def test_parsing(self): t = dshape('Int[X]') cls = type(t) self.assertIsInstance(cls, T.TypeConstructor) self.assertEqual(str(cls(32)), 'Int[32]') self.assertIsInstance(cls(32), cls) flags0 = t.flags[0] self.assertEqual(flags0, {'coercible': False})
def test_descriptor_getitem_types(self): a = nd.array([[1, 2, 3], [4, 5, 6]]) dd = DyNDDataDescriptor(a) self.assertEqual(dd.dshape, datashape.dshape('2, 3, int32')) # Indexing should produce DyNDDataDescriptor instances self.assertTrue(isinstance(dd[0], DyNDDataDescriptor)) self.assertEqual(dd_as_py(dd[0]), [1,2,3]) self.assertTrue(isinstance(dd[1,2], DyNDDataDescriptor)) self.assertEqual(dd_as_py(dd[1,2]), 6)
def decorator(f, signature=signature): if signature is None: signature = dummy_signature(f) else: signature = dshape(signature) dispatcher = func or f.__globals__.get(f.__name__) dispatcher = dispatcher or Dispatcher() dispatcher.add_overload(f, signature, kwds) return dispatcher
def test_element_iter_write_buffered(self): a = nd.array([1, 2, 3, 4, 5], access='rw').ucast(ndt.int64) dd = DyNDDataDescriptor(a) self.assertEqual(dd.dshape, datashape.dshape('5, int64')) with dd.element_write_iter() as ge: self.assertTrue(isinstance(ge, IElementWriteIter)) for val, ptr in izip([5,7,4,5,3], ge): x = ctypes.c_int64(val) ctypes.memmove(ptr, ctypes.addressof(x), 8) self.assertEqual(dd_as_py(dd), [5,7,4,5,3])
def test_element_iter_write(self): a = np.array([1, 2, 3, 4, 5], dtype=np.int32) dd = NumPyDataDescriptor(a) self.assertEqual(dd.dshape, datashape.dshape('5, int32')) with dd.element_write_iter() as ge: self.assertTrue(isinstance(ge, IElementWriteIter)) for val, ptr in izip([5,7,4,5,3], ge): x = ctypes.c_int32(val) ctypes.memmove(ptr, ctypes.addressof(x), 4) self.assertEqual(dd_as_py(dd), [5,7,4,5,3])
def test_record(self): class ctds(ctypes.Structure): _fields_ = [('a', ctypes.c_int8), ('b', ctypes.c_double), ('c', ctypes.c_uint8), ('d', ctypes.c_uint16)] ds = datashape.dshape('{a: int8; b: float64; c: uint8; d: float16}') self.assertEqual(ds.c_itemsize, ctypes.sizeof(ctds)) self.assertEqual(ds.c_alignment, ctypes.alignment(ctds)) self.assertEqual(ds.c_offsets, (ctds.a.offset, ctds.b.offset, ctds.c.offset, ctds.d.offset))
def test_descriptor_iter_types(self): a = nd.array([[1, 2, 3], [4, 5, 6]]) dd = DyNDDataDescriptor(a) self.assertEqual(dd.dshape, datashape.dshape('2, 3, int32')) # Iteration should produce DyNDDataDescriptor instances vals = [] for el in dd: self.assertTrue(isinstance(el, DyNDDataDescriptor)) self.assertTrue(isinstance(el, IDataDescriptor)) vals.append(dd_as_py(el)) self.assertEqual(vals, [[1, 2, 3], [4, 5, 6]])
def test_element_iter_types(self): a = nd.array([[1, 2, 3], [4, 5, 6]]) dd = DyNDDataDescriptor(a) self.assertEqual(dd.dshape, datashape.dshape('2, 3, int32')) # Requesting element iteration should produce an # IElementReadIter object ei = dd.element_read_iter() self.assertTrue(isinstance(ei, IElementReadIter)) # Iteration over the IElementReadIter object should produce # raw ints which are pointers for ptr in ei: self.assertTrue(isinstance(ptr, _inttypes))
def test_array_subarray(self): self.assertEqual(datashape.dshape('3, int32').subarray(0), datashape.dshape('3, int32')) self.assertEqual(datashape.dshape('3, int32').subarray(1), datashape.int32) self.assertEqual(str(datashape.dshape('3, var, M, int32').subarray(2)), str(datashape.dshape('M, int32'))) self.assertEqual(str(datashape.dshape('3, var, M, float64').subarray(3)), str(datashape.float64))
def test_element_write(self): a = nd.array([1, 2, 3, 4, 5], access='rw') dd = DyNDDataDescriptor(a) self.assertEqual(dd.dshape, datashape.dshape('5, int32')) ge = dd.element_writer(1) self.assertTrue(isinstance(ge, IElementWriter)) x = ctypes.c_int32(123) ge.write_single((1,), ctypes.addressof(x)) self.assertEqual(dd_as_py(dd), [1,123,3,4,5]) with ge.buffered_ptr((3,)) as dst_ptr: x = ctypes.c_int32(456) ctypes.memmove(dst_ptr, ctypes.addressof(x), 4) self.assertEqual(dd_as_py(dd), [1,123,3,456,5])
def test_element_write_buffered(self): a = np.array([1, 2, 3, 4, 5], dtype=np.dtype(np.int32).newbyteorder()) dd = NumPyDataDescriptor(a) self.assertEqual(dd.dshape, datashape.dshape('5, int32')) self.assertFalse(dd.npyarr.dtype.isnative) ge = dd.element_writer(1) self.assertTrue(isinstance(ge, IElementWriter)) x = ctypes.c_int32(123) ge.write_single((1,), ctypes.addressof(x)) self.assertEqual(dd_as_py(dd), [1,123,3,4,5]) with ge.buffered_ptr((3,)) as dst_ptr: x = ctypes.c_int32(456) ctypes.memmove(dst_ptr, ctypes.addressof(x), 4) self.assertEqual(dd_as_py(dd), [1,123,3,456,5])
def test_element_getitem_types(self): a = nd.array([[1, 2, 3], [4, 5, 6]]) dd = DyNDDataDescriptor(a) self.assertEqual(dd.dshape, datashape.dshape('2, 3, int32')) # Requesting element_reader with one index should produce an # IElementReader object ge = dd.element_reader(1) self.assertTrue(isinstance(ge, IElementReader)) # Iteration over the IElementReadIter object should produce # raw ints which are pointers self.assertTrue(isinstance(ge.read_single((1,)), _inttypes)) # Requesting element reader with two indices should produce an # IElementReader object ge = dd.element_reader(2) self.assertTrue(isinstance(ge, IElementReader)) # Iteration over the IElementReadIter object should produce # raw ints which are pointers self.assertTrue(isinstance(ge.read_single((1,2)), _inttypes))
def test_array(self): ds = datashape.dshape('3, 5, 2, int32') self.assertEqual(ds.c_itemsize, 3 * 5 * 2 * 4) self.assertEqual(ds.c_alignment, datashape.int32.c_alignment) self.assertEqual(ds.c_strides, (40, 8, 4))
def test_record_nolayout(self): ds = datashape.dshape('{a: int8; b: M, float32}') self.assertRaises(AttributeError, lambda x : x.c_itemsize, ds) self.assertRaises(AttributeError, lambda x : x.c_alignment, ds) self.assertRaises(AttributeError, lambda x : x.c_offsets, ds)
def parse(s): if s[0].isupper() and re.match('\w+$', s): # HACK return TypeConstructor(s, 0, []) return dshape(s)
def test_array_nolayout(self): # If the datashape has no layout, it should raise errors ds = datashape.dshape('3, 5, M, int32') self.assertRaises(AttributeError, lambda x : x.c_itemsize, ds) self.assertRaises(AttributeError, lambda x : x.c_alignment, ds) self.assertRaises(AttributeError, lambda x : x.c_strides, ds)
def test_dshape_compare(self): self.assertNotEqual(datashape.int32, datashape.dshape('1, int32'))