Struct ndarray_npz::NpzWriter

source ·
pub struct NpzWriter<W: Write + Seek> { /* private fields */ }
Expand description

Writer for .npz files.

Note that the inner ZipWriter is wrapped in a BufWriter when writing each array with .add_array(). If desired, you could additionally buffer the innermost writer (e.g. the File when writing to a file) by wrapping it in a BufWriter. This may be somewhat beneficial if the arrays are large and have non-standard layouts but may decrease performance if the arrays have standard or Fortran layout, so it’s not recommended without testing to compare.

Example

use ndarray_npz::{
	ndarray::{array, aview0, Array1, Array2},
	NpzWriter,
};
use std::fs::File;

let mut npz = NpzWriter::new(File::create("arrays.npz")?);
let a: Array2<i32> = array![[1, 2, 3], [4, 5, 6]];
let b: Array1<i32> = array![7, 8, 9];
npz.add_array("a", &a)?;
npz.add_array("b", &b)?;
npz.add_array("c", &aview0(&10))?;
npz.finish()?;

Implementations§

source§

impl<W: Write + Seek> NpzWriter<W>

source

pub fn new(writer: W) -> NpzWriter<W>

Creates a new .npz file without compression. See numpy.savez.

Ensures .npy files are 64-byte aligned for memory-mapping via NpzView/NpzViewMut.

source

pub fn new_compressed(writer: W) -> NpzWriter<W>

Available on crate feature compressed only.

Creates a new .npz file with compression. See numpy.savez_compressed.

source

pub fn add_array<N, S, D>( &mut self, name: N, array: &ArrayBase<S, D> ) -> Result<(), WriteNpzError>where N: Into<String>, S::Elem: WritableElement, S: Data, D: Dimension,

Adds an array with the specified name to the .npz file.

To write a scalar value, create a zero-dimensional array using arr0 or aview0.

Errors

Adding an array can fail with WriteNpyError.

source

pub fn finish(self) -> Result<W, WriteNpzError>

Calls .finish() on the zip file and .flush() on the writer, and then returns the writer.

This finishes writing the remaining zip structures and flushes the writer. While dropping will automatically attempt to finish the zip file and (for writers that flush on drop, such as BufWriter) flush the writer, any errors that occur during drop will be silently ignored. So, it’s necessary to call .finish() to properly handle errors.

Errors

Finishing the zip archive can fail with ZipError.

Auto Trait Implementations§

§

impl<W> RefUnwindSafe for NpzWriter<W>where W: RefUnwindSafe,

§

impl<W> Send for NpzWriter<W>where W: Send,

§

impl<W> Sync for NpzWriter<W>where W: Sync,

§

impl<W> Unpin for NpzWriter<W>where W: Unpin,

§

impl<W> UnwindSafe for NpzWriter<W>where W: UnwindSafe,

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, 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.