[yt-svn] commit/yt: chummels: Merged in ngoldbaum/yt (pull request #1696)

commits-noreply at bitbucket.org commits-noreply at bitbucket.org
Thu Aug 13 09:31:07 PDT 2015


1 new commit in yt:

https://bitbucket.org/yt_analysis/yt/commits/0be6b1dfd994/
Changeset:   0be6b1dfd994
Branch:      yt
User:        chummels
Date:        2015-08-13 16:30:51+00:00
Summary:     Merged in ngoldbaum/yt (pull request #1696)

Add assert_allclose_units, like assert_allclose but properly handles units
Affected #:  9 files

diff -r a46e96ef9fcbbcd1d4ebccae406938d166212f2a -r 0be6b1dfd994759358b1d5f25694bf6d0d11e890 yt/analysis_modules/absorption_spectrum/tests/test_absorption_spectrum.py
--- a/yt/analysis_modules/absorption_spectrum/tests/test_absorption_spectrum.py
+++ b/yt/analysis_modules/absorption_spectrum/tests/test_absorption_spectrum.py
@@ -12,7 +12,7 @@
 
 import numpy as np
 from yt.testing import \
-    assert_allclose, requires_file, requires_module
+    assert_allclose_units, requires_file, requires_module
 from yt.analysis_modules.absorption_spectrum.absorption_line import \
     voigt_old, voigt_scipy
 from yt.analysis_modules.absorption_spectrum.api import AbsorptionSpectrum
@@ -129,4 +129,4 @@
 def test_voigt_profiles():
     a = 1.7e-4
     x = np.linspace(5.0, -3.6, 60)
-    yield assert_allclose, voigt_old(a, x), voigt_scipy(a, x), 1e-8
+    yield assert_allclose_units, voigt_old(a, x), voigt_scipy(a, x), 1e-8

diff -r a46e96ef9fcbbcd1d4ebccae406938d166212f2a -r 0be6b1dfd994759358b1d5f25694bf6d0d11e890 yt/analysis_modules/ppv_cube/tests/test_ppv.py
--- a/yt/analysis_modules/ppv_cube/tests/test_ppv.py
+++ b/yt/analysis_modules/ppv_cube/tests/test_ppv.py
@@ -46,7 +46,7 @@
     a = cube.data.mean(axis=(0,1)).v
     b = dv*np.exp(-((cube.vmid+v_shift)/v_th)**2)/(np.sqrt(np.pi)*v_th)
 
-    yield assert_allclose, a, b, 1.0e-2
+    yield assert_allclose_units, a, b, 1.0e-2
 
     E_0 = 6.8*u.keV
 
@@ -58,4 +58,4 @@
 
     c = dE*np.exp(-((cube.vmid-E_shift)/delta_E)**2)/(np.sqrt(np.pi)*delta_E)
 
-    yield assert_allclose, a, c, 1.0e-2
+    yield assert_allclose_units, a, c, 1.0e-2

diff -r a46e96ef9fcbbcd1d4ebccae406938d166212f2a -r 0be6b1dfd994759358b1d5f25694bf6d0d11e890 yt/frontends/athena/tests/test_outputs.py
--- a/yt/frontends/athena/tests/test_outputs.py
+++ b/yt/frontends/athena/tests/test_outputs.py
@@ -78,10 +78,10 @@
     prj2 = ds1.proj("density",0)
 
     yield assert_equal, sp1.quantities.extrema("pressure"), sp2.quantities.extrema("pressure")
-    yield assert_allclose, sp1.quantities.total_quantity("pressure"), sp2.quantities.total_quantity("pressure")
+    yield assert_allclose_units, sp1.quantities.total_quantity("pressure"), sp2.quantities.total_quantity("pressure")
     for ax in "xyz":
         yield assert_equal, sp1.quantities.extrema("velocity_%s" % ax), sp2.quantities.extrema("velocity_%s" % ax)
-    yield assert_allclose, sp1.quantities.bulk_velocity(), sp2.quantities.bulk_velocity()
+    yield assert_allclose_units, sp1.quantities.bulk_velocity(), sp2.quantities.bulk_velocity()
     yield assert_equal, prj1["density"], prj2["density"]
 
     ytcfg["yt","skip_dataset_cache"] = "False"

diff -r a46e96ef9fcbbcd1d4ebccae406938d166212f2a -r 0be6b1dfd994759358b1d5f25694bf6d0d11e890 yt/testing.py
--- a/yt/testing.py
+++ b/yt/testing.py
@@ -751,3 +751,59 @@
     finally:
         os.chdir(initial_dir)
         mylog.setLevel(orig_level)
+
+def assert_allclose_units(actual, desired, rtol=1e-7, atol=0, **kwargs):
+    """Raise an error if two objects are not equal up to desired tolerance
+
+    This is a wrapper for :func:`numpy.testing.assert_allclose` that also
+    verifies unit consistency
+
+    Parameters
+    ----------
+    actual : array-like
+        Array obtained (possibly with attached units)
+    desired : array-like
+        Array to compare with (possibly with attached units)
+    rtol : float, oprtional
+        Relative tolerance, defaults to 1e-7
+    atol : float or quantity, optional
+        Absolute tolerance. If units are attached, they must be consistent
+        with the units of ``actual`` and ``desired``. If no units are attached,
+        assumes the same units as ``desired``. Defaults to zero.
+
+    Also accepts additional keyword arguments accepted by
+    :func:`numpy.testing.assert_allclose`, see the documentation of that
+    function for details.
+    """
+    # Create a copy to ensure this function does not alter input arrays
+    act = YTArray(actual)
+    des = YTArray(desired)
+
+    try:
+        des = des.in_units(act.units)
+    except YTUnitOperationError:
+        raise AssertionError("Units of actual (%s) and desired (%s) do not have "
+                             "equivalent dimensions" % (act.units, des.units))
+
+    rt = YTArray(rtol)
+    if not rt.units.is_dimensionless:
+        raise AssertionError("Units of rtol (%s) are not "
+                             "dimensionless" % rt.units)
+
+    if not isinstance(atol, YTArray):
+        at = YTQuantity(atol, des.units)
+
+    try:
+        at = at.in_units(act.units)
+    except YTUnitOperationError:
+        raise AssertionError("Units of atol (%s) and actual (%s) do not have "
+                             "equivalent dimensions" % (at.units, act.units))
+
+    # units have been validated, so we strip units before calling numpy
+    # to avoid spurious errors
+    act = act.value
+    des = des.value
+    rt = rt.value
+    at = at.value
+
+    return assert_allclose(act, des, rt, at, **kwargs)

diff -r a46e96ef9fcbbcd1d4ebccae406938d166212f2a -r 0be6b1dfd994759358b1d5f25694bf6d0d11e890 yt/units/tests/test_units.py
--- a/yt/units/tests/test_units.py
+++ b/yt/units/tests/test_units.py
@@ -18,12 +18,13 @@
 import nose
 import numpy as np
 from numpy.testing import \
-    assert_approx_equal, assert_array_almost_equal_nulp, \
-    assert_allclose, assert_raises
+    assert_array_almost_equal_nulp, \
+    assert_raises
 from nose.tools import assert_true
 import operator
 from sympy import Symbol
-from yt.testing import fake_random_ds
+from yt.testing import \
+    fake_random_ds, assert_allclose_units
 
 # dimensions
 from yt.units.dimensions import \
@@ -155,10 +156,10 @@
     yield assert_true, u3.expr == s3
     yield assert_true, u4.expr == s4
 
-    yield assert_allclose, u1.base_value, pc_cgs, 1e-12
-    yield assert_allclose, u2.base_value, yr_cgs, 1e-12
-    yield assert_allclose, u3.base_value, pc_cgs * yr_cgs, 1e-12
-    yield assert_allclose, u4.base_value, pc_cgs**2 / yr_cgs, 1e-12
+    yield assert_allclose_units, u1.base_value, pc_cgs, 1e-12
+    yield assert_allclose_units, u2.base_value, yr_cgs, 1e-12
+    yield assert_allclose_units, u3.base_value, pc_cgs * yr_cgs, 1e-12
+    yield assert_allclose_units, u4.base_value, pc_cgs**2 / yr_cgs, 1e-12
 
     yield assert_true, u1.dimensions == length
     yield assert_true, u2.dimensions == time
@@ -180,7 +181,7 @@
     yield assert_true, u1.base_value == 1
     yield assert_true, u1.dimensions == power
 
-    yield assert_allclose, u2.base_value, km_cgs / Mpc_cgs, 1e-12
+    yield assert_allclose_units, u2.base_value, km_cgs / Mpc_cgs, 1e-12
     yield assert_true, u2.dimensions == rate
 
 def test_create_new_symbol():
@@ -324,7 +325,7 @@
     u3 = u1 * u2
 
     yield assert_true, u3.expr == msun_sym * pc_sym
-    yield assert_allclose, u3.base_value, msun_cgs * pc_cgs, 1e-12
+    yield assert_allclose_units, u3.base_value, msun_cgs * pc_cgs, 1e-12
     yield assert_true, u3.dimensions == mass * length
 
     # Pow and Mul operations
@@ -334,7 +335,7 @@
     u6 = u4 * u5
 
     yield assert_true, u6.expr == pc_sym**2 * msun_sym * s_sym
-    yield assert_allclose, u6.base_value, pc_cgs**2 * msun_cgs, 1e-12
+    yield assert_allclose_units, u6.base_value, pc_cgs**2 * msun_cgs, 1e-12
     yield assert_true, u6.dimensions == length**2 * mass * time
 
 
@@ -358,7 +359,7 @@
     u3 = u1 / u2
 
     yield assert_true, u3.expr == pc_sym / (km_sym * s_sym)
-    yield assert_allclose, u3.base_value, pc_cgs / km_cgs, 1e-12
+    yield assert_allclose_units, u3.base_value, pc_cgs / km_cgs, 1e-12
     yield assert_true, u3.dimensions == 1 / time
 
 
@@ -377,12 +378,12 @@
     u2 = u1**2
 
     yield assert_true, u2.dimensions == u1_dims**2
-    yield assert_allclose, u2.base_value, (pc_cgs**2 * mK_cgs**4)**2, 1e-12
+    yield assert_allclose_units, u2.base_value, (pc_cgs**2 * mK_cgs**4)**2, 1e-12
 
     u3 = u1**(-1.0/3)
 
     yield assert_true, u3.dimensions == nsimplify(u1_dims**(-1.0/3))
-    yield assert_allclose, u3.base_value, (pc_cgs**2 * mK_cgs**4)**(-1.0/3), 1e-12
+    yield assert_allclose_units, u3.base_value, (pc_cgs**2 * mK_cgs**4)**(-1.0/3), 1e-12
 
 
 def test_equality():
@@ -414,7 +415,7 @@
     yield assert_true, u2.expr == u3.expr
     yield assert_true, u2 == u3
 
-    yield assert_allclose, u1.base_value, Msun_cgs / Mpc_cgs**3, 1e-12
+    yield assert_allclose_units, u1.base_value, Msun_cgs / Mpc_cgs**3, 1e-12
     yield assert_true, u2.base_value == 1
     yield assert_true, u3.base_value == 1
 
@@ -424,7 +425,7 @@
     yield assert_true, u2.dimensions == mass_density
     yield assert_true, u3.dimensions == mass_density
 
-    yield assert_allclose, get_conversion_factor(u1, u3)[0], \
+    yield assert_allclose_units, get_conversion_factor(u1, u3)[0], \
         Msun_cgs / Mpc_cgs**3, 1e-12
 
 def test_is_code_unit():

diff -r a46e96ef9fcbbcd1d4ebccae406938d166212f2a -r 0be6b1dfd994759358b1d5f25694bf6d0d11e890 yt/units/tests/test_ytarray.py
--- a/yt/units/tests/test_ytarray.py
+++ b/yt/units/tests/test_ytarray.py
@@ -28,8 +28,7 @@
     assert_array_equal, \
     assert_equal, assert_raises, \
     assert_array_almost_equal_nulp, \
-    assert_array_almost_equal, \
-    assert_allclose
+    assert_array_almost_equal
 from numpy import array
 from yt.units.yt_array import \
     YTArray, YTQuantity, \
@@ -38,7 +37,9 @@
     uunion1d, loadtxt, savetxt
 from yt.utilities.exceptions import \
     YTUnitOperationError, YTUfuncUnitError
-from yt.testing import fake_random_ds, requires_module
+from yt.testing import \
+    fake_random_ds, requires_module, \
+    assert_allclose_units
 from yt.funcs import fix_length
 from yt.units.unit_symbols import \
     cm, m, g
@@ -894,14 +895,14 @@
 
     E = mp.to_equivalent("keV","mass_energy")
     yield assert_equal, E, mp*clight*clight
-    yield assert_allclose, mp, E.to_equivalent("g", "mass_energy")
+    yield assert_allclose_units, mp, E.to_equivalent("g", "mass_energy")
 
     # Thermal
 
     T = YTQuantity(1.0e8,"K")
     E = T.to_equivalent("W*hr","thermal")
     yield assert_equal, E, (kboltz*T).in_units("W*hr")
-    yield assert_allclose, T, E.to_equivalent("K", "thermal")
+    yield assert_allclose_units, T, E.to_equivalent("K", "thermal")
 
     # Spectral
 
@@ -910,11 +911,11 @@
     yield assert_equal, nu, clight/l
     E = hcgs*nu
     l2 = E.to_equivalent("angstrom", "spectral")
-    yield assert_allclose, l, l2
+    yield assert_allclose_units, l, l2
     nu2 = clight/l2.in_units("cm")
-    yield assert_allclose, nu, nu2
+    yield assert_allclose_units, nu, nu2
     E2 = nu2.to_equivalent("keV", "spectral")
-    yield assert_allclose, E2, E.in_units("keV")
+    yield assert_allclose_units, E2, E.in_units("keV")
 
     # Sound-speed
 
@@ -922,13 +923,13 @@
     gg = 5./3.
     c_s = T.to_equivalent("km/s","sound_speed")
     yield assert_equal, c_s, np.sqrt(gg*kboltz*T/(mu*mh))
-    yield assert_allclose, T, c_s.to_equivalent("K","sound_speed")
+    yield assert_allclose_units, T, c_s.to_equivalent("K","sound_speed")
 
     mu = 0.5
     gg = 4./3.
     c_s = T.to_equivalent("km/s","sound_speed", mu=mu, gamma=gg)
     yield assert_equal, c_s, np.sqrt(gg*kboltz*T/(mu*mh))
-    yield assert_allclose, T, c_s.to_equivalent("K","sound_speed",
+    yield assert_allclose_units, T, c_s.to_equivalent("K","sound_speed",
                                                     mu=mu, gamma=gg)
 
     # Lorentz
@@ -936,21 +937,21 @@
     v = 0.8*clight
     g = v.to_equivalent("dimensionless","lorentz")
     g2 = YTQuantity(1./np.sqrt(1.-0.8*0.8), "dimensionless")
-    yield assert_allclose, g, g2
+    yield assert_allclose_units, g, g2
     v2 = g2.to_equivalent("mile/hr", "lorentz")
-    yield assert_allclose, v2, v.in_units("mile/hr")
+    yield assert_allclose_units, v2, v.in_units("mile/hr")
 
     # Schwarzschild
 
     R = mass_sun_cgs.to_equivalent("kpc","schwarzschild")
     yield assert_equal, R.in_cgs(), 2*G*mass_sun_cgs/(clight*clight)
-    yield assert_allclose, mass_sun_cgs, R.to_equivalent("g", "schwarzschild")
+    yield assert_allclose_units, mass_sun_cgs, R.to_equivalent("g", "schwarzschild")
 
     # Compton
 
     l = me.to_equivalent("angstrom","compton")
     yield assert_equal, l, hcgs/(me*clight)
-    yield assert_allclose, me, l.to_equivalent("g", "compton")
+    yield assert_allclose_units, me, l.to_equivalent("g", "compton")
 
     # Number density
 
@@ -958,18 +959,18 @@
 
     n = rho.to_equivalent("cm**-3","number_density")
     yield assert_equal, n, rho/(mh*0.6)
-    yield assert_allclose, rho, n.to_equivalent("g/cm**3","number_density")
+    yield assert_allclose_units, rho, n.to_equivalent("g/cm**3","number_density")
 
     n = rho.to_equivalent("cm**-3","number_density", mu=0.75)
     yield assert_equal, n, rho/(mh*0.75)
-    yield assert_allclose, rho, n.to_equivalent("g/cm**3","number_density", mu=0.75)
+    yield assert_allclose_units, rho, n.to_equivalent("g/cm**3","number_density", mu=0.75)
 
     # Effective temperature
 
     T = YTQuantity(1.0e4, "K")
     F = T.to_equivalent("erg/s/cm**2","effective_temperature")
     yield assert_equal, F, stefan_boltzmann_constant_cgs*T**4
-    yield assert_allclose, T, F.to_equivalent("K", "effective_temperature")
+    yield assert_allclose_units, T, F.to_equivalent("K", "effective_temperature")
 
 def test_electromagnetic():
     from yt.units.dimensions import charge_mks, pressure, current_cgs, \

diff -r a46e96ef9fcbbcd1d4ebccae406938d166212f2a -r 0be6b1dfd994759358b1d5f25694bf6d0d11e890 yt/utilities/answer_testing/framework.py
--- a/yt/utilities/answer_testing/framework.py
+++ b/yt/utilities/answer_testing/framework.py
@@ -403,8 +403,8 @@
             assert_equal(new_result, old_result,
                          err_msg=err_msg, verbose=True)
         else:
-            assert_allclose(new_result, old_result, 10.**(-self.decimals),
-                             err_msg=err_msg, verbose=True)
+            assert_allclose_units(new_result, old_result, 10.**(-self.decimals),
+                                  err_msg=err_msg, verbose=True)
 
 class AllFieldValuesTest(AnswerTestingTest):
     _type_name = "AllFieldValues"
@@ -478,8 +478,8 @@
             if self.decimals is None:
                 assert_equal(nres, ores, err_msg=err_msg)
             else:
-                assert_allclose(nres, ores, 10.**-(self.decimals),
-                                err_msg=err_msg)
+                assert_allclose_units(nres, ores, 10.**-(self.decimals),
+                                      err_msg=err_msg)
 
 class PixelizedProjectionValuesTest(AnswerTestingTest):
     _type_name = "PixelizedProjectionValues"
@@ -727,7 +727,8 @@
             if self.decimals is None:
                 assert_equal(new_result[k], old_result[k])
             else:
-                assert_allclose(new_result[k], old_result[k], 10**(-self.decimals))
+                assert_allclose_units(new_result[k], old_result[k],
+                                      10**(-self.decimals))
 
 class GenericImageTest(AnswerTestingTest):
     _type_name = "GenericImage"

diff -r a46e96ef9fcbbcd1d4ebccae406938d166212f2a -r 0be6b1dfd994759358b1d5f25694bf6d0d11e890 yt/utilities/exceptions.py
--- a/yt/utilities/exceptions.py
+++ b/yt/utilities/exceptions.py
@@ -195,7 +195,7 @@
           "the %s base system of units." % (self.unit, self.units_base)
         return err
 
-class YTEquivalentDimsError(Exception):
+class YTEquivalentDimsError(YTUnitOperationError):
     def __init__(self, old_units, new_units, base):
         self.old_units = old_units
         self.new_units = new_units

diff -r a46e96ef9fcbbcd1d4ebccae406938d166212f2a -r 0be6b1dfd994759358b1d5f25694bf6d0d11e890 yt/visualization/tests/test_export_frb.py
--- a/yt/visualization/tests/test_export_frb.py
+++ b/yt/visualization/tests/test_export_frb.py
@@ -16,7 +16,7 @@
 import numpy as np
 from yt.testing import \
     fake_random_ds, assert_equal, \
-    assert_allclose
+    assert_allclose_units
 
 def setup():
     """Test specific setup."""
@@ -35,5 +35,6 @@
     yield assert_equal, frb_ds.domain_right_edge.v, np.array([0.75,0.75,1.0])
     yield assert_equal, frb_ds.domain_width.v, np.array([0.5,0.5,1.0])
     yield assert_equal, frb_ds.domain_dimensions, np.array([64,64,1], dtype="int64")
-    yield assert_allclose, frb["density"].sum(), dd_frb.quantities.total_quantity("density")
+    yield assert_allclose_units, frb["density"].sum(), \
+        dd_frb.quantities.total_quantity("density")
     yield assert_equal, frb_ds.index.num_grids, 8

Repository URL: https://bitbucket.org/yt_analysis/yt/

--

This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.


More information about the yt-svn mailing list