From 23434ba84a0470681b69df82f2b94a80bbf112cb Mon Sep 17 00:00:00 2001 From: liaoth Date: Tue, 18 Sep 2018 11:43:24 +0800 Subject: [PATCH 1/2] fix wrong leaves determination bug --- plotly/figure_factory/_dendrogram.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/plotly/figure_factory/_dendrogram.py b/plotly/figure_factory/_dendrogram.py index 72fc36c7d36..9dc985f584a 100644 --- a/plotly/figure_factory/_dendrogram.py +++ b/plotly/figure_factory/_dendrogram.py @@ -140,7 +140,10 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None, for i in range(len(yvals_flat)): if yvals_flat[i] == 0.0 and xvals_flat[i] not in self.zero_vals: self.zero_vals.append(xvals_flat[i]) - + if len(self.zero_vals) > len(yvals) + 1: + l_border = int(min(self.zero_vals)) + r_border = int(max(self.zero_vals)) + self.zero_vals = [v for v in range(l_border,r_border + 1, int((r_border-l_border) / len(yvals)))] self.zero_vals.sort() self.layout = self.set_figure_layout(width, height) From 0d85f1db276a60c8595ac7dd3379abbaaf4dcbb4 Mon Sep 17 00:00:00 2001 From: liaoth Date: Thu, 20 Sep 2018 10:37:17 +0800 Subject: [PATCH 2/2] add test_dendrogram_ticklabels and comments about why/how to fix the dendrogram bugs. --- plotly/figure_factory/_dendrogram.py | 13 +++++++++++-- .../test_figure_factory/test_figure_factory.py | 9 +++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/plotly/figure_factory/_dendrogram.py b/plotly/figure_factory/_dendrogram.py index 9dc985f584a..a59d33099d0 100644 --- a/plotly/figure_factory/_dendrogram.py +++ b/plotly/figure_factory/_dendrogram.py @@ -140,12 +140,21 @@ def __init__(self, X, orientation='bottom', labels=None, colorscale=None, for i in range(len(yvals_flat)): if yvals_flat[i] == 0.0 and xvals_flat[i] not in self.zero_vals: self.zero_vals.append(xvals_flat[i]) + 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)) - self.zero_vals = [v for v in range(l_border,r_border + 1, int((r_border-l_border) / len(yvals)))] - self.zero_vals.sort() + 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 diff --git a/plotly/tests/test_optional/test_figure_factory/test_figure_factory.py b/plotly/tests/test_optional/test_figure_factory/test_figure_factory.py index 32c25113859..fb4ad1e4604 100644 --- a/plotly/tests/test_optional/test_figure_factory/test_figure_factory.py +++ b/plotly/tests/test_optional/test_figure_factory/test_figure_factory.py @@ -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):