Skip to content

Commit

Permalink
Merge pull request #13 from TutteInstitute/issue_12
Browse files Browse the repository at this point in the history
Allow for no labels per issue #12
  • Loading branch information
lmcinnes authored Feb 29, 2024
2 parents cd45c0d + 6808e34 commit 8409d01
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 154 deletions.
89 changes: 51 additions & 38 deletions datamapplot/create_plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

def create_plot(
data_map_coords,
labels,
labels=None,
*,
title=None,
sub_title=None,
Expand Down Expand Up @@ -155,28 +155,34 @@ def create_plot(
The axes contained within the figure that the plot is rendered to.
"""
cluster_label_vector = np.asarray(labels)
unique_non_noise_labels = [
label for label in np.unique(cluster_label_vector) if label != noise_label
]
if use_medoids:
label_locations = np.asarray(
[
medoid(data_map_coords[cluster_label_vector == i])
for i in unique_non_noise_labels
]
)
if labels is None:
label_locations = np.zeros((0, 2), dtype=np.float32)
label_text = []
cluster_label_vector = np.full(data_map_coords.shape[0], "Unlabelled", dtype=object)
unique_non_noise_labels = []
else:
label_locations = np.asarray(
[
data_map_coords[cluster_label_vector == i].mean(axis=0)
for i in unique_non_noise_labels
]
)
label_text = [
textwrap.fill(x, width=label_wrap_width, break_long_words=False)
for x in unique_non_noise_labels
]
cluster_label_vector = np.asarray(labels)
unique_non_noise_labels = [
label for label in np.unique(cluster_label_vector) if label != noise_label
]
if use_medoids:
label_locations = np.asarray(
[
medoid(data_map_coords[cluster_label_vector == i])
for i in unique_non_noise_labels
]
)
else:
label_locations = np.asarray(
[
data_map_coords[cluster_label_vector == i].mean(axis=0)
for i in unique_non_noise_labels
]
)
label_text = [
textwrap.fill(x, width=label_wrap_width, break_long_words=False)
for x in unique_non_noise_labels
]
if highlight_labels is not None:
highlight_labels = [
textwrap.fill(x, width=label_wrap_width, break_long_words=False)
Expand Down Expand Up @@ -225,7 +231,7 @@ def create_plot(

label_colors = [label_color_map[x] for x in unique_non_noise_labels]

if color_label_text:
if color_label_text and len(label_colors) > 0:
# Darken and reduce chroma of label colors to get text labels
if darkmode:
label_text_colors = pastel_palette(label_colors)
Expand Down Expand Up @@ -421,21 +427,28 @@ def create_interactive_plot(
"""
if len(label_layers) == 0:
return None

label_dataframe = pd.concat(
[
label_text_and_polygon_dataframes(
labels,
data_map_coords,
noise_label=noise_label,
use_medoids=use_medoids,
cluster_polygons=cluster_boundary_polygons,
alpha=polygon_alpha,
)
for labels in label_layers
]
)
label_dataframe = pd.DataFrame(
{
"x": [data_map_coords.T[0].mean()],
"y": [data_map_coords.T[1].mean()],
"label": [""],
"size": [np.power(data_map_coords.shape[0], 0.25)],
}
)
else:
label_dataframe = pd.concat(
[
label_text_and_polygon_dataframes(
labels,
data_map_coords,
noise_label=noise_label,
use_medoids=use_medoids,
cluster_polygons=cluster_boundary_polygons,
alpha=polygon_alpha,
)
for labels in label_layers
]
)

if label_color_map is None:
if cmap is None:
Expand Down
9 changes: 6 additions & 3 deletions datamapplot/interactive_rendering.py
Original file line number Diff line number Diff line change
Expand Up @@ -751,9 +751,12 @@ def render_html(

# Compute text scaling
size_range = label_dataframe["size"].max() - label_dataframe["size"].min()
label_dataframe["size"] = (
label_dataframe["size"] - label_dataframe["size"].min()
) * ((max_fontsize - min_fontsize) / size_range) + min_fontsize
if size_range > 0:
label_dataframe["size"] = (
label_dataframe["size"] - label_dataframe["size"].min()
) * ((max_fontsize - min_fontsize) / size_range) + min_fontsize
else:
label_dataframe["size"] = (max_fontsize + min_fontsize) / 2.0

# Prep data for inlining or storage
if enable_search:
Expand Down
6 changes: 6 additions & 0 deletions datamapplot/palette_handling.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ def palette_from_datamap(
theta_range=np.pi / 16,
radius_weight_power=1.0,
):
if label_locations.shape[0] == 0:
return []

data_center = np.asarray(
umap_coords.min(axis=0)
+ (umap_coords.max(axis=0) - umap_coords.min(axis=0)) / 2
Expand Down Expand Up @@ -143,6 +146,9 @@ def palette_from_cmap_and_datamap(
theta_range=np.pi / 16,
radius_weight_power=1.0,
):
if label_locations.shape[0] == 0:
return [cmap(0.5)]

endpoints = cmap((0.0, 1.0))
endpoint_distance = np.sum((endpoints[0] - endpoints[1]) ** 2)
if endpoint_distance < 0.05:
Expand Down
Loading

0 comments on commit 8409d01

Please sign in to comment.