Esempio n. 1
0
        /// <summary>
        /// Fills destination column with values from an original column by help of a fractional index.
        /// </summary>
        /// <param name="destinationColumn">Column to fill. The old data in this column will be cleared before.</param>
        /// <param name="fractionalIndex">Array of fractional indices. Each item points into the originalColumn to that value that should be included in the master column at the item's index.</param>
        /// <param name="originalColumn">Column with the source data.</param>
        public static void SetColumnFromFractionalIndex(
            this DataColumn destinationColumn,
            DoubleColumn fractionalIndex,
            DataColumn originalColumn)
        {
            destinationColumn.Clear();

            for (int i = 0; i < fractionalIndex.Count; i++)
            {
                double fracIdx = fractionalIndex[i];
                int    idxBase = (int)Math.Floor(fracIdx);
                double rel     = fracIdx - idxBase;

                if (0 == rel)
                {
                    destinationColumn[i] = originalColumn[idxBase];
                }
                else
                {
                    try
                    {
                        var firstValue  = originalColumn[idxBase];
                        var secondValue = originalColumn[idxBase + 1];
                        destinationColumn[i] = firstValue + rel * (secondValue - firstValue);
                    }
                    catch (Exception)
                    {
                        destinationColumn[i] = originalColumn[idxBase];
                    }
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates a matrix from three selected columns. This must be a x-column, a y-column, and a value column.
        /// </summary>
        /// <param name="xcol">Column of x-values.</param>
        /// <param name="ycol">Column of y-values.</param>
        /// <param name="vcol">Column of v-values.</param>
        /// <param name="newtable">On return, contains the newly created table matrix.</param>
        /// <returns>Null if no error occurs, or an error message.</returns>
        public static string XYVToMatrix(DataColumn xcol, DataColumn ycol, DataColumn vcol, out DataTable newtable)
        {
            newtable = null;
            System.Collections.SortedList xx = new System.Collections.SortedList();
            System.Collections.SortedList yy = new System.Collections.SortedList();
            int len = xcol.Count;

            len = Math.Min(len, ycol.Count);
            len = Math.Min(len, vcol.Count);

            // Fill the xx and yy lists
            for (int i = 0; i < len; ++i)
            {
                if (!xx.Contains(xcol[i]))
                {
                    xx.Add(xcol[i], null);
                }

                if (!yy.Contains(ycol[i]))
                {
                    yy.Add(ycol[i], null);
                }
            }

            DataColumn xnew = (DataColumn)Activator.CreateInstance(xcol.GetType());
            DataColumn ynew = (DataColumn)Activator.CreateInstance(ycol.GetType());

            xnew.Clear();
            ynew.Clear();

            for (int i = xx.Count - 1; i >= 0; --i)
            {
                xnew[i]          = (AltaxoVariant)xx.GetKey(i);
                xx[xx.GetKey(i)] = i;
            }

            for (int i = yy.Count - 1; i >= 0; --i)
            {
                ynew[1 + i]      = (AltaxoVariant)yy.GetKey(i); // 1 + is because the table will get an additional x-column
                yy[yy.GetKey(i)] = i;
            }

            DataColumn vtemplate = (DataColumn)Activator.CreateInstance(vcol.GetType());

            // make a new table with yy.Count number of columns
            DataColumn[] vcols = new DataColumn[yy.Count];
            for (int i = yy.Count - 1; i >= 0; --i)
            {
                vcols[i] = (DataColumn)vtemplate.Clone();
            }

            // now fill the columns
            for (int i = 0; i < len; ++i)
            {
                int xidx = (int)xx[xcol[i]];
                int yidx = (int)yy[ycol[i]];

                vcols[yidx][xidx] = vcol[i];
            }

            // assemble all columns together in a table
            newtable = new DataTable();
            newtable.DataColumns.Add(xnew, "X", ColumnKind.X, 0);
            newtable.PropertyColumns.Add(ynew, "Y", ColumnKind.Y, 0);

            for (int i = 0; i < vcols.Length; ++i)
            {
                newtable.DataColumns.Add(vcols[i], "V" + i.ToString(), ColumnKind.V, 0);
            }

            return(null);
        }
Esempio n. 3
0
        public static void StatisticsOnColumns(
            Altaxo.AltaxoDocument mainDocument,
            Altaxo.Data.DataTable srctable,
            IAscendingIntegerCollection selectedColumns,
            IAscendingIntegerCollection selectedRows
            )
        {
            bool bUseSelectedColumns = (null != selectedColumns && 0 != selectedColumns.Count);
            int  numcols             = bUseSelectedColumns ? selectedColumns.Count : srctable.DataColumns.ColumnCount;

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

            if (numcols == 0)
            {
                return;                  // nothing selected
            }
            Data.DataTable table = null; // the created table


            // 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();

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

            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.DataColumns.RowCount;
                if (rows == 0)
                {
                    continue;
                }

                // now do the statistics
                Data.INumericColumn ncol = (Data.INumericColumn)col;
                double sum    = 0;
                double sumsqr = 0;
                int    NN     = 0;
                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);
                }
                // now fill a new row in the worksheet

                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);

                    colCol[currRow]  = col.Name;
                    colMean[currRow] = mean; // mean
                    colSd[currRow]   = sd;
                    colSe[currRow]   = se;
                    colSum[currRow]  = sum;
                    colN[currRow]    = NN;
                    currRow++; // for the next column
                }
            } // for all selected columns


            if (currRow != 0)
            {
                table = new Altaxo.Data.DataTable("Statistics of " + srctable.Name);
                table.DataColumns.Add(colCol, "Col", Altaxo.Data.ColumnKind.X);

                // 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];
                    }
                    table.DataColumns.Add(clonedColumn, srctable.PropertyColumns.GetColumnName(i), srctable.PropertyColumns.GetColumnKind(i), srctable.PropertyColumns.GetColumnGroup(i));
                }

                table.DataColumns.Add(colMean, "Mean");
                table.DataColumns.Add(colSd, "Sd");
                table.DataColumns.Add(colSe, "Se");
                table.DataColumns.Add(colSum, "Sum");
                table.DataColumns.Add(colN, "N");

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