Ejemplo n.º 1
0
        public void WriteDataWithAnnotationColumnsTest()
        {
            // main data
            IDataWithAnnotationColumns mdata = PerseusFactory.CreateDataWithAnnotationColumns();

            // annotation columns
            mdata.AddStringColumn("strcol1", "this is stringcol1", new[] { "1", "2" });
            mdata.AddStringColumn("strcol2", "", new[] { "", "hallo" });
            mdata.AddNumericColumn("numcol", "", new[] { 1.0, 2.0 });
            mdata.AddMultiNumericColumn("multnumcol", "this is multnumcol", new[] { new[] { -2.0, 2.0 }, new double[] {} });
            mdata.AddCategoryColumn("catcol", "", new[] { new[] { "cat1", "cat1.1" }, new[] { "cat2", "cat1" } });
            string mdataStr;

            using (MemoryStream memstream = new MemoryStream())
                using (StreamWriter writer = new StreamWriter(memstream)) {
                    PerseusUtils.WriteDataWithAnnotationColumns(mdata, writer);
                    writer.Flush();
                    mdataStr = Encoding.UTF8.GetString(memstream.ToArray());
                }
            IMatrixData mdata3 = PerseusFactory.CreateMatrixData();

            PerseusUtils.ReadMatrix(mdata3, new ProcessInfo(new Settings(), status => { }, progress => { }, 1, i => { }),
                                    () => {
                StreamReader tmpStream = new StreamReader(new MemoryStream(Encoding.UTF8.GetBytes(mdataStr)));
                return(tmpStream);
            }, "matrix1", '\t');
            IDataWithAnnotationColumns mdata2 = mdata3;

            Assert.AreEqual(2, mdata2.RowCount);
            Assert.AreEqual(2, mdata2.StringColumnCount);
            Assert.AreEqual(1, mdata2.NumericColumnCount);
            Assert.AreEqual(1, mdata2.CategoryColumnCount);
            Assert.AreEqual(1, mdata2.MultiNumericColumnCount);
            Assert.AreEqual("hallo", mdata2.StringColumns[mdata2.StringColumnNames.FindIndex(col => col.Equals("strcol2"))][1]);
        }
Ejemplo n.º 2
0
        public static void ProcessData(IDataWithAnnotationColumns mdata, IAnnotationProvider annotationProvider, Parameters para, ProcessInfo processInfo)
        {
            string[] baseIds = GetBaseIds(mdata, annotationProvider, para);
            bool     success = ProcessDataAddAnnotation(mdata.RowCount, annotationProvider, para, baseIds, processInfo, out string[] name,
                                                        out int[] catColInds,
                                                        out int[] textColInds, out int[] numColInds, out string[][][] catCols, out string[][] textCols,
                                                        out double[][] numCols);

            if (!success)
            {
                return;
            }
            for (int i = 0; i < catCols.Length; i++)
            {
                mdata.AddCategoryColumn(name[catColInds[i]], "", catCols[i]);
            }
            for (int i = 0; i < textCols.Length; i++)
            {
                mdata.AddStringColumn(name[textColInds[i]], "", textCols[i]);
            }
            for (int i = 0; i < numCols.Length; i++)
            {
                mdata.AddNumericColumn(name[numColInds[i]], "", numCols[i]);
            }
        }
Ejemplo n.º 3
0
        private static void AddNumericColumns(IDataWithAnnotationColumns mdata1, IDataWithAnnotationColumns mdata2,
                                              Parameters parameters, IList <int[]> indexMap, IDataWithAnnotationColumns result)
        {
            Func <double[], double> avNumerical = GetAveraging(parameters.GetParam <int>("Combine numerical values").Value);

            int[] numCols = parameters.GetParam <int[]>("Numerical columns").Value;
            if (avNumerical != null)
            {
                double[][] newNumericalColumns = new double[numCols.Length][];
                string[]   newNumColNames      = new string[numCols.Length];
                for (int i = 0; i < numCols.Length; i++)
                {
                    double[] oldCol = mdata2.NumericColumns[numCols[i]];
                    newNumColNames[i]      = mdata2.NumericColumnNames[numCols[i]];
                    newNumericalColumns[i] = new double[mdata1.RowCount];
                    for (int j = 0; j < mdata1.RowCount; j++)
                    {
                        int[]         inds   = indexMap[j];
                        List <double> values = new List <double>();
                        foreach (int ind in inds)
                        {
                            double v = oldCol[ind];
                            if (!double.IsNaN(v))
                            {
                                values.Add(v);
                            }
                        }
                        newNumericalColumns[i][j] = values.Count == 0 ? double.NaN : avNumerical(values.ToArray());
                    }
                }
                for (int i = 0; i < numCols.Length; i++)
                {
                    result.AddNumericColumn(newNumColNames[i], "", newNumericalColumns[i]);
                }
            }
            else
            {
                double[][][] newMultiNumericalColumns = new double[numCols.Length][][];
                string[]     newMultiNumColNames      = new string[numCols.Length];
                for (int i = 0; i < numCols.Length; i++)
                {
                    double[] oldCol = mdata2.NumericColumns[numCols[i]];
                    newMultiNumColNames[i]      = mdata2.NumericColumnNames[numCols[i]];
                    newMultiNumericalColumns[i] = new double[mdata1.RowCount][];
                    for (int j = 0; j < mdata1.RowCount; j++)
                    {
                        int[]         inds   = indexMap[j];
                        List <double> values = new List <double>();
                        foreach (int ind in inds)
                        {
                            double v = oldCol[ind];
                            if (!double.IsNaN(v))
                            {
                                values.Add(v);
                            }
                        }
                        newMultiNumericalColumns[i][j] = values.ToArray();
                    }
                }
                for (int i = 0; i < numCols.Length; i++)
                {
                    result.AddMultiNumericColumn(newMultiNumColNames[i], "", newMultiNumericalColumns[i]);
                }
            }
        }