From 77b87e6d2ec85ce01ff8950bff850315c0728704 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sat, 21 Aug 2010 18:22:50 -0400 Subject: [PATCH] Fix hooke.util.yaml handling of assorted numpy.dtypes. --- hooke/util/yaml.py | 54 +++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 49 insertions(+), 5 deletions(-) diff --git a/hooke/util/yaml.py b/hooke/util/yaml.py index 22eeb14..f86e3b1 100644 --- a/hooke/util/yaml.py +++ b/hooke/util/yaml.py @@ -42,10 +42,10 @@ if False: # YAML dump debugging code """To help isolate data types etc. that give YAML problems. This is usually caused by external C modules (e.g. numpy) that - define new types (e.g. numpy.dtype) which YAML cannot inspect. + define new types (e.g. numpy.ndarray) which YAML cannot inspect. """ def ignore_aliases(data): - print data, type(data) + print data, repr(data), type(data), repr(type(data)) sys.stdout.flush() if data in [None, ()]: return True @@ -53,8 +53,52 @@ if False: # YAML dump debugging code return True yaml.representer.SafeRepresenter.ignore_aliases = staticmethod( ignore_aliases) +else: + # Avoid error with + # numpy.dtype(numpy.int32) in [None, ()] + # See + # http://projects.scipy.org/numpy/ticket/1001 + def ignore_aliases(data): + try: + if data in [None, ()]: + return True + if isinstance(data, (str, unicode, bool, int, float)): + return True + except TypeError, e: + pass + yaml.representer.SafeRepresenter.ignore_aliases = staticmethod( + ignore_aliases) -def ndarray_representer(dumper, data): + +def none_representer(dumper, data): return dumper.represent_none(None) -yaml.add_representer(numpy.ndarray, ndarray_representer) -yaml.add_representer(Data, ndarray_representer) +yaml.add_representer(numpy.ndarray, none_representer) +yaml.add_representer(numpy.dtype, none_representer) + +def bool_representer(dumper, data): + return dumper.represent_bool(data) +yaml.add_representer(numpy.bool_, bool_representer) + +def int_representer(dumper, data): + return dumper.represent_int(data) +yaml.add_representer(numpy.int32, int_representer) +yaml.add_representer(numpy.dtype(numpy.int32), int_representer) + +def long_representer(dumper, data): + return dumper.represent_long(data) +yaml.add_representer(numpy.int64, int_representer) + +def float_representer(dumper, data): + return dumper.represent_float(data) +yaml.add_representer(numpy.float32, float_representer) +yaml.add_representer(numpy.float64, float_representer) + +def data_representer(dumper, data): + info = dict(data.info) + print 'KEYS', info.keys() + for key in info.keys(): + if key.startswith('raw '):# or 'peak' in key: #or key not in ['surface deflection offset (m)', 'z piezo sensitivity (m/V)', 'z piezo scan (V/bit)', 'z piezo gain', 'deflection range (V)', 'z piezo range (V)', 'spring constant (N/m)', 'z piezo scan size (V)', 'deflection sensitivity (V/bit)', 'z piezo ramp size (V/bit)', 'surface deflection offset', 'z piezo offset (V)', 'name']: + del(info[key]) + print 'AAAS', info.keys() + return dumper.represent_mapping(u'!hooke.curve.DataInfo', info) +yaml.add_representer(Data, data_representer) -- 2.26.2