Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #517 error when loading a .npy file containing a scalar value #518

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/NumSharp.Core/APIs/np.load.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ public static NDArray load(Stream stream)
if (!parseReader(reader, out bytes, out type, out shape))
throw new FormatException();

Array array = Arrays.Create(type, shape.Aggregate((dims, dim) => dims * dim));

Array array = Arrays.Create(type, shape.Aggregate(1, (dims, dim) => dims * dim));
var result = new NDArray(readValueMatrix(reader, array, bytes, type, shape));
return result.reshape(shape);
}
Expand Down Expand Up @@ -165,6 +164,10 @@ public static Array LoadMatrix(Stream stream)
int[] shape;
if (!parseReader(reader, out bytes, out type, out shape))
throw new FormatException();

// Read scalar as a single element array
if (shape.Length == 0)
shape = new int[] { 1 };

Array matrix = Arrays.Create(type, shape);

Expand All @@ -188,6 +191,10 @@ public static Array LoadJagged(Stream stream, bool trim = true)
int[] shape;
if (!parseReader(reader, out bytes, out type, out shape))
throw new FormatException();

// Read scalar as a single element array
if (shape.Length == 0)
shape = new int[] { 1 };

Array matrix = Arrays.Create(type, shape);

Expand Down Expand Up @@ -357,7 +364,7 @@ private static bool parseReader(BinaryReader reader, out int bytes, out Type t,

mark = "'shape': (";
s = header.IndexOf(mark) + mark.Length;
e = header.IndexOf(")", s + 1);
e = header.IndexOf(")", s);
shape = header.Substring(s, e - s).Split(',').Where(v => !String.IsNullOrEmpty(v)).Select(Int32.Parse).ToArray();

return true;
Expand Down
76 changes: 66 additions & 10 deletions test/NumSharp.UnitTest/APIs/np.load.Test.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,6 @@ public void NumpyLoadTest()
int[] b = np.Load<int[]>(mem);
}

[TestMethod]
public void NumpyLoad1DimTest()
{
int[] arr = np.Load<int[]>(@"data/1-dim-int32_4_comma_empty.npy");
Assert.IsTrue(arr[0] == 0);
Assert.IsTrue(arr[1] == 1);
Assert.IsTrue(arr[2] == 2);
Assert.IsTrue(arr[3] == 3);
}

[TestMethod]
public void NumpyNPZRoundTripTest()
{
Expand All @@ -42,5 +32,71 @@ public void NumpyNPZRoundTripTest()
var d2 = np.Load_Npz<Array>(ms);
Assert.IsTrue(d2.Count == 2);
}

[TestMethod]
[DataRow(@"data/arange_f4_le.npy")]
[DataRow(@"data/arange_f8_le.npy")]
[DataRow(@"data/arange_i1.npy")]
[DataRow(@"data/arange_i2_le.npy")]
[DataRow(@"data/arange_i4_le.npy")]
[DataRow(@"data/arange_i8_le.npy")]
[DataRow(@"data/arange_u1.npy")]
[DataRow(@"data/arange_u2_le.npy")]
[DataRow(@"data/arange_u4_le.npy")]
[DataRow(@"data/arange_u8_le.npy")]
public void load_Arange(string path)
{
NDArray arr = np.load(path);

for (int i = 0; i < arr.shape[0]; ++i)
{
int value = (int)Convert.ChangeType(arr.GetValue(i), typeof(int));
Assert.AreEqual(i, value);
}
}

[TestMethod]
[DataRow(@"data/hello_S5.npy")]
public void load_HelloWorld(string path)
{
string[] arr = np.Load<string[]>(path);
Assert.AreEqual("Hello", arr[0]);
Assert.AreEqual("World", arr[1]);
}

[TestMethod]
[DataRow(@"data/mgrid_i4.npy")]
public void load_Mgrid(string path)
{
NDArray arr = np.load(path);

for (int i = 0; i < arr.shape[0]; i++)
{
for (int j = 0; j < arr.shape[1]; j++)
{
Assert.AreEqual(i, (int)arr.GetValue(0, i, j));
Assert.AreEqual(j, (int)arr.GetValue(1, i, j));
}
}
}

[TestMethod]
[DataRow(@"data/scalar_b1.npy", false)]
[DataRow(@"data/scalar_i4_le.npy", 42)]
public void load_Scalar(string path, object expected)
{
NDArray arr = np.load(path);
Assert.AreEqual(Shape.Scalar, arr.shape);
Assert.AreEqual(expected, arr.GetValue(0));
}

[TestMethod]
[DataRow(@"data/scalar_b1.npy", false)]
[DataRow(@"data/scalar_i4_le.npy", 42)]
public void LoadMatrix_Scalar(string path, object expected)
{
Array arr = np.LoadMatrix(path);
Assert.AreEqual(expected, arr.GetValue(0));
}
}
}
Binary file added test/NumSharp.UnitTest/data/arange_f4_le.npy
Binary file not shown.
Binary file added test/NumSharp.UnitTest/data/arange_f8_le.npy
Binary file not shown.
Binary file added test/NumSharp.UnitTest/data/arange_i1.npy
Binary file not shown.
Binary file added test/NumSharp.UnitTest/data/arange_i2_le.npy
Binary file not shown.
Binary file added test/NumSharp.UnitTest/data/arange_i4_le.npy
Binary file not shown.
Binary file added test/NumSharp.UnitTest/data/arange_i8_le.npy
Binary file not shown.
Binary file added test/NumSharp.UnitTest/data/arange_u1.npy
Binary file not shown.
Binary file added test/NumSharp.UnitTest/data/arange_u2_le.npy
Binary file not shown.
Binary file added test/NumSharp.UnitTest/data/arange_u4_le.npy
Binary file not shown.
Binary file added test/NumSharp.UnitTest/data/arange_u8_le.npy
Binary file not shown.
Binary file added test/NumSharp.UnitTest/data/hello_S5.npy
Binary file not shown.
Binary file added test/NumSharp.UnitTest/data/mgrid_i4.npy
Binary file not shown.
Binary file added test/NumSharp.UnitTest/data/scalar_S1.npy
Binary file not shown.
Binary file added test/NumSharp.UnitTest/data/scalar_b1.npy
Binary file not shown.
Binary file added test/NumSharp.UnitTest/data/scalar_i4_le.npy
Binary file not shown.