UNPKG

numpy-parser

Version:
2 lines 3.72 kB
"use strict";Object.defineProperty(exports,"__esModule",{value:!0}),exports.fromArrayBuffer=fromArrayBuffer;function _classCallCheck(a,b){if(!(a instanceof b))throw new TypeError("Cannot call a class as a function")}function _defineProperties(a,b){for(var c,d=0;d<b.length;d++)c=b[d],c.enumerable=c.enumerable||!1,c.configurable=!0,"value"in c&&(c.writable=!0),Object.defineProperty(a,c.key,c)}function _createClass(a,b,c){return b&&_defineProperties(a.prototype,b),c&&_defineProperties(a,c),a}var DataViewReader=function(){function a(b){_classCallCheck(this,a),b instanceof DataView?this.dataView=b:b instanceof ArrayBuffer&&(this.dataView=new DataView(b)),this.offset=0}return _createClass(a,[{key:"readBytes",value:function c(a){var b=new DataView(this.dataView.buffer,this.offset,a);return this.offset+=a,b}},{key:"readAndASCIIDecodeBytes",value:function c(a){var b=new Uint8Array(this.dataView.buffer,this.offset,a);return this.offset+=a,this._decodeASCIIByteArray(b)}},{key:"readUint8",value:function c(){var a=!!(0<arguments.length&&void 0!==arguments[0])&&arguments[0],b=this.dataView.getUint8(this.offset,a);return this.offset+=Uint8Array.BYTES_PER_ELEMENT,b}},{key:"readUint16",value:function c(){var a=!!(0<arguments.length&&void 0!==arguments[0])&&arguments[0],b=this.dataView.getUint16(this.offset,a);return this.offset+=Uint16Array.BYTES_PER_ELEMENT,b}},{key:"readUint32",value:function c(){var a=!!(0<arguments.length&&void 0!==arguments[0])&&arguments[0],b=this.dataView.getUint32(this.offset,a);return this.offset+=Uint32Array.BYTES_PER_ELEMENT,b}},{key:"_decodeASCIIByteArray",value:function k(a){var b=String.fromCharCode,c=[],d=!0,e=!1,f=void 0;try{for(var g,h=a[Symbol.iterator]();!(d=(g=h.next()).done);d=!0){var i=g.value,j=b(i);c.push(j)}}catch(a){e=!0,f=a}finally{try{d||null==h.return||h.return()}finally{if(e)throw f}}return c.join("")}}]),a}();function fromArrayBuffer(a){if(!a instanceof ArrayBuffer)throw new Error("Argument must be an ArrayBuffer.");var b=new DataViewReader(a),c=b.readUint8(),d=b.readAndASCIIDecodeBytes(5);if(147!=c||"NUMPY"!=d)throw new Error("unknown file type: \"".concat(c).concat(d,"\""));var e,f=b.readUint8(),g=b.readUint8();e=1>=f?b.readUint16(!0):b.readUint32(!0);var h=10+e;0!=h%16&&console.warn("NPY file header is incorrectly padded. (".concat(h," is not evenly divisible by 16.)"));var i=b.readAndASCIIDecodeBytes(e),j=parseHeaderStr(i);if(j.fortran_order)throw new Error("NPY file is written in Fortran byte order, support for this byte order is not yet implemented.");var k=typedArrayConstructorForDescription(j.descr),l=new k(a,b.offset);return{data:l,shape:j.shape}}function parseHeaderStr(a){var b=a.toLowerCase().replace("(","[").replace("),","]").replace("[,","[1,]").replace(",]",",1]").replace(/'/g,"\"");return JSON.parse(b)}function typedArrayConstructorForDescription(a){switch(a){case"|u1":return Uint8Array;case"<u2":return Uint16Array;case"<u4":return Uint32Array;case"<u8":throw new Error("Because JavaScript doesn't currently include standard support for 64-bit unsigned integer values, support for this dtype is not yet implemented.");case"|i1":return Int8Array;case"<i2":return Int16Array;case"<i4":return Int32Array;case"<i8":throw new Error("Because JavaScript doesn't currently include standard support for 64-bit integer values, support for this dtype is not yet implemented.");case"<f2":throw new Error("Because JavaScript doesn't currently include standard support for 16-bit floating point values, support for this dtype is not yet implemented.");case"<f4":return Float32Array;case"<f8":return Float64Array;default:throw new Error("Unknown or not yet implemented numpy dtype description: "+dtype);}} //# sourceMappingURL=main.js.map