Struct ndarray_npz::NpzView

source ·
pub struct NpzView<'a> { /* private fields */ }
Expand description

Immutable view for memory-mapped .npz files.

The primary use-case for this is viewing .npy files within a memory-mapped .npz archive.

Notes

  • For types for which not all bit patterns are valid, such as bool, the implementation iterates over all of the elements when creating the view to ensure they have a valid bit pattern.
  • The data in the buffer containing an .npz archive must be properly aligned for its .npy file with the maximum alignment requirement for its element type. Typically, this should not be a concern for memory-mapped files (unless an option like MAP_FIXED is used), since memory mappings are usually aligned to a page boundary.
  • The .npy files within the .npz archive must be properly aligned for their element type. Archives not created by this crate can be aligned with the help of the CLI tool rezip as in rezip in.npz -o out.npz.

Example

This is an example of opening an immutably memory-mapped .npz archive as an NpzView providing an NpyView for each non-compressed and non-encrypted .npy file within the archive which can be accessed via NpyView::view as immutable ArrayView.

This example uses the memmap2 crate because that appears to be the best-maintained memory-mapping crate at the moment, but Self::new takes a &mut [u8] instead of a file so that you can use the memory-mapping crate you’re most comfortable with.

use std::fs::OpenOptions;

use memmap2::MmapOptions;
use ndarray::Ix1;
use ndarray_npz::{NpzView, ViewNpzError};

// Open `.npz` archive of non-compressed and non-encrypted `.npy` files in
// native endian.
#[cfg(target_endian = "little")]
let file = OpenOptions::new()
	.read(true)
	.open("tests/examples_little_endian_64_byte_aligned.npz")
	.unwrap();
#[cfg(target_endian = "big")]
let file = OpenOptions::new()
	.read(true)
	.open("tests/examples_big_endian_64_byte_aligned.npz")
	.unwrap();
// Memory-map `.npz` archive of 64-byte aligned `.npy` files.
let mmap = unsafe { MmapOptions::new().map(&file).unwrap() };
let npz = NpzView::new(&mmap)?;
// List non-compressed and non-encrypted files only.
for npy in npz.names() {
	println!("{}", npy);
}
// Get immutable `.npy` views.
let mut x_npy_view = npz.by_name("i64.npy")?;
let mut y_npy_view = npz.by_name("f64.npy")?;
// Optionally verify CRC-32 checksums.
x_npy_view.verify()?;
y_npy_view.verify()?;
// Get and print immutable `ArrayView`s.
let x_array_view = x_npy_view.view::<i64, Ix1>()?;
let y_array_view = y_npy_view.view::<f64, Ix1>()?;
println!("{}", x_array_view);
println!("{}", y_array_view);

Implementations§

source§

impl<'a> NpzView<'a>

source

pub fn new(bytes: &'a [u8]) -> Result<Self, ViewNpzError>

Creates a new immutable view of a memory-mapped .npz file.

Errors

Viewing an archive can fail with ZipError.

source

pub fn is_empty(&self) -> bool

Returns true iff the .npz file doesn’t contain any viewable arrays.

Viewable arrays are neither directories, nor compressed, nor encrypted.

source

pub fn len(&self) -> usize

Returns the number of viewable arrays in the .npz file.

Viewable arrays are neither directories, nor compressed, nor encrypted.

source

pub fn names(&self) -> impl Iterator<Item = &str>

Returns the names of all of the viewable arrays in the .npz file.

Viewable arrays are neither directories, nor compressed, nor encrypted.

source

pub fn directory_names(&self) -> impl Iterator<Item = &str>

Returns the names of all of the directories in the .npz file.

source

pub fn compressed_names(&self) -> impl Iterator<Item = &str>

Returns the names of all of the compressed files in the .npz file.

source

pub fn encrypted_names(&self) -> impl Iterator<Item = &str>

Returns the names of all of the encrypted files in the .npz file.

source

pub fn by_name(&self, name: &str) -> Result<NpyView<'a>, ViewNpzError>

Returns an immutable .npy file view by name.

Errors

Viewing an .npy file can fail with ViewNpyError. Trying to view a directory, compressed file, or encrypted file, fails with ViewNpzError::Directory, ViewNpzError::CompressedFile, or ViewNpzError::CompressedFile. Fails with ZipError::FileNotFound if the name is not found.

source

pub fn by_index(&self, index: usize) -> Result<NpyView<'a>, ViewNpzError>

Returns an immutable .npy file view by index in 0..len().

The index does not necessarily correspond to the index of the zip archive as directories, compressed files, and encrypted files are skipped.

Errors

Viewing an .npy file can fail with ViewNpyError. Fails with ZipError::FileNotFound if the index is not found.

Trait Implementations§

source§

impl<'a> Clone for NpzView<'a>

source§

fn clone(&self) -> NpzView<'a>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a> Debug for NpzView<'a>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> RefUnwindSafe for NpzView<'a>

§

impl<'a> Send for NpzView<'a>

§

impl<'a> Sync for NpzView<'a>

§

impl<'a> Unpin for NpzView<'a>

§

impl<'a> UnwindSafe for NpzView<'a>

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.