Versatile#

For versatile utility.

check_black()[source]#

Check if black is installed or not.

Returns:

  • (bool) – installed ?

check_dict_keys(d, ref, name='input', allowed=None, free=None)[source]#

Check if all keys in dict are known, and raise error for unknown keys.

Parameters:
  • d (dict) – dict to check.

  • ref (dict) – reference dict (default values).

  • name (str, optional) – name of dict used in error message.

  • allowed (dict, optional) – dict[path, [key]], acceptable keys at path instead of those in ref.

  • free (list, optional) – paths whose keys are not checked.

Raises:

ValueError – if unknown keys are found.

Note

  • path is keys joined by “/”, e.g., “output/dispersion”.

  • sub dicts are checked recursively when both values in d and ref are dict.

check_latex()[source]#

Check if LaTeX is installed or not.

Returns:

  • (bool) – installed ?

check_qtdraw()[source]#

Check if qtdraw is installed or not.

Returns:

  • (bool) – installed ?

deep_update(d, u)[source]#

Update dict with deepcopy.

Parameters:
  • d (dict) – dict to update (inplace).

  • u (dict) – additional dict.

do_black(w_dir, pattern='*.py')[source]#

Execute black for Python file.

Parameters:
  • w_dir (str) – directory.

  • pattern (str, optional) – pattern for black.

get_n_jobs()[source]#

Get number of parallel jobs.

Returns:

-1 (all cores)]

Return type:

  • (int) – number of jobs for joblib, given by environment variable MULTIPIE_N_JOBS. [default

is_integer(x)[source]#

Is x an integer (Python or NumPy integer, but not bool) ?

Parameters:

x (any) – object.

Returns:

  • (bool) – is integer ?

normalize_vector(vec, tol=1e-11)[source]#

Normalize vector (sympy or complex/float).

Parameters:
  • vec (array-like) – list of vectors.

  • tol (float, optional) – absolute norm tolerance for float.

Returns:

  • (ndaray) – normalized vector.

parallel_map(func, args_list, size)[source]#

Apply function to each argument tuple in parallel (joblib).

Parameters:
  • func (callable) – function, picklable (module-level).

  • args_list (list) – list of argument tuples, [tuple].

  • size (int) – size of total work, e.g., no. of sympy elements.

Returns:

  • (list) – results in the order of args_list.

Note

  • the number of jobs is given by get_n_jobs().

  • it runs serially for MULTIPIE_N_JOBS=1, a single task, or size < PARALLEL_MIN_SIZE.

progress_bar_done(length=50, label='')[source]#

Finalize progress bar.

Parameters:
  • length (int, optional) – width.

  • label (str, optional) – prefix.

progress_bar_step(length=50, label='')[source]#

Show progress bar.

Parameters:
  • length (int, optional) – width.

  • label (str, optional) – prefix.

read_dict(filename, r_dir=None)[source]#

Read dict.

Parameters:
  • filename (str) – file name.

  • r_dir (str, optional) – directory to read, if None, cwd is used.

Returns:

  • (dict) – read dict.

Note

  • the file must contain exactly one dict, “var = {…}” or “{…}”, and no other statements except for docstrings and comments.

read_dict_file(data, topdir=None, verbose=False)[source]#

Read dict file or dict.

Parameters:
  • data (str or dict) – filename or dict.

  • topdir (str, optional) – top directory.

  • verbose (bool, optional) – verbose ?

Returns:

  • (dict) – dict[var, data_dict].

Note

  • if topdir is None, current directory is used.

  • relative file names are read from topdir. The current directory is not changed.

replace(a, s)[source]#

Replace expression (exchange among variables is ok).

Parameters:
  • a (ndarray) – array.

  • s (dict) – dict for substitution.

Returns:

  • (ndarray) – replaced array.

setup_logging(level=20)[source]#

Setup logging of MultiPie, i.e., the “multipie” logger, to write messages to stdout.

Parameters:

level (int, optional) – log level.

Note

  • the root logger and the other loggers of the application are not changed.

  • if the “multipie” logger already has a handler (set by MultiPie or by the user), nothing is changed.

simplify(obj, full_factor=False)[source]#

Simplify expressions.

Parameters:
  • obj (sympy or ndarray) – expressions.

  • full_factor (bool, optional) – use factor at last?

Returns:

  • (sympy or ndarray) – simplied expressions.

simplify_ex(ex, full_factor=False)[source]#

Simplify expression.

Parameters:
  • ex (sympy) – sympy expression.

  • full_factor (bool, optional) – use factor at last?

Returns
  • (sympy) – simplified expression.

str_to_sympy(s, check_var=None, check_shape=None, rational=True, subs=None, **assumptions)[source]#

Convert a string to a sympy.

Parameters:
  • s (str) – a string.

  • check_var (list, optional) – variables to accept, None (all).

  • check_shape (tuple, optional) – shape, (), (n,), (n,m), …

  • rational (bool, optional) – use rational number ?

  • subs (dict, optional) – replace dict for local variables.

  • **assumptions (dict, optional) – common assumptions for all variables.

Returns:

  • (ndarray) – (list of) sympy.

Notes

  • if format error occurs, raise ValueError.

  • if s cannot be converted to a sympy, raise ValueError.

time_stamp()[source]#

Get current time stamp.

Returns:

  • (str) – time stamp.

timer(name=None, verbose=True)[source]#
to_latex(a, style='scalar')[source]#

convert list to latex list.

Parameters:
  • a (array-like) – list of sympy.

  • style (str, optional) – style, “scalar/vector/matrix”.

Returns:

  • (ndarray or str) – (list of) LaTeX string without “$”.

write_dict(dic, filename, var=None, comment='', w_dir=None)[source]#

Write dict.

Parameters:
  • dic (dict) – dict to write.

  • filename (str) – file name.

  • var (str, optional) – dict variable, if None, filename is used.

  • comment (str, optional) – comment.

  • w_dir (str, optional) – directory to write, if None, cwd is used.