for save_tag, length_stats in all_results.items():
for ref in ['total_base_len', 'total_char_len']:
df = pd.DataFrame.from_dict(length_stats, orient='index').map(lambda d:d['total_tokens_len']/d[ref])
df.loc['mean'] = df.mean()
display(df.style.highlight_min(axis=1, props='font-weight:bold; text-decoration:underline;').background_gradient(cmap='Blues_r', axis=1).set_caption(f"Compression ratios (tokens/{ref}) on for {save_tag}").format("{:.3f}"))
display as 2x2 grid in jupyter (i.e. one df display per 'table cell')
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
To display the DataFrames in a 2x2 grid in Jupyter, you can collect all styled DataFrames first and then format them into an HTML table. Here's the modified code:
python
from IPython.display import display, HTML
# Collect all styled DataFrames
dfs = []
for save_tag, length_stats in all_results.items():
for ref in ['total_base_len', 'total_char_len']:
df = pd.DataFrame.from_dict(length_stats, orient='index').map(lambda d: d['total_tokens_len'] / d[ref])
df.loc['mean'] = df.mean()
styled_df = df.style.highlight_min(axis=1, props='font-weight:bold; text-decoration:underline;') \
.background_gradient(cmap='Blues_r', axis=1) \
.set_caption(f"Compression ratios (tokens/{ref}) on for {save_tag}") \
.format("{:.3f}")
dfs.append(styled_df)
# Create an HTML table with 2 columns per row
html = "<table>"
current_row = []
for df in dfs:
current_row.append(df.render())
if len(current_row) == 2:
html += "<tr>" + "".join(f"<td>{cell}</td>" for cell in current_row) + "</tr>"
current_row = []
# Add any remaining cells if the number of DataFrames isn't even (though assumed to be 4 here)
if current_row:
html += "<tr>" + "".join(f"<td>{cell}</td>" for cell in current_row) + "</tr>"
html += "</table>"
# Display the HTML table
display(HTML(html))
Explanation:
Collect DataFrames: Iterate through all_results and store each styled DataFrame in a list (dfs).
Build HTML Table:
Use a loop to construct an HTML table with two columns per row.
Each DataFrame's HTML representation (from .render()) is placed in a <td> cell.
Display: Use display(HTML(html)) to render the table in Jupyter.
This will arrange the four DataFrames (assuming two save_tags and two ref values) into a 2x2 grid. Each cell contains a styled DataFrame with its caption and formatting.
User
dfs = []
for save_tag, length_stats in all_results.items():
for ref in ['total_base_len', 'total_char_len']:
df = pd.DataFrame.from_dict(length_stats, orient='index').map(lambda d: d['total_tokens_len'] / d[ref])
df.loc['mean'] = df.mean()
dfs.append(HTML(df.style.highlight_min(axis=1, props='font-weight:bold; text-decoration:underline;')
.background_gradient(cmap='Blues_r', axis=1)
.set_caption(f"Compression ratios (tokens/{ref}) for {save_tag}")
.format("{:.3f}")
.to_html()))
To make the captions stand out more, you can apply CSS styling directly to the caption text (e.g., bold text, background color, padding, etc.). Here's how to modify your code:
python
from IPython.display import HTML, display, GridSpec, GridBox
dfs = []
for save_tag, length_stats in all_results.items():
for ref in ['total_base_len', 'total_char_len']:
df = pd.DataFrame.from_dict(length_stats, orient='index').map(lambda d: d['total_tokens_len'] / d[ref])
df.loc['mean'] = df.mean()
# Style the DataFrame and apply CSS to the caption
styled_df = df.style.highlight_min(axis=1, props='font-weight:bold; text-decoration:underline;') \
.background_gradient(cmap='Blues_r', axis=1) \
.set_caption(f"<div style='font-weight: bold; color: #1f77b4; padding: 8px; background-color: #f0f0f0; border-radius: 4px; text-align: center;'>" +
f"Compression ratios (tokens/{ref}) for {save_tag}</div>") \
.format("{:.3f}")
dfs.append(HTML(styled_df.render()))
# Display in a 2x2 grid
display(GridBox(children=dfs, layout={'grid_template_columns': '1fr 1fr'}))
Key Changes:
Stylized Captions:
The caption is wrapped in a <div> with CSS styles for:
font-weight: bold (bold text)
color: #1f77b4 (blue color)
background-color: #f0f0f0 (light gray background)
padding: 8px (spacing around text)
border-radius: 4px (rounded corners)
text-align: center (centered text)
Grid Layout:
Uses GridBox with grid_template_columns: '1fr 1fr' to split into 2 columns (2x2 grid for 4 tables).
Example Output:
Each table will now have a caption that looks like this (simplified CSS visualization):