def test_specified_dtype(self):
     # Linspace only supports real-valued outputs
     self.assertRaises(RuntimeError, nd.old_linspace, 0, 1, dtype=ndt.bool)
     self.assertRaises(RuntimeError, nd.old_linspace, 0, 1, dtype=ndt.int8)
     self.assertRaises(RuntimeError, nd.old_linspace, 0, 1, dtype=ndt.int16)
     self.assertRaises(RuntimeError, nd.old_linspace, 0, 1, dtype=ndt.int32)
     self.assertRaises(RuntimeError, nd.old_linspace, 0, 1, dtype=ndt.int64)
     self.assertRaises(RuntimeError, nd.old_linspace, 0, 1, dtype=ndt.uint8)
     self.assertRaises(RuntimeError,
                       nd.old_linspace,
                       0,
                       1,
                       dtype=ndt.uint16)
     self.assertRaises(RuntimeError,
                       nd.old_linspace,
                       0,
                       1,
                       dtype=ndt.uint32)
     self.assertRaises(RuntimeError,
                       nd.old_linspace,
                       0,
                       1,
                       dtype=ndt.uint64)
     # Should obey the float/complex type requests
     self.assertEqual(nd.dtype_of(nd.old_linspace(0, 1, dtype=ndt.float32)),
                      ndt.float32)
     self.assertEqual(nd.dtype_of(nd.old_linspace(0, 1, dtype=ndt.float64)),
                      ndt.float64)
     self.assertEqual(
         nd.dtype_of(nd.old_linspace(0, 1, dtype=ndt.complex_float32)),
         ndt.complex_float32)
     self.assertEqual(
         nd.dtype_of(nd.old_linspace(0, 1, dtype=ndt.complex_float64)),
         ndt.complex_float64)
    def test_dynd_view_of_numpy_array(self):
        # Tests viewing a numpy array as a dynd.array
        nonnative = self.nonnative

        a = np.arange(10, dtype=np.int32)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n), ndt.int32)
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)

        a = np.arange(12, dtype=(nonnative + 'i4')).reshape(3,4)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n), ndt.make_byteswap(ndt.int32))
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)

        a = np.arange(49, dtype='i1')
        a = a[1:].view(dtype=np.int32).reshape(4,3)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n), ndt.make_unaligned(ndt.int32))
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)

        a = np.arange(49, dtype='i1')
        a = a[1:].view(dtype=(nonnative + 'i4')).reshape(2,2,3)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n),
                ndt.make_unaligned(ndt.make_byteswap(ndt.int32)))
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)
    def test_dynd_view_of_numpy_array(self):
        # Tests viewing a numpy array as a dynd.array
        nonnative = self.nonnative

        a = np.arange(10, dtype=np.int32)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n), ndt.int32)
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)

        """
        a = np.arange(12, dtype=(nonnative + 'i4')).reshape(3,4)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n), ndt.make_byteswap(ndt.int32))
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)
        """

        a = np.arange(49, dtype='i1')
        a = a[1:].view(dtype=np.int32).reshape(4,3)
        n = nd.view(a)
        self.assertEqual(nd.dtype_of(n), ndt.make_unaligned(ndt.int32))
        self.assertEqual(nd.ndim_of(n), a.ndim)
        self.assertEqual(n.shape, a.shape)
        self.assertEqual(n.strides, a.strides)

        """
Example #4
0
 def test_simple(self):
     a = nd.array([(1, 2, 'a', 'b'), (3, 4, 'ab', 'cd'),
                   (5, 6, 'def', 'ghi')],
                  dtype='{x: int32, y: int32, z: string, w: string}')
     # Selecting a single field
     b = nd.fields(a, 'x')
     self.assertEqual(nd.dtype_of(b), ndt.make_struct([ndt.int32], ['x']))
     self.assertEqual(nd.as_py(b.x), nd.as_py(a.x))
     # Selecting two fields
     b = nd.fields(a, 'z', 'y')
     self.assertEqual(nd.dtype_of(b),
                      ndt.make_struct([ndt.string, ndt.int32], ['z', 'y']))
     self.assertEqual(nd.as_py(b.z), nd.as_py(a.z))
     self.assertEqual(nd.as_py(b.y), nd.as_py(a.y))
     # Selecting three fields
     b = nd.fields(a, 'w', 'y', 'z')
     self.assertEqual(
         nd.dtype_of(b),
         ndt.make_struct([ndt.string, ndt.int32, ndt.string],
                         ['w', 'y', 'z']))
     self.assertEqual(nd.as_py(b.w), nd.as_py(a.w))
     self.assertEqual(nd.as_py(b.y), nd.as_py(a.y))
     self.assertEqual(nd.as_py(b.z), nd.as_py(a.z))
     # Reordering all four fields
     b = nd.fields(a, 'w', 'y', 'x', 'z')
     self.assertEqual(
         nd.dtype_of(b),
         ndt.make_struct([ndt.string, ndt.int32, ndt.int32, ndt.string],
                         ['w', 'y', 'x', 'z']))
     self.assertEqual(nd.as_py(b.w), nd.as_py(a.w))
     self.assertEqual(nd.as_py(b.y), nd.as_py(a.y))
     self.assertEqual(nd.as_py(b.x), nd.as_py(a.x))
     self.assertEqual(nd.as_py(b.z), nd.as_py(a.z))
Example #5
0
 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)
Example #6
0
 def test_default_dtype(self):
     # Defaults to float64 when given ints
     self.assertEqual(nd.dtype_of(nd.linspace(0, 1)), ndt.float64)
     # Gives float64 when given floats
     self.assertEqual(nd.dtype_of(nd.linspace(0, 1.0)), ndt.float64)
     self.assertEqual(nd.dtype_of(nd.linspace(0.0, 1)), ndt.float64)
     # Gives complex[float64] when given complex
     self.assertEqual(nd.dtype_of(nd.linspace(1.0, 1.0j)), ndt.complex_float64)
     self.assertEqual(nd.dtype_of(nd.linspace(0.0j, 1.0)), ndt.complex_float64)
Example #7
0
 def test_date_properties(self):
     a = nd.array(date(1955,3,13))
     self.assertEqual(str(a), '1955-03-13')
     self.assertEqual(nd.dtype_of(a.year.eval()), ndt.int32)
     self.assertEqual(nd.dtype_of(a.month.eval()), ndt.int32)
     self.assertEqual(nd.dtype_of(a.day.eval()), ndt.int32)
     self.assertEqual(nd.as_py(a.year), 1955)
     self.assertEqual(nd.as_py(a.month), 3)
     self.assertEqual(nd.as_py(a.day), 13)
Example #8
0
 def test_date_properties(self):
     a = nd.array(date(1955, 3, 13))
     self.assertEqual(str(a), '1955-03-13')
     self.assertEqual(nd.dtype_of(a.year.eval()), ndt.int32)
     self.assertEqual(nd.dtype_of(a.month.eval()), ndt.int32)
     self.assertEqual(nd.dtype_of(a.day.eval()), ndt.int32)
     self.assertEqual(nd.as_py(a.year), 1955)
     self.assertEqual(nd.as_py(a.month), 3)
     self.assertEqual(nd.as_py(a.day), 13)
     self.assertEqual(nd.as_py(a), date(1955, 3, 13))
    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_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)
Example #11
0
    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)
Example #12
0
    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)
Example #13
0
    def test_int64(self):
        lst = [0, 100, 20000000000, -1000000000]
        a = nd.array(lst)
        self.assertEqual(nd.dtype_of(a), ndt.int64)
        self.assertEqual(a.shape, (4,))
        self.assertEqual(nd.as_py(a), lst)

        lst = [[100, 103, -20], [-30, 0, 1000000000000]]
        a = nd.array(lst)
        self.assertEqual(nd.dtype_of(a), ndt.int64)
        self.assertEqual(a.shape, (2,3))
        self.assertEqual(nd.as_py(a), lst)
Example #14
0
    def test_float64(self):
        lst = [0, 100.0, 1e25, -1000000000+3j]
        a = nd.array(lst)
        self.assertEqual(nd.dtype_of(a), ndt.complex_float64)
        self.assertEqual(a.shape, (4,))
        self.assertEqual(nd.as_py(a), lst)

        lst = [[100, 103j, -20], [-30, 0.0125, 1000000000000]]
        a = nd.array(lst)
        self.assertEqual(nd.dtype_of(a), ndt.complex_float64)
        self.assertEqual(a.shape, (2,3))
        self.assertEqual(nd.as_py(a), lst)
Example #15
0
    def test_int64(self):
        lst = [0, 100, 20000000000, -1000000000]
        a = nd.array(lst)
        self.assertEqual(nd.dtype_of(a), ndt.int64)
        self.assertEqual(a.shape, (4, ))
        self.assertEqual(nd.as_py(a), lst)

        lst = [[100, 103, -20], [-30, 0, 1000000000000]]
        a = nd.array(lst)
        self.assertEqual(nd.dtype_of(a), ndt.int64)
        self.assertEqual(a.shape, (2, 3))
        self.assertEqual(nd.as_py(a), lst)
Example #16
0
    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)
Example #17
0
 def test_time_properties(self):
     a = nd.array(time(14, 25, 59, 123456))
     self.assertEqual(str(a), '14:25:59.123456')
     self.assertEqual(nd.dtype_of(a.hour.eval()), ndt.int32)
     self.assertEqual(nd.dtype_of(a.minute.eval()), ndt.int32)
     self.assertEqual(nd.dtype_of(a.second.eval()), ndt.int32)
     self.assertEqual(nd.dtype_of(a.microsecond.eval()), ndt.int32)
     self.assertEqual(nd.dtype_of(a.tick.eval()), ndt.int32)
     self.assertEqual(nd.as_py(a.hour), 14)
     self.assertEqual(nd.as_py(a.minute), 25)
     self.assertEqual(nd.as_py(a.second), 59)
     self.assertEqual(nd.as_py(a.microsecond), 123456)
     self.assertEqual(nd.as_py(a.tick), 1234560)
     self.assertEqual(nd.as_py(a), time(14, 25, 59, 123456))
Example #18
0
 def test_time_properties(self):
     a = nd.array(time(14, 25, 59, 123456))
     self.assertEqual(str(a), '14:25:59.123456')
     self.assertEqual(nd.dtype_of(a.hour.eval()), ndt.int32)
     self.assertEqual(nd.dtype_of(a.minute.eval()), ndt.int32)
     self.assertEqual(nd.dtype_of(a.second.eval()), ndt.int32)
     self.assertEqual(nd.dtype_of(a.microsecond.eval()), ndt.int32)
     self.assertEqual(nd.dtype_of(a.tick.eval()), ndt.int32)
     self.assertEqual(nd.as_py(a.hour), 14)
     self.assertEqual(nd.as_py(a.minute), 25)
     self.assertEqual(nd.as_py(a.second), 59)
     self.assertEqual(nd.as_py(a.microsecond), 123456)
     self.assertEqual(nd.as_py(a.tick), 1234560)
     self.assertEqual(nd.as_py(a), time(14, 25, 59, 123456))
Example #19
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"])
Example #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"])
Example #21
0
 def test_specified_dtype(self):
     # Linspace only supports real-valued outputs
     self.assertRaises(RuntimeError, nd.linspace, 0, 1, dtype=ndt.bool)
     self.assertRaises(RuntimeError, nd.linspace, 0, 1, dtype=ndt.int8)
     self.assertRaises(RuntimeError, nd.linspace, 0, 1, dtype=ndt.int16)
     self.assertRaises(RuntimeError, nd.linspace, 0, 1, dtype=ndt.int32)
     self.assertRaises(RuntimeError, nd.linspace, 0, 1, dtype=ndt.int64)
     self.assertRaises(RuntimeError, nd.linspace, 0, 1, dtype=ndt.uint8)
     self.assertRaises(RuntimeError, nd.linspace, 0, 1, dtype=ndt.uint16)
     self.assertRaises(RuntimeError, nd.linspace, 0, 1, dtype=ndt.uint32)
     self.assertRaises(RuntimeError, nd.linspace, 0, 1, dtype=ndt.uint64)
     # Should obey the float/complex type requests
     self.assertEqual(nd.dtype_of(nd.linspace(0, 1, dtype=ndt.float32)), ndt.float32)
     self.assertEqual(nd.dtype_of(nd.linspace(0, 1, dtype=ndt.float64)), ndt.float64)
     self.assertEqual(nd.dtype_of(nd.linspace(0, 1, dtype=ndt.complex_float32)), ndt.complex_float32)
     self.assertEqual(nd.dtype_of(nd.linspace(0, 1, dtype=ndt.complex_float64)), ndt.complex_float64)
Example #22
0
 def test_date(self):
     lst = [date(2011, 3, 15), date(1933, 12, 25), date(1979, 3, 22)]
     lststr = ['2011-03-15', '1933-12-25', '1979-03-22']
     a = nd.array(lst)
     self.assertEqual(nd.dtype_of(a), ndt.date)
     self.assertEqual(a.shape, (3, ))
     self.assertEqual(nd.as_py(a), lst)
     self.assertEqual(nd.as_py(a.ucast(ndt.string)), lststr)
Example #23
0
 def test_date(self):
     lst = [date(2011, 3, 15), date(1933, 12, 25), date(1979, 3, 22)]
     lststr = ['2011-03-15', '1933-12-25', '1979-03-22']
     a = nd.array(lst)
     self.assertEqual(nd.dtype_of(a), ndt.date)
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a), lst)
     self.assertEqual(nd.as_py(a.ucast(ndt.string)), lststr)
Example #24
0
    def test_struct_function(self):
        import os

        a = nd.array(date(1955, 3, 13))
        s = a.to_struct.eval()
        self.assertEqual(nd.dtype_of(s), ndt.make_struct([ndt.int16, ndt.int8, ndt.int8], ["year", "month", "day"]))
        self.assertEqual(nd.as_py(s.year), 1955)
        self.assertEqual(nd.as_py(s.month), 3)
        self.assertEqual(nd.as_py(s.day), 13)
Example #25
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)
Example #26
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)
Example #27
0
 def test_struct_function(self):
     a = nd.array(date(1955, 3, 13))
     s = a.to_struct().eval()
     self.assertEqual(
         nd.dtype_of(s),
         ndt.make_struct([ndt.int16, ndt.int8, ndt.int8],
                         ['year', 'month', 'day']))
     self.assertEqual(nd.as_py(s.year), 1955)
     self.assertEqual(nd.as_py(s.month), 3)
     self.assertEqual(nd.as_py(s.day), 13)
Example #28
0
 def test_struct_function(self):
     a = nd.array(date(1955,3,13))
     s = a.to_struct().eval()
     self.assertEqual(nd.dtype_of(s),
                     ndt.make_cstruct(
                         [ndt.int32, ndt.int16, ndt.int16],
                         ['year', 'month', 'day']))
     self.assertEqual(nd.as_py(s.year), 1955)
     self.assertEqual(nd.as_py(s.month), 3)
     self.assertEqual(nd.as_py(s.day), 13)
Example #29
0
 def test_struct_function(self):
     a = nd.array(time(13, 25, 8, 765432))
     s = a.to_struct().eval()
     self.assertEqual(
         nd.dtype_of(s),
         ndt.make_struct([ndt.int8, ndt.int8, ndt.int8, ndt.int32],
                         ['hour', 'minute', 'second', 'tick']))
     self.assertEqual(nd.as_py(s.hour), 13)
     self.assertEqual(nd.as_py(s.minute), 25)
     self.assertEqual(nd.as_py(s.second), 8)
     self.assertEqual(nd.as_py(s.tick), 7654320)
Example #30
0
 def test_struct_function(self):
     a = nd.array(time(13, 25, 8, 765432))
     s = a.to_struct.eval()
     self.assertEqual(
         nd.dtype_of(s),
         ndt.make_struct([ndt.int8, ndt.int8, ndt.int8, ndt.int32], ["hour", "minute", "second", "tick"]),
     )
     self.assertEqual(nd.as_py(s.hour), 13)
     self.assertEqual(nd.as_py(s.minute), 25)
     self.assertEqual(nd.as_py(s.second), 8)
     self.assertEqual(nd.as_py(s.tick), 7654320)
Example #31
0
 def test_struct_function(self):
     a = nd.array(time(13, 25, 8, 765432))
     s = a.to_struct().eval()
     self.assertEqual(nd.dtype_of(s),
                     ndt.make_cstruct(
                         [ndt.int8, ndt.int8, ndt.int8, ndt.int32],
                         ['hour', 'minute', 'second', 'tick']))
     self.assertEqual(nd.as_py(s.hour), 13)
     self.assertEqual(nd.as_py(s.minute), 25)
     self.assertEqual(nd.as_py(s.second), 8)
     self.assertEqual(nd.as_py(s.tick), 7654320)
Example #32
0
 def test_datetime(self):
     lst = [datetime(2011, 3, 15, 3, 15, 12, 123456),
            datetime(1933, 12, 25),
            datetime(1979, 3, 22, 14, 30)]
     lststr = ['2011-03-15T03:15:12.123456',
               '1933-12-25T00:00:00.000000',
               '1979-03-22T14:30:00.000000']
     a = nd.array(lst)
     self.assertEqual(nd.dtype_of(a), ndt.type('datetime["usec"]'))
     self.assertEqual(a.shape, (3,))
     self.assertEqual(nd.as_py(a), lst)
     self.assertEqual(nd.as_py(a.ucast(ndt.string)), lststr)
Example #33
0
 def test_bytes_string(self):
     if sys.version_info >= (3, 0):
         a = nd.array(b"Testing 1 2 3")
         b = nd.array([b"First", b"Second"])
     else:
         # In Python 2, str and bytes are the same,
         # so we have to manually request a bytes type
         a = nd.array(b"Testing 1 2 3", type=ndt.bytes)
         b = nd.array([b"First", b"Second"], dtype=ndt.bytes)
     self.assertEqual(nd.type_of(a), ndt.bytes)
     self.assertEqual(nd.dtype_of(b), ndt.bytes)
     self.assertEqual(nd.as_py(a), b"Testing 1 2 3")
     self.assertEqual(nd.as_py(b), [b"First", b"Second"])
Example #34
0
 def test_bytes_string(self):
     if sys.version_info >= (3, 0):
         a = nd.array(b"Testing 1 2 3")
         b = nd.array([b"First", b"Second"])
     else:
         # In Python 2, str and bytes are the same,
         # so we have to manually request a bytes type
         a = nd.array(b"Testing 1 2 3", type=ndt.bytes)
         b = nd.array([b"First", b"Second"], dtype=ndt.bytes)
     self.assertEqual(nd.type_of(a), ndt.bytes)
     self.assertEqual(nd.dtype_of(b), ndt.bytes)
     self.assertEqual(nd.as_py(a), b"Testing 1 2 3")
     self.assertEqual(nd.as_py(b), [b"First", b"Second"])
Example #35
0
    def test_numpy_dynd_fixed_string_interop(self):
        # Tests converting fixed-size string arrays to/from numpy
        # ASCII Numpy -> dynd
        a = np.array(['abc', 'testing', 'array'])
        b = nd.view(a)
        if sys.version_info >= (3, 0):
            self.assertEqual(ndt.make_fixed_string(7, 'utf_32'), nd.dtype_of(b))
        else:
            self.assertEqual(ndt.make_fixed_string(7, 'ascii'), nd.dtype_of(b))
        self.assertEqual(nd.dtype_of(b), ndt.type(a.dtype))

        # Make sure it's ascii
        a = a.astype('S7')
        b = nd.view(a)

        # ASCII dynd -> Numpy
        c = np.asarray(b)
        self.assertEqual(a.dtype, c.dtype)
        assert_array_equal(a, c)
        # verify 'a' and 'c' are looking at the same data
        a[1] = 'modify'
        assert_array_equal(a, c)

        # ASCII dynd -> UTF32 dynd
        b_u = b.cast(ndt.make_fixed_dim(3, ndt.make_fixed_string(7, 'utf_32')))
        # Evaluate to its value array
        b_u = b_u
        self.assertEqual(
                ndt.make_fixed_string(7, 'utf_32'),
                nd.dtype_of(b_u))

        # UTF32 dynd -> Numpy
        c_u = np.asarray(b_u)
        self.assertEqual(nd.dtype_of(b_u), ndt.type(c_u.dtype))
        assert_array_equal(a.astype('U'), c_u)
        # 'a' and 'c_u' are not looking at the same data
        a[1] = 'diff'
        self.assertFalse(np.all(a == c_u))
Example #36
0
 def test_bytes_string(self):
     if sys.version_info >= (3, 0):
         return
         # This needs to be fixed for Python 3
         a = nd.array(b"Testing 1 2 3")
         b = nd.array([b"First", b"Second"])
     else:
         # In Python 2, str and bytes are the same,
         # so we have to manually request a bytes type
         a = nd.array(b"Testing 1 2 3", type=ndt.bytes)
         b = nd.array([b"First", b"Second"], type=ndt.make_fixed_dim(2, ndt.bytes))
     self.assertEqual(nd.type_of(a), ndt.bytes)
     self.assertEqual(nd.dtype_of(b), ndt.bytes)
     self.assertEqual(nd.as_py(a), b"Testing 1 2 3")
     self.assertEqual(nd.as_py(b), [b"First", b"Second"])
Example #37
0
 def test_simple(self):
     a = nd.array([
             (1, 2, 'a', 'b'),
             (3, 4, 'ab', 'cd'),
             (5, 6, 'def', 'ghi')],
             type='3 * {x: int32, y: int32, z: string, w: string}')
     # Selecting a single field
     b = nd.fields(a, 'x')
     self.assertEqual(nd.dtype_of(b), ndt.make_struct(
                     [ndt.int32],
                     ['x']))
     self.assertEqual(nd.as_py(b.x), nd.as_py(a.x))
     # Selecting two fields
     b = nd.fields(a, 'z', 'y')
     self.assertEqual(nd.dtype_of(b), ndt.make_struct(
                     [ndt.string, ndt.int32],
                     ['z', 'y']))
     self.assertEqual(nd.as_py(b.z), nd.as_py(a.z))
     self.assertEqual(nd.as_py(b.y), nd.as_py(a.y))
     # Selecting three fields
     b = nd.fields(a, 'w', 'y', 'z')
     self.assertEqual(nd.dtype_of(b), ndt.make_struct(
                     [ndt.string, ndt.int32, ndt.string],
                     ['w', 'y', 'z']))
     self.assertEqual(nd.as_py(b.w), nd.as_py(a.w))
     self.assertEqual(nd.as_py(b.y), nd.as_py(a.y))
     self.assertEqual(nd.as_py(b.z), nd.as_py(a.z))
     # Reordering all four fields
     b = nd.fields(a, 'w', 'y', 'x', 'z')
     self.assertEqual(nd.dtype_of(b), ndt.make_struct(
                     [ndt.string, ndt.int32, ndt.int32, ndt.string],
                     ['w', 'y', 'x', 'z']))
     self.assertEqual(nd.as_py(b.w), nd.as_py(a.w))
     self.assertEqual(nd.as_py(b.y), nd.as_py(a.y))
     self.assertEqual(nd.as_py(b.x), nd.as_py(a.x))
     self.assertEqual(nd.as_py(b.z), nd.as_py(a.z))
Example #38
0
 def test_datetime(self):
     lst = [
         datetime(2011, 3, 15, 3, 15, 12, 123456),
         datetime(1933, 12, 25),
         datetime(1979, 3, 22, 14, 30)
     ]
     lststr = [
         '2011-03-15T03:15:12.123456', '1933-12-25T00:00',
         '1979-03-22T14:30'
     ]
     a = nd.array(lst)
     self.assertEqual(nd.dtype_of(a), ndt.type('datetime'))
     self.assertEqual(a.shape, (3, ))
     self.assertEqual(nd.as_py(a), lst)
     self.assertEqual(nd.as_py(a.ucast(ndt.string)), lststr)
Example #39
0
def render_dynd_datashape_recursive(base_url, arr, indent):
    result = ''
    if isinstance(arr, ndt.type):
        dt = arr.value_type
    else:
        dt = nd.type_of(arr).value_type

    if dt.kind == 'struct':
        result += '{' + json_comment(base_url)
        for farr, fname in zip(dt.field_types, dt.field_names):
            farr = nd.as_py(farr)
            child_url = base_url + '.' + str(fname)
            child_result = render_dynd_datashape_recursive(child_url,
                            farr, indent + '  ')
            result += (indent + '  ' +
                '<a href="' + child_url + '">' + str(fname) + '</a>'
                ': ' + child_result + ';')
            dt = nd.dtype_of(farr) if isinstance(farr, nd.array) else farr
            if dt.kind != 'struct':
                result += json_comment(child_url)
            else:
                result += '\n'
        result += (indent + '}')
    elif dt.kind == 'uniform_dim':
        if dt.type_id in ['strided_dim', 'var_dim']:
            if isinstance(arr, ndt.type):
                result += 'var, '
            else:
                result += ('%d, ' % len(arr))
        elif dt.type_id == 'fixed_dim':
            result += ('%d, ' % dt.fixed_dim_size)
        else:
            raise TypeError('Unrecognized DyND uniform array type ' + str(dt))
        # Descend to the element type
        if isinstance(arr, ndt.type):
            arr = arr.element_type
        elif len(arr) == 1:
            # If there's only one element in the array, can
            # keep using the array sizes in the datashape
            arr = arr[0]
        else:
            arr = nd.type_of(arr).element_type
        result += render_dynd_datashape_recursive(base_url, arr, indent)
    elif dt.kind in ['bool', 'int', 'uint', 'real', 'datetime', 'json', 'string']:
        result += str(dt.dshape)
    else:
        raise TypeError('Unrecognized DyND type ' + str(dt))
    return result
Example #40
0
 def test_bytes_string(self):
     if sys.version_info >= (3, 0):
         return
         # This needs to be fixed for Python 3
         a = nd.array(b"Testing 1 2 3")
         b = nd.array([b"First", b"Second"])
     else:
         # In Python 2, str and bytes are the same,
         # so we have to manually request a bytes type
         a = nd.array(b"Testing 1 2 3", type=ndt.bytes)
         b = nd.array([b"First", b"Second"],
                      type=ndt.make_fixed_dim(2, ndt.bytes))
     self.assertEqual(nd.type_of(a), ndt.bytes)
     self.assertEqual(nd.dtype_of(b), ndt.bytes)
     self.assertEqual(nd.as_py(a), b"Testing 1 2 3")
     self.assertEqual(nd.as_py(b), [b"First", b"Second"])
    def test_numpy_dynd_fixed_string_interop(self):
        # Tests converting fixed-size string arrays to/from numpy
        # ASCII Numpy -> dynd
        a = np.array(['abc', 'testing', 'array'])
        b = nd.view(a)
        if sys.version_info >= (3, 0):
            self.assertEqual(ndt.make_fixed_string(7, 'utf_32'), nd.dtype_of(b))
        else:
            self.assertEqual(ndt.make_fixed_string(7, 'ascii'), nd.dtype_of(b))
        self.assertEqual(nd.dtype_of(b), ndt.type(a.dtype))

        # Make sure it's ascii
        a = a.astype('S7')
        b = nd.view(a)

        # ASCII dynd -> Numpy
        c = np.asarray(b)
        self.assertEqual(a.dtype, c.dtype)
        assert_array_equal(a, c)
        # verify 'a' and 'c' are looking at the same data
        a[1] = 'modify'
        assert_array_equal(a, c)

        # ASCII dynd -> UTF32 dynd
        b_u = b.ucast(ndt.make_fixed_string(7, 'utf_32'))
        self.assertEqual(
                ndt.make_convert(
                    ndt.make_fixed_string(7, 'utf_32'),
                    ndt.make_fixed_string(7, 'ascii')),
                nd.dtype_of(b_u))
        # Evaluate to its value array
        b_u = b_u.eval()
        self.assertEqual(
                ndt.make_fixed_string(7, 'utf_32'),
                nd.dtype_of(b_u))

        # UTF32 dynd -> Numpy
        c_u = np.asarray(b_u)
        self.assertEqual(nd.dtype_of(b_u), ndt.type(c_u.dtype))
        assert_array_equal(a.astype('U'), c_u)
        # 'a' and 'c_u' are not looking at the same data
        a[1] = 'diff'
        self.assertFalse(np.all(a == c_u))
 def test_numpy_scalar_conversion_dtypes(self):
     self.assertEqual(nd.dtype_of(nd.array(np.bool_(True))), ndt.bool)
     self.assertEqual(nd.dtype_of(nd.array(np.bool(True))), ndt.bool)
     self.assertEqual(nd.dtype_of(nd.array(np.int8(100))), ndt.int8)
     self.assertEqual(nd.dtype_of(nd.array(np.int16(100))), ndt.int16)
     self.assertEqual(nd.dtype_of(nd.array(np.int32(100))), ndt.int32)
     self.assertEqual(nd.dtype_of(nd.array(np.int64(100))), ndt.int64)
     self.assertEqual(nd.dtype_of(nd.array(np.uint8(100))), ndt.uint8)
     self.assertEqual(nd.dtype_of(nd.array(np.uint16(100))), ndt.uint16)
     self.assertEqual(nd.dtype_of(nd.array(np.uint32(100))), ndt.uint32)
     self.assertEqual(nd.dtype_of(nd.array(np.uint64(100))), ndt.uint64)
     self.assertEqual(nd.dtype_of(nd.array(np.float32(100.))), ndt.float32)
     self.assertEqual(nd.dtype_of(nd.array(np.float64(100.))), ndt.float64)
     self.assertEqual(nd.dtype_of(nd.array(np.complex64(100j))),
                      ndt.complex_float32)
     self.assertEqual(nd.dtype_of(nd.array(np.complex128(100j))),
                      ndt.complex_float64)
     if np.__version__ >= '1.7':
         self.assertEqual(nd.dtype_of(nd.array(np.datetime64('2000-12-13'))),
                          ndt.date)
         self.assertEqual(nd.dtype_of(nd.array(np.datetime64('2000-12-13T12:30'))),
                          ndt.type('datetime[tz="UTC"]'))
 def test_numpy_scalar_conversion_dtypes(self):
     self.assertEqual(nd.dtype_of(nd.array(np.bool_(True))), ndt.bool)
     self.assertEqual(nd.dtype_of(nd.array(np.bool(True))), ndt.bool)
     self.assertEqual(nd.dtype_of(nd.array(np.int8(100))), ndt.int8)
     self.assertEqual(nd.dtype_of(nd.array(np.int16(100))), ndt.int16)
     self.assertEqual(nd.dtype_of(nd.array(np.int32(100))), ndt.int32)
     self.assertEqual(nd.dtype_of(nd.array(np.int64(100))), ndt.int64)
     self.assertEqual(nd.dtype_of(nd.array(np.uint8(100))), ndt.uint8)
     self.assertEqual(nd.dtype_of(nd.array(np.uint16(100))), ndt.uint16)
     self.assertEqual(nd.dtype_of(nd.array(np.uint32(100))), ndt.uint32)
     self.assertEqual(nd.dtype_of(nd.array(np.uint64(100))), ndt.uint64)
     self.assertEqual(nd.dtype_of(nd.array(np.float32(100.))), ndt.float32)
     self.assertEqual(nd.dtype_of(nd.array(np.float64(100.))), ndt.float64)
     self.assertEqual(nd.dtype_of(nd.array(np.complex64(100j))),
                      ndt.complex_float32)
     self.assertEqual(nd.dtype_of(nd.array(np.complex128(100j))),
                      ndt.complex_float64)
     if np.__version__ >= '1.7':
         self.assertEqual(nd.dtype_of(nd.array(np.datetime64('2000-12-13'))),
                          ndt.date)
         self.assertEqual(nd.dtype_of(nd.array(np.datetime64('2000-12-13T12:30'))),
                          ndt.type('datetime[tz="UTC"]'))
 def test_specified_dtype(self):
     # Must return the requested type
     self.assertRaises(OverflowError, nd.old_range, 10, dtype=ndt.bool)
     self.assertEqual(nd.dtype_of(nd.old_range(10, dtype=ndt.int8)),
                      ndt.int8)
     self.assertEqual(nd.dtype_of(nd.old_range(10, dtype=ndt.int16)),
                      ndt.int16)
     self.assertEqual(nd.dtype_of(nd.old_range(10, dtype=ndt.int32)),
                      ndt.int32)
     self.assertEqual(nd.dtype_of(nd.old_range(10, dtype=ndt.int64)),
                      ndt.int64)
     self.assertEqual(nd.dtype_of(nd.old_range(10, dtype=ndt.uint8)),
                      ndt.uint8)
     self.assertEqual(nd.dtype_of(nd.old_range(10, dtype=ndt.uint16)),
                      ndt.uint16)
     self.assertEqual(nd.dtype_of(nd.old_range(10, dtype=ndt.uint32)),
                      ndt.uint32)
     self.assertEqual(nd.dtype_of(nd.old_range(10, dtype=ndt.uint64)),
                      ndt.uint64)
     self.assertEqual(nd.dtype_of(nd.old_range(10, dtype=ndt.float32)),
                      ndt.float32)
     self.assertEqual(nd.dtype_of(nd.old_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.old_range,
                       10,
                       dtype=ndt.complex_float32)
     self.assertRaises(TypeError,
                       nd.old_range,
                       10,
                       dtype=ndt.complex_float64)
     # Float/complex should convert when the dtype is specified
     self.assertEqual(nd.dtype_of(nd.old_range(10.0, dtype=ndt.uint16)),
                      ndt.uint16)
     self.assertEqual(
         nd.dtype_of(nd.old_range(1.0, step=0.5 + 0j, dtype=ndt.float32)),
         ndt.float32)
Example #45
0
 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)
Example #46
0
 def test_numpy_scalar_conversion_dtypes(self):
     self.assertEqual(nd.dtype_of(nd.array(np.bool_(True))), ndt.bool)
     self.assertEqual(nd.dtype_of(nd.array(np.bool(True))), ndt.bool)
     self.assertEqual(nd.dtype_of(nd.array(np.int8(100))), ndt.int8)
     self.assertEqual(nd.dtype_of(nd.array(np.int16(100))), ndt.int16)
     self.assertEqual(nd.dtype_of(nd.array(np.int32(100))), ndt.int32)
     self.assertEqual(nd.dtype_of(nd.array(np.int64(100))), ndt.int64)
     self.assertEqual(nd.dtype_of(nd.array(np.uint8(100))), ndt.uint8)
     self.assertEqual(nd.dtype_of(nd.array(np.uint16(100))), ndt.uint16)
     self.assertEqual(nd.dtype_of(nd.array(np.uint32(100))), ndt.uint32)
     self.assertEqual(nd.dtype_of(nd.array(np.uint64(100))), ndt.uint64)
     self.assertEqual(nd.dtype_of(nd.array(np.float32(100.))), ndt.float32)
     self.assertEqual(nd.dtype_of(nd.array(np.float64(100.))), ndt.float64)
     self.assertEqual(nd.dtype_of(nd.array(np.complex64(100j))),
                      ndt.complex_float32)
     self.assertEqual(nd.dtype_of(nd.array(np.complex128(100j))),
                      ndt.complex_float64)