Skip to main content

Module io

Module io 

Source
Expand description

I/O for arrays: .npy, .npz and CSV files, like np.save, np.load, np.savez, np.savetxt and np.loadtxt.

Every function returns crate::Result: file problems are crate::Error::Io, a file whose dtype differs from the requested element type is crate::Error::DType, and malformed content is crate::Error::Format or crate::Error::Parse.

use rustnum as np;
let a = np::array![[1.5, 2.5], [3.5, 4.5]];
let bytes = np::io::to_npy_bytes(&a);
let b: np::Array<f64> = np::io::from_npy_bytes(&bytes).unwrap();
assert_eq!(a, b);

§Architecture

  • npy (private): .npy format v1.0/v2.0 on (Vec<T>, shape).
  • csv (private): CSV parse and format on (Vec<T>, shape).
  • api: the public functions, which wrap those kernels and return Array.
  • npz (private module): ZIP/DEFLATE layer behind savez, savez_compressed, load_npz and NpzFile, which are re-exported here.

Structs§

NpzFile
An opened .npz file whose arrays are decompressed only when requested, like np.load(path) on a .npz (NpzFile in NumPy).

Functions§

from_npy_bytes
Deserializes an array from .npy bytes in memory (see load_npy for the rules).
load_csv
Loads delimited text as a 1-d or 2-d array, like np.loadtxt(path, delimiter=..., skiprows=...).
load_npy
Loads an array from a .npy file, like np.load(path).
load_npz
Loads every array of an .npz file, like dict(np.load(path)).
save_csv
Saves a 1-d or 2-d array as delimited text, like np.savetxt(path, a, delimiter=...).
save_npy
Saves an array to a .npy file, like np.save(path, a).
savez
Saves several named arrays into one uncompressed .npz file, like np.savez(path, **arrays).
savez_compressed
Saves several named arrays into one DEFLATE-compressed .npz file, like np.savez_compressed(path, **arrays).
to_npy_bytes
Serializes an array to .npy bytes in memory, like np.save into a buffer.