Exemplo n.º 1
0
        public IMatrixData ProcessData(IMatrixData[] inputData, Parameters param, ref IMatrixData[] supplTables,
                                       ref IDocumentData[] documents, ProcessInfo processInfo)
        {
            IMatrixData mdata1 = inputData[0];
            IMatrixData mdata2 = inputData[1];
            int         nrows1 = mdata1.RowCount;
            int         nrows2 = mdata2.RowCount;
            int         nrows  = nrows1 + nrows2;

            string[] expColNames = SpecialSort(mdata1.ColumnNames, mdata2.ColumnNames, out Dictionary <string, int> dic1,
                                               out Dictionary <string, int> dic2);
            double[,] ex = new double[nrows, expColNames.Length];
            for (int i = 0; i < ex.GetLength(0); i++)
            {
                for (int j = 0; j < ex.GetLength(1); j++)
                {
                    ex[i, j] = double.NaN;
                }
            }
            for (int i = 0; i < expColNames.Length; i++)
            {
                if (dic1.ContainsKey(expColNames[i]))
                {
                    int ind = dic1[expColNames[i]];
                    for (int j = 0; j < nrows1; j++)
                    {
                        ex[j, i] = mdata1.Values.Get(j, ind);
                    }
                }
                if (dic2.ContainsKey(expColNames[i]))
                {
                    int ind = dic2[expColNames[i]];
                    for (int j = 0; j < nrows2; j++)
                    {
                        ex[nrows1 + j, i] = mdata2.Values.Get(j, ind);
                    }
                }
            }
            string[] numColNames =
                SpecialSort(mdata1.NumericColumnNames, mdata2.NumericColumnNames, out dic1, out dic2);
            List <double[]> numCols = new List <double[]>();

            for (int i = 0; i < numColNames.Length; i++)
            {
                numCols.Add(new double[nrows]);
                for (int j = 0; j < nrows; j++)
                {
                    numCols[numCols.Count - 1][j] = double.NaN;
                }
            }
            for (int i = 0; i < numColNames.Length; i++)
            {
                if (dic1.ContainsKey(numColNames[i]))
                {
                    int ind = dic1[numColNames[i]];
                    for (int j = 0; j < nrows1; j++)
                    {
                        numCols[i][j] = mdata1.NumericColumns[ind][j];
                    }
                }
                if (dic2.ContainsKey(numColNames[i]))
                {
                    int ind = dic2[numColNames[i]];
                    for (int j = 0; j < nrows2; j++)
                    {
                        numCols[i][nrows1 + j] = mdata2.NumericColumns[ind][j];
                    }
                }
            }
            string[] stringColNames =
                SpecialSort(mdata1.StringColumnNames, mdata2.StringColumnNames, out dic1, out dic2);
            List <string[]> stringCols = new List <string[]>();

            for (int i = 0; i < stringColNames.Length; i++)
            {
                stringCols.Add(new string[nrows]);
                for (int j = 0; j < nrows; j++)
                {
                    stringCols[stringCols.Count - 1][j] = "";
                }
            }
            for (int i = 0; i < stringColNames.Length; i++)
            {
                if (dic1.ContainsKey(stringColNames[i]))
                {
                    int ind = dic1[stringColNames[i]];
                    for (int j = 0; j < nrows1; j++)
                    {
                        stringCols[i][j] = mdata1.StringColumns[ind][j];
                    }
                }
                if (dic2.ContainsKey(stringColNames[i]))
                {
                    int ind = dic2[stringColNames[i]];
                    for (int j = 0; j < nrows2; j++)
                    {
                        stringCols[i][nrows1 + j] = mdata2.StringColumns[ind][j];
                    }
                }
            }
            string[] catColNames =
                SpecialSort(mdata1.CategoryColumnNames, mdata2.CategoryColumnNames, out dic1, out dic2);
            List <string[][]> catCols = new List <string[][]>();

            for (int i = 0; i < catColNames.Length; i++)
            {
                catCols.Add(new string[nrows][]);
                for (int j = 0; j < nrows; j++)
                {
                    catCols[catCols.Count - 1][j] = new string[0];
                }
            }
            for (int i = 0; i < catColNames.Length; i++)
            {
                if (dic1.ContainsKey(catColNames[i]))
                {
                    int ind = dic1[catColNames[i]];
                    for (int j = 0; j < nrows1; j++)
                    {
                        catCols[i][j] = mdata1.GetCategoryColumnEntryAt(ind, j);
                    }
                }
                if (dic2.ContainsKey(catColNames[i]))
                {
                    int ind = dic2[catColNames[i]];
                    for (int j = 0; j < nrows2; j++)
                    {
                        catCols[i][nrows1 + j] = mdata2.GetCategoryColumnEntryAt(ind, j);
                    }
                }
            }
            string[] multiNumColNames = SpecialSort(mdata1.MultiNumericColumnNames, mdata2.MultiNumericColumnNames,
                                                    out dic1, out dic2);
            List <double[][]> multiNumCols = new List <double[][]>();

            for (int i = 0; i < multiNumColNames.Length; i++)
            {
                multiNumCols.Add(new double[nrows][]);
                for (int j = 0; j < nrows; j++)
                {
                    multiNumCols[multiNumCols.Count - 1][j] = new double[0];
                }
            }
            for (int i = 0; i < multiNumColNames.Length; i++)
            {
                if (dic1.ContainsKey(multiNumColNames[i]))
                {
                    int ind = dic1[multiNumColNames[i]];
                    for (int j = 0; j < nrows1; j++)
                    {
                        multiNumCols[i][j] = mdata1.MultiNumericColumns[ind][j];
                    }
                }
                if (dic2.ContainsKey(multiNumColNames[i]))
                {
                    int ind = dic2[multiNumColNames[i]];
                    for (int j = 0; j < nrows2; j++)
                    {
                        multiNumCols[i][nrows1 + j] = mdata2.MultiNumericColumns[ind][j];
                    }
                }
            }
            IMatrixData result = PerseusUtils.CreateMatrixData(inputData[0], ex, expColNames.ToList());

            result.ColumnDescriptions             = result.ColumnNames;
            result.NumericColumnNames             = new List <string>(numColNames);
            result.NumericColumnDescriptions      = result.NumericColumnNames;
            result.NumericColumns                 = numCols;
            result.StringColumnNames              = new List <string>(stringColNames);
            result.StringColumnDescriptions       = result.ColumnNames;
            result.StringColumns                  = stringCols;
            result.CategoryColumnNames            = new List <string>(catColNames);
            result.CategoryColumnDescriptions     = result.CategoryColumnNames;
            result.CategoryColumns                = catCols;
            result.MultiNumericColumnNames        = new List <string>(multiNumColNames);
            result.MultiNumericColumnDescriptions = result.MultiNumericColumnNames;
            result.MultiNumericColumns            = multiNumCols;
            return(result);
        }
Exemplo n.º 2
0
        public IMatrixData ProcessData(IMatrixData[] inputData, Parameters param, ref IMatrixData[] supplTables,
                                       ref IDocumentData[] documents, ProcessInfo processInfo)
        {
            IMatrixData resultinput = (IMatrixData)inputData[0].CreateNewInstance(DataType.Matrix);

            if (nummatrix == 2)
            {
                IMatrixData mdata1  = inputData[0];
                IMatrixData mdata2  = inputData[1];
                string[]    header1 = new string[mdata1.RowCount];
                for (int i = 0; i < mdata1.RowCount; i++)
                {
                    header1[i] = mdata1.Name;
                }
                string[] header2 = new string[mdata2.RowCount];
                for (int i = 0; i < mdata2.RowCount; i++)
                {
                    header2[i] = mdata2.Name;
                }
                int      nrows1      = mdata1.RowCount;
                int      nrows2      = mdata2.RowCount;
                int      nrows       = nrows1 + nrows2;
                string[] expColNames = SpecialSort(mdata1.ColumnNames, mdata2.ColumnNames,
                                                   out Dictionary <string, int> dic1, out Dictionary <string, int> dic2);
                double[,] ex = new double[nrows, expColNames.Length];
                for (int i = 0; i < ex.GetLength(0); i++)
                {
                    for (int j = 0; j < ex.GetLength(1); j++)
                    {
                        ex[i, j] = double.NaN;
                    }
                }
                for (int i = 0; i < expColNames.Length; i++)
                {
                    if (dic1.ContainsKey(expColNames[i]))
                    {
                        int ind = dic1[expColNames[i]];
                        for (int j = 0; j < nrows1; j++)
                        {
                            ex[j, i] = mdata1.Values.Get(j, ind);
                        }
                    }
                    if (dic2.ContainsKey(expColNames[i]))
                    {
                        int ind = dic2[expColNames[i]];
                        for (int j = 0; j < nrows2; j++)
                        {
                            ex[nrows1 + j, i] = mdata2.Values.Get(j, ind);
                        }
                    }
                }
                string[] numColNames = SpecialSort(mdata1.NumericColumnNames, mdata2.NumericColumnNames, out dic1,
                                                   out dic2);
                List <double[]> numCols = new List <double[]>();
                for (int i = 0; i < numColNames.Length; i++)
                {
                    numCols.Add(new double[nrows]);
                    for (int j = 0; j < nrows; j++)
                    {
                        numCols[numCols.Count - 1][j] = double.NaN;
                    }
                }
                for (int i = 0; i < numColNames.Length; i++)
                {
                    if (dic1.ContainsKey(numColNames[i]))
                    {
                        int ind = dic1[numColNames[i]];
                        for (int j = 0; j < nrows1; j++)
                        {
                            numCols[i][j] = mdata1.NumericColumns[ind][j];
                        }
                    }
                    if (dic2.ContainsKey(numColNames[i]))
                    {
                        int ind = dic2[numColNames[i]];
                        for (int j = 0; j < nrows2; j++)
                        {
                            numCols[i][nrows1 + j] = mdata2.NumericColumns[ind][j];
                        }
                    }
                }
                string[] stringColNames =
                    SpecialSort(mdata1.StringColumnNames, mdata2.StringColumnNames, out dic1, out dic2);
                List <string[]> stringCols = new List <string[]>();
                for (int i = 0; i < stringColNames.Length; i++)
                {
                    stringCols.Add(new string[nrows]);
                    for (int j = 0; j < nrows; j++)
                    {
                        stringCols[stringCols.Count - 1][j] = "";
                    }
                }
                for (int i = 0; i < stringColNames.Length; i++)
                {
                    if (dic1.ContainsKey(stringColNames[i]))
                    {
                        int ind = dic1[stringColNames[i]];
                        for (int j = 0; j < nrows1; j++)
                        {
                            stringCols[i][j] = mdata1.StringColumns[ind][j];
                        }
                    }
                    if (dic2.ContainsKey(stringColNames[i]))
                    {
                        int ind = dic2[stringColNames[i]];
                        for (int j = 0; j < nrows2; j++)
                        {
                            stringCols[i][nrows1 + j] = mdata2.StringColumns[ind][j];
                        }
                    }
                }
                string[] catColNames = SpecialSort(mdata1.CategoryColumnNames, mdata2.CategoryColumnNames, out dic1,
                                                   out dic2);
                List <string[][]> catCols = new List <string[][]>();
                for (int i = 0; i < catColNames.Length; i++)
                {
                    catCols.Add(new string[nrows][]);
                    for (int j = 0; j < nrows; j++)
                    {
                        catCols[catCols.Count - 1][j] = new string[0];
                    }
                }
                for (int i = 0; i < catColNames.Length; i++)
                {
                    if (dic1.ContainsKey(catColNames[i]))
                    {
                        int ind = dic1[catColNames[i]];
                        for (int j = 0; j < nrows1; j++)
                        {
                            catCols[i][j] = mdata1.GetCategoryColumnEntryAt(ind, j);
                        }
                    }
                    if (dic2.ContainsKey(catColNames[i]))
                    {
                        int ind = dic2[catColNames[i]];
                        for (int j = 0; j < nrows2; j++)
                        {
                            catCols[i][nrows1 + j] = mdata2.GetCategoryColumnEntryAt(ind, j);
                        }
                    }
                }
                string[] multiNumColNames = SpecialSort(mdata1.MultiNumericColumnNames, mdata2.MultiNumericColumnNames,
                                                        out dic1, out dic2);
                List <double[][]> multiNumCols = new List <double[][]>();
                for (int i = 0; i < multiNumColNames.Length; i++)
                {
                    multiNumCols.Add(new double[nrows][]);
                    for (int j = 0; j < nrows; j++)
                    {
                        multiNumCols[multiNumCols.Count - 1][j] = new double[0];
                    }
                }
                for (int i = 0; i < multiNumColNames.Length; i++)
                {
                    if (dic1.ContainsKey(multiNumColNames[i]))
                    {
                        int ind = dic1[multiNumColNames[i]];
                        for (int j = 0; j < nrows1; j++)
                        {
                            multiNumCols[i][j] = mdata1.MultiNumericColumns[ind][j];
                        }
                    }
                    if (dic2.ContainsKey(multiNumColNames[i]))
                    {
                        int ind = dic2[multiNumColNames[i]];
                        for (int j = 0; j < nrows2; j++)
                        {
                            multiNumCols[i][nrows1 + j] = mdata2.MultiNumericColumns[ind][j];
                        }
                    }
                }
                string   MatrixName        = "Matrix Name";
                string   MatrixDescription = "Description";
                string[] listnames         = header1.Concat(header2).ToArray();
                // string[][] resultarray = catlistnames.Select(x => x.ToArray()).ToArray();
                //IMPORTANT!!!!! TODO: check if the name of the matrix if changed
                IMatrixData result = PerseusUtils.CreateMatrixData(inputData[0], ex, expColNames.ToList());
                result.NumericColumnNames             = new List <string>(numColNames);
                result.NumericColumnDescriptions      = result.NumericColumnNames;
                result.NumericColumns                 = numCols;
                result.StringColumnNames              = new List <string>(stringColNames);
                result.StringColumns                  = stringCols;
                result.CategoryColumnNames            = new List <string>(catColNames);
                result.CategoryColumnDescriptions     = result.CategoryColumnNames;
                result.CategoryColumns                = catCols;
                result.MultiNumericColumnNames        = new List <string>(multiNumColNames);
                result.MultiNumericColumnDescriptions = result.MultiNumericColumnNames;
                result.MultiNumericColumns            = multiNumCols;
                HashSet <string> taken = new HashSet <string>(result.StringColumnNames);
                result.AddStringColumn(MatrixName, MatrixName, listnames);
                taken.Add(MatrixName);
                return(result);
            }
            else if (nummatrix == 3)
            {
                ////////////////////////////
                ////////////////////////////
                IMatrixData mdata1  = inputData[0];
                IMatrixData mdata2  = inputData[1];
                IMatrixData mdata3  = inputData[2];
                string[]    header1 = new string[mdata1.RowCount];
                for (int i = 0; i < mdata1.RowCount; i++)
                {
                    header1[i] = mdata1.Name;
                }
                string[] header2 = new string[mdata2.RowCount];
                for (int i = 0; i < mdata2.RowCount; i++)
                {
                    header2[i] = mdata2.Name;
                }
                string[] header3 = new string[mdata3.RowCount];
                for (int i = 0; i < mdata3.RowCount; i++)
                {
                    header3[i] = mdata3.Name;
                }
                int      nrows1      = mdata1.RowCount;
                int      nrows2      = mdata2.RowCount;
                int      nrows3      = mdata3.RowCount;
                int      nrows       = nrows1 + nrows2 + nrows3;
                string[] expColNames = SpecialSort3(mdata1.ColumnNames, mdata2.ColumnNames, mdata3.ColumnNames,
                                                    out Dictionary <string, int> dic1, out Dictionary <string, int> dic2,
                                                    out Dictionary <string, int> dic3);
                double[,] ex = new double[nrows, expColNames.Length];
                for (int i = 0; i < ex.GetLength(0); i++)
                {
                    for (int j = 0; j < ex.GetLength(1); j++)
                    {
                        ex[i, j] = double.NaN;
                    }
                }
                for (int i = 0; i < expColNames.Length; i++)
                {
                    if (dic1.ContainsKey(expColNames[i]))
                    {
                        int ind = dic1[expColNames[i]];
                        for (int j = 0; j < nrows1; j++)
                        {
                            ex[j, i] = mdata1.Values.Get(j, ind);
                        }
                    }
                    if (dic2.ContainsKey(expColNames[i]))
                    {
                        int ind = dic2[expColNames[i]];
                        for (int j = 0; j < nrows2; j++)
                        {
                            ex[nrows1 + j, i] = mdata2.Values.Get(j, ind);
                        }
                    }
                    if (dic3.ContainsKey(expColNames[i]))
                    {
                        int ind = dic3[expColNames[i]];
                        for (int j = 0; j < nrows3; j++)
                        {
                            ex[nrows2 + j, i] = mdata3.Values.Get(j, ind);
                        }
                    }
                }
                string[] numColNames = SpecialSort3(mdata1.NumericColumnNames, mdata2.NumericColumnNames,
                                                    mdata3.NumericColumnNames, out dic1, out dic2, out dic3);
                List <double[]> numCols = new List <double[]>();
                for (int i = 0; i < numColNames.Length; i++)
                {
                    numCols.Add(new double[nrows]);
                    for (int j = 0; j < nrows; j++)
                    {
                        numCols[numCols.Count - 1][j] = double.NaN;
                    }
                }
                for (int i = 0; i < numColNames.Length; i++)
                {
                    if (dic1.ContainsKey(numColNames[i]))
                    {
                        int ind = dic1[numColNames[i]];
                        for (int j = 0; j < nrows1; j++)
                        {
                            numCols[i][j] = mdata1.NumericColumns[ind][j];
                        }
                    }
                    if (dic2.ContainsKey(numColNames[i]))
                    {
                        int ind = dic2[numColNames[i]];
                        for (int j = 0; j < nrows2; j++)
                        {
                            numCols[i][nrows1 + j] = mdata2.NumericColumns[ind][j];
                        }
                    }
                    if (dic3.ContainsKey(numColNames[i]))
                    {
                        int ind = dic3[numColNames[i]];
                        for (int j = 0; j < nrows3; j++)
                        {
                            numCols[i][nrows2 + j] = mdata3.NumericColumns[ind][j];
                        }
                    }
                }
                string[] stringColNames = SpecialSort3(mdata1.StringColumnNames, mdata2.StringColumnNames,
                                                       mdata3.StringColumnNames, out dic1, out dic2, out dic3);
                List <string[]> stringCols = new List <string[]>();
                for (int i = 0; i < stringColNames.Length; i++)
                {
                    stringCols.Add(new string[nrows]);
                    for (int j = 0; j < nrows; j++)
                    {
                        stringCols[stringCols.Count - 1][j] = "";
                    }
                }
                for (int i = 0; i < stringColNames.Length; i++)
                {
                    if (dic1.ContainsKey(stringColNames[i]))
                    {
                        int ind = dic1[stringColNames[i]];
                        for (int j = 0; j < nrows1; j++)
                        {
                            stringCols[i][j] = mdata1.StringColumns[ind][j];
                        }
                    }
                    if (dic2.ContainsKey(stringColNames[i]))
                    {
                        int ind = dic2[stringColNames[i]];
                        for (int j = 0; j < nrows2; j++)
                        {
                            stringCols[i][nrows1 + j] = mdata2.StringColumns[ind][j];
                        }
                    }
                    if (dic3.ContainsKey(stringColNames[i]))
                    {
                        int ind = dic3[stringColNames[i]];
                        for (int j = 0; j < nrows3; j++)
                        {
                            stringCols[i][nrows2 + j] = mdata3.StringColumns[ind][j];
                        }
                    }
                }
                string[] catColNames = SpecialSort3(mdata1.CategoryColumnNames, mdata2.CategoryColumnNames,
                                                    mdata3.CategoryColumnNames, out dic1, out dic2, out dic3);
                List <string[][]> catCols = new List <string[][]>();
                for (int i = 0; i < catColNames.Length; i++)
                {
                    catCols.Add(new string[nrows][]);
                    for (int j = 0; j < nrows; j++)
                    {
                        catCols[catCols.Count - 1][j] = new string[0];
                    }
                }
                for (int i = 0; i < catColNames.Length; i++)
                {
                    if (dic1.ContainsKey(catColNames[i]))
                    {
                        int ind = dic1[catColNames[i]];
                        for (int j = 0; j < nrows1; j++)
                        {
                            catCols[i][j] = mdata1.GetCategoryColumnEntryAt(ind, j);
                        }
                    }
                    if (dic2.ContainsKey(catColNames[i]))
                    {
                        int ind = dic2[catColNames[i]];
                        for (int j = 0; j < nrows2; j++)
                        {
                            catCols[i][nrows1 + j] = mdata2.GetCategoryColumnEntryAt(ind, j);
                        }
                    }
                    if (dic3.ContainsKey(catColNames[i]))
                    {
                        int ind = dic3[catColNames[i]];
                        for (int j = 0; j < nrows3; j++)
                        {
                            catCols[i][nrows2 + j] = mdata3.GetCategoryColumnEntryAt(ind, j);
                        }
                    }
                }
                string[] multiNumColNames = SpecialSort3(mdata1.MultiNumericColumnNames, mdata2.MultiNumericColumnNames,
                                                         mdata3.MultiNumericColumnNames, out dic1, out dic2, out dic3);
                List <double[][]> multiNumCols = new List <double[][]>();
                for (int i = 0; i < multiNumColNames.Length; i++)
                {
                    multiNumCols.Add(new double[nrows][]);
                    for (int j = 0; j < nrows; j++)
                    {
                        multiNumCols[multiNumCols.Count - 1][j] = new double[0];
                    }
                }
                for (int i = 0; i < multiNumColNames.Length; i++)
                {
                    if (dic1.ContainsKey(multiNumColNames[i]))
                    {
                        int ind = dic1[multiNumColNames[i]];
                        for (int j = 0; j < nrows1; j++)
                        {
                            multiNumCols[i][j] = mdata1.MultiNumericColumns[ind][j];
                        }
                    }
                    if (dic2.ContainsKey(multiNumColNames[i]))
                    {
                        int ind = dic2[multiNumColNames[i]];
                        for (int j = 0; j < nrows2; j++)
                        {
                            multiNumCols[i][nrows1 + j] = mdata2.MultiNumericColumns[ind][j];
                        }
                    }
                    if (dic3.ContainsKey(multiNumColNames[i]))
                    {
                        int ind = dic3[multiNumColNames[i]];
                        for (int j = 0; j < nrows3; j++)
                        {
                            multiNumCols[i][nrows2 + j] = mdata3.MultiNumericColumns[ind][j];
                        }
                    }
                }
                string   MatrixName        = "Matrix Name";
                string   MatrixDescription = "Description";
                string[] listnames         = header1.Concat(header2).ToArray();
                string[] listnames3        = listnames.Concat(header3).ToArray();
                // string[][] resultarray = catlistnames.Select(x => x.ToArray()).ToArray();
                //IMPORTANT!!!!! TODO: check if the name of the matrix if changed
                IMatrixData result = PerseusUtils.CreateMatrixData(inputData[0], ex, expColNames.ToList());
                result.NumericColumnNames             = new List <string>(numColNames);
                result.NumericColumnDescriptions      = result.NumericColumnNames;
                result.NumericColumns                 = numCols;
                result.StringColumnNames              = new List <string>(stringColNames);
                result.StringColumns                  = stringCols;
                result.CategoryColumnNames            = new List <string>(catColNames);
                result.CategoryColumnDescriptions     = result.CategoryColumnNames;
                result.CategoryColumns                = catCols;
                result.MultiNumericColumnNames        = new List <string>(multiNumColNames);
                result.MultiNumericColumnDescriptions = result.MultiNumericColumnNames;
                result.MultiNumericColumns            = multiNumCols;
                HashSet <string> taken = new HashSet <string>(result.StringColumnNames);
                result.AddStringColumn(MatrixName, MatrixName, listnames3);
                taken.Add(MatrixName);
                return(result);
            }
            else
            {
                return(resultinput);
            }
        }