Last active
December 7, 2015 18:52
-
-
Save joferkington/4ce9dc5960bc205eadd0 to your computer and use it in GitHub Desktop.
Writing an arbitrary, pre-existing numpy array to a hetergenous binary format
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
# Our input data... | |
x = np.random.randint(0, 3200, (1000,1000)) | |
# We're replacing something like | |
# struct.pack(">"+"hB"*x.size) | |
# Note that that's a 2-byte signed int followed by 1-byte unsigned | |
# We'll need to create the output 1D array and assign manually: | |
out = np.zeros(x.size // 2, dtype='>i2,>u1') | |
out['f0'], out['f1'] = x.flat[::2], x.flat[1::2] | |
# or more generally: | |
# out[out.dtype.names[0]], out[out.dtype.names[1]] = x[::2], x[1::2] | |
result = out.tostring() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment