tnpy is a C++17 library for reading and writing numpy .npy files.
The library requires C++17 compiler and cmake 3.21+.
host$ docker run -it --rm -v $PWD:/mnt ubuntu:22.04
docker# apt update && apt install cmake g++
docker# mkdir /tmp/build && cd /tmp/build && cmake /mnt && make -j4
#include "npy.hpp"
#include <fstream>
int main() {
{
// read
std::ifstream File("input.npy");
tnpy::Npy InNpy(File);
std::vector<uint32_t> const Shape = InNpy.shape();
tnpy::Npy::DataOrder const Order = InNpy.order();
tnpy::Npy::dtype_t const DType = InNpy.dtype();
}
{
// write
std::vector<uint32_t> const Shape{2, 1};
std::vector<float> const Data{1., 1.};
tnpy::Npy OutNpy(Shape, Data);
std::ofstream("output.npy") << OutNpy;
}
}
- Only subset of python data types is supported
- big endian is not supported