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: boolStream the payload in chunks instead of buffering it whole.
chunk_elements: usizeNumber 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: CastPolicyBehaviour when a forced cast would lose information.
format: Option<Format>Override format selection (otherwise taken from the file extension).
compression: CompressionCompression for container formats.
pickle_protocol: u8Pickle protocol to emit (2..=5). Default 4 — readable by Python 3.4+.
fortran_order: boolWrite arrays in Fortran (column-major) order.
Implementations§
Source§impl WriteOptions
impl WriteOptions
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a new value.
Examples found in repository?
More examples
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}Sourcepub fn chunk_elements(self, n: usize) -> Self
pub fn chunk_elements(self, n: usize) -> Self
Elements per chunk in lazy mode.
Sourcepub fn float_as(self, kind: FloatKind) -> Self
pub fn float_as(self, kind: FloatKind) -> Self
Force floats to kind.
Examples found in repository?
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}Sourcepub fn shape(self, shape: impl Into<Vec<usize>>) -> Self
pub fn shape(self, shape: impl Into<Vec<usize>>) -> Self
Pin the exact shape — (2,) and (1, 2) are different files.
Sourcepub fn cast_policy(self, p: CastPolicy) -> Self
pub fn cast_policy(self, p: CastPolicy) -> Self
Choose the overflow behaviour of forced casts.
Sourcepub fn compression(self, c: Compression) -> Self
pub fn compression(self, c: Compression) -> Self
Set container compression.
Sourcepub fn pickle_protocol(self, p: u8) -> Self
pub fn pickle_protocol(self, p: u8) -> Self
Choose the pickle protocol (clamped to 2..=5).
Source§impl WriteOptions
impl WriteOptions
Sourcepub fn fortran_order(self, yes: bool) -> Self
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
impl Clone for WriteOptions
Source§impl Debug for WriteOptions
impl Debug for WriteOptions
Auto Trait Implementations§
impl Freeze for WriteOptions
impl RefUnwindSafe for WriteOptions
impl Send for WriteOptions
impl Sync for WriteOptions
impl Unpin for WriteOptions
impl UnsafeUnpin for WriteOptions
impl UnwindSafe for WriteOptions
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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 more