numir.io

NumPy file IO package

  • Declaration

    enum string[string] np2d;

    numpy type string -> dlang type string conversion dictionary

  • Declaration

    auto readBytes(Dtype)(ref File file, string dtype, size_t size);

    read bytes from file with dtype and size

  • Declaration

    struct NpyHeaderInfo(size_t N);

    NpyHeaderInfo

    • Declaration

      string descr;

      numpy type info discription

    • Declaration

      ptrdiff_t[N] shape;

      numpy array shape

  • Declaration

    auto enforceNPY(string magic, string path);

    validate the magic numbers in npy file header

  • Declaration

    auto loadNpy(Dtype, size_t Ndim)(string path);

    load numpy format file into mir.ndslice.Slice

  • Declaration

    template toDtype(D)

    convert D type (not string) into numpy type string

  • Declaration

    string npyHeader(S)(S x);

    create numpy format header from mir.ndslice.Slice

    Examples

    1. import mir.ndslice : iota, sliced, map; auto endianMark = endian == Endian.littleEndian ? "<" : ">"; string descrS, fortranS, shapeS; auto a1 = iota(1).map!(to!int); auto h1 = a1.npyHeader; formattedRead(h1, npyfmt, descrS, fortranS, shapeS); assert(descrS == endianMark ~ "i4"); assert(fortranS == "False"); assert(shapeS == "1,"); auto a23 = iota(2, 3).map!(to!double); auto h23 = a23.npyHeader; formattedRead(h23, npyfmt, descrS, fortranS, shapeS); assert(descrS == endianMark ~ "f8"); assert(fortranS == "False"); assert(shapeS == "2, 3");

  • Declaration

    void saveNpy(S)(string path, S x);

    save mir.ndslice.Slice as numpy file format

    Examples

    1. 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"); }

    Examples

    1. if (exists("./test/b1_f8.npy")) { // test exceptions auto f = File("./test/b1_f8.npy", "rb"); try { readBytes!double(f, "x4", 1LU); } catch (FileException e) { // NOTE: why ": Success" is appended on Linux? auto expected = "dtype(%s) is not supported yet: %s".format("x4", f.name); assert(e.msg[0 .. expected.length] == expected); } auto fi8 = File("./test/b1_f8.npy", "rb"); auto es = (endian == Endian.littleEndian ? ">" : "<") ~ "i8"; try { readBytes!double(fi8, es, 1LU); } catch (FileException e) { // NOTE: why ": Success" is appended? auto expected = "endian conversion (file %s) is not supported yet: %s".format(es, fi8.name); assert(e.msg[0 .. expected.length] == expected); } auto fname = "foo.npy"; try { parseHeader!1("{'descr': '<f4', 'fortran_order': True, 'shape': (6,), }", fname); } catch (FileException e) { // NOTE: why ": Success" is appended? auto expected = "Fortran ordered ndarray is not supported yet: %s".format(fname); assert(e.msg[0 .. expected.length] == expected); } try { parseHeader!2("{'descr': '<f4', 'fortran_order': False, 'shape': (6,), }", fname); } catch (FileException e) { // NOTE: why ": Success" is appended? auto expected = "your expected Ndim %s != %s in the actual npy: %s".format(2, 1, fname); assert(e.msg[0 .. expected.length] == expected); } try { enforceNPY("foo", fname); } catch (FileException e) { auto expected = "invalid npy header: %s".format(fname); assert(e.msg[0 .. expected.length] == expected); } } else { writeln("WARNING: install numpy to test numir.io"); }