diff --git a/CHANGELOG b/CHANGELOG index 78dc34bc..d781611a 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -256,6 +256,11 @@ angle reached") when the abort event was set while iterating over the datasets; also pass a flat list of quantiles to `get_contour_lines` (nested list only worked by chance) (#419) + - fix: rename misspelled `PolygonFilter.instace_exists` to + `PolygonFilter.instance_exists` and + `KernelDensityEstimationForEmtpyArrayWarning` to + `KernelDensityEstimationForEmptyArrayWarning` (the old names are + deprecated aliases) (#420) 0.74.0 - fix: CLI commands compress, condense, join, and repack deleted the input file if it was also given as the output path diff --git a/dclab/kde/binning.py b/dclab/kde/binning.py index 1e6e9ff7..be1c13a1 100644 --- a/dclab/kde/binning.py +++ b/dclab/kde/binning.py @@ -5,10 +5,22 @@ from scipy.stats import skew -class KernelDensityEstimationForEmtpyArrayWarning(UserWarning): +class KernelDensityEstimationForEmptyArrayWarning(UserWarning): """Used when user attempts to compute KDE for an empty array""" +def __getattr__(name): + """Provide deprecated aliases for renamed objects""" + if name == "KernelDensityEstimationForEmtpyArrayWarning": + warnings.warn("`KernelDensityEstimationForEmtpyArrayWarning` is " + "deprecated; please use " + "`KernelDensityEstimationForEmptyArrayWarning`", + DeprecationWarning, + stacklevel=2) + return KernelDensityEstimationForEmptyArrayWarning + raise AttributeError(f"module {__name__!r} has no attribute {name!r}") + + def bin_num_doane(a): """Compute number of bins based on Doane's formula @@ -78,7 +90,7 @@ def bin_width_doane(a): else: warnings.warn( "KDE encountered an empty array", - KernelDensityEstimationForEmtpyArrayWarning, + KernelDensityEstimationForEmptyArrayWarning, ) acc = 1 return acc @@ -134,7 +146,7 @@ def bin_width_percentile(a): if not data.size: warnings.warn( "KDE encountered an empty array", - KernelDensityEstimationForEmtpyArrayWarning, + KernelDensityEstimationForEmptyArrayWarning, ) acc = 1 else: diff --git a/dclab/polygon_filter.py b/dclab/polygon_filter.py index a8d7bb91..2461bd97 100644 --- a/dclab/polygon_filter.py +++ b/dclab/polygon_filter.py @@ -206,7 +206,7 @@ def _set_unique_id(self, unique_id): """Define a unique id""" assert isinstance(unique_id, int), "unique_id must be an integer" - if PolygonFilter.instace_exists(unique_id): + if PolygonFilter.instance_exists(unique_id): newid = max(PolygonFilter._instance_counter, unique_id+1) msg = f"PolygonFilter with unique_id '{unique_id}' exists." msg += f" Using new unique id '{newid}'." @@ -318,6 +318,19 @@ def import_all(path): @staticmethod def instace_exists(unique_id): + """Deprecated alias of :meth:`.PolygonFilter.instance_exists` + + .. deprecated:: 0.75.0 + Use :meth:`.PolygonFilter.instance_exists` instead. + """ + warnings.warn("`PolygonFilter.instace_exists` is deprecated; please " + "use `PolygonFilter.instance_exists`", + DeprecationWarning, + stacklevel=2) + return PolygonFilter.instance_exists(unique_id) + + @staticmethod + def instance_exists(unique_id): """Determine whether an instance with this unique id exists""" try: PolygonFilter.get_instance_from_id(unique_id) diff --git a/tests/test_kde.py b/tests/test_kde.py index eef00142..1727cad6 100644 --- a/tests/test_kde.py +++ b/tests/test_kde.py @@ -7,7 +7,7 @@ from dclab.external import skimage from dclab.kde import KernelDensityEstimator, binning from dclab.kde.base import ContourSpacingTooLarge -from dclab.kde.binning import KernelDensityEstimationForEmtpyArrayWarning +from dclab.kde.binning import KernelDensityEstimationForEmptyArrayWarning def test_contour_lines(): @@ -307,14 +307,14 @@ def test_kde_get_at_all_nan_inf(): ds = dclab.new_dataset(ddict) kde_instance = KernelDensityEstimator(ds) - with pytest.warns(KernelDensityEstimationForEmtpyArrayWarning, + with pytest.warns(KernelDensityEstimationForEmptyArrayWarning, match="KDE encountered an empty array"): sc = kde_instance.get_at(xax="area_um", yax="deform") assert np.all(np.isnan(sc)) ds.filter.manual[0] = False ds.apply_filter() - with pytest.warns(KernelDensityEstimationForEmtpyArrayWarning, + with pytest.warns(KernelDensityEstimationForEmptyArrayWarning, match="KDE encountered an empty array"): sc2 = kde_instance.get_at(xax="area_um", yax="deform", positions=(ds["area_um"], ds["deform"])) @@ -445,7 +445,7 @@ def test_kde_get_raster_all_nan_inf(): ds = dclab.new_dataset(ddict) kde_instance = KernelDensityEstimator(ds) - with pytest.warns(KernelDensityEstimationForEmtpyArrayWarning, + with pytest.warns(KernelDensityEstimationForEmptyArrayWarning, match="KDE encountered an empty array"): x, y, z = kde_instance.get_raster(xax="area_um", yax="deform") assert x.shape == y.shape == z.shape @@ -461,8 +461,17 @@ def test_kde_get_raster_empty(): kde_instance = KernelDensityEstimator(ds) - with pytest.warns(KernelDensityEstimationForEmtpyArrayWarning, + with pytest.warns(KernelDensityEstimationForEmptyArrayWarning, match="KDE encountered an empty array"): x, y, z = kde_instance.get_raster(xax="area_um", yax="deform") assert x.shape == y.shape == z.shape assert np.all(np.isnan(z)) + + +def test_kde_empty_array_warning_deprecated_alias(): + from dclab.kde import binning + with pytest.warns(DeprecationWarning, match="EmptyArrayWarning"): + old = binning.KernelDensityEstimationForEmtpyArrayWarning + assert old is KernelDensityEstimationForEmptyArrayWarning + with pytest.raises(AttributeError): + binning.does_not_exist # noqa: B018 diff --git a/tests/test_polygon_filter.py b/tests/test_polygon_filter.py index 09a836fb..bff1a710 100644 --- a/tests/test_polygon_filter.py +++ b/tests/test_polygon_filter.py @@ -454,3 +454,15 @@ def test_load_missing_axis(tmp_path, axis_line): with pytest.raises(dclab.polygon_filter.PolygonFilterError, match="'X Axis' or 'Y Axis' not defined"): dclab.PolygonFilter(filename=path) + + +def test_instance_exists(): + dclab.PolygonFilter.clear_all_filters() + pf = dclab.PolygonFilter(axes=["area_um", "deform"], + points=[[0, 0], [1, 1], [0, 1]]) + assert dclab.PolygonFilter.instance_exists(pf.unique_id) + assert not dclab.PolygonFilter.instance_exists(pf.unique_id + 1) + # deprecated misspelled alias + with pytest.warns(DeprecationWarning, match="instance_exists"): + assert dclab.PolygonFilter.instace_exists(pf.unique_id) + dclab.PolygonFilter.clear_all_filters()