File size: 5,842 Bytes
5999247 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 | """render.py - render an RNFL thickness map as the PNG the VLM sees (same function for training and the app).
Fixed colormap + fixed colour scale + ring/sector overlay + orientation labels."""
import numpy as np
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import measure as M
def render_map(m, path=None, center=None, vmax=230.0, radius=M.RING_RADIUS_PX, overlay=True, dpi=100):
"""Draw the map. center=(cx, cy) disc centre (None = image centre). Saves PNG if path given; returns the figure otherwise."""
cx, cy = center if center is not None else M.DEFAULT_CENTER
fig = plt.figure(figsize=(5.4, 4.6), dpi=dpi)
ax = fig.add_axes([0.08, 0.08, 0.70, 0.84])
im = ax.imshow(m, cmap="jet", vmin=0, vmax=vmax)
ax.axis("off")
if overlay:
t = np.linspace(0, 2 * np.pi, 361)
ax.plot(cx + radius * np.sin(t), cy - radius * np.cos(t), color="white", lw=1.0)
for deg in range(15, 360, 30): # 12 clock-hour sector boundaries
a = np.radians(deg)
ax.plot([cx + (radius - 5) * np.sin(a), cx + (radius + 5) * np.sin(a)],
[cy - (radius - 5) * np.cos(a), cy - (radius + 5) * np.cos(a)], color="white", lw=0.8)
for h in range(1, 13): # clock numbers, 12 = top
a = np.radians((h % 12) * 30)
ax.text(cx + (radius + 14) * np.sin(a), cy - (radius + 14) * np.cos(a), str(h),
color="white", fontsize=6, ha="center", va="center")
ax.text(0.5, 1.01, "Superior", transform=ax.transAxes, ha="center", va="bottom", fontsize=8)
ax.text(0.5, -0.01, "Inferior", transform=ax.transAxes, ha="center", va="top", fontsize=8)
ax.text(-0.01, 0.5, "image-left", transform=ax.transAxes, ha="right", va="center", rotation=90, fontsize=8)
ax.text(1.01, 0.5, "image-right", transform=ax.transAxes, ha="left", va="center", rotation=270, fontsize=8)
cax = fig.add_axes([0.86, 0.12, 0.03, 0.76])
fig.colorbar(im, cax=cax); cax.set_ylabel("RNFL thickness (µm)", fontsize=8); cax.tick_params(labelsize=7)
if path is not None:
fig.savefig(path); plt.close(fig); return None
return fig
# ---------------------------------------------------------------------------------------------
# Added for the app: exact-geometry clickable map (pixel -> map coordinate is a pure scaling)
# ---------------------------------------------------------------------------------------------
def _fig_to_pil(fig):
from PIL import Image
fig.canvas.draw()
buf = np.asarray(fig.canvas.buffer_rgba())
img = Image.fromarray(buf[..., :3].copy())
plt.close(fig)
return img
def render_map_pil(m, center=None, vmax=230.0, radius=M.RING_RADIUS_PX):
"""Same picture as render_map (the one the VLM was trained on), returned as a PIL image."""
fig = render_map(m, None, center=center, vmax=vmax, radius=radius)
return _fig_to_pil(fig)
CLICK_SIZE = 675 # 3 screen px per map px
def click_to_map(px, py, size=CLICK_SIZE, n=M.MAP_SIZE):
"""Pixel in the clickable image -> map coordinates (x, y) in map pixels."""
return (px + 0.5) * n / size - 0.5, (py + 0.5) * n / size - 0.5
def render_clickable(m, center=None, vmax=230.0, radius=M.RING_RADIUS_PX, size=CLICK_SIZE, point=None, hour=None, quad=None, disc_r=None):
"""The map fills the whole canvas, so clicks map exactly. Optional highlights: a clicked point, a clock-hour wedge,
a 90-degree quadrant wedge, or the optic-nerve-head circle."""
cx, cy = center if center is not None else M.DEFAULT_CENTER
n = m.shape[0]
fig = plt.figure(figsize=(size / 100, size / 100), dpi=100)
ax = fig.add_axes([0, 0, 1, 1])
ax.imshow(m, cmap="jet", vmin=0, vmax=vmax, interpolation="nearest")
ax.set_xlim(-0.5, n - 0.5); ax.set_ylim(n - 0.5, -0.5); ax.axis("off")
t = np.linspace(0, 2 * np.pi, 361)
ax.plot(cx + radius * np.sin(t), cy - radius * np.cos(t), color="white", lw=1.2)
for deg in range(15, 360, 30):
a = np.radians(deg)
ax.plot([cx + (radius - 5) * np.sin(a), cx + (radius + 5) * np.sin(a)], [cy - (radius - 5) * np.cos(a), cy - (radius + 5) * np.cos(a)], color="white", lw=0.9)
for h in range(1, 13):
a = np.radians((h % 12) * 30)
ax.text(cx + (radius + 15) * np.sin(a), cy - (radius + 15) * np.cos(a), str(h), color="white", fontsize=8, ha="center", va="center")
for txt, (x, y, ha, va) in {"Superior": (0.5, 0.985, "center", "top"), "Inferior": (0.5, 0.015, "center", "bottom"),
"image-left": (0.012, 0.5, "left", "center"), "image-right": (0.988, 0.5, "right", "center")}.items():
ax.text(x, y, txt, transform=ax.transAxes, color="white", fontsize=8, ha=ha, va=va,
bbox=dict(facecolor="black", alpha=0.35, pad=1.5, edgecolor="none"))
def wedge(a1, a2, r_in, r_out, color="white", alpha=0.38):
a = np.radians(np.linspace(a1, a2, 40))
xs = np.r_[cx + r_out * np.sin(a), (cx + r_in * np.sin(a))[::-1]]; ys = np.r_[cy - r_out * np.cos(a), (cy - r_in * np.cos(a))[::-1]]
ax.fill(xs, ys, color=color, alpha=alpha, lw=0); ax.plot(np.r_[xs, xs[0]], np.r_[ys, ys[0]], color="yellow", lw=1.5)
if hour is not None:
c = 0 if hour == 12 else hour * 30; wedge(c - 15, c + 15, radius - 12, radius + 12)
if quad is not None:
s = {"S": -45, "R": 45, "I": 135, "L": 225}[quad]; wedge(s, s + 90, radius - 12, radius + 12, alpha=0.30)
if disc_r:
ax.plot(cx + disc_r * np.sin(t), cy - disc_r * np.cos(t), color="yellow", lw=2)
if point is not None:
ax.plot(point[0], point[1], marker="o", ms=11, mfc="none", mec="yellow", mew=2.2)
ax.plot(point[0], point[1], marker="+", ms=14, mec="yellow", mew=1.5)
return _fig_to_pil(fig)
|