Update report.py
Browse files
report.py
CHANGED
|
@@ -7,14 +7,6 @@ import uuid
|
|
| 7 |
|
| 8 |
def build_pdf_report(volume, pred, fig_path, combined_path, save_path="report.pdf",
|
| 9 |
case_id=None):
|
| 10 |
-
"""
|
| 11 |
-
Produces a clean, single-page clinical-style PDF report:
|
| 12 |
-
- Header block with title, generated case ID, and timestamp
|
| 13 |
-
- Findings table with voxel counts for WT / TC / ET
|
| 14 |
-
- Region breakdown image strip
|
| 15 |
-
- Combined segmentation overlay
|
| 16 |
-
- Legend + disclaimer footer
|
| 17 |
-
"""
|
| 18 |
if case_id is None:
|
| 19 |
case_id = f"MSN-{uuid.uuid4().hex[:8].upper()}"
|
| 20 |
|
|
@@ -38,62 +30,61 @@ def build_pdf_report(volume, pred, fig_path, combined_path, save_path="report.pd
|
|
| 38 |
fig.text(0.94, 0.947, datetime.now().strftime("%Y-%m-%d %H:%M"),
|
| 39 |
fontsize=9, color="#dbeafe", ha="right", va="center")
|
| 40 |
|
| 41 |
-
# ---- Findings summary table ----
|
| 42 |
fig.text(0.06, 0.905, "Segmentation Findings", fontsize=13, weight="bold",
|
| 43 |
color="#1e293b")
|
| 44 |
|
| 45 |
-
table_top = 0.885
|
| 46 |
-
row_h = 0.024
|
| 47 |
-
col_x = [0.06, 0.38, 0.68, 0.85]
|
| 48 |
-
headers = ["Region", "Description", "Voxel Count", ""]
|
| 49 |
-
|
| 50 |
colors = {"WT": "#eab308", "TC": "#16a34a", "ET": "#dc2626"}
|
| 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 |
# ---- Region breakdown strip ----
|
| 81 |
-
|
| 82 |
-
fig.text(0.06, breakdown_top + 0.015, "Region Breakdown", fontsize=11,
|
| 83 |
weight="bold", color="#1e293b")
|
| 84 |
|
| 85 |
img1 = plt.imread(fig_path)
|
| 86 |
-
ax1 = fig.add_axes([0.06,
|
| 87 |
ax1.imshow(img1)
|
| 88 |
ax1.axis("off")
|
| 89 |
|
| 90 |
# ---- Combined segmentation ----
|
| 91 |
-
|
| 92 |
-
fig.text(0.06, combined_top + 0.015, "Combined Segmentation Overlay",
|
| 93 |
fontsize=11, weight="bold", color="#1e293b")
|
| 94 |
|
| 95 |
img2 = plt.imread(combined_path)
|
| 96 |
-
ax2 = fig.add_axes([0.27,
|
| 97 |
ax2.imshow(img2)
|
| 98 |
ax2.axis("off")
|
| 99 |
|
|
|
|
| 7 |
|
| 8 |
def build_pdf_report(volume, pred, fig_path, combined_path, save_path="report.pdf",
|
| 9 |
case_id=None):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
if case_id is None:
|
| 11 |
case_id = f"MSN-{uuid.uuid4().hex[:8].upper()}"
|
| 12 |
|
|
|
|
| 30 |
fig.text(0.94, 0.947, datetime.now().strftime("%Y-%m-%d %H:%M"),
|
| 31 |
fontsize=9, color="#dbeafe", ha="right", va="center")
|
| 32 |
|
| 33 |
+
# ---- Findings summary table (real matplotlib table) ----
|
| 34 |
fig.text(0.06, 0.905, "Segmentation Findings", fontsize=13, weight="bold",
|
| 35 |
color="#1e293b")
|
| 36 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
colors = {"WT": "#eab308", "TC": "#16a34a", "ET": "#dc2626"}
|
| 38 |
+
table_data = [
|
| 39 |
+
["\u25A0 Whole Tumor (WT)", "Edema + core", f"{wt_voxels:,}"],
|
| 40 |
+
["\u25A0 Tumor Core (TC)", "Solid tumor mass", f"{tc_voxels:,}"],
|
| 41 |
+
["\u25A0 Enhancing Tumor (ET)", "Enhancing region", f"{et_voxels:,}"],
|
| 42 |
]
|
| 43 |
+
col_labels = ["Region", "Description", "Voxel Count"]
|
| 44 |
+
|
| 45 |
+
table_ax = fig.add_axes([0.06, 0.72, 0.88, 0.16])
|
| 46 |
+
table_ax.axis("off")
|
| 47 |
+
tbl = table_ax.table(
|
| 48 |
+
cellText=table_data,
|
| 49 |
+
colLabels=col_labels,
|
| 50 |
+
colWidths=[0.35, 0.40, 0.25],
|
| 51 |
+
cellLoc="left",
|
| 52 |
+
colLoc="left",
|
| 53 |
+
loc="center",
|
| 54 |
+
)
|
| 55 |
+
tbl.auto_set_font_size(False)
|
| 56 |
+
tbl.set_fontsize(9.5)
|
| 57 |
+
tbl.scale(1, 2.0)
|
| 58 |
+
|
| 59 |
+
for j in range(len(col_labels)):
|
| 60 |
+
cell = tbl[0, j]
|
| 61 |
+
cell.set_facecolor("#1e293b")
|
| 62 |
+
cell.set_text_props(color="white", weight="bold")
|
| 63 |
+
cell.set_edgecolor("#1e293b")
|
| 64 |
+
|
| 65 |
+
region_colors = [colors["WT"], colors["TC"], colors["ET"]]
|
| 66 |
+
for i in range(1, len(table_data) + 1):
|
| 67 |
+
for j in range(len(col_labels)):
|
| 68 |
+
cell = tbl[i, j]
|
| 69 |
+
cell.set_edgecolor("#e2e8f0")
|
| 70 |
+
cell.set_facecolor("#f8fafc" if i % 2 == 0 else "white")
|
| 71 |
+
tbl[i, 0].set_text_props(color=region_colors[i - 1], weight="bold")
|
| 72 |
|
| 73 |
# ---- Region breakdown strip ----
|
| 74 |
+
fig.text(0.06, 0.685, "Region Breakdown", fontsize=11,
|
|
|
|
| 75 |
weight="bold", color="#1e293b")
|
| 76 |
|
| 77 |
img1 = plt.imread(fig_path)
|
| 78 |
+
ax1 = fig.add_axes([0.06, 0.44, 0.88, 0.235])
|
| 79 |
ax1.imshow(img1)
|
| 80 |
ax1.axis("off")
|
| 81 |
|
| 82 |
# ---- Combined segmentation ----
|
| 83 |
+
fig.text(0.06, 0.41, "Combined Segmentation Overlay",
|
|
|
|
| 84 |
fontsize=11, weight="bold", color="#1e293b")
|
| 85 |
|
| 86 |
img2 = plt.imread(combined_path)
|
| 87 |
+
ax2 = fig.add_axes([0.27, 0.11, 0.46, 0.29])
|
| 88 |
ax2.imshow(img2)
|
| 89 |
ax2.axis("off")
|
| 90 |
|