import logging
logging.getLogger("matplotlib.font_manager").setLevel(logging.ERROR)
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.patches import Circle, Ellipse, FancyArrowPatch, FancyBboxPatch, RegularPolygon
from gridiron import Play, Player, Field, offense, technique, style, routes as r
style.apply()
GREEN = "#0f7a55" # darker step of the aqua highlight, for text on the light field
OL_KEYS = ("LT", "LG", "C", "RG", "RT")
def pdf_build():
return os.environ.get("RTG_FORMAT", "").lower() in ("pdf", "typst", "print")
def O(key, pos, d, w, label):
return Player(key, pos, "off", d, w, label=label)
def D(key, pos, d, w, label):
return Player(key, pos, "def", d, w, label=label)
def line():
"""The five offensive linemen with one-letter labels, T G C G T."""
short = {"LT": "T", "LG": "G", "C": "C", "RG": "G", "RT": "T"}
return [p.moved(label=short[p.key]) for p in offense("singleback") if p.key in OL_KEYS]
def T(t, side=1):
"""Width of technique t on the offense's right (side=+1) or left (side=-1)."""
return technique(t, side)
def split_backs(y_side=1, z_w=13.0, x_w=-13.0, h_side=-1):
"""Walsh's base look: split backs (the pro set), one back behind each tackle, a tight end, a split end (X)
and a flanker (Z). The halfback (H) is on the offense's left by default; h_side=+1 puts him on the right."""
return line() + [O("QB", "QB", -1.9, 0.0, "Q"), O("HB", "RB", -5.0, 3.2 * h_side, "H"),
O("FB", "FB", -5.0, -3.2 * h_side, "F"),
O("Y", "TE", -0.7, 4.8 * y_side, "Y"), O("X", "WR", -0.7, x_w, "X"),
O("Z", "WR", -1.5, z_w, "Z")]
def era_43(shell="one", ss_side=1):
"""A 1980s 4-3 Over against a pro set (tight end on the offense's right), corners 7 off."""
dfn = [D("WE", "DE", 1.0, T("5", -1), "E"), D("WT", "DT", 1.0, T("1", -1), "T"),
D("ST", "DT", 1.0, T("3"), "T"), D("SE", "DE", 1.0, T("5") + 0.2, "E"),
D("SAM", "LB", 4.0, 6.4, "S"), D("MIKE", "LB", 4.5, 0.8, "M"), D("WILL", "LB", 4.5, -3.0, "W"),
D("LCB", "CB", 7.0, -13.5, "CB"), D("RCB", "CB", 7.0, 13.5, "CB")]
if shell == "two":
dfn += [D("FS", "FS", 13.0, -8.0, "FS"), D("SS", "SS", 13.0, 8.0, "SS")]
else:
dfn += [D("FS", "FS", 13.0, 0.0, "FS"), D("SS", "SS", 8.0, 7.5 * ss_side, "SS")]
return dfn
def go_to(p, key, end, kind="block", delay=0.0, speed=None, via=()):
"""Move `key` through absolute (d, w) points `via` to `end`, as a block, pull, path, run, rush or route."""
start = np.array(p._cur(key))
pts = [tuple(np.array(q) - start) for q in (*via, end)]
if kind == "block":
return p.block(key, pts=pts, delay=delay, speed=speed)
if kind == "pull":
return p.pull(key, pts, delay=delay, speed=speed)
if kind == "rush":
return p.rush(key, pts=pts, delay=delay, speed=speed)
if kind == "route":
return p.route(key, [tuple(q) for q in (*via, end)], absolute=True, delay=delay, speed=speed)
return {"path": p.path, "run": p.run}[kind](key, pts, delay=delay, speed=speed)
def pass_pro(p, keys=OL_KEYS, depth=-1.2):
for k in keys:
p.block(k, pts=[(depth + 0.7, 0.0)])
def _P(fld, p, d, w):
return fld.xy(*p.to_abs(d, w))
def say(fld, p, d, w, text, color=style.INK_2, fontsize=7.2, ha="center", weight="normal", alpha=0.92):
"""A short on-field label at (d, w)."""
x, y = _P(fld, p, d, w)
fld.ax.text(float(x), float(y), text, fontsize=fontsize, color=color, ha=ha, va="center", zorder=9,
fontweight=weight, clip_on=True,
bbox=dict(boxstyle="round,pad=0.18", fc=style.SURFACE, ec="none", alpha=alpha))
def point_to(fld, p, text_at, target, text, color=style.INK_2, fontsize=7, shrink=7):
"""A label with a thin leader line to the player or spot it names."""
(xt, yt), (xp, yp) = _P(fld, p, *text_at), _P(fld, p, *target)
fld.ax.annotate(text, (float(xp), float(yp)), xytext=(float(xt), float(yt)), fontsize=fontsize,
color=color, ha="center", va="center", zorder=9,
arrowprops=dict(arrowstyle="-", color=color, lw=0.7, shrinkA=2, shrinkB=shrink),
bbox=dict(boxstyle="round,pad=0.18", fc=style.SURFACE, ec="none", alpha=0.92))
def num(fld, p, d, w, n, color=GREEN):
"""A numbered progression circle."""
x, y = (float(v) for v in _P(fld, p, d, w))
fld.ax.add_patch(Circle((x, y), 0.85, fc="white", ec=color, lw=1.4, zorder=10))
fld.ax.text(x, y, str(n), ha="center", va="center", fontsize=7, color=color, fontweight="bold", zorder=11)
def clean(ax, numbers=True):
"""Drop hash ticks (and optionally yard numbers); clip text to the window."""
for ln in list(ax.lines):
xs, ys = ln.get_data()
if len(xs) == 2 and ln.get_linewidth() == 0.5 and np.hypot(xs[1] - xs[0], ys[1] - ys[0]) < 1.0:
ln.remove()
for t in list(ax.texts):
if numbers and t.get_text().isdigit() and t.get_alpha() == 0.55:
t.remove()
else:
t.set_clip_on(True)
def draw(p, width_in=6.2, ax=None, numbers=True, **kw):
"""Play.draw, sized for the page, cleaned for a zoomed window."""
fld = p.draw(width_in=width_in, ax=ax, **kw)
clean(fld.ax, numbers=numbers)
return fld
def ruler(fld, p, depths=(5, 10, 15), fs=6.5):
"""Depth labels (yards past the line of scrimmage) along the left edge of the window."""
x_left = (160 / 3) - fld.y1 + 0.4
for d in depths:
y = p.los + d
if fld.x0 + 0.8 <= y <= fld.x1 - 0.8:
fld.ax.text(x_left, y, f"{d}" + (" yd" if d == depths[0] else ""), fontsize=fs, color=style.INK_2,
ha="left", va="center", zorder=12,
bbox=dict(boxstyle="round,pad=0.12", fc=style.SURFACE, ec="none", alpha=0.85))
def title(ax, text, size=8.6):
ax.set_title(text, fontsize=size, loc="left", color=style.INK, fontweight="bold")
# --- frame strips (PDF) and video (web) for simulated plays ------------------------------------------
MARK_R = 0.62
def tracking(play):
df = play.to_tracking()
df["_id"] = df["displayName"].astype(str)
return df
def catch_time(df, key, t_throw):
"""First tracking time after the throw when the ball reaches `key` (the simulated catch)."""
b = df[df.side == "ball"].set_index("time")[["x", "y"]]
q = df[df.displayName == key].set_index("time")[["x", "y"]]
gap = ((b - q) ** 2).sum(axis=1) ** 0.5
gap = gap[gap.index > t_throw + 0.05]
return float(gap[gap < 0.6].index[0])
def panel(ax, play, df, t, lateral, window, rings=(), ring_r=0.95, mark_r=None, trails=None):
"""One moment of a simulated play on `ax`: markers, trails (only for the keys in `trails`, if given), rings."""
tmap = df.groupby("frameId")["time"].first()
fid = int((tmap - t).abs().idxmin())
ylo, yhi = sorted((play.ball_y - lateral[0], play.ball_y - lateral[1]))
fld = Field(ax=ax, orient="vertical", los=play.los, window=window, numbers=False, hashes=False,
lateral=(ylo, yhi), to_go=play.to_go)
cur = df[df["frameId"] == fid]
shown = cur[cur["x"].between(play.los + window[0] + 0.5, play.los + window[1] - 0.3)
& cur["y"].between(ylo + 0.5, yhi - 0.5) | (cur["side"] == "ball")]
ids = set(shown["_id"])
keep = ids if trails is None else ids & set(trails)
tr = df[(df["frameId"] <= fid) & df["_id"].isin(keep) & (df["side"] != "ball")]
for (_, side), g in tr.groupby(["_id", "side"]):
xs, ys = fld.xy(g["x"].values, g["y"].values)
ax.plot(xs, ys, color=style.OFFENSE if side == "off" else style.DEFENSE, lw=0.9, alpha=0.4, zorder=3)
ppy = fld.pts_per_yard()
mr = mark_r or MARK_R
for _, r_ in shown.iterrows():
x, y = (float(v) for v in fld.xy(r_["x"], r_["y"]))
if r_["side"] == "ball":
ax.add_patch(Ellipse((x, y + 0.45), 0.75, 0.45, fc=style.BALL, ec="white", lw=0.5, zorder=12))
continue
lab = str(r_.get("label", "") or "")
if r_["side"] == "off":
ax.add_patch(Circle((x, y), mr, fc=style.OFFENSE, ec="white", lw=0.6, zorder=8))
tc = "white"
else:
ax.add_patch(RegularPolygon((x, y), 4, radius=mr * 1.3, orientation=np.pi / 4,
fc=style.DEFENSE_TINT, ec=style.DEFENSE, lw=1.0, zorder=8))
tc = style.INK
fs = max(4.0, min(8.0, ppy * mr / MARK_R * (0.85 if len(lab) <= 1 else 0.6)))
ax.text(x, y, lab, ha="center", va="center", fontsize=fs, color=tc, fontweight="bold", zorder=10)
for key, col in rings:
r_ = cur[cur["displayName"] == key].iloc[0]
x, y = fld.xy(r_["x"], r_["y"])
ax.add_patch(Circle((float(x), float(y)), ring_r, fc="none", ec=col, lw=1.6, zorder=7))
for patch in ax.patches:
patch.set_clip_on(True)
return fld
def strip(moments, lateral, window, notes=None, rings=(), ncols=2, width_in=6.2, after=None, mark_r=None,
trails=None, note_h=0.8):
"""Numbered frame strip. `moments` is a list of (play, t, tag)."""
cache = {}
nrows = int(np.ceil(len(moments) / ncols))
pw = width_in / ncols
span_w, span_d = lateral[1] - lateral[0], window[1] - window[0]
fig, axes = plt.subplots(nrows, ncols, squeeze=False,
figsize=(width_in, nrows * (pw * span_d / span_w + (note_h if notes else 0.25))))
for ax in axes.flat[len(moments):]:
ax.axis("off")
for i, (ax, (play, t, tag)) in enumerate(zip(axes.flat, moments)):
if id(play) not in cache:
cache[id(play)] = tracking(play)
fld = panel(ax, play, cache[id(play)], t, lateral, window, rings, mark_r=mark_r, trails=trails)
if after:
after(fld, play, t, i)
lab = "snap" if abs(t) < 1e-6 else f"{t:+.1f} s"
ax.set_title(f"{i + 1}. {lab}" + (f" {tag}" if tag else ""), fontsize=8.3, color=style.INK_2, loc="left")
if notes:
ax.text(0.5, -0.03, notes[i], transform=ax.transAxes, ha="center", va="top",
fontsize=7.1 if ncols < 3 else 6.6, color=style.INK)
fig.tight_layout(h_pad=1.3 if notes else 0.6, w_pad=0.6)
return fig
def video(play, notes, lateral, window, rings=(), width_in=4.8, after=None, mark_r=None, trails=None):
"""Web version: the play frame by frame, with each moment's note shown as the caption."""
import shutil
from matplotlib import animation
from IPython.display import HTML, display
span_w, span_d = lateral[1] - lateral[0], window[1] - window[0]
fig, ax = plt.subplots(figsize=(width_in, width_in * span_d / span_w + 0.5))
df = tracking(play)
tmap = df.groupby("frameId")["time"].first()
frames = [float(tmap[f]) for f in sorted(df["frameId"].unique())]
def update(k):
t = frames[k]
ax.clear()
fld = panel(ax, play, df, t, lateral, window, rings, mark_r=mark_r, trails=trails)
if after:
after(fld, play, t, -1)
cur = [n for tt, n in notes if tt <= t + 1e-6]
ax.set_title(("snap" if abs(t) < 0.05 else f"{t:+.1f} s") + (("\n" + cur[-1].replace("\n", " ")) if cur else ""),
fontsize=8.2, color=style.INK, loc="left")
return []
anim = animation.FuncAnimation(fig, update, frames=range(len(frames)), interval=100)
html = (anim.to_html5_video(embed_limit=40).replace("<video ", '<video style="max-width:100%;height:auto" ')
if shutil.which("ffmpeg") else anim.to_jshtml(default_mode="once"))
plt.close(fig)
display(HTML(html))
def animation_or_strip(play, moments, lateral, window, rings=(), after=None, ncols=2, width_in=6.2, mark_r=None,
trails=None, note_h=0.8):
"""Video on the website; a numbered, captioned frame strip in the PDF. moments = [(t, tag, note)]."""
if pdf_build():
strip([(play, t, tag) for t, tag, _ in moments], lateral, window, notes=[n for *_, n in moments],
rings=rings, ncols=ncols, width_in=width_in, after=after, mark_r=mark_r, trails=trails, note_h=note_h)
plt.show()
return
video(play, [(t, n) for t, _, n in moments], lateral, window, rings=rings, after=after, mark_r=mark_r,
trails=trails)