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

fix wrong leaves determination bug #1186

Merged
merged 3 commits into from
Sep 25, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 13 additions & 1 deletion plotly/figure_factory/_dendrogram.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,20 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None,
if yvals_flat[i] == 0.0 and xvals_flat[i] not in self.zero_vals:
self.zero_vals.append(xvals_flat[i])

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a nice comment here explaining why this check is here. (e.g. the situation with multiple identical values)

self.zero_vals.sort()
if len(self.zero_vals) > len(yvals) + 1:
# If the length of zero_vals is larger than the length of yvals,
# it means that there are wrong vals because of the identicial samples.
# Three and more identicial samples will make the yvals of spliting center into 0 and it will \
# accidentally take it as leaves.
l_border = int(min(self.zero_vals))
r_border = int(max(self.zero_vals))
correct_leaves_pos = range(l_border,
r_border + 1,
int((r_border - l_border) / len(yvals)))
# Regenerating the leaves pos from the self.zero_vals with equally intervals.
self.zero_vals = [v for v in correct_leaves_pos]

self.zero_vals.sort()
self.layout = self.set_figure_layout(width, height)
self.data = dd_traces

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -777,6 +777,15 @@ def test_dendrogram_colorscale(self):
self.assert_fig_equal(dendro['data'][1], expected_dendro['data'][1])
self.assert_fig_equal(dendro['data'][2], expected_dendro['data'][2])

def test_dendrogram_ticklabels(self):
X = np.array([[1, 2, 3, 4], [1, 2, 3, 4], [1, 3, 5, 6], [1, 4, 2, 3]])
dendro = ff.create_dendrogram(X=X)

expected_ticktext = ['2', '3', '0', '1']
expected_tickvals = [5, 15, 25, 35]

self.assertEqual(len(dendro.layout.xaxis.ticktext), 4)
self.assertEqual(len(dendro.layout.xaxis.tickvals), 4)

class TestTrisurf(NumpyTestUtilsMixin, TestCase):

Expand Down