sas.sascalc.fit package
Submodules
sas.sascalc.fit.AbstractFitEngine module
-
class
sas.sascalc.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.sascalc.fit.AbstractFitEngine.
FitAbort
[source] Bases:
Exception
Exception raise to stop the fit
-
class
sas.sascalc.fit.AbstractFitEngine.
FitArrange
[source] Bases:
object
-
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.sascalc.fit.AbstractFitEngine.
FitData1D
(x, y, dx=None, dy=None, smearer=None, data=None, lam=None, dlam=None)[source] Bases:
sas.sascalc.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.sascalc.fit.AbstractFitEngine.
FitData2D
(sas_data2d, data=None, err_data=None)[source] Bases:
sas.sascalc.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.sascalc.fit.AbstractFitEngine.
FitEngine
[source] Bases:
object
-
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.sascalc.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.sascalc.fit.AbstractFitEngine.
Model
(sas_model, sas_data=None, **kw)[source] Bases:
object
Fit wrapper for SAS models.
-
eval
(x)[source] Override eval method of 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 parameter to fit
Parameters: fitparams – list of parameters 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.sascalc.fit.BumpsFitting module
BumpsFitting module runs the bumps optimizer.
-
class
sas.sascalc.fit.BumpsFitting.
BumpsFit
[source] Bases:
sas.sascalc.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.sascalc.fit.BumpsFitting.
BumpsMonitor
(handler, max_step, pars, dof)[source] Bases:
object
-
config_history
(history)[source]
-
-
class
sas.sascalc.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.sascalc.fit.BumpsFitting.
ParameterExpressions
(models)[source] Bases:
object
-
class
sas.sascalc.fit.BumpsFitting.
Progress
(history, max_step, pars, dof)[source] Bases:
object
-
class
sas.sascalc.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.sascalc.fit.BumpsFitting.
get_fitter
()[source]
-
sas.sascalc.fit.BumpsFitting.
run_bumps
(problem, handler, curr_thread)[source]
sas.sascalc.fit.Loader module
-
class
sas.sascalc.fit.Loader.
Load
(x=None, y=None, dx=None, dy=None)[source] Bases:
object
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.sascalc.fit.MultiplicationModel module
-
class
sas.sascalc.fit.MultiplicationModel.
MultiplicationModel
(p_model, s_model)[source] Bases:
sas.sascalc.calculator.BaseComponent.BaseComponent
Use for P(Q)*S(Q); function call must be in the order of P(Q) and then S(Q): The model parameters are combined from both models, P(Q) and S(Q), except 1) ‘radius_effective’ of S(Q) which will be calculated from P(Q) via calculate_ER(), and 2) ‘scale’ in P model which is synchronized w/ volfraction in S then P*S is multiplied by a new parameter, ‘scale_factor’. The polydispersion is applicable only to P(Q), not to S(Q).
Note
P(Q) refers to ‘form factor’ model while S(Q) does to ‘structure factor’.
-
evalDistribution
(x=[])[source] Evaluate the model in cartesian coordinates
Parameters: x – input q[], or [qx[], qy[]] Returns: scattering function P(q[])
-
fill_description
(p_model, s_model)[source] Fill the description for P(Q)*S(Q)
-
getProfile
()[source] Get SLD profile of p_model if exists
Returns: (r, beta) where r is a list of radius of the transition points beta is a list of the corresponding SLD values Note
This works only for func_shell num = 2 (exp function).
-
run
(x=0.0)[source] Evaluate the model
Parameters: x – input q-value (float or [float, float] as [r, theta]) Returns: (scattering function value)
-
runXY
(x=0.0)[source] Evaluate the model
Parameters: x – input q-value (float or [float, float] as [qx, qy]) Returns: scattering function value
-
setParam
(name, value)[source] Set the value of a model parameter
Parameters: - name – name of the parameter
- value – value of the parameter
-
set_dispersion
(parameter, dispersion)[source] Set the dispersion object for a model parameter
Parameters: parameter – name of the parameter [string] Dispersion: dispersion object of type DispersionModel
-
sas.sascalc.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.sascalc.fit.expression.
check_constraints
(symtab, exprs, context={}, html=False)[source] Returns a list of errors in exprs or the empty list if there are none. If the html flag is set to True, the list elements will have html <b> markups that allow the caller to control rendering:
Unknown symbol: tags unknown symbols in exprs Syntax error: tags the beginning of a syntax error in exprs Cyclic dependency: tags comma separated parameters that have cyclic dependency
All symbols must exist in context or in symtab. The symbols in context should be constants or functions. The symbols in symtab can be constants or parameter objects with a value attribute.
It first runs
compile_constraints()
, returning a list of errors if any. If there are no errors it runs the compiled constraints function, returning any errors it produces. Any parameters in symtab are copied with a shallow copy so they aren’t overridden when the constraints are run.
-
sas.sascalc.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 expressionRaises:
RunTimeError if any expression contains a syntax error, if any symbol used is not defined, or if there are circular dependencies between symbols. Runtime error argument is a string describing all found errors.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.sascalc.fit.expression.
iskeyword
() x.__contains__(y) <==> y in x.
-
sas.sascalc.fit.expression.
no_constraints
()[source] This parameter set has no constraints between the parameters.
-
sas.sascalc.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.sascalc.fit.expression.
standard_symbols
(context={})[source]
-
sas.sascalc.fit.expression.
test_deps
()[source]
-
sas.sascalc.fit.expression.
test_expr
()[source]
sas.sascalc.fit.models module
Utilities to manage models
-
class
sas.sascalc.fit.models.
ModelManager
[source] Bases:
object
manage the list of available models
-
base
= None
-
cat_model_list
()[source]
-
composable_models
()[source]
-
get_model_dictionary
()[source]
-
get_model_list
()[source]
-
plugins_reset
()[source]
-
update
()[source]
-
-
class
sas.sascalc.fit.models.
ModelManagerBase
[source] Bases:
object
Base class for the model manager
-
composable_models
()[source] return list of standard models that can be used in sum/multiply
-
get_model_list
()[source] return dictionary of classified models
Structure Factors are the structure factor models Multi-Functions are the multiplicity models Plugin Models are the plugin models
Note that a model can be both a plugin and a structure factor or multiplicity model.
-
last_time_dir_modified
= 0 timestamp on the plugin directory at the last plugin update
-
model_dictionary
= None mutable dictionary of models, continually updated to reflect the current set of plugins
-
plugin_models
= None list of plugin models reset each time the plugin directory is queried
-
plugins_reset
()[source] return a dictionary of model
-
plugins_update
()[source] return a dictionary of model if new models were added else return empty dictionary
-
standard_models
= None constant list of standard models
-
-
class
sas.sascalc.fit.models.
ReportProblem
[source] Bases:
object
Class to check for problems with specific values
-
sas.sascalc.fit.models.
compile_file
(dir)[source] Compile a py file
-
sas.sascalc.fit.models.
find_plugin_models
()[source] Find custom models
-
sas.sascalc.fit.models.
find_plugins_dir
()[source] Find path of the plugins directory. The plugin directory is located in the user’s home directory.
-
sas.sascalc.fit.models.
initialize_plugins_dir
(path)[source]
-
sas.sascalc.fit.models.
plugin_log
(message)[source] Log a message in a file located in the user’s home directory
sas.sascalc.fit.pagestate module
Class that holds a fit page state
-
class
sas.sascalc.fit.pagestate.
PageState
(model=None, data=None)[source] Bases:
object
Contains information to reconstruct a page of the fitpanel.
-
clone
()[source] Create a new copy of the current object
-
from_xml
(file=None, node=None)[source] Load fitting state from a file
Parameters: - file – .fitv file
- node – node of a XML document to read from
-
static
param_remap_from_sasmodels_convert
(params)[source] Converts {name : value} map back to [] param list :param params: parameter map returned from sasmodels :return: None
-
static
param_remap_to_sasmodels_convert
(params, is_string=False)[source] Remaps the parameters for sasmodels conversion
Parameters: params – list of parameters (likely self.parameters) Returns: remapped dictionary of parameters
-
report
(fig_urls)[source] Invoke report dialog panel
: param figs: list of pylab figures [list]
-
to_xml
(file='fitting_state.fitv', doc=None, entry_node=None, batch_fit_state=None)[source] Writes the state of the fit panel to file, as XML.
Compatible with standalone writing, or appending to an already existing XML document. In that case, the XML document is required. An optional entry node in the XML document may also be given.
Parameters: - file – file to write to
- doc – XML document object [optional]
- entry_node – XML node within the XML document at which we will append the data [optional]
- batch_fit_state – simultaneous fit state
-
-
class
sas.sascalc.fit.pagestate.
Reader
(call_back=None, cansas=True)[source] Bases:
sas.sascalc.dataloader.readers.cansas_reader.Reader
Class to load a .fitv fitting file
-
ext
= ['.fitv', '.FITV', '.svs', 'SVS']
-
get_state
()[source]
-
read
(path)[source] Load a new P(r) inversion state from file
Parameters: path – file path
-
type
= ['Fitting files (*.fitv)|*.fitvSASView file (*.svs)|*.svs']
-
type_name
= 'Fitting'
-
write
(filename, datainfo=None, fitstate=None)[source] Write the content of a Data1D as a CanSAS XML file only for standalone
Parameters: - filename – name of the file to write
- datainfo – Data1D object
- fitstate – PageState object
-
write_toXML
(datainfo=None, state=None, batchfit=None)[source] Write toXML, a helper for write(), could be used by guimanager._on_save()
: return: xml doc
-
-
class
sas.sascalc.fit.pagestate.
SimFitPageState
[source] Bases:
object
State of the simultaneous fit page for saving purposes
-
sas.sascalc.fit.pagestate.
parse_entry_helper
(node, item)[source] Create a numpy list from value extrated from the node
Parameters: - node – node from each the value is stored
- item – list name of three strings.the two first are name of data attribute and the third one is the type of the value of that attribute. type can be string, float, bool, etc.
: return: numpy array
sas.sascalc.fit.pluginmodel module
-
class
sas.sascalc.fit.pluginmodel.
Model1DPlugin
(name='Plugin Model')[source] Bases:
sas.sascalc.calculator.BaseComponent.BaseComponent
-
function
(x)[source] Function to be implemented by the plug-in writer
-
is_multiplicity_model
= False
-
run
(x=0.0)[source] Evaluate the model
Parameters: x – input x, or [x, phi] [radian] Returns: function value
-
runXY
(x=0.0)[source] Evaluate the model
Parameters: x – input x, or [x, y] Returns: function value
-
set_details
()[source] Set default details
-
sas.sascalc.fit.qsmearing module
Handle Q smearing
-
class
sas.sascalc.fit.qsmearing.
PySmear
(resolution, model, offset=None)[source] Bases:
object
Wrapper for pure python sasmodels resolution functions.
-
apply
(iq_in, first_bin=0, last_bin=None)[source] Apply the resolution function to the data. Note that this is called with iq_in matching data.x, but with iq_in[first_bin:last_bin] set to theory values for these bins, and the remainder left undefined. The first_bin, last_bin values should be those returned from get_bin_range. The returned value is of the same length as iq_in, with the range first_bin:last_bin set to the resolution smeared values.
-
get_bin_range
(q_min=None, q_max=None)[source] For a given q_min, q_max, find the corresponding indices in the data. Returns first, last. Note that these are indexes into q from the data, not the q_calc needed by the resolution function. Note also that these are the indices, not the range limits. That is, the complete range will be q[first:last+1].
-
-
class
sas.sascalc.fit.qsmearing.
PySmear2D
(data=None, model=None)[source] Bases:
object
Q smearing class for SAS 2d pinhole data
-
get_value
()[source] Over sampling of r_nbins times phi_nbins, calculate Gaussian weights, then find smeared intensity
-
set_accuracy
(accuracy='Low')[source] Set accuracy.
Parameters: accuracy – string
-
set_data
(data=None)[source] Set data.
Parameters: data – DataLoader.Data_info type
-
set_index
(index=None)[source] Set index.
Parameters: index – 1d arrays
-
set_model
(model=None)[source] Set model.
Parameters: model – sas.models instance
-
set_smearer
(smearer=True)[source] Set whether or not smearer will be used
Parameters: smearer – smear object
-
-
sas.sascalc.fit.qsmearing.
pinhole_smear
(data, model=None)[source]
-
sas.sascalc.fit.qsmearing.
slit_smear
(data, model=None)[source]
-
sas.sascalc.fit.qsmearing.
smear_selection
(data, model=None)[source] Creates the right type of smearer according to the data. The canSAS format has a rule that either slit smearing data OR resolution smearing data is available.
For the present purpose, we choose the one that has none-zero data. If both slit and resolution smearing arrays are filled with good data (which should not happen), then we choose the resolution smearing data.
Parameters: - data – Data1D object
- model – sas.model instance