Click or drag to resize

CsvDoubleMatrixSerializerDeserialize Method (TextReader)

Deserializes as DoubleMatrix the CSV document contained by the specified TextReader.

Namespace:  Novacta.Analytics
Assembly:  Novacta.Analytics (in Novacta.Analytics.dll) Version: 2.0.0
Syntax
public static DoubleMatrix Deserialize(
	TextReader reader
)

Parameters

reader
Type: System.IOTextReader
The TextReader that contains the CSV document to deserialize.

Return Value

Type: DoubleMatrix
The DoubleMatrix being deserialized.
Exceptions
ExceptionCondition
ArgumentNullExceptionreader is null.
InvalidOperationException An error occurred during deserialization. The original exception is available using the InnerException property.
Remarks

This method reads CSV files created by method Serialize(TextWriter, DoubleMatrix) or one of its overloaded versions.

Examples

In the following example, a matrix instance is serialized by writing a CSV document to a stream. Hence the matrix is deserialized by reading such stream.

Serialization and deserialization of a matrix using a CSV document
using System;
using System.IO;

namespace Novacta.Analytics.CodeExamples
{
    public class CsvSerializeExample1  
    {
        public void Main()
        {
            // Create a matrix.
            var matrix = DoubleMatrix.Sparse(3, 2, capacity: 2);
            matrix[0, 1] = Double.NaN;
            matrix[2, 0] = -2.2;

            // Add a name to the matrix.
            matrix.Name = "My sparse matrix";

            // Add names to rows and columns.
            matrix.SetRowName(0, "row0");
            matrix.SetRowName(1, "row1");
            matrix.SetRowName(2, "row2");
            matrix.SetColumnName(0, "column0");
            matrix.SetColumnName(1, "column1");

            Console.WriteLine("The data matrix:");
            Console.WriteLine(matrix);
            Console.WriteLine();

            // Create a stream to serialize the matrix.
            MemoryStream stream = new();
            StreamWriter writer = new(stream);

            // Serialize the matrix.
            CsvDoubleMatrixSerializer.Serialize(writer, matrix);

            // Read the CSV representation of the matrix from the stream.
            stream.Position = 0;
            StreamReader reader = new(stream);
            string line;
            Console.WriteLine("CSV representation of the matrix:");
            while ((line = reader.ReadLine()) != null) {
                Console.WriteLine(line);
            }
            Console.WriteLine();

            // Deserialize the matrix from the stream.
            stream.Position = 0;
            var deserializedMatrix = 
                CsvDoubleMatrixSerializer.Deserialize(reader);

            // Show the deserialized matrix.
            Console.WriteLine("Deserialized matrix:");
            Console.WriteLine(deserializedMatrix);

            // Serialize is overloaded to accept data as a read-only matrix:
            // serialize a read-only wrapper of the matrix.
            stream.Position = 0;
            CsvDoubleMatrixSerializer.Serialize(writer, matrix.AsReadOnly());

            // Deserialize again the matrix.
            stream.Position = 0;
            var deserializedReadOnlyMatrix = 
                CsvDoubleMatrixSerializer.Deserialize(reader);

            // Show the deserialized matrix.
            Console.WriteLine("Deserialized matrix after serialization of a read-only matrix:");
            Console.WriteLine(deserializedReadOnlyMatrix);
        }
    }
}

// Executing method Main() produces the following output:
// 
// The data matrix:
//                  [column0]        [column1]        
// [row0]           0                NaN              
// [row1]           0                0                
// [row2]           -2.2             0                
// 
// 
// 
// CSV representation of the matrix:
// Sparse|Double,3,2,My sparse matrix,2
// 0,1,NaN
// 2,0,-2.2
// 0,column0,1,column1
// 0,row0,1,row1,2,row2
// 
// Deserialized matrix:
//                  [column0]        [column1]        
// [row0]           0                NaN              
// [row1]           0                0                
// [row2]           -2.2             0                
// 
// 
// Deserialized matrix after serialization of a read-only matrix:
//                  [column0]        [column1]        
// [row0]           0                NaN              
// [row1]           0                0                
// [row2]           -2.2             0                
// 

See Also