def test_init_v7(self): # list of array fa = FieldArray( "x", [np.array([[1, 2], [3, 4]]), np.array([[1, 2], [3, 4]])], is_input=True) self.assertEqual(fa.pytype, int) self.assertEqual(fa.dtype, np.int)
def test_getitem_v1(self): fa = FieldArray("y", [[1.1, 2.2, 3.3, 4.4, 5.5], [1, 2, 3, 4, 5]], is_input=True) self.assertEqual(fa[0], [1.1, 2.2, 3.3, 4.4, 5.5]) ans = fa[[0, 1]] self.assertTrue(isinstance(ans, np.ndarray)) self.assertTrue(isinstance(ans[0], np.ndarray)) self.assertEqual(ans[0].tolist(), [1.1, 2.2, 3.3, 4.4, 5.5]) self.assertEqual(ans[1].tolist(), [1, 2, 3, 4, 5]) self.assertEqual(ans.dtype, np.float64)
def add_field(self, name, fields, padder=AutoPadder(pad_val=0), is_input=False, is_target=False): """Add a new field to the DataSet. :param str name: the name of the field. :param fields: a list of int, float, or other objects. :param int padder: PadBase对象,如何对该Field进行padding。大部分情况使用默认值即可 :param bool is_input: whether this field is model input. :param bool is_target: whether this field is label or target. """ if len(self.field_arrays) != 0: if len(self) != len(fields): raise RuntimeError(f"The field to append must have the same size as dataset. " f"Dataset size {len(self)} != field size {len(fields)}") self.field_arrays[name] = FieldArray(name, fields, is_target=is_target, is_input=is_input, padder=padder)
def append(self, ins): """Add an instance to the DataSet. If the DataSet is not empty, the instance must have the same field names as the rest instances in the DataSet. :param ins: an Instance object """ if len(self.field_arrays) == 0: # DataSet has no field yet for name, field in ins.fields.items(): self.field_arrays[name] = FieldArray(name, [field]) else: assert len(self.field_arrays) == len(ins.fields) for name, field in ins.fields.items(): assert name in self.field_arrays self.field_arrays[name].append(field)
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])
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.ndarray) self.assertEqual(fa.pytype, np.ndarray) fa.append(np.array([1.1, 2.2, 3.3, 4.4, 5.5])) self.assertEqual(fa.dtype, np.ndarray) self.assertEqual(fa.pytype, np.ndarray) 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)
def append(self, ins): """Add an instance to the DataSet. If the DataSet is not empty, the instance must have the same field names as the rest instances in the DataSet. :param ins: an Instance object """ if len(self.field_arrays) == 0: # DataSet has no field yet for name, field in ins.fields.items(): field = field.tolist() if isinstance(field, np.ndarray) else field self.field_arrays[name] = FieldArray(name, [field]) # 第一个样本,必须用list包装起来 else: if len(self.field_arrays) != len(ins.fields): raise ValueError( "DataSet object has {} fields, but attempt to append an Instance object with {} fields." .format(len(self.field_arrays), len(ins.fields))) for name, field in ins.fields.items(): assert name in self.field_arrays self.field_arrays[name].append(field)
def add_field(self, name, fields, padding_val=0, is_input=False, is_target=False): """Add a new field to the DataSet. :param str name: the name of the field. :param fields: a list of int, float, or other objects. :param int padding_val: integer for padding. :param bool is_input: whether this field is model input. :param bool is_target: whether this field is label or target. """ if len(self.field_arrays) != 0: assert len(self) == len(fields) self.field_arrays[name] = FieldArray(name, fields, padding_val=padding_val, is_target=is_target, is_input=is_input)
def add_field(self, name, fields, padding_val=0, is_input=False, is_target=False): """Add a new field to the DataSet. :param str name: the name of the field. :param fields: a list of int, float, or other objects. :param int padding_val: integer for padding. :param bool is_input: whether this field is model input. :param bool is_target: whether this field is label or target. """ if len(self.field_arrays) != 0: if len(self) != len(fields): raise RuntimeError( f"The field to append must have the same size as dataset. " f"Dataset size {len(self)} != field size {len(fields)}") self.field_arrays[name] = FieldArray(name, fields, padding_val=padding_val, is_target=is_target, is_input=is_input)
def test_nested_list(self): fa = FieldArray("y", [[1.1, 2.2, 3.3, 4.4, 5.5], [1.1, 2.2, 3.3, 4.4, 5.5]], is_input=True) self.assertEqual(fa.pytype, float) self.assertEqual(fa.dtype, np.float64)
def test_type_conversion(self): fa = FieldArray("x", [1.2, 2.2, 3, 4, 5], is_input=True) self.assertEqual(fa.pytype, float) self.assertEqual(fa.dtype, np.float64) fa = FieldArray("x", [1, 2, 3, 4, 5], is_input=True) fa.append(1.3333) self.assertEqual(fa.pytype, float) self.assertEqual(fa.dtype, np.float64) fa = FieldArray("y", [1.1, 2.2, 3.3, 4.4, 5.5], is_input=True) fa.append(10) self.assertEqual(fa.pytype, float) self.assertEqual(fa.dtype, np.float64) fa = FieldArray("y", ["a", "b", "c", "d"], is_input=True) fa.append("e") self.assertEqual(fa.dtype, np.str) self.assertEqual(fa.pytype, str)
def test_getitem_v2(self): x = np.random.rand(10, 5) fa = FieldArray("my_field", x, is_input=True) indices = [0, 1, 3, 4, 6] for a, b in zip(fa[indices], x[indices]): self.assertListEqual(a.tolist(), b.tolist())
def test_init_v7(self): # 二维list val = np.array([[1, 2], [3, 4]]) fa = FieldArray("x", [val], is_input=True) fa.append(val)
def test_init_v6(self): # 二维array val = [[1, 2], [3, 4]] fa = FieldArray("x", [val], is_input=True) fa.append(val)
def test_init_v5(self): # 一维array val = np.array([1, 2, 3, 4]) fa = FieldArray("x", [val], is_input=True) fa.append(val)
def test_init_v4(self): # 一维list val = [1, 2, 3, 4] fa = FieldArray("x", [val], is_input=True) fa.append(val)
def test_init_v3(self): # 三维list fa = FieldArray("x", [[[1, 2], [3, 4]], [[1, 2], [3, 4]]], is_input=True)
def test_init_v2(self): # 二维array fa = FieldArray("x", np.array([[1, 2], [3, 4]] * 5), is_input=True)
def test_init_v1(self): # 二维list fa = FieldArray("x", [[1, 2], [3, 4]] * 5, is_input=True)
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])