https://github.com/symisc/sy-numpy-cpp
A C++17 library for reading and writing NumPy .npy files, with optional .npz (zip) archive support
https://github.com/symisc/sy-numpy-cpp
cpp-library cpp17 numpy numpy-library serialization
Last synced: 6 months ago
JSON representation
A C++17 library for reading and writing NumPy .npy files, with optional .npz (zip) archive support
- Host: GitHub
- URL: https://github.com/symisc/sy-numpy-cpp
- Owner: symisc
- License: lgpl-2.1
- Created: 2025-01-06T23:19:54.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2025-01-06T23:52:52.000Z (about 1 year ago)
- Last Synced: 2025-02-28T07:04:32.343Z (11 months ago)
- Topics: cpp-library, cpp17, numpy, numpy-library, serialization
- Homepage: https://pixlab.io/numpy-cpp-library
- Size: 14.6 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# syNumpy
### A **C++17** library for **reading and writing** [NumPy](https://numpy.org/) `.npy` files, with **optional** `.npz` (zip) archive support
#### Documentation & Homepage: [https://pixlab.io/numpy-cpp-library](https://pixlab.io/numpy-cpp-library).
## Features
- **NpyArray**: A container that stores the loaded array’s shape, word size, fortran order, and data in memory.
- **Load & Save**:
- `syNumpy::loadNpy(...)` reads from a file.
- `syNumpy::loadNpyBuffer(...)` reads from a raw memory buffer.
- `syNumpy::saveNpy(...)` writes or appends data.
- **Append Mode**: Save with `mode = "a"` to extend the first dimension of an existing file (if shapes match).
- **Endian Checks**: Warn if the file is big-endian while the system is little-endian (or vice versa). No auto-swapping.
- **Optional NPZ**: `-DSYNUMPY_ENABLE_NPZ=1` enables zip-based `.npz` archiving (requires **zlib**). Minimal example included.
## Quick Start
1. **Include** `syNumpy.hpp` in your C++17 project.
2. **Compile** with `-std=c++17`.
3. **(Optional)** Define `SYNUMPY_ENABLE_NPZ=1` and link **zlib** (`-lz`) for `.npz` archiving.
### Minimal Example
```cpp
#include "syNumpy.hpp"
#include
int main() {
// 1) Save a float array
{
std::vector data = {1.0f, 2.0f, 3.0f};
syNumpy::saveNpy("floats.npy", data); // overwrites if file exists
}
// 2) Load it back
{
syNumpy::NpyArray arr = syNumpy::loadNpy("floats.npy");
std::vector loaded = arr.asVector();
std::cout << "Loaded " << loaded.size() << " floats: ";
for (auto f : loaded) std::cout << f << " ";
std::cout << "\n";
}
#if SYNUMPY_ENABLE_NPZ
// 3) Create a .npz archive with multiple arrays
{
syNumpy::NpzArchive zip;
std::vector arr1 = {10, 20, 30};
std::vector arr2 = {3.14, 2.71};
zip.addArray("ints", arr1);
zip.addArray("doubles", arr2);
zip.save("arrays.npz");
}
#endif
return 0;
}