def assert_equal(actual,desired,err_msg=''): """Asserts that two items are equal. """ # Case #1: dictionary ..... if isinstance(desired, dict): assert isinstance(actual, dict), repr(type(actual)) assert_equal(len(actual),len(desired),err_msg) for k,i in desired.items(): assert actual.has_key(k), repr(k) assert_equal(actual[k], desired[k], 'key=%r\n%s' % (k,err_msg)) return # Case #2: lists ..... if isinstance(desired, (list,tuple)) and isinstance(actual, (list,tuple)): return _assert_equal_on_sequences(actual, desired, err_msg='') if not (isinstance(actual, ndarray) or isinstance(desired, ndarray)): msg = build_err_msg([actual, desired], err_msg,) assert desired == actual, msg return # Case #4. arrays or equivalent if ((actual is masked) and not (desired is masked)) or \ ((desired is masked) and not (actual is masked)): msg = build_err_msg([actual, desired], err_msg, header='', names=('x', 'y')) raise ValueError(msg) actual = N.array(actual, copy=False, subok=True) desired = N.array(desired, copy=False, subok=True) if actual.dtype.char in "OS" and desired.dtype.char in "OS": return _assert_equal_on_sequences(actual.tolist(), desired.tolist(), err_msg='') return assert_array_equal(actual, desired, err_msg)
def assert_array_compare(self, comparison, x, y, err_msg='', header='', fill_value=True): """Asserts that a comparison relation between two masked arrays is satisfied elementwise.""" xf = self.filled(x) yf = self.filled(y) m = self.mask_or(self.getmask(x), self.getmask(y)) x = self.filled(self.masked_array(xf, mask=m), fill_value) y = self.filled(self.masked_array(yf, mask=m), fill_value) if (x.dtype.char != "O"): x = x.astype(float_) if isinstance(x, numpy.ndarray) and x.size > 1: x[numpy.isnan(x)] = 0 elif numpy.isnan(x): x = 0 if (y.dtype.char != "O"): y = y.astype(float_) if isinstance(y, numpy.ndarray) and y.size > 1: y[numpy.isnan(y)] = 0 elif numpy.isnan(y): y = 0 try: cond = (x.shape == () or y.shape == ()) or x.shape == y.shape if not cond: msg = build_err_msg([x, y], err_msg + '\n(shapes %s, %s mismatch)' % (x.shape, y.shape), header=header, names=('x', 'y')) assert cond, msg val = comparison(x, y) if m is not self.nomask and fill_value: val = self.masked_array(val, mask=m) if isinstance(val, bool): cond = val reduced = [0] else: reduced = val.ravel() cond = reduced.all() reduced = reduced.tolist() if not cond: match = 100 - 100.0 * reduced.count(1) / len(reduced) msg = build_err_msg([x, y], err_msg + '\n(mismatch %s%%)' % (match, ), header=header, names=('x', 'y')) assert cond, msg except ValueError: msg = build_err_msg([x, y], err_msg, header=header, names=('x', 'y')) raise ValueError(msg)
def assert_array_compare(comparison, x, y, err_msg='', header='', fill_value=True): """Asserts that a comparison relation between two masked arrays is satisfied elementwise.""" xf = filled(x) yf = filled(y) m = mask_or(getmask(x), getmask(y)) x = masked_array(xf, copy=False, subok=False, mask=m).filled(fill_value) y = masked_array(yf, copy=False, subok=False, mask=m).filled(fill_value) if ((x is masked) and not (y is masked)) or \ ((y is masked) and not (x is masked)): msg = build_err_msg([x, y], err_msg, header=header, names=('x', 'y')) raise ValueError(msg) if (x.dtype.char != "O") and (x.dtype.char != "S"): x = x.astype(float_) if isinstance(x, N.ndarray) and x.size > 1: x[N.isnan(x)] = 0 elif N.isnan(x): x = 0 if (y.dtype.char != "O") and (y.dtype.char != "S"): y = y.astype(float_) if isinstance(y, N.ndarray) and y.size > 1: y[N.isnan(y)] = 0 elif N.isnan(y): y = 0 try: cond = (x.shape==() or y.shape==()) or x.shape == y.shape if not cond: msg = build_err_msg([x, y], err_msg + '\n(shapes %s, %s mismatch)' % (x.shape, y.shape), header=header, names=('x', 'y')) assert cond, msg val = comparison(x,y) if m is not nomask and fill_value: val = masked_array(val, mask=m, copy=False) if isinstance(val, bool): cond = val reduced = [0] else: reduced = val.ravel() cond = reduced.all() reduced = reduced.tolist() if not cond: match = 100-100.0*reduced.count(1)/len(reduced) msg = build_err_msg([x, y], err_msg + '\n(mismatch %s%%)' % (match,), header=header, names=('x', 'y')) assert cond, msg except ValueError: msg = build_err_msg([x, y], err_msg, header=header, names=('x', 'y')) raise ValueError(msg)
def assert_array_compare(self, comparison, x, y, err_msg='', header='', fill_value=True): """ Assert that a comparison of two masked arrays is satisfied elementwise. """ xf = self.filled(x) yf = self.filled(y) m = self.mask_or(self.getmask(x), self.getmask(y)) x = self.filled(self.masked_array(xf, mask=m), fill_value) y = self.filled(self.masked_array(yf, mask=m), fill_value) if (x.dtype.char != "O"): x = x.astype(float_) if isinstance(x, np.ndarray) and x.size > 1: x[np.isnan(x)] = 0 elif np.isnan(x): x = 0 if (y.dtype.char != "O"): y = y.astype(float_) if isinstance(y, np.ndarray) and y.size > 1: y[np.isnan(y)] = 0 elif np.isnan(y): y = 0 try: cond = (x.shape == () or y.shape == ()) or x.shape == y.shape if not cond: msg = build_err_msg([x, y], err_msg + '\n(shapes %s, %s mismatch)' % (x.shape, y.shape), header=header, names=('x', 'y')) assert cond, msg val = comparison(x, y) if m is not self.nomask and fill_value: val = self.masked_array(val, mask=m) if isinstance(val, bool): cond = val reduced = [0] else: reduced = val.ravel() cond = reduced.all() reduced = reduced.tolist() if not cond: match = 100-100.0*reduced.count(1)/len(reduced) msg = build_err_msg([x, y], err_msg + '\n(mismatch %s%%)' % (match,), header=header, names=('x', 'y')) assert cond, msg except ValueError: msg = build_err_msg([x, y], err_msg, header=header, names=('x', 'y')) raise ValueError(msg)
def assert_almost_equal(actual,desired,decimal=7,err_msg=''): """Asserts that two items are almost equal. The test is equivalent to abs(desired-actual) < 0.5 * 10**(-decimal) """ if isinstance(actual, N.ndarray) or isinstance(desired, N.ndarray): return assert_array_almost_equal(actual, desired, decimal, err_msg) msg = build_err_msg([actual, desired], err_msg) assert round(abs(desired - actual),decimal) == 0, msg
def assert_almost_equal(actual, desired, decimal=7, err_msg=''): """Asserts that two items are almost equal. The test is equivalent to abs(desired-actual) < 0.5 * 10**(-decimal) """ if isinstance(actual, N.ndarray) or isinstance(desired, N.ndarray): return assert_array_almost_equal(actual, desired, decimal, err_msg) msg = build_err_msg([actual, desired], err_msg) assert round(abs(desired - actual), decimal) == 0, msg
def assert_almost_identical(x, y, tol=1e-2): """ """ unequal_pos = np.where(x != y) error = len(unequal_pos[0]) / x.size try: assert (error < tol) except AssertionError: msg = build_err_msg([error, tol], 'Not almost identical') raise AssertionError(msg)
def assert_equal(actual,desired,err_msg=''): """Asserts that two items are equal. """ # Case #1: dictionary ..... if isinstance(desired, dict): assert isinstance(actual, dict), repr(type(actual)) assert_equal(len(actual),len(desired),err_msg) for k,i in desired.items(): assert k in actual, repr(k) assert_equal(actual[k], desired[k], 'key=%r\n%s' % (k,err_msg)) return # Case #2: lists ..... if isinstance(desired, (list,tuple)) and isinstance(actual, (list,tuple)): return _assert_equal_on_sequences(actual, desired, err_msg='') if not (isinstance(actual, ndarray) or isinstance(desired, ndarray)): msg = build_err_msg([actual, desired], err_msg,) assert desired == actual, msg return # Case #4. arrays or equivalent if ((actual is masked) and not (desired is masked)) or \ ((desired is masked) and not (actual is masked)): msg = build_err_msg([actual, desired], err_msg, header='', names=('x', 'y')) raise ValueError(msg) actual = np.array(actual, copy=False, subok=True) desired = np.array(desired, copy=False, subok=True) (actual_dtype, desired_dtype) = (actual.dtype, desired.dtype) if actual_dtype.char == "S" and desired_dtype.char == "S": return _assert_equal_on_sequences(actual.tolist(), desired.tolist(), err_msg='') elif actual_dtype.char in "OV" and desired_dtype.char in "OV": if (actual_dtype != desired_dtype) and actual_dtype: msg = build_err_msg([actual_dtype, desired_dtype], err_msg, header='', names=('actual', 'desired')) raise ValueError(msg) return _assert_equal_on_sequences(actual.tolist(), desired.tolist(), err_msg='') return assert_array_equal(actual, desired, err_msg)
def fail_if_equal(actual,desired,err_msg='',): """Raises an assertion error if two items are equal. """ if isinstance(desired, dict): assert isinstance(actual, dict), repr(type(actual)) fail_if_equal(len(actual),len(desired),err_msg) for k,i in desired.items(): assert actual.has_key(k), repr(k) fail_if_equal(actual[k], desired[k], 'key=%r\n%s' % (k,err_msg)) return if isinstance(desired, (list,tuple)) and isinstance(actual, (list,tuple)): fail_if_equal(len(actual),len(desired),err_msg) for k in range(len(desired)): fail_if_equal(actual[k], desired[k], 'item=%r\n%s' % (k,err_msg)) return if isinstance(actual, N.ndarray) or isinstance(desired, N.ndarray): return fail_if_array_equal(actual, desired, err_msg) msg = build_err_msg([actual, desired], err_msg) assert desired != actual, msg
def fail_if_equal(actual,desired,err_msg='',): """Raises an assertion error if two items are equal. """ if isinstance(desired, dict): assert isinstance(actual, dict), repr(type(actual)) fail_if_equal(len(actual),len(desired),err_msg) for k,i in desired.items(): assert k in actual, repr(k) fail_if_equal(actual[k], desired[k], 'key=%r\n%s' % (k,err_msg)) return if isinstance(desired, (list,tuple)) and isinstance(actual, (list,tuple)): fail_if_equal(len(actual),len(desired),err_msg) for k in range(len(desired)): fail_if_equal(actual[k], desired[k], 'item=%r\n%s' % (k,err_msg)) return if isinstance(actual, np.ndarray) or isinstance(desired, np.ndarray): return fail_if_array_equal(actual, desired, err_msg) msg = build_err_msg([actual, desired], err_msg) assert desired != actual, msg
def assert_array_compare(comparison, x, y, err_msg='', verbose=True, header='', fill_value=True): """Asserts that a comparison relation between two masked arrays is satisfied elementwise.""" # Fill the data first xf = filled(x) yf = filled(y) # Allocate a common mask and refill m = mask_or(getmask(x), getmask(y)) x = masked_array(xf, copy=False, mask=m) y = masked_array(yf, copy=False, mask=m) if ((x is masked) and not (y is masked)) or \ ((y is masked) and not (x is masked)): msg = build_err_msg([x, y], err_msg=err_msg, verbose=verbose, header=header, names=('x', 'y')) raise ValueError(msg) # OK, now run the basic tests on filled versions return utils.assert_array_compare(comparison, x.filled(fill_value), y.filled(fill_value), err_msg=err_msg, verbose=verbose, header=header)
def _build_err_msg(): header = ('Arrays are not almost equal to %d decimals' % decimal) return build_err_msg([actual, desired], err_msg, verbose=verbose, header=header)