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

Feature: Save() function for TypedArrays and regular arrays #15

Open
TheNewSound opened this issue Jan 26, 2021 · 2 comments
Open

Feature: Save() function for TypedArrays and regular arrays #15

TheNewSound opened this issue Jan 26, 2021 · 2 comments

Comments

@TheNewSound
Copy link

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.

@j6k4m8
Copy link
Member

j6k4m8 commented Jan 26, 2021

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

@TheNewSound
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants