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)
Exemplo n.º 2
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(dd_as_py(c._data), [[2, 3, 4], [6, 8], [10, 11, 12]])
     c = blaze.eval(2 * a - b)
     self.assertEqual(dd_as_py(c._data), [[1, 0, -1], [0, 1], [2, 4, 6]])
 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_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)
Exemplo n.º 5
0
 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(dd_as_py(c._data), [0, 2, 4])
         c = blaze.eval(((a+a)*a))
         self.assertEqual(dd_as_py(c._data), [0, 2, 8])
 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_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])
Exemplo n.º 8
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(dd_as_py(c._data),
                 [[2, 3, 4], [6, 8], [10, 11, 12]])
     c = blaze.eval(2 * a - b)
     self.assertEqual(dd_as_py(c._data),
                 [[1, 0, -1], [0, 1], [2, 4, 6]])
 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)
Exemplo n.º 10
0
 def test_all(self):
     # Sanity check of reduction op
     self.assertEqual(dd_as_py(blaze.eval(blaze.all(True))._data), True)
     self.assertEqual(dd_as_py(blaze.eval(blaze.all(False))._data), False)
     self.assertEqual(dd_as_py(blaze.eval(blaze.all(blaze.array([], dshape='0 * bool')))._data), True)
     self.assertEqual(dd_as_py(blaze.eval(blaze.all([False, True]))._data),
                      False)
     self.assertEqual(dd_as_py(blaze.eval(blaze.all([True, True]))._data),
                      True)
Exemplo n.º 11
0
    def test_overload(self):
        myfunc = create_overloaded_add()

        # Test int32 overload -> add
        a = blaze.eval(myfunc(blaze.array([3,4]), blaze.array([1,2])))
        self.assertEqual(a.dshape, blaze.dshape('2, int32'))
        self.assertEqual(nd.as_py(a._data.dynd_arr()), [4, 6])
        # Test int16 overload -> subtract
        a = blaze.eval(myfunc(blaze.array([3,4], dshape='int16'),
                        blaze.array([1,2], dshape='int16')))
        self.assertEqual(a.dshape, blaze.dshape('2, int16'))
        self.assertEqual(nd.as_py(a._data.dynd_arr()), [2, 2])
Exemplo n.º 12
0
    def test_overload(self):
        myfunc = create_overloaded_add()

        # Test int32 overload -> add
        a = blaze.eval(myfunc(blaze.array([3, 4]), blaze.array([1, 2])))
        self.assertEqual(a.dshape, dshape('2 * int32'))
        self.assertEqual(nd.as_py(a.ddesc.dynd_arr()), [4, 6])
        # Test int16 overload -> subtract
        a = blaze.eval(myfunc(blaze.array([3, 4], dshape='int16'),
                       blaze.array([1, 2], dshape='int16')))
        self.assertEqual(a.dshape, dshape('2 * int16'))
        self.assertEqual(nd.as_py(a.ddesc.dynd_arr()), [2, 2])
 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)
Exemplo n.º 15
0
    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._data.capabilities.deferred)
        p = _store(storage3 + 'Rd') if storage3 == 'dsk' else None
        try:
            Rc = blaze.eval(Rd, storage=p)
            self.assert_(isinstance(Rc, blaze.Array))
            npy_data = getattr(self, 'npy' + R)
            assert_allclose(np.array(dd_as_py(Rc._data)), npy_data)

            if storage3 == 'dsk':
                self.assert_(Rc._data.capabilities.persistent)
            else:
                self.assert_(not Rc._data.capabilities.persistent)

        finally:
            try:
                if p is not None:
                    blaze.drop(p)
            except:
                pass # show the real error...
Exemplo n.º 16
0
 def test_exec_promotion(self):
     expr = make_expr(dshape('10, int32'), dshape('10, float32'))
     result = blaze.eval(expr)
     expected = blaze.array([ 0,  2,  6, 12, 20, 30, 42, 56, 72, 90],
                            dshape=dshape('10, float64'))
     self.assertEqual(type(result), blaze.Array)
     self.assertTrue(np.all(result == expected))
Exemplo n.º 17
0
 def test_jit_promotion(self):
     expr = make_expr(dshape('10, int32'), dshape('10, float32'))
     result = blaze.eval(expr, strategy='jit')
     expected = blaze.array([ 0,  2,  6, 12, 20, 30, 42, 56, 72, 90],
                            dshape=dshape('10, float64'))
     self.assertEqual(type(result), blaze.Array)
     self.assertTrue(np.all(result == expected))
Exemplo n.º 18
0
    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...
Exemplo n.º 19
0
    def test_nesting(self):
        myfunc = create_overloaded_add()

        # A little bit of nesting
        a = blaze.eval(myfunc(myfunc(blaze.array([3, 4]), blaze.array([1, 2])),
                              blaze.array([2, 10])))
        self.assertEqual(a.dshape, dshape('2 * int32'))
        self.assertEqual(nd.as_py(a.ddesc.dynd_arr()), [6, 16])
Exemplo n.º 20
0
 def test_exec_scalar(self):
     a = blaze.array(range(10), dshape=dshape('10, int32'))
     b = 10
     expr = add(a, multiply(a, b))
     result = blaze.eval(expr)
     np_a = np.arange(10)
     expected = np_a + np_a * b
     self.assertTrue(np.all(result == expected))
Exemplo n.º 21
0
    def test_nesting(self):
        myfunc = create_overloaded_add()

        # A little bit of nesting
        a = blaze.eval(myfunc(myfunc(blaze.array([3,4]), blaze.array([1,2])),
                        blaze.array([2,10])))
        self.assertEqual(a.dshape, blaze.dshape('2, int32'))
        self.assertEqual(nd.as_py(a._data.dynd_arr()), [6, 16])
Exemplo n.º 22
0
 def test_jit_scalar(self):
     a = blaze.array(range(10), dshape=dshape('10, int32'))
     b = 10
     expr = add(a, mul(a, b))
     result = blaze.eval(expr)
     np_a = np.arange(10)
     expected = np_a + np_a * b
     self.assertTrue(np.all(result == expected))
Exemplo n.º 23
0
 def test_query_where(self):
     expr = ops.index(self.table, self.col_i > 5)
     result = eval(expr)
     row1, row2 = result
     self.assertEqual((int(row1[0]), str(row1[1]), float(row1[2])),
                      (8, "world", 4.2))
     self.assertEqual((int(row2[0]), str(row2[1]), float(row2[2])),
                      (16, "!", 8.4))
Exemplo n.º 24
0
 def test_interp(self):
     a = array(range(10), dshape=dshape('10, int32'))
     b = array(range(10), dshape=dshape('10, float32'))
     expr = add(a, mul(a, b))
     result = blaze.eval(expr, strategy='py')
     expected = blaze.array([ 0,  2,  6, 12, 20, 30, 42, 56, 72, 90])
     self.assertEqual(type(result), blaze.Array)
     self.assertTrue(np.all(result == expected))
 def test_query_where(self):
     expr = ops.index(self.table, self.col_i > 5)
     result = eval(expr)
     row1, row2 = result
     self.assertEqual((int(row1[0]), str(row1[1]), float(row1[2])),
                      (8, "world", 4.2))
     self.assertEqual((int(row2[0]), str(row2[1]), float(row2[2])),
                      (16, "!", 8.4))
Exemplo n.º 26
0
    def test_overload(self):
        # Create an overloaded blaze func, populate it with
        # some ckernel implementations extracted from numpy,
        # and test some calls on it.
        d = blaze.overloading.Dispatcher()
        myfunc = blaze.BlazeFunc(d)
        def myfunc_dummy(x, y): raise NotImplementedError

        # overload int32 -> np.add
        sig = blaze.dshape("A..., int32 -> A..., int32 -> A..., int32")
        d.add_overload(myfunc_dummy, sig, {})
        ckd = _lowlevel.ckernel_deferred_from_ufunc(np.add,
                        (np.int32, np.int32, np.int32), False)
        myfunc.implement(myfunc_dummy, sig, "ckernel", ckd)

        # overload int16 -> np.subtract (so we can see the difference)
        sig = blaze.dshape("A..., int16 -> A..., int16 -> A..., int16")
        d.add_overload(myfunc_dummy, sig, {})
        ckd = _lowlevel.ckernel_deferred_from_ufunc(np.subtract,
                        (np.int16, np.int16, np.int16), False)
        myfunc.implement(myfunc_dummy, sig, "ckernel", ckd)

        # int32 overload -> add
        a = blaze.eval(myfunc(blaze.array([3,4]), blaze.array([1,2])))
        self.assertEqual(a.dshape, blaze.dshape('2, int32'))
        self.assertEqual(nd.as_py(a._data.dynd_arr()), [4, 6])
        # int16 overload -> subtract
        a = blaze.eval(myfunc(blaze.array([3,4], dshape='int16'),
                        blaze.array([1,2], dshape='int16')))
        self.assertEqual(a.dshape, blaze.dshape('2, int16'))
        self.assertEqual(nd.as_py(a._data.dynd_arr()), [2, 2])

        # type promotion to int32
        a = blaze.eval(myfunc(blaze.array([3,4], dshape='int16'),
                        blaze.array([1,2])))
        self.assertEqual(a.dshape, blaze.dshape('2, int32'))
        self.assertEqual(nd.as_py(a._data.dynd_arr()), [4, 6])
        a = blaze.eval(myfunc(blaze.array([3,4]),
                        blaze.array([1,2], dshape='int16')))
        self.assertEqual(a.dshape, blaze.dshape('2, int32'))
        self.assertEqual(nd.as_py(a._data.dynd_arr()), [4, 6])

        # type promotion to int16
        a = blaze.eval(myfunc(blaze.array([3,4], dshape='int8'),
                        blaze.array([1,2], dshape='int8')))
        self.assertEqual(a.dshape, blaze.dshape('2, int16'))
        self.assertEqual(nd.as_py(a._data.dynd_arr()), [2, 2])

        # A little bit of nesting
        a = blaze.eval(myfunc(myfunc(blaze.array([3,4]), blaze.array([1,2])),
                        blaze.array([2,10])))
        self.assertEqual(a.dshape, blaze.dshape('2, int32'))
        self.assertEqual(nd.as_py(a._data.dynd_arr()), [6, 16])

        # More nesting, with conversions
        a = blaze.eval(myfunc(myfunc(blaze.array([1,2]), blaze.array([-2, 10])),
                        myfunc(blaze.array([1, 5], dshape='int16'),
                               blaze.array(3, dshape='int16'))))
        self.assertEqual(a.dshape, blaze.dshape('2, int32'))
        self.assertEqual(nd.as_py(a._data.dynd_arr()), [-3, 14])
Exemplo n.º 27
0
    def test_nesting_and_coercion(self):
        myfunc = create_overloaded_add()

        # More nesting, with conversions
        a = blaze.eval(myfunc(myfunc(blaze.array([1,2]), blaze.array([-2, 10])),
                       myfunc(blaze.array([1, 5], dshape='int16'),
                              blaze.array(3, dshape='int16'))))
        self.assertEqual(a.dshape, blaze.dshape('2, int32'))
        self.assertEqual(nd.as_py(a._data.dynd_arr()), [-3, 14])
 def test_min_zerosize(self):
     # Empty min operations should raise, because it has no
     # reduction identity
     self.assertRaises(ValueError, blaze.eval, blaze.min([]))
     self.assertRaises(ValueError, blaze.eval, blaze.min([], keepdims=True))
     self.assertRaises(ValueError, blaze.eval, blaze.min([[], []]))
     self.assertRaises(ValueError, blaze.eval,
                       blaze.min([[], []], keepdims=True))
     self.assertRaises(ValueError, blaze.eval, blaze.min([[], []], axis=-1))
     self.assertRaises(ValueError, blaze.eval,
                       blaze.min([[], []], axis=-1, keepdims=True))
     # However, if we're only reducing on a non-empty dimension, it's ok
     self.assertEqual(
         ddesc_as_py(blaze.eval(blaze.min([[], []], axis=0)).ddesc), [])
     self.assertEqual(
         ddesc_as_py(
             blaze.eval(blaze.min([[], []], axis=0, keepdims=True)).ddesc),
         [[]])
Exemplo n.º 29
0
    def test_overload_coercion(self):
        myfunc = create_overloaded_add()

        # Test type promotion to int32
        a = blaze.eval(myfunc(blaze.array([3,4], dshape='int16'),
                        blaze.array([1,2])))
        self.assertEqual(a.dshape, blaze.dshape('2, int32'))
        self.assertEqual(nd.as_py(a._data.dynd_arr()), [4, 6])
        a = blaze.eval(myfunc(blaze.array([3,4]),
                        blaze.array([1,2], dshape='int16')))
        self.assertEqual(a.dshape, blaze.dshape('2, int32'))
        self.assertEqual(nd.as_py(a._data.dynd_arr()), [4, 6])

        # Test type promotion to int16
        a = blaze.eval(myfunc(blaze.array([3,4], dshape='int8'),
                        blaze.array([1,2], dshape='int8')))
        self.assertEqual(a.dshape, blaze.dshape('2, int16'))
        self.assertEqual(nd.as_py(a._data.dynd_arr()), [2, 2])
Exemplo n.º 30
0
    def test_overload_coercion(self):
        myfunc = create_overloaded_add()

        # Test type promotion to int32
        a = blaze.eval(myfunc(blaze.array([3, 4], dshape='int16'),
                       blaze.array([1, 2])))
        self.assertEqual(a.dshape, dshape('2 * int32'))
        self.assertEqual(nd.as_py(a.ddesc.dynd_arr()), [4, 6])
        a = blaze.eval(myfunc(blaze.array([3, 4]),
                       blaze.array([1, 2], dshape='int16')))
        self.assertEqual(a.dshape, dshape('2 * int32'))
        self.assertEqual(nd.as_py(a.ddesc.dynd_arr()), [4, 6])

        # Test type promotion to int16
        a = blaze.eval(myfunc(blaze.array([3, 4], dshape='int8'),
                       blaze.array([1, 2], dshape='int8')))
        self.assertEqual(a.dshape, dshape('2 * int16'))
        self.assertEqual(nd.as_py(a.ddesc.dynd_arr()), [2, 2])
 def test_mixed(self):
     types1 = ['int8', 'int16', 'int32', 'int64']
     types2 = ['int16', 'int32', 'float32', 'float64']
     for ty1, ty2 in zip(types1, types2):
         a = blaze.array(range(1, 6), dshape=ty1)
         b = blaze.array(range(5), dshape=ty2)
         c = (a + b) * (a - b)
         c = blaze.eval(c)
         result = [a * a - b * b for (a, b) in zip(range(1, 6), range(5))]
         self.assertEqual(ddesc_as_py(c.ddesc), result)
Exemplo n.º 32
0
def reduce_dim(kernel, a):
    from blaze import eval

    a = eval(a)
    it = iter(a)
    result = next(it)
    for x in it:
        result = kernel(result, x)

    return result
    def test_select_where(self):
        expr = ops.index(self.col_i + 2 * self.col_price,
                         blaze.logical_and(self.col_price > 5, self.col_price < 7))
        result = eval(expr)

        np_result = (self.np_i + 2 * self.np_price)[
            np.logical_and(self.np_price > 5, self.np_price < 7)]

        self.assertEqual([float(x) for x in result],
                         [float(x) for x in np_result])
Exemplo n.º 34
0
    def test_select_where(self):
        expr = ops.index(self.col_i + 2 * self.col_price,
                         blaze.logical_and(self.col_price > 5, self.col_price < 7))
        result = eval(expr)

        np_result = (self.np_i + 2 * self.np_price)[
            np.logical_and(self.np_price > 5, self.np_price < 7)]

        self.assertEqual([float(x) for x in result],
                         [float(x) for x in np_result])
Exemplo n.º 35
0
def reduce_dim(kernel, a):
    from blaze import eval

    a = eval(a)
    it = iter(a)
    result = next(it)
    for x in it:
        result = kernel(result, x)

    return result
Exemplo n.º 36
0
 def test_mixed(self):
     types1 = ['int8', 'int16', 'int32', 'int64']
     types2 = ['int16', 'int32', 'float32', 'float64']
     for ty1, ty2 in zip(types1, types2):
         a = blaze.array(range(1,6), dshape=ty1)
         b = blaze.array(range(5), dshape=ty2)
         c = (a+b)*(a-b)
         c = blaze.eval(c)
         result = [a*a - b*b for (a,b) in zip(range(1,6),range(5))]
         self.assertEqual(dd_as_py(c._data), result)
 def test_min_zerosize(self):
     # Empty min operations should raise, because it has no
     # reduction identity
     self.assertRaises(ValueError, blaze.eval, blaze.min([]))
     self.assertRaises(ValueError, blaze.eval, blaze.min([], keepdims=True))
     self.assertRaises(ValueError, blaze.eval, blaze.min([[], []]))
     self.assertRaises(ValueError, blaze.eval, blaze.min([[], []],
                                                         keepdims=True))
     self.assertRaises(ValueError, blaze.eval, blaze.min([[], []], axis=-1))
     self.assertRaises(ValueError, blaze.eval, blaze.min([[], []],
                                                         axis=-1,
                                                         keepdims=True))
     # However, if we're only reducing on a non-empty dimension, it's ok
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[], []],
                                                    axis=0)).ddesc),
                      [])
     self.assertEqual(ddesc_as_py(blaze.eval(blaze.min([[], []],
                                                    axis=0,
                                                    keepdims=True)).ddesc),
                      [[]])
Exemplo n.º 38
0
    def test_binary_kerneltree(self):
        # Create some simple blaze funcs, using Numba
        def _add(a,b):
            return a + b

        def _mul(a,b):
            return a * b
        add = BlazeFunc('add',[('f8(f8,f8)', _add),
                               ('c16(c16,c16)', _add)])
        mul = BlazeFunc('mul', {(double,)*3: _mul,
                                (c128,)*3: _mul})
        # Array data and expression
        af = blaze.array([[1,2,3], [4,5,6]],dshape=double)
        bf = blaze.array([2,3,4],dshape=double)
        cf = add(af,bf)
        df = mul(cf,cf)
        ubck = df._data.kerneltree.unbound_single_ckernel

        # Allocate the result, and run the kernel across the arguments
        result = blaze.zeros(df.dshape)
        args = [arg.arr._data for arg in df._data.args]
        ck = ubck.bind(result._data, args)
        execute_expr_single(result._data, args,
                        df._data.kerneltree.kernel.dshapes[-1],
                        df._data.kerneltree.kernel.dshapes[:-1], ck)
        self.assertEqual(dd_as_py(result._data),
                        [[(a+b) * (a+b) for a, b in izip(a1, b1)]
                                for a1, b1 in izip(
                                    [[1,2,3], [4,5,6]], [[2,3,4]]*2)])

        # Use blaze.eval to evaluate cf and df into concrete arrays
        cf2 = blaze.eval(cf)
        self.assertEqual(dd_as_py(cf2._data),
                        [[(a+b) for a, b in izip(a1, b1)]
                                for a1, b1 in izip(
                                    [[1,2,3], [4,5,6]], [[2,3,4]]*2)])
        df2 = blaze.eval(df)
        self.assertEqual(dd_as_py(df2._data),
                        [[(a+b) * (a+b) for a, b in izip(a1, b1)]
                                for a1, b1 in izip(
                                    [[1,2,3], [4,5,6]], [[2,3,4]]*2)])
Exemplo n.º 39
0
def evaluation(operand_dict):
    a = blaze.load(operand_dict['a'])
    b = blaze.load(operand_dict['b'])

    expr = (a+b)*(a*b)

    print(type(expr)) # would this be "blaze.array"?
    print(type(expr._data)) # would this be blaze.BlazeFuncDataDescriptor?

    print(expr) # what should this print???
    c = blaze.eval(expr, out_caps={}, hints={})
    print(c) #should print the result... rejoice!
Exemplo n.º 40
0
def evaluation(operand_dict):
    a = blaze.load(operand_dict['a'])
    b = blaze.load(operand_dict['b'])

    expr = (a + b) * (a * b)

    print(type(expr))  # would this be "blaze.array"?
    print(type(expr._data))  # would this be blaze.BlazeFuncDataDescriptor?

    print(expr)  # what should this print???
    c = blaze.eval(expr, out_caps={}, hints={})
    print(c)  #should print the result... rejoice!
Exemplo n.º 41
0
    def test_select_expr(self):
        raise SkipTest("Correctly compose queries with aggregations")

        expr = ((ops.max(self.col_price) / ops.min(self.col_price)) *
                (self.col_i + 2) * 3.1 - ops.avg(self.col_i))
        result = eval(expr)

        np_result = ((np.max(self.np_price) / np.min(self.np_price)) *
                     (self.np_i + 2) * 3.1 -
                     np.average(self.np_i) / np.max(self.np_price))

        self.assertEqual([float(x) for x in result],
                         [float(x) for x in np_result])
    def test_select_expr(self):
        raise SkipTest("Correctly compose queries with aggregations")

        expr = ((ops.max(self.col_price) / ops.min(self.col_price)) *
                (self.col_i + 2) * 3.1 -
                ops.avg(self.col_i))
        result = eval(expr)

        np_result = ((np.max(self.np_price) / np.min(self.np_price)) *
                     (self.np_i + 2) * 3.1 -
                     np.average(self.np_i) / np.max(self.np_price))

        self.assertEqual([float(x) for x in result],
                         [float(x) for x in np_result])
Exemplo n.º 43
0
def isolated():
    npyA = np.arange(0.0, 100.0).reshape(20, 5)
    npyB = np.arange(0.0, 100.0).reshape(20, 5)

    memA = blaze.array(npyA)
    memB = blaze.array(npyB)

    _mk_dir()
    dskA = blaze.array(npyA, storage=_store('dskA'))
    dskB = blaze.array(npyB, storage=_store('dskB'))

    expr = memA + memA
    #expr = memA + dskA
    #expr = dskA + dskA

    print(memA.dshape, expr.dshape)
    p = _store('dskRd')

    result = blaze.eval(expr, storage=p)
    #print(result)
    result2 = blaze.eval(result == npyA + npyA)

    assert np.all(result2)
Exemplo n.º 44
0
    def test_query_exec(self):
        print("establishing connection...")
        conn = interface.SciDBShimInterface('http://192.168.56.101:8080/')
        print(conn)

        a = zeros(ds, conn)
        b = ones(ds, conn)

        expr = a + b

        graph, ctx = expr.expr
        self.assertEqual(graph.dshape, dshape('10, 10, float64'))

        result = eval(expr)
        print(result)
Exemplo n.º 45
0
    def test_query_exec(self):
        print("establishing connection...")
        conn = interface.SciDBShimInterface('http://192.168.56.101:8080/')
        print(conn)

        a = zeros(ds, conn)
        b = ones(ds, conn)

        expr = a + b

        graph, ctx = expr.expr
        self.assertEqual(graph.dshape, dshape('10, 10, float64'))

        result = eval(expr)
        print(result)
Exemplo n.º 46
0
def dd_as_py(dd):
    """
    Converts the data in a data descriptor into Python
    types. This uses the data_descriptor iteration methods,
    so is not expected to be fast. Its main initial purpose
    is to assist with writing unit tests.
    """
    # TODO: This function should probably be removed.
    if not isinstance(dd, IDataDescriptor):
        raise TypeError('expected DataDescriptor, got %r' % type(dd))

    if isinstance(dd, BLZDataDescriptor):
        return [dd_as_py(child_dd) for child_dd in dd]

    if dd.capabilities.deferred:
        from blaze import Array, eval
        dd = eval(Array(dd))._data
    return nd.as_py(dd.dynd_arr())
Exemplo n.º 47
0
def ddesc_as_py(ddesc):
    """
    Converts the data in a data descriptor into Python
    types. This uses the data_descriptor iteration methods,
    so is not expected to be fast. Its main initial purpose
    is to assist with writing unit tests.
    """
    # TODO: This function should probably be removed.
    if not isinstance(ddesc, DDesc):
        raise TypeError('expected DDesc instance, got %r' % type(ddesc))

    if isinstance(ddesc, BLZ_DDesc):
        return [ddesc_as_py(child_ddesc) for child_ddesc in ddesc]

    if ddesc.capabilities.deferred:
        from blaze import Array, eval
        ddesc = eval(Array(ddesc)).ddesc
    return nd.as_py(ddesc.dynd_arr())
Exemplo n.º 48
0
    def test_query(self):
        a = zeros(ds, self.conn)
        b = ones(ds, self.conn)

        expr = add(a, multiply(a, b))

        graph, ctx = expr.expr
        self.assertEqual(graph.dshape, dshape('10, 10, float64'))

        result = eval(expr)

        self.assertEqual(len(self.conn.recorded), 1)
        [(query, persist)] = self.conn.recorded

        query = str(query)

        self.assertIn("+", query)
        self.assertIn("*", query)
        self.assertIn("build", query)
Exemplo n.º 49
0
    def test_query(self):
        a = zeros(ds, self.conn)
        b = ones(ds, self.conn)

        expr = add(a, mul(a, b))

        graph, ctx = expr.expr
        self.assertEqual(graph.dshape, dshape('10, 10, float64'))

        result = eval(expr)

        self.assertEqual(len(self.conn.recorded), 1)
        [(query, persist)] = self.conn.recorded

        query = str(query)

        self.assertIn("+", query)
        self.assertIn("*", query)
        self.assertIn("build", query)
 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),
         [[]])
Exemplo n.º 51
0
 def test_neg_scalar(self):
     expr = -self.col_i
     result = eval(expr)
     self.assertEqual([int(x) for x in result], [-4, -8, -16])
Exemplo n.º 52
0
 def method(self, *args, **kwargs):
     result = blaze.eval(blaze.Array(self))
     self._result = result
     method = getattr(result._data, methname)
     return method(*args, **kwargs)
Exemplo n.º 53
0
 def test_mod_scalar(self):
     expr = self.col_i % 3
     result = eval(expr)
     self.assertEqual([int(x) for x in result], [1, 2, 1])
Exemplo n.º 54
0
 def test_eq_scalar(self):
     expr = self.col_i == 8
     result = eval(expr)
     self.assertEqual(result.dshape.measure, bool_)
     self.assertEqual([bool(x) for x in result], [False, True, False])
    return result


def ooc_filter(array, conditions):
    pass


if __name__ == '__main__':
    #import numpy as np
    from dynd import nd

    arr = nd.array([[1, 2], [3, 4], [5, 6]])
    conditions = nd.array([[True, False], [True, True], [False, False]])
    expr = filter(arr, conditions)

    result = blaze.eval(expr, strategy='py')
    print(">>> result = filter([[1, 2], [3, 4], [5, 6]],\n"
          "                    [[True, False], [True, True], [False, False]])")
    print(">>> result")
    print(result)
    print(">>> type(result)")
    print(type(result))

    expr = filter(arr + 10, conditions)
    result = blaze.eval(expr, strategy='py')
    print(">>> result = filter([[1, 2], [3, 4], [5, 6]] + 10,\n"
          "                    [[True, False], [True, True], [False, False]])")
    print(">>> result")
    print(result)
    print(">>> type(result)")
    print(type(result))
Exemplo n.º 56
0
 def test_floordiv_scalar(self):
     expr = self.col_i // 2
     result = eval(expr)
     self.assertEqual([int(x) for x in result], [2, 4, 8])
Exemplo n.º 57
0
 def test_ne_scalar(self):
     expr = self.col_i != 8
     result = eval(expr)
     self.assertEqual([bool(x) for x in result], [True, False, True])
 def method(self, *args, **kwargs):
     result = blaze.eval(blaze.Array(self))
     self._result = result
     method = getattr(result.ddesc, methname)
     return method(*args, **kwargs)
Exemplo n.º 59
0
 def test_truediv_scalar(self):
     expr = self.col_i / 2
     result = eval(expr)
     self.assertEqual([float(x) for x in result], [2., 4., 8.])