Skip to content

Commit

Permalink
When using level-specific colormaps, sort cluster names to assign colors
Browse files Browse the repository at this point in the history
  • Loading branch information
maltekuehl committed Sep 30, 2024
1 parent 6353065 commit beab0ff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
6 changes: 3 additions & 3 deletions docs/source/example.ipynb

Large diffs are not rendered by default.

9 changes: 8 additions & 1 deletion pyclustree/_clustree.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ def clustree(
]

unique_clusters = [np.unique(df_cluster_assignments[key]).tolist() for key in cluster_keys]
unique_clusters_sorted = [sorted(unique_clusters_level) for unique_clusters_level in unique_clusters]

if order_clusters:
unique_clusters = order_unique_clusters(unique_clusters, transition_matrices)
Expand Down Expand Up @@ -211,7 +212,13 @@ def clustree(
plt.cm.get_cmap(node_colormap[i]) if isinstance(node_colormap[i], str) else node_colormap[i]
)
norm_level = plt.Normalize(vmin=0, vmax=len(unique_clusters[i]) - 1)
node_colors.extend([node_colormap_level(norm_level(j)) for j in range(len(unique_clusters[i]))])

node_colors.extend(
[
node_colormap_level(norm_level(unique_clusters_sorted[i].index(unique_cluster)))
for unique_cluster in unique_clusters[i]
]
)

# If a node color gene is provided, use the expression of the gene to color the nodes
if node_color_gene is not None:
Expand Down
5 changes: 4 additions & 1 deletion pyclustree/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ def get_centered_positions(
x_start = -(n_nodes - 1) / 2 * x_spacing # Center the nodes on the x-axis
pos = {}
for i, node in enumerate(nodes):
pos[node] = (x_start + i * x_spacing, -level * y_spacing) # Calculate and assign position
pos[node] = (
x_start + i * x_spacing,
-level * y_spacing,
) # Calculate and assign position
return pos


Expand Down

0 comments on commit beab0ff

Please sign in to comment.