public object Deserialize(object o, Altaxo.Serialization.Xml.IXmlDeserializationInfo info, object parent)
            {
                MultivariateContentMemento s = null != o ? (MultivariateContentMemento)o : new MultivariateContentMemento();

                s.OriginalDataTableName     = info.GetString("Name");
                s.SpectrumIsRow             = info.GetBoolean("SpectrumIsRow");
                s.SpectralIndices           = (IAscendingIntegerCollection)info.GetValue("SpectralIndices", s);
                s.ConcentrationIndices      = (IAscendingIntegerCollection)info.GetValue("ConcentrationIndices", s);
                s.MeasurementIndices        = (IAscendingIntegerCollection)info.GetValue("MeasurementIndices", s);
                s._PreferredNumberOfFactors = info.GetInt32("PreferredNumberOfFactors");

                // new in version 1
                if (info.CurrentElementName == "SpectralPreprocessingRegions")
                {
                    info.GetArray("SpectralPreprocessingRegions", out int[] regions);
                    s._spectralPreprocessing.Regions         = regions;
                    s._spectralPreprocessing.Method          = (SpectralPreprocessingMethod)info.GetEnum("SpectralPreprocessingMethod", typeof(SpectralPreprocessingMethod));
                    s._spectralPreprocessing.DetrendingOrder = info.GetInt32("SpectralPreprocessingDetrending");
                    s._spectralPreprocessing.EnsembleScale   = info.GetBoolean("SpectralPreprocessingEnsembleScale");
                }

                // neccessary since version 2
                s.Analysis = new PLS2WorksheetAnalysis();

                return(s);
            }
            public object Deserialize(object o, Altaxo.Serialization.Xml.IXmlDeserializationInfo info, object parent)
            {
                MultivariateContentMemento s = null != o ? (MultivariateContentMemento)o : new MultivariateContentMemento();

                s.OriginalDataTableName     = info.GetString("Name");
                s.SpectrumIsRow             = info.GetBoolean("SpectrumIsRow");
                s.SpectralIndices           = (IAscendingIntegerCollection)info.GetValue("SpectralIndices", s);
                s.ConcentrationIndices      = (IAscendingIntegerCollection)info.GetValue("ConcentrationIndices", s);
                s.MeasurementIndices        = (IAscendingIntegerCollection)info.GetValue("MeasurementIndices", s);
                s._PreferredNumberOfFactors = info.GetInt32("PreferredNumberOfFactors");

                // new in version 1
                info.GetArray("SpectralPreprocessingRegions", out int[] regions);
                s._spectralPreprocessing.Regions         = regions;
                s._spectralPreprocessing.Method          = (SpectralPreprocessingMethod)info.GetEnum("SpectralPreprocessingMethod", typeof(SpectralPreprocessingMethod));
                s._spectralPreprocessing.DetrendingOrder = info.GetInt32("SpectralPreprocessingDetrending");
                s._spectralPreprocessing.EnsembleScale   = info.GetBoolean("SpectralPreprocessingEnsembleScale");

                // new in version 2
                s._ClassNameOfAnalysisClass = info.GetString("ClassNameOfAnalysisClass");

                // added fix after version 2 : forgotten to serialize crossPRESSCalculationType
                if (info.GetNodeName() == "CrossPRESSCalculationType")
                {
                    s._crossPRESSCalculationType = (CrossPRESSCalculationType)info.GetValue("CrossPRESSCalculationType", s);
                }
                else
                {
                    s._crossPRESSCalculationType = CrossPRESSCalculationType.ExcludeGroupsOfSimilarMeasurements;
                }

                return(s);
            }
            public void Serialize(object obj, Altaxo.Serialization.Xml.IXmlSerializationInfo info)
            {
                MultivariateContentMemento s = (MultivariateContentMemento)obj;

                info.AddValue("TableName", s.TableName); // name of the Table
                info.AddValue("SpectrumIsRow", s.SpectrumIsRow);
                info.AddValue("SpectralIndices", s.SpectralIndices);
                info.AddValue("ConcentrationIndices", s.ConcentrationIndices);
                info.AddValue("MeasurementIndices", s.MeasurementIndices);
                info.AddValue("PreferredNumberOfFactors", s._PreferredNumberOfFactors); // the property columns of that table
            }
            public object Deserialize(object o, Altaxo.Serialization.Xml.IXmlDeserializationInfo info, object parent)
            {
                MultivariateContentMemento s = null != o ? (MultivariateContentMemento)o : new MultivariateContentMemento();

                s.OriginalDataTableName     = info.GetString("Name");
                s.SpectrumIsRow             = info.GetBoolean("SpectrumIsRow");
                s.SpectralIndices           = (IAscendingIntegerCollection)info.GetValue("SpectralIndices", s);
                s.ConcentrationIndices      = (IAscendingIntegerCollection)info.GetValue("ConcentrationIndices", s);
                s.MeasurementIndices        = (IAscendingIntegerCollection)info.GetValue("MeasurementIndices", s);
                s._PreferredNumberOfFactors = info.GetInt32("PreferredNumberOfFactors");

                // neccessary since version 2
                s.Analysis = new PLS2WorksheetAnalysis();

                return(s);
            }
            public void Serialize(object obj, Altaxo.Serialization.Xml.IXmlSerializationInfo info)
            {
                MultivariateContentMemento s = (MultivariateContentMemento)obj;

                info.AddValue("TableName", s.TableName); // name of the Table
                info.AddValue("SpectrumIsRow", s.SpectrumIsRow);
                info.AddValue("SpectralIndices", s.SpectralIndices);
                info.AddValue("ConcentrationIndices", s.ConcentrationIndices);
                info.AddValue("MeasurementIndices", s.MeasurementIndices);
                info.AddValue("PreferredNumberOfFactors", s._PreferredNumberOfFactors); // the property columns of that table

                // new in version 1
                info.AddArray("SpectralPreprocessingRegions", s._spectralPreprocessing.Regions, s._spectralPreprocessing.Regions.Length);
                info.AddEnum("SpectralPreprocessingMethod", s._spectralPreprocessing.Method);
                info.AddValue("SpectralPreprocessingDetrending", s._spectralPreprocessing.DetrendingOrder);
                info.AddValue("SpectralPreprocessingEnsembleScale", s._spectralPreprocessing.EnsembleScale);
            }
Example #6
0
		/// <summary>
		/// Get the matrix of x and y values (raw data).
		/// </summary>
		/// <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="matrixX">On return, gives the matrix of spectra (each spectra is a row in the matrix).</param>
		/// <param name="matrixY">On return, gives the matrix of y-values (each measurement is a row in the matrix).</param>
		/// <param name="plsContent">Holds information about the analysis results.</param>
		/// <param name="xOfX">On return, this is the vector of values corresponding to each spectral bin, i.e. wavelength values, frequencies etc.</param>
		/// <returns></returns>
		public static string GetXYMatrices(
			Altaxo.Data.DataTable srctable,
			IAscendingIntegerCollection selectedColumns,
			IAscendingIntegerCollection selectedRows,
			IAscendingIntegerCollection selectedPropertyColumns,
			bool bHorizontalOrientedSpectrum,
			MultivariateContentMemento plsContent,
			out IMatrix matrixX,
			out IMatrix matrixY,
			out IROVector xOfX
			)
		{
			matrixX = null;
			matrixY = null;
			xOfX = null;
			plsContent.SpectrumIsRow = bHorizontalOrientedSpectrum;

			Altaxo.Data.DataColumn xColumnOfX = null;
			Altaxo.Data.DataColumn labelColumnOfX = new Altaxo.Data.DoubleColumn();

			Altaxo.Data.DataColumnCollection concentration = bHorizontalOrientedSpectrum ? srctable.DataColumns : srctable.PropertyColumns;

			// we presume for now that the spectrum is horizontally,
			// if not we exchange the collections later

			AscendingIntegerCollection numericDataCols = new AscendingIntegerCollection();
			AscendingIntegerCollection numericDataRows = new AscendingIntegerCollection();
			AscendingIntegerCollection concentrationIndices = new AscendingIntegerCollection();

			AscendingIntegerCollection spectralIndices = bHorizontalOrientedSpectrum ? numericDataCols : numericDataRows;
			AscendingIntegerCollection measurementIndices = bHorizontalOrientedSpectrum ? numericDataRows : numericDataCols;

			plsContent.ConcentrationIndices = concentrationIndices;
			plsContent.MeasurementIndices = measurementIndices;
			plsContent.SpectralIndices = spectralIndices;
			plsContent.SpectrumIsRow = bHorizontalOrientedSpectrum;
			plsContent.OriginalDataTableName = srctable.Name;

			bool bUseSelectedColumns = (null != selectedColumns && 0 != selectedColumns.Count);
			// this is the number of columns (for now), but it can be less than this in case
			// not all columns are numeric
			int prenumcols = bUseSelectedColumns ? selectedColumns.Count : srctable.DataColumns.ColumnCount;
			// check for the number of numeric columns
			int numcols = 0;
			for (int i = 0; i < prenumcols; i++)
			{
				int idx = bUseSelectedColumns ? selectedColumns[i] : i;
				if (srctable[idx] is Altaxo.Data.INumericColumn)
				{
					numericDataCols.Add(idx);
					numcols++;
				}
			}

			// check the number of rows
			bool bUseSelectedRows = (null != selectedRows && 0 != selectedRows.Count);
			int numrows;
			if (bUseSelectedRows)
			{
				numrows = selectedRows.Count;
				numericDataRows.Add(selectedRows);
			}
			else
			{
				numrows = 0;
				for (int i = 0; i < numcols; i++)
				{
					int idx = bUseSelectedColumns ? selectedColumns[i] : i;
					numrows = Math.Max(numrows, srctable[idx].Count);
				}
				numericDataRows.Add(ContiguousIntegerRange.FromStartAndCount(0, numrows));
			}

			if (bHorizontalOrientedSpectrum)
			{
				if (numcols < 2)
					return "At least two numeric columns are neccessary to do Partial Least Squares (PLS) analysis!";

				// check that the selected columns are in exactly two groups
				// the group which has more columns is then considered to have
				// the spectrum, the other group is the y-values
				int group0 = -1;
				int group1 = -1;
				int groupcount0 = 0;
				int groupcount1 = 0;

				for (int i = 0; i < numcols; i++)
				{
					int grp = srctable.DataColumns.GetColumnGroup(numericDataCols[i]);

					if (group0 < 0)
					{
						group0 = grp;
						groupcount0 = 1;
					}
					else if (group0 == grp)
					{
						groupcount0++;
					}
					else if (group1 < 0)
					{
						group1 = grp;
						groupcount1 = 1;
					}
					else if (group1 == grp)
					{
						groupcount1++;
					}
					else
					{
						return "The columns you selected must be members of two groups (y-values and spectrum), but actually there are more than two groups!";
					}
				} // end for all columns

				if (groupcount1 <= 0)
					return "The columns you selected must be members of two groups (y-values and spectrum), but actually only one group was detected!";

				if (groupcount1 < groupcount0)
				{
					int hlp;
					hlp = groupcount1;
					groupcount1 = groupcount0;
					groupcount0 = hlp;

					hlp = group1;
					group1 = group0;
					group0 = hlp;
				}

				// group0 is now the group of y-values (concentrations)
				// group1 is now the group of x-values (spectra)

				// we delete group0 from numericDataCols and add it to concentrationIndices

				for (int i = numcols - 1; i >= 0; i--)
				{
					int index = numericDataCols[i];
					if (group0 == srctable.DataColumns.GetColumnGroup(index))
					{
						numericDataCols.Remove(index);
						concentrationIndices.Add(index);
					}
				}

				// fill the corresponding X-Column of the spectra
				xColumnOfX = Altaxo.Data.DataColumn.CreateColumnOfSelectedRows(
					srctable.PropertyColumns.FindXColumnOfGroup(group1),
					spectralIndices);
			}
			else // vertically oriented spectrum -> one spectrum is one data column
			{
				// we have to exchange measurementIndices and

				// if PLS on columns, than we should have property columns selected
				// that designates the y-values
				// so count all property columns

				bool bUseSelectedPropCols = (null != selectedPropertyColumns && 0 != selectedPropertyColumns.Count);
				// this is the number of property columns (for now), but it can be less than this in case
				// not all columns are numeric
				int prenumpropcols = bUseSelectedPropCols ? selectedPropertyColumns.Count : srctable.PropCols.ColumnCount;
				// check for the number of numeric property columns
				for (int i = 0; i < prenumpropcols; i++)
				{
					int idx = bUseSelectedPropCols ? selectedPropertyColumns[i] : i;
					if (srctable.PropCols[idx] is Altaxo.Data.INumericColumn)
					{
						concentrationIndices.Add(idx);
					}
				}

				if (concentrationIndices.Count < 1)
					return "At least one numeric property column must exist to hold the y-values!";

				// fill the corresponding X-Column of the spectra
				xColumnOfX = Altaxo.Data.DataColumn.CreateColumnOfSelectedRows(
					srctable.DataColumns.FindXColumnOf(srctable[measurementIndices[0]]), spectralIndices);
			} // else vertically oriented spectrum

			IVector xOfXRW = VectorMath.CreateExtensibleVector(xColumnOfX.Count);
			xOfX = xOfXRW;
			if (xColumnOfX is INumericColumn)
			{
				for (int i = 0; i < xOfX.Length; i++)
					xOfXRW[i] = ((INumericColumn)xColumnOfX)[i];
			}
			else
			{
				for (int i = 0; i < xOfX.Length; i++)
					xOfXRW[i] = spectralIndices[i];
			}

			// now fill the matrix

			// fill in the y-values
			matrixY = new MatrixMath.BEMatrix(measurementIndices.Count, concentrationIndices.Count);
			for (int i = 0; i < concentrationIndices.Count; i++)
			{
				Altaxo.Data.INumericColumn col = concentration[concentrationIndices[i]] as Altaxo.Data.INumericColumn;
				for (int j = 0; j < measurementIndices.Count; j++)
				{
					matrixY[j, i] = col[measurementIndices[j]];
				}
			} // end fill in yvalues

			matrixX = new MatrixMath.BEMatrix(measurementIndices.Count, spectralIndices.Count);
			if (bHorizontalOrientedSpectrum)
			{
				for (int i = 0; i < spectralIndices.Count; i++)
				{
					labelColumnOfX[i] = spectralIndices[i];
					Altaxo.Data.INumericColumn col = srctable[spectralIndices[i]] as Altaxo.Data.INumericColumn;
					for (int j = 0; j < measurementIndices.Count; j++)
					{
						matrixX[j, i] = col[measurementIndices[j]];
					}
				} // end fill in x-values
			}
			else // vertical oriented spectrum
			{
				for (int i = 0; i < spectralIndices.Count; i++)
				{
					labelColumnOfX[i] = spectralIndices[i];
				}
				for (int i = 0; i < measurementIndices.Count; i++)
				{
					Altaxo.Data.INumericColumn col = srctable[measurementIndices[i]] as Altaxo.Data.INumericColumn;
					for (int j = 0; j < spectralIndices.Count; j++)
					{
						matrixX[i, j] = col[spectralIndices[j]];
					}
				} // end fill in x-values
			}

			return null;
		}
Example #7
0
		/// <summary>
		/// Using the information in the plsMemo, gets the matrix of original Y (concentration) data.
		/// </summary>
		/// <param name="plsMemo">The PLS mememto containing the information where to find the original data.</param>
		/// <returns>Matrix of orignal Y (concentration) data.</returns>
		public static IMatrix GetOriginalY(MultivariateContentMemento plsMemo)
		{
			string tablename = plsMemo.OriginalDataTableName;

			Altaxo.Data.DataTable srctable = Current.Project.DataTableCollection[tablename];

			if (srctable == null)
				throw new ApplicationException(string.Format("Table[{0}] containing original spectral data not found!", tablename));

			Altaxo.Data.DataColumnCollection concentration = plsMemo.SpectrumIsRow ? srctable.DataColumns : srctable.PropertyColumns;
			Altaxo.Collections.IAscendingIntegerCollection concentrationIndices = plsMemo.ConcentrationIndices;
			Altaxo.Collections.IAscendingIntegerCollection measurementIndices = plsMemo.MeasurementIndices;

			// fill in the y-values
			MatrixMath.BEMatrix matrixY = new MatrixMath.BEMatrix(measurementIndices.Count, concentrationIndices.Count);
			for (int i = 0; i < concentrationIndices.Count; i++)
			{
				Altaxo.Data.INumericColumn col = concentration[concentrationIndices[i]] as Altaxo.Data.INumericColumn;
				for (int j = 0; j < measurementIndices.Count; j++)
				{
					matrixY[j, i] = col[measurementIndices[j]];
				}
			} // end fill in yvalues

			return matrixY;
		}
Example #8
0
		/// <summary>
		/// Using the information in the plsMemo, gets the matrix of original spectra. The spectra are horizontal in the matrix, i.e. each spectra is a matrix row.
		/// </summary>
		/// <param name="plsMemo">The PLS memento containing the information about the location of the original data.</param>
		/// <returns>The matrix of the original spectra.</returns>
		public static IMatrix GetRawSpectra(MultivariateContentMemento plsMemo)
		{
			string tablename = plsMemo.OriginalDataTableName;
			if (!Current.Project.DataTableCollection.Contains(tablename))
				throw new OriginalDataTableNotFoundException(string.Format("The original data table <<{0}>> does not exist, was it renamed?", tablename));

			Altaxo.Data.DataTable srctable = Current.Project.DataTableCollection[tablename];

			if (srctable == null)
				throw new ApplicationException(string.Format("Table[{0}] containing original spectral data not found!", tablename));

			Altaxo.Collections.IAscendingIntegerCollection spectralIndices = plsMemo.SpectralIndices;
			Altaxo.Collections.IAscendingIntegerCollection measurementIndices = plsMemo.MeasurementIndices;

			MatrixMath.BEMatrix matrixX =
				new MatrixMath.BEMatrix(measurementIndices.Count, spectralIndices.Count);

			return GetRawSpectra(srctable, plsMemo.SpectrumIsRow, spectralIndices, measurementIndices);
		}
Example #9
0
		/// <summary>
		/// Calculate the cross PRESS values and stores the results in the provided table.
		/// </summary>
		/// <param name="xOfX">Vector of spectral wavelengths. Necessary to divide the spectras in different regions.</param>
		/// <param name="matrixX">Matrix of spectra (horizontal oriented).</param>
		/// <param name="matrixY">Matrix of concentrations.</param>
		/// <param name="plsOptions">Analysis options.</param>
		/// <param name="plsContent">Information about this analysis.</param>
		/// <param name="table">Table to store the results.</param>
		public virtual void CalculateCrossPRESS(
			IROVector xOfX,
			IMatrix matrixX,
			IMatrix matrixY,
			MultivariateAnalysisOptions plsOptions,
			MultivariateContentMemento plsContent,
			DataTable table
			)
		{
			IROVector crossPRESSMatrix;

			Altaxo.Data.DoubleColumn crosspresscol = new Altaxo.Data.DoubleColumn();

			double meanNumberOfExcludedSpectra = 0;
			if (plsOptions.CrossPRESSCalculation != CrossPRESSCalculationType.None)
			{
				// now a cross validation - this can take a long time for bigger matrices

				MultivariateRegression.GetCrossPRESS(
					xOfX, matrixX, matrixY, plsOptions.MaxNumberOfFactors, GetGroupingStrategy(plsOptions),
					plsContent.SpectralPreprocessing,
					this.CreateNewRegressionObject(),
					out crossPRESSMatrix);

				VectorMath.Copy(crossPRESSMatrix, DataColumnWrapper.ToVector(crosspresscol, crossPRESSMatrix.Length));

				table.DataColumns.Add(crosspresscol, GetCrossPRESSValue_ColumnName(), Altaxo.Data.ColumnKind.V, 4);

				plsContent.MeanNumberOfMeasurementsInCrossPRESSCalculation = plsContent.NumberOfMeasurements - meanNumberOfExcludedSpectra;
			}
			else
			{
				table.DataColumns.Add(crosspresscol, GetCrossPRESSValue_ColumnName(), Altaxo.Data.ColumnKind.V, 4);
			}
		}
Example #10
0
		/// <summary>
		/// Execute an analysis and stores the result in the provided table.
		/// </summary>
		/// <param name="matrixX">The matrix of spectra (horizontal oriented), centered and preprocessed.</param>
		/// <param name="matrixY">The matrix of concentrations, centered.</param>
		/// <param name="plsOptions">Information how to perform the analysis.</param>
		/// <param name="plsContent">A structure to store information about the results of the analysis.</param>
		/// <param name="table">The table where to store the results to.</param>
		/// <param name="press">On return, gives a vector holding the PRESS values of the analysis.</param>
		public virtual void ExecuteAnalysis(
			IMatrix matrixX,
			IMatrix matrixY,
			MultivariateAnalysisOptions plsOptions,
			MultivariateContentMemento plsContent,
			DataTable table,
			out IROVector press
			)
		{
			int numFactors = Math.Min(matrixX.Columns, plsOptions.MaxNumberOfFactors);
			MultivariateRegression regress = this.CreateNewRegressionObject();
			regress.AnalyzeFromPreprocessed(matrixX, matrixY, numFactors);
			plsContent.NumberOfFactors = regress.NumberOfFactors;
			plsContent.CrossValidationType = plsOptions.CrossPRESSCalculation;
			press = regress.GetPRESSFromPreprocessed(matrixX);

			Import(regress.CalibrationModel, table);
		}
Example #11
0
		/// <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;
		}
Example #12
0
		public virtual void StoreFRatioData(
			DataTable table,
			MultivariateContentMemento plsContent)
		{
			DoubleColumn pressColumn = null;
			DoubleColumn crossPRESSColumn = null;
			if (table.DataColumns.Contains(GetPRESSValue_ColumnName()))
				pressColumn = table[GetPRESSValue_ColumnName()] as DoubleColumn;
			if (table.DataColumns.Contains(GetCrossPRESSValue_ColumnName()))
				crossPRESSColumn = table[GetCrossPRESSValue_ColumnName()] as DoubleColumn;

			IROVector press;
			double meanNumberOfIncludedSpectra = plsContent.NumberOfMeasurements;

			if (crossPRESSColumn != null && crossPRESSColumn.Count > 0)
			{
				press = DataColumnWrapper.ToROVector(crossPRESSColumn);
				meanNumberOfIncludedSpectra = plsContent.MeanNumberOfMeasurementsInCrossPRESSCalculation;
			}
			else if (pressColumn != null && pressColumn.Count > 0)
			{
				press = DataColumnWrapper.ToROVector(pressColumn);
				meanNumberOfIncludedSpectra = plsContent.NumberOfMeasurements;
			}
			else
				return;

			// calculate the F-ratio and the F-Probability
			int numberOfSignificantFactors = press.Length;
			double pressMin = double.MaxValue;
			for (int i = 0; i < press.Length; i++)
				pressMin = Math.Min(pressMin, press[i]);
			DoubleColumn fratiocol = new DoubleColumn();
			DoubleColumn fprobcol = new DoubleColumn();
			for (int i = 0; i < press.Length; i++)
			{
				double fratio = press[i] / pressMin;
				double fprob = Calc.Probability.FDistribution.CDF(fratio, meanNumberOfIncludedSpectra, meanNumberOfIncludedSpectra);
				fratiocol[i] = fratio;
				fprobcol[i] = fprob;
				if (fprob < 0.75 && numberOfSignificantFactors > i)
					numberOfSignificantFactors = i;
			}
			plsContent.PreferredNumberOfFactors = numberOfSignificantFactors;
			table.DataColumns.Add(fratiocol, _FRatio_ColumnName, Altaxo.Data.ColumnKind.V, _FRatio_ColumnGroup);
			table.DataColumns.Add(fprobcol, _FProbability_ColumnName, Altaxo.Data.ColumnKind.V, _FProbability_ColumnGroup);
		}
Example #13
0
 public PredictionModelExporter(Altaxo.Data.DataTable table, int numberOfFactors)
 {
     _table           = table;
     _memento         = table.GetTableProperty("Content") as MultivariateContentMemento;
     _numberOfFactors = numberOfFactors;
 }
    public static void QuestPreferredNumberOfFactors(MultivariateContentMemento plsMemo)
    {
      // quest the number of factors to export
      IntegerValueInputController ivictrl = new IntegerValueInputController(1,"Please choose preferred number of factors(>0):");
        

      ivictrl.Validator = new IntegerValueInputController.ZeroOrPositiveIntegerValidator();
      if(!Current.Gui.ShowDialog(ivictrl,"Number of factors",false))
        return;

      plsMemo.PreferredNumberOfFactors = ivictrl.EnteredContents;
    }
Example #15
0
		public void StoreOriginalY(
			DataTable table,
			MultivariateContentMemento plsContent
			)
		{
			IMatrix matrixY = GetOriginalY(plsContent);
			IMultivariateCalibrationModel calib = GetCalibrationModel(table);

			// add a label column for the measurement number
			Altaxo.Data.DoubleColumn measurementLabel = new Altaxo.Data.DoubleColumn();
			for (int i = 0; i < plsContent.MeasurementIndices.Count; i++)
				measurementLabel[i] = plsContent.MeasurementIndices[i];
			table.DataColumns.Add(measurementLabel, _MeasurementLabel_ColumnName, Altaxo.Data.ColumnKind.Label, _MeasurementLabel_ColumnGroup);

			// now add the original Y-Columns
			for (int i = 0; i < matrixY.Columns; i++)
			{
				Altaxo.Data.DoubleColumn col = new Altaxo.Data.DoubleColumn();
				for (int j = 0; j < matrixY.Rows; j++)
					col[j] = matrixY[j, i];
				table.DataColumns.Add(col, _YOriginal_ColumnName + i.ToString(), Altaxo.Data.ColumnKind.X, 5 + i);
			}
		}
        /// <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);
            }
        }
Example #17
0
		public PredictionModelExporter(Altaxo.Data.DataTable table, int numberOfFactors)
		{
			_table = table;
			_memento = table.GetTableProperty("Content") as MultivariateContentMemento;
			_numberOfFactors = numberOfFactors;
		}