Chris@87
|
1 from __future__ import division, absolute_import, print_function
|
Chris@87
|
2
|
Chris@87
|
3 import sys
|
Chris@87
|
4 import gzip
|
Chris@87
|
5 import os
|
Chris@87
|
6 import threading
|
Chris@87
|
7 from tempfile import mkstemp, NamedTemporaryFile
|
Chris@87
|
8 import time
|
Chris@87
|
9 import warnings
|
Chris@87
|
10 import gc
|
Chris@87
|
11 from io import BytesIO
|
Chris@87
|
12 from datetime import datetime
|
Chris@87
|
13
|
Chris@87
|
14 import numpy as np
|
Chris@87
|
15 import numpy.ma as ma
|
Chris@87
|
16 from numpy.lib._iotools import (ConverterError, ConverterLockError,
|
Chris@87
|
17 ConversionWarning)
|
Chris@87
|
18 from numpy.compat import asbytes, asbytes_nested, bytes, asstr
|
Chris@87
|
19 from nose import SkipTest
|
Chris@87
|
20 from numpy.ma.testutils import (
|
Chris@87
|
21 TestCase, assert_equal, assert_array_equal,
|
Chris@87
|
22 assert_raises, assert_raises_regex, run_module_suite
|
Chris@87
|
23 )
|
Chris@87
|
24 from numpy.testing import assert_warns, assert_, build_err_msg
|
Chris@87
|
25 from numpy.testing.utils import tempdir
|
Chris@87
|
26
|
Chris@87
|
27
|
Chris@87
|
28 class TextIO(BytesIO):
|
Chris@87
|
29 """Helper IO class.
|
Chris@87
|
30
|
Chris@87
|
31 Writes encode strings to bytes if needed, reads return bytes.
|
Chris@87
|
32 This makes it easier to emulate files opened in binary mode
|
Chris@87
|
33 without needing to explicitly convert strings to bytes in
|
Chris@87
|
34 setting up the test data.
|
Chris@87
|
35
|
Chris@87
|
36 """
|
Chris@87
|
37 def __init__(self, s=""):
|
Chris@87
|
38 BytesIO.__init__(self, asbytes(s))
|
Chris@87
|
39
|
Chris@87
|
40 def write(self, s):
|
Chris@87
|
41 BytesIO.write(self, asbytes(s))
|
Chris@87
|
42
|
Chris@87
|
43 def writelines(self, lines):
|
Chris@87
|
44 BytesIO.writelines(self, [asbytes(s) for s in lines])
|
Chris@87
|
45
|
Chris@87
|
46
|
Chris@87
|
47 MAJVER, MINVER = sys.version_info[:2]
|
Chris@87
|
48 IS_64BIT = sys.maxsize > 2**32
|
Chris@87
|
49
|
Chris@87
|
50
|
Chris@87
|
51 def strptime(s, fmt=None):
|
Chris@87
|
52 """This function is available in the datetime module only
|
Chris@87
|
53 from Python >= 2.5.
|
Chris@87
|
54
|
Chris@87
|
55 """
|
Chris@87
|
56 if sys.version_info[0] >= 3:
|
Chris@87
|
57 return datetime(*time.strptime(s.decode('latin1'), fmt)[:3])
|
Chris@87
|
58 else:
|
Chris@87
|
59 return datetime(*time.strptime(s, fmt)[:3])
|
Chris@87
|
60
|
Chris@87
|
61
|
Chris@87
|
62 class RoundtripTest(object):
|
Chris@87
|
63 def roundtrip(self, save_func, *args, **kwargs):
|
Chris@87
|
64 """
|
Chris@87
|
65 save_func : callable
|
Chris@87
|
66 Function used to save arrays to file.
|
Chris@87
|
67 file_on_disk : bool
|
Chris@87
|
68 If true, store the file on disk, instead of in a
|
Chris@87
|
69 string buffer.
|
Chris@87
|
70 save_kwds : dict
|
Chris@87
|
71 Parameters passed to `save_func`.
|
Chris@87
|
72 load_kwds : dict
|
Chris@87
|
73 Parameters passed to `numpy.load`.
|
Chris@87
|
74 args : tuple of arrays
|
Chris@87
|
75 Arrays stored to file.
|
Chris@87
|
76
|
Chris@87
|
77 """
|
Chris@87
|
78 save_kwds = kwargs.get('save_kwds', {})
|
Chris@87
|
79 load_kwds = kwargs.get('load_kwds', {})
|
Chris@87
|
80 file_on_disk = kwargs.get('file_on_disk', False)
|
Chris@87
|
81
|
Chris@87
|
82 if file_on_disk:
|
Chris@87
|
83 target_file = NamedTemporaryFile(delete=False)
|
Chris@87
|
84 load_file = target_file.name
|
Chris@87
|
85 else:
|
Chris@87
|
86 target_file = BytesIO()
|
Chris@87
|
87 load_file = target_file
|
Chris@87
|
88
|
Chris@87
|
89 try:
|
Chris@87
|
90 arr = args
|
Chris@87
|
91
|
Chris@87
|
92 save_func(target_file, *arr, **save_kwds)
|
Chris@87
|
93 target_file.flush()
|
Chris@87
|
94 target_file.seek(0)
|
Chris@87
|
95
|
Chris@87
|
96 if sys.platform == 'win32' and not isinstance(target_file, BytesIO):
|
Chris@87
|
97 target_file.close()
|
Chris@87
|
98
|
Chris@87
|
99 arr_reloaded = np.load(load_file, **load_kwds)
|
Chris@87
|
100
|
Chris@87
|
101 self.arr = arr
|
Chris@87
|
102 self.arr_reloaded = arr_reloaded
|
Chris@87
|
103 finally:
|
Chris@87
|
104 if not isinstance(target_file, BytesIO):
|
Chris@87
|
105 target_file.close()
|
Chris@87
|
106 # holds an open file descriptor so it can't be deleted on win
|
Chris@87
|
107 if not isinstance(arr_reloaded, np.lib.npyio.NpzFile):
|
Chris@87
|
108 os.remove(target_file.name)
|
Chris@87
|
109
|
Chris@87
|
110 def check_roundtrips(self, a):
|
Chris@87
|
111 self.roundtrip(a)
|
Chris@87
|
112 self.roundtrip(a, file_on_disk=True)
|
Chris@87
|
113 self.roundtrip(np.asfortranarray(a))
|
Chris@87
|
114 self.roundtrip(np.asfortranarray(a), file_on_disk=True)
|
Chris@87
|
115 if a.shape[0] > 1:
|
Chris@87
|
116 # neither C nor Fortran contiguous for 2D arrays or more
|
Chris@87
|
117 self.roundtrip(np.asfortranarray(a)[1:])
|
Chris@87
|
118 self.roundtrip(np.asfortranarray(a)[1:], file_on_disk=True)
|
Chris@87
|
119
|
Chris@87
|
120 def test_array(self):
|
Chris@87
|
121 a = np.array([], float)
|
Chris@87
|
122 self.check_roundtrips(a)
|
Chris@87
|
123
|
Chris@87
|
124 a = np.array([[1, 2], [3, 4]], float)
|
Chris@87
|
125 self.check_roundtrips(a)
|
Chris@87
|
126
|
Chris@87
|
127 a = np.array([[1, 2], [3, 4]], int)
|
Chris@87
|
128 self.check_roundtrips(a)
|
Chris@87
|
129
|
Chris@87
|
130 a = np.array([[1 + 5j, 2 + 6j], [3 + 7j, 4 + 8j]], dtype=np.csingle)
|
Chris@87
|
131 self.check_roundtrips(a)
|
Chris@87
|
132
|
Chris@87
|
133 a = np.array([[1 + 5j, 2 + 6j], [3 + 7j, 4 + 8j]], dtype=np.cdouble)
|
Chris@87
|
134 self.check_roundtrips(a)
|
Chris@87
|
135
|
Chris@87
|
136 def test_array_object(self):
|
Chris@87
|
137 if sys.version_info[:2] >= (2, 7):
|
Chris@87
|
138 a = np.array([], object)
|
Chris@87
|
139 self.check_roundtrips(a)
|
Chris@87
|
140
|
Chris@87
|
141 a = np.array([[1, 2], [3, 4]], object)
|
Chris@87
|
142 self.check_roundtrips(a)
|
Chris@87
|
143 # Fails with UnpicklingError: could not find MARK on Python 2.6
|
Chris@87
|
144
|
Chris@87
|
145 def test_1D(self):
|
Chris@87
|
146 a = np.array([1, 2, 3, 4], int)
|
Chris@87
|
147 self.roundtrip(a)
|
Chris@87
|
148
|
Chris@87
|
149 @np.testing.dec.knownfailureif(sys.platform == 'win32', "Fail on Win32")
|
Chris@87
|
150 def test_mmap(self):
|
Chris@87
|
151 a = np.array([[1, 2.5], [4, 7.3]])
|
Chris@87
|
152 self.roundtrip(a, file_on_disk=True, load_kwds={'mmap_mode': 'r'})
|
Chris@87
|
153
|
Chris@87
|
154 a = np.asfortranarray([[1, 2.5], [4, 7.3]])
|
Chris@87
|
155 self.roundtrip(a, file_on_disk=True, load_kwds={'mmap_mode': 'r'})
|
Chris@87
|
156
|
Chris@87
|
157 def test_record(self):
|
Chris@87
|
158 a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
|
Chris@87
|
159 self.check_roundtrips(a)
|
Chris@87
|
160
|
Chris@87
|
161 def test_format_2_0(self):
|
Chris@87
|
162 dt = [(("%d" % i) * 100, float) for i in range(500)]
|
Chris@87
|
163 a = np.ones(1000, dtype=dt)
|
Chris@87
|
164 with warnings.catch_warnings(record=True):
|
Chris@87
|
165 warnings.filterwarnings('always', '', UserWarning)
|
Chris@87
|
166 self.check_roundtrips(a)
|
Chris@87
|
167
|
Chris@87
|
168
|
Chris@87
|
169 class TestSaveLoad(RoundtripTest, TestCase):
|
Chris@87
|
170 def roundtrip(self, *args, **kwargs):
|
Chris@87
|
171 RoundtripTest.roundtrip(self, np.save, *args, **kwargs)
|
Chris@87
|
172 assert_equal(self.arr[0], self.arr_reloaded)
|
Chris@87
|
173 assert_equal(self.arr[0].dtype, self.arr_reloaded.dtype)
|
Chris@87
|
174 assert_equal(self.arr[0].flags.fnc, self.arr_reloaded.flags.fnc)
|
Chris@87
|
175
|
Chris@87
|
176
|
Chris@87
|
177 class TestSavezLoad(RoundtripTest, TestCase):
|
Chris@87
|
178 def roundtrip(self, *args, **kwargs):
|
Chris@87
|
179 RoundtripTest.roundtrip(self, np.savez, *args, **kwargs)
|
Chris@87
|
180 try:
|
Chris@87
|
181 for n, arr in enumerate(self.arr):
|
Chris@87
|
182 reloaded = self.arr_reloaded['arr_%d' % n]
|
Chris@87
|
183 assert_equal(arr, reloaded)
|
Chris@87
|
184 assert_equal(arr.dtype, reloaded.dtype)
|
Chris@87
|
185 assert_equal(arr.flags.fnc, reloaded.flags.fnc)
|
Chris@87
|
186 finally:
|
Chris@87
|
187 # delete tempfile, must be done here on windows
|
Chris@87
|
188 if self.arr_reloaded.fid:
|
Chris@87
|
189 self.arr_reloaded.fid.close()
|
Chris@87
|
190 os.remove(self.arr_reloaded.fid.name)
|
Chris@87
|
191
|
Chris@87
|
192 @np.testing.dec.skipif(not IS_64BIT, "Works only with 64bit systems")
|
Chris@87
|
193 @np.testing.dec.slow
|
Chris@87
|
194 def test_big_arrays(self):
|
Chris@87
|
195 L = (1 << 31) + 100000
|
Chris@87
|
196 a = np.empty(L, dtype=np.uint8)
|
Chris@87
|
197 with tempdir(prefix="numpy_test_big_arrays_") as tmpdir:
|
Chris@87
|
198 tmp = os.path.join(tmpdir, "file.npz")
|
Chris@87
|
199 np.savez(tmp, a=a)
|
Chris@87
|
200 del a
|
Chris@87
|
201 npfile = np.load(tmp)
|
Chris@87
|
202 a = npfile['a']
|
Chris@87
|
203 npfile.close()
|
Chris@87
|
204
|
Chris@87
|
205 def test_multiple_arrays(self):
|
Chris@87
|
206 a = np.array([[1, 2], [3, 4]], float)
|
Chris@87
|
207 b = np.array([[1 + 2j, 2 + 7j], [3 - 6j, 4 + 12j]], complex)
|
Chris@87
|
208 self.roundtrip(a, b)
|
Chris@87
|
209
|
Chris@87
|
210 def test_named_arrays(self):
|
Chris@87
|
211 a = np.array([[1, 2], [3, 4]], float)
|
Chris@87
|
212 b = np.array([[1 + 2j, 2 + 7j], [3 - 6j, 4 + 12j]], complex)
|
Chris@87
|
213 c = BytesIO()
|
Chris@87
|
214 np.savez(c, file_a=a, file_b=b)
|
Chris@87
|
215 c.seek(0)
|
Chris@87
|
216 l = np.load(c)
|
Chris@87
|
217 assert_equal(a, l['file_a'])
|
Chris@87
|
218 assert_equal(b, l['file_b'])
|
Chris@87
|
219
|
Chris@87
|
220 def test_savez_filename_clashes(self):
|
Chris@87
|
221 # Test that issue #852 is fixed
|
Chris@87
|
222 # and savez functions in multithreaded environment
|
Chris@87
|
223
|
Chris@87
|
224 def writer(error_list):
|
Chris@87
|
225 fd, tmp = mkstemp(suffix='.npz')
|
Chris@87
|
226 os.close(fd)
|
Chris@87
|
227 try:
|
Chris@87
|
228 arr = np.random.randn(500, 500)
|
Chris@87
|
229 try:
|
Chris@87
|
230 np.savez(tmp, arr=arr)
|
Chris@87
|
231 except OSError as err:
|
Chris@87
|
232 error_list.append(err)
|
Chris@87
|
233 finally:
|
Chris@87
|
234 os.remove(tmp)
|
Chris@87
|
235
|
Chris@87
|
236 errors = []
|
Chris@87
|
237 threads = [threading.Thread(target=writer, args=(errors,))
|
Chris@87
|
238 for j in range(3)]
|
Chris@87
|
239 for t in threads:
|
Chris@87
|
240 t.start()
|
Chris@87
|
241 for t in threads:
|
Chris@87
|
242 t.join()
|
Chris@87
|
243
|
Chris@87
|
244 if errors:
|
Chris@87
|
245 raise AssertionError(errors)
|
Chris@87
|
246
|
Chris@87
|
247 def test_not_closing_opened_fid(self):
|
Chris@87
|
248 # Test that issue #2178 is fixed:
|
Chris@87
|
249 # verify could seek on 'loaded' file
|
Chris@87
|
250
|
Chris@87
|
251 fd, tmp = mkstemp(suffix='.npz')
|
Chris@87
|
252 os.close(fd)
|
Chris@87
|
253 try:
|
Chris@87
|
254 fp = open(tmp, 'wb')
|
Chris@87
|
255 np.savez(fp, data='LOVELY LOAD')
|
Chris@87
|
256 fp.close()
|
Chris@87
|
257
|
Chris@87
|
258 fp = open(tmp, 'rb', 10000)
|
Chris@87
|
259 fp.seek(0)
|
Chris@87
|
260 assert_(not fp.closed)
|
Chris@87
|
261 _ = np.load(fp)['data']
|
Chris@87
|
262 assert_(not fp.closed)
|
Chris@87
|
263 # must not get closed by .load(opened fp)
|
Chris@87
|
264 fp.seek(0)
|
Chris@87
|
265 assert_(not fp.closed)
|
Chris@87
|
266
|
Chris@87
|
267 finally:
|
Chris@87
|
268 fp.close()
|
Chris@87
|
269 os.remove(tmp)
|
Chris@87
|
270
|
Chris@87
|
271 def test_closing_fid(self):
|
Chris@87
|
272 # Test that issue #1517 (too many opened files) remains closed
|
Chris@87
|
273 # It might be a "weak" test since failed to get triggered on
|
Chris@87
|
274 # e.g. Debian sid of 2012 Jul 05 but was reported to
|
Chris@87
|
275 # trigger the failure on Ubuntu 10.04:
|
Chris@87
|
276 # http://projects.scipy.org/numpy/ticket/1517#comment:2
|
Chris@87
|
277 fd, tmp = mkstemp(suffix='.npz')
|
Chris@87
|
278 os.close(fd)
|
Chris@87
|
279
|
Chris@87
|
280 try:
|
Chris@87
|
281 fp = open(tmp, 'wb')
|
Chris@87
|
282 np.savez(fp, data='LOVELY LOAD')
|
Chris@87
|
283 fp.close()
|
Chris@87
|
284 # We need to check if the garbage collector can properly close
|
Chris@87
|
285 # numpy npz file returned by np.load when their reference count
|
Chris@87
|
286 # goes to zero. Python 3 running in debug mode raises a
|
Chris@87
|
287 # ResourceWarning when file closing is left to the garbage
|
Chris@87
|
288 # collector, so we catch the warnings. Because ResourceWarning
|
Chris@87
|
289 # is unknown in Python < 3.x, we take the easy way out and
|
Chris@87
|
290 # catch all warnings.
|
Chris@87
|
291 with warnings.catch_warnings():
|
Chris@87
|
292 warnings.simplefilter("ignore")
|
Chris@87
|
293 for i in range(1, 1025):
|
Chris@87
|
294 try:
|
Chris@87
|
295 np.load(tmp)["data"]
|
Chris@87
|
296 except Exception as e:
|
Chris@87
|
297 msg = "Failed to load data from a file: %s" % e
|
Chris@87
|
298 raise AssertionError(msg)
|
Chris@87
|
299 finally:
|
Chris@87
|
300 os.remove(tmp)
|
Chris@87
|
301
|
Chris@87
|
302 def test_closing_zipfile_after_load(self):
|
Chris@87
|
303 # Check that zipfile owns file and can close it.
|
Chris@87
|
304 # This needs to pass a file name to load for the
|
Chris@87
|
305 # test.
|
Chris@87
|
306 with tempdir(prefix="numpy_test_closing_zipfile_after_load_") as tmpdir:
|
Chris@87
|
307 fd, tmp = mkstemp(suffix='.npz', dir=tmpdir)
|
Chris@87
|
308 os.close(fd)
|
Chris@87
|
309 np.savez(tmp, lab='place holder')
|
Chris@87
|
310 data = np.load(tmp)
|
Chris@87
|
311 fp = data.zip.fp
|
Chris@87
|
312 data.close()
|
Chris@87
|
313 assert_(fp.closed)
|
Chris@87
|
314
|
Chris@87
|
315
|
Chris@87
|
316 class TestSaveTxt(TestCase):
|
Chris@87
|
317 def test_array(self):
|
Chris@87
|
318 a = np.array([[1, 2], [3, 4]], float)
|
Chris@87
|
319 fmt = "%.18e"
|
Chris@87
|
320 c = BytesIO()
|
Chris@87
|
321 np.savetxt(c, a, fmt=fmt)
|
Chris@87
|
322 c.seek(0)
|
Chris@87
|
323 assert_equal(c.readlines(),
|
Chris@87
|
324 [asbytes((fmt + ' ' + fmt + '\n') % (1, 2)),
|
Chris@87
|
325 asbytes((fmt + ' ' + fmt + '\n') % (3, 4))])
|
Chris@87
|
326
|
Chris@87
|
327 a = np.array([[1, 2], [3, 4]], int)
|
Chris@87
|
328 c = BytesIO()
|
Chris@87
|
329 np.savetxt(c, a, fmt='%d')
|
Chris@87
|
330 c.seek(0)
|
Chris@87
|
331 assert_equal(c.readlines(), [b'1 2\n', b'3 4\n'])
|
Chris@87
|
332
|
Chris@87
|
333 def test_1D(self):
|
Chris@87
|
334 a = np.array([1, 2, 3, 4], int)
|
Chris@87
|
335 c = BytesIO()
|
Chris@87
|
336 np.savetxt(c, a, fmt='%d')
|
Chris@87
|
337 c.seek(0)
|
Chris@87
|
338 lines = c.readlines()
|
Chris@87
|
339 assert_equal(lines, [b'1\n', b'2\n', b'3\n', b'4\n'])
|
Chris@87
|
340
|
Chris@87
|
341 def test_record(self):
|
Chris@87
|
342 a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
|
Chris@87
|
343 c = BytesIO()
|
Chris@87
|
344 np.savetxt(c, a, fmt='%d')
|
Chris@87
|
345 c.seek(0)
|
Chris@87
|
346 assert_equal(c.readlines(), [b'1 2\n', b'3 4\n'])
|
Chris@87
|
347
|
Chris@87
|
348 def test_delimiter(self):
|
Chris@87
|
349 a = np.array([[1., 2.], [3., 4.]])
|
Chris@87
|
350 c = BytesIO()
|
Chris@87
|
351 np.savetxt(c, a, delimiter=',', fmt='%d')
|
Chris@87
|
352 c.seek(0)
|
Chris@87
|
353 assert_equal(c.readlines(), [b'1,2\n', b'3,4\n'])
|
Chris@87
|
354
|
Chris@87
|
355 def test_format(self):
|
Chris@87
|
356 a = np.array([(1, 2), (3, 4)])
|
Chris@87
|
357 c = BytesIO()
|
Chris@87
|
358 # Sequence of formats
|
Chris@87
|
359 np.savetxt(c, a, fmt=['%02d', '%3.1f'])
|
Chris@87
|
360 c.seek(0)
|
Chris@87
|
361 assert_equal(c.readlines(), [b'01 2.0\n', b'03 4.0\n'])
|
Chris@87
|
362
|
Chris@87
|
363 # A single multiformat string
|
Chris@87
|
364 c = BytesIO()
|
Chris@87
|
365 np.savetxt(c, a, fmt='%02d : %3.1f')
|
Chris@87
|
366 c.seek(0)
|
Chris@87
|
367 lines = c.readlines()
|
Chris@87
|
368 assert_equal(lines, [b'01 : 2.0\n', b'03 : 4.0\n'])
|
Chris@87
|
369
|
Chris@87
|
370 # Specify delimiter, should be overiden
|
Chris@87
|
371 c = BytesIO()
|
Chris@87
|
372 np.savetxt(c, a, fmt='%02d : %3.1f', delimiter=',')
|
Chris@87
|
373 c.seek(0)
|
Chris@87
|
374 lines = c.readlines()
|
Chris@87
|
375 assert_equal(lines, [b'01 : 2.0\n', b'03 : 4.0\n'])
|
Chris@87
|
376
|
Chris@87
|
377 # Bad fmt, should raise a ValueError
|
Chris@87
|
378 c = BytesIO()
|
Chris@87
|
379 assert_raises(ValueError, np.savetxt, c, a, fmt=99)
|
Chris@87
|
380
|
Chris@87
|
381 def test_header_footer(self):
|
Chris@87
|
382 """
|
Chris@87
|
383 Test the functionality of the header and footer keyword argument.
|
Chris@87
|
384 """
|
Chris@87
|
385 c = BytesIO()
|
Chris@87
|
386 a = np.array([(1, 2), (3, 4)], dtype=np.int)
|
Chris@87
|
387 test_header_footer = 'Test header / footer'
|
Chris@87
|
388 # Test the header keyword argument
|
Chris@87
|
389 np.savetxt(c, a, fmt='%1d', header=test_header_footer)
|
Chris@87
|
390 c.seek(0)
|
Chris@87
|
391 assert_equal(c.read(),
|
Chris@87
|
392 asbytes('# ' + test_header_footer + '\n1 2\n3 4\n'))
|
Chris@87
|
393 # Test the footer keyword argument
|
Chris@87
|
394 c = BytesIO()
|
Chris@87
|
395 np.savetxt(c, a, fmt='%1d', footer=test_header_footer)
|
Chris@87
|
396 c.seek(0)
|
Chris@87
|
397 assert_equal(c.read(),
|
Chris@87
|
398 asbytes('1 2\n3 4\n# ' + test_header_footer + '\n'))
|
Chris@87
|
399 # Test the commentstr keyword argument used on the header
|
Chris@87
|
400 c = BytesIO()
|
Chris@87
|
401 commentstr = '% '
|
Chris@87
|
402 np.savetxt(c, a, fmt='%1d',
|
Chris@87
|
403 header=test_header_footer, comments=commentstr)
|
Chris@87
|
404 c.seek(0)
|
Chris@87
|
405 assert_equal(c.read(),
|
Chris@87
|
406 asbytes(commentstr + test_header_footer + '\n' + '1 2\n3 4\n'))
|
Chris@87
|
407 # Test the commentstr keyword argument used on the footer
|
Chris@87
|
408 c = BytesIO()
|
Chris@87
|
409 commentstr = '% '
|
Chris@87
|
410 np.savetxt(c, a, fmt='%1d',
|
Chris@87
|
411 footer=test_header_footer, comments=commentstr)
|
Chris@87
|
412 c.seek(0)
|
Chris@87
|
413 assert_equal(c.read(),
|
Chris@87
|
414 asbytes('1 2\n3 4\n' + commentstr + test_header_footer + '\n'))
|
Chris@87
|
415
|
Chris@87
|
416 def test_file_roundtrip(self):
|
Chris@87
|
417 f, name = mkstemp()
|
Chris@87
|
418 os.close(f)
|
Chris@87
|
419 try:
|
Chris@87
|
420 a = np.array([(1, 2), (3, 4)])
|
Chris@87
|
421 np.savetxt(name, a)
|
Chris@87
|
422 b = np.loadtxt(name)
|
Chris@87
|
423 assert_array_equal(a, b)
|
Chris@87
|
424 finally:
|
Chris@87
|
425 os.unlink(name)
|
Chris@87
|
426
|
Chris@87
|
427 def test_complex_arrays(self):
|
Chris@87
|
428 ncols = 2
|
Chris@87
|
429 nrows = 2
|
Chris@87
|
430 a = np.zeros((ncols, nrows), dtype=np.complex128)
|
Chris@87
|
431 re = np.pi
|
Chris@87
|
432 im = np.e
|
Chris@87
|
433 a[:] = re + 1.0j * im
|
Chris@87
|
434
|
Chris@87
|
435 # One format only
|
Chris@87
|
436 c = BytesIO()
|
Chris@87
|
437 np.savetxt(c, a, fmt=' %+.3e')
|
Chris@87
|
438 c.seek(0)
|
Chris@87
|
439 lines = c.readlines()
|
Chris@87
|
440 assert_equal(
|
Chris@87
|
441 lines,
|
Chris@87
|
442 [b' ( +3.142e+00+ +2.718e+00j) ( +3.142e+00+ +2.718e+00j)\n',
|
Chris@87
|
443 b' ( +3.142e+00+ +2.718e+00j) ( +3.142e+00+ +2.718e+00j)\n'])
|
Chris@87
|
444
|
Chris@87
|
445 # One format for each real and imaginary part
|
Chris@87
|
446 c = BytesIO()
|
Chris@87
|
447 np.savetxt(c, a, fmt=' %+.3e' * 2 * ncols)
|
Chris@87
|
448 c.seek(0)
|
Chris@87
|
449 lines = c.readlines()
|
Chris@87
|
450 assert_equal(
|
Chris@87
|
451 lines,
|
Chris@87
|
452 [b' +3.142e+00 +2.718e+00 +3.142e+00 +2.718e+00\n',
|
Chris@87
|
453 b' +3.142e+00 +2.718e+00 +3.142e+00 +2.718e+00\n'])
|
Chris@87
|
454
|
Chris@87
|
455 # One format for each complex number
|
Chris@87
|
456 c = BytesIO()
|
Chris@87
|
457 np.savetxt(c, a, fmt=['(%.3e%+.3ej)'] * ncols)
|
Chris@87
|
458 c.seek(0)
|
Chris@87
|
459 lines = c.readlines()
|
Chris@87
|
460 assert_equal(
|
Chris@87
|
461 lines,
|
Chris@87
|
462 [b'(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n',
|
Chris@87
|
463 b'(3.142e+00+2.718e+00j) (3.142e+00+2.718e+00j)\n'])
|
Chris@87
|
464
|
Chris@87
|
465 def test_custom_writer(self):
|
Chris@87
|
466
|
Chris@87
|
467 class CustomWriter(list):
|
Chris@87
|
468 def write(self, text):
|
Chris@87
|
469 self.extend(text.split(b'\n'))
|
Chris@87
|
470
|
Chris@87
|
471 w = CustomWriter()
|
Chris@87
|
472 a = np.array([(1, 2), (3, 4)])
|
Chris@87
|
473 np.savetxt(w, a)
|
Chris@87
|
474 b = np.loadtxt(w)
|
Chris@87
|
475 assert_array_equal(a, b)
|
Chris@87
|
476
|
Chris@87
|
477
|
Chris@87
|
478 class TestLoadTxt(TestCase):
|
Chris@87
|
479 def test_record(self):
|
Chris@87
|
480 c = TextIO()
|
Chris@87
|
481 c.write('1 2\n3 4')
|
Chris@87
|
482 c.seek(0)
|
Chris@87
|
483 x = np.loadtxt(c, dtype=[('x', np.int32), ('y', np.int32)])
|
Chris@87
|
484 a = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
|
Chris@87
|
485 assert_array_equal(x, a)
|
Chris@87
|
486
|
Chris@87
|
487 d = TextIO()
|
Chris@87
|
488 d.write('M 64.0 75.0\nF 25.0 60.0')
|
Chris@87
|
489 d.seek(0)
|
Chris@87
|
490 mydescriptor = {'names': ('gender', 'age', 'weight'),
|
Chris@87
|
491 'formats': ('S1', 'i4', 'f4')}
|
Chris@87
|
492 b = np.array([('M', 64.0, 75.0),
|
Chris@87
|
493 ('F', 25.0, 60.0)], dtype=mydescriptor)
|
Chris@87
|
494 y = np.loadtxt(d, dtype=mydescriptor)
|
Chris@87
|
495 assert_array_equal(y, b)
|
Chris@87
|
496
|
Chris@87
|
497 def test_array(self):
|
Chris@87
|
498 c = TextIO()
|
Chris@87
|
499 c.write('1 2\n3 4')
|
Chris@87
|
500
|
Chris@87
|
501 c.seek(0)
|
Chris@87
|
502 x = np.loadtxt(c, dtype=np.int)
|
Chris@87
|
503 a = np.array([[1, 2], [3, 4]], int)
|
Chris@87
|
504 assert_array_equal(x, a)
|
Chris@87
|
505
|
Chris@87
|
506 c.seek(0)
|
Chris@87
|
507 x = np.loadtxt(c, dtype=float)
|
Chris@87
|
508 a = np.array([[1, 2], [3, 4]], float)
|
Chris@87
|
509 assert_array_equal(x, a)
|
Chris@87
|
510
|
Chris@87
|
511 def test_1D(self):
|
Chris@87
|
512 c = TextIO()
|
Chris@87
|
513 c.write('1\n2\n3\n4\n')
|
Chris@87
|
514 c.seek(0)
|
Chris@87
|
515 x = np.loadtxt(c, dtype=int)
|
Chris@87
|
516 a = np.array([1, 2, 3, 4], int)
|
Chris@87
|
517 assert_array_equal(x, a)
|
Chris@87
|
518
|
Chris@87
|
519 c = TextIO()
|
Chris@87
|
520 c.write('1,2,3,4\n')
|
Chris@87
|
521 c.seek(0)
|
Chris@87
|
522 x = np.loadtxt(c, dtype=int, delimiter=',')
|
Chris@87
|
523 a = np.array([1, 2, 3, 4], int)
|
Chris@87
|
524 assert_array_equal(x, a)
|
Chris@87
|
525
|
Chris@87
|
526 def test_missing(self):
|
Chris@87
|
527 c = TextIO()
|
Chris@87
|
528 c.write('1,2,3,,5\n')
|
Chris@87
|
529 c.seek(0)
|
Chris@87
|
530 x = np.loadtxt(c, dtype=int, delimiter=',',
|
Chris@87
|
531 converters={3: lambda s: int(s or - 999)})
|
Chris@87
|
532 a = np.array([1, 2, 3, -999, 5], int)
|
Chris@87
|
533 assert_array_equal(x, a)
|
Chris@87
|
534
|
Chris@87
|
535 def test_converters_with_usecols(self):
|
Chris@87
|
536 c = TextIO()
|
Chris@87
|
537 c.write('1,2,3,,5\n6,7,8,9,10\n')
|
Chris@87
|
538 c.seek(0)
|
Chris@87
|
539 x = np.loadtxt(c, dtype=int, delimiter=',',
|
Chris@87
|
540 converters={3: lambda s: int(s or - 999)},
|
Chris@87
|
541 usecols=(1, 3,))
|
Chris@87
|
542 a = np.array([[2, -999], [7, 9]], int)
|
Chris@87
|
543 assert_array_equal(x, a)
|
Chris@87
|
544
|
Chris@87
|
545 def test_comments(self):
|
Chris@87
|
546 c = TextIO()
|
Chris@87
|
547 c.write('# comment\n1,2,3,5\n')
|
Chris@87
|
548 c.seek(0)
|
Chris@87
|
549 x = np.loadtxt(c, dtype=int, delimiter=',',
|
Chris@87
|
550 comments='#')
|
Chris@87
|
551 a = np.array([1, 2, 3, 5], int)
|
Chris@87
|
552 assert_array_equal(x, a)
|
Chris@87
|
553
|
Chris@87
|
554 def test_skiprows(self):
|
Chris@87
|
555 c = TextIO()
|
Chris@87
|
556 c.write('comment\n1,2,3,5\n')
|
Chris@87
|
557 c.seek(0)
|
Chris@87
|
558 x = np.loadtxt(c, dtype=int, delimiter=',',
|
Chris@87
|
559 skiprows=1)
|
Chris@87
|
560 a = np.array([1, 2, 3, 5], int)
|
Chris@87
|
561 assert_array_equal(x, a)
|
Chris@87
|
562
|
Chris@87
|
563 c = TextIO()
|
Chris@87
|
564 c.write('# comment\n1,2,3,5\n')
|
Chris@87
|
565 c.seek(0)
|
Chris@87
|
566 x = np.loadtxt(c, dtype=int, delimiter=',',
|
Chris@87
|
567 skiprows=1)
|
Chris@87
|
568 a = np.array([1, 2, 3, 5], int)
|
Chris@87
|
569 assert_array_equal(x, a)
|
Chris@87
|
570
|
Chris@87
|
571 def test_usecols(self):
|
Chris@87
|
572 a = np.array([[1, 2], [3, 4]], float)
|
Chris@87
|
573 c = BytesIO()
|
Chris@87
|
574 np.savetxt(c, a)
|
Chris@87
|
575 c.seek(0)
|
Chris@87
|
576 x = np.loadtxt(c, dtype=float, usecols=(1,))
|
Chris@87
|
577 assert_array_equal(x, a[:, 1])
|
Chris@87
|
578
|
Chris@87
|
579 a = np.array([[1, 2, 3], [3, 4, 5]], float)
|
Chris@87
|
580 c = BytesIO()
|
Chris@87
|
581 np.savetxt(c, a)
|
Chris@87
|
582 c.seek(0)
|
Chris@87
|
583 x = np.loadtxt(c, dtype=float, usecols=(1, 2))
|
Chris@87
|
584 assert_array_equal(x, a[:, 1:])
|
Chris@87
|
585
|
Chris@87
|
586 # Testing with arrays instead of tuples.
|
Chris@87
|
587 c.seek(0)
|
Chris@87
|
588 x = np.loadtxt(c, dtype=float, usecols=np.array([1, 2]))
|
Chris@87
|
589 assert_array_equal(x, a[:, 1:])
|
Chris@87
|
590
|
Chris@87
|
591 # Checking with dtypes defined converters.
|
Chris@87
|
592 data = '''JOE 70.1 25.3
|
Chris@87
|
593 BOB 60.5 27.9
|
Chris@87
|
594 '''
|
Chris@87
|
595 c = TextIO(data)
|
Chris@87
|
596 names = ['stid', 'temp']
|
Chris@87
|
597 dtypes = ['S4', 'f8']
|
Chris@87
|
598 arr = np.loadtxt(c, usecols=(0, 2), dtype=list(zip(names, dtypes)))
|
Chris@87
|
599 assert_equal(arr['stid'], [b"JOE", b"BOB"])
|
Chris@87
|
600 assert_equal(arr['temp'], [25.3, 27.9])
|
Chris@87
|
601
|
Chris@87
|
602 def test_fancy_dtype(self):
|
Chris@87
|
603 c = TextIO()
|
Chris@87
|
604 c.write('1,2,3.0\n4,5,6.0\n')
|
Chris@87
|
605 c.seek(0)
|
Chris@87
|
606 dt = np.dtype([('x', int), ('y', [('t', int), ('s', float)])])
|
Chris@87
|
607 x = np.loadtxt(c, dtype=dt, delimiter=',')
|
Chris@87
|
608 a = np.array([(1, (2, 3.0)), (4, (5, 6.0))], dt)
|
Chris@87
|
609 assert_array_equal(x, a)
|
Chris@87
|
610
|
Chris@87
|
611 def test_shaped_dtype(self):
|
Chris@87
|
612 c = TextIO("aaaa 1.0 8.0 1 2 3 4 5 6")
|
Chris@87
|
613 dt = np.dtype([('name', 'S4'), ('x', float), ('y', float),
|
Chris@87
|
614 ('block', int, (2, 3))])
|
Chris@87
|
615 x = np.loadtxt(c, dtype=dt)
|
Chris@87
|
616 a = np.array([('aaaa', 1.0, 8.0, [[1, 2, 3], [4, 5, 6]])],
|
Chris@87
|
617 dtype=dt)
|
Chris@87
|
618 assert_array_equal(x, a)
|
Chris@87
|
619
|
Chris@87
|
620 def test_3d_shaped_dtype(self):
|
Chris@87
|
621 c = TextIO("aaaa 1.0 8.0 1 2 3 4 5 6 7 8 9 10 11 12")
|
Chris@87
|
622 dt = np.dtype([('name', 'S4'), ('x', float), ('y', float),
|
Chris@87
|
623 ('block', int, (2, 2, 3))])
|
Chris@87
|
624 x = np.loadtxt(c, dtype=dt)
|
Chris@87
|
625 a = np.array([('aaaa', 1.0, 8.0,
|
Chris@87
|
626 [[[1, 2, 3], [4, 5, 6]], [[7, 8, 9], [10, 11, 12]]])],
|
Chris@87
|
627 dtype=dt)
|
Chris@87
|
628 assert_array_equal(x, a)
|
Chris@87
|
629
|
Chris@87
|
630 def test_empty_file(self):
|
Chris@87
|
631 with warnings.catch_warnings():
|
Chris@87
|
632 warnings.filterwarnings("ignore",
|
Chris@87
|
633 message="loadtxt: Empty input file:")
|
Chris@87
|
634 c = TextIO()
|
Chris@87
|
635 x = np.loadtxt(c)
|
Chris@87
|
636 assert_equal(x.shape, (0,))
|
Chris@87
|
637 x = np.loadtxt(c, dtype=np.int64)
|
Chris@87
|
638 assert_equal(x.shape, (0,))
|
Chris@87
|
639 assert_(x.dtype == np.int64)
|
Chris@87
|
640
|
Chris@87
|
641 def test_unused_converter(self):
|
Chris@87
|
642 c = TextIO()
|
Chris@87
|
643 c.writelines(['1 21\n', '3 42\n'])
|
Chris@87
|
644 c.seek(0)
|
Chris@87
|
645 data = np.loadtxt(c, usecols=(1,),
|
Chris@87
|
646 converters={0: lambda s: int(s, 16)})
|
Chris@87
|
647 assert_array_equal(data, [21, 42])
|
Chris@87
|
648
|
Chris@87
|
649 c.seek(0)
|
Chris@87
|
650 data = np.loadtxt(c, usecols=(1,),
|
Chris@87
|
651 converters={1: lambda s: int(s, 16)})
|
Chris@87
|
652 assert_array_equal(data, [33, 66])
|
Chris@87
|
653
|
Chris@87
|
654 def test_dtype_with_object(self):
|
Chris@87
|
655 "Test using an explicit dtype with an object"
|
Chris@87
|
656 from datetime import date
|
Chris@87
|
657 import time
|
Chris@87
|
658 data = """ 1; 2001-01-01
|
Chris@87
|
659 2; 2002-01-31 """
|
Chris@87
|
660 ndtype = [('idx', int), ('code', np.object)]
|
Chris@87
|
661 func = lambda s: strptime(s.strip(), "%Y-%m-%d")
|
Chris@87
|
662 converters = {1: func}
|
Chris@87
|
663 test = np.loadtxt(TextIO(data), delimiter=";", dtype=ndtype,
|
Chris@87
|
664 converters=converters)
|
Chris@87
|
665 control = np.array(
|
Chris@87
|
666 [(1, datetime(2001, 1, 1)), (2, datetime(2002, 1, 31))],
|
Chris@87
|
667 dtype=ndtype)
|
Chris@87
|
668 assert_equal(test, control)
|
Chris@87
|
669
|
Chris@87
|
670 def test_uint64_type(self):
|
Chris@87
|
671 tgt = (9223372043271415339, 9223372043271415853)
|
Chris@87
|
672 c = TextIO()
|
Chris@87
|
673 c.write("%s %s" % tgt)
|
Chris@87
|
674 c.seek(0)
|
Chris@87
|
675 res = np.loadtxt(c, dtype=np.uint64)
|
Chris@87
|
676 assert_equal(res, tgt)
|
Chris@87
|
677
|
Chris@87
|
678 def test_int64_type(self):
|
Chris@87
|
679 tgt = (-9223372036854775807, 9223372036854775807)
|
Chris@87
|
680 c = TextIO()
|
Chris@87
|
681 c.write("%s %s" % tgt)
|
Chris@87
|
682 c.seek(0)
|
Chris@87
|
683 res = np.loadtxt(c, dtype=np.int64)
|
Chris@87
|
684 assert_equal(res, tgt)
|
Chris@87
|
685
|
Chris@87
|
686 def test_universal_newline(self):
|
Chris@87
|
687 f, name = mkstemp()
|
Chris@87
|
688 os.write(f, b'1 21\r3 42\r')
|
Chris@87
|
689 os.close(f)
|
Chris@87
|
690
|
Chris@87
|
691 try:
|
Chris@87
|
692 data = np.loadtxt(name)
|
Chris@87
|
693 assert_array_equal(data, [[1, 21], [3, 42]])
|
Chris@87
|
694 finally:
|
Chris@87
|
695 os.unlink(name)
|
Chris@87
|
696
|
Chris@87
|
697 def test_empty_field_after_tab(self):
|
Chris@87
|
698 c = TextIO()
|
Chris@87
|
699 c.write('1 \t2 \t3\tstart \n4\t5\t6\t \n7\t8\t9.5\t')
|
Chris@87
|
700 c.seek(0)
|
Chris@87
|
701 dt = {'names': ('x', 'y', 'z', 'comment'),
|
Chris@87
|
702 'formats': ('<i4', '<i4', '<f4', '|S8')}
|
Chris@87
|
703 x = np.loadtxt(c, dtype=dt, delimiter='\t')
|
Chris@87
|
704 a = np.array([b'start ', b' ', b''])
|
Chris@87
|
705 assert_array_equal(x['comment'], a)
|
Chris@87
|
706
|
Chris@87
|
707 def test_structure_unpack(self):
|
Chris@87
|
708 txt = TextIO("M 21 72\nF 35 58")
|
Chris@87
|
709 dt = {'names': ('a', 'b', 'c'), 'formats': ('|S1', '<i4', '<f4')}
|
Chris@87
|
710 a, b, c = np.loadtxt(txt, dtype=dt, unpack=True)
|
Chris@87
|
711 assert_(a.dtype.str == '|S1')
|
Chris@87
|
712 assert_(b.dtype.str == '<i4')
|
Chris@87
|
713 assert_(c.dtype.str == '<f4')
|
Chris@87
|
714 assert_array_equal(a, np.array([b'M', b'F']))
|
Chris@87
|
715 assert_array_equal(b, np.array([21, 35]))
|
Chris@87
|
716 assert_array_equal(c, np.array([72., 58.]))
|
Chris@87
|
717
|
Chris@87
|
718 def test_ndmin_keyword(self):
|
Chris@87
|
719 c = TextIO()
|
Chris@87
|
720 c.write('1,2,3\n4,5,6')
|
Chris@87
|
721 c.seek(0)
|
Chris@87
|
722 assert_raises(ValueError, np.loadtxt, c, ndmin=3)
|
Chris@87
|
723 c.seek(0)
|
Chris@87
|
724 assert_raises(ValueError, np.loadtxt, c, ndmin=1.5)
|
Chris@87
|
725 c.seek(0)
|
Chris@87
|
726 x = np.loadtxt(c, dtype=int, delimiter=',', ndmin=1)
|
Chris@87
|
727 a = np.array([[1, 2, 3], [4, 5, 6]])
|
Chris@87
|
728 assert_array_equal(x, a)
|
Chris@87
|
729
|
Chris@87
|
730 d = TextIO()
|
Chris@87
|
731 d.write('0,1,2')
|
Chris@87
|
732 d.seek(0)
|
Chris@87
|
733 x = np.loadtxt(d, dtype=int, delimiter=',', ndmin=2)
|
Chris@87
|
734 assert_(x.shape == (1, 3))
|
Chris@87
|
735 d.seek(0)
|
Chris@87
|
736 x = np.loadtxt(d, dtype=int, delimiter=',', ndmin=1)
|
Chris@87
|
737 assert_(x.shape == (3,))
|
Chris@87
|
738 d.seek(0)
|
Chris@87
|
739 x = np.loadtxt(d, dtype=int, delimiter=',', ndmin=0)
|
Chris@87
|
740 assert_(x.shape == (3,))
|
Chris@87
|
741
|
Chris@87
|
742 e = TextIO()
|
Chris@87
|
743 e.write('0\n1\n2')
|
Chris@87
|
744 e.seek(0)
|
Chris@87
|
745 x = np.loadtxt(e, dtype=int, delimiter=',', ndmin=2)
|
Chris@87
|
746 assert_(x.shape == (3, 1))
|
Chris@87
|
747 e.seek(0)
|
Chris@87
|
748 x = np.loadtxt(e, dtype=int, delimiter=',', ndmin=1)
|
Chris@87
|
749 assert_(x.shape == (3,))
|
Chris@87
|
750 e.seek(0)
|
Chris@87
|
751 x = np.loadtxt(e, dtype=int, delimiter=',', ndmin=0)
|
Chris@87
|
752 assert_(x.shape == (3,))
|
Chris@87
|
753
|
Chris@87
|
754 # Test ndmin kw with empty file.
|
Chris@87
|
755 with warnings.catch_warnings():
|
Chris@87
|
756 warnings.filterwarnings("ignore",
|
Chris@87
|
757 message="loadtxt: Empty input file:")
|
Chris@87
|
758 f = TextIO()
|
Chris@87
|
759 assert_(np.loadtxt(f, ndmin=2).shape == (0, 1,))
|
Chris@87
|
760 assert_(np.loadtxt(f, ndmin=1).shape == (0,))
|
Chris@87
|
761
|
Chris@87
|
762 def test_generator_source(self):
|
Chris@87
|
763 def count():
|
Chris@87
|
764 for i in range(10):
|
Chris@87
|
765 yield "%d" % i
|
Chris@87
|
766
|
Chris@87
|
767 res = np.loadtxt(count())
|
Chris@87
|
768 assert_array_equal(res, np.arange(10))
|
Chris@87
|
769
|
Chris@87
|
770 def test_bad_line(self):
|
Chris@87
|
771 c = TextIO()
|
Chris@87
|
772 c.write('1 2 3\n4 5 6\n2 3')
|
Chris@87
|
773 c.seek(0)
|
Chris@87
|
774
|
Chris@87
|
775 # Check for exception and that exception contains line number
|
Chris@87
|
776 assert_raises_regex(ValueError, "3", np.loadtxt, c)
|
Chris@87
|
777
|
Chris@87
|
778
|
Chris@87
|
779 class Testfromregex(TestCase):
|
Chris@87
|
780 # np.fromregex expects files opened in binary mode.
|
Chris@87
|
781 def test_record(self):
|
Chris@87
|
782 c = TextIO()
|
Chris@87
|
783 c.write('1.312 foo\n1.534 bar\n4.444 qux')
|
Chris@87
|
784 c.seek(0)
|
Chris@87
|
785
|
Chris@87
|
786 dt = [('num', np.float64), ('val', 'S3')]
|
Chris@87
|
787 x = np.fromregex(c, r"([0-9.]+)\s+(...)", dt)
|
Chris@87
|
788 a = np.array([(1.312, 'foo'), (1.534, 'bar'), (4.444, 'qux')],
|
Chris@87
|
789 dtype=dt)
|
Chris@87
|
790 assert_array_equal(x, a)
|
Chris@87
|
791
|
Chris@87
|
792 def test_record_2(self):
|
Chris@87
|
793 c = TextIO()
|
Chris@87
|
794 c.write('1312 foo\n1534 bar\n4444 qux')
|
Chris@87
|
795 c.seek(0)
|
Chris@87
|
796
|
Chris@87
|
797 dt = [('num', np.int32), ('val', 'S3')]
|
Chris@87
|
798 x = np.fromregex(c, r"(\d+)\s+(...)", dt)
|
Chris@87
|
799 a = np.array([(1312, 'foo'), (1534, 'bar'), (4444, 'qux')],
|
Chris@87
|
800 dtype=dt)
|
Chris@87
|
801 assert_array_equal(x, a)
|
Chris@87
|
802
|
Chris@87
|
803 def test_record_3(self):
|
Chris@87
|
804 c = TextIO()
|
Chris@87
|
805 c.write('1312 foo\n1534 bar\n4444 qux')
|
Chris@87
|
806 c.seek(0)
|
Chris@87
|
807
|
Chris@87
|
808 dt = [('num', np.float64)]
|
Chris@87
|
809 x = np.fromregex(c, r"(\d+)\s+...", dt)
|
Chris@87
|
810 a = np.array([(1312,), (1534,), (4444,)], dtype=dt)
|
Chris@87
|
811 assert_array_equal(x, a)
|
Chris@87
|
812
|
Chris@87
|
813
|
Chris@87
|
814 #####--------------------------------------------------------------------------
|
Chris@87
|
815
|
Chris@87
|
816
|
Chris@87
|
817 class TestFromTxt(TestCase):
|
Chris@87
|
818 #
|
Chris@87
|
819 def test_record(self):
|
Chris@87
|
820 "Test w/ explicit dtype"
|
Chris@87
|
821 data = TextIO('1 2\n3 4')
|
Chris@87
|
822 # data.seek(0)
|
Chris@87
|
823 test = np.ndfromtxt(data, dtype=[('x', np.int32), ('y', np.int32)])
|
Chris@87
|
824 control = np.array([(1, 2), (3, 4)], dtype=[('x', 'i4'), ('y', 'i4')])
|
Chris@87
|
825 assert_equal(test, control)
|
Chris@87
|
826 #
|
Chris@87
|
827 data = TextIO('M 64.0 75.0\nF 25.0 60.0')
|
Chris@87
|
828 # data.seek(0)
|
Chris@87
|
829 descriptor = {'names': ('gender', 'age', 'weight'),
|
Chris@87
|
830 'formats': ('S1', 'i4', 'f4')}
|
Chris@87
|
831 control = np.array([('M', 64.0, 75.0), ('F', 25.0, 60.0)],
|
Chris@87
|
832 dtype=descriptor)
|
Chris@87
|
833 test = np.ndfromtxt(data, dtype=descriptor)
|
Chris@87
|
834 assert_equal(test, control)
|
Chris@87
|
835
|
Chris@87
|
836 def test_array(self):
|
Chris@87
|
837 "Test outputing a standard ndarray"
|
Chris@87
|
838 data = TextIO('1 2\n3 4')
|
Chris@87
|
839 control = np.array([[1, 2], [3, 4]], dtype=int)
|
Chris@87
|
840 test = np.ndfromtxt(data, dtype=int)
|
Chris@87
|
841 assert_array_equal(test, control)
|
Chris@87
|
842 #
|
Chris@87
|
843 data.seek(0)
|
Chris@87
|
844 control = np.array([[1, 2], [3, 4]], dtype=float)
|
Chris@87
|
845 test = np.loadtxt(data, dtype=float)
|
Chris@87
|
846 assert_array_equal(test, control)
|
Chris@87
|
847
|
Chris@87
|
848 def test_1D(self):
|
Chris@87
|
849 "Test squeezing to 1D"
|
Chris@87
|
850 control = np.array([1, 2, 3, 4], int)
|
Chris@87
|
851 #
|
Chris@87
|
852 data = TextIO('1\n2\n3\n4\n')
|
Chris@87
|
853 test = np.ndfromtxt(data, dtype=int)
|
Chris@87
|
854 assert_array_equal(test, control)
|
Chris@87
|
855 #
|
Chris@87
|
856 data = TextIO('1,2,3,4\n')
|
Chris@87
|
857 test = np.ndfromtxt(data, dtype=int, delimiter=',')
|
Chris@87
|
858 assert_array_equal(test, control)
|
Chris@87
|
859
|
Chris@87
|
860 def test_comments(self):
|
Chris@87
|
861 "Test the stripping of comments"
|
Chris@87
|
862 control = np.array([1, 2, 3, 5], int)
|
Chris@87
|
863 # Comment on its own line
|
Chris@87
|
864 data = TextIO('# comment\n1,2,3,5\n')
|
Chris@87
|
865 test = np.ndfromtxt(data, dtype=int, delimiter=',', comments='#')
|
Chris@87
|
866 assert_equal(test, control)
|
Chris@87
|
867 # Comment at the end of a line
|
Chris@87
|
868 data = TextIO('1,2,3,5# comment\n')
|
Chris@87
|
869 test = np.ndfromtxt(data, dtype=int, delimiter=',', comments='#')
|
Chris@87
|
870 assert_equal(test, control)
|
Chris@87
|
871
|
Chris@87
|
872 def test_skiprows(self):
|
Chris@87
|
873 "Test row skipping"
|
Chris@87
|
874 control = np.array([1, 2, 3, 5], int)
|
Chris@87
|
875 kwargs = dict(dtype=int, delimiter=',')
|
Chris@87
|
876 #
|
Chris@87
|
877 data = TextIO('comment\n1,2,3,5\n')
|
Chris@87
|
878 test = np.ndfromtxt(data, skip_header=1, **kwargs)
|
Chris@87
|
879 assert_equal(test, control)
|
Chris@87
|
880 #
|
Chris@87
|
881 data = TextIO('# comment\n1,2,3,5\n')
|
Chris@87
|
882 test = np.loadtxt(data, skiprows=1, **kwargs)
|
Chris@87
|
883 assert_equal(test, control)
|
Chris@87
|
884
|
Chris@87
|
885 def test_skip_footer(self):
|
Chris@87
|
886 data = ["# %i" % i for i in range(1, 6)]
|
Chris@87
|
887 data.append("A, B, C")
|
Chris@87
|
888 data.extend(["%i,%3.1f,%03s" % (i, i, i) for i in range(51)])
|
Chris@87
|
889 data[-1] = "99,99"
|
Chris@87
|
890 kwargs = dict(delimiter=",", names=True, skip_header=5, skip_footer=10)
|
Chris@87
|
891 test = np.genfromtxt(TextIO("\n".join(data)), **kwargs)
|
Chris@87
|
892 ctrl = np.array([("%f" % i, "%f" % i, "%f" % i) for i in range(41)],
|
Chris@87
|
893 dtype=[(_, float) for _ in "ABC"])
|
Chris@87
|
894 assert_equal(test, ctrl)
|
Chris@87
|
895
|
Chris@87
|
896 def test_skip_footer_with_invalid(self):
|
Chris@87
|
897 with warnings.catch_warnings():
|
Chris@87
|
898 warnings.filterwarnings("ignore")
|
Chris@87
|
899 basestr = '1 1\n2 2\n3 3\n4 4\n5 \n6 \n7 \n'
|
Chris@87
|
900 # Footer too small to get rid of all invalid values
|
Chris@87
|
901 assert_raises(ValueError, np.genfromtxt,
|
Chris@87
|
902 TextIO(basestr), skip_footer=1)
|
Chris@87
|
903 # except ValueError:
|
Chris@87
|
904 # pass
|
Chris@87
|
905 a = np.genfromtxt(
|
Chris@87
|
906 TextIO(basestr), skip_footer=1, invalid_raise=False)
|
Chris@87
|
907 assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]]))
|
Chris@87
|
908 #
|
Chris@87
|
909 a = np.genfromtxt(TextIO(basestr), skip_footer=3)
|
Chris@87
|
910 assert_equal(a, np.array([[1., 1.], [2., 2.], [3., 3.], [4., 4.]]))
|
Chris@87
|
911 #
|
Chris@87
|
912 basestr = '1 1\n2 \n3 3\n4 4\n5 \n6 6\n7 7\n'
|
Chris@87
|
913 a = np.genfromtxt(
|
Chris@87
|
914 TextIO(basestr), skip_footer=1, invalid_raise=False)
|
Chris@87
|
915 assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.], [6., 6.]]))
|
Chris@87
|
916 a = np.genfromtxt(
|
Chris@87
|
917 TextIO(basestr), skip_footer=3, invalid_raise=False)
|
Chris@87
|
918 assert_equal(a, np.array([[1., 1.], [3., 3.], [4., 4.]]))
|
Chris@87
|
919
|
Chris@87
|
920 def test_header(self):
|
Chris@87
|
921 "Test retrieving a header"
|
Chris@87
|
922 data = TextIO('gender age weight\nM 64.0 75.0\nF 25.0 60.0')
|
Chris@87
|
923 test = np.ndfromtxt(data, dtype=None, names=True)
|
Chris@87
|
924 control = {'gender': np.array([b'M', b'F']),
|
Chris@87
|
925 'age': np.array([64.0, 25.0]),
|
Chris@87
|
926 'weight': np.array([75.0, 60.0])}
|
Chris@87
|
927 assert_equal(test['gender'], control['gender'])
|
Chris@87
|
928 assert_equal(test['age'], control['age'])
|
Chris@87
|
929 assert_equal(test['weight'], control['weight'])
|
Chris@87
|
930
|
Chris@87
|
931 def test_auto_dtype(self):
|
Chris@87
|
932 "Test the automatic definition of the output dtype"
|
Chris@87
|
933 data = TextIO('A 64 75.0 3+4j True\nBCD 25 60.0 5+6j False')
|
Chris@87
|
934 test = np.ndfromtxt(data, dtype=None)
|
Chris@87
|
935 control = [np.array([b'A', b'BCD']),
|
Chris@87
|
936 np.array([64, 25]),
|
Chris@87
|
937 np.array([75.0, 60.0]),
|
Chris@87
|
938 np.array([3 + 4j, 5 + 6j]),
|
Chris@87
|
939 np.array([True, False]), ]
|
Chris@87
|
940 assert_equal(test.dtype.names, ['f0', 'f1', 'f2', 'f3', 'f4'])
|
Chris@87
|
941 for (i, ctrl) in enumerate(control):
|
Chris@87
|
942 assert_equal(test['f%i' % i], ctrl)
|
Chris@87
|
943
|
Chris@87
|
944 def test_auto_dtype_uniform(self):
|
Chris@87
|
945 "Tests whether the output dtype can be uniformized"
|
Chris@87
|
946 data = TextIO('1 2 3 4\n5 6 7 8\n')
|
Chris@87
|
947 test = np.ndfromtxt(data, dtype=None)
|
Chris@87
|
948 control = np.array([[1, 2, 3, 4], [5, 6, 7, 8]])
|
Chris@87
|
949 assert_equal(test, control)
|
Chris@87
|
950
|
Chris@87
|
951 def test_fancy_dtype(self):
|
Chris@87
|
952 "Check that a nested dtype isn't MIA"
|
Chris@87
|
953 data = TextIO('1,2,3.0\n4,5,6.0\n')
|
Chris@87
|
954 fancydtype = np.dtype([('x', int), ('y', [('t', int), ('s', float)])])
|
Chris@87
|
955 test = np.ndfromtxt(data, dtype=fancydtype, delimiter=',')
|
Chris@87
|
956 control = np.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=fancydtype)
|
Chris@87
|
957 assert_equal(test, control)
|
Chris@87
|
958
|
Chris@87
|
959 def test_names_overwrite(self):
|
Chris@87
|
960 "Test overwriting the names of the dtype"
|
Chris@87
|
961 descriptor = {'names': ('g', 'a', 'w'),
|
Chris@87
|
962 'formats': ('S1', 'i4', 'f4')}
|
Chris@87
|
963 data = TextIO(b'M 64.0 75.0\nF 25.0 60.0')
|
Chris@87
|
964 names = ('gender', 'age', 'weight')
|
Chris@87
|
965 test = np.ndfromtxt(data, dtype=descriptor, names=names)
|
Chris@87
|
966 descriptor['names'] = names
|
Chris@87
|
967 control = np.array([('M', 64.0, 75.0),
|
Chris@87
|
968 ('F', 25.0, 60.0)], dtype=descriptor)
|
Chris@87
|
969 assert_equal(test, control)
|
Chris@87
|
970
|
Chris@87
|
971 def test_commented_header(self):
|
Chris@87
|
972 "Check that names can be retrieved even if the line is commented out."
|
Chris@87
|
973 data = TextIO("""
|
Chris@87
|
974 #gender age weight
|
Chris@87
|
975 M 21 72.100000
|
Chris@87
|
976 F 35 58.330000
|
Chris@87
|
977 M 33 21.99
|
Chris@87
|
978 """)
|
Chris@87
|
979 # The # is part of the first name and should be deleted automatically.
|
Chris@87
|
980 test = np.genfromtxt(data, names=True, dtype=None)
|
Chris@87
|
981 ctrl = np.array([('M', 21, 72.1), ('F', 35, 58.33), ('M', 33, 21.99)],
|
Chris@87
|
982 dtype=[('gender', '|S1'), ('age', int), ('weight', float)])
|
Chris@87
|
983 assert_equal(test, ctrl)
|
Chris@87
|
984 # Ditto, but we should get rid of the first element
|
Chris@87
|
985 data = TextIO(b"""
|
Chris@87
|
986 # gender age weight
|
Chris@87
|
987 M 21 72.100000
|
Chris@87
|
988 F 35 58.330000
|
Chris@87
|
989 M 33 21.99
|
Chris@87
|
990 """)
|
Chris@87
|
991 test = np.genfromtxt(data, names=True, dtype=None)
|
Chris@87
|
992 assert_equal(test, ctrl)
|
Chris@87
|
993
|
Chris@87
|
994 def test_autonames_and_usecols(self):
|
Chris@87
|
995 "Tests names and usecols"
|
Chris@87
|
996 data = TextIO('A B C D\n aaaa 121 45 9.1')
|
Chris@87
|
997 test = np.ndfromtxt(data, usecols=('A', 'C', 'D'),
|
Chris@87
|
998 names=True, dtype=None)
|
Chris@87
|
999 control = np.array(('aaaa', 45, 9.1),
|
Chris@87
|
1000 dtype=[('A', '|S4'), ('C', int), ('D', float)])
|
Chris@87
|
1001 assert_equal(test, control)
|
Chris@87
|
1002
|
Chris@87
|
1003 def test_converters_with_usecols(self):
|
Chris@87
|
1004 "Test the combination user-defined converters and usecol"
|
Chris@87
|
1005 data = TextIO('1,2,3,,5\n6,7,8,9,10\n')
|
Chris@87
|
1006 test = np.ndfromtxt(data, dtype=int, delimiter=',',
|
Chris@87
|
1007 converters={3: lambda s: int(s or - 999)},
|
Chris@87
|
1008 usecols=(1, 3,))
|
Chris@87
|
1009 control = np.array([[2, -999], [7, 9]], int)
|
Chris@87
|
1010 assert_equal(test, control)
|
Chris@87
|
1011
|
Chris@87
|
1012 def test_converters_with_usecols_and_names(self):
|
Chris@87
|
1013 "Tests names and usecols"
|
Chris@87
|
1014 data = TextIO('A B C D\n aaaa 121 45 9.1')
|
Chris@87
|
1015 test = np.ndfromtxt(data, usecols=('A', 'C', 'D'), names=True,
|
Chris@87
|
1016 dtype=None, converters={'C': lambda s: 2 * int(s)})
|
Chris@87
|
1017 control = np.array(('aaaa', 90, 9.1),
|
Chris@87
|
1018 dtype=[('A', '|S4'), ('C', int), ('D', float)])
|
Chris@87
|
1019 assert_equal(test, control)
|
Chris@87
|
1020
|
Chris@87
|
1021 def test_converters_cornercases(self):
|
Chris@87
|
1022 "Test the conversion to datetime."
|
Chris@87
|
1023 converter = {
|
Chris@87
|
1024 'date': lambda s: strptime(s, '%Y-%m-%d %H:%M:%SZ')}
|
Chris@87
|
1025 data = TextIO('2009-02-03 12:00:00Z, 72214.0')
|
Chris@87
|
1026 test = np.ndfromtxt(data, delimiter=',', dtype=None,
|
Chris@87
|
1027 names=['date', 'stid'], converters=converter)
|
Chris@87
|
1028 control = np.array((datetime(2009, 2, 3), 72214.),
|
Chris@87
|
1029 dtype=[('date', np.object_), ('stid', float)])
|
Chris@87
|
1030 assert_equal(test, control)
|
Chris@87
|
1031
|
Chris@87
|
1032 def test_converters_cornercases2(self):
|
Chris@87
|
1033 "Test the conversion to datetime64."
|
Chris@87
|
1034 converter = {
|
Chris@87
|
1035 'date': lambda s: np.datetime64(strptime(s, '%Y-%m-%d %H:%M:%SZ'))}
|
Chris@87
|
1036 data = TextIO('2009-02-03 12:00:00Z, 72214.0')
|
Chris@87
|
1037 test = np.ndfromtxt(data, delimiter=',', dtype=None,
|
Chris@87
|
1038 names=['date', 'stid'], converters=converter)
|
Chris@87
|
1039 control = np.array((datetime(2009, 2, 3), 72214.),
|
Chris@87
|
1040 dtype=[('date', 'datetime64[us]'), ('stid', float)])
|
Chris@87
|
1041 assert_equal(test, control)
|
Chris@87
|
1042
|
Chris@87
|
1043 def test_unused_converter(self):
|
Chris@87
|
1044 "Test whether unused converters are forgotten"
|
Chris@87
|
1045 data = TextIO("1 21\n 3 42\n")
|
Chris@87
|
1046 test = np.ndfromtxt(data, usecols=(1,),
|
Chris@87
|
1047 converters={0: lambda s: int(s, 16)})
|
Chris@87
|
1048 assert_equal(test, [21, 42])
|
Chris@87
|
1049 #
|
Chris@87
|
1050 data.seek(0)
|
Chris@87
|
1051 test = np.ndfromtxt(data, usecols=(1,),
|
Chris@87
|
1052 converters={1: lambda s: int(s, 16)})
|
Chris@87
|
1053 assert_equal(test, [33, 66])
|
Chris@87
|
1054
|
Chris@87
|
1055 def test_invalid_converter(self):
|
Chris@87
|
1056 strip_rand = lambda x: float((b'r' in x.lower() and x.split()[-1]) or
|
Chris@87
|
1057 (b'r' not in x.lower() and x.strip() or 0.0))
|
Chris@87
|
1058 strip_per = lambda x: float((b'%' in x.lower() and x.split()[0]) or
|
Chris@87
|
1059 (b'%' not in x.lower() and x.strip() or 0.0))
|
Chris@87
|
1060 s = TextIO("D01N01,10/1/2003 ,1 %,R 75,400,600\r\n"
|
Chris@87
|
1061 "L24U05,12/5/2003, 2 %,1,300, 150.5\r\n"
|
Chris@87
|
1062 "D02N03,10/10/2004,R 1,,7,145.55")
|
Chris@87
|
1063 kwargs = dict(
|
Chris@87
|
1064 converters={2: strip_per, 3: strip_rand}, delimiter=",",
|
Chris@87
|
1065 dtype=None)
|
Chris@87
|
1066 assert_raises(ConverterError, np.genfromtxt, s, **kwargs)
|
Chris@87
|
1067
|
Chris@87
|
1068 def test_tricky_converter_bug1666(self):
|
Chris@87
|
1069 "Test some corner case"
|
Chris@87
|
1070 s = TextIO('q1,2\nq3,4')
|
Chris@87
|
1071 cnv = lambda s: float(s[1:])
|
Chris@87
|
1072 test = np.genfromtxt(s, delimiter=',', converters={0: cnv})
|
Chris@87
|
1073 control = np.array([[1., 2.], [3., 4.]])
|
Chris@87
|
1074 assert_equal(test, control)
|
Chris@87
|
1075
|
Chris@87
|
1076 def test_dtype_with_converters(self):
|
Chris@87
|
1077 dstr = "2009; 23; 46"
|
Chris@87
|
1078 test = np.ndfromtxt(TextIO(dstr,),
|
Chris@87
|
1079 delimiter=";", dtype=float, converters={0: bytes})
|
Chris@87
|
1080 control = np.array([('2009', 23., 46)],
|
Chris@87
|
1081 dtype=[('f0', '|S4'), ('f1', float), ('f2', float)])
|
Chris@87
|
1082 assert_equal(test, control)
|
Chris@87
|
1083 test = np.ndfromtxt(TextIO(dstr,),
|
Chris@87
|
1084 delimiter=";", dtype=float, converters={0: float})
|
Chris@87
|
1085 control = np.array([2009., 23., 46],)
|
Chris@87
|
1086 assert_equal(test, control)
|
Chris@87
|
1087
|
Chris@87
|
1088 def test_dtype_with_converters_and_usecols(self):
|
Chris@87
|
1089 dstr = "1,5,-1,1:1\n2,8,-1,1:n\n3,3,-2,m:n\n"
|
Chris@87
|
1090 dmap = {'1:1':0, '1:n':1, 'm:1':2, 'm:n':3}
|
Chris@87
|
1091 dtyp = [('E1','i4'),('E2','i4'),('E3','i2'),('N', 'i1')]
|
Chris@87
|
1092 conv = {0: int, 1: int, 2: int, 3: lambda r: dmap[r.decode()]}
|
Chris@87
|
1093 test = np.recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',',
|
Chris@87
|
1094 names=None, converters=conv)
|
Chris@87
|
1095 control = np.rec.array([[1,5,-1,0], [2,8,-1,1], [3,3,-2,3]], dtype=dtyp)
|
Chris@87
|
1096 assert_equal(test, control)
|
Chris@87
|
1097 dtyp = [('E1','i4'),('E2','i4'),('N', 'i1')]
|
Chris@87
|
1098 test = np.recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',',
|
Chris@87
|
1099 usecols=(0,1,3), names=None, converters=conv)
|
Chris@87
|
1100 control = np.rec.array([[1,5,0], [2,8,1], [3,3,3]], dtype=dtyp)
|
Chris@87
|
1101 assert_equal(test, control)
|
Chris@87
|
1102
|
Chris@87
|
1103 def test_dtype_with_object(self):
|
Chris@87
|
1104 "Test using an explicit dtype with an object"
|
Chris@87
|
1105 from datetime import date
|
Chris@87
|
1106 import time
|
Chris@87
|
1107 data = """ 1; 2001-01-01
|
Chris@87
|
1108 2; 2002-01-31 """
|
Chris@87
|
1109 ndtype = [('idx', int), ('code', np.object)]
|
Chris@87
|
1110 func = lambda s: strptime(s.strip(), "%Y-%m-%d")
|
Chris@87
|
1111 converters = {1: func}
|
Chris@87
|
1112 test = np.genfromtxt(TextIO(data), delimiter=";", dtype=ndtype,
|
Chris@87
|
1113 converters=converters)
|
Chris@87
|
1114 control = np.array(
|
Chris@87
|
1115 [(1, datetime(2001, 1, 1)), (2, datetime(2002, 1, 31))],
|
Chris@87
|
1116 dtype=ndtype)
|
Chris@87
|
1117 assert_equal(test, control)
|
Chris@87
|
1118 #
|
Chris@87
|
1119 ndtype = [('nest', [('idx', int), ('code', np.object)])]
|
Chris@87
|
1120 try:
|
Chris@87
|
1121 test = np.genfromtxt(TextIO(data), delimiter=";",
|
Chris@87
|
1122 dtype=ndtype, converters=converters)
|
Chris@87
|
1123 except NotImplementedError:
|
Chris@87
|
1124 pass
|
Chris@87
|
1125 else:
|
Chris@87
|
1126 errmsg = "Nested dtype involving objects should be supported."
|
Chris@87
|
1127 raise AssertionError(errmsg)
|
Chris@87
|
1128
|
Chris@87
|
1129 def test_userconverters_with_explicit_dtype(self):
|
Chris@87
|
1130 "Test user_converters w/ explicit (standard) dtype"
|
Chris@87
|
1131 data = TextIO('skip,skip,2001-01-01,1.0,skip')
|
Chris@87
|
1132 test = np.genfromtxt(data, delimiter=",", names=None, dtype=float,
|
Chris@87
|
1133 usecols=(2, 3), converters={2: bytes})
|
Chris@87
|
1134 control = np.array([('2001-01-01', 1.)],
|
Chris@87
|
1135 dtype=[('', '|S10'), ('', float)])
|
Chris@87
|
1136 assert_equal(test, control)
|
Chris@87
|
1137
|
Chris@87
|
1138 def test_spacedelimiter(self):
|
Chris@87
|
1139 "Test space delimiter"
|
Chris@87
|
1140 data = TextIO("1 2 3 4 5\n6 7 8 9 10")
|
Chris@87
|
1141 test = np.ndfromtxt(data)
|
Chris@87
|
1142 control = np.array([[1., 2., 3., 4., 5.],
|
Chris@87
|
1143 [6., 7., 8., 9., 10.]])
|
Chris@87
|
1144 assert_equal(test, control)
|
Chris@87
|
1145
|
Chris@87
|
1146 def test_integer_delimiter(self):
|
Chris@87
|
1147 "Test using an integer for delimiter"
|
Chris@87
|
1148 data = " 1 2 3\n 4 5 67\n890123 4"
|
Chris@87
|
1149 test = np.genfromtxt(TextIO(data), delimiter=3)
|
Chris@87
|
1150 control = np.array([[1, 2, 3], [4, 5, 67], [890, 123, 4]])
|
Chris@87
|
1151 assert_equal(test, control)
|
Chris@87
|
1152
|
Chris@87
|
1153 def test_missing(self):
|
Chris@87
|
1154 data = TextIO('1,2,3,,5\n')
|
Chris@87
|
1155 test = np.ndfromtxt(data, dtype=int, delimiter=',',
|
Chris@87
|
1156 converters={3: lambda s: int(s or - 999)})
|
Chris@87
|
1157 control = np.array([1, 2, 3, -999, 5], int)
|
Chris@87
|
1158 assert_equal(test, control)
|
Chris@87
|
1159
|
Chris@87
|
1160 def test_missing_with_tabs(self):
|
Chris@87
|
1161 "Test w/ a delimiter tab"
|
Chris@87
|
1162 txt = "1\t2\t3\n\t2\t\n1\t\t3"
|
Chris@87
|
1163 test = np.genfromtxt(TextIO(txt), delimiter="\t",
|
Chris@87
|
1164 usemask=True,)
|
Chris@87
|
1165 ctrl_d = np.array([(1, 2, 3), (np.nan, 2, np.nan), (1, np.nan, 3)],)
|
Chris@87
|
1166 ctrl_m = np.array([(0, 0, 0), (1, 0, 1), (0, 1, 0)], dtype=bool)
|
Chris@87
|
1167 assert_equal(test.data, ctrl_d)
|
Chris@87
|
1168 assert_equal(test.mask, ctrl_m)
|
Chris@87
|
1169
|
Chris@87
|
1170 def test_usecols(self):
|
Chris@87
|
1171 "Test the selection of columns"
|
Chris@87
|
1172 # Select 1 column
|
Chris@87
|
1173 control = np.array([[1, 2], [3, 4]], float)
|
Chris@87
|
1174 data = TextIO()
|
Chris@87
|
1175 np.savetxt(data, control)
|
Chris@87
|
1176 data.seek(0)
|
Chris@87
|
1177 test = np.ndfromtxt(data, dtype=float, usecols=(1,))
|
Chris@87
|
1178 assert_equal(test, control[:, 1])
|
Chris@87
|
1179 #
|
Chris@87
|
1180 control = np.array([[1, 2, 3], [3, 4, 5]], float)
|
Chris@87
|
1181 data = TextIO()
|
Chris@87
|
1182 np.savetxt(data, control)
|
Chris@87
|
1183 data.seek(0)
|
Chris@87
|
1184 test = np.ndfromtxt(data, dtype=float, usecols=(1, 2))
|
Chris@87
|
1185 assert_equal(test, control[:, 1:])
|
Chris@87
|
1186 # Testing with arrays instead of tuples.
|
Chris@87
|
1187 data.seek(0)
|
Chris@87
|
1188 test = np.ndfromtxt(data, dtype=float, usecols=np.array([1, 2]))
|
Chris@87
|
1189 assert_equal(test, control[:, 1:])
|
Chris@87
|
1190
|
Chris@87
|
1191 def test_usecols_as_css(self):
|
Chris@87
|
1192 "Test giving usecols with a comma-separated string"
|
Chris@87
|
1193 data = "1 2 3\n4 5 6"
|
Chris@87
|
1194 test = np.genfromtxt(TextIO(data),
|
Chris@87
|
1195 names="a, b, c", usecols="a, c")
|
Chris@87
|
1196 ctrl = np.array([(1, 3), (4, 6)], dtype=[(_, float) for _ in "ac"])
|
Chris@87
|
1197 assert_equal(test, ctrl)
|
Chris@87
|
1198
|
Chris@87
|
1199 def test_usecols_with_structured_dtype(self):
|
Chris@87
|
1200 "Test usecols with an explicit structured dtype"
|
Chris@87
|
1201 data = TextIO("JOE 70.1 25.3\nBOB 60.5 27.9")
|
Chris@87
|
1202 names = ['stid', 'temp']
|
Chris@87
|
1203 dtypes = ['S4', 'f8']
|
Chris@87
|
1204 test = np.ndfromtxt(
|
Chris@87
|
1205 data, usecols=(0, 2), dtype=list(zip(names, dtypes)))
|
Chris@87
|
1206 assert_equal(test['stid'], [b"JOE", b"BOB"])
|
Chris@87
|
1207 assert_equal(test['temp'], [25.3, 27.9])
|
Chris@87
|
1208
|
Chris@87
|
1209 def test_usecols_with_integer(self):
|
Chris@87
|
1210 "Test usecols with an integer"
|
Chris@87
|
1211 test = np.genfromtxt(TextIO(b"1 2 3\n4 5 6"), usecols=0)
|
Chris@87
|
1212 assert_equal(test, np.array([1., 4.]))
|
Chris@87
|
1213
|
Chris@87
|
1214 def test_usecols_with_named_columns(self):
|
Chris@87
|
1215 "Test usecols with named columns"
|
Chris@87
|
1216 ctrl = np.array([(1, 3), (4, 6)], dtype=[('a', float), ('c', float)])
|
Chris@87
|
1217 data = "1 2 3\n4 5 6"
|
Chris@87
|
1218 kwargs = dict(names="a, b, c")
|
Chris@87
|
1219 test = np.genfromtxt(TextIO(data), usecols=(0, -1), **kwargs)
|
Chris@87
|
1220 assert_equal(test, ctrl)
|
Chris@87
|
1221 test = np.genfromtxt(TextIO(data),
|
Chris@87
|
1222 usecols=('a', 'c'), **kwargs)
|
Chris@87
|
1223 assert_equal(test, ctrl)
|
Chris@87
|
1224
|
Chris@87
|
1225 def test_empty_file(self):
|
Chris@87
|
1226 "Test that an empty file raises the proper warning."
|
Chris@87
|
1227 with warnings.catch_warnings():
|
Chris@87
|
1228 warnings.filterwarnings("ignore",
|
Chris@87
|
1229 message="genfromtxt: Empty input file:")
|
Chris@87
|
1230 data = TextIO()
|
Chris@87
|
1231 test = np.genfromtxt(data)
|
Chris@87
|
1232 assert_equal(test, np.array([]))
|
Chris@87
|
1233
|
Chris@87
|
1234 def test_fancy_dtype_alt(self):
|
Chris@87
|
1235 "Check that a nested dtype isn't MIA"
|
Chris@87
|
1236 data = TextIO('1,2,3.0\n4,5,6.0\n')
|
Chris@87
|
1237 fancydtype = np.dtype([('x', int), ('y', [('t', int), ('s', float)])])
|
Chris@87
|
1238 test = np.mafromtxt(data, dtype=fancydtype, delimiter=',')
|
Chris@87
|
1239 control = ma.array([(1, (2, 3.0)), (4, (5, 6.0))], dtype=fancydtype)
|
Chris@87
|
1240 assert_equal(test, control)
|
Chris@87
|
1241
|
Chris@87
|
1242 def test_shaped_dtype(self):
|
Chris@87
|
1243 c = TextIO("aaaa 1.0 8.0 1 2 3 4 5 6")
|
Chris@87
|
1244 dt = np.dtype([('name', 'S4'), ('x', float), ('y', float),
|
Chris@87
|
1245 ('block', int, (2, 3))])
|
Chris@87
|
1246 x = np.ndfromtxt(c, dtype=dt)
|
Chris@87
|
1247 a = np.array([('aaaa', 1.0, 8.0, [[1, 2, 3], [4, 5, 6]])],
|
Chris@87
|
1248 dtype=dt)
|
Chris@87
|
1249 assert_array_equal(x, a)
|
Chris@87
|
1250
|
Chris@87
|
1251 def test_withmissing(self):
|
Chris@87
|
1252 data = TextIO('A,B\n0,1\n2,N/A')
|
Chris@87
|
1253 kwargs = dict(delimiter=",", missing_values="N/A", names=True)
|
Chris@87
|
1254 test = np.mafromtxt(data, dtype=None, **kwargs)
|
Chris@87
|
1255 control = ma.array([(0, 1), (2, -1)],
|
Chris@87
|
1256 mask=[(False, False), (False, True)],
|
Chris@87
|
1257 dtype=[('A', np.int), ('B', np.int)])
|
Chris@87
|
1258 assert_equal(test, control)
|
Chris@87
|
1259 assert_equal(test.mask, control.mask)
|
Chris@87
|
1260 #
|
Chris@87
|
1261 data.seek(0)
|
Chris@87
|
1262 test = np.mafromtxt(data, **kwargs)
|
Chris@87
|
1263 control = ma.array([(0, 1), (2, -1)],
|
Chris@87
|
1264 mask=[(False, False), (False, True)],
|
Chris@87
|
1265 dtype=[('A', np.float), ('B', np.float)])
|
Chris@87
|
1266 assert_equal(test, control)
|
Chris@87
|
1267 assert_equal(test.mask, control.mask)
|
Chris@87
|
1268
|
Chris@87
|
1269 def test_user_missing_values(self):
|
Chris@87
|
1270 data = "A, B, C\n0, 0., 0j\n1, N/A, 1j\n-9, 2.2, N/A\n3, -99, 3j"
|
Chris@87
|
1271 basekwargs = dict(dtype=None, delimiter=",", names=True,)
|
Chris@87
|
1272 mdtype = [('A', int), ('B', float), ('C', complex)]
|
Chris@87
|
1273 #
|
Chris@87
|
1274 test = np.mafromtxt(TextIO(data), missing_values="N/A",
|
Chris@87
|
1275 **basekwargs)
|
Chris@87
|
1276 control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
|
Chris@87
|
1277 (-9, 2.2, -999j), (3, -99, 3j)],
|
Chris@87
|
1278 mask=[(0, 0, 0), (0, 1, 0), (0, 0, 1), (0, 0, 0)],
|
Chris@87
|
1279 dtype=mdtype)
|
Chris@87
|
1280 assert_equal(test, control)
|
Chris@87
|
1281 #
|
Chris@87
|
1282 basekwargs['dtype'] = mdtype
|
Chris@87
|
1283 test = np.mafromtxt(TextIO(data),
|
Chris@87
|
1284 missing_values={0: -9, 1: -99, 2: -999j}, **basekwargs)
|
Chris@87
|
1285 control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
|
Chris@87
|
1286 (-9, 2.2, -999j), (3, -99, 3j)],
|
Chris@87
|
1287 mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
|
Chris@87
|
1288 dtype=mdtype)
|
Chris@87
|
1289 assert_equal(test, control)
|
Chris@87
|
1290 #
|
Chris@87
|
1291 test = np.mafromtxt(TextIO(data),
|
Chris@87
|
1292 missing_values={0: -9, 'B': -99, 'C': -999j},
|
Chris@87
|
1293 **basekwargs)
|
Chris@87
|
1294 control = ma.array([(0, 0.0, 0j), (1, -999, 1j),
|
Chris@87
|
1295 (-9, 2.2, -999j), (3, -99, 3j)],
|
Chris@87
|
1296 mask=[(0, 0, 0), (0, 1, 0), (1, 0, 1), (0, 1, 0)],
|
Chris@87
|
1297 dtype=mdtype)
|
Chris@87
|
1298 assert_equal(test, control)
|
Chris@87
|
1299
|
Chris@87
|
1300 def test_user_filling_values(self):
|
Chris@87
|
1301 "Test with missing and filling values"
|
Chris@87
|
1302 ctrl = np.array([(0, 3), (4, -999)], dtype=[('a', int), ('b', int)])
|
Chris@87
|
1303 data = "N/A, 2, 3\n4, ,???"
|
Chris@87
|
1304 kwargs = dict(delimiter=",",
|
Chris@87
|
1305 dtype=int,
|
Chris@87
|
1306 names="a,b,c",
|
Chris@87
|
1307 missing_values={0: "N/A", 'b': " ", 2: "???"},
|
Chris@87
|
1308 filling_values={0: 0, 'b': 0, 2: -999})
|
Chris@87
|
1309 test = np.genfromtxt(TextIO(data), **kwargs)
|
Chris@87
|
1310 ctrl = np.array([(0, 2, 3), (4, 0, -999)],
|
Chris@87
|
1311 dtype=[(_, int) for _ in "abc"])
|
Chris@87
|
1312 assert_equal(test, ctrl)
|
Chris@87
|
1313 #
|
Chris@87
|
1314 test = np.genfromtxt(TextIO(data), usecols=(0, -1), **kwargs)
|
Chris@87
|
1315 ctrl = np.array([(0, 3), (4, -999)], dtype=[(_, int) for _ in "ac"])
|
Chris@87
|
1316 assert_equal(test, ctrl)
|
Chris@87
|
1317
|
Chris@87
|
1318 data2 = "1,2,*,4\n5,*,7,8\n"
|
Chris@87
|
1319 test = np.genfromtxt(TextIO(data2), delimiter=',', dtype=int,
|
Chris@87
|
1320 missing_values="*", filling_values=0)
|
Chris@87
|
1321 ctrl = np.array([[1, 2, 0, 4], [5, 0, 7, 8]])
|
Chris@87
|
1322 assert_equal(test, ctrl)
|
Chris@87
|
1323 test = np.genfromtxt(TextIO(data2), delimiter=',', dtype=int,
|
Chris@87
|
1324 missing_values="*", filling_values=-1)
|
Chris@87
|
1325 ctrl = np.array([[1, 2, -1, 4], [5, -1, 7, 8]])
|
Chris@87
|
1326 assert_equal(test, ctrl)
|
Chris@87
|
1327
|
Chris@87
|
1328 def test_withmissing_float(self):
|
Chris@87
|
1329 data = TextIO('A,B\n0,1.5\n2,-999.00')
|
Chris@87
|
1330 test = np.mafromtxt(data, dtype=None, delimiter=',',
|
Chris@87
|
1331 missing_values='-999.0', names=True,)
|
Chris@87
|
1332 control = ma.array([(0, 1.5), (2, -1.)],
|
Chris@87
|
1333 mask=[(False, False), (False, True)],
|
Chris@87
|
1334 dtype=[('A', np.int), ('B', np.float)])
|
Chris@87
|
1335 assert_equal(test, control)
|
Chris@87
|
1336 assert_equal(test.mask, control.mask)
|
Chris@87
|
1337
|
Chris@87
|
1338 def test_with_masked_column_uniform(self):
|
Chris@87
|
1339 "Test masked column"
|
Chris@87
|
1340 data = TextIO('1 2 3\n4 5 6\n')
|
Chris@87
|
1341 test = np.genfromtxt(data, dtype=None,
|
Chris@87
|
1342 missing_values='2,5', usemask=True)
|
Chris@87
|
1343 control = ma.array([[1, 2, 3], [4, 5, 6]], mask=[[0, 1, 0], [0, 1, 0]])
|
Chris@87
|
1344 assert_equal(test, control)
|
Chris@87
|
1345
|
Chris@87
|
1346 def test_with_masked_column_various(self):
|
Chris@87
|
1347 "Test masked column"
|
Chris@87
|
1348 data = TextIO('True 2 3\nFalse 5 6\n')
|
Chris@87
|
1349 test = np.genfromtxt(data, dtype=None,
|
Chris@87
|
1350 missing_values='2,5', usemask=True)
|
Chris@87
|
1351 control = ma.array([(1, 2, 3), (0, 5, 6)],
|
Chris@87
|
1352 mask=[(0, 1, 0), (0, 1, 0)],
|
Chris@87
|
1353 dtype=[('f0', bool), ('f1', bool), ('f2', int)])
|
Chris@87
|
1354 assert_equal(test, control)
|
Chris@87
|
1355
|
Chris@87
|
1356 def test_invalid_raise(self):
|
Chris@87
|
1357 "Test invalid raise"
|
Chris@87
|
1358 data = ["1, 1, 1, 1, 1"] * 50
|
Chris@87
|
1359 for i in range(5):
|
Chris@87
|
1360 data[10 * i] = "2, 2, 2, 2 2"
|
Chris@87
|
1361 data.insert(0, "a, b, c, d, e")
|
Chris@87
|
1362 mdata = TextIO("\n".join(data))
|
Chris@87
|
1363 #
|
Chris@87
|
1364 kwargs = dict(delimiter=",", dtype=None, names=True)
|
Chris@87
|
1365 # XXX: is there a better way to get the return value of the callable in
|
Chris@87
|
1366 # assert_warns ?
|
Chris@87
|
1367 ret = {}
|
Chris@87
|
1368
|
Chris@87
|
1369 def f(_ret={}):
|
Chris@87
|
1370 _ret['mtest'] = np.ndfromtxt(mdata, invalid_raise=False, **kwargs)
|
Chris@87
|
1371 assert_warns(ConversionWarning, f, _ret=ret)
|
Chris@87
|
1372 mtest = ret['mtest']
|
Chris@87
|
1373 assert_equal(len(mtest), 45)
|
Chris@87
|
1374 assert_equal(mtest, np.ones(45, dtype=[(_, int) for _ in 'abcde']))
|
Chris@87
|
1375 #
|
Chris@87
|
1376 mdata.seek(0)
|
Chris@87
|
1377 assert_raises(ValueError, np.ndfromtxt, mdata,
|
Chris@87
|
1378 delimiter=",", names=True)
|
Chris@87
|
1379
|
Chris@87
|
1380 def test_invalid_raise_with_usecols(self):
|
Chris@87
|
1381 "Test invalid_raise with usecols"
|
Chris@87
|
1382 data = ["1, 1, 1, 1, 1"] * 50
|
Chris@87
|
1383 for i in range(5):
|
Chris@87
|
1384 data[10 * i] = "2, 2, 2, 2 2"
|
Chris@87
|
1385 data.insert(0, "a, b, c, d, e")
|
Chris@87
|
1386 mdata = TextIO("\n".join(data))
|
Chris@87
|
1387 kwargs = dict(delimiter=",", dtype=None, names=True,
|
Chris@87
|
1388 invalid_raise=False)
|
Chris@87
|
1389 # XXX: is there a better way to get the return value of the callable in
|
Chris@87
|
1390 # assert_warns ?
|
Chris@87
|
1391 ret = {}
|
Chris@87
|
1392
|
Chris@87
|
1393 def f(_ret={}):
|
Chris@87
|
1394 _ret['mtest'] = np.ndfromtxt(mdata, usecols=(0, 4), **kwargs)
|
Chris@87
|
1395 assert_warns(ConversionWarning, f, _ret=ret)
|
Chris@87
|
1396 mtest = ret['mtest']
|
Chris@87
|
1397 assert_equal(len(mtest), 45)
|
Chris@87
|
1398 assert_equal(mtest, np.ones(45, dtype=[(_, int) for _ in 'ae']))
|
Chris@87
|
1399 #
|
Chris@87
|
1400 mdata.seek(0)
|
Chris@87
|
1401 mtest = np.ndfromtxt(mdata, usecols=(0, 1), **kwargs)
|
Chris@87
|
1402 assert_equal(len(mtest), 50)
|
Chris@87
|
1403 control = np.ones(50, dtype=[(_, int) for _ in 'ab'])
|
Chris@87
|
1404 control[[10 * _ for _ in range(5)]] = (2, 2)
|
Chris@87
|
1405 assert_equal(mtest, control)
|
Chris@87
|
1406
|
Chris@87
|
1407 def test_inconsistent_dtype(self):
|
Chris@87
|
1408 "Test inconsistent dtype"
|
Chris@87
|
1409 data = ["1, 1, 1, 1, -1.1"] * 50
|
Chris@87
|
1410 mdata = TextIO("\n".join(data))
|
Chris@87
|
1411
|
Chris@87
|
1412 converters = {4: lambda x: "(%s)" % x}
|
Chris@87
|
1413 kwargs = dict(delimiter=",", converters=converters,
|
Chris@87
|
1414 dtype=[(_, int) for _ in 'abcde'],)
|
Chris@87
|
1415 assert_raises(ValueError, np.genfromtxt, mdata, **kwargs)
|
Chris@87
|
1416
|
Chris@87
|
1417 def test_default_field_format(self):
|
Chris@87
|
1418 "Test default format"
|
Chris@87
|
1419 data = "0, 1, 2.3\n4, 5, 6.7"
|
Chris@87
|
1420 mtest = np.ndfromtxt(TextIO(data),
|
Chris@87
|
1421 delimiter=",", dtype=None, defaultfmt="f%02i")
|
Chris@87
|
1422 ctrl = np.array([(0, 1, 2.3), (4, 5, 6.7)],
|
Chris@87
|
1423 dtype=[("f00", int), ("f01", int), ("f02", float)])
|
Chris@87
|
1424 assert_equal(mtest, ctrl)
|
Chris@87
|
1425
|
Chris@87
|
1426 def test_single_dtype_wo_names(self):
|
Chris@87
|
1427 "Test single dtype w/o names"
|
Chris@87
|
1428 data = "0, 1, 2.3\n4, 5, 6.7"
|
Chris@87
|
1429 mtest = np.ndfromtxt(TextIO(data),
|
Chris@87
|
1430 delimiter=",", dtype=float, defaultfmt="f%02i")
|
Chris@87
|
1431 ctrl = np.array([[0., 1., 2.3], [4., 5., 6.7]], dtype=float)
|
Chris@87
|
1432 assert_equal(mtest, ctrl)
|
Chris@87
|
1433
|
Chris@87
|
1434 def test_single_dtype_w_explicit_names(self):
|
Chris@87
|
1435 "Test single dtype w explicit names"
|
Chris@87
|
1436 data = "0, 1, 2.3\n4, 5, 6.7"
|
Chris@87
|
1437 mtest = np.ndfromtxt(TextIO(data),
|
Chris@87
|
1438 delimiter=",", dtype=float, names="a, b, c")
|
Chris@87
|
1439 ctrl = np.array([(0., 1., 2.3), (4., 5., 6.7)],
|
Chris@87
|
1440 dtype=[(_, float) for _ in "abc"])
|
Chris@87
|
1441 assert_equal(mtest, ctrl)
|
Chris@87
|
1442
|
Chris@87
|
1443 def test_single_dtype_w_implicit_names(self):
|
Chris@87
|
1444 "Test single dtype w implicit names"
|
Chris@87
|
1445 data = "a, b, c\n0, 1, 2.3\n4, 5, 6.7"
|
Chris@87
|
1446 mtest = np.ndfromtxt(TextIO(data),
|
Chris@87
|
1447 delimiter=",", dtype=float, names=True)
|
Chris@87
|
1448 ctrl = np.array([(0., 1., 2.3), (4., 5., 6.7)],
|
Chris@87
|
1449 dtype=[(_, float) for _ in "abc"])
|
Chris@87
|
1450 assert_equal(mtest, ctrl)
|
Chris@87
|
1451
|
Chris@87
|
1452 def test_easy_structured_dtype(self):
|
Chris@87
|
1453 "Test easy structured dtype"
|
Chris@87
|
1454 data = "0, 1, 2.3\n4, 5, 6.7"
|
Chris@87
|
1455 mtest = np.ndfromtxt(TextIO(data), delimiter=",",
|
Chris@87
|
1456 dtype=(int, float, float), defaultfmt="f_%02i")
|
Chris@87
|
1457 ctrl = np.array([(0, 1., 2.3), (4, 5., 6.7)],
|
Chris@87
|
1458 dtype=[("f_00", int), ("f_01", float), ("f_02", float)])
|
Chris@87
|
1459 assert_equal(mtest, ctrl)
|
Chris@87
|
1460
|
Chris@87
|
1461 def test_autostrip(self):
|
Chris@87
|
1462 "Test autostrip"
|
Chris@87
|
1463 data = "01/01/2003 , 1.3, abcde"
|
Chris@87
|
1464 kwargs = dict(delimiter=",", dtype=None)
|
Chris@87
|
1465 mtest = np.ndfromtxt(TextIO(data), **kwargs)
|
Chris@87
|
1466 ctrl = np.array([('01/01/2003 ', 1.3, ' abcde')],
|
Chris@87
|
1467 dtype=[('f0', '|S12'), ('f1', float), ('f2', '|S8')])
|
Chris@87
|
1468 assert_equal(mtest, ctrl)
|
Chris@87
|
1469 mtest = np.ndfromtxt(TextIO(data), autostrip=True, **kwargs)
|
Chris@87
|
1470 ctrl = np.array([('01/01/2003', 1.3, 'abcde')],
|
Chris@87
|
1471 dtype=[('f0', '|S10'), ('f1', float), ('f2', '|S5')])
|
Chris@87
|
1472 assert_equal(mtest, ctrl)
|
Chris@87
|
1473
|
Chris@87
|
1474 def test_replace_space(self):
|
Chris@87
|
1475 "Test the 'replace_space' option"
|
Chris@87
|
1476 txt = "A.A, B (B), C:C\n1, 2, 3.14"
|
Chris@87
|
1477 # Test default: replace ' ' by '_' and delete non-alphanum chars
|
Chris@87
|
1478 test = np.genfromtxt(TextIO(txt),
|
Chris@87
|
1479 delimiter=",", names=True, dtype=None)
|
Chris@87
|
1480 ctrl_dtype = [("AA", int), ("B_B", int), ("CC", float)]
|
Chris@87
|
1481 ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype)
|
Chris@87
|
1482 assert_equal(test, ctrl)
|
Chris@87
|
1483 # Test: no replace, no delete
|
Chris@87
|
1484 test = np.genfromtxt(TextIO(txt),
|
Chris@87
|
1485 delimiter=",", names=True, dtype=None,
|
Chris@87
|
1486 replace_space='', deletechars='')
|
Chris@87
|
1487 ctrl_dtype = [("A.A", int), ("B (B)", int), ("C:C", float)]
|
Chris@87
|
1488 ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype)
|
Chris@87
|
1489 assert_equal(test, ctrl)
|
Chris@87
|
1490 # Test: no delete (spaces are replaced by _)
|
Chris@87
|
1491 test = np.genfromtxt(TextIO(txt),
|
Chris@87
|
1492 delimiter=",", names=True, dtype=None,
|
Chris@87
|
1493 deletechars='')
|
Chris@87
|
1494 ctrl_dtype = [("A.A", int), ("B_(B)", int), ("C:C", float)]
|
Chris@87
|
1495 ctrl = np.array((1, 2, 3.14), dtype=ctrl_dtype)
|
Chris@87
|
1496 assert_equal(test, ctrl)
|
Chris@87
|
1497
|
Chris@87
|
1498 def test_incomplete_names(self):
|
Chris@87
|
1499 "Test w/ incomplete names"
|
Chris@87
|
1500 data = "A,,C\n0,1,2\n3,4,5"
|
Chris@87
|
1501 kwargs = dict(delimiter=",", names=True)
|
Chris@87
|
1502 # w/ dtype=None
|
Chris@87
|
1503 ctrl = np.array([(0, 1, 2), (3, 4, 5)],
|
Chris@87
|
1504 dtype=[(_, int) for _ in ('A', 'f0', 'C')])
|
Chris@87
|
1505 test = np.ndfromtxt(TextIO(data), dtype=None, **kwargs)
|
Chris@87
|
1506 assert_equal(test, ctrl)
|
Chris@87
|
1507 # w/ default dtype
|
Chris@87
|
1508 ctrl = np.array([(0, 1, 2), (3, 4, 5)],
|
Chris@87
|
1509 dtype=[(_, float) for _ in ('A', 'f0', 'C')])
|
Chris@87
|
1510 test = np.ndfromtxt(TextIO(data), **kwargs)
|
Chris@87
|
1511
|
Chris@87
|
1512 def test_names_auto_completion(self):
|
Chris@87
|
1513 "Make sure that names are properly completed"
|
Chris@87
|
1514 data = "1 2 3\n 4 5 6"
|
Chris@87
|
1515 test = np.genfromtxt(TextIO(data),
|
Chris@87
|
1516 dtype=(int, float, int), names="a")
|
Chris@87
|
1517 ctrl = np.array([(1, 2, 3), (4, 5, 6)],
|
Chris@87
|
1518 dtype=[('a', int), ('f0', float), ('f1', int)])
|
Chris@87
|
1519 assert_equal(test, ctrl)
|
Chris@87
|
1520
|
Chris@87
|
1521 def test_names_with_usecols_bug1636(self):
|
Chris@87
|
1522 "Make sure we pick up the right names w/ usecols"
|
Chris@87
|
1523 data = "A,B,C,D,E\n0,1,2,3,4\n0,1,2,3,4\n0,1,2,3,4"
|
Chris@87
|
1524 ctrl_names = ("A", "C", "E")
|
Chris@87
|
1525 test = np.genfromtxt(TextIO(data),
|
Chris@87
|
1526 dtype=(int, int, int), delimiter=",",
|
Chris@87
|
1527 usecols=(0, 2, 4), names=True)
|
Chris@87
|
1528 assert_equal(test.dtype.names, ctrl_names)
|
Chris@87
|
1529 #
|
Chris@87
|
1530 test = np.genfromtxt(TextIO(data),
|
Chris@87
|
1531 dtype=(int, int, int), delimiter=",",
|
Chris@87
|
1532 usecols=("A", "C", "E"), names=True)
|
Chris@87
|
1533 assert_equal(test.dtype.names, ctrl_names)
|
Chris@87
|
1534 #
|
Chris@87
|
1535 test = np.genfromtxt(TextIO(data),
|
Chris@87
|
1536 dtype=int, delimiter=",",
|
Chris@87
|
1537 usecols=("A", "C", "E"), names=True)
|
Chris@87
|
1538 assert_equal(test.dtype.names, ctrl_names)
|
Chris@87
|
1539
|
Chris@87
|
1540 def test_fixed_width_names(self):
|
Chris@87
|
1541 "Test fix-width w/ names"
|
Chris@87
|
1542 data = " A B C\n 0 1 2.3\n 45 67 9."
|
Chris@87
|
1543 kwargs = dict(delimiter=(5, 5, 4), names=True, dtype=None)
|
Chris@87
|
1544 ctrl = np.array([(0, 1, 2.3), (45, 67, 9.)],
|
Chris@87
|
1545 dtype=[('A', int), ('B', int), ('C', float)])
|
Chris@87
|
1546 test = np.ndfromtxt(TextIO(data), **kwargs)
|
Chris@87
|
1547 assert_equal(test, ctrl)
|
Chris@87
|
1548 #
|
Chris@87
|
1549 kwargs = dict(delimiter=5, names=True, dtype=None)
|
Chris@87
|
1550 ctrl = np.array([(0, 1, 2.3), (45, 67, 9.)],
|
Chris@87
|
1551 dtype=[('A', int), ('B', int), ('C', float)])
|
Chris@87
|
1552 test = np.ndfromtxt(TextIO(data), **kwargs)
|
Chris@87
|
1553 assert_equal(test, ctrl)
|
Chris@87
|
1554
|
Chris@87
|
1555 def test_filling_values(self):
|
Chris@87
|
1556 "Test missing values"
|
Chris@87
|
1557 data = b"1, 2, 3\n1, , 5\n0, 6, \n"
|
Chris@87
|
1558 kwargs = dict(delimiter=",", dtype=None, filling_values=-999)
|
Chris@87
|
1559 ctrl = np.array([[1, 2, 3], [1, -999, 5], [0, 6, -999]], dtype=int)
|
Chris@87
|
1560 test = np.ndfromtxt(TextIO(data), **kwargs)
|
Chris@87
|
1561 assert_equal(test, ctrl)
|
Chris@87
|
1562
|
Chris@87
|
1563 def test_comments_is_none(self):
|
Chris@87
|
1564 # Github issue 329 (None was previously being converted to 'None').
|
Chris@87
|
1565 test = np.genfromtxt(TextIO("test1,testNonetherestofthedata"),
|
Chris@87
|
1566 dtype=None, comments=None, delimiter=',')
|
Chris@87
|
1567 assert_equal(test[1], b'testNonetherestofthedata')
|
Chris@87
|
1568 test = np.genfromtxt(TextIO("test1, testNonetherestofthedata"),
|
Chris@87
|
1569 dtype=None, comments=None, delimiter=',')
|
Chris@87
|
1570 assert_equal(test[1], b' testNonetherestofthedata')
|
Chris@87
|
1571
|
Chris@87
|
1572 def test_recfromtxt(self):
|
Chris@87
|
1573 #
|
Chris@87
|
1574 data = TextIO('A,B\n0,1\n2,3')
|
Chris@87
|
1575 kwargs = dict(delimiter=",", missing_values="N/A", names=True)
|
Chris@87
|
1576 test = np.recfromtxt(data, **kwargs)
|
Chris@87
|
1577 control = np.array([(0, 1), (2, 3)],
|
Chris@87
|
1578 dtype=[('A', np.int), ('B', np.int)])
|
Chris@87
|
1579 self.assertTrue(isinstance(test, np.recarray))
|
Chris@87
|
1580 assert_equal(test, control)
|
Chris@87
|
1581 #
|
Chris@87
|
1582 data = TextIO('A,B\n0,1\n2,N/A')
|
Chris@87
|
1583 test = np.recfromtxt(data, dtype=None, usemask=True, **kwargs)
|
Chris@87
|
1584 control = ma.array([(0, 1), (2, -1)],
|
Chris@87
|
1585 mask=[(False, False), (False, True)],
|
Chris@87
|
1586 dtype=[('A', np.int), ('B', np.int)])
|
Chris@87
|
1587 assert_equal(test, control)
|
Chris@87
|
1588 assert_equal(test.mask, control.mask)
|
Chris@87
|
1589 assert_equal(test.A, [0, 2])
|
Chris@87
|
1590
|
Chris@87
|
1591 def test_recfromcsv(self):
|
Chris@87
|
1592 #
|
Chris@87
|
1593 data = TextIO('A,B\n0,1\n2,3')
|
Chris@87
|
1594 kwargs = dict(missing_values="N/A", names=True, case_sensitive=True)
|
Chris@87
|
1595 test = np.recfromcsv(data, dtype=None, **kwargs)
|
Chris@87
|
1596 control = np.array([(0, 1), (2, 3)],
|
Chris@87
|
1597 dtype=[('A', np.int), ('B', np.int)])
|
Chris@87
|
1598 self.assertTrue(isinstance(test, np.recarray))
|
Chris@87
|
1599 assert_equal(test, control)
|
Chris@87
|
1600 #
|
Chris@87
|
1601 data = TextIO('A,B\n0,1\n2,N/A')
|
Chris@87
|
1602 test = np.recfromcsv(data, dtype=None, usemask=True, **kwargs)
|
Chris@87
|
1603 control = ma.array([(0, 1), (2, -1)],
|
Chris@87
|
1604 mask=[(False, False), (False, True)],
|
Chris@87
|
1605 dtype=[('A', np.int), ('B', np.int)])
|
Chris@87
|
1606 assert_equal(test, control)
|
Chris@87
|
1607 assert_equal(test.mask, control.mask)
|
Chris@87
|
1608 assert_equal(test.A, [0, 2])
|
Chris@87
|
1609 #
|
Chris@87
|
1610 data = TextIO('A,B\n0,1\n2,3')
|
Chris@87
|
1611 test = np.recfromcsv(data, missing_values='N/A',)
|
Chris@87
|
1612 control = np.array([(0, 1), (2, 3)],
|
Chris@87
|
1613 dtype=[('a', np.int), ('b', np.int)])
|
Chris@87
|
1614 self.assertTrue(isinstance(test, np.recarray))
|
Chris@87
|
1615 assert_equal(test, control)
|
Chris@87
|
1616 #
|
Chris@87
|
1617 data = TextIO('A,B\n0,1\n2,3')
|
Chris@87
|
1618 dtype = [('a', np.int), ('b', np.float)]
|
Chris@87
|
1619 test = np.recfromcsv(data, missing_values='N/A', dtype=dtype)
|
Chris@87
|
1620 control = np.array([(0, 1), (2, 3)],
|
Chris@87
|
1621 dtype=dtype)
|
Chris@87
|
1622 self.assertTrue(isinstance(test, np.recarray))
|
Chris@87
|
1623 assert_equal(test, control)
|
Chris@87
|
1624
|
Chris@87
|
1625 def test_gft_using_filename(self):
|
Chris@87
|
1626 # Test that we can load data from a filename as well as a file object
|
Chris@87
|
1627 wanted = np.arange(6).reshape((2, 3))
|
Chris@87
|
1628 if sys.version_info[0] >= 3:
|
Chris@87
|
1629 # python 3k is known to fail for '\r'
|
Chris@87
|
1630 linesep = ('\n', '\r\n')
|
Chris@87
|
1631 else:
|
Chris@87
|
1632 linesep = ('\n', '\r\n', '\r')
|
Chris@87
|
1633
|
Chris@87
|
1634 for sep in linesep:
|
Chris@87
|
1635 data = '0 1 2' + sep + '3 4 5'
|
Chris@87
|
1636 f, name = mkstemp()
|
Chris@87
|
1637 # We can't use NamedTemporaryFile on windows, because we cannot
|
Chris@87
|
1638 # reopen the file.
|
Chris@87
|
1639 try:
|
Chris@87
|
1640 os.write(f, asbytes(data))
|
Chris@87
|
1641 assert_array_equal(np.genfromtxt(name), wanted)
|
Chris@87
|
1642 finally:
|
Chris@87
|
1643 os.close(f)
|
Chris@87
|
1644 os.unlink(name)
|
Chris@87
|
1645
|
Chris@87
|
1646 def test_gft_using_generator(self):
|
Chris@87
|
1647 # gft doesn't work with unicode.
|
Chris@87
|
1648 def count():
|
Chris@87
|
1649 for i in range(10):
|
Chris@87
|
1650 yield asbytes("%d" % i)
|
Chris@87
|
1651
|
Chris@87
|
1652 res = np.genfromtxt(count())
|
Chris@87
|
1653 assert_array_equal(res, np.arange(10))
|
Chris@87
|
1654
|
Chris@87
|
1655
|
Chris@87
|
1656 def test_gzip_load():
|
Chris@87
|
1657 a = np.random.random((5, 5))
|
Chris@87
|
1658
|
Chris@87
|
1659 s = BytesIO()
|
Chris@87
|
1660 f = gzip.GzipFile(fileobj=s, mode="w")
|
Chris@87
|
1661
|
Chris@87
|
1662 np.save(f, a)
|
Chris@87
|
1663 f.close()
|
Chris@87
|
1664 s.seek(0)
|
Chris@87
|
1665
|
Chris@87
|
1666 f = gzip.GzipFile(fileobj=s, mode="r")
|
Chris@87
|
1667 assert_array_equal(np.load(f), a)
|
Chris@87
|
1668
|
Chris@87
|
1669
|
Chris@87
|
1670 def test_gzip_loadtxt():
|
Chris@87
|
1671 # Thanks to another windows brokeness, we can't use
|
Chris@87
|
1672 # NamedTemporaryFile: a file created from this function cannot be
|
Chris@87
|
1673 # reopened by another open call. So we first put the gzipped string
|
Chris@87
|
1674 # of the test reference array, write it to a securely opened file,
|
Chris@87
|
1675 # which is then read from by the loadtxt function
|
Chris@87
|
1676 s = BytesIO()
|
Chris@87
|
1677 g = gzip.GzipFile(fileobj=s, mode='w')
|
Chris@87
|
1678 g.write(b'1 2 3\n')
|
Chris@87
|
1679 g.close()
|
Chris@87
|
1680 s.seek(0)
|
Chris@87
|
1681
|
Chris@87
|
1682 f, name = mkstemp(suffix='.gz')
|
Chris@87
|
1683 try:
|
Chris@87
|
1684 os.write(f, s.read())
|
Chris@87
|
1685 s.close()
|
Chris@87
|
1686 assert_array_equal(np.loadtxt(name), [1, 2, 3])
|
Chris@87
|
1687 finally:
|
Chris@87
|
1688 os.close(f)
|
Chris@87
|
1689 os.unlink(name)
|
Chris@87
|
1690
|
Chris@87
|
1691
|
Chris@87
|
1692 def test_gzip_loadtxt_from_string():
|
Chris@87
|
1693 s = BytesIO()
|
Chris@87
|
1694 f = gzip.GzipFile(fileobj=s, mode="w")
|
Chris@87
|
1695 f.write(b'1 2 3\n')
|
Chris@87
|
1696 f.close()
|
Chris@87
|
1697 s.seek(0)
|
Chris@87
|
1698
|
Chris@87
|
1699 f = gzip.GzipFile(fileobj=s, mode="r")
|
Chris@87
|
1700 assert_array_equal(np.loadtxt(f), [1, 2, 3])
|
Chris@87
|
1701
|
Chris@87
|
1702
|
Chris@87
|
1703 def test_npzfile_dict():
|
Chris@87
|
1704 s = BytesIO()
|
Chris@87
|
1705 x = np.zeros((3, 3))
|
Chris@87
|
1706 y = np.zeros((3, 3))
|
Chris@87
|
1707
|
Chris@87
|
1708 np.savez(s, x=x, y=y)
|
Chris@87
|
1709 s.seek(0)
|
Chris@87
|
1710
|
Chris@87
|
1711 z = np.load(s)
|
Chris@87
|
1712
|
Chris@87
|
1713 assert_('x' in z)
|
Chris@87
|
1714 assert_('y' in z)
|
Chris@87
|
1715 assert_('x' in z.keys())
|
Chris@87
|
1716 assert_('y' in z.keys())
|
Chris@87
|
1717
|
Chris@87
|
1718 for f, a in z.items():
|
Chris@87
|
1719 assert_(f in ['x', 'y'])
|
Chris@87
|
1720 assert_equal(a.shape, (3, 3))
|
Chris@87
|
1721
|
Chris@87
|
1722 assert_(len(z.items()) == 2)
|
Chris@87
|
1723
|
Chris@87
|
1724 for f in z:
|
Chris@87
|
1725 assert_(f in ['x', 'y'])
|
Chris@87
|
1726
|
Chris@87
|
1727 assert_('x' in z.keys())
|
Chris@87
|
1728
|
Chris@87
|
1729
|
Chris@87
|
1730 def test_load_refcount():
|
Chris@87
|
1731 # Check that objects returned by np.load are directly freed based on
|
Chris@87
|
1732 # their refcount, rather than needing the gc to collect them.
|
Chris@87
|
1733
|
Chris@87
|
1734 f = BytesIO()
|
Chris@87
|
1735 np.savez(f, [1, 2, 3])
|
Chris@87
|
1736 f.seek(0)
|
Chris@87
|
1737
|
Chris@87
|
1738 gc.collect()
|
Chris@87
|
1739 n_before = len(gc.get_objects())
|
Chris@87
|
1740 np.load(f)
|
Chris@87
|
1741 n_after = len(gc.get_objects())
|
Chris@87
|
1742
|
Chris@87
|
1743 assert_equal(n_before, n_after)
|
Chris@87
|
1744
|
Chris@87
|
1745 if __name__ == "__main__":
|
Chris@87
|
1746 run_module_suite()
|