From 1aa3bf57f4dc372ae2482cd57ad6e8e0520bb833 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20Robert?= Date: Sat, 6 May 2023 10:42:02 +0200 Subject: [PATCH] DEPR: deprecate yt.testing.assert_allclose_units in favour of equivalent function in unyt.testing --- yt/data_objects/tests/test_bbox.py | 3 +- yt/fields/tests/test_angular_momentum.py | 3 +- yt/fields/tests/test_fields.py | 2 +- yt/fields/tests/test_particle_fields.py | 4 +- yt/fields/tests/test_species_fields.py | 3 +- yt/fields/tests/test_vector_fields.py | 8 +--- .../amrvac/tests/test_units_override.py | 3 +- yt/frontends/arepo/tests/test_outputs.py | 3 +- yt/frontends/artio/tests/test_outputs.py | 2 +- yt/frontends/athena/tests/test_outputs.py | 7 +-- yt/frontends/athena_pp/tests/test_outputs.py | 8 +--- yt/frontends/enzo/tests/test_outputs.py | 2 +- yt/frontends/ytdata/tests/test_old_outputs.py | 9 +--- yt/frontends/ytdata/tests/test_outputs.py | 2 +- yt/geometry/tests/test_grid_index.py | 4 +- yt/testing.py | 47 ++++--------------- yt/utilities/answer_testing/framework.py | 9 +--- yt/visualization/tests/test_export_frb.py | 3 +- yt/visualization/tests/test_plotwindow.py | 2 +- yt/visualization/tests/test_profile_plots.py | 3 +- 20 files changed, 44 insertions(+), 83 deletions(-) diff --git a/yt/data_objects/tests/test_bbox.py b/yt/data_objects/tests/test_bbox.py index a2e98c88083..8f28b445eae 100644 --- a/yt/data_objects/tests/test_bbox.py +++ b/yt/data_objects/tests/test_bbox.py @@ -2,8 +2,9 @@ import numpy as np from numpy.testing import assert_equal +from unyt.testing import assert_allclose_units -from yt.testing import assert_allclose_units, fake_amr_ds +from yt.testing import fake_amr_ds def test_object_bbox(): diff --git a/yt/fields/tests/test_angular_momentum.py b/yt/fields/tests/test_angular_momentum.py index 21387069f93..290e854e059 100644 --- a/yt/fields/tests/test_angular_momentum.py +++ b/yt/fields/tests/test_angular_momentum.py @@ -1,6 +1,7 @@ import numpy as np +from unyt.testing import assert_allclose_units -from yt.testing import assert_allclose_units, fake_amr_ds +from yt.testing import fake_amr_ds def test_AM_value(): diff --git a/yt/fields/tests/test_fields.py b/yt/fields/tests/test_fields.py index 08c4590ebdc..678220c9102 100644 --- a/yt/fields/tests/test_fields.py +++ b/yt/fields/tests/test_fields.py @@ -6,11 +6,11 @@ assert_equal, assert_raises, ) +from unyt.testing import assert_allclose_units from yt import load from yt.frontends.stream.fields import StreamFieldInfo from yt.testing import ( - assert_allclose_units, fake_amr_ds, fake_particle_ds, fake_random_ds, diff --git a/yt/fields/tests/test_particle_fields.py b/yt/fields/tests/test_particle_fields.py index e0140559d54..fa35714d849 100644 --- a/yt/fields/tests/test_particle_fields.py +++ b/yt/fields/tests/test_particle_fields.py @@ -1,4 +1,6 @@ -from yt.testing import assert_allclose_units, requires_file, requires_module +from unyt.testing import assert_allclose_units + +from yt.testing import requires_file, requires_module from yt.utilities.answer_testing.framework import data_dir_load g30 = "IsolatedGalaxy/galaxy0030/galaxy0030" diff --git a/yt/fields/tests/test_species_fields.py b/yt/fields/tests/test_species_fields.py index 16360b64c51..a8ea300dc14 100644 --- a/yt/fields/tests/test_species_fields.py +++ b/yt/fields/tests/test_species_fields.py @@ -1,6 +1,7 @@ from numpy.testing import assert_equal +from unyt.testing import assert_allclose_units -from yt.testing import assert_allclose_units, fake_random_ds +from yt.testing import fake_random_ds from yt.utilities.chemical_formulas import ChemicalFormula from yt.utilities.physical_ratios import _primordial_mass_fraction diff --git a/yt/fields/tests/test_vector_fields.py b/yt/fields/tests/test_vector_fields.py index a0d53a9895f..9ab08f92c2a 100644 --- a/yt/fields/tests/test_vector_fields.py +++ b/yt/fields/tests/test_vector_fields.py @@ -1,11 +1,7 @@ import numpy as np +from unyt.testing import assert_allclose_units -from yt.testing import ( - assert_allclose_units, - fake_random_ds, - requires_file, - requires_module, -) +from yt.testing import fake_random_ds, requires_file, requires_module from yt.units import cm, s # type: ignore from yt.utilities.answer_testing.framework import data_dir_load from yt.visualization.volume_rendering.off_axis_projection import off_axis_projection diff --git a/yt/frontends/amrvac/tests/test_units_override.py b/yt/frontends/amrvac/tests/test_units_override.py index 43165c5f0fd..e35651efeb5 100644 --- a/yt/frontends/amrvac/tests/test_units_override.py +++ b/yt/frontends/amrvac/tests/test_units_override.py @@ -1,6 +1,7 @@ from numpy.testing import assert_raises +from unyt.testing import assert_allclose_units -from yt.testing import assert_allclose_units, requires_file +from yt.testing import requires_file from yt.units import YTQuantity from yt.utilities.answer_testing.framework import data_dir_load diff --git a/yt/frontends/arepo/tests/test_outputs.py b/yt/frontends/arepo/tests/test_outputs.py index adfaba56f97..968b10f0c98 100644 --- a/yt/frontends/arepo/tests/test_outputs.py +++ b/yt/frontends/arepo/tests/test_outputs.py @@ -1,9 +1,10 @@ from collections import OrderedDict +from unyt.testing import assert_allclose_units + from yt.frontends.arepo.api import ArepoHDF5Dataset from yt.testing import ( ParticleSelectionComparison, - assert_allclose_units, requires_file, requires_module, ) diff --git a/yt/frontends/artio/tests/test_outputs.py b/yt/frontends/artio/tests/test_outputs.py index 934d71bb66c..fa4c44a95d9 100644 --- a/yt/frontends/artio/tests/test_outputs.py +++ b/yt/frontends/artio/tests/test_outputs.py @@ -1,9 +1,9 @@ from numpy.testing import assert_equal +from unyt.testing import assert_allclose_units from yt.frontends.artio.api import ARTIODataset from yt.loaders import load from yt.testing import ( - assert_allclose_units, requires_file, units_override_check, ) diff --git a/yt/frontends/athena/tests/test_outputs.py b/yt/frontends/athena/tests/test_outputs.py index 4fe38de2cb7..8609bb8bcc6 100644 --- a/yt/frontends/athena/tests/test_outputs.py +++ b/yt/frontends/athena/tests/test_outputs.py @@ -1,13 +1,10 @@ import numpy as np from numpy.testing import assert_equal +from unyt.testing import assert_allclose_units from yt.frontends.athena.api import AthenaDataset from yt.loaders import load -from yt.testing import ( - assert_allclose_units, - disable_dataset_cache, - requires_file, -) +from yt.testing import disable_dataset_cache, requires_file from yt.utilities.answer_testing.framework import ( data_dir_load, requires_ds, diff --git a/yt/frontends/athena_pp/tests/test_outputs.py b/yt/frontends/athena_pp/tests/test_outputs.py index b911e48ecf5..13ef34a9c3c 100644 --- a/yt/frontends/athena_pp/tests/test_outputs.py +++ b/yt/frontends/athena_pp/tests/test_outputs.py @@ -1,14 +1,10 @@ import numpy as np from numpy.testing import assert_equal +from unyt.testing import assert_allclose_units from yt.frontends.athena_pp.api import AthenaPPDataset from yt.loaders import load -from yt.testing import ( - assert_allclose_units, - disable_dataset_cache, - requires_file, - units_override_check, -) +from yt.testing import disable_dataset_cache, requires_file, units_override_check from yt.units import dimensions from yt.utilities.answer_testing.framework import ( data_dir_load, diff --git a/yt/frontends/enzo/tests/test_outputs.py b/yt/frontends/enzo/tests/test_outputs.py index 88479636416..bd84e48d2d9 100644 --- a/yt/frontends/enzo/tests/test_outputs.py +++ b/yt/frontends/enzo/tests/test_outputs.py @@ -1,9 +1,9 @@ import numpy as np +from unyt.testing import assert_allclose_units from yt.frontends.enzo.api import EnzoDataset from yt.frontends.enzo.fields import NODAL_FLAGS from yt.testing import ( - assert_allclose_units, assert_almost_equal, assert_array_equal, assert_equal, diff --git a/yt/frontends/ytdata/tests/test_old_outputs.py b/yt/frontends/ytdata/tests/test_old_outputs.py index d57c4116056..9735c4990df 100644 --- a/yt/frontends/ytdata/tests/test_old_outputs.py +++ b/yt/frontends/ytdata/tests/test_old_outputs.py @@ -10,6 +10,7 @@ import tempfile import numpy as np +from unyt.testing import assert_allclose_units from yt.data_objects.api import create_profile from yt.frontends.ytdata.api import ( @@ -23,13 +24,7 @@ YTDataFieldTest, compare_unit_attributes, ) -from yt.testing import ( - assert_allclose_units, - assert_array_equal, - requires_file, - requires_module, - skip, -) +from yt.testing import assert_array_equal, requires_file, requires_module, skip from yt.units.yt_array import YTArray from yt.utilities.answer_testing.framework import data_dir_load, requires_ds from yt.visualization.profile_plotter import PhasePlot, ProfilePlot diff --git a/yt/frontends/ytdata/tests/test_outputs.py b/yt/frontends/ytdata/tests/test_outputs.py index a9bd8bea858..eb15726eb02 100644 --- a/yt/frontends/ytdata/tests/test_outputs.py +++ b/yt/frontends/ytdata/tests/test_outputs.py @@ -4,6 +4,7 @@ import numpy as np from numpy.testing import assert_array_equal, assert_equal +from unyt.testing import assert_allclose_units from yt.data_objects.api import create_profile from yt.frontends.ytdata.api import ( @@ -15,7 +16,6 @@ save_as_dataset, ) from yt.loaders import load -from yt.testing import assert_allclose_units from yt.units.yt_array import YTArray, YTQuantity from yt.utilities.answer_testing.framework import ( AnswerTestingTest, diff --git a/yt/geometry/tests/test_grid_index.py b/yt/geometry/tests/test_grid_index.py index f03cf8a1a13..0df7029500f 100644 --- a/yt/geometry/tests/test_grid_index.py +++ b/yt/geometry/tests/test_grid_index.py @@ -1,4 +1,6 @@ -from yt.testing import assert_allclose_units, fake_amr_ds +from unyt.testing import assert_allclose_units + +from yt.testing import fake_amr_ds def test_icoords_to_ires(): diff --git a/yt/testing.py b/yt/testing.py index a00deec6540..00ed850fb35 100644 --- a/yt/testing.py +++ b/yt/testing.py @@ -16,13 +16,11 @@ import pytest from more_itertools import always_iterable from numpy.random import RandomState -from unyt.exceptions import UnitOperationError from yt._maintenance.deprecation import issue_deprecation_warning from yt.config import ytcfg from yt.funcs import is_sequence from yt.loaders import load -from yt.units.yt_array import YTArray, YTQuantity ANSWER_TEST_TAG = "answer_test" @@ -1261,43 +1259,16 @@ def assert_allclose_units(actual, desired, rtol=1e-7, atol=0, **kwargs): function for details. """ - from numpy.testing import assert_allclose + from unyt.testing import assert_allclose_units - # 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 UnitOperationError as e: - raise AssertionError( - f"Units of actual ({act.units}) and desired ({des.units}) " - "do not have equivalent dimensions" - ) from e - - rt = YTArray(rtol) - if not rt.units.is_dimensionless: - raise AssertionError(f"Units of rtol ({rt.units}) are not dimensionless") - - if not isinstance(atol, YTArray): - at = YTQuantity(atol, des.units) - - try: - at = at.in_units(act.units) - except UnitOperationError as e: - raise AssertionError( - f"Units of atol ({at.units}) and actual ({act.units}) " - "do not have equivalent dimensions" - ) from e - - # 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) + issue_deprecation_warning( + "yt.testing.assert_allclose_units is a deprecated alias for " + "unyt.testing.assert_allclose_units. " + "Please use the function from unyt directly.", + since="4.2", + stacklevel=2, + ) + assert_allclose_units(actual, desired, rtol=rtol, atol=atol, **kwargs) def assert_fname(fname): diff --git a/yt/utilities/answer_testing/framework.py b/yt/utilities/answer_testing/framework.py index 1f53c528e30..30cac5409ca 100644 --- a/yt/utilities/answer_testing/framework.py +++ b/yt/utilities/answer_testing/framework.py @@ -21,18 +21,13 @@ from matplotlib import image as mpimg from matplotlib.testing.compare import compare_images from nose.plugins import Plugin +from unyt.testing import assert_allclose_units from yt.config import ytcfg from yt.data_objects.static_output import Dataset from yt.funcs import get_pbar, get_yt_version from yt.loaders import load, load_simulation -from yt.testing import ( - assert_allclose_units, - assert_almost_equal, - assert_equal, - assert_rel_equal, - skipif, -) +from yt.testing import assert_almost_equal, assert_equal, assert_rel_equal, skipif from yt.utilities.exceptions import ( YTAmbiguousDataType, YTCloudError, diff --git a/yt/visualization/tests/test_export_frb.py b/yt/visualization/tests/test_export_frb.py index f5dc9133fac..c4dbb3adddd 100644 --- a/yt/visualization/tests/test_export_frb.py +++ b/yt/visualization/tests/test_export_frb.py @@ -1,7 +1,8 @@ import numpy as np from numpy.testing import assert_equal +from unyt.testing import assert_allclose_units -from yt.testing import assert_allclose_units, fake_random_ds +from yt.testing import fake_random_ds def setup(): diff --git a/yt/visualization/tests/test_plotwindow.py b/yt/visualization/tests/test_plotwindow.py index cbcd01d946b..a7d36f4e674 100644 --- a/yt/visualization/tests/test_plotwindow.py +++ b/yt/visualization/tests/test_plotwindow.py @@ -13,10 +13,10 @@ assert_raises, ) from unyt import unyt_array +from unyt.testing import assert_allclose_units from yt.loaders import load_uniform_grid from yt.testing import ( - assert_allclose_units, assert_fname, assert_rel_equal, fake_amr_ds, diff --git a/yt/visualization/tests/test_profile_plots.py b/yt/visualization/tests/test_profile_plots.py index 062dc7c6703..d42318b87ae 100644 --- a/yt/visualization/tests/test_profile_plots.py +++ b/yt/visualization/tests/test_profile_plots.py @@ -4,9 +4,10 @@ import unittest import pytest +from unyt.testing import assert_allclose_units import yt -from yt.testing import assert_allclose_units, fake_random_ds +from yt.testing import fake_random_ds from yt.visualization.api import PhasePlot