{"spec_id":"audiogram-clinical","library":"bokeh","language":"python","code":"\"\"\" anyplot.ai\naudiogram-clinical: Clinical Audiogram\nLibrary: bokeh 3.9.1 | Python 3.13.13\nQuality: 87/100 | Updated: 2026-06-15\n\"\"\"\n\nimport io\nimport os\nimport sys\nimport time\nfrom pathlib import Path\n\n\n# Remove current dir from sys.path so bokeh.py doesn't shadow the bokeh package\nsys.path = [p for p in sys.path if p != \"\" and not (os.path.isfile(os.path.join(p, \"bokeh.py\")) if p else False)]\n\nimport numpy as np\nfrom bokeh.io import output_file, save\nfrom bokeh.models import BoxAnnotation, FixedTicker, Label\nfrom bokeh.plotting import figure\nfrom PIL import Image\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# Audiogram semantic colors (clinical convention: red = right, blue = left)\nCOLOR_RIGHT = \"#AE3030\"  # Imprint position 5 (matte red) — semantic: right ear\nCOLOR_LEFT = \"#4467A3\"  # Imprint position 3 (blue) — semantic: left ear\n\n# Data: high-frequency sensorineural notch (typical noise-induced pattern)\nnp.random.seed(42)\nfrequencies = [125, 250, 500, 1000, 2000, 4000, 8000]\n\n# Right ear: mild-to-moderate high-frequency sensorineural loss\nthreshold_right = [15, 20, 25, 30, 45, 75, 80]\n\n# Left ear: slightly milder, similar sloping pattern\nthreshold_left = [10, 15, 20, 25, 40, 70, 75]\n\n# Severity bands: (y_start, y_end in dB HL, fill color, label)\n# Colors chosen from Imprint palette — severity gradient green→red\nseverity_bands = [\n    (-10, 25, \"#009E73\", \"Normal\"),  # green  (Imprint pos 1)\n    (25, 40, \"#99B314\", \"Mild\"),  # lime   (Imprint pos 8)\n    (40, 55, \"#BD8233\", \"Moderate\"),  # ochre  (Imprint pos 4)\n    (55, 70, \"#DDCC77\", \"Mod. Severe\"),  # amber  (semantic anchor)\n    (70, 90, \"#C475FD\", \"Severe\"),  # lavender (Imprint pos 2)\n    (90, 120, \"#AE3030\", \"Profound\"),  # red    (Imprint pos 5)\n]\n\n# Title — 49 chars < 67 baseline, default '50pt' applies\ntitle = \"audiogram-clinical · python · bokeh · anyplot.ai\"\n\n# Plot — square canvas suits the symmetric clinical format\np = figure(\n    width=2400,\n    height=2400,\n    title=title,\n    x_axis_label=\"Frequency (Hz)\",\n    y_axis_label=\"Hearing Level (dB HL)\",\n    x_axis_type=\"log\",\n    x_range=(100, 10000),\n    y_range=(125, -15),  # inverted: 0 dB (best hearing) at top, loss increases down\n    toolbar_location=None,\n    min_border_bottom=160,\n    min_border_left=200,\n    min_border_top=120,\n    min_border_right=120,\n)\n\n# Theme-adaptive band alpha — subtle on light, slightly more visible on dark\nBAND_ALPHA = 0.09 if THEME == \"light\" else 0.13\n\n# Severity band shading\nfor y_start, y_end, color, _label in severity_bands:\n    p.add_layout(BoxAnnotation(bottom=y_start, top=y_end, fill_color=color, fill_alpha=BAND_ALPHA, line_color=None))\n\n# Severity band labels — placed at right side of each band\nfor y_start, y_end, _color, band_label in severity_bands:\n    y_mid = (y_start + y_end) / 2\n    if band_label == \"Severe\":\n        y_mid += 4  # offset down to avoid overlap with 8k right-ear marker at 80 dB\n    p.add_layout(\n        Label(\n            x=9200,\n            y=y_mid,\n            x_units=\"data\",\n            y_units=\"data\",\n            text=band_label,\n            text_font_size=\"26pt\",\n            text_color=INK_MUTED,\n            text_align=\"right\",\n            text_baseline=\"middle\",\n        )\n    )\n\n# Right ear: open circles (O) + solid connecting line\np.line(frequencies, threshold_right, line_color=COLOR_RIGHT, line_width=4)\np.scatter(\n    frequencies,\n    threshold_right,\n    marker=\"circle\",\n    size=24,\n    fill_color=PAGE_BG,\n    fill_alpha=1.0,\n    line_color=COLOR_RIGHT,\n    line_width=3,\n    legend_label=\"Right Ear (O)\",\n)\n\n# Left ear: X markers + dashed connecting line\np.line(frequencies, threshold_left, line_color=COLOR_LEFT, line_width=4, line_dash=\"dashed\")\np.scatter(\n    frequencies, threshold_left, marker=\"x\", size=24, line_color=COLOR_LEFT, line_width=3, legend_label=\"Left Ear (X)\"\n)\n\n# X-axis: fixed ticks at standard audiometric frequencies with kHz labels\np.xaxis.ticker = FixedTicker(ticks=[125, 250, 500, 1000, 2000, 4000, 8000])\np.xgrid.ticker = FixedTicker(ticks=[125, 250, 500, 1000, 2000, 4000, 8000])\np.xaxis.major_label_overrides = {125: \"125\", 250: \"250\", 500: \"500\", 1000: \"1k\", 2000: \"2k\", 4000: \"4k\", 8000: \"8k\"}\n\n# Y-axis: gridlines every 10 dB\np.yaxis.ticker = FixedTicker(ticks=list(range(-10, 130, 10)))\n\n# Text sizes (bokeh 'pt' → CSS pt → ~1.333 source-px; '50pt' ≈ 67 source-px)\np.title.text_font_size = \"50pt\"\np.xaxis.axis_label_text_font_size = \"42pt\"\np.yaxis.axis_label_text_font_size = \"42pt\"\np.xaxis.major_label_text_font_size = \"34pt\"\np.yaxis.major_label_text_font_size = \"34pt\"\np.legend.label_text_font_size = \"34pt\"\n\n# Theme-adaptive chrome\np.background_fill_color = PAGE_BG\np.border_fill_color = PAGE_BG\np.outline_line_color = INK_SOFT\n\np.title.text_color = INK\np.xaxis.axis_label_text_color = INK\np.yaxis.axis_label_text_color = INK\np.xaxis.major_label_text_color = INK_SOFT\np.yaxis.major_label_text_color = INK_SOFT\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.15\np.ygrid.grid_line_alpha = 0.15\n\np.legend.background_fill_color = ELEVATED_BG\np.legend.border_line_color = INK_SOFT\np.legend.label_text_color = INK_SOFT\np.legend.location = \"top_right\"\n\n# Save interactive HTML\noutput_file(f\"plot-{THEME}.html\")\nsave(p)\n\n# Screenshot via Selenium — window taller than target so browser chrome\n# overhead doesn't clip the figure; PIL crops to exact 2400×2400 canvas.\nFIG_W, FIG_H = 2400, 2400\nWIN_W, WIN_H = FIG_W, FIG_H + 200\nopts = Options()\nfor arg in (\n    \"--headless=new\",\n    \"--no-sandbox\",\n    \"--disable-dev-shm-usage\",\n    \"--disable-gpu\",\n    f\"--window-size={WIN_W},{WIN_H}\",\n    \"--hide-scrollbars\",\n):\n    opts.add_argument(arg)\ndriver = webdriver.Chrome(options=opts)\ndriver.set_window_size(WIN_W, WIN_H)\ndriver.get(f\"file://{Path(f'plot-{THEME}.html').resolve()}\")\ntime.sleep(3)\nraw = driver.get_screenshot_as_png()\ndriver.quit()\nImage.open(io.BytesIO(raw)).crop((0, 0, FIG_W, FIG_H)).save(f\"plot-{THEME}.png\")\n"}