pybop._utils#

Classes#

NumpyEncoder

Numpy serialiser helper class that converts numpy arrays to a list.

Functions#

add_spaces(string)

Return the class name as a string with spaces before each new capitalised word.

is_numeric(x)

Check if a variable is numeric.

load_data_dict(→ dict)

Load data as dictionary from a given file. Restores data saved with

save_data_dict(→ str | None)

Save data from given data dictionary

Module Contents#

class pybop._utils.NumpyEncoder(*, skipkeys=False, ensure_ascii=True, check_circular=True, allow_nan=True, sort_keys=False, indent=None, separators=None, default=None)[source]#

Bases: json.JSONEncoder

Numpy serialiser helper class that converts numpy arrays to a list. Numpy arrays cannot be directly converted to JSON, so the arrays are converted to python list objects before encoding.

default(obj)[source]#

Implement this method in a subclass such that it returns a serializable object for o, or calls the base implementation (to raise a TypeError).

For example, to support arbitrary iterators, you could implement default like this:

def default(self, o):
    try:
        iterable = iter(o)
    except TypeError:
        pass
    else:
        return list(iterable)
    # Let the base class default method raise the TypeError
    return super().default(o)
pybop._utils.add_spaces(string)[source]#

Return the class name as a string with spaces before each new capitalised word.

pybop._utils.is_numeric(x)[source]#

Check if a variable is numeric.

pybop._utils.load_data_dict(filename: str, file_format: str = 'pickle', data_keys_0d: list[str] | None = None, data_keys_1d: list[str] | None = None) dict[source]#

Load data as dictionary from a given file. Restores data saved with save_data_dict.

Parameters:
  • filename (str) – The name of the file containing the data.

  • file_format (str, optional) – The format the data was save to. Options are: - ‘pickle’ (default) - ‘matlab’ - ‘csv’ - ‘json’

  • data_keys_0d (list[str], optional) – A list of keys for which the data is a scalar/0-dimensional. This is only needed for file_format=’matlab’ or file_format = ‘csv’. scipy.io.savemat turns any data into a multi-dimensional array with at least 2 dimensions. If provided, data dimensions will be consistent with the original data.

  • data_keys_1d (list[str], optional) – A list of keys for which the data is a 1-dimensional list or array. This is only needed for file_format=’matlab’. scipy.io.savemat turns any data into a multi-dimensional array with at least 2 dimensions. If provided, data dimensions will be consistent with the original data.

Returns:

python dictionary containing the data in the file.

Return type:

data_dict

pybop._utils.save_data_dict(data_dict: dict, filename: str | None = None, to_format: str = 'pickle') str | None[source]#

Save data from given data dictionary

Based on pybamm.Solution.save_data

Parameters:
  • filename (str, optional) – The name of the file to save data to. If None, then a str is returned

  • to_format (str, optional) –

    The format to save to. Options are:

    • ’pickle’ (default): creates a pickle file with the data dictionary

    • ’matlab’: creates a .mat file, for loading in matlab

    • ’csv’: creates a csv file (0D variables only)

    • ’json’: creates a json file

Returns:

data – str if ‘json’ is chosen and filename is None, otherwise None

Return type:

str, optional