numpy.testing.utils

assert_(val, msg='') Assert that works in release mode. assert_almost_equal(actual, desired, decimal=7, err_msg='', verbose=True) Raise an assertion if two items are not equal up to desired precision. The test is equivalent to abs(desired-actual) < 0.5 * 10**(-decimal) Given two objects (numbers or ndarrays), check that all elements of these objects are almost equal. An exception is raised at conflicting values. For ndarrays this delegates to assert_array_almost_equal Parameters ---------- actual : number or ndarray The object to check. desired : number or ndarray The expected object. decimal : integer (decimal=7) desired precision err_msg : string The error message to be printed in case of failure. verbose : bool If True, the conflicting values are appended to the error message. Raises ------ AssertionError If actual and desired are not equal up to specified precision. See Also -------- assert_array_almost_equal: compares array_like objects assert_equal: tests objects for equality Examples -------- >>> npt.assert_almost_equal(2.3333333333333, 2.33333334) >>> npt.assert_almost_equal(2.3333333333333, 2.33333334, decimal=10) ... <type 'exceptions.AssertionError'>: Items are not equal: ACTUAL: 2.3333333333333002 DESIRED: 2.3333333399999998 >>> npt.assert_almost_equal(np.array([1.0,2.3333333333333]), np.array([1.0,2.33333334]), decimal=9) ... <type 'exceptions.AssertionError'>: Arrays are not almost equal <BLANKLINE> (mismatch 50.0%) x: array([ 1. , 2.33333333]) y: array([ 1. , 2.33333334]) assert_approx_equal(actual, desired, significant=7, err_msg='', verbose=True) Raise an assertion if two items are not equal up to significant digits. Given two numbers, check that they are approximately equal. Approximately equal is defined as the number of significant digits that agree. Parameters ---------- actual : number The object to check. desired : number The expected object. significant : integer (significant=7) desired precision err_msg : string The error message to be printed in case of failure. verbose : bool If True, the conflicting values are appended to the error message. Raises ------ AssertionError If actual and desired are not equal up to specified precision. See Also -------- assert_almost_equal: compares objects by decimals assert_array_almost_equal: compares array_like objects by decimals assert_equal: tests objects for equality Examples -------- >>> np.testing.assert_approx_equal(0.12345677777777e-20, 0.1234567e-20) >>> np.testing.assert_approx_equal(0.12345670e-20, 0.12345671e-20, significant=8) >>> np.testing.assert_approx_equal(0.12345670e-20, 0.12345672e-20, significant=8) ... <type 'exceptions.AssertionError'>: Items are not equal to 8 significant digits: ACTUAL: 1.234567e-021 DESIRED: 1.2345672000000001e-021 the evaluated condition that raises the exception is >>> abs(0.12345670e-20/1e-21 - 0.12345672e-20/1e-21) >= 10**-(8-1) True assert_array_almost_equal(x, y, decimal=6, err_msg='', verbose=True) Raise an assertion if two objects are not equal up to desired precision. The test verifies identical shapes and verifies values with abs(desired-actual) < 0.5 * 10**(-decimal) Given two array_like objects, check that the shape is equal and all elements of these objects are almost equal. An exception is raised at shape mismatch or conflicting values. In contrast to the standard usage in numpy, NaNs are compared like numbers, no assertion is raised if both objects have NaNs in the same positions. Parameters ---------- x : array_like The actual object to check. y : array_like The desired, expected object. decimal : integer (decimal=6) desired precision err_msg : string The error message to be printed in case of failure. verbose : bool If True, the conflicting values are appended to the error message. Raises ------ AssertionError If actual and desired are not equal up to specified precision. See Also -------- assert_almost_equal: simple version for comparing numbers assert_array_equal: tests objects for equality Examples -------- the first assert does not raise an exception >>> np.testing.assert_array_almost_equal([1.0,2.333,np.nan], [1.0,2.333,np.nan]) >>> np.testing.assert_array_almost_equal([1.0,2.33333,np.nan], [1.0,2.33339,np.nan], decimal=5) ... <type 'exceptions.AssertionError'>: AssertionError: Arrays are not almost equal <BLANKLINE> (mismatch 50.0%) x: array([ 1. , 2.33333, NaN]) y: array([ 1. , 2.33339, NaN]) >>> np.testing.assert_array_almost_equal([1.0,2.33333,np.nan], [1.0,2.33333, 5], decimal=5) <type 'exceptions.ValueError'>: ValueError: Arrays are not almost equal x: array([ 1. , 2.33333, NaN]) y: array([ 1. , 2.33333, 5. ]) assert_array_compare(comparison, x, y, err_msg='', verbose=True, header='') assert_array_equal(x, y, err_msg='', verbose=True) Raise an assertion if two array_like objects are not equal. Given two array_like objects, check that the shape is equal and all elements of these objects are equal. An exception is raised at shape mismatch or conflicting values. In contrast to the standard usage in numpy, NaNs are compared like numbers, no assertion is raised if both objects have NaNs in the same positions. The usual caution for verifying equality with floating point numbers is advised. Parameters ---------- x : array_like The actual object to check. y : array_like The desired, expected object. err_msg : string The error message to be printed in case of failure. verbose : bool If True, the conflicting values are appended to the error message. Raises ------ AssertionError If actual and desired objects are not equal. See Also -------- assert_array_almost_equal: test objects for equality up to precision assert_equal: tests objects for equality Examples -------- the first assert does not raise an exception >>> np.testing.assert_array_equal([1.0,2.33333,np.nan], [np.exp(0),2.33333, np.nan]) assert fails with numerical inprecision with floats >>> np.testing.assert_array_equal([1.0,np.pi,np.nan], [1, np.sqrt(np.pi)**2, np.nan]) ... <type 'exceptions.ValueError'>: AssertionError: Arrays are not equal <BLANKLINE> (mismatch 50.0%) x: array([ 1. , 3.14159265, NaN]) y: array([ 1. , 3.14159265, NaN]) use assert_array_almost_equal for these cases instead >>> np.testing.assert_array_almost_equal([1.0,np.pi,np.nan], [1, np.sqrt(np.pi)**2, np.nan], decimal=15) assert_array_less(x, y, err_msg='', verbose=True) Raise an assertion if two array_like objects are not ordered by less than. Given two array_like objects, check that the shape is equal and all elements of the first object are strictly smaller than those of the second object. An exception is raised at shape mismatch or incorrectly ordered values. Shape mismatch does not raise if an object has zero dimension. In contrast to the standard usage in numpy, NaNs are compared, no assertion is raised if both objects have NaNs in the same positions. Parameters ---------- x : array_like The smaller object to check. y : array_like The larger object to compare. err_msg : string The error message to be printed in case of failure. verbose : bool If True, the conflicting values are appended to the error message. Raises ------ AssertionError If actual and desired objects are not equal. See Also -------- assert_array_equal: tests objects for equality assert_array_almost_equal: test objects for equality up to precision Examples -------- >>> np.testing.assert_array_less([1.0, 1.0, np.nan], [1.1, 2.0, np.nan]) >>> np.testing.assert_array_less([1.0, 1.0, np.nan], [1, 2.0, np.nan]) ... <type 'exceptions.ValueError'>: Arrays are not less-ordered (mismatch 50.0%) x: array([ 1., 1., NaN]) y: array([ 1., 2., NaN]) >>> np.testing.assert_array_less([1.0, 4.0], 3) ... <type 'exceptions.ValueError'>: Arrays are not less-ordered (mismatch 50.0%) x: array([ 1., 4.]) y: array(3) >>> np.testing.assert_array_less([1.0, 2.0, 3.0], [4]) ... <type 'exceptions.ValueError'>: Arrays are not less-ordered (shapes (3,), (1,) mismatch) x: array([ 1., 2., 3.]) y: array([4]) assert_equal(actual, desired, err_msg='', verbose=True) Raise an assertion if two objects are not equal. Given two objects (lists, tuples, dictionaries or numpy arrays), check that all elements of these objects are equal. An exception is raised at the first conflicting values. Parameters ---------- actual : list, tuple, dict or ndarray The object to check. desired : list, tuple, dict or ndarray The expected object. err_msg : string The error message to be printed in case of failure. verbose : bool If True, the conflicting values are appended to the error message. Raises ------ AssertionError If actual and desired are not equal. Examples -------- >>> np.testing.assert_equal([4,5], [4,6]) ... <type 'exceptions.AssertionError'>: Items are not equal: item=1 ACTUAL: 5 DESIRED: 6 assert_raises(*args, **kwargs) assert_raises(excecption_class, callable, *args, **kwargs) Fail unless an exception of class exception_class is thrown by callable when invoked with arguments args and keyword arguments kwargs. If a different type of exception is thrown, it will not be caught, and the test case will be deemed to have suffered an error, exactly as for an unexpected exception. assert_string_equal(actual, desired) build_err_msg(arrays, err_msg, header='Items are not equal:', verbose=True, names=('ACTUAL', 'DESIRED')) decorate_methods(cls, decorator, testmatch=None) Apply decorator to all methods in class matching testmatch Parameters ---------- cls : class Class to decorate methods for decorator : function Decorator to apply to methods testmatch : compiled regexp or string to compile to regexp Decorators are applied if testmatch.search(methodname) is not None. Default value is re.compile(r'(?:^|[\b_\.%s-])[Tt]est' % os.sep) (the default for nose) jiffies(_proc_pid_stat='/proc/17578/stat', _load_time=[]) Return number of jiffies (1/100ths of a second) that this process has been scheduled in user mode. See man 5 proc. measure(code_str, times=1, label=None) Return elapsed time for executing code_str in the namespace of the caller for given times. memusage(_proc_pid_stat='/proc/17578/stat') Return virtual memory size in bytes of the running python. print_assert_equal(test_string, actual, desired) raises(*args, **kwargs) rand(*args) Returns an array of random numbers with the given shape. This only uses the standard library, so it is useful for testing purposes. rundocs(filename=None) Run doc string tests found in filename. runstring(astr, dict)

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • 目标检测的定义

    目标检测与识别是指从一幅场景(图片)中找出目标,包括检测(where)和识别(what)两个过程。任务的难点在于待检测区域候选的提取与识别,所以,任务的大框架为...

    于小勇
  • Selective Search for Object Recognition 论文笔记【图片目标分割】

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。 ...

    于小勇
  • Stereo R-CNN based 3D Object Detection for Autonomous Driving

    版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。

    于小勇
  • 「R」数据可视化21: Edge Bunding图

    本文作者蒋刘一琦,自嘲是一个有艺术追求的生信狗,毕业于浙江大学生物信息学专业,目前在复旦大学就读研究生,研究方向为宏基因组。

    王诗翔呀
  • Python多线程端口扫描工具

        昨晚今晚写了两晚,总算把Py Port Scanner 写完了,姑且称之为0.1版本,算是一个Python多线程端口扫描工具。

    phith0n
  • Extjs-lesson3

    Ext.js 系列课程笔记「组件」更多精彩文章请关注公众号『Pythonnote』或者『全栈技术精选』

    小闫同学啊
  • 超详细,Windows系统搭建Flink官方练习环境

    如何快速的投入到Flink的学习当中,很多人在搭建环境过程中浪费了太多的时间。一套一劳永逸的本机Flink开发环境可以让我们快速的投入到Flink的学习中去,将...

    实时计算
  • js中判断由字符串from和to代表的两个日期的先后关系

    源哥
  • 腾讯产品经理现身说法:to b的产品经理和to c产品经理区别

    用户1756920
  • 如何对自己写的代码负责

    这个月转岗了,从之前做的一年多的交易营销相关的系统,转去做交易网关系统。由于所属两个团队,在融入之后,明显感觉到两个团队做事情方法的不同,而最大的区别是做事方式...

    春哥大魔王

扫码关注云+社区

领取腾讯云代金券