File: numpy.py

package info (click to toggle)
python-trame-client 3.10.4-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 3,128 kB
  • sloc: python: 9,583; javascript: 3,897; sh: 9; makefile: 6
file content (55 lines) | stat: -rw-r--r-- 1,391 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import json

import numpy as np

np_version = tuple(map(int, np.__version__.split(".")))
if np_version < (2, 0, 0):
    NP_FLOATS = (np.float_, np.float16, np.float32, np.float64)
    NP_COMPLEX = (np.complex_, np.complex64, np.complex128)
else:
    NP_FLOATS = (np.float16, np.float32, np.float64)
    NP_COMPLEX = (np.complex64, np.complex128)


class NumpyEncoder(json.JSONEncoder):
    """Custom encoder for numpy data types"""

    def default(self, obj):
        if isinstance(
            obj,
            (
                np.int_,
                np.intc,
                np.intp,
                np.int8,
                np.int16,
                np.int32,
                np.int64,
                np.uint8,
                np.uint16,
                np.uint32,
                np.uint64,
            ),
        ):
            return int(obj)

        elif isinstance(obj, NP_FLOATS):
            return float(obj)

        elif isinstance(obj, NP_COMPLEX):
            return {"real": obj.real, "imag": obj.imag}

        elif isinstance(obj, (np.ndarray,)):
            return obj.tolist()

        elif isinstance(obj, (np.bool_)):
            return bool(obj)

        elif isinstance(obj, (np.void)):
            return None

        return json.JSONEncoder.default(self, obj)


def encode(data_structure):
    return json.loads(json.dumps(data_structure, cls=NumpyEncoder))