示例#1
0
        public static Matrix ReadMatrixFromFile(string filename, int networkId, int dyadicVariable)
        {
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }

            BufferedFileReader reader = BufferedFileTable.GetFile(filename);

            lock (reader)
            {
                switch (reader.FileType)
                {
                case BufferedFileReader.Type.Matrix:
                    return(ReadMatrixFromMatrixFile(reader, networkId));

                case BufferedFileReader.Type.Dyadic:
                    return(ReadMatrixFromDyadicFile(reader, networkId, dyadicVariable));

                case BufferedFileReader.Type.Vector:
                    return(ReadMatrixFromVectorFile(reader, networkId));

                default:
                    throw new FileLoadException("Invalid file type.");
                }
            }
        }
示例#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);
            }
        }
示例#3
0
        public static List <Matrix> ReadMatrixListFromFile(string filename, int dyadicVariable)
        {
            if (filename == null)
            {
                throw new ArgumentNullException("filename");
            }

            BufferedFileReader reader = BufferedFileTable.GetFile(filename);
            int           numIds      = reader.CountNetworkIds();
            List <Matrix> mlist       = new List <Matrix>();
            Matrix        temp;

            lock (reader)
            {
                switch (reader.FileType)
                {
                case BufferedFileReader.Type.Matrix:
                {
                    for (int i = 0; i < numIds; i++)
                    {
                        temp = ReadMatrixFromMatrixFile(reader, i);
                        mlist.Add(temp);
                    }
                    return(mlist);
                }

                case BufferedFileReader.Type.Dyadic:
                {
                    for (int i = 0; i < numIds; i++)
                    {
                        temp = ReadMatrixFromDyadicFile(reader, i, dyadicVariable);
                        mlist.Add(temp);
                    }
                    return(mlist);
                }

                case BufferedFileReader.Type.Vector:
                {
                    for (int i = 0; i < numIds; i++)
                    {
                        temp = ReadMatrixFromVectorFile(reader, i);
                        mlist.Add(temp);
                    }
                    return(mlist);
                }

                default:
                    throw new FileLoadException("Invalid file type.");
                }
            }
        }
示例#4
0
        public static Matrix createTransposeMatrix(string filename, int netID) // working here - working for integers and random nodes
        {
            Dictionary <int, double> d = new Dictionary <int, double>();

            BufferedFileReader reader = BufferedFileTable.GetFile(filename);

            reader.GoToLine(0);

            while (!reader.EndOfStream) //reading file
            {
                string line = reader.ReadLine();

                if (line == null)
                {
                    break;
                }

                string[] line_parts = line.Split(',');

                if (Int64.Parse(line_parts[0]) == netID)
                {
                    d.Add(ExtractNode(line_parts[1]), ExtractDouble(line_parts[2])); // need to work here for string case
                }
            }

            Matrix m = new Matrix(d.Count, d.Count);  //transpose matrix


            //calculation
            for (int i = 0; i < d.Count; i++)
            {
                for (int j = 0; j < d.Count; j++)
                {
                    m[i, j] = d[i + 1] * d[j + 1];
                }
            }

            return(m);
        }
示例#5
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);
            }
        }