Bases: object
Storing fit result
Bases: exceptions.Exception
Exception raise to stop the fit
add_data fill a self.data_list with data to fit
Parameters: | data – Data to add in the list |
---|
Returns: | list of data data_list |
---|
Returns: | saved model |
---|
return self.selected value
Remove one element from the list
Parameters: | data – Data to remove from data_list |
---|
set_model save a copy of the model
Parameters: | model – the model being set |
---|
set self.selected to 0 or 1 for other values raise an exception
Parameters: | value – integer between 0 or 1 |
---|
Bases: sas.dataloader.data_info.Data1D
Wrapper class for SAS data FitData1D inherits from DataLoader.data_info.Data1D. Implements a way to get residuals from data.
Return the range of data.x to fit
Compute residuals.
If self.smearer has been set, use if to smear the data before computing chi squared.
Parameters: | fn – function that return model value |
---|---|
Returns: | residuals |
Returns: | residuals derivatives . |
---|---|
Note: | in this case just return empty array |
to set the fit range
Number of measurement points in data set after masking, etc.
Bases: sas.dataloader.data_info.Data2D
Wrapper class for SAS data
return the range of data.x to fit
return the residuals
Returns: | residuals derivatives . |
---|---|
Note: | in this case just return empty array |
Determine the correct qx_data and qy_data within range to fit
To set the fit range
Set smearer
Number of measurement points in data set after masking, etc.
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 |
return the self.selected value of the fit problem of id
Parameters: | id – the id of the problem |
---|
remove fitarrange in id
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 |
---|
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: |
|
---|
set a model on a given in the fit engine.
Parameters: |
|
---|---|
Note: | pars must contains only name of existing model’s parameters |
Bases: object
Abstract interface for fit thread handler.
The methods in this class are called by the optimizer as the fit progresses.
Note that it is up to the optimizer to call the fit handler correctly, reporting all status changes and maintaining the ‘done’ flag.
Fit was aborted.
True when the fit job is complete
Model had an error; print traceback
Fit is complete; best results are reported
Called when a result is observed which is better than previous results from the fit.
result is a FitResult object, with parameters, #calls and fitness.
Called each cycle of the fit, reporting the current and the expected amount of work. The meaning of these values is optimizer dependent, but they can be converted into a percent complete using (100*current)//expected.
Progress is updated each iteration of the fit, whatever that means for the particular optimization algorithm. It is called after any calls to improvement for the iteration so that the update handler can control I/O bandwidth by suppressing intermediate improvements until the fit is complete.
The current best result of the fit
Fit wrapper for SAS models.
Override eval method of park model.
Parameters: | x – the x value used to compute a function |
---|
Evaluate the model and derivatives wrt pars at x.
pars is a list of the names of the parameters for which derivatives are desired.
This method needs to be specialized in the model to evaluate the model function. Alternatively, the model can implement is own version of residuals which calculates the residuals directly instead of calling eval.
return a list of value of paramter to fit
Parameters: | fitparams – list of paramaters name to fit |
---|
Set value for parameters to fit
Parameters: | params – list of value for parameters to fit |
---|
BumpsFitting module runs the bumps optimizer.
Bases: sas.fit.AbstractFitEngine.FitEngine
Fit a model using bumps.
Bases: object
Bases: object
ConvergenceMonitor contains population summary statistics to show progress of the fit. This is a list [ (best, 0%, 25%, 50%, 75%, 100%) ] or just a list [ (best, ) ] if population size is 1.
Bases: object
Bases: object
Bases: object
Wrap SAS model as a bumps fitness object
Flag a set of parameters as fitted parameters.
Class Fit contains ScipyFit and ParkFit methods declaration allows to create instance of type ScipyFit or ParkFit to perform either a park fit or a scipy fit.
Bases: object
Wrap class that allows to select the fitting type.this class can be used as follow :
from sas.fit.Fitting import Fit
fitter= Fit()
fitter.fit_engine('scipy') or fitter.fit_engine('park')
engine = fitter.returnEngine()
engine.set_data(data,id)
engine.set_param( model,model.name, pars)
engine.set_model(model,id)
chisqr1, out1, cov1=engine.fit(pars,qmin,qmax)
Perform the fit
return list of data
return the self.selected value of the fit problem of id
Parameters: | id – the id of the problem |
---|
remove fitarrange in id
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 |
---|
Store data to fit at the psotion id of the fit engine
Parameters: |
|
---|
Select the type of Fit
Parameters: | word – the keyword to select the fit type |
---|---|
Raise: | if the user does not enter ‘scipy’ or ‘park’, a valueError is raised |
store a model model to fit at the position id of the fit engine
This class is loading values from given file or value giving by the user
return the file’s path
Return x, y, dx, dy
Return plottable
Store path into a variable.If the user doesn’t give a path as a parameter a pop-up window appears to select the file.
Parameters: | path – the path given by the user |
---|
Store the values loaded from file in local variables
ParkFitting module contains SasParameter,Model,Data FitArrange, ParkFit,Parameter classes.All listed classes work together to perform a simple fit with park optimizer.
Bases: park.assembly.Assembly
Recalculate the theory functions, and from them, the residuals and chisq.
Note: | Call this after the parameters have been updated. |
---|
Extend result from the fit with the calculated 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.
Bases: sas.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 sas 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.
Extract sasmodel and sasdata 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)])
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: |
|
---|---|
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 |
Bases: park.model.Model
PARK wrapper for SAS models.
Override eval method of park model.
Parameters: | x – the x value used to compute a function |
---|
Evaluate the model and derivatives wrt pars at x.
pars is a list of the names of the parameters for which derivatives are desired.
This method needs to be specialized in the model to evaluate the model function. Alternatively, the model can implement is own version of residuals which calculates the residuals directly instead of calling eval.
return a list of value of paramter to fit
Parameters: | fitparams – list of paramaters name to fit |
---|
Set value for parameters to fit
Parameters: | params – list of value for parameters to fit |
---|
Bases: sas.fit.ParkFitting.SasFitter
Monte Carlo optimizer.
This implements park.fit.Fitter.
Bases: park.fitresult.FitParameter
Fit result for an individual parameter.
Return parameter range string.
E.g., ” Gold .....|.... 5.2043 in [2,7]”
Bases: park.fitresult.FitResult
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.
Run the fit
Stop when vertex values are within ftol of each other
Maximum number of iterations before fit terminates
Size of the initial simplex; this is a portion between 0 and 1
Stop when simplex vertices are within xtol of each other
Bases: park.fit.Fitter
Global optimizer.
This function should return immediately
Bases: park.parameter.Parameter
SAS model parameters for use in the PARK fitting service. The parameter attribute value is redirected to the underlying parameter value in the SAS model.
Override _getrange of park parameter return the range of parameter
override the _getvalue of park parameter
:return value the parameter associates with self.name
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.
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)
ScipyFitting module contains FitArrange , ScipyFit, Parameter classes.All listed classes work together to perform a simple fit with scipy optimizer.
Sas Assembly class a class wrapper to be call in optimizer.leastsq method
Check the lower and upper bound of the parameter value and set res to the inf if the value is outside of the range :limitation: the initial values must be within range.
Calculates chi^2
Parameters: | params – list of parameter values |
---|---|
Returns: | chi^2 |
Bases: sas.fit.AbstractFitEngine.FitEngine
ScipyFit performs the Fit.This class can be used as follow: #Do the fit SCIPY create an engine: engine = ScipyFit() Use data must be of type plottable Use a sas model
Add data with a dictionnary of FitArrangeDict 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.In case of Scipyfit set_param is called in fit () automatically. |
---|
engine.set_param( model,”M1”, {‘A’:2,’B’:4})
Add model with a dictionnary of FitArrangeDict{} 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)
Parameter expression evaluator.
For systems in which constraints are expressed as string expressions rather than python code, compile_constraints() can construct an expression evaluator that substitutes the computed values of the expressions into the parameters.
The compiler requires a symbol table, an expression set and a context. The symbol table maps strings containing fully qualified names such as ‘M1.c[3].full_width’ to parameter objects with a ‘value’ property that can be queried and set. The expression set maps symbol names from the symbol table to string expressions. The context provides additional symbols for the expressions in addition to the usual mathematical functions and constants.
The expressions are compiled and interpreted by python, with only minimal effort to make sure that they don’t contain bad code. The resulting constraints function returns 0 so it can be used directly in a fit problem definition.
Extracting the symbol table from the model depends on the structure of the model. If fitness.parameters() is set correctly, then this should simply be a matter of walking the parameter data, remembering the path to each parameter in the symbol table. For compactness, dictionary elements should be referenced by .name rather than [“name”]. Model name can be used as the top level.
Getting the parameter expressions applied correctly is challenging. The following monkey patch works by overriding model_update in FitProblem so that after setp(p) is called and, the constraints expression can be applied before telling the underlying fitness function that the model is out of date:
# Override model update so that parameter constraints are applied
problem._model_update = problem.model_update
def model_update():
constraints()
problem._model_update()
problem.model_update = model_update
Ideally, this interface will change
Build and return a function to evaluate all parameter expressions in the proper order.
Input:
symtab is the symbol table for the model: { ‘name’: parameter }
exprs is the set of computed symbols: { ‘name’: ‘expression’ }
context is any additional context needed to evaluate the expression
Return:
updater function which sets parameter.value for each expression
Raises:
AssertionError - model, parameter or function is missing
SyntaxError - improper expression syntax
ValueError - expressions have circular dependencies
This function is not terribly sophisticated, and it would be easy to trick. However it handles the common cases cleanly and generates reasonable messages for the common errors.
This code has not been fully audited for security. While we have removed the builtins and the ability to import modules, there may be other vectors for users to perform more than simple function evaluations. Unauthenticated users should not be running this code.
Parameter names are assumed to contain only _.a-zA-Z0-9#[]
Both names are provided for inverse functions, e.g., acos and arccos.
Should try running the function to identify syntax errors before running it in a fit.
Use help(fn) to see the code generated for the returned function fn. dis.dis(fn) will show the corresponding python vm instructions.
This parameter set has no constraints between the parameters.
Order elements from pairs so that b comes before a in the ordered list for all pairs (a,b).