utils

This module provides some functions that may be useful in other modules.

class AttrDict[source]

Bases: dict

array_split(data, batch=None)[source]

Split a data array. batch is the number of data in a row.

check_positive_definite(m)[source]

check if matrix m is postive definite

>>> check_positive_definite([[1.0,0.0],[0.0, 0.1]])
True
>>> check_positive_definite([[1.0,0.0],[1.0,-0.1]])
eigvalues:  [-0.1  1. ]
False
deep_iter(base, deep=1)[source]
deep_ordered_iter(base, deep=1)[source]
deep_ordered_range(size, deep=1, start=0)[source]
error_print(x, err=None)[source]

It returns a format string “value +/- error”. The precision is modified according to err

Parameters
  • x – Value

  • err – Error

Returns

String

fit_normal(data, weights=None)[source]

Fit data distribution with Gaussian distribution. Though minimize the negative log likelihood function

\[- \ln L = \frac{1}{2}\sum w_i \frac{(\mu - x_i )^2}{\sigma^2} + (\sum w_i) \ln (\sqrt{2\pi} \sigma )\]

the fit result can be solved as

\[\frac{\partial (-\ln L)}{\partial \mu} = 0 \Rightarrow \bar{\mu} = \frac{\sum w_i x_i}{ \sigma^2 \sum w_i}\]
\[\frac{\partial (-\ln L)}{\partial \sigma} = 0 \Rightarrow \bar{\sigma} = \sqrt{\frac{\sum w_i (\bar{\mu} - x_i)^2}{\sum w_i}}\]

From hessian

\[\frac{\partial^2 (-\ln L)}{\partial \mu^2} = \frac{\sum w_i}{\sigma^2}\]
\[\frac{\partial^2 (-\ln L)}{\partial \sigma^2} = 3\sum \frac{\sum w_i (\mu - x)^2}{\sigma^4} - \frac{\sum w_i}{\sigma^2}\]

the error matrix can wrotten as [[ \(\bar{\sigma}^2/N\) , 0], [0, \(\bar{\sigma}^2/(2N)\) ]] .

flatten_dict_data(data, fun=<built-in method format of str object>)[source]

Flatten nested dictionary data into one layer dictionary.

Returns

Dictionary

flatten_np_data(data)
is_complex(x)[source]

If x is of type complex, it returns True.

load_config_file(name)[source]

Load config file such as Resonances.yml.

Parameters

name – File name. Either yml file or json file.

Returns

Dictionary read from the file.

pprint(dicts)[source]

Print dictionary using json format.

print_dic(dic)[source]

Another way to print dictionary.

std_periodic_var(p, mid=0.0, pi=3.141592653589793)[source]

Transform a periodic variable into its range.

>>> std_periodic_var(math.pi)
-3.1415...
>>> std_periodic_var(2*math.pi + 0.01)
0.0...
Parameters
  • p – Value

  • mid – The middle value

  • pi – Half-range

Returns

The transformed value

std_polar(rho, phi)[source]

To standardize a polar variable. By standard form, it means \(\rho>0, -\pi<\phi<\pi\).

Parameters
  • rho – Real number

  • phi – Real number

Returns

rho, phi

time_print(f)[source]

It provides a wrapper to print the time cost on a process.

tuple_table(fit_frac)[source]