Skip to content

Conversation

@LizaShch
Copy link
Collaborator

Summary:

Updated dimensionality reduction plot to take in pin_colors keyword argument which allows users to pass in a string of a key located in ann-data uns. It then grabs the dictionary/color mapping and passes into 2dscatter function.

Changes:

  • Adds color_mapping string keyword
  • Changes the ordering of coloring for the 2dscatter, using color_map as an if/else condition.
  • Accidentally added the changes from scatter into this one due to changing keyword condition.

raise ValueError("x and y must have the same length.")
if labels is not None and len(labels) != len(x):
raise ValueError("Labels length should match x and y length.")
if color_map is not None:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checking if color_map is None isn't needed, isinstance will correctly check if color_map is dict, even if color_map is None

if color_map is not None:
if not isinstance(color_map, dict):
raise ValueError("`color_map` must be a dict mapping label→color.")
color_dict = color_map
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reassignment isn't needed, since you're just assigning it to cluster_to_color again down below. You can just set cluster_to_color = color_map online 146

# Use the number of unique clusters to set the colormap length
cmap = ListedColormap(colors[:len(unique_clusters)])
if color_map is not None:
cluster_to_color = color_dict
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cluster_to_color = color_map

cmap2 = plt.get_cmap('tab20b')
cmap3 = plt.get_cmap('tab20c')
colors = cmap1.colors + cmap2.colors + cmap3.colors
cluster_to_color = {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing the whole if statement and just doing this might be more concise:

cmap1 = plt.get_cmap('tab20')
cmap2 = plt.get_cmap('tab20b')
cmap3 = plt.get_cmap('tab20c')
colors = cmap1.colors + cmap2.colors + cmap3.colors
cluster_to_color = color_map if color_map is not None else {
    str(cluster): colors[i % len(colors)]
    for i, cluster in enumerate(unique_clusters)
}

@LizaShch LizaShch requested a review from Ahmad8864 April 24, 2025 21:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants