Exemplo n.º 1
0
    def test_DUnion_object(self):
        union_object = DUnion([bool, DChar(), int]).as_numpy_object('c')
        self.assertEqual(union_object[0]["DChar8"], 99)

        with self.assertRaises(
                DeltaTypeError,
                msg="NumPy unions cannot be converted to Python types."):
            self.check('c', DUnion([bool, DChar(), int]))
Exemplo n.º 2
0
    def test_as_delta_type(self):
        """Test conversion from python to Deltaflow data types."""
        # special
        self.assertEqual(as_delta_type(object), Top())
        self.assertEqual(as_delta_type(type(object)), Top())
        self.assertEqual(as_delta_type(type), Top())

        self.assertEqual(as_delta_type(Void), Void)

        with self.assertRaises(DeltaTypeError):
            as_delta_type(None)
        with self.assertRaises(DeltaTypeError):
            as_delta_type(type(None))

        # primitive
        self.assertNotEqual(as_delta_type(bool), DUInt(DSize(1)))
        self.assertEqual(as_delta_type(bool), DBool())
        self.assertEqual(as_delta_type(np.bool_), DBool())
        self.assertEqual(as_delta_type(int), DInt(DSize(32)))
        self.assertEqual(as_delta_type(np.int8), DChar())
        self.assertEqual(as_delta_type(np.int16), DInt(DSize(16)))
        self.assertEqual(as_delta_type(np.int32), DInt(DSize(32)))
        self.assertEqual(as_delta_type(np.int64), DInt(DSize(64)))
        self.assertEqual(as_delta_type(np.uint8), DChar())
        self.assertEqual(as_delta_type(np.uint16), DUInt(DSize(16)))
        self.assertEqual(as_delta_type(np.uint32), DUInt(DSize(32)))
        self.assertEqual(as_delta_type(np.uint64), DUInt(DSize(64)))
        self.assertEqual(as_delta_type(float), DFloat())
        self.assertEqual(as_delta_type(np.float32), DFloat(DSize(32)))
        self.assertEqual(as_delta_type(np.float64), DFloat(DSize(64)))
        self.assertEqual(as_delta_type(complex), DComplex())
        self.assertEqual(as_delta_type(np.complex64), DComplex(DSize(64)))
        self.assertEqual(as_delta_type(np.complex128), DComplex(DSize(128)))

        # compound
        with self.assertRaises(DeltaTypeError):
            as_delta_type(Tuple[int, bool])
        with self.assertRaises(DeltaTypeError):
            as_delta_type(List[int])
        self.assertNotEqual(as_delta_type(str), DArray(DChar(), DSize(1024)))
        self.assertEqual(as_delta_type(str), DStr())
        self.assertEqual(as_delta_type(RecBI), DRecord(RecBI))

        # numpy compound
        self.assertEqual(as_delta_type(DArray(int, DSize(5)).as_numpy_type()),
                         DArray(int, DSize(5)))
        self.assertEqual(as_delta_type(DStr().as_numpy_type()), DStr())
        self.assertEqual(
            as_delta_type(DTuple([int, bool, float]).as_numpy_type()),
            DTuple([int, bool, float])
        )
        self.assertEqual(as_delta_type(DRecord(RecBI).as_numpy_type()),
                         DRecord(RecBI))
        self.assertEqual(
            as_delta_type(DUnion([bool, float, int]).as_numpy_type()),
            DUnion([bool, float, int]))
Exemplo n.º 3
0
    def test_delta_type(self):
        """Test mapping python objects to Deltaflow data types."""
        # special
        with self.assertRaises(DeltaTypeError):
            delta_type(None)

        # primitive
        self.assertEqual(delta_type(False), DBool())
        self.assertEqual(delta_type(np.bool_(False)), DBool())
        self.assertEqual(delta_type(5), DInt(DSize(32)))
        self.assertEqual(delta_type(np.int16(5)), DInt(DSize(16)))
        self.assertEqual(delta_type(np.int32(5)), DInt(DSize(32)))
        self.assertEqual(delta_type(np.int64(5)), DInt(DSize(64)))
        self.assertEqual(delta_type(np.uint16(5)), DUInt(DSize(16)))
        self.assertEqual(delta_type(np.uint32(5)), DUInt(DSize(32)))
        self.assertEqual(delta_type(np.uint64(5)), DUInt(DSize(64)))
        self.assertEqual(delta_type(4.2), DFloat(DSize(32)))
        self.assertEqual(delta_type(np.float32(4.2)), DFloat(DSize(32)))
        self.assertEqual(delta_type(np.float64(4.2)), DFloat(DSize(64)))
        self.assertEqual(delta_type(3+1j), DComplex(DSize(64)))
        self.assertEqual(delta_type(np.complex64(3+1j)), DComplex(DSize(64)))
        self.assertEqual(delta_type(np.complex128(3+1j)), DComplex(DSize(128)))
        self.assertEqual(delta_type('c'), DChar())

        # compound
        self.assertEqual(delta_type((1, True, 3.7)),
                         DTuple([int, bool, float]))
        self.assertEqual(delta_type([1, 2, 4]), DArray(int, DSize(3)))
        self.assertEqual(delta_type(RecBI(True, 5)), DRecord(RecBI))

        # numpy compound
        self.assertEqual(delta_type(np.array([1, 2, 3, 4, 5])),
                         DArray(DInt(DSize(64)), DSize(5)))
        self.assertEqual(delta_type(np.array([1, 2.0, 3, 4, 5])),
                         DArray(DFloat(DSize(64)), DSize(5)))
        self.assertEqual(delta_type(
            DStr(DSize(5)).as_numpy_object("abcde")), DStr(DSize(5)))
        self.assertEqual(
            delta_type(DTuple([int, float, bool]
                              ).as_numpy_object((1, 2.0, True))),
            DTuple([int, float, bool])
        )
        self.assertEqual(
            delta_type(DRecord(RecBI).as_numpy_object(RecBI(True, 2))),
            DRecord(RecBI)
        )
        self.assertEqual(
            delta_type(DUnion([bool, float, int]).as_numpy_object(5.0)),
            DUnion([bool, float, int])
        )

        # different combinations
        self.assertEqual(delta_type([(4, 4.3), (2, 3.3)]),
                         DArray(DTuple([int, float]), DSize(2)))
Exemplo n.º 4
0
    def test_DUnion(self):
        """DUnion specific types."""

        with self.assertRaises(DeltaTypeError):
            DUnion([])

        with self.assertRaises(DeltaTypeError):
            DUnion([None])

        with self.assertRaises(DeltaTypeError):
            DUnion([None, int])
Exemplo n.º 5
0
 def test_DUnion_type(self):
     union_type = DUnion([bool, DChar(), int]).as_numpy_type()
     self.assertEqual(union_type[0], np.bool_)
     self.assertEqual(union_type[1], np.uint8)
     self.assertEqual(union_type[2], np.int32)
     self.assertEqual(union_type.fields['DBool'][1], 0)
     self.assertEqual(union_type.fields['DChar8'][1], 0)
     self.assertEqual(union_type.fields['DInt32'][1], 0)
Exemplo n.º 6
0
    def test_size(self):
        """Test how many bits each data type takes."""
        # primitive
        self.assertEqual(DInt().size, DSize(32))
        self.assertEqual(DUInt().size, DSize(32))
        self.assertEqual(DBool().size, DSize(1))
        self.assertEqual(DChar().size, DSize(8))
        self.assertEqual(DFloat().size, DSize(32))

        # compound
        self.assertEqual(DTuple([int, bool]).size, DSize(33))
        self.assertEqual(DArray(int, DSize(10)).size, DSize(320))
        self.assertEqual(DStr().size, DSize(8192))
        self.assertEqual(DRecord(RecBI).size, DSize(33))

        # compound: DUnion
        self.assertEqual(DUnion([bool]).size, DSize(9))
        self.assertEqual(DUnion([int, bool]).size, DSize(40))
        self.assertEqual(DUnion([int, DTuple([int, int])]).size, DSize(2*32+8))
Exemplo n.º 7
0
    def test_compound_objects(self):
        t = DArray(DTuple([bool, int]), DSize(3))
        val = [(True, 1), (False, 2), (True, 3), (False, 4), (True, 5)]
        self.check(val, t)

        t = DTuple([int, DTuple([bool, int])])
        val = (12, (True, 8))
        self.check(val, t)

        t = DTuple([int, DArray(int, DSize(2))])
        val = (12, [14, 18])
        self.check(val, t)

        t = DTuple([int, DStr()])
        val = (12, "hello")
        self.check(val, t)

        t = DTuple([int, DRecord(RecBI)])
        val = (12, RecBI(True, 8))
        self.check(val, t)

        t = DTuple([int, DUnion([bool, int])])
        val = (12, True)
        np_val = t.as_numpy_object(val)
        self.assertEqual(DInt().from_numpy_object(np_val[0][0]), 12)
        self.assertEqual(DBool().from_numpy_object(np_val[0][1][1]), True)

        t = DRecord(RecATI)
        val = RecATI([1, 2], (3.0, 4), 5)
        self.check(val, t)

        t = DUnion([DArray(int, DSize(2)), int])
        val = [1, 2]
        np_val = t.as_numpy_object(val)
        new_val = DArray(int, DSize(2)).from_numpy_object(np_val[0][1])
        self.assertEqual(val, new_val)

        t = DUnion([str, int])
        val = "abcde"
        np_val = t.as_numpy_object(val)
        new_val = DStr().from_numpy_object(np_val[0][1])
        self.assertEqual(val, new_val)
Exemplo n.º 8
0
    def test_DUnion(self):
        # primitive
        self.check(5, DUnion([int, bool]))
        self.check(True, DUnion([int, bool]))

        # compound
        self.check(5, DUnion([int, DTuple([int, float])]))
        self.check((4, 5), DUnion([int, DTuple([int, int])]))
        self.check((4, 5),
                   DUnion([DArray(int, DSize(2)), DTuple([int, int])]))
        self.check([4, 5],
                   DUnion([DArray(int, DSize(2)), DTuple([int, int])]))
        self.assertTrue(DeltaGraph.check_wire(DRaw(DUnion([DStr(), int])),
                                              DRaw(DUnion([DStr(), int]))))
Exemplo n.º 9
0
    def test_as_python_type(self):
        """Test conversion of Deltaflow data types to python."""
        # special
        self.assertEqual(Top().as_python_type(), Any)

        # primitive
        self.assertEqual(DInt(DSize(32)).as_python_type(), int)
        self.assertEqual(DInt(DSize(64)).as_python_type(), int)
        self.assertEqual(DUInt(DSize(32)).as_python_type(), int)
        self.assertEqual(DUInt(DSize(64)).as_python_type(), int)
        self.assertEqual(DBool().as_python_type(), bool)
        with self.assertRaises(NotImplementedError):
            DChar().as_python_type()
        self.assertEqual(DFloat(DSize(32)).as_python_type(), float)
        self.assertEqual(DFloat(DSize(64)).as_python_type(), float)
        self.assertEqual(DComplex(DSize(64)).as_python_type(), complex)
        self.assertEqual(DComplex(DSize(128)).as_python_type(), complex)

        # compound
        self.assertEqual(DTuple([int, bool]).as_python_type(),
                         Tuple[int, bool])
        self.assertEqual(DTuple([int, DTuple([int, bool])]).as_python_type(),
                         Tuple[int, Tuple[int, bool]])
        self.assertEqual(DArray(int, DSize(3)).as_python_type(),
                         List[int])

        self.assertEqual(DStr().as_python_type(), str)
        self.assertEqual(DStr(DSize(10)).as_python_type(), str)

        self.assertEqual(DRecord(RecBI).as_python_type(), RecBI)
        self.assertEqual(DRecord(RecBDi).as_python_type(), RecBDi)
        self.assertNotEqual(DRecord(RecBI).as_python_type(), RecBI_copy)

        # compound: DUnion
        self.assertEqual(DUnion([bool, int]).as_python_type(),
                         Union[bool, int])
        self.assertEqual(DUnion([bool, DTuple([int, bool])]).as_python_type(),
                         Union[bool, Tuple[int, bool]])
Exemplo n.º 10
0
    def test_str(self):
        """Test string representation of data types."""
        # primitive
        self.assertEqual(str(DInt()), "DInt32")
        self.assertEqual(str(DInt(DSize(64))), "DInt64")
        self.assertEqual(str(DUInt()), "DUInt32")
        self.assertEqual(str(DUInt(DSize(64))), "DUInt64")
        self.assertEqual(str(DBool()), "DBool")
        self.assertEqual(str(DChar()), "DChar8")
        self.assertEqual(str(DFloat()), "DFloat32")
        self.assertEqual(str(DFloat(DSize(64))), "DFloat64")

        # compound
        self.assertEqual(str(DArray(int, DSize(8))), "[DInt32 x 8]")
        self.assertEqual(str(DStr()), "DStr8192")
        self.assertEqual(str(DStr(DSize(100))), "DStr800")
        self.assertEqual(str(DTuple([int, bool])), "(DInt32, DBool)")
        self.assertEqual(str(DRecord(RecBIS)),
                         "{x: DBool, y: DInt32, z: DStr8192}")
        self.assertEqual(str(DUnion([int, bool])), "<DBool | DInt32>")

        # compound: DUnion
        self.assertEqual(str(DUnion([int])), "<DInt32>")
        self.assertEqual(str(DUnion([int, DUnion([int, bool])])),
                         "<DBool | DInt32>")
        self.assertEqual(str(DUnion([int, DUnion([int, DUnion([int, bool])])])),
                         "<DBool | DInt32>")

        # encapsulation of various types
        self.assertEqual(str(DUnion([int, DTuple([int, bool])])),
                         "<(DInt32, DBool) | DInt32>")
        self.assertEqual(str(DArray(DTuple([int, bool]), DSize(8))),
                         "[(DInt32, DBool) x 8]")

        # special
        self.assertEqual(str(Top()), "T")
        self.assertEqual(str(DSize(5)), "5")
        self.assertEqual(str(DSize(NamespacedName("a", "b"))), "(a.b)")
        self.assertEqual(str(ForkedReturn(dict(x=int, y=bool, z=str))),
                         "ForkedReturn(x:DInt32, y:DBool, z:DStr8192)")
Exemplo n.º 11
0
    def test_Top(self):
        """Everything can be accepted as Top()."""
        self.assertTrue(DeltaGraph.check_wire(DInt(), Top()))
        self.assertTrue(DeltaGraph.check_wire(DUInt(), Top()))
        self.assertTrue(DeltaGraph.check_wire(DBool(), Top()))
        self.assertTrue(DeltaGraph.check_wire(DTuple([int, bool]), Top()))
        self.assertTrue(DeltaGraph.check_wire(DUnion([int, bool]), Top()))
        self.assertTrue(DeltaGraph.check_wire(DArray(int, DSize(8)), Top()))
        self.assertTrue(DeltaGraph.check_wire(DStr(), Top()))
        self.assertTrue(DeltaGraph.check_wire(DRecord(RecBI), Top()))
        self.assertTrue(DeltaGraph.check_wire(Top(), Top()))

        with self.assertRaises(DeltaTypeError):
            DeltaGraph.check_wire(Top(), DInt())

        # however it's not true if Top is used within a non-primitive type
        with self.assertRaises(DeltaTypeError):
            DeltaGraph.check_wire(DTuple([int, int]), DTuple([int, Top()]))
        with self.assertRaises(DeltaTypeError):
            DeltaGraph.check_wire(DArray(int, DSize(8)),
                                  DArray(Top(), DSize(8)))
        with self.assertRaises(DeltaTypeError):
            DeltaGraph.check_wire(DRecord(RecBI), DRecord(RecBT))
Exemplo n.º 12
0
    def test_DUnion(self):
        """Test wires with DUnion."""
        # examples of obvious behaiviour
        self.assertTrue(DeltaGraph.check_wire(DUnion([int, bool]),
                                              DUnion([int, bool])))
        self.assertTrue(DeltaGraph.check_wire(DUnion([int, bool]),
                                              DUnion([bool, int])))

        with self.assertRaises(DeltaTypeError):
            DeltaGraph.check_wire(DUnion([int, bool]), DInt())
        with self.assertRaises(DeltaTypeError):
            DeltaGraph.check_wire(DUnion([int, bool, float]),
                                  DUnion([int, bool]))

        # strict typing even with DUnion, i.e. all subtypes should match
        with self.assertRaises(DeltaTypeError):
            DeltaGraph.check_wire(DUnion([bool, int]),
                                  DUnion([bool, int, float]))

        # DUnion changes packing method, thus these tests should fail
        with self.assertRaises(DeltaTypeError):
            DeltaGraph.check_wire(DInt(), DUnion([int]))
        with self.assertRaises(DeltaTypeError):
            DeltaGraph.check_wire(DUnion([int]), DInt())
Exemplo n.º 13
0
    def test_DUnion(self):
        # primitive
        self.check(5, DUnion([int, bool]), DUnion([int, bool]))
        self.check(True, DUnion([int, bool]), DUnion([bool, int]))

        # compound
        self.check(5, DUnion([int, DTuple([int, float])]))
        self.check((4, 5), DUnion([int, DTuple([int, int])]))
        self.check((4, 5),
                   DUnion([DArray(int, DSize(2)), DTuple([int, int])]))
        self.check([4, 5],
                   DUnion([DArray(int, DSize(2)), DTuple([int, int])]))

        # buffer's size is always the same
        self.assertEqual(len(DUnion([int, bool]).pack(5)),
                         DUnion([int, bool]).size.val)
        self.assertEqual(len(DUnion([int, bool]).pack(True)),
                         DUnion([int, bool]).size.val)

        # numpy (throws error)
        with self.assertRaises(
                DeltaTypeError,
                msg="NumPy unions cannot be converted to Python types."):
            self.check_numpy(5, DUnion([bool, float, int]))
Exemplo n.º 14
0
    def test_types_comparison(self):
        """Various tests of types comparison."""
        # primitive
        self.assertEqual(DInt(DSize(32)), DInt())
        self.assertNotEqual(DInt(), DUInt())
        self.assertNotEqual(DInt(), DInt(DSize(64)))

        # compound
        self.assertEqual(DTuple([int, bool]), DTuple([int, bool]))
        self.assertNotEqual(DTuple([int, bool]), DTuple([bool, int]))
        self.assertEqual(DArray(int, DSize(4)), DArray(int, DSize(4)))
        self.assertEqual(DArray(int, DSize(4)), DArray(DInt(), DSize(4)))
        self.assertNotEqual(DArray(int, DSize(4)), DArray(int, DSize(5)))
        self.assertNotEqual(DStr(), DStr(DSize(100)))
        self.assertEqual(DRecord(RecBI), DRecord(RecBI))

        # compound: DUnion
        self.assertEqual(DUnion([int, bool]), DUnion([bool, int]))
        self.assertEqual(DUnion([int, DUnion([int, bool])]),
                         DUnion([int, bool]))
        self.assertEqual(DUnion([int, DUnion([int, DUnion([int, bool])])]),
                         DUnion([int, bool]))
        self.assertEqual(DUnion([int, int]), DUnion([int]))
        self.assertNotEqual(DUnion([DInt()]), DInt())

        # special
        self.assertEqual(ForkedReturn(dict(x=int, y=bool, z=str)),
                         ForkedReturn(dict(x=int, y=bool, z=str)))
Exemplo n.º 15
0
 def test_DUnion_of_single(self):
     """DUnion of a single type is not converted to a single type."""
     port = InPort(NamespacedName("test_name", None), DUnion([DInt()]),
                   None, 0)
     self.assertEqual(port.port_type, DUnion([DInt()]))
     self.assertEqual(port.is_optional, False)
Exemplo n.º 16
0
 def test_DOptiona_of_DUnion(self):
     port = InPort(NamespacedName("test_name", None),
                   DOptional(DUnion([DInt(), DFloat()])), None, 0)
     self.assertEqual(port.port_type, DUnion([DInt(), DFloat()]))
     self.assertEqual(port.is_optional, True)
Exemplo n.º 17
0
    def test_can_save_to_tempfile(self):
        """Test StateSaver can save to a file."""
        st = [(k, k**2) for k in range(5)]
        # Note the conversion to a list as the json format doesn't care
        # for tuples.
        st_expected = "\n".join(repr(list(x)) for x in st)

        items = [
            ((int, int), (42, 100), "[42, 100]"),
            ((int, int), st, st_expected),
            (str, "Hello", '"Hello"'),
            (bool, True, "true"),
            (float, 3.91, "3.91"),
            (DTuple([int, int]), (1, 2), "[1, 2]"),
            (DUnion([int, float]), 90, "90"),
            (DUnion([int, float]), 90.0, "90.0"),
            (complex, 1j, '{"real": 0.0, "imaginary": 1.0}'),
            (SimpleRecord, SimpleRecord(x=1, y=True), '{"x": 1, "y": true}'),
            (
                ComplexRecord,
                ComplexRecord(x=1 + 2j),
                '{"x": {"real": 1.0, "imaginary": 2.0}}'
            ),
            (
                NestedRecord,
                NestedRecord(x=3, y=SimpleRecord(x=1, y=True)),
                '{"x": 3, "y": {"x": 1, "y": true}}'
            ),
            (
                ArrayRecord,
                ArrayRecord(x=[ComplexRecord(x=-1+4j)]),
                '{"x": [{"x": {"real": -1.0, "imaginary": 4.0}}]}'
            )
        ]

        for i, item in enumerate(items):
            t, data, expected = item
            with self.subTest(i=i):
                with tempfile.NamedTemporaryFile(mode="w+") as f:
                    s = StateSaver(t, verbose=True, filename=f.name)

                    @DeltaBlock(allow_const=False)
                    def save_things_node() -> object:
                        # If it's a list, save them independently, otherwise
                        # it's just one thing.
                        if type(data) == list:
                            for d in data:
                                s.save(d)
                        else:
                            s.save(data)
                        raise DeltaRuntimeExit

                    with DeltaGraph() as graph:
                        save_things_node()

                    rt = DeltaPySimulator(graph)
                    rt.run()

                    f.seek(0)

                    contents = f.read()
                    self.assertEqual(contents, f"{expected}\n")