Exemplo n.º 1
0
 def test_skip_zero_cluster_cols(self):
     clustered = cluster(self.dataframe, cluster_cols=True, skip_zero=True)
     self.assertEqual(
         clustered.as_matrix().tolist(),
         [[0, 0, 4, 5], [8, 8, 4, 5], [1, 1, 4, 5], [9, 9, 4, 5]])
     self.assertEqual(clustered.columns.tolist(), ['c1', 'c3', 'c2', 'c4'])
     self.assertEqual(clustered.index.tolist(), ['r1', 'r3', 'r4', 'r6'])
Exemplo n.º 2
0
 def assert_no_change(self, array):
     dataframe = pandas.DataFrame(array)
     clustered = cluster(dataframe,
                         skip_zero=False,
                         cluster_rows=True,
                         cluster_cols=True)
     self.assertEqual(clustered.as_matrix().tolist(), array)
Exemplo n.º 3
0
 def test_pass_through(self):
     clustered = cluster(self.dataframe,
                         skip_zero=False,
                         cluster_rows=False,
                         cluster_cols=False)
     self.assertEqual(clustered.as_matrix().tolist(),
                      [[0, 4, 0, 5], [0, 0, 0, 0], [8, 4, 8, 5],
                       [1, 4, 1, 5], [0, 0, 0, 0], [9, 4, 9, 5]])
     self.assertEqual(clustered.columns.tolist(), ['c1', 'c2', 'c3', 'c4'])
     self.assertEqual(clustered.index.tolist(),
                      ['r1', 'r2', 'r3', 'r4', 'r5', 'r6'])
Exemplo n.º 4
0
 def test_no_skip_cluster_both(self):
     clustered = cluster(self.dataframe,
                         skip_zero=False,
                         cluster_rows=True,
                         cluster_cols=True)
     self.assertEqual(clustered.as_matrix().tolist(), [
         [8, 8, 4, 5],
         [9, 9, 4, 5],
         [0, 0, 0, 0],
         [0, 0, 0, 0],
         [0, 0, 4, 5],
         [1, 1, 4, 5],
     ])
     self.assertEqual(clustered.columns.tolist(), ['c1', 'c3', 'c2', 'c4'])
     self.assertEqual(clustered.index.tolist(),
                      ['r3', 'r6', 'r2', 'r5', 'r1', 'r4'])
Exemplo n.º 5
0
    def _update_heatmap(self, selected_conditions_ids_json,
                        selected_gene_ids_json, scale, palette, cluster_rows,
                        cluster_cols, label_rows_mode, label_cols_mode,
                        row_scaling_mode):
        selected_conditions = (json.loads(selected_conditions_ids_json)
                               or self._conditions)
        base_df = self._scale_dataframe(row_scaling_mode)
        selected_conditions_df = base_df[selected_conditions]
        selected_conditions_genes_df = self._filter_by_gene_ids_json(
            selected_conditions_df, selected_gene_ids_json)
        truncated_dataframe = (
            sort_by_variance(selected_conditions_genes_df).head(
                self._most_variable_rows)
            if self._most_variable_rows else selected_conditions_genes_df)
        cluster_dataframe = cluster(truncated_dataframe,
                                    cluster_rows=(cluster_rows == 'cluster'),
                                    cluster_cols=(cluster_cols == 'cluster'))

        show_genes = (len(cluster_dataframe.index.tolist()) < 40
                      and label_rows_mode == 'auto') or \
            label_rows_mode == 'always'
        show_conditions = label_cols_mode in ['always', 'auto']

        # With a proportional font, this is only an estimate.
        char_width = 10

        if show_genes:
            row_max = max([len(s) for s in list(cluster_dataframe.index)])
            left_margin = row_max * char_width
        else:
            left_margin = 75

        if show_conditions:
            col_max = max([len(s) for s in list(cluster_dataframe)])
            bottom_margin = col_max * char_width
        else:
            bottom_margin = 10

        return {
            'data': [
                self._heatmap(cluster_dataframe,
                              is_log_scale=(scale == 'log'),
                              palette=palettes[palette])
            ],
            'layout':
            go.Layout(
                xaxis={
                    'ticks': '',
                    'showticklabels': show_conditions,
                    'tickangle': 90
                },
                yaxis={
                    'ticks': '',
                    'showticklabels': show_genes
                },
                margin={
                    'l': left_margin,
                    'b': bottom_margin,
                    't': 30,  # so infobox on hover is not truncated
                    'r': 0
                })
        }