"""Determination of KDE grid spacings that yield smooth contour lines"""
from __future__ import annotations
import threading
import numpy as np
from .base import KernelDensityEstimator
from .binning import bin_width_percentile
[docs]
def find_smooth_contour_spacing(
ds_list,
xax: str,
yax: str,
xrange: list[float] | tuple[float, float],
yrange: list[float] | tuple[float, float],
quantiles: list[float] | tuple[float],
xscale: str = "linear",
yscale: str = "linear",
kde_type: str = "histogram",
kde_kwargs: dict | None = None,
max_iter: int = 15,
abort_event: threading.Event | None = None,
) -> dict:
"""Determine contour spacing values for visually pleasing contours
The algorithm reduces the "kinks" in contours.
Parameters
----------
ds_list:
list of :class:`.RTDCBase` instances for which smooth contours
should be found
xax, yax:
X- and Y-axis of the contour plot
xrange, yrange:
Plotting range of the contour plot
quantiles:
Data quantiles for which contour lines should be computed; only
the highest quantile is used for smoothing
xscale, yscale:
"linear" or "log" scale of the contour plot axes
kde_type:
Kernel density estimate method to use
kde_kwargs:
Custom arguments for the kernel density estimate method
max_iter:
Maximum number of iterations to perform before returning
abort_event:
Optional event for prematurely stopping the iteration
Returns
-------
result:
Dictionary containing the iteration result:
- *total iterations*: iterations performed
- *success*: whether the smoothing succeeded
- *reason*: reason for success or failure
- *corners found*: whether a contour touched the plot boundary
- *spacing x*: contour spacing along x
- *spacing y*: contour spacing along y
TODO
----
- Exclude datasets in subsequent iterations when conditions already met
- Include all quantiles (and exclude when conditions met)
- Turn smoothing angle of 23° into keyword argument
- Turn maximum contour length into keyword argument (and possibly make
inversely dependent on quantile value)
"""
kde_list = [KernelDensityEstimator(ds) for ds in ds_list]
kde_kwargs = kde_kwargs or {}
# Make an initial estimate
spacings = []
for ax, scale, rang in [(xax, xscale, xrange), (yax, yscale, yrange)]:
spi = []
for kdi, ds in zip(kde_list, ds_list):
data = ds[ax]
use = ds.filter.all & (rang[0] <= data) & (data <= rang[1])
a = data[use]
if a.size:
spi.append(kdi.estimate_spacing(
a=a,
feat=ax,
scale=scale,
method=bin_width_percentile,
))
if spi:
spacings.append(np.min(spi))
else:
spacings.append(1)
xacc, yacc = spacings
results = {
"total iterations": 0,
"success": False,
}
corners_found = False
for ii in range(max_iter): # hard-limit is 15 iterations
if abort_event is not None and abort_event.is_set():
results["reason"] = "abort event"
break
if ii > 0:
# The contours computed with the previous spacing were not
# smooth enough. Halve the spacing here (and not at the end
# of the previous iteration), so that the spacing returned
# is always the last one that was evaluated.
xacc /= 2
yacc /= 2
corners_found = False
results["total iterations"] += 1
# maximum difference of opening angle from 180° [rad]
max_dphi = 0
max_length = np.inf
# number of datasets for which a contour could be computed
num_contours = 0
for kdi, ds in zip(kde_list, ds_list):
if abort_event is not None and abort_event.is_set():
break
# Compute the contour for the highest percentile of the plot.
contours = kdi.get_contour_lines(
quantiles=[np.max(quantiles)],
xax=xax,
yax=yax,
xacc=xacc,
yacc=yacc,
xscale=xscale,
yscale=yscale,
kde_type=kde_type,
kde_kwargs=kde_kwargs)
if not contours or not contours[0]:
# No contour for this dataset (e.g. all events filtered).
continue
num_contours += 1
# There may be several contours for the highest percentile
# (e.g. for multiple populations). All of them must be smooth.
for cc in contours[0]:
angles = compute_contour_opening_angles(
contour=cc,
xrange=xrange,
yrange=yrange,
xscale=xscale,
yscale=yscale)
# The opening angle is undefined (NaN) e.g. for duplicate
# contour points. Ignore these points.
valid = ~np.isnan(angles)
if (np.allclose(np.abs(angles[0]), np.pi / 2,
atol=0.001, rtol=0)
and np.all(angles[1:6] == 0)):
# We have probably encountered a contour at the
# boundary of the plot. This is ok.
corners = np.abs(np.abs(angles) - np.pi / 2) < 0.001
valid &= ~corners
corners_found = True
if np.any(valid):
dphi = np.max(np.abs(angles[valid]))
else:
# We cannot tell whether this contour is smooth.
dphi = np.inf
max_dphi = max(max_dphi, dphi)
max_length = min(max_length, len(cc))
if abort_event is not None and abort_event.is_set():
# Aborted while iterating over the datasets; the contours
# of the remaining datasets were not checked.
results["reason"] = "abort event"
break
elif not num_contours:
# There is no contour we could smooth.
results["reason"] = "no contour found"
break
results["reason"] = "maximum iterations reached"
if max_dphi < np.deg2rad(23):
# A contour is considered smooth when the maximum angle between
# adjacent line segments is smaller than 23°.
results["reason"] = "target opening angle reached"
results["success"] = True
break
elif max_length > 100:
results["reason"] = "maximum contour length reached"
results["success"] = True
break
results["corners found"] = corners_found
results["spacing x"] = xacc
results["spacing y"] = yacc
return results
[docs]
def compute_contour_opening_angles(contour, xrange, yrange, xscale, yscale):
"""For each point of the contour, compute the opening angle
This takes the visible plot area into account.
Parameters
----------
contour: ndarray
Contour coordinates of shape (N, 2) (closed automatically)
xrange, yrange: tuple of float
Visible plot range used for normalizing the contour
xscale, yscale: str
"linear" or "log" scale of the plot axes
Returns
-------
opang: 1d ndarray
For each contour point, the angle [rad] between the line
segments to its neighbors, folded into [-π/2, π/2] such that
a straight line yields 0 (nan for duplicate points)
"""
cc = np.array(contour, copy=True)
if not np.all(cc[0] == cc[-1]):
cc = np.resize(cc, (len(contour)+1, 2))
# Apply scale (before normalization, so that the contour is
# normalized to the visible plot area on a log scale)
assert xscale in ["log", "linear"]
if xscale == "log":
cc[:, 0] = np.log10(cc[:, 0])
xrange = np.log10(xrange)
assert yscale in ["log", "linear"]
if yscale == "log":
cc[:, 1] = np.log10(cc[:, 1])
yrange = np.log10(yrange)
# Normalize contour
cc[:, 0] = (cc[:, 0] - xrange[0]) / (xrange[1] - xrange[0])
cc[:, 1] = (cc[:, 1] - yrange[0]) / (yrange[1] - yrange[0])
opang = np.zeros(len(cc)-1, dtype=float)
for jj, c0 in enumerate(cc[:-1]): # we have a closed contour
cl = cc[:-1][jj - 1]
cr = cc[jj + 1]
# vector a
a = np.array(cl) - np.array(c0)
# vector b
b = np.array(cr) - np.array(c0)
absa = np.sqrt(np.sum(a ** 2))
absb = np.sqrt(np.sum(b ** 2))
denom = absa * absb
# avoid division by zero warnings (denom == 0)
phi = np.arccos(np.sum(a * b) / (denom or np.nan))
if np.abs(phi) > np.pi/2:
phi -= np.sign(phi) * np.pi
opang[jj] = phi
return opang