Ejemplo n.º 1
0
        private static Matrix ReadMatrixFromMatrixFile(BufferedFileReader reader, int networkId)
        {
            networkId = reader.JumpToNetworkId(networkId, true);
            if (networkId == -1)
            {
                throw new Exception("Network ID does not exist!");
            }
            reader.ReadLine(); // Skip first line with the network id

            string[] colLabels = reader.ReadLine().Split(',');
            //Yushan
            List <string> rowLabels = new List <string>();
            //string[] colLabels = tempLabels.Split(',');
            int rows = reader.CountLines(networkId) - 2; // Subtract off header columns
            //int rows = reader.CountLinesAlt(tempIdStr) - 2;
            int cols = colLabels.Length - 1;

            Matrix matrix = new Matrix(rows, cols);

            matrix.NetworkId    = networkId;
            matrix.NetworkIdStr = reader.GetNetworkRealId(networkId);
            matrix.ColLabels.SetLabels(colLabels);

            for (int row = 0; row < rows; ++row)
            {
                string temp = reader.ReadLine();
                if (temp == null)
                {
                    break;
                }
                //string[] parts = reader.ReadLine().Split(',');
                string[] parts = temp.Split(',');

                if (parts.Length > cols + 1) // one extra for header
                {
                    throw new FileLoadException("Matrix file has too many entries for network id: " + matrix.NetworkIdStr + ", row " + parts[0]);
                }

                if (parts.Length == 0)
                {
                    throw new FileLoadException("Matrix file has no entries for network id: " + matrix.NetworkIdStr);
                }

                //matrix.RowLabels[row] = parts[0];
                rowLabels.Add(parts[0]);

                for (int i = 1; i < parts.Length; ++i)
                {
                    matrix[row, i - 1] = ExtractDouble(parts[i]);
                }
            }
            matrix.RowLabels.SetLabels(rowLabels);
            reader.closeStream();
            reader.Dispose(); /* Change made 12/3/2010 - PM */

            return(matrix);
        }
Ejemplo n.º 2
0
        public static List <Matrix> ReadMatrixFromMultipleDyadicFile(string filename, int networkId)
        {
            BufferedFileReader reader = BufferedFileTable.GetFile(filename);

            lock (reader)
            {
                networkId = reader.JumpToNetworkId(networkId, true);

                Dictionary <string, int> labels = reader.GetDyadicLabels(networkId);
                int rows = labels.Count;

                // initialize the matrices
                List <Matrix> multipleMatrices = new List <Matrix>();

                string[] topLabels = reader.TopLine.Split(',');
                //string[] labels = new string[reader.CountVarsInDyadicFile()];

                for (int var = 0; var < reader.CountVarsInDyadicFile(); var++)
                {
                    Matrix matrix = new Matrix(rows, rows);
                    matrix.NetworkId = networkId;
                    matrix.Name      = topLabels[var + 3];
                    matrix.RowLabels.SetLabels(labels.Keys);
                    matrix.ColLabels.SetLabels(labels.Keys);

                    multipleMatrices.Add(matrix);
                }

                int totalLines = reader.CountLines(networkId);

                for (int i = 0; i < totalLines; ++i)
                {
                    string   s     = reader.ReadLine();
                    string[] parts = s.Split(',');

                    //if (parts.Length < 3 + reader.CountVarsInDyadicFile())
                    //  throw new FileLoadException("Missing value for line: " + s);

                    for (int var = 0; var < reader.CountVarsInDyadicFile(); var++)
                    {
                        multipleMatrices[var][labels[parts[1]], labels[parts[2]]] = ExtractDouble(parts[3 + var]);
                    }
                }

                /*
                 * if (networkId < 1000)
                 *  networkId = int.Parse("1" + networkId);
                 * else
                 *  networkId = int.Parse("2" + networkId);
                 */


                return(multipleMatrices);
            }
        }
Ejemplo n.º 3
0
        private static Vector ReadVectorFromVectorFile(BufferedFileReader reader, int networkId)
        {
            networkId = reader.JumpToNetworkId(networkId, true);

            Vector v = new Vector(reader.CountLines(networkId));

            v.NetworkId = networkId;
            for (int i = 0; i < v.Size; ++i)
            {
                string   s     = reader.ReadLine();
                string[] parts = s.Split(',');
                v[i]        = ExtractDouble(parts[parts.Length - 1]);
                v.Labels[i] = parts[parts.Length - 2];
            }

            return(v);
        }
Ejemplo n.º 4
0
        private static Matrix ReadMatrixFromVectorFile(BufferedFileReader reader, int networkId)
        {
            networkId = reader.JumpToNetworkId(networkId, true);

            Matrix m = new Matrix(reader.CountLines(networkId));

            m.NetworkId = networkId;
            for (int i = 0; i < m.Rows; ++i)
            {
                string   s     = reader.ReadLine();
                string[] parts = s.Split(',');
                m[i, i]        = ExtractDouble(parts[parts.Length - 1]);
                m.RowLabels[i] = m.ColLabels[i] = parts[parts.Length - 2];
            }

            return(m);
        }
Ejemplo n.º 5
0
        private static Matrix ReadMatrixFromDyadicFile(BufferedFileReader reader, int networkId, int dyadicVariable)
        {
            networkId = reader.JumpToNetworkId(networkId, true);

            Dictionary <string, int> labels = reader.GetDyadicLabels(networkId);
            int rows = labels.Count;

            Matrix matrix = new Matrix(rows, rows);

            //matrix.NetworkId = networkId;
            //matrix.NetworkId = int.Parse("1" + matrix.NetworkId); // new
            matrix.RowLabels.SetLabels(labels.Keys);
            matrix.ColLabels.SetLabels(labels.Keys);

            int totalLines = reader.CountLines(networkId);

            for (int i = 0; i < totalLines; ++i)
            {
                string s = reader.ReadLine();

                string[] parts = s.Split(',');

                if (parts.Length < 3 + dyadicVariable)
                {
                    throw new FileLoadException("Missing value for line: " + s);
                }

                matrix[labels[parts[1]], labels[parts[2]]] = ExtractDouble(parts[3 + dyadicVariable]);
            }

            /*
             * if (networkId < 1000)
             *  networkId = int.Parse("1" + networkId);
             * else
             *  networkId = int.Parse("2" + networkId);
             */
            matrix.NetworkId = networkId;


            return(matrix);
        }
Ejemplo n.º 6
0
        private static Vector ReadVectorFromVectorFile(BufferedFileReader reader, int networkId)
        {
            networkId = reader.JumpToNetworkId(networkId, true);

            Vector v = new Vector(reader.CountLines(networkId));
            v.NetworkId = networkId;
            for (int i = 0; i < v.Size; ++i)
            {
                string s = reader.ReadLine();
                string[] parts = s.Split(',');
                v[i] = ExtractDouble(parts[parts.Length - 1]);
                v.Labels[i] = parts[parts.Length - 2];
            }

            return v;
        }
Ejemplo n.º 7
0
        private static Matrix ReadMatrixFromVectorFile(BufferedFileReader reader, int networkId)
        {
            networkId = reader.JumpToNetworkId(networkId, true);

            Matrix m = new Matrix(reader.CountLines(networkId));
            m.NetworkId = networkId;
            for (int i = 0; i < m.Rows; ++i)
            {
                string s = reader.ReadLine();
                string[] parts = s.Split(',');
                m[i, i] = ExtractDouble(parts[parts.Length - 1]);
                m.RowLabels[i] = m.ColLabels[i] = parts[parts.Length - 2];
            }

            return m;
        }
Ejemplo n.º 8
0
        private static Matrix ReadMatrixFromMatrixFile(BufferedFileReader reader, int networkId)
        {
            networkId = reader.JumpToNetworkId(networkId, true);
            reader.ReadLine(); // Skip first line with the network id
            /*
            string tempLabels = reader.ReadLine();
            if (tempLabels == null)
                return null;
            */

            string[] colLabels = reader.ReadLine().Split(',');
            //string[] colLabels = tempLabels.Split(',');
            int rows = reader.CountLines(networkId) - 2; // Subtract off header columns
            int cols = colLabels.Length - 1;

            Matrix matrix = new Matrix(rows, cols);
            matrix.NetworkId = networkId;
            matrix.ColLabels.SetLabels(colLabels);

            for (int row = 0; row < rows; ++row)
            {
                string temp = reader.ReadLine();
                if (temp == null)
                    break;
                //string[] parts = reader.ReadLine().Split(',');
                string[] parts = temp.Split(',');

                if (parts.Length > cols + 1) // one extra for header
                    throw new FileLoadException("Matrix file has too many entries for network id: " + networkId.ToString() + ", row " + parts[0]);

                if (parts.Length == 0)
                    throw new FileLoadException("Matrix file has no entries for network id: " + networkId.ToString());

                matrix.RowLabels[row] = parts[0];

                for (int i = 1; i < parts.Length; ++i)
                    matrix[row, i - 1] = ExtractDouble(parts[i]);
            }

            reader.closeStream();
            reader.Dispose(); /* Change made 12/3/2010 - PM */

            return matrix;
        }
Ejemplo n.º 9
0
        private static Matrix ReadMatrixFromDyadicFile(BufferedFileReader reader, int networkId, int dyadicVariable)
        {
            networkId = reader.JumpToNetworkId(networkId, true);

            Dictionary<string, int> labels = reader.GetDyadicLabels(networkId);
            int rows = labels.Count;

            Matrix matrix = new Matrix(rows, rows);
            //matrix.NetworkId = networkId;
            //matrix.NetworkId = int.Parse("1" + matrix.NetworkId); // new
            matrix.RowLabels.SetLabels(labels.Keys);
            matrix.ColLabels.SetLabels(labels.Keys);

            int totalLines = reader.CountLines(networkId);
            for (int i = 0; i < totalLines; ++i)
            {
                string s = reader.ReadLine();

                string[] parts = s.Split(',');

                if (parts.Length < 3 + dyadicVariable)
                    throw new FileLoadException("Missing value for line: " + s);

                matrix[labels[parts[1]], labels[parts[2]]] = ExtractDouble(parts[3 + dyadicVariable]);
            }
            /*
            if (networkId < 1000)
                networkId = int.Parse("1" + networkId);
            else
                networkId = int.Parse("2" + networkId);
            */
            matrix.NetworkId = networkId;

            return matrix;
        }
Ejemplo n.º 10
0
        public static List <Matrix> ReadMatrixFromMultipleDyadicFile(string filename, int networkId)
        {
            BufferedFileReader reader = BufferedFileTable.GetFile(filename);

            lock (reader)
            {
                networkId = reader.JumpToNetworkId(networkId, true);

                //Dictionary<string, int> labels = reader.GetDyadicLabels(networkId);
                //int rows = labels.Count;

                // initialize the matrices
                List <Matrix> multipleMatrices = new List <Matrix>();
                List <string> rowLabels        = new List <string>();
                List <string> colLabels        = new List <string>();
                //string[] topLabels = reader.TopLine.Split(',');
                //string[] labels = new string[reader.CountVarsInDyadicFile()];
                int           varCnt         = reader.CountVarsInDyadicFile();
                List <double> flatMatrixList = new List <double>();
                //for (int var = 0; var < reader.CountVarsInDyadicFile(); var++)
                //{
                //    Matrix matrix = new Matrix(rows, rows);
                //    matrix.NetworkId = networkId;
                //    matrix.Name = topLabels[var + 3];
                //    matrix.RowLabels.SetLabels(labels.Keys);
                //    matrix.ColLabels.SetLabels(labels.Keys);

                //    multipleMatrices.Add(matrix);
                //}

                int totalLines = reader.CountLines(networkId);

                for (int i = 0; i < totalLines; ++i)
                {
                    string   s     = reader.ReadLine();
                    string[] parts = s.Split(',');
                    if (!rowLabels.Contains(parts[1]))
                    {
                        rowLabels.Add(parts[1]);
                    }
                    if (!colLabels.Contains(parts[2]))
                    {
                        colLabels.Add(parts[2]);
                    }
                    //if (parts.Length < 3 + reader.CountVarsInDyadicFile())
                    //  throw new FileLoadException("Missing value for line: " + s);

                    for (int var = 0; var < varCnt; var++)
                    {
                        //multipleMatrices[var][labels[parts[1]], labels[parts[2]]] = ExtractDouble(parts[3 + var]);
                        flatMatrixList.Add(ExtractDouble(parts[3 + var]));
                    }
                }

                int rows = rowLabels.Count;
                int cols = colLabels.Count;
                for (int var = 0; var < varCnt; var++)
                {
                    Matrix matrix = new Matrix(rows, cols);
                    matrix.RowLabels.SetLabels(rowLabels);
                    matrix.ColLabels.SetLabels(colLabels);
                    matrix.NetworkId    = networkId;
                    matrix.NetworkIdStr = reader.GetNetworkRealId(networkId);
                    multipleMatrices.Add(matrix);
                }

                for (int i = 0; i < rows; i++)
                {
                    for (int j = 0; j < cols; j++)
                    {
                        for (int var = 0; var < varCnt; var++)
                        {
                            multipleMatrices[var][i, j] = flatMatrixList[i * cols * varCnt + j * varCnt + var];
                        }
                    }
                }

                /*
                 * if (networkId < 1000)
                 *  networkId = int.Parse("1" + networkId);
                 * else
                 *  networkId = int.Parse("2" + networkId);
                 */


                return(multipleMatrices);
            }
        }
Ejemplo n.º 11
0
        private static Matrix ReadMatrixFromDyadicFile(BufferedFileReader reader, int networkId, int dyadicVariable)
        {
            networkId = reader.JumpToNetworkId(networkId, true);
            // Yushan
            List <string> rowLabels  = new List <string>();
            List <string> colLabels  = new List <string>();
            List <double> flatMatrix = new List <double>();

            //Dictionary<string, int> labels = reader.GetDyadicLabels(networkId);
            //int rows = labels.Count;

            //Matrix matrix = new Matrix(rows, rows);

            //matrix.RowLabels.SetLabels(labels.Keys);
            //matrix.ColLabels.SetLabels(labels.Keys);

            int totalLines = reader.CountLines(networkId);

            //Console.WriteLine("Total Lines: {0}", totalLines);
            for (int i = 0; i < totalLines; ++i)
            {
                string s = reader.ReadLine();

                string[] parts = s.Split(',');

                if (parts.Length < 3 + dyadicVariable)
                {
                    throw new FileLoadException("Missing value for line: " + s);
                }

                // Yushan
                if (!rowLabels.Contains(parts[1]))
                {
                    rowLabels.Add(parts[1]);
                }
                if (!colLabels.Contains(parts[2]))
                {
                    colLabels.Add(parts[2]);
                }
                flatMatrix.Add(ExtractDouble(parts[3 + dyadicVariable]));
                //matrix[labels[parts[1]], labels[parts[2]]] = ExtractDouble(parts[3 + dyadicVariable]);
            }

            /*
             * if (networkId < 1000)
             *  networkId = int.Parse("1" + networkId);
             * else
             *  networkId = int.Parse("2" + networkId);
             */
            int    rows   = rowLabels.Count;
            int    cols   = colLabels.Count;
            Matrix matrix = new Matrix(rows, cols);

            matrix.NetworkId    = networkId;
            matrix.NetworkIdStr = reader.GetNetworkRealId(networkId);
            matrix.RowLabels.SetLabels(rowLabels);
            matrix.ColLabels.SetLabels(colLabels);
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    matrix[i, j] = flatMatrix[i * cols + j];
                }
            }

            return(matrix);
        }