chriscanal commited on
Commit
65fc294
1 Parent(s): 1d6adda

Changed to Plotly for interactive graphs!

Browse files

suggestion by Nathan Habib implemented. So much better than the dumb printed graphs!

Files changed (1) hide show
  1. src/display_models/plot_results.py +72 -27
src/display_models/plot_results.py CHANGED
@@ -1,8 +1,9 @@
1
  import pandas as pd
2
- import altair as alt
 
3
  import pickle
4
  from datetime import datetime, timezone
5
- from typing import List, Dict, Tuple, Any, Union
6
 
7
  # Average ⬆️ human baseline is 0.897 (source: averaging human baselines below)
8
  # ARC human baseline is 0.80 (source: https://lab42.global/arc/)
@@ -82,6 +83,7 @@ def create_scores_df(results_df: pd.DataFrame) -> pd.DataFrame:
82
  "MMLU": [],
83
  "TruthfulQA": [],
84
  "Result Date": [],
 
85
  }
86
 
87
  # Step 3: Iterate over the rows of the DataFrame and update the scores dictionary
@@ -92,6 +94,9 @@ def create_scores_df(results_df: pd.DataFrame) -> pd.DataFrame:
92
  if not scores[column] or scores[column][-1] <= date:
93
  scores[column].append(date)
94
  continue
 
 
 
95
  current_max = scores[column][-1] if scores[column] else float("-inf")
96
  scores[column].append(max(current_max, row[column]))
97
 
@@ -114,7 +119,7 @@ def create_plot_df(scores_df: pd.DataFrame) -> pd.DataFrame:
114
 
115
  # Iterate over the cols and create a new DataFrame for each column
116
  for col in cols:
117
- d = scores_df[[col, "Result Date"]].copy().reset_index(drop=True)
118
  d["Metric Name"] = col
119
  d.rename(columns={col: "Metric Value"}, inplace=True)
120
  dfs.append(d)
@@ -133,37 +138,77 @@ def create_plot_df(scores_df: pd.DataFrame) -> pd.DataFrame:
133
  return concat_df
134
 
135
 
136
- def create_metric_plot_obj(df: pd.DataFrame, metrics: List[str], human_baselines: Dict[str, float]) -> alt.LayerChart:
 
 
137
  """
138
- Creates a visualization of metrics over time compared to human baselines.
139
-
140
- :param df: A DataFrame containing 'Metric Name', 'Metric Value', and 'Result Date' columns.
141
- :param metrics: A list of metric names to be included in the plot.
142
- :param human_baselines: A dictionary mapping metric names to their corresponding human baseline values.
143
- :return: An Altair LayerChart object visualizing the metrics over time.
 
 
 
 
 
144
  """
145
- # Filter the DataFrame based on the metrics parameter
 
146
  df = df[df["Metric Name"].isin(metrics)]
147
 
148
- # Filter the human_baselines dictionary to include only the specified metrics
149
  filtered_human_baselines = {k: v for k, v in human_baselines.items() if k in metrics}
150
 
151
- # Create a DataFrame from filtered human baselines
152
- human_baselines_df = pd.DataFrame(list(filtered_human_baselines.items()), columns=["Metric Name", "Metric Value"])
153
-
154
- # Create the lines chart for each metric over time.
155
- base = alt.Chart(df).encode(x="Result Date:T")
156
- lines = base.mark_line().encode(
157
- alt.Y("Metric Value:Q", scale=alt.Scale(domain=[0, 100])),
158
- color="Metric Name:N",
 
159
  )
160
 
161
- # Create the rules (horizontal lines) chart for the human baselines.
162
- yrules = (
163
- alt.Chart(human_baselines_df)
164
- .mark_rule(strokeDash=[12, 6], size=2)
165
- .encode(y="Metric Value:Q", color="Metric Name:N")
 
 
 
 
 
166
  )
167
 
168
- # Combine lines with yrules and return the chart.
169
- return lines + yrules
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import pandas as pd
2
+ import plotly.express as px
3
+ from plotly.graph_objs import Figure
4
  import pickle
5
  from datetime import datetime, timezone
6
+ from typing import List, Dict, Tuple, Any
7
 
8
  # Average ⬆️ human baseline is 0.897 (source: averaging human baselines below)
9
  # ARC human baseline is 0.80 (source: https://lab42.global/arc/)
 
83
  "MMLU": [],
84
  "TruthfulQA": [],
85
  "Result Date": [],
86
+ "Model Name": [],
87
  }
88
 
89
  # Step 3: Iterate over the rows of the DataFrame and update the scores dictionary
 
94
  if not scores[column] or scores[column][-1] <= date:
95
  scores[column].append(date)
96
  continue
97
+ if column == "Model Name":
98
+ scores[column].append(row["model_name_for_query"])
99
+ continue
100
  current_max = scores[column][-1] if scores[column] else float("-inf")
101
  scores[column].append(max(current_max, row[column]))
102
 
 
119
 
120
  # Iterate over the cols and create a new DataFrame for each column
121
  for col in cols:
122
+ d = scores_df[[col, "Model Name", "Result Date"]].copy().reset_index(drop=True)
123
  d["Metric Name"] = col
124
  d.rename(columns={col: "Metric Value"}, inplace=True)
125
  dfs.append(d)
 
138
  return concat_df
139
 
140
 
141
+ def create_metric_plot_obj(
142
+ df: pd.DataFrame, metrics: List[str], human_baselines: Dict[str, float], title: str
143
+ ) -> Figure:
144
  """
145
+ Create a Plotly figure object with lines representing different metrics
146
+ and horizontal dotted lines representing human baselines.
147
+
148
+ :param df: The DataFrame containing the metric values, names, and dates.
149
+ :param metrics: A list of strings representing the names of the metrics
150
+ to be included in the plot.
151
+ :param human_baselines: A dictionary where keys are metric names
152
+ and values are human baseline values for the metrics.
153
+ :param title: A string representing the title of the plot.
154
+ :return: A Plotly figure object with lines representing metrics and
155
+ horizontal dotted lines representing human baselines.
156
  """
157
+
158
+ # Filter the DataFrame based on the specified metrics
159
  df = df[df["Metric Name"].isin(metrics)]
160
 
161
+ # Filter the human baselines based on the specified metrics
162
  filtered_human_baselines = {k: v for k, v in human_baselines.items() if k in metrics}
163
 
164
+ # Create a line figure using plotly express with specified markers and custom data
165
+ fig = px.line(
166
+ df,
167
+ x="Result Date",
168
+ y="Metric Value",
169
+ color="Metric Name",
170
+ markers=True,
171
+ custom_data=["Metric Name", "Metric Value", "Model Name"],
172
+ title=title,
173
  )
174
 
175
+ # Update hovertemplate for better hover interaction experience
176
+ fig.update_traces(
177
+ hovertemplate="<br>".join(
178
+ [
179
+ "Model Name: %{customdata[2]}",
180
+ "Metric Name: %{customdata[0]}",
181
+ "Date: %{x}",
182
+ "Metric Value: %{y}",
183
+ ]
184
+ )
185
  )
186
 
187
+ # Create a dictionary to hold the color mapping for each metric
188
+ metric_color_mapping = {}
189
+
190
+ # Map each metric name to its color in the figure
191
+ for trace in fig.data:
192
+ metric_color_mapping[trace.name] = trace.line.color
193
+
194
+ # Iterate over filtered human baselines and add horizontal lines to the figure
195
+ for metric, value in filtered_human_baselines.items():
196
+ color = metric_color_mapping.get(metric, "blue") # Retrieve color from mapping; default to blue if not found
197
+ location = "top left" if metric == "HellaSwag" else "bottom left" # Set annotation position
198
+ # Add horizontal line with matched color and positioned annotation
199
+ fig.add_hline(
200
+ y=value,
201
+ line_dash="dot",
202
+ annotation_text=f"{metric} human baseline",
203
+ annotation_position=location,
204
+ annotation_font_size=10,
205
+ annotation_font_color=color,
206
+ line_color=color,
207
+ )
208
+
209
+ return fig
210
+
211
+
212
+ # Example Usage:
213
+ # human_baselines dictionary is defined.
214
+ # chart = create_metric_plot_obj(scores_df, ["ARC", "HellaSwag", "MMLU", "TruthfulQA"], human_baselines, "Graph Title")