openvino/thirdparty/cnpy
Ilya Lavrenov 84db7d0ee6
Build using conanfile.txt (#17580)
* Build using conanfile.txt

* Update .ci/azure/linux_arm64.yml

* Several improvements

* Removed conanfile.py

* Try to use activate / deactivate

* Fixed clang-format code style

* Supported TBB version from Conan

* Added more NOMINMAX

* Fixed static build

* More improvements for static build

* Add usage of static snappy in case of static build

* More fixes

* Small fixes

* Final fixes
2023-05-19 14:01:39 +04:00
..
CMakeLists.txt Build using conanfile.txt (#17580) 2023-05-19 14:01:39 +04:00
cnpy.cpp Fixed LTO build (#16629) 2023-03-31 11:34:42 +04:00
cnpy.h Put mingw on gcc code path (#16101) 2023-05-17 00:47:55 +04:00
LICENSE [Speech sample] Added numpy array support (#5479) 2021-06-03 12:22:06 +03:00
README.md DOCS: Fixing broken links in documentation. (#14935) 2023-01-05 11:25:03 +04:00

Purpose:

NumPy offers the save method for easy saving of arrays into .npy and savez for zipping multiple .npy arrays together into a .npz file.

cnpy lets you read and write to these formats in C++.

The motivation comes from scientific programming where large amounts of data are generated in C++ and analyzed in Python.

Writing to .npy has the advantage of using low-level C++ I/O (fread and fwrite) for speed and binary format for size. The .npy file header takes care of specifying the size, shape, and data type of the array, so specifying the format of the data is unnecessary.

Loading data written in numpy formats into C++ is equally simple, but requires you to type-cast the loaded data to the type of your choice.

Installation:

Default installation directory is /usr/local. To specify a different directory, add -DCMAKE_INSTALL_PREFIX=/path/to/install/dir to the cmake invocation in step 4.

  1. get cmake
  2. create a build directory, say $HOME/build
  3. cd $HOME/build
  4. cmake /path/to/cnpy
  5. make
  6. make install

Using:

To use, #include"cnpy.h" in your source code. Compile the source code mycode.cpp as

g++ -o mycode mycode.cpp -L/path/to/install/dir -lcnpy -lz --std=c++11

Description:

There are two functions for writing data: npy_save and npz_save.

There are 3 functions for reading:

  • npy_load will load a .npy file.
  • npz_load(fname) will load a .npz and return a dictionary of NpyArray structues.
  • npz_load(fname,varname) will load and return the NpyArray for data varname from the specified .npz file.

The data structure for loaded data is below. Data is accessed via the data<T>()-method, which returns a pointer of the specified type (which must match the underlying datatype of the data). The array shape and word size are read from the npy header.

struct NpyArray {
    std::vector<size_t> shape;
    size_t word_size;
    template<typename T> T* data();
};