Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve compound boxplot and violinplot aesthetics #92

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 36 additions & 2 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,23 @@ def enable_load_session(season: int | None, event: str | None, session: str | No
return not (season is not None and event is not None and session is not None)


def create_compound_dropdown_options(compounds: Iterable[str]) -> list[dict]:
"""Create compound dropdown options with styling."""
# sort the compounds
compound_order = ["SOFT", "MEDIUM", "HARD", "INTERMEDIATE", "WET"]
compound_index = [compound_order.index(compound) for compound in compounds]
sorted_compounds = sorted(zip(compounds, compound_index), key=lambda x: x[1])
compounds = [compound for compound, _ in sorted_compounds]

return [
{
"label": html.Span(compound, style={"color": COMPOUND_PALETTE[compound]}),
"value": compound,
}
for compound in compounds
]


@callback(
Output("session-info", "data"),
Input("load-session", "n_clicks"),
Expand Down Expand Up @@ -463,12 +480,14 @@ def render_distplot(
@callback(
Output("compound-plot", "figure"),
Input("compounds", "value"),
Input("compound-type", "value"),
Input("compound-unit", "value"),
State("laps", "data"),
State("session-info", "data"),
)
def render_compound_plot(
compounds: list[str],
plot_type: str,
show_seconds: bool,
included_laps: dict,
session_info: Session_info,
Expand All @@ -478,10 +497,25 @@ def render_compound_plot(
return go.Figure()

included_laps = pd.DataFrame.from_dict(included_laps)
included_laps = included_laps[included_laps["Compound"].isin(compounds)]
included_laps = included_laps[
(included_laps["Compound"].isin(compounds)) & (included_laps["PctFromLapRep"] <= 10)
]

y = "DeltaToLapRep" if show_seconds else "PctFromLapRep"
fig = pg.compounds_lineplot(included_laps, y, compounds)
fig = go.Figure()

match plot_type:
case "lineplot":
fig = pg.compounds_lineplot(included_laps, y, compounds)
case "boxplot":
fig = pg.compounds_distplot(included_laps, y, compounds, False)
case "violinplot":
fig = pg.compounds_distplot(included_laps, y, compounds, True)
case _:
# this should never be triggered
# but just in case, return empty plot
return fig

event_name = session_info[1]
fig.update_layout(title=event_name)
return fig
Expand Down
63 changes: 63 additions & 0 deletions f1_visualization/plotly_dash/graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,3 +341,66 @@ def compounds_lineplot(included_laps: pd.DataFrame, y: str, compounds: list[str]
height=500,
)
return fig


def compounds_distplot(
included_laps: pd.DataFrame, y: str, compounds: list[str], violin_plot: bool
) -> go.Figure:
"""PLot compound performance vs tyre age as either a boxplot or violin plot."""
fig = go.Figure()
yaxis_title = "Seconds to LRT" if y == "DeltaToLapRep" else "Percent from LRT"

_, palette, _, _ = _plot_args()
max_stint_length = 0

for compound in compounds:
compound_laps = included_laps[included_laps["Compound"] == compound]
# clip tyre life range to where there are at least three records
tyre_life_range = compound_laps.groupby("TyreLife").size()
tyre_life_range = tyre_life_range[tyre_life_range >= 3].index
max_stint_length = max(max_stint_length, tyre_life_range.max())

compound_laps = compound_laps[compound_laps["TyreLife"].isin(tyre_life_range)]

if violin_plot:
fig.add_trace(
go.Violin(
x=compound_laps["TyreLife"],
y=compound_laps[y],
fillcolor=palette[compound],
line={"color": palette[compound]},
name=compound,
opacity=1,
spanmode="soft",
)
)
else:
fig.add_trace(
go.Box(
x=compound_laps["TyreLife"],
y=compound_laps[y],
boxpoints="outliers",
pointpos=0,
fillcolor=palette[compound],
line={"color": "dimgray"},
name=compound,
showwhiskers=True,
)
)

fig.update_layout(
template="plotly_dark",
boxmode="group",
violinmode="group",
xaxis={
"tickmode": "array",
"tickvals": list(range(5, max_stint_length, 5)),
"title": "Tyre Age",
},
yaxis_title=yaxis_title,
showlegend=False,
autosize=False,
width=1250,
height=500,
)
return fig
41 changes: 29 additions & 12 deletions f1_visualization/plotly_dash/layout.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,18 +279,35 @@ def lap_numbers_slider(slider_id: str, **kwargs) -> dcc.RangeSlider:
compound_plot_caveats,
html.Br(),
dbc.Row(
dbc.Col(
dcc.Dropdown(
options=[
{"label": "Show delta as seconds", "value": True},
{"label": "Show delta as percentages", "value": False},
],
value=True,
clearable=False,
placeholder="Select a unit",
id="compound-unit",
)
)
[
dbc.Col(
dcc.Dropdown(
options=[
{"label": "Lineplot", "value": "lineplot"},
{"label": "Boxplot", "value": "boxplot"},
{"label": "Violin Plot", "value": "violinplot"},
],
value="lineplot",
clearable=False,
placeholder="Select a plot type",
id="compound-type",
),
width=6,
),
dbc.Col(
dcc.Dropdown(
options=[
{"label": "Show delta as seconds", "value": True},
{"label": "Show delta as percentages", "value": False},
],
value=True,
clearable=False,
placeholder="Select a unit",
id="compound-unit",
),
width=6,
),
]
),
html.Br(),
dbc.Row(
Expand Down