ParkFitting

:mod:` sans.fit.ParkFitting`

ParkFitting module contains SansParameter,Model,Data FitArrange, ParkFit,Parameter classes.All listed classes work together to perform a simple fit with park optimizer.

class sans.fit.ParkFitting.MyAssembly

Bases: park.assembly.Assembly

__call__()

Cost function.

Evaluate the system for the parameter vector pvec, returning chisq as the cost function to be minimized.

Raises a runtime error if the number of fit parameters is different than the length of the vector.

__class__

alias of type

__delattr__

x.__delattr__(‘name’) <==> del x.name

__delitem__()

Delete the nth model

__dict__ = <dictproxy object at 0x01E46BB0>
__getattribute__

x.__getattribute__(‘name’) <==> x.name

__getitem__()

Return the nth model

__hash__

x.__hash__() <==> hash(x)

__init__()

Build an assembly from a list of models.

__iter__()

Iterate through the models in order

__module__ = 'sans.fit.ParkFitting'
static __new__(S, ...) → a new object with type S, a subtype of T
__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

x.__repr__() <==> repr(x)

__setattr__

x.__setattr__(‘name’, value) <==> x.name = value

__setitem__()

Replace the nth model

__str__

x.__str__() <==> str(x)

__weakref__

list of weak references to the object (if defined)

_reset()

Adjust the parameter set after the addition of a new model.

abort()

Interrupt the current function evaluation.

Forward this to the currently executing model if possible.

all_results()

Extend result from the fit with the calculated parameters.

append()

Add a model to the end of set.

Parameters:fitness – the fitting model

The fitting model can be an instance of park.assembly.Fitness, or a tuple of (park.model.Model,`park.data.Data1D`) :param weight: model weighting (usually 1.0) :param isfitted: whether model should be fit (equivalent to weight 0.)

cov()

Return the covariance matrix inv(J’J) at point p.

eval()

Recalculate the theory functions, and from them, the residuals and chisq.

Note :Call this after the parameters have been updated.
fit_parameters()

Return an alphabetical list of the fitting parameters.

This function is called once at the beginning of a fit, and serves as a convenient place to precalculate what can be precalculated such as the set of fitting parameters and the parameter expressions evaluator.

fresiduals()
insert()

Add a model to a particular position in the set.

isfeasible()

Returns true if the parameter set is in a feasible region of the modeling space.

isfitted()

Query if a particular model is fitted.

Set isfitted to value if value is supplied.

Parameters:
  • idx (integer) – model number
  • value
jacobian()

Returns the derivative wrt the fit parameters at point p.

Numeric derivatives are calculated based on step, where step is the portion of the total range for parameter j, or the portion of point value p_j if the range on parameter j is infinite.

result()

Details to send back to the fitting client on an improved fit.

status is ‘start’, ‘step’ or ‘end’ depending if this is the first result to return, an improved result, or the final result.

[Not implemented]

set_result()

Set the parameters resulting from the fit into the parameter set, and update the calculated expression.

The parameter values may be retrieved by walking the assembly.parameterset tree, checking each parameter for isfitted, iscomputed, or isfixed. For example:

assembly.set_result(result)
for p in assembly.parameterset.flatten():
    if p.isfitted():
        print "%s %g in [%g,%g]"%(p.path,p.value,p.range[0],p.range[1])
    elif p.iscomputed():
        print "%s computed as %g"%(p.path.p.value)

This does not calculate the function or the residuals for these parameters. You can call assembly.eval() to do this. The residuals will be set in assembly[i].residuals. The theory and data are model specific, and can be found in assembly[i].fitness.data.

stderr()

Return parameter uncertainty.

This is just the sqrt diagonal of covariance matrix inv(J’J) at point p.

weight()

Query the weight on a particular model.

Set weight to value if value is supplied.

Parameters :
idx : integer

model number

value : float

model weight

Returns:

model weight

class sans.fit.ParkFitting.ParkFit

Bases: sans.fit.AbstractFitEngine.FitEngine

ParkFit performs the Fit.This class can be used as follow: #Do the fit Park create an engine: engine = ParkFit() Use data must be of type plottable Use a sans model

Add data with a dictionnary of FitArrangeList where Uid is a key and data is saved in FitArrange object. engine.set_data(data,Uid)

Set model parameter “M1”= model.name add {model.parameter.name:value}.

Note :Set_param() if used must always preceded set_model() for the fit to be performed.

engine.set_param( model,”M1”, {‘A’:2,’B’:4})

Add model with a dictionnary of FitArrangeList{} where Uid is a key and model is save in FitArrange object. engine.set_model(model,Uid)

engine.fit return chisqr,[model.parameter 1,2,..],[[err1....][..err2...]] chisqr1, out1, cov1=engine.fit({model.parameter.name:value},qmin,qmax)

Note :{model.parameter.name:value} is ignored in fit function since the user should make sure to call set_param himself.
__init__()

Creates a dictionary (self.fitArrangeList={})of FitArrange elements with Uid as keys

__module__ = 'sans.fit.ParkFitting'
create_assembly()

Extract sansmodel and sansdata from self.FitArrangelist ={Uid:FitArrange} Create parkmodel and park data ,form a list couple of parkmodel and parkdata create an assembly self.problem= park.Assembly([(parkmodel,parkdata)])

fit()

Performs fit with park.fit module.It can perform fit with one model and a set of data, more than two fit of one model and sets of data or fit with more than two model associated with their set of data and constraints

Parameters:
  • pars – Dictionary of parameter names for the model and their values.
  • qmin – The minimum value of data’s range to be fit
  • qmax – The maximum value of data’s range to be fit
Note :

all parameter are ignored most of the time.Are just there to keep ScipyFit and ParkFit interface the same.

Returns:

result.fitness Value of the goodness of fit metric

Returns:

result.pvec list of parameter with the best value found during fitting

Returns:

result.cov Covariance matrix

get_model()
Parameters:id – id is key in the dictionary containing the model to return
Returns:a model at this id or None if no FitArrange element was created with this id
get_problem_to_fit()

return the self.selected value of the fit problem of id

Parameters:id – the id of the problem
remove_fit_problem()

remove fitarrange in id

select_problem_for_fit()

select a couple of model and data at the id position in dictionary and set in self.selected value to value

Parameters:value – the value to allow fitting. can only have the value one or zero
set_data()

Receives plottable, creates a list of data to fit,set data in a FitArrange object and adds that object in a dictionary with key id.

Parameters:
  • data – data added
  • id – unique key corresponding to a fitArrange object with data
set_model()

set a model on a given in the fit engine.

Parameters:model – sans.models type
:param : is the key of the fitArrange dictionary where model is
saved as a value
Parameters:
  • pars – the list of parameters to fit
  • constraints

    list of tuple (name of parameter, value of parameters) the value of parameter must be a string to constraint 2 different parameters. Example: we want to fit 2 model M1 and M2 both have parameters A and B. constraints can be:

    constraints = [(M1.A, M2.B+2), (M1.B= M2.A *5),...,]
Note :

pars must contains only name of existing model’s parameters

class sans.fit.ParkFitting.SansFitMC

Bases: sans.fit.ParkFitting.SansFitter

Monte Carlo optimizer.

This implements park.fit.Fitter.

__class__

alias of type

__delattr__

x.__delattr__(‘name’) <==> del x.name

__dict__ = <dictproxy object at 0x01D351B0>
__getattribute__

x.__getattribute__(‘name’) <==> x.name

__hash__

x.__hash__() <==> hash(x)

__init__()
__module__ = 'sans.fit.ParkFitting'
static __new__(S, ...) → a new object with type S, a subtype of T
__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

x.__repr__() <==> repr(x)

__setattr__

x.__setattr__(‘name’, value) <==> x.name = value

__str__

x.__str__() <==> str(x)

__weakref__

list of weak references to the object (if defined)

_fit()

Run a monte carlo fit.

This procedure maps a local optimizer across a set of initial points.

_threaded()
fit()

Global optimizer.

This function should return immediately

localfit = <sans.fit.ParkFitting.SansFitSimplex object at 0x01D084F0>
start_points = 10
class sans.fit.ParkFitting.SansFitParameter

Bases: park.fitresult.FitParameter

Fit result for an individual parameter.

__class__

alias of type

__delattr__

x.__delattr__(‘name’) <==> del x.name

__dict__ = <dictproxy object at 0x01D352D0>
__getattribute__

x.__getattribute__(‘name’) <==> x.name

__hash__

x.__hash__() <==> hash(x)

__init__()
__module__ = 'sans.fit.ParkFitting'
static __new__(S, ...) → a new object with type S, a subtype of T
__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__()
__setattr__

x.__setattr__(‘name’, value) <==> x.name = value

__str__

x.__str__() <==> str(x)

__weakref__

list of weak references to the object (if defined)

summarize()

Return parameter range string.

E.g., ” Gold .....|.... 5.2043 in [2,7]”

class sans.fit.ParkFitting.SansFitResult

Bases: park.fitresult.FitResult

__class__

alias of type

__delattr__

x.__delattr__(‘name’) <==> del x.name

__dict__ = <dictproxy object at 0x01D358D0>
__getattribute__

x.__getattribute__(‘name’) <==> x.name

__hash__

x.__hash__() <==> hash(x)

__init__()
__module__ = 'sans.fit.ParkFitting'
static __new__(S, ...) → a new object with type S, a subtype of T
__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

x.__repr__() <==> repr(x)

__setattr__

x.__setattr__(‘name’, value) <==> x.name = value

__str__()
__weakref__

list of weak references to the object (if defined)

calc_cov()

Return the covariance matrix inv(J’J) at point p.

print_summary()
set_cov()

Return the covariance matrix inv(J’J) at point p.

update()
class sans.fit.ParkFitting.SansFitSimplex

Bases: park.fitmc.FitSimplex

Local minimizer using Nelder-Mead simplex algorithm.

Simplex is robust and derivative free, though not very efficient.

This class wraps the bounds contrained Nelder-Mead simplex implementation for park.simplex.simplex.

__class__

alias of type

__delattr__

x.__delattr__(‘name’) <==> del x.name

__dict__ = <dictproxy object at 0x01D35CB0>
__getattribute__

x.__getattribute__(‘name’) <==> x.name

__hash__

x.__hash__() <==> hash(x)

__init__()
__module__ = 'sans.fit.ParkFitting'
static __new__(S, ...) → a new object with type S, a subtype of T
__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

x.__repr__() <==> repr(x)

__setattr__

x.__setattr__(‘name’, value) <==> x.name = value

__str__

x.__str__() <==> str(x)

__weakref__

list of weak references to the object (if defined)

_iscancelled()
abort()

Cancel the fit in progress from another thread of execution

fit()

Run the fit

ftol = 5.0000000000000002e-005

Stop when vertex values are within ftol of each other

maxiter = None

Maximum number of iterations before fit terminates

radius = 0.050000000000000003

Size of the initial simplex; this is a portion between 0 and 1

xtol = 1

Stop when simplex vertices are within xtol of each other

class sans.fit.ParkFitting.SansFitter

Bases: park.fit.Fitter

__class__

alias of type

__delattr__

x.__delattr__(‘name’) <==> del x.name

__dict__ = <dictproxy object at 0x01D35B10>
__getattribute__

x.__getattribute__(‘name’) <==> x.name

__hash__

x.__hash__() <==> hash(x)

__init__()
__module__ = 'sans.fit.ParkFitting'
static __new__(S, ...) → a new object with type S, a subtype of T
__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

x.__repr__() <==> repr(x)

__setattr__

x.__setattr__(‘name’, value) <==> x.name = value

__str__

x.__str__() <==> str(x)

__weakref__

list of weak references to the object (if defined)

_fit()

Run the actual fit in a separate thread

Each cycle k of n:
self.handler.progress(k,n)
Each improvement:
self.handler.result.update(x,fx,ncalls) self.handler.improvement()
On completion (if not already performed):
self.hander.result.update(x,fx,ncalls) self.handler.done self.handler.finalize()
_threaded()
fit()

Global optimizer.

This function should return immediately

class sans.fit.ParkFitting.SansPart

Bases: park.assembly.Part

Part of a fitting assembly. Part holds the model itself and associated data. The part can be initialized with a fitness object or with a pair (model,data) for the default fitness function.

fitness (Fitness)
object implementing the park.assembly.Fitness interface. In particular, fitness should provide a parameterset attribute containing a ParameterSet and a residuals method returning a vector of residuals.
weight (dimensionless)
weight for the model. See comments in assembly.py for details.
isfitted (boolean)
True if the model residuals should be included in the fit. The model parameters may still be used in parameter expressions, but there will be no comparison to the data.
residuals (vector)
Residuals for the model if they have been calculated, or None
degrees_of_freedom
Number of residuals minus number of fitted parameters. Degrees of freedom for individual models does not make sense in the presence of expressions combining models, particularly in the case where a model has many parameters but no data or many computed parameters. The degrees of freedom for the model is set to be at least one.
chisq

sum(residuals**2); use chisq/degrees_of_freedom to get the reduced chisq value.

Get/set the weight on the given model.

assembly.weight(3) returns the weight on model 3 (0-origin) assembly.weight(3,0.5) sets the weight on model 3 (0-origin)

__class__

alias of type

__delattr__

x.__delattr__(‘name’) <==> del x.name

__dict__ = <dictproxy object at 0x01D29050>
__getattribute__

x.__getattribute__(‘name’) <==> x.name

__hash__

x.__hash__() <==> hash(x)

__init__()
__module__ = 'sans.fit.ParkFitting'
static __new__(S, ...) → a new object with type S, a subtype of T
__reduce__()

helper for pickle

__reduce_ex__()

helper for pickle

__repr__

x.__repr__() <==> repr(x)

__setattr__

x.__setattr__(‘name’, value) <==> x.name = value

__str__

x.__str__() <==> str(x)

__weakref__

list of weak references to the object (if defined)

Table Of Contents

Previous topic

Loader

Next topic

ScipyFitting

This Page