Utilities

Utility functions for handling files and plotting data.

ionworkspipeline.util.subplots_autolayout(n, *args, n_rows=None, figsize=None, layout='constrained', **kwargs)

Create a subplot element

ionworkspipeline.util.export_python_script(parameter_values, filename, data_path=None, format_with_black=True)

Print a python script that can be used to reproduce the parameter set

Parameters

parameter_valuesiwp.ParameterValues

The parameter values to export

filenamestring

The name to save the parameter set under

data_pathstring, optional

The path to the where to look for data, e.g. when loading using PyBaMM’s process_parameter_data functions. If None, then data will be looked for in the same directory as the exported file. Default is None.

format_with_blackbool, optional

Whether to format the output with black. Default is True.

ionworkspipeline.util.exp_safe(x)

Safe exponential function that avoids overflow.

ionworkspipeline.util.square_safe(x)

Safe square function that avoids overflow.

ionworkspipeline.util.nanmin(x: ndarray | list | float, *args, **kwargs) float | ndarray

Minimum function that returns infinity if all values are NaN.

Examples

>>> nanmin([1.0, np.nan, 3.0])
1.0
>>> nanmin([np.nan, np.nan])
inf
ionworkspipeline.util.nanmax(x: ndarray | list | float, *args, **kwargs) float | ndarray

Maximum function that returns negative infinity if all values are NaN.

Examples

>>> nanmax([1.0, np.nan, 3.0])
3.0
>>> nanmax([np.nan, np.nan])
-inf
ionworkspipeline.util.nanargmin(x: ndarray | list | float, *args, **kwargs) int

Argument of the minimum function that returns 0 if all values are NaN.

Examples

>>> nanargmin([3.0, np.nan, 1.0, 2.0])
2
>>> nanargmin([np.nan, np.nan])
0
ionworkspipeline.util.nanargmax(x: ndarray | list | float, *args, **kwargs) int

Argument of the maximum function that returns 0 if all values are NaN.

Examples

>>> nanargmax([1.0, np.nan, 3.0, 2.0])
2
>>> nanargmax([np.nan, np.nan])
0
ionworkspipeline.util.FunctionPrefactor(function, prefactor)

Wrapper for a function that, when called, multiplies the original function output by a prefactor.

Parameters

functioncallable

The function to wrap.

prefactorfloat, int, or pybamm.Parameter

The prefactor to multiply the output of the function by.

Examples

>>> def my_function(x):
...     return x**2
>>> my_function_prefactor = FunctionPrefactor(my_function, 2)
>>> my_function_prefactor(3)
18
ionworkspipeline.util.InputFunctionWrapper(fun)

Wrapper for a pybamm expression that moves the inputs argument to to the first position.

Parameters

funpybamm.Symbol

The expression to wrap.

class ionworkspipeline.util.FunctionTimeout(function: Callable, maxiters: int | None, maxtime: float | None)

A class to timeout a function based on the number of iterations or the time. Once the maximum number of iterations or the maximum time is reached, the function will raise a FunctionTimeoutError.

Parameters

functioncallable

The function to wrap.

maxitersint | None

The maximum number of iterations.

maxtimefloat | None

The maximum time in seconds.

class ionworkspipeline.util.FunctionTimeoutError

Exception raised when the function timeout is reached.

Extends: builtins.RuntimeError