{"spec_id":"curve-power-duration","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\ncurve-power-duration: Mean-Maximal Power Duration Curve\nLibrary: bokeh 3.9.1 | Python 3.13.13\nQuality: 89/100 | Created: 2026-06-13\n\"\"\"\n\nimport os\nimport time\nfrom pathlib import Path\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import ColumnDataSource, CustomJSTickFormatter, FixedTicker, Label, Range1d, Span\nfrom bokeh.plotting import figure\nfrom selenium import webdriver\nfrom selenium.webdriver.chrome.options import Options\n\n\n# Theme tokens\nTHEME = os.getenv(\"ANYPLOT_THEME\", \"light\")\nPAGE_BG = \"#FAF8F1\" if THEME == \"light\" else \"#1A1A17\"\nELEVATED_BG = \"#FFFDF6\" if THEME == \"light\" else \"#242420\"\nINK = \"#1A1A17\" if THEME == \"light\" else \"#F0EFE8\"\nINK_SOFT = \"#4A4A44\" if THEME == \"light\" else \"#B8B7B0\"\nINK_MUTED = \"#6B6A63\" if THEME == \"light\" else \"#A8A79F\"\n\n# Imprint palette — canonical order; positions 1 and 2 for the two data series\nBRAND = \"#009E73\"  # position 1 — empirical mean-maximal curve (always first)\nMODEL_COLOR = \"#C475FD\"  # position 2 — fitted CP model line\n\n# Data — well-trained competitive cyclist\nnp.random.seed(42)\nCP_VAL = 280  # Critical Power in watts (aerobic ceiling)\nWPRIME = 22000  # W' anaerobic work capacity in joules\nPMAX = 1050  # Neuromuscular peak power (sprint ceiling, ~1 s effort)\n\n# Log-spaced effort durations: 1 s → 18,000 s (5 h)\ndurations = np.logspace(0, np.log10(18000), 52)\n\n# CP model: P(t) = CP + W'/t, capped at neuromuscular maximum\nmodel_power = np.minimum(PMAX, CP_VAL + WPRIME / durations)\n\n# Empirical mean-maximal power: slight positive scatter around model\nnoise = np.random.normal(0, 5, len(durations))\nempirical_power = model_power + noise + np.random.uniform(0, 12, len(durations))\nempirical_power = np.clip(empirical_power, model_power * 0.97, model_power * 1.03)\n# Enforce monotonically non-increasing (best average power always drops with duration)\nfor i in range(1, len(empirical_power)):\n    empirical_power[i] = min(empirical_power[i], empirical_power[i - 1])\n\n# Dense smooth curve for the model overlay\ndurations_dense = np.logspace(0, np.log10(18000), 400)\nmodel_dense = np.minimum(PMAX, CP_VAL + WPRIME / durations_dense)\n\n# Reference annotation durations (per specification)\nref_durations = {\"5 s\": 5, \"1 min\": 60, \"5 min\": 300, \"20 min\": 1200}\n\n# Title (50 chars — within 67-char baseline; default font size applies)\ntitle = \"curve-power-duration · python · bokeh · anyplot.ai\"\n\n# Figure — landscape 3200×1800, log x-axis\np = figure(\n    width=3200,\n    height=1800,\n    title=title,\n    x_axis_label=\"Duration\",\n    y_axis_label=\"Power (W)\",\n    x_axis_type=\"log\",\n    x_range=(0.6, 22000),\n    y_range=Range1d(200, 1200),\n    toolbar_location=None,\n    min_border_bottom=160,\n    min_border_left=180,\n    min_border_top=110,\n    min_border_right=60,\n)\n\n# Empirical mean-maximal power (primary series — BRAND green, always first)\nsrc_emp = ColumnDataSource({\"x\": durations, \"y\": empirical_power})\np.line(\"x\", \"y\", source=src_emp, line_color=BRAND, line_width=5, line_alpha=0.7, legend_label=\"Mean-Maximal Power\")\np.scatter(\"x\", \"y\", source=src_emp, color=BRAND, size=14, alpha=0.85, line_color=PAGE_BG, line_width=2)\n\n# Fitted CP model overlay — dashed, secondary series color\nsrc_mdl = ColumnDataSource({\"x\": durations_dense, \"y\": model_dense})\np.line(\n    \"x\",\n    \"y\",\n    source=src_mdl,\n    line_color=MODEL_COLOR,\n    line_width=5,\n    line_dash=\"dashed\",\n    legend_label=f\"CP Model (CP={CP_VAL} W, W′=22 kJ)\",\n)\n\n# CP asymptote — neutral dotted horizontal reference line\np.add_layout(\n    Span(location=CP_VAL, dimension=\"width\", line_color=INK_SOFT, line_width=2, line_dash=\"dotted\", line_alpha=0.75)\n)\n\n# Vertical reference markers at key durations\nfor t in ref_durations.values():\n    p.add_layout(\n        Span(location=t, dimension=\"height\", line_color=INK_MUTED, line_width=1.5, line_dash=\"dashed\", line_alpha=0.5)\n    )\n\n# Reference duration labels near the top of the plot\nfor label, t in ref_durations.items():\n    p.add_layout(\n        Label(\n            x=t,\n            y=1150,\n            text=label,\n            text_font_size=\"34pt\",\n            text_color=INK_SOFT,\n            text_align=\"center\",\n            text_baseline=\"bottom\",\n            x_units=\"data\",\n            y_units=\"data\",\n        )\n    )\n\n# CP asymptote label (right side)\np.add_layout(\n    Label(\n        x=15000,\n        y=CP_VAL + 20,\n        text=f\"CP = {CP_VAL} W\",\n        text_font_size=\"34pt\",\n        text_color=INK_SOFT,\n        text_align=\"right\",\n        text_baseline=\"bottom\",\n        x_units=\"data\",\n        y_units=\"data\",\n    )\n)\n\n# Log x-axis: human-readable tick labels\np.xaxis.ticker = FixedTicker(ticks=[1, 5, 30, 60, 300, 600, 1200, 3600, 18000])\np.xaxis.formatter = CustomJSTickFormatter(\n    code=\"\"\"\n    var labels = {1: '1 s', 5: '5 s', 30: '30 s', 60: '1 min',\n                  300: '5 min', 600: '10 min', 1200: '20 min',\n                  3600: '1 h', 18000: '5 h'};\n    return labels[tick] || String(tick);\n    \"\"\"\n)\n\n# Theme-adaptive chrome\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = None\n\np.title.text_color = INK\np.title.text_font_size = \"50pt\"\np.title.text_font_style = \"normal\"\np.title.align = \"center\"\n\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.axis_label_text_font_size = \"42pt\"\np.yaxis.axis_label_text_font_size = \"42pt\"\n\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\np.xaxis.major_label_text_font_size = \"34pt\"\np.yaxis.major_label_text_font_size = \"34pt\"\n\np.xaxis.axis_line_color = INK_SOFT\np.yaxis.axis_line_color = INK_SOFT\np.xaxis.major_tick_line_color = INK_SOFT\np.yaxis.major_tick_line_color = INK_SOFT\n\np.xgrid.grid_line_color = INK\np.ygrid.grid_line_color = INK\np.xgrid.grid_line_alpha = 0.12\np.ygrid.grid_line_alpha = 0.12\n\np.legend.background_fill_color = ELEVATED_BG\np.legend.border_line_color = INK_SOFT\np.legend.label_text_color = INK_SOFT\np.legend.label_text_font_size = \"34pt\"\np.legend.location = \"top_right\"\n\n# Save interactive HTML artifact\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot via headless Chrome (Selenium 4 / Selenium Manager)\nW, H = 3200, 1800\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={W},{H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\n# CDP override pins the viewport to exactly W×H at DPR=1, avoiding\n# headless-Chrome viewport-vs-window-size discrepancies\ndriver.execute_cdp_cmd(\n    \"Emulation.setDeviceMetricsOverride\", {\"width\": W, \"height\": H, \"deviceScaleFactor\": 1, \"mobile\": False}\n)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)\ndriver.save_screenshot(f\"plot-{THEME}.png\")\ndriver.quit()\n"}