Example #1
0
    def test_append(self):
        with self.assertRaises(Exception):
            fa = FieldArray("y", [[1.1, 2.2, 3.3, 4.4, 5.5], [1, 2, 3, 4, 5]],
                            is_input=True)
            fa.append(0)

        with self.assertRaises(Exception):
            fa = FieldArray("y", [1.1, 2.2, 3.3, 4.4, 5.5], is_input=True)
            fa.append([1, 2, 3, 4, 5])

        with self.assertRaises(Exception):
            fa = FieldArray("y", [[1.1, 2.2, 3.3, 4.4, 5.5], [1, 2, 3, 4, 5]],
                            is_input=True)
            fa.append([])

        with self.assertRaises(Exception):
            fa = FieldArray("y", [[1.1, 2.2, 3.3, 4.4, 5.5], [1, 2, 3, 4, 5]],
                            is_input=True)
            fa.append(["str", 0, 0, 0, 1.89])

        fa = FieldArray("y", [[1.1, 2.2, 3.3, 4.4, 5.5], [1, 2, 3, 4, 5]],
                        is_input=True)
        fa.append([1.2, 2.3, 3.4, 4.5, 5.6])
        self.assertEqual(len(fa), 3)
        self.assertEqual(fa[2], [1.2, 2.3, 3.4, 4.5, 5.6])
Example #2
0
    def test_ignore_type(self):
        # 测试新添加的参数ignore_type,用来跳过类型检查
        fa = FieldArray("y",
                        [[1.1, 2.2, "jin", {}, "hahah"], [int, 2, "$", 4, 5]],
                        is_input=True,
                        ignore_type=True)
        fa.append([1.2, 2.3, str, 4.5, print])

        fa = FieldArray("y", [(1, "1"), (2, "2"), (3, "3"), (4, "4")],
                        is_target=True,
                        ignore_type=True)
Example #3
0
    def test_type_conversion(self):
        fa = FieldArray("x", [1, 2, 3, 4, 5], is_input=True)
        self.assertEqual(fa.dtype, int)

        fa = FieldArray("y", [1.1, 2.2, 3.3, 4.4, 5.5], is_input=True)
        fa.append(10.0)
        self.assertEqual(fa.dtype, float)

        fa = FieldArray("y", ["a", "b", "c", "d"], is_input=True)
        fa.append("e")
        self.assertEqual(fa.dtype, str)
Example #4
0
    def test_support_np_array(self):
        fa = FieldArray("y",
                        np.array([[1.1, 2.2, 3.3, 4.4, 5.5]]),
                        is_input=True)
        self.assertEqual(fa.dtype, np.float64)

        fa.append(np.array([1.1, 2.2, 3.3, 4.4, 5.5]))
        self.assertEqual(fa.dtype, np.float64)

        fa = FieldArray("my_field", np.random.rand(3, 5), is_input=True)
        # in this case, pytype is actually a float. We do not care about it.
        self.assertEqual(fa.dtype, np.float64)
Example #5
0
    def test_main(self):
        fa = FieldArray("x", [1, 2, 3, 4, 5], is_input=True)
        self.assertEqual(len(fa), 5)
        fa.append(6)
        self.assertEqual(len(fa), 6)

        self.assertEqual(fa[-1], 6)
        self.assertEqual(fa[0], 1)
        fa[-1] = 60
        self.assertEqual(fa[-1], 60)

        self.assertEqual(fa.get(0), 1)
        self.assertTrue(isinstance(fa.get([0, 1, 2]), np.ndarray))
        self.assertListEqual(list(fa.get([0, 1, 2])), [1, 2, 3])
Example #6
0
 def test_init_v7(self):
     # 二维list
     val = np.array([[1, 2], [3, 4]])
     fa = FieldArray("x", [val], is_input=True)
     fa.append(val)
Example #7
0
 def test_init_v6(self):
     # 二维array
     val = [[1, 2], [3, 4]]
     fa = FieldArray("x", [val], is_input=True)
     fa.append(val)
Example #8
0
 def test_init_v5(self):
     # 一维array
     val = np.array([1, 2, 3, 4])
     fa = FieldArray("x", [val], is_input=True)
     fa.append(val)
Example #9
0
 def test_init_v4(self):
     # 一维list
     val = [1, 2, 3, 4]
     fa = FieldArray("x", [val], is_input=True)
     fa.append(val)