load_audio_representation

ketos.data_handling.parsing.load_audio_representation(path, name=None, return_unparsed=False)[source]

Load audio representation from JSON file.

By default the function attempts to parse the individual parameter values, e.g., the value “20 kHz” will be returned as 20000 and the value “11 ms” will be returned as 0.011. Use the return_unparsed argument to change this behaviour.

Args:
path: str

Path to json file

name: str

Heading of the relevant section of the json file. If None, the function returns the entire content of the JSON file.

return_unparsed: bool

Do not parse the parameter values. Default is False.

Returns:
d: dict

Audio representation

Example:
>>> import json
>>> import os
>>> from ketos.data_handling.parsing import load_audio_representation
>>> # create json file with spectrogram settings
>>> json_str = '{"spectrogram": {"type": "MagSpectrogram", "rate": "20 kHz", "window": "0.1 s", "step": "0.025 s", "window_func": "hamming", "freq_min": "30Hz", "freq_max": "3000Hz"}}'
>>> path = 'ketos/tests/assets/tmp/config.py'
>>> file = open(path, 'w')
>>> _ = file.write(json_str)
>>> file.close()
>>> # load settings back from json file
>>> settings = load_audio_representation(path=path, name='spectrogram')
>>> print(settings)
{'type': <class 'ketos.audio.spectrogram.MagSpectrogram'>, 'rate': 20000.0, 'window': 0.1, 'step': 0.025, 'window_func': 'hamming', 'freq_min': 30, 'freq_max': 3000}
>>> # clean up
>>> os.remove(path)

It is also possible to pass a custom audio representation class to this function. In this case, include a key/value pair indicating the path to the module you are loading the class from. For instance:

>>> import json 
>>> import os 
>>> from ketos.data_handling.parsing import load_audio_representation 
>>> # create json file with spectrogram settings
>>> json_str = '{"custom_representation": {"type": "Cepstrum", "module": "path/to/my/audio_representation.py", "any": "parameter", "for": "the", "custom": "representation"}}' 
>>> path = 'my/custom/config.py' 
>>> settings = load_audio_representation(path=path, name='custom_representation') 
>>> print(settings) 
{'type': <class 'audio_representation.Cepstrum'>, "module": "path/to/my/audio_representation.py", "any": "parameter", "for": "the", "custom": "representation"}