8000 ENH: Add support for float hex format to loadtxt. by charris · Pull Request #5504 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

ENH: Add support for float hex format to loadtxt. #5504

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 13, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions doc/release/1.10.0-notes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
=======
Expand Down
13 changes: 12 additions & 1 deletion numpy/lib/npyio.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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_):
Expand Down Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions numpy/lib/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down
0