示例#1
0
文件: toplevel.py 项目: garfee/blaze
def zeros(dshape, params=None):
    """ Create an Array and fill it with zeros.

    Parameters
    ----------
    dshape : str, blaze.dshape instance
        Specifies the datashape of the outcome object.
    params : blaze.params object
        Any parameter supported by the backend library.

    Returns
    -------
    out : an Array object.

    """
    if isinstance(dshape, basestring):
        dshape = _dshape(dshape)
    shape, dtype = to_numpy(dshape)
    cparams, rootdir, format_flavor = to_cparams(params or _params())
    if rootdir is not None:
        carray.zeros(shape, dtype, rootdir=rootdir, cparams=cparams)
        return open(rootdir)
    else:
        source = CArraySource(carray.zeros(shape, dtype, cparams=cparams),
                              params=params)
        return Array(source)
示例#2
0
def zeros(dshape, params=None, eclass=_eclass.manifest):
    """ Create an Array and fill it with zeros.

    Parameters
    ----------
    dshape : str, blaze.dshape instance
        Specifies the datashape of the outcome object.
    params : blaze.params object
        Any parameter supported by the backend library.

    Returns
    -------
    out : an Array object.

    """
    if isinstance(dshape, basestring):
        dshape = _dshape(dshape)
    shape, dtype = to_numpy(dshape)
    cparams, rootdir, format_flavor = to_cparams(params or _params())
    if rootdir is not None:
        carray.zeros(shape, dtype, rootdir=rootdir, cparams=cparams)
        return open(rootdir)
    else:
        source = CArraySource(carray.zeros(shape, dtype, cparams=cparams),
                              params=params)
        if eclass is _eclass.manifest:
            return Array(source)
        elif eclass is _eclass.delayed:
            return NDArray(source)
示例#3
0
    def test01b(self):
        """Creating a carray in "w" mode."""

        N = 50000
        cn = ca.zeros(N, dtype="i1", rootdir=self.rootdir)
        self.assert_(len(cn) == N)

        cn = ca.zeros(N - 2, dtype="i1", rootdir=self.rootdir, mode='w')
        self.assert_(len(cn) == N - 2)

        # Now check some accesses (no errors should be raised)
        cn.append([1, 1])
        self.assert_(len(cn) == N)
        cn[1] = 2
        self.assert_(cn[1] == 2)
示例#4
0
    def test01b(self):
        """Creating a carray in "w" mode."""

        N = 50000
        cn = ca.zeros(N, dtype="i1", rootdir=self.rootdir)
        self.assert_(len(cn) == N)

        cn = ca.zeros(N-2, dtype="i1", rootdir=self.rootdir, mode='w')
        self.assert_(len(cn) == N-2)

        # Now check some accesses (no errors should be raised)
        cn.append([1,1])
        self.assert_(len(cn) == N)
        cn[1] = 2
        self.assert_(cn[1] == 2)
示例#5
0
 def test01c(self):
     """Testing zeros() constructor, with a string type."""
     a = np.zeros(self.N, dtype='S5')
     ac = ca.zeros(self.N, dtype='S5', rootdir=self.rootdir)
     #print "ac-->", `ac`
     self.assert_(a.dtype == ac.dtype)
     self.assert_(np.all(a == ac[:]))
示例#6
0
 def test01b(self):
     """Testing zeros() constructor, with a `dtype`."""
     a = np.zeros(self.N, dtype='i4')
     ac = ca.zeros(self.N, dtype='i4', rootdir=self.rootdir)
     #print "dtypes-->", a.dtype, ac.dtype
     self.assert_(a.dtype == ac.dtype)
     self.assert_(np.all(a == ac[:]))
示例#7
0
 def test01b(self):
     """Testing zeros() constructor, with a `dtype`."""
     a = np.zeros(self.N, dtype='i4')
     ac = ca.zeros(self.N, dtype='i4', rootdir=self.rootdir)
     #print "dtypes-->", a.dtype, ac.dtype
     self.assert_(a.dtype == ac.dtype)
     self.assert_(np.all(a == ac[:]))
示例#8
0
 def test01c(self):
     """Testing zeros() constructor, with a string type."""
     a = np.zeros(self.N, dtype='S5')
     ac = ca.zeros(self.N, dtype='S5', rootdir=self.rootdir)
     #print "ac-->", `ac`
     self.assert_(a.dtype == ac.dtype)
     self.assert_(np.all(a == ac[:]))
示例#9
0
 def test01c(self):
     """Testing `zeros` constructor (III)"""
     a = np.zeros((2, 2), dtype='(4,)i4')
     b = ca.zeros((2, 2), dtype='(4,)i4', rootdir=self.rootdir)
     if self.open:
         b = ca.open(rootdir=self.rootdir)
     #print "b->", `b`
     assert_array_equal(a, b, "Arrays are not equal")
示例#10
0
 def test01c(self):
     """Testing `zeros` constructor (III)"""
     a = np.zeros((2,2), dtype='(4,)i4')
     b = ca.zeros((2,2), dtype='(4,)i4', rootdir=self.rootdir)
     if self.open:
         b = ca.open(rootdir=self.rootdir)
     #print "b->", `b`
     assert_array_equal(a, b, "Arrays are not equal")
示例#11
0
    def test01c(self):
        """Creating a carray in "a" mode."""

        N = 30003
        cn = ca.zeros(N, dtype="i1", rootdir=self.rootdir)
        self.assert_(len(cn) == N)

        self.assertRaises(RuntimeError, ca.zeros, 
                          N-2, dtype="i1", rootdir=self.rootdir, mode='a')
示例#12
0
 def test00a(self):
     """Testing wheretrue() in combination with a list constructor"""
     a = ca.zeros(self.N, dtype="bool")
     a[30:40] = ca.ones(10, dtype="bool")
     alist = list(a)
     blist1 = [r for r in a.wheretrue()]
     self.assert_(blist1 == range(30,40))
     alist2 = list(a)
     self.assert_(alist == alist2, "wheretrue() not working correctly")
示例#13
0
 def getobject(self):
     if self.flavor == 'carray':
         obj = ca.zeros(10, dtype="i1", rootdir=self.rootdir)
         assert type(obj) == ca.carray
     elif self.flavor == 'ctable':
         obj = ca.fromiter(((i,i*2) for i in range(10)), dtype='i2,f4',
                           count=10, rootdir=self.rootdir)
         assert type(obj) == ca.ctable
     return obj
示例#14
0
 def test01a(self):
     """Testing where() in combination with a list constructor"""
     a = ca.zeros(self.N, dtype="bool")
     a[30:40] = ca.ones(10, dtype="bool")
     b = ca.arange(self.N, dtype="f4")
     blist = list(b)
     blist1 = [r for r in b.where(a)]
     self.assert_(blist1 == range(30,40))
     blist2 = list(b)
     self.assert_(blist == blist2, "where() not working correctly")
示例#15
0
    def test02a(self):
        """Opening a carray in "r" mode."""

        N = 10001
        cn = ca.zeros(N, dtype="i1", rootdir=self.rootdir)
        self.assert_(len(cn) == N)

        cn = ca.carray(rootdir=self.rootdir, mode='r')
        self.assert_(len(cn) == N)

        # Now check some accesses
        self.assertRaises(RuntimeError, cn.__setitem__, 1, 1)
        self.assertRaises(RuntimeError, cn.append, 1)
示例#16
0
    def test02a(self):
        """Opening a carray in "r" mode."""

        N = 10001
        cn = ca.zeros(N, dtype="i1", rootdir=self.rootdir)
        self.assert_(len(cn) == N)

        cn = ca.carray(rootdir=self.rootdir, mode='r')
        self.assert_(len(cn) == N)

        # Now check some accesses
        self.assertRaises(RuntimeError, cn.__setitem__, 1, 1)
        self.assertRaises(RuntimeError, cn.append, 1)
示例#17
0
    def test01c(self):
        """Creating a carray in "a" mode."""

        N = 30003
        cn = ca.zeros(N, dtype="i1", rootdir=self.rootdir)
        self.assert_(len(cn) == N)

        self.assertRaises(RuntimeError,
                          ca.zeros,
                          N - 2,
                          dtype="i1",
                          rootdir=self.rootdir,
                          mode='a')
示例#18
0
    def test02b(self):
        """Opening a carray in "w" mode."""

        N = 100001
        cn = ca.zeros(N, dtype="i1", rootdir=self.rootdir)
        self.assert_(len(cn) == N)

        cn = ca.carray(rootdir=self.rootdir, mode='w')
        self.assert_(len(cn) == 0)

        # Now check some accesses (no errors should be raised)
        cn.append([1,1])
        self.assert_(len(cn) == 2)
        cn[1] = 2
        self.assert_(cn[1] == 2)
示例#19
0
    def test02b(self):
        """Opening a carray in "w" mode."""

        N = 100001
        cn = ca.zeros(N, dtype="i1", rootdir=self.rootdir)
        self.assert_(len(cn) == N)

        cn = ca.carray(rootdir=self.rootdir, mode='w')
        self.assert_(len(cn) == 0)

        # Now check some accesses (no errors should be raised)
        cn.append([1, 1])
        self.assert_(len(cn) == 2)
        cn[1] = 2
        self.assert_(cn[1] == 2)
示例#20
0
 def test07(self):
     """Checking carray constructor from another carray.
     
     Test introduced after it was seen failing (blaze issue #30)
     """
     types = [np.int8, np.int16, np.int32, np.int64,
              np.uint8, np.uint16, np.uint32, np.uint64,
              np.float16, np.float32, np.float64, np.float128,
              np.complex64, np.complex128, np.complex256]
     shapes = [(10,), (10,10), (10,10,10)]
     for shape in shapes:
         for t in types:
             a = ca.zeros(shape, t)
             b = ca.carray(a)
             self.assertEqual(a.dtype, b.dtype)
             self.assertEqual(a.shape, b.shape)
             self.assertEqual(a.shape, shape)
示例#21
0
    def test01(self):
        """Creating an extremely large carray (> 2**32) on disk."""

        cn = ca.zeros(5e9, dtype="i1", rootdir=self.rootdir)
        self.assertEqual(len(cn), int(5e9))

        # Now check some accesses
        cn[1] = 1
        self.assertEqual(cn[1], 1)
        cn[int(2e9)] = 2
        self.assertEqual(cn[int(2e9)], 2)
        cn[long(3e9)] = 3
        self.assertEqual(cn[long(3e9)], 3)
        cn[-1] = 4
        self.assertEqual(cn[-1], 4)

        self.assertEqual(cn.sum(), 10)
    def test01(self):
        """Creating an extremely large carray (> 2**32) on disk."""

        cn = ca.zeros(5e9, dtype="i1", rootdir=self.rootdir)
        self.assert_(len(cn) == int(5e9))

        # Now check some accesses
        cn[1] = 1
        self.assert_(cn[1] == 1)
        cn[int(2e9)] = 2
        self.assert_(cn[int(2e9)] == 2)
        cn[long(3e9)] = 3
        self.assert_(cn[long(3e9)] == 3)
        cn[-1] = 4
        self.assert_(cn[-1] == 4)

        self.assert_(cn.sum() == 10)
示例#23
0
    def test00(self):
        """Creating an extremely large carray (> 2**32) in memory."""

        cn = ca.zeros(5e9, dtype="i1")
        self.assertEqual(len(cn), int(5e9))

        # Now check some accesses
        cn[1] = 1
        self.assertEqual(cn[1], 1)
        cn[int(2e9)] = 2
        self.assertEqual(cn[int(2e9)], 2)
        cn[long(3e9)] = 3
        self.assertEqual(cn[long(3e9)], 3)
        cn[-1] = 4
        self.assertEqual(cn[-1], 4)

        self.assertEqual(cn.sum(), 10)
示例#24
0
 def test07(self):
     """Checking carray constructor from another carray.
     
     Test introduced after it was seen failing (blaze issue #30)
     """
     types = [
         np.int8, np.int16, np.int32, np.int64, np.uint8, np.uint16,
         np.uint32, np.uint64, np.float16, np.float32, np.float64,
         np.complex64, np.complex128
     ]
     if hasattr(np, 'float128'):
         types.extend([np.float128, np.complex256])
     shapes = [(10, ), (10, 10), (10, 10, 10)]
     for shape in shapes:
         for t in types:
             a = ca.zeros(shape, t)
             b = ca.carray(a)
             self.assertEqual(a.dtype, b.dtype)
             self.assertEqual(a.shape, b.shape)
             self.assertEqual(a.shape, shape)
示例#25
0
    def test02(self):
        """Opening an extremely large carray (> 2**32) on disk."""

        # Create the array on-disk
        cn = ca.zeros(5e9, dtype="i1", rootdir=self.rootdir)
        self.assertEqual(len(cn), int(5e9))
        # Reopen it from disk
        cn = ca.carray(rootdir=self.rootdir)
        self.assertEqual(len(cn), int(5e9))

        # Now check some accesses
        cn[1] = 1
        self.assertEqual(cn[1], 1)
        cn[int(2e9)] = 2
        self.assertEqual(cn[int(2e9)], 2)
        cn[long(3e9)] = 3
        self.assertEqual(cn[long(3e9)], 3)
        cn[-1] = 4
        self.assertEqual(cn[-1], 4)

        self.assertEqual(cn.sum(), 10)
示例#26
0
 def test00b(self):
     """Testing wheretrue() with a multidimensional array"""
     a = ca.zeros((self.N, 10), dtype="bool")
     a[30:40] = ca.ones(10, dtype="bool")
     self.assertRaises(NotImplementedError, a.wheretrue)
示例#27
0
 def test01b(self):
     """Testing where() with a multidimensional array"""
     a = ca.zeros((self.N, 10), dtype="bool")
     a[30:40] = ca.ones(10, dtype="bool")
     b = ca.arange(self.N*10, dtype="f4").reshape((self.N, 10))
     self.assertRaises(NotImplementedError, b.where, a)
示例#28
0
 def test02(self):
     """Testing sum() with strings (TypeError)."""
     ac = ca.zeros(10, 'S3')
     self.assertRaises(TypeError, ac.sum)
示例#29
0
 def test01a(self):
     """Testing zeros() constructor."""
     a = np.zeros(self.N)
     ac = ca.zeros(self.N, rootdir=self.rootdir)
     self.assert_(a.dtype == ac.dtype)
     self.assert_(np.all(a == ac[:]))
示例#30
0
 def test02(self):
     """Testing sum() with strings (TypeError)."""
     ac = ca.zeros(10, 'S3')
     self.assertRaises(TypeError, ac.sum)
示例#31
0
 def test01a(self):
     """Testing zeros() constructor."""
     a = np.zeros(self.N)
     ac = ca.zeros(self.N, rootdir=self.rootdir)
     self.assert_(a.dtype == ac.dtype)
     self.assert_(np.all(a == ac[:]))