npyjs icon indicating copy to clipboard operation
npyjs copied to clipboard

Feature: Save() function for TypedArrays and regular arrays

Open TheNewSound opened this issue 4 years ago • 2 comments

Title says it all.

Would it be possible to include a save() function for TypedArrays and regular arrays? For both NodeJS filesystem as in-browser download? This way I can export my javascript arrays to python/numpy.

TheNewSound avatar Jan 26 '21 21:01 TheNewSound

Any interest in submitting a PR? Would love to have this functionality!

j6k4m8 avatar Jan 26 '21 23:01 j6k4m8

Since I am on a tight schedule, I currently solved this problem with the following (not using .npy file format):

Export in javascript:

const array = new Float32Array([1,2,3,4]);
const buffer = Buffer.from(array.buffer);
fs.writeFileSync("output/matrix.bin", buffer);

Import in python:

import math
import numpy as np
filename = '../node/output/matrix.bin'
with open(filename, 'rb') as f:
    simmatrix_flat = np.fromfile(f, dtype=np.float32)
print(f'Read file "{filename}" containing a matrix of size: {simmatrix_flat.size}')
#reshape matrix
simmatrix = np.reshape(simmatrix_flat, [math.isqrt(simmatrix_flat.size), math.isqrt(simmatrix_flat.size)])

I might implement this feature in the future, however, don't count on it.

TheNewSound avatar Jan 26 '21 23:01 TheNewSound