@@ -1335,32 +1335,96 @@ def test_eq_on_structured(self):
1335
1335
ndtype = [('A' , int ), ('B' , int )]
1336
1336
a = array ([(1 , 1 ), (2 , 2 )], mask = [(0 , 1 ), (0 , 0 )], dtype = ndtype )
1337
1337
test = (a == a )
1338
- assert_equal (test , [True , True ])
1338
+ assert_equal (test .data , [True , True ])
1339
+ assert_equal (test .mask , [False , False ])
1340
+ test = (a == a [0 ])
1341
+ assert_equal (test .data , [True , False ])
1339
1342
assert_equal (test .mask , [False , False ])
1340
1343
b = array ([(1 , 1 ), (2 , 2 )], mask = [(1 , 0 ), (0 , 0 )], dtype = ndtype )
1341
1344
test = (a == b )
1342
- assert_equal (test , [False , True ])
1345
+ assert_equal (test .data , [False , True ])
1346
+ assert_equal (test .mask , [True , False ])
1347
+ test = (a [0 ] == b )
1348
+ assert_equal (test .data , [False , False ])
1343
1349
assert_equal (test .mask , [True , False ])
1344
1350
b = array ([(1 , 1 ), (2 , 2 )], mask = [(0 , 1 ), (1 , 0 )], dtype = ndtype )
1345
1351
test = (a == b )
1346
- assert_equal (test , [True , False ])
1352
+ assert_equal (test . data , [True , True ])
1347
1353
assert_equal (test .mask , [False , False ])
1354
+ # complicated dtype, 2-dimensional array.
1355
+ ndtype = [('A' , int ), ('B' , [('BA' , int ), ('BB' , int )])]
1356
+ a = array ([[(1 , (1 , 1 )), (2 , (2 , 2 ))],
1357
+ [(3 , (3 , 3 )), (4 , (4 , 4 ))]],
1358
+ mask = [[(0 , (1 , 0 )), (0 , (0 , 1 ))],
1359
+ [(1 , (0 , 0 )), (1 , (1 , 1 ))]], dtype = ndtype )
1360
+ test = (a [0 , 0 ] == a )
1361
+ assert_equal (test .data , [[True , False ], [False , False ]])
1362
+ assert_equal (test .mask , [[False , False ], [False , True ]])
1348
1363
1349
1364
def test_ne_on_structured (self ):
1350
1365
# Test the equality of structured arrays
1351
1366
ndtype = [('A' , int ), ('B' , int )]
1352
1367
a = array ([(1 , 1 ), (2 , 2 )], mask = [(0 , 1 ), (0 , 0 )], dtype = ndtype )
1353
1368
test = (a != a )
1354
- assert_equal (test , [False , False ])
1369
+ assert_equal (test .data , [False , False ])
1370
+ assert_equal (test .mask , [False , False ])
1371
+ test = (a != a [0 ])
1372
+ assert_equal (test .data , [False , True ])
1355
1373
assert_equal (test .mask , [False , False ])
1356
1374
b = array ([(1 , 1 ), (2 , 2 )], mask = [(1 , 0 ), (0 , 0 )], dtype = ndtype )
1357
1375
test = (a != b )
1358
- assert_equal (test , [True , False ])
1376
+ assert_equal (test .data , [True , False ])
1377
+ assert_equal (test .mask , [True , False ])
1378
+ test = (a [0 ] != b )
1379
+ assert_equal (test .data , [True , True ])
1359
1380
assert_equal (test .mask , [True , False ])
1360
1381
b = array ([(1 , 1 ), (2 , 2 )], mask = [(0 , 1 ), (1 , 0 )], dtype = ndtype )
1361
1382
test = (a != b )
1362
- assert_equal (test , [False , True ])
1383
+ assert_equal (test . data , [False , False ])
1363
1384
assert_equal (test .mask , [False , False ])
1385
+ # complicated dtype, 2-dimensional array.
1386
+ ndtype = [('A' , int ), ('B' , [('BA' , int ), ('BB' , int )])]
1387
+ a = array ([[(1 , (1 , 1 )), (2 , (2 , 2 ))],
1388
+ [(3 , (3, 3 )), (4 , (4 , 4 ))]],
1389
+ mask = [[(0 , (1 , 0 )), (0 , (0 , 1 ))],
1390
+ [(1 , (0 , 0 )), (1 , (1 , 1 ))]], dtype = ndtype )
1391
+ test = (a [0 , 0 ] != a )
1392
+ assert_equal (test .data , [[False , True ], [True , True ]])
1393
+ assert_equal (test .mask , [[False , False ], [False , True ]])
1394
+
1395
+ def test_eq_ne_structured_extra (self ):
1396
+ # ensure simple examples are symmetric and make sense.
1397
+ # from https://github.com/numpy/numpy/pull/8590#discussion_r101126465
1398
+ dt = np .dtype ('i4,i4' )
1399
+ for m1 in (mvoid ((1 , 2 ), mask = (0 , 0 ), dtype = dt ),
1400
+ mvoid ((1 , 2 ), mask = (0 , 1 ), dtype = dt ),
1401
+ mvoid ((1 , 2 ), mask = (1 , 0 ), dtype = dt ),
1402
+ mvoid ((1 , 2 ), mask = (1 , 1 ), dtype = dt )):
1403
+ ma1 = m1 .view (MaskedArray )
1404
+ r1 = ma1 .view ('2i4' )
1405
+ for m2 in (np .array ((1 , 1 ), dtype = dt ),
1406
+ mvoid ((1 , 1 ), dtype = dt ),
1407
+ mvoid ((1 , 0 ), mask = (0 , 1 ), dtype = dt ),
1408
+ mvoid ((3 , 2 ), mask = (0 , 1 ), dtype = dt )):
1409
+ ma2 = m2 .view (MaskedArray )
1410
+ r2 = ma2 .view ('2i4' )
1411
+ eq_expected = (r1 == r2 ).all ()
1412
+ assert_equal (m1 == m2 , eq_expected )
1413
+ assert_equal (m2 == m1 , eq_expected )
1414
+ assert_equal (ma1 == m2 , eq_expected )
1415
+ assert_equal (m1 == ma2 , eq_expected )
1416
+ assert_equal (ma1 == ma2 , eq_expected )
1417
+ # Also check it is the same if we do it element by element.
1418
+ el_by_el = [m1 [name ] == m2 [name ] for name in dt .names ]
1419
+ assert_equal (array (el_by_el , dtype = bool ).all (), eq_expected )
1420
+ ne_expected = (r1 != r2 ).any ()
1421
+ assert_equal (m1 != m2 , ne_expected )
1422
+ assert_equal (m2 != m1 , ne_expected )
1423
+ assert_equal (ma1 != m2 , ne_expected )
1424
+ assert_equal (m1 != ma2 , ne_expected )
1425
+ assert_equal (ma1 != ma2 , ne_expected )
1426
+ el_by_el = [m1 [name ] != m2 [name ] for name in dt .names ]
1427
+ assert_equal (array (el_by_el , dtype = bool ).any (), ne_expected )
1364
1428
1365
1429
def test_eq_with_None (self ):
1366
1430
# Really, comparisons with None should not be done, but check them
@@ -1393,6 +1457,22 @@ def test_eq_with_scalar(self):
1393
1457
assert_equal (a == 0 , False )
1394
1458
assert_equal (a != 1 , False )
1395
1459
assert_equal (a != 0 , True )
1460
+ b = array (1 , mask = True )
1461
+ assert_equal (b == 0 , masked )
1462
+ assert_equal (b == 1 , masked )
1463
+ assert_equal (b != 0 , masked )
1464
+ assert_equal (b != 1 , masked )
1465
+
1466
+ def test_eq_different_dimensions (self ):
1467
+ m1 = array ([1 , 1 ], mask = [0 , 1 ])
1468
+ # test comparison with both masked and regular arrays.
1469
+ for m2 in (array ([[0 , 1 ], [1 , 2 ]]),
1470
+ np .array ([[0 , 1 ], [1 , 2 ]])):
1471
+ test = (m1 == m2 )
1472
+ assert_equal (test .data , [[False , False ],
1473
+ [True , False ]])
1474
+ assert_equal (test .mask , [[False , True ],
1475
+ [False , True ]])
1396
1476
1397
1477
def test_numpyarithmetics (self ):
1398
1478
# Check that the mask is not back-propagated when using numpy functions
@@ -3978,7 +4058,15 @@ def test_make_mask(self):
3978
4058
test = make_mask (mask , dtype = mask .dtype )
3979
4059
assert_equal (test .dtype , bdtype )
3980
4060
assert_equal (test , np .array ([(0 , 0 ), (0 , 1 )], dtype = bdtype ))
3981
-
4061
+ # Ensure this also works for void
4062
+ mask = np .array ((False , True ), dtype = '?,?' )[()]
4063
+ assert_ (isinstance (mask , np .void ))
4064
+ test = make_mask (mask , dtype = mask .dtype )
4065
+ assert_equal (test , mask )
4066
+ assert_ (test is not mask )
4067
+ mask = np .array ((0 , 1 ), dtype = 'i4,i4' )[()]
4068
+ test2 = make_mask (mask , dtype = mask .dtype )
4069
+ assert_equal (test2 , test )
3982
4070
# test that nomask is returned when m is nomask.
3983
4071
bools = [True , False ]
3984
4072
dtypes = [MaskType , np .float ]
@@ -3987,7 +4075,6 @@
5FA9
def test_make_mask(self):
3987
4075
res = make_mask (nomask , copy = cpy , shrink = shr , dtype = dt )
3988
4076
assert_ (res is nomask , msgformat % (cpy , shr , dt ))
3989
4077
3990
-
3991
4078
def test_mask_or (self ):
3992
4079
# Initialize
3993
4080
mtype = [('a' , np .bool ), ('b' , np .bool )]
0 commit comments