File size: 9,407 Bytes
e6b36e6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
478c826
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
00e4d88
 
 
 
 
 
 
 
 
e6b36e6
 
 
 
 
00e4d88
699f6c2
00e4d88
e6b36e6
 
 
600a077
699f6c2
 
 
00e4d88
 
 
 
e6b36e6
 
 
 
 
 
 
00e4d88
e6b36e6
 
 
 
 
478c826
79c3a5f
478c826
 
 
 
 
 
 
 
 
79c3a5f
 
 
 
 
 
e6b36e6
 
 
 
79c3a5f
e6b36e6
79c3a5f
 
e6b36e6
00e4d88
e6b36e6
 
 
79c3a5f
 
 
 
 
 
 
 
e6b36e6
79c3a5f
e6b36e6
 
 
 
79c3a5f
e6b36e6
 
79c3a5f
e6b36e6
 
 
 
 
 
 
 
 
79c3a5f
e6b36e6
 
 
 
 
79c3a5f
 
 
 
 
 
 
 
 
 
 
478c826
79c3a5f
478c826
 
 
 
 
 
 
 
 
79c3a5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""Report and heatmap export utilities."""
import io
from datetime import datetime

import cv2
import numpy as np
from PIL import Image
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import inch
from reportlab.lib.utils import ImageReader
from reportlab.pdfgen import canvas

from config.constants import COLORMAPS


def _draw_pdf_footer(c, margin=72, footer_y=40, include_web_app=False):
    """Draw common footer for S2F PDF reports."""
    c.setFont("Helvetica", 8)
    c.setFillColorRGB(0.4, 0.4, 0.4)
    gen_date = datetime.now().strftime("%Y-%m-%d %H:%M")
    c.drawString(margin, footer_y, f"Generated by Shape2Force (S2F) on {gen_date}")
    c.drawString(margin, footer_y - 12, "Model: https://huggingface.co/Angione-Lab/Shape2Force")
    if include_web_app:
        c.drawString(margin, footer_y - 24, "Web app: https://huggingface.co/spaces/Angione-Lab/Shape2force")
    c.setFillColorRGB(0, 0, 0)


def _pdf_image_layout(page_w_pt, page_h_pt, margin=72, n_images=2):
    """Return layout dict for centered side-by-side images: img_w, img_h, img_gap, img_left, bf_x, hm_x, img_bottom, y_top."""
    img_w = 2.8 * inch
    img_h = 2.8 * inch
    img_gap = 20
    total_img_width = n_images * img_w + (n_images - 1) * img_gap
    img_left = margin + (page_w_pt - 2 * margin - total_img_width) / 2
    bf_x = img_left
    hm_x = img_left + img_w + img_gap
    y_top = page_h_pt - 50
    img_bottom = y_top - 35 - img_h  # header (title + image name) takes 35pt
    return {
        "img_w": img_w,
        "img_h": img_h,
        "img_gap": img_gap,
        "img_left": img_left,
        "bf_x": bf_x,
        "hm_x": hm_x,
        "img_bottom": img_bottom,
        "y_top": y_top,
    }


def heatmap_to_rgb(scaled_heatmap, colormap_name="Jet", zmin=None, zmax=None):
    """Convert scaled heatmap to RGB array using the given colormap.
    If zmin, zmax are provided (e.g. for Range mode), map [zmin,zmax] to 0-1 for coloring."""
    arr = np.asarray(scaled_heatmap, dtype=np.float32)
    if zmin is not None and zmax is not None and zmax > zmin:
        arr = np.clip((arr - zmin) / (zmax - zmin), 0, 1)
    else:
        arr = np.clip(arr, 0, 1)
    heatmap_uint8 = (arr * 255).astype(np.uint8)
    cv2_colormap = COLORMAPS.get(colormap_name, cv2.COLORMAP_JET)
    heatmap_rgb = cv2.cvtColor(cv2.applyColorMap(heatmap_uint8, cv2_colormap), cv2.COLOR_BGR2RGB)
    return heatmap_rgb


def heatmap_to_rgb_with_contour(scaled_heatmap, colormap_name="Jet", cell_mask=None, zmin=None, zmax=None):
    """Convert heatmap to RGB, optionally drawing red cell contour. Mask must match heatmap shape."""
    heatmap_rgb = heatmap_to_rgb(scaled_heatmap, colormap_name, zmin=zmin, zmax=zmax)
    if cell_mask is not None and np.any(cell_mask > 0):
        contours, _ = cv2.findContours(cell_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
        if contours:
            cv2.drawContours(heatmap_rgb, contours, -1, (255, 0, 0), 3)
    return heatmap_rgb


def heatmap_to_png_bytes(scaled_heatmap, colormap_name="Jet", cell_mask=None, zmin=None, zmax=None):
    """Convert scaled heatmap to PNG bytes buffer. Optionally draw red cell contour.
    If zmin, zmax provided (Range mode), map that range to full colormap."""
    heatmap_rgb = heatmap_to_rgb_with_contour(scaled_heatmap, colormap_name, cell_mask, zmin=zmin, zmax=zmax)
    buf = io.BytesIO()
    Image.fromarray(heatmap_rgb).save(buf, format="PNG")
    buf.seek(0)
    return buf


def create_pdf_report(img, display_heatmap, raw_heatmap, pixel_sum, force, base_name, colormap_name="Jet",
                      cell_mask=None, cell_pixel_sum=None, cell_force=None, cell_mean=None, zmin=None, zmax=None):
    """Create a PDF report with input image, heatmap, and metrics."""
    buf = io.BytesIO()
    c = canvas.Canvas(buf, pagesize=A4)
    c.setTitle("Shape2Force")
    c.setAuthor("Angione-Lab")
    page_w_pt, page_h_pt = A4[0], A4[1]
    margin = 72
    layout = _pdf_image_layout(page_w_pt, page_h_pt, margin)
    img_w = layout["img_w"]
    img_h = layout["img_h"]
    bf_x = layout["bf_x"]
    hm_x = layout["hm_x"]
    img_bottom = layout["img_bottom"]
    y_top = layout["y_top"]

    _draw_pdf_footer(c, margin=margin, include_web_app=True)
    c.setFont("Helvetica-Bold", 16)
    c.drawString(margin, y_top, "Shape2Force (S2F) - Prediction Report")
    c.setFont("Helvetica", 10)
    c.drawString(margin, y_top - 14, f"Image: {base_name}")
    y_top -= 35

    img_pil = Image.fromarray(img) if img.ndim == 2 else Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    img_buf = io.BytesIO()
    img_pil.save(img_buf, format="PNG")
    img_buf.seek(0)
    c.drawImage(ImageReader(img_buf), bf_x, img_bottom, width=img_w, height=img_h, preserveAspectRatio=True)
    c.setFont("Helvetica", 9)
    bf_label_w = c.stringWidth("Bright-field", "Helvetica", 9)
    c.drawString(bf_x + (img_w - bf_label_w) / 2, img_bottom - 14, "Bright-field")

    heatmap_rgb = heatmap_to_rgb_with_contour(display_heatmap, colormap_name, cell_mask, zmin=zmin, zmax=zmax)
    hm_buf = io.BytesIO()
    Image.fromarray(heatmap_rgb).save(hm_buf, format="PNG")
    hm_buf.seek(0)
    c.drawImage(ImageReader(hm_buf), hm_x, img_bottom, width=img_w, height=img_h, preserveAspectRatio=True)
    hm_label = "Force map (red = estimated boundary)" if cell_mask is not None and np.any(cell_mask > 0) else "Force map"
    hm_label_w = c.stringWidth(hm_label, "Helvetica", 9)
    c.drawString(hm_x + (img_w - hm_label_w) / 2, img_bottom - 14, hm_label)

    # Metrics section with spacing
    row_height = 14
    y = img_bottom - 14 - row_height
    c.setFont("Helvetica-Bold", 10)
    c.drawString(margin, y, "Metrics")
    c.setFont("Helvetica", 9)
    y -= 18
    if cell_pixel_sum is not None and cell_force is not None and cell_mean is not None:
        metrics = [
            ("Cell sum (estimated boundary)", f"{cell_pixel_sum:.2f}"),
            ("Cell force (scaled)", f"{cell_force:.2f}"),
            ("Heatmap max", f"{np.max(raw_heatmap):.4f}"),
            ("Heatmap mean (estimated boundary)", f"{cell_mean:.4f}"),
        ]
    else:
        metrics = [
            ("Sum of all pixels", f"{pixel_sum:.2f}"),
            ("Cell force (scaled)", f"{force:.2f}"),
            ("Heatmap max", f"{np.max(raw_heatmap):.4f}"),
            ("Heatmap mean", f"{np.mean(raw_heatmap):.4f}"),
        ]
    for label, val in metrics:
        c.drawString(margin, y, f"{label}: {val}")
        y -= 16

    c.save()
    buf.seek(0)
    return buf.getvalue()


def create_measure_pdf_report(bf_img, heatmap_labeled_rgb, table_rows, base_name):
    """
    Create PDF report for measure tool.
    Contents: bright-field, heatmap with region labels (R1, R2...), table.
    """
    buf = io.BytesIO()
    c = canvas.Canvas(buf, pagesize=A4)
    c.setTitle("Shape2Force - Region Measurement")
    c.setAuthor("Angione-Lab")
    page_w_pt, page_h_pt = A4[0], A4[1]
    margin = 72
    layout = _pdf_image_layout(page_w_pt, page_h_pt, margin)
    img_w = layout["img_w"]
    img_h = layout["img_h"]
    bf_x = layout["bf_x"]
    hm_x = layout["hm_x"]
    img_bottom = layout["img_bottom"]
    y_top = layout["y_top"]

    _draw_pdf_footer(c, margin=margin)
    c.setFont("Helvetica-Bold", 14)
    c.drawString(margin, y_top, "Region Measurement Report")
    c.setFont("Helvetica", 10)
    c.drawString(margin, y_top - 14, f"Image: {base_name}")

    bf_pil = Image.fromarray(bf_img) if bf_img.ndim == 2 else Image.fromarray(
        cv2.cvtColor(bf_img, cv2.COLOR_BGR2RGB)
    )
    bf_buf = io.BytesIO()
    bf_pil.save(bf_buf, format="PNG")
    bf_buf.seek(0)
    c.drawImage(ImageReader(bf_buf), bf_x, img_bottom, width=img_w, height=img_h, preserveAspectRatio=True)
    c.setFont("Helvetica", 9)
    bf_label_w = c.stringWidth("Bright-field", "Helvetica", 9)
    c.drawString(bf_x + (img_w - bf_label_w) / 2, img_bottom - 14, "Bright-field")

    hm_labeled_buf = io.BytesIO()
    Image.fromarray(heatmap_labeled_rgb).save(hm_labeled_buf, format="PNG")
    hm_labeled_buf.seek(0)
    c.drawImage(ImageReader(hm_labeled_buf), hm_x, img_bottom, width=img_w, height=img_h, preserveAspectRatio=True)
    hm_label_w = c.stringWidth("Force map", "Helvetica", 9)
    c.drawString(hm_x + (img_w - hm_label_w) / 2, img_bottom - 14, "Force map")

    # Spacing: one line between images/labels and measurements section
    row_height = 14
    y_table_top = img_bottom - 14 - row_height  # label + one line space

    # Table with borders
    n_cols = len(table_rows[0]) if table_rows else 0
    table_width = page_w_pt - 2 * margin
    col_w = table_width / n_cols if n_cols else 1
    cell_pad = 4

    c.setFont("Helvetica-Bold", 9)
    c.drawString(margin, y_table_top, "Measurements")
    y_table_top -= row_height + 8  # extra space before table

    for ri, row in enumerate(table_rows):
        y_cell_bottom = y_table_top - (ri + 1) * row_height
        for ci, cell in enumerate(row):
            x_left = margin + ci * col_w
            # Draw cell border
            c.rect(x_left, y_cell_bottom, col_w, row_height, stroke=1, fill=0)
            # Draw text (with padding)
            if ri == 0:
                c.setFont("Helvetica-Bold", 8)
            c.drawString(x_left + cell_pad, y_cell_bottom + 4, str(cell)[:22])
            if ri == 0:
                c.setFont("Helvetica", 8)

    c.save()
    buf.seek(0)
    return buf.getvalue()