This example project implements a custom fitting model creating three curve expressions, representing a line and its confidence bands.
Overview
The custom fitting models API enables developers to define collections of curves and points. This Straight line fit with confidence bands example adds three curve expressions representing the line and its confidence bands of the following image:
Background Information
- Creating a Fitting Model
Curve fitting is the process of calculating values from visualization coordinates and input parameters, to draw reference curves and points.
- Fitting Model: A Comprehensive Example
The following step-by-step example, including the basic extension pattern steps, shows how to implement a simple fitting model that displays a max and a min curve and a large star at the center.
Prerequisites
Spotfire SDK\Examples\Extensions\SpotfireDeveloper.CustomFittingModelExample
The Straight Line Fit with Confidence Bands
The goal of the extension described in this tutorial is best explained by the following image:
The curve becomes available from the Add button in the Properties window:
The confidence level is provided when adding the curve, but can be altered also after it has been added:
The resulting set of curves allows for individual line control:
Implementation
The base class for curves and points performs most of the document node handling. Focus is on creating the curves, points and parameters, and how to calculate the parameters.
Construction
The curves and points are created during construction of a custom fitting model instance.
public StraightLineFitWithConfidenceBands() : base()
{
CreateProperty(PropertyNames.ConfidenceLevel, out this.confidenceLevel, DefaultConfidenceLevel);
base.CreateCurve(CurveNames.Line, CurveDisplayNames.Line, CurveExpressions.Line);
ReferenceCurve upperConfidenceBand = base.CreateCurve(CurveNames.UpperConfidenceBand, CurveDisplayNames.UpperConfidenceBand, CurveExpressions.UpperConfidenceBand);
upperConfidenceBand.LineStyle = LineStyle.Dash;
ReferenceCurve lowerConfidenceBand = base.CreateCurve(CurveNames.LowerConfidenceBand, CurveDisplayNames.LowerConfidenceBand, CurveExpressions.LowerConfidenceBand);
lowerConfidenceBand.LineStyle = LineStyle.Dash;
}
Refer to the CurveExpressions helper class for details on the expression definition of the curves.
Parameter Configuration
Parameters are created by overriding ConfigureModelCore:
protected override void ConfigureModelCore(FittingModelConfigurator configurator)
{
configurator.RegisterResultVariable(VariableNames.SigmaSquared, VariableDescriptions.SigmaSquared);
configurator.RegisterResultVariable(VariableNames.A, VariableDescriptions.A);
configurator.RegisterResultVariable(VariableNames.B, VariableDescriptions.B);
configurator.RegisterResultVariable(VariableNames.VarA, VariableDescriptions.VarA);
configurator.RegisterResultVariable(VariableNames.VarB, VariableDescriptions.VarB);
configurator.RegisterResultVariable(VariableNames.CovarAB, VariableDescriptions.CovarAB);
configurator.RegisterResultVariable(VariableNames.ConfidenceLevel, VariableDescriptions.ConfidenceLevel);
configurator.RegisterResultVariable(VariableNames.Df, VariableDescriptions.Df);
configurator.RegisterResultVariable(VariableNames.CountXY, VariableDescriptions.CountXY);
}
Parameter Configuration in Runtime
The parameter collection may need to changed due to changes to the properties of the model. Specify a trigger in an override of GetConfigureModelTriggerCore :
protected override Trigger GetConfigureModelTriggerCore()
{
return Trigger.CreatePropertyTrigger(this, PropertyNames.Degree);
}
ConfigureModelCore is be called when the trigger fires and then the developer can change the definition of the model in runtime.
Calculating Results on Display
The curves are defined by parameters. When the client is to display a custom fitting model, parameters must be calculated and set. This is performed in the Fit method of the custom fitting model.
To be able to calculate the parameter estimates, the developer must be able to read the data. This is usually carried out by creating a DataValueCursor<double> over X and Y respectively, iterating over the rows where both the x-values and the y-values are valid, that is only for the points displayed in the plot. This iteration can be prepared as:
DataValueCursor<double> xValues = data.CreateXCursor();
DataValueCursor<double> yValues = data.CreateYCursor();
IEnumerable<DataRow> rows = data.GetRows(xValues, yValues);
Values used in the client are included, also the invalid values. The extension example contains a helper class restricting the enumeration to valid values only:
rows = new ValidValues(rows, xValues, yValues);
The Straight line fit with confidence bands example contains several parameters that must be set. The WriteResultToOutput method, called at the end of the Fit method, shows how to set these parameters.