Skip to content

tdakhran/tnpy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TNPY

tnpy is a C++17 library for reading and writing numpy .npy files.

Compilation

Prerequisites

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++

Compile tnpy

docker# mkdir /tmp/build && cd /tmp/build && cmake /mnt && make -j4

Usage

#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;
  }
}

Known issues

  • Only subset of python data types is supported
  • big endian is not supported