/// <summary>
 /// Creates the collection copied from another <see cref="IAscendingIntegerCollection" />.
 /// </summary>
 /// <param name="from"></param>
 public AscendingIntegerCollection(IAscendingIntegerCollection from)
 {
     for (int i = 0; i < from.Count; i++)
     {
         this.Add(from[i]);
     }
 }
Example #2
0
 /// <summary>
 /// Sets the group number of the currently selected columns to <code>nGroup</code>.
 /// </summary>
 /// <param name="datacoll">The data column collection to which to apply this procedure.</param>
 /// <param name="selected">Collection of column indices for which to set the group number.</param>
 /// <param name="nGroup">The group number to set for the selected columns.</param>
 public static void SetColumnGroupNumber(this DataColumnCollection datacoll, IAscendingIntegerCollection selected, int nGroup)
 {
     for (int i = 0; i < selected.Count; i++)
     {
         datacoll.SetColumnGroup(selected[i], nGroup);
     }
 }
Example #3
0
        /// <summary>
        /// Sets the group number of the selected column
        /// </summary>
        /// <param name="dataTable">The data table</param>
        /// <param name="selectedDataColumns">Indices of the currently selected data column of the table</param>
        /// <param name="selectedPropColumns">Indices of the currently selected property columns of the table.</param>
        public static bool ShowSetColumnGroupNumberDialog(this DataTable dataTable,
                                                          IAscendingIntegerCollection selectedDataColumns,
                                                          IAscendingIntegerCollection selectedPropColumns)
        {
            if (selectedDataColumns.Count > 0 || selectedPropColumns.Count > 0)
            {
                int grpNumber = 0;
                if (selectedDataColumns.Count > 0)
                {
                    grpNumber = dataTable.DataColumns.GetColumnGroup(selectedDataColumns[0]);
                }
                else if (selectedPropColumns.Count > 0)
                {
                    grpNumber = dataTable.PropertyColumns.GetColumnGroup(selectedPropColumns[0]);
                }

                var ivictrl = new IntegerValueInputController(grpNumber, "Please enter a group number (>=0):")
                {
                    Validator = new IntegerValueInputController.ZeroOrPositiveIntegerValidator()
                };
                if (Current.Gui.ShowDialog(ivictrl, "Set group number", false))
                {
                    SetColumnGroupNumber(dataTable, selectedDataColumns, selectedPropColumns, ivictrl.EnteredContents);
                    return(true);
                }
            }
            return(false);
        }
Example #4
0
 public void Dispose()
 {
     _col             = null;
     _srctable        = null;
     _selectedColumns = null;
     _selectedRows    = null;
 }
        public XYZMeshedColumnPlotData(Altaxo.Data.DataColumnCollection coll, IAscendingIntegerCollection selected)
        {
            m_XColumn = new Altaxo.Data.IndexerColumn();
            m_YColumn = new Altaxo.Data.IndexerColumn();

            int len = selected == null ? coll.ColumnCount : selected.Count;

            m_DataColumns = new Altaxo.Data.IReadableColumn[len];
            for (int i = 0; i < len; i++)
            {
                int idx = null == selected ? i : selected[i];
                m_DataColumns[i] = coll[idx];

                // set the event chain
                if (m_DataColumns[i] is Altaxo.Data.DataColumn)
                {
                    ((Altaxo.Data.DataColumn)m_DataColumns[i]).Changed += new EventHandler(EhColumnDataChangedEventHandler);
                }
            }


            this.SetXBoundsFromTemplate(new FiniteNumericalBoundaries());
            this.SetYBoundsFromTemplate(new FiniteNumericalBoundaries());
            this.SetVBoundsFromTemplate(new FiniteNumericalBoundaries());
        }
Example #6
0
        /// <summary>
        /// Determines which of the rows of a set of columns is truly numeric, i.e. all columns in this row contains a value, which is not double.NaN.
        /// </summary>
        /// <param name="table">Array of numeric columns.</param>
        /// <param name="selectedCols">The indizes of the columns in question into the collection.</param>
        /// <param name="selectedRowIndices">The selected data row indices.</param>
        /// <param name="rowCount">The minimum row count of all the selected columns.</param>
        /// <returns>A boolean array. If an element of the array is true at a given index, that row contains valid numeric values in all columns.</returns>
        public static bool[] GetValidNumericRows(IReadableColumn[] table, IAscendingIntegerCollection selectedCols, IEnumerable <int> selectedRowIndices, int rowCount)
        {
            // determine the number of valid rows
            bool[] rowValid = new bool[rowCount];

            foreach (int i in selectedRowIndices)
            {
                rowValid[i] = true;
            }

            for (int i = 0; i < selectedCols.Count; i++)
            {
                var col = table[selectedCols[i]];
                if (col is null)
                {
                    for (int j = 0; j < rowCount; j++)
                    {
                        rowValid[j] = false;
                    }
                }
                else
                {
                    for (int j = 0; j < rowCount; j++)
                    {
                        if (double.IsNaN(col[j]))
                        {
                            rowValid[j] = false;
                        }
                    }
                }
            }
            return(rowValid);
        }
Example #7
0
		public static List<IGPlotItem> CreatePlotItems(DataTable table, IAscendingIntegerCollection selectedColumns, G3DPlotStyleCollection templatePlotStyle)
		{
			var selColumns = new List<DataColumn>(selectedColumns.Count);
			foreach (int i in selectedColumns)
				selColumns.Add(table[i]);

			return CreatePlotItems(selColumns, templatePlotStyle, table.GetPropertyContext());
		}
Example #8
0
        public static LinearFitBySvd Regress(MultivariateLinearFitParameters parameters, out string[] paramNames)
        {
            DataColumnCollection        table        = parameters.Table;
            IAscendingIntegerCollection selectedCols = parameters.SelectedDataColumns;
            var selectedColsWODependent = new AscendingIntegerCollection(selectedCols);

            selectedColsWODependent.RemoveAt(parameters.DependentColumnIndexIntoSelection);

            IAscendingIntegerCollection validRows = DataTableWrapper.GetCollectionOfValidNumericRows(parameters.Table, selectedCols);

            parameters.SelectedDataRows = validRows;

            IROMatrix <double> xbase;

            if (parameters.IncludeIntercept)
            {
                xbase = DataTableWrapper.ToROColumnMatrixWithIntercept(parameters.Table, selectedColsWODependent, validRows);
            }
            else
            {
                xbase = DataTableWrapper.ToROColumnMatrix(parameters.Table, selectedColsWODependent, validRows);
            }

            paramNames = new string[xbase.ColumnCount];
            if (parameters.IncludeIntercept)
            {
                paramNames[0] = "Intercept";
                for (int i = 0; i < selectedColsWODependent.Count; i++)
                {
                    paramNames[i + 1] = table[selectedColsWODependent[i]].Name;
                }
            }
            else
            {
                for (int i = 0; i < selectedColsWODependent.Count; i++)
                {
                    paramNames[i] = table[selectedColsWODependent[i]].Name;
                }
            }

            // Fill the y and the error array
            double[] yarr = new double[validRows.Count];
            double[] earr = new double[validRows.Count];

            var ycol = (Altaxo.Data.INumericColumn)table[selectedCols[parameters.DependentColumnIndexIntoSelection]];

            for (int i = 0; i < validRows.Count; i++)
            {
                yarr[i] = ycol[validRows[i]];
                earr[i] = 1;
            }

            var fit =
                new LinearFitBySvd(
                    xbase, yarr, earr, xbase.RowCount, xbase.ColumnCount, 1E-5);

            return(fit);
        }
Example #9
0
        /// <summary>
        /// Remove the selected data columns <b>and the corresponding property rows</b>.
        /// </summary>
        /// <param name="selectedColumns">A collection of the indizes to the columns that have to be removed.</param>
        public virtual void RemoveColumns(IAscendingIntegerCollection selectedColumns)
        {
            Suspend();

            _dataColumns.RemoveColumns(selectedColumns);  // remove the columns from the collection
            _propertyColumns.RemoveRows(selectedColumns); // remove also the corresponding rows from the Properties

            Resume();
        }
        /// <summary>
        /// Adds all values from another <see cref="IAscendingIntegerCollection" />.
        /// </summary>
        /// <param name="from">The source collection.</param>
        public void Add(IAscendingIntegerCollection from)
        {
            int end = from.Count;

            for (int i = 0; i < end; i++)
            {
                Add(from[i]);
            }
        }
Example #11
0
 public static void SetColumnGroupNumber(
     this DataTable dataTable,
     IAscendingIntegerCollection selectedDataColumns,
     IAscendingIntegerCollection selectedPropColumns,
     int nGroup)
 {
     SetColumnGroupNumber(dataTable.DataColumns, selectedDataColumns, nGroup);
     SetColumnGroupNumber(dataTable.PropCols, selectedPropColumns, nGroup);
 }
Example #12
0
		/// <summary>
		/// Calculates statistics of selected columns. Returns a new table where the statistical data will be written to.
		/// </summary>
		/// <param name="srctable">Source table.</param>
		/// <param name="selectedColumns">Selected data columns in the source table.</param>
		/// <param name="selectedRows">Selected rows in the source table.</param>
		public static DataTable DoStatisticsOnColumns(
			this DataColumnCollection srctable,
			IAscendingIntegerCollection selectedColumns,
			IAscendingIntegerCollection selectedRows
			)
		{
			var table = CreateStatisticalTable(srctable, selectedColumns);
			DoStatisticsOnColumns(srctable, selectedColumns, selectedRows, table.DataColumns);
			return table;
		}
Example #13
0
 public XYZMeshedColumnPlotData(DataTable table, IAscendingIntegerCollection selectedDataRows, IAscendingIntegerCollection selectedDataColumns, IAscendingIntegerCollection selectedPropertyColumns)
 {
     _matrixProxy = new DataTableMatrixProxy(table, selectedDataRows, selectedDataColumns, selectedPropertyColumns)
     {
         ParentObject = this
     };
     SetXBoundsFromTemplate(new FiniteNumericalBoundaries());
     SetYBoundsFromTemplate(new FiniteNumericalBoundaries());
     SetVBoundsFromTemplate(new FiniteNumericalBoundaries());
 }
Example #14
0
        /// <summary>
        /// Calculates the jacobian values, i.e. the derivatives of the fitting values with respect to the parameters.
        /// </summary>
        /// <param name="parameter">The parameter used to calculate the values.</param>
        /// <param name="outputValues">You must provide an array to hold the calculated values. Size of the array must be
        /// at least <see cref="NumberOfData" />*<see cref="FitElement.NumberOfParameters" />.</param>
        /// <param name="adata">Currently ignored.</param>
        /// <remarks>The values of the fit elements are stored in the order from element_0 to element_n*m. If there is more
        /// than one used dependent variable per fit element, the output values are stored in interleaved order. The derivatives
        /// on one fitting value  are stored in successive order.
        /// </remarks>
        public void EvaluateFitJacobian(double[] parameter, double[] outputValues, object adata)
        {
            outputValues.Initialize(); // make sure every element contains zero

            int outputValuesPointer = 0;

            for (int ele = 0; ele < _cachedFitElementInfo.Length; ele++)
            {
                CachedFitElementInfo info   = _cachedFitElementInfo[ele];
                FitElement           fitEle = _fitEnsemble[ele];


                // make sure, that the dimension of the DYs is ok
                if (info.DYs == null || info.DYs.Length != fitEle.NumberOfDependentVariables || info.DYs[0].Length != fitEle.NumberOfParameters)
                {
                    info.DYs = LinearAlgebra.JaggedArrayMath.GetMatrixArray(fitEle.NumberOfDependentVariables, fitEle.NumberOfParameters);
                }

                // copy of the parameter to the temporary array
                for (int i = 0; i < info.Parameters.Length; i++)
                {
                    int idx = info.ParameterMapping[i];
                    info.Parameters[i] = idx >= 0 ? parameter[idx] : _constantParameters[-1 - idx];
                }


                IAscendingIntegerCollection validRows = info.ValidRows;
                int numValidRows = validRows.Count;
                // Evaluate the function for all points
                for (int i = 0; i < numValidRows; ++i)
                {
                    for (int k = info.Xs.Length - 1; k >= 0; k--)
                    {
                        info.Xs[k] = fitEle.IndependentVariables(k)[validRows[i]];
                    }

                    ((IFitFunctionWithGradient)fitEle.FitFunction).EvaluateGradient(info.Xs, info.Parameters, info.DYs);

                    // copy the evaluation result to the output array (interleaved)
                    for (int k = 0; k < info.DependentVariablesInUse.Length; ++k)
                    {
                        for (int l = 0; l < info.Parameters.Length; ++l)
                        {
                            int idx = info.ParameterMapping[l];
                            if (idx >= 0)
                            {
                                outputValues[outputValuesPointer + idx] += info.DYs[info.DependentVariablesInUse[k]][l];
                            }
                        }
                        outputValuesPointer += parameter.Length; // increase output pointer only by the varying (!) number of parameters
                    }
                }
            }
        }
Example #15
0
        /// <summary>
        /// Creates the controller for the Galactic SPC file export dialog.
        /// </summary>
        /// <param name="table">The data table which is about to be exported.</param>
        /// <param name="selectedRows">The selected rows of the data table.</param>
        /// <param name="selectedColumns">The selected columns of the data table.</param>
        public ExportGalacticSpcFileDialogController(
            Altaxo.Data.DataTable table,
            IAscendingIntegerCollection selectedRows,
            IAscendingIntegerCollection selectedColumns)
        {
            m_Table           = table;
            m_SelectedRows    = selectedRows;
            m_SelectedColumns = selectedColumns;

            InitializeElements();
        }
Example #16
0
        /// <summary>
        /// Calculates statistics of selected columns. Returns a new table where the statistical data will be written to.
        /// </summary>
        /// <param name="srctable">Source table.</param>
        /// <param name="selectedColumns">Selected data columns in the source table.</param>
        /// <param name="selectedRows">Selected rows in the source table.</param>
        public static DataTable DoStatisticsOnColumns(
            this DataColumnCollection srctable,
            IAscendingIntegerCollection selectedColumns,
            IAscendingIntegerCollection selectedRows
            )
        {
            var table = CreateStatisticalTable(srctable, selectedColumns);

            DoStatisticsOnColumns(srctable, selectedColumns, selectedRows, table.DataColumns);
            return(table);
        }
Example #17
0
        /// <summary>
        /// Plots selected data columns of a table.
        /// </summary>
        /// <param name="table">The source table.</param>
        /// <param name="selectedColumns">The data columns of the table that should be plotted.</param>
        /// <param name="graph">The graph document to plot into.</param>
        /// <param name="templatePlotStyle">The plot style which is the template for all plot items.</param>
        /// <param name="groupStyles">The group styles for the newly built plot item collection.</param>
        public static Altaxo.Gui.Graph.Gdi.Viewing.IGraphController Plot(
            DataTable table,
            IAscendingIntegerCollection selectedColumns,
            Graph.Gdi.GraphDocument graph,
            G2DPlotStyleCollection templatePlotStyle,
            PlotGroupStyleCollection groupStyles)
        {
            List <IGPlotItem> pilist = CreatePlotItems(table, selectedColumns, templatePlotStyle);
            // now create a new Graph with this plot associations
            var gc      = Current.ProjectService.CreateNewGraph(graph);
            var xylayer = gc.Doc.RootLayer.Layers.OfType <Altaxo.Graph.Gdi.XYPlotLayer>().First();

            // Set x and y axes according to the first plot item in the list
            if (pilist.Count > 0 && (pilist[0] is XYColumnPlotItem))
            {
                var firstitem = (XYColumnPlotItem)pilist[0];
                if (firstitem.Data.XColumn is TextColumn)
                {
                    xylayer.Scales[0] = new TextScale();
                }
                else if (firstitem.Data.XColumn is DateTimeColumn)
                {
                    xylayer.Scales[0] = new DateTimeScale();
                }

                if (firstitem.Data.YColumn is TextColumn)
                {
                    xylayer.Scales[1] = new TextScale();
                }
                else if (firstitem.Data.YColumn is DateTimeColumn)
                {
                    xylayer.Scales[1] = new DateTimeScale();
                }
            }

            var newPlotGroup = new PlotItemCollection(xylayer.PlotItems);

            foreach (IGPlotItem pi in pilist)
            {
                newPlotGroup.Add(pi);
            }
            if (groupStyles != null)
            {
                newPlotGroup.GroupStyles = groupStyles;
            }
            else
            {
                newPlotGroup.CollectStyles(newPlotGroup.GroupStyles);
            }

            xylayer.PlotItems.Add(newPlotGroup);

            return(gc);
        }
Example #18
0
        /// <summary>
        /// Sets the data columns from an enumeration of data column proxies.
        /// </summary>
        /// <param name="dataRows">The enumeration of data rows.</param>
        public void SetDataRows(IAscendingIntegerCollection dataRows)
        {
            _participatingDataRows.Clear();

            foreach (var range in dataRows.RangesAscending)
            {
                _participatingDataRows.AddRange(range.Start, range.Count);
            }

            _isDirty = true;
        }
Example #19
0
        public static List <IGPlotItem> CreatePlotItems(DataTable table, IAscendingIntegerCollection selectedColumns, G3DPlotStyleCollection templatePlotStyle)
        {
            var selColumns = new List <DataColumn>(selectedColumns.Count);

            foreach (int i in selectedColumns)
            {
                selColumns.Add(table[i]);
            }

            return(CreatePlotItems(selColumns, templatePlotStyle, table.GetPropertyContext()));
        }
Example #20
0
 /// <summary>
 /// Constructor. Besides the table, the current selections must be provided. Only the areas that corresponds to the selections are
 /// serialized. The serialization process has to occur immediately after this constructor, because only a reference
 /// to the table is hold by this object.
 /// </summary>
 /// <param name="table">The table to serialize.</param>
 /// <param name="selectedDataColumns">The selected data columns.</param>
 /// <param name="selectedDataRows">The selected data rows.</param>
 /// <param name="selectedPropertyColumns">The selected property columns.</param>
 /// <param name="selectedPropertyRows">The selected property rows.</param>
 public ClipboardMemento(DataTable table, IAscendingIntegerCollection selectedDataColumns,
                         IAscendingIntegerCollection selectedDataRows,
                         IAscendingIntegerCollection selectedPropertyColumns,
                         IAscendingIntegerCollection selectedPropertyRows
                         )
 {
     this._table = table;
     this._selectedDataColumns     = selectedDataColumns;
     this._selectedDataRows        = selectedDataRows;
     this._selectedPropertyColumns = selectedPropertyColumns;
     this._selectedPropertyRows    = selectedPropertyRows;
 }
 /// <summary>
 /// Gets the indices of the data rows of the table that contribute to the matrix.
 /// </summary>
 /// <returns></returns>
 public IAscendingIntegerCollection GetParticipatingDataRows()
 {
     if (null == _participatingDataColumns)
     {
         GetParticipatingDataColumns();
     }
     if (null == _participatingDataRows)
     {
         _participatingDataRows = GetParticipatingDataRows(_sourceTable, _selectedRows, _participatingDataColumns);
     }
     return(_participatingDataRows);
 }
Example #22
0
        /// <summary>
        /// Shows the dialog in which the user can select options for the 2D Fourier transformation, and then executes the Fourier transformation
        /// The result is stored in a newly created data table in the same folder as the source data table.
        /// </summary>
        /// <param name="table">The table containing the data to transform.</param>
        /// <param name="selectedDataRows">The selected data rows of the table. (A value of <c>null</c> can be provided here).</param>
        /// <param name="selectedDataColumns">The selected data columns of the table. (A value of <c>null</c> can be provided here).</param>
        /// <param name="selectedPropertyColumns">The selected property columns of the table. (A value of <c>null</c> can be provided here).</param>
        public static void ShowRealFourierTransformation2DDialog(DataTable table, IAscendingIntegerCollection selectedDataRows, IAscendingIntegerCollection selectedDataColumns, IAscendingIntegerCollection selectedPropertyColumns)
        {
            DataTableMatrixProxy proxy = null;
            RealFourierTransformation2DOptions options = null;

            try
            {
                proxy = new DataTableMatrixProxy(table, selectedDataRows, selectedDataColumns, selectedPropertyColumns);

                options = null != _lastUsedOptions ? (RealFourierTransformation2DOptions)_lastUsedOptions.Clone() : new RealFourierTransformation2DOptions();
                proxy.TryGetRowHeaderIncrement(out var rowIncrementValue, out var rowIncrementMessage);
                proxy.TryGetColumnHeaderIncrement(out var columnIncrementValue, out var columnIncrementMessage);

                options.IsUserDefinedRowIncrementValue = false;
                options.RowIncrementValue   = rowIncrementValue;
                options.RowIncrementMessage = rowIncrementMessage;

                options.IsUserDefinedColumnIncrementValue = false;
                options.ColumnIncrementValue   = columnIncrementValue;
                options.ColumnIncrementMessage = columnIncrementMessage;
            }
            catch (Exception ex)
            {
                Current.Gui.ErrorMessageBox(string.Format("{0}\r\nDetails:\r\n{1}", ex.Message, ex.ToString()), "Error in preparation of Fourier transformation");
                return;
            }

            if (!Current.Gui.ShowDialog(ref options, "Choose fourier transform options", false))
            {
                return;
            }

            _lastUsedOptions = options;

            try
            {
                var resultTable = new DataTable {
                    Name = string.Format("{0}Fourier{1} of {2}", table.Folder, options.OutputKind, table.ShortName)
                };
                ExecuteFouriertransformation2D(proxy, options, resultTable);

                // Create a DataSource
                var dataSource = new FourierTransformation2DDataSource(proxy, options, new Altaxo.Data.DataSourceImportOptions());
                resultTable.DataSource = dataSource;

                Current.ProjectService.OpenOrCreateWorksheetForTable(resultTable);
            }
            catch (Exception ex)
            {
                Current.Gui.ErrorMessageBox(string.Format("{0}\r\nDetails:\r\n{1}", ex.Message, ex.ToString()), "Error in execution of Fourier transformation");
                return;
            }
        }
Example #23
0
        /// <summary>
        /// Creates a table for statistics on columns. Property columns are included in the statistical table.
        /// </summary>
        /// <param name="srcTable"></param>
        /// <param name="selectedColumns"></param>
        /// <returns></returns>
        private static DataTable CreateStatisticalTable(DataTable srcTable, IAscendingIntegerCollection selectedColumns)
        {
            var result = new DataTable
            {
                Name = Altaxo.Main.ProjectFolder.PrependToName(srcTable.Name, "Statistics of ")
            };

            result.DataColumns.Add(new TextColumn(), DefaultColumnNameColumnName, ColumnKind.X, 0);
            AddSourcePropertyColumns(srcTable, selectedColumns, result);
            AddStatisticColumns(result);
            return(result);
        }
Example #24
0
        /// <summary>
        /// Plots selected data columns of a table.
        /// </summary>
        /// <param name="table">The source table.</param>
        /// <param name="selectedColumns">The data columns of the table that should be plotted.</param>
        /// <param name="graph">The graph document to plot into.</param>
        /// <param name="templatePlotStyle">The plot style which is the template for all plot items.</param>
        /// <param name="groupStyles">The group styles for the newly built plot item collection.</param>
        public static IGraphController Plot(DataTable table,
                                            IAscendingIntegerCollection selectedColumns,
                                            Graph.Gdi.GraphDocument graph,
                                            G2DPlotStyleCollection templatePlotStyle,
                                            PlotGroupStyleCollection groupStyles)
        {
            List <IGPlotItem> pilist = CreatePlotItems(table, selectedColumns, templatePlotStyle);

            // now create a new Graph with this plot associations
            Altaxo.Graph.GUI.IGraphController gc = Current.ProjectService.CreateNewGraph(graph);
            // Set x and y axes according to the first plot item in the list
            if (pilist.Count > 0 && (pilist[0] is XYColumnPlotItem))
            {
                XYColumnPlotItem firstitem = (XYColumnPlotItem)pilist[0];
                if (firstitem.Data.XColumn is TextColumn)
                {
                    gc.Doc.Layers[0].LinkedScales.SetScale(0, new Graph.Scales.TextScale());
                }
                else if (firstitem.Data.XColumn is DateTimeColumn)
                {
                    gc.Doc.Layers[0].LinkedScales.SetScale(0, new Graph.Scales.DateTimeScale());
                }

                if (firstitem.Data.YColumn is TextColumn)
                {
                    gc.Doc.Layers[0].LinkedScales.SetScale(1, new Graph.Scales.TextScale());
                }
                else if (firstitem.Data.YColumn is DateTimeColumn)
                {
                    gc.Doc.Layers[0].LinkedScales.SetScale(1, new Graph.Scales.DateTimeScale());
                }
            }


            PlotItemCollection newPlotGroup = new PlotItemCollection(gc.Doc.Layers[0].PlotItems);

            foreach (IGPlotItem pi in pilist)
            {
                newPlotGroup.Add(pi);
            }
            if (groupStyles != null)
            {
                newPlotGroup.GroupStyles = groupStyles;
            }
            else
            {
                newPlotGroup.CollectStyles(newPlotGroup.GroupStyles);
            }

            gc.Doc.Layers[0].PlotItems.Add(newPlotGroup);

            return(gc);
        }
        public static void ShowActionDialog(this DataTable srcTable, IAscendingIntegerCollection selectedDataColumns)
        {
            DataTableMultipleColumnProxy proxy   = null;
            ConvertXYVToMatrixOptions    options = null;

            try
            {
                proxy = new DataTableMultipleColumnProxy(ConvertXYVToMatrixDataAndOptions.ColumnV, srcTable, null, selectedDataColumns);
                proxy.EnsureExistenceOfIdentifier(ConvertXYVToMatrixDataAndOptions.ColumnX, 1);
                proxy.EnsureExistenceOfIdentifier(ConvertXYVToMatrixDataAndOptions.ColumnY, 1);

                options = new ConvertXYVToMatrixOptions();
            }
            catch (Exception ex)
            {
                Current.Gui.ErrorMessageBox(string.Format("{0}\r\nDetails:\r\n{1}", ex.Message, ex.ToString()), "Error in preparation of 'Convert X-Y-V values to matrix'");
                return;
            }

            var dataAndOptions = new ConvertXYVToMatrixDataAndOptions(proxy, options);

            // in order to show the column names etc in the dialog, it is neccessary to set the source
            if (true == Current.Gui.ShowDialog(ref dataAndOptions, "Choose options", false))
            {
                var destTable = new DataTable();
                proxy   = dataAndOptions.Data;
                options = dataAndOptions.Options;

                string error = null;
                try
                {
                    error = ConvertXYVToMatrix(dataAndOptions.Data, dataAndOptions.Options, destTable);
                }
                catch (Exception ex)
                {
                    error = ex.ToString();
                }
                if (null != error)
                {
                    Current.Gui.ErrorMessageBox(error);
                }

                destTable.Name = srcTable.Name + "_Decomposed";

                // Create a DataSource
                var dataSource = new ConvertXYVToMatrixDataSource(proxy, options, new Altaxo.Data.DataSourceImportOptions());
                destTable.DataSource = dataSource;

                Current.Project.DataTableCollection.Add(destTable);
                Current.IProjectService.ShowDocumentView(destTable);
            }
        }
Example #26
0
		/// <summary>
		/// Shows the dialog in which the user can select options for the 2D Fourier transformation, and then executes the Fourier transformation
		/// The result is stored in a newly created data table in the same folder as the source data table.
		/// </summary>
		/// <param name="table">The table containing the data to transform.</param>
		/// <param name="selectedDataRows">The selected data rows of the table. (A value of <c>null</c> can be provided here).</param>
		/// <param name="selectedDataColumns">The selected data columns of the table. (A value of <c>null</c> can be provided here).</param>
		/// <param name="selectedPropertyColumns">The selected property columns of the table. (A value of <c>null</c> can be provided here).</param>
		public static void ShowRealFourierTransformation2DDialog(DataTable table, IAscendingIntegerCollection selectedDataRows, IAscendingIntegerCollection selectedDataColumns, IAscendingIntegerCollection selectedPropertyColumns)
		{
			DataTableMatrixProxy proxy = null;
			RealFourierTransformation2DOptions options = null;

			try
			{
				proxy = new DataTableMatrixProxy(table, selectedDataRows, selectedDataColumns, selectedPropertyColumns);

				options = null != _lastUsedOptions ? (RealFourierTransformation2DOptions)_lastUsedOptions.Clone() : new RealFourierTransformation2DOptions();

				double rowIncrementValue; string rowIncrementMessage;
				proxy.TryGetRowHeaderIncrement(out rowIncrementValue, out rowIncrementMessage);
				double columnIncrementValue; string columnIncrementMessage;
				proxy.TryGetColumnHeaderIncrement(out columnIncrementValue, out columnIncrementMessage);

				options.IsUserDefinedRowIncrementValue = false;
				options.RowIncrementValue = rowIncrementValue;
				options.RowIncrementMessage = rowIncrementMessage;

				options.IsUserDefinedColumnIncrementValue = false;
				options.ColumnIncrementValue = columnIncrementValue;
				options.ColumnIncrementMessage = columnIncrementMessage;
			}
			catch (Exception ex)
			{
				Current.Gui.ErrorMessageBox(string.Format("{0}\r\nDetails:\r\n{1}", ex.Message, ex.ToString()), "Error in preparation of Fourier transformation");
				return;
			}

			if (!Current.Gui.ShowDialog(ref options, "Choose fourier transform options", false))
				return;

			_lastUsedOptions = options;

			try
			{
				var resultTable = new DataTable { Name = string.Format("{0}Fourier{1} of {2}", table.Folder, options.OutputKind, table.ShortName) };
				ExecuteFouriertransformation2D(proxy, options, resultTable);

				// Create a DataSource
				Altaxo.Worksheet.Commands.Analysis.FourierTransformation2DDataSource dataSource = new FourierTransformation2DDataSource(proxy, options, new Altaxo.Data.DataSourceImportOptions());
				resultTable.DataSource = dataSource;

				Current.ProjectService.OpenOrCreateWorksheetForTable(resultTable);
			}
			catch (Exception ex)
			{
				Current.Gui.ErrorMessageBox(string.Format("{0}\r\nDetails:\r\n{1}", ex.Message, ex.ToString()), "Error in execution of Fourier transformation");
				return;
			}
		}
Example #27
0
        /// <summary>
        /// Plots selected data columns of a table.
        /// </summary>
        /// <param name="table">The source table.</param>
        /// <param name="selectedColumns">The data columns of the table that should be plotted.</param>
        /// <param name="templatePlotStyle">The plot style which is the template for all plot items.</param>
        /// <param name="groupStyles">The group styles for the newly built plot item collection.</param>
        public static IGraphController Plot(DataTable table,
                                            IAscendingIntegerCollection selectedColumns,
                                            G2DPlotStyleCollection templatePlotStyle,
                                            PlotGroupStyleCollection groupStyles)
        {
            Altaxo.Graph.Gdi.GraphDocument graph = new Altaxo.Graph.Gdi.GraphDocument();
            Altaxo.Graph.Gdi.XYPlotLayer   layer = new Altaxo.Graph.Gdi.XYPlotLayer(graph.DefaultLayerPosition, graph.DefaultLayerSize);
            layer.CreateDefaultAxes();
            graph.Layers.Add(layer);
            Current.Project.GraphDocumentCollection.Add(graph);

            return(Plot(table, selectedColumns, graph, templatePlotStyle, groupStyles));
        }
Example #28
0
        /// <summary>
        /// Copies selected rows into newly created property columns and deletes the rows afterwards.
        /// </summary>
        /// <param name="table">Table which contains the data rows to change into property columns.</param>
        /// <param name="selectedDataRows">Indices of the rows to change.</param>
        /// <param name="selectedDataColumns">Indices of those columns which should be copies into the property column cells.</param>
        public static void ChangeRowsToPropertyColumns(this DataTable table, IAscendingIntegerCollection selectedDataRows, IAscendingIntegerCollection selectedDataColumns)
        {
            // first copy the rows into property columns
            foreach (var rowIdx in selectedDataRows)
            {
                var propColType = GetTypeOfColumnForRowReplacement(table.DataColumns, rowIdx, selectedDataColumns);
                var propCol     = (DataColumn)Activator.CreateInstance(propColType);
                CopyRowToDataColumn(table.DataColumns, rowIdx, selectedDataColumns, propCol);
                table.PropCols.Add(propCol);
            }

            // now delete the rows
            table.DataColumns.RemoveRows(selectedDataRows);
        }
        /// <summary>
        /// Exports to a single SPC spectrum from a single table column.
        /// </summary>
        /// <param name="filename">The name of the file where to export to.</param>
        /// <param name="table">The table from which to export.</param>
        /// <param name="columnnumber">The number of the table column that contains the data to export.</param>
        /// <param name="xcolumn">The x column that contains the x data.</param>
        /// <param name="selectedRows">The rows that where selected in the table, i.e. the rows which are exported. If this parameter is null
        /// or no rows are selected, then all data of a column will be exported.</param>
        /// <returns>Null if export was successfull, error description otherwise.</returns>
        public static string FromColumn(
            string filename,
            Altaxo.Data.DataTable table,
            int columnnumber,
            Altaxo.Data.INumericColumn xcolumn,
            IAscendingIntegerCollection selectedRows)
        {
            if (!(table.DataColumns[columnnumber] is Altaxo.Data.INumericColumn))
            {
                return(string.Format("Table column[{0}] ({1}) is not a numeric column!", columnnumber, table.DataColumns[columnnumber].FullName));
            }



            // test that all x and y cells have numeric values
            bool bUseSel     = null != selectedRows && selectedRows.Count > 0;
            int  spectrumlen = (bUseSel)? selectedRows.Count : table.DataColumns[columnnumber].Count;

            int i, j;

            for (j = 0; j < spectrumlen; j++)
            {
                i = bUseSel ? selectedRows[j] : j;

                if (xcolumn[i] == Double.NaN)
                {
                    return(string.Format("X column at index {i} has no numeric value!", i));
                }

                if (((Altaxo.Data.INumericColumn)table.DataColumns[columnnumber])[i] == Double.NaN)
                {
                    return(string.Format("Table cell [{0},{1}] (column {2}) has no numeric value!", columnnumber, i, table.DataColumns[columnnumber].FullName));
                }
            }


            // this first test was successfull, so start exporting now

            double[] xvalues = new double[spectrumlen];
            double[] yvalues = new double[spectrumlen];

            for (j = 0; j < spectrumlen; j++)
            {
                i          = bUseSel ? selectedRows[j] : j;
                xvalues[j] = xcolumn[i];
                yvalues[j] = ((Altaxo.Data.INumericColumn)table.DataColumns[columnnumber])[i];
            }
            return(FromArrays(xvalues, yvalues, filename));
        }
Example #30
0
        /// <summary>
        /// Adds to the destination table selected columns from another table. Additionally, the properties of those columns will be added to the destination table.
        /// </summary>
        /// <param name="destinationTable">Table where the columns should be added to.</param>
        /// <param name="tableToAddFrom">Source table.</param>
        /// <param name="selectedColumns">Indexes of the columns of the source table that should be added to the destination table.</param>
        public static void AddDataColumnsWithPropertiesFrom(this DataTable destinationTable, DataTable tableToAddFrom, IAscendingIntegerCollection selectedColumns)
        {
            IAscendingIntegerCollection mergedPropColsIndices = destinationTable.PropCols.MergeColumnTypesFrom(tableToAddFrom.PropCols);

            for (int i = 0; i < selectedColumns.Count; i++)
            {
                int sourceIdx      = selectedColumns[i];
                int destinationIdx = destinationTable.DataColumns.AppendCopiedColumnFrom(tableToAddFrom.DataColumns, sourceIdx, true, true);

                for (int j = 0; j < mergedPropColsIndices.Count; j++)
                {
                    destinationTable.PropCols[mergedPropColsIndices[j]][destinationIdx] = tableToAddFrom.PropCols[j][sourceIdx];
                }
            }
        }
        /// <summary>
        /// Gets the indices of the data columns of the data table that contribute to the matrix.
        /// </summary>
        /// <returns>Indices of the participating data columns of the data table.</returns>
        /// <exception cref="InvalidDimensionMatrixException">No columns found that can be used for the matrix. Thus number of columns of the matrix would be zero.</exception>
        public IAscendingIntegerCollection GetParticipatingDataColumns()
        {
            if (null == _participatingDataColumns)
            {
                _participatingDataColumns = GetParticipatingDataColumns(_sourceTable, _selectedColumns);

                if (0 == _participatingDataColumns.Count)
                {
                    throw new InvalidDimensionMatrixException("No columns found that can be used for the matrix. Thus number of columns of the matrix would be zero.");
                }

                _dataColumnsGroupNumber = _sourceTable.DataColumns.GetColumnGroup(_participatingDataColumns[0]);
            }

            return(_participatingDataColumns);
        }
Example #32
0
        /// <summary>
        /// Calculates the fitting values.
        /// </summary>
        /// <param name="parameter">The parameter used to calculate the values.</param>
        /// <param name="outputValues">You must provide an array to hold the calculated values. Size of the array must be
        /// at least <see cref="NumberOfData" />.</param>
        /// <param name="calculateUnusedDependentVariablesAlso">If <c>true</c>, the unused dependent variables are also calculated (and plotted).</param>
        /// <remarks>The values of the fit elements are stored in the order from element_0 to element_n. If there is more
        /// than one used dependent variable per fit element, the output values are stored in interleaved order.
        /// </remarks>
        public void EvaluateFitValues(double[] parameter, double[] outputValues, bool calculateUnusedDependentVariablesAlso)
        {
            int outputValuesPointer = 0;

            for (int ele = 0; ele < _cachedFitElementInfo.Length; ele++)
            {
                CachedFitElementInfo info   = _cachedFitElementInfo[ele];
                FitElement           fitEle = _fitEnsemble[ele];

                // copy of the parameter to the temporary array
                for (int i = 0; i < info.Parameters.Length; i++)
                {
                    int idx = info.ParameterMapping[i];
                    info.Parameters[i] = idx >= 0 ? parameter[idx] : _constantParameters[-1 - idx];
                }

                IAscendingIntegerCollection validRows = info.ValidRows;
                int numValidRows = validRows.Count;
                // Evaluate the function for all points
                for (int i = 0; i < numValidRows; ++i)
                {
                    for (int k = info.Xs.Length - 1; k >= 0; k--)
                    {
                        info.Xs[k] = fitEle.IndependentVariables(k)[validRows[i]];
                    }

                    fitEle.FitFunction.Evaluate(info.Xs, info.Parameters, info.Ys);

                    if (calculateUnusedDependentVariablesAlso)
                    {
                        // copy the evaluation result to the output array (interleaved)
                        for (int k = 0; k < fitEle.NumberOfDependentVariables; ++k)
                        {
                            outputValues[outputValuesPointer++] = info.Ys[k];
                        }
                    }
                    else
                    {
                        // copy the evaluation result to the output array (interleaved)
                        for (int k = 0; k < info.DependentVariablesInUse.Length; ++k)
                        {
                            outputValues[outputValuesPointer++] = info.Ys[info.DependentVariablesInUse[k]];
                        }
                    }
                }
            }
        }
Example #33
0
            public NumericTableRegionEnumerator(DataTable srctable,
                                                IAscendingIntegerCollection selectedColumns,
                                                IAscendingIntegerCollection selectedRows)
            {
                _srctable        = srctable;
                _selectedColumns = selectedColumns;
                _selectedRows    = selectedRows;

                if (selectedColumns.Count == 0)
                {
                    _selectedColumns = Altaxo.Collections.ContiguousIntegerRange.FromStartAndCount(0, srctable.DataColumns.ColumnCount);
                }
                if (selectedRows.Count == 0)
                {
                    _selectedRows = Altaxo.Collections.ContiguousIntegerRange.FromStartAndCount(0, srctable.DataColumns.RowCount);
                }
            }
Example #34
0
		/// <summary>
		/// Merges two tables by fractional index.
		/// </summary>
		/// <param name="destinationTable">Table to merge into.</param>
		/// <param name="fractionalIndex">Array of fractional indices. Each item points into the slaveTable to the value that should be included in the master column at the item's index.</param>
		/// <param name="slaveTable">The table providing the data for merging into the master table.</param>
		/// <param name="slaveColumnsToMerge">Indices of that columns of the slave table that should be merged into the master table.</param>
		public static void MergeTable(
			this DataTable destinationTable,
			DoubleColumn fractionalIndex,
			DataTable slaveTable,
			IAscendingIntegerCollection slaveColumnsToMerge)
		{
			int destinationTableStartIdx = destinationTable.DataColumnCount;
			destinationTable.AddDataColumnsWithPropertiesFrom(slaveTable, slaveColumnsToMerge);

			for (int i = 0; i < slaveColumnsToMerge.Count; i++)
			{
				int slaveColIdx = slaveColumnsToMerge[i];
				DataColumn newCol = destinationTable[destinationTableStartIdx + i];

				SetColumnFromFractionalIndex(newCol, fractionalIndex, slaveTable[slaveColIdx]);
			}
		}
    /// <summary>
    /// Makes a PCA (a principal component 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="bHorizontalOrientedSpectrum">True if a spectrum is a single row, False if a spectrum is a single column.</param>
    /// <param name="maxNumberOfFactors">The maximum number of factors to calculate.</param>
    /// <returns></returns>
    public static string PrincipalComponentAnalysis(
      Altaxo.AltaxoDocument mainDocument,
      Altaxo.Data.DataTable srctable,
      IAscendingIntegerCollection selectedColumns,
      IAscendingIntegerCollection selectedRows,
      bool bHorizontalOrientedSpectrum,
      int maxNumberOfFactors
      )
    {
      bool bUseSelectedColumns = (null!=selectedColumns && 0!=selectedColumns.Count);
      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[i] is Altaxo.Data.INumericColumn)
          numcols++;
      }

      // check the number of rows
      bool bUseSelectedRows = (null!=selectedRows && 0!=selectedRows.Count);

      int numrows;
      if(bUseSelectedRows)
        numrows = selectedRows.Count;
      else
      {
        numrows = 0;
        for(int i=0;i<numcols;i++)
        {
          int idx = bUseSelectedColumns ? selectedColumns[i] : i;
          numrows = Math.Max(numrows,srctable[idx].Count);
        }     
      }

      // check that both dimensions are at least 2 - otherwise PCA is not possible
      if(numrows<2)
        return "At least two rows are neccessary to do Principal Component Analysis!";
      if(numcols<2)
        return "At least two numeric columns are neccessary to do Principal Component Analysis!";

      // Create a matrix of appropriate dimensions and fill it

      MatrixMath.BEMatrix matrixX;
      if(bHorizontalOrientedSpectrum)
      {
        matrixX = new MatrixMath.BEMatrix(numrows,numcols);
        int ccol = 0; // current column in the matrix
        for(int i=0;i<prenumcols;i++)
        {
          int colidx = bUseSelectedColumns ? selectedColumns[i] : i;
          Altaxo.Data.INumericColumn col = srctable[colidx] as Altaxo.Data.INumericColumn;
          if(null!=col)
          {
            for(int j=0;j<numrows;j++)
            {
              int rowidx = bUseSelectedRows ? selectedRows[j] : j;
              matrixX[j,ccol] = col[rowidx];
            }
            ++ccol;
          }
        }
      } // end if it was a horizontal oriented spectrum
      else // if it is a vertical oriented spectrum
      {
        matrixX = new MatrixMath.BEMatrix(numcols,numrows);
        int ccol = 0; // current column in the matrix
        for(int i=0;i<prenumcols;i++)
        {
          int colidx = bUseSelectedColumns ? selectedColumns[i] : i;
          Altaxo.Data.INumericColumn col = srctable[colidx] as Altaxo.Data.INumericColumn;
          if(null!=col)
          {
            for(int j=0;j<numrows;j++)
            {
              int rowidx = bUseSelectedRows ? selectedRows[j] : j;
              matrixX[ccol,j] = col[rowidx];
            }
            ++ccol;
          }
        }
      } // if it was a vertical oriented spectrum

      // now do PCA with the matrix
      MatrixMath.REMatrix factors = new MatrixMath.REMatrix(0,0);
      MatrixMath.BEMatrix loads = new MatrixMath.BEMatrix(0,0);
      MatrixMath.BEMatrix residualVariances = new MatrixMath.BEMatrix(0,0);
      MatrixMath.HorizontalVector meanX = new MatrixMath.HorizontalVector(matrixX.Columns);
      // first, center the matrix
      MatrixMath.ColumnsToZeroMean(matrixX,meanX);
      MatrixMath.NIPALS_HO(matrixX,maxNumberOfFactors,1E-9,factors,loads,residualVariances);

      // 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
      Altaxo.Data.DataTable table = new Altaxo.Data.DataTable("PCA of " + srctable.Name);

      // Fill the Table
      table.Suspend();

      // first of all store the meanscore
    {
      double meanScore = MatrixMath.LengthOf(meanX);
      MatrixMath.NormalizeRows(meanX);
    
      Altaxo.Data.DoubleColumn col = new Altaxo.Data.DoubleColumn();
      for(int i=0;i<factors.Rows;i++)
        col[i] = meanScore;
      table.DataColumns.Add(col,"MeanFactor",Altaxo.Data.ColumnKind.V,0);
    }

      // first store the factors
      for(int i=0;i<factors.Columns;i++)
      {
        Altaxo.Data.DoubleColumn col = new Altaxo.Data.DoubleColumn();
        for(int j=0;j<factors.Rows;j++)
          col[j] = factors[j,i];
        
        table.DataColumns.Add(col,"Factor"+i.ToString(),Altaxo.Data.ColumnKind.V,1);
      }

      // now store the mean of the matrix
    {
      Altaxo.Data.DoubleColumn col = new Altaxo.Data.DoubleColumn();
      
      for(int j=0;j<meanX.Columns;j++)
        col[j] = meanX[0,j];
      table.DataColumns.Add(col,"MeanLoad",Altaxo.Data.ColumnKind.V,2);
    }

      // now store the loads - careful - they are horizontal in the matrix
      for(int i=0;i<loads.Rows;i++)
      {
        Altaxo.Data.DoubleColumn col = new Altaxo.Data.DoubleColumn();
        
        for(int j=0;j<loads.Columns;j++)
          col[j] = loads[i,j];
        
        table.DataColumns.Add(col,"Load"+i.ToString(),Altaxo.Data.ColumnKind.V,3);
      }

      // now store the residual variances, they are vertical in the vector
    {
      Altaxo.Data.DoubleColumn col = new Altaxo.Data.DoubleColumn();
      
      for(int i=0;i<residualVariances.Rows;i++)
        col[i] = residualVariances[i,0];
      table.DataColumns.Add(col,"ResidualVariance",Altaxo.Data.ColumnKind.V,4);
    }

      table.Resume();
      mainDocument.DataTableCollection.Add(table);
      // create a new worksheet without any columns
      Current.ProjectService.CreateNewWorksheet(table);

      return null;
    }
Example #36
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 #37
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 #38
0
		public static bool ArePropertyCellsSelected(DataTable table, IAscendingIntegerCollection selectedPropertyColumns, IAscendingIntegerCollection selectedPropertyRows)
		{
			return table.PropCols.ColumnCount > 0 && (selectedPropertyColumns.Count > 0 || selectedPropertyRows.Count > 0);
		}
Example #39
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="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);
			var memento = GetContentAsMultivariateContentMemento(modelTable);

			// 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);
			}
		}
Example #40
0
		/// <summary>
		/// Adds to the destination table selected columns from another table. Additionally, the properties of those columns will be added to the destination table.
		/// </summary>
		/// <param name="destinationTable">Table where the columns should be added to.</param>
		/// <param name="tableToAddFrom">Source table.</param>
		/// <param name="selectedColumns">Indexes of the columns of the source table that should be added to the destination table.</param>
		public static void AddDataColumnsWithPropertiesFrom(this DataTable destinationTable, DataTable tableToAddFrom, IAscendingIntegerCollection selectedColumns)
		{
			IAscendingIntegerCollection mergedPropColsIndices = destinationTable.PropCols.MergeColumnTypesFrom(tableToAddFrom.PropCols);

			for (int i = 0; i < selectedColumns.Count; i++)
			{
				int sourceIdx = selectedColumns[i];
				int destinationIdx = destinationTable.DataColumns.AppendCopiedColumnFrom(tableToAddFrom.DataColumns, sourceIdx, true, true);

				for (int j = 0; j < mergedPropColsIndices.Count; j++)
				{
					destinationTable.PropCols[mergedPropColsIndices[j]][destinationIdx] = tableToAddFrom.PropCols[j][sourceIdx];
				}
			}
		}
Example #41
0
		/// <summary>
		/// Exports to a single SPC spectrum from a single table column.
		/// </summary>
		/// <param name="filename">The name of the file where to export to.</param>
		/// <param name="table">The table from which to export.</param>
		/// <param name="columnnumber">The number of the table column that contains the data to export.</param>
		/// <param name="xcolumn">The x column that contains the x data.</param>
		/// <param name="selectedRows">The rows that where selected in the table, i.e. the rows which are exported. If this parameter is null
		/// or no rows are selected, then all data of a column will be exported.</param>
		/// <returns>Null if export was successfull, error description otherwise.</returns>
		public static string FromColumn(
			string filename,
			Altaxo.Data.DataTable table,
			int columnnumber,
			Altaxo.Data.INumericColumn xcolumn,
			IAscendingIntegerCollection selectedRows)
		{
			if (!(table.DataColumns[columnnumber] is Altaxo.Data.INumericColumn))
				return string.Format("Table column[{0}] ({1}) is not a numeric column!", columnnumber, table.DataColumns[columnnumber].FullName);

			// test that all x and y cells have numeric values
			bool bUseSel = null != selectedRows && selectedRows.Count > 0;
			int spectrumlen = (bUseSel) ? selectedRows.Count : table.DataColumns[columnnumber].Count;

			int i, j;

			for (j = 0; j < spectrumlen; j++)
			{
				i = bUseSel ? selectedRows[j] : j;

				if (double.IsNaN(xcolumn[i]))
					return string.Format("X column at index {i} has no numeric value!", i);

				if (((Altaxo.Data.INumericColumn)table.DataColumns[columnnumber])[i] == Double.NaN)
					return string.Format("Table cell [{0},{1}] (column {2}) has no numeric value!", columnnumber, i, table.DataColumns[columnnumber].FullName);
			}

			// this first test was successfull, so start exporting now

			double[] xvalues = new double[spectrumlen];
			double[] yvalues = new double[spectrumlen];

			for (j = 0; j < spectrumlen; j++)
			{
				i = bUseSel ? selectedRows[j] : j;
				xvalues[j] = xcolumn[i];
				yvalues[j] = ((Altaxo.Data.INumericColumn)table.DataColumns[columnnumber])[i];
			}
			return FromArrays(xvalues, yvalues, filename);
		}
Example #42
0
		/// <summary>
		/// Create a statistical table for statistics on columns. Property columns are not included in the statistical table.
		/// </summary>
		/// <param name="srcTable"></param>
		/// <param name="selectedColumns"></param>
		/// <returns></returns>
		private static DataTable CreateStatisticalTable(DataColumnCollection srcTable, IAscendingIntegerCollection selectedColumns)
		{
			DataTable result = new DataTable();

			result.DataColumns.Add(new TextColumn(), DefaultColumnNameColumnName, ColumnKind.X, 0);
			AddStatisticColumns(result);
			return result;
		}
Example #43
0
		/// <summary>
		/// Paints part of the worksheet to the drawing context. Row and column header are always threaten as visible here.
		/// </summary>
		/// <param name="dc">Drawing context.</param>
		/// <param name="layout">Worksheet layout.</param>
		/// <param name="viewSize">Width and height of the viewing area (Pixel or Wpf coordinates).</param>
		/// <param name="clipRectangle">Bounds of the clipping region. Only that parts of the worksheet that are visible within the clipping region are drawn.</param>
		/// <param name="selectedDataColumns">Selected data columns.</param>
		/// <param name="selectedDataRows">Selected data rows.</param>
		/// <param name="selectedPropertyColumns">Selected property columns.</param>
		/// <param name="selectedPropertyRows">Selected property rows.</param>
		/// <param name="horzScrollPos">Horizontal scroll position (0 = first column visible).</param>
		/// <param name="vertScrollPos">Vertical scroll position (0 = first data column visible, negative values: one or more property columns visible).</param>
		public static void PaintTableArea(
			Graphics dc,
			Altaxo.Worksheet.WorksheetLayout layout,
			Size viewSize,
			RectangleD2D clipRectangle,
			IAscendingIntegerCollection selectedDataColumns,
			IAscendingIntegerCollection selectedDataRows,
			IAscendingIntegerCollection selectedPropertyColumns,
			IAscendingIntegerCollection selectedPropertyRows,
			int horzScrollPos, int vertScrollPos
			)
		{
			var dataTable = layout.DataTable;

			bool bDrawColumnHeader = false;

			int firstTableRowToDraw = WA.GetFirstVisibleTableRow(clipRectangle.Top, layout, vertScrollPos);
			int numberOfTableRowsToDraw = WA.GetVisibleTableRows(clipRectangle.Top, clipRectangle.Bottom, layout, vertScrollPos);

			int firstPropertyColumnToDraw = WA.GetFirstVisiblePropertyColumn(clipRectangle.Top, layout, vertScrollPos);
			int numberOfPropertyColumnsToDraw = WA.GetVisiblePropertyColumns(clipRectangle.Top, clipRectangle.Bottom, layout, vertScrollPos);

			bool bAreColumnsSelected = selectedDataColumns.Count > 0;
			bool bAreRowsSelected = selectedDataRows.Count > 0;
			bool bAreCellsSelected = bAreRowsSelected || bAreColumnsSelected;

			bool bArePropertyColsSelected = selectedPropertyColumns.Count > 0;
			bool bArePropertyRowsSelected = selectedPropertyRows.Count > 0;
			bool bArePropertyCellsSelected = ArePropertyCellsSelected(dataTable, selectedPropertyColumns, selectedPropertyRows);

			int yShift = 0;

			var cellRectangle = new RectangleD2D();
			double left, width;

			if (clipRectangle.Top < layout.ColumnHeaderStyle.Height)
			{
				bDrawColumnHeader = true;
			}

			// if neccessary, draw the row header (the most left column)
			if (clipRectangle.Left < layout.RowHeaderStyle.Width)
			{
				cellRectangle.Height = layout.ColumnHeaderStyle.Height;
				cellRectangle.Width = layout.RowHeaderStyle.Width;
				cellRectangle.X = 0;

				// if visible, draw the top left corner of the table
				if (bDrawColumnHeader)
				{
					cellRectangle.Y = 0;
					layout.RowHeaderStyle.PaintBackground(dc, (Rectangle)cellRectangle, false);
				}

				// if visible, draw property column header items
				yShift = WA.GetTopCoordinateOfPropertyColumn(firstPropertyColumnToDraw, layout, vertScrollPos);
				cellRectangle.Height = layout.PropertyColumnHeaderStyle.Height;
				for (int nPropCol = firstPropertyColumnToDraw, nInc = 0; nInc < numberOfPropertyColumnsToDraw; nPropCol++, nInc++)
				{
					cellRectangle.Y = yShift + nInc * layout.PropertyColumnHeaderStyle.Height;
					bool bPropColSelected = bArePropertyColsSelected && selectedPropertyColumns.Contains(nPropCol);
					layout.PropertyColumnHeaderStyle.Paint(dc, (Rectangle)cellRectangle, nPropCol, dataTable.PropCols[nPropCol], bPropColSelected);
				}
			}

			// draw the table row Header Items
			yShift = WA.GetTopCoordinateOfTableRow(firstTableRowToDraw, layout, vertScrollPos);
			cellRectangle.Height = layout.RowHeaderStyle.Height;
			for (int nRow = firstTableRowToDraw, nInc = 0; nInc < numberOfTableRowsToDraw; nRow++, nInc++)
			{
				cellRectangle.Y = yShift + nInc * layout.RowHeaderStyle.Height;
				layout.RowHeaderStyle.Paint(dc, (Rectangle)cellRectangle, nRow, null, bAreRowsSelected && selectedDataRows.Contains(nRow));
			}

			if (clipRectangle.Bottom >= layout.ColumnHeaderStyle.Height || clipRectangle.Right >= layout.RowHeaderStyle.Width)
			{
				int numberOfColumnsToDraw;
				int firstColToDraw = WA.GetFirstAndNumberOfVisibleColumn(clipRectangle.Left, clipRectangle.Right, layout, horzScrollPos, out numberOfColumnsToDraw);

				// draw the property columns
				for (int nPropCol = firstPropertyColumnToDraw, nIncPropCol = 0; nIncPropCol < numberOfPropertyColumnsToDraw; nPropCol++, nIncPropCol++)
				{
					Altaxo.Worksheet.ColumnStyle cs = layout.PropertyColumnStyles[dataTable.PropCols[nPropCol]];
					bool bPropColSelected = bArePropertyColsSelected && selectedPropertyColumns.Contains(nPropCol);
					bool bPropColIncluded = bArePropertyColsSelected ? bPropColSelected : true; // Property cells are only included if the column is explicite selected

					cellRectangle.Y = WA.GetTopCoordinateOfPropertyColumn(nPropCol, layout, vertScrollPos);
					cellRectangle.Height = layout.PropertyColumnHeaderStyle.Height;

					for (int nCol = firstColToDraw, nIncCol = 0; nIncCol < numberOfColumnsToDraw; nCol++, nIncCol++)
					{
						if (nCol == firstColToDraw)
						{
							WA.GetXCoordinatesOfColumn(nCol, layout, horzScrollPos, out left, out width);
							cellRectangle.X = left;
							cellRectangle.Width = width;
						}
						else
						{
							cellRectangle.X += cellRectangle.Width;
							cellRectangle.Width = layout.DataColumnStyles[dataTable.DataColumns[nCol]].WidthD;
						}

						bool bPropRowSelected = bArePropertyRowsSelected && selectedPropertyRows.Contains(nCol);
						bool bPropRowIncluded = bArePropertyRowsSelected ? bPropRowSelected : true;

						cs.Paint(dc, (Rectangle)cellRectangle, nCol, dataTable.PropCols[nPropCol], bArePropertyCellsSelected && bPropColIncluded && bPropRowIncluded);
					}
				}

				// draw the cells
				for (int nCol = firstColToDraw, nIncCol = 0; nIncCol < numberOfColumnsToDraw; nCol++, nIncCol++)
				{
					Altaxo.Worksheet.ColumnStyle cs = layout.DataColumnStyles[dataTable.DataColumns[nCol]];
					if (nCol == firstColToDraw)
					{
						WA.GetXCoordinatesOfColumn(nCol, layout, horzScrollPos, out left, out width);
						cellRectangle.X = left;
						cellRectangle.Width = width;
					}
					else
					{
						cellRectangle.X += cellRectangle.Width;
						cellRectangle.Width = cs.WidthD;
					}

					bool bColumnSelected = bAreColumnsSelected && selectedDataColumns.Contains(nCol);
					bool bDataColumnIncluded = bAreColumnsSelected ? bColumnSelected : true;
					bool bPropertyRowSelected = bArePropertyRowsSelected && selectedPropertyRows.Contains(nCol);

					if (bDrawColumnHeader) // must the column Header been drawn?
					{
						cellRectangle.Height = layout.ColumnHeaderStyle.Height;
						cellRectangle.Y = 0;
						layout.ColumnHeaderStyle.Paint(dc, (Rectangle)cellRectangle, 0, dataTable[nCol], bColumnSelected || bPropertyRowSelected);
					}

					yShift = WA.GetTopCoordinateOfTableRow(firstTableRowToDraw, layout, vertScrollPos);
					cellRectangle.Height = layout.RowHeaderStyle.Height;
					for (int nRow = firstTableRowToDraw, nIncRow = 0; nIncRow < numberOfTableRowsToDraw; nRow++, nIncRow++)
					{
						bool bRowSelected = bAreRowsSelected && selectedDataRows.Contains(nRow);
						bool bDataRowIncluded = bAreRowsSelected ? bRowSelected : true;
						cellRectangle.Y = yShift + nIncRow * layout.RowHeaderStyle.Height;
						cs.Paint(dc, (Rectangle)cellRectangle, nRow, dataTable[nCol], bAreCellsSelected && bDataColumnIncluded && bDataRowIncluded);
					}
				}
			}
		}
 public MultivariateLinearFitParameters(DataColumnCollection table, IAscendingIntegerCollection selectedDataColumns)
 {
   _table = table;
   _selectedDataColumns = selectedDataColumns;
 }
Example #45
0
		/// <summary>
		/// Calculates statistics of selected columns. Creates a new table where the statistical data will be written to.
		/// </summary>
		/// <param name="srctable">Source table.</param>
		/// <param name="selectedColumns">Selected data columns in the source table.</param>
		/// <param name="selectedRows">Selected rows in the source table.</param>
		/// <param name="destinationTable">The table where the statistical results are written to.</param>
		public static void DoStatisticsOnRows(
			this DataColumnCollection srctable,
			IAscendingIntegerCollection selectedColumns,
			IAscendingIntegerCollection selectedRows,
			DataColumnCollection destinationTable
			)
		{
			bool bUseSelectedColumns = (null != selectedColumns && 0 != selectedColumns.Count);
			int numcols = bUseSelectedColumns ? selectedColumns.Count : srctable.ColumnCount;
			if (numcols == 0)
				return; // nothing selected

			bool bUseSelectedRows = (null != selectedRows && 0 != selectedRows.Count);
			int numrows = bUseSelectedRows ? selectedRows.Count : srctable.RowCount;
			if (numrows == 0)
				return;

			Data.DoubleColumn cRows = new DoubleColumn();

			// 1st column is the mean, and holds the sum during the calculation
			Data.DoubleColumn colMean = new Data.DoubleColumn();

			// 2rd column is the standard deviation, and holds the square sum during calculation
			Data.DoubleColumn colSD = new Data.DoubleColumn();

			// 3th column is the standard e (N)
			Data.DoubleColumn colSE = new Data.DoubleColumn();

			// 4th column is the sum
			Data.DoubleColumn colSum = new Data.DoubleColumn();

			// 5th column is the number of items for statistics
			Data.DoubleColumn colNN = new Data.DoubleColumn();

			var colSumSqr = new Data.DoubleColumn();
			var colFracOneSigma = new Data.DoubleColumn();
			var colFracTwoSigma = new Data.DoubleColumn();
			var colFracThreeSigma = new Data.DoubleColumn();
			var colMinimum = new DoubleColumn();
			var colMaximum = new DoubleColumn();

			// first fill the cols c1, c2, c5 with zeros because we want to sum up
			for (int i = 0; i < numrows; i++)
			{
				colSum[i] = 0;
				colSumSqr[i] = 0;
				colNN[i] = 0;
				colMinimum[i] = double.PositiveInfinity;
				colMaximum[i] = double.NegativeInfinity;
			}

			for (int si = 0; si < numcols; si++)
			{
				Altaxo.Data.DataColumn col = bUseSelectedColumns ? srctable[selectedColumns[si]] : srctable[si];
				if (!(col is Altaxo.Data.INumericColumn))
					continue;

				// now do the statistics
				Data.INumericColumn ncol = (Data.INumericColumn)col;
				for (int i = 0; i < numrows; i++)
				{
					int row = bUseSelectedRows ? selectedRows[i] : i;
					cRows[i] = row;

					double val = ncol[row];
					if (Double.IsNaN(val))
						continue;

					colSum[i] += val;
					colSumSqr[i] += val * val;
					colNN[i] += 1;
					colMinimum[i] = Math.Min(colMinimum[i], val);
					colMaximum[i] = Math.Max(colMaximum[i], val);
				}
			} // for all selected columns

			// now calculate the statistics
			for (int i = 0; i < numrows; i++)
			{
				// now fill a new row in the worksheet
				double NN = colNN[i];
				double sum = colSum[i];
				double sumsqr = colSumSqr[i];
				if (NN > 0)
				{
					double mean = sum / NN;
					double ymy0sqr = sumsqr - sum * sum / NN;
					if (ymy0sqr < 0) ymy0sqr = 0; // if this is lesser zero, it is a rounding error, so set it to zero
					double sd = NN > 1 ? Math.Sqrt(ymy0sqr / (NN - 1)) : 0;
					double se = sd / Math.Sqrt(NN);

					colMean[i] = mean; // mean
					colSD[i] = sd;
					colSE[i] = se;
				}
				else
				{
					colMinimum[i] = double.NaN;
					colMaximum[i] = double.NaN;
				}
			} // for all rows

			// calculate fractions

			for (int i = 0; i < numrows; i++)
			{
				int row = bUseSelectedRows ? selectedRows[i] : i;

				double mean = colMean[i];
				double sd = colSD[i];

				// calculate fractions
				double oneSigmaLo = mean - 1 * sd, oneSigmaHi = mean + 1 * sd;
				double twoSigmaLo = mean - 2 * sd, twoSigmaHi = mean + 2 * sd;
				double threeSigmaLo = mean - 3 * sd, threeSigmaHi = mean + 3 * sd;
				int cntOneSigma = 0, cntTwoSigma = 0, cntThreeSigma = 0;

				for (int si = 0; si < numcols; si++)
				{
					Altaxo.Data.DataColumn col = bUseSelectedColumns ? srctable[selectedColumns[si]] : srctable[si];
					if (!(col is Altaxo.Data.INumericColumn))
						continue;

					// now do the statistics
					Data.INumericColumn ncol = (Data.INumericColumn)col;
					double val = ncol[row];
					if (Double.IsNaN(val))
						continue;

					if (Altaxo.Calc.RMath.IsInIntervalCC(val, oneSigmaLo, oneSigmaHi)) ++cntOneSigma;
					if (Altaxo.Calc.RMath.IsInIntervalCC(val, twoSigmaLo, twoSigmaHi)) ++cntTwoSigma;
					if (Altaxo.Calc.RMath.IsInIntervalCC(val, threeSigmaLo, threeSigmaHi)) ++cntThreeSigma;
				}

				colFracOneSigma[i] = cntOneSigma / (double)colNN[i];
				colFracTwoSigma[i] = cntTwoSigma / (double)colNN[i];
				colFracThreeSigma[i] = cntThreeSigma / (double)colNN[i];
			}

			destinationTable.EnsureExistence(DefaultRowNumberColumnName, typeof(DoubleColumn), ColumnKind.X, 0).Append(cRows);
			AppendStatisticalData(destinationTable, colMean, colSD, colSE, colSum, colSumSqr, colNN, colFracOneSigma, colFracTwoSigma, colFracThreeSigma, colMinimum, colMaximum);
		}
    public static LinearFitBySvd ShowDialogAndRegress(DataColumnCollection table, IAscendingIntegerCollection selectedColumns)
    {
      if(selectedColumns.Count<2)
        return null;

      object paramobject = new MultivariateLinearFitParameters(table,selectedColumns);

      if(!Current.Gui.ShowDialog(ref paramobject,"Multivariate linear fit"))
        return null;

      MultivariateLinearFitParameters parameters = (MultivariateLinearFitParameters)paramobject;

      LinearFitBySvd result =  Regress(parameters, true);

      return result;
    }
Example #47
0
    /// <summary>
    /// Remove the selected data columns <b>and the corresponding property rows</b>.
    /// </summary>
    /// <param name="selectedColumns">A collection of the indizes to the columns that have to be removed.</param>
    public virtual void RemoveColumns(IAscendingIntegerCollection selectedColumns)
    {
  
      Suspend();
            
      _dataColumns.RemoveColumns(selectedColumns); // remove the columns from the collection
      _propertyColumns.RemoveRows(selectedColumns); // remove also the corresponding rows from the Properties

      Resume();
    }
		/// <summary>
		/// Creates the controller for the Galactic SPC file export dialog.
		/// </summary>
		/// <param name="table">The data table which is about to be exported.</param>
		/// <param name="selectedRows">The selected rows of the data table.</param>
		/// <param name="selectedColumns">The selected columns of the data table.</param>
		public ExportGalacticSpcFileDialogController(
			Altaxo.Data.DataTable table,
			IAscendingIntegerCollection selectedRows,
			IAscendingIntegerCollection selectedColumns)
		{
			m_Table = table;
			m_SelectedRows = selectedRows;
			m_SelectedColumns = selectedColumns;

			InitializeElements();
		}
    /// <summary>
    /// Multiplies selected columns to form a matrix.
    /// </summary>
    /// <param name="mainDocument"></param>
    /// <param name="srctable"></param>
    /// <param name="selectedColumns"></param>
    /// <returns>Null if successful, else the description of the error.</returns>
    /// <remarks>The user must select an even number of columns. All columns of the first half of the selection 
    /// must have the same number of rows, and all columns of the second half of selection must also have the same
    /// number of rows. The first half of selected columns form a matrix of dimensions(firstrowcount,halfselected), and the second half
    /// of selected columns form a matrix of dimension(halfselected, secondrowcount). The resulting matrix has dimensions (firstrowcount,secondrowcount) and is
    /// stored in a separate worksheet.</remarks>
    public static string MultiplyColumnsToMatrix(
      Altaxo.AltaxoDocument mainDocument,
      Altaxo.Data.DataTable srctable,
      IAscendingIntegerCollection selectedColumns
      )
    {
      // check that there are columns selected
      if(0==selectedColumns.Count)
        return "You must select at least two columns to multiply!";
      // selected columns must contain an even number of columns
      if(0!=selectedColumns.Count%2)
        return "You selected an odd number of columns. Please select an even number of columns to multiply!";
      // all selected columns must be numeric columns
      for(int i=0;i<selectedColumns.Count;i++)
      {
        if(!(srctable[selectedColumns[i]] is Altaxo.Data.INumericColumn))
          return string.Format("The column[{0}] (name:{1}) is not a numeric column!",selectedColumns[i],srctable[selectedColumns[i]].Name);
      }


      int halfselect = selectedColumns.Count/2;
    
      // check that all columns from the first half of selected colums contain the same
      // number of rows

      int rowsfirsthalf=int.MinValue;
      for(int i=0;i<halfselect;i++)
      {
        int idx = selectedColumns[i];
        if(rowsfirsthalf<0)
          rowsfirsthalf = srctable[idx].Count;
        else if(rowsfirsthalf != srctable[idx].Count)
          return "The first half of selected columns have not all the same length!";
      }

      int rowssecondhalf=int.MinValue;
      for(int i=halfselect;i<selectedColumns.Count;i++)
      {
        int idx = selectedColumns[i];
        if(rowssecondhalf<0)
          rowssecondhalf = srctable[idx].Count;
        else if(rowssecondhalf != srctable[idx].Count)
          return "The second half of selected columns have not all the same length!";
      }


      // now create the matrices to multiply from the 

      MatrixMath.REMatrix firstMat = new MatrixMath.REMatrix(rowsfirsthalf,halfselect);
      for(int i=0;i<halfselect;i++)
      {
        Altaxo.Data.INumericColumn col = (Altaxo.Data.INumericColumn)srctable[selectedColumns[i]];
        for(int j=0;j<rowsfirsthalf;j++)
          firstMat[j,i] = col[j];
      }
      
      MatrixMath.BEMatrix secondMat = new MatrixMath.BEMatrix(halfselect,rowssecondhalf);
      for(int i=0;i<halfselect;i++)
      {
        Altaxo.Data.INumericColumn col = (Altaxo.Data.INumericColumn)srctable[selectedColumns[i+halfselect]];
        for(int j=0;j<rowssecondhalf;j++)
          secondMat[i,j] = col[j];
      }

      // now multiply the two matrices
      MatrixMath.BEMatrix resultMat = new MatrixMath.BEMatrix(rowsfirsthalf,rowssecondhalf);
      MatrixMath.Multiply(firstMat,secondMat,resultMat);


      // and store the result in a new worksheet 
      Altaxo.Data.DataTable table = new Altaxo.Data.DataTable("ResultMatrix of " + srctable.Name);
      table.Suspend();

      // first store the factors
      for(int i=0;i<resultMat.Columns;i++)
      {
        Altaxo.Data.DoubleColumn col = new Altaxo.Data.DoubleColumn();
        for(int j=0;j<resultMat.Rows;j++)
          col[j] = resultMat[j,i];
        
        table.DataColumns.Add(col,i.ToString());
      }

      table.Resume();
      mainDocument.DataTableCollection.Add(table);
      // create a new worksheet without any columns
      Current.ProjectService.CreateNewWorksheet(table);

      return null;
    }
Example #50
0
		public static void ShowDecomposeByColumnContentDialog(this DataTable srcTable, IAscendingIntegerCollection selectedDataRows, IAscendingIntegerCollection selectedDataColumns)
		{
			DataTableMultipleColumnProxy proxy = null;
			DecomposeByColumnContentOptions options = null;

			try
			{
				proxy = new DataTableMultipleColumnProxy(DecomposeByColumnContentDataAndOptions.ColumnsParticipatingIdentifier, srcTable, selectedDataRows, selectedDataColumns);
				proxy.EnsureExistenceOfIdentifier(DecomposeByColumnContentDataAndOptions.ColumnWithCyclingVariableIdentifier, 1);

				options = new DecomposeByColumnContentOptions();
			}
			catch (Exception ex)
			{
				Current.Gui.ErrorMessageBox(string.Format("{0}\r\nDetails:\r\n{1}", ex.Message, ex.ToString()), "Error in preparation of 'Decompose by column content'");
				return;
			}

			var dataAndOptions = new DecomposeByColumnContentDataAndOptions(proxy, options);

			// in order to show the column names etc in the dialog, it is neccessary to set the source
			if (true == Current.Gui.ShowDialog(ref dataAndOptions, "Choose options", false))
			{
				var destTable = new DataTable();
				proxy = dataAndOptions.Data;
				options = dataAndOptions.Options;

				string error = null;
				try
				{
					error = DecomposeByColumnContent(dataAndOptions.Data, dataAndOptions.Options, destTable);
				}
				catch (Exception ex)
				{
					error = ex.ToString();
				}
				if (null != error)
					Current.Gui.ErrorMessageBox(error);

				destTable.Name = srcTable.Name + "_Decomposed";

				// Create a DataSource
				var dataSource = new DecomposeByColumnContentDataSource(proxy, options, new Altaxo.Data.DataSourceImportOptions());
				destTable.DataSource = dataSource;

				Current.Project.DataTableCollection.Add(destTable);
				Current.ProjectService.ShowDocumentView(destTable);
			}
		}
 /// <summary>
 /// This is neccessary to use the dialog.
 /// </summary>
 /// <param name="table">The table which contains the data to export.</param>
 /// <param name="selectedRows">The rows selected in the table.</param>
 /// <param name="selectedColumns">The columns selected in the table.</param>
 public void Initialize(Altaxo.Data.DataTable table, IAscendingIntegerCollection selectedRows, IAscendingIntegerCollection selectedColumns)
 {
   m_Controller = new ExportGalacticSpcFileDialogController(this,table,selectedRows,selectedColumns);
 }
Example #52
0
		/// <summary>
		/// Maps each data column of the source table to a corresponding data columns of the destination table.
		/// The matching is based on the index (order) and on the currently selected columns of the destination table.
		/// Attention: The match here does <b>not</b> mean that the two columns are data compatible to each other!
		/// </summary>
		/// <param name="desttable">The destination table.</param>
		/// <param name="selectedDestColumns">The currently selected columns of the destination table.</param>
		/// <param name="sourcetable">The source table.</param>
		/// <returns>An array of columns. Each column of the array is a data column in the destination table, which
		/// matches (by index) the data column in the source table.</returns>
		/// <remarks>
		/// 1.) Since the returned columns are part of the DataColumns collection of the destination table, you must not
		/// use these for inserting i.e. in other tables.
		/// 2.) The match is based on the index and the selected columns of the destination table. The rules are as follows: if there is
		/// no selection, the first column of the destination table matches the first column of the source table, and so forth.
		/// If there is a column selection, the first selected column of the destination table matches the first column of the source table,
		/// the second selected column of the destination table matches the second column of the source table. If more source columns than selected columns in the destination
		/// table exists, the match is done 1:1 after the last selected column of the destination table. If there is no further column in the destination
		/// table to match, new columns are created in the destination table.
		/// </remarks>
		static protected Altaxo.Data.DataColumn[] MapOrCreateDataColumns(Altaxo.Data.DataTable desttable, IAscendingIntegerCollection selectedDestColumns, Altaxo.Data.DataTable sourcetable)
		{
			Altaxo.Data.DataColumn[] columnmap = new Altaxo.Data.DataColumn[sourcetable.DataColumns.ColumnCount];
			int nDestCol = -1;
			for (int nCol = 0; nCol < sourcetable.DataColumns.ColumnCount; nCol++)
			{
				nDestCol = nCol < selectedDestColumns.Count ? selectedDestColumns[nCol] : nDestCol + 1;

				string name = sourcetable.DataColumns.GetColumnName(nCol);
				int group = sourcetable.DataColumns.GetColumnGroup(nCol);
				Altaxo.Data.ColumnKind kind = sourcetable.DataColumns.GetColumnKind(nCol);

				if (nDestCol < desttable.DataColumns.ColumnCount)
				{
					columnmap[nCol] = desttable.DataColumns[nDestCol];
				}
				else
				{
					columnmap[nCol] = (DataColumn)Activator.CreateInstance(sourcetable.DataColumns[nCol].GetType());
					desttable.DataColumns.Add(columnmap[nCol], name, kind, group);
				}
			}
			return columnmap;
		}
Example #53
0
		public XYZMeshedColumnPlotData(DataTable table, IAscendingIntegerCollection selectedDataRows, IAscendingIntegerCollection selectedDataColumns, IAscendingIntegerCollection selectedPropertyColumns)
		{
			_matrixProxy = new DataTableMatrixProxy(table, selectedDataRows, selectedDataColumns, selectedPropertyColumns) { ParentObject = this };
			this.SetXBoundsFromTemplate(new FiniteNumericalBoundaries());
			this.SetYBoundsFromTemplate(new FiniteNumericalBoundaries());
			this.SetVBoundsFromTemplate(new FiniteNumericalBoundaries());
		}
Example #54
0
 /// <summary>
 /// Constructor. Besides the table, the current selections must be provided. Only the areas that corresponds to the selections are
 /// serialized. The serialization process has to occur immediately after this constructor, because only a reference
 /// to the table is hold by this object.
 /// </summary>
 /// <param name="table">The table to serialize.</param>
 /// <param name="selectedDataColumns">The selected data columns.</param>
 /// <param name="selectedDataRows">The selected data rows.</param>
 /// <param name="selectedPropertyColumns">The selected property columns.</param>
 /// <param name="selectedPropertyRows">The selected property rows.</param>
 public ClipboardMemento(DataTable table, IAscendingIntegerCollection selectedDataColumns, 
   IAscendingIntegerCollection selectedDataRows,
   IAscendingIntegerCollection selectedPropertyColumns,
   IAscendingIntegerCollection selectedPropertyRows
   )
 {
   this._table                   = table;
   this._selectedDataColumns     = selectedDataColumns;
   this._selectedDataRows        = selectedDataRows;
   this._selectedPropertyColumns = selectedPropertyColumns;
   this._selectedPropertyRows    = selectedPropertyRows;
 }
Example #55
0
		/// <summary>
		/// Creates a table for statistics on columns. Property columns are included in the statistical table.
		/// </summary>
		/// <param name="srcTable"></param>
		/// <param name="selectedColumns"></param>
		/// <returns></returns>
		private static DataTable CreateStatisticalTable(DataTable srcTable, IAscendingIntegerCollection selectedColumns)
		{
			DataTable result = new DataTable();
			result.Name = Altaxo.Main.ProjectFolder.PrependToName(srcTable.Name, "Statistics of ");

			result.DataColumns.Add(new TextColumn(), DefaultColumnNameColumnName, ColumnKind.X, 0);
			AddSourcePropertyColumns(srcTable, selectedColumns, result);
			AddStatisticColumns(result);
			return result;
		}
Example #56
0
		/// <summary>
		/// Shows the dialog for Galactic SPC file export, and exports the data of the table using the options provided in that dialog.
		/// </summary>
		/// <param name="dataTable">DataTable to export.</param>
		/// <param name="selectedDataRows">Rows to export (can be null - then all rows will be considered for export).</param>
		/// <param name="selectedDataColumns">Columns to export (can be null - then all columns will be considered for export).</param>
		public static void ShowExportGalacticSPCDialog(this DataTable dataTable, IAscendingIntegerCollection selectedDataRows, IAscendingIntegerCollection selectedDataColumns)
		{
			var exportCtrl = new Altaxo.Gui.Worksheet.ExportGalacticSpcFileDialogController(dataTable, selectedDataRows, selectedDataColumns);
			Current.Gui.ShowDialog(exportCtrl, "Export Galactic SPC format");
		}
Example #57
0
		private static void AddSourcePropertyColumns(DataTable srctable, IAscendingIntegerCollection selectedColumns, DataTable destinationTable)
		{
			bool bUseSelectedColumns = (null != selectedColumns && 0 != selectedColumns.Count);
			int numcols = bUseSelectedColumns ? selectedColumns.Count : srctable.DataColumnCount;

			// new : add a copy of all property columns; can be usefull
			for (int i = 0; i < srctable.PropertyColumnCount; i++)
			{
				DataColumn originalColumn = srctable.PropertyColumns[i];
				DataColumn clonedColumn = (DataColumn)originalColumn.Clone();
				clonedColumn.Clear();
				for (int si = 0; si < numcols; si++)
				{
					int idx = bUseSelectedColumns ? selectedColumns[si] : si;
					clonedColumn[si] = originalColumn[idx];
				}
				destinationTable.DataColumns.Add(clonedColumn, srctable.PropertyColumns.GetColumnName(i), srctable.PropertyColumns.GetColumnKind(i), srctable.PropertyColumns.GetColumnGroup(i));
			}
		}
 /// <summary>
 /// This is neccessary to use the dialog.
 /// </summary>
 /// <param name="table">The table which contains the data to export.</param>
 /// <param name="selectedRows">The rows selected in the table.</param>
 /// <param name="selectedColumns">The columns selected in the table.</param>
 public void Initialize(Altaxo.Data.DataTable table, IAscendingIntegerCollection selectedRows, IAscendingIntegerCollection selectedColumns)
 {
  
 }
Example #59
0
		/// <summary>
		/// Calculates statistics of selected columns. Creates a new table where the statistical data will be written to.
		/// </summary>
		/// <param name="srctable">Source table.</param>
		/// <param name="selectedColumns">Selected data columns in the source table. If the argument is null, all columns will be used.</param>
		/// <param name="selectedRows">Selected rows in the source table. If the argument is null, all rows will be used.</param>
		/// <param name="destinationTable">The table where the statistical results are written to.</param>
		public static void DoStatisticsOnColumns(
			this DataColumnCollection srctable,
			IAscendingIntegerCollection selectedColumns,
			IAscendingIntegerCollection selectedRows,
			DataColumnCollection destinationTable
			)
		{
			bool bUseSelectedColumns = (null != selectedColumns && 0 != selectedColumns.Count);
			int numcols = bUseSelectedColumns ? selectedColumns.Count : srctable.ColumnCount;

			bool bUseSelectedRows = (null != selectedRows && 0 != selectedRows.Count);

			if (numcols == 0)
				return; // nothing selected

			// add a text column and some double columns
			// note: statistics is only possible for numeric columns since
			// otherwise in one column doubles and i.e. dates are mixed, which is not possible

			// 1st column is the name of the column of which the statistics is made
			Data.TextColumn colCol = new Data.TextColumn();

			// 2nd column is the mean
			Data.DoubleColumn colMean = new Data.DoubleColumn();

			// 3rd column is the standard deviation
			Data.DoubleColumn colSd = new Data.DoubleColumn();

			// 4th column is the standard e (N)
			Data.DoubleColumn colSe = new Data.DoubleColumn();

			// 5th column is the sum
			Data.DoubleColumn colSum = new Data.DoubleColumn();

			var colSumSqr = new Data.DoubleColumn();

			// 6th column is the number of items for statistics
			Data.DoubleColumn colN = new Data.DoubleColumn();

			var colFracOneSigma = new Data.DoubleColumn();
			var colFracTwoSigma = new Data.DoubleColumn();
			var colFracThreeSigma = new Data.DoubleColumn();

			var colMinimum = new DoubleColumn(); // Minimum of the values
			var colMaximum = new DoubleColumn(); // Maximum of the values

			int currRow = 0;
			for (int si = 0; si < numcols; si++)
			{
				Altaxo.Data.DataColumn col = bUseSelectedColumns ? srctable[selectedColumns[si]] : srctable[si];
				if (!(col is Altaxo.Data.INumericColumn))
					continue;

				int rows = bUseSelectedRows ? selectedRows.Count : srctable.RowCount;
				if (rows == 0)
					continue;

				// now do the statistics
				Data.INumericColumn ncol = (Data.INumericColumn)col;
				double sum = 0;
				double sumsqr = 0;
				int NN = 0;
				double minimum = double.PositiveInfinity;
				double maximum = double.NegativeInfinity;

				for (int i = 0; i < rows; i++)
				{
					double val = bUseSelectedRows ? ncol[selectedRows[i]] : ncol[i];
					if (Double.IsNaN(val))
						continue;

					NN++;
					sum += val;
					sumsqr += (val * val);
					minimum = Math.Min(minimum, val);
					maximum = Math.Max(maximum, val);
				}
				// now fill a new row in the worksheet

				double mean = sum / NN;
				double ymy0sqr = sumsqr - sum * sum / NN;
				if (ymy0sqr < 0) ymy0sqr = 0; // if this is lesser zero, it is a rounding error, so set it to zero
				double sd = NN > 1 ? Math.Sqrt(ymy0sqr / (NN - 1)) : 0;
				double se = sd / Math.Sqrt(NN);

				// calculate fractions
				double oneSigmaLo = mean - 1 * sd, oneSigmaHi = mean + 1 * sd;
				double twoSigmaLo = mean - 2 * sd, twoSigmaHi = mean + 2 * sd;
				double threeSigmaLo = mean - 3 * sd, threeSigmaHi = mean + 3 * sd;
				int cntOneSigma = 0, cntTwoSigma = 0, cntThreeSigma = 0;

				for (int i = 0; i < rows; i++)
				{
					double val = bUseSelectedRows ? ncol[selectedRows[i]] : ncol[i];
					if (Double.IsNaN(val))
						continue;

					if (Altaxo.Calc.RMath.IsInIntervalCC(val, oneSigmaLo, oneSigmaHi)) ++cntOneSigma;
					if (Altaxo.Calc.RMath.IsInIntervalCC(val, twoSigmaLo, twoSigmaHi)) ++cntTwoSigma;
					if (Altaxo.Calc.RMath.IsInIntervalCC(val, threeSigmaLo, threeSigmaHi)) ++cntThreeSigma;
				}

				if (0 == NN)
				{
					minimum = maximum = double.NaN;
				}

				colCol[currRow] = col.Name;
				colMean[currRow] = mean; // mean
				colSd[currRow] = sd;
				colSe[currRow] = se;
				colSum[currRow] = sum;
				colSumSqr[currRow] = sumsqr;
				colN[currRow] = NN;
				colFracOneSigma[currRow] = cntOneSigma / (double)NN;
				colFracTwoSigma[currRow] = cntTwoSigma / (double)NN;
				colFracThreeSigma[currRow] = cntThreeSigma / (double)NN;
				colMinimum[currRow] = minimum;
				colMaximum[currRow] = maximum;
				currRow++; // for the next column
			} // for all selected columns

			if (currRow != 0)
			{
				destinationTable.EnsureExistence(DefaultColumnNameColumnName, typeof(TextColumn), ColumnKind.X, 0).Append(colCol);
				AppendStatisticalData(destinationTable, colMean, colSd, colSe, colSum, colSumSqr, colN, colFracOneSigma, colFracTwoSigma, colFracThreeSigma, colMinimum, colMaximum);
			}
		}
Example #60
0
		/// <summary>
		/// Maps each data column of the source table to a corresponding data row (!) of the destination table.
		/// The matching is based on the index (order) and on the currently selected columns of the destination table.
		/// Attention: The match here does <b>not</b> mean that the data of destination columns and source rows are compatible to each other!
		/// </summary>
		/// <param name="desttable">The destination table.</param>
		/// <param name="selectedDestColumns">The currently selected columns of the destination table.</param>
		/// <param name="sourcetable">The source table.</param>
		/// <returns>An array of columns. Each column of the array is a data column in the destination table, which
		/// matches (by index) the data row (!) in the source table with the same index.</returns>
		/// <remarks>
		/// 1.) Since the returned columns are part of the DataColumns collection of the destination table, you must not
		/// use these for inserting i.e. in other tables.
		/// 2.) The match is based on the index and the selected columns of the destination table. The rules are as follows: if there is
		/// no selection, the first column of the destination table matches the first row of the source table, and so forth.
		/// If there is a column selection, the first selected column of the destination table matches the first row of the source table,
		/// the second selected column of the destination table matches the second row of the source table. If there are more source rows than selected columns in the destination
		/// table exists, the match is done 1:1 after the last selected column of the destination table. If there is no further column in the destination
		/// table to match, new columns are created in the destination table. The type of the newly created columns in the destination table is
		/// the same as the first column of the source table in this case.
		/// </remarks>
		static protected Altaxo.Data.DataColumn[] MapOrCreateDataColumnsToRows(Altaxo.Data.DataTable desttable, IAscendingIntegerCollection selectedDestColumns, Altaxo.Data.DataTable sourcetable)
		{
			Altaxo.Data.DataColumn[] columnmap = new Altaxo.Data.DataColumn[sourcetable.DataColumns.RowCount];
			int nDestCol = -1;
			int group = 0;
			for (int nCol = 0; nCol < sourcetable.DataColumns.RowCount; nCol++)
			{
				nDestCol = nCol < selectedDestColumns.Count ? selectedDestColumns[nCol] : nDestCol + 1;

				if (nDestCol < desttable.DataColumns.ColumnCount)
				{
					group = desttable.DataColumns.GetColumnGroup(nDestCol); // we preserve the group of the last existing column for creation of new columns
					columnmap[nCol] = desttable.DataColumns[nDestCol];
				}
				else
				{
					columnmap[nCol] = (DataColumn)Activator.CreateInstance(sourcetable.DataColumns[0].GetType());
					desttable.DataColumns.Add(columnmap[nCol], desttable.DataColumns.FindNewColumnName(), ColumnKind.V, group);
				}
			}
			return columnmap;
		}