From fe6444438d621b2ba5df1e0de025e71c2cdbd0de Mon Sep 17 00:00:00 2001 From: Antonino Ingargiola Date: Thu, 29 Oct 2015 13:50:40 -0700 Subject: [PATCH] Cast bools to integers for compatibility --- phconvert/hdf5.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/phconvert/hdf5.py b/phconvert/hdf5.py index 7c81f85..3e3c493 100644 --- a/phconvert/hdf5.py +++ b/phconvert/hdf5.py @@ -550,6 +550,24 @@ def photon_data_mapping(h5file, name='timestamps'): mapping[ch] = ph return mapping +def _is_sequence(obj): + is_sequence = False + if (isinstance(obj, tuple) or isinstance(obj, list) or + isinstance(obj, np.ndarray)): + is_sequence = True + return is_sequence + +def _normalize_bools(data_dict): + """Cast bools (both scalars or in sequences) to integers.""" + for name, value in data_dict.items(): + if isinstance(value, dict): + _normalize_bools(value) + else: + if isinstance(value, bool): + data_dict[name] = int(value) + elif _is_sequence(value) and isinstance(value[0], bool): + data_dict[name] = np.asarray(value, dtype='uint8') + def _sanitize_data(data_dict): """Perform type conversions to strictly conform to Photon-HDF5 specs. @@ -557,7 +575,11 @@ def _sanitize_data(data_dict): - assure that fields in detectors_specs have same dtype as detectors - convert scalar fields that are array of size == 1 to scalars + - cast bools or sequences of bools to integers """ + ## Cast bools to ints + _normalize_bools(data_dict) + ## detectors_specs conversions ph_data = data_dict[_sorted_photon_data(data_dict)[0]] dtype = ph_data['detectors'].dtype