pub struct NpzWriter<W>{ /* 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::{array, aview0, Array1, Array2};
use ndarray_npy::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> NpzWriter<W>
impl<W> NpzWriter<W>
Sourcepub fn new(writer: W) -> NpzWriter<W>
pub fn new(writer: W) -> NpzWriter<W>
Create a new .npz file without compression. See numpy.savez.
Sourcepub fn new_compressed(writer: W) -> NpzWriter<W>
pub fn new_compressed(writer: W) -> NpzWriter<W>
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.
Sourcepub fn new_with_options(
writer: W,
options: FileOptions<'static, ()>,
) -> NpzWriter<W>
pub fn new_with_options( writer: W, options: FileOptions<'static, ()>, ) -> NpzWriter<W>
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.
Sourcepub fn add_array_with_options<N, T, U>(
&mut self,
name: N,
array: &T,
options: FileOptions<'_, U>,
) -> Result<(), WriteNpzError>
pub fn add_array_with_options<N, T, U>( &mut self, name: N, array: &T, options: FileOptions<'_, U>, ) -> Result<(), WriteNpzError>
Adds an array with the specified name and options to the .npz file.
The specified options override those passed to the NpzWriter constructor (if any).
Note that a .npy extension will be appended to name; this matches NumPy’s behavior.
To write a scalar value, create a zero-dimensional array using arr0 or
aview0.
Sourcepub fn finish(self) -> Result<W, WriteNpzError>
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.
Auto Trait Implementations§
impl<W> Freeze for NpzWriter<W>where
W: Freeze,
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CheckedAs for T
impl<T> CheckedAs for T
Source§fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
fn checked_as<Dst>(self) -> Option<Dst>where
T: CheckedCast<Dst>,
Source§impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
impl<Src, Dst> CheckedCastFrom<Src> for Dstwhere
Src: CheckedCast<Dst>,
Source§fn checked_cast_from(src: Src) -> Option<Dst>
fn checked_cast_from(src: Src) -> Option<Dst>
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> OverflowingAs for T
impl<T> OverflowingAs for T
Source§fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
fn overflowing_as<Dst>(self) -> (Dst, bool)where
T: OverflowingCast<Dst>,
Source§impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
impl<Src, Dst> OverflowingCastFrom<Src> for Dstwhere
Src: OverflowingCast<Dst>,
Source§fn overflowing_cast_from(src: Src) -> (Dst, bool)
fn overflowing_cast_from(src: Src) -> (Dst, bool)
Source§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
Source§impl<T> SaturatingAs for T
impl<T> SaturatingAs for T
Source§fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
fn saturating_as<Dst>(self) -> Dstwhere
T: SaturatingCast<Dst>,
Source§impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
impl<Src, Dst> SaturatingCastFrom<Src> for Dstwhere
Src: SaturatingCast<Dst>,
Source§fn saturating_cast_from(src: Src) -> Dst
fn saturating_cast_from(src: Src) -> Dst
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.