From 4aef6a89fda5015129e124099f3809fa4da894a7 Mon Sep 17 00:00:00 2001 From: Chris Laumann Date: Wed, 3 Aug 2011 22:35:43 -0400 Subject: [PATCH 1/3] ENH: Add support for float hex format to loadtxt. Add _floatconv to npyio.py as a default floating point converter. This uses float() as a type conversion with a fallback on (ValueError) to float.fromhex(). Closes #2517. --- numpy/lib/npyio.py | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/numpy/lib/npyio.py b/numpy/lib/npyio.py index 90ef83f4d619..0632ba1f8fe6 100644 --- a/numpy/lib/npyio.py +++ b/numpy/lib/npyio.py @@ -621,6 +621,13 @@ def _savez(file, args, kwds, compress): def _getconv(dtype): """ Find the correct dtype converter. Adapted from matplotlib """ + + def floatconv(x): + x.lower() + if b'0x' in x: + return float.fromhex(asstr(x)) + return float(x) + typ = dtype.type if issubclass(typ, np.bool_): return lambda x: bool(int(x)) @@ -631,7 +638,7 @@ def _getconv(dtype): if issubclass(typ, np.integer): return lambda x: int(float(x)) elif issubclass(typ, np.floating): - return float + return floatconv elif issubclass(typ, np.complex): return complex elif issubclass(typ, np.bytes_): @@ -706,6 +713,10 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, `genfromtxt` function provides more sophisticated handling of, e.g., lines with missing values. + .. versionadded:: 1.10.0 + The strings produced by the Python float.hex method can be used as + input for floats. + Examples -------- >>> from StringIO import StringIO # StringIO behaves like a file object From 31f5d40b6c5a1ea1cafd2a0892bd53f7e6d50061 Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Thu, 12 Feb 2015 12:51:37 -0700 Subject: [PATCH 2/3] TST: Make loadtxt able to load floats as hex strings. The strings must be produced by the python float.hex method. --- numpy/lib/tests/test_io.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index 05db34d0f6a6..7054ab1fef7a 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -694,6 +694,19 @@ def test_int64_type(self): res = np.loadtxt(c, dtype=np.int64) assert_equal(res, tgt) + def test_from_float_hex(self): + # IEEE doubles and floats only, otherwise the float32 + # conversion may fail. + tgt = np.logspace(-10, 10, 5).astype(np.float32) + tgt = np.hstack((tgt, -tgt)).astype(np.float) + inp = '\n'.join(map(float.hex, tgt)) + c = TextIO() + c.write(inp) + for dt in [np.float, np.float32]: + c.seek(0) + res = np.loadtxt(c, dtype=dt) + assert_equal(res, tgt, err_msg="%s" % dt) + def test_universal_newline(self): f, name = mkstemp() os.write(f, b'1 21\r3 42\r') From 05164650d0818f6a8fbbe81612666f96f0f0efdf Mon Sep 17 00:00:00 2001 From: Charles Harris Date: Thu, 12 Feb 2015 13:09:29 -0700 Subject: [PATCH 3/3] DOC: Document loadtxt support for float.hex float representation. --- doc/release/1.10.0-notes.rst | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/doc/release/1.10.0-notes.rst b/doc/release/1.10.0-notes.rst index ec54b1f14e9b..a758be6b5628 100644 --- a/doc/release/1.10.0-notes.rst +++ b/doc/release/1.10.0-notes.rst @@ -139,6 +139,11 @@ fallback implementations of the following functions. As a result of these improvements, there will be some small changes in returned values, especially for corner cases. +*np.loadtxt* support for the strings produced by the ``float.hex`` method +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +The strings produced by ``float.hex`` look like ``0x1.921fb54442d18p+1``, +so this is not the hex used to represent unsigned integer types. + Changes =======