Skip to content

Commit da746a0

Browse files
authored
Merge pull request #275 from nberliner/master
Improve handling of infinite lambda values
2 parents 5a5cc96 + 9df3b27 commit da746a0

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

hdbscan/plots.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,22 @@ def plot(self, leaf_separation=1, cmap='viridis', select_clusters=False,
374374
raise ImportError('You must have matplotlib.patches available to plot selected clusters.')
375375

376376
chosen_clusters = self._select_clusters()
377+
378+
# Extract the chosen cluster bounds. If enough duplicate data points exist in the
379+
# data the lambda value might be infinite. This breaks labeling and highlighting
380+
# the chosen clusters.
381+
cluster_bounds = np.array([ plot_data['cluster_bounds'][c] for c in chosen_clusters ])
382+
if not np.isfinite(cluster_bounds).all():
383+
warn('Infinite lambda values encountered in chosen clusters.'
384+
' This might be due to duplicates in the data.')
385+
386+
# Extract the plot range of the y-axis and set default center and height values for ellipses.
387+
# Extremly dense clusters might result in near infinite lambda values. Setting max_height
388+
# based on the percentile should alleviate the impact on plotting.
389+
plot_range = np.hstack([plot_data['bar_tops'], plot_data['bar_bottoms']])
390+
plot_range = plot_range[np.isfinite(plot_range)]
391+
mean_y_center = np.mean([np.max(plot_range), np.min(plot_range)])
392+
max_height = np.diff(np.percentile(plot_range, q=[10,90]))
377393

378394
for i, c in enumerate(chosen_clusters):
379395
c_bounds = plot_data['cluster_bounds'][c]
@@ -383,6 +399,17 @@ def plot(self, leaf_separation=1, cmap='viridis', select_clusters=False,
383399
np.mean([c_bounds[CB_LEFT], c_bounds[CB_RIGHT]]),
384400
np.mean([c_bounds[CB_TOP], c_bounds[CB_BOTTOM]]),
385401
)
402+
403+
# Set center and height to default values if necessary
404+
if not np.isfinite(center[1]):
405+
center = (center[0], mean_y_center)
406+
if not np.isfinite(height):
407+
height = max_height
408+
409+
# Ensure the ellipse is visible
410+
min_height = 0.1*max_height
411+
if height < min_height:
412+
height = min_height
386413

387414
if selection_palette is not None and \
388415
len(selection_palette) >= len(chosen_clusters):

0 commit comments

Comments
 (0)