From 587a0f4565a017286b6c46562e763091f3000b19 Mon Sep 17 00:00:00 2001 From: Robert Jordens Date: Tue, 5 Apr 2016 15:26:48 +0800 Subject: [PATCH] worker: trust that h5py maps all types as we want --- artiq/master/worker_db.py | 53 ++------------------------------------ artiq/test/test_h5types.py | 5 ++-- 2 files changed, 4 insertions(+), 54 deletions(-) diff --git a/artiq/master/worker_db.py b/artiq/master/worker_db.py index 16046cd26..15c57fe00 100644 --- a/artiq/master/worker_db.py +++ b/artiq/master/worker_db.py @@ -167,56 +167,6 @@ def get_hdf5_output(start_time, rid, name): 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: def __init__(self, ddb): self.broadcast = Notifier(dict()) @@ -251,4 +201,5 @@ class DatasetManager: def write_hdf5(self, f): g = f.create_group("datasets") - result_dict_to_hdf5(g, self.local) + for k, v in self.local.items(): + g[k] = v diff --git a/artiq/test/test_h5types.py b/artiq/test/test_h5types.py index c64a9c2e5..eafcfd6cd 100644 --- a/artiq/test/test_h5types.py +++ b/artiq/test/test_h5types.py @@ -3,8 +3,6 @@ import unittest import h5py import numpy as np -from artiq.master.worker_db import result_dict_to_hdf5 - class TypesCase(unittest.TestCase): def test_types(self): @@ -25,4 +23,5 @@ class TypesCase(unittest.TestCase): d["f"+str(size)] = getattr(np, "float" + str(size))(42) 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