/// <summary> /// Copies all settings from another instance. /// </summary> /// <param name="from">The other instance to copy the data from.</param> public void CopyFrom(SpectralPreprocessingOptions from) { this._method = from._method; this._detrendingOrder = from._detrendingOrder; this._ensembleScale = from._ensembleScale; this._regions = (int[])from._regions.Clone(); }
public CrossPRESSEvaluator( int[] spectralRegions, int numFactors, ICrossValidationGroupingStrategy groupingStrategy, SpectralPreprocessingOptions preprocessOptions, MultivariateRegression analysis ) : base(spectralRegions,numFactors,groupingStrategy,preprocessOptions,analysis) { }
public CrossPRESSEvaluator( int[] spectralRegions, int numFactors, ICrossValidationGroupingStrategy groupingStrategy, SpectralPreprocessingOptions preprocessOptions, MultivariateRegression analysis ) : base(spectralRegions, numFactors, groupingStrategy, preprocessOptions, analysis) { }
/// <summary> /// Copies all settings from another instance. /// </summary> /// <param name="from">The other instance to copy the data from.</param> public void CopyFrom(SpectralPreprocessingOptions from) { if (object.ReferenceEquals(this, from)) { return; } _method = from._method; _detrendingOrder = from._detrendingOrder; _ensembleScale = from._ensembleScale; _regions = (int[])from._regions.Clone(); }
public CrossPredictedXResidualsEvaluator( int numberOfPoints, int[] spectralRegions, int numFactors, ICrossValidationGroupingStrategy groupingStrategy, SpectralPreprocessingOptions preprocessOptions, MultivariateRegression analysis ) : base(spectralRegions, numFactors, groupingStrategy, preprocessOptions, analysis) { _numberOfPoints = numberOfPoints; }
public CrossPredictedYEvaluator( int[] spectralRegions, int numFactors, ICrossValidationGroupingStrategy groupingStrategy, SpectralPreprocessingOptions preprocessOptions, MultivariateRegression analysis, IMatrix YCrossValidationPrediction ) : base(spectralRegions, numFactors, groupingStrategy, preprocessOptions, analysis) { _YCrossValidationPrediction = YCrossValidationPrediction; }
public CrossValidationWorker( int[] spectralRegions, int numFactors, ICrossValidationGroupingStrategy groupingStrategy, SpectralPreprocessingOptions preprocessOptions, MultivariateRegression analysis ) { _spectralRegions = spectralRegions; _numFactors = numFactors; _groupingStrategy = groupingStrategy; _preprocessOptions = preprocessOptions; _analysis = analysis; }
public CrossValidationWorker( int[] spectralRegions, int numFactors, ICrossValidationGroupingStrategy groupingStrategy, SpectralPreprocessingOptions preprocessOptions, MultivariateRegression analysis ) { _spectralRegions = spectralRegions; _numFactors = numFactors; _groupingStrategy = groupingStrategy; _preprocessOptions = preprocessOptions; _analysis = analysis; }
/// <summary> /// Makes a PLS (a partial least squares) analysis of the table or the selected columns / rows and stores the results in a newly created table. /// </summary> /// <param name="mainDocument">The main document of the application.</param> /// <param name="srctable">The table where the data come from.</param> /// <param name="selectedColumns">The selected columns.</param> /// <param name="selectedRows">The selected rows.</param> /// <param name="selectedPropertyColumns">The selected property column(s).</param> /// <param name="bHorizontalOrientedSpectrum">True if a spectrum is a single row, False if a spectrum is a single column.</param> /// <param name="plsOptions">Provides information about the max number of factors and the calculation of cross PRESS value.</param> /// <param name="preprocessOptions">Provides information about how to preprocess the spectra.</param> /// <returns></returns> public virtual string ExecuteAnalysis( Altaxo.AltaxoDocument mainDocument, Altaxo.Data.DataTable srctable, IAscendingIntegerCollection selectedColumns, IAscendingIntegerCollection selectedRows, IAscendingIntegerCollection selectedPropertyColumns, bool bHorizontalOrientedSpectrum, MultivariateAnalysisOptions plsOptions, SpectralPreprocessingOptions preprocessOptions ) { IMatrix matrixX, matrixY; IROVector xOfX; var plsContent = new MultivariateContentMemento(); plsContent.Analysis = this; // now we have to create a new table where to place the calculated factors and loads // we will do that in a vertical oriented manner, i.e. even if the loads are // here in horizontal vectors: in our table they are stored in (vertical) columns string newName = this.AnalysisName + " of " + Main.ProjectFolder.GetNamePart(srctable.Name); newName = Main.ProjectFolder.CreateFullName(srctable.Name, newName); Altaxo.Data.DataTable table = new Altaxo.Data.DataTable(newName); // Fill the Table using (var suspendToken = table.SuspendGetToken()) { table.SetTableProperty("Content", plsContent); plsContent.OriginalDataTableName = srctable.Name; // Get matrices GetXYMatrices( srctable, selectedColumns, selectedRows, selectedPropertyColumns, bHorizontalOrientedSpectrum, plsContent, out matrixX, out matrixY, out xOfX); StoreXOfX(xOfX, table); // Preprocess plsContent.SpectralPreprocessing = preprocessOptions; IVector meanX, scaleX, meanY, scaleY; MultivariateRegression.PreprocessForAnalysis(preprocessOptions, xOfX, matrixX, matrixY, out meanX, out scaleX, out meanY, out scaleY); StorePreprocessedData(meanX, scaleX, meanY, scaleY, table); // Analyze and Store IROVector press; ExecuteAnalysis( matrixX, matrixY, plsOptions, plsContent, table, out press); this.StorePRESSData(press, table); if (plsOptions.CrossPRESSCalculation != CrossPRESSCalculationType.None) CalculateCrossPRESS(xOfX, matrixX, matrixY, plsOptions, plsContent, table); StoreFRatioData(table, plsContent); StoreOriginalY(table, plsContent); suspendToken.Dispose(); } Current.Project.DataTableCollection.Add(table); // create a new worksheet without any columns Current.ProjectService.CreateNewWorksheet(table); return null; }
/// <summary> /// Creates an analyis from the raw spectra and raw concentrations. /// </summary> /// <param name="preprocessOptions">Information how to preprocess the data.</param> /// <param name="xOfX">The spectral x variable (e.g. frequencies, wavelength).</param> /// <param name="matrixX">Matrix of preprocessed spectra (number of observations, number of wavelengths).</param> /// <param name="matrixY">Matrix of preprocessed y values (number of observations, number of y).</param> /// <param name="maxFactors">Maximum number of factors to calculate.</param> public void AnalyzeFromRaw( SpectralPreprocessingOptions preprocessOptions, IROVector xOfX, IMatrix matrixX, IMatrix matrixY, int maxFactors) { AnalyzeFromRaw(preprocessOptions, SpectralPreprocessingOptions.IdentifyRegions(xOfX), matrixX, matrixY, maxFactors); }
/// <summary> /// Copies all settings from another instance. /// </summary> /// <param name="from">The other instance to copy the data from.</param> public void CopyFrom(SpectralPreprocessingOptions from) { this._method = from._method; this._detrendingOrder = from._detrendingOrder; this._ensembleScale = from._ensembleScale; this._regions = (int[])from._regions.Clone(); }
public CrossPredictedYEvaluator( int[] spectralRegions, int numFactors, ICrossValidationGroupingStrategy groupingStrategy, SpectralPreprocessingOptions preprocessOptions, MultivariateRegression analysis, IMatrix YCrossValidationPrediction ) : base(spectralRegions,numFactors,groupingStrategy,preprocessOptions,analysis) { _YCrossValidationPrediction = YCrossValidationPrediction; }
/// <summary> /// Asks the user for the maximum number of factors and the cross validation calculation. /// </summary> /// <param name="options">The PLS options to ask for. On return, this is the user's choice.</param> /// <param name="preprocessOptions">The spectral preprocessing options to ask for (output).</param> /// <returns>True if the user has made his choice, false if the user pressed the Cancel button.</returns> public static bool QuestPLSAnalysisOptions(out MultivariateAnalysisOptions options, out SpectralPreprocessingOptions preprocessOptions) { options = new MultivariateAnalysisOptions(); options.MaxNumberOfFactors =20; options.CrossPRESSCalculation = CrossPRESSCalculationType.ExcludeGroupsOfSimilarMeasurements; PLSStartAnalysisController ctrlAA = new PLSStartAnalysisController(options); PLSStartAnalysisControl viewAA = new PLSStartAnalysisControl(); ctrlAA.View = viewAA; preprocessOptions = new SpectralPreprocessingOptions(); SpectralPreprocessingController ctrlBB = new SpectralPreprocessingController(preprocessOptions); SpectralPreprocessingControl viewBB = new SpectralPreprocessingControl(); ctrlBB.View = viewBB; TabbedElementController tabController = new TabbedElementController(); tabController.AddTab("Factors",ctrlAA,viewAA); tabController.AddTab("Preprocessing", ctrlBB, viewBB); if (Current.Gui.ShowDialog(tabController, "Enter analysis parameters", false)) { options = ctrlAA.Doc; return true; } /* TabbedDialogController dialogctrl = new TabbedDialogController("PLS Analysis",false); dialogctrl.AddTab("Factors",ctrlAA,viewAA); dialogctrl.AddTab("Preprocessing",ctrlBB,viewBB); TabbedDialogView dialogview = new TabbedDialogView(); dialogctrl.View = dialogview; if(dialogctrl.ShowDialog(Current.MainWindow)) { options = ctrlAA.Doc; return true; } */ return false; }
/// <summary> /// This will convert the raw spectra (horizontally in matrixX) to preprocessed spectra according to the calibration model. /// </summary> /// <param name="preprocessOptions">Information how to preprocess the spectra.</param> /// <param name="matrixX">Matrix of raw spectra. On return, this matrix contains the preprocessed spectra.</param> /// <param name="meanX">Mean spectrum.</param> /// <param name="scaleX">Scale spectrum.</param> public static void PreprocessSpectraForPrediction( SpectralPreprocessingOptions preprocessOptions, IMatrix matrixX, IROVector meanX, IROVector scaleX) { preprocessOptions.ProcessForPrediction(matrixX,meanX,scaleX); }
/// <summary> /// This will convert the raw spectra (horizontally in matrixX) to preprocessed spectra according to the calibration model. /// </summary> /// <param name="calib">The calibration model containing the instructions to process the spectra.</param> /// <param name="preprocessOptions">Contains the information how to preprocess the spectra.</param> /// <param name="matrixX">The matrix of spectra. Each spectrum is a row of the matrix.</param> public static void PreprocessSpectraForPrediction( IMultivariateCalibrationModel calib, SpectralPreprocessingOptions preprocessOptions, IMatrix matrixX) { preprocessOptions.ProcessForPrediction(matrixX,calib.PreprocessingModel.XMean,calib.PreprocessingModel.XScale); }
/// <summary> /// This will process the spectra before analysis in multivariate calibration. /// </summary> /// <param name="preprocessOptions">Contains the information how to preprocess the spectra.</param> /// <param name="xOfX"></param> /// <param name="matrixX">The matrix of spectra. Each spectrum is a row of the matrix.</param> /// <param name="meanX"></param> /// <param name="scaleX"></param> public static void PreprocessSpectraForAnalysis( SpectralPreprocessingOptions preprocessOptions, IROVector xOfX, IMatrix matrixX, out IVector meanX, out IVector scaleX ) { // Before we can apply PLS, we have to center the x and y matrices meanX = new MatrixMath.HorizontalVector(matrixX.Columns); scaleX = new MatrixMath.HorizontalVector(matrixX.Columns); // MatrixMath.HorizontalVector scaleX = new MatrixMath.HorizontalVector(matrixX.Cols); preprocessOptions.SetRegionsByIdentification(xOfX); preprocessOptions.Process(matrixX,meanX,scaleX); }
/// <summary> /// Preprocesses the x and y matrices before usage in multivariate calibrations. /// </summary> /// <param name="preprocessOptions">Information how to preprocess the data.</param> /// <param name="xOfX">Spectral wavelength values.</param> /// <param name="matrixX">Matrix of spectra.</param> /// <param name="matrixY">Matrix of concentrations.</param> /// <returns>The collected data about proprocessing.</returns> public static MultivariatePreprocessingModel PreprocessForAnalysis( SpectralPreprocessingOptions preprocessOptions, IROVector xOfX, IMatrix matrixX, IMatrix matrixY) { MultivariatePreprocessingModel result= PreprocessForAnalysis( preprocessOptions, SpectralPreprocessingOptions.IdentifyRegions(xOfX), matrixX, matrixY); result.XOfX = xOfX; return result; }
/// <summary> /// Preprocesses the x and y matrices before usage in multivariate calibrations. /// </summary> /// <param name="preprocessOptions">Information how to preprocess the data.</param> /// <param name="spectralRegions">Array of ascending indices representing the starting indices of spectral regions.</param> /// <param name="matrixX">Matrix of spectra.</param> /// <param name="matrixY">Matrix of concentrations.</param> /// <returns>The collected data about proprocessing.</returns> public static MultivariatePreprocessingModel PreprocessForAnalysis( SpectralPreprocessingOptions preprocessOptions, int[] spectralRegions, IMatrix matrixX, IMatrix matrixY) { MultivariatePreprocessingModel data = new MultivariatePreprocessingModel(); data.PreprocessOptions = (SpectralPreprocessingOptions)preprocessOptions.Clone(); data.SpectralRegions = spectralRegions; IVector meanX, scaleX; PreprocessSpectraForAnalysis(preprocessOptions,spectralRegions,matrixX,out meanX, out scaleX); data.XMean = meanX; data.XScale = scaleX; IVector meanY, scaleY; PreprocessYForAnalysis(matrixY,out meanY, out scaleY); data.YMean = meanY; data.YScale = scaleY; return data; }
/// <summary> /// Preprocesses the x and y matrices before usage in multivariate calibrations. /// </summary> /// <param name="preprocessOptions">Information how to preprocess the data.</param> /// <param name="xOfX"></param> /// <param name="matrixX"></param> /// <param name="matrixY"></param> /// <param name="meanX"></param> /// <param name="scaleX"></param> /// <param name="meanY"></param> /// <param name="scaleY"></param> public static void PreprocessForAnalysis( SpectralPreprocessingOptions preprocessOptions, IROVector xOfX, IMatrix matrixX, IMatrix matrixY, out IVector meanX, out IVector scaleX, out IVector meanY, out IVector scaleY) { PreprocessSpectraForAnalysis(preprocessOptions,xOfX,matrixX,out meanX, out scaleX); PreprocessYForAnalysis(matrixY,out meanY, out scaleY); }
/// <summary> /// /// </summary> /// <param name="mcalib"></param> /// <param name="groupingStrategy"></param> /// <param name="preprocessOptions"></param> /// <param name="xOfX"></param> /// <param name="matrixX">Matrix of horizontal spectra, centered and preprocessed.</param> /// <param name="matrixY">Matrix of concentrations, centered.</param> /// <param name="numberOfFactors"></param> /// <param name="predictedY"></param> /// <param name="spectralResiduals"></param> public virtual void CalculateCrossPredictedY( IMultivariateCalibrationModel mcalib, ICrossValidationGroupingStrategy groupingStrategy, SpectralPreprocessingOptions preprocessOptions, IROVector xOfX, IMatrix matrixX, IMatrix matrixY, int numberOfFactors, IMatrix predictedY, IMatrix spectralResiduals) { MultivariateRegression.GetCrossYPredicted(xOfX, matrixX, matrixY, numberOfFactors, groupingStrategy, preprocessOptions, this.CreateNewRegressionObject(), predictedY); }
/// <summary> /// For a given set of spectra, predicts the y-values and stores them in the matrix <c>predictedY</c> /// </summary> /// <param name="mcalib">The calibration model of the analysis.</param> /// <param name="preprocessOptions">The information how to preprocess the spectra.</param> /// <param name="matrixX">The matrix of spectra to predict. Each spectrum is a row in the matrix.</param> /// <param name="numberOfFactors">The number of factors used for prediction.</param> /// <param name="predictedY">On return, this matrix holds the predicted y-values. Each row in this matrix corresponds to the same row (spectrum) in matrixX.</param> /// <param name="spectralResiduals">If you set this parameter to a appropriate matrix, the spectral residuals will be stored in this matrix. Set this parameter to null if you don't need the residuals.</param> public virtual void CalculatePredictedY( IMultivariateCalibrationModel mcalib, SpectralPreprocessingOptions preprocessOptions, IMatrix matrixX, int numberOfFactors, MatrixMath.BEMatrix predictedY, IMatrix spectralResiduals) { MultivariateRegression.PreprocessSpectraForPrediction(mcalib, preprocessOptions, matrixX); MultivariateRegression regress = this.CreateNewRegressionObject(); regress.SetCalibrationModel(mcalib); regress.PredictedYAndSpectralResidualsFromPreprocessed(matrixX, numberOfFactors, predictedY, spectralResiduals); MultivariateRegression.PostprocessY(mcalib.PreprocessingModel, predictedY); }
/// <summary> /// Get the cross predicted error sum of squares for the number of factors=0...numFactors. /// </summary> /// <param name="spectralRegions">Array of ascending indices representing the starting indices of spectral regions.</param> /// <param name="X">Matrix of spectra (a spectrum = a row in the matrix).</param> /// <param name="Y">Matrix of y values (e.g. concentrations).</param> /// <param name="numFactors">Maximum number of factors to calculate the cross PRESS for.</param> /// <param name="groupingStrategy">The strategy how to group the spectra for cross prediction.</param> /// <param name="preprocessOptions">Information how to preprocess the data.</param> /// <param name="regress">The type of regression (e.g. PCR, PLS1, PLS2) provided as an empty regression object.</param> /// <param name="crossPRESS">The vector of CROSS press values. Note that this vector has the length numFactor+1.</param> /// <returns>The mean number of spectra used for prediction.</returns> public static double GetCrossPRESS( int[] spectralRegions, IROMatrix X, // matrix of spectra (a spectra is a row of this matrix) IROMatrix Y, // matrix of concentrations (a mixture is a row of this matrix) int numFactors, ICrossValidationGroupingStrategy groupingStrategy, SpectralPreprocessingOptions preprocessOptions, MultivariateRegression regress, out IROVector crossPRESS // vertical value of PRESS values for the cross validation ) { CrossPRESSEvaluator worker = new CrossPRESSEvaluator(spectralRegions,numFactors,groupingStrategy,preprocessOptions,regress); double result = CrossValidationIteration(X,Y,groupingStrategy,new CrossValidationIterationFunction(worker.EhCrossPRESS)); crossPRESS = VectorMath.ToROVector(worker.CrossPRESS,worker.NumberOfFactors+1); return result; }
/// <summary> /// Constructor. Supply a document to control here. /// </summary> /// <param name="doc">The instance of option to set-up.</param> public SpectralPreprocessingController(SpectralPreprocessingOptions doc) { _doc = doc; }
/// <summary> /// Calculates the cross predicted y values. /// </summary> /// <param name="spectralRegions">Array of ascending indices representing the starting indices of spectral regions.</param> /// <param name="X">Matrix of spectra (a spectrum = a row in the matrix).</param> /// <param name="Y">Matrix of y values (e.g. concentrations).</param> /// <param name="numFactors">Number of factors used for calculation.</param> /// <param name="groupingStrategy">The strategy how to group the spectra for cross prediction.</param> /// <param name="preprocessOptions">Information how to preprocess the data.</param> /// <param name="regress">The type of regression (e.g. PCR, PLS1, PLS2) provided as an empty regression object.</param> /// <param name="yCrossPredicted">Matrix of cross predicted y values. Must be of same dimension as the Y matrix.</param> /// <returns>Mean number of spectra used for cross prediction.</returns> public static double GetCrossYPredicted( int[] spectralRegions, IROMatrix X, // matrix of spectra (a spectra is a row of this matrix) IROMatrix Y, // matrix of concentrations (a mixture is a row of this matrix) int numFactors, ICrossValidationGroupingStrategy groupingStrategy, SpectralPreprocessingOptions preprocessOptions, MultivariateRegression regress, IMatrix yCrossPredicted // vertical value of PRESS values for the cross validation ) { CrossPredictedYEvaluator worker = new CrossPredictedYEvaluator(spectralRegions,numFactors,groupingStrategy,preprocessOptions,regress,yCrossPredicted); double result = CrossValidationIteration(X,Y,groupingStrategy,new CrossValidationIterationFunction(worker.EhYCrossPredicted)); return result; }
public CrossPredictedXResidualsEvaluator( int numberOfPoints, int[] spectralRegions, int numFactors, ICrossValidationGroupingStrategy groupingStrategy, SpectralPreprocessingOptions preprocessOptions, MultivariateRegression analysis ) : base(spectralRegions,numFactors,groupingStrategy,preprocessOptions,analysis) { _numberOfPoints = numberOfPoints; }
/// <summary> /// Calculates the cross predicted y values. /// </summary> /// <param name="xOfX">The spectral wavelength values corresponding to the spectral bins.</param> /// <param name="X">Matrix of spectra (a spectrum = a row in the matrix).</param> /// <param name="Y">Matrix of y values (e.g. concentrations).</param> /// <param name="numFactors">Number of factors used for calculation.</param> /// <param name="groupingStrategy">The strategy how to group the spectra for cross prediction.</param> /// <param name="preprocessOptions">Information how to preprocess the data.</param> /// <param name="regress">The type of regression (e.g. PCR, PLS1, PLS2) provided as an empty regression object.</param> /// <param name="yCrossPredicted">Matrix of cross predicted y values. Must be of same dimension as the Y matrix.</param> /// <returns>Mean number of spectra used for cross prediction.</returns> public static double GetCrossYPredicted( IROVector xOfX, IROMatrix X, // matrix of spectra (a spectra is a row of this matrix) IROMatrix Y, // matrix of concentrations (a mixture is a row of this matrix) int numFactors, ICrossValidationGroupingStrategy groupingStrategy, SpectralPreprocessingOptions preprocessOptions, MultivariateRegression regress, IMatrix yCrossPredicted // vertical value of PRESS values for the cross validation ) { return GetCrossYPredicted( SpectralPreprocessingOptions.IdentifyRegions(xOfX), X, // matrix of spectra (a spectra is a row of this matrix) Y, // matrix of concentrations (a mixture is a row of this matrix) numFactors, groupingStrategy, preprocessOptions, regress, yCrossPredicted ); }
/// <summary> /// Copy constructor. /// </summary> /// <param name="from"></param> public SpectralPreprocessingOptions(SpectralPreprocessingOptions from) { CopyFrom(from); }
/// <summary> /// Asks the user for the maximum number of factors and the cross validation calculation. /// </summary> /// <param name="options">The PLS options to ask for. On return, this is the user's choice.</param> /// <param name="preprocessOptions">The spectral preprocessing options to ask for (output).</param> /// <returns>True if the user has made his choice, false if the user pressed the Cancel button.</returns> public static bool QuestPLSAnalysisOptions(out MultivariateAnalysisOptions options, out SpectralPreprocessingOptions preprocessOptions) { options = new MultivariateAnalysisOptions(); options.MaxNumberOfFactors = 20; options.CrossPRESSCalculation = CrossPRESSCalculationType.ExcludeGroupsOfSimilarMeasurements; PLSStartAnalysisController ctrlAA = new PLSStartAnalysisController(options); Current.Gui.FindAndAttachControlTo(ctrlAA); preprocessOptions = new SpectralPreprocessingOptions(); SpectralPreprocessingController ctrlBB = new SpectralPreprocessingController(preprocessOptions); Current.Gui.FindAndAttachControlTo(ctrlBB); TabbedElementController tabController = new TabbedElementController(); tabController.AddTab("Factors", ctrlAA, ctrlAA.ViewObject); tabController.AddTab("Preprocessing", ctrlBB, ctrlBB.ViewObject); if (Current.Gui.ShowDialog(tabController, "Enter analysis parameters", false)) { options = ctrlAA.Doc; return true; } return false; }
/// <summary> /// Copy constructor. /// </summary> /// <param name="from"></param> public SpectralPreprocessingOptions(SpectralPreprocessingOptions from) { CopyFrom(from); }
/// <summary> /// Calculates the spectral leverage from raw spectra. /// </summary> /// <param name="preprocessOptions"></param> /// <param name="matrixX">Matrix of spectra (a spectrum = a row in the matrix).</param> /// <param name="numFactors">Number of factors used for calculation.</param> /// <returns>Matrix of spectral leverages. Normally, this is a (NumberOfPoints,1) matrix, with exception of PLS1, where it is a (NumberOfPoints,NumberOfY) matrix.</returns> public virtual IROMatrix GetXLeverageFromRaw(SpectralPreprocessingOptions preprocessOptions, IMatrix matrixX, int numFactors) { MultivariateRegression.PreprocessSpectraForPrediction(InternalCalibrationModel,preprocessOptions,matrixX); return GetXLeverageFromPreprocessed(matrixX,numFactors); }
/// <summary> /// Calculates the spectral residuals obtained from cross validation. /// </summary> /// <param name="spectralRegions">Array of ascending indices representing the starting indices of spectral regions.</param> /// <param name="X">Matrix of spectra (a spectrum = a row in the matrix).</param> /// <param name="Y">Matrix of y values (e.g. concentrations).</param> /// <param name="numFactors">Number of factors used for calculation.</param> /// <param name="groupingStrategy">The strategy how to group the spectra for cross prediction.</param> /// <param name="preprocessOptions">Information how to preprocess the data.</param> /// <param name="regress">The type of regression (e.g. PCR, PLS1, PLS2) provided as an empty regression object.</param> /// <param name="crossXResiduals">Returns the matrix of spectral residuals</param> /// <returns>Mean number of spectra used for prediction.</returns> public static double GetCrossXResiduals( int[] spectralRegions, IROMatrix X, // matrix of spectra (a spectra is a row of this matrix) IROMatrix Y, // matrix of concentrations (a mixture is a row of this matrix) int numFactors, ICrossValidationGroupingStrategy groupingStrategy, SpectralPreprocessingOptions preprocessOptions, MultivariateRegression regress, out IROMatrix crossXResiduals ) { CrossPredictedXResidualsEvaluator worker = new CrossPredictedXResidualsEvaluator(X.Rows,spectralRegions,numFactors,groupingStrategy,preprocessOptions,regress); double result = CrossValidationIteration(X,Y,groupingStrategy,new CrossValidationIterationFunction(worker.EhCrossValidationWorker)); crossXResiduals = worker.XCrossResiduals; return result; }
/// <summary> /// Creates an analyis from the raw spectra and raw concentrations. /// </summary> /// <param name="preprocessOptions">Information how to preprocess the data.</param> /// <param name="spectralRegions">Array of ascending indices representing the starting indices of spectral regions.</param> /// <param name="matrixX">Matrix of preprocessed spectra (number of observations, number of wavelengths).</param> /// <param name="matrixY">Matrix of preprocessed y values (number of observations, number of y).</param> /// <param name="maxFactors">Maximum number of factors to calculate.</param> public void AnalyzeFromRaw( SpectralPreprocessingOptions preprocessOptions, int[] spectralRegions, IMatrix matrixX, IMatrix matrixY, int maxFactors) { Reset(); InternalCalibrationModel.SetPreprocessingModel( PreprocessForAnalysis(preprocessOptions,spectralRegions,matrixX,matrixY) ); InternalCalibrationModel.NumberOfX = matrixX.Columns; InternalCalibrationModel.NumberOfY = matrixY.Columns; AnalyzeFromPreprocessedWithoutReset(matrixX, matrixY, maxFactors); }
/// <summary> /// Calculates the spectral residuals obtained from cross validation. /// </summary> /// <param name="xOfX">The spectral wavelength values corresponding to the spectral bins.</param> /// <param name="X">Matrix of spectra (a spectrum = a row in the matrix).</param> /// <param name="Y">Matrix of y values (e.g. concentrations).</param> /// <param name="numFactors">Number of factors used for calculation.</param> /// <param name="groupingStrategy">The strategy how to group the spectra for cross prediction.</param> /// <param name="preprocessOptions">Information how to preprocess the data.</param> /// <param name="regress">The type of regression (e.g. PCR, PLS1, PLS2) provided as an empty regression object.</param> /// <param name="crossXResiduals">Returns the matrix of spectral residuals</param> /// <returns>Mean number of spectra used for prediction.</returns> public static double GetCrossXResiduals( IROVector xOfX, IROMatrix X, // matrix of spectra (a spectra is a row of this matrix) IROMatrix Y, // matrix of concentrations (a mixture is a row of this matrix) int numFactors, ICrossValidationGroupingStrategy groupingStrategy, SpectralPreprocessingOptions preprocessOptions, MultivariateRegression regress, out IROMatrix crossXResiduals ) { return GetCrossXResiduals( SpectralPreprocessingOptions.IdentifyRegions(xOfX), X, // matrix of spectra (a spectra is a row of this matrix) Y, // matrix of concentrations (a mixture is a row of this matrix) numFactors, groupingStrategy, preprocessOptions, regress, out crossXResiduals); }
/// <summary> /// Copies all settings from another instance. /// </summary> /// <param name="from">The other instance to copy the data from.</param> public void CopyFrom(SpectralPreprocessingOptions from) { if (object.ReferenceEquals(this, from)) return; this._method = from._method; this._detrendingOrder = from._detrendingOrder; this._ensembleScale = from._ensembleScale; this._regions = (int[])from._regions.Clone(); }