1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use {ReadNpyError, ReadNpyExt, ReadableElement, WritableElement, WriteNpyError, WriteNpyExt};
use ndarray::{Data, DataOwned};
use ndarray::prelude::*;
use std::error::Error;
use std::io::{self, Read, Seek, Write};
use zip::{CompressionMethod, ZipArchive, ZipWriter};
use zip::result::ZipError;
use zip::write::FileOptions;
quick_error! {
#[derive(Debug)]
pub enum WriteNpzError {
Io(err: io::Error) {
description("I/O error")
display(x) -> ("{}: {}", x.description(), err)
cause(err)
from()
}
Zip(err: ZipError) {
description("zip file error")
display(x) -> ("{}: {}", x.description(), err)
cause(err)
from()
}
Npy(err: WriteNpyError) {
description("error writing npy file to npz archive")
display(x) -> ("{}: {}", x.description(), err)
cause(err)
from()
}
}
}
pub struct NpzWriter<W: Write + Seek> {
zip: ZipWriter<W>,
options: FileOptions,
}
impl<W: Write + Seek> NpzWriter<W> {
pub fn new(writer: W) -> NpzWriter<W> {
NpzWriter {
zip: ZipWriter::new(writer),
options: FileOptions::default().compression_method(CompressionMethod::Stored),
}
}
#[cfg(feature = "compressed_npz")]
pub fn new_compressed(writer: W) -> NpzWriter<W> {
NpzWriter {
zip: ZipWriter::new(writer),
options: FileOptions::default().compression_method(CompressionMethod::Deflated),
}
}
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,
{
self.zip.start_file(name, self.options)?;
array.write_npy(&mut self.zip)?;
Ok(())
}
}
quick_error! {
#[derive(Debug)]
pub enum ReadNpzError {
Io(err: io::Error) {
description("I/O error")
display(x) -> ("{}: {}", x.description(), err)
cause(err)
from()
}
Zip(err: ZipError) {
description("zip file error")
display(x) -> ("{}: {}", x.description(), err)
cause(err)
from()
}
Npy(err: ReadNpyError) {
description("error reading npy file in npz archive")
display(x) -> ("{}: {}", x.description(), err)
cause(err)
from()
}
}
}
pub struct NpzReader<R: Read + Seek> {
zip: ZipArchive<R>,
}
impl<R: Read + Seek> NpzReader<R> {
pub fn new(reader: R) -> Result<NpzReader<R>, ReadNpzError> {
Ok(NpzReader {
zip: ZipArchive::new(reader)?,
})
}
pub fn len(&self) -> usize {
self.zip.len()
}
pub fn names(&mut self) -> Result<Vec<String>, ReadNpzError> {
Ok((0..self.zip.len())
.map(|i| Ok(self.zip.by_index(i)?.name().to_owned()))
.collect::<Result<_, ZipError>>()?)
}
pub fn by_name<S, D>(&mut self, name: &str) -> Result<ArrayBase<S, D>, ReadNpzError>
where
S::Elem: ReadableElement,
S: DataOwned,
D: Dimension,
{
Ok(ArrayBase::<S, D>::read_npy(self.zip.by_name(name)?)?)
}
pub fn by_index<S, D>(&mut self, index: usize) -> Result<ArrayBase<S, D>, ReadNpzError>
where
S::Elem: ReadableElement,
S: DataOwned,
D: Dimension,
{
Ok(ArrayBase::<S, D>::read_npy(self.zip.by_index(index)?)?)
}
}