static void TestWrite(bool compressed, ref int result) { string filename = compressed ? "test_compressed.npz" : "test.npz"; byte[] expected = File.ReadAllBytes(Test.AssetPath(filename)); UInt8Tensor color = Test.Tensor <UInt8Tensor, byte, UInt8Buffer>(new Shape(new uint[] { 5, 5, 3 })); Float32Tensor depth = Test.Tensor <Float32Tensor, float, Float32Buffer>(new Shape(new uint[] { 5, 5 })); string path = Path.GetRandomFileName(); NPZOutputStream stream = new NPZOutputStream(path, compressed ? CompressionMethod.DEFLATED : CompressionMethod.STORED); stream.Write("color.npy", color); stream.Write("depth.npy", depth); stream.Close(); byte[] actual = File.ReadAllBytes(path); string tag = "c#_npz_write"; if (compressed) { tag += "_compressed"; } Test.AssertEqual <byte, byte[]>(expected, actual, ref result, tag); File.Delete(path); }
static void TestPeek(ref int result, string tag, DataType dtype, Endian endianness, bool fortranOrder) { Shape shape = new Shape(new uint[] { 5, 2, 5 }); HeaderInfo expected = new HeaderInfo(dtype, endianness, fortranOrder, shape); HeaderInfo actual = NumpyIO.NumpyIO.Peek(Test.AssetPath(tag + ".npy")); Test.AssertEqual(expected, actual, ref result, "c#_npy_peek_" + tag); }
static void TestRead <T, D, B>(ref int result, string tag) where B : IList <D> where T : Tensor <D, B> { Shape shape = new Shape(new uint[] { 5, 2, 5 }); T expected = Test.Tensor <T, D, B>(shape); string path = Test.AssetPath(tag + ".npy"); T actual = (T)Activator.CreateInstance(typeof(T), new object[] { path }); Test.AssertEqual <D, B>(expected, actual, ref result, "c#_npy_read_" + tag); }
static void TestWrite <T, D, B>(ref int result, string tag) where B : IList <D> where T : Tensor <D, B> { Shape shape = new Shape(new uint[] { 5, 2, 5 }); T tensor = Test.Tensor <T, D, B>(shape); string path = Path.GetRandomFileName(); tensor.Save(path); byte[] actual = File.ReadAllBytes(path); byte[] expected = File.ReadAllBytes(Test.AssetPath(tag + ".npy")); Test.AssertEqual <byte, byte[]>(expected, actual, ref result, "c#_npy_write_" + tag); File.Delete(path); }
static void TestRead(bool compressed, ref int result) { UInt8Tensor expectedColor = Test.Tensor <UInt8Tensor, byte, UInt8Buffer>(new Shape(new uint[] { 5, 5, 3 })); Float32Tensor expectedDepth = Test.Tensor <Float32Tensor, float, Float32Buffer>(new Shape(new uint[] { 5, 5 })); string filename = compressed ? "test_compressed.npz" : "test.npz"; NPZInputStream stream = new NPZInputStream(Test.AssetPath(filename)); UInt8Tensor actualColor = stream.ReadUInt8("color.npy"); Float32Tensor actualDepth = stream.ReadFloat32("depth.npy"); string tag = "c#_npz_read"; if (compressed) { tag += "_compressed"; } Test.AssertEqual <byte, UInt8Buffer>(expectedColor, actualColor, ref result, tag + " color"); Test.AssertEqual <float, Float32Buffer>(expectedDepth, actualDepth, ref result, tag + " depth"); }
static void TestPeek(bool compressed, ref int result) { HeaderInfo expectedColor = new HeaderInfo(DataType.UINT8, Endian.NATIVE, false, new Shape(new uint[] { 5, 5, 3 })); HeaderInfo expectedDepth = new HeaderInfo(DataType.FLOAT32, Endian.LITTLE, false, new Shape(new uint[] { 5, 5 })); string filename = compressed ? "test_compressed.npz" : "test.npz"; NPZInputStream stream = new NPZInputStream(Test.AssetPath(filename)); HeaderInfo actualColor = stream.Peek("color.npy"); HeaderInfo actualDepth = stream.Peek("depth.npy"); string tag = "c#_npz_read"; if (compressed) { tag += "_compressed"; } Test.AssertEqual(false, stream.Contains("not_there.npy"), ref result, tag + " contains not_there"); Test.AssertEqual(true, stream.Contains("color.npy"), ref result, tag + " contains color"); Test.AssertEqual(true, stream.Contains("depth.npy"), ref result, tag + " contains depth"); Test.AssertEqual(expectedColor, actualColor, ref result, tag + " color"); Test.AssertEqual(expectedDepth, actualDepth, ref result, tag + " depth"); }