Rydhi commited on
Commit
d5aa4fc
·
1 Parent(s): 184650f

Update report.py

Browse files
Files changed (1) hide show
  1. report.py +38 -47
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
- rows_data = [
52
- ("Whole Tumor (WT)", "Edema + core", wt_voxels, colors["WT"]),
53
- ("Tumor Core (TC)", "Solid tumor mass", tc_voxels, colors["TC"]),
54
- ("Enhancing Tumor (ET)", "Enhancing region", et_voxels, colors["ET"]),
55
  ]
56
-
57
- fig.add_artist(Rectangle((0.06, table_top - row_h + 0.005), 0.88, row_h,
58
- transform=fig.transFigure, facecolor="#f1f5f9",
59
- edgecolor="none"))
60
- for i, h in enumerate(headers[:3]):
61
- fig.text(col_x[i], table_top, h, fontsize=9, weight="bold",
62
- color="#334155", ha="left", va="center")
63
-
64
- for r, (name, desc, count, color) in enumerate(rows_data):
65
- y = table_top - row_h * (r + 1)
66
- if r % 2 == 0:
67
- fig.add_artist(Rectangle((0.06, y - row_h + 0.005), 0.88, row_h,
68
- transform=fig.transFigure, facecolor="#f8fafc",
69
- edgecolor="none"))
70
- fig.add_artist(Rectangle((col_x[0], y - 0.006), 0.012, 0.012,
71
- transform=fig.transFigure, facecolor=color,
72
- edgecolor="none"))
73
- fig.text(col_x[0] + 0.02, y, name, fontsize=9.5, color="#1e293b",
74
- ha="left", va="center")
75
- fig.text(col_x[1], y, desc, fontsize=8, color="#64748b",
76
- ha="left", va="center")
77
- fig.text(col_x[2], y, f"{count:,}", fontsize=9.5, color="#1e293b",
78
- ha="left", va="center")
 
 
 
 
 
 
79
 
80
  # ---- Region breakdown strip ----
81
- breakdown_top = table_top - row_h * (len(rows_data) + 1.8)
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, breakdown_top - 0.24, 0.88, 0.24])
87
  ax1.imshow(img1)
88
  ax1.axis("off")
89
 
90
  # ---- Combined segmentation ----
91
- combined_top = breakdown_top - 0.28
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, combined_top - 0.30, 0.46, 0.30])
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