Example #1
0
        /// <summary>
        /// This predicts the selected columns/rows against a user choosen calibration model.
        /// The orientation of spectra is given by the parameter <c>spectrumIsRow</c>.
        /// </summary>
        /// <param name="ctrl">The worksheet controller containing the selected data.</param>
        /// <param name="spectrumIsRow">If true, the spectra is horizontally oriented, else it is vertically oriented.</param>
        public static void PredictValues(WorksheetController ctrl, bool spectrumIsRow)
        {
            string modelName, destName;

            if (false == QuestCalibrationModelAndDestinationTable(out modelName, out destName) || null == modelName)
            {
                return; // Cancelled by user
            }
            Altaxo.Data.DataTable modelTable = Current.Project.DataTableCollection[modelName];
            Altaxo.Data.DataTable destTable  = (null == destName ? new Altaxo.Data.DataTable() : Current.Project.DataTableCollection[destName]);

            if (modelTable == null || destTable == null)
            {
                throw new ApplicationException("Unexpected: modelTable or destTable is null");
            }

            int numberOfFactors = 0;

            MultivariateContentMemento memento = modelTable.GetTableProperty("Content") as MultivariateContentMemento;

            if (memento != null)
            {
                numberOfFactors = memento.PreferredNumberOfFactors;
            }

            if (numberOfFactors == 0)
            {
                QuestPreferredNumberOfFactors(modelTable);
                memento = modelTable.GetTableProperty("Content") as MultivariateContentMemento;
                if (memento != null)
                {
                    numberOfFactors = memento.PreferredNumberOfFactors;
                }
            }

            memento.Analysis.PredictValues(
                ctrl.DataTable,
                ctrl.SelectedDataColumns,
                ctrl.SelectedDataRows,
                spectrumIsRow,
                numberOfFactors,
                modelTable,
                destTable);



            // if destTable is new, show it
            if (destTable.ParentObject == null)
            {
                Current.Project.DataTableCollection.Add(destTable);
                Current.ProjectService.OpenOrCreateWorksheetForTable(destTable);
            }
        }
Example #2
0
        /// <summary>
        /// Plots all preprocessed spectra into a newly created graph.
        /// </summary>
        /// <param name="table">The table of PLS output data.</param>
        public static void PlotPredictionScores(Altaxo.Data.DataTable table)
        {
            MultivariateContentMemento plsMemo = table.GetTableProperty("Content") as MultivariateContentMemento;

            if (plsMemo == null)
            {
                return;
            }
            if (plsMemo.PreferredNumberOfFactors <= 0)
            {
                QuestPreferredNumberOfFactors(plsMemo);
            }

            GetAnalysis(table).CalculateAndStorePredictionScores(table, plsMemo.PreferredNumberOfFactors);

            AscendingIntegerCollection sel = new AscendingIntegerCollection();

            for (int i = 0; i < plsMemo.NumberOfConcentrationData; i++)
            {
                string name = WorksheetAnalysis.GetPredictionScore_ColumnName(i, plsMemo.PreferredNumberOfFactors);
                if (null != table[name])
                {
                    sel.Add(table.DataColumns.GetColumnNumber(table[name]));
                }
            }

            Worksheet.Commands.PlotCommands.PlotLine(table, sel, true, false);
        }
Example #3
0
        /// <summary>
        /// Asks the user for the preferred number of factors to use for calculation and plotting and stores that number in the
        /// PLS content tag of the table.
        /// </summary>
        /// <param name="table">The table which contains the PLS model.</param>
        public static void QuestPreferredNumberOfFactors(Altaxo.Data.DataTable table)
        {
            MultivariateContentMemento plsMemo = table.GetTableProperty("Content") as MultivariateContentMemento;

            if (plsMemo == null)
            {
                return;
            }

            QuestPreferredNumberOfFactors(plsMemo);
        }
Example #4
0
        /// <summary>
        /// Plots the x (spectral) leverage into a graph.
        /// </summary>
        /// <param name="table">The table with the PLS model data.</param>
        public static void PlotXLeverage(Altaxo.Data.DataTable table)
        {
            MultivariateContentMemento plsMemo = table.GetTableProperty("Content") as MultivariateContentMemento;

            if (plsMemo == null)
            {
                return;
            }
            if (plsMemo.PreferredNumberOfFactors <= 0)
            {
                QuestPreferredNumberOfFactors(plsMemo);
            }

            Altaxo.Graph.GUI.IGraphController graphctrl = Current.ProjectService.CreateNewGraph();
            PlotXLeverage(table, graphctrl.Doc.Layers[0], plsMemo.PreferredNumberOfFactors);
        }
Example #5
0
        /// <summary>
        /// Plots the cross prediction values of all y components invidually in a  graph.
        /// </summary>
        /// <param name="table">The table with the PLS model data.</param>
        public static void PlotCrossPredictedVersusActualY(Altaxo.Data.DataTable table)
        {
            MultivariateContentMemento plsMemo = table.GetTableProperty("Content") as MultivariateContentMemento;

            if (plsMemo == null)
            {
                return;
            }
            if (plsMemo.PreferredNumberOfFactors <= 0)
            {
                QuestPreferredNumberOfFactors(plsMemo);
            }

            for (int nComponent = 0; nComponent < plsMemo.NumberOfConcentrationData; nComponent++)
            {
                Altaxo.Graph.GUI.IGraphController graphctrl = Current.ProjectService.CreateNewGraph();
                PlotCrossPredictedVersusActualY(table, graphctrl.Doc.Layers[0], nComponent, plsMemo.PreferredNumberOfFactors);
            }
        }
Example #6
0
		public static MultivariateContentMemento GetContentAsMultivariateContentMemento(DataTable table)
		{
			var result = table.GetTableProperty("Content") as MultivariateContentMemento;
			return result;
		}
    public  static WorksheetAnalysis GetAnalysis(DataTable table)
    {
      MultivariateContentMemento plsMemo = table.GetTableProperty("Content") as MultivariateContentMemento;

      if(plsMemo==null)
        throw new ArgumentException("Table does not contain a PLSContentMemento");
  
      return plsMemo.Analysis;
    }
Example #8
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 PCRCalibrationModel calibrationSet)
		{
			int numberOfX = GetNumberOfX(table);
			int numberOfY = GetNumberOfY(table);
			int numberOfFactors = GetNumberOfFactors(table);
			int numberOfMeasurements = GetNumberOfMeasurements(table);

			calibrationSet = new PCRCalibrationModel();

			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.DataColumns.TryGetColumn(GetXOfX_ColumnName());
			if (col == null || !(col is INumericColumn)) NotFound(GetXOfX_ColumnName());
			preprocessSet.XOfX = Altaxo.Calc.LinearAlgebra.DataColumnWrapper.ToROVector((INumericColumn)col, numberOfX);

			col = table.DataColumns.TryGetColumn(GetXMean_ColumnName());
			if (col == null) NotFound(GetXMean_ColumnName());
			preprocessSet.XMean = Altaxo.Calc.LinearAlgebra.DataColumnWrapper.ToROVector(col, numberOfX);

			col = table.DataColumns.TryGetColumn(GetXScale_ColumnName());
			if (col == null) NotFound(GetXScale_ColumnName());
			preprocessSet.XScale = Altaxo.Calc.LinearAlgebra.DataColumnWrapper.ToROVector(col, numberOfX);

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

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

			sel.Clear();
			for (int i = 0; i < numberOfFactors; i++)
			{
				string colname = GetXScore_ColumnName(i);
				col = table.DataColumns.TryGetColumn(colname);
				if (col == null) NotFound(colname);
				sel.Add(table.DataColumns.GetColumnNumber(col));
			}
			calibrationSet.XScores = DataTableWrapper.ToROColumnMatrix(table.DataColumns, sel, numberOfMeasurements);

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

			sel.Clear();
			for (int i = 0; i < numberOfY; i++)
			{
				string colname = GetYLoad_ColumnName(i);
				col = table.DataColumns.TryGetColumn(colname);
				if (col == null) NotFound(colname);
				sel.Add(table.DataColumns.GetColumnNumber(col));
			}
			calibrationSet.YLoads = DataTableWrapper.ToROColumnMatrix(table.DataColumns, sel, numberOfMeasurements);

			sel.Clear();
			col = table.DataColumns.TryGetColumn(GetCrossProduct_ColumnName());
			if (col == null) NotFound(GetCrossProduct_ColumnName());
			calibrationSet.CrossProduct = Altaxo.Calc.LinearAlgebra.DataColumnWrapper.ToROVector(col, numberOfFactors);
		}
    /// <summary>
    /// Calculates the leverage of the spectral data.
    /// </summary>
    /// <param name="table">Table where the calibration model is stored.</param>
    /// <param name="numberOfFactors">Number of factors used to calculate leverage.</param>
    public virtual void CalculateXLeverage(
      DataTable table, int numberOfFactors)
    {
      MultivariateContentMemento plsMemo = table.GetTableProperty("Content") as MultivariateContentMemento;

      if(plsMemo==null)
        throw new ArgumentException("Table does not contain a PLSContentMemento");

      IMultivariateCalibrationModel calib = this.GetCalibrationModel(table);
      IMatrix matrixX = GetRawSpectra(plsMemo);

      MultivariateRegression.PreprocessSpectraForPrediction(calib,plsMemo.SpectralPreprocessing,matrixX);

      MultivariateRegression regress = this.CreateNewRegressionObject();
      regress.SetCalibrationModel(calib);

      IROMatrix xLeverage = regress.GetXLeverageFromRaw(matrixX,numberOfFactors);

      for(int i=0;i<xLeverage.Columns;i++)
      {
        Altaxo.Data.DoubleColumn col = new Altaxo.Data.DoubleColumn();
        MatrixMath.SetColumn(xLeverage,i,DataColumnWrapper.ToVertMatrix(col,xLeverage.Rows),0);
        table.DataColumns.Add(
          col,
          xLeverage.Columns==1 ? GetXLeverage_ColumnName(numberOfFactors) : GetXLeverage_ColumnName(i,numberOfFactors),
          Altaxo.Data.ColumnKind.V,GetXLeverage_ColumnGroup());
      }
    }
    /// <summary>
    /// This predicts the selected columns/rows against a user choosen calibration model.
    /// The orientation of spectra is given by the parameter <c>spectrumIsRow</c>.
    /// </summary>
    /// <param name="srctable">Table holding the specta to predict values for.</param>
    /// <param name="selectedColumns">Columns selected in the source table.</param>
    /// <param name="selectedRows">Rows selected in the source table.</param>
    /// <param name="destTable">The table to store the prediction result.</param>
    /// <param name="modelTable">The table where the calibration model is stored.</param>
    /// <param name="numberOfFactors">Number of factors used to predict the values.</param>
    /// <param name="spectrumIsRow">If true, the spectra is horizontally oriented, else it is vertically oriented.</param>
    public virtual void PredictValues(
      DataTable srctable,
      IAscendingIntegerCollection selectedColumns,
      IAscendingIntegerCollection selectedRows,
      bool spectrumIsRow,
      int numberOfFactors,
      DataTable modelTable,
      DataTable destTable)
    {

      IMultivariateCalibrationModel calibModel = GetCalibrationModel(modelTable);
      //      Export(modelTable, out calibModel);
      MultivariateContentMemento memento = modelTable.GetTableProperty("Content") as MultivariateContentMemento;

      // Fill matrixX with spectra
      Altaxo.Collections.AscendingIntegerCollection spectralIndices;
      Altaxo.Collections.AscendingIntegerCollection measurementIndices;
      
      
      spectralIndices = new Altaxo.Collections.AscendingIntegerCollection(selectedColumns);
      measurementIndices = new Altaxo.Collections.AscendingIntegerCollection(selectedRows);
      RemoveNonNumericCells(srctable,measurementIndices,spectralIndices);

      // exchange selection if spectrum is column
      if(!spectrumIsRow)
      {
        Altaxo.Collections.AscendingIntegerCollection hlp;
        hlp = spectralIndices;
        spectralIndices = measurementIndices;
        measurementIndices = hlp;
      }
      
      // if there are more data than expected, we have to map the spectral indices
      if(spectralIndices.Count>calibModel.NumberOfX)
      {
        double[] xofx = GetXOfSpectra(srctable,spectrumIsRow,spectralIndices,measurementIndices);

        string errormsg;
        AscendingIntegerCollection map = MapSpectralX(calibModel.PreprocessingModel.XOfX,VectorMath.ToROVector(xofx),out errormsg);
        if(map==null)
          throw new ApplicationException("Can not map spectral data: " + errormsg);
        else
        {
          AscendingIntegerCollection newspectralindices = new AscendingIntegerCollection();
          for(int i=0;i<map.Count;i++)
            newspectralindices.Add(spectralIndices[map[i]]);
          spectralIndices = newspectralindices;
        }
      }

      IMatrix matrixX = GetRawSpectra(srctable,spectrumIsRow,spectralIndices,measurementIndices);

      MatrixMath.BEMatrix predictedY = new MatrixMath.BEMatrix(measurementIndices.Count,calibModel.NumberOfY);
      CalculatePredictedY(calibModel,memento.SpectralPreprocessing,matrixX,numberOfFactors, predictedY,null);

      // now save the predicted y in the destination table

      Altaxo.Data.DoubleColumn labelCol = new Altaxo.Data.DoubleColumn();
      for(int i=0;i<measurementIndices.Count;i++)
      {
        labelCol[i] = measurementIndices[i];
      }
      destTable.DataColumns.Add(labelCol,"MeasurementLabel",Altaxo.Data.ColumnKind.Label,0);

      for(int k=0;k<predictedY.Columns;k++)
      {
        Altaxo.Data.DoubleColumn predictedYcol = new Altaxo.Data.DoubleColumn();

        for(int i=0;i<measurementIndices.Count;i++)
        {
          predictedYcol[i] = predictedY[i,k];
        }
        destTable.DataColumns.Add(predictedYcol,"Predicted Y" + k.ToString(), Altaxo.Data.ColumnKind.V,0);
      }

      
    }
    public  virtual void CalculatePredictedAndResidual(
      DataTable table,
      int whichY,
      int numberOfFactors,
      bool saveYPredicted,
      bool saveYResidual,
      bool saveXResidual)
    {
      MultivariateContentMemento plsMemo = table.GetTableProperty("Content") as MultivariateContentMemento;

      if(plsMemo==null)
        throw new ArgumentException("Table does not contain a PLSContentMemento");

      IMultivariateCalibrationModel calib = GetCalibrationModel(table);
      //      Export(table,out calib);


      IMatrix matrixX = GetRawSpectra(plsMemo);

      MatrixMath.BEMatrix predictedY = new MatrixMath.BEMatrix(matrixX.Rows,calib.NumberOfY);
      MatrixMath.BEMatrix spectralResiduals = new MatrixMath.BEMatrix(matrixX.Rows,1);
      CalculatePredictedY(calib,plsMemo.SpectralPreprocessing,matrixX,numberOfFactors,predictedY,spectralResiduals);

      if(saveYPredicted)
      {
        // insert a column with the proper name into the table and fill it
        string ycolname = GetYPredicted_ColumnName(whichY,numberOfFactors);
        Altaxo.Data.DoubleColumn ycolumn = new Altaxo.Data.DoubleColumn();

        for(int i=0;i<predictedY.Rows;i++)
          ycolumn[i] = predictedY[i,whichY];
      
        table.DataColumns.Add(ycolumn,ycolname,Altaxo.Data.ColumnKind.V,GetYPredicted_ColumnGroup());
      }

      // subract the original y data
      IMatrix matrixY = GetOriginalY(plsMemo);
      MatrixMath.SubtractColumn(predictedY,matrixY,whichY,predictedY);

      if(saveYResidual)
      {
        // insert a column with the proper name into the table and fill it
        string ycolname = GetYResidual_ColumnName(whichY,numberOfFactors);
        Altaxo.Data.DoubleColumn ycolumn = new Altaxo.Data.DoubleColumn();

        for(int i=0;i<predictedY.Rows;i++)
          ycolumn[i] = predictedY[i,whichY];
      
        table.DataColumns.Add(ycolumn,ycolname,Altaxo.Data.ColumnKind.V,GetYResidual_ColumnGroup());
      }


      if(saveXResidual)
      {
        // insert a column with the proper name into the table and fill it
        string ycolname = GetXResidual_ColumnName(whichY,numberOfFactors);
        Altaxo.Data.DoubleColumn ycolumn = new Altaxo.Data.DoubleColumn();

        for(int i=0;i<matrixX.Rows;i++)
        {
          ycolumn[i] = spectralResiduals[i,0];
        }
        table.DataColumns.Add(ycolumn,ycolname,Altaxo.Data.ColumnKind.V,GetXResidual_ColumnGroup());
      }
      
    }
 public PredictionModelExporter(Altaxo.Data.DataTable table, int numberOfFactors)
 {
     _table           = table;
     _memento         = table.GetTableProperty("Content") as MultivariateContentMemento;
     _numberOfFactors = numberOfFactors;
 }