Struct npyz::NpyFile[][src]

pub struct NpyFile<R: Read> { /* fields omitted */ }
Expand description

Object for reading an npy file.

This type represents a partially read npy file, where the header has been parsed and we are ready to begin parsing data.

use std::fs::File;
use std::io;

let file = io::BufReader::new(File::open("./test-data/c-order.npy")?);
let npy = npyz::NpyFile::new(file)?;

// Helper methods for inspecting the layout of the data.
assert_eq!(npy.shape(), &[2, 3, 4]);
assert_eq!(npy.strides(), &[12, 4, 1]);
assert_eq!(npy.order(), npyz::Order::C);

// Get the data!
let data: Vec<i64> = npy.into_vec()?;
assert_eq!(data.len(), 24);

Working with large files

For large files, it may be undesirable to read all of the data into a Vec. The NpyFile::data method allows you to iterate over the data instead.

let npy = npyz::NpyFile::new(file)?;

let mut sum = 0;
for x in npy.data::<i64>()? {
    sum += x?;  // items are Result
}
println!("{}", sum);

Migrating from NpyData

At construction, since &[u8] impls Read, you can still use them as input.

was:
    npyz::NpyData::<i64>::from_bytes(&bytes).to_vec()
now:
    npyz::NpyFile::new(&bytes[..]).into_vec::<i64>()

If you were using the iterator API of NpyData, this is now on NpyReader, which requires us to call NpyFile::data:

was:
    let iter = npyz::NpyData::<i64>::new(&bytes)?;
now:
    let iter = npyz::NpyFile::new(&bytes[..]).data::<i64>.map_err(invalid_data)?;

where the following function has been used to paper over the fact that NpyFile::data has a different Error type:

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

NpyData::is_empty is gone due to possible ambiguity between NpyReader::len and NpyReader::total_len. Use the one that is appropriate for what you are doing.

If you were using NpyData::get… well, honestly, you should first consider whether you could just iterate over the reader instead. But if you were using NpyData::get because you genuinely need random access, then there is NpyReader::read_at.

was:
    // note:  0 <= i < arr.len()
    arr.get(i)
now:
    // note:  0 <= i < reader.total_len()
    reader.read_at(i)?

Implementations

Read the header of an npy file and construct an NpyReader for reading the data.

Get the dtype as written in the file.

Get the shape as written in the file.

Get strides for each of the dimensions.

This is the amount by which the item index changes as you move along each dimension. It is a function of both Self::order and Self::shape, provided for your convenience.

Get whether the data is in C order or fortran order.

Get the total number of elements in the file. (This is the product of Self::shape)

Read all elements into a flat Vec, in the order they are stored as.

This is a convenience wrapper around Self::data and Iterator::collect.

Produce an NpyReader to begin reading elements, if T can be deserialized from the file’s dtype.

The returned type implements Iterator<Item=io::Result<T>>, and provides additional methods for random access when R: Seek. See NpyReader for more details.

Produce an NpyReader to begin reading elements, if T can be deserialized from the file’s dtype.

This fallible form of the function returns self on error, so that you can try again with a different T.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.