Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 15 additions & 3 deletions dclab/kde/binning.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -78,7 +90,7 @@ def bin_width_doane(a):
else:
warnings.warn(
"KDE encountered an empty array",
KernelDensityEstimationForEmtpyArrayWarning,
KernelDensityEstimationForEmptyArrayWarning,
)
acc = 1
return acc
Expand Down Expand Up @@ -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:
Expand Down
15 changes: 14 additions & 1 deletion dclab/polygon_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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}'."
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 14 additions & 5 deletions tests/test_kde.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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"]))
Expand Down Expand Up @@ -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
Expand All @@ -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
12 changes: 12 additions & 0 deletions tests/test_polygon_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Loading