apollon.io.json module

apollon/io/json.py – General JSON IO.

Licensed under the terms of the BSD-3-Clause license. Copyright (C) 2020 Michael Blaß, mblass@posteo.net

Classes:

ArrayEncoder

Functions:

dump decode_ndarray encode_ndarray load validate_ndarray

class apollon.io.json.ArrayEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Bases: json.encoder.JSONEncoder

Encode np.ndarrays to JSON.

Simply set the cls parameter of the dump method to this class.

default(inp: Any)Any

Custon SON encoder for numpy arrays. Other types are passed on to JSONEncoder.default.

Parameters

inp – Object to encode.

Returns

JSON-serializable dictionary.

apollon.io.json.decode_ndarray(instance: dict)numpy.ndarray

Decode numerical numpy arrays from a JSON data stream.

Parameters

instance – Instance of ndarray.schema.json.

Returns

Numpy array.

apollon.io.json.dump(obj: Any, path: Union[str, pathlib.Path])None

Write obj to JSON file.

This function can handel numpy arrays.

If path is None, this fucntion writes to stdout. Otherwise, encoded object is written to path.

Parameters
  • obj – Object to be encoded.

  • path – Output file path.

apollon.io.json.encode_ndarray(arr: numpy.ndarray)dict

Transform an numpy array to a JSON-serializable dict.

Array must have a numerical dtype. Datetime objects are currently not supported.

Parameters

arr – Numpy ndarray.

Returns

JSON-serializable dict adhering ndarray.schema.json.

apollon.io.json.load(path: Union[str, pathlib.Path])

Load JSON file.

Parameters

path – Path to file.

Returns

JSON file as FeatureSpace.

apollon.io.json.load_schema(schema_name: str)dict

Load a JSON schema.

This function searches within apollon’s own schema repository. If a schema is found it is additionally validated agains Draft 7.

Parameters

schema_name – Name of schema. Must be file name without extension.

Returns

Schema instance.

Raises

IOError

apollon.io.json.validate_ndarray(encoded_arr: dict)bool

Check whether encoded_arr is a valid instance of ndarray.schema.json.

Parameters

encoded_arr – Instance to validate.

Returns

True, if instance is valid.