convert lists to numpy arrays (in order to handle non-1D lists)
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import numpy as np
|
||||
from bsread.sender import Sender, PUB
|
||||
|
||||
|
||||
@@ -18,10 +19,20 @@ def pack_bsread_data(orig, prefix, skip=None):
|
||||
continue
|
||||
if isinstance(v, bool):
|
||||
v = int(v)
|
||||
elif isinstance(v, list) and not v:
|
||||
v = None
|
||||
elif isinstance(v, list):
|
||||
# bsread fails for empty lists and non-1D lists
|
||||
v = list_to_array(v)
|
||||
data[f"{prefix}:{k}"] = v
|
||||
return data
|
||||
|
||||
|
||||
def list_to_array(x):
|
||||
try:
|
||||
# let numpy figure out the dtype
|
||||
return np.array(x)
|
||||
except ValueError:
|
||||
# the above fails for ragged lists, which need to be object arrays
|
||||
return np.array(x, dtype=object)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user