apollon.io module

apollon/io.py – General I/O functionallity.

Classes:
ArrayEncoder Serialize numpy array to JSON. FileAccessControl Descriptor for file name attributes.
Functions:
array_print_opt Set format for printing numpy arrays. decode_array Decode numpy array from JSON. files_in_folder Iterate over all files in given folder. load Load pickled data. repath Change path but keep file name. save Pickle some data.
class apollon.io.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(o)

Custon default JSON encoder. Properly handles numpy arrays and JSONEncoder.default for all other types.

Params:
o (any) Object to encode.
Returns:(dict)
class apollon.io.PoissonHmmEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)

Bases: apollon.io.ArrayEncoder

JSON encoder for PoissonHmm.

default(o)

Custon default JSON encoder. Properly handles <class ‘PoissonHMM’>.

Note: Falls back to ArrayEncoder for all types that do not implement a to_dict() method.

Params:
o (any) Object to encode.
Returns:(dict)
class apollon.io.WavFileAccessControl

Bases: object

Control initialization and access to the file attribute of class:AudioData.

This assures that the path indeed points to a file, which has to be a .wav file. Otherwise an error is raised. The path to the file is saved as absolute path and the attribute is read-only.

apollon.io.array_print_opt(*args, **kwargs)

Set print format for numpy arrays.

Thanks to unutbu: https://stackoverflow.com/questions/2891790/how-to-pretty-print-a-numpy-array-without- scientific-notation-and-with-given-pre

apollon.io.decode_array(json_data: dict) → Any

Properly decodes numpy arrays from a JSON data stream.

This method need to be called on the return value of json.load or json.loads.

Parameters:json_data (dict) –
Returns:(any)
apollon.io.dump_json(obj, path: Union[str, pathlib.Path] = None) → None

Write obj to JSON.

This function can handel numpy arrays.

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

Parameters:
  • obj (any) –
  • path (PathType) –
apollon.io.load(path: Union[str, pathlib.Path]) → Any

Load a pickled file.

Parameters:path (str) –
Returns:(object) unpickled object
apollon.io.repath(current_path: Union[str, pathlib.Path], new_path: Union[str, pathlib.Path], ext: str = None) → Union[str, pathlib.Path]

Change the path and keep the file name. Optinally change the extension, too.

Parameters:
  • current_path (str or Path) –
  • new_path (str or Path) –
  • ext (str or None) –
Returns:

(pathlib.Path)

apollon.io.save(data: Any, path: Union[str, pathlib.Path])

Pickles data to path.

Parameters:
  • data (Any) –
  • path (str or Path) –