import mir.ndslice : iota, map;
if (exists("./test/a1_i4.npy")) {
// FIXME: make this generic
auto a1_i4 = loadNpy!(int, 1)("./test/a1_i4.npy");
assert(a1_i4 == iota(6).map!(to!int));
auto a2_i4 = loadNpy!(int, 2)("./test/a2_i4.npy");
assert(a2_i4 == iota(2, 3).map!(to!int));
saveNpy("./test/b1_i4.npy", a1_i4);
saveNpy("./test/b2_i4.npy", a2_i4);
auto b1_i4 = loadNpy!(int, 1)("./test/b1_i4.npy");
assert(b1_i4 == iota(6).map!(to!int));
auto b2_i4 = loadNpy!(int, 2)("./test/b2_i4.npy");
assert(b2_i4 == iota(2, 3).map!(to!int));
auto a1_i8 = loadNpy!(long, 1)("./test/a1_i8.npy");
assert(a1_i8 == iota(6).map!(to!long));
auto a2_i8 = loadNpy!(long, 2)("./test/a2_i8.npy");
assert(a2_i8 == iota(2, 3).map!(to!long));
saveNpy("./test/b1_i8.npy", a1_i8);
saveNpy("./test/b2_i8.npy", a2_i8);
auto b1_i8 = loadNpy!(long, 1)("./test/b1_i8.npy");
assert(b1_i8 == iota(6).map!(to!long));
auto b2_i8 = loadNpy!(long, 2)("./test/b2_i8.npy");
assert(b2_i8 == iota(2, 3).map!(to!long));
auto a1_f4 = loadNpy!(float, 1)("./test/a1_f4.npy");
assert(a1_f4 == iota(6).map!(to!float));
auto a2_f4 = loadNpy!(float, 2)("./test/a2_f4.npy");
assert(a2_f4 == iota(2, 3).map!(to!float));
saveNpy("./test/b1_f4.npy", a1_f4);
saveNpy("./test/b2_f4.npy", a2_f4);
auto b1_f4 = loadNpy!(float, 1)("./test/b1_f4.npy");
assert(b1_f4 == iota(6).map!(to!float));
auto b2_f4 = loadNpy!(float, 2)("./test/b2_f4.npy");
assert(b2_f4 == iota(2, 3).map!(to!float));
auto a1_f8 = loadNpy!(double, 1)("./test/a1_f8.npy");
assert(a1_f8 == iota(6).map!(to!double));
auto a2_f8 = loadNpy!(double, 2)("./test/a2_f8.npy");
assert(a2_f8 == iota(2, 3).map!(to!double));
saveNpy("./test/b1_f8.npy", a1_f8);
saveNpy("./test/b2_f8.npy", a2_f8);
auto b1_f8 = loadNpy!(double, 1)("./test/b1_f8.npy");
assert(b1_f8 == iota(6).map!(to!double));
auto b2_f8 = loadNpy!(double, 2)("./test/b2_f8.npy");
assert(b2_f8 == iota(2, 3).map!(to!double));
} else {
writeln("WARNING: install numpy to test numir.io");
}
1 if (exists("./test/b1_f8.npy")) {
2 // test exceptions
3 auto f = File("./test/b1_f8.npy", "rb");
4 try
5 {
6 readBytes!double(f, "x4", 1LU);
7 }
8 catch (FileException e)
9 {
10 // NOTE: why ": Success" is appended on Linux?
11 auto expected = "dtype(%s) is not supported yet: %s".format("x4", f.name);
12 assert(e.msg[0 .. expected.length] == expected);
13 }
14
15 auto fi8 = File("./test/b1_f8.npy", "rb");
16 auto es = (endian == Endian.littleEndian ? ">" : "<") ~ "i8";
17 try
18 {
19 readBytes!double(fi8, es, 1LU);
20 }
21 catch (FileException e)
22 {
23 // NOTE: why ": Success" is appended?
24 auto expected = "endian conversion (file %s) is not supported yet: %s".format(es, fi8.name);
25 assert(e.msg[0 .. expected.length] == expected);
26 }
27
28
29 auto fname = "foo.npy";
30 try
31 {
32 parseHeader!1("{'descr': '<f4', 'fortran_order': True, 'shape': (6,), }", fname);
33 }
34 catch (FileException e)
35 {
36 // NOTE: why ": Success" is appended?
37 auto expected = "Fortran ordered ndarray is not supported yet: %s".format(fname);
38 assert(e.msg[0 .. expected.length] == expected);
39 }
40
41 try
42 {
43 parseHeader!2("{'descr': '<f4', 'fortran_order': False, 'shape': (6,), }", fname);
44 }
45 catch (FileException e)
46 {
47 // NOTE: why ": Success" is appended?
48 auto expected = "your expected Ndim %s != %s in the actual npy: %s".format(2, 1, fname);
49 assert(e.msg[0 .. expected.length] == expected);
50 }
51
52 try
53 {
54 enforceNPY("foo", fname);
55 }
56 catch (FileException e)
57 {
58 auto expected = "invalid npy header: %s".format(fname);
59 assert(e.msg[0 .. expected.length] == expected);
60 }
61 } else {
62 writeln("WARNING: install numpy to test numir.io");
63 }
save mir.ndslice.Slice as numpy file format