Skip to content

Instantly share code, notes, and snippets.

@alexland
Last active November 28, 2023 07:12
Show Gist options
  • Save alexland/ce02d6ae5c8b63413843 to your computer and use it in GitHub Desktop.
Save alexland/ce02d6ae5c8b63413843 to your computer and use it in GitHub Desktop.
serialize, persist, retrieve, and de-serialize a NumPy array as a binary string (any dimension, any dtype); exemplary use case: a web app calculates some result--eg, from a Machine Learning algorithm, using NumPy and the result is a NumPy array; it is efficient to just return that result to rather than persist the array then retrieve it via query
import time
import numpy as NP
from redis import StrictRedis as redis
# a 2D array to serialize
A = 10 * NP.random.randn(10000).reshape(1000, 10)
# flatten the 2D NumPy array and save it as a binary string
array_dtype = str(A.dtype)
l, w = A.shape
A = A.ravel().tostring()
# create a key as a UNIX timestamp w/ array shape appended to end of key delimited by '|'
db = redis(db=0)
key = '{0}|{1}#{2}#{3}'.format(int(time.time()), array_dtype, l, w)
# store the binary string in redis
db.set(key, A)
# retrieve the proto-array from redis
A1 = db.get(key)
# deserialize it
array_dtype, l, w = key.split('|')[1].split('#')
A = NP.fromstring(A1, dtype=array_dtype).reshape(int(l), int(w))
@gwerbin
Copy link

gwerbin commented Oct 15, 2018

Both the BytesIO and "binary string" methods require making copies of the array, right? Would np.frombuffer instead of np.fromstring be able to load data from Redis without copying?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment