Пример #1
0
    def test_default_dendrogram(self):
        X = np.array([[1, 2, 3, 4], [1, 1, 3, 4], [1, 2, 1, 4], [1, 2, 3, 1]])
        dendro = tls.FigureFactory.create_dendrogram(X=X)

        expected_dendro = go.Figure(
            data=go.Data([
                go.Scatter(x=np.array([25., 25., 35., 35.]),
                           y=np.array([0., 1., 1., 0.]),
                           marker=go.Marker(color='rgb(61,153,112)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y'),
                go.Scatter(x=np.array([15., 15., 30., 30.]),
                           y=np.array([0., 2.23606798, 2.23606798, 1.]),
                           marker=go.Marker(color='rgb(61,153,112)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y'),
                go.Scatter(x=np.array([5., 5., 22.5, 22.5]),
                           y=np.array([0., 3.60555128, 3.60555128,
                                       2.23606798]),
                           marker=go.Marker(color='rgb(0,116,217)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y')
            ]),
            layout=go.Layout(autosize=False,
                             height='100%',
                             hovermode='closest',
                             showlegend=False,
                             width='100%',
                             xaxis=go.XAxis(mirror='allticks',
                                            rangemode='tozero',
                                            showgrid=False,
                                            showline=True,
                                            showticklabels=True,
                                            tickmode='array',
                                            ticks='outside',
                                            ticktext=np.array(
                                                ['3', '2', '0', '1']),
                                            tickvals=[5.0, 15.0, 25.0, 35.0],
                                            type='linear',
                                            zeroline=False),
                             yaxis=go.YAxis(mirror='allticks',
                                            rangemode='tozero',
                                            showgrid=False,
                                            showline=True,
                                            showticklabels=True,
                                            ticks='outside',
                                            type='linear',
                                            zeroline=False)))

        self.assertEqual(len(dendro['data']), 3)

        # this is actually a bit clearer when debugging tests.
        self.assert_dict_equal(dendro['data'][0], expected_dendro['data'][0])
        self.assert_dict_equal(dendro['data'][1], expected_dendro['data'][1])
        self.assert_dict_equal(dendro['data'][2], expected_dendro['data'][2])

        self.assert_dict_equal(dendro['layout'], expected_dendro['layout'])
Пример #2
0
def make_violin_rugplot(vals, pdf_max, distance, color='#1f77b4'):
    """
    Returns a rugplot fig for a violin plot.
    """
    return graph_objs.Scatter(y=vals,
                              x=[-pdf_max - distance] * len(vals),
                              marker=graph_objs.Marker(color=color,
                                                       symbol='line-ew-open'),
                              mode='markers',
                              name='',
                              showlegend=False,
                              hoverinfo='y')
Пример #3
0
    def get_dendrogram_traces(self, X, colorscale):
        from plotly.graph_objs import graph_objs

        Z = sch.linkage(X, method=self.method, metric=self.metric)
        P = sch.dendrogram(Z,
                           orientation=self.orientation,
                           labels=self.labels,
                           no_plot=True)

        icoord = scp.array(P['icoord'])
        dcoord = scp.array(P['dcoord'])
        ordered_labels = scp.array(P['ivl'])
        color_list = scp.array(P['color_list'])
        colors = self.get_color_dict(colorscale)

        trace_list = []

        def trace_axis(axis):
            try:
                return axis[0] + str(int(axis[-1]))
            except:
                return axis[0]

        for xs, ys, color_key in zip(icoord, dcoord, color_list):
            if self.orientation not in ['top', 'bottom']:
                xs, ys = ys, xs

            trace = graph_objs.Scatter(
                x=xs * self.sign[self.xaxis],
                y=ys * self.sign[self.yaxis],
                mode='lines',
                xaxis=trace_axis(self.xaxis),
                yaxis=trace_axis(self.yaxis),
                marker=graph_objs.Marker(color=colors[color_key]),
            )

            trace_list.append(trace)

        return trace_list, icoord, dcoord, ordered_labels, P['leaves']
Пример #4
0
    def test_dendrogram_colorscale(self):
        X = np.array([[1, 2, 3, 4], [1, 1, 3, 4], [1, 2, 1, 4], [1, 2, 3, 1]])
        greyscale = [
            'rgb(0,0,0)',  # black
            'rgb(05,105,105)',  # dim grey
            'rgb(128,128,128)',  # grey
            'rgb(169,169,169)',  # dark grey
            'rgb(192,192,192)',  # silver
            'rgb(211,211,211)',  # light grey
            'rgb(220,220,220)',  # gainsboro
            'rgb(245,245,245)'
        ]  # white smoke

        dendro = tls.FigureFactory.create_dendrogram(X, colorscale=greyscale)

        expected_dendro = go.Figure(
            data=go.Data([
                go.Scatter(x=np.array([25., 25., 35., 35.]),
                           y=np.array([0., 1., 1., 0.]),
                           marker=go.Marker(color='rgb(128,128,128)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y'),
                go.Scatter(x=np.array([15., 15., 30., 30.]),
                           y=np.array([0., 2.23606798, 2.23606798, 1.]),
                           marker=go.Marker(color='rgb(128,128,128)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y'),
                go.Scatter(x=np.array([5., 5., 22.5, 22.5]),
                           y=np.array([0., 3.60555128, 3.60555128,
                                       2.23606798]),
                           marker=go.Marker(color='rgb(0,0,0)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y')
            ]),
            layout=go.Layout(autosize=False,
                             height='100%',
                             hovermode='closest',
                             showlegend=False,
                             width='100%',
                             xaxis=go.XAxis(mirror='allticks',
                                            rangemode='tozero',
                                            showgrid=False,
                                            showline=True,
                                            showticklabels=True,
                                            tickmode='array',
                                            ticks='outside',
                                            ticktext=np.array(
                                                ['3', '2', '0', '1']),
                                            tickvals=[5.0, 15.0, 25.0, 35.0],
                                            type='linear',
                                            zeroline=False),
                             yaxis=go.YAxis(mirror='allticks',
                                            rangemode='tozero',
                                            showgrid=False,
                                            showline=True,
                                            showticklabels=True,
                                            ticks='outside',
                                            type='linear',
                                            zeroline=False)))

        self.assertEqual(len(dendro['data']), 3)

        # this is actually a bit clearer when debugging tests.
        self.assert_dict_equal(dendro['data'][0], expected_dendro['data'][0])
        self.assert_dict_equal(dendro['data'][1], expected_dendro['data'][1])
        self.assert_dict_equal(dendro['data'][2], expected_dendro['data'][2])
Пример #5
0
    def test_dendrogram_random_matrix(self):

        # create a random uncorrelated matrix
        X = np.random.rand(5, 5)

        # variable 2 is correlated with all the other variables
        X[2, :] = sum(X, 0)

        names = ['Jack', 'Oxana', 'John', 'Chelsea', 'Mark']
        dendro = tls.FigureFactory.create_dendrogram(X, labels=names)

        expected_dendro = go.Figure(
            data=go.Data([
                go.Scatter(marker=go.Marker(color='rgb(61,153,112)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y'),
                go.Scatter(marker=go.Marker(color='rgb(61,153,112)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y'),
                go.Scatter(marker=go.Marker(color='rgb(61,153,112)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y'),
                go.Scatter(marker=go.Marker(color='rgb(0,116,217)'),
                           mode='lines',
                           xaxis='x',
                           yaxis='y')
            ]),
            layout=go.Layout(autosize=False,
                             height='100%',
                             hovermode='closest',
                             showlegend=False,
                             width='100%',
                             xaxis=go.XAxis(
                                 mirror='allticks',
                                 rangemode='tozero',
                                 showgrid=False,
                                 showline=True,
                                 showticklabels=True,
                                 tickmode='array',
                                 ticks='outside',
                                 tickvals=[5.0, 15.0, 25.0, 35.0, 45.0],
                                 type='linear',
                                 zeroline=False),
                             yaxis=go.YAxis(mirror='allticks',
                                            rangemode='tozero',
                                            showgrid=False,
                                            showline=True,
                                            showticklabels=True,
                                            ticks='outside',
                                            type='linear',
                                            zeroline=False)))

        self.assertEqual(len(dendro['data']), 4)

        # it's random, so we can only check that the values aren't equal
        y_vals = [
            dendro['data'][0].pop('y'), dendro['data'][1].pop('y'),
            dendro['data'][2].pop('y'), dendro['data'][3].pop('y')
        ]
        for i in range(len(y_vals)):
            for j in range(len(y_vals)):
                if i != j:
                    self.assertFalse(np.allclose(y_vals[i], y_vals[j]))

        x_vals = [
            dendro['data'][0].pop('x'), dendro['data'][1].pop('x'),
            dendro['data'][2].pop('x'), dendro['data'][3].pop('x')
        ]
        for i in range(len(x_vals)):
            for j in range(len(x_vals)):
                if i != j:
                    self.assertFalse(np.allclose(x_vals[i], x_vals[j]))

        # we also need to check the ticktext manually
        xaxis_ticktext = dendro['layout']['xaxis'].pop('ticktext')
        self.assertEqual(xaxis_ticktext[0], 'John')

        # this is actually a bit clearer when debugging tests.
        self.assert_dict_equal(dendro['data'][0], expected_dendro['data'][0])
        self.assert_dict_equal(dendro['data'][1], expected_dendro['data'][1])
        self.assert_dict_equal(dendro['data'][2], expected_dendro['data'][2])
        self.assert_dict_equal(dendro['data'][3], expected_dendro['data'][3])

        self.assert_dict_equal(dendro['layout'], expected_dendro['layout'])
    def get_dendrogram_traces(self, X, colorscale, distfun, linkagefun,
                              hovertext):
        """
        Calculates all the elements needed for plotting a dendrogram.

        :param (ndarray) X: Matrix of observations as array of arrays
        :param (list) colorscale: Color scale for dendrogram tree clusters
        :param (function) distfun: Function to compute the pairwise distance
                                   from the observations
        :param (function) linkagefun: Function to compute the linkage matrix
                                      from the pairwise distances
        :param (list) hovertext: List of hovertext for constituent traces of dendrogram
        :rtype (tuple): Contains all the traces in the following order:
            (a) trace_list: List of Plotly trace objects for dendrogram tree
            (b) icoord: All X points of the dendrogram tree as array of arrays
                with length 4
            (c) dcoord: All Y points of the dendrogram tree as array of arrays
                with length 4
            (d) ordered_labels: leaf labels in the order they are going to
                appear on the plot
            (e) P['leaves']: left-to-right traversal of the leaves

        """
        d = distfun(X)
        Z = linkagefun(d)
        P = sch.dendrogram(Z,
                           orientation=self.orientation,
                           labels=self.labels,
                           no_plot=True)

        icoord = scp.array(P['icoord'])
        dcoord = scp.array(P['dcoord'])
        ordered_labels = scp.array(P['ivl'])
        color_list = scp.array(P['color_list'])
        colors = self.get_color_dict(colorscale)

        trace_list = []

        for i in range(len(icoord)):
            # xs and ys are arrays of 4 points that make up the '∩' shapes
            # of the dendrogram tree
            if self.orientation in ['top', 'bottom']:
                xs = icoord[i]
            else:
                xs = dcoord[i]

            if self.orientation in ['top', 'bottom']:
                ys = dcoord[i]
            else:
                ys = icoord[i]
            color_key = color_list[i]
            hovertext_label = None
            if hovertext:
                hovertext_label = hovertext[i]
            trace = graph_objs.Scatter(
                x=np.multiply(self.sign[self.xaxis], xs),
                y=np.multiply(self.sign[self.yaxis], ys),
                mode='lines',
                marker=graph_objs.Marker(color=colors[color_key]),
                text=hovertext_label,
                hoverinfo='text')

            try:
                x_index = int(self.xaxis[-1])
            except ValueError:
                x_index = ''

            try:
                y_index = int(self.yaxis[-1])
            except ValueError:
                y_index = ''

            trace['xaxis'] = 'x' + x_index
            trace['yaxis'] = 'y' + y_index

            trace_list.append(trace)

        return trace_list, icoord, dcoord, ordered_labels, P['leaves']