Assert that a comparison of two masked arrays is satisfied elementwise.
(self, comparison, x, y, err_msg='', header='',
fill_value=True)
| 46 | self.testnames = [] |
| 47 | |
| 48 | def assert_array_compare(self, comparison, x, y, err_msg='', header='', |
| 49 | fill_value=True): |
| 50 | """ |
| 51 | Assert that a comparison of two masked arrays is satisfied elementwise. |
| 52 | |
| 53 | """ |
| 54 | xf = self.filled(x) |
| 55 | yf = self.filled(y) |
| 56 | m = self.mask_or(self.getmask(x), self.getmask(y)) |
| 57 | |
| 58 | x = self.filled(self.masked_array(xf, mask=m), fill_value) |
| 59 | y = self.filled(self.masked_array(yf, mask=m), fill_value) |
| 60 | if (x.dtype.char != "O"): |
| 61 | x = x.astype(float_) |
| 62 | if isinstance(x, np.ndarray) and x.size > 1: |
| 63 | x[np.isnan(x)] = 0 |
| 64 | elif np.isnan(x): |
| 65 | x = 0 |
| 66 | if (y.dtype.char != "O"): |
| 67 | y = y.astype(float_) |
| 68 | if isinstance(y, np.ndarray) and y.size > 1: |
| 69 | y[np.isnan(y)] = 0 |
| 70 | elif np.isnan(y): |
| 71 | y = 0 |
| 72 | try: |
| 73 | cond = (x.shape == () or y.shape == ()) or x.shape == y.shape |
| 74 | if not cond: |
| 75 | msg = build_err_msg([x, y], |
| 76 | err_msg |
| 77 | + f'\n(shapes {x.shape}, {y.shape} mismatch)', |
| 78 | header=header, |
| 79 | names=('x', 'y')) |
| 80 | assert cond, msg |
| 81 | val = comparison(x, y) |
| 82 | if m is not self.nomask and fill_value: |
| 83 | val = self.masked_array(val, mask=m) |
| 84 | if isinstance(val, bool): |
| 85 | cond = val |
| 86 | reduced = [0] |
| 87 | else: |
| 88 | reduced = val.ravel() |
| 89 | cond = reduced.all() |
| 90 | reduced = reduced.tolist() |
| 91 | if not cond: |
| 92 | match = 100-100.0*reduced.count(1)/len(reduced) |
| 93 | msg = build_err_msg([x, y], |
| 94 | err_msg |
| 95 | + '\n(mismatch %s%%)' % (match,), |
| 96 | header=header, |
| 97 | names=('x', 'y')) |
| 98 | assert cond, msg |
| 99 | except ValueError as e: |
| 100 | msg = build_err_msg([x, y], err_msg, header=header, names=('x', 'y')) |
| 101 | raise ValueError(msg) from e |
| 102 | |
| 103 | def assert_array_equal(self, x, y, err_msg=''): |
| 104 | """ |