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

Added link_method parameter #589

Merged
merged 4 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## Unreleased - ???

### Changed
* [#589](https://github.com/plotly/dash-bio/pull/589) Removed hardcoded clustergram linkage method, added parameter `link_method` instead.

## [0.8.0] - 2021-09-27

### Fixed
Expand Down
21 changes: 19 additions & 2 deletions dash_bio/component_factory/_clustergram.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ def Clustergram(
row_dist="euclidean",
col_dist="euclidean",
dist_fun=scs.distance.pdist,
link_fun=lambda x, **kwargs: sch.linkage(x, "complete", **kwargs),
link_fun=None,
link_method=None,
color_threshold=None,
optimal_leaf_order=False,
color_map=None,
Expand Down Expand Up @@ -87,6 +88,11 @@ def Clustergram(
- link_fun (function; default scipy.cluster.hierarchy.linkage): Function to
compute the linkage matrix from the pairwise distances (see docs for
scipy.cluster.hierarchy.linkage).
- link_method (string; default 'complete'): The linkage algorithm to use
if link_fun not set. For method 'single', an optimized algorithm based
on minimum spanning, for methods 'complete', 'average', 'weighted' and
'ward', an algorithm called nearest-neighbors chain is implemented
(see docs for scipy.cluster.hierarchy.linkage).
- color_threshold (dict; default {'row': 0, 'col': 0}): Maximum
linkage value for which unique colors are assigned to clusters;
'row' for rows, and 'col' for columns.
Expand Down Expand Up @@ -162,6 +168,7 @@ def Clustergram(
- width (number; default 500): The width of the graph, in px.

"""

if color_threshold is None:
color_threshold = dict(row=0, col=0)

Expand Down Expand Up @@ -209,7 +216,8 @@ def __init__(
row_dist="euclidean",
col_dist="euclidean",
dist_fun=scs.distance.pdist,
link_fun=lambda x, **kwargs: sch.linkage(x, "complete", **kwargs),
link_fun=None,
link_method=None,
color_threshold=None,
optimal_leaf_order=False,
color_map=None,
Expand Down Expand Up @@ -246,6 +254,15 @@ def __init__(
# Always keep unique identifiers for columns
column_ids = list(range(data.shape[1]))

if link_method is None:
link_method = "complete"

if link_fun is None:
def linkage(x, **kwargs):
return sch.linkage(x, link_method, **kwargs)

link_fun = linkage

self._data = data
self._row_labels = row_labels
self._row_ids = row_ids
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/test_clustergram.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,18 @@ def test_column_labels():
clustered_data = CLUSTERED_DATA.T

assert np.array_equal(curves_dict['heatmap']['z'], clustered_data)


def test_link_method():
"""Test that specifying linkage method."""

data = DATA
_, _, curves_dict = Clustergram(
data,
generate_curves_dict=True,
return_computed_traces=True,
center_values=False,
link_method='centroid'
)
clustered_data = CLUSTERED_DATA
assert not np.array_equal(curves_dict['heatmap']['z'], clustered_data)