This package provides a simple interface for loading binary Numpy array data (.npy) from URL and reading them in an elegant fashion. The Numpy data can be single-typed or mixed-typed (aka. structured).
Numpy arrays data (.npy) is usually generated by the Python scientific computing package Numpy.
https://gywn.github.io/NumpyArray.js/
Load Numpy arrays data from url
and return a Promise object. Once loaded, the array is given as the first argument for the fulfillment handler.
An optional header
can be given to describe the structure of the Numpy arrays data. Otherwise the package will try to use the header object extracted from the binary data.
getFromURL
only supports GET
method.
const promise = NumpyArray.getFromURL('npy/World Population Density.npy');
promise.then(na => window['na'] = na);
Construct a NumpyArray object directly from an ArrayBuffer object.
const r = new XMLHttpRequest();
r.open('GET', 'npy/World Population Density.npy');
r.responseType = 'arraybuffer';
r.onload = () => window['na'] = new NumpyArray(r.response);
r.send();
Return an array of the fields' name. If the array is single-typed, the fieldNames is always ["value"]
, unless overriden by the header
argument.
na.fieldNames; // ["Country", "Population", "Density"]
Return the number of records. If the number of dimensions of the data is higher than 1, it will be flatten according to header.fortran_order
.
na.size; // 197
Extract columns or rows from the data. The returned value is also a NumpyArray object (which is a view on the buffer).
// Extract columns
na.get('Country').fieldNames // ["Country"]
na.get(['Density', 'Country']).size // 197
// Extract rows
na.get(99) // ["Liechtenstein", 35236, 219.430419921875]
na.get([195, 196]) // [["Zambia", 13881336, 18.444271087646484],
// ["Zimbabwe", 12084304, 30.93951988220215]]
Return a copy the data as Javascript Array object.
na.toArray() // [["Afghanistan", 29835392, 46.0780029296875], ...]
na.get('Country').toArray() // ["Afghanistan", "Albania", ...]
Return the raw bytes of the header from the data. Could be useful if the package failed to extract the header object automatically and thus a header
object must be given explicitly.
na.numpyHeader // "{'descr': [('Country', '|S22'),
// ('Population', '<i4'),
// ('Density', '<f4')],
// 'fortran_order': False,
// 'shape': (197,),
// }"