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

numpy-svn@scip... numpy-svn@scip...
Wed Apr 16 09:28:19 CDT 2008


Author: dhuard
Date: 2008-04-16 09:28:11 -0500 (Wed, 16 Apr 2008)
New Revision: 5037

Modified:
   trunk/numpy/lib/io.py
   trunk/numpy/lib/tests/test_io.py
Log:
Added and fixed some tests for loadtxt and savetxt. Cleaned up the docstring of savetxt, added some info on formatting.

Modified: trunk/numpy/lib/io.py
===================================================================
--- trunk/numpy/lib/io.py	2008-04-15 05:08:04 UTC (rev 5036)
+++ trunk/numpy/lib/io.py	2008-04-16 14:28:11 UTC (rev 5037)
@@ -323,19 +323,52 @@
     Save the data in X to file fname using fmt string to convert the
     data to strings
 
-    fname can be a filename or a file handle.  If the filename ends in .gz,
-    the file is automatically saved in compressed gzip format.  The load()
-    command understands gzipped files transparently.
-
-    Example usage:
-
-    save('test.out', X)         # X is an array
-    save('test1.out', (x,y,z))  # x,y,z equal sized 1D arrays
-    save('test2.out', x)        # x is 1D
-    save('test3.out', x, fmt='%1.4e')  # use exponential notation
-
-    delimiter is used to separate the fields, eg delimiter ',' for
-    comma-separated values
+    Parameters
+    ----------
+    fname : filename or a file handle
+      If the filename ends in .gz, the file is automatically saved in 
+      compressed gzip format.  The load() command understands gzipped files 
+      transparently.
+    X : array or sequence
+      Data to write to file.
+    fmt : string 
+      A format string %[flags][width][.precision]specifier. See notes below for 
+      a description of some common flags and specifiers.
+    delimiter : str
+      Character separating columns.
+  
+    Examples
+    --------
+      >>> savetxt('test.out', x, delimiter=',')         # X is an array
+      >>> savetxt('test.out', (x,y,z))     # x,y,z equal sized 1D arrays
+      >>> savetxt('test.out', x, fmt='%1.4e')  # use exponential notation      
+    
+    Notes on fmt
+    ------------
+    flags:
+      - : left justify
+      + : Forces to preceed result with + or -.
+      0 : Left pad the number with zeros instead of space (see width).
+    width:
+      Minimum number of characters to be printed. The value is not truncated.
+    precision:
+      For integer specifiers (eg. d,i,o,x), the minimum number of digits.
+      For e, E and f specifiers, the number of digits to print after the decimal
+      point.
+      For g and G, the maximum number of significant digits.
+      For s, the maximum number of characters. 
+    specifiers:
+      c : character
+      d or i : signed decimal integer
+      e or E : scientific notation with e or E. 
+      f : decimal floating point
+      g,G : use the shorter of e,E or f
+      o : signed octal
+      s : string of characters
+      u : unsigned decimal integer
+      x,X : unsigned hexadecimal integer
+      
+    This is not an exhaustive specification. 
     """
 
     if _string_like(fname):

Modified: trunk/numpy/lib/tests/test_io.py
===================================================================
--- trunk/numpy/lib/tests/test_io.py	2008-04-15 05:08:04 UTC (rev 5036)
+++ trunk/numpy/lib/tests/test_io.py	2008-04-16 14:28:11 UTC (rev 5037)
@@ -14,25 +14,55 @@
 
         a =np.array( [[1,2],[3,4]], int)
         c = StringIO.StringIO()
-        np.savetxt(c, a)
+        np.savetxt(c, a, fmt='%d')
         c.seek(0)
-        assert(c.readlines(), ['1 2\n', '3 4\n'])
+        assert_equal(c.readlines(), ['1 2\n', '3 4\n'])
 
     def test_1D(self):
         a = np.array([1,2,3,4], int)
         c = StringIO.StringIO()
         np.savetxt(c, a, fmt='%d')
         c.seek(0)
-        assert(c.readlines(), ['1\n', '2\n', '3\n', '4\n'])
+        lines = c.readlines()
+        assert_equal(lines, ['1\n', '2\n', '3\n', '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'])
+        assert_equal(c.readlines(), ['1 2\n', '3 4\n'])
+    def test_delimiter(self):
+        a = np.array([[1., 2.], [3., 4.]])
+        c = StringIO.StringIO()
+        np.savetxt(c, a, delimiter=',', fmt='%d')
+        c.seek(0)
+        assert_equal(c.readlines(), ['1,2\n', '3,4\n'])
+        
+        
+##    def test_format(self):
+##        a = np.array([(1, 2), (3, 4)])
+##        c = StringIO.StringIO()
+##        # Sequence of formats
+##        np.savetxt(c, a, fmt=['%02d', '%3.1f'])
+##        c.seek(0)
+##        assert_equal(c.readlines(), ['01 2.0\n', '03 4.0\n'])
+##        
+##        # A single multiformat string
+##        c = StringIO.StringIO()
+##        np.savetxt(c, a, fmt='%02d : %3.1f')
+##        c.seek(0)
+##        lines = c.readlines()
+##        assert_equal(lines, ['01 : 2.0\n', '03 : 4.0\n'])
+##        
+##        # Specify delimiter, should be overiden
+##        c = StringIO.StringIO()
+##        np.savetxt(c, a, fmt='%02d : %3.1f', delimiter=',')
+##        c.seek(0)
+##        lines = c.readlines()
+##        assert_equal(lines, ['01 : 2.0\n', '03 : 4.0\n'])
+        
 
-
 class TestLoadTxt(NumpyTestCase):
     def test_record(self):
         c = StringIO.StringIO()
@@ -45,7 +75,6 @@
         d = StringIO.StringIO()
         d.write('M 64.0 75.0\nF 25.0 60.0')
         d.seek(0)
-
         mydescriptor = {'names': ('gender','age','weight'),
                         'formats': ('S1',
                                     'i4', 'f4')}
@@ -54,6 +83,7 @@
         y = np.loadtxt(d, dtype=mydescriptor)
         assert_array_equal(y, b)
 
+
     def test_array(self):
         c = StringIO.StringIO()
         c.write('1 2\n3 4')
@@ -92,6 +122,48 @@
             converters={3:lambda s: int(s or -999)})
         a = np.array([1,2,3,-999,5], int)
         assert_array_equal(x, a)
+        
+    def test_comments(self):
+        c = StringIO.StringIO()
+        c.write('# comment\n1,2,3,5\n')
+        c.seek(0)
+        x = np.loadtxt(c, dtype=int, delimiter=',', \
+            comments='#')
+        a = np.array([1,2,3,5], int)
+        assert_array_equal(x, a)
+        
+    def test_skiprows(self):
+        c = StringIO.StringIO()
+        c.write('comment\n1,2,3,5\n')
+        c.seek(0)
+        x = np.loadtxt(c, dtype=int, delimiter=',', \
+            skiprows=1)
+        a = np.array([1,2,3,5], int)
+        assert_array_equal(x, a)
+        
+        c = StringIO.StringIO()
+        c.write('# comment\n1,2,3,5\n')
+        c.seek(0)
+        x = np.loadtxt(c, dtype=int, delimiter=',', \
+            skiprows=1)
+        a = np.array([1,2,3,5], int)
+        assert_array_equal(x, a)
+        
+    def test_usecols(self):
+        a =np.array( [[1,2],[3,4]], float)
+        c = StringIO.StringIO()
+        np.savetxt(c, a)
+        c.seek(0)
+        x = np.loadtxt(c, dtype=float, usecols=(1,))
+        assert_array_equal(x, a[:,1])
+        
+        a =np.array( [[1,2,3],[3,4,5]], float)
+        c = StringIO.StringIO()
+        np.savetxt(c, a)
+        c.seek(0)
+        x = np.loadtxt(c, dtype=float, usecols=(1,2))
+        assert_array_equal(x, a[:,1:])
+        
 
 class Testfromregex(NumpyTestCase):
     def test_record(self):



More information about the Numpy-svn mailing list