/// <summary> /// Writes a .mat file. /// </summary> /// <param name="file">A file to write.</param> public void Write(IMatFile file) { var header = Header.CreateNewHeader(); using (var writer = new BinaryWriter(Stream)) { WriteHeader(writer, header); foreach (var variable in file.Variables) { switch (_options.UseCompression) { case CompressionUsage.Always: WriteCompressedVariable(writer, variable); break; case CompressionUsage.Never: WriteVariable(writer, variable); break; default: throw new ArgumentOutOfRangeException(); } } } }
public void Open() { using (var fileStream = new FileStream(_FileName, FileMode.Open)) { var reader = new MatFileReader(fileStream); _MatFile = reader.Read(); } }
private void CompareMatFiles(IMatFile expected, IMatFile actual) { Assert.That(expected.Variables.Length, Is.EqualTo(actual.Variables.Length)); for (var i = 0; i < expected.Variables.Length; i++) { var expectedVariable = expected.Variables[i]; var actualVariable = actual.Variables[i]; Assert.That(expectedVariable.Name, Is.EqualTo(actualVariable.Name)); Assert.That(expectedVariable.IsGlobal, Is.EqualTo(actualVariable.IsGlobal)); CompareMatArrays(expectedVariable.Value, actualVariable.Value); } }
private void MatCompareWithTestData(string factoryName, string testName, IMatFile actual) { var expected = GetMatTestData(factoryName)[testName]; CompareTestDataWithWritingOptions(expected, actual, null); CompareTestDataWithWritingOptions( expected, actual, new MatFileWriterOptions { UseCompression = CompressionUsage.Always }); CompareTestDataWithWritingOptions( expected, actual, new MatFileWriterOptions { UseCompression = CompressionUsage.Never }); }
private void CompareTestDataWithWritingOptions( IMatFile expected, IMatFile actual, MatFileWriterOptions?maybeOptions) { byte[] buffer; using (var stream = new MemoryStream()) { var writer = maybeOptions is MatFileWriterOptions options ? new MatFileWriter(stream, options) : new MatFileWriter(stream); writer.Write(actual); buffer = stream.ToArray(); } using (var stream = new MemoryStream(buffer)) { var reader = new MatFileReader(stream); var actualRead = reader.Read(); CompareMatFiles(expected, actualRead); } }
public void createNetwork(bool matlabFile) { if (_nodesInputLayer <= 0 || _nodesOutputLayer <= 0) { throw new Exception("Invalid amount of input or output layer nodes"); } //For creating txt FileStream outFile = null; StreamWriter fileWriterTxt = null; //For creating mat-file MatFileWriter fileWriterMatlab = null; DataBuilder dataBuilder = null; IArrayOf <int> array = null; //Total number of nodes int nodesTotal = _nodesInputLayer + _nodesOutputLayer; foreach (int nodes in _nodesHiddenLayers) { nodesTotal += nodes; } try{ if (!matlabFile) { outFile = File.Create(Path.Combine(Directory.GetCurrentDirectory(), "network.nn")); fileWriterTxt = new System.IO.StreamWriter(outFile); } else { outFile = new System.IO.FileStream("network.mat", System.IO.FileMode.Create); fileWriterMatlab = new MatFileWriter(outFile); dataBuilder = new DataBuilder(); //Create the two dimensional array array = dataBuilder.NewArray <int>(nodesTotal, nodesTotal); } int layerVertical = 0; int layerHorizontal = 0; if (!matlabFile) { fileWriterTxt.WriteLine("network = ["); } for (int row = 0; row < nodesTotal; row++) { layerVertical = currentLayer(row); for (int column = 0; column < nodesTotal; column++) { layerHorizontal = currentLayer(column); /* * Sets node to one if when following condition is full filed * * L0 | L1 | L2 | L3 | * ----+----+----+----+----+ * L0 | | X | | * ----+----+----+----+----+ * L1 | | | X | * ----+----+----+----+----+ * L2 | | | | X * ----+----+----+----+----+ * L3 | | | | * ----+----+----+----+----+ */ if (layerVertical == (layerHorizontal - 1)) { if (!matlabFile) { fileWriterTxt.Write(" 1"); } else { array[row, column] = 1; } } else { if (!matlabFile) { fileWriterTxt.Write(" 0"); } else { array[row, column] = 0; } } } if (!matlabFile && row != nodesTotal) { fileWriterTxt.Write(";\n"); } } if (!matlabFile) { fileWriterTxt.WriteLine("];"); } else { //Create the variable IVariable network = dataBuilder.NewVariable("network", array); //Create the matfile IMatFile matFile = dataBuilder.NewFile(new[] { network }); //Write to the file fileWriterMatlab.Write(matFile); } }catch (Exception ex) { Console.WriteLine(ex); } finally{ if (!matlabFile) { fileWriterTxt.Dispose(); } outFile.Dispose(); } }