Download report.py from EScO-V10/MedSegNet: direct link, hf CLI and curl.
- Browser
- Download file 4.25 kB
-
https://huggingface.co/spaces/EScO-V10/MedSegNet/resolve/main/report.py
- Command line
-
hf download hf://spaces/EScO-V10/MedSegNet/report.py
-
curl -L -o report.py https://huggingface.co/spaces/EScO-V10/MedSegNet/resolve/main/report.py
4.25 kB
| from matplotlib.backends.backend_pdf import PdfPages | |
| import matplotlib.pyplot as plt | |
| from matplotlib.patches import Rectangle | |
| from datetime import datetime | |
| import uuid | |
| def build_pdf_report(volume, pred, fig_path, combined_path, save_path="report.pdf", | |
| case_id=None): | |
| if case_id is None: | |
| case_id = f"MSN-{uuid.uuid4().hex[:8].upper()}" | |
| tc_voxels = int(pred[0].sum()) | |
| wt_voxels = int(pred[1].sum()) | |
| et_voxels = int(pred[2].sum()) | |
| with PdfPages(save_path) as pdf: | |
| fig = plt.figure(figsize=(8.27, 11.69)) # A4 | |
| # ---- Header ---- | |
| fig.add_artist(Rectangle((0, 0.94), 1, 0.06, transform=fig.transFigure, | |
| facecolor="#1e3a8a", edgecolor="none")) | |
| fig.text(0.06, 0.965, "MedSegNet", fontsize=20, weight="bold", color="white", | |
| ha="left", va="center") | |
| fig.text(0.06, 0.947, "Automated Brain Tumor Segmentation Report", | |
| fontsize=10, color="#dbeafe", ha="left", va="center") | |
| fig.text(0.94, 0.965, f"Case ID: {case_id}", fontsize=9, color="white", | |
| ha="right", va="center") | |
| fig.text(0.94, 0.947, datetime.now().strftime("%Y-%m-%d %H:%M"), | |
| fontsize=9, color="#dbeafe", ha="right", va="center") | |
| # ---- Findings summary table (real matplotlib table) ---- | |
| fig.text(0.06, 0.905, "Segmentation Findings", fontsize=13, weight="bold", | |
| color="#1e293b") | |
| colors = {"WT": "#eab308", "TC": "#16a34a", "ET": "#dc2626"} | |
| table_data = [ | |
| ["\u25A0 Whole Tumor (WT)", "Edema + core", f"{wt_voxels:,}"], | |
| ["\u25A0 Tumor Core (TC)", "Solid tumor mass", f"{tc_voxels:,}"], | |
| ["\u25A0 Enhancing Tumor (ET)", "Enhancing region", f"{et_voxels:,}"], | |
| ] | |
| col_labels = ["Region", "Description", "Voxel Count"] | |
| table_ax = fig.add_axes([0.06, 0.72, 0.88, 0.16]) | |
| table_ax.axis("off") | |
| tbl = table_ax.table( | |
| cellText=table_data, | |
| colLabels=col_labels, | |
| colWidths=[0.35, 0.40, 0.25], | |
| cellLoc="left", | |
| colLoc="left", | |
| loc="center", | |
| ) | |
| tbl.auto_set_font_size(False) | |
| tbl.set_fontsize(9.5) | |
| tbl.scale(1, 2.0) | |
| for j in range(len(col_labels)): | |
| cell = tbl[0, j] | |
| cell.set_facecolor("#1e293b") | |
| cell.set_text_props(color="white", weight="bold") | |
| cell.set_edgecolor("#1e293b") | |
| region_colors = [colors["WT"], colors["TC"], colors["ET"]] | |
| for i in range(1, len(table_data) + 1): | |
| for j in range(len(col_labels)): | |
| cell = tbl[i, j] | |
| cell.set_edgecolor("#e2e8f0") | |
| cell.set_facecolor("#f8fafc" if i % 2 == 0 else "white") | |
| tbl[i, 0].set_text_props(color=region_colors[i - 1], weight="bold") | |
| # ---- Region breakdown strip ---- | |
| fig.text(0.06, 0.685, "Region Breakdown", fontsize=11, | |
| weight="bold", color="#1e293b") | |
| img1 = plt.imread(fig_path) | |
| ax1 = fig.add_axes([0.06, 0.44, 0.88, 0.235]) | |
| ax1.imshow(img1) | |
| ax1.axis("off") | |
| # ---- Combined segmentation ---- | |
| fig.text(0.06, 0.41, "Combined Segmentation Overlay", | |
| fontsize=11, weight="bold", color="#1e293b") | |
| img2 = plt.imread(combined_path) | |
| ax2 = fig.add_axes([0.27, 0.11, 0.46, 0.29]) | |
| ax2.imshow(img2) | |
| ax2.axis("off") | |
| # ---- Footer ---- | |
| fig.add_artist(Rectangle((0, 0.05), 1, 0.002, transform=fig.transFigure, | |
| facecolor="#cbd5e1", edgecolor="none")) | |
| fig.text(0.06, 0.035, | |
| "Legend: Yellow = Whole Tumor (WT) Green = Tumor Core (TC) Red = Enhancing Tumor (ET)", | |
| fontsize=8.5, color="#475569") | |
| fig.text(0.06, 0.018, | |
| "Generated by MedSegNet (SegFormer3D architecture). Research / academic demonstration " | |
| "output only \u2014 not for clinical diagnosis or treatment planning.", | |
| fontsize=7.5, color="#94a3b8", style="italic") | |
| pdf.savefig(fig) | |
| plt.close(fig) | |
| return save_path |