Standard library · Encoding and data

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
One multi dimensional NumPy array. The data is always held in memory as 64 bit floats.
NameTypeDescription
shapeVectorThe size of each dimension, such as [2, 3].
dtypeintThe data type in the file; one of the NPY_ constants.
dataVectorThe elements in row order.
npy-Npy.salam
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()
end
Output[2, 3] 2 6 true true true [2, 3] [0, 1, 2, 3, 4, 5] true 5 true true 6 true ["x", "y"] true true [2, 3] 1 2 3 4 5 6 7 8 9

ndim

func ndim(): int
The number of dimensions.

The example for this item is shown with the ones above: npy-Npy.salam

count

func count(): int
The total number of elements.

The example for this item is shown with the ones above: npy-Npy.salam

free

func free()
Frees the memory of the array.

The example for this item is shown with the ones above: npy-Npy.salam

Constants and variables

Data types NPY_…

The type of the elements in the file: 8 and 4 byte floats, 1 to 8 byte signed and unsigned integers, and boolean.
NameValue
NPY_F640
NPY_F321
NPY_I82
NPY_I163
NPY_I324
NPY_I645
NPY_U86
NPY_U167
NPY_U328
NPY_BOOL9

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
Reads the bytes of a .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
Reads a .npy file.

The example for this item is shown with the ones above: npy-Npy.salam

WriteNpy

func WriteNpy(a &: NpyArray): compress.ByteBuf
Turns the array into .npy bytes.

The example for this item is shown with the ones above: npy-Npy.salam

WriteNpyFile

func WriteNpyFile(a &: NpyArray, path: str): bool
Writes the array to a .npy file.

The example for this item is shown with the ones above: npy-Npy.salam

ToMatrix

func ToMatrix(a &: NpyArray, ok &: bool): matrix.Matrix
Turns a two dimensional array into a matrix; for any other number of dimensions ok becomes false.

The example for this item is shown with the ones above: npy-Npy.salam

FromMatrix

func FromMatrix(m &: matrix.Matrix): NpyArray
Turns a matrix into a two dimensional array of 64 bit floats.

The example for this item is shown with the ones above: npy-Npy.salam

NpzList

func NpzList(path: str, ok &: bool): Vector<str>
The names of the arrays in a .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
Reads the array with the given name out of a .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
Writes several arrays with their names into one .npz file.

The example for this item is shown with the ones above: npy-Npy.salam