holopy.inference package

Submodules

Stochastic fitting of models to data

class CmaStrategy(npixels=None, popsize=None, resample_pixels=True, parent_fraction=0.25, weight_function=None, walker_initial_pos=None, tols={}, seed=None, parallel='auto')

Bases: holopy.core.holopy_object.HoloPyObject

Inference strategy defining a Covariance Matrix Adaptation Evolutionary Strategy using cma package

Parameters:
  • npixels (int, optional) – Number of pixels in the image to fit. default fits all.
  • resample_pixels (Boolean, optional) – If true (default), new pixels are chosen for each call of posterior. Otherwise, a single pixel subset is used throughout calculation.
  • parent_fraction (float, optional) – Fraction of each generation to use to construct the next generation. Takes symbol mu in cma literature
  • weight_function (function, optional) – takes arguments (i, popsize), i in range(popsize); returns weight of i
  • tols (dict, optional) – tolerance values to overwrite the cma defaults
  • seed (int, optional) – random seed to use
  • parallel (optional) – number of threads to use or pool object or one of {None, ‘all’, ‘mpi’}. Default tries ‘mpi’ then ‘all’.
fit(model, data)
run_cma(obj_func, parameters, initial_population, weight_function, tols={}, seed=None, parallel='auto')

instantiate and run a CMAEvolutionStrategy object

Parameters:
  • obj_func (Function) – function to be minimized (not maximized like posterior)
  • parameters (list of Prior objects) – parameters to fit
  • initial_population (array) – starting population with shape = (popsize, len(parameters))
  • weight_function (function) – takes arguments (i, popsize), i in range(popsize); returns weight of i
  • tols (dict, optional) – tolerance values to overwrite the cma defaults
  • seed (int, optional) – random seed to use
  • parallel (optional) – number of threads to use or pool object or one of {None, ‘all’, ‘mpi’}. Default tries ‘mpi’ then ‘all’.

Sample posterior probabilities given model and data

class EmceeStrategy(nwalkers=100, nsamples=None, npixels=None, walker_initial_pos=None, parallel='auto', seed=None)

Bases: holopy.core.holopy_object.HoloPyObject

sample(model, data)
class TemperedStrategy(next_initial_dist=<function sample_one_sigma_gaussian>, nwalkers=100, nsamples=1000, min_pixels=None, npixels=1000, walker_initial_pos=None, parallel='auto', stages=3, stage_len=30, seed=None)

Bases: holopy.inference.emcee.EmceeStrategy

add_stage_strategy(nsamples, npixels)
sample(model, data)
emcee_lnprobs_DataArray(sampler)
emcee_samples_DataArray(sampler, parameter_names)
sample_emcee(model, data, nwalkers, nsamples, walker_initial_pos, parallel='auto', seed=None)
sample_one_sigma_gaussian(result)
fit(data, model, parameters=None, strategy=None)
make_default_model(base_scatterer, fitting_parameters=None)
make_uniform(guesses, key)
parameterize_scatterer(base_scatterer, fitting_parameters)
replace_center(parameters, key)
sample(data, model, strategy=None)
validate_strategy(strategy, operation)
class AlphaModel(scatterer, alpha=1, noise_sd=None, medium_index=None, illum_wavelen=None, illum_polarization=None, theory='auto', constraints=[])

Bases: holopy.inference.model.Model

Model of hologram image formation with scaling parameter alpha.

alpha
class ExactModel(scatterer, calc_func=<function calc_holo>, noise_sd=None, medium_index=None, illum_wavelen=None, illum_polarization=None, theory='auto', constraints=[])

Bases: holopy.inference.model.Model

Model of arbitrary scattering function given by calc_func.

class LimitOverlaps(fraction=0.1)

Bases: holopy.core.holopy_object.HoloPyObject

Constraint prohibiting overlaps beyond a certain tolerance. fraction is the largest overlap allowed, in terms of sphere diameter.

check(s)
class Model(scatterer, noise_sd=None, medium_index=None, illum_wavelen=None, illum_polarization=None, theory='auto', constraints=[])

Bases: holopy.core.holopy_object.HoloPyObject

Model probabilites of observing data

Compute probabilities that observed data could be explained by a set of scatterer and observation parameters.

add_tie(parameters_to_tie, new_name=None)

Defines new ties between model parameters

Parameters:
  • parameters_to_tie (listlike) – names of parameters to tie, as given by keys in model.parameters
  • new_name (string, optional) – the name for the new tied parameter
ensure_parameters_are_listlike(pars)
fit(data, strategy=None)
forward(pars, detector)
classmethod from_yaml(loader, node)

Convert a representation node to a Python object.

generate_guess(n=1, scaling=1, seed=None)
illum_polarization
illum_wavelen
initial_guess

dictionary of initial guess values for each parameter

initial_guess_scatterer
lnlike(pars, data)

Compute the log-likelihood for pars given data

Parameters:
  • pars (dict or list) – list - values for each parameter in the order of self._parameters dict - keys should match self.parameters
  • data (xarray) – The data to compute likelihood against
Returns:

lnlike

Return type:

float

lnposterior(pars, data, pixels=None)

Compute the log-posterior probability of pars given data

Parameters:
  • pars (dict or list) – list - values for each parameter in the order of self._parameters dict - keys should match self.parameters
  • data (xarray) – The data to compute posterior against
  • pixels (int(optional)) – Specify to use a random subset of all pixels in data
Returns:

lnposterior

Return type:

float

lnprior(pars)

Compute the log-prior probability of pars

Parameters:pars (dict or list) – list - values for each parameter in the order of self._parameters dict - keys should match self.parameters
Returns:lnprior
Return type:float
medium_index
noise_sd
parameters

dictionary of the model’s parameters

sample(data, strategy=None)
scatterer
scatterer_from_parameters(pars)

Creates a scatterer by setting values for model parameters

Parameters:pars (dict or list) – list - values for each parameter in the order of self._parameters dict - keys should match self.parameters
Returns:
Return type:scatterer
theory_from_parameters(pars)

Interfaces to minimizers. Classes here provide a common interface to a variety of third party minimizers.

class NmpfitStrategy(npixels=None, quiet=True, ftol=1e-10, xtol=1e-10, gtol=1e-10, damp=0, maxiter=100, seed=None)

Bases: holopy.core.holopy_object.HoloPyObject

Levenberg-Marquardt minimizer, from Numpy/Python translation of Craig Markwardt’s mpfit.pro.

Parameters:
  • npixels (None) – Fit only a randomly selected fraction of the data points in data
  • quiet (Boolean) – If False, print output on minimizer convergence. Default is True
  • ftol (float) – Convergence criterion for minimizer: converges if actual and predicted relative reductions in chi squared <= ftol
  • xtol (float) – Convergence criterion for minimizer: converges if relative error between two Levenberg-Marquardt iterations is <= xtol
  • gtol (float) – Convergence criterion for minimizer: converges if absolute value of cosine of angle between vector of cost function evaluated at current solution for minimized parameters and any column of the Jacobian is <= gtol
  • damp (float) – If nonzero, residuals larger than damp will be replaced by tanh. See nmpfit documentation.
  • maxiter (int) – Maximum number of Levenberg-Marquardt iterations to be performed.

Notes

See nmpfit documentation for further details. Not all functionalities of nmpfit are implemented here: in particular, we do not allow analytical derivatives of the residual function, which is impractical and/or impossible to calculate for holograms. If you want to weight the residuals, you need to supply a custom residual function.

calc_residuals(par_vals)
cleanup_from_fit()
fit(model, data)

fit a model to some data

Parameters:
  • model (Model object) – A model describing the scattering system which leads to your data and the parameters to vary to fit it to the data
  • data (xarray.DataArray) – The data to fit
Returns:

result – an object containing the best fit parameters and information about the fit

Return type:

FitResult

get_errors_from_minimizer(fitted_pars)
initialize_fit(model, data)
minimize(parameters, obj_func)
unscale_pars_from_minimizer(values)

Results of sampling

class FitResult(data, model, strategy, time, kwargs={})

Bases: holopy.core.holopy_object.HoloPyObject

add_attr(kwargs)
best_fit()
forward(pars)
guess_hologram
guess_parameters
guess_scatterer
hologram
max_lnprob
parameters
scatterer
class SamplingResult(data, model, strategy, time, kwargs={})

Bases: holopy.inference.result.FitResult

burn_in(sample_number)
class TemperedSamplingResult(end_result, stage_results, strategy, time)

Bases: holopy.inference.result.SamplingResult

class UncertainValue(guess, plus, minus=None, name=None)

Bases: holopy.core.holopy_object.HoloPyObject

Represent an uncertain value

Parameters:
  • value (float) – The value
  • plus (float) – The plus n_sigma uncertainty (or the uncertainty if it is symmetric)
  • minus (float or None) – The minus n_sigma uncertainty, or None if the uncertainty is symmetric
  • n_sigma (int (or float)) – The number of sigma the uncertainties represent
class LeastSquaresScipyStrategy(ftol=1e-10, xtol=1e-10, gtol=1e-10, max_nfev=None, npixels=None)

Bases: holopy.core.holopy_object.HoloPyObject

fit(model, data)

fit a model to some data

Parameters:
  • model (Model object) – A model describing the scattering system which leads to your data and the parameters to vary to fit it to the data
  • data (xarray.DataArray) – The data to fit
Returns:

result – Contains the best fit parameters and information about the fit

Return type:

FitResult

minimize(parameters, residuals_function)
unscale_pars_from_minimizer(parameters, values)