382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472 | def make_growth_charts(results: list[dict]) -> list[tuple[Image.Image, str]]:
if len(results) < 2:
return []
df = pd.DataFrame(results)
if "error" in df.columns:
df = df[df["error"].fillna("").astype(str).str.strip() == ""].copy()
if len(df) < 2:
return []
numeric_cols = [
"days_since_start", "area_mm2", "diameter_mm", "perimeter_mm",
"eccentricity", "edge_roughness", "centre_delta_mm",
"entropy", "texture_std",
"crack_area_mm2", "crack_coverage_pct", "crack_count",
"hyph_frangi_mm", "hyph_meijering_mm", "hyph_hybrid_mm",
"rgr_per_day", "relative_growth_per_day",
]
for col in numeric_cols:
if col in df.columns:
df[col] = pd.to_numeric(df[col], errors="coerce")
# Build a numeric sort key from day_code ("d07" → 7); fall back to days_since_start
if "day_code" in df.columns:
df["_day_num"] = df["day_code"].apply(_dc_to_num)
if "days_since_start" in df.columns:
mask = df["_day_num"] == 0
df.loc[mask, "_day_num"] = df.loc[mask, "days_since_start"].fillna(0).astype(int)
elif "days_since_start" in df.columns:
df["_day_num"] = df["days_since_start"].fillna(0).astype(int)
else:
df["_day_num"] = range(len(df))
df = df.sort_values("_day_num").reset_index(drop=True)
# Build an x-axis label for every row: use day_code when valid, else construct dXX from number
def _make_x_label(dc_val, day_num: int) -> str:
dc = str(dc_val) if dc_val else ""
if dc.startswith("d") and dc[1:].isdigit():
return dc
return f"d{day_num:02d}" if day_num > 0 else "d?"
df["_x_label"] = [
_make_x_label(dc, int(n))
for dc, n in zip(
df.get("day_code", pd.Series([""] * len(df))),
df["_day_num"],
)
]
charts: list[tuple[Image.Image, str]] = []
chart_defs = [
("area_mm2", "Area (mm²)", "Colony Area", "#e74c3c", "o", True),
("diameter_mm", "Diameter (mm)", "Colony Diameter", "#2980b9", "s", False),
("perimeter_mm", "Perimeter (mm)", "Colony Perimeter", "#8e44ad", "^", False),
("eccentricity", "Eccentricity", "Colony Eccentricity", "#e67e22", "D", False),
("edge_roughness", "Edge Roughness", "Edge Roughness (P/πd)", "#16a085", "v", False),
("centre_delta_mm", "Centre Offset (mm)", "Colony Centre Offset", "#d35400", "p", False),
("entropy", "Entropy", "Colony Texture Entropy", "#7f8c8d", "h", False),
("texture_std", "Texture Std Dev", "Colony Texture Std Dev", "#2c3e50", "*", False),
("crack_area_mm2", "Crack Area (mm²)", "Crack Area", "#f1c40f", "o", True),
("crack_coverage_pct", "Crack Coverage (%)", "Crack Coverage", "#d4ac0d", "s", False),
("crack_count", "Crack Count", "Number of Cracks", "#b7950b", "^", False),
("hyph_frangi_mm", "Length (mm)", "Hyphae Length — Frangi", "#1abc9c", "o", False),
("hyph_meijering_mm", "Length (mm)", "Hyphae Length — Meijering", "#3498db", "s", False),
("hyph_hybrid_mm", "Length (mm)", "Hyphae Length — Hybrid", "#2ecc71", "D", False),
("rgr_per_day", "RGR (ln mm²/day)", "Relative Growth Rate", "#c0392b", "o", False),
("relative_growth_per_day", "Growth (mm²/day)", "Absolute Growth Rate", "#e74c3c", "s", False),
]
for col, ylabel, title, color, marker, fill in chart_defs:
if col not in df.columns:
continue
valid = df[col].notna() & (df[col].astype(str).str.strip() != "")
if valid.sum() < 2:
continue
sub = df.loc[valid].sort_values("_day_num").reset_index(drop=True).copy()
x = sub["_day_num"].tolist()
x_labels = sub["_x_label"].tolist()
fig, ax = plt.subplots(figsize=(8, 4))
ax.plot(x, sub[col].tolist(), f"{marker}-", color=color, lw=2, ms=8)
if fill:
ax.fill_between(x, 0, sub[col].tolist(), alpha=0.15, color=color)
ax.set_xticks(x)
ax.set_xticklabels(x_labels, rotation=30 if len(x_labels) > 6 else 0, fontsize=8)
ax.set(xlabel="Day", ylabel=ylabel, title=title)
ax.grid(True, alpha=0.3)
charts.append((_fig_to_pil(fig), title))
return charts
|