Skip to content

Commit

Permalink
Merge pull request #21 from /issues/19-and-20
Browse files Browse the repository at this point in the history
* Fix #19 
* Fix #20 
* Upgrade test dependencies
* Build and test with .NET 5
  • Loading branch information
mahalex authored Jul 10, 2021
2 parents b6c387a + d7c9491 commit 404b982
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 15 deletions.
17 changes: 7 additions & 10 deletions MatFileHandler.Tests/MatFileHandler.Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,22 +1,19 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netcoreapp3.0;net472</TargetFrameworks>
<TargetFrameworks>net5.0;net472</TargetFrameworks>
<IsPackable>false</IsPackable>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<CodeAnalysisRuleSet>..\MatFileHandler.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PropertyGroup>
<CodeAnalysisRuleSet>..\MatFileHandler.ruleset</CodeAnalysisRuleSet>
<DocumentationFile>bin\Debug\netcoreapp2.0\MatFileHandler.Tests.xml</DocumentationFile>
<DocumentationFile>bin\Debug\net5.0\MatFileHandler.Tests.xml</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<AdditionalFiles Include="..\stylecop.json" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0" />
<PackageReference Include="NUnit" Version="3.9.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.9.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.10.0" />
<PackageReference Include="NUnit" Version="3.13.2" />
<PackageReference Include="NUnit3TestAdapter" Version="4.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\MatFileHandler\MatFileHandler.csproj" />
Expand All @@ -25,7 +22,7 @@
<Content Include="test-data\**\*.mat" CopyToOutputDirectory="PreserveNewest" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="StyleCop.Analyzers" Version="1.1.0-beta004">
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<PrivateAssets>All</PrivateAssets>
</PackageReference>
</ItemGroup>
Expand Down
63 changes: 63 additions & 0 deletions MatFileHandler.Tests/MatFileReaderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,19 @@ public void TestStruct()
Assert.That((structure["y", 0, 2] as IArrayOf<double>)?[0, 2], Is.EqualTo(3.0));
Assert.That((structure["x", 1, 2] as IArrayOf<float>)?[0], Is.EqualTo(1.5f));
Assert.That(structure["y", 1, 2].IsEmpty, Is.True);

Assert.That(structure["y", 0, 2].ConvertTo2dDoubleArray(), Is.EqualTo(
new double[,]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
}));
Assert.That(structure["y", 0, 2].ConvertToMultidimensionalDoubleArray(), Is.EqualTo(
new double[,]
{
{ 1, 2, 3 },
{ 4, 5, 6 },
}));
}

/// <summary>
Expand All @@ -221,6 +234,15 @@ public void TestSparse()
Assert.That(sparseArray[0, 4], Is.EqualTo(0.0));
Assert.That(sparseArray[3, 0], Is.EqualTo(0.0));
Assert.That(sparseArray[3, 4], Is.EqualTo(0.0));

Assert.That(sparseArray.ConvertTo2dDoubleArray(), Is.EqualTo(
new double[,]
{
{ 0, 0, 0, 0, 0 },
{ 0, 1, 2, 0, 0 },
{ 0, 3, 0, 4, 0 },
{ 0, 0, 0, 0, 0 },
}));
}

/// <summary>
Expand Down Expand Up @@ -462,6 +484,47 @@ public void TestDatetime_Unrepresentable()
Assert.That(d0, Is.Null);
}

[Test]
public void Test_3DArrays()
{
var matFile = GetTests("good")["issue20.mat"];
var obj = matFile["a3d"].Value;
var values = obj.ConvertToDoubleArray();
Assert.That(values, Is.EqualTo(Enumerable.Range(1, 24)));
var expected = new double[3, 4, 2]
{
{
{ 1, 13 },
{ 4, 16 },
{ 7, 19 },
{ 10, 22 },
},
{
{ 2, 14 },
{ 5, 17 },
{ 8, 20 },
{ 11, 23 },
},
{
{ 3, 15 },
{ 6, 18 },
{ 9, 21 },
{ 12, 24 },
},
};
Assert.That(obj.ConvertToMultidimensionalDoubleArray(), Is.EqualTo(expected));
Assert.That(obj.ConvertTo2dDoubleArray(), Is.EqualTo(null));
}

[Test]
public void Test_4DArrays()
{
var matFile = GetTests("good")["issue20.mat"];
var obj = matFile["a4d"].Value;
Assert.That(obj.ConvertToDoubleArray(), Is.EqualTo(Enumerable.Range(1, 120)));
Assert.That(obj.ConvertTo2dDoubleArray(), Is.EqualTo(null));
}

private static AbstractTestDataFactory<IMatFile> GetTests(string factoryName) =>
new MatTestDataFactory(Path.Combine(TestDirectory, factoryName));

Expand Down
Binary file added MatFileHandler.Tests/test-data/good/issue20.mat
Binary file not shown.
44 changes: 44 additions & 0 deletions MatFileHandler/DimensionCalculator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2017-2018 Alexander Luzgarev

using System;
using System.Linq;

namespace MatFileHandler
Expand Down Expand Up @@ -36,5 +37,48 @@ public static int NumberOfElements(this int[] dimensions)
{
return dimensions.Aggregate(1, (x, y) => x * y);
}

/// <summary>
/// Rearrange data from a flat (column-major) array into a multi-dimensional array.
/// </summary>
/// <typeparam name="T">Array element type.</typeparam>
/// <param name="dimensions">Target array dimensions.</param>
/// <param name="data">Flat (column-major) data to rearrange.</param>
/// <returns>An array of specified dimensions containing data from the original flat array, layed out according to column-major order.</returns>
public static Array UnflattenArray<T>(this int[] dimensions, T[] data)
{
var result = Array.CreateInstance(typeof(T), dimensions);
var n = dimensions.NumberOfElements();
var indices = new int[dimensions.Length];
for (var i = 0; i < n; i++)
{
result.SetValue(data[i], indices);
IncrementMultiIndex(dimensions, indices);
}

return result;
}

private static void IncrementMultiIndex(int[] dimensions, int[] indices)
{
var currentPosition = 0;
while (true)
{
if (currentPosition >= indices.Length)
{
break;
}
indices[currentPosition]++;
if (indices[currentPosition] >= dimensions[currentPosition])
{
indices[currentPosition] = 0;
currentPosition++;
}
else
{
break;
}
}
}
}
}
13 changes: 13 additions & 0 deletions MatFileHandler/IArray.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2017-2018 Alexander Luzgarev

using System;
using System.Numerics;

namespace MatFileHandler
Expand Down Expand Up @@ -30,6 +31,18 @@ public interface IArray
/// <returns>Array of values of the array, converted to Double, or null if the conversion is not possible.</returns>
double[]? ConvertToDoubleArray();

/// <summary>
/// Tries to convert the array to a 2-dimensional array of Double values.
/// </summary>
/// <returns>2-dimensional array of values of the array, converted to Double, or null if the conversion is not possible (for example, when the array has more than 2 dimensions).</returns>
double[,]? ConvertTo2dDoubleArray();

/// <summary>
/// Tries to convert the array to a multidimensional array of Double values.
/// </summary>
/// <returns>Multidimensional array of values of the array, converted to Double, or null if the conversion is not possible.</returns>
Array? ConvertToMultidimensionalDoubleArray();

/// <summary>
/// Tries to convert the array to an array of Complex values.
/// </summary>
Expand Down
8 changes: 8 additions & 0 deletions MatFileHandler/IMatFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,13 @@ public interface IMatFile
/// </summary>
/// <param name="variableName">Variable name.</param>
IVariable this[string variableName] { get; set; }

/// <summary>
/// Gets the variable with the specified name.
/// </summary>
/// <param name="name">The name of the variable to get.</param>
/// <param name="variable">When this method returns, contains the variable with the specified name, if it is found; otherwise, null.</param>
/// <returns>True if the file contains a variable with the specified name; otherwise, false.</returns>
public bool TryGetVariable(string name, out IVariable? variable);
}
}
13 changes: 13 additions & 0 deletions MatFileHandler/MatArray.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Copyright 2017-2018 Alexander Luzgarev

using System;
using System.Numerics;

namespace MatFileHandler
Expand Down Expand Up @@ -59,6 +60,18 @@ public static MatArray Empty()
return null;
}

/// <inheritdoc />
public virtual double[,]? ConvertTo2dDoubleArray()
{
return ConvertToMultidimensionalDoubleArray() as double[,];
}

/// <inheritdoc />
public virtual Array? ConvertToMultidimensionalDoubleArray()
{
return null;
}

/// <inheritdoc />
public virtual Complex[]? ConvertToComplexArray()
{
Expand Down
6 changes: 6 additions & 0 deletions MatFileHandler/MatFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,11 @@ public IVariable this[string variableName]
get => _variables[variableName];
set => _variables[variableName] = value;
}

/// <inheritdoc />
public bool TryGetVariable(string name, out IVariable value)
{
return _variables.TryGetValue(name, out value);
}
}
}
2 changes: 1 addition & 1 deletion MatFileHandler/MatFileHandler.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFrameworks>netstandard2.0;net461;net472</TargetFrameworks>
<PackageVersion>1.3.0</PackageVersion>
<PackageVersion>1.4.0-beta1</PackageVersion>
<PackageId>MatFileHandler</PackageId>
<Title>A library for reading and writing MATLAB .mat files.</Title>
<Authors>Alexander Luzgarev</Authors>
Expand Down
10 changes: 8 additions & 2 deletions MatFileHandler/MatNumericalArrayOf.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
// Copyright 2017-2018 Alexander Luzgarev

using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;

namespace MatFileHandler
Expand Down Expand Up @@ -59,6 +57,14 @@ public override double[] ConvertToDoubleArray()
};
}

/// <inheritdoc />
public override Array? ConvertToMultidimensionalDoubleArray()
{
var doubleData = ConvertToDoubleArray();
var rearrangedData = Dimensions.UnflattenArray(doubleData);
return rearrangedData;
}

/// <summary>
/// Tries to convert the array to an array of Complex values.
/// </summary>
Expand Down
17 changes: 17 additions & 0 deletions MatFileHandler/MatSparseArrayOf.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,23 @@ public override double[] ConvertToDoubleArray()
return data as double[] ?? data.Select(x => Convert.ToDouble(x)).ToArray();
}

/// <inheritdoc />
public override Array? ConvertToMultidimensionalDoubleArray()
{
if (Dimensions.Length != 2)
{
return null;
}

var result = new double[Dimensions[0], Dimensions[1]];
foreach (var pair in Data)
{
result[pair.Key.row, pair.Key.column] = Convert.ToDouble(pair.Value);
}

return result;
}

/// <summary>
/// Tries to convert the array to an array of Complex values.
/// </summary>
Expand Down
4 changes: 2 additions & 2 deletions azure-pipelines.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ variables:

steps:
- task: UseDotNet@2
displayName: 'Use .NET Core SDK 3.x'
displayName: 'Use .NET Core SDK 5.x'
inputs:
version: 3.x
version: 5.x

- script: dotnet build --configuration $(buildConfiguration)
displayName: 'dotnet build $(buildConfiguration)'
Expand Down

0 comments on commit 404b982

Please sign in to comment.