Skip to content

Commit

Permalink
fix wrong dendrogram leaves determination bug (#1186)
Browse files Browse the repository at this point in the history
* fix wrong leaves determination bug
* add test_dendrogram_ticklabels and comments about why/how to fix the dendrogram bugs.
  • Loading branch information
444thLiao authored and jonmmease committed Sep 25, 2018
1 parent f727c46 commit fac00f1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
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])

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

0 comments on commit fac00f1

Please sign in to comment.