Skip to content

Commit

Permalink
Added and fixed some tests for loadtxt and savetxt. Cleaned up the do…
Browse files Browse the repository at this point in the history
…cstring of savetxt, added some info on formatting.
  • Loading branch information
dhuard committed Apr 16, 2008
1 parent 3b6397f commit 13f9b4a
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 19 deletions.
59 changes: 46 additions & 13 deletions numpy/lib/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,19 +323,52 @@ def savetxt(fname, X, fmt='%.18e',delimiter=' '):
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):
Expand Down
84 changes: 78 additions & 6 deletions numpy/lib/tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,54 @@ def test_array(self):

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):
Expand All @@ -45,7 +75,6 @@ def test_record(self):
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')}
Expand All @@ -54,6 +83,7 @@ def test_record(self):
y = np.loadtxt(d, dtype=mydescriptor)
assert_array_equal(y, b)


def test_array(self):
c = StringIO.StringIO()
c.write('1 2\n3 4')
Expand Down Expand Up @@ -92,6 +122,48 @@ def test_missing(self):
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):
Expand Down

0 comments on commit 13f9b4a

Please sign in to comment.