コード例 #1
0
ファイル: test_range_linspace.py プロジェクト: menghaozhu/hat
 def test_default_dtype(self):
     # Defaults to int32 when given ints
     self.assertEqual(nd.dtype_of(nd.range(10)), ndt.int32)
     # Except if the input numbers don't fit, then returns int64
     self.assertEqual(nd.dtype_of(nd.range(2**32, 2**32+10)), ndt.int64)
     self.assertEqual(nd.dtype_of(nd.range(-2**32, -2**32+10)), ndt.int64)
     # Gives float64 when given floats
     self.assertEqual(nd.dtype_of(nd.range(10.0)), ndt.float64)
コード例 #2
0
ファイル: test_graph.py プロジェクト: AbhiAgarwal/blaze
 def test_graph(self):
     a = array(nd.range(10, dtype=ndt.int32))
     b = array(nd.range(10, dtype=ndt.float32))
     expr = add(a, multiply(a, b))
     graph, ctx = expr.expr
     self.assertEqual(len(ctx.params), 2)
     self.assertFalse(ctx.constraints)
     self.assertEqual(graph.dshape, dshape('10, float64'))
コード例 #3
0
 def test_graph(self):
     a = array(nd.range(10, dtype=ndt.int32))
     b = array(nd.range(10, dtype=ndt.float32))
     expr = add(a, multiply(a, b))
     graph, ctx = expr.expr
     self.assertEqual(len(ctx.params), 2)
     self.assertFalse(ctx.constraints)
     self.assertEqual(graph.dshape, dshape('10 * float64'))
コード例 #4
0
ファイル: test_range_linspace.py プロジェクト: menghaozhu/hat
 def test_float_step(self):
     # Should produce the correct count for 1.0/int steps
     for i in range(1, 32):
         a = nd.range(1.0, step=1.0/i)
         self.assertEqual(len(a), i)
         self.assertEqual(nd.as_py(a[0]), 0)
     # For powers of two, should be getting exact answers
     for i in range(5):
         a = nd.range(1.0, step=1.0/2**i)
         self.assertEqual(nd.as_py(a), [float(x)/2**i for x in range(2**i)])
コード例 #5
0
 def test_numpy_view_of_noncontig_dynd_array(self):
     n = nd.range(10)[1::3]
     a = np.asarray(n)
     self.assertEqual(a.dtype, np.dtype('i4'))
     self.assertFalse(a.flags.c_contiguous)
     self.assertEqual(a.ndim, nd.ndim_of(n))
     self.assertEqual(a.shape, n.shape)
     self.assertEqual(a.strides, n.strides)
     # Make sure it's a view as needed
     a[1] = 100
     self.assertEqual(nd.as_py(n[1]), 100)
コード例 #6
0
 def test_numpy_view_of_noncontig_dynd_array(self):
     n = nd.range(10)[1::3]
     a = np.asarray(n)
     self.assertEqual(a.dtype, np.dtype('i4'))
     self.assertFalse(a.flags.c_contiguous)
     self.assertEqual(a.ndim, nd.ndim_of(n))
     self.assertEqual(a.shape, n.shape)
     self.assertEqual(a.strides, n.strides)
     # Make sure it's a view as needed
     a[1] = 100
     self.assertEqual(nd.as_py(n[1]), 100)
コード例 #7
0
    def test_numpy_view_of_dynd_array(self):
        # Tests viewing a dynd.array as a numpy array
        nonnative = self.nonnative

        n = nd.range(10, dtype=ndt.int32)
        a = np.asarray(n)
        self.assertEqual(a.dtype, np.dtype(np.int32))
        self.assertTrue(a.flags.aligned)
        self.assertEqual(a.ndim, nd.ndim_of(n))
        self.assertEqual(a.shape, n.shape)
        self.assertEqual(a.strides, n.strides)
        # Make sure it's a view
        a[1] = 100
        self.assertEqual(nd.as_py(n[1]), 100)

        n = nd.view(np.arange(12, dtype=(nonnative + 'i4')).reshape(3, 4))
        a = np.asarray(n)
        self.assertEqual(a.dtype, np.dtype(nonnative + 'i4'))
        self.assertTrue(a.flags.aligned)
        self.assertEqual(a.ndim, nd.ndim_of(n))
        self.assertEqual(a.shape, n.shape)
        self.assertEqual(a.strides, n.strides)
        # Make sure it's a view
        a[1, 2] = 100
        self.assertEqual(nd.as_py(n[1, 2]), 100)

        n = nd.view(
            np.arange(49, dtype='i1')[1:].view(dtype=np.int32).reshape(4, 3))
        a = np.asarray(n)
        self.assertEqual(a.dtype, np.dtype(np.int32))
        self.assertFalse(a.flags.aligned)
        self.assertEqual(a.ndim, nd.ndim_of(n))
        self.assertEqual(a.shape, n.shape)
        self.assertEqual(a.strides, n.strides)
        # Make sure it's a view
        a[1, 2] = 100
        self.assertEqual(nd.as_py(n[1, 2]), 100)

        n = nd.view(
            np.arange(49,
                      dtype='i1')[1:].view(dtype=(nonnative + 'i4')).reshape(
                          2, 2, 3))
        a = np.asarray(n)
        self.assertEqual(a.dtype, np.dtype(nonnative + 'i4'))
        self.assertFalse(a.flags.aligned)
        self.assertEqual(a.ndim, nd.ndim_of(n))
        self.assertEqual(a.shape, n.shape)
        self.assertEqual(a.strides, n.strides)
        # Make sure it's a view
        a[1, 1, 1] = 100
        self.assertEqual(nd.as_py(n[1, 1, 1]), 100)
コード例 #8
0
ファイル: test_elwise_map.py プロジェクト: garaud/dynd-python
 def test_unary_function(self):
     def doubler(dst, src):
         dst[...] = [2 * nd.as_py(x) for x in src]
     # 1D array
     a = nd.range(5)
     b = nd.elwise_map([a], doubler, ndt.int32)
     self.assertEqual(nd.as_py(b), [0, 2, 4, 6, 8])
     # indexing into the deferred dynd array
     self.assertEqual(nd.as_py(b[1]), 2)
     self.assertEqual(nd.as_py(b[3:]), [6, 8])
     self.assertEqual(nd.as_py(b[1::2]), [2, 6])
     # Modifying 'a' affects 'b'
     a[1:4] = [-1, 10, -2]
     self.assertEqual(nd.as_py(b), [0, -2, 20, -4, 8])
コード例 #9
0
 def test_strided_dim(self):
     a = nd.empty(100, ndt.int32)
     a[...] = nd.range(100)
     self.assertEqual(nd.type_of(a), ndt.type('A * int32'))
     self.assertEqual(nd.type_of(a[...]), ndt.type('A * int32'))
     self.assertEqual(nd.type_of(a[0]), ndt.int32)
     self.assertEqual(nd.type_of(a[0:1]), ndt.type('A * int32'))
     self.assertEqual(nd.as_py(a[0]), 0)
     self.assertEqual(nd.as_py(a[99]), 99)
     self.assertEqual(nd.as_py(a[-1]), 99)
     self.assertEqual(nd.as_py(a[-100]), 0)
     self.assertRaises(IndexError, lambda x : x[-101], a)
     self.assertRaises(IndexError, lambda x : x[100], a)
     self.assertRaises(IndexError, lambda x : x[-101:], a)
     self.assertRaises(IndexError, lambda x : x[-5:101:2], a)
コード例 #10
0
    def test_numpy_view_of_dynd_array(self):
        # Tests viewing a dynd.array as a numpy array
        nonnative = self.nonnative

        n = nd.range(10, dtype=ndt.int32)
        a = np.asarray(n)
        self.assertEqual(a.dtype, np.dtype(np.int32))
        self.assertTrue(a.flags.aligned)
        self.assertEqual(a.ndim, nd.ndim_of(n))
        self.assertEqual(a.shape, n.shape)
        self.assertEqual(a.strides, n.strides)
        # Make sure it's a view
        a[1] = 100
        self.assertEqual(nd.as_py(n[1]), 100)

        n = nd.view(np.arange(12, dtype=(nonnative + 'i4')).reshape(3,4))
        a = np.asarray(n)
        self.assertEqual(a.dtype, np.dtype(nonnative + 'i4'))
        self.assertTrue(a.flags.aligned)
        self.assertEqual(a.ndim, nd.ndim_of(n))
        self.assertEqual(a.shape, n.shape)
        self.assertEqual(a.strides, n.strides)
        # Make sure it's a view
        a[1,2] = 100
        self.assertEqual(nd.as_py(n[1,2]), 100)

        n = nd.view(np.arange(49, dtype='i1')[1:].view(dtype=np.int32).reshape(4,3))
        a = np.asarray(n)
        self.assertEqual(a.dtype, np.dtype(np.int32))
        self.assertFalse(a.flags.aligned)
        self.assertEqual(a.ndim, nd.ndim_of(n))
        self.assertEqual(a.shape, n.shape)
        self.assertEqual(a.strides, n.strides)
        # Make sure it's a view
        a[1,2] = 100
        self.assertEqual(nd.as_py(n[1,2]), 100)

        n = nd.view(np.arange(49, dtype='i1')[1:].view(
                    dtype=(nonnative + 'i4')).reshape(2,2,3))
        a = np.asarray(n)
        self.assertEqual(a.dtype, np.dtype(nonnative + 'i4'))
        self.assertFalse(a.flags.aligned)
        self.assertEqual(a.ndim, nd.ndim_of(n))
        self.assertEqual(a.shape, n.shape)
        self.assertEqual(a.strides, n.strides)
        # Make sure it's a view
        a[1,1,1] = 100
        self.assertEqual(nd.as_py(n[1,1,1]), 100)
コード例 #11
0
 def test_fixed_dim(self):
     a = nd.empty(100, ndt.int32)
     a[...] = nd.range(100)
     b = list(range(100))
     self.assertEqual(nd.type_of(a), ndt.type('100 * int32'))
     self.assertEqual(nd.type_of(a[...]), ndt.type('100 * int32'))
     self.assertEqual(nd.type_of(a[0]), ndt.int32)
     self.assertEqual(nd.type_of(a[0:1]), ndt.type('1 * int32'))
     self.assertEqual(nd.as_py(a[0]), b[0])
     self.assertEqual(nd.as_py(a[99]), b[99])
     self.assertEqual(nd.as_py(a[-1]), b[-1])
     self.assertEqual(nd.as_py(a[-100]), b[-100])
     self.assertEqual(nd.as_py(a[-101:]), b[-101:])
     self.assertEqual(nd.as_py(a[-5:101:2]), b[-5:101:2])
     self.assertRaises(IndexError, lambda x : x[-101], a)
     self.assertRaises(IndexError, lambda x : x[100], a)
コード例 #12
0
ファイル: test_elwise_map.py プロジェクト: garaud/dynd-python
 def test_unary_function_exception(self):
     threshold_val = 10
     def threshold_raise(dst, src):
         for x in src:
             if nd.as_py(x) >= threshold_val:
                 raise ValueError("Bad value %s" % x)
             dst[...] = src
     a = nd.range(20)
     b = nd.elwise_map([a], threshold_raise, ndt.int32)
     # Should raise when the whole array is evaluated
     self.assertRaises(ValueError, b.eval)
     # If the actual evaluated values are ok, shouldn't raise
     self.assertEqual(nd.as_py(b[5:10]), [5, 6, 7, 8, 9])
     # threshold_raise is a closure, test that it works
     threshold_val = 9
     self.assertRaises(ValueError, b[5:10].eval)
コード例 #13
0
 def test_fixed_dim(self):
     a = nd.empty(100, ndt.int32)
     a[...] = nd.range(100)
     b = list(range(100))
     self.assertEqual(nd.type_of(a), ndt.type('100 * int32'))
     self.assertEqual(nd.type_of(a[...]), ndt.type('100 * int32'))
     self.assertEqual(nd.type_of(a[0]), ndt.int32)
     self.assertEqual(nd.type_of(a[0:1]), ndt.type('1 * int32'))
     self.assertEqual(nd.as_py(a[0]), b[0])
     self.assertEqual(nd.as_py(a[99]), b[99])
     self.assertEqual(nd.as_py(a[-1]), b[-1])
     self.assertEqual(nd.as_py(a[-100]), b[-100])
     self.assertEqual(nd.as_py(a[-101:]), b[-101:])
     self.assertEqual(nd.as_py(a[-5:101:2]), b[-5:101:2])
     self.assertRaises(IndexError, lambda x: x[-101], a)
     self.assertRaises(IndexError, lambda x: x[100], a)
コード例 #14
0
 def test_strided_dim(self):
     a = nd.empty(100, ndt.int32)
     a[...] = nd.range(100)
     a[0] = 1000
     self.assertEqual(nd.as_py(a[0]), 1000)
     a[1:8:3] = 120
     self.assertEqual(nd.as_py(a[:11]),
                     [1000, 120, 2, 3, 120, 5, 6, 120, 8, 9, 10])
     a[5:2:-1] = [-10, -20, -30]
     self.assertEqual(nd.as_py(a[:11]),
                     [1000, 120, 2, -30, -20, -10, 6, 120, 8, 9, 10])
     a[1] = False
     self.assertEqual(nd.as_py(a[1]), 0)
     a[2] = True
     self.assertEqual(nd.as_py(a[2]), 1)
     a[3] = -10.0
     self.assertEqual(nd.as_py(a[3]), -10)
コード例 #15
0
 def test_strided_dim(self):
     a = nd.empty(100, ndt.int32)
     a[...] = nd.range(100)
     a[0] = 1000
     self.assertEqual(nd.as_py(a[0]), 1000)
     a[1:8:3] = 120
     self.assertEqual(nd.as_py(a[:11]),
                      [1000, 120, 2, 3, 120, 5, 6, 120, 8, 9, 10])
     a[5:2:-1] = [-10, -20, -30]
     self.assertEqual(nd.as_py(a[:11]),
                      [1000, 120, 2, -30, -20, -10, 6, 120, 8, 9, 10])
     a[1] = False
     self.assertEqual(nd.as_py(a[1]), 0)
     a[2] = True
     self.assertEqual(nd.as_py(a[2]), 1)
     a[3] = -10.0
     self.assertEqual(nd.as_py(a[3]), -10)
コード例 #16
0
 def test_var_dim(self):
     # TODO: Reenable tests below when var dim slicing is implemented properly
     a = nd.empty('var * int32')
     a[...] = nd.range(100)
     b = list(range(100))
     self.assertEqual(nd.type_of(a), ndt.type('var * int32'))
     self.assertEqual(nd.type_of(a[...]), ndt.type('var * int32'))
     self.assertEqual(nd.type_of(a[:]), ndt.type('var * int32'))
     self.assertEqual(nd.type_of(a[0]), ndt.int32)
     # self.assertEqual(nd.type_of(a[0:1]), ndt.type('fixed * int32'))
     self.assertEqual(nd.as_py(a[0]), b[0])
     self.assertEqual(nd.as_py(a[99]), b[99])
     self.assertEqual(nd.as_py(a[-1]), b[-1])
     self.assertEqual(nd.as_py(a[-100]), b[-100])
     #self.assertEqual(nd.as_py(a[-101:]), b[-101:])
     #self.assertEqual(nd.as_py(a[-5:101:2]), b[-5:101:2])
     self.assertRaises(IndexError, lambda x : x[-101], a)
     self.assertRaises(IndexError, lambda x : x[100], a)
コード例 #17
0
 def test_var_dim(self):
     # TODO: Reenable tests below when var dim slicing is implemented properly
     a = nd.empty('var * int32')
     a[...] = nd.range(100)
     b = list(range(100))
     self.assertEqual(nd.type_of(a), ndt.type('var * int32'))
     self.assertEqual(nd.type_of(a[...]), ndt.type('var * int32'))
     self.assertEqual(nd.type_of(a[:]), ndt.type('var * int32'))
     self.assertEqual(nd.type_of(a[0]), ndt.int32)
     # self.assertEqual(nd.type_of(a[0:1]), ndt.type('fixed * int32'))
     self.assertEqual(nd.as_py(a[0]), b[0])
     self.assertEqual(nd.as_py(a[99]), b[99])
     self.assertEqual(nd.as_py(a[-1]), b[-1])
     self.assertEqual(nd.as_py(a[-100]), b[-100])
     #self.assertEqual(nd.as_py(a[-101:]), b[-101:])
     #self.assertEqual(nd.as_py(a[-5:101:2]), b[-5:101:2])
     self.assertRaises(IndexError, lambda x: x[-101], a)
     self.assertRaises(IndexError, lambda x: x[100], a)
コード例 #18
0
ファイル: test_elwise_map.py プロジェクト: garaud/dynd-python
 def test_binary_function_broadcast(self):
     def multiplier(dst, src0, src1):
         for d, s0, s1 in zip(dst, src0, src1):
             d[...] = nd.as_py(s0) * nd.as_py(s1)
     # 1D array
     a = nd.range(5)
     b = nd.array(12).eval_copy(access='readwrite')
     c = nd.elwise_map([a,b], multiplier, ndt.int32)
     self.assertEqual(nd.as_py(c), [0, 12, 24, 36, 48])
     # indexing into the deferred dynd array
     self.assertEqual(nd.as_py(c[1]), 12)
     self.assertEqual(nd.as_py(c[3:]), [36, 48])
     self.assertEqual(nd.as_py(c[1::2]), [12, 36])
     # Modifying 'a' or 'b' affects 'c'
     a[1:4] = [-1, 10, -2]
     self.assertEqual(nd.as_py(c), [0, -12, 120, -24, 48])
     b[...] = 100
     self.assertEqual(nd.as_py(c), [0, -100, 1000, -200, 400])
コード例 #19
0
ファイル: test_elwise_map.py プロジェクト: garaud/dynd-python
 def test_binary_function(self):
     def multiplier(dst, src0, src1):
         for d, s0, s1 in zip(dst, src0, src1):
             d[...] = nd.as_py(s0) * nd.as_py(s1)
     # 1D array
     a = nd.range(5)
     b = nd.array([1, 3, -2, 4, 12], access='rw')
     c = nd.elwise_map([a,b], multiplier, ndt.int32)
     self.assertEqual(nd.as_py(c), [0, 3, -4, 12, 48])
     # indexing into the deferred dynd array
     self.assertEqual(nd.as_py(c[1]), 3)
     self.assertEqual(nd.as_py(c[3:]), [12, 48])
     self.assertEqual(nd.as_py(c[1::2]), [3, 12])
     # Modifying 'a' or 'b' affects 'c'
     a[1:4] = [-1, 10, -2]
     self.assertEqual(nd.as_py(c), [0, -3, -20, -8, 48])
     b[-1] = 100
     self.assertEqual(nd.as_py(c), [0, -3, -20, -8, 400])
コード例 #20
0
ファイル: test_elwise_map.py プロジェクト: garaud/dynd-python
 def test_unary_function_chained(self):
     a = nd.range(3).ucast(ndt.float32)
     def multiscale(dst, src):
         dst.once = src
         dst.twice = [2 * nd.as_py(x) for x in src]
         dst.thrice = [3 * nd.as_py(x) for x in src]
     b = nd.elwise_map([a], multiscale, ndt.type('{once: int32, twice: int32, thrice: int32}'))
     self.assertEqual(nd.as_py(b), [{'once':0,'twice':0,'thrice':0},
                     {'once':1,'twice':2,'thrice':3},
                     {'once':2,'twice':4,'thrice':6}])
     self.assertEqual(nd.as_py(b.once), [0, 1, 2])
     self.assertEqual(nd.as_py(b.twice), [0, 2, 4])
     self.assertEqual(nd.as_py(b.thrice), [0, 3, 6])
     # Modifying 'a' affects 'b'
     a[0] = -10
     self.assertEqual(nd.as_py(b.once), [-10, 1, 2])
     self.assertEqual(nd.as_py(b.twice), [-20, 2, 4])
     self.assertEqual(nd.as_py(b.thrice), [-30, 3, 6])
コード例 #21
0
ファイル: test_range_linspace.py プロジェクト: menghaozhu/hat
 def test_specified_dtype(self):
     # Must return the requested type
     self.assertRaises(OverflowError, nd.range, 10, dtype=ndt.bool)
     self.assertEqual(nd.dtype_of(nd.range(10, dtype=ndt.int8)), ndt.int8)
     self.assertEqual(nd.dtype_of(nd.range(10, dtype=ndt.int16)), ndt.int16)
     self.assertEqual(nd.dtype_of(nd.range(10, dtype=ndt.int32)), ndt.int32)
     self.assertEqual(nd.dtype_of(nd.range(10, dtype=ndt.int64)), ndt.int64)
     self.assertEqual(nd.dtype_of(nd.range(10, dtype=ndt.uint8)), ndt.uint8)
     self.assertEqual(nd.dtype_of(nd.range(10, dtype=ndt.uint16)), ndt.uint16)
     self.assertEqual(nd.dtype_of(nd.range(10, dtype=ndt.uint32)), ndt.uint32)
     self.assertEqual(nd.dtype_of(nd.range(10, dtype=ndt.uint64)), ndt.uint64)
     self.assertEqual(nd.dtype_of(nd.range(10, dtype=ndt.float32)), ndt.float32)
     self.assertEqual(nd.dtype_of(nd.range(10, dtype=ndt.float64)), ndt.float64)
     # Maybe in the future add complex support when start.imag == stop.imag
     # and step.imag == 0?
     self.assertRaises(TypeError, nd.range, 10, dtype=ndt.complex_float32)
     self.assertRaises(TypeError, nd.range, 10, dtype=ndt.complex_float64)
     # Float/complex should convert when the dtype is specified
     self.assertEqual(nd.dtype_of(nd.range(10.0, dtype=ndt.uint16)), ndt.uint16)
     self.assertEqual(nd.dtype_of(nd.range(1.0, step=0.5+0j, dtype=ndt.float32)), ndt.float32)
コード例 #22
0
ファイル: test_range_linspace.py プロジェクト: menghaozhu/hat
 def test_simple(self):
     self.assertEqual(nd.as_py(nd.range(10)), list(range(10)))
     self.assertEqual(nd.as_py(nd.range(5, 10)), list(range(5, 10)))
     self.assertEqual(nd.as_py(nd.range(5, 10, 3)), list(range(5, 10, 3)))
     self.assertEqual(nd.as_py(nd.range(10, 5, -1)), list(range(10, 5, -1)))
     self.assertEqual(nd.as_py(nd.range(stop=10, step=2)), list(range(0, 10, 2)))
コード例 #23
0
ファイル: custom_py.py プロジェクト: xsixing/blaze
import blaze
from dynd import nd, ndt

# Available variables from the Blaze loader:
#  `catconf`   Catalog configuration object
#  `impdata`   Import data from the .array file
#  `catpath`   Catalog path of the array
#  `fspath`    Equivalent filesystem path of the array
#  `dshape`    The datashape expected
#
# The loaded array must be placed in `result`.

begin = impdata['begin']
end = impdata['end']
result = blaze.array(nd.range(begin, end))
コード例 #24
0
 def test_nd_array_int(self):
     a = nd.range(3)
     self.assertEqual(nd.as_py(a[a[0]]), 0)
     self.assertEqual(nd.as_py(a[a[0] + 1]), 1)
コード例 #25
0
 def test_nd_array_int(self):
     a = nd.range(3)
     self.assertEqual(nd.as_py(a[a[0]]), 0)
     self.assertEqual(nd.as_py(a[a[0] + 1]), 1)