comparison DEPENDENCIES/mingw32/Python27/Lib/site-packages/numpy/lib/tests/test__iotools.py @ 87:2a2c65a20a8b

Add Python libs and headers
author Chris Cannam
date Wed, 25 Feb 2015 14:05:22 +0000
parents
children
comparison
equal deleted inserted replaced
86:413a9d26189e 87:2a2c65a20a8b
1 from __future__ import division, absolute_import, print_function
2
3 import sys
4 import time
5 from datetime import date
6
7 import numpy as np
8 from numpy.compat import asbytes, asbytes_nested
9 from numpy.testing import (
10 run_module_suite, TestCase, assert_, assert_equal
11 )
12 from numpy.lib._iotools import (
13 LineSplitter, NameValidator, StringConverter,
14 has_nested_fields, easy_dtype, flatten_dtype
15 )
16
17
18 class TestLineSplitter(TestCase):
19 "Tests the LineSplitter class."
20
21 def test_no_delimiter(self):
22 "Test LineSplitter w/o delimiter"
23 strg = asbytes(" 1 2 3 4 5 # test")
24 test = LineSplitter()(strg)
25 assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5']))
26 test = LineSplitter('')(strg)
27 assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5']))
28
29 def test_space_delimiter(self):
30 "Test space delimiter"
31 strg = asbytes(" 1 2 3 4 5 # test")
32 test = LineSplitter(asbytes(' '))(strg)
33 assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5']))
34 test = LineSplitter(asbytes(' '))(strg)
35 assert_equal(test, asbytes_nested(['1 2 3 4', '5']))
36
37 def test_tab_delimiter(self):
38 "Test tab delimiter"
39 strg = asbytes(" 1\t 2\t 3\t 4\t 5 6")
40 test = LineSplitter(asbytes('\t'))(strg)
41 assert_equal(test, asbytes_nested(['1', '2', '3', '4', '5 6']))
42 strg = asbytes(" 1 2\t 3 4\t 5 6")
43 test = LineSplitter(asbytes('\t'))(strg)
44 assert_equal(test, asbytes_nested(['1 2', '3 4', '5 6']))
45
46 def test_other_delimiter(self):
47 "Test LineSplitter on delimiter"
48 strg = asbytes("1,2,3,4,,5")
49 test = LineSplitter(asbytes(','))(strg)
50 assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5']))
51 #
52 strg = asbytes(" 1,2,3,4,,5 # test")
53 test = LineSplitter(asbytes(','))(strg)
54 assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5']))
55
56 def test_constant_fixed_width(self):
57 "Test LineSplitter w/ fixed-width fields"
58 strg = asbytes(" 1 2 3 4 5 # test")
59 test = LineSplitter(3)(strg)
60 assert_equal(test, asbytes_nested(['1', '2', '3', '4', '', '5', '']))
61 #
62 strg = asbytes(" 1 3 4 5 6# test")
63 test = LineSplitter(20)(strg)
64 assert_equal(test, asbytes_nested(['1 3 4 5 6']))
65 #
66 strg = asbytes(" 1 3 4 5 6# test")
67 test = LineSplitter(30)(strg)
68 assert_equal(test, asbytes_nested(['1 3 4 5 6']))
69
70 def test_variable_fixed_width(self):
71 strg = asbytes(" 1 3 4 5 6# test")
72 test = LineSplitter((3, 6, 6, 3))(strg)
73 assert_equal(test, asbytes_nested(['1', '3', '4 5', '6']))
74 #
75 strg = asbytes(" 1 3 4 5 6# test")
76 test = LineSplitter((6, 6, 9))(strg)
77 assert_equal(test, asbytes_nested(['1', '3 4', '5 6']))
78
79 #-------------------------------------------------------------------------------
80
81
82 class TestNameValidator(TestCase):
83
84 def test_case_sensitivity(self):
85 "Test case sensitivity"
86 names = ['A', 'a', 'b', 'c']
87 test = NameValidator().validate(names)
88 assert_equal(test, ['A', 'a', 'b', 'c'])
89 test = NameValidator(case_sensitive=False).validate(names)
90 assert_equal(test, ['A', 'A_1', 'B', 'C'])
91 test = NameValidator(case_sensitive='upper').validate(names)
92 assert_equal(test, ['A', 'A_1', 'B', 'C'])
93 test = NameValidator(case_sensitive='lower').validate(names)
94 assert_equal(test, ['a', 'a_1', 'b', 'c'])
95
96 def test_excludelist(self):
97 "Test excludelist"
98 names = ['dates', 'data', 'Other Data', 'mask']
99 validator = NameValidator(excludelist=['dates', 'data', 'mask'])
100 test = validator.validate(names)
101 assert_equal(test, ['dates_', 'data_', 'Other_Data', 'mask_'])
102
103 def test_missing_names(self):
104 "Test validate missing names"
105 namelist = ('a', 'b', 'c')
106 validator = NameValidator()
107 assert_equal(validator(namelist), ['a', 'b', 'c'])
108 namelist = ('', 'b', 'c')
109 assert_equal(validator(namelist), ['f0', 'b', 'c'])
110 namelist = ('a', 'b', '')
111 assert_equal(validator(namelist), ['a', 'b', 'f0'])
112 namelist = ('', 'f0', '')
113 assert_equal(validator(namelist), ['f1', 'f0', 'f2'])
114
115 def test_validate_nb_names(self):
116 "Test validate nb names"
117 namelist = ('a', 'b', 'c')
118 validator = NameValidator()
119 assert_equal(validator(namelist, nbfields=1), ('a',))
120 assert_equal(validator(namelist, nbfields=5, defaultfmt="g%i"),
121 ['a', 'b', 'c', 'g0', 'g1'])
122
123 def test_validate_wo_names(self):
124 "Test validate no names"
125 namelist = None
126 validator = NameValidator()
127 assert_(validator(namelist) is None)
128 assert_equal(validator(namelist, nbfields=3), ['f0', 'f1', 'f2'])
129
130 #-------------------------------------------------------------------------------
131
132
133 def _bytes_to_date(s):
134 if sys.version_info[0] >= 3:
135 return date(*time.strptime(s.decode('latin1'), "%Y-%m-%d")[:3])
136 else:
137 return date(*time.strptime(s, "%Y-%m-%d")[:3])
138
139
140 class TestStringConverter(TestCase):
141 "Test StringConverter"
142
143 def test_creation(self):
144 "Test creation of a StringConverter"
145 converter = StringConverter(int, -99999)
146 assert_equal(converter._status, 1)
147 assert_equal(converter.default, -99999)
148
149 def test_upgrade(self):
150 "Tests the upgrade method."
151 converter = StringConverter()
152 assert_equal(converter._status, 0)
153 converter.upgrade(asbytes('0'))
154 assert_equal(converter._status, 1)
155 converter.upgrade(asbytes('0.'))
156 assert_equal(converter._status, 2)
157 converter.upgrade(asbytes('0j'))
158 assert_equal(converter._status, 3)
159 converter.upgrade(asbytes('a'))
160 assert_equal(converter._status, len(converter._mapper) - 1)
161
162 def test_missing(self):
163 "Tests the use of missing values."
164 converter = StringConverter(missing_values=(asbytes('missing'),
165 asbytes('missed')))
166 converter.upgrade(asbytes('0'))
167 assert_equal(converter(asbytes('0')), 0)
168 assert_equal(converter(asbytes('')), converter.default)
169 assert_equal(converter(asbytes('missing')), converter.default)
170 assert_equal(converter(asbytes('missed')), converter.default)
171 try:
172 converter('miss')
173 except ValueError:
174 pass
175
176 def test_upgrademapper(self):
177 "Tests updatemapper"
178 dateparser = _bytes_to_date
179 StringConverter.upgrade_mapper(dateparser, date(2000, 1, 1))
180 convert = StringConverter(dateparser, date(2000, 1, 1))
181 test = convert(asbytes('2001-01-01'))
182 assert_equal(test, date(2001, 1, 1))
183 test = convert(asbytes('2009-01-01'))
184 assert_equal(test, date(2009, 1, 1))
185 test = convert(asbytes(''))
186 assert_equal(test, date(2000, 1, 1))
187
188 def test_string_to_object(self):
189 "Make sure that string-to-object functions are properly recognized"
190 conv = StringConverter(_bytes_to_date)
191 assert_equal(conv._mapper[-2][0](0), 0j)
192 assert_(hasattr(conv, 'default'))
193
194 def test_keep_default(self):
195 "Make sure we don't lose an explicit default"
196 converter = StringConverter(None, missing_values=asbytes(''),
197 default=-999)
198 converter.upgrade(asbytes('3.14159265'))
199 assert_equal(converter.default, -999)
200 assert_equal(converter.type, np.dtype(float))
201 #
202 converter = StringConverter(
203 None, missing_values=asbytes(''), default=0)
204 converter.upgrade(asbytes('3.14159265'))
205 assert_equal(converter.default, 0)
206 assert_equal(converter.type, np.dtype(float))
207
208 def test_keep_default_zero(self):
209 "Check that we don't lose a default of 0"
210 converter = StringConverter(int, default=0,
211 missing_values=asbytes("N/A"))
212 assert_equal(converter.default, 0)
213
214 def test_keep_missing_values(self):
215 "Check that we're not losing missing values"
216 converter = StringConverter(int, default=0,
217 missing_values=asbytes("N/A"))
218 assert_equal(
219 converter.missing_values, set(asbytes_nested(['', 'N/A'])))
220
221 def test_int64_dtype(self):
222 "Check that int64 integer types can be specified"
223 converter = StringConverter(np.int64, default=0)
224 val = asbytes("-9223372036854775807")
225 assert_(converter(val) == -9223372036854775807)
226 val = asbytes("9223372036854775807")
227 assert_(converter(val) == 9223372036854775807)
228
229 def test_uint64_dtype(self):
230 "Check that uint64 integer types can be specified"
231 converter = StringConverter(np.uint64, default=0)
232 val = asbytes("9223372043271415339")
233 assert_(converter(val) == 9223372043271415339)
234
235
236 class TestMiscFunctions(TestCase):
237
238 def test_has_nested_dtype(self):
239 "Test has_nested_dtype"
240 ndtype = np.dtype(np.float)
241 assert_equal(has_nested_fields(ndtype), False)
242 ndtype = np.dtype([('A', '|S3'), ('B', float)])
243 assert_equal(has_nested_fields(ndtype), False)
244 ndtype = np.dtype([('A', int), ('B', [('BA', float), ('BB', '|S1')])])
245 assert_equal(has_nested_fields(ndtype), True)
246
247 def test_easy_dtype(self):
248 "Test ndtype on dtypes"
249 # Simple case
250 ndtype = float
251 assert_equal(easy_dtype(ndtype), np.dtype(float))
252 # As string w/o names
253 ndtype = "i4, f8"
254 assert_equal(easy_dtype(ndtype),
255 np.dtype([('f0', "i4"), ('f1', "f8")]))
256 # As string w/o names but different default format
257 assert_equal(easy_dtype(ndtype, defaultfmt="field_%03i"),
258 np.dtype([('field_000', "i4"), ('field_001', "f8")]))
259 # As string w/ names
260 ndtype = "i4, f8"
261 assert_equal(easy_dtype(ndtype, names="a, b"),
262 np.dtype([('a', "i4"), ('b', "f8")]))
263 # As string w/ names (too many)
264 ndtype = "i4, f8"
265 assert_equal(easy_dtype(ndtype, names="a, b, c"),
266 np.dtype([('a', "i4"), ('b', "f8")]))
267 # As string w/ names (not enough)
268 ndtype = "i4, f8"
269 assert_equal(easy_dtype(ndtype, names=", b"),
270 np.dtype([('f0', "i4"), ('b', "f8")]))
271 # ... (with different default format)
272 assert_equal(easy_dtype(ndtype, names="a", defaultfmt="f%02i"),
273 np.dtype([('a', "i4"), ('f00', "f8")]))
274 # As list of tuples w/o names
275 ndtype = [('A', int), ('B', float)]
276 assert_equal(easy_dtype(ndtype), np.dtype([('A', int), ('B', float)]))
277 # As list of tuples w/ names
278 assert_equal(easy_dtype(ndtype, names="a,b"),
279 np.dtype([('a', int), ('b', float)]))
280 # As list of tuples w/ not enough names
281 assert_equal(easy_dtype(ndtype, names="a"),
282 np.dtype([('a', int), ('f0', float)]))
283 # As list of tuples w/ too many names
284 assert_equal(easy_dtype(ndtype, names="a,b,c"),
285 np.dtype([('a', int), ('b', float)]))
286 # As list of types w/o names
287 ndtype = (int, float, float)
288 assert_equal(easy_dtype(ndtype),
289 np.dtype([('f0', int), ('f1', float), ('f2', float)]))
290 # As list of types w names
291 ndtype = (int, float, float)
292 assert_equal(easy_dtype(ndtype, names="a, b, c"),
293 np.dtype([('a', int), ('b', float), ('c', float)]))
294 # As simple dtype w/ names
295 ndtype = np.dtype(float)
296 assert_equal(easy_dtype(ndtype, names="a, b, c"),
297 np.dtype([(_, float) for _ in ('a', 'b', 'c')]))
298 # As simple dtype w/o names (but multiple fields)
299 ndtype = np.dtype(float)
300 assert_equal(
301 easy_dtype(ndtype, names=['', '', ''], defaultfmt="f%02i"),
302 np.dtype([(_, float) for _ in ('f00', 'f01', 'f02')]))
303
304 def test_flatten_dtype(self):
305 "Testing flatten_dtype"
306 # Standard dtype
307 dt = np.dtype([("a", "f8"), ("b", "f8")])
308 dt_flat = flatten_dtype(dt)
309 assert_equal(dt_flat, [float, float])
310 # Recursive dtype
311 dt = np.dtype([("a", [("aa", '|S1'), ("ab", '|S2')]), ("b", int)])
312 dt_flat = flatten_dtype(dt)
313 assert_equal(dt_flat, [np.dtype('|S1'), np.dtype('|S2'), int])
314 # dtype with shaped fields
315 dt = np.dtype([("a", (float, 2)), ("b", (int, 3))])
316 dt_flat = flatten_dtype(dt)
317 assert_equal(dt_flat, [float, int])
318 dt_flat = flatten_dtype(dt, True)
319 assert_equal(dt_flat, [float] * 2 + [int] * 3)
320 # dtype w/ titles
321 dt = np.dtype([(("a", "A"), "f8"), (("b", "B"), "f8")])
322 dt_flat = flatten_dtype(dt)
323 assert_equal(dt_flat, [float, float])
324
325 if __name__ == "__main__":
326 run_module_suite()