class TestArrayConversion(ParallelTestCase):
    """ Test array conversion methods. """

    def setUp(self):
        # On Python3, an 'int' gets converted to 'np.int64' on copy,
        # so we force the numpy type to start with so we get back
        # the same thing.
        self.int_type = np.int64
        self.distribution = Distribution.from_shape(comm=self.comm,
                                                    shape=(4,))
        self.int_larr = LocalArray(self.distribution, dtype=self.int_type)
        self.int_larr.fill(3)

    def test_astype(self):
        """ Test that astype() works as expected. """
        # Convert int array to float.
        float_larr = self.int_larr.astype(float)
        for global_inds, value in ndenumerate(float_larr):
            self.assertEqual(value, 3.0)
            self.assertTrue(isinstance(value, float))
        # No type specification for a copy.
        # Should get same type as we started with.
        int_larr2 = self.int_larr.astype(None)
        for global_inds, value in ndenumerate(int_larr2):
            self.assertEqual(value, 3)
            self.assertTrue(isinstance(value, self.int_type))
 def test_copy_bn(self):
     distribution = Distribution.from_shape(comm=self.comm,
                                     shape=(16, 16), dist=('b', 'n'))
     a = LocalArray(distribution, dtype=np.int_)
     a.fill(11)
     b = a.copy()
     assert_localarrays_equal(a, b, check_dtype=True)
示例#3
0
    def test_add(self):
        """See if binary ufunc works for a LocalArray."""
        d = Distribution.from_shape(comm=self.comm, shape=(16, 16))
        a = LocalArray(d, dtype='int32')
        b = LocalArray(d, dtype='int32')
        a.fill(1)
        b.fill(1)
        c = localarray.add(a, b)
        self.assertTrue(np.all(c.ndarray == 2))

        c = localarray.empty_like(a)
        c = localarray.add(a, b, c)
        self.assertTrue(np.all(c.ndarray == 2))

        d0 = Distribution.from_shape(comm=self.comm, shape=(16, 16))
        d1 = Distribution.from_shape(comm=self.comm, shape=(20, 20))
        a = LocalArray(d0, dtype='int32')
        b = LocalArray(d1, dtype='int32')
        self.assertRaises(IncompatibleArrayError, localarray.add, a, b)

        d0 = Distribution.from_shape(comm=self.comm, shape=(16, 16))
        d1 = Distribution.from_shape(comm=self.comm, shape=(20, 20))
        a = LocalArray(d0, dtype='int32')
        b = LocalArray(d0, dtype='int32')
        c = LocalArray(d1, dtype='int32')
        self.assertRaises(IncompatibleArrayError, localarray.add, a, b, c)
示例#4
0
 def test_abs(self):
     """See if unary ufunc works for a LocalArray."""
     d = Distribution.from_shape(comm=self.comm, shape=(16, 16))
     a = LocalArray(d, dtype='int32')
     a.fill(-5)
     a[2, 3] = 11
     b = abs(a)
     self.assertTrue(np.all(abs(a.ndarray) == b.ndarray))
示例#5
0
 def test_copy_bn(self):
     distribution = Distribution.from_shape(comm=self.comm,
                                            shape=(16, 16),
                                            dist=('b', 'n'))
     a = LocalArray(distribution, dtype=np.int_)
     a.fill(11)
     b = a.copy()
     assert_localarrays_equal(a, b, check_dtype=True)
 def test_astype_cbc(self):
     new_dtype = np.int8
     d = Distribution(comm=self.comm, dim_data=self.ddpr[self.comm.Get_rank()])
     a = LocalArray(d, dtype=np.int32)
     a.fill(12)
     b = a.astype(new_dtype)
     assert_localarrays_allclose(a, b, check_dtype=False)
     self.assertEqual(b.dtype, new_dtype)
     self.assertEqual(b.ndarray.dtype, new_dtype)
 def test_astype_bn(self):
     new_dtype = np.float32
     d = Distribution.from_shape(comm=self.comm,
                          shape=(16, 16), dist=('b', 'n'))
     a = LocalArray(d, dtype=np.int_)
     a.fill(11)
     b = a.astype(new_dtype)
     assert_localarrays_allclose(a, b, check_dtype=False)
     self.assertEqual(b.dtype, new_dtype)
     self.assertEqual(b.ndarray.dtype, new_dtype)
示例#8
0
 def test_astype_cbc(self):
     new_dtype = np.int8
     d = Distribution(comm=self.comm,
                      dim_data=self.ddpr[self.comm.Get_rank()])
     a = LocalArray(d, dtype=np.int32)
     a.fill(12)
     b = a.astype(new_dtype)
     assert_localarrays_allclose(a, b, check_dtype=False)
     self.assertEqual(b.dtype, new_dtype)
     self.assertEqual(b.ndarray.dtype, new_dtype)
示例#9
0
 def test_astype_bn(self):
     new_dtype = np.float32
     d = Distribution.from_shape(comm=self.comm,
                                 shape=(16, 16),
                                 dist=('b', 'n'))
     a = LocalArray(d, dtype=np.int_)
     a.fill(11)
     b = a.astype(new_dtype)
     assert_localarrays_allclose(a, b, check_dtype=False)
     self.assertEqual(b.dtype, new_dtype)
     self.assertEqual(b.ndarray.dtype, new_dtype)
示例#10
0
    def test_negative(self):
        """See if unary ufunc works for a LocalArray."""
        d = Distribution.from_shape(comm=self.comm, shape=(16, 16))
        a = LocalArray(d, dtype='int32')
        a.fill(1)
        b = localarray.negative(a)
        self.assertTrue(np.all(a.ndarray == -b.ndarray))

        b = localarray.empty_like(a)
        b = localarray.negative(a, b)
        self.assertTrue(np.all(a.ndarray == -b.ndarray))

        d2 = Distribution.from_shape(comm=self.comm, shape=(20, 20))
        a = LocalArray(d, dtype='int32')
        b = LocalArray(d2, dtype='int32')
        self.assertRaises(IncompatibleArrayError, localarray.negative, b, a)
示例#11
0
    def test_negative(self):
        """See if unary ufunc works for a LocalArray."""
        d = Distribution.from_shape(comm=self.comm, shape=(16, 16))
        a = LocalArray(d, dtype='int32')
        a.fill(1)
        b = localarray.negative(a)
        self.assertTrue(np.all(a.ndarray == -b.ndarray))

        b = localarray.empty_like(a)
        b = localarray.negative(a, b)
        self.assertTrue(np.all(a.ndarray == -b.ndarray))

        d2 = Distribution.from_shape(comm=self.comm, shape=(20, 20))
        a = LocalArray(d, dtype='int32')
        b = LocalArray(d2, dtype='int32')
        self.assertRaises(IncompatibleArrayError, localarray.negative, b, a)
 def test_copy_cbc(self):
     distribution = Distribution(comm=self.comm, dim_data=self.ddpr[self.comm.Get_rank()])
     a = LocalArray(distribution, dtype=np.int_)
     a.fill(12)
     b = a.copy()
     assert_localarrays_equal(a, b, check_dtype=True)