Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add dendrogram #6511

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open

feat: Add dendrogram #6511

wants to merge 33 commits into from

Conversation

hoxbro
Copy link
Member

@hoxbro hoxbro commented Feb 18, 2025

resolves #6501

TODO:

  • Adjoint plot support responsive (will be done with: Improve responsive in adjoint layout for Bokeh backend #6527)
  • Adjoint plot weird height issue (problem: shared kdims in height axis: hv.Curve([]) << hv.Path([(0, 0), (1, 1)]) << hv.Path([(0, 0), (200, 200)]))
  • Add tests
  • Add examples
  • Don't need to use Dendrogram(zip(...)) for creating the data
  • Add simple dendrogram for mpl and plotly
Screencast.From.2025-02-24.17-41-32.mp4
Details
import random

import pandas as pd
import panel as pn

import holoviews as hv

hv.extension("bokeh")
hv.opts.defaults(
    hv.opts.HeatMap(width=700, height=400, border=0, tools=["hover"]),
)
random.seed(1)


df = pd.DataFrame(
    [(i, chr(65 + j), random.random()) for j in range(10) for i in range(5)],
    columns=["z", "x", "y"]
)
dataset = hv.Dataset(df, vdims="y")  # TODO: Should not be needed

for adjoint_dims in (["x", "z"], ["x"], ["z"]):
    dendro = hv.operation.dendrogram(dataset, adjoint_dims=adjoint_dims, main_dim="y")
    pn.panel(dendro).servable()

Copy link

codecov bot commented Feb 19, 2025

Codecov Report

Attention: Patch coverage is 99.11504% with 2 lines in your changes missing coverage. Please review.

Project coverage is 88.83%. Comparing base (bb77ab0) to head (6957d2a).

Files with missing lines Patch % Lines
holoviews/operation/element.py 95.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6511      +/-   ##
==========================================
+ Coverage   88.79%   88.83%   +0.03%     
==========================================
  Files         323      323              
  Lines       68961    69181     +220     
==========================================
+ Hits        61235    61454     +219     
- Misses       7726     7727       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@droumis
Copy link
Member

droumis commented Feb 24, 2025

@hoxbro , is this ready for review?


adjoint_dims = param.List(item_type=str) # , bounds=(1, 2))

main_element = param.ClassSelector(default=HeatMap, class_=Dataset, instantiate=False, is_instance=False)
Copy link
Member Author

@hoxbro hoxbro Mar 5, 2025

Choose a reason for hiding this comment

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

Maybe also expose some scipy setting for dendrogram and linkage here.

I think optimal_ordering is a good start, ref.

Comment on lines +358 to +364
xaxis=None,
yaxis=None,
show_grid=False,
show_title=False,
show_frame=False,
border=0,
default_tools=[],
Copy link
Member Author

Choose a reason for hiding this comment

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

I am not sure if a blank plot is the best default.

Copy link
Member

Choose a reason for hiding this comment

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

probably fine for now, and easy enough to restore

data[side_dim] = list(0.5 / data_adj.min() * data_adj)
else:
main_src = main.renderers[0].data_source.data
data_adj, data_main = np.asarray(data[side_dim]), np.asarray(main_src.get(main_dim, main_src.get(f"{main_dim}s")))
Copy link
Member Author

Choose a reason for hiding this comment

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

This is not great. Is there a better way to extract data_main?

@hoxbro hoxbro marked this pull request as ready for review March 6, 2025 15:29
else:
main = self.p.main_element(dataset.sort(sort_dims).reindex(element_kdims[:2]), vdims=vdims)

for dim in map(str, main.kdims[::-1]):
Copy link
Member Author

Choose a reason for hiding this comment

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

Likely, this is a bit sensitive to invert_axes.

"source": [
"from holoviews.operation import dendrogram\n",
"\n",
"hv.operation.dendrogram(heatmap, adjoint_dims=[\"z\"], main_dim=\"y\")"
Copy link
Member Author

Choose a reason for hiding this comment

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

Something is wrong with the right plot in the matplotlib backend, e.g., it does not show up:

image

Though there seems to be a general problem with it

image

Copy link
Member

Choose a reason for hiding this comment

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

Can confirm, I see the same.

@droumis droumis requested review from philippjfr, droumis and cvaske and removed request for cvaske March 18, 2025 16:16
@@ -0,0 +1,108 @@
{
Copy link
Member

@droumis droumis Mar 20, 2025

Choose a reason for hiding this comment

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

"In this example we will create a simple tree structure. While the default behavior for dendrograms is to hide axis labels, we will show them for this first example to make it easier to see the relationship between the input data and the output plot."


Reply via ReviewNB

@@ -0,0 +1,108 @@
{
Copy link
Member

@droumis droumis Mar 20, 2025

Choose a reason for hiding this comment

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

dendrogram = hv.Dendrogram(x, y).opts(xaxis=True, yaxis=True)

Reply via ReviewNB

@@ -0,0 +1,108 @@
{
Copy link
Member

@droumis droumis Mar 20, 2025

Choose a reason for hiding this comment

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

"In this example we will create a simple tree structure. While the default behavior for dendrograms is to hide axis labels, we will show them for this first example to make it easier to see the relationship between the input data and the output plot."


Reply via ReviewNB

@@ -0,0 +1,108 @@
{
Copy link
Member

@droumis droumis Mar 20, 2025

Choose a reason for hiding this comment

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

Line #17.    dendrogram = hv.Dendrogram(x, y)

dendrogram = hv.Dendrogram(x, y).opts(xaxis=True, yaxis=True)


Reply via ReviewNB

Copy link
Member

@droumis droumis left a comment

Choose a reason for hiding this comment

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

Are we saving hover support for a future PR?

return super().__new__(cls)

def __init__(self, x, y=None, kdims=None, vdims=None, **params):
data = x if y is None else zip(x, y) # strict=True
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
data = x if y is None else zip(x, y) # strict=True
if y is not None and len(x) != len(y):
raise ValueError("x and y must have the same length.")
data = x if y is None else zip(x, y) # strict=True when 3.9 is dropped

Explicit strict enforcement for now?

Comment on lines +358 to +364
xaxis=None,
yaxis=None,
show_grid=False,
show_title=False,
show_frame=False,
border=0,
default_tools=[],
Copy link
Member

Choose a reason for hiding this comment

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

probably fine for now, and easy enough to restore

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.

Add Dendrogram Element for hierarchical clustering
2 participants