From 11a7d8870b541f8df68c52d3cdafb7779a9809fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thales=20Magalh=C3=A3es?= Date: Tue, 24 Sep 2024 11:34:41 -0300 Subject: [PATCH 1/4] Add more tests for `np.load` --- test/NumSharp.UnitTest/APIs/np.load.Test.cs | 85 +++++++++++++++--- test/NumSharp.UnitTest/data/arange_f2_be.npy | Bin 0 -> 148 bytes test/NumSharp.UnitTest/data/arange_f2_le.npy | Bin 0 -> 148 bytes test/NumSharp.UnitTest/data/arange_f4_be.npy | Bin 0 -> 168 bytes test/NumSharp.UnitTest/data/arange_f4_le.npy | Bin 0 -> 168 bytes test/NumSharp.UnitTest/data/arange_f8_be.npy | Bin 0 -> 208 bytes test/NumSharp.UnitTest/data/arange_f8_le.npy | Bin 0 -> 208 bytes test/NumSharp.UnitTest/data/arange_i1.npy | Bin 0 -> 138 bytes test/NumSharp.UnitTest/data/arange_i2_be.npy | Bin 0 -> 148 bytes test/NumSharp.UnitTest/data/arange_i2_le.npy | Bin 0 -> 148 bytes test/NumSharp.UnitTest/data/arange_i4_be.npy | Bin 0 -> 168 bytes test/NumSharp.UnitTest/data/arange_i4_le.npy | Bin 0 -> 168 bytes test/NumSharp.UnitTest/data/arange_i8_be.npy | Bin 0 -> 208 bytes test/NumSharp.UnitTest/data/arange_i8_le.npy | Bin 0 -> 208 bytes test/NumSharp.UnitTest/data/arange_u1.npy | Bin 0 -> 138 bytes test/NumSharp.UnitTest/data/arange_u2_be.npy | Bin 0 -> 148 bytes test/NumSharp.UnitTest/data/arange_u2_le.npy | Bin 0 -> 148 bytes test/NumSharp.UnitTest/data/arange_u4_be.npy | Bin 0 -> 168 bytes test/NumSharp.UnitTest/data/arange_u4_le.npy | Bin 0 -> 168 bytes test/NumSharp.UnitTest/data/arange_u8_be.npy | Bin 0 -> 208 bytes test/NumSharp.UnitTest/data/arange_u8_le.npy | Bin 0 -> 208 bytes test/NumSharp.UnitTest/data/hello_S5.npy | Bin 0 -> 138 bytes test/NumSharp.UnitTest/data/hello_U5_be.npy | Bin 0 -> 168 bytes test/NumSharp.UnitTest/data/hello_U5_le.npy | Bin 0 -> 168 bytes test/NumSharp.UnitTest/data/mgrid_i4.npy | Bin 0 -> 328 bytes .../data/mgrid_i4_fortran_order.npy | Bin 0 -> 328 bytes test/NumSharp.UnitTest/data/scalar_S1.npy | Bin 0 -> 129 bytes test/NumSharp.UnitTest/data/scalar_b1.npy | Bin 0 -> 129 bytes test/NumSharp.UnitTest/data/scalar_i4_be.npy | Bin 0 -> 132 bytes test/NumSharp.UnitTest/data/scalar_i4_le.npy | Bin 0 -> 132 bytes 30 files changed, 75 insertions(+), 10 deletions(-) create mode 100644 test/NumSharp.UnitTest/data/arange_f2_be.npy create mode 100644 test/NumSharp.UnitTest/data/arange_f2_le.npy create mode 100644 test/NumSharp.UnitTest/data/arange_f4_be.npy create mode 100644 test/NumSharp.UnitTest/data/arange_f4_le.npy create mode 100644 test/NumSharp.UnitTest/data/arange_f8_be.npy create mode 100644 test/NumSharp.UnitTest/data/arange_f8_le.npy create mode 100644 test/NumSharp.UnitTest/data/arange_i1.npy create mode 100644 test/NumSharp.UnitTest/data/arange_i2_be.npy create mode 100644 test/NumSharp.UnitTest/data/arange_i2_le.npy create mode 100644 test/NumSharp.UnitTest/data/arange_i4_be.npy create mode 100644 test/NumSharp.UnitTest/data/arange_i4_le.npy create mode 100644 test/NumSharp.UnitTest/data/arange_i8_be.npy create mode 100644 test/NumSharp.UnitTest/data/arange_i8_le.npy create mode 100644 test/NumSharp.UnitTest/data/arange_u1.npy create mode 100644 test/NumSharp.UnitTest/data/arange_u2_be.npy create mode 100644 test/NumSharp.UnitTest/data/arange_u2_le.npy create mode 100644 test/NumSharp.UnitTest/data/arange_u4_be.npy create mode 100644 test/NumSharp.UnitTest/data/arange_u4_le.npy create mode 100644 test/NumSharp.UnitTest/data/arange_u8_be.npy create mode 100644 test/NumSharp.UnitTest/data/arange_u8_le.npy create mode 100644 test/NumSharp.UnitTest/data/hello_S5.npy create mode 100644 test/NumSharp.UnitTest/data/hello_U5_be.npy create mode 100644 test/NumSharp.UnitTest/data/hello_U5_le.npy create mode 100644 test/NumSharp.UnitTest/data/mgrid_i4.npy create mode 100644 test/NumSharp.UnitTest/data/mgrid_i4_fortran_order.npy create mode 100644 test/NumSharp.UnitTest/data/scalar_S1.npy create mode 100644 test/NumSharp.UnitTest/data/scalar_b1.npy create mode 100644 test/NumSharp.UnitTest/data/scalar_i4_be.npy create mode 100644 test/NumSharp.UnitTest/data/scalar_i4_le.npy diff --git a/test/NumSharp.UnitTest/APIs/np.load.Test.cs b/test/NumSharp.UnitTest/APIs/np.load.Test.cs index 190c5803..57df0a5f 100644 --- a/test/NumSharp.UnitTest/APIs/np.load.Test.cs +++ b/test/NumSharp.UnitTest/APIs/np.load.Test.cs @@ -19,16 +19,6 @@ public void NumpyLoadTest() int[] b = np.Load(mem); } - [TestMethod] - public void NumpyLoad1DimTest() - { - int[] arr = np.Load(@"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() { @@ -42,5 +32,80 @@ public void NumpyNPZRoundTripTest() var d2 = np.Load_Npz(ms); Assert.IsTrue(d2.Count == 2); } + + [TestMethod] + // [DataRow(@"data/arange_f2_le.npy")] // Ignore: Half-precision floats not supported + [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")] + // [DataRow(@"data/arange_f2_be.npy")] // Ignore: Big-endian types not supported + // [DataRow(@"data/arange_f4_be.npy")] // Ignore: Big-endian types not supported + // [DataRow(@"data/arange_f8_be.npy")] // Ignore: Big-endian types not supported + // [DataRow(@"data/arange_i2_be.npy")] // Ignore: Big-endian types not supported + // [DataRow(@"data/arange_i4_be.npy")] // Ignore: Big-endian types not supported + // [DataRow(@"data/arange_i8_be.npy")] // Ignore: Big-endian types not supported + // [DataRow(@"data/arange_u2_be.npy")] // Ignore: Big-endian types not supported + // [DataRow(@"data/arange_u4_be.npy")] // Ignore: Big-endian types not supported + // [DataRow(@"data/arange_u8_be.npy")] // Ignore: Big-endian types not supported + public void load_Arange(string path) + { + NDArray arr = np.load(path); + // Assert.IsNotNull(arr); + // Assert.IsTrue(arr.ndim == 1 && arr.shape[0] > 0); + + 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")] + // [DataRow(@"data/hello_U5_be.npy")] // Ignore: Unicode strings not supported + // [DataRow(@"data/hello_U5_le.npy")] // Ignore: Unicode strings not supported + public void load_HelloWorld(string path) + { + string[] arr = np.Load(path); + + Assert.AreEqual("Hello", arr[0]); + Assert.AreEqual("World", arr[1]); + } + + [TestMethod] + [DataRow(@"data/mgrid_i4.npy")] + // [DataRow(@"data/mgrid_i4_fortran_order.npy")] // Ignore: Fortran order not supported + 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)] + // [DataRow(@"data/scalar_i4_be.npy", 42)] // Ignore: Big-endian types not supported + 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)); + } } } diff --git a/test/NumSharp.UnitTest/data/arange_f2_be.npy b/test/NumSharp.UnitTest/data/arange_f2_be.npy new file mode 100644 index 0000000000000000000000000000000000000000..4616822862356dfdd2631abdd13a5c1c6666a15c GIT binary patch literal 148 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1Zmu94{qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= qXCxM+0{I$-20EHL3bhL411<&z8wLjkCk7V=R|Yo*cLomzj|Kpd_8v?C literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/arange_f2_le.npy b/test/NumSharp.UnitTest/data/arange_f2_le.npy new file mode 100644 index 0000000000000000000000000000000000000000..0a37ad2727c8a3324dd473ce5bc1a051b476aedd GIT binary patch literal 148 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+r@qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= qXCxM+0{I$-20EHL3bhL411<&z1{($k1}6p=23H0*26qOJ1`hy_6dpqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= xXCxM+0{I$-20EHL3bhL411<(IXs`!T4nPFN4M4mAhz|hq10ZH_WMB|*1ONrZAvpj5 literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/arange_f8_be.npy b/test/NumSharp.UnitTest/data/arange_f8_be.npy new file mode 100644 index 0000000000000000000000000000000000000000..6b1f14e01e6863640225b00b1ae51119ba5ca4ab GIT binary patch literal 208 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1Zmu8`^qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= zXCxM+0{I$-20EHL3bhL411<(Au>SyII6(Oh98kUhloo-~5>Q$ON-IEVB?t`wmx~`i literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/arange_f8_le.npy b/test/NumSharp.UnitTest/data/arange_f8_le.npy new file mode 100644 index 0000000000000000000000000000000000000000..724acbe47cfe3c3d708996adab0fbf75d47e8729 GIT binary patch literal 208 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+i=qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= xXCxM+0{I$-20EHL3bhL411<&>@WCF+b%4+uP+9;=i$G}!C@lk}6`-_|0|0)4A3Xp7 literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/arange_i1.npy b/test/NumSharp.UnitTest/data/arange_i1.npy new file mode 100644 index 0000000000000000000000000000000000000000..a61d940c68fe2143390ad663d41007fbefc16d35 GIT binary patch literal 138 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1JlWC}~qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= gXCxM+0{I$-20EHL3bhL411<(eCT12^Hg*n90EPP-)c^nh literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/arange_i2_be.npy b/test/NumSharp.UnitTest/data/arange_i2_be.npy new file mode 100644 index 0000000000000000000000000000000000000000..a39124596ba798a9fa8b353767f5d29109094f73 GIT binary patch literal 148 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZmuaM~qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= qXCxM+0{I$-20EHL3bhL411<&z21W)Z24)5p237_(26hGx22KEc%^R2i literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/arange_i2_le.npy b/test/NumSharp.UnitTest/data/arange_i2_le.npy new file mode 100644 index 0000000000000000000000000000000000000000..837037d403a98ab4e94fd70243e3a104d33a2a8d GIT binary patch literal 148 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlWC-`qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= qXCxM+0{I$-20EHL3bhL411<&zMg}GZW(F1pRt7c(b_NaxP6hyb{2P`4 literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/arange_i4_be.npy b/test/NumSharp.UnitTest/data/arange_i4_be.npy new file mode 100644 index 0000000000000000000000000000000000000000..17c22775ffcc743e581f5b62e2b00823a01d5dbb GIT binary patch literal 168 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZmuaG|qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= uXCxM+0{I$-20EHL3bhL411<&#U<6W3K+FupEI`Z(#B4y!4#XTl%n1OaSsR)F literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/arange_i4_le.npy b/test/NumSharp.UnitTest/data/arange_i4_le.npy new file mode 100644 index 0000000000000000000000000000000000000000..a95c6a637ba7f1120fc47410fa781c1ecc91591b GIT binary patch literal 168 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlWC%^qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= vXCxM+0{I$-20EHL3bhL411<(2U<6_&AZ7+)79eH?Vm2UV2VxE&<^*B@qPiQH literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/arange_i8_be.npy b/test/NumSharp.UnitTest/data/arange_i8_be.npy new file mode 100644 index 0000000000000000000000000000000000000000..d87f2b53a291d3fed54bf7eb715a3ef16117fcb6 GIT binary patch literal 208 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZmuaD{qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= vXCxM+0{I$-20EHL3bhL411<(szzF3sL1|_v%>t!ap)?zmW{1)oP?{3}@nIXD literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/arange_i8_le.npy b/test/NumSharp.UnitTest/data/arange_i8_le.npy new file mode 100644 index 0000000000000000000000000000000000000000..a3ff5af9540510ac6518b5a2478bc1a40f992371 GIT binary patch literal 208 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlWC!@qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= wXCxM+0{I$-20EHL3bhL411<(AV1&|4P?{M^vp{K9D9r|?*`YKCl;(ue0P!XpoB#j- literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/arange_u1.npy b/test/NumSharp.UnitTest/data/arange_u1.npy new file mode 100644 index 0000000000000000000000000000000000000000..69a32813804a307539f8c4cccce94a4a1631fa74 GIT binary patch literal 138 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1JQ);NLqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= gXCxM+0{I$-20EHL3bhL411<(eCT12^Hg*n90E=rI;Q#;t literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/arange_u2_be.npy b/test/NumSharp.UnitTest/data/arange_u2_be.npy new file mode 100644 index 0000000000000000000000000000000000000000..5eca74edca091115e2ebe9e58c6bca61d5d71971 GIT binary patch literal 148 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZS8AlLqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= qXCxM+0{I$-20EHL3bhL411<&z21W)Z24)5p237_(26hGx22KEiwHu@W literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/arange_u2_le.npy b/test/NumSharp.UnitTest/data/arange_u2_le.npy new file mode 100644 index 0000000000000000000000000000000000000000..4d16a3e359dc907566f8ccb6956b3eaac93cc4d1 GIT binary patch literal 148 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZQ);BHqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= qXCxM+0{I$-20EHL3bhL411<&zMg}GZW(F1pRt7c(b_NaxP6hyht!ap)?zmW{1)oP?{3}`WYLl literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/arange_u8_le.npy b/test/NumSharp.UnitTest/data/arange_u8_le.npy new file mode 100644 index 0000000000000000000000000000000000000000..b41bc9566264c84a2bf732287f883e4d7564a4ae GIT binary patch literal 208 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZQ);2EqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= wXCxM+0{I$-20EHL3bhL411<(AV1&|4P?{M^vp{K9D9r|?*`YKCl;(ue0Que2h!m{45EvmVkrRG`X5IC literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/hello_U5_le.npy b/test/NumSharp.UnitTest/data/hello_U5_le.npy new file mode 100644 index 0000000000000000000000000000000000000000..1d0ef1f83835c449964f6fe84bc886d00f167252 GIT binary patch literal 168 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1Z6KbliqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= qXCxM+0{I$7I+{8PwF(pfE)NCOt}# J0J4KxVgLQO4T6Ot7g!Ld;KJld@dfzG*K_P|f17>1 zJyhWp-rA+%WV#mJT$dU}&%HgYy&Ze&68)V$Q*IBJ_MY;0wurLIZyx0%e`*?vs=`s> lxNuSk$hgT-lY`_S@)>lKqb3KD@A?e7$x$P}?YlmM?jPK18_NIy literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/scalar_S1.npy b/test/NumSharp.UnitTest/data/scalar_S1.npy new file mode 100644 index 0000000000000000000000000000000000000000..2c4ad0b5496dd9063eb04c569070e35b99f9fe7e GIT binary patch literal 129 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1J6KtriqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= UXCxM+0{I%6ItsN46ag+10C?*gnE(I) literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/scalar_b1.npy b/test/NumSharp.UnitTest/data/scalar_b1.npy new file mode 100644 index 0000000000000000000000000000000000000000..185568c8e6c27c78b481829412d46658aac3a23b GIT binary patch literal 129 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1JlVqr_qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= UXCxM+0{I%6ItsN46ag*<0Dk-%bN~PV literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/scalar_i4_be.npy b/test/NumSharp.UnitTest/data/scalar_i4_be.npy new file mode 100644 index 0000000000000000000000000000000000000000..5e498f2b2ef97fc41f72c976730480e6f54a76bd GIT binary patch literal 132 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZmuaG|qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= XXCxM+0{I%6ItsN46ag*<1_mtvy6GEg literal 0 HcmV?d00001 diff --git a/test/NumSharp.UnitTest/data/scalar_i4_le.npy b/test/NumSharp.UnitTest/data/scalar_i4_le.npy new file mode 100644 index 0000000000000000000000000000000000000000..4faa14c534ffe5319502e3ad32a18059a0965f90 GIT binary patch literal 132 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlWC%^qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= XXCxM+0{I%6ItsN46ag+R1_lNIx``WT literal 0 HcmV?d00001 From 799ae712e170275558af9f28e7b69e75c476c4e3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thales=20Magalh=C3=A3es?= Date: Tue, 24 Sep 2024 11:43:17 -0300 Subject: [PATCH 2/4] Fix error when loading an `.npy` file containing a scalar Fix parsing error when `shape` is an empty tuple. When read as an `Array`, scalar values are interpreted as a single element array. --- src/NumSharp.Core/APIs/np.load.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/NumSharp.Core/APIs/np.load.cs b/src/NumSharp.Core/APIs/np.load.cs index b74addeb..f8790a14 100644 --- a/src/NumSharp.Core/APIs/np.load.cs +++ b/src/NumSharp.Core/APIs/np.load.cs @@ -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); } @@ -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); @@ -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); @@ -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; From 5721fa1c254e7d58d636370aef18c271256effdc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thales=20Magalh=C3=A3es?= Date: Tue, 24 Sep 2024 14:47:14 -0300 Subject: [PATCH 3/4] Add test for `np.LoadMatrix` --- test/NumSharp.UnitTest/APIs/np.load.Test.cs | 29 +++++++-------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/test/NumSharp.UnitTest/APIs/np.load.Test.cs b/test/NumSharp.UnitTest/APIs/np.load.Test.cs index 57df0a5f..a0118e7f 100644 --- a/test/NumSharp.UnitTest/APIs/np.load.Test.cs +++ b/test/NumSharp.UnitTest/APIs/np.load.Test.cs @@ -34,7 +34,6 @@ public void NumpyNPZRoundTripTest() } [TestMethod] - // [DataRow(@"data/arange_f2_le.npy")] // Ignore: Half-precision floats not supported [DataRow(@"data/arange_f4_le.npy")] [DataRow(@"data/arange_f8_le.npy")] [DataRow(@"data/arange_i1.npy")] @@ -45,20 +44,9 @@ public void NumpyNPZRoundTripTest() [DataRow(@"data/arange_u2_le.npy")] [DataRow(@"data/arange_u4_le.npy")] [DataRow(@"data/arange_u8_le.npy")] - // [DataRow(@"data/arange_f2_be.npy")] // Ignore: Big-endian types not supported - // [DataRow(@"data/arange_f4_be.npy")] // Ignore: Big-endian types not supported - // [DataRow(@"data/arange_f8_be.npy")] // Ignore: Big-endian types not supported - // [DataRow(@"data/arange_i2_be.npy")] // Ignore: Big-endian types not supported - // [DataRow(@"data/arange_i4_be.npy")] // Ignore: Big-endian types not supported - // [DataRow(@"data/arange_i8_be.npy")] // Ignore: Big-endian types not supported - // [DataRow(@"data/arange_u2_be.npy")] // Ignore: Big-endian types not supported - // [DataRow(@"data/arange_u4_be.npy")] // Ignore: Big-endian types not supported - // [DataRow(@"data/arange_u8_be.npy")] // Ignore: Big-endian types not supported public void load_Arange(string path) { - NDArray arr = np.load(path); - // Assert.IsNotNull(arr); - // Assert.IsTrue(arr.ndim == 1 && arr.shape[0] > 0); + NDArray arr = np.load(path); for (int i = 0; i < arr.shape[0]; ++i) { @@ -69,19 +57,15 @@ public void load_Arange(string path) [TestMethod] [DataRow(@"data/hello_S5.npy")] - // [DataRow(@"data/hello_U5_be.npy")] // Ignore: Unicode strings not supported - // [DataRow(@"data/hello_U5_le.npy")] // Ignore: Unicode strings not supported public void load_HelloWorld(string path) { string[] arr = np.Load(path); - Assert.AreEqual("Hello", arr[0]); Assert.AreEqual("World", arr[1]); } [TestMethod] [DataRow(@"data/mgrid_i4.npy")] - // [DataRow(@"data/mgrid_i4_fortran_order.npy")] // Ignore: Fortran order not supported public void load_Mgrid(string path) { NDArray arr = np.load(path); @@ -99,13 +83,20 @@ public void load_Mgrid(string path) [TestMethod] [DataRow(@"data/scalar_b1.npy", false)] [DataRow(@"data/scalar_i4_le.npy", 42)] - // [DataRow(@"data/scalar_i4_be.npy", 42)] // Ignore: Big-endian types not supported 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)); + } } } From 8cc220ba87021cb1e427fc19ec917a88fd12ab13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thales=20Magalh=C3=A3es?= Date: Tue, 24 Sep 2024 15:09:33 -0300 Subject: [PATCH 4/4] Remove unused files --- test/NumSharp.UnitTest/data/arange_f2_be.npy | Bin 148 -> 0 bytes test/NumSharp.UnitTest/data/arange_f2_le.npy | Bin 148 -> 0 bytes test/NumSharp.UnitTest/data/arange_f4_be.npy | Bin 168 -> 0 bytes test/NumSharp.UnitTest/data/arange_f8_be.npy | Bin 208 -> 0 bytes test/NumSharp.UnitTest/data/arange_i2_be.npy | Bin 148 -> 0 bytes test/NumSharp.UnitTest/data/arange_i4_be.npy | Bin 168 -> 0 bytes test/NumSharp.UnitTest/data/arange_i8_be.npy | Bin 208 -> 0 bytes test/NumSharp.UnitTest/data/arange_u2_be.npy | Bin 148 -> 0 bytes test/NumSharp.UnitTest/data/arange_u4_be.npy | Bin 168 -> 0 bytes test/NumSharp.UnitTest/data/arange_u8_be.npy | Bin 208 -> 0 bytes test/NumSharp.UnitTest/data/hello_U5_be.npy | Bin 168 -> 0 bytes test/NumSharp.UnitTest/data/hello_U5_le.npy | Bin 168 -> 0 bytes .../data/mgrid_i4_fortran_order.npy | Bin 328 -> 0 bytes test/NumSharp.UnitTest/data/scalar_i4_be.npy | Bin 132 -> 0 bytes 14 files changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test/NumSharp.UnitTest/data/arange_f2_be.npy delete mode 100644 test/NumSharp.UnitTest/data/arange_f2_le.npy delete mode 100644 test/NumSharp.UnitTest/data/arange_f4_be.npy delete mode 100644 test/NumSharp.UnitTest/data/arange_f8_be.npy delete mode 100644 test/NumSharp.UnitTest/data/arange_i2_be.npy delete mode 100644 test/NumSharp.UnitTest/data/arange_i4_be.npy delete mode 100644 test/NumSharp.UnitTest/data/arange_i8_be.npy delete mode 100644 test/NumSharp.UnitTest/data/arange_u2_be.npy delete mode 100644 test/NumSharp.UnitTest/data/arange_u4_be.npy delete mode 100644 test/NumSharp.UnitTest/data/arange_u8_be.npy delete mode 100644 test/NumSharp.UnitTest/data/hello_U5_be.npy delete mode 100644 test/NumSharp.UnitTest/data/hello_U5_le.npy delete mode 100644 test/NumSharp.UnitTest/data/mgrid_i4_fortran_order.npy delete mode 100644 test/NumSharp.UnitTest/data/scalar_i4_be.npy diff --git a/test/NumSharp.UnitTest/data/arange_f2_be.npy b/test/NumSharp.UnitTest/data/arange_f2_be.npy deleted file mode 100644 index 4616822862356dfdd2631abdd13a5c1c6666a15c..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 148 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1Zmu94{qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= qXCxM+0{I$-20EHL3bhL411<&z8wLjkCk7V=R|Yo*cLomzj|Kpd_8v?C diff --git a/test/NumSharp.UnitTest/data/arange_f2_le.npy b/test/NumSharp.UnitTest/data/arange_f2_le.npy deleted file mode 100644 index 0a37ad2727c8a3324dd473ce5bc1a051b476aedd..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 148 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZlV+r@qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= qXCxM+0{I$-20EHL3bhL411<&z1{($k1}6p=23H0*26qOJ1`hy_6dpSyII6(Oh98kUhloo-~5>Q$ON-IEVB?t`wmx~`i diff --git a/test/NumSharp.UnitTest/data/arange_i2_be.npy b/test/NumSharp.UnitTest/data/arange_i2_be.npy deleted file mode 100644 index a39124596ba798a9fa8b353767f5d29109094f73..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 148 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZmuaM~qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= qXCxM+0{I$-20EHL3bhL411<&z21W)Z24)5p237_(26hGx22KEc%^R2i diff --git a/test/NumSharp.UnitTest/data/arange_i4_be.npy b/test/NumSharp.UnitTest/data/arange_i4_be.npy deleted file mode 100644 index 17c22775ffcc743e581f5b62e2b00823a01d5dbb..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 168 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZmuaG|qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= uXCxM+0{I$-20EHL3bhL411<&#U<6W3K+FupEI`Z(#B4y!4#XTl%n1OaSsR)F diff --git a/test/NumSharp.UnitTest/data/arange_i8_be.npy b/test/NumSharp.UnitTest/data/arange_i8_be.npy deleted file mode 100644 index d87f2b53a291d3fed54bf7eb715a3ef16117fcb6..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 208 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZmuaD{qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= vXCxM+0{I$-20EHL3bhL411<(szzF3sL1|_v%>t!ap)?zmW{1)oP?{3}@nIXD diff --git a/test/NumSharp.UnitTest/data/arange_u2_be.npy b/test/NumSharp.UnitTest/data/arange_u2_be.npy deleted file mode 100644 index 5eca74edca091115e2ebe9e58c6bca61d5d71971..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 148 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZS8AlLqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= qXCxM+0{I$-20EHL3bhL411<&z21W)Z24)5p237_(26hGx22KEiwHu@W diff --git a/test/NumSharp.UnitTest/data/arange_u4_be.npy b/test/NumSharp.UnitTest/data/arange_u4_be.npy deleted file mode 100644 index e6cc58b91b4dd1b7c2b9cabb588ac5dc8422d50e..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 168 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZS8AfJqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= uXCxM+0{I$-20EHL3bhL411<&#U<6W3K+FupEI`Z(#B4y!4#XTl%n1OhF&m}; diff --git a/test/NumSharp.UnitTest/data/arange_u8_be.npy b/test/NumSharp.UnitTest/data/arange_u8_be.npy deleted file mode 100644 index 7cb80edf848198538b0b5b0ea41c237062bed22f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 208 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZS8AcIqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= vXCxM+0{I$-20EHL3bhL411<(szzF3sL1|_v%>t!ap)?zmW{1)oP?{3}`WYLl diff --git a/test/NumSharp.UnitTest/data/hello_U5_be.npy b/test/NumSharp.UnitTest/data/hello_U5_be.npy deleted file mode 100644 index b907a863fee5baa0307c568a46d7b5bd847d6a1d..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 168 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1Z7iy}mqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= qXCxM+0{I$7I+{8PwF(pfE(Qh$42h!m{45EvmVkrRG`X5IC diff --git a/test/NumSharp.UnitTest/data/hello_U5_le.npy b/test/NumSharp.UnitTest/data/hello_U5_le.npy deleted file mode 100644 index 1d0ef1f83835c449964f6fe84bc886d00f167252..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 168 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1Z6KbliqoAIaUsO_*m=~X4l#&V(cT3DEP6dh= qXCxM+0{I$7I+{8PwF(pfE)NCQO4T6Ot7g!Ld;KJld@dfzG*K_P|f17>1 zJyhWp-rA+%WV#mJT$dU}&%HgYy&Ze&68)V$Q*IBJ_MY;0wurLIZyx0%e`*?vs=`s> lxNuSk$hgT-lY`_S@)>lKqb3KD@A?e7$x$P}?YlmM?jPK18_NIy diff --git a/test/NumSharp.UnitTest/data/scalar_i4_be.npy b/test/NumSharp.UnitTest/data/scalar_i4_be.npy deleted file mode 100644 index 5e498f2b2ef97fc41f72c976730480e6f54a76bd..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 132 zcmbR27wQ`j$;eQ~P_3SlTAW;@Zl$1ZmuaG|qoAIaUsO_*m=~X4l#&V(cT3DEP6dh= XXCxM+0{I%6ItsN46ag*<1_mtvy6GEg