-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from jturner314/names-in-npz
Match NumPy regarding .npy for names in .npz files
- Loading branch information
Showing
3 changed files
with
79 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
//! .npz examples. | ||
use ndarray::{array, Array2}; | ||
use ndarray_npy::{NpzReader, NpzWriter}; | ||
use std::{error::Error, io::Cursor}; | ||
|
||
#[test] | ||
fn round_trip_npz() -> Result<(), Box<dyn Error>> { | ||
let mut buf = Vec::<u8>::new(); | ||
|
||
let arr1 = array![[1i32, 3, 0], [4, 7, -1]]; | ||
let arr2 = array![[9i32, 6], [-5, 2], [3, -1]]; | ||
|
||
{ | ||
let mut writer = NpzWriter::new(Cursor::new(&mut buf)); | ||
writer.add_array("arr1", &arr1)?; | ||
writer.add_array("arr2", &arr2)?; | ||
writer.finish()?; | ||
} | ||
|
||
{ | ||
let mut reader = NpzReader::new(Cursor::new(&buf))?; | ||
assert!(!reader.is_empty()); | ||
assert_eq!(reader.len(), 2); | ||
assert_eq!( | ||
reader.names()?, | ||
vec!["arr1".to_string(), "arr2".to_string()], | ||
); | ||
{ | ||
let by_name: Array2<i32> = reader.by_name("arr1")?; | ||
assert_eq!(by_name, arr1); | ||
} | ||
{ | ||
let by_name: Array2<i32> = reader.by_name("arr1.npy")?; | ||
assert_eq!(by_name, arr1); | ||
} | ||
{ | ||
let by_name: Array2<i32> = reader.by_name("arr2")?; | ||
assert_eq!(by_name, arr2); | ||
} | ||
{ | ||
let by_name: Array2<i32> = reader.by_name("arr2.npy")?; | ||
assert_eq!(by_name, arr2); | ||
} | ||
{ | ||
let res: Result<Array2<i32>, _> = reader.by_name("arr1.npy.npy"); | ||
assert!(res.is_err()); | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |