peri.states

Index of members within peri.states:

peri.states.State

class peri.states.State(params, values, logpriors=None, hyper_params=[‘sigma’], hyper_values=[0.04], **kwargs)

A model and corresponding functions to perform a fit to data using a variety of optimization routines. A model takes parameters and values (names and values) which determine the output of a model, which is then compared with data.

Parameters:
  • params (list of strings) – The names of the parameters (should be a unique set)
  • values (list of numbers) – The corresponding values of the parameters
  • logpriors (list of peri.prior.Prior) – Priors (constraints) to apply to parameters
  • hyper_params (list of strings, optional) – The names of any hyper-parameters (should be a unique set). Stored as a peri.comp.ParameterGroup. Default is [‘sigma’], the standard-deviation of the noise distribution.
  • hyper_values (list of numbers, optional) – The corresponding values of the hyper-parameters. Stored as a peri.comp.ParameterGroup. Default is [0.04]
  • kwargs – Arguments to pass to super class peri.comp.ParameterGroup including ordered and category.
build_funcs()

Here, we build gradient and hessian functions based on the properties of a state that are generally wanted. For each one, we fill in _grad or _hess with a function that takes care of various options such as slicing and flattening. For example, m below takes the model, selects different indices from it, maybe flattens it and copies it. This is then used in the fisherinformation, gradmodel, and hessmodel functions.

crb(params=None, *args, **kwargs)

Calculate the diagonal elements of the minimum covariance of the model with respect to parameters params. *args and **kwargs go to fisherinformation.

data

Class property – the raw data of the model fit. Should return a number (preferrably float) or an ndarray (essentially any object which as operands +-/…). This object is constant since it is data.

error

Class property – Sum of the squared errors, \(E = \sum_i (D_i - M_i(\theta))^2\)

loglikelihood

Class property – loglikelihood calculated by the model error, \(\mathcal{L} = - \frac{1}{2} \sum\left[ \left(\frac{D_i - M_i(\theta)}{\sigma}\right)^2 + \log{(2\pi \sigma^2)} \right]\)

logposterior

Class property – log of posterior prob (including likelihood calculated by the model error and priors):

self.logprior + self.loglikelihood

logprior

Class property – logprior calculated from the sum of all prior objects

model

Class property – the current model fit to the data. Should return a number or ndarray. Ideally this object should be an object updated by the peri.states.State.update() function and simply returned in this property

pop_update()

Pop the last update from the stack push by peri.states.States.push_update() by undoing the chnage last performed.

push_update(params, values)

Perform a parameter update and keep track of the change on the state. Same call structure as peri.states.States.update()

residuals

Class property – the model residuals wrt data, residuals = data - model, \(R_i = D_i - M_i(\theta)\)

temp_update(params, values)

Context manager to temporarily perform a parameter update (by using the stack structure). To use:

with state.temp_update(params, values):
# measure the cost or something state.error
update(params, values)

Update a single parameter or group of parameters params with values.

Parameters:
  • params (string or list of strings) – Parameter names which to update
  • value (number or list of numbers) – Values of those parameters which to update
update_hyper(params, values)

Update any single hyper parameter or group of parameters params with values.

Parameters:
  • params (string or list of strings) – Parameter names which to update
  • value (number or list of numbers) – Values of those parameters which to update
update_sigma(sigma)

Updates the expected sigma of the noise distribution

peri.states.ImageState

class peri.states.ImageState(image, comps, mdl=ConfocalImageModel : H(I*(1-P)+C*P) + B, sigma=0.04, priors=None, pad=24, model_as_data=False)

The state object to create a confocal image. The model is that of a spatially varying illumination field, from which platonic particle shapes are subtracted. This is then spread with a point spread function (PSF).

Parameters:
  • image (peri.util.Image object) – The raw image with which to compare the model image from this class. This image should have been prepared through prepare_for_state, which does things such as padding necessary for this class. In the case of the RawImage, paths are used to keep track of the image object to save on pickle size.
  • comp (list of peri.comp.comp.Component or peri.comp.comp.ComponentCollection) –

    Components used to make up the model image. Each separate component must be of a different category, otherwise combining them would be ambiguous. If you desire multiple Components of one category, combine them using a ComponentCollection (which has functions for combining) and supply that to the comps list.

    The component types must match the list of categories in the ImageState.catmap which tells how components are matched to parts of the model equation.

  • mdl (peri.models.Model object) – Model defining how to combine different Components into a single model.
  • priors (list of peri.priors [default: ()]) – Whether or not to turn on overlap priors using neighborlists
  • pad (integer or tuple of integers (optional)) – No recommended to set by hand. The padding level of the raw image needed by the PSF support.
  • model_as_data (boolean) – Whether to use the model image as the true image after initializing
data

Get the raw data of the model fit

get(name)

Return component by category name

get_update_io_tiles(params, values)

Get the tiles corresponding to a particular section of image needed to be updated. Inputs are the parameters and values. Returned is the padded tile, inner tile, and slicer to go between, but accounting for wrap with the edge of the image as necessary.

model

Get the current model fit to the data

model_to_data(sigma=0.0)

Switch out the data for the model’s recreation of the data.

set_image(image)

Update the current comparison (real) image

set_mem_level(mem_level=’hi’)

Sets the memory usage level of the state.

Parameters:mem_level (string) –
Can be set to one of:
  • hi : all mem’s are np.float64
  • med-hi : image, platonic are float32, rest are float64
  • med : all mem’s are float32
  • med-lo : image, platonic are float16, rest float32
  • lo : all are float16, which is bad for accuracy.

Notes

Right now the PSF is not affected by the mem-level changes, which is OK for mem but it means that self._model, self._residuals are always float64, which can be a chunk of mem.

set_model(mdl)

Setup the image model formation equation and corresponding objects into their various objects. mdl is a peri.models.Model object

update(params, values)

Actually perform an image (etc) update based on a set of params and values. These parameter can be any present in the components in any number. If there is only one component affected then difference image updates will be employed.

update_from_model_change(oldmodel, newmodel, tile)

Update various internal variables from a model update from oldmodel to newmodel for the tile tile

peri.states.{save,load}

peri.states.save(state, filename=None, desc=”, extra=None)

Save the current state with extra information (for example samples and LL from the optimization procedure).

Parameters:
  • state (peri.states.ImageState) – the state object which to save
  • filename (string) – if provided, will override the default that is constructed based on the state’s raw image file. If there is no filename and the state has a RawImage, the it is saved to RawImage.filename + “-peri-save.pkl”
  • desc (string) – if provided, will augment the default filename to be RawImage.filename + ‘-peri-‘ + desc + ‘.pkl’
  • extra (list of pickleable objects) – if provided, will be saved with the state
peri.states.load(filename)

Load the state from the given file, moving to the file’s directory during load (temporarily, moving back after loaded)

Parameters:filename (string) – name of the file to open, should be a .pkl file