HErmes.fitting package

Submodules

HErmes.fitting.fit module

Provide routines for fitting charge histograms

HErmes.fitting.fit.fit_model(charges, model, startparams=None, rej_outliers=False, nbins=200, silent=False, parameter_text=(('$\\mu_{{SPE}}$& {:4.2e}\\\\', 5), ), use_minuit=False, normalize=True, **kwargs)[source]

Standardazied fitting routine

Parameters:
  • charges (np.ndarray) – Charges obtained in a measurement (no histogram)
  • model (pyosci.fit.Model) – A model to fit to the data
  • startparams (tuple) – initial parameters to model, or None for first guess
Keyword Arguments:
 
  • rej_outliers (bool) – Remove extreme outliers from data
  • nbins (int) – Number of bins
  • parameter_text (tuple) – will be passed to model.plot_result
  • use_miniuit (bool) – use minuit to minimize startparams for best chi2
  • normalize (bool) – normalize data before fitting
  • silent (bool) – silence output
Returns:

tuple

HErmes.fitting.fit.reject_outliers(data, m=2)[source]

A simple way to remove extreme outliers from data

Parameters:
  • data (np.ndarray) – data with outliers
  • m (int) – number of standard deviations outside the data should be discarded
Returns:

np.ndarray

HErmes.fitting.functions module

Provide some simple functions which can be used to create models

HErmes.fitting.functions.calculate_chi_square(data, model_data)[source]

Very simple estimator for goodness-of-fit. Use with care. Non normalized bin counts are required.

Parameters:
  • data (np.ndarray) – observed data (bincounts)
  • model_data (np.ndarray) – model predictions for each bin
Returns:

np.ndarray

HErmes.fitting.functions.calculate_sigma_from_amp(amp)[source]

Get the sigma for the gauss from its peak value. Gauss is normed

Parameters:amp (float) –
Returns:float
HErmes.fitting.functions.exponential(x, lmbda)[source]

An exponential model, e.g. for a decay with coefficent lmbda.

Parameters:
  • x (float) – input
  • lmbda (float) – The exponent of the exponential
Returns:

np.ndarray

HErmes.fitting.functions.gauss(x, mu, sigma)[source]

Returns a normed gaussian.

Parameters:
  • x (np.ndarray) – x values
  • mu (float) – Gauss mu
  • sigma (float) – Gauss sigma
  • n

Returns:

HErmes.fitting.functions.n_gauss(x, mu, sigma, n)[source]

Returns a normed gaussian in the case of n ==1. If n > 1, The gaussian mean is shifted by n and its width is enlarged by the factor of n. The envelope of a sequence of these gaussians will be an expoenential.

Parameters:
  • x (np.ndarray) – x values
  • mu (float) – Gauss mu
  • sigma (float) – Gauss sigma
  • n (int) – > 0, linear coefficient

Returns:

HErmes.fitting.functions.pandel_factory(c_ice)[source]

Create a pandel function with the defined parameters

Parameters:c_ice (float) – group velocity in ice in m/ns
Returns:callable
HErmes.fitting.functions.poisson(x, lmbda)[source]

Poisson probability

Parameters:
  • x (int) – measured number of occurences
  • lmbda (int) – expected number of occurences
Returns:

np.ndarray

HErmes.fitting.model module

Provide a simple, easy to use model for fitting data and especially distributions

class HErmes.fitting.model.Model(func, startparams=None, limits=((-inf, inf), ), errors=(10.0, ), func_norm=1)[source]

Bases: object

Model data with a parametrized prediction

add_data(data, bins=200, create_distribution=False, normalize=False, density=True, xs=None, subtract=None)[source]

Add some data to the model, in preparation for the fit

Parameters:data (np.array) –
Keyword Args

nbins (int): subtract (callable): normalize (bool): normalize the data before adding density (bool): if normalized, assume the data is a pdf.

if False, use bincount for normalization.

Returns:

add_first_guess(func)[source]

Use func to estimate better startparameters

Parameters:func – Has to yield a set of startparameters

Returns:

clear()[source]

Reset the model

Returns:None
components
construct_error_function(startparams, errors, limits, errordef)[source]
couple_all_models()[source]

Use the first models startparams for the combined model

Returns:None
couple_models(coupling_variable)[source]

Couple the models by a variable, which means use the variable not independently in all model components, but fit it only once. E.g. if there are 3 models with parameters p1, p2, k each and they are coupled by k, parameters p11, p21, p12, p22, and k will be fitted instead of p11, p12, k1, p21, p22, k2.

Parameters:coupling_variable – variable number of the number in startparams
Returns:None
distribution
eval_first_guess(data)[source]

Assign a new set of start parameters obtained by calling the first geuss metthod

Parameters:data
Returns:
extract_parameters()[source]

Get the variable names and coupling references for the individual model components

Returns:tuple
fit_to_data(silent=False, use_minuit=True, errors=None, limits=None, errordef=1000, **kwargs)[source]

Apply this model to data

Parameters:
  • data (np.ndarray) – the data, unbinned
  • silent (bool) – silence output
  • use_minuit (bool) – use minuit for fitting
  • errors (list) – errors for minuit, see miniuit manual
  • limits (list of tuples) – limits for minuit, see minuit manual
  • errordef (int) – convergence criterion, see minuit manual
  • **kwargs – will be passed on to scipy.optimize.curvefit
Returns:

None

n_free_params

The number of free parameters of this model

Returns:int
plot_result(ymin=1000, xmax=8, ylabel='normed bincount', xlabel='Q [C]', fig=None, log=True, axes_range='auto', model_alpha=0.3, add_parameter_text=(('$\\mu_{{SPE}}$& {:4.2e}\\\\', 0), ), histostyle='scatter', datacolor='k', modelcolor='r')[source]

Show the fit result

Parameters:
  • ymin (float) – limit the yrange to ymin
  • xmax (float) – limit the xrange to xmax
  • model_alpha (float) – 0 <= x <= 1 the alpha value of the lineplot for the model
  • ylabel (str) – label for yaxis
  • log (bool) – plot in log scale
  • axes_range (str) – the “field of view” to show
  • fig (pylab.figure) – A figure instance
  • add_parameter_text (tuple) – Display a parameter in the table on the plot ((text, parameter_number), (text, parameter_number),…)
  • datacolor (matplotlib color compatible) –
  • modelcolor (matplotlib color compatible) –
Returns:

pylab.figure

set_distribution(distr)[source]

Assing a distribution to the model

Parameters:distr

Returns:

HErmes.fitting.model.concat_functions(fncs)[source]

Inspect functions and construct a new one which returns the added result. concat_functions(A(x, apars), B(x, bpars)) -> C(x, apars,bpars) C(x, apars, bpars) returns (A(x, apars) + B(x, bpars))

Parameters:fncs (list) – The callables to concat
Returns:tuple (callable, list(pars))
HErmes.fitting.model.construct_efunc(x, data, jointfunc, joint_pars)[source]

Construct a least-squares function

Parameters:
  • x
  • data
  • jointfunc
  • joint_pars

Returns:

HErmes.fitting.model.copy_func(f)[source]

Based on http://stackoverflow.com/a/6528148/190597 (Glenn Maynard)

HErmes.fitting.model.create_minuit_pardict(fn, startparams, errors, limits, errordef)[source]

Construct a dictionary for minuit fitting

Parameters:
Returns:

dict

Module contents

Simple to use fitting tools