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 turbo_broccoli.context import Context 8from turbo_broccoli.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 38# pylint: disable=missing-function-docstring 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 48 49 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()
51def to_json(obj: Any, ctx: Context) -> dict: 52 """ 53 Serializes a Scipy object into JSON by cases. See the README for the 54 precise list of supported types. The return dict has the following 55 structure: 56 57 - [`csr_matrix`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html#scipy.sparse.csr_matrix) 58 59 ```py 60 { 61 "__type__": "scipy.csr_matrix", 62 "__version__": 2, 63 "data": ..., 64 "dtype": ..., 65 "indices": ..., 66 "indptr": ..., 67 "shape": ..., 68 } 69 ``` 70 71 """ 72 encoders: list[Tuple[type, Callable[[Any, Context], dict]]] = [ 73 (csr_matrix, _csr_matrix_to_json), 74 ] 75 for t, f in encoders: 76 if isinstance(obj, t): 77 return f(obj, ctx) 78 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": ..., }