jorgensd / dolfinx_mpc

Extension for dolfinx to handle multi-point constraints.
https://jorgensd.github.io/dolfinx_mpc/
MIT License
30 stars 12 forks source link

use numpy.testing for array asserts #103

Closed minrk closed 7 months ago

minrk commented 7 months ago

yields better error messages when tests fail

especially for nt.assert_allclose, where you get e.g.:

E           Not equal to tolerance rtol=5e-13, atol=5e-13
E
E           Mismatched elements: 36 / 36 (100%)
E           Max absolute difference: 0.01901673
E           Max relative difference: inf
E            x: array([-0.000405, -0.00106 , -0.014619, -0.001312, -0.007975, -0.01086 ,
E                   0.012003,  0.00969 , -0.0054  ,  0.018766,  0.013964,  0.007522,
E                   0.000405, -0.000405, -0.00106 ,  0.010049, -0.001312, -0.010049,...
E            y: array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
E                  0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
E                  0., 0.])

rather than the same assertion with assert np.allclose:

E               AssertionError: assert False
E                +  where False = <function allclose at 0x10ac612f0>(array([-0.00040501, -0.00106036, -0.01461884, -0.00131173, -0.00797458,\n       -0.0108598 ,  0.0120033 ,  0.00969028, -0.0054    ,  0.01876642,\n        0.0139635 ,  0.00752241,  0.00040501, -0.00040501, -0.00106036,\n        0.0100491 , -0.00131173, -0.0100491 ,  0.00131173,  0.00106036,\n       -0.0139635 , -0.00752241,  0.00040501, -0.00969028, -0.01876642,\n        0.0054    ,  0.00797458, -0.0120033 , -0.01901673,  0.0108598 ,\n        0.01461884,  0.01134797, -0.01134797,  0.00131173,  0.00106036,\n        0.01901673]), array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n       0., 0.]), rtol=(500 * 1e-15), atol=(500 * 1e-15))
E                +    where <function allclose at 0x10ac612f0> = np.allclose
E                +    and   array([-0.00040501, -0.00106036, -0.01461884, -0.00131173, -0.00797458,\n       -0.0108598 ,  0.0120033 ,  0.00969028, -0.0054    ,  0.01876642,\n        0.0139635 ,  0.00752241,  0.00040501, -0.00040501, -0.00106036,\n        0.0100491 , -0.00131173, -0.0100491 ,  0.00131173,  0.00106036,\n       -0.0139635 , -0.00752241,  0.00040501, -0.00969028, -0.01876642,\n        0.0054    ,  0.00797458, -0.0120033 , -0.01901673,  0.0108598 ,\n        0.01461884,  0.01134797, -0.01134797,  0.00131173,  0.00106036,\n        0.01901673]) = <built-in method astype of numpy.ndarray object at 0x12d9c15f0>(dtype('float64'))
E                +      where <built-in method astype of numpy.ndarray object at 0x12d9c15f0> = array([-0.00040501, -0.00106036, -0.01461884, -0.00131173, -0.00797458,\n       -0.0108598 ,  0.0120033 ,  0.00969028, -0.0054    ,  0.01876642,\n        0.0139635 ,  0.00752241,  0.00040501, -0.00040501, -0.00106036,\n        0.0100491 , -0.00131173, -0.0100491 ,  0.00131173,  0.00106036,\n       -0.0139635 , -0.00752241,  0.00040501, -0.00969028, -0.01876642,\n        0.0054    ,  0.00797458, -0.0120033 , -0.01901673,  0.0108598 ,\n        0.01461884,  0.01134797, -0.01134797,  0.00131173,  0.00106036,\n        0.01901673]).astype
E                +      and   dtype('float64') = array([0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n       0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,\n       0., 0.]).dtype
E                +    and   1e-15 = finfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64).resolution
E                +      where finfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64) = <class 'numpy.finfo'>(default_scalar_type)
E                +        where <class 'numpy.finfo'> = np.finfo
E                +    and   1e-15 = finfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64).resolution
E                +      where finfo(resolution=1e-15, min=-1.7976931348623157e+308, max=1.7976931348623157e+308, dtype=float64) = <class 'numpy.finfo'>(default_scalar_type)
E                +        where <class 'numpy.finfo'> = np.finfo
jorgensd commented 7 months ago

I guess numpy.testing.assert_almost_equal with default decimal=7 is stricter than https://numpy.org/doc/stable/reference/generated/numpy.allclose.html i.e. assert_almost_equal

abs(desired-actual) < float64(1.5 * 10(-decimal)) allclose* absolute(a - b) <= (atol + rtol absolute(b)) Maybe we need to add decimal=6 to the rectangular test?

minrk commented 7 months ago

Probably overzealous, maybe should just do the all close comparisons, not almost_equal