### NpzWriter Example: Creating and Writing Arrays Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npz.rs.html Demonstrates how to create a new .npz file and add multiple arrays of different dimensions to it. This example uses the default uncompressed writing. ```rust use ndarray::{array, aview0, Array1, Array2}; use ndarray_npy::NpzWriter; use std::fs::File; let mut npz = NpzWriter::new(File::create("arrays.npz")?); let a: Array2 = array![[1, 2, 3], [4, 5, 6]]; let b: Array1 = array![7, 8, 9]; npz.add_array("a", &a)?; npz.add_array("b", &b)?; npz.add_array("c", &aview0(&10))?; npz.finish()?; # Ok::<_, Box>(()) ``` -------------------------------- ### Example: Viewing a Memory-Mapped .npy File Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npy/mod.rs.html Demonstrates opening a readonly memory-mapped file as an ArrayView using the memmap2 crate and the view_npy method. This example requires the target to be little-endian and not running under Miri. ```rust # // Miri doesn't support mmap, and the file is in little endian format. # if !cfg!(miri) && cfg!(target_endian = "little") { use memmap2::Mmap; use ndarray::ArrayView2; use ndarray_npy::ViewNpyExt; use std::fs::File; let file = File::open("resources/array.npy")?; let mmap = unsafe { Mmap::map(&file)? }; let view = ArrayView2::::view_npy(&mmap)?; # println!("view = {}", view); # } # Ok::<_, Box>(()) ``` -------------------------------- ### Example Usage of write_zeroed_npy Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/fn.write_zeroed_npy.html An example demonstrating how to use write_zeroed_npy to create a large zeroed .npy file and then memory-map it for modification. ```APIDOC ## Example In this example, a file containing 64 GiB of zeroed `f64` elements is created. Then, an `ArrayViewMut` is created by memory-mapping the file. Modifications to the data in the `ArrayViewMut` will be applied to the backing file. This works even on systems with less than 64 GiB of physical memory. On filesystems which support sparse files, the disk space that’s actually used depends on how much data is modified. ```rust use memmap2::MmapMut; use ndarray::ArrayViewMut3; use ndarray_npy::{write_zeroed_npy, ViewMutNpyExt}; use std::fs::{File, OpenOptions}; let path = "array.npy"; // Create a (sparse if supported) file containing 64 GiB of zeroed data. let file = File::create(path)?; write_zeroed_npy::(&file, (1024, 2048, 4096))?; // Memory-map the file and create the mutable view. let file = OpenOptions::new().read(true).write(true).open(path)?; let mut mmap = unsafe { MmapMut::map_mut(&file)? }; let mut view_mut = ArrayViewMut3::::view_mut_npy(&mut mmap)?; // Modify an element in the view. view_mut[[500, 1000, 2000]] = 3.14; ``` ``` -------------------------------- ### Example Usage of ViewNpyExt Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.ViewNpyExt.html Demonstrates how to use the `view_npy` method to create a readonly memory-mapped ArrayView from an .npy file. ```APIDOC ```rust use memmap2::Mmap; use ndarray::ArrayView2; use ndarray_npy::ViewNpyExt; use std::fs::File; let file = File::open("resources/array.npy")?; let mmap = unsafe { Mmap::map(&file)? }; let view = ArrayView2::::view_npy(&mmap)?; ``` ``` -------------------------------- ### Example: Reading Arrays from NPZ File Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npz.rs.html Demonstrates how to open an NPZ file and read arrays by their names. Requires the 'ndarray' and 'ndarray-npy' crates. ```rust use ndarray::{Array1, Array2}; use ndarray_npy::NpzReader; use std::fs::File; let mut npz = NpzReader::new(File::open("arrays.npz")?)?; let a: Array2 = npz.by_name("a")?; let b: Array1 = npz.by_name("b")?; # Ok::<_, Box>(()) ``` -------------------------------- ### Example: Opening a Writable Memory-Mapped File Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.ViewMutNpyExt.html Demonstrates opening a memory-mapped file for reading and writing using `memmap2` and creating a mutable ArrayView using `view_mut_npy`. Changes to the view will modify the file. ```rust use memmap2::MmapMut; use ndarray::ArrayViewMut2; use ndarray_npy::ViewMutNpyExt; use std::fs; let file = fs::OpenOptions::new() .read(true) .write(true) .open("resources/array.npy")?; let mut mmap = unsafe { MmapMut::map_mut(&file)? }; let view_mut = ArrayViewMut2::::view_mut_npy(&mut mmap)?; ``` -------------------------------- ### Example: Writing an Array to a .npy File Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.WriteNpyExt.html Demonstrates how to use the write_npy method to save an Array2 to a file named 'array.npy'. It's recommended to wrap the file writer in a BufWriter for performance. ```rust use ndarray::{array, Array2}; use ndarray_npy::WriteNpyExt; use std::fs::File; use std::io::BufWriter; let arr: Array2 = array![[1, 2, 3], [4, 5, 6]]; let writer = BufWriter::new(File::create("array.npy")?); arr.write_npy(writer)?; ``` -------------------------------- ### Writing a Zeroed .npy File and Memory Mapping Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/fn.write_zeroed_npy.html This example demonstrates creating a large, zeroed .npy file and then memory-mapping it to create a mutable view. Modifications to the view are reflected in the file. This is useful for handling arrays larger than physical memory. ```rust use memmap2::MmapMut; use ndarray::ArrayViewMut3; use ndarray_npy::{write_zeroed_npy, ViewMutNpyExt}; use std::fs::{File, OpenOptions}; let path = "array.npy"; // Create a (sparse if supported) file containing 64 GiB of zeroed data. let file = File::create(path)?; write_zeroed_npy::(&file, (1024, 2048, 4096))?; // Memory-map the file and create the mutable view. let file = OpenOptions::new().read(true).write(true).open(path)?; let mut mmap = unsafe { MmapMut::map_mut(&file)? }; let mut view_mut = ArrayViewMut3::::view_mut_npy(&mut mmap)?; // Modify an element in the view. view_mut[[500, 1000, 2000]] = 3.14; ``` -------------------------------- ### View .npy file as ArrayView using memmap2 Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.ViewNpyExt.html Demonstrates opening a readonly memory-mapped .npy file and creating an ArrayView from its buffer. This example uses the memmap2 crate and requires the file to be present at 'resources/array.npy'. ```rust use memmap2::Mmap; use ndarray::ArrayView2; use ndarray_npy::ViewNpyExt; use std::fs::File; let file = File::open("resources/array.npy")?; let mmap = unsafe { Mmap::map(&file)? }; let view = ArrayView2::::view_npy(&mmap)?; ``` -------------------------------- ### Create Mutable Array View from Buffer Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npy/mod.rs.html Opens a writable memory-mapped file as an `ArrayViewMut`. Changes to the data in the view will modify the underlying file. This example requires the `memmap2` crate and assumes a little-endian system. ```rust # // Miri doesn't support mmap, and the file is in little endian format. # if !cfg!(miri) && cfg!(target_endian = "little") { use memmap2::MmapMut; use ndarray::ArrayViewMut2; use ndarray_npy::ViewMutNpyExt; use std::fs; let file = fs::OpenOptions::new() .read(true) .write(true) .open("resources/array.npy")?; let mut mmap = unsafe { MmapMut::map_mut(&file)? }; let view_mut = ArrayViewMut2::::view_mut_npy(&mut mmap)?; # println!("view_mut = {}", view_mut); # } # Ok::<_, Box>(()) ``` -------------------------------- ### Reading an .npy file with read_npy Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/fn.read_npy.html This example demonstrates how to use the read_npy function to load a 2D array of i32 integers from a file named 'resources/array.npy'. Ensure the file exists at the specified path. ```rust use ndarray::Array2; use ndarray_npy::read_npy; let arr: Array2 = read_npy("resources/array.npy")?; ``` -------------------------------- ### Writing an Array to an .npy File Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/fn.write_npy.html Example demonstrating how to use the write_npy function to save a 2D ndarray to 'array.npy'. Ensure you have the necessary imports. ```rust use ndarray::array; use ndarray_npy::write_npy; let arr = array![[1, 2, 3], [4, 5, 6]]; write_npy("array.npy", &arr)?; ``` -------------------------------- ### Error::description Implementation for FormatHeaderError (Deprecated) Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/header/enum.FormatHeaderError.html Deprecated method to get a string description of the error. Use Display or to_string() instead. ```rust fn description(&self) -> &str ``` -------------------------------- ### Any::type_id Implementation Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/header/enum.FormatHeaderError.html Gets the TypeId of the value. ```rust fn type_id(&self) -> TypeId ``` -------------------------------- ### Get Minor Version Number Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npy/header.rs.html Returns the minor version number for a given Version enum variant. ```rust impl Version { // ... other methods fn minor_version(self) -> u8 { match self { Version::V1_0 => 0, Version::V2_0 => 0, Version::V3_0 => 0, } } // ... other methods } ``` -------------------------------- ### Get Major Version Number Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npy/header.rs.html Returns the major version number for a given Version enum variant. ```rust impl Version { // ... other methods fn major_version(self) -> u8 { match self { Version::V1_0 => 1, Version::V2_0 => 2, Version::V3_0 => 3, } } // ... other methods } ``` -------------------------------- ### Get Number of Arrays in NPZ File Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npz.rs.html Returns the total count of arrays stored within the NPZ file. ```rust pub fn len(&self) -> usize { self.zip.len() } ``` -------------------------------- ### Error::cause Implementation for FormatHeaderError (Deprecated) Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/header/enum.FormatHeaderError.html Deprecated method to get the underlying cause of the error. Use Error::source() instead. ```rust fn cause(&self) -> Option<&dyn Error> ``` -------------------------------- ### ndarray-npy Crate Overview Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/lib.rs.html This snippet shows the main crate-level documentation, outlining its purpose, features, and links to external resources like the repository and NumPy documentation. ```rust #![cfg_attr(docsrs, feature(doc_cfg))] //! This crate provides methods to read/write [`ndarray`]'s //! [`ArrayBase`](ndarray::ArrayBase) type from/to [`.npy`] and [`.npz`] files. //! //! See the [repository] for information about the default features and how to //! use this crate with Cargo. //! //! [repository]: https://github.com/jturner314/ndarray-npy //! [`ndarray`]: https://github.com/bluss/ndarray //! [`.npy`]: https://docs.scipy.org/doc/numpy/reference/generated/numpy.lib.format.html //! [`.npz`]: https://docs.scipy.org/doc/numpy/reference/generated/numpy.savez.html //! ``` -------------------------------- ### Writing Multiple Arrays to an NPZ File Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/struct.NpzWriter.html Demonstrates how to create a new .npz file and add multiple arrays (2D, 1D, and scalar) to it using NpzWriter. Ensure the 'npz' feature is enabled. ```rust use ndarray::{array, aview0, Array1, Array2}; use ndarray_npy::NpzWriter; use std::fs::File; let mut npz = NpzWriter::new(File::create("arrays.npz")?); let a: Array2 = array![[1, 2, 3], [4, 5, 6]]; let b: Array1 = array![7, 8, 9]; npz.add_array("a", &a)?; npz.add_array("b", &b)?; npz.add_array("c", &aview0(&10))?; npz.finish()?; ``` -------------------------------- ### Reading Arrays from NPZ File Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/struct.NpzReader.html Demonstrates how to open an NPZ file and read arrays by their names. Ensure the 'npz' feature is enabled. ```rust use ndarray::{Array1, Array2}; use ndarray_npy::NpzReader; use std::fs::File; let mut npz = NpzReader::new(File::open("arrays.npz")?)?; let a: Array2 = npz.by_name("a")?; let b: Array1 = npz.by_name("b")?; ``` -------------------------------- ### Get Header Length Number of Bytes Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npy/header.rs.html Determines the number of bytes used to represent the header length based on the .npy file version. ```rust impl Version { // ... other methods fn header_len_num_bytes(self) -> usize { match self { Version::V1_0 => 2, Version::V2_0 | Version::V3_0 => 4, } } // ... other methods } ``` -------------------------------- ### WritableElement Trait Definition Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.WritableElement.html The WritableElement trait provides methods for writing individual elements and slices to a writer, along with a method to get a type descriptor for the header. ```APIDOC ## Trait WritableElement An array element type that can be written to an `.npy` or `.npz` file. ### Required Methods #### fn type_descriptor() -> PyValue Returns a descriptor of the type that can be used in the header. #### fn write(&self, writer: W) -> Result<(), WriteDataError> Writes a single instance of `Self` to the writer. #### fn write_slice( slice: &[Self], writer: W, ) -> Result<(), WriteDataError> Writes a slice of `Self` to the writer. ``` -------------------------------- ### From for T Implementation Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/header/enum.FormatHeaderError.html Returns the argument unchanged. ```rust fn from(t: T) -> T ``` -------------------------------- ### NpzWriter::new_with_options Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/struct.NpzWriter.html Creates a new .npz file writer with specified options for all subsequent arrays. ```APIDOC ## NpzWriter::new_with_options ### Description Creates a new `.npz` file with the specified options to be used for each array. This allows you to use a custom compression method, such as zstd, or set other options. Make sure to enable the relevant features of the `zip` crate. ### Method `new_with_options` ### Signature `pub fn new_with_options(writer: W, options: SimpleFileOptions) -> NpzWriter` ### Parameters - **writer**: The underlying writer implementing `Write` and `Seek`. - **options**: `SimpleFileOptions` to configure the compression and other settings for arrays. ``` -------------------------------- ### Implement ViewElement for Multi-Byte Types Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npy/elements/mod.rs.html Macro to implement `ViewElement` for multi-byte types, handling native and non-native byte orderings based on type description. ```rust macro_rules! impl_view_and_view_mut_always_valid_cast_multi_byte { ($elem:ty, $native_desc:expr, $non_native_desc:expr) => { impl $crate::ViewElement for $elem { fn bytes_as_slice<'a>( bytes: &'a [u8], type_desc: &::py_literal::Value, len: usize, ) -> Result<&'a [Self], $crate::ViewDataError> { match *type_desc { ::py_literal::Value::String(ref s) if s == $native_desc => unsafe { ``` -------------------------------- ### Get Names of Arrays in NPZ File Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npz.rs.html Retrieves a list of all array names stored within the NPZ file. The '.npy' suffix is automatically stripped from each name, mirroring NumPy's behavior. ```APIDOC ## names ### Description Returns the names of all of the arrays in the file. The '.npy' suffix (if present) will be stripped from each name, matching NumPy's behavior. ### Signature `pub fn names(&mut self) -> Result, ReadNpzError>` ### Returns - `Result, ReadNpzError>`: A `Result` containing a vector of strings (the array names) on success, or a `ReadNpzError` on failure. ``` -------------------------------- ### Create New NPZ Reader Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npz.rs.html Initializes a new NpzReader from a given Read + Seek source. This prepares the reader to access arrays within the NPZ archive. ```rust pub fn new(reader: R) -> Result, ReadNpzError> { Ok(NpzReader { zip: ZipArchive::new(reader)?, }) } ``` -------------------------------- ### Create a new NPY file Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/fn.create_new_npy.html Demonstrates how to use `create_new_npy` to write an array to a new file and shows that it returns an error if the file already exists. Ensure you have the `ndarray` and `ndarray-npy` crates in your Cargo.toml. ```rust use ndarray::array; use ndarray_npy::create_new_npy; let arr = array![[1, 2, 3], [4, 5, 6]]; create_new_npy("new_array.npy", &arr)?; assert!(create_new_npy("new_array.npy", &arr).is_err()); ``` -------------------------------- ### Get all array names from NPZ file Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npz.rs.html Retrieves a list of all array names stored within an NPZ file. The '.npy' suffix is automatically stripped from each name to match NumPy's behavior. ```rust pub fn names(&mut self) -> Result, ReadNpzError> { Ok((0..self.zip.len()) .map(|i| { let file = self.zip.by_index(i)?; let name = file.name(); let stripped = name.strip_suffix(".npy").unwrap_or(name); Ok(stripped.to_owned()) }) .collect::>()?) } ``` -------------------------------- ### Reading and Writing .npz Files Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/lib.rs.html Provides structures for reading from and writing to .npz (NumPy compressed archive) files. ```APIDOC ## Reading and Writing .npz Files ### Description Provides structures for reading from and writing to .npz (NumPy compressed archive) files. ### Structures - `NpzReader`: For reading data from .npz files. - `NpzWriter`: For writing data to .npz files. ### Errors - `ReadNpzError`: Errors encountered during .npz file reading. - `WriteNpzError`: Errors encountered during .npz file writing. ``` -------------------------------- ### NpzWriter::new Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/struct.NpzWriter.html Creates a new, uncompressed .npz file writer. This is equivalent to numpy.savez. ```APIDOC ## NpzWriter::new ### Description Creates a new `.npz` file without compression. See `numpy.savez`. ### Method `new` ### Signature `pub fn new(writer: W) -> NpzWriter` ### Parameters - **writer**: The underlying writer implementing `Write` and `Seek`. ``` -------------------------------- ### Read NPY file into Array2 Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npy/mod.rs.html Use the `read_npy` method from the `ReadNpyExt` trait to load data from a file into an `Array2` instance. Ensure the file path is correct and the file exists. This example requires `std::fs::File` and `ndarray_npy::ReadNpyExt`. ```rust use ndarray::Array2; use ndarray_npy::ReadNpyExt; use std::fs::File; # use ndarray_npy::ReadNpyError; let reader = File::open("resources/array.npy")?; let arr = Array2::::read_npy(reader)?; # println!("arr = {}", arr); # Ok::<_, ReadNpyError>(()) ``` -------------------------------- ### Header Implementations Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/header/struct.Header.html Provides methods for interacting with the Header struct, including deserializing from a reader, serializing to bytes, and writing to a writer. ```APIDOC ## impl Header ### `pub fn from_reader(reader: &mut R) -> Result` Deserializes a header from the provided reader. ### `pub fn to_bytes(&self) -> Result, FormatHeaderError>` Returns the serialized representation of the header. ### `pub fn write(&self, writer: W) -> Result<(), WriteHeaderError>` Writes the serialized representation of the header to the provided writer. ``` -------------------------------- ### Writing .npz Files Source: https://docs.rs/ndarray-npy Offers functionality to write data to .npz (compressed archive) files. ```APIDOC ## Writing .npz Files This section details writing data to `.npz` files. ### Writer - `NpzWriter`: A struct for writing `.npz` files. ### Errors - `WriteNpzError`: Errors that can occur when writing a `.npz` file. ``` -------------------------------- ### WritableElement for i64 Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.WritableElement.html Implementation for writing 64-bit signed integers. ```rust impl WritableElement for i64 ``` -------------------------------- ### Writing .npz Files Source: https://docs.rs/ndarray-npy/0.10.0/index.html Offers functionality to write data to .npz files. ```APIDOC ## Writing .npz Files ### Description Offers functionality to write data to .npz files. ### Structs - `NpzWriter`: Writer for `.npz` files. ### Errors - `WriteNpzError`: An error writing a `.npz` file. ``` -------------------------------- ### WritableElement for f64 Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.WritableElement.html Implementation for writing 64-bit floating-point numbers. ```rust impl WritableElement for f64 ``` -------------------------------- ### Error::provide Implementation for FormatHeaderError (Nightly) Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/header/enum.FormatHeaderError.html Nightly-only experimental API for providing type-based access to error context. ```rust fn provide<'a>(&'a self, request: &mut Request<'a>) ``` -------------------------------- ### WritableElement for i32 Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.WritableElement.html Implementation for writing 32-bit signed integers. ```rust impl WritableElement for i32 ``` -------------------------------- ### npy::create_new_npy Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/all.html Creates a new NPY file with specified dimensions and data type, initializing it with zeros. ```APIDOC ## npy::create_new_npy ### Description Creates a new NPY file with specified dimensions and data type, initializing it with zeros. This function is useful for pre-allocating space for NPY data. ### Function Signature ```rust pub fn create_new_npy, T: WritableElement>(path: P, shape: &[usize], dtype: DType) -> Result<(), WriteNpyError> ``` ### Parameters #### Path Parameter - **path** (*P*): A type that can be converted into a reference to a Path, representing the file path for the new NPY file. #### Generic Type Parameters - **T**: The type of elements to be written to the NPY file. Must implement the `WritableElement` trait. #### Arguments - **shape** (*&[usize]*): A slice representing the dimensions of the NPY array. - **dtype** (*DType*): The data type of the elements in the NPY file. ### Returns - `Result<(), WriteNpyError>`: Ok(()) if the file was created successfully, or a `WriteNpyError` if an error occurred during file creation or writing. ``` -------------------------------- ### WritableElement for u64 Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.WritableElement.html Implementation for writing 64-bit unsigned integers. ```rust impl WritableElement for u64 ``` -------------------------------- ### WritableElement for i8 Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.WritableElement.html Implementation for writing 8-bit signed integers. ```rust impl WritableElement for i8 ``` -------------------------------- ### write() Method Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.WritableElement.html Writes a single instance of the element to the provided writer. ```rust fn write(&self, writer: W) -> Result<(), WriteDataError>; ``` -------------------------------- ### From for WriteHeaderError Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/header/enum.WriteHeaderError.html Converts a FormatHeaderError into a WriteHeaderError. ```rust impl From for WriteHeaderError { fn from(err: FormatHeaderError) -> WriteHeaderError { // ... implementation details ... } } ``` -------------------------------- ### ViewNpyExt::view_npy Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.ViewNpyExt.html Creates an ArrayView from a buffer containing an .npy file. This is useful for memory-mapped files where direct viewing is preferred over full loading. ```APIDOC ## fn view_npy(buf: &'a [u8]) -> Result ### Description Creates an `ArrayView` from a buffer containing an `.npy` file. ### Parameters #### Path Parameters - `buf` (&'a [u8]): A byte slice representing the content of an .npy file. ### Returns - `Result`: An `ArrayView` of the appropriate type and dimension, or a `ViewNpyError` if the buffer cannot be interpreted as an .npy file. ``` -------------------------------- ### TryInto for T Trait Implementation Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/header/enum.ReadHeaderError.html Demonstrates the generic `try_into` conversion trait implementation. ```rust fn try_into(self) -> Result>::Error> Performs the conversion. ``` -------------------------------- ### WritableElement for f32 Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.WritableElement.html Implementation for writing 32-bit floating-point numbers. ```rust impl WritableElement for f32 ``` -------------------------------- ### NpzReader Methods Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/struct.NpzReader.html This section details the public methods available on the NpzReader struct for interacting with .npz files. ```APIDOC ## NpzReader Reader for `.npz` files. ### `new(reader: R) -> Result, ReadNpzError>` Creates a new `.npz` file reader. ### `is_empty(&self) -> bool` Returns `true` iff the `.npz` file doesn’t contain any arrays. ### `len(&self) -> usize` Returns the number of arrays in the `.npz` file. ### `names(&mut self) -> Result, ReadNpzError>` Returns the names of all of the arrays in the file. Note that a single ".npy" suffix (if present) will be stripped from each name; this matches NumPy’s behavior. ### `by_name(&mut self, name: &str) -> Result, ReadNpzError>` Reads an array by name. Note that this first checks for `name` in the `.npz` file, and if that is not present, checks for `format!("{name}.npy")`. This matches NumPy’s behavior. ### `by_index(&mut self, index: usize) -> Result, ReadNpzError>` Reads an array by index in the `.npz` file. ``` -------------------------------- ### WritableElement for i16 Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.WritableElement.html Implementation for writing 16-bit signed integers. ```rust impl WritableElement for i16 ``` -------------------------------- ### Implement i16 primitive type Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npy/elements/num.rs.html Applies the `impl_primitive_multi_byte` macro to i16. This configures its reading, writing, and viewing capabilities using little-endian ('i2') descriptors. ```Rust impl_primitive_multi_byte!(i16, "i2", 0, read_i16_into); ``` -------------------------------- ### Writing .npz Files Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/index.html Provides functionality to write data to .npz files, enabling the creation of archives containing multiple .npy files. ```APIDOC ## NpzWriter ### Description A writer for `.npz` files, allowing the creation of archives containing multiple `.npy` arrays. ### Methods - `NpzWriter::new()`: Creates a new `NpzWriter` instance. - Methods for adding `.npy` files to the archive (details not specified). ### Errors - `WriteNpzError`: If there is an error writing the `.npz` file. ``` -------------------------------- ### Implementing ViewElement for bool Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npy/elements/bool.rs.html Provides a safe way to view raw bytes as a slice of bools. It validates that each byte is 0 or 1 before casting. ```rust impl ViewElement for bool { fn bytes_as_slice<'a>( bytes: &'a [u8], type_desc: &PyValue, len: usize, ) -> Result<&'a [Self], ViewDataError> { match *type_desc { PyValue::String(ref s) if s == "|b1" => { check_valid_for_bool(bytes)?; unsafe { bytes_as_slice(bytes, len) } } ref other => Err(ViewDataError::WrongDescriptor(other.clone())), } } } ``` -------------------------------- ### Implement ViewElement and ViewMutElement for one-byte primitive types Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npy/elements/num.rs.html Implements `ViewElement` and `ViewMutElement` traits for one-byte primitive types. This allows viewing the raw bytes of the data as slices of the primitive type, based on matching type descriptors. ```Rust macro_rules! impl_view_and_view_mut_primitive_one_byte { ($elem:ty, [$($desc:expr),*]) => { impl ViewElement for $elem { fn bytes_as_slice<'a>( bytes: &'a [u8], type_desc: &PyValue, len: usize, ) -> Result<&'a [Self], ViewDataError> { match *type_desc { PyValue::String(ref s) if $(s == $desc)||* => unsafe { bytes_as_slice(bytes, len) } ref other => Err(ViewDataError::WrongDescriptor(other.clone())), } } } impl ViewMutElement for $elem { fn bytes_as_mut_slice<'a>( bytes: &'a mut [u8], type_desc: &PyValue, len: usize, ) -> Result<&'a mut [Self], ViewDataError> { match *type_desc { PyValue::String(ref s) if $(s == $desc)||* => unsafe { bytes_as_mut_slice(bytes, len) } ref other => Err(ViewDataError::WrongDescriptor(other.clone())), } } } }; } ``` -------------------------------- ### NpzWriter::new_compressed Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/struct.NpzWriter.html Creates a new .npz file writer with Deflated compression. This is equivalent to numpy.savez_compressed. ```APIDOC ## NpzWriter::new_compressed ### Description Creates a new `.npz` file with `Deflated` compression. See `numpy.savez_compressed`. For other compression algorithms, use `NpzWriter::new_with_options` or `NpzWriter::add_array_with_options`. ### Method `new_compressed` ### Signature `pub fn new_compressed(writer: W) -> NpzWriter` ### Parameters - **writer**: The underlying writer implementing `Write` and `Seek`. ``` -------------------------------- ### ViewMutElement Implementations Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.ViewMutElement.html Shows implementations of the ViewMutElement trait for various primitive and complex number types. ```APIDOC ### impl ViewMutElement for Complex Available on **crate feature`num-complex-0_4`** only. #### fn bytes_as_mut_slice<'a>( bytes: &'a mut [u8], type_desc: &Value, len: usize, ) -> Result<&'a mut [Self], ViewDataError> ``` ```APIDOC ### impl ViewMutElement for Complex Available on **crate feature`num-complex-0_4`** only. #### fn bytes_as_mut_slice<'a>( bytes: &'a mut [u8], type_desc: &Value, len: usize, ) -> Result<&'a mut [Self], ViewDataError> ``` ```APIDOC ### impl ViewMutElement for bool #### fn bytes_as_mut_slice<'a>( bytes: &'a mut [u8], type_desc: &PyValue, len: usize, ) -> Result<&'a mut [Self], ViewDataError> ``` ```APIDOC ### impl ViewMutElement for f32 #### fn bytes_as_mut_slice<'a>( bytes: &'a mut [u8], type_desc: &Value, len: usize, ) -> Result<&'a mut [Self], ViewDataError> ``` ```APIDOC ### impl ViewMutElement for f64 #### fn bytes_as_mut_slice<'a>( bytes: &'a mut [u8], type_desc: &Value, len: usize, ) -> Result<&'a mut [Self], ViewDataError> ``` ```APIDOC ### impl ViewMutElement for i8 #### fn bytes_as_mut_slice<'a>( bytes: &'a mut [u8], type_desc: &PyValue, len: usize, ) -> Result<&'a mut [Self], ViewDataError> ``` ```APIDOC ### impl ViewMutElement for i16 #### fn bytes_as_mut_slice<'a>( bytes: &'a mut [u8], type_desc: &Value, len: usize, ) -> Result<&'a mut [Self], ViewDataError> ``` ```APIDOC ### impl ViewMutElement for i32 #### fn bytes_as_mut_slice<'a>( bytes: &'a mut [u8], type_desc: &Value, len: usize, ) -> Result<&'a mut [Self], ViewDataError> ``` ```APIDOC ### impl ViewMutElement for i64 #### fn bytes_as_mut_slice<'a>( bytes: &'a mut [u8], type_desc: &Value, len: usize, ) -> Result<&'a mut [Self], ViewDataError> ``` ```APIDOC ### impl ViewMutElement for u8 #### fn bytes_as_mut_slice<'a>( bytes: &'a mut [u8], type_desc: &PyValue, len: usize, ) -> Result<&'a mut [Self], ViewDataError> ``` ```APIDOC ### impl ViewMutElement for u16 #### fn bytes_as_mut_slice<'a>( bytes: &'a mut [u8], type_desc: &Value, len: usize, ) -> Result<&'a mut [Self], ViewDataError> ``` ```APIDOC ### impl ViewMutElement for u32 #### fn bytes_as_mut_slice<'a>( bytes: &'a mut [u8], type_desc: &Value, len: usize, ) -> Result<&'a mut [Self], ViewDataError> ``` ```APIDOC ### impl ViewMutElement for u64 #### fn bytes_as_mut_slice<'a>( bytes: &'a mut [u8], type_desc: &Value, len: usize, ) -> Result<&'a mut [Self], ViewDataError> ``` -------------------------------- ### Implement primitive types with one-byte descriptors Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npy/elements/num.rs.html A convenience macro to implement `WritableElement`, `ReadableElement`, `ViewElement`, and `ViewMutElement` for primitive types that use one-byte descriptors. It takes the element type, write descriptor, read descriptors, zero value, and read function. ```Rust macro_rules! impl_primitive_one_byte { ($elem:ty, $write_desc:expr, [$($read_desc:expr),*], $zero:expr, $read_into:ident) => { impl_writable_element_always_valid_cast!($elem, $write_desc, $write_desc); impl_readable_primitive_one_byte!($elem, [$($read_desc),*], $zero, $read_into); impl_view_and_view_mut_primitive_one_byte!($elem, [$($read_desc),*]); }; } ``` -------------------------------- ### Display Format for Header Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/header/struct.Header.html Implements the Display trait for the Header struct, enabling user-friendly string representation. ```rust fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> ``` -------------------------------- ### WritableElement Implementations Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.WritableElement.html This section details the implementations of the WritableElement trait for various primitive types and complex numbers, indicating compatibility with specific feature flags. ```APIDOC ### impl WritableElement for Complex Available on **crate feature`num-complex-0_4`** only. ### impl WritableElement for Complex Available on **crate feature`num-complex-0_4`** only. ### impl WritableElement for bool ### impl WritableElement for f32 ### impl WritableElement for f64 ### impl WritableElement for i8 ### impl WritableElement for i16 ### impl WritableElement for i32 ### impl WritableElement for i64 ### impl WritableElement for u8 ### impl WritableElement for u16 ### impl WritableElement for u32 ### impl WritableElement for u64 ``` -------------------------------- ### Reading .npz Files Source: https://docs.rs/ndarray-npy Provides functionality to read data from .npz (compressed archive) files. ```APIDOC ## Reading .npz Files This section covers reading data from `.npz` files. ### Reader - `NpzReader`: A struct for reading `.npz` files. ### Errors - `ReadNpzError`: Errors that can occur when reading a `.npz` file. ``` -------------------------------- ### WritableElement for u32 Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.WritableElement.html Implementation for writing 32-bit unsigned integers. ```rust impl WritableElement for u32 ``` -------------------------------- ### TryInto for T Implementation Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/header/enum.FormatHeaderError.html Performs a fallible conversion into another type. ```rust fn try_into(self) -> Result>::Error> ``` -------------------------------- ### Implement i8 and u8 primitive types Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npy/elements/num.rs.html Applies the `impl_primitive_one_byte` macro to i8 and u8. This sets up their reading, writing, and viewing capabilities with their respective NumPy descriptors and zero values. ```Rust impl_primitive_one_byte!(i8, "|i1", ["|i1", "i1", "b"], 0, read_i8_into); impl_primitive_one_byte!(u8, "|u1", ["|u1", "u1", "B"], 0, read_exact); ``` -------------------------------- ### HeaderLengthInfo Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npy/header.rs.html Information about the calculated header length. ```APIDOC struct HeaderLengthInfo { /// Total header length (including magic string, version number, header /// length value, array format description, padding, and final newline). total_len: usize, /// Formatted `HEADER_LEN` value. (This is the number of bytes in the array /// format description, padding, and final newline.) formatted_header_len: Vec, } ``` -------------------------------- ### create_new_npy Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/fn.create_new_npy.html Writes an array to a new .npy file at the specified path. Returns an error if the file already exists. This function is a shortcut for creating a new file and then writing the array using `WriteNpyExt`. ```APIDOC ## Function create_new_npy ### Description Writes an array to a new `.npy` file at the specified path; error if the file exists. This is a convenience function for `BufWriter::new(File::create_new(path)?)` followed by `WriteNpyExt::write_npy`. ### Signature ```rust pub fn create_new_npy(path: P, array: &T) -> Result<(), WriteNpyError> where P: AsRef, T: WriteNpyExt + ?Sized, ``` ### Example ```rust use ndarray::array; use ndarray_npy::create_new_npy; let arr = array![[1, 2, 3], [4, 5, 6]]; create_new_npy("new_array.npy", &arr)?; assert!(create_new_npy("new_array.npy", &arr).is_err()); ``` ``` -------------------------------- ### Reading .npz Files Source: https://docs.rs/ndarray-npy/0.10.0/index.html Provides functionality to read data from .npz files. ```APIDOC ## Reading .npz Files ### Description Provides functionality to read data from .npz files. ### Structs - `NpzReader`: Reader for `.npz` files. ### Errors - `ReadNpzError`: An error reading a `.npz` file. ``` -------------------------------- ### From for WriteNpzError Conversion (npz feature) Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/enum.WriteNpyError.html Shows the conversion from WriteNpyError to WriteNpzError, available when the 'npz' crate feature is enabled. This allows for error handling across both .npy and .npz file writing operations. ```rust impl From for WriteNpzError { fn from(err: WriteNpyError) -> WriteNpzError { WriteNpzError::Io(err.into()) } } ``` -------------------------------- ### TryFrom for T Implementation Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/header/enum.FormatHeaderError.html Performs a fallible conversion from one type to another. ```rust fn try_from(value: U) -> Result>::Error> ``` -------------------------------- ### Serialize Header to Bytes Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/header/struct.Header.html Returns the serialized byte representation of the header. Useful for writing .npy files. ```rust pub fn to_bytes(&self) -> Result, FormatHeaderError> ``` -------------------------------- ### WritableElement for u8 Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.WritableElement.html Implementation for writing 8-bit unsigned integers. ```rust impl WritableElement for u8 ``` -------------------------------- ### Reading and Writing .npy Files Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/lib.rs.html This section details the extension traits and convenience functions available for reading and writing .npy files, including options for zeroed data and readonly/mutable viewing. ```rust #!/usr/bin/env // This crate provides methods to read/write [`ndarray`]'s //! [`ArrayBase`](ndarray::ArrayBase) type from/to [`.npy`] and [`.npz`] files. //! //! See the [repository] for information about the default features and how to //! use this crate with Cargo. //! //! [repository]: https://github.com/jturner314/ndarray-npy //! [`ndarray`]: https://github.com/bluss/ndarray //! [`.npy`]: https://docs.scipy.org/doc/numpy/reference/generated/numpy.lib.format.html //! [`.npz`]: https://docs.scipy.org/doc/numpy/reference/generated/numpy.savez.html //! ``` -------------------------------- ### WritableElement for u16 Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/trait.WritableElement.html Implementation for writing 16-bit unsigned integers. ```rust impl WritableElement for u16 ``` -------------------------------- ### Generic CloneToUninit Trait Implementation (Nightly) Source: https://docs.rs/ndarray-npy/0.10.0/ndarray_npy/npy/header/struct.Header.html Implements the nightly-only CloneToUninit trait for cloning data into uninitialized memory. ```rust unsafe fn clone_to_uninit(&self, dest: *mut u8) ``` -------------------------------- ### Finish Writing NPZ File Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npz.rs.html Finalizes the NPZ file by calling finish on the zip writer and flush on the underlying writer. Essential for proper error handling. ```rust pub fn finish(self) -> Result { let mut writer = self.zip.finish()?; writer.flush().map_err(ZipError::from)?; Ok(writer) } ``` -------------------------------- ### finish Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npz.rs.html Finalizes the .npz file by writing zip structures and flushing the underlying writer. This method is crucial for error handling during the finalization process. ```APIDOC ## finish ### Description Completes the writing of the `.npz` file by calling `.finish()` on the internal zip writer and `.flush()` on the underlying writer. This ensures all data is written and zip structures are finalized. It's recommended to call this method explicitly to handle potential errors during finalization, as dropping the writer might silently ignore them. ### Method Signature `pub fn finish(self) -> Result` ### Type Parameters * `W`: The type of the underlying writer, which must implement `Write` and `Seek`. ### Returns * `Result`: The underlying writer if the process completed successfully, or an error if any part of the finalization failed. ``` -------------------------------- ### add_array_with_options Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npz.rs.html Adds an array to the .npz file with specified options, overriding any default options. The .npy extension is automatically appended to the name. ```APIDOC ## add_array_with_options ### Description Adds an array to the .npz file with custom file options. These options override any defaults set during the `NpzWriter` initialization. A `.npy` extension is appended to the provided name. ### Method Signature `pub fn add_array_with_options(&mut self, name: N, array: &T, options: FileOptions<'_, U>) -> Result<(), WriteNpzError>` ### Type Parameters * `N`: Type that can be converted into a `String` (used for the array name). * `T`: Type that implements `WriteNpyExt` (the array to be written). * `U`: Type that implements `FileOptionExtension` (specifies extension options for the file). ### Parameters * **name**: `N` - The name of the array to be added. * **array**: `&T` - A reference to the array to be written. * **options**: `FileOptions<'_, U>` - The file options to use for writing this specific array. ### Returns * `Result<(), WriteNpzError>`: Ok if the array was added successfully, or an error if writing failed. ``` -------------------------------- ### add_array Source: https://docs.rs/ndarray-npy/0.10.0/src/ndarray_npy/npz.rs.html Adds an array to the .npz file with default options. The .npy extension is automatically appended to the name. ```APIDOC ## add_array ### Description Adds an array to the .npz file using the default file options. ### Method Signature `pub fn add_array(&mut self, name: N, array: &T) -> Result<(), WriteNpzError>` ### Type Parameters * `N`: Type that can be converted into a `String` (used for the array name). * `T`: Type that implements `WriteNpyExt` (the array to be written). ### Parameters * **name**: `N` - The name of the array to be added. * **array**: `&T` - A reference to the array to be written. ### Returns * `Result<(), WriteNpzError>`: Ok if the array was added successfully, or an error if writing failed. ```