From 1ef8575621d59923c8d45b4ce841857beea396ea Mon Sep 17 00:00:00 2001 From: Christoph Ruegg Date: Sat, 14 Jun 2014 22:51:35 +0200 Subject: [PATCH] Data: refactor MatlabMatrixReader to simpler static design --- build.fsx | 17 +- src/Data/Matlab/MatlabReader.cs | 171 ++---------------- src/DataUnitTests/Matlab/MatlabReaderTests.cs | 72 +++----- src/DataUnitTests/Matlab/MatlabWriterTests.cs | 12 +- 4 files changed, 52 insertions(+), 220 deletions(-) diff --git a/build.fsx b/build.fsx index 76d9ab67..0deb58fb 100644 --- a/build.fsx +++ b/build.fsx @@ -122,14 +122,12 @@ let nativePackageVersion = nativeRelease.NugetVersion let nativeReleaseNotes = nativeRelease.Notes |> List.map (fun l -> l.Replace("*","").Replace("`","")) |> toLines trace (sprintf " Math.NET Numerics Native Providers v%s" nativePackageVersion) -let nativeSummary = "Intel MKL native libraries for Math.NET Numerics. Requires an Intel MKL license if redistributed." - let nativeMKLWin32Pack = { Id = "MathNet.Numerics.MKL.Win-x86" Version = nativePackageVersion Title = "Math.NET Numerics - MKL Native Libraries (Windows 32-bit)" - Summary = nativeSummary - Description = nativeSummary + Summary = "" + Description = "Intel MKL native libraries for Math.NET Numerics. Requires an Intel MKL license if redistributed." ReleaseNotes = nativeReleaseNotes Tags = "math numeric statistics probability integration interpolation linear algebra matrix fft native mkl" Authors = [ "Christoph Ruegg"; "Marcus Cuda"; "Jurgen Van Gael" ] @@ -154,15 +152,12 @@ let dataReleaseNotes = dataRelease.Notes |> List.map (fun l -> l.Replace("*","") trace (sprintf " Math.NET Numerics Data Extensions v%s" dataPackageVersion) trace "" -let dataTextSummary = "Text Data Input/Output Extensions for Math.NET Numerics, the numerical foundation of the Math.NET project, aiming to provide methods and algorithms for numerical computations in science, engineering and every day use." -let dataMatlabSummary = "MathWorks MATLAB Data Input/Output Extensions for Math.NET Numerics, the numerical foundation of the Math.NET project, aiming to provide methods and algorithms for numerical computations in science, engineering and every day use." - let dataTextPack = { Id = "MathNet.Numerics.Data.Text" Version = dataPackageVersion Title = "Math.NET Numerics - Text Data I/O Extensions" - Summary = dataTextSummary - Description = dataTextSummary + Summary = "" + Description = "Text Data Input/Output Extensions for Math.NET Numerics, the numerical foundation of the Math.NET project, aiming to provide methods and algorithms for numerical computations in science, engineering and every day use." ReleaseNotes = dataReleaseNotes Tags = "math numeric data text csv tsv json xml" Authors = [ "Christoph Ruegg"; "Marcus Cuda" ] @@ -174,8 +169,8 @@ let dataMatlabPack = { Id = "MathNet.Numerics.Data.Matlab" Version = dataPackageVersion Title = "Math.NET Numerics - MATLAB Data I/O Extensions" - Summary = dataMatlabSummary - Description = dataMatlabSummary + Summary = "" + Description = "MathWorks MATLAB Data Input/Output Extensions for Math.NET Numerics, the numerical foundation of the Math.NET project, aiming to provide methods and algorithms for numerical computations in science, engineering and every day use." ReleaseNotes = dataReleaseNotes Tags = "math numeric data matlab" Authors = [ "Christoph Ruegg"; "Marcus Cuda" ] diff --git a/src/Data/Matlab/MatlabReader.cs b/src/Data/Matlab/MatlabReader.cs index dbaa3e25..5ebafa5d 100644 --- a/src/Data/Matlab/MatlabReader.cs +++ b/src/Data/Matlab/MatlabReader.cs @@ -33,60 +33,17 @@ using System.Collections.Generic; using System.IO; using System.Linq; using MathNet.Numerics.LinearAlgebra; -using MathNet.Numerics.Properties; namespace MathNet.Numerics.Data.Matlab { /// /// Creates matrices from Matlab files. /// - /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. - public class MatlabMatrixReader where TDataType : struct, IEquatable, IFormattable + public static class MatlabMatrixReader { - /// - /// The name of the file to read from. - /// - private readonly string _filename; - - /// - /// The stream to read from if we are not reading from a file directly. - /// - private readonly Stream _stream; - - /// - /// Initializes a new instance of the class. - /// - /// Name of the file to read matrices from. - public MatlabMatrixReader(string filename) - { - if (string.IsNullOrEmpty(filename)) - { - throw new ArgumentException(Resources.StringNullOrEmpty, "filename"); - } - - if (!File.Exists(filename)) - { - throw new FileNotFoundException(Resources.FileDoesNotExist, "filename"); - } - - _filename = filename; - } - - /// - /// Initializes a new instance of the class. - /// - /// The stream to reader matrices from. - public MatlabMatrixReader(Stream stream) - { - if (stream == null) - { - throw new ArgumentNullException("stream"); - } - - _stream = stream; - } - - public static Matrix ReadMatrix(Stream stream, string matrixName = null) + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static Matrix ReadMatrix(Stream stream, string matrixName = null) + where TDataType : struct, IEquatable, IFormattable { var names = string.IsNullOrEmpty(matrixName) ? new string[] { } : new[] { matrixName }; var parser = new MatlabParser(stream, names); @@ -103,125 +60,33 @@ namespace MathNet.Numerics.Data.Matlab return file.Matrices[matrixName]; } - public static Matrix ReadMatrix(string filePath, string matrixName = null) + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static Matrix ReadMatrix(string filePath, string matrixName = null) + where TDataType : struct, IEquatable, IFormattable { using (var stream = File.OpenRead(filePath)) { - return ReadMatrix(stream, matrixName); + return ReadMatrix(stream, matrixName); } } - public static Dictionary> ReadMatrices(Stream stream, params string[] matrixNames) - { - var reader = new MatlabMatrixReader(stream); - return reader.ReadMatrices(matrixNames); - } - - public static Dictionary> ReadMatrices(string filePath, params string[] matrixNames) + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static Dictionary> ReadMatrices(Stream stream, params string[] matrixNames) + where TDataType : struct, IEquatable, IFormattable { - using (var stream = File.OpenRead(filePath)) - { - return ReadMatrices(stream, matrixNames); - } - } - - /// - /// Reads the first matrix from the file or stream. - /// - /// - /// A sparse or dense matrix depending on how the matrix - /// is defined in the Matlab file. - /// - public Matrix ReadMatrix() - { - return ReadMatrix(null); - } - - /// - /// Reads the named matrix from the file or stream. - /// - /// The name of the matrix to read. - /// - /// A sparse or dense matrix depending on how the matrix - /// is defined in the Matlab file. - /// is returned if a matrix with the requests name doesn't exist. - /// - public Matrix ReadMatrix(string matrixName) - { - Stream stream; - if (_filename == null) - { - stream = _stream; - _stream.Seek(0, SeekOrigin.Begin); - } - else - { - stream = new FileStream(_filename, FileMode.Open, FileAccess.Read); - } - - var names = string.IsNullOrEmpty(matrixName) ? new string[] { } : new[] { matrixName }; - var parser = new MatlabParser(stream, names); + var parser = new MatlabParser(stream, matrixNames); var file = parser.Parse(); - - Matrix matrix = null; - if (string.IsNullOrEmpty(matrixName)) - { - matrix = file.FirstMatrix; - } - else if (file.Matrices.ContainsKey(matrixName)) - { - matrix = file.Matrices[matrixName]; - } - - if (_filename != null) - { - stream.Close(); - stream.Dispose(); - } - - return matrix; - } - - /// - /// Reads all matrices from the file or stream. - /// - /// All matrices from the file or stream. The key to the - /// is the matrix's name. - public Dictionary> ReadMatrices() - { - return ReadMatrices(new string[] { }); + return file.Matrices.ToDictionary(matrix => matrix.Key, matrix => matrix.Value); } - /// - /// Reads the named matrices from the file or stream. - /// - /// The names of the matrices to retrieve. - /// - /// The named matrices from the file or stream. The key to the - /// is the matrix's name. - public Dictionary> ReadMatrices(IEnumerable names) + /// The data type of the Matrix. It can be either: double, float, Complex, or Complex32. + public static Dictionary> ReadMatrices(string filePath, params string[] matrixNames) + where TDataType : struct, IEquatable, IFormattable { - Stream stream; - if (_filename == null) - { - stream = _stream; - _stream.Seek(0, SeekOrigin.Begin); - } - else - { - stream = new BufferedStream(new FileStream(_filename, FileMode.Open, FileAccess.Read)); - } - - var parser = new MatlabParser(stream, names); - var file = parser.Parse(); - - if (_filename != null) + using (var stream = File.OpenRead(filePath)) { - stream.Close(); - stream.Dispose(); + return ReadMatrices(stream, matrixNames); } - - return file.Matrices.ToDictionary(matrix => matrix.Key, matrix => matrix.Value); } } } diff --git a/src/DataUnitTests/Matlab/MatlabReaderTests.cs b/src/DataUnitTests/Matlab/MatlabReaderTests.cs index ccfab689..a0501ccb 100644 --- a/src/DataUnitTests/Matlab/MatlabReaderTests.cs +++ b/src/DataUnitTests/Matlab/MatlabReaderTests.cs @@ -42,8 +42,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadAllMatrices() { - var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); - var matrices = dmr.ReadMatrices(); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/collection.mat"); Assert.AreEqual(30, matrices.Count); foreach (var matrix in matrices) { @@ -57,8 +56,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadFirstMatrix() { - var dmr = new MatlabMatrixReader("./data/Matlab/A.mat"); - var matrix = dmr.ReadMatrix(); + var matrix = MatlabMatrixReader.ReadMatrix("./data/Matlab/A.mat"); Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(typeof(LinearAlgebra.Double.DenseMatrix), matrix.GetType()); @@ -71,8 +69,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadNamedMatrices() { - var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); - var matrices = dmr.ReadMatrices(new[] { "Ad", "Au64" }); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/collection.mat", "Ad", "Au64"); Assert.AreEqual(2, matrices.Count); foreach (var matrix in matrices) { @@ -86,8 +83,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadNamedMatrix() { - var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); - var matrices = dmr.ReadMatrices(new[] { "Ad" }); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/collection.mat", "Ad"); Assert.AreEqual(1, matrices.Count); var ad = matrices["Ad"]; Assert.AreEqual(100, ad.RowCount); @@ -102,8 +98,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadNamedSparseMatrix() { - var dmr = new MatlabMatrixReader("./data/Matlab/sparse-small.mat"); - var matrix = dmr.ReadMatrix("S"); + var matrix = MatlabMatrixReader.ReadMatrix("./data/Matlab/sparse-small.mat", "S"); Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(typeof(LinearAlgebra.Double.SparseMatrix), matrix.GetType()); @@ -116,8 +111,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadComplexAllMatrices() { - var dmr = new MatlabMatrixReader("./data/Matlab/complex.mat"); - var matrices = dmr.ReadMatrices(); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/complex.mat"); Assert.AreEqual(3, matrices.Count); foreach (var matrix in matrices) { @@ -137,8 +131,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadSparseComplexAllMatrices() { - var dmr = new MatlabMatrixReader("./data/Matlab/sparse_complex.mat"); - var matrices = dmr.ReadMatrices(); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/sparse_complex.mat"); Assert.AreEqual(3, matrices.Count); foreach (var matrix in matrices) { @@ -158,8 +151,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadNonComplexAllMatrices() { - var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); - var matrices = dmr.ReadMatrices(); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/collection.mat"); Assert.AreEqual(30, matrices.Count); foreach (var matrix in matrices) { @@ -173,8 +165,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadNonComplexFirstMatrix() { - var dmr = new MatlabMatrixReader("./data/Matlab/A.mat"); - var matrix = dmr.ReadMatrix(); + var matrix = MatlabMatrixReader.ReadMatrix("./data/Matlab/A.mat"); Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(typeof(LinearAlgebra.Complex.DenseMatrix), matrix.GetType()); @@ -187,8 +178,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadNonComplexNamedMatrices() { - var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); - var matrices = dmr.ReadMatrices(new[] { "Ad", "Au64" }); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/collection.mat", "Ad", "Au64"); Assert.AreEqual(2, matrices.Count); foreach (var matrix in matrices) { @@ -202,8 +192,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadNonComplexNamedMatrix() { - var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); - var matrices = dmr.ReadMatrices(new[] { "Ad" }); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/collection.mat", "Ad"); Assert.AreEqual(1, matrices.Count); var ad = matrices["Ad"]; Assert.AreEqual(100, ad.RowCount); @@ -218,8 +207,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadNonComplexNamedSparseMatrix() { - var dmr = new MatlabMatrixReader("./data/Matlab/sparse-small.mat"); - var matrix = dmr.ReadMatrix("S"); + var matrix = MatlabMatrixReader.ReadMatrix("./data/Matlab/sparse-small.mat", "S"); Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(typeof(LinearAlgebra.Complex.SparseMatrix), matrix.GetType()); @@ -232,8 +220,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadComplex32AllMatrices() { - var dmr = new MatlabMatrixReader("./data/Matlab/complex.mat"); - var matrices = dmr.ReadMatrices(); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/complex.mat"); Assert.AreEqual(3, matrices.Count); foreach (var matrix in matrices) { @@ -253,8 +240,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadSparseComplex32AllMatrices() { - var dmr = new MatlabMatrixReader("./data/Matlab/sparse_complex.mat"); - var matrices = dmr.ReadMatrices(); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/sparse_complex.mat"); Assert.AreEqual(3, matrices.Count); foreach (var matrix in matrices) { @@ -274,8 +260,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadNonComplex32AllMatrices() { - var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); - var matrices = dmr.ReadMatrices(); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/collection.mat"); Assert.AreEqual(30, matrices.Count); foreach (var matrix in matrices) { @@ -289,8 +274,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadNonComplex32FirstMatrix() { - var dmr = new MatlabMatrixReader("./data/Matlab/A.mat"); - var matrix = dmr.ReadMatrix(); + var matrix = MatlabMatrixReader.ReadMatrix("./data/Matlab/A.mat"); Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(typeof(LinearAlgebra.Complex32.DenseMatrix), matrix.GetType()); @@ -303,8 +287,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadNonComplex32NamedMatrices() { - var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); - var matrices = dmr.ReadMatrices(new[] { "Ad", "Au64" }); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/collection.mat", "Ad", "Au64"); Assert.AreEqual(2, matrices.Count); foreach (var matrix in matrices) { @@ -318,8 +301,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadNonComplex32NamedMatrix() { - var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); - var matrices = dmr.ReadMatrices(new[] { "Ad" }); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/collection.mat", "Ad"); Assert.AreEqual(1, matrices.Count); var ad = matrices["Ad"]; Assert.AreEqual(100, ad.RowCount); @@ -334,8 +316,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadNonComplex32NamedSparseMatrix() { - var dmr = new MatlabMatrixReader("./data/Matlab/sparse-small.mat"); - var matrix = dmr.ReadMatrix("S"); + var matrix = MatlabMatrixReader.ReadMatrix("./data/Matlab/sparse-small.mat", "S"); Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(typeof(LinearAlgebra.Complex32.SparseMatrix), matrix.GetType()); @@ -348,8 +329,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadFloatAllMatrices() { - var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); - var matrices = dmr.ReadMatrices(); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/collection.mat"); Assert.AreEqual(30, matrices.Count); foreach (var matrix in matrices) { @@ -363,8 +343,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadFloatFirstMatrix() { - var dmr = new MatlabMatrixReader("./data/Matlab/A.mat"); - var matrix = dmr.ReadMatrix(); + var matrix = MatlabMatrixReader.ReadMatrix("./data/Matlab/A.mat"); Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(typeof(LinearAlgebra.Single.DenseMatrix), matrix.GetType()); @@ -377,8 +356,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadFloatNamedMatrices() { - var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); - var matrices = dmr.ReadMatrices(new[] { "Ad", "Au64" }); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/collection.mat", "Ad", "Au64"); Assert.AreEqual(2, matrices.Count); foreach (var matrix in matrices) { @@ -392,8 +370,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadFloatNamedMatrix() { - var dmr = new MatlabMatrixReader("./data/Matlab/collection.mat"); - var matrices = dmr.ReadMatrices(new[] { "Ad" }); + var matrices = MatlabMatrixReader.ReadMatrices("./data/Matlab/collection.mat", "Ad"); Assert.AreEqual(1, matrices.Count); var ad = matrices["Ad"]; Assert.AreEqual(100, ad.RowCount); @@ -408,8 +385,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab [Test] public void CanReadFloatNamedSparseMatrix() { - var dmr = new MatlabMatrixReader("./data/Matlab/sparse-small.mat"); - var matrix = dmr.ReadMatrix("S"); + var matrix = MatlabMatrixReader.ReadMatrix("./data/Matlab/sparse-small.mat", "S"); Assert.AreEqual(100, matrix.RowCount); Assert.AreEqual(100, matrix.ColumnCount); Assert.AreEqual(typeof(LinearAlgebra.Single.SparseMatrix), matrix.GetType()); diff --git a/src/DataUnitTests/Matlab/MatlabWriterTests.cs b/src/DataUnitTests/Matlab/MatlabWriterTests.cs index d910d101..cd4f7bb1 100644 --- a/src/DataUnitTests/Matlab/MatlabWriterTests.cs +++ b/src/DataUnitTests/Matlab/MatlabWriterTests.cs @@ -116,8 +116,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab writer.WriteMatrices(write, names); writer.Dispose(); - var reader = new MatlabMatrixReader("testd.mat"); - var read = reader.ReadMatrices(names); + var read = MatlabMatrixReader.ReadMatrices("testd.mat", names); Assert.AreEqual(write.Length, read.Count); @@ -172,8 +171,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab writer.WriteMatrices(write, names); writer.Dispose(); - var reader = new MatlabMatrixReader("tests.mat"); - var read = reader.ReadMatrices(names); + var read = MatlabMatrixReader.ReadMatrices("tests.mat", names); Assert.AreEqual(write.Length, read.Count); @@ -228,8 +226,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab writer.WriteMatrices(write, names); writer.Dispose(); - var reader = new MatlabMatrixReader("testc.mat"); - var read = reader.ReadMatrices(names); + var read = MatlabMatrixReader.ReadMatrices("testc.mat", names); Assert.AreEqual(write.Length, read.Count); @@ -284,8 +281,7 @@ namespace MathNet.Numerics.Data.UnitTests.Matlab writer.WriteMatrices(write, names); writer.Dispose(); - var reader = new MatlabMatrixReader("testz.mat"); - var read = reader.ReadMatrices(names); + var read = MatlabMatrixReader.ReadMatrices("testz.mat", names); Assert.AreEqual(write.Length, read.Count);