def test_max(self): # Sanity check of reduction op self.assertEqual(ddesc_as_py(blaze.eval(blaze.max(5)).ddesc), 5) self.assertRaises(ValueError, blaze.eval, blaze.max([])) self.assertEqual(ddesc_as_py(blaze.eval(blaze.max([3, -2])).ddesc), 3) self.assertEqual(ddesc_as_py(blaze.eval(blaze.max([1.5, 2.0])).ddesc), 2.0)
def test_descriptor_setitem(self): dd = HDF5_DDesc(self.hdf5_file, '/g/a2', mode='a') self.assertEqual(dd.dshape, datashape.dshape('2 * 3 * int64')) dd[1, 2] = 10 self.assertEqual(ddesc_as_py(dd[1, 2]), 10) dd[1] = [10, 11, 12] self.assertEqual(ddesc_as_py(dd[1]), [10, 11, 12])
def test_bitwise_xor_bool(self): t = blaze.array(True) f = blaze.array(False) self.assertEqual(ddesc_as_py((t ^ t).ddesc), False) self.assertEqual(ddesc_as_py((t ^ f).ddesc), True) self.assertEqual(ddesc_as_py((f ^ t).ddesc), True) self.assertEqual(ddesc_as_py((f ^ f).ddesc), False)
def test_bitwise_and_bool(self): t = blaze.array(True) f = blaze.array(False) self.assertEqual(ddesc_as_py((t & t).ddesc), True) self.assertEqual(ddesc_as_py((t & f).ddesc), False) self.assertEqual(ddesc_as_py((f & t).ddesc), False) self.assertEqual(ddesc_as_py((f & f).ddesc), False)
def test_descriptor_setitem(self): dd = HDF5_DDesc(self.hdf5_file, '/g/a2', mode='a') self.assertEqual(dd.dshape, datashape.dshape('2 * 3 * int64')) dd[1,2] = 10 self.assertEqual(ddesc_as_py(dd[1,2]), 10) dd[1] = [10, 11, 12] self.assertEqual(ddesc_as_py(dd[1]), [10, 11, 12])
def test_product(self): # Sanity check of reduction op self.assertEqual(ddesc_as_py(blaze.eval(blaze.product(5)).ddesc), 5) self.assertEqual(ddesc_as_py(blaze.eval(blaze.product([])).ddesc), 1) self.assertEqual(ddesc_as_py(blaze.eval(blaze.product([3, -2])).ddesc), -6) self.assertEqual( ddesc_as_py(blaze.eval(blaze.product([1.5, 2.0])).ddesc), 3.0)
def test_content(self): dd = CSV_DDesc(self.csv_file, dialect='excel', schema=self.schema, delimiter=' ') print(ddesc_as_py(dd)) s = str(ddesc_as_py(dd)) assert 'Alice' in s and 'Bob' in s
def test_product(self): # Sanity check of reduction op self.assertEqual(ddesc_as_py(blaze.eval(blaze.product(5)).ddesc), 5) self.assertEqual(ddesc_as_py(blaze.eval(blaze.product([])).ddesc), 1) self.assertEqual(ddesc_as_py(blaze.eval(blaze.product([3, -2])).ddesc), -6) self.assertEqual(ddesc_as_py(blaze.eval(blaze.product([1.5, 2.0])).ddesc), 3.0)
def test_ragged(self): a = blaze.array([[1], [2, 3], [4, 5, 6]]) b = blaze.array([[1, 2, 3], [4, 5], [6]]) c = blaze.eval(a + b) self.assertEqual(ddesc_as_py(c.ddesc), [[2, 3, 4], [6, 8], [10, 11, 12]]) c = blaze.eval(2 * a - b) self.assertEqual(ddesc_as_py(c.ddesc), [[1, 0, -1], [0, 1], [2, 4, 6]])
def test_add(self): types = ['int8', 'int16', 'int32', 'int64'] for type_ in types: a = blaze.array(range(3), dshape=type_) c = blaze.eval(a+a) self.assertEqual(ddesc_as_py(c.ddesc), [0, 2, 4]) c = blaze.eval(((a+a)*a)) self.assertEqual(ddesc_as_py(c.ddesc), [0, 2, 8])
def test_descriptor_getitem_types(self): dd = HDF5_DDesc(self.hdf5_file, '/g/a2') self.assertEqual(dd.dshape, datashape.dshape('2 * 3 * int64')) # Indexing should produce DyND_DDesc instances self.assertTrue(isinstance(dd[0], DyND_DDesc)) self.assertEqual(ddesc_as_py(dd[0]), [1, 2, 3]) self.assertTrue(isinstance(dd[1, 2], DyND_DDesc)) self.assertEqual(ddesc_as_py(dd[1, 2]), 1)
def test_descriptor_getitem_types(self): dd = HDF5_DDesc(self.hdf5_file, '/g/a2') self.assertEqual(dd.dshape, datashape.dshape('2 * 3 * int64')) # Indexing should produce DyND_DDesc instances self.assertTrue(isinstance(dd[0], DyND_DDesc)) self.assertEqual(ddesc_as_py(dd[0]), [1,2,3]) self.assertTrue(isinstance(dd[1,2], DyND_DDesc)) self.assertEqual(ddesc_as_py(dd[1,2]), 1)
def test_descriptor_setitem(self): if not netCDF4_is_here: return dd = netCDF4_DDesc(self.nc4_file, '/g/a2', mode='a') self.assertEqual(dd.dshape, datashape.dshape('2 * 3 * int64')) dd[1,2] = 10 self.assertEqual(ddesc_as_py(dd[1,2]), 10) dd[1] = [10, 11, 12] self.assertEqual(ddesc_as_py(dd[1]), [10, 11, 12])
def test_add(self): types = ['int8', 'int16', 'int32', 'int64'] for type_ in types: a = blaze.array(range(3), dshape=type_) c = blaze.eval(a + a) self.assertEqual(ddesc_as_py(c.ddesc), [0, 2, 4]) c = blaze.eval(((a + a) * a)) self.assertEqual(ddesc_as_py(c.ddesc), [0, 2, 8])
def test_any(self): # Sanity check of reduction op self.assertEqual(ddesc_as_py(blaze.eval(blaze.any(True)).ddesc), True) self.assertEqual(ddesc_as_py(blaze.eval(blaze.any(False)).ddesc), False) self.assertEqual(ddesc_as_py(blaze.eval(blaze.any(blaze.array([], dshape='0 * bool'))).ddesc), False) self.assertEqual(ddesc_as_py(blaze.eval(blaze.any([False, True])).ddesc), True) self.assertEqual(ddesc_as_py(blaze.eval(blaze.any([False, False])).ddesc), False)
def test_add_with_pyobj(self): a = blaze.array(3) + 3 self.assertEqual(ddesc_as_py(a.ddesc), 6) a = 3 + blaze.array(4) self.assertEqual(ddesc_as_py(a.ddesc), 7) a = blaze.array([1, 2]) + 4 self.assertEqual(ddesc_as_py(a.ddesc), [5, 6]) a = [1, 2] + blaze.array(5) self.assertEqual(ddesc_as_py(a.ddesc), [6, 7])
def test_var_dim(self): a = nd.array([[1, 2, 3], [4, 5], [6]]) dd = DyND_DDesc(a) self.assertEqual(dd.dshape, datashape.dshape('3 * var * int32')) self.assertEqual(ddesc_as_py(dd), [[1, 2, 3], [4, 5], [6]]) self.assertEqual(ddesc_as_py(dd[0]), [1, 2, 3]) self.assertEqual(ddesc_as_py(dd[1]), [4, 5]) self.assertEqual(ddesc_as_py(dd[2]), [6])
def test_descriptor_getitem_types(self): if not netCDF4_is_here: return dd = netCDF4_DDesc(self.nc4_file, '/g/a2') self.assertEqual(dd.dshape, datashape.dshape('2 * 3 * int64')) # Indexing should produce DyND_DDesc instances self.assertTrue(isinstance(dd[0], DyND_DDesc)) self.assertEqual(ddesc_as_py(dd[0]), [1,2,3]) self.assertTrue(isinstance(dd[1,2], DyND_DDesc)) self.assertEqual(ddesc_as_py(dd[1,2]), 1)
def test_descriptor_getitem_types(self): a = nd.array([[1, 2, 3], [4, 5, 6]]) dd = DyND_DDesc(a) self.assertEqual(dd.dshape, datashape.dshape('2 * 3 * int32')) # Indexing should produce DyND_DDesc instances self.assertTrue(isinstance(dd[0], DyND_DDesc)) self.assertEqual(ddesc_as_py(dd[0]), [1,2,3]) self.assertTrue(isinstance(dd[1,2], DyND_DDesc)) self.assertEqual(ddesc_as_py(dd[1,2]), 6)
def test_descriptor_getitem_types(self): a = nd.array([[1, 2, 3], [4, 5, 6]]) dd = DyND_DDesc(a) self.assertEqual(dd.dshape, datashape.dshape('2 * 3 * int32')) # Indexing should produce DyND_DDesc instances self.assertTrue(isinstance(dd[0], DyND_DDesc)) self.assertEqual(ddesc_as_py(dd[0]), [1, 2, 3]) self.assertTrue(isinstance(dd[1, 2], DyND_DDesc)) self.assertEqual(ddesc_as_py(dd[1, 2]), 6)
def test_create_record(self): # A simple record array a = blaze.array([(10, 3.5), (15, 2.25)], dshape="var * {val: int32, flt: float32}") self.assertEqual(ddesc_as_py(a.ddesc), [{'val': 10, 'flt': 3.5}, {'val': 15, 'flt': 2.25}]) # Test field access via attributes aval = a.val self.assertEqual(ddesc_as_py(aval.ddesc), [10, 15]) aflt = a.flt self.assertEqual(ddesc_as_py(aflt.ddesc), [3.5, 2.25])
def test_scalar(self): a = ctypes.c_int(3) dd = data_descriptor_from_ctypes(a, writable=True) self.assertEqual(dd.dshape, dshape('int32')) self.assertEqual(ddesc_as_py(dd), 3) self.assertTrue(isinstance(ddesc_as_py(dd), int)) a = ctypes.c_float(3.25) dd = data_descriptor_from_ctypes(a, writable=True) self.assertEqual(dd.dshape, dshape('float32')) self.assertEqual(ddesc_as_py(dd), 3.25) self.assertTrue(isinstance(ddesc_as_py(dd), float))
def test_1d_array(self): # An array where the size is in the type a = ffi.new('short[32]', [2 * i for i in range(32)]) dd = data_descriptor_from_cffi(ffi, a, writable=True) self.assertEqual(dd.dshape, dshape('32 * int16')) self.assertEqual(ddesc_as_py(dd), [2 * i for i in range(32)]) # An array where the size is not in the type a = ffi.new('double[]', [1.5 * i for i in range(32)]) dd = data_descriptor_from_cffi(ffi, a, writable=True) self.assertEqual(dd.dshape, dshape('32 * float64')) self.assertEqual(ddesc_as_py(dd), [1.5 * i for i in range(32)])
def test_scalar(self): a = ffi.new('int *', 3) dd = data_descriptor_from_cffi(ffi, a, writable=True) self.assertEqual(dd.dshape, dshape('int32')) self.assertEqual(ddesc_as_py(dd), 3) self.assertTrue(isinstance(ddesc_as_py(dd), int)) a = ffi.new('float *', 3.25) dd = data_descriptor_from_cffi(ffi, a, writable=True) self.assertEqual(dd.dshape, dshape('float32')) self.assertEqual(ddesc_as_py(dd), 3.25) self.assertTrue(isinstance(ddesc_as_py(dd), float))
def test_sum_zerosize(self): # Empty sum operations should produce 0, the reduction identity self.assertEqual(ddesc_as_py(blaze.eval(blaze.sum([])).ddesc), 0) self.assertEqual(ddesc_as_py(blaze.eval(blaze.sum([], keepdims=True)).ddesc), [0]) self.assertEqual(ddesc_as_py(blaze.eval(blaze.sum([[], []])).ddesc), 0) self.assertEqual(ddesc_as_py(blaze.eval(blaze.sum([[], []], keepdims=True)).ddesc), [[0]]) self.assertEqual(ddesc_as_py(blaze.eval(blaze.sum([[], []], axis=-1)).ddesc), [0, 0]) self.assertEqual(ddesc_as_py(blaze.eval(blaze.sum([[], []], axis=-1, keepdims=True)).ddesc), [[0], [0]]) # If we're only reducing on a non-empty dimension, we might still # end up with zero-sized outputs self.assertEqual(ddesc_as_py(blaze.eval(blaze.sum([[], []], axis=0)).ddesc), []) self.assertEqual(ddesc_as_py(blaze.eval(blaze.sum([[], []], axis=0, keepdims=True)).ddesc), [[]])
def test_any(self): # Sanity check of reduction op self.assertEqual(ddesc_as_py(blaze.eval(blaze.any(True)).ddesc), True) self.assertEqual(ddesc_as_py(blaze.eval(blaze.any(False)).ddesc), False) self.assertEqual( ddesc_as_py( blaze.eval(blaze.any(blaze.array([], dshape='0 * bool'))).ddesc), False) self.assertEqual( ddesc_as_py(blaze.eval(blaze.any([False, True])).ddesc), True) self.assertEqual( ddesc_as_py(blaze.eval(blaze.any([False, False])).ddesc), False)
def test_1d_array(self): a = (ctypes.c_short * 32)() for i in range(32): a[i] = 2 * i dd = data_descriptor_from_ctypes(a, writable=True) self.assertEqual(dd.dshape, dshape('32 * int16')) self.assertEqual(ddesc_as_py(dd), [2 * i for i in range(32)]) a = (ctypes.c_double * 32)() for i in range(32): a[i] = 1.5 * i dd = data_descriptor_from_ctypes(a, writable=True) self.assertEqual(dd.dshape, dshape('32 * float64')) self.assertEqual(ddesc_as_py(dd), [1.5 * i for i in range(32)])
def test_2d_array(self): # An array where the leading array size is in the type vals = [[2**i + j for i in range(35)] for j in range(32)] a = ffi.new('long long[32][35]', vals) dd = data_descriptor_from_cffi(ffi, a, writable=True) self.assertEqual(dd.dshape, dshape('32 * 35 * int64')) self.assertEqual(ddesc_as_py(dd), vals) # An array where the leading array size is not in the type vals = [[a + b * 2 for a in range(35)] for b in range(32)] a = ffi.new('unsigned char[][35]', vals) dd = data_descriptor_from_cffi(ffi, a, writable=True) self.assertEqual(dd.dshape, dshape('32 * 35 * uint8')) self.assertEqual(ddesc_as_py(dd), vals)
def test_1d_array(self): a = (ctypes.c_short * 32)() for i in range(32): a[i] = 2*i dd = data_descriptor_from_ctypes(a, writable=True) self.assertEqual(dd.dshape, dshape('32 * int16')) self.assertEqual(ddesc_as_py(dd), [2*i for i in range(32)]) a = (ctypes.c_double * 32)() for i in range(32): a[i] = 1.5*i dd = data_descriptor_from_ctypes(a, writable=True) self.assertEqual(dd.dshape, dshape('32 * float64')) self.assertEqual(ddesc_as_py(dd), [1.5*i for i in range(32)])
def test_uints(self): types = ['uint8', 'uint16', 'uint32', 'uint64'] for type_ in types: a = blaze.array(np.arange(3), dshape=type_) dtype = to_numpy_dtype(a.dshape) self.assertEqual(dtype, np.dtype(type_)) self.assertEqual(ddesc_as_py(a.ddesc), [0, 1, 2])
def test_getitem_start_step(self): dd = CSV_DDesc(self.csv_file, schema=self.schema) el = dd[1::2] self.assertTrue(isinstance(el, DyND_DDesc)) vals = ddesc_as_py(el) self.assertEqual(vals, [ {u'f0': u'k2', u'f1': u'v2', u'f2': 2, u'f3': True}])
def test_getitem_stop(self): dd = CSV_DDesc(self.csv_file, schema=self.schema) el = dd[:1] self.assertTrue(isinstance(el, DyND_DDesc)) vals = ddesc_as_py(el) self.assertEqual(vals, [ {u'f0': u'k1', u'f1': u'v1', u'f2': 1, u'f3': False}])
def test_append2(self): ddesc = BLZ_DDesc(path=self.rootdir, mode='w') a = blaze.empty('0 * 2 * float64', ddesc=ddesc) self.assertTrue(isinstance(a, blaze.Array)) lvals = [[i,i*2] for i in range(10)] append(a, lvals) self.assertEqual(ddesc_as_py(a.ddesc), lvals)
def test_basic_object_type(self): self.assertTrue(issubclass(DyND_DDesc, DDesc)) a = nd.array([[1, 2, 3], [4, 5, 6]]) dd = DyND_DDesc(a) # Make sure the right type is returned self.assertTrue(isinstance(dd, DDesc)) self.assertEqual(ddesc_as_py(dd), [[1, 2, 3], [4, 5, 6]])
def function(self): A = getattr(self, storage1 + 'A') B = getattr(self, storage2 + 'B') Rd = kernel(A, B) self.assert_(isinstance(Rd, blaze.Array)) self.assert_(Rd.ddesc.capabilities.deferred) p = _ddesc(storage3 + 'Rd') if storage3 == 'dsk' else None try: Rc = blaze.eval(Rd, ddesc=p) self.assert_(isinstance(Rc, blaze.Array)) npy_data = getattr(self, 'npy' + R) assert_allclose(np.array(ddesc_as_py(Rc.ddesc)), npy_data) if storage3 == 'dsk': self.assert_(Rc.ddesc.capabilities.persistent) else: self.assert_(not Rc.ddesc.capabilities.persistent) finally: try: if p is not None: p.remove() except: pass # show the real error...
def test_iter(self): dd = CSV_DDesc(self.csv_file, schema=self.schema) # Iteration should produce DyND_DDesc instances vals = [] for el in dd: self.assertTrue(isinstance(el, DyND_DDesc)) self.assertTrue(isinstance(el, DDesc)) vals.append(ddesc_as_py(el)) self.assertEqual(vals, [{ u'f0': u'k1', u'f1': u'v1', u'f2': 1, u'f3': False }, { u'f0': u'k2', u'f1': u'v2', u'f2': 2, u'f3': True }, { u'f0': u'k3', u'f1': u'v3', u'f2': 3, u'f3': False }])
def test_append2(self): ddesc = HDF5_DDesc(path=self.file, datapath='/earray', mode='a') a = blaze.empty('0 * 2 * float64', ddesc=ddesc) self.assertTrue(isinstance(a, blaze.Array)) lvals = [[i,i*2] for i in range(10)] append(a, lvals) self.assertEqual(ddesc_as_py(a.ddesc), lvals)
def test_3d_array(self): # Simple 3D array vals = [[[(i + 2 * j + 3 * k) for i in range(10)] for j in range(12)] for k in range(14)] a = ffi.new('unsigned int[14][12][10]', vals) dd = data_descriptor_from_cffi(ffi, a, writable=True) self.assertEqual(dd.dshape, dshape('14 * 12 * 10 * uint32')) self.assertEqual(ddesc_as_py(dd), vals)
def test_complex(self): types = ['complex64', 'complex128'] for type_ in types: a = blaze.array(np.arange(3), dshape=type_) dtype = to_numpy_dtype(a.dshape) self.assertEqual(dtype, np.dtype(type_)) # ddesc_as_py does not support complexes yet.. self.assertEqual(ddesc_as_py(a.ddesc), [0, 1, 2])
def test_iterchunks_start(self): dd = CSV_DDesc(self.csv_file, schema=self.schema) vals = [] for el in dd.iterchunks(blen=2, start=1): vals.extend(ddesc_as_py(el)) self.assertEqual(vals, [ {u'f0': u'k2', u'f1': u'v2', u'f2': 2, u'f3': True}, {u'f0': u'k3', u'f1': u'v3', u'f2': 3, u'f3': False}])
def test_from_dialect(self): ddesc = CSV_DDesc(self.fname, mode='r', schema=csv_schema, dialect='excel') a = blaze.array(ddesc) self.assert_(isinstance(a, blaze.Array)) self.assertEqual(ddesc_as_py(a.ddesc), csv_ldict)
def test_from_has_header(self): ddesc = CSV_DDesc(self.fname, mode='r', schema=csv_schema, has_header=False) a = blaze.array(ddesc) self.assert_(isinstance(a, blaze.Array)) self.assertEqual(ddesc_as_py(a.ddesc), csv_ldict)
def test_2d_array(self): a = (ctypes.c_double * 35 * 32)() vals = [[2**i + j for i in range(35)] for j in range(32)] for i in range(32): for j in range(35): a[i][j] = vals[i][j] dd = data_descriptor_from_ctypes(a, writable=True) self.assertEqual(dd.dshape, dshape('32 * 35 * float64')) self.assertEqual(ddesc_as_py(dd), vals) a = (ctypes.c_uint8 * 35 * 32)() vals = [[i + j*2 for i in range(35)] for j in range(32)] for i in range(32): for j in range(35): a[i][j] = vals[i][j] dd = data_descriptor_from_ctypes(a, writable=True) self.assertEqual(dd.dshape, dshape('32 * 35 * uint8')) self.assertEqual(ddesc_as_py(dd), vals)
def test_iterchunks_start_stop(self): dd = CSV_DDesc(self.csv_file, schema=self.schema) vals = [ddesc_as_py(v) for v in dd.iterchunks(blen=1, start=1, stop=2)] self.assertEqual(vals, [[{ u'f0': u'k2', u'f1': u'v2', u'f2': 2, u'f3': True }]])
def test_basic_object_type(self): self.assertTrue(issubclass(CSV_DDesc, DDesc)) dd = CSV_DDesc(self.csv_file, schema=self.schema) self.assertTrue(isinstance(dd, DDesc)) self.assertTrue(isinstance(dd.dshape.shape[0], datashape.Var)) self.assertEqual(ddesc_as_py(dd), [ {u'f0': u'k1', u'f1': u'v1', u'f2': 1, u'f3': False}, {u'f0': u'k2', u'f1': u'v2', u'f2': 2, u'f3': True}, {u'f0': u'k3', u'f1': u'v3', u'f2': 3, u'f3': False}])
def test_basic_object_type(self): # For reasons that I ignore, the above decorator is not working for # 2.6, so will disable the tests the hard way... if not netCDF4_is_here: return self.assertTrue(issubclass(netCDF4_DDesc, DDesc)) dd = netCDF4_DDesc(self.nc4_file, '/a1') # Make sure the right type is returned self.assertTrue(isinstance(dd, DDesc)) self.assertEqual(ddesc_as_py(dd), [[1, 2, 3], [4, 5, 6]])