예제 #1
0
        public static void SavitzkyGolayFiltering(WorksheetController ctrl)
        {
            if (ctrl.SelectedDataColumns.Count == 0)
            {
                return;
            }

            object paramobject = new SavitzkyGolayParameters();

            if (!Current.Gui.ShowDialog(ref paramobject, "Savitzky-Golay parameters"))
            {
                return;
            }

            SavitzkyGolayParameters parameters = (SavitzkyGolayParameters)paramobject;

            Altaxo.Data.DataColumn yCol = ctrl.Doc.DataColumns[ctrl.SelectedDataColumns[0]];
            Altaxo.Data.DataColumn xCol = ctrl.Doc.DataColumns.FindXColumnOf(yCol);

            double spacing = 1;

            if (xCol is Data.INumericColumn)
            {
                Calc.LinearAlgebra.VectorSpacingEvaluator calcspace = new Calc.LinearAlgebra.VectorSpacingEvaluator(Calc.LinearAlgebra.DataColumnWrapper.ToROVector(xCol));
                if (!calcspace.HasValidSpaces || calcspace.HasInvalidSpaces)
                {
                    Current.Gui.ErrorMessageBox(string.Format("The x-column {0} contains invalid spaces (is not equally spaced)", xCol.Name));
                    return;
                }
                if (calcspace.RelativeSpaceDeviation > 1E-2)
                {
                    System.Windows.Forms.DialogResult dlgresult =
                        System.Windows.Forms.MessageBox.Show(Current.MainWindow,
                                                             string.Format("The x-column {0} is not equally spaced, the deviation is {1}, the mean spacing is {2}. Continue anyway?", xCol.Name, calcspace.RelativeSpaceDeviation, calcspace.SpaceMeanValue),
                                                             "Continue?", System.Windows.Forms.MessageBoxButtons.YesNo,
                                                             System.Windows.Forms.MessageBoxIcon.Question,
                                                             System.Windows.Forms.MessageBoxDefaultButton.Button1);

                    if (dlgresult == System.Windows.Forms.DialogResult.No)
                    {
                        return;
                    }
                }

                spacing = calcspace.SpaceMeanValue;
            }

            Calc.Regression.SavitzkyGolay filter = new SavitzkyGolay(parameters);

            yCol.Suspend();
            filter.Apply(DataColumnWrapper.ToROVectorCopy(yCol), DataColumnWrapper.ToVector(yCol));

            if (parameters.DerivativeOrder > 0)
            {
                double factor = Math.Pow(1 / spacing, parameters.DerivativeOrder) * Calc.GammaRelated.Fac(parameters.DerivativeOrder);
                yCol.Data = yCol * factor;
            }

            yCol.Resume();
        }
예제 #2
0
        public static void Interpolation(Altaxo.Data.DataColumn xCol, Altaxo.Data.DataColumn yCol,
                                         Calc.Interpolation.IInterpolationFunction interpolInstance, IReadOnlyList <double> samplePoints,
                                         Altaxo.Data.DataColumn xRes, Altaxo.Data.DataColumn yRes)
        {
            int rows = Math.Min(xCol.Count, yCol.Count);
            var yVec = DataColumnWrapper.ToROVector((INumericColumn)yCol, rows);
            var xVec = DataColumnWrapper.ToROVector((INumericColumn)xCol, rows);

            interpolInstance.Interpolate(xVec, yVec);

            using (var suspendToken_xRes = xRes.SuspendGetToken())
            {
                using (var suspendToken_yRes = yRes.SuspendGetToken())
                {
                    for (int i = 0; i < samplePoints.Count; i++)
                    {
                        //double r = i / (double)(parameters.NumberOfPoints - 1);
                        //double x = parameters.XOrg * (1 - r) + parameters.XEnd * (r);
                        double x = samplePoints[i];
                        double y = interpolInstance.GetYOfX(x);
                        xRes[i] = x;
                        yRes[i] = y;
                    }
                    suspendToken_yRes.Resume();
                }
                suspendToken_xRes.Resume();
            }
        }
예제 #3
0
        public static void Interpolation(WorksheetController ctrl)
        {
            if (ctrl.SelectedDataColumns.Count == 0)
            {
                return;
            }

            object paramobject = new InterpolationParameters();

            if (!Current.Gui.ShowDialog(ref paramobject, "Interpolation"))
            {
                return;
            }

            InterpolationParameters parameters = (InterpolationParameters)paramobject;


            Altaxo.Data.DataColumn yCol = ctrl.Doc.DataColumns[ctrl.SelectedDataColumns[0]];
            Altaxo.Data.DataColumn xCol = ctrl.Doc.DataColumns.FindXColumnOf(yCol);

            if (!(yCol is INumericColumn))
            {
                Current.Gui.ErrorMessageBox("The selected column is not numeric!");
                return;
            }
            if (!(xCol is INumericColumn))
            {
                Current.Gui.ErrorMessageBox("The x-column of the selected column is not numeric!");
                return;
            }

            int       rows = Math.Min(xCol.Count, yCol.Count);
            IROVector yVec = DataColumnWrapper.ToROVector((INumericColumn)yCol, rows);
            IROVector xVec = DataColumnWrapper.ToROVector((INumericColumn)xCol, rows);

            parameters.InterpolationInstance.Interpolate(xVec, yVec);

            DoubleColumn xRes = new DoubleColumn();
            DoubleColumn yRes = new DoubleColumn();

            for (int i = 0; i < parameters.NumberOfPoints; i++)
            {
                double r = i / (double)(parameters.NumberOfPoints - 1);
                double x = parameters.XOrg * (1 - r) + parameters.XEnd * (r);
                double y = ((IInterpolationFunction)parameters.InterpolationInstance).GetYOfX(x);
                xRes[i] = x;
                yRes[i] = y;
            }

            int newgroup = ctrl.DataTable.DataColumns.GetUnusedColumnGroupNumber();

            ctrl.DataTable.DataColumns.Add(xRes, xCol.Name + ".I", ColumnKind.X, newgroup);
            ctrl.DataTable.DataColumns.Add(yRes, yCol.Name + ".I", ColumnKind.V, newgroup);
        }
예제 #4
0
        public static void GenerateValues(MultivariateLinearFitParameters parameters, LinearFitBySvd fit)
        {
            DataColumn dependentColumn = parameters.Table[parameters.SelectedDataColumns[parameters.DependentColumnIndexIntoSelection]];

            if (parameters.GenerateRegressionValues)
            {
                var col = new DoubleColumn();
                VectorMath.Copy(VectorMath.ToROVector(fit.PredictedValues), DataColumnWrapper.ToVector(col, parameters.SelectedDataRows));
                parameters.Table.Add(col, dependentColumn.Name + "(predicted)", ColumnKind.V, parameters.Table.GetColumnGroup(dependentColumn));
            }

            if (parameters.GenerateResidualValues)
            {
                var col = new DoubleColumn();
                VectorMath.Copy(VectorMath.ToROVector(fit.ResidualValues), DataColumnWrapper.ToVector(col, parameters.SelectedDataRows));
                parameters.Table.Add(col, dependentColumn.Name + "(residual)", ColumnKind.V, parameters.Table.GetColumnGroup(dependentColumn));
            }
        }
예제 #5
0
        public static void SavitzkyGolay(SavitzkyGolayParameters parameters, Altaxo.Data.DataColumn yCol, Altaxo.Data.DataColumn xCol)
        {
            double spacing = 1;

            if (xCol is Data.INumericColumn)
            {
                var calcspace = new Calc.LinearAlgebra.VectorSpacingEvaluator(Calc.LinearAlgebra.DataColumnWrapper.ToROVector(xCol));
                if (!calcspace.HasValidSpaces || calcspace.HasInvalidSpaces)
                {
                    Current.Gui.ErrorMessageBox(string.Format("The x-column {0} contains invalid spaces (is not equally spaced)", xCol.Name));
                    return;
                }
                if (calcspace.RelativeSpaceDeviation > 1E-2)
                {
                    if (!Current.Gui.YesNoMessageBox(
                            string.Format("The x-column {0} is not equally spaced, the deviation is {1}, the mean spacing is {2}. Continue anyway?", xCol.Name, calcspace.RelativeSpaceDeviation, calcspace.SpaceMeanValue),
                            "Continue?", true))
                    {
                        return;
                    }
                }

                spacing = calcspace.SpaceMeanValue;
            }

            var filter = new SavitzkyGolay(parameters);

            using (var suspendToken = yCol.SuspendGetToken())
            {
                filter.Apply(DataColumnWrapper.ToROVectorCopy(yCol), DataColumnWrapper.ToVector(yCol));

                if (parameters.DerivativeOrder > 0)
                {
                    double factor = Math.Pow(1 / spacing, parameters.DerivativeOrder) * Calc.GammaRelated.Fac(parameters.DerivativeOrder);
                    yCol.Data = yCol * factor;
                }
                suspendToken.Dispose();
            }
        }
예제 #6
0
        /// <summary>
        /// Exports a table to a PLS2CalibrationSet
        /// </summary>
        /// <param name="table">The table where the calibration model is stored.</param>
        /// <param name="calibrationSet"></param>
        public static void Export(
            DataTable table,
            out PLS1CalibrationModel calibrationSet)
        {
            int numberOfX       = GetNumberOfX(table);
            int numberOfY       = GetNumberOfY(table);
            int numberOfFactors = GetNumberOfFactors(table);

            calibrationSet                 = new PLS1CalibrationModel();
            calibrationSet.NumberOfX       = numberOfX;
            calibrationSet.NumberOfY       = numberOfY;
            calibrationSet.NumberOfFactors = numberOfFactors;
            MultivariatePreprocessingModel preprocessSet = new MultivariatePreprocessingModel();
            MultivariateContentMemento     plsMemo       = table.GetTableProperty("Content") as MultivariateContentMemento;

            if (plsMemo != null)
            {
                preprocessSet.PreprocessOptions = plsMemo.SpectralPreprocessing;
            }
            calibrationSet.SetPreprocessingModel(preprocessSet);

            Altaxo.Collections.AscendingIntegerCollection sel = new Altaxo.Collections.AscendingIntegerCollection();
            Altaxo.Data.DataColumn col;

            col = table[GetXOfX_ColumnName()];
            if (col == null || !(col is INumericColumn))
            {
                NotFound(GetXOfX_ColumnName());
            }
            preprocessSet.XOfX = Altaxo.Calc.LinearAlgebra.DataColumnWrapper.ToROVector((INumericColumn)col, numberOfX);


            col = table[GetXMean_ColumnName()];
            if (col == null)
            {
                NotFound(GetXMean_ColumnName());
            }
            preprocessSet.XMean = Altaxo.Calc.LinearAlgebra.DataColumnWrapper.ToROVector(col, numberOfX);

            col = table[GetXScale_ColumnName()];
            if (col == null)
            {
                NotFound(GetXScale_ColumnName());
            }
            preprocessSet.XScale = Altaxo.Calc.LinearAlgebra.DataColumnWrapper.ToROVector(col, numberOfX);



            sel.Clear();
            col = table[GetYMean_ColumnName()];
            if (col == null)
            {
                NotFound(GetYMean_ColumnName());
            }
            sel.Add(table.DataColumns.GetColumnNumber(col));
            preprocessSet.YMean = DataColumnWrapper.ToROVector(col, numberOfY);

            sel.Clear();
            col = table[GetYScale_ColumnName()];
            if (col == null)
            {
                NotFound(GetYScale_ColumnName());
            }
            sel.Add(table.DataColumns.GetColumnNumber(col));
            preprocessSet.YScale = DataColumnWrapper.ToROVector(col, numberOfY);


            for (int yn = 0; yn < numberOfY; yn++)
            {
                sel.Clear();
                for (int i = 0; i < numberOfFactors; i++)
                {
                    string colname = GetXWeight_ColumnName(yn, i);
                    col = table[colname];
                    if (col == null)
                    {
                        NotFound(colname);
                    }
                    sel.Add(table.DataColumns.GetColumnNumber(col));
                }
                calibrationSet.XWeights[yn] = DataTableWrapper.ToRORowMatrix(table.DataColumns, sel, numberOfX);


                sel.Clear();
                for (int i = 0; i < numberOfFactors; i++)
                {
                    string colname = GetXLoad_ColumnName(yn, i);
                    col = table[colname];
                    if (col == null)
                    {
                        NotFound(colname);
                    }
                    sel.Add(table.DataColumns.GetColumnNumber(col));
                }
                calibrationSet.XLoads[yn] = DataTableWrapper.ToRORowMatrix(table.DataColumns, sel, numberOfX);


                sel.Clear();
                for (int i = 0; i < numberOfFactors; i++)
                {
                    string colname = GetYLoad_ColumnName(yn, i);
                    col = table[colname];
                    if (col == null)
                    {
                        NotFound(colname);
                    }
                    sel.Add(table.DataColumns.GetColumnNumber(col));
                }
                calibrationSet.YLoads[yn] = DataTableWrapper.ToRORowMatrix(table.DataColumns, sel, numberOfY);


                sel.Clear();
                col = table[GetCrossProduct_ColumnName(yn)];
                if (col == null)
                {
                    NotFound(GetCrossProduct_ColumnName());
                }
                sel.Add(table.DataColumns.GetColumnNumber(col));
                calibrationSet.CrossProduct[yn] = DataTableWrapper.ToRORowMatrix(table.DataColumns, sel, numberOfFactors);
            }
        }