def test_multi_index_df_to_jinja_table(): # Create multi-index table idx = np.array([['super1', 'super1', 'super1', 'super1', 'super2', 'super2', 'super2'], ['a', 'a', 'b', 'b', 'c', 'c', 'c'], ['aa', 'aa', 'ba', 'bb', 'ca', 'cb', 'cc']]) idx_tuples = list(zip(*idx)) multi_index = pd.MultiIndex.from_tuples(idx_tuples, names=['super-level', 'a-level', 'aa-level']) columns = ['This is an incredibly long column name', 'column2', 'column3', 'column4', 'column5'] data = pd.DataFrame(np.random.rand(7, 5) * 2 - 1, index=multi_index, columns=columns) fmt_expand_multi_index = tf.FmtExpandMultiIndex(operator=tf.OP_SUM, bold=True, hline_color=colors.DARK_BLUE) fmt_nDecimal = tf.FmtDecimals(n=2) fmt_align_cells = tf.FmtAlignCellContents(alignment='right') fmt_heatmap_1 = tf.FmtHeatmap(columns=['column2', 'column3'], rows=['aa', 'ac'], threshold=0., axis=0) fmt_heatmap_2 = tf.FmtHeatmap(columns=['column4', 'column5'], rows=['ca', 'cc'], threshold=0.3, min_color=colors.PURPLE, max_color=colors.ORANGE) fmt_stripes_bg = tf.FmtStripeBackground(first_color=colors.LIGHT_GREY) fmt_rotate_header = tf.FmtHeader(fixed_width='500px', top_padding='200px') formatters = [fmt_expand_multi_index, fmt_align_cells, fmt_stripes_bg, fmt_heatmap_1, fmt_heatmap_2, fmt_rotate_header, fmt_nDecimal] # Create table table = Block(data, formatters=formatters) filename = 'Multi_index_table.pdf' table.save(filename) # And clean up file afterwards os.remove(filename)
def test_FmtHeatmap__get_selected_cell_values(): df_pn = df - 5. fmt = pbtf.FmtHeatmap() res = fmt._get_selected_cell_values(None, None, df_pn) assert res.equals(df_pn) res = fmt._get_selected_cell_values(['a'], ['aa', 'bb'], df) assert res.equals(df.loc[['a'], ['aa', 'bb']])
def test_df_to_jinja_table_add_extra_formatters(): fmt_heatmap = blformat.FmtHeatmap(threshold=0., axis=0) fmt_add_totals_mean = blformat.FmtAppendTotalsRow(row_name='Sum', operator=blformat.OP_SUM, bold=True) fmt_fontsize_20 = blformat.FmtFontsize(fontsize=20) formatters = [fmt_fontsize_20, fmt_heatmap, fmt_add_totals_mean] return Block(FLAT_DATA, formatters=formatters, use_default_formatters=False)
def test_FmtHeatmap_cell_css(): df_pn = df - 5. fmt = pbtf.FmtHeatmap(threshold=0.5) data = FormatterData(df_pn.min().min(), None, None, df_pn) res = fmt._create_cell_level_css(data) assert res == pbtf.CSS_BACKGROUND_COLOR + colors.css_color(colors.HEATMAP_RED) data = FormatterData(df_pn.max().max(), None, None, df_pn) res = fmt._create_cell_level_css(data) assert res == pbtf.CSS_BACKGROUND_COLOR + colors.css_color(colors.HEATMAP_GREEN) data = FormatterData(0.1, None, None, df_pn) res = fmt._create_cell_level_css(data) assert res == pbtf.CSS_BACKGROUND_COLOR + colors.css_color(colors.WHITE)
def test_multi_index_df_to_jinja_table(): # Create multi-index table idx = np.array([['super1', 'super1', 'super1', 'super1', 'super2', 'super2', 'super2'], ['a', 'a', 'b', 'b', 'c', 'c', 'c'], ['aa', 'ab', 'ba', 'bb', 'ca', 'cb', 'cc']]) idx_tuples = list(zip(*idx)) multi_index = pd.MultiIndex.from_tuples(idx_tuples, names=['super-level', 'a-level', 'aa-level']) columns = ['This is an incredibly long column name', 'column2', 'column3', 'column4', 'column5'] data = pd.DataFrame(TABLE_DATA, index=multi_index, columns=columns) fmt_expand_multi_index = blformat.FmtExpandMultiIndex(operator=blformat.OP_SUM, bold=True, hline_color=colors.DARK_BLUE) fmt_ndecimal = blformat.FmtDecimals(n=2) fmt_align_cells = blformat.FmtAlignCellContents(alignment='right') fmt_heatmap_1 = blformat.FmtHeatmap(columns=['column2', 'column3'], rows=['aa', 'ab', 'ac'], threshold=0., axis=0) fmt_heatmap_2 = blformat.FmtHeatmap(columns=['column4', 'column5'], rows=['ca', 'cb', 'cc'], threshold=0.3, min_color=colors.PURPLE, max_color=colors.ORANGE) fmt_rotate_header = blformat.FmtHeader(fixed_width='500px', top_padding='200px') formatters = [fmt_expand_multi_index, fmt_align_cells, fmt_heatmap_1, fmt_heatmap_2, fmt_rotate_header, fmt_ndecimal] return Block(data, formatters=formatters, use_default_formatters=False)
def test_FmtHeatmap__get_min_max_from_selected_cell_values_without_cache(): df_pn = df - 5. cache = None fmt = pbtf.FmtHeatmap(cache=cache) res = fmt._get_min_max_from_selected_cell_values(None, None, df_pn) assert cache is None assert res == (np.nanmin(df_pn), np.nanmax(df_pn)) min_value, max_value = np.nanmin(df.loc[['a'], ['aa', 'bb']]), np.nanmax(df.loc[['a'], ['aa', 'bb']]) res = fmt._get_min_max_from_selected_cell_values(['a'], ['aa', 'bb'], df) assert cache is None assert res == (min_value, max_value) res = fmt._get_min_max_from_selected_cell_values(['a'], ['aa', 'bb'], df) assert cache is None assert res == (min_value, max_value)
def test_FmtHeatmap__get_min_max_from_selected_cell_values_with_cache(): df_pn = df - 5. cache = {} fmt = pbtf.FmtHeatmap(cache=cache) res = fmt._get_min_max_from_selected_cell_values(None, None, df_pn) assert len(cache) == 1 and (None, None) in cache.keys() assert res == (np.nanmin(df_pn), np.nanmax(df_pn)) min_value, max_value = np.nanmin(df.loc[['a'], ['aa', 'bb']]), np.nanmax(df.loc[['a'], ['aa', 'bb']]) res = fmt._get_min_max_from_selected_cell_values(['a'], ['aa', 'bb'], df) assert len(cache) == 2 and (frozenset(['a']), frozenset(['aa', 'bb'])) in cache.keys() assert res == (min_value, max_value) res = fmt._get_min_max_from_selected_cell_values(['a'], ['aa', 'bb'], df) assert len(cache) == 2 and (frozenset(['a']), frozenset(['aa', 'bb'])) in cache.keys() assert res == (min_value, max_value)
def test_df_to_data_tables_jinja_table_extra_formatters_only(): return DataTablesHTMLJinjaTableBlock( FLAT_DATA, formatters=[blformat.FmtHeatmap(threshold=0., axis=0)], use_default_formatters=False)