Issues/19 and 20 #21

Merged
mahalex merged 4 commits from issues/19-and-20 into master 2021-07-10 10:13:51 +00:00
12 changed files with 182 additions and 15 deletions

View File

@ -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'">
<PropertyGroup>
<CodeAnalysisRuleSet>..\MatFileHandler.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<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" />
@ -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>

View File

@ -201,6 +201,19 @@ namespace MatFileHandler.Tests
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>
@ -221,6 +234,15 @@ namespace MatFileHandler.Tests
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>
@ -462,6 +484,47 @@ namespace MatFileHandler.Tests
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));

Binary file not shown.

View File

@ -1,5 +1,6 @@
// Copyright 2017-2018 Alexander Luzgarev
using System;
using System.Linq;
namespace MatFileHandler
@ -36,5 +37,48 @@ namespace MatFileHandler
{
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;
}
}
}
}
}

View File

@ -1,5 +1,6 @@
// Copyright 2017-2018 Alexander Luzgarev
using System;
using System.Numerics;
namespace MatFileHandler
@ -30,6 +31,18 @@ namespace MatFileHandler
/// <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>

View File

@ -20,5 +20,13 @@ namespace MatFileHandler
/// </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);
}
}

View File

@ -1,5 +1,6 @@
// Copyright 2017-2018 Alexander Luzgarev
using System;
using System.Numerics;
namespace MatFileHandler
@ -59,6 +60,18 @@ namespace MatFileHandler
return null;
}
/// <inheritdoc />
public virtual double[,]? ConvertTo2dDoubleArray()
{
return ConvertToMultidimensionalDoubleArray() as double[,];
}
/// <inheritdoc />
public virtual Array? ConvertToMultidimensionalDoubleArray()
{
return null;
}
/// <inheritdoc />
public virtual Complex[]? ConvertToComplexArray()
{

View File

@ -34,5 +34,11 @@ namespace MatFileHandler
get => _variables[variableName];
set => _variables[variableName] = value;
}
/// <inheritdoc />
public bool TryGetVariable(string name, out IVariable value)
{
return _variables.TryGetValue(name, out value);
}
}
}

View File

@ -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>

View File

@ -1,8 +1,6 @@
// Copyright 2017-2018 Alexander Luzgarev
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
namespace MatFileHandler
@ -59,6 +57,14 @@ namespace MatFileHandler
};
}
/// <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>

View File

@ -64,6 +64,23 @@ namespace MatFileHandler
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>

View File

@ -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)'