def test_FmtExpandMultiIndex_cell_css(): mi_df = make_multiindex_table() fmt = pbtf.FmtExpandMultiIndex(bold=True, hline_color=colors.LIGHT_GREY, indent_px=123) df = fmt._modify_dataframe(mi_df) # Because this formatter is quite stateful, index column must be called first data = FormatterData(0., 'a', pbtf.INDEX_COL_NAME, df) res = fmt._create_cell_level_css(data) # Test that level0 row gets index indented assert 'padding-left:0px' in res # Test that level0 row gets all highlighting data = FormatterData(0., 'a', 'column1', df) res = fmt._create_cell_level_css(data) assert pbtf.CSS_BOLD in res assert 'border-bottom' in res assert 'border-top' in res assert colors.css_color(colors.LIGHT_GREY) in res # Run for index column again, so we proceed to next row data = FormatterData(0., 'a', pbtf.INDEX_COL_NAME, df) res = fmt._create_cell_level_css(data) # Test that level1 row gets index indented more and no highlighting data = FormatterData(0., 'aa', pbtf.INDEX_COL_NAME, df) res = fmt._create_cell_level_css(data) assert 'padding-left:123px' in res assert pbtf.CSS_BOLD not in res # Check that bold-highlighting is not used if not desired fmt = pbtf.FmtExpandMultiIndex(bold=False) df = fmt._modify_dataframe(mi_df) data = FormatterData(0., 'a', pbtf.INDEX_COL_NAME, df) res = fmt._create_cell_level_css(data) assert pbtf.CSS_BOLD not in res
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_FmtExpandMultiIndex_modify_dataframe(): mi_df = make_multiindex_table() fmt = pbtf.FmtExpandMultiIndex(operator=pbtf.OP_SUM) res = fmt._modify_dataframe(mi_df) assert res.shape == (6, 4) assert res.index.tolist() == ['a', 'aa', 'ab', 'b', 'ba', 'bb'] assert res.index.name == '' assert res.ix['a'].tolist() == [3., 5., 7., ('a',)] assert fmt.index_level == [0, 1, 1, 0, 1, 1] fmt = pbtf.FmtExpandMultiIndex(operator=pbtf.OP_MEAN) res = fmt._modify_dataframe(mi_df) assert res.ix['a'].tolist() == [1.5, 2.5, 3.5, ('a',)] fmt = pbtf.FmtExpandMultiIndex(operator=pbtf.OP_NONE) res = fmt._modify_dataframe(mi_df) assert res.ix['a'].tolist() == ['', '', '', ('a',)] fmt = pbtf.FmtExpandMultiIndex(operator=pbtf.OP_SUM, total_columns=['column1']) res = fmt._modify_dataframe(mi_df) assert res.ix['a'].tolist() == ['', 5., '', ('a',)]
def test__jinja_hides_multiindex_flattening(): df = pd.DataFrame(columns=['a', 'b', 'c'], index=pd.MultiIndex.from_tuples([('x', 'x'), ('y', 'y'), ('z', 'z')])) table = abt.HTMLJinjaTableBlock(df, formatters=[abtf.FmtExpandMultiIndex()], use_default_formatters=False) container = MagicMock() actual_cfg = MagicMock() table._write_contents(container, actual_cfg) assert "('x', 'x')" not in str(container.append.call_args_list[0][0][0])
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 get_performance_stats_html(self): pct_rows = [('P&L', 'Total return'), ('P&L', 'Annual return'), ('P&L', 'Annual return (asset mode)'), ('Risk-adjusted return based on Drawdown', 'Max percentage drawdown')] dec_rows = [ ('P&L', 'Starting cash'), ('P&L', 'End value'), ('Risk-adjusted return based on Drawdown', 'Max money drawdown'), # Distribution ('Distribution moments', 'Returns volatility'), ('Distribution moments', 'Returns skewness'), ('Distribution moments', 'Returns kurtosis'), # Risk-adjusted return based on Volatility ('Risk-adjusted return based on Volatility', 'Treynor ratio'), ('Risk-adjusted return based on Volatility', 'Sharpe ratio'), ('Risk-adjusted return based on Volatility', 'Information ratio'), # Risk-adjusted return based on Value at Risk ('Risk-adjusted return based on Value at Risk', 'VaR'), ('Risk-adjusted return based on Value at Risk', 'Expected Shortfall'), ('Risk-adjusted return based on Value at Risk', 'Excess var'), ('Risk-adjusted return based on Value at Risk', 'Conditional sharpe ratio'), # Risk-adjusted return based on Lower Partial Moments ('Risk-adjusted return based on Lower Partial Moments', 'Omega ratio'), ('Risk-adjusted return based on Lower Partial Moments', 'Sortino ratio'), ('Risk-adjusted return based on Lower Partial Moments', 'Kappa three ratio'), ('Risk-adjusted return based on Lower Partial Moments', 'Gain loss ratio'), ('Risk-adjusted return based on Lower Partial Moments', 'Upside potential ratio'), # Risk-adjusted return based on Drawdown ('Risk-adjusted return based on Drawdown', 'Calmar ratio') ] fmt_pct = tf.FmtPercent(1, rows=pct_rows, apply_to_header_and_index=False) fmt_dec = tf.FmtDecimals(2, rows=dec_rows, apply_to_header_and_index=False) fmt_align = tf.FmtAlignTable("left") fmt_background = tf.FmtStripeBackground( first_color=tf.colors.LIGHT_GREY, second_color=tf.colors.WHITE, header_color=tf.colors.BLACK) fmt_multiindex = tf.FmtExpandMultiIndex(operator=tf.OP_NONE) perf_data = Block(self.InputList[2], formatters=[ fmt_multiindex, fmt_pct, fmt_dec, fmt_align, fmt_background ], use_default_formatters=False)._repr_html_() perf_data = {'performance_table': perf_data} return perf_data