File size: 4,248 Bytes
25b6de6
 
cdb7833
25b6de6
cdb7833
 
 
 
 
 
 
 
 
 
 
25b6de6
 
 
ab78d06
cdb7833
 
 
 
 
 
 
 
 
 
 
 
 
d5aa4fc
cdb7833
 
 
 
d5aa4fc
 
 
 
cdb7833
d5aa4fc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cdb7833
 
d5aa4fc
cdb7833
25b6de6
ab78d06
d5aa4fc
ab78d06
 
 
cdb7833
d5aa4fc
cdb7833
 
ab78d06
d5aa4fc
ab78d06
 
25b6de6
cdb7833
 
 
 
 
 
 
 
 
 
25b6de6
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
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