[Numpy-svn] r4956 - in trunk/numpy/lib: . tests

numpy-svn@scip... numpy-svn@scip...
Thu Apr 3 09:06:36 CDT 2008


Author: dhuard
Date: 2008-04-03 09:06:33 -0500 (Thu, 03 Apr 2008)
New Revision: 4956

Added:
   trunk/numpy/lib/tests/test_io.py
Modified:
   trunk/numpy/lib/io.py
Log:
Fixed a bug with loadtxt and savetxt failing on record arrays. This addresses ticket #623. 
Added simple tests for loadtxt and savetxt.

Modified: trunk/numpy/lib/io.py
===================================================================
--- trunk/numpy/lib/io.py	2008-04-03 07:06:46 UTC (rev 4955)
+++ trunk/numpy/lib/io.py	2008-04-03 14:06:33 UTC (rev 4956)
@@ -312,9 +312,7 @@
         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 @@
 
     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:

Added: trunk/numpy/lib/tests/test_io.py
===================================================================
--- trunk/numpy/lib/tests/test_io.py	2008-04-03 07:06:46 UTC (rev 4955)
+++ trunk/numpy/lib/tests/test_io.py	2008-04-03 14:06:33 UTC (rev 4956)
@@ -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()



More information about the Numpy-svn mailing list