libnpy is a simple C++ library for reading and writing of numpy's .npy files.

Related tags

Utilities libnpy
Overview

libnpy

libnpy is a simple C++ library for reading and writing of numpy's .npy files.

Refer to format.py for a detailed description of the .npy format.

This libraries primary purpose is writing numerical data easily and efficiently into the .npy format. It also allows reading .npy files, although only a very limited subset of data types are supported.

Features

  • Writing C++ vectors (std::vector) to .npy files
  • Reading (some) simple .npy files into C++ vectors

Supported data types and mapping

Only scalar numeric data types are supported. There is no natural way to represent more complex objects and parsing the header becomes tremendously more complex. Supported types:

  • unsigned integer
  • signed integer
  • floating point
  • complex floating point (std::complex, ...)

Usage

#include "libnpy.hpp"

// TODO: include example code here

See test/ for examples. C++11 is required. If you use g++, use -std=c++11.

Known limitations

  1. Only a few data types are supported.

  2. The numpy header is a literal Python dictionary and the Python syntax is very permissive. libnpy's parser was only tested with numpy's implemenation of the .npy format.

Contributing

Feel free to send me a pull request, open an issue, or contact me directly.

License

The project is licensed under the MIT license

Comments
  • [clang] constexpr variable cannot have non-literal type

    [clang] constexpr variable cannot have non-literal type

    Any plans to make it compatible with clang?

    Problematic line: https://github.com/llohse/libnpy/blob/master/npy.hpp#L67

    -- Check for working C compiler: /root/workspace/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang
    -- Check for working C compiler: /root/workspace/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang -- works
    
    -- Check for working CXX compiler: /root/workspace/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++
    -- Check for working CXX compiler: /root/workspace/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ -- works
    
    /root/workspace/neo-ai-dlr/3rdparty/libnpy/npy.hpp:66:30: error: constexpr variable cannot have non-literal type 'const std::array<char, 3>'
    constexpr std::array<char,3> endian_chars = { little_endian_char, big_endian_char, no_endian_char };
                                 ^
    /root/workspace/neo-ai-dlr/3rdparty/libnpy/npy.hpp:66:30: error: implicit instantiation of undefined template 'std::__ndk1::array<char, 3>'
    /root/workspace/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1/__tuple:223:64: note: template is declared here
    template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
                                                                   ^
    In file included from /root/workspace/neo-ai-dlr/demo/cpp/run_resnet.cc:16:
    /root/workspace/neo-ai-dlr/3rdparty/libnpy/npy.hpp:67:30: error: constexpr variable cannot have non-literal type 'const std::array<char, 4>'
    constexpr std::array<char,4> numtype_chars = { 'f', 'i', 'u', 'c' };
                                 ^
    /root/workspace/neo-ai-dlr/3rdparty/libnpy/npy.hpp:67:30: error: implicit instantiation of undefined template 'std::__ndk1::array<char, 4>'
    /root/workspace/android-ndk-r19c/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/include/c++/v1/__tuple:223:64: note: template is declared here
    template <class _Tp, size_t _Size> struct _LIBCPP_TEMPLATE_VIS array;
    

    More on the issue: https://stackoverflow.com/questions/46576847/clang-vs-gcc-crtp-constexpr-variable-cannot-have-non-literal-type

    C++ named requirements: LiteralType: https://en.cppreference.com/w/cpp/named_req/LiteralType

    opened by apivovarov 4
  • [bug] Reading unsigned char as (signed) char

    [bug] Reading unsigned char as (signed) char

    In read_header(), header length is read as an unsigned, little-endian short int, which is currently done by reading two char and concatenating them into an unsigned int.

    However the sign-ness of char in C++ is not specified (implementation defined). When char is treated as signed char, the code immediately overflows for byte in range [128, 255], and causing all latter buffer allocating go wrong.

      uint32_t header_length;
      if (version == version_t{1, 0}) {
        char header_len_le16[2];
        istream.read(header_len_le16, 2);
        header_length = (header_len_le16[0] << 0) | (header_len_le16[1] << 8); // header_length incorrect for signed char!
      .......
    
    opened by dhbloo 3
  • duplicate symbol under GCC-10

    duplicate symbol under GCC-10

    In my project, we include npy.hpp in different source files, generate .o files, and link them together. However, gcc-10 defaults to -fno-common . This part is raising duplicate symbol error while linking https://github.com/llohse/libnpy/blob/b67016355374fe29d75430842d72865f99e3f697/include/npy.hpp#L131

    To reproduce: case.zip Based on libnpy/tests, split test.cpp into 3 cpp files, each containing one function, i.e. load, save, and main. Compile with g++-10 -c, and link. The linker generates:

    duplicate symbol 'npy::has_typestring::dtype' in: test_load.o test_save.o duplicate symbol 'npy::has_typestring::dtype' in: test_load.o test_save.o ...

    for each has_typestring<T>.

    opened by caic99 3
  • Add signed char to acceptable Scalar type

    Add signed char to acceptable Scalar type

    In C++, the signedness of char is platform dependent.

    FIY: Fundamental types - cppreference.com

    The signedness of char depends on the compiler and the target platform: the defaults for ARM and PowerPC are typically unsigned, the defaults for x86 and x64 are typically signed.

    It is better to use signed char instead of char in signedness-aware coding.

    opened by fur0ut0 2
  • Fix android string

    Fix android string

    • Use custom to_string because it is not available in the standard library in some Android toolchains.
    • Fix std::string initialization from char array by explicitly specifying the string length (to avoid including null terminator, which might happen with some compilers).
    opened by lsc1234582 2
  • Moved declaration of short LoadArrayFromNumpy after the original

    Moved declaration of short LoadArrayFromNumpy after the original

    LoadArrayFromNumpy(const std::string&, std::vector &, std::vector &) was declared after LoadArrayFromNumpy(const std::string&, std::vector &, bool &, std::vector &) and was using it.

    opened by BananaFructa 1
  • fix asan container overflow

    fix asan container overflow

    Hi,

    Using the lib I spotted an ASAN issue du to: l.476

      auto buf_v = std::vector<char>();
      buf_v.reserve(header_length);
      istream.read(buf_v.data(), header_length);
    

    it seems that reserve not changing the size of the vector and using istream read on the buffer (which does not change the size again) lead to a container overflow later on.

    The proper way would be to use a resize or simply declare the vector with the correct size:

      auto buf_v = std::vector<char>(header_length);
      istream.read(buf_v.data(), header_length);
    

    This get rid of the issue.

    Cheers, Matt

    opened by MattIsInIt 1
  • Cumulative improvements

    Cumulative improvements

    Thanks for making this library! When I'm trying to use it, I found some room for improvement so I changed the codes in the following aspects. I really hope you can help review it. Thanks!

    1. Changed sprintf into snprintf because the latter is a safer function.
    2. Supported numpy scalars as 0-dimensional arrays. Test cases are expanded to include these cases.
    3. Add an interface such that SaveArrayAsNumpy can work with raw pointer buffer, which is very useful if the user is using a contiguous container other than std::vector, such as Eigen::Matrix or std::array.
    opened by drelatgithub 1
  • Exposing fortran_order in LoadArrayFromNumpy

    Exposing fortran_order in LoadArrayFromNumpy

    I've exposed "fortran_order" in LoadArrayFromNumpy because (as shown in added examples created by createnpy.py) transposing the matrix in numpy only changes that attribute by default and the layout of the matrix data after the header stays the same as without trasposition. Having fortran_order is therefore crucial for interpreting the data. Not wanting to break the interface I added overloaded function. Also silenced a casting warning.

    opened by ziembla 1
  • unable to  npy.hpp file

    unable to npy.hpp file

    Debuger Error;

    abort() has been called

    490 line; read_header0x00b739c0{Project1.exe!npy::read_header(std::basic_istream<char,std::char_traits> &)}

    unable to access read_header function, parameter

    opened by HyeonWuJeon 0
  • [Feature request] Save std::vector<torch::Tensor> to .npy file

    [Feature request] Save std::vector to .npy file

    Foremost, I should have thank you for an impressive project. However, for a wider approach to this project, it seems like there has no support for saving libtorch datatypes into .npy without catching errors yet. Should there be a feature like this in the future, it would be very helpful for those using torch via C++. Regards.

    opened by ngctnnnn 0
  • "'std::runtime_error' what(): formatting error: typestrings not matching",

    I find i can't load the numpy file due to "terminate called after throwing an instance of "'std::runtime_error' what(): formatting error: typestrings not matching", and i tried "std::vector<std::complex> data", std::vector<std::complex> data, std::vector data, std::vector data and so on, it didn't work too, the size of my numpy file is (1, 25, 224,224). here is my code: npy::LoadArrayFromNumpy("00000_1_video.npy", video.shape, video.fortran_order, video.data);

    opened by coreeey 3
Owner
Leon Merten Lohse
Leon Merten Lohse
Library for writing text-based user interfaces

IMPORTANT This library is no longer maintained. It's pretty small if you have a big project that relies on it, just maintain it yourself. Or look for

null 1.9k Dec 22, 2022
Group project: writing our own printf function

0x11. C - printf By Julien Barbier, co-founder & CEO Concepts For this project, students are expected to look at these concepts: Group Projects Pair P

Pericles ADJOVI 5 Oct 24, 2022
Writing our own printf function, this is a project done under ALX Low Level Programming.

0x11. C - printf Writing our own printf function, this is a project done under ALX Low Level Programming. Resource secrets of printf Implementing prin

Ephantus Mwangi 4 Oct 26, 2022
Libelf is a simple library to read ELF files

libelf Libelf is a simple library which provides functions to read ELF files. Headers #include <stdint.h> #include <elf.h> Structures typedef struct

David du Colombier 44 Aug 7, 2022
ByteCopy , or BCP, intends to copy files accurately (down to the bytes) in a simple , safe and efficient manner.

ByteCopy v3.6 About ByteCopy , or BCP, intends to copy files accurately (down to the bytes) in a simple , safe and efficient manner. It's functionalit

A.P. Jo. 16 Jun 22, 2022
RapidObj is an easy-to-use, single-header C++17 library that loads and parses Wavefront .obj files.

RapidObj About Integration Prerequisites Manual Integration CMake Integration API RapidObj Result Next Steps OS Support Third Party Tools and Resource

Slobodan Pavlic 108 Jan 2, 2023
A shebang-friendly script for "interpreting" single C99, C11, and C++ files, including rcfile support.

c99sh Basic Idea Control Files Shebang Tricks C++ C11 Credits Basic Idea A shebang-friendly script for "interpreting" single C99, C11, and C++ files,

Rhys Ulerich 100 Dec 3, 2022
A tool for use with clang to analyze #includes in C and C++ source files

Include What You Use For more in-depth documentation, see docs. Instructions for Users "Include what you use" means this: for every symbol (type, func

null 3.2k Jan 4, 2023
This is a collection of tools for creating and manipulating BitTorrent v2 torrent files

torrent tools This is a collection of tools for creating and manipulating BitTorrent v2 torrent files. torrent-new can create hybrid torrents, but the

Arvid Norberg 9 Nov 12, 2022
Cobalt Strike BOF Files with Nim!

BOF-Nim oh yeah baby I have an inkling it's possible, right now the problem seems to be getting the go function to be present in the Symbol table. No

byt3bl33d3r 77 Dec 24, 2022
A tool to edit Cyberpunk 2077 sav.dat files

This is a holidays project and will probably not reach the user-friendly GUI state that a save editor is expected to have.

null 284 Dec 31, 2022
Load Aseprite files for animated sprites in raylib.

raylib-aseprite Load Aseprite .aseprite files for animated sprites in raylib. Features Load Aseprite files directly for use in raylib Draw individual

Rob Loach 30 Dec 20, 2022
Disassembling .class files

jvmdisassembler Contribution You can contribute by creating an issue or pull request. Please keep the code clean and readable. All contributed code mu

Jonas 6 Jun 20, 2022
Atomically exchange two files in Linux

Atomically exchange two files in Linux.

David Pape 9 Aug 4, 2022
A combined suite of utilities for manipulating binary data files.

BinaryTools A combined suite of utilities for manipulating binary data files. It was developed for use on Windows but might compile on other systems.

David Walters 6 Oct 1, 2022
A tool for Pikmin 1 model files

MODConv A Pikmin 1 model format converter Functionality NOTE: these are not command-line parameters, the program has a built-in input parser load (inp

null 4 Oct 20, 2021
Authenticode Hash Calculator for PE32/PE32+ files

AuthHashCalc Authenticode Hash Calculator for PE32/PE32+ files System Requirements x86/x64 Windows 7/8/8.1/10/11 Administrative privilges are not requ

null 65 Dec 7, 2022
zsh module for automatically compiling sourced files

Zinit Module Motivation The module is a binary Zsh module (think about zmodload Zsh command, it's that topic) which transparently and automatically co

zdharma-continuum 13 Dec 25, 2022
A simple and easy-to-use library to enjoy videogames programming

hb-raylib v3.5 Harbour bindings for raylib 3.5, a simple and easy to use library to learn videogames programming raylib v3.5. The project has an educa

MarcosLMG 1 Aug 28, 2022