Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds format functionality #18

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ module.exports = {
"rules": {
"indent": [
"error",
4
2
],
"linebreak-style": [
"error",
Expand Down
28 changes: 28 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,31 @@ of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.


END OF TERMS AND CONDITIONS

APPENDIX: How to apply the Apache License to your work.

To apply the Apache License to your work, attach the following
boilerplate notice, with the fields enclosed by brackets "[]"
replaced with your own identifying information. (Don't include
the brackets!) The text should be enclosed in the appropriate
comment syntax for the file format. We also recommend that a
file or class name and description of purpose be included on the
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright [yyyy] [name of copyright owner]

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
81 changes: 46 additions & 35 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,54 +1,65 @@
<h1 align=center>npy.js</h1>
<h6 align=center>Read .npy files directly in JS</h6>

<p align=center>
<img src="https://img.shields.io/npm/v/npyjs.svg?style=for-the-badge" />
<img src="https://img.shields.io/github/issues/aplbrain/npyjs.svg?style=for-the-badge" />
<img src="https://img.shields.io/github/license/aplbrain/npyjs.svg?style=for-the-badge" />
<img alt="GitHub Workflow Status" src="https://img.shields.io/github/workflow/status/aplbrain/npyjs/Node.js CI?label=Tests&style=for-the-badge">
</p>

## Installation

Include npy.js in your project directly, or:

```shell
yarn add npyjs
# npm i npyjs
```

## Usage
Import as a module:

```js
import npyjs from 'npyjs'
```

- Create a new npyjs object.
Or in a script tag:

```javascript
let n = new npyjs();
```html
<script type='module'>
import npyjs from './npyjs.js'
window.npyjs = npyjs
</script>
```

- This object can now be used load .npy files. Arrays are returned via a JavaScript callback, so usage looks like this:
## Parse

```javascript
n.load("my-array.npy", (array, shape) => {
// `array` is a one-dimensional array of the raw data
// `shape` is a one-dimensional array that holds a numpy-style shape.
console.log(
`You loaded an array with ${array.length} elements and ${shape.length} dimensions.`
);
});
**npyjs.format** takes a npy file and returns an object with the following properties:
- `data`: a typed array of data
- `shape`: an array with the shape of the data
- `dtype`: a string with type of data

You can load a file with fetch:

```js
const {data, shape, dtype} = npyjs.parse(await(await fetch('ints.npy')).arrayBuffer())
```

You can also use this library promise-style:
Or from disk:

```javascript
n.load("test.npy").then((res) => {
// res has { data, shape, dtype } members.
});
```js
import fs from 'fs'

fs.readFile('ints.npy', (err, res) => {
const ints = npyjs.parse(res)
console.log(ints)
// {
// data: Int8Array(10) [
// 0, 1, 2, 3, 4,
// 5, 6, 7, 8, 9
// ],
// shape: [ 5, 2 ]
// dtype: 'int8',
// }
})
```

Unless otherwise specified, all code inside of this repository is covered under the license in [LICENSE](LICENSE).
## Format

**npyjs.format** takes a [typed array](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Typed_arrays) of data and an array with the dimensions of the data. It returns a [npy file](https://numpy.org/devdocs/reference/generated/numpy.lib.format.html).

Please report bugs or contribute pull-requests on [GitHub](https://github.com/aplbrain/npyjs).
```js
import fs from 'fs'

---
const typedArray = new Int8Array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
const out = npyjs.format(typedArray, [5, 2])

<p align="center"><small>Made with ♥ at <a href="http://www.jhuapl.edu/"><img alt="JHU APL" align="center" src="./docs/apl-logo.png" height="23px"></a></small></p>
fs.writeFileSync('ints.npy', out)
```
213 changes: 95 additions & 118 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,129 +1,106 @@
const fetch = this.fetch ? this.fetch : require("node-fetch");
const dtypes = {
'<u1': {
name: 'uint8',
size: 8,
arrayConstructor: Uint8Array,
},
'|u1': {
name: 'uint8',
size: 8,
arrayConstructor: Uint8Array,
},
'<u2': {
name: 'uint16',
size: 16,
arrayConstructor: Uint16Array,
},
'|i1': {
name: 'int8',
size: 8,
arrayConstructor: Int8Array,
},
'<i2': {
name: 'int16',
size: 16,
arrayConstructor: Int16Array,
},
'<u4': {
name: 'uint32',
size: 32,
arrayConstructor: Int32Array,
},
'<i4': {
name: 'int32',
size: 32,
arrayConstructor: Int32Array,
},
'<u8': {
name: 'uint64',
size: 64,
arrayConstructor: BigUint64Array,
},
'<i8': {
name: 'int64',
size: 64,
arrayConstructor: BigInt64Array,
},
'<f4': {
name: 'float32',
size: 32,
arrayConstructor: Float32Array
},
'<f8': {
name: 'float64',
size: 64,
arrayConstructor: Float64Array
},
};

class npyjs {
function parse(buffer){
const buf = new Uint8Array(buffer);
if (buf[6] != 1) throw 'Only npy version 1 is supported';

constructor(opts) {
if (opts) {
console.error([
"No arguments accepted to npyjs constructor.",
"For usage, go to https://github.com/jhuapl-boss/npyjs."
].join(" "));
}
const headerLength = buf[8] + buf[9]*256;
const offsetBytes = 10 + headerLength;

this.dtypes = {
"<u1": {
name: "uint8",
size: 8,
arrayConstructor: Uint8Array,
},
"|u1": {
name: "uint8",
size: 8,
arrayConstructor: Uint8Array,
},
"<u2": {
name: "uint16",
size: 16,
arrayConstructor: Uint16Array,
},
"|i1": {
name: "int8",
size: 8,
arrayConstructor: Int8Array,
},
"<i2": {
name: "int16",
size: 16,
arrayConstructor: Int16Array,
},
"<u4": {
name: "uint32",
size: 32,
arrayConstructor: Int32Array,
},
"<i4": {
name: "int32",
size: 32,
arrayConstructor: Int32Array,
},
"<u8": {
name: "uint64",
size: 64,
arrayConstructor: BigUint64Array,
},
"<i8": {
name: "int64",
size: 64,
arrayConstructor: BigInt64Array,
},
"<f4": {
name: "float32",
size: 32,
arrayConstructor: Float32Array
},
"<f8": {
name: "float64",
size: 64,
arrayConstructor: Float64Array
},
};
}
const header = JSON.parse(
new TextDecoder('utf-8')
.decode(buf.slice(10, 10 + headerLength))
.replace(/'/g, '"')
.replace('False', 'false')
.replace('(', '[')
.replace(/,*\),*/g, ']')
);

parse(arrayBufferContents) {
if (header.fortan_order) throw 'Fortran-contiguous array data are not supported';
const dtype = dtypes[header.descr];

// const version = arrayBufferContents.slice(6, 8); // Uint8-encoded
const headerLength = new DataView(arrayBufferContents.slice(8, 10)).getUint8(0);
const offsetBytes = 10 + headerLength;

let hcontents = new TextDecoder("utf-8").decode(
new Uint8Array(arrayBufferContents.slice(10, 10 + headerLength))
);
var header = JSON.parse(
hcontents
.replace(/'/g, '"')
.replace("False", "false")
.replace("(", "[")
.replace(/,*\),*/g, "]")
);
var shape = header.shape;
return {
data: new dtype['arrayConstructor'](buf.slice(offsetBytes).buffer),
shape: header.shape,
dtype: dtype.name,
};
}

let dtype = this.dtypes[header.descr];
function format(typedArray, shape){
let dtype = null;
for (let d in dtypes){
if (dtypes[d].arrayConstructor == typedArray.constructor) dtype = d;
}
if (dtype === null) throw 'Invalid typedArray';

let nums = new dtype["arrayConstructor"](
arrayBufferContents,
offsetBytes
);
const header = `{'descr': '${dtype}', 'fortran_order': False, 'shape': (${shape.join(',')},), }\n`;
const spacepad = Array.from({length: 64 - (8 + header.length) % 64}, d => '\x20').join('');

return {
dtype: dtype.name,
data: nums,
shape
};
}
const hl = (header + spacepad).length;

async load(filename, callback) {
/*
Loads an array from a stream of bytes.
*/
let self = this;
return fetch(filename).then(fh => {
if (fh.ok) {
return fh.blob().then(i => {
var content = i;
var reader = new FileReader();
reader.addEventListener("loadend", function () {
var text = reader.result;
var res = self.parse(text);
if (callback) {
return callback(res);
}
return res;
});
reader.readAsArrayBuffer(content);
}).catch(err => console.error(err));
}
}).catch(err => console.error(err));
}
return Buffer.concat([
Buffer.from('\x93NUMPY\x01\x00', 'latin1'),
// convert to little-endian
Buffer.from(new Uint8Array([hl % 256, hl/256 | 0])),
Buffer.from(header + spacepad, 'latin1'),
Buffer.from(typedArray.buffer)
]);
}

module.exports = npyjs;
export default {parse, format};
6 changes: 2 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "npyjs",
"version": "0.2.0",
"version": "0.3.0",
"description": "Parse npy files in JS",
"main": "index.js",
"repository": "https://github.com/jhuapl-boss/npyjs.git",
Expand All @@ -12,7 +12,5 @@
"scripts": {
"test": "mocha"
},
"dependencies": {
"node-fetch": "^2.6.0"
}
"type": "module"
}
Binary file modified test/data/10-float32.npy
Binary file not shown.
Binary file modified test/data/10-float64.npy
Binary file not shown.
Binary file modified test/data/10-int16.npy
Binary file not shown.
Binary file modified test/data/10-int64.npy
Binary file not shown.
Binary file modified test/data/10-int8.npy
Binary file not shown.
Binary file modified test/data/100x100x100-float32.npy
Binary file not shown.
Binary file modified test/data/100x100x100-float64.npy
Binary file not shown.
Binary file modified test/data/100x100x100-int16.npy
Binary file not shown.
Binary file modified test/data/100x100x100-int64.npy
Binary file not shown.
Binary file modified test/data/100x100x100-int8.npy
Binary file not shown.
Binary file modified test/data/4x4x4x4x4-float32.npy
Binary file not shown.
Binary file modified test/data/4x4x4x4x4-float64.npy
Binary file not shown.
Binary file modified test/data/4x4x4x4x4-int16.npy
Binary file not shown.
Binary file modified test/data/4x4x4x4x4-int64.npy
Binary file not shown.
Binary file modified test/data/4x4x4x4x4-int8.npy
Binary file not shown.
Binary file modified test/data/65x65-float32.npy
Binary file not shown.
Binary file modified test/data/65x65-float64.npy
Binary file not shown.
Binary file modified test/data/65x65-int16.npy
Binary file not shown.
Binary file modified test/data/65x65-int64.npy
Binary file not shown.
Binary file modified test/data/65x65-int8.npy
Binary file not shown.
Binary file added test/data/out.npy
Binary file not shown.
Loading