npyz 0.9.0

NumPy file format (de-)serialization. Fork of outdated npy-rs.
Documentation
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
//! Tools for reading and writing Scipy sparse matrices in NPZ format.
//!
//! ```rust
//! use std::io;
//! use npyz::sparse;
//!
//! fn main() -> io::Result<()> {
//!     let mut npz = npyz::npz::NpzArchive::open("test-data/sparse/csr.npz")?;
//!     let mat = sparse::Csr::<i64>::from_npz(&mut npz)?;
//!
//!     // sparse matrices have public fields named after the attributes found in scipy.
//!     // read or manipulate them however you like!
//!     let sparse::Csr { data, indices, indptr, shape } = &mat;
//!     println!("Shape: {:?}", shape);
//!     println!("Indices: {:?}", indices);
//!     println!("Indptr: {:?}", indptr);
//!     println!("Data: {:?}", data);
//!
//!     // write to any io::Write
//!     let writer = io::BufWriter::new(std::fs::File::create("examples/output/sparse-doctest.npz")?);
//!     mat.write_npz(&mut npyz::npz::NpzWriter::new(writer))?;
//!     Ok(())
//! }
//! ```
//!
//! No methods are provided on these types beyond reading and writing.  If you want to do sparse
//! matrix math, then you should use the data you have read to construct a matrix type from a
//! dedicated sparse matrix library.
//!
//! For instance, an example of how to use this module to save and load CSR matrices from the
//! [`sprs`](https://crates.io/crates/sprs) crate can be found
//! [in the examples directory](https://github.com/ExpHP/npyz/tree/master/examples).
//!
//! _This module requires the **`"npz"`** feature._

use std::io;
use std::ops::Deref;

use zip::read::ZipFile;

use crate::serialize::{Deserialize, AutoSerialize};
use crate::read::{Order, NpyFile};
use crate::write::{WriterBuilder};
use crate::npz::{NpzArchive, NpzWriter};
use crate::header::DType;

// =============================================================================
// Types

/// Raw representation of a scipy sparse matrix whose exact format is known at runtime.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SparseBase<T, Data, Indices, Indptr, Offsets>
where  // note: explicit 'where' makes rustdoc less intimidating
    Data: Deref<Target=[T]>,
    Indices: AsRef<[u64]>,
    Indptr: AsRef<[usize]>,
    Offsets: AsRef<[i64]>,
{
    /// The matrix is in COOrdinate format.
    Coo(CooBase<T, Data, Indices>),
    /// The matrix is in Compressed Sparse Row format.
    Csr(CsrBase<T, Data, Indices, Indptr>),
    /// The matrix is in Compressed Sparse Column format.
    Csc(CscBase<T, Data, Indices, Indptr>),
    /// The matrix is in DIAgonal format.
    Dia(DiaBase<T, Data, Offsets>),
    /// The matrix is in Block Sparse Row format.
    Bsr(BsrBase<T, Data, Indices, Indptr>),
}

/// A sparse matrix (of type known at runtime) that owns its data.
///
/// This is an enum.  Please consult [`SparseBase`] to see the list of variants.
pub type Sparse<T> = SparseBase<T, Vec<T>, Vec<u64>, Vec<usize>, Vec<i64>>;

/// Raw representation of a [`scipy.sparse.coo_matrix`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.coo_matrix.html).
///
/// In spirit, each field is simply a Vec. (see the type alias [`Coo`]).
/// This generic base class exists in order to allow you to use slices when writing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CooBase<T, Data, Indices>
where  // note: explicit 'where' makes rustdoc less intimidating
    Data: Deref<Target=[T]>,
    Indices: AsRef<[u64]>,
{
    /// Dimensions of the matrix `[nrow, ncol]`.
    pub shape: [u64; 2],
    /// A vector of length `nnz` containing all of the stored elements.
    pub data: Data,
    /// A vector of length `nnz` indicating the row of each element.
    pub row: Indices,
    /// A vector of length `nnz` indicating the column of each element.
    pub col: Indices,
}

/// A COO matrix that owns its data.
///
/// Please consult the documentation of [`CooBase`] to see the list of fields publicly available on this type.
pub type Coo<T> = CooBase<T, Vec<T>, Vec<u64>>;

/// Raw representation of a [`scipy.sparse.csr_matrix`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csr_matrix.html).
///
/// In spirit, each field is simply a Vec. (see the type alias [`Csr`]).
/// This generic base class exists in order to allow you to use slices when writing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CsrBase<T, Data, Indices, Indptr>
where  // note: explicit 'where' makes rustdoc less intimidating
    Data: Deref<Target=[T]>,
    Indices: AsRef<[u64]>,
    Indptr: AsRef<[usize]>,
{
    /// Dimensions of the matrix `[nrow, ncol]`.
    pub shape: [u64; 2],
    /// A vector of length `nnz` containing all of the nonzero elements, sorted by row.
    pub data: Data,
    /// A vector of length `nnz` indicating the column of each element.
    ///
    /// **Beware:** scipy **does not** require or guarantee that the column indices within each row are sorted.
    pub indices: Indices,
    /// A vector of length `nrow + 1` indicating the indices that partition [`Self::data`]
    /// and [`Self::indices`] into data for each row.
    ///
    /// Typically, the elements are nondecreasing, with the first equal to 0 and the final equal
    /// to `nnz` (though the set of requirements that are actually *validated* by scipy are
    /// weaker and somewhat arbitrary).
    pub indptr: Indptr,
}

/// A CSR matrix that owns its data.
///
/// Please consult the documentation of [`CsrBase`] to see the list of fields publicly available on this type.
pub type Csr<T> = CsrBase<T, Vec<T>, Vec<u64>, Vec<usize>>;

/// Raw representation of a [`scipy.sparse.csc_matrix`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.csc_matrix.html).
///
/// In spirit, each field is simply a Vec. (see the type alias [`Csc`]).
/// This generic base class exists in order to allow you to use slices when writing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct CscBase<T, Data, Indices, Indptr>
where  // note: explicit 'where' makes rustdoc less intimidating
    Data: Deref<Target=[T]>,
    Indices: AsRef<[u64]>,
    Indptr: AsRef<[usize]>,
{
    /// Dimensions of the matrix `[nrow, ncol]`.
    pub shape: [u64; 2],
    /// A vector of length `nnz` containing all of the nonzero elements, sorted by column.
    pub data: Data,
    /// A vector of length `nnz` indicating the row of each element.
    ///
    /// **Beware:** scipy **does not** require or guarantee that the row indices within each column are sorted.
    pub indices: Indices,
    /// A vector of length `ncol + 1` indicating the indices that partition [`Self::data`]
    /// and [`Self::indices`] into data for each column.
    ///
    /// Typically, the elements are nondecreasing, with the first equal to 0 and the final equal
    /// to `nnz` (though the set of requirements that are actually *validated* by scipy are
    /// weaker and somewhat arbitrary).
    pub indptr: Indptr,
}

/// A CSC matrix that owns its data.
///
/// Please consult the documentation of [`CscBase`] to see the list of fields publicly available on this type.
pub type Csc<T> = CscBase<T, Vec<T>, Vec<u64>, Vec<usize>>;

/// Raw representation of a [`scipy.sparse.dia_matrix`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.dia_matrix.html).
///
/// In spirit, each field is simply a Vec. (see the type alias [`Dia`]).
/// This generic base class exists in order to allow you to use slices when writing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DiaBase<T, Data, Offsets>
where  // note: explicit 'where' makes rustdoc less intimidating
    Data: Deref<Target=[T]>,
    Offsets: AsRef<[i64]>,
{
    /// Dimensions of the matrix `[nrow, ncol]`.
    pub shape: [u64; 2],
    /// Contains the C-order data of a shape `[nnzd, length]` ndarray.
    ///
    /// Scipy's own documentation is lackluster, but the value of `length` appears to be any
    /// value `0 <= length <= ncol` and is typically 1 greater than the rightmost column that
    /// contains a nonzero entry.  The values in each diagonal appear to be stored at an index
    /// equal to their column.
    pub data: Data,
    /// A vector of length `nnzd` indicating which diagonal is stored in each row of `data`.
    ///
    /// Negative offsets are below the main diagonal.  Offsets can appear in any order.
    pub offsets: Offsets,
}

/// A DIA matrix that owns its data.
///
/// Please consult the documentation of [`DiaBase`] to see the list of fields publicly available on this type.
pub type Dia<T> = DiaBase<T, Vec<T>, Vec<i64>>;

/// Raw representation of a [`scipy.sparse.bsr_matrix`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.bsr_matrix.html).
///
/// In spirit, each field is simply a Vec. (see the type alias [`Bsr`]).
/// This generic base class exists in order to allow you to use slices when writing.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BsrBase<T, Data, Indices, Indptr>
where  // note: explicit 'where' makes rustdoc less intimidating
    Data: Deref<Target=[T]>,
    Indices: AsRef<[u64]>,
    Indptr: AsRef<[usize]>,
{
    /// Dimensions of the matrix `[nrow, ncol]`.
    ///
    /// These dimensions must be divisible by the respective elements of `blocksize`.
    pub shape: [u64; 2],
    /// Size of the blocks in the matrix.
    pub blocksize: [usize; 2],

    /// Contains the C-order data of a shape `[nnzb, block_nrow, block_ncol]` ndarray.
    ///
    /// (effectively concatenating the flattened data of all nonzero blocks, sorted by superrow)
    pub data: Data,
    /// A vector of length `nnzb` indicating the supercolumn index of each block.
    ///
    /// **Beware:** scipy **does not** require or guarantee that the column indices within each row are sorted.
    pub indices: Indices,
    /// A vector of length `(nrow / block_nrow) + 1` indicating the indices which partition
    /// [`Self::indices`] and the outermost axis of [`Self::data`] into data for each superrow.
    ///
    /// Typically, the elements are nondecreasing, with the first equal to 0 and the final equal
    /// to `nnzb` (though the set of requirements that are actually *validated* by scipy are
    /// weaker and somewhat arbitrary).
    pub indptr: Indptr,
}

/// A BSR matrix that owns its data.
///
/// Please consult the documentation of [`BsrBase`] to see the list of fields publicly available on this type.
pub type Bsr<T> = BsrBase<T, Vec<T>, Vec<u64>, Vec<usize>>;

// =============================================================================
// Reading

impl<T: Deserialize> Sparse<T> {
    /// Read a sparse matrix saved by `scipy.sparse.save_npz`.
    pub fn from_npz<R: io::Read + io::Seek>(npz: &mut NpzArchive<R>) -> io::Result<Self> {
        let format = extract_scalar::<Vec<u8>, _>(npz, "format")?;

        match &format[..] {
            b"coo" => Ok(Sparse::Coo(Coo::from_npz(npz)?)),
            b"csc" => Ok(Sparse::Csc(Csc::from_npz(npz)?)),
            b"csr" => Ok(Sparse::Csr(Csr::from_npz(npz)?)),
            b"dia" => Ok(Sparse::Dia(Dia::from_npz(npz)?)),
            b"bsr" => Ok(Sparse::Bsr(Bsr::from_npz(npz)?)),
            _ => Err(invalid_data(format_args!("bad format: {}", show_format(&format[..])))),
        }
    }
}

impl<T: Deserialize> Coo<T> {
    /// Read a sparse `coo_matrix` saved by `scipy.sparse.save_npz`.
    pub fn from_npz<R: io::Read + io::Seek>(npz: &mut NpzArchive<R>) -> io::Result<Self> {
        expect_format(npz, "coo")?;
        let shape = extract_shape(npz, "shape")?;
        let row = extract_indices(npz, "row")?;
        let col = extract_indices(npz, "col")?;
        let data = extract_1d::<T, _>(npz, "data")?;
        Ok(Coo { data, shape, row, col })
    }
}

impl<T: Deserialize> Csr<T> {
    /// Read a sparse `csr_matrix` saved by `scipy.sparse.save_npz`.
    pub fn from_npz<R: io::Read + io::Seek>(npz: &mut NpzArchive<R>) -> io::Result<Self> {
        expect_format(npz, "csr")?;
        let shape = extract_shape(npz, "shape")?;
        let indices = extract_indices(npz, "indices")?;
        let indptr = extract_usize_indices(npz, "indptr")?;
        let data = extract_1d::<T, _>(npz, "data")?;
        Ok(Csr { data, shape, indices, indptr })
    }
}

impl<T: Deserialize> Csc<T> {
    /// Read a sparse `csc_matrix` saved by `scipy.sparse.save_npz`.
    pub fn from_npz<R: io::Read + io::Seek>(npz: &mut NpzArchive<R>) -> io::Result<Self> {
        expect_format(npz, "csc")?;
        let shape = extract_shape(npz, "shape")?;
        let indices = extract_indices(npz, "indices")?;
        let indptr = extract_usize_indices(npz, "indptr")?;
        let data = extract_1d::<T, _>(npz, "data")?;
        Ok(Csc { data, shape, indices, indptr })
    }
}

impl<T: Deserialize> Dia<T> {
    /// Read a sparse `dia_matrix` saved by `scipy.sparse.save_npz`.
    pub fn from_npz<R: io::Read + io::Seek>(npz: &mut NpzArchive<R>) -> io::Result<Self> {
        expect_format(npz, "dia")?;
        let shape = extract_shape(npz, "shape")?;
        let offsets = extract_signed_indices(npz, "offsets")?;
        let (data, _) = extract_nd::<T, _>(npz, "data", 2)?;
        Ok(Dia { data, shape, offsets })
    }
}

impl<T: Deserialize> Bsr<T> {
    /// Read a sparse `bsr_matrix` saved by `scipy.sparse.save_npz`.
    pub fn from_npz<R: io::Read + io::Seek>(npz: &mut NpzArchive<R>) -> io::Result<Self> {
        expect_format(npz, "bsr")?;
        let shape = extract_shape(npz, "shape")?;
        let indices = extract_indices(npz, "indices")?;
        let indptr = extract_usize_indices(npz, "indptr")?;
        let (data, data_shape) = extract_nd::<T, _>(npz, "data", 3)?;
        let blocksize = [data_shape[1], data_shape[2]];
        Ok(Bsr { data, shape, indices, indptr, blocksize })
    }
}

// -----

fn show_format(format: &[u8]) -> String {
    let str = format.iter().map(|&b| match b {
        // ASCII printable
        0x20..=0x7f => std::str::from_utf8(&[b]).unwrap().to_string(),
        _ => format!("\\x{:02X}", b),
    }).collect::<Vec<_>>().join("");

    format!("'{}'", str)
}

fn expect_format<R: io::Read + io::Seek>(npz: &mut NpzArchive<R>, expected: &str) -> io::Result<()> {
    let format: Vec<u8> = extract_scalar(npz, "format")?;
    if format != expected.as_bytes() {
        return Err(invalid_data(format_args!("wrong format: expected '{}', got {}", expected, show_format(&format))))
    }
    Ok(())
}

fn extract_scalar<T: Deserialize, R: io::Read + io::Seek>(npz: &mut NpzArchive<R>, name: &str) -> io::Result<T> {
    let npy = extract_and_check_ndim(npz, name, 0)?;
    Ok(npy.into_vec::<T>()?.into_iter().next().expect("scalar so must have 1 elem"))
}

fn extract_shape<R: io::Read + io::Seek>(npz: &mut NpzArchive<R>, name: &str) -> io::Result<[u64; 2]> {
    let shape = extract_indices(npz, name)?;
    if shape.len() != 2 {
        return Err(invalid_data(format_args!("invalid length for '{}' (got {}, expected 2)", name, shape.len())))
    }
    Ok([shape[0], shape[1]])
}

fn extract_usize_indices<R: io::Read + io::Seek>(npz: &mut NpzArchive<R>, name: &str) -> io::Result<Vec<usize>> {
    Ok(extract_indices(npz, name)?.into_iter().map(|x| x as usize).collect())
}

// Read indices from npz which may be i32 or i64, but are nonnegative.
// FIXME: in the future we may allow automatic widening during deserialization, in which case
//        this can be simplified extract_1d::<u64>
fn extract_indices<R: io::Read + io::Seek>(npz: &mut NpzArchive<R, >, name: &str) -> io::Result<Vec<u64>> {
    let npy = extract_and_check_ndim(npz, name, 1)?;
    match npy.try_data::<i32>() {
        Ok(data) => data.map(|result| result.map(|x| x as u64)).collect(),
        Err(npy) => match npy.try_data::<i64>() {
            Ok(data) => data.map(|result| result.map(|x| x as u64)).collect(),
            Err(npy) => Err(invalid_data(format_args!("invalid dtype for '{}' in sparse matrix: {}", name, npy.dtype().descr()))),
        },
    }
}

// Read indices from npz which may be i32 or i64.
// FIXME: in the future we may allow automatic widening during deserialization, in which case
//        this can be replaced with extract_1d::<i64>
fn extract_signed_indices<R: io::Read + io::Seek>(npz: &mut NpzArchive<R>, name: &str) -> io::Result<Vec<i64>> {
    let npy = extract_and_check_ndim(npz, name, 1)?;
    match npy.try_data::<i32>() {
        Ok(data) => data.map(|result| result.map(|x| x as i64)).collect(),
        Err(npy) => match npy.try_data::<i64>() {
            Ok(data) => data.collect(),
            Err(npy) => Err(invalid_data(format_args!("invalid dtype for '{}' in sparse matrix: {}", name, npy.dtype().descr()))),
        },
    }
}

fn extract_1d<T: Deserialize, R: io::Read + io::Seek>(npz: &mut NpzArchive<R>, name: &str) -> io::Result<Vec<T>> {
    let npy = extract_and_check_ndim(npz, name, 1)?;
    npy.into_vec::<T>()
}

fn extract_nd<T: Deserialize, R: io::Read + io::Seek>(npz: &mut NpzArchive<R>, name: &str, expected_ndim: usize) -> io::Result<(Vec<T>, Vec<usize>)> {
    let npy = extract_and_check_ndim(npz, name, expected_ndim)?;
    if npy.order() != Order::C {
        return Err(invalid_data(format_args!("fortran order is not currently supported for array '{}' in sparse NPZ file", name)));
    }
    let shape = npy.shape().iter().map(|&x| x as usize).collect();
    let data = npy.into_vec::<T>()?;
    Ok((data, shape))
}

fn extract_and_check_ndim<'a, R: io::Read + io::Seek>(npz: &'a mut NpzArchive<R>, name: &str, expected_ndim: usize) -> io::Result<NpyFile<ZipFile<'a>>> {
    let npy = npz.by_name(name)?.ok_or_else(|| invalid_data(format_args!("missing array '{}' from sparse array", name)))?;
    let ndim = npy.shape().len();
    if ndim != expected_ndim {
        return Err(invalid_data(format_args!("invalid ndim for {}: {} (expected {})", name, ndim, expected_ndim)));
    }
    Ok(npy)
}

fn invalid_data<S: ToString>(s: S) -> io::Error {
    io::Error::new(io::ErrorKind::InvalidData, s.to_string())
}

// =============================================================================
// Writing

impl<T, Data, Indices, Indptr, Offsets> SparseBase<T, Data, Indices, Indptr, Offsets>
where
    T: AutoSerialize,
    Data: Deref<Target=[T]>,
    Indices: AsRef<[u64]>,
    Indptr: AsRef<[usize]>,
    Offsets: AsRef<[i64]>
{
    /// Write a sparse matrix, like `scipy.sparse.save_npz`.
    pub fn write_npz<W: io::Write + io::Seek>(&self, npz: &mut NpzWriter<W>) -> io::Result<()> {
        match self {
            SparseBase::Coo(m) => m.write_npz(npz),
            SparseBase::Csc(m) => m.write_npz(npz),
            SparseBase::Csr(m) => m.write_npz(npz),
            SparseBase::Dia(m) => m.write_npz(npz),
            SparseBase::Bsr(m) => m.write_npz(npz),
        }
    }
}

impl<T, Data, Indices> CooBase<T, Data, Indices>
where
    T: AutoSerialize,
    Data: Deref<Target=[T]>,
    Indices: AsRef<[u64]>,
{
    /// Write a sparse `coo_matrix` matrix, like `scipy.sparse.save_npz`.
    ///
    /// # Panics
    ///
    /// This method does not currently perform any significant validation of input,
    /// but validation (with panics) may be added later in a future semver major bump.
    pub fn write_npz<W: io::Write + io::Seek>(&self, npz: &mut NpzWriter<W>) -> io::Result<()> {
        let CooBase { data, shape, row, col } = self;
        write_format(npz, "coo")?;
        write_shape(npz, shape)?;
        write_indices(npz, "row", row.as_ref().iter().map(|&x| x as i64))?;
        write_indices(npz, "col", col.as_ref().iter().map(|&x| x as i64))?;
        write_data(npz, &data, &[data.len() as u64])?;
        Ok(())
    }
}

impl<T, Data, Indices, Indptr> CsrBase<T, Data, Indices, Indptr>
where
    T: AutoSerialize,
    Data: Deref<Target=[T]>,
    Indices: AsRef<[u64]>,
    Indptr: AsRef<[usize]>,
{
    /// Write a sparse `csr_matrix` matrix, like `scipy.sparse.save_npz`.
    ///
    /// # Panics
    ///
    /// This method does not currently perform any significant validation of input,
    /// but validation (with panics) may be added later in a future semver major bump.
    pub fn write_npz<W: io::Write + io::Seek>(&self, npz: &mut NpzWriter<W>) -> io::Result<()> {
        let CsrBase { data, shape, indices, indptr } = self;
        write_format(npz, "csr")?;
        write_shape(npz, shape)?;
        write_indices(npz, "indices", indices.as_ref().iter().map(|&x| x as i64))?;
        write_indices(npz, "indptr", indptr.as_ref().iter().map(|&x| x as i64))?;
        write_data(npz, &data, &[data.len() as u64])?;
        Ok(())
    }
}

impl<T, Data, Indices, Indptr> CscBase<T, Data, Indices, Indptr>
where
    T: AutoSerialize,
    Data: Deref<Target=[T]>,
    Indices: AsRef<[u64]>,
    Indptr: AsRef<[usize]>,
{
    /// Write a sparse `csc_matrix` matrix, like `scipy.sparse.save_npz`.
    ///
    /// # Panics
    ///
    /// This method does not currently perform any significant validation of input,
    /// but validation (with panics) may be added later in a future semver major bump.
    pub fn write_npz<W: io::Write + io::Seek>(&self, npz: &mut NpzWriter<W>) -> io::Result<()> {
        let CscBase { data, shape, indices, indptr } = self;
        write_format(npz, "csc")?;
        write_shape(npz, shape)?;
        write_indices(npz, "indices", indices.as_ref().iter().map(|&x| x as i64))?;
        write_indices(npz, "indptr", indptr.as_ref().iter().map(|&x| x as i64))?;
        write_data(npz, &data, &[data.len() as u64])?;
        Ok(())
    }
}

impl<T, Data, Offsets> DiaBase<T, Data, Offsets>
where
    T: AutoSerialize,
    Data: Deref<Target=[T]>,
    Offsets: AsRef<[i64]>,
{
    /// Write a sparse `dia_matrix` matrix, like `scipy.sparse.save_npz`.
    ///
    /// # Panics
    ///
    /// Panics if `data.len()` is not a multiple of `offsets.len()`.
    pub fn write_npz<W: io::Write + io::Seek>(&self, npz: &mut NpzWriter<W>) -> io::Result<()> {
        let DiaBase { data, shape, offsets } = self;
        write_format(npz, "dia")?;
        write_shape(npz, shape)?;
        write_indices(npz, "offsets", offsets.as_ref().iter().copied())?;

        let num_offsets = offsets.as_ref().len();
        assert_eq!(data.len() % num_offsets, 0);
        let length = data.len() / num_offsets;
        write_data(npz, &data, &[length as u64, num_offsets as u64])?;
        Ok(())
    }
}

impl<T, Data, Indices, Indptr> BsrBase<T, Data, Indices, Indptr>
where
    T: AutoSerialize,
    Data: Deref<Target=[T]>,
    Indices: AsRef<[u64]>,
    Indptr: AsRef<[usize]>,
{
    /// Write a sparse `bsr_matrix` matrix, like `scipy.sparse.save_npz`.
    ///
    /// # Panics
    ///
    /// Panics if `data.len()` is not equal to `indices.len() * blocksize[0] * blocksize[1]`.
    pub fn write_npz<W: io::Write + io::Seek>(&self, npz: &mut NpzWriter<W>) -> io::Result<()> {
        let BsrBase { data, shape, indices, indptr, blocksize } = self;
        write_format(npz, "bsr")?;
        write_shape(npz, shape)?;
        write_indices(npz, "indices", indices.as_ref().iter().map(|&x| x as i64))?;
        write_indices(npz, "indptr", indptr.as_ref().iter().map(|&x| x as i64))?;

        assert_eq!(data.len(), indices.as_ref().len() * blocksize[0] * blocksize[1]);
        write_data(npz, &data, &[indices.as_ref().len() as u64, blocksize[0] as u64, blocksize[1] as u64])?;
        Ok(())
    }
}

// -----

fn zip_file_options() -> zip::write::FileOptions {
    Default::default()
}

fn write_format<W: io::Write + io::Seek>(npz: &mut NpzWriter<W>, format: &str) -> io::Result<()> {
    npz.array("format", zip_file_options())?
        .dtype(DType::Plain("|S3".parse().unwrap()))
        .shape(&[])
        .begin_nd()?
        .push(format.as_bytes())
}

fn write_shape<W: io::Write + io::Seek>(npz: &mut NpzWriter<W>, shape: &[u64]) -> io::Result<()> {
    assert_eq!(shape.len(), 2);
    npz.array("shape", zip_file_options())?
        .default_dtype()
        .shape(&[2])
        .begin_nd()?
        .extend(shape.iter().map(|&x| x as i64))
}

// Write signed ints as either i32 or i64 depending on their max value.
fn write_indices<W: io::Write + io::Seek>(npz: &mut NpzWriter<W>, name: &str, data: impl ExactSizeIterator<Item=i64> + Clone) -> io::Result<()> {
    let (min, max) = most_negative_and_positive(data.clone());
    if (i32::MIN as i64) <= min && max <= (i32::MAX as i64) {
        // small indices
        npz.array(name, zip_file_options())?
            .default_dtype()
            .shape(&[data.len() as u64])
            .begin_nd()?
            .extend(data.map(|x| x as i32))
    } else {
        // long indices
        npz.array(name, zip_file_options())?
            .default_dtype()
            .shape(&[data.len() as u64])
            .begin_nd()?
            .extend(data)
    }
}

fn most_negative_and_positive(data: impl ExactSizeIterator<Item=i64>) -> (i64, i64) {
    let mut best_negative = 0;
    let mut best_positive = 0;
    // single pass for better memory characteristics
    for x in data {
        best_negative = best_negative.min(x);
        best_positive = best_positive.max(x);
    }
    (best_negative, best_positive)
}

fn write_data<W: io::Write + io::Seek, T: AutoSerialize>(npz: &mut NpzWriter<W>, data: &[T], shape: &[u64]) -> io::Result<()> {
    npz.array("data", zip_file_options())?
        .default_dtype()
        .shape(shape)
        .begin_nd()?
        .extend(data)
}