diff options
author | Allan Haldane <allan.haldane@gmail.com> | 2015-07-28 09:36:02 -0400 |
---|---|---|
committer | Allan Haldane <allan.haldane@gmail.com> | 2017-09-07 16:47:39 -0400 |
commit | 50f4d0d810efbc8a3e3a4bdf8908faa9df4aa710 (patch) | |
tree | 0c5095946d694d19b0496db74f5fac0950955f19 | |
parent | 7412b85a1b4920e5b72ab694e51b11e4cb615866 (diff) | |
download | python-numpy-50f4d0d810efbc8a3e3a4bdf8908faa9df4aa710.tar.gz python-numpy-50f4d0d810efbc8a3e3a4bdf8908faa9df4aa710.tar.bz2 python-numpy-50f4d0d810efbc8a3e3a4bdf8908faa9df4aa710.zip |
TST: Fix/Remove old unit tests for structure assignment
-rw-r--r-- | numpy/core/tests/test_dtype.py | 7 | ||||
-rw-r--r-- | numpy/core/tests/test_multiarray.py | 79 | ||||
-rw-r--r-- | numpy/core/tests/test_nditer.py | 103 | ||||
-rw-r--r-- | numpy/core/tests/test_records.py | 17 | ||||
-rw-r--r-- | numpy/lib/tests/test_io.py | 4 | ||||
-rw-r--r-- | numpy/ma/tests/test_core.py | 16 |
6 files changed, 45 insertions, 181 deletions
diff --git a/numpy/core/tests/test_dtype.py b/numpy/core/tests/test_dtype.py index 6f6654d42..87c6f8b98 100644 --- a/numpy/core/tests/test_dtype.py +++ b/numpy/core/tests/test_dtype.py @@ -211,11 +211,12 @@ class TestRecord(object): dt = np.dtype({'names':['f0', 'f1', 'f2'], 'formats':['<u4', '<u2', '<u2'], 'offsets':[4, 0, 2]}, align=True) assert_equal(dt.itemsize, 8) + # field name should not matter: assignment is by position dt2 = np.dtype({'names':['f2', 'f0', 'f1'], - 'formats':['<u2', '<u4', '<u2'], - 'offsets':[2, 4, 0]}, align=True) + 'formats':['<u4', '<u2', '<u2'], + 'offsets':[4, 0, 2]}, align=True) vals = [(0, 1, 2), (3, -1, 4)] - vals2 = [(2, 0, 1), (4, 3, -1)] + vals2 = [(0, 1, 2), (3, -1, 4)] a = np.array(vals, dt) b = np.array(vals2, dt2) assert_equal(a.astype(dt2), b) diff --git a/numpy/core/tests/test_multiarray.py b/numpy/core/tests/test_multiarray.py index 74f6a3af9..a3d5fd2b9 100644 --- a/numpy/core/tests/test_multiarray.py +++ b/numpy/core/tests/test_multiarray.py @@ -952,16 +952,13 @@ class TestStructured(object): # Check that equality comparison works on structured arrays if # they are 'equiv'-castable a = np.array([(5, 42), (10, 1)], dtype=[('a', '>i4'), ('b', '<f8')]) - b = np.array([(42, 5), (1, 10)], dtype=[('b', '>f8'), ('a', '<i4')]) + b = np.array([(5, 42), (10, 1)], dtype=[('a', '<i4'), ('b', '>f8')]) assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) assert_equal(a == b, [True, True]) - # Check that 'equiv' casting can reorder fields and change byte - # order - # New in 1.12: This behavior changes in 1.13, test for dep warning + # Check that 'equiv' casting can change byte order assert_(np.can_cast(a.dtype, b.dtype, casting='equiv')) - with assert_warns(FutureWarning): - c = a.astype(b.dtype, casting='equiv') + c = a.astype(b.dtype, casting='equiv') assert_equal(a == c, [True, True]) # Check that 'safe' casting can change byte order and up-cast @@ -4496,23 +4493,11 @@ class TestRecord(object): # multiple subfields fn2 = func('f2') b[fn2] = 3 - with suppress_warnings() as sup: - sup.filter(FutureWarning, - "Assignment between structured arrays.*") - sup.filter(FutureWarning, - "Numpy has detected that you .*") - - assert_equal(b[['f1', 'f2']][0].tolist(), (2, 3)) - assert_equal(b[['f2', 'f1']][0].tolist(), (3, 2)) - assert_equal(b[['f1', 'f3']][0].tolist(), (2, (1,))) - # view of subfield view/copy - assert_equal(b[['f1', 'f2']][0].view(('i4', 2)).tolist(), - (2, 3)) - assert_equal(b[['f2', 'f1']][0].view(('i4', 2)).tolist(), - (3, 2)) - view_dtype = [('f1', 'i4'), ('f3', [('', 'i4')])] - assert_equal(b[['f1', 'f3']][0].view(view_dtype).tolist(), - (2, (1,))) + + assert_equal(b[['f1', 'f2']][0].tolist(), (2, 3)) + assert_equal(b[['f2', 'f1']][0].tolist(), (3, 2)) + assert_equal(b[['f1', 'f3']][0].tolist(), (2, (1,))) + # non-ascii unicode field indexing is well behaved if not is_py3: raise SkipTest('non ascii unicode field indexing skipped; ' @@ -4521,54 +4506,6 @@ class TestRecord(object): assert_raises(ValueError, a.__setitem__, u'\u03e0', 1) assert_raises(ValueError, a.__getitem__, u'\u03e0') - def test_field_names_deprecation(self): - - def collect_warnings(f, *args, **kwargs): - with warnings.catch_warnings(record=True) as log: - warnings.simplefilter("always") - f(*args, **kwargs) - return [w.category for w in log] - - a = np.zeros((1,), dtype=[('f1', 'i4'), - ('f2', 'i4'), - ('f3', [('sf1', 'i4')])]) - a['f1'][0] = 1 - a['f2'][0] = 2 - a['f3'][0] = (3,) - b = np.zeros((1,), dtype=[('f1', 'i4'), - ('f2', 'i4'), - ('f3', [('sf1', 'i4')])]) - b['f1'][0] = 1 - b['f2'][0] = 2 - b['f3'][0] = (3,) - - # All the different functions raise a warning, but not an error - assert_equal(collect_warnings(a[['f1', 'f2']].__setitem__, 0, (10, 20)), - [FutureWarning]) - # For <=1.12 a is not modified, but it will be in 1.13 - assert_equal(a, b) - - # Views also warn - subset = a[['f1', 'f2']] - subset_view = subset.view() - assert_equal(collect_warnings(subset_view['f1'].__setitem__, 0, 10), - [FutureWarning]) - # But the write goes through: - assert_equal(subset['f1'][0], 10) - # Only one warning per multiple field indexing, though (even if there - # are multiple views involved): - assert_equal(collect_warnings(subset['f1'].__setitem__, 0, 10), []) - - # make sure views of a multi-field index warn too - c = np.zeros(3, dtype='i8,i8,i8') - assert_equal(collect_warnings(c[['f0', 'f2']].view, 'i8,i8'), - [FutureWarning]) - - # make sure assignment using a different dtype warns - a = np.zeros(2, dtype=[('a', 'i4'), ('b', 'i4')]) - b = np.zeros(2, dtype=[('b', 'i4'), ('a', 'i4')]) - assert_equal(collect_warnings(a.__setitem__, (), b), [FutureWarning]) - def test_record_hash(self): a = np.array([(1, 2), (1, 2)], dtype='i1,i2') a.flags.writeable = False diff --git a/numpy/core/tests/test_nditer.py b/numpy/core/tests/test_nditer.py index 885dcb56f..59e11f22e 100644 --- a/numpy/core/tests/test_nditer.py +++ b/numpy/core/tests/test_nditer.py @@ -1816,100 +1816,45 @@ def test_iter_buffered_cast_structured_type(): if HAS_REFCOUNT: assert_equal(sys.getrefcount(a[0]), rc) - # struct type -> simple (takes the first value) - sdt = [('a', 'f4'), ('b', 'i8'), ('d', 'O')] - a = np.array([(5.5, 7, 'test'), (8, 10, 11)], dtype=sdt) + # single-field struct type -> simple + sdt = [('a', 'f4')] + a = np.array([(5.5,), (8,)], dtype=sdt) i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], casting='unsafe', op_dtypes='i4') assert_equal([x_[()] for x_ in i], [5, 8]) + # make sure multi-field struct type -> simple doesn't work + sdt = [('a', 'f4'), ('b', 'i8'), ('d', 'O')] + a = np.array([(5.5, 7, 'test'), (8, 10, 11)], dtype=sdt) + assert_raises(ValueError, lambda: ( + nditer(a, ['buffered', 'refs_ok'], ['readonly'], + casting='unsafe', + op_dtypes='i4'))) + # struct type -> struct type (field-wise copy) sdt1 = [('a', 'f4'), ('b', 'i8'), ('d', 'O')] sdt2 = [('d', 'u2'), ('a', 'O'), ('b', 'f8')] a = np.array([(1, 2, 3), (4, 5, 6)], dtype=sdt1) - # New in 1.12: This behavior changes in 1.13, test for dep warning - with assert_warns(FutureWarning): - i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], - casting='unsafe', - op_dtypes=sdt2) + i = nditer(a, ['buffered', 'refs_ok'], ['readonly'], + casting='unsafe', + op_dtypes=sdt2) assert_equal(i[0].dtype, np.dtype(sdt2)) assert_equal([np.array(x_) for x_ in i], - [np.array((3, 1, 2), dtype=sdt2), - np.array((6, 4, 5), dtype=sdt2)]) + [np.array((1, 2, 3), dtype=sdt2), + np.array((4, 5, 6), dtype=sdt2)]) - # struct type -> struct type (field gets discarded) + # make sure struct type -> struct type with different + # number of fields fails sdt1 = [('a', 'f4'), ('b', 'i8'), ('d', 'O')] sdt2 = [('b', 'O'), ('a', 'f8')] a = np.array([(1, 2, 3), (4, 5, 6)], dtype=sdt1) - # New in 1.12: This behavior changes in 1.13, test for dep warning - with assert_warns(FutureWarning): - i = nditer(a, ['buffered', 'refs_ok'], ['readwrite'], - casting='unsafe', - op_dtypes=sdt2) - assert_equal(i[0].dtype, np.dtype(sdt2)) - vals = [] - for x in i: - vals.append(np.array(x)) - x['a'] = x['b']+3 - assert_equal(vals, [np.array((2, 1), dtype=sdt2), - np.array((5, 4), dtype=sdt2)]) - assert_equal(a, np.array([(5, 2, None), (8, 5, None)], dtype=sdt1)) - - # struct type -> struct type (structured field gets discarded) - sdt1 = [('a', 'f4'), ('b', 'i8'), ('d', [('a', 'i2'), ('b', 'i4')])] - sdt2 = [('b', 'O'), ('a', 'f8')] - a = np.array([(1, 2, (0, 9)), (4, 5, (20, 21))], dtype=sdt1) - # New in 1.12: This behavior changes in 1.13, test for dep warning - with assert_warns(FutureWarning): - i = nditer(a, ['buffered', 'refs_ok'], ['readwrite'], - casting='unsafe', - op_dtypes=sdt2) - assert_equal(i[0].dtype, np.dtype(sdt2)) - vals = [] - for x in i: - vals.append(np.array(x)) - x['a'] = x['b']+3 - assert_equal(vals, [np.array((2, 1), dtype=sdt2), - np.array((5, 4), dtype=sdt2)]) - assert_equal(a, np.array([(5, 2, (0, 0)), (8, 5, (0, 0))], dtype=sdt1)) - - # struct type -> struct type (structured field w/ ref gets discarded) - sdt1 = [('a', 'f4'), ('b', 'i8'), ('d', [('a', 'i2'), ('b', 'O')])] - sdt2 = [('b', 'O'), ('a', 'f8')] - a = np.array([(1, 2, (0, 9)), (4, 5, (20, 21))], dtype=sdt1) - # New in 1.12: This behavior changes in 1.13, test for dep warning - with assert_warns(FutureWarning): - i = nditer(a, ['buffered', 'refs_ok'], ['readwrite'], - casting='unsafe', - op_dtypes=sdt2) - assert_equal(i[0].dtype, np.dtype(sdt2)) - vals = [] - for x in i: - vals.append(np.array(x)) - x['a'] = x['b']+3 - assert_equal(vals, [np.array((2, 1), dtype=sdt2), - np.array((5, 4), dtype=sdt2)]) - assert_equal(a, np.array([(5, 2, (0, None)), (8, 5, (0, None))], dtype=sdt1)) - - # struct type -> struct type back (structured field w/ ref gets discarded) - sdt1 = [('b', 'O'), ('a', 'f8')] - sdt2 = [('a', 'f4'), ('b', 'i8'), ('d', [('a', 'i2'), ('b', 'O')])] - a = np.array([(1, 2), (4, 5)], dtype=sdt1) - # New in 1.12: This behavior changes in 1.13, test for dep warning - with assert_warns(FutureWarning): - i = nditer(a, ['buffered', 'refs_ok'], ['readwrite'], - casting='unsafe', - op_dtypes=sdt2) - assert_equal(i[0].dtype, np.dtype(sdt2)) - vals = [] - for x in i: - vals.append(np.array(x)) - assert_equal(x['d'], np.array((0, None), dtype=[('a', 'i2'), ('b', 'O')])) - x['a'] = x['b']+3 - assert_equal(vals, [np.array((2, 1, (0, None)), dtype=sdt2), - np.array((5, 4, (0, None)), dtype=sdt2)]) - assert_equal(a, np.array([(1, 4), (4, 7)], dtype=sdt1)) + + assert_raises(ValueError, lambda : ( + nditer(a, ['buffered', 'refs_ok'], ['readwrite'], + casting='unsafe', + op_dtypes=sdt2))) + def test_iter_buffered_cast_subarray(): # Tests buffering of subarrays diff --git a/numpy/core/tests/test_records.py b/numpy/core/tests/test_records.py index d7714132b..27d35fa65 100644 --- a/numpy/core/tests/test_records.py +++ b/numpy/core/tests/test_records.py @@ -153,11 +153,6 @@ class TestFromrecords(object): assert_equal(r['c'].dtype.type, np.record) assert_equal(type(r['c']), np.recarray) - # suppress deprecation warning in 1.12 (remove in 1.13) - with assert_warns(FutureWarning): - assert_equal(r[['a', 'b']].dtype.type, np.record) - assert_equal(type(r[['a', 'b']]), np.recarray) - #and that it preserves subclasses (gh-6949) class C(np.recarray): pass @@ -334,15 +329,6 @@ class TestRecord(object): with assert_raises(ValueError): r.setfield([2,3], *r.dtype.fields['f']) - def test_out_of_order_fields(self): - """Ticket #1431.""" - # this test will be invalid in 1.13 - # suppress deprecation warning in 1.12 (remove in 1.13) - with assert_warns(FutureWarning): - x = self.data[['col1', 'col2']] - y = self.data[['col2', 'col1']] - assert_equal(x[0][0], y[0][1]) - def test_pickle_1(self): # Issue #1529 a = np.array([(1, [])], dtype=[('a', np.int32), ('b', np.int32, 0)]) @@ -371,8 +357,7 @@ class TestRecord(object): # https://github.com/numpy/numpy/issues/3256 ra = np.recarray((2,), dtype=[('x', object), ('y', float), ('z', int)]) - with assert_warns(FutureWarning): - ra[['x','y']] # TypeError? + ra[['x','y']] # TypeError? def test_record_scalar_setitem(self): # https://github.com/numpy/numpy/issues/3561 diff --git a/numpy/lib/tests/test_io.py b/numpy/lib/tests/test_io.py index f2fd37230..e6b1eac7a 100644 --- a/numpy/lib/tests/test_io.py +++ b/numpy/lib/tests/test_io.py @@ -1178,12 +1178,12 @@ M 33 21.99 conv = {0: int, 1: int, 2: int, 3: lambda r: dmap[r.decode()]} test = np.recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',', names=None, converters=conv) - control = np.rec.array([[1,5,-1,0], [2,8,-1,1], [3,3,-2,3]], dtype=dtyp) + control = np.rec.array([(1,5,-1,0), (2,8,-1,1), (3,3,-2,3)], dtype=dtyp) assert_equal(test, control) dtyp = [('e1','i4'),('e2','i4'),('n', 'i1')] test = np.recfromcsv(TextIO(dstr,), dtype=dtyp, delimiter=',', usecols=(0,1,3), names=None, converters=conv) - control = np.rec.array([[1,5,0], [2,8,1], [3,3,3]], dtype=dtyp) + control = np.rec.array([(1,5,0), (2,8,1), (3,3,3)], dtype=dtyp) assert_equal(test, control) def test_dtype_with_object(self): diff --git a/numpy/ma/tests/test_core.py b/numpy/ma/tests/test_core.py index 5da19f9ca..c06a42de3 100644 --- a/numpy/ma/tests/test_core.py +++ b/numpy/ma/tests/test_core.py @@ -1760,16 +1760,12 @@ class TestFillingValues(object): assert_equal(fval.item(), [-999, -12345678.9, b"???"]) #.....Using a flexible type w/ a different type shouldn't matter - # BEHAVIOR in 1.5 and earlier: match structured types by position - #fill_val = np.array((-999, -12345678.9, "???"), - # dtype=[("A", int), ("B", float), ("C", "|S3")]) - # BEHAVIOR in 1.6 and later: match structured types by name - fill_val = np.array(("???", -999, -12345678.9), - dtype=[("c", "|S3"), ("a", int), ("b", float), ]) - # suppress deprecation warning in 1.12 (remove in 1.13) - with assert_warns(FutureWarning): - fval = _check_fill_value(fill_val, ndtype) - assert_(isinstance(fval, ndarray)) + # BEHAVIOR in 1.5 and earlier, and 1.13 and later: match structured + # types by position + fill_val = np.array((-999, -12345678.9, "???"), + dtype=[("A", int), ("B", float), ("C", "|S3")]) + fval = _check_fill_value(fill_val, ndtype) + self.assertTrue(isinstance(fval, ndarray)) assert_equal(fval.item(), [-999, -12345678.9, b"???"]) #.....Using an object-array shouldn't matter either |