sas.fit package

Submodules

sas.fit.AbstractFitEngine module

class sas.fit.AbstractFitEngine.FResult(model=None, param_list=None, data=None)[source]

Bases: object

Storing fit result

print_summary()[source]
set_fitness(fitness)[source]
set_model(model)[source]
exception sas.fit.AbstractFitEngine.FitAbort[source]

Bases: exceptions.Exception

Exception raise to stop the fit

class sas.fit.AbstractFitEngine.FitArrange[source]
add_data(data)[source]

add_data fill a self.data_list with data to fit

Parameters:data – Data to add in the list
get_data()[source]
Returns:list of data data_list
get_model()[source]
Returns:saved model
get_to_fit()[source]

return self.selected value

remove_data(data)[source]

Remove one element from the list

Parameters:data – Data to remove from data_list
set_model(model)[source]

set_model save a copy of the model

Parameters:model – the model being set
set_to_fit(value=0)[source]

set self.selected to 0 or 1 for other values raise an exception

Parameters:value – integer between 0 or 1
class sas.fit.AbstractFitEngine.FitData1D(x, y, dx=None, dy=None, smearer=None, data=None)[source]

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.

get_fit_range()[source]

Return the range of data.x to fit

residuals(fn)[source]

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
residuals_deriv(model, pars=[])[source]
Returns:residuals derivatives .
Note:in this case just return empty array
set_fit_range(qmin=None, qmax=None)[source]

to set the fit range

size()[source]

Number of measurement points in data set after masking, etc.

class sas.fit.AbstractFitEngine.FitData2D(sas_data2d, data=None, err_data=None)[source]

Bases: sas.dataloader.data_info.Data2D

Wrapper class for SAS data

get_fit_range()[source]

return the range of data.x to fit

residuals(fn)[source]

return the residuals

residuals_deriv(model, pars=[])[source]
Returns:residuals derivatives .
Note:in this case just return empty array
set_data(sas_data2d, qmin=None, qmax=None)[source]

Determine the correct qx_data and qy_data within range to fit

set_fit_range(qmin=None, qmax=None)[source]

To set the fit range

set_smearer(smearer)[source]

Set smearer

size()[source]

Number of measurement points in data set after masking, etc.

class sas.fit.AbstractFitEngine.FitEngine[source]
get_model(id)[source]
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(id)[source]

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

Parameters:id – the id of the problem
remove_fit_problem(id)[source]

remove fitarrange in id

select_problem_for_fit(id, value)[source]

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(data, id, smearer=None, qmin=None, qmax=None)[source]

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(model, id, pars=[], constraints=[], data=None)[source]

set a model on a given in the fit engine.

Parameters:
  • model – sas.models type
  • id – is the key of the fitArrange dictionary where model is saved as a value
  • 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 sas.fit.AbstractFitEngine.FitHandler[source]

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.

abort()[source]

Fit was aborted.

done = False

True when the fit job is complete

error(msg)[source]

Model had an error; print traceback

finalize()[source]

Fit is complete; best results are reported

improvement()[source]

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.

progress(current, expected)[source]

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.

result = None

The current best result of the fit

set_result(result=None)[source]
update_fit(last=False)[source]
class sas.fit.AbstractFitEngine.Model(sas_model, sas_data=None, **kw)[source]

Fit wrapper for SAS models.

eval(x)[source]

Override eval method of park model.

Parameters:x – the x value used to compute a function
eval_derivs(x, pars=[])[source]

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.

get_params(fitparams)[source]

return a list of value of paramter to fit

Parameters:fitparams – list of paramaters name to fit
set(**kw)[source]
set_params(paramlist, params)[source]

Set value for parameters to fit

Parameters:params – list of value for parameters to fit

sas.fit.BumpsFitting module

BumpsFitting module runs the bumps optimizer.

class sas.fit.BumpsFitting.BumpsFit[source]

Bases: sas.fit.AbstractFitEngine.FitEngine

Fit a model using bumps.

fit(msg_q=None, q=None, handler=None, curr_thread=None, ftol=1.49012e-08, reset_flag=False)[source]
class sas.fit.BumpsFitting.BumpsMonitor(handler, max_step, pars, dof)[source]

Bases: object

config_history(history)[source]
class sas.fit.BumpsFitting.ConvergenceMonitor[source]

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.

config_history(history)[source]
class sas.fit.BumpsFitting.ParameterExpressions(models)[source]

Bases: object

class sas.fit.BumpsFitting.Progress(history, max_step, pars, dof)[source]

Bases: object

class sas.fit.BumpsFitting.SasFitness(model, data, fitted=[], constraints={}, initial_values=None, **kw)[source]

Bases: object

Wrap SAS model as a bumps fitness object

nllf()[source]
numpoints()[source]
parameters()[source]
residuals()[source]
set_fitted(param_list)[source]

Flag a set of parameters as fitted parameters.

theory()[source]
update()[source]
sas.fit.BumpsFitting.run_bumps(problem, handler, curr_thread)[source]

sas.fit.Fitting module

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.

class sas.fit.Fitting.Fit(engine='scipy', *args, **kw)[source]

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)
fit(msg_q=None, q=None, handler=None, curr_thread=None, ftol=1.49012e-08, reset_flag=False)[source]

Perform the fit

get_model(id)[source]

return list of data

get_problem_to_fit(id)[source]

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

Parameters:id – the id of the problem
remove_fit_problem(id)[source]

remove fitarrange in id

select_problem_for_fit(id, value)[source]

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(data, id, smearer=None, qmin=None, qmax=None)[source]

Store data to fit at the psotion id of the fit engine

Parameters:
  • data – data to fit
  • smearer – smearerobject to smear data
  • qmin – the minimum q range to fit
  • qmax – the minimum q range to fit
set_engine(word, *args, **kw)[source]

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
set_model(model, id, pars=[], constraints=[], data=None)[source]

store a model model to fit at the position id of the fit engine

sas.fit.Loader module

class sas.fit.Loader.Load(x=None, y=None, dx=None, dy=None)[source]

This class is loading values from given file or value giving by the user

get_filename()[source]

return the file’s path

get_values()[source]

Return x, y, dx, dy

load_data(data)[source]

Return plottable

set_filename(path=None)[source]

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
set_values()[source]

Store the values loaded from file in local variables

sas.fit.ParkFitting module

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

class sas.fit.ParkFitting.MyAssembly(models, curr_thread=None)[source]

Bases: park.assembly.Assembly

eval()[source]

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

Note:Call this after the parameters have been updated.
extend_results_with_calculated_parameters(result)[source]

Extend result from the fit with the calculated parameters.

fit_parameters()[source]

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.

class sas.fit.ParkFitting.ParkFit[source]

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.
create_assembly(curr_thread, reset_flag=False)[source]

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)])

fit(msg_q=None, q=None, handler=None, curr_thread=None, ftol=1.49012e-08, reset_flag=False)[source]

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

class sas.fit.ParkFitting.ParkModel(sas_model, sas_data=None, **kw)[source]

Bases: park.model.Model

PARK wrapper for SAS models.

eval(x)[source]

Override eval method of park model.

Parameters:x – the x value used to compute a function
eval_derivs(x, pars=[])[source]

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.

get_params(fitparams)[source]

return a list of value of paramter to fit

Parameters:fitparams – list of paramaters name to fit
parameters = []
parameterset = []
set_params(paramlist, params)[source]

Set value for parameters to fit

Parameters:params – list of value for parameters to fit
class sas.fit.ParkFitting.SasFitMC(localfit, start_points=10)[source]

Bases: sas.fit.ParkFitting.SasFitter

Monte Carlo optimizer.

This implements park.fit.Fitter.

localfit = <sas.fit.ParkFitting.SasFitSimplex object at 0x10ab534d0>
start_points = 10
class sas.fit.ParkFitting.SasFitParameter(name, range, value, model, data)[source]

Bases: park.fitresult.FitParameter

Fit result for an individual parameter.

summarize()[source]

Return parameter range string.

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

class sas.fit.ParkFitting.SasFitResult(*args, **kwrds)[source]

Bases: park.fitresult.FitResult

class sas.fit.ParkFitting.SasFitSimplex(ftol=5e-05)[source]

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.

fit(fitness, x0)[source]

Run the fit

ftol = 5e-05

Stop when vertex values are within ftol of each other

maxiter = None

Maximum number of iterations before fit terminates

radius = 0.05

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 sas.fit.ParkFitting.SasFitter(**kw)[source]

Bases: park.fit.Fitter

fit(fitness, handler)[source]

Global optimizer.

This function should return immediately

class sas.fit.ParkFitting.SasParameter(name, model, data)[source]

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.

get_name()[source]
range

Override _getrange of park parameter return the range of parameter

value

override the _getvalue of park parameter

:return value the parameter associates with self.name

class sas.fit.ParkFitting.SasPart(fitness, weight=1.0, isfitted=True)[source]

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)

sas.fit.ScipyFitting module

ScipyFitting module contains FitArrange , ScipyFit, Parameter classes.All listed classes work together to perform a simple fit with scipy optimizer.

class sas.fit.ScipyFitting.SasAssembly(paramlist, model=None, data=None, fitresult=None, handler=None, curr_thread=None, msg_q=None)[source]

Sas Assembly class a class wrapper to be call in optimizer.leastsq method

check_param_range()[source]

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.

chisq()[source]

Calculates chi^2

Parameters:params – list of parameter values
Returns:chi^2
class sas.fit.ScipyFitting.ScipyFit[source]

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)

fit(msg_q=None, q=None, handler=None, curr_thread=None, ftol=1.49012e-08, reset_flag=False)[source]

sas.fit.expression module

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

sas.fit.expression.compile_constraints(symtab, exprs, context={})[source]

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.

sas.fit.expression.no_constraints()[source]

This parameter set has no constraints between the parameters.

sas.fit.expression.order_dependencies(pairs)[source]

Order elements from pairs so that b comes before a in the ordered list for all pairs (a,b).

sas.fit.expression.test_deps()[source]
sas.fit.expression.test_expr()[source]

Module contents