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
combine_asym_error(errs, N=10000)[source]

combine asymmetry uncertanties using convolution

>>> a, b = combine_asym_error([[-0.4, 0.4], 0.3])
>>> assert abs(a+0.5) < 0.01
>>> assert abs(b-0.5) < 0.01
create_dir(name)[source]
create_test_config(model_name, params={}, plot_params={})[source]
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, dig=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.

plot_particle_model(model_name, params={}, plot_params={}, axis=None, special_points=None)[source]
plot_pole_function(model_name, params={}, plot_params={}, axis=None, **kwargs)[source]
pprint(dicts)[source]

Print dictionary using json format.

print_dic(dic)[source]

Another way to print dictionary.

save_frac_csv(file_name, fit_frac)[source]
search_interval(px, cl=0.6826894921370859, xrange=(0, 1))[source]

Search interval (a, b) that satisfly \(p(a)=p(b)\) and

\[\frac{\int_{a}^{b} p(x) dx}{\int_{x_{min]}^{x_{max}} p(x) dx} = cl\]
>>> x = np.linspace(-10, 10, 10000)
>>> a, b = search_interval(np.exp(-x**2/2), xrange=(-10, 10))
>>> assert abs(a+1) < 0.01
>>> assert abs(b-1) < 0.01
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, ignore_items=['sum_diag'])[source]