// Client-side parser for .npy files // See the specification: http://docs.scipy.org/doc/numpy-dev/neps/npy-format.html const NumpyLoader = (function NumpyLoader() { function asciiDecode(buf) { return String.fromCharCode.apply(null, new Uint8Array(buf)); } function readUint16LE(buffer) { var view = new DataView(buffer); var val = view.getUint8(0); val |= view.getUint8(1) << 8; return val; } function fromArrayBuffer(buf) { // Check the magic number var magic = asciiDecode(buf.slice(0,6)); if (magic !== "\x93NUMPY") { throw new Error("Bad magic number"); } var version = new Uint8Array(buf.slice(6,8)), headerLength = readUint16LE(buf.slice(8,10)), headerStr = asciiDecode(buf.slice(10, 10+headerLength)); const offsetBytes = 10 + headerLength; //rest = buf.slice(10+headerLength); XXX -- This makes a copy!!! https://www.khronos.org/registry/typedarray/specs/latest/#5 // Hacky conversion of dict literal string to JS Object const info = JSON.parse(headerStr.toLowerCase() .replace('(','[') .replace('),',']') .replace(/'/g, '"') .replace(',]', ']')); // Intepret the bytes according to the specified dtype var data; if (info.descr === "|u1") { data = new Uint8Array(buf, offsetBytes); } else if (info.descr === "|i1") { data = new Int8Array(buf, offsetBytes); } else if (info.descr === "