turbo_broccoli.custom.scipy

scipy objects

 1"""scipy objects"""
 2
 3from typing import Any, Callable, Tuple
 4
 5from scipy.sparse import csr_matrix
 6
 7from ..context import Context
 8from ..exceptions import DeserializationError, TypeNotSupported
 9
10
11def _csr_matrix_to_json(m: csr_matrix, ctx: Context) -> dict:
12    return {
13        "__type__": "scipy.csr_matrix",
14        "__version__": 2,
15        "data": m.data,
16        "dtype": m.dtype,
17        "indices": m.indices,
18        "indptr": m.indptr,
19        "shape": m.shape,
20    }
21
22
23def _json_to_csr_matrix(dct: dict, ctx: Context) -> csr_matrix:
24    decoders = {
25        2: _json_to_csr_matrix_v2,
26    }
27    return decoders[dct["__version__"]](dct, ctx)
28
29
30def _json_to_csr_matrix_v2(dct: dict, ctx: Context) -> csr_matrix:
31    return csr_matrix(
32        (dct["data"], dct["indices"], dct["indptr"]),
33        shape=dct["shape"],
34        dtype=dct["dtype"],
35    )
36
37
38def from_json(dct: dict, ctx: Context) -> Any:
39    decoders = {
40        "scipy.csr_matrix": _json_to_csr_matrix,
41    }
42    try:
43        type_name = dct["__type__"]
44        return decoders[type_name](dct, ctx)
45    except KeyError as exc:
46        raise DeserializationError() from exc
47
48
49def to_json(obj: Any, ctx: Context) -> dict:
50    """
51    Serializes a Scipy object into JSON by cases. See the README for the
52    precise list of supported types. The return dict has the following
53    structure:
54
55    - [`csr_matrix`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html#scipy.sparse.csr_matrix)
56
57        ```py
58        {
59            "__type__": "scipy.csr_matrix",
60            "__version__": 2,
61            "data": ...,
62            "dtype": ...,
63            "indices": ...,
64            "indptr": ...,
65            "shape": ...,
66        }
67        ```
68
69    """
70    encoders: list[Tuple[type, Callable[[Any, Context], dict]]] = [
71        (csr_matrix, _csr_matrix_to_json),
72    ]
73    for t, f in encoders:
74        if isinstance(obj, t):
75            return f(obj, ctx)
76    raise TypeNotSupported()
def from_json(dct: dict, ctx: turbo_broccoli.context.Context) -> Any:
39def from_json(dct: dict, ctx: Context) -> Any:
40    decoders = {
41        "scipy.csr_matrix": _json_to_csr_matrix,
42    }
43    try:
44        type_name = dct["__type__"]
45        return decoders[type_name](dct, ctx)
46    except KeyError as exc:
47        raise DeserializationError() from exc
def to_json(obj: Any, ctx: turbo_broccoli.context.Context) -> dict:
50def to_json(obj: Any, ctx: Context) -> dict:
51    """
52    Serializes a Scipy object into JSON by cases. See the README for the
53    precise list of supported types. The return dict has the following
54    structure:
55
56    - [`csr_matrix`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html#scipy.sparse.csr_matrix)
57
58        ```py
59        {
60            "__type__": "scipy.csr_matrix",
61            "__version__": 2,
62            "data": ...,
63            "dtype": ...,
64            "indices": ...,
65            "indptr": ...,
66            "shape": ...,
67        }
68        ```
69
70    """
71    encoders: list[Tuple[type, Callable[[Any, Context], dict]]] = [
72        (csr_matrix, _csr_matrix_to_json),
73    ]
74    for t, f in encoders:
75        if isinstance(obj, t):
76            return f(obj, ctx)
77    raise TypeNotSupported()

Serializes a Scipy object into JSON by cases. See the README for the precise list of supported types. The return dict has the following structure:

  • turbo_broccoli.custom.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html#scipy.sparse.csr_matrix">csr_matrix

    {
        "__type__": "scipy.csr_matrix",
        "__version__": 2,
        "data": ...,
        "dtype": ...,
        "indices": ...,
        "indptr": ...,
        "shape": ...,
    }