Skip to main content

WriteOptions

Struct WriteOptions 

Source
pub struct WriteOptions {
    pub lazy: bool,
    pub chunk_elements: usize,
    pub int_as: Option<IntKind>,
    pub float_as: Option<FloatKind>,
    pub dtype: Option<DType>,
    pub shape: Option<Vec<usize>>,
    pub cast_policy: CastPolicy,
    pub format: Option<Format>,
    pub compression: Compression,
    pub pickle_protocol: u8,
    pub fortran_order: bool,
}
Expand description

Everything that influences how a value is encoded.

Fields§

§lazy: bool

Stream the payload in chunks instead of buffering it whole.

§chunk_elements: usize

Number of elements per chunk in lazy mode. Default: 1 Mi elements.

§int_as: Option<IntKind>

Force every integer to this width on the way out.

§float_as: Option<FloatKind>

Force every real float to this width on the way out.

§dtype: Option<DType>

Explicit target element type; wins over int_as / float_as.

§shape: Option<Vec<usize>>

Explicit target shape. numpy distinguishes shape = (2,) from shape = (1, 2), so this is how you pin the exact one you need.

§cast_policy: CastPolicy

Behaviour when a forced cast would lose information.

§format: Option<Format>

Override format selection (otherwise taken from the file extension).

§compression: Compression

Compression for container formats.

§pickle_protocol: u8

Pickle protocol to emit (2..=5). Default 4 — readable by Python 3.4+.

§fortran_order: bool

Write arrays in Fortran (column-major) order.

Implementations§

Source§

impl WriteOptions

Source

pub fn new() -> Self

Create a new value.

Examples found in repository?
examples/conv.rs (line 5)
2fn main() -> Result<()> {
3    let mut a = std::env::args().skip(1);
4    let (src, dst) = (a.next().unwrap(), a.next().unwrap());
5    convert(&src, &dst, &ReadOptions::new(), &WriteOptions::new())
6}
More examples
Hide additional examples
examples/gen.rs (line 48)
23fn main() -> Result<()> {
24    let out = std::path::PathBuf::from(std::env::args().nth(1).unwrap());
25    let mut d = Dict::new();
26    d.insert(Value::Str("w".into()), Value::Array(arr_f32()));
27    d.insert(Value::Str("b".into()), Value::Array(arr_i64()));
28    d.insert(Value::Str("x".into()), Value::Array(arr_f64()));
29    d.insert(Value::Str("q".into()), Value::Array(arr_u8()));
30    let dict = Value::Dict(d);
31
32    write(out.join("w.npy"), &Value::Array(arr_f32()))?;
33    write(out.join("w.npz"), &dict)?;
34    write(out.join("w.pkl"), &dict)?;
35    write(out.join("w.joblib"), &dict)?;
36    write(out.join("w.h5"), &dict)?;
37    write(out.join("w.pt"), &dict)?;
38
39    // nested group in HDF5
40    let mut inner = Dict::new();
41    inner.insert(Value::Str("deep".into()), Value::Array(arr_f32()));
42    let mut outer = Dict::new();
43    outer.insert(Value::Str("layer1".into()), Value::Dict(inner));
44    outer.insert(Value::Str("top".into()), Value::Array(arr_i64()));
45    write(out.join("nested.h5"), &Value::Dict(outer))?;
46
47    // down-cast on write: f64 source stored as f32
48    let opts = WriteOptions::new().float_as(FloatKind::F32);
49    write_with(out.join("down.npy"), &Value::Array(arr_f64()), &opts)?;
50
51    // lazy write
52    let mut lw = write_lazy(out.join("lazy.npy"), DType::F32, &[10, 100], &WriteOptions::new())?;
53    for c in 0..10 {
54        let chunk = Array::F32(
55            ArrayD::from_shape_vec(
56                ndarray::IxDyn(&[100]),
57                (0..100).map(|i| (c * 100 + i) as f32).collect(),
58            )
59            .unwrap(),
60        );
61        lw.push(&chunk)?;
62    }
63    lw.finish()?;
64
65    // validation must reject this
66    match write(out.join("bad.npy"), &Value::Str("not an array".into())) {
67        Err(e) => println!("rejected as expected: {e}"),
68        Ok(_) => println!("BUG: string accepted into .npy"),
69    }
70    println!("written");
71    Ok(())
72}
Source

pub fn lazy(self, yes: bool) -> Self

Enable chunked writing.

Source

pub fn chunk_elements(self, n: usize) -> Self

Elements per chunk in lazy mode.

Source

pub fn int_as(self, kind: IntKind) -> Self

Force integers to kind.

Source

pub fn float_as(self, kind: FloatKind) -> Self

Force floats to kind.

Examples found in repository?
examples/gen.rs (line 48)
23fn main() -> Result<()> {
24    let out = std::path::PathBuf::from(std::env::args().nth(1).unwrap());
25    let mut d = Dict::new();
26    d.insert(Value::Str("w".into()), Value::Array(arr_f32()));
27    d.insert(Value::Str("b".into()), Value::Array(arr_i64()));
28    d.insert(Value::Str("x".into()), Value::Array(arr_f64()));
29    d.insert(Value::Str("q".into()), Value::Array(arr_u8()));
30    let dict = Value::Dict(d);
31
32    write(out.join("w.npy"), &Value::Array(arr_f32()))?;
33    write(out.join("w.npz"), &dict)?;
34    write(out.join("w.pkl"), &dict)?;
35    write(out.join("w.joblib"), &dict)?;
36    write(out.join("w.h5"), &dict)?;
37    write(out.join("w.pt"), &dict)?;
38
39    // nested group in HDF5
40    let mut inner = Dict::new();
41    inner.insert(Value::Str("deep".into()), Value::Array(arr_f32()));
42    let mut outer = Dict::new();
43    outer.insert(Value::Str("layer1".into()), Value::Dict(inner));
44    outer.insert(Value::Str("top".into()), Value::Array(arr_i64()));
45    write(out.join("nested.h5"), &Value::Dict(outer))?;
46
47    // down-cast on write: f64 source stored as f32
48    let opts = WriteOptions::new().float_as(FloatKind::F32);
49    write_with(out.join("down.npy"), &Value::Array(arr_f64()), &opts)?;
50
51    // lazy write
52    let mut lw = write_lazy(out.join("lazy.npy"), DType::F32, &[10, 100], &WriteOptions::new())?;
53    for c in 0..10 {
54        let chunk = Array::F32(
55            ArrayD::from_shape_vec(
56                ndarray::IxDyn(&[100]),
57                (0..100).map(|i| (c * 100 + i) as f32).collect(),
58            )
59            .unwrap(),
60        );
61        lw.push(&chunk)?;
62    }
63    lw.finish()?;
64
65    // validation must reject this
66    match write(out.join("bad.npy"), &Value::Str("not an array".into())) {
67        Err(e) => println!("rejected as expected: {e}"),
68        Ok(_) => println!("BUG: string accepted into .npy"),
69    }
70    println!("written");
71    Ok(())
72}
Source

pub fn dtype(self, dt: DType) -> Self

Pin the exact element type.

Source

pub fn shape(self, shape: impl Into<Vec<usize>>) -> Self

Pin the exact shape — (2,) and (1, 2) are different files.

Source

pub fn cast_policy(self, p: CastPolicy) -> Self

Choose the overflow behaviour of forced casts.

Source

pub fn format(self, fmt: Format) -> Self

Skip extension sniffing and use fmt.

Source

pub fn compression(self, c: Compression) -> Self

Set container compression.

Source

pub fn pickle_protocol(self, p: u8) -> Self

Choose the pickle protocol (clamped to 2..=5).

Source§

impl WriteOptions

Source

pub fn fortran_order(self, yes: bool) -> Self

Write .npy payloads in column-major (Fortran) order.

numpy reads either order transparently; this only changes how the bytes are laid out on disk, and the header records which was used.

Trait Implementations§

Source§

impl Clone for WriteOptions

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for WriteOptions

Source§

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

Formats the value using the given formatter. Read more
Source§

impl Default for WriteOptions

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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 T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.