Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/aplbrain/npyjs
Read numpy .npy files in JavaScript
https://github.com/aplbrain/npyjs
3d javascript jhuapl nodejs npy npy-files numpy
Last synced: about 20 hours ago
JSON representation
Read numpy .npy files in JavaScript
- Host: GitHub
- URL: https://github.com/aplbrain/npyjs
- Owner: aplbrain
- License: apache-2.0
- Created: 2016-09-16T15:09:20.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2025-01-13T23:01:03.000Z (9 days ago)
- Last Synced: 2025-01-14T17:51:52.469Z (8 days ago)
- Topics: 3d, javascript, jhuapl, nodejs, npy, npy-files, numpy
- Language: JavaScript
- Homepage: https://aplbrain.github.io/npyjs/
- Size: 16.2 MB
- Stars: 80
- Watchers: 9
- Forks: 22
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
npy.js
Read .npy files directly in JS
Read .npy files from [numpy](https://numpy.org/doc/1.18/reference/generated/numpy.save.html) in Node/JS.
## Installation
Include npy.js in your project directly, or:
```shell
yarn add npyjs
# npm i npyjs
```## Import
```javascript
import npyjs from "npyjs";
```## Usage
- Create a new npyjs object:
```javascript
let n = new npyjs();
// Or with options:
let n = new npyjs({ convertFloat16: false }); // Disable float16 to float32 conversion
```- This object can now be used load .npy files. Arrays can be returned via a JavaScript callback, so usage looks like this:
```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.`);
});
```- You can also use this library promise-style using either .then or async await:
```javascript
n.load("test.npy").then((res) => {
// res has { data, shape, dtype } members.
});
``````javascript
const npyArray = await n.load("test.npy");
```## Accessing multidimensional array elements
- You can conveniently access multidimensional array elements using the 'ndarray' library:
```javascript
import ndarray from "ndarray";
const npyArray = ndarray(data, shape);
npyArray.get(10, 15);
```## Supported Data Types
The library supports the following NumPy data types:
- `int8`, `uint8`
- `int16`, `uint16`
- `int32`, `uint32`
- `int64`, `uint64` (as BigInt)
- `float32`
- `float64`
- `float16` (converted to float32 by default)### Float16 Support
By default, float16 arrays are automatically converted to float32 for compatibility, since JavaScript doesn't natively support float16. You can control this behavior with the constructor options:
```javascript
// Default behavior - float16 is converted to float32
const n1 = new npyjs();
// Keep float16 as raw uint16 values without conversion
const n2 = new npyjs({ convertFloat16: false });
```Unless otherwise specified, all code inside of this repository is covered under the license in [LICENSE](LICENSE).
Please report bugs or contribute pull-requests on [GitHub](https://github.com/aplbrain/npyjs).
---