/js-numpy-parser

A JS parser for (binary) `.npy` files.

Primary LanguageJavaScriptApache License 2.0Apache-2.0

numpy-parser

A JS parser for (binary) .npy files, supporting all TypedArray subclasses:

  • float32 & float64
  • int8, int16, int32
  • uint8, uint16, uint32

Install via npm

npm install --save numpy-parser

Example usage

import { fromArrayBuffer } from "numpy-parser";

const url = 
const response = await fetch(url);
const arrayBuffer = await response.arrayBuffer();
const { data, shape } = fromArrayBuffer(arrayBuffer);

For ergonomic usage I recommend wrapping this in, e.g. this ndarray implementation:

import ndarray from "ndarray";
const array = ndarray(data, shape);

Acknowledgements

This implementation was inspired by this gist by nvictus.

Future Work

.npy files can encode floats as 16 bit long, too. While JS runtimes may not support this as a native data type, we could still consider supporting it and parsing into 32 bit floats to at least get the bandwidth savings in transit.