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

Merged
merged 35 commits into from
Mar 24, 2025
Merged

feat: Add dendrogram #6511

merged 35 commits into from
Mar 24, 2025

Conversation

hoxbro
Copy link
Member

@hoxbro hoxbro commented Feb 18, 2025

resolves #6501 (main part of)

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.18699% with 2 lines in your changes missing coverage. Please review.

Project coverage is 88.85%. Comparing base (bb77ab0) to head (58de7aa).
Report is 12 commits behind head on main.

Files with missing lines Patch % Lines
holoviews/operation/element.py 95.55% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #6511      +/-   ##
==========================================
+ Coverage   88.79%   88.85%   +0.06%     
==========================================
  Files         323      324       +1     
  Lines       68961    69226     +265     
==========================================
+ Hits        61235    61513     +278     
+ Misses       7726     7713      -13     

☔ 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
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?

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 plan to drop 3.9 in the next release (1.21.0), so I will let it slide even though this is correct.

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

operation is typically used to visualize hierarchical clustering of the
data.
"""

Copy link
Member

Choose a reason for hiding this comment

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

Maybe add adjoined as a parameter and if it's false just return the Dendrogram itself.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added in 58de7aa

@holoviz holoviz deleted a comment from review-notebook-app bot Mar 24, 2025
@hoxbro
Copy link
Member Author

hoxbro commented Mar 24, 2025

Are we saving hover support for a future PR?

Yes.

@hoxbro hoxbro enabled auto-merge (squash) March 24, 2025 14:24
@hoxbro hoxbro merged commit eaab13e into main Mar 24, 2025
16 checks passed
@hoxbro hoxbro deleted the dendrogram branch March 24, 2025 14:42
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
3 participants