def zeros(dshape, ddesc=None): """Create an array and fill it with zeros. Parameters ---------- dshape : datashape The datashape for the resulting array. ddesc : data descriptor instance This comes with the necessary info for storing the data. If None, a DyND_DDesc will be used. Returns ------- out : a concrete blaze array. """ dshape = _normalize_dshape(dshape) if ddesc is None: ddesc = DyND_DDesc(nd.zeros(str(dshape), access='rw')) return Array(ddesc) if isinstance(ddesc, BLZ_DDesc): shape, dt = to_numpy(dshape) ddesc.blzarr = blz.zeros( shape, dt, rootdir=ddesc.path, mode=ddesc.mode, **ddesc.kwargs) elif isinstance(ddesc, HDF5_DDesc): obj = nd.as_numpy(nd.zeros(str(dshape))) with tb.open_file(ddesc.path, mode=ddesc.mode) as f: where, name = split_path(ddesc.datapath) f.create_earray(where, name, filters=ddesc.filters, obj=obj) ddesc.mode = 'a' # change into 'a'ppend mode for further operations return Array(ddesc)
def test_access_zeros(self): a = nd.zeros(ndt.int32) self.assertEqual(a.access_flags, 'readwrite') a = nd.zeros(ndt.int32, access='rw') self.assertEqual(a.access_flags, 'readwrite') a = nd.zeros(ndt.int32, access='r') self.assertEqual(a.access_flags, 'immutable')
def test_squeeze_strided(self): # Simple strided array a = nd.zeros(1, 1, 3, 1, 2, 1, 1, 1, ndt.int32) self.assertEqual(a.shape, (1, 1, 3, 1, 2, 1, 1, 1)) self.assertEqual(nd.squeeze(a).shape, (3, 1, 2)) # Strip dimensions from the start a = nd.zeros(1, 3, ndt.float32) self.assertEqual(a.shape, (1, 3)) self.assertEqual(nd.squeeze(a).shape, (3,)) # Strip dimensions from the end a = nd.zeros(3, 1, ndt.float32) self.assertEqual(a.shape, (3, 1)) self.assertEqual(nd.squeeze(a).shape, (3,))
def test_squeeze_strided(self): # Simple strided array a = nd.zeros(1, 1, 3, 1, 2, 1, 1, 1, ndt.int32) self.assertEqual(a.shape, (1, 1, 3, 1, 2, 1, 1, 1)) self.assertEqual(nd.squeeze(a).shape, (3, 1, 2)) # Strip dimensions from the start a = nd.zeros(1, 3, ndt.float32) self.assertEqual(a.shape, (1, 3)) self.assertEqual(nd.squeeze(a).shape, (3, )) # Strip dimensions from the end a = nd.zeros(3, 1, ndt.float32) self.assertEqual(a.shape, (3, 1)) self.assertEqual(nd.squeeze(a).shape, (3, ))
def test_array_attributes(self): a = nd.zeros(3, 5, ndt.int32) # Property "ndim" self.assertEqual(a.ndim, 2) # Property "shape" self.assertEqual(a.shape, (3, 5)) # Property "strides" self.assertEqual(a.strides, (20, 4)) # Property "dtype" self.assertEqual(a.dtype, ndt.int32)
def test_squeeze_errors(self): a = nd.zeros(1, 3, 1, 2, 1, ndt.int32) # Out of bound axis self.assertRaises(IndexError, nd.squeeze, a, axis=-6) self.assertRaises(IndexError, nd.squeeze, a, axis=5) self.assertRaises(IndexError, nd.squeeze, a, axis=(0, 5)) self.assertRaises(IndexError, nd.squeeze, a, axis=(2, -6)) # Dimension of non-one size self.assertRaises(IndexError, nd.squeeze, a, axis=1) self.assertRaises(IndexError, nd.squeeze, a, axis=(2,3)) # Axis not an integer self.assertRaises(TypeError, nd.squeeze, a, axis=2.0) self.assertRaises(TypeError, nd.squeeze, a, axis=(0, 2.0+0j))
def test_squeeze_errors(self): a = nd.zeros(1, 3, 1, 2, 1, ndt.int32) # Out of bound axis self.assertRaises(IndexError, nd.squeeze, a, axis=-6) self.assertRaises(IndexError, nd.squeeze, a, axis=5) self.assertRaises(IndexError, nd.squeeze, a, axis=(0, 5)) self.assertRaises(IndexError, nd.squeeze, a, axis=(2, -6)) # Dimension of non-one size self.assertRaises(IndexError, nd.squeeze, a, axis=1) self.assertRaises(IndexError, nd.squeeze, a, axis=(2, 3)) # Axis not an integer self.assertRaises(TypeError, nd.squeeze, a, axis=2.0) self.assertRaises(TypeError, nd.squeeze, a, axis=(0, 2.0 + 0j))
def test_squeeze_axis(self): a = nd.zeros(1, 3, 1, 2, 1, ndt.int32) self.assertEqual(a.shape, (1, 3, 1, 2, 1)) self.assertEqual(nd.squeeze(a, axis=0).shape, (3, 1, 2, 1)) self.assertEqual(nd.squeeze(a, axis=2).shape, (1, 3, 2, 1)) self.assertEqual(nd.squeeze(a, axis=4).shape, (1, 3, 1, 2)) self.assertEqual(nd.squeeze(a, axis=-5).shape, (3, 1, 2, 1)) self.assertEqual(nd.squeeze(a, axis=-3).shape, (1, 3, 2, 1)) self.assertEqual(nd.squeeze(a, axis=-1).shape, (1, 3, 1, 2)) self.assertEqual(nd.squeeze(a, axis=(0,2)).shape, (3, 2, 1)) self.assertEqual(nd.squeeze(a, axis=(0,4)).shape, (3, 1, 2)) self.assertEqual(nd.squeeze(a, axis=(0,-1)).shape, (3, 1, 2)) self.assertEqual(nd.squeeze(a, axis=(2,4)).shape, (1, 3, 2)) self.assertEqual(nd.squeeze(a, axis=(0,2,4)).shape, (3, 2))
def test_squeeze_axis(self): a = nd.zeros(1, 3, 1, 2, 1, ndt.int32) self.assertEqual(a.shape, (1, 3, 1, 2, 1)) self.assertEqual(nd.squeeze(a, axis=0).shape, (3, 1, 2, 1)) self.assertEqual(nd.squeeze(a, axis=2).shape, (1, 3, 2, 1)) self.assertEqual(nd.squeeze(a, axis=4).shape, (1, 3, 1, 2)) self.assertEqual(nd.squeeze(a, axis=-5).shape, (3, 1, 2, 1)) self.assertEqual(nd.squeeze(a, axis=-3).shape, (1, 3, 2, 1)) self.assertEqual(nd.squeeze(a, axis=-1).shape, (1, 3, 1, 2)) self.assertEqual(nd.squeeze(a, axis=(0, 2)).shape, (3, 2, 1)) self.assertEqual(nd.squeeze(a, axis=(0, 4)).shape, (3, 1, 2)) self.assertEqual(nd.squeeze(a, axis=(0, -1)).shape, (3, 1, 2)) self.assertEqual(nd.squeeze(a, axis=(2, 4)).shape, (1, 3, 2)) self.assertEqual(nd.squeeze(a, axis=(0, 2, 4)).shape, (3, 2))
def zeros(dshape, ddesc=None): """Create an array and fill it with zeros. Parameters ---------- dshape : datashape The datashape for the resulting array. ddesc : data descriptor instance This comes with the necessary info for storing the data. If None, a DyND_DDesc will be used. Returns ------- out : a concrete blaze array. """ dshape = _normalize_dshape(dshape) if ddesc is None: ddesc = DyND_DDesc(nd.zeros(str(dshape), access='rw')) return Array(ddesc) if isinstance(ddesc, BLZ_DDesc): shape, dt = to_numpy(dshape) ddesc.blzarr = blz.zeros(shape, dt, rootdir=ddesc.path, mode=ddesc.mode, **ddesc.kwargs) elif isinstance(ddesc, HDF5_DDesc): obj = nd.as_numpy(nd.zeros(str(dshape))) with tb.open_file(ddesc.path, mode=ddesc.mode) as f: where, name = split_path(ddesc.datapath) f.create_earray(where, name, filters=ddesc.filters, obj=obj) ddesc.mode = 'a' # change into 'a'ppend mode for further operations return Array(ddesc)
def test_access_zeros(self): a = nd.zeros(ndt.int32) self.assertEqual(a.access_flags, 'readwrite')
def test_access_zeros(self): a = nd.zeros(ndt.int32) self.assertEqual(a.access_flags, "immutable") a = nd.zeros(ndt.int32, access="rw") self.assertEqual(a.access_flags, "readwrite")