MCPcopy Index your code
hub / github.com/ExpHP/npyz

github.com/ExpHP/npyz @0.9.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release 0.9.0 ↗ · + Follow
469 symbols 1,279 edges 31 files 115 documented · 25%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

npyz

crates.io version Documentation Build Status

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.

npyz is a fork and successor of the seemingly-dead npy.

API Documentation on docs.rs

Usage

[dependencies]
npyz = "0.8"

You also may be interested in enabling some features:

[dependencies]
npyz = {version = "0.8", features = ["derive", "complex", "npz", "arrayvec", "half"]}

Data can now be read from a *.npy file:

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let bytes = std::fs::read("test-data/plain.npy")?;

    // Note: In addition to byte slices, this accepts any io::Read
    let npy = npyz::NpyFile::new(&bytes[..])?;
    for number in npy.data::<f64>()? {
        let number = number?;
        eprintln!("{}", number);
    }
    Ok(())
}

For further examples and information on: * Reading npy files, * Writing npy files, * Working with structured arrays, * Interop with the ndarray crate, * NPZ files and scipy sparse matrices,

please see the documentation on the root module.

Relation to similar crates

The name npyz is actually an abbreviation. Here is the full name of the crate:

npy plus npz support, and a lot of other features that are frankly a lot more important than npz—not to mention the fact that npz support isn't even actually included in the first release—but I had to call it something, okay

To clarify, npyz is a fork of Pavel Potoček's npy crate. The original npy supported structured arrays with derives, but had many, many limitations.

Compared to npy, npyz has:

  • Support for multidimensional arrays.
  • Support for big endian files.
  • Support for Complex, strings and bytestrings.
  • A more versatile reading API based on std::io::Read, so you can read directly from e.g. a zip::read::ZipFile. The original API required a &[u8], with the expectation that a user can use a memmap for large files.
  • Writing APIs based on std::io::Write.

Originally, ~~nippy~~ npyz was a place for me to protype new features with reckless abandon before finally making a PR to npy, but even my first few foundational PRs have yet to be merged upstream. I believe Pavel has a good head on their shoulders and a great attention to detail, and I appreciated their initial response on my PRs, but nearly two years have passed since the last time I have heard from them. Therefore, I've decided to go forward and publish the fork.

License

npyz is Copyright 2021 Michael Lamparski, and provided under the terms of the MIT License.

npyz is based off of npy. npy is Copyright 2018 Pavel Potoček, which was provided under the terms of the MIT License.

Extension points exported contracts — how you extend this code

Deserialize (Interface)
Trait that permits reading a type from an `.npy` file. > Complete documentation of all types that implement this trait [11 …
src/serialize/traits.rs
WriterBuilder (Interface)
Trait that provides methods on [`WriteOptions`]. To obtain an initial instance of this trait, call [`WriteOptions::new` [4 …
src/write.rs
Serialize (Interface)
Trait that permits writing a type to an `.npy` file. > Complete documentation of all types that implement this trait is [8 …
src/serialize/traits.rs
HasDType (Interface)
Indicates that a Writer options type includes a DType. If you are receiving the following compiler error: > ```text > [1 …
src/write.rs
TypeRead (Interface)
Like some sort of `for Fn(R) -> io::Result `. To obtain one of these, use the [`Deserialize`] trait. Fo [18 implementers]
src/serialize/traits.rs
HasShape (Interface)
Indicates that a Writer options type includes a shape. If you are receiving the following compiler error: > ```text > [1 …
src/write.rs
TypeWrite (Interface)
Like some sort of `for Fn(W, &T) -> io::Result<()>`. To obtain one of these, use the [`Serialize`] trait. [14 implementers]
src/serialize/traits.rs
HasWriter (Interface)
Indicates that a Writer options type includes an output stream. If you are receiving the following compiler error: > ` [1 …
src/write.rs

Core symbols most depended-on inside this repo

parse
called by 97
src/header.rs
len
called by 41
src/read.rs
clone
called by 34
src/write.rs
extend
called by 28
src/write.rs
shape
called by 21
src/read.rs
finish
called by 21
src/write.rs
push
called by 19
src/write.rs
into_inner
called by 18
src/read.rs

Shape

Function 232
Method 135
Class 73
Interface 15
Enum 14

Languages

Rust100%

Modules by API surface

src/write.rs61 symbols
src/serialize/slice.rs55 symbols
src/header.rs54 symbols
src/read.rs52 symbols
tests/sparse.rs29 symbols
src/type_str.rs27 symbols
tests/roundtrip.rs26 symbols
src/sparse.rs25 symbols
src/serialize/traits.rs21 symbols
src/serialize/primitive.rs20 symbols
tests/serialize_array.rs12 symbols
src/npz_feature.rs11 symbols

For agents

$ claude mcp add npyz \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact