Module NpySource
Read numpy .npy files.
A portable OCaml library for parsing the numpy binary format. Data is returned as flat Bigarray.Array1 values in C layout; use shape to interpret the dimensions.
Supported dtypes
|i1— signed 8-bit integer|u1— unsigned 8-bit integer<f4— 32-bit float (little-endian)<f8— 64-bit float (little-endian)
Example
let data = In_channel.with_open_bin "embeddings.npy" In_channel.input_all in
match Npy.of_string data with
| Error msg -> failwith msg
| Ok t ->
let shape = Npy.shape t in
Printf.printf "shape: %s\n"
(String.concat "x" (Array.to_list (Array.map string_of_int shape)));
match Npy.data_float32 t with
| Some ba -> Printf.printf "first value: %f\n" (Bigarray.Array1.get ba 0)
| None -> Printf.printf "not a float32 array\n"Types
Element types supported by this library.
A parsed .npy file.
Parsing
Parse a .npy file from its complete contents as a string. Supports format versions 1.0 and 2.0. Returns Error msg if the magic bytes are wrong, the header is malformed, or the dtype is unsupported.
Metadata
The shape of the array. For example, a 10x8 matrix returns [|10; 8|]. A scalar returns [||].
Whether the data is stored in Fortran (column-major) order. Most numpy files use C order (false).
Data access
Each accessor returns Some bigarray if the dtype matches, or None otherwise. The returned Bigarray.Array1 is a flat (1-dimensional) view of the data in row-major order; use shape to interpret the dimensions.
For multi-dimensional indexing, compute the flat index as: row * cols + col (for 2D) or i * (d1 * d2) + j * d2 + k (for 3D).
val data_int8 :
Npy.t ->
(int, Stdlib.Bigarray.int8_signed_elt, Stdlib.Bigarray.c_layout)
Stdlib.Bigarray.Array1.t
optionAccess data as signed 8-bit integers (numpy dtype |i1).
val data_uint8 :
Npy.t ->
(int, Stdlib.Bigarray.int8_unsigned_elt, Stdlib.Bigarray.c_layout)
Stdlib.Bigarray.Array1.t
optionAccess data as unsigned 8-bit integers (numpy dtype |u1).
val data_float32 :
Npy.t ->
(float, Stdlib.Bigarray.float32_elt, Stdlib.Bigarray.c_layout)
Stdlib.Bigarray.Array1.t
optionAccess data as 32-bit floats (numpy dtype <f4, little-endian).
val data_float64 :
Npy.t ->
(float, Stdlib.Bigarray.float64_elt, Stdlib.Bigarray.c_layout)
Stdlib.Bigarray.Array1.t
optionAccess data as 64-bit floats (numpy dtype <f8, little-endian).