diff options
author | pierregm <pierregm@localhost> | 2009-10-09 02:17:30 +0000 |
---|---|---|
committer | pierregm <pierregm@localhost> | 2009-10-09 02:17:30 +0000 |
commit | 8cad335f8b97100df988dbb6fd5d06072c667515 (patch) | |
tree | 5eb53d599da9028fc42d31ffdd979f022268a472 /numpy/lib/tests | |
parent | e54df63621db89eddbadc7bf0f36798ee1f79e0a (diff) | |
download | numpy-8cad335f8b97100df988dbb6fd5d06072c667515.tar.gz |
* ma.masked_equal : force the `fill_value` of the output to `value` (ticket #1253)
* lib._iotools:
- NameValidator : add the `nbfields` optional argument to validate
- add easy_dtype
* lib.io.genfromtxt :
- add the `autostrip` optional argument (ticket #1238)
- use `invalid_raise=True` as default
- use the easy_dtype mechanism (ticket #1252)
Diffstat (limited to 'numpy/lib/tests')
-rw-r--r-- | numpy/lib/tests/test__iotools.py | 87 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 81 |
2 files changed, 157 insertions, 11 deletions
diff --git a/numpy/lib/tests/test__iotools.py b/numpy/lib/tests/test__iotools.py index 2cb8461c3..c16491aee 100644 --- a/numpy/lib/tests/test__iotools.py +++ b/numpy/lib/tests/test__iotools.py @@ -3,7 +3,7 @@ import StringIO import numpy as np from numpy.lib._iotools import LineSplitter, NameValidator, StringConverter,\ - has_nested_fields + has_nested_fields, easy_dtype from numpy.testing import * class TestLineSplitter(TestCase): @@ -90,6 +90,35 @@ class TestNameValidator(TestCase): validator = NameValidator(excludelist = ['dates', 'data', 'mask']) test = validator.validate(names) assert_equal(test, ['dates_', 'data_', 'Other_Data', 'mask_']) + # + def test_missing_names(self): + "Test validate missing names" + namelist = ('a', 'b', 'c') + validator = NameValidator() + assert_equal(validator(namelist), ['a', 'b', 'c']) + namelist = ('', 'b', 'c') + assert_equal(validator(namelist), ['f0', 'b', 'c']) + namelist = ('a', 'b', '') + assert_equal(validator(namelist), ['a', 'b', 'f0']) + namelist = ('', 'f0', '') + assert_equal(validator(namelist), ['f1', 'f0', 'f2']) + # + def test_validate_nb_names(self): + "Test validate nb names" + namelist = ('a', 'b', 'c') + validator = NameValidator() + assert_equal(validator(namelist, nbfields=1), ('a', )) + assert_equal(validator(namelist, nbfields=5, defaultfmt="g%i"), + ['a', 'b', 'c', 'g0', 'g1']) + # + def test_validate_wo_names(self): + "Test validate no names" + namelist = None + validator = NameValidator() + assert(validator(namelist) is None) + assert_equal(validator(namelist, nbfields=3), ['f0', 'f1', 'f2']) + + #------------------------------------------------------------------------------- @@ -165,3 +194,59 @@ class TestMiscFunctions(TestCase): ndtype = np.dtype([('A', int), ('B', [('BA', float), ('BB', '|S1')])]) assert_equal(has_nested_fields(ndtype), True) + def test_easy_dtype(self): + "Test ndtype on dtypes" + # Simple case + ndtype = float + assert_equal(easy_dtype(ndtype), np.dtype(float)) + # As string w/o names + ndtype = "i4, f8" + assert_equal(easy_dtype(ndtype), + np.dtype([('f0', "i4"), ('f1', "f8")])) + # As string w/o names but different default format + assert_equal(easy_dtype(ndtype, defaultfmt="field_%03i"), + np.dtype([('field_000', "i4"), ('field_001', "f8")])) + # As string w/ names + ndtype = "i4, f8" + assert_equal(easy_dtype(ndtype, names="a, b"), + np.dtype([('a', "i4"), ('b', "f8")])) + # As string w/ names (too many) + ndtype = "i4, f8" + assert_equal(easy_dtype(ndtype, names="a, b, c"), + np.dtype([('a', "i4"), ('b', "f8")])) + # As string w/ names (not enough) + ndtype = "i4, f8" + assert_equal(easy_dtype(ndtype, names=", b"), + np.dtype([('f0', "i4"), ('b', "f8")])) + # ... (with different default format) + assert_equal(easy_dtype(ndtype, names="a", defaultfmt="f%02i"), + np.dtype([('a', "i4"), ('f00', "f8")])) + # As list of tuples w/o names + ndtype = [('A', int), ('B', float)] + assert_equal(easy_dtype(ndtype), np.dtype([('A', int), ('B', float)])) + # As list of tuples w/ names + assert_equal(easy_dtype(ndtype, names="a,b"), + np.dtype([('a', int), ('b', float)])) + # As list of tuples w/ not enough names + assert_equal(easy_dtype(ndtype, names="a"), + np.dtype([('a', int), ('f0', float)])) + # As list of tuples w/ too many names + assert_equal(easy_dtype(ndtype, names="a,b,c"), + np.dtype([('a', int), ('b', float)])) + # As list of types w/o names + ndtype = (int, float, float) + assert_equal(easy_dtype(ndtype), + np.dtype([('f0', int), ('f1', float), ('f2', float)])) + # As list of types w names + ndtype = (int, float, float) + assert_equal(easy_dtype(ndtype, names="a, b, c"), + np.dtype([('a', int), ('b', float), ('c', float)])) + # As simple dtype w/ names + ndtype = np.dtype(float) + assert_equal(easy_dtype(ndtype, names="a, b, c"), + np.dtype([(_, float) for _ in ('a', 'b', 'c')])) + # As simple dtype w/o names (but multiple fields) + ndtype = np.dtype(float) + assert_equal(easy_dtype(ndtype, names=['', '', ''], defaultfmt="f%02i"), + np.dtype([(_, float) for _ in ('f00', 'f01', 'f02')])) + diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index bc05ed4d3..5e0d666c2 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -763,8 +763,8 @@ M 33 21.99 def test_withmissing(self): data = StringIO.StringIO('A,B\n0,1\n2,N/A') - test = np.mafromtxt(data, dtype=None, delimiter=',', missing='N/A', - names=True) + kwargs = dict(delimiter=",", missing="N/A", names=True) + test = np.mafromtxt(data, dtype=None, **kwargs) control = ma.array([(0, 1), (2, -1)], mask=[(False, False), (False, True)], dtype=[('A', np.int), ('B', np.int)]) @@ -772,9 +772,10 @@ M 33 21.99 assert_equal(test.mask, control.mask) # data.seek(0) - test = np.mafromtxt(data, delimiter=',', missing='N/A', names=True) + test = np.mafromtxt(data, **kwargs) control = ma.array([(0, 1), (2, -1)], - mask=[[False, False], [False, True]],) + mask=[(False, False), (False, True)], + dtype=[('A', np.float), ('B', np.float)]) assert_equal(test, control) assert_equal(test.mask, control.mask) @@ -848,13 +849,14 @@ M 33 21.99 data.insert(0, "a, b, c, d, e") mdata = StringIO.StringIO("\n".join(data)) # - mtest = np.ndfromtxt(mdata, delimiter=",", names=True, dtype=None,) + kwargs = dict(delimiter=",", dtype=None, names=True) + mtest = np.ndfromtxt(mdata, invalid_raise=False, **kwargs) assert_equal(len(mtest), 45) assert_equal(mtest, np.ones(45, dtype=[(_, int) for _ in 'abcde'])) # mdata.seek(0) assert_raises(ValueError, np.ndfromtxt, mdata, - delimiter=",", names=True, invalid_raise=True) + delimiter=",", names=True) def test_invalid_raise_with_usecols(self): "Test invalid_raise with usecols" @@ -863,15 +865,15 @@ M 33 21.99 data[10 * i] = "2, 2, 2, 2 2" data.insert(0, "a, b, c, d, e") mdata = StringIO.StringIO("\n".join(data)) + kwargs = dict(delimiter=",", dtype=None, names=True, + invalid_raise=False) # - mtest = np.ndfromtxt(mdata, delimiter=",", names=True, dtype=None, - usecols=(0, 4)) + mtest = np.ndfromtxt(mdata, usecols=(0, 4), **kwargs) assert_equal(len(mtest), 45) assert_equal(mtest, np.ones(45, dtype=[(_, int) for _ in 'ae'])) # mdata.seek(0) - mtest = np.ndfromtxt(mdata, delimiter=",", names=True, dtype=None, - usecols=(0, 1)) + mtest = np.ndfromtxt(mdata, usecols=(0, 1), **kwargs) assert_equal(len(mtest), 50) control = np.ones(50, dtype=[(_, int) for _ in 'ab']) control[[10 * _ for _ in range(5)]] = (2, 2) @@ -879,6 +881,7 @@ M 33 21.99 def test_inconsistent_dtype(self): + "Test inconsistent dtype" data = ["1, 1, 1, 1, -1.1"] * 50 mdata = StringIO.StringIO("\n".join(data)) @@ -888,6 +891,64 @@ M 33 21.99 assert_raises(TypeError, np.genfromtxt, mdata, **kwargs) + def test_default_field_format(self): + "Test default format" + data = "0, 1, 2.3\n4, 5, 6.7" + mtest = np.ndfromtxt(StringIO.StringIO(data), + delimiter=",", dtype=None, defaultfmt="f%02i") + ctrl = np.array([(0, 1, 2.3), (4, 5, 6.7)], + dtype=[("f00", int), ("f01", int), ("f02", float)]) + assert_equal(mtest, ctrl) + + def test_single_dtype_wo_names(self): + "Test single dtype w/o names" + data = "0, 1, 2.3\n4, 5, 6.7" + mtest = np.ndfromtxt(StringIO.StringIO(data), + delimiter=",", dtype=float, defaultfmt="f%02i") + ctrl = np.array([[0., 1., 2.3], [4., 5., 6.7]], dtype=float) + assert_equal(mtest, ctrl) + + def test_single_dtype_w_explicit_names(self): + "Test single dtype w explicit names" + data = "0, 1, 2.3\n4, 5, 6.7" + mtest = np.ndfromtxt(StringIO.StringIO(data), + delimiter=",", dtype=float, names="a, b, c") + ctrl = np.array([(0., 1., 2.3), (4., 5., 6.7)], + dtype=[(_, float) for _ in "abc"]) + assert_equal(mtest, ctrl) + + def test_single_dtype_w_implicit_names(self): + "Test single dtype w implicit names" + data = "a, b, c\n0, 1, 2.3\n4, 5, 6.7" + mtest = np.ndfromtxt(StringIO.StringIO(data), + delimiter=",", dtype=float, names=True) + ctrl = np.array([(0., 1., 2.3), (4., 5., 6.7)], + dtype=[(_, float) for _ in "abc"]) + assert_equal(mtest, ctrl) + + def test_easy_structured_dtype(self): + "Test easy structured dtype" + data = "0, 1, 2.3\n4, 5, 6.7" + mtest = np.ndfromtxt(StringIO.StringIO(data), delimiter=",", + dtype=(int, float, float), defaultfmt="f_%02i") + ctrl = np.array([(0, 1., 2.3), (4, 5., 6.7)], + dtype=[("f_00", int), ("f_01", float), ("f_02", float)]) + assert_equal(mtest, ctrl) + + def test_autostrip(self): + "Test autostrip" + data = "01/01/2003 , 1.3, abcde" + kwargs = dict(delimiter=",", dtype=None) + mtest = np.ndfromtxt(StringIO.StringIO(data), **kwargs) + ctrl = np.array([('01/01/2003 ', 1.3, ' abcde')], + dtype=[('f0', '|S12'), ('f1', float), ('f2', '|S8')]) + assert_equal(mtest, ctrl) + mtest = np.ndfromtxt(StringIO.StringIO(data), autostrip=True, **kwargs) + ctrl = np.array([('01/01/2003', 1.3, 'abcde')], + dtype=[('f0', '|S10'), ('f1', float), ('f2', '|S5')]) + assert_equal(mtest, ctrl) + + def test_recfromtxt(self): # data = StringIO.StringIO('A,B\n0,1\n2,3') |