Crates.io | npy |
lib.rs | npy |
version | 0.4.0 |
source | src |
created_at | 2017-03-07 14:45:50.653703 |
updated_at | 2018-03-05 10:41:27.893976 |
description | NumPy file format (de-)serialization |
homepage | https://github.com/potocpav/npy-rs |
repository | https://github.com/potocpav/npy-rs |
max_upload_size | |
id | 8876 |
size | 41,538 |
Numpy format (*.npy) serialization and deserialization.
NPY is a simple binary data format. It stores the type, shape and endianness information in a header, which is followed by a flat binary data field. This crate offers a simple, mostly type-safe way to read and write *.npy files. Files are handled using iterators, so they don't need to fit in memory.
To use npy-rs, two dependencies must be specified in Cargo.toml
:
npy = "0.4"
npy-derive = "0.4"
A typical way to import everything needed is:
#[macro_use]
extern crate npy_derive;
extern crate npy;
The npy-derive
dependency is only needed for
structured array
serialization.
Data can now be imported from a *.npy
file:
use npy::NpyData;
std::fs::File::open("data.npy").unwrap().read_to_end(&mut buf).unwrap();
let data: Vec<f64> = NpyData::from_bytes(&buf).unwrap().to_vec();
and exported to a *.npy
file:
npy::to_file("data.npy", data).unwrap();
See the documentation for more information.
Several usage examples are available in the examples directory; the simple example shows how to load a file, roundtrip shows both reading and writing. Large files can be memory-mapped as illustrated in the large example.