npy
Reading and writing NumPy .npy and .npz files.
import npy
.npy files are the standard way NumPy arrays are stored in Python, and .npz keeps several named arrays in one zip archive. With this package you can move data and model weights between Python and Salam.
Types
NpyArray
struct NpyArray| Name | Type | Description |
|---|---|---|
shape | Vector | The size of each dimension, such as [2, 3]. |
dtype | int | The data type in the file; one of the NPY_ constants. |
data | Vector | The elements in row order. |
import npy
import matrix
func main:
mut m := matrix.Matrix { rows = 2, cols = 3 }
repeat 6 in i:
m.a.push(i as f64)
end
mut a := npy.FromMatrix(m)
println a.shape, a.ndim(), a.count(), a.dtype == npy.NPY_F64
bytes := npy.WriteNpy(a)
println bytes.len > 64
mut good := false
mut back := npy.ReadNpy(bytes.data, bytes.len, good)
println good, back.shape, back.data
m2 := npy.ToMatrix(back, good)
println good, m2.at(1, 2)
println npy.WriteNpyFile(a, "a.npy")
mut from_file := npy.ReadNpyFile("a.npy", good)
println good, from_file.count()
mut keys := Vector {} as Vector<str>
keys.push("x")
keys.push("y")
mut arrays := Vector {} as Vector<npy.NpyArray>
arrays.push(a)
arrays.push(back)
println npy.NpzWrite("ab.npz", keys, arrays)
println npy.NpzList("ab.npz", good), good
mut y := npy.NpzRead("ab.npz", "y", good)
println good, y.shape
println npy.NPY_F32, npy.NPY_I8, npy.NPY_I16, npy.NPY_I32, npy.NPY_I64, npy.NPY_U8, npy.NPY_U16, npy.NPY_U32, npy.NPY_BOOL
y.free()
from_file.free()
back.free()
a.free()
endndim
func ndim(): intThe example for this item is shown with the ones above: npy-Npy.salam
count
func count(): intThe example for this item is shown with the ones above: npy-Npy.salam
free
func free()The example for this item is shown with the ones above: npy-Npy.salam
Constants and variables
Data types NPY_…
| Name | Value |
|---|---|
NPY_F64 | 0 |
NPY_F32 | 1 |
NPY_I8 | 2 |
NPY_I16 | 3 |
NPY_I32 | 4 |
NPY_I64 | 5 |
NPY_U8 | 6 |
NPY_U16 | 7 |
NPY_U32 | 8 |
NPY_BOOL | 9 |
The example for this item is shown with the ones above: npy-Npy.salam
Functions
ReadNpy
func ReadNpy(p: void*, n: int, ok &: bool): NpyArray.npy file from memory.The example for this item is shown with the ones above: npy-Npy.salam
ReadNpyFile
func ReadNpyFile(path: str, ok &: bool): NpyArray.npy file.The example for this item is shown with the ones above: npy-Npy.salam
WriteNpy
func WriteNpy(a &: NpyArray): compress.ByteBuf.npy bytes.The example for this item is shown with the ones above: npy-Npy.salam
WriteNpyFile
func WriteNpyFile(a &: NpyArray, path: str): bool.npy file.The example for this item is shown with the ones above: npy-Npy.salam
ToMatrix
func ToMatrix(a &: NpyArray, ok &: bool): matrix.Matrixok becomes false.The example for this item is shown with the ones above: npy-Npy.salam
FromMatrix
func FromMatrix(m &: matrix.Matrix): NpyArrayThe example for this item is shown with the ones above: npy-Npy.salam
NpzList
func NpzList(path: str, ok &: bool): Vector<str>.npz file.The example for this item is shown with the ones above: npy-Npy.salam
NpzRead
func NpzRead(path: str, key: str, ok &: bool): NpyArray.npz.The example for this item is shown with the ones above: npy-Npy.salam
NpzWrite
func NpzWrite(path: str, keys &: Vector<str>, arrays &: Vector<NpyArray>): bool.npz file.The example for this item is shown with the ones above: npy-Npy.salam