/// <summary> /// Get the names of the x and y column of the active plot. /// </summary> /// <param name="ctrl">The current active graph controller.</param> /// <returns>An array of two strings. The first string is the name of the x-column, the second /// the name of the y-column.</returns> public static string[] GetActivePlotName(Altaxo.Graph.GUI.GraphController ctrl) { string[] result = new string[2] { String.Empty, String.Empty }; IGPlotItem plotItem = ctrl.ActiveLayer.PlotItems.Flattened[ctrl.CurrentPlotNumber]; XYColumnPlotItem xyPlotItem = plotItem as XYColumnPlotItem; if (xyPlotItem == null) { return(result); } XYColumnPlotData data = xyPlotItem.XYColumnPlotData; if (data == null) { return(result); } result[0] = data.XColumn.FullName; result[1] = data.YColumn.FullName; return(result); }
/// <summary>This will remove the GraphController <paramref>ctrl</paramref> from the graph forms collection.</summary> /// <param name="ctrl">The GraphController to remove.</param> /// <remarks>No exception is thrown if the Form frm is not a member of the graph forms collection.</remarks> public void RemoveGraph(Altaxo.Graph.GUI.GraphController ctrl) { foreach (IViewContent content in WorkbenchSingleton.Workbench.ViewContentCollection) { if ((content is Altaxo.Gui.IMVCControllerWrapper) && object.ReferenceEquals(((Altaxo.Gui.IMVCControllerWrapper)content).MVCController, ctrl)) { WorkbenchSingleton.Workbench.CloseContent(content); break; } } }
public void OnAfterFittingStep() { if (_view != null) { _view.SetChiSquare(this._chiSquare); } if (_doc.FitContext is Altaxo.Graph.GUI.GraphController) { // for every dependent variable in the FitEnsemble, create a function graph Altaxo.Graph.GUI.GraphController graph = _doc.FitContext as Altaxo.Graph.GUI.GraphController; int funcNumber = 0; for (int i = 0; i < _doc.FitEnsemble.Count; i++) { FitElement fitEle = _doc.FitEnsemble[i]; for (int k = 0; k < fitEle.NumberOfDependentVariables; k++, funcNumber++) { if (funcNumber < _functionPlotItems.Count && _functionPlotItems[funcNumber] != null) { XYFunctionPlotItem plotItem = (XYFunctionPlotItem)_functionPlotItems[funcNumber]; FitFunctionToScalarFunctionDDWrapper wrapper = (FitFunctionToScalarFunctionDDWrapper)plotItem.Data.Function; wrapper.Initialize(fitEle.FitFunction, k, 0, _doc.GetParametersForFitElement(i)); } else { FitFunctionToScalarFunctionDDWrapper wrapper = new FitFunctionToScalarFunctionDDWrapper(fitEle.FitFunction, k, _doc.GetParametersForFitElement(i)); XYFunctionPlotData plotdata = new XYFunctionPlotData(wrapper); XYFunctionPlotItem plotItem = new XYFunctionPlotItem(plotdata, new G2DPlotStyleCollection(LineScatterPlotStyleKind.Line)); graph.ActiveLayer.PlotItems.Add(plotItem); _functionPlotItems.Add(plotItem); } } } // if there are more elements in _functionPlotItems, remove them from the graph for (int i = _functionPlotItems.Count - 1; i >= funcNumber; --i) { if (_functionPlotItems[i] != null) { graph.ActiveLayer.PlotItems.Remove((IGPlotItem)_functionPlotItems[i]); _functionPlotItems.RemoveAt(i); } } graph.RefreshGraph(); } }
/// <summary> /// Retrieves the data points of the current active plot. /// </summary> /// <param name="ctrl">The graph controller which controls the graph from which the points are to retrieve.</param> /// <param name="xarr">The array of the data point's x values.</param> /// <param name="yarr">The array of the data point's y values.</param> /// <param name="nPlotPoints">The number of plot points (may be smaller than the length of x and y arrays.</param> /// <returns>Null if all is ok, or error message if not.</returns> public static string GetActivePlotPoints(Altaxo.Graph.GUI.GraphController ctrl, ref double[] xarr, ref double[] yarr, out int nPlotPoints) { nPlotPoints = 0; ctrl.EnsureValidityOfCurrentLayerNumber(); ctrl.EnsureValidityOfCurrentPlotNumber(); IGPlotItem plotItem = ctrl.ActiveLayer.PlotItems.Flattened[ctrl.CurrentPlotNumber]; XYColumnPlotItem xyPlotItem = plotItem as XYColumnPlotItem; if (xyPlotItem == null) { return("No active plot!"); } XYColumnPlotData data = xyPlotItem.XYColumnPlotData; if (data == null) { return("Active plot item has no data"); } if (!(data.XColumn is Altaxo.Data.INumericColumn) || !(data.YColumn is Altaxo.Data.INumericColumn)) { return("X-Y values of plot data are not both numeric"); } Altaxo.Data.INumericColumn xcol = (Altaxo.Data.INumericColumn)data.XColumn; Altaxo.Data.INumericColumn ycol = (Altaxo.Data.INumericColumn)data.YColumn; int n = data.PlottablePoints; if (null == xarr || xarr.Length < n) { xarr = new double[n]; } if (null == yarr || yarr.Length < n) { yarr = new double[n]; } int end = data.PlotRangeEnd; int j = 0; for (int i = data.PlotRangeStart; i < end && j < n; i++) { double x = xcol[i]; double y = ycol[i]; if (double.IsNaN(x) || double.IsNaN(y)) { continue; } xarr[j] = x; yarr[j] = y; ++j; } nPlotPoints = j; return(null); }
public static string Fit(Altaxo.Graph.GUI.GraphController ctrl, int order, double fitCurveXmin, double fitCurveXmax, bool showFormulaOnGraph) { string error; int numberOfDataPoints; double[] xarr = null, yarr = null, earr = null; error = GetActivePlotPoints(ctrl, ref xarr, ref yarr, out numberOfDataPoints); if (null != error) { return(error); } string[] plotNames = GetActivePlotName(ctrl); // Error-Array earr = new double[numberOfDataPoints]; for (int i = 0; i < earr.Length; i++) { earr[i] = 1; } int numberOfParameter = order + 1; double[] parameter = new double[numberOfParameter]; LinearFitBySvd fit = new LinearFitBySvd( xarr, yarr, earr, numberOfDataPoints, order + 1, new FunctionBaseEvaluator(EvaluatePolynomialBase), 1E-5); // Output of results Current.Console.WriteLine(""); Current.Console.WriteLine("---- " + DateTime.Now.ToString() + " -----------------------"); Current.Console.WriteLine("Polynomial regression of order {0} of {1} over {2}", order, plotNames[1], plotNames[0]); Current.Console.WriteLine( "Name Value Error F-Value Prob>F"); for (int i = 0; i < fit.Parameter.Length; i++) { Current.Console.WriteLine("A{0,-3} {1,20} {2,20} {3,20} {4,20}", i, fit.Parameter[i], fit.StandardErrorOfParameter(i), fit.TofParameter(i), 1 - FDistribution.CDF(fit.TofParameter(i), numberOfParameter, numberOfDataPoints - 1) ); } Current.Console.WriteLine("R²: {0}, Adjusted R²: {1}", fit.RSquared, fit.AdjustedRSquared); Current.Console.WriteLine("------------------------------------------------------------"); Current.Console.WriteLine("Source of Degrees of"); Current.Console.WriteLine("variation freedom Sum of Squares Mean Square F0 P value"); double regressionmeansquare = fit.RegressionCorrectedSumOfSquares / numberOfParameter; double residualmeansquare = fit.ResidualSumOfSquares / (numberOfDataPoints - numberOfParameter - 1); Current.Console.WriteLine("Regression {0,10} {1,20} {2,20} {3,20} {4,20}", numberOfParameter, fit.RegressionCorrectedSumOfSquares, fit.RegressionCorrectedSumOfSquares / numberOfParameter, regressionmeansquare / residualmeansquare, 1 - FDistribution.CDF(regressionmeansquare / residualmeansquare, numberOfParameter, numberOfDataPoints - 1) ); Current.Console.WriteLine("Residual {0,10} {1,20} {2,20}", numberOfDataPoints - 1 - numberOfParameter, fit.ResidualSumOfSquares, residualmeansquare ); Current.Console.WriteLine("Total {0,10} {1,20}", numberOfDataPoints - 1, fit.TotalCorrectedSumOfSquares ); Current.Console.WriteLine("------------------------------------------------------------"); // add the fit curve to the graph IScalarFunctionDD plotfunction = new PolynomialFunction(fit.Parameter); XYFunctionPlotItem fittedCurve = new XYFunctionPlotItem(new XYFunctionPlotData(plotfunction), new G2DPlotStyleCollection(LineScatterPlotStyleKind.Line)); ctrl.ActiveLayer.PlotItems.Add(fittedCurve); return(null); }
public static string Fit(Altaxo.Graph.GUI.GraphController ctrl) { if (ctrl.CurrentPlotNumber < 0) { return("No active plot!"); } IGPlotItem plotItem = ctrl.ActiveLayer.PlotItems.Flattened[ctrl.CurrentPlotNumber]; XYColumnPlotItem xyPlotItem = plotItem as XYColumnPlotItem; if (xyPlotItem == null) { return("Active plot is not a X-Y Plot!"); } INumericColumn xColumn = xyPlotItem.XYColumnPlotData.XColumn as INumericColumn; INumericColumn yColumn = xyPlotItem.XYColumnPlotData.YColumn as INumericColumn; if (xColumn == null) { return("The x-column is not numeric"); } if (yColumn == null) { return("The y-column is not numeric"); } Calc.Regression.Nonlinear.NonlinearFitDocument localdoc = ctrl.Doc.GetGraphProperty(FitDocumentPropertyName) as Calc.Regression.Nonlinear.NonlinearFitDocument; if (localdoc == null) { if (_lastFitDocument == null) { localdoc = new Altaxo.Calc.Regression.Nonlinear.NonlinearFitDocument(); } else { localdoc = (Altaxo.Calc.Regression.Nonlinear.NonlinearFitDocument)_lastFitDocument.Clone(); } } if (localdoc.FitEnsemble.Count == 0) { Calc.Regression.Nonlinear.FitElement fitele = new Altaxo.Calc.Regression.Nonlinear.FitElement( xColumn, yColumn, xyPlotItem.XYColumnPlotData.PlotRangeStart, xyPlotItem.XYColumnPlotData.PlotRangeLength); localdoc.FitEnsemble.Add(fitele); } else // localdoc.FitEnsemble.Count>0 { localdoc.FitEnsemble[0].SetIndependentVariable(0, xColumn); localdoc.FitEnsemble[0].SetDependentVariable(0, yColumn); localdoc.FitEnsemble[0].SetRowRange(xyPlotItem.XYColumnPlotData.PlotRangeStart, xyPlotItem.XYColumnPlotData.PlotRangeLength); } localdoc.FitContext = ctrl; object fitdocasobject = localdoc; if (true == Current.Gui.ShowDialog(ref fitdocasobject, "Non-linear fitting")) { // store the fit document in the graphs property ctrl.Doc.SetGraphProperty(FitDocumentPropertyName, localdoc); _lastFitDocument = (Altaxo.Calc.Regression.Nonlinear.NonlinearFitDocument)localdoc.Clone(); } return(null); }