Ejemplo n.º 1
0
        /// <summary>
        /// Creates an instance of PaddedDouble from a file in PaddedDouble format.
        /// </summary>
        /// <param name="paddedDoubleFileName">a file in PaddedDouble format</param>
        /// <param name="parallelOptions">A ParallelOptions instance that configures the multithreaded behavior of this operation.</param>
        /// <returns>The created PaddedDouble</returns>
        public static PaddedDouble GetInstance(string paddedDoubleFileName, ParallelOptions parallelOptions)
        {
            PaddedDouble paddedDouble = new PaddedDouble();

            paddedDouble.GetInstanceInternal(paddedDoubleFileName, parallelOptions);
            return(paddedDouble);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Create a PaddedDouble object from a sequence of RowKeyColKeyValue triples.
        /// </summary>
        /// <param name="tripleEnumerable">a sequence of RowKeyColKeyValue</param>
        /// <returns>A PaddedDouble object</returns>
        public static PaddedDouble GetInstanceFromSparse(IEnumerable <RowKeyColKeyValue <string, string, double> > tripleEnumerable)
        {
            PaddedDouble paddedDouble = new PaddedDouble();

            paddedDouble.GetInstanceFromSparseInternal(tripleEnumerable);
            return(paddedDouble);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Create an instance of PaddedDouble from a sparse input file
        /// </summary>
        /// <param name="inputSparsePattern">The sparse input file</param>
        /// <returns>A PaddedDouble</returns>
        public static PaddedDouble GetInstanceFromSparse(string inputSparsePattern)
        {
            PaddedDouble paddedDouble = new PaddedDouble();

            paddedDouble.GetInstanceFromSparseInternal(inputSparsePattern);
            return(paddedDouble);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Create an empty instance of a PaddedDouble file
        /// </summary>
        /// <param name="rowKeySequence">A sequence of row keys. The items will become the RowKeys of the Matrix.</param>
        /// <param name="colKeySequence">A sequence of colKeys. The items will come the ColKeys of the Matrix.</param>
        /// <param name="missingValue">The special value that represents missing</param>
        /// <returns>An empty PaddedDouble instance</returns>
        static public PaddedDouble CreateEmptyInstance(IEnumerable <string> rowKeySequence, IEnumerable <string> colKeySequence, double missingValue)
        {
            Helper.CheckCondition(missingValue.Equals(StaticMissingValue), "For PaddedDouble the missingValue must be '{0}'", StaticMissingValue); //OK to use Equals because double can't be null
            PaddedDouble paddedDouble = new PaddedDouble();

            paddedDouble.InternalCreateEmptyInstance(rowKeySequence, colKeySequence);
            return(paddedDouble);
        }
Ejemplo n.º 5
0
#pragma warning disable 1591
        protected override byte[] ValueOrMissingToByteArray(double value)
#pragma warning restore 1591
        {
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            string s = PaddedDouble.StoreToSparseVal(value);

            byte[] byteArray = encoding.GetBytes(s);
            return(byteArray);
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Converts matrix to a PaddedDouble. Even if the matrix is already an paddedDouble, a new one is created..
        /// </summary>
        /// <param name="matrix">The matrix to convert from</param>
        /// <param name="parallelOptions">A ParallelOptions instance that configures the multithreaded behavior of this operation.</param>
        /// <returns>A paddedDouble version of the matrix</returns>
        public static PaddedDouble ToPaddedDouble(this Matrix <string, string, double> matrix, ParallelOptions parallelOptions)
        {
            var paddedDouble = PaddedDouble.CreateEmptyInstance(matrix.RowKeys, matrix.ColKeys, PaddedDouble.StaticMissingValue);

            //Console.WriteLine("Convert no more than {0} values", matrix.RowCount * matrix.ColCount);
            //CounterWithMessages counterWithMessages = new CounterWithMessages("adding value #{0}", 100000, null);
            Parallel.ForEach(matrix.RowKeyColKeyValues, parallelOptions, triple =>
            {
                //counterWithMessages.Increment();
                paddedDouble[triple.RowKey, triple.ColKey] = triple.Value;
            });
            return(paddedDouble);
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Create an instance of PaddedDouble from a sparse input file
 /// </summary>
 /// <param name="inputSparsePattern">The sparse input file</param>
 /// <param name="paddedDouble">The PaddedDouble matrix created</param>
 /// <returns>true if the file parses as PaddedDouble; otherwise, false</returns>
 public static bool TryGetInstanceFromSparse(string inputSparsePattern, out PaddedDouble paddedDouble)
 {
     try
     {
         paddedDouble = GetInstanceFromSparse(inputSparsePattern);
         return(true);
     }
     catch
     {
         paddedDouble = null;
         return(false);
     }
 }
Ejemplo n.º 8
0
        //Similar code elsewhere, but hard to move static code to common location
        //Would be nice if could work from, say Matrix<string,string,int>

        /// <summary>
        /// Write in PaddedDouble file format to a TextWriter
        /// </summary>
        /// <param name="matrix">The matrix to write</param>
        /// <param name="textWriter">The TextWriter to write to</param>
        /// <param name="parallelOptions">A ParallelOptions instance that configures the multithreaded behavior of this operation.</param>
        public static void WritePaddedDouble(this Matrix <string, string, double> matrix, TextWriter textWriter, ParallelOptions parallelOptions)
        {
            textWriter.WriteLine("var\t{0}", matrix.ColKeys.StringJoin("\t"));
            foreach (string rowKey in matrix.RowKeys)
            {
                textWriter.Write(rowKey);
                textWriter.Write("\t");
                int rowIndex = matrix.IndexOfRowKey[rowKey];

                List <double> storeList = new List <double>(matrix.ColCount);
                for (int colIndex = 0; colIndex < matrix.ColCount; ++colIndex)
                {
                    double store;
                    if (!matrix.TryGetValue(rowIndex, colIndex, out store))
                    {
                        store = PaddedDouble.StaticStoreMissingValue;
                    }
                    storeList.Add(store);
                }
                Helper.CheckCondition(storeList.Count == matrix.ColCount, Properties.Resource.ExpectedOneValueForEveryColKey);
                string s = PaddedDouble.StoreListToString(storeList, matrix.ColCount);
                textWriter.WriteLine(s);
            }
        }