コード例 #1
0
 def test_complex128_PA(self):
     pa = numpy.array(
         [[complex(1, 0.1), complex(-2.0, 0)],
          [complex(-3.0, 1), complex(4.0, -1.0)]],
         dtype="complex128",
     ).view(numpy.PackedArray)
     self.ensure_roundtrip(pa)
コード例 #2
0
def encode_image(serializer, img):
    # some PIL mode are directly mapped to WL ones. Best case fast (de)serialization.
    try:
        if img.mode in MODE_MAPPING:
            wl_data_type, colorspace, interleaving = MODE_MAPPING[img.mode]
            return serializer.encode(
                wl.Image(
                    normalize_array(numpy.array(img)),
                    wl_data_type,
                    ColorSpace=colorspace or wl.Automatic,
                    Interleaving=interleaving,
                )
            )
    except ImportError:
        pass
    # try to use format and import/export, may fail during save() and raise exception.
    stream = six.BytesIO()
    img_format = img.format or "PNG"
    try:
        img.save(stream, format=img_format)
    except KeyError:
        raise NotImplementedError("Format %s is not supported." % img_format)
    return serializer.serialize_function(
        serializer.serialize_symbol(b"ImportByteArray"),
        (
            serializer.serialize_bytes(stream.getvalue(), as_byte_array=True),
            serializer.serialize_string(img_format),
        ),
    )
コード例 #3
0
 def test_int8_RA(self):
     self.compare_serializer(
         self.initOnlyRA(),
         numpy.array([[-(1 << 7), -1], [1, (1 << 7) - 1]], numpy.int8),
         b"\x38\x3a\xc2\x00\x02\x02\x02\x80\xff\x01\x7f",
         test_export_wxf=True,
     )
コード例 #4
0
ファイル: wxf_numpy.py プロジェクト: cosmosZhou/sagemath
    def test_int16(self):
        s = self.initDefault()
        sRA = self.initOnlyRA()
        arr = numpy.array([[-(1 << 15)], [(1 << 15) - 1]], numpy.int16)

        self.compare_serializer(s, arr, b"8:\xc1\x01\x02\x02\x01\x00\x80\xff\x7f")
        self.compare_serializer(
            sRA, arr, b"8:\xc2\x01\x02\x02\x01\x00\x80\xff\x7f", test_export_wxf=True
        )
コード例 #5
0
 def test_bool_img(self):
     a = numpy.array([[1, 0], [0, 1]], dtype='bool')
     img = PIL.fromarray(a)
     out = export(img, target_format='wl')
     self.assertTrue(
         out ==
         b'Image[BinaryDeserialize[ByteArray["ODrCEAICAgEAAAA="]], "Bit", Rule[ColorSpace, Automatic], Rule[Interleaving, True]]'
         or out ==
         b'Image[BinaryDeserialize[ByteArray["ODrCEAICAgEAAAA="]], "Bit", Rule[Interleaving, True], Rule[ColorSpace, Automatic]]'
     )
コード例 #6
0
    def test_int64(self):

        arr = numpy.array([[-(1 << 62)], [(1 << 62)]], numpy.int64)
        self.compare_serializer(
            self.initDefault(), arr,
            b'8:\xc1\x03\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\xc0\x00\x00\x00\x00\x00\x00\x00@'
        )
        self.compare_serializer(
            self.initOnlyRA(), arr,
            b'8:\xc2\x03\x02\x02\x01\x00\x00\x00\x00\x00\x00\x00\xc0\x00\x00\x00\x00\x00\x00\x00@'
        )
コード例 #7
0
    def test_int32(self):

        arr = numpy.array([[-(1 << 31)], [(1 << 31) - 1]], numpy.int32)

        self.compare_serializer(
            self.initDefault(), arr,
            b'8:\xc1\x02\x02\x02\x01\x00\x00\x00\x80\xff\xff\xff\x7f')
        self.compare_serializer(
            self.initOnlyRA(),
            arr,
            b'8:\xc2\x02\x02\x02\x01\x00\x00\x00\x80\xff\xff\xff\x7f',
            test_export_wxf=True)
コード例 #8
0
def encode_packed_array(serializer, o):
    try:
        wl_type, cast_to = PACKEDARRAY_NUMPY_MAPPING[o.dtype.type]
    except KeyError:
        raise NotImplementedError(
            "Packed array serialization not implemented for %s. Choices are: %s"
            % (repr(o.dtype), ", ".join(
                map(repr, PACKEDARRAY_NUMPY_MAPPING.keys()))))
    o = to_little_endian(o)
    if cast_to is not None:
        o = numpy.array(o, dtype=cast_to)
    if hasattr(o, "tobytes"):
        # Numpy 1.9+ support array.tobytes, but previous versions don't and use tostring instead.
        data = o.tobytes()
    else:
        data = o.tostring()

    return serializer.serialize_packed_array(data, o.shape, wl_type)
コード例 #9
0
    def test_python_array(self):

        for array, numpy_type, wl_type in (
            ([True, False, True, False, True, False], numpy.int8, "Integer8"),
            ([1, 2, 3, 4, 5, 6], numpy.int8, "Integer8"),
            ([1, 2, 3, 4, 5, 6], numpy.int32, "Integer32"),
            ([1, 2, 3, 4, 5, 6], numpy.int64, "Integer64"),
            ([1.2, 2.3, 3, 4, 5, 6], numpy.float32, "Real32"),
            ([1.2, 2.3, 3, 4, 5, 6], numpy.float64, "Real64"),
        ):

            for shape in ((3, 2), None):

                arr = numpy.array(array, numpy_type)
                if shape:
                    arr = arr.reshape(shape)

                self.assertEqual(
                    export(arr, target_format="wxf"),
                    export(NumericArray(array, wl_type, shape=shape), target_format="wxf"),
                )
コード例 #10
0
 def test_int32(self):
     pa = numpy.array([[-(1 << 31)], [(1 << 31) - 1]], numpy.int32).view(numpy.PackedArray)
     self.ensure_roundtrip(pa)
コード例 #11
0
 def test_int8_PA(self):
     self.compare_serializer(
         self.initDefault(),
         numpy.array([[-(1 << 7), -1], [1, (1 << 7) - 1]], numpy.int8),
         b"\x38\x3a\xc1\x00\x02\x02\x02\x80\xff\x01\x7f",
     )
コード例 #12
0
 def test_int8_Both(self):
     self.compare_serializer(
         self.initBothArraySupport(),
         numpy.array([[-(1 << 7), -1], [1, (1 << 7) - 1]], numpy.int8),
         b"\x38\x3a\xc1\x00\x02\x02\x02\x80\xff\x01\x7f",
     )
コード例 #13
0
 def test_uint8_PA(self):
     self.compare_serializer(
         self.initDefault(),
         numpy.array([[0, (1 << 7)]], numpy.uint8),
         b"\x38\x3a\xc1\x01\x02\x01\x02\x00\x00\x80\x00",
     )
コード例 #14
0
 def test_uint64_RA(self):
     self.compare_serializer(
         self.initBothArraySupport(),
         numpy.array([0, (1 << 64) - 1], numpy.uint64),
         b"8:\xc2\x13\x01\x02\x00\x00\x00\x00\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff",
     )
コード例 #15
0
 def test_int16_be(self):
     arr = numpy.array([[-(1 << 15)], [(1 << 15) - 1]], numpy.int16)
     wxf = export(arr, target_format="wxf")
     self.assertEqual(wxf, b"8:\xc2\x01\x02\x02\x01\x00\x80\xff\x7f")
コード例 #16
0
 def test_int32_be(self):
     arr = numpy.array([[-(1 << 31)], [(1 << 31) - 1]], dtype=">i4")
     wxf = export(arr, target_format="wxf")
     self.assertEqual(
         wxf, b"8:\xc2\x02\x02\x02\x01\x00\x00\x00\x80\xff\xff\xff\x7f")
コード例 #17
0
 def test_double_PA(self):
     pa = numpy.array([[1.0, -2.0], [-3.0, 4.0]], dtype="double").view(numpy.PackedArray)
     self.ensure_roundtrip(pa)
コード例 #18
0
 def test_uint32_RA(self):
     self.compare_serializer(
         self.initBothArraySupport(),
         numpy.array([0, (1 << 32) - 1], numpy.uint32),
         b'8:\xc2\x12\x01\x02\x00\x00\x00\x00\xff\xff\xff\xff')
コード例 #19
0
 def test_uint32_RA(self):
     pa = numpy.array([0, (1 << 32) - 1], numpy.uint32).view(numpy.PackedArray)
     res = binary_deserialize(export(pa, target_format="wxf"))
     numpy.assert_array_equal(res, numpy.array([0, (1 << 32) - 1], numpy.int64))
コード例 #20
0
 def test_uint64_RA(self):
     with self.assertRaises(NotImplementedError):
         pa = numpy.array([0, (1 << 64) - 1], numpy.uint64).view(numpy.PackedArray)
         export(pa, target_format="wxf")
コード例 #21
0
 def test_int64(self):
     pa = numpy.array([[-(1 << 62)], [(1 << 62)]], numpy.int64).view(numpy.PackedArray)
     self.ensure_roundtrip(pa)
コード例 #22
0
 def test_numpy_1d_array(self):
     arr = numpy.array([0, 1], 'uint8')
     wxf = export(arr, target_format='wxf')
     res = binary_deserialize(wxf, consumer=WXFConsumerNumpy())
     self.assertListEqual(res.tolist(), arr.tolist())
コード例 #23
0
 def test_int8_PA(self):
     pa = numpy.array([[-(1 << 7), -1], [1, (1 << 7) - 1]], numpy.int8).view(
         numpy.PackedArray
     )
     self.ensure_roundtrip(pa)
コード例 #24
0
 def test_numpy_2d_array(self):
     arr = numpy.array([[0, 1], [1, 1], [2, 1]], "uint8")
     wxf = export(arr, target_format="wxf")
     res = binary_deserialize(wxf, consumer=WXFConsumerNumpy())
     self.assertEqual(res.tolist(), arr.tolist())
コード例 #25
0
 def test_numpy_1d_array(self):
     arr = numpy.array([0, 1], "uint8")
     wxf = export(arr, target_format="wxf")
     res = binary_deserialize(wxf)
     numpy.assert_array_equal(res, arr)