worker: trust that h5py maps all types as we want

This commit is contained in:
Robert Jördens 2016-04-05 15:26:48 +08:00
parent 4ba07e01d1
commit 690eb8c304
2 changed files with 4 additions and 54 deletions

View File

@ -157,56 +157,6 @@ def get_hdf5_output(start_time, rid, name):
return h5py.File(os.path.join(dirname, filename), "w") return h5py.File(os.path.join(dirname, filename), "w")
_type_to_hdf5 = {
int: h5py.h5t.STD_I64BE,
float: h5py.h5t.IEEE_F64BE,
np.int8: h5py.h5t.STD_I8BE,
np.int16: h5py.h5t.STD_I16BE,
np.int32: h5py.h5t.STD_I32BE,
np.int64: h5py.h5t.STD_I64BE,
np.uint8: h5py.h5t.STD_U8BE,
np.uint16: h5py.h5t.STD_U16BE,
np.uint32: h5py.h5t.STD_U32BE,
np.uint64: h5py.h5t.STD_U64BE,
np.float16: h5py.h5t.IEEE_F16BE,
np.float32: h5py.h5t.IEEE_F32BE,
np.float64: h5py.h5t.IEEE_F64BE
}
def result_dict_to_hdf5(f, rd):
for name, data in rd.items():
flag = None
# beware: isinstance(True/False, int) == True
if isinstance(data, bool):
data = np.int8(data)
flag = "py_bool"
elif isinstance(data, int):
data = np.int64(data)
flag = "py_int"
if isinstance(data, np.ndarray):
dataset = f.create_dataset(name, data=data)
else:
ty = type(data)
if ty is str:
ty_h5 = "S{}".format(len(data))
data = data.encode()
else:
try:
ty_h5 = _type_to_hdf5[ty]
except KeyError:
raise TypeError("Type {} is not supported for HDF5 output"
.format(ty)) from None
dataset = f.create_dataset(name, (), ty_h5)
dataset[()] = data
if flag is not None:
dataset.attrs[flag] = np.int8(1)
class DatasetManager: class DatasetManager:
def __init__(self, ddb): def __init__(self, ddb):
self.broadcast = Notifier(dict()) self.broadcast = Notifier(dict())
@ -241,4 +191,5 @@ class DatasetManager:
def write_hdf5(self, f): def write_hdf5(self, f):
g = f.create_group("datasets") g = f.create_group("datasets")
result_dict_to_hdf5(g, self.local) for k, v in self.local.items():
g[k] = v

View File

@ -3,8 +3,6 @@ import unittest
import h5py import h5py
import numpy as np import numpy as np
from artiq.master.worker_db import result_dict_to_hdf5
class TypesCase(unittest.TestCase): class TypesCase(unittest.TestCase):
def test_types(self): def test_types(self):
@ -25,4 +23,5 @@ class TypesCase(unittest.TestCase):
d["f"+str(size)] = getattr(np, "float" + str(size))(42) d["f"+str(size)] = getattr(np, "float" + str(size))(42)
with h5py.File("h5types.h5", "w", "core", backing_store=False) as f: with h5py.File("h5types.h5", "w", "core", backing_store=False) as f:
result_dict_to_hdf5(f, d) for k, v in d.items():
f[k] = v