internal static bool TryGetInstance(string filename, double missingValue, ParallelOptions parallelOptions, out Matrix <string, string, double> matrix) { matrix = null; Matrix <string, string, char> sscMatrix; if (!DenseAnsi.TryGetInstance(filename, '?', parallelOptions, out sscMatrix)) { return(false); } matrix = sscMatrix.ConvertValueView(ValueConverter.CharToDouble, missingValue); return(true); }
/// <summary> /// Converts matrix to a DenseAnsi. Even if the matrix is already an denseAnsi, 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 denseAnsi version of the matrix</returns> public static DenseAnsi ToDenseAnsi(this Matrix <string, string, char> matrix, ParallelOptions parallelOptions) { var denseAnsi = DenseAnsi.CreateEmptyInstance(matrix.RowKeys, matrix.ColKeys, DenseAnsi.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(); denseAnsi[triple.RowKey, triple.ColKey] = triple.Value; }); return(denseAnsi); }
/// <summary> /// Creates a DenseAnsi from file(s) in sparse format. /// </summary> /// <param name="inputSparsePattern">The name of a file (or a pattern matching several files). The file(s) are in sparse format.</param> /// <param name="denseAnsi">The new DenseAnsi (or null, if the method fails).</param> /// <returns>true if the file(s) is read and the DenseAnsi is created; otherwise, false </returns> public static bool TryGetInstanceFromSparse(string inputSparsePattern, out DenseAnsi denseAnsi) { try { denseAnsi = GetInstanceFromSparse(inputSparsePattern); return(true); } catch { denseAnsi = null; return(false); } }
/// <summary> /// Tries to read a file in DenseAnsi format and then creates a DenseAnsi class with values of the desired type. /// </summary> /// <typeparam name="TValue">The type of values wanted, for example, double</typeparam> /// <param name="filename">The dense ansi formatted file to read from.</param> /// <param name="missingValue">The special value that represents missing</param> /// <param name="parallelOptions">A ParallelOptions instance that configures the multithreaded behavior of this operation.</param> /// <param name="matrix">A matrix that internally stores values in a DenseAnsi object.</param> /// <returns>true, if the file can be parsed; false, otherwise.</returns> public static bool TryParseDenseAnsiFormatAsGenericMatrix <TValue>(string filename, TValue missingValue, ParallelOptions parallelOptions, out Matrix <string, string, TValue> matrix) { matrix = null; Matrix <string, string, char> sscMatrix; if (!DenseAnsi.TryGetInstance(filename, '?', parallelOptions, out sscMatrix)) { return(false); } matrix = sscMatrix.ConvertValueView(new CharToGenericConverter <TValue>(), missingValue); //matrix = sscMatrix.ConvertValueView(c => Parser.Parse<TValue>(c.ToString()), val => SpecialFunctions.FirstAndOnly<char>(val.ToString()), missingValue); return(true); }
/// <summary> /// Writes a matrix with char values in DenseAnsi format to a textWriter. Does not need to convert to DenseAnsi format. /// </summary> /// <param name="matrix">The matrix to write</param> /// <param name="textWriter">The stream to write to</param> /// <param name="parallelOptions">A ParallelOptions instance that configures the multithreaded behavior of this operation.</param> public static void WriteDenseAnsi(this Matrix <string, string, char> matrix, TextWriter textWriter, ParallelOptions parallelOptions, bool verbose = false) { DenseAnsi denseAnsi = matrix as DenseAnsi; if (null != denseAnsi) { denseAnsi.WriteDenseAnsi <char>(textWriter, parallelOptions, verbose); return; } var counterWithMessages = new CounterWithMessages("writeDenseAnsi {0} {1} ", 1000, matrix.RowCount, !verbose); var lineQuery = from rowKey in matrix.RowKeys .AsParallel().AsOrdered().WithDegreeOfParallelism(parallelOptions.MaxDegreeOfParallelism) select CreateLine(matrix, rowKey, counterWithMessages); textWriter.WriteLine("var\t{0}", matrix.ColKeys.StringJoin("\t")); foreach (string line in lineQuery) { textWriter.WriteLine(line); } }