def test_access_array(self):
     a = nd.array(1)
     self.assertEqual(a.access_flags, 'readwrite')
     a = nd.array(1, access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     a = nd.array(1, access='r')
     self.assertEqual(a.access_flags, 'immutable')
Beispiel #2
0
 def test_dynd(self):
     self.assertEqual(nd.as_py(into(nd.array(), (1, 2, 3))),
                      nd.as_py(nd.array([1, 2, 3])))
     self.assertEqual(into([], nd.array([1, 2])),
                               [1, 2])
     self.assertEqual(into([], nd.array([[1, 2], [3, 4]])),
                               [[1, 2], [3, 4]])
Beispiel #3
0
 def test_array_unicode(self):
     a = nd.array(u"\uc548\ub155")
     b = nd.array([u"\uc548\ub155", u"Hello"])
     self.assertEqual(nd.type_of(a), ndt.string)
     self.assertEqual(nd.dtype_of(b), ndt.string)
     #        self.assertEqual(unicode(a), u"\uc548\ub155")
     self.assertEqual(nd.as_py(b), [u"\uc548\ub155", u"Hello"])
 def test_simple(self):
     a = nd.array([1, 2, 3], access='rw')
     self.assertEqual(nd.type_of(a), ndt.type('3 * int32'))
     # Modifying 'a' shouldn't affect 'b', because it's a copy
     b = nd.array(a)
     a[1] = 10
     self.assertEqual(nd.as_py(b), [1, 2, 3])
Beispiel #5
0
 def test_nonzero_errors(self):
     # Non-scalars raise errors like NumPy, because their
     # truth value is ambiguous
     self.assertRaises(ValueError, bool, nd.array([0]))
     self.assertRaises(ValueError, bool, nd.array([1, 2, 3]))
     self.assertRaises(ValueError, bool,
                       nd.array(['abc', 3], type='{x:string, y:int32}'))
Beispiel #6
0
 def test_arrfunc_from_instantiate_pyfunc(self):
     # Test wrapping make_assignment_ckernel as an arrfunc
     def instantiate_assignment(out_ckb, ckb_offset, dst_tp, dst_arrmeta,
                                src_tp, src_arrmeta, kernreq, ectx):
         out_ckb = _lowlevel.CKernelBuilderStruct.from_address(out_ckb)
         return _lowlevel.make_assignment_ckernel(out_ckb, ckb_offset,
                         dst_tp, dst_arrmeta,
                         src_tp[0], src_arrmeta[0],
                         kernreq, ectx)
     af = _lowlevel.arrfunc_from_instantiate_pyfunc(
                 instantiate_assignment, "(date) -> string")
     self.assertEqual(nd.as_py(af.proto), ndt.type("(date) -> string"))
     in0 = nd.array('2012-11-05', ndt.date)
     out = af(in0)
     self.assertEqual(nd.as_py(out), '2012-11-05')
     # Also test it as a lifted kernel
     af_lifted = _lowlevel.lift_arrfunc(af)
     self.assertEqual(nd.as_py(af_lifted.proto),
                      ndt.type("(Dims... * date) -> Dims... * string"))
     from datetime import date
     in0 = nd.array([['2013-03-11', date(2010, 10, 10)],
                     [date(1999, 12, 31)],
                     []], type='3 * var * date')
     out = af_lifted(in0)
     self.assertEqual(nd.as_py(out),
                     [['2013-03-11', '2010-10-10'],
                      ['1999-12-31'], []])
Beispiel #7
0
    def test_lift_arrfunc(self):
        # First get a ckernel from numpy
        requiregil = False
        af = _lowlevel.arrfunc_from_ufunc(np.ldexp,
                        (np.float64, np.float64, np.int32),
                        requiregil)
        self.assertEqual(nd.as_py(af.proto),
                         ndt.type("(float64, int32) -> float64"))

        # Now lift it
        af_lifted = _lowlevel.lift_arrfunc(af)
        self.assertEqual(nd.as_py(af_lifted.proto),
                         ndt.type("(Dims... * float64, Dims... * int32) -> Dims... * float64"))
        # Create some compatible arguments
        in0 = nd.array([[1, 2, 3], [4, 5], [6], [7,9,10]],
                       type='fixed * var * float64')
        in1 = nd.array([[-1], [10], [100], [-12]], type='fixed * 1 * int32')
        # Instantiate and call the kernel on these arguments
        out = af_lifted(in0, in1)
        # Verify that we got the expected result
        self.assertEqual(nd.as_py(out),
                    [[0.5, 1.0, 1.5],
                     [4096.0, 5120.0],
                     [float(6*2**100)],
                     [0.001708984375, 0.002197265625, 0.00244140625]])
Beispiel #8
0
    def test_dynamic_fromiter_onetype(self):
        # Constructing with an iterator like this uses a dynamic
        # array construction method. In this simple case, we
        # use generators that have a consistent type
        # bool result
        a = nd.array(iter([True, False]))
        self.assertEqual(nd.type_of(a), ndt.type('2 * bool'))
        self.assertEqual(nd.as_py(a), [True, False])
        # int32 result
        a = nd.array(iter([1, 2, True, False]))
        self.assertEqual(nd.type_of(a), ndt.type('4 * int32'))
        self.assertEqual(nd.as_py(a), [1, 2, 1, 0])
        # int64 result
        a = nd.array(iter([10000000000, 1, 2, True, False]))
        self.assertEqual(nd.type_of(a), ndt.type('5 * int64'))
        self.assertEqual(nd.as_py(a), [10000000000, 1, 2, 1, 0])
        # float64 result
        a = nd.array(iter([3.25, 10000000000, 1, 2, True, False]))
        self.assertEqual(nd.type_of(a), ndt.type('6 * float64'))
        self.assertEqual(nd.as_py(a), [3.25, 10000000000, 1, 2, 1, 0])
        # complex[float64] result
        a = nd.array(iter([3.25j, 3.25, 10000000000, 1, 2, True, False]))
        self.assertEqual(nd.type_of(a), ndt.type('7 * complex[float64]'))
        self.assertEqual(nd.as_py(a), [3.25j, 3.25, 10000000000, 1, 2, 1, 0])

        """
    def test_numpy_struct_scalar(self):
        # Create a NumPy struct scalar object, by indexing into
        # a structured array
        a = np.array([(10, 11, 12)], dtype='i4,i8,f8')[0]
        aligned_tp = ndt.type('c{f0: int32, f1: int64, f2: float64}')
        val = {'f0': 10, 'f1': 11, 'f2': 12}

        # Construct using nd.array
        b = nd.array(a)
        self.assertEqual(nd.type_of(b), aligned_tp)
        self.assertEqual(nd.as_py(b), val)
        self.assertEqual(b.access_flags, 'immutable')
        b = nd.array(a, access='rw')
        self.assertEqual(nd.type_of(b), aligned_tp)
        self.assertEqual(nd.as_py(b), val)
        self.assertEqual(b.access_flags, 'readwrite')

        # Construct using nd.asarray
        b = nd.asarray(a)
        self.assertEqual(nd.type_of(b), aligned_tp)
        self.assertEqual(nd.as_py(b), val)
        self.assertEqual(b.access_flags, 'immutable')
        b = nd.asarray(a, access='rw')
        self.assertEqual(nd.type_of(b), aligned_tp)
        self.assertEqual(nd.as_py(b), val)
        self.assertEqual(b.access_flags, 'readwrite')

        # nd.view should fail
        self.assertRaises(RuntimeError, nd.view, a)
    def test_filesystem(self):
        with filetexts(data) as filenames:
            dd = Files(sorted(filenames), CSV, subdshape='var * 2 * int32')

            self.assertEqual(dd.filenames, ['a.csv', 'b.csv', 'c.csv'])
            self.assertEqual(str(dd.schema), '2 * int32')
            self.assertEqual(str(dd.dshape), 'var * 2 * int32')

            expected = [[1, 1], [2, 2], [3, 3], [4, 4], [5, 5], [6, 6], [7, 7]]

            self.assertEqual(dd.as_py(), expected)

            result = dd.as_dynd()
            expected2 = nd.array(expected, dtype='int32')
            self.assertEqual(nd.as_py(result),
                             nd.as_py(expected2))

            self.assertEqual(list(dd), expected)
            self.assertEqual(list(dd), expected)  # Not one use only

            chunks = list(dd.chunks(blen=3))
            expected = [nd.array([[1, 1], [2, 2], [3, 3]], dtype='int32'),
                        nd.array([[4, 4], [5, 5], [6, 6]], dtype='int32')]

            assert all(nd.as_py(a) == nd.as_py(b) for a, b in zip(chunks, expected))
    def test_single_struct_array(self):
        a = nd.array([(0,0), (3,5), (12,10)], dtype='{x:int32, y:int32}')
        self.assertEqual(nd.type_of(a), ndt.type('3 * {x:int32, y:int32}'))
        self.assertEqual(nd.as_py(a.x), [0, 3, 12])
        self.assertEqual(nd.as_py(a.y), [0, 5, 10])

        a = nd.array([{'x':0,'y':0}, {'x':3,'y':5}, {'x':12,'y':10}],
                    dtype='{x:int32, y:int32}')
        self.assertEqual(nd.type_of(a), ndt.type('3 * {x:int32, y:int32}'))
        self.assertEqual(nd.as_py(a.x), [0, 3, 12])
        self.assertEqual(nd.as_py(a.y), [0, 5, 10])

        a = nd.array([[(3, 'X')], [(10, 'L'), (12, 'M')]],
                        dtype='{count:int32, size:fixed_string[1,"A"]}')
        self.assertEqual(nd.type_of(a),
                       ndt.type('2 * var * {count:int32, size:fixed_string[1,"A"]}'))
        self.assertEqual(nd.as_py(a.count), [[3], [10, 12]])
        self.assertEqual(nd.as_py(a.size), [['X'], ['L', 'M']])

        a = nd.array([[{'count':3, 'size':'X'}],
                        [{'count':10, 'size':'L'}, {'count':12, 'size':'M'}]],
                        dtype='{count:int32, size:fixed_string[1,"A"]}')
        self.assertEqual(nd.type_of(a), ndt.type('2 * var * {count:int32, size:fixed_string[1,"A"]}'))
        self.assertEqual(nd.as_py(a.count), [[3], [10, 12]])
        self.assertEqual(nd.as_py(a.size), [['X'], ['L', 'M']])
 def test_dynamic_fromiter_onetype(self):
     # Constructing with an iterator like this uses a dynamic
     # array construction method. In this simple case, we
     # use generators that have a consistent type
     # bool result
     a = nd.array(iter([True, False]))
     self.assertEqual(nd.type_of(a), ndt.type('2 * bool'))
     self.assertEqual(nd.as_py(a), [True, False])
     # int32 result
     a = nd.array(iter([1, 2, True, False]))
     self.assertEqual(nd.type_of(a), ndt.type('4 * int32'))
     self.assertEqual(nd.as_py(a), [1, 2, 1, 0])
     # int64 result
     a = nd.array(iter([10000000000, 1, 2, True, False]))
     self.assertEqual(nd.type_of(a), ndt.type('5 * int64'))
     self.assertEqual(nd.as_py(a), [10000000000, 1, 2, 1, 0])
     # float64 result
     a = nd.array(iter([3.25, 10000000000, 1, 2, True, False]))
     self.assertEqual(nd.type_of(a), ndt.type('6 * float64'))
     self.assertEqual(nd.as_py(a), [3.25, 10000000000, 1, 2, 1, 0])
     # complex[float64] result
     a = nd.array(iter([3.25j, 3.25, 10000000000, 1, 2, True, False]))
     self.assertEqual(nd.type_of(a), ndt.type('7 * complex[float64]'))
     self.assertEqual(nd.as_py(a), [3.25j, 3.25, 10000000000, 1, 2, 1, 0])
     """
 def test_ndt_type_from_h5py_special(self):
     # h5py 2.3 style "special dtype"
     dt = np.dtype(object, metadata={'vlen' : str})
     self.assertEqual(ndt.type(dt), ndt.string)
     if sys.version_info < (3, 0):
         dt = np.dtype(object, metadata={'vlen' : unicode})
         self.assertEqual(ndt.type(dt), ndt.string)
     # h5py 2.2 style "special dtype"
     dt = np.dtype(('O', [( ({'type': str},'vlen'), 'O' )] ))
     self.assertEqual(ndt.type(dt), ndt.string)
     if sys.version_info < (3, 0):
         dt = np.dtype(('O', [( ({'type': unicode},'vlen'), 'O' )] ))
         self.assertEqual(ndt.type(dt), ndt.string)
     # Should be able to roundtrip dynd -> numpy -> dynd
     x = nd.array(['testing', 'one', 'two'])
     self.assertEqual(nd.type_of(x), ndt.type('3 * string'))
     y = nd.as_numpy(x, allow_copy=True)
     self.assertEqual(y.dtype.kind, 'O')
     if sys.version_info < (3, 0):
         self.assertEqual(y.dtype.metadata, {'vlen' : unicode})
     else:
         self.assertEqual(y.dtype.metadata, {'vlen' : str})
     z = nd.array(y)
     self.assertEqual(nd.type_of(z), nd.type_of(x))
     self.assertEqual(nd.as_py(z), nd.as_py(x))
    def test_single_struct(self):
        a = nd.array([12, "test", True], type="{x:int32, y:string, z:bool}")
        self.assertEqual(nd.type_of(a), ndt.type("{x:int32, y:string, z:bool}"))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), "test")
        self.assertEqual(nd.as_py(a[2]), True)

        # With dtype= parameter instead of type=
        a = nd.array([12, "test", True], dtype="{x:int32, y:string, z:bool}")
        self.assertEqual(nd.type_of(a), ndt.type("{x:int32, y:string, z:bool}"))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), "test")
        self.assertEqual(nd.as_py(a[2]), True)

        a = nd.array({"x": 12, "y": "test", "z": True}, type="{x:int32, y:string, z:bool}")
        self.assertEqual(nd.type_of(a), ndt.type("{x:int32, y:string, z:bool}"))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), "test")
        self.assertEqual(nd.as_py(a[2]), True)

        # With dtype= parameter instead of type=
        a = nd.array({"x": 12, "y": "test", "z": True}, dtype="{x:int32, y:string, z:bool}")
        self.assertEqual(nd.type_of(a), ndt.type("{x:int32, y:string, z:bool}"))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), "test")
        self.assertEqual(nd.as_py(a[2]), True)
 def test_access_array_with_type(self):
     a = nd.array(1, type=ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     a = nd.array(1, type=ndt.int32, access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     a = nd.array(1, type=ndt.int32, access='r')
     self.assertEqual(a.access_flags, 'immutable')
    def test_single_struct(self):
        a = nd.array([12, 'test', True], type='{x:int32, y:string, z:bool}')
        self.assertEqual(nd.type_of(a), ndt.type('{x:int32, y:string, z:bool}'))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), 'test')
        self.assertEqual(nd.as_py(a[2]), True)

        # With dtype= parameter instead of type=
        a = nd.array([12, 'test', True], dtype='{x:int32, y:string, z:bool}')
        self.assertEqual(nd.type_of(a), ndt.type('{x:int32, y:string, z:bool}'))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), 'test')
        self.assertEqual(nd.as_py(a[2]), True)

        a = nd.array({'x':12, 'y':'test', 'z':True}, type='{x:int32, y:string, z:bool}')
        self.assertEqual(nd.type_of(a), ndt.type('{x:int32, y:string, z:bool}'))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), 'test')
        self.assertEqual(nd.as_py(a[2]), True)

        # With dtype= parameter instead of type=
        a = nd.array({'x':12, 'y':'test', 'z':True}, dtype='{x:int32, y:string, z:bool}')
        self.assertEqual(nd.type_of(a), ndt.type('{x:int32, y:string, z:bool}'))
        self.assertEqual(nd.as_py(a[0]), 12)
        self.assertEqual(nd.as_py(a[1]), 'test')
        self.assertEqual(nd.as_py(a[2]), True)
 def test__type_from_h5py_special(self):
     # h5py 2.3 style "special dtype"
     dt = np.dtype(object, metadata={'vlen' : str})
     self.assertEqual(ndt.type(dt), ndt.string)
     if sys.version_info < (3, 0):
         dt = np.dtype(object, metadata={'vlen' : unicode})
         self.assertEqual(ndt.type(dt), ndt.string)
     # h5py 2.2 style "special dtype"
     dt = np.dtype(('O', [( ({'type': str},'vlen'), 'O' )] ))
     self.assertEqual(ndt.type(dt), ndt.string)
     if sys.version_info < (3, 0):
         dt = np.dtype(('O', [( ({'type': unicode},'vlen'), 'O' )] ))
         self.assertEqual(ndt.type(dt), ndt.string)
     # Should be able to roundtrip dynd -> numpy -> dynd
     x = nd.array(['testing', 'one', 'two'])
     self.assertEqual(nd.type_of(x), ndt.type('3 * string'))
     y = nd.as_numpy(x, allow_copy=True)
     self.assertEqual(y.shape, (3,))
     self.assertEqual(y[0], 'testing')
     self.assertEqual(y[1], 'one')
     self.assertEqual(y[2], 'two')
     self.assertEqual(y.dtype.kind, 'O')
     if sys.version_info < (3, 0):
         self.assertEqual(y.dtype.metadata, {'vlen' : unicode})
     else:
         self.assertEqual(y.dtype.metadata, {'vlen' : str})
     z = nd.array(y)
     self.assertEqual(nd.type_of(z), nd.type_of(x))
     self.assertEqual(nd.as_py(z), nd.as_py(x))
Beispiel #18
0
 def test_simple(self):
     a = nd.array([1, 2, 3])
     self.assertEqual(nd.type_of(a), ndt.type('3 * int32'))
     # Modifying 'a' shouldn't affect 'b', because it's a copy
     b = nd.array(a)
     a[1] = 10
     self.assertEqual(nd.as_py(b), [1, 2, 3])
 def test_iter(self):
     # Iteration of a 1D array
     a = nd.array([1, 2, 3])
     self.assertEqual([nd.as_py(x) for x in a], [1, 2, 3])
     # Iteration of a 2D array
     a = nd.array([[1, 2, 3], [4,5,6]])
     self.assertEqual([nd.as_py(x) for x in a], [[1, 2, 3], [4,5,6]])
Beispiel #20
0
    def test_array_unicode(self):
        a = nd.array(u"\uc548\ub155")
        b = nd.array([u"\uc548\ub155", u"Hello"])
        self.assertEqual(nd.type_of(a), ndt.string)
        self.assertEqual(nd.dtype_of(b), ndt.string)
#        self.assertEqual(unicode(a), u"\uc548\ub155")
        self.assertEqual(nd.as_py(b), [u"\uc548\ub155", u"Hello"])
Beispiel #21
0
 def test_index(self):
     # Test that the __index__ method/nb_index slot
     # in dynd arrays is working
     a = [1, 2, 3, 4, 5, 6]
     self.assertEqual(a[nd.array(0)], 1)
     self.assertEqual(a[nd.array(1):nd.array(3)], [2, 3])
     self.assertEqual(a[nd.array(-1, type=ndt.int8)], 6)
Beispiel #22
0
    def __init__(self,
                 path,
                 mode='rt',
                 schema=None,
                 columns=None,
                 types=None,
                 typehints=None,
                 dialect=None,
                 header=None,
                 open=open,
                 nrows_discovery=50,
                 chunksize=1024,
                 encoding=DEFAULT_ENCODING,
                 **kwargs):
        if 'r' in mode and not os.path.isfile(path):
            raise ValueError('CSV file "%s" does not exist' % path)

        if schema is None and 'w' in mode:
            raise ValueError('Please specify schema for writable CSV file')

        self.path = path
        self.mode = mode
        self.open = {'gz': gzip.open, 'bz2': bz2.BZ2File}.get(ext(path), open)
        self._abspath = os.path.abspath(path)
        self.chunksize = chunksize
        self.encoding = encoding

        sample = get_sample(self)
        self.dialect = dialect = discover_dialect(sample, dialect, **kwargs)

        if header is None:
            header = has_header(sample, encoding=encoding)
        elif isinstance(header, int):
            header = True
        self.header = header

        if not schema and 'w' not in mode:
            schema = discover_csv(path,
                                  encoding=encoding,
                                  dialect=dialect,
                                  header=self.header,
                                  typehints=typehints,
                                  types=types,
                                  columns=columns,
                                  nrows_discovery=nrows_discovery)
        self._schema = schema
        self.header = header

        if 'w' not in mode:
            try:
                nd.array(list(take(10, self._iter(chunksize=10))),
                         dtype=str(schema))
            except (ValueError, TypeError) as e:
                raise ValueError("Automatic datashape discovery failed\n"
                                 "Discovered the following datashape: %s\n"
                                 "But DyND generated the following error: %s\n"
                                 "Consider providing type hints using "
                                 "typehints={'column-name': 'type'}\n"
                                 "like typehints={'start-time': 'string'}" %
                                 (schema, e.args[0]))
Beispiel #23
0
    def test_lift_ckernel(self):
        # First get a ckernel from numpy
        requiregil = False
        ckd = _lowlevel.ckernel_deferred_from_ufunc(np.ldexp,
                        (np.float64, np.float64, np.int32),
                        requiregil)
        self.assertEqual(nd.as_py(ckd.types),
                        [ndt.float64, ndt.float64, ndt.int32])

        # Now lift it
        ckd_lifted = _lowlevel.lift_ckernel_deferred(ckd,
                        ['var * var * float64', 'strided * var * float64',
                         'strided * 1 * int32'])
        self.assertEqual(nd.as_py(ckd_lifted.types),
                        [ndt.type(x) for x in ['var * var * float64',
                                    'strided * var * float64', 'strided * 1 * int32']])
        # Create some compatible arguments
        out = nd.empty('var * var * float64')
        in0 = nd.array([[1, 2, 3], [4, 5], [6], [7,9,10]], type='strided * var * float64')
        in1 = nd.array([[-1], [10], [100], [-12]], type='strided * 1 * int32')
        # Instantiate and call the kernel on these arguments
        ckd_lifted.__call__(out, in0, in1)
        # Verify that we got the expected result
        self.assertEqual(nd.as_py(out),
                    [[0.5, 1.0, 1.5],
                     [4096.0, 5120.0],
                     [float(6*2**100)],
                     [0.001708984375, 0.002197265625, 0.00244140625]])
Beispiel #24
0
 def test_iter(self):
     # Iteration of a 1D array
     a = nd.array([1, 2, 3])
     self.assertEqual([nd.as_py(x) for x in a], [1, 2, 3])
     # Iteration of a 2D array
     a = nd.array([[1, 2, 3], [4, 5, 6]])
     self.assertEqual([nd.as_py(x) for x in a], [[1, 2, 3], [4, 5, 6]])
 def test_access_array(self):
     a = nd.array(1)
     self.assertEqual(a.access_flags, 'readwrite')
     a = nd.array(1, access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     a = nd.array(1, access='r')
     self.assertEqual(a.access_flags, 'immutable')
 def test_access_array_with_type(self):
     a = nd.array(1, type=ndt.int32)
     self.assertEqual(a.access_flags, 'readwrite')
     a = nd.array(1, type=ndt.int32, access='rw')
     self.assertEqual(a.access_flags, 'readwrite')
     a = nd.array(1, type=ndt.int32, access='r')
     self.assertEqual(a.access_flags, 'immutable')
Beispiel #27
0
 def test_ckernel_deferred_from_pyfunc(self):
     # Test wrapping make_assignment_ckernel as a deferred ckernel
     def instantiate_assignment(out_ckb, ckb_offset, types, meta,
                                kerntype, ectx):
         out_ckb = _lowlevel.CKernelBuilderStruct.from_address(out_ckb)
         return _lowlevel.make_assignment_ckernel(out_ckb, ckb_offset,
                         types[0], meta[0],
                         types[1], meta[1],
                         'expr', kerntype, ectx)
     ckd = _lowlevel.ckernel_deferred_from_pyfunc(instantiate_assignment,
                     [ndt.string, ndt.date])
     self.assertEqual(nd.as_py(ckd.types), [ndt.string, ndt.date])
     out = nd.empty(ndt.string)
     in0 = nd.array('2012-11-05', ndt.date)
     ckd.__call__(out, in0)
     self.assertEqual(nd.as_py(out), '2012-11-05')
     # Also test it as a lifted kernel
     ckd_lifted = _lowlevel.lift_ckernel_deferred(ckd,
                     ['3 * var * string', '3 * var * date'])
     self.assertEqual(nd.as_py(ckd_lifted.types),
                 [ndt.type('3 * var * string'), ndt.type('3 * var * date')])
     out = nd.empty('3 * var * string')
     from datetime import date
     in0 = nd.array([['2013-03-11', date(2010, 10, 10)],
                     [date(1999, 12, 31)],
                     []], type='3 * var * date')
     ckd_lifted.__call__(out, in0)
     self.assertEqual(nd.as_py(out),
                     [['2013-03-11', '2010-10-10'],
                      ['1999-12-31'], []])
 def test_index(self):
     # Test that the __index__ method/nb_index slot
     # in dynd arrays is working
     a = [1, 2, 3, 4, 5, 6]
     self.assertEqual(a[nd.array(0)], 1)
     self.assertEqual(a[nd.array(1):nd.array(3)], [2, 3])
     self.assertEqual(a[nd.array(-1, type=ndt.int8)], 6)
Beispiel #29
0
    def test_nested_struct(self):
        a = nd.array(
            [[1, 2], ['test', 3.5], [3j]],
            type=
            '{x: 2 * int16, y: {a: string, b: float64}, z: 1 * complex[float32]}'
        )
        self.assertEqual(nd.as_py(a.x), [1, 2])
        self.assertEqual(nd.as_py(a.y.a), 'test')
        self.assertEqual(nd.as_py(a.y.b), 3.5)
        self.assertEqual(nd.as_py(a.z), [3j])

        a = nd.array(
            {
                'x': [1, 2],
                'y': {
                    'a': 'test',
                    'b': 3.5
                },
                'z': [3j]
            },
            type=
            '{x: 2 * int16, y: {a: string, b: float64}, z: 1 * complex[float32]}'
        )
        self.assertEqual(nd.as_py(a.x), [1, 2])
        self.assertEqual(nd.as_py(a.y.a), 'test')
        self.assertEqual(nd.as_py(a.y.b), 3.5)
        self.assertEqual(nd.as_py(a.z), [3j])
Beispiel #30
0
 def test03(self):
     """Testing with only dynd arrays"""
     a, b = np.arange(self.N), np.arange(1, self.N+1)
     c = nd.array(a)
     d = nd.array(b)
     cr = blaze._elwise_eval("c * d", vm=self.vm)
     nr = a * b
     assert_array_equal(cr[:], nr, "eval does not work correctly")
Beispiel #31
0
 def test_index_errors(self):
     a = [1, 2, 3, 4, 5, 6]
     self.assertRaises(TypeError, lambda x: a[x],
                       nd.array(True, type=ndt.bool))
     self.assertRaises(TypeError, lambda x: a[x],
                       nd.array(3.5, type=ndt.float64))
     self.assertRaises(IndexError, lambda x: a[x],
                       nd.array(10, type=ndt.int32))
Beispiel #32
0
 def test_empty_array(self):
     # Empty arrays default to int32
     a = nd.array([])
     self.assertEqual(nd.type_of(a), ndt.type('0 * int32'))
     self.assertEqual(a.shape, (0, ))
     a = nd.array([[], [], []])
     self.assertEqual(nd.type_of(a), ndt.type('3 * 0 * int32'))
     self.assertEqual(a.shape, (3, 0))
 def test_empty_array(self):
     # Empty arrays default to int32
     a = nd.array([])
     self.assertEqual(nd.type_of(a), ndt.type('0 * int32'))
     self.assertEqual(a.shape, (0,))
     a = nd.array([[], [], []])
     self.assertEqual(nd.type_of(a), ndt.type('3 * 0 * int32'))
     self.assertEqual(a.shape, (3, 0))
    def test_unicode_array(self):
        a = nd.array([u"this", "is", u"a", "test"], dtype=ndt.string)
        self.assertEqual(nd.type_of(a), ndt.type("strided * string"))
        self.assertEqual(nd.as_py(a), ["this", "is", "a", "test"])

        a = nd.array([u"this", "is", u"a", "test"], dtype='string["U16"]')
        self.assertEqual(nd.type_of(a), ndt.type('strided * string["U16"]'))
        self.assertEqual(nd.as_py(a), ["this", "is", "a", "test"])
    def test_unicode_array(self):
        a = nd.array([u'this', 'is', u'a', 'test'], dtype=ndt.string)
        self.assertEqual(nd.type_of(a), ndt.type('4 * string'))
        self.assertEqual(nd.as_py(a), ['this', 'is', 'a', 'test'])

        a = nd.array([u'this', 'is', u'a', 'test'], dtype='string["U16"]')
        self.assertEqual(nd.type_of(a), ndt.type('4 * string["U16"]'))
        self.assertEqual(nd.as_py(a), ['this', 'is', 'a', 'test'])
Beispiel #36
0
    def test_strftime(self):
        a = nd.array(date(1955,3,13))
        self.assertEqual(nd.as_py(a.strftime('%Y')), '1955')
        self.assertEqual(nd.as_py(a.strftime('%m/%d/%y')), '03/13/55')
        self.assertEqual(nd.as_py(a.strftime('%Y and %j')), '1955 and 072')

        a = nd.array([date(1931,12,12), date(2013,5,14), date(2012,12,25)])
        self.assertEqual(nd.as_py(a.strftime('%Y-%m-%d %j %U %w %W')),
                        ['1931-12-12 346 49 6 49', '2013-05-14 134 19 2 19', '2012-12-25 360 52 2 52'])
Beispiel #37
0
 def test02(self):
     """Testing elwise_eval() with pure dynd arrays and scalars"""
     a = np.arange(self.N*self.M).reshape(self.N, self.M)
     b = np.arange(1, self.N*self.M+1).reshape(self.N, self.M)
     c = nd.array(a)
     d = nd.array(b)
     cr = blaze._elwise_eval("c * d + 2", vm=self.vm)
     nr = a * b + 2
     assert_array_equal(cr[:], nr, "eval does not work correctly")
 def test_iter_fixed_dim(self):
     # Iteration of a 1D array
     a = nd.array([1, 2, 3], type='3 * int64')
     self.assertEqual(len(a), 3)
     self.assertEqual([nd.as_py(x) for x in a], [1, 2, 3])
     # Iteration of a 2D array
     a = nd.array([[1, 2, 3], [4,5,6]], type='2 * 3 * int64')
     self.assertEqual(len(a), 2)
     self.assertEqual([nd.as_py(x) for x in a], [[1, 2, 3], [4,5,6]])
 def test_string(self):
     a = nd.array('abcdef')
     self.assertEqual(nd.type_of(a), ndt.string)
     self.assertEqual(type(nd.as_py(a)), unicode)
     self.assertEqual(nd.as_py(a), u'abcdef')
     a = nd.array(u'abcdef')
     self.assertEqual(nd.type_of(a), ndt.string)
     self.assertEqual(type(nd.as_py(a)), unicode)
     self.assertEqual(nd.as_py(a), u'abcdef')
Beispiel #40
0
    def test_strftime(self):
        a = nd.array(date(1955,3,13))
        self.assertEqual(nd.as_py(a.strftime('%Y')), '1955')
        self.assertEqual(nd.as_py(a.strftime('%m/%d/%y')), '03/13/55')
        self.assertEqual(nd.as_py(a.strftime('%Y and %j')), '1955 and 072')

        a = nd.array([date(1931,12,12), date(2013,5,14), date(2012,12,25)])
        self.assertEqual(nd.as_py(a.strftime('%Y-%m-%d %j %U %w %W')),
                        ['1931-12-12 346 49 6 49', '2013-05-14 134 19 2 19', '2012-12-25 360 52 2 52'])
Beispiel #41
0
 def test_string(self):
     a = nd.array('abcdef')
     self.assertEqual(nd.type_of(a), ndt.string)
     self.assertEqual(type(nd.as_py(a)), unicode)
     self.assertEqual(nd.as_py(a), u'abcdef')
     a = nd.array(u'abcdef')
     self.assertEqual(nd.type_of(a), ndt.string)
     self.assertEqual(type(nd.as_py(a)), unicode)
     self.assertEqual(nd.as_py(a), u'abcdef')
Beispiel #42
0
 def test_iter_fixed_dim(self):
     # Iteration of a 1D array
     a = nd.array([1, 2, 3], type='3 * int64')
     self.assertEqual(len(a), 3)
     self.assertEqual([nd.as_py(x) for x in a], [1, 2, 3])
     # Iteration of a 2D array
     a = nd.array([[1, 2, 3], [4, 5, 6]], type='2 * 3 * int64')
     self.assertEqual(len(a), 2)
     self.assertEqual([nd.as_py(x) for x in a], [[1, 2, 3], [4, 5, 6]])
Beispiel #43
0
    def test_complex_type_realimag(self):
        a = nd.array(1 + 3j)
        self.assertEqual(ndt.complex_float64, nd.type_of(a))
        self.assertEqual(1, nd.as_py(a.real))
        self.assertEqual(3, nd.as_py(a.imag))

        a = nd.array([1 + 2j, 3 + 4j, 5 + 6j])
        self.assertEqual(ndt.type('3 * complex[float64]'), nd.type_of(a))
        self.assertEqual([1, 3, 5], nd.as_py(a.real))
        self.assertEqual([2, 4, 6], nd.as_py(a.imag))
 def test_ragged_fromlistofiter_typepromo(self):
     # list of iterators
     vals = [[True, False], [False, 2, 3], [-10000000000], [True, 10, 3.125, 5.5j]]
     a = nd.array([iter(x) for x in vals])
     self.assertEqual(nd.type_of(a), ndt.type("strided * var * complex[float64]"))
     self.assertEqual(nd.as_py(a), vals)
     # list of list/iterator
     a = nd.array([[1, 2, 3], (1.5 * x for x in range(4)), iter([-1, 1])])
     self.assertEqual(nd.type_of(a), ndt.type("strided * var * float64"))
     self.assertEqual(nd.as_py(a), [[1, 2, 3], [1.5 * x for x in range(4)], [-1, 1]])
Beispiel #45
0
 def test_array_unicode(self):
     a = nd.array(u"\uc548\ub155")
     b = nd.array([u"\uc548\ub155", u"Hello"])
     self.assertEqual(nd.type_of(a), ndt.string)
     self.assertEqual(nd.dtype_of(b), ndt.string)
     self.assertEqual(unicode(a), u"\uc548\ub155")
     self.assertEqual(nd.as_py(b), [u"\uc548\ub155", u"Hello"])
     # In Python 2, 'str' is not unicode
     if sys.version_info < (3, 0):
         self.assertRaises(UnicodeEncodeError, str, a)
 def test_bool(self):
     # Boolean true/false
     a = nd.array(True)
     self.assertEqual(nd.type_of(a), ndt.bool)
     self.assertEqual(type(nd.as_py(a)), bool)
     self.assertEqual(nd.as_py(a), True)
     a = nd.array(False)
     self.assertEqual(nd.type_of(a), ndt.bool)
     self.assertEqual(type(nd.as_py(a)), bool)
     self.assertEqual(nd.as_py(a), False)
Beispiel #47
0
 def test_bool(self):
     # Boolean true/false
     a = nd.array(True)
     self.assertEqual(nd.type_of(a), ndt.bool)
     self.assertEqual(type(nd.as_py(a)), bool)
     self.assertEqual(nd.as_py(a), True)
     a = nd.array(False)
     self.assertEqual(nd.type_of(a), ndt.bool)
     self.assertEqual(type(nd.as_py(a)), bool)
     self.assertEqual(nd.as_py(a), False)
Beispiel #48
0
def blz_descriptor_iter(blzarr):
    if blzarr.ndim > 1:
        for el in blzarr:
            yield DyNDDataDescriptor(nd.array(el))
    else:
        for i in range(len(blzarr)):
            # BLZ doesn't have a convenient way to avoid collapsing
            # to a scalar, this is a way to avoid that
            el = np.array(blzarr[i], dtype=blzarr.dtype)
            yield DyNDDataDescriptor(nd.array(el))
Beispiel #49
0
    def test_complex_type_realimag(self):
        a = nd.array(1 + 3j)
        self.assertEqual(ndt.complex_float64, nd.type_of(a))
        self.assertEqual(1, nd.as_py(a.real))
        self.assertEqual(3, nd.as_py(a.imag))

        a = nd.array([1 + 2j, 3 + 4j, 5 + 6j])
        self.assertEqual(ndt.type('3 * complex[float64]'), nd.type_of(a))
        self.assertEqual([1, 3, 5], nd.as_py(a.real))
        self.assertEqual([2, 4, 6], nd.as_py(a.imag))
    def test_unicode_array(self):
        a = nd.array([u'this', 'is', u'a', 'test'],
                        dtype=ndt.string)
        self.assertEqual(nd.type_of(a), ndt.type('4 * string'))
        self.assertEqual(nd.as_py(a), ['this', 'is', 'a', 'test'])

        a = nd.array([u'this', 'is', u'a', 'test'],
                        dtype='string["U16"]')
        self.assertEqual(nd.type_of(a), ndt.type('4 * string["U16"]'))
        self.assertEqual(nd.as_py(a), ['this', 'is', 'a', 'test'])
Beispiel #51
0
 def __iter__(self):
     f = tb.open_file(self.path, mode='r')
     dset = f.get_node(self.datapath)
     # Get rid of the leading dimension on which we iterate
     dshape = datashape.from_numpy(dset.shape[1:], dset.dtype)
     for el in dset:
         if hasattr(el, "nrow"):
             yield DyND_DDesc(nd.array(el[:], type=str(dshape)))
         else:
             yield DyND_DDesc(nd.array(el, type=str(dshape)))
     dset._v_file.close()
Beispiel #52
0
 def test_squeeze_var(self):
     # Simple var case (squeeze can see into leading size-1 var dims)
     a = nd.array([[[1], [2, 3]]], type='var * var * var * int32')
     self.assertEqual(a.shape, (1, 2, -1))
     self.assertEqual(nd.squeeze(a).shape, (2, -1))
     self.assertEqual(nd.as_py(nd.squeeze(a)), [[1], [2, 3]])
     # With an additional fixed 1 dimension at the end
     a = nd.array([[[[1]], [[2], [3]]]])
     self.assertEqual(a.shape, (1, 2, -1, 1))
     self.assertEqual(nd.squeeze(a).shape, (2, -1))
     self.assertEqual(nd.as_py(nd.squeeze(a)), [[1], [2, 3]])
Beispiel #53
0
 def test_inf(self):
     # Validate nd.inf
     self.assertEqual(nd.inf * 2, nd.inf)
     self.assertTrue(nd.inf > 0)
     self.assertTrue(-nd.inf < 0)
     # as an array
     a = nd.array(nd.inf)
     self.assertEqual(nd.as_py(a), nd.inf)
     self.assertEqual(nd.type_of(a), ndt.float64)
     a = nd.array(nd.inf, type=ndt.float32)
     self.assertEqual(nd.as_py(a), nd.inf)
    def test_numpy_blockref_string(self):
        # Blockref strings don't have a corresponding Numpy construct
        # Therefore numpy makes an object array scalar out of them.
        a = nd.array("abcdef")
        self.assertEqual(nd.dtype_of(a), ndt.string)
        # Some versions of NumPy produce an error instead,
        # so this assertion is removed
        #self.assertEqual(np.asarray(a).dtype, np.dtype(object))

        a = nd.array(u"abcdef \uc548\ub155")
        self.assertEqual(nd.dtype_of(a), ndt.string)
    def test_float64(self):
        lst = [0, 100.0, 1e25, -1000000000]
        a = nd.array(lst)
        self.assertEqual(nd.dtype_of(a), ndt.float64)
        self.assertEqual(a.shape, (4, ))
        self.assertEqual(nd.as_py(a), lst)

        lst = [[100, 103, -20], [-30, 0.0125, 1000000000000]]
        a = nd.array(lst)
        self.assertEqual(nd.dtype_of(a), ndt.float64)
        self.assertEqual(a.shape, (2, 3))
        self.assertEqual(nd.as_py(a), lst)
    def test_bool(self):
        lst = [True, False, True, True]
        a = nd.array(lst)
        self.assertEqual(nd.dtype_of(a), ndt.bool)
        self.assertEqual(a.shape, (4, ))
        self.assertEqual(nd.as_py(a), lst)

        lst = [[True, True], [False, False], [True, False]]
        a = nd.array(lst)
        self.assertEqual(nd.dtype_of(a), ndt.bool)
        self.assertEqual(a.shape, (3, 2))
        self.assertEqual(nd.as_py(a), lst)
 def test_ragged_fromlistofiter_typepromo(self):
     # list of iterators
     vals = [[True, False], [False, 2, 3], [-10000000000],
             [True, 10, 3.125, 5.5j]]
     a = nd.array([iter(x) for x in vals])
     self.assertEqual(nd.type_of(a), ndt.type('4 * var * complex[float64]'))
     self.assertEqual(nd.as_py(a), vals)
     # list of list/iterator
     a = nd.array([[1, 2, 3], (1.5 * x for x in range(4)), iter([-1, 1])])
     self.assertEqual(nd.type_of(a), ndt.type('3 * var * float64'))
     self.assertEqual(nd.as_py(a),
                      [[1, 2, 3], [1.5 * x for x in range(4)], [-1, 1]])