diff options
author | dhuard <dhuard@localhost> | 2008-04-03 14:06:33 +0000 |
---|---|---|
committer | dhuard <dhuard@localhost> | 2008-04-03 14:06:33 +0000 |
commit | 8784a1abd5c74dbd134a04a4bcd0f050fe650ed6 (patch) | |
tree | 35c66ab63c3c41aa432c2165091a12c87c41f87d | |
parent | 5fc40c054140b8f43eb2838f892367ca05bcf302 (diff) | |
download | numpy-8784a1abd5c74dbd134a04a4bcd0f050fe650ed6.tar.gz |
Fixed a bug with loadtxt and savetxt failing on record arrays. This addresses ticket #623.
Added simple tests for loadtxt and savetxt.
-rw-r--r-- | numpy/lib/io.py | 6 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 52 |
2 files changed, 54 insertions, 4 deletions
diff --git a/numpy/lib/io.py b/numpy/lib/io.py index 521148a5f..7be3c9804 100644 --- a/numpy/lib/io.py +++ b/numpy/lib/io.py @@ -312,9 +312,7 @@ def loadtxt(fname, dtype=float, comments='#', delimiter=None, converters=None, X.append(row) X = np.array(X, dtype) - r,c = X.shape - if r==1 or c==1: - X.shape = max([r,c]), + X = np.squeeze(X) if unpack: return X.T else: return X @@ -355,7 +353,7 @@ def savetxt(fname, X, fmt='%.18e',delimiter=' '): X = np.asarray(X) origShape = None - if len(X.shape)==1: + if len(X.shape)==1 and not hasattr(X.dtype, 'names'): origShape = X.shape X.shape = len(X), 1 for row in X: diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py new file mode 100644 index 000000000..19dbc8fd6 --- /dev/null +++ b/numpy/lib/tests/test_io.py @@ -0,0 +1,52 @@ +from numpy.testing import * +import numpy as np +import StringIO + +class Testsavetxt(NumpyTestCase): + def test_array(self): + a =np.array( [[1,2],[3,4]], float) + c = StringIO.StringIO() + np.savetxt(c, a) + c.seek(0) + assert(c.readlines(), ['1.000000000000000000e+00 2.000000000000000000e+00\n', '3.000000000000000000e+00 4.000000000000000000e+00\n']) + + a =np.array( [[1,2],[3,4]], int) + c = StringIO.StringIO() + np.savetxt(c, a) + c.seek(0) + assert(c.readlines(), ['1 2\n', '3 4\n']) + + def test_record(self): + a = np.array([(1, 2), (3, 4)], dtype=[('x', '<i4'), ('y', '<i4')]) + c = StringIO.StringIO() + np.savetxt(c, a, fmt='%d') + c.seek(0) + assert(c.readlines(), ['1 2\n', '3 4\n']) + + + +class Testloadtxt(NumpyTestCase): + def test_record(self): + c = StringIO.StringIO() + c.write('1 2\n3 4') + c.seek(0) + x = np.loadtxt(c, dtype=[('x', np.int32), ('y', np.int32)]) + a = np.array([(1, 2), (3, 4)], dtype=[('x', '<i4'), ('y', '<i4')]) + assert_array_equal(x, a) + + def test_array(self): + c = StringIO.StringIO() + c.write('1 2\n3 4') + + c.seek(0) + x = np.loadtxt(c, dtype=int) + a = np.array([[1,2],[3,4]], int) + assert_array_equal(x, a) + + c.seek(0) + x = np.loadtxt(c, dtype=float) + a = np.array([[1,2],[3,4]], float) + assert_array_equal(x, a) + +if __name__ == "__main__": + NumpyTest().run() |