コード例 #1
0
    def test_extract_items(self):
        """Tests the extract items from carrays."""
        container = CarrayContainer(10, {
            "x": "double",
            "y": "double",
            "z": "double"
        })
        container["x"][:] = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        container["y"][:] = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1]
        container["z"][:] = [0, 0, 1, 1, 1, 0, 4, 0, 1, 5]

        indices = LongArray(5)
        ind_npy = indices.get_npy_array()
        ind_npy[:] = np.array([5, 1, 7, 3, 9])

        container2 = container.extract_items(indices)

        self.assertEqual(check_array(container2["x"], [6, 2, 8, 4, 10]), True)

        self.assertEqual(check_array(container2["y"], [5, 9, 3, 7, 1]), True)

        self.assertEqual(check_array(container2["z"], [0, 0, 0, 1, 5]), True)

        # remove selected carrays
        container2 = container.extract_items(indices, ["x", "z"])

        self.assertEqual(check_array(container2["x"], [6, 2, 8, 4, 10]), True)

        self.assertEqual(check_array(container2["z"], [0, 0, 0, 1, 5]), True)
コード例 #2
0
    def test_resize(self):
        """Tests the resize function."""
        la = LongArray(0)

        la.resize(20)
        self.assertEqual(la.length, 20)
        self.assertEqual(len(la.get_npy_array()), 20)
        self.assertEqual(la.alloc >= la.length, True)
コード例 #3
0
    def test_reset(self):
        """Tests the reset function."""
        la = LongArray(5)
        la.reset()

        self.assertEqual(la.length, 0)
        self.assertEqual(la.alloc, 5)
        self.assertEqual(len(la.get_npy_array()), 0)
コード例 #4
0
    def test_get_npy_array(self):
        """Tests the get_npy_array array."""
        la = LongArray(3)
        la[0] = 1
        la[1] = 2
        la[2] = 3

        nparray = la.get_npy_array()
        self.assertEqual(len(nparray), 3)

        for i in range(3):
            self.assertEqual(nparray[i], la[i])
コード例 #5
0
    def test_paste_values(self):
        """Tests the paste values function."""
        lla1 = LongLongArray(3)
        lla1_array = lla1.get_npy_array()
        lla1_array[:] = 2

        lla2 = LongLongArray(6)
        lla2_array = lla2.get_npy_array()
        lla2_array[:] = 0

        indices = LongArray(3)
        ind_array = indices.get_npy_array()
        ind_array[:] = [0, 2, 4]

        lla1.paste_values(indices, lla2)
        for i in indices:
            self.assertTrue(lla2[i] == 2)
コード例 #6
0
    def test_paste_values(self):
        """Tests the paste values function."""
        ia1 = IntArray(3)
        ia1_array = ia1.get_npy_array()
        ia1_array[:] = 2

        ia2 = IntArray(6)
        ia2_array = ia2.get_npy_array()
        ia2_array[:] = 0

        indices = LongArray(3)
        ind_array = indices.get_npy_array()
        ind_array[:] = [0, 2, 4]

        ia1.paste_values(indices, ia2)
        for i in indices:
            self.assertTrue(ia2[i] == 2)
コード例 #7
0
    def test_paste_values(self):
        """Tests the paste values function."""
        da1 = DoubleArray(3)
        da1_array = da1.get_npy_array()
        da1_array[:] = 2.0

        da2 = DoubleArray(6)
        da2_array = da2.get_npy_array()
        da2_array[:] = 0.0

        indices = LongArray(3)
        ind_array = indices.get_npy_array()
        ind_array[:] = [0, 2, 4]

        da1.paste_values(indices, da2)
        for i in indices:
            self.assertTrue(da2[i] == 2.0)
コード例 #8
0
    def test_copy_values(self):
        """Tests the copy values function."""
        la1 = LongArray(5)
        la2 = LongArray(5)
        indices = LongArray(5)

        for i in range(5):
            la1[i] = i
            la2[i] = 0
            indices[i] = i

        la1.copy_values(indices, la2)
        self.assertEqual(np.allclose(la1.get_npy_array(), la2.get_npy_array()),
                         True)
コード例 #9
0
    def test_paste(self):
        """Tests paste function."""
        container = CarrayContainer(5, {"x": "int", "y": "int"})
        container["x"][:] = [1, 2, 3, 4, 5]
        container["y"][:] = [10, 9, 8, 7, 6]

        container2 = CarrayContainer(3, {"x": "int", "y": "int"})
        container2["x"][:] = [6, 7, 8]
        container2["y"][:] = [5, 4, 3]

        indices = LongArray(3)
        ind_npy = indices.get_npy_array()
        ind_npy[:] = np.array([1, 4, 3])

        container.paste(container2, indices, ["x", "y"])
        self.assertEqual(container.get_carray_size(), 5)

        self.assertEqual(check_array(container["x"], [1, 6, 3, 8, 7]), True)
        self.assertEqual(check_array(container["y"], [10, 5, 8, 3, 4]), True)
コード例 #10
0
    def test_copy(self):
        """Tests copy function."""
        container = CarrayContainer(5, {"x": "int", "y": "int"})
        container["x"][:] = [1, 2, 3, 4, 5]
        container["y"][:] = [10, 9, 8, 7, 6]

        container2 = CarrayContainer(5, {"x": "int", "y": "int"})
        container2["x"][:] = [6, 7, 8, 9, 10]
        container2["y"][:] = [5, 4, 3, 2, 1]

        indices = LongArray(3)
        ind_npy = indices.get_npy_array()
        ind_npy[:] = np.array([1, 4, 3])

        container.copy(container2, indices, ["x", "y"])
        self.assertEqual(container.get_carray_size(), 3)

        self.assertEqual(check_array(container["x"], [7, 10, 9]), True)
        self.assertEqual(check_array(container["y"], [4, 1, 2]), True)
コード例 #11
0
    def test_get_set_indexing(self):
        """Test get/set and [] operator."""
        la = LongArray(10)
        la.set(0, 10)
        la.set(9, 1)

        self.assertEqual(la.get(0), 10)
        self.assertEqual(la.get(9), 1)

        la[9] = 2
        self.assertEqual(la[9], 2)
コード例 #12
0
    def test_squeeze(self):
        """Tests the squeeze function."""
        la = LongArray(5)
        la.append(4)

        self.assertEqual(la.alloc > la.length, True)

        la.squeeze()

        self.assertEqual(la.length, 6)
        self.assertEqual(la.alloc == la.length, True)
        self.assertEqual(len(la.get_npy_array()), 6)
コード例 #13
0
    def test_copy_values(self):
        """Tests the copy values function."""
        da1 = DoubleArray(5)
        da2 = DoubleArray(5)
        indices = LongArray(5)

        for i in range(5):
            da1[i] = i
            da2[i] = 0
            indices[i] = i

        da1.copy_values(indices, da2)
        self.assertEqual(np.allclose(da1.get_npy_array(), da2.get_npy_array()),
                         True)
コード例 #14
0
    def test_constructor(self):
        """Test the constructor."""
        lla = LongLongArray(10)

        self.assertEqual(lla.length, 10)
        self.assertEqual(lla.alloc, 10)
        self.assertEqual(len(lla.get_npy_array()), 10)
        self.assertEqual(lla.get_npy_array().itemsize, 8)

        lla = LongArray()

        self.assertEqual(lla.length, 0)
        self.assertEqual(lla.alloc, 16)
        self.assertEqual(len(lla.get_npy_array()), 0)
コード例 #15
0
    def test_extend(self):
        """Tests teh extend function."""
        la1 = LongArray(5)

        for i in range(5):
            la1[i] = i

        la2 = LongArray(5)

        for i in range(5):
            la2[i] = 5 + i

        la1.extend(la2.get_npy_array())

        self.assertEqual(la1.length, 10)
        self.assertEqual(
            np.allclose(la1.get_npy_array(), np.arange(10, dtype=np.int32)),
            True)
コード例 #16
0
    def test_append(self):
        """Test the append function."""
        la = LongArray(0)
        la.append(1)
        la.append(2)
        la.append(3)

        self.assertEqual(la.length, 3)
        self.assertEqual(la[0], 1)
        self.assertEqual(la[1], 2)
        self.assertEqual(la[2], 3)
コード例 #17
0
    def test_aling_array(self):
        """Test the align_array function."""
        la1 = LongArray(10)
        la1_array = la1.get_npy_array()
        la1_array[:] = np.arange(10, dtype=np.int32)

        new_indices = np.array([1, 5, 3, 2, 4, 7, 8, 6, 9, 0], dtype=np.int)
        la1.align_array(new_indices)

        self.assertEqual(
            np.allclose(
                np.array([1, 5, 3, 2, 4, 7, 8, 6, 9, 0], dtype=np.int32),
                la1.get_npy_array()), True)
コード例 #18
0
 def test_dtype(self):
     """Test data type."""
     la = LongArray(10)
     self.assertEqual(la.get_npy_array().dtype, np.int32)
コード例 #19
0
    def test_remove(self):
        """Tests the remove function"""
        la1 = LongArray(10)
        la1_array = la1.get_npy_array()
        la1_array[:] = np.arange(10, dtype=np.int32)

        rem = [0, 4, 3]
        la1.remove(np.array(rem, dtype=np.int))
        self.assertEqual(la1.length, 7)
        self.assertEqual(
            np.allclose(np.array([7, 1, 2, 8, 9, 5, 6], dtype=np.int32),
                        la1.get_npy_array()), True)

        la1.remove(np.array(rem, dtype=np.int))
        self.assertEqual(la1.length, 4)
        self.assertEqual(
            np.allclose(np.array([6.0, 1.0, 2.0, 5.0], dtype=np.int32),
                        la1.get_npy_array()), True)

        rem = [0, 1, 3]
        la1.remove(np.array(rem, dtype=np.int))
        self.assertEqual(la1.length, 1)
        self.assertEqual(
            np.allclose(np.array([2.0], dtype=np.int32), la1.get_npy_array()),
            True)

        la1.remove(np.array([0], dtype=np.int))
        self.assertEqual(la1.length, 0)
        self.assertEqual(len(la1.get_npy_array()), 0)