File size: 14,243 Bytes
b2ffc9b
 
 
 
 
a691f55
b2ffc9b
 
 
 
 
 
6cb080f
 
b2ffc9b
6cb080f
 
 
 
 
b2ffc9b
a730958
 
6cb080f
b2ffc9b
6cb080f
 
b2ffc9b
 
 
 
 
6cb080f
 
 
 
b2ffc9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c0ff09
b2ffc9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0c0ff09
b2ffc9b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
@author : Romain Graux
@date : 2023 April 25, 14:39:03
@last modified : 2024 February 01, 15:59:37
"""

# TODO : add the training of the vae
# TODO : add the description of the settings


import gradio as gr
import json
import numpy as np
import shutil
import sys
import tempfile
import torch

from PIL import Image, ImageDraw
from app.dl_inference import inference_fn
from app.knn import knn, segment_image, bokeh_plot_knn, color_palette
from app.tiff_utils import extract_physical_metadata
from collections import namedtuple
from datetime import datetime
from zipfile import ZipFile

block_state_entry = namedtuple(
    "block_state", ["results", "knn_results", "physical_metadata"]
)

if torch_availbale := torch.cuda.is_available():
    print(f"CUDA device: {torch.cuda.get_device_name(torch.cuda.current_device())}")
print(f"Is CUDA available: {torch_availbale}")

if ".." not in sys.path:
    sys.path.append("..")

from utils.constants import ModelArgs


def inf(img, n_species, threshold, architecture):
    # Get the coordinates of the atoms
    img, results = inference_fn(architecture, img, threshold, n_species=n_species)
    draw = ImageDraw.Draw(img)
    for (k, v), color in zip(results["species"].items(), color_palette):
        color = "#" + "".join([f"{int(255 * x):02x}" for x in color])
        draw.text((5, 5 + 15 * k), f"species {k}", fill=color)
        for x, y in v["coords"]:
            draw.ellipse(
                [x - 5, y - 5, x + 5, y + 5],
                outline=color,
                width=2,
            )
    return img, results


def batch_fn(files, n_species, threshold, architecture, block_state):
    block_state = {}
    if not files:
        raise ValueError("No files were uploaded")

    gallery = []
    for file in files:
        error_physical_metadata = None
        try:
            physical_metadata = extract_physical_metadata(file.name)
            if physical_metadata.unit != "nm":
                raise ValueError(f"Unit of {file.name} is not nm, cannot process it")
        except Exception as e:
            error_physical_metadata = e
            physical_metadata = None

        original_file_name = file.name.split("/")[-1]
        img, results = inf(file.name, n_species, threshold, architecture)
        mask = segment_image(file.name)
        gallery.append((img, original_file_name))

        if physical_metadata is not None:
            factor = 1.0 - np.mean(mask)
            scale = physical_metadata.pixel_width
            edge = physical_metadata.pixel_width * physical_metadata.width
            knn_results = {
                k: knn(results["species"][k]["coords"], scale, factor, edge)
                for k in results["species"]
            }
        else:
            knn_results = None

        block_state[original_file_name] = block_state_entry(
            results, knn_results, physical_metadata
        )

    knn_args = [
        (
            original_file_name,
            {
                k: block_state[original_file_name].knn_results[k]["distances"]
                for k in block_state[original_file_name].knn_results
            },
        )
        for original_file_name in block_state
        if block_state[original_file_name].knn_results is not None
    ]
    if len(knn_args) > 0:
        bokeh_plot = gr.update(
            value=bokeh_plot_knn(knn_args, with_cumulative=True), visible=True
        )
    else:
        bokeh_plot = gr.update(visible=False)
    return (
        gallery,
        block_state,
        gr.update(visible=True),
        bokeh_plot,
        gr.HTML.update(
            value=f"<p style='width:fit-content; background-color:rgba(255, 0, 0, 0.75); border-radius:5px; padding:5px; color:white;'>{error_physical_metadata}</p>",
            visible=bool(error_physical_metadata),
        ),
    )


class NumpyEncoder(json.JSONEncoder):
    """Special json encoder for numpy types"""

    def default(self, obj):
        if isinstance(obj, np.integer):
            return int(obj)
        elif isinstance(obj, np.floating):
            return float(obj)
        elif isinstance(obj, np.ndarray):
            return obj.tolist()
        return json.JSONEncoder.default(self, obj)


def batch_export_files(gallery, block_state):
    # Return images, coords as csv and a zip containing everything
    files = []
    tmpdir = tempfile.mkdtemp()
    with ZipFile(
        f"{tmpdir}/all_results_{datetime.now().isoformat()}.zip", "w"
    ) as zipObj:
        # Add all metatada
        for data_dict, original_file_name in gallery:
            file_name = original_file_name.split(".")[0]

            # Save the image
            pred_map_path = f"{tmpdir}/pred_map_{file_name}.png"
            file_path = data_dict["name"]
            shutil.copy(file_path, pred_map_path)
            zipObj.write(pred_map_path, arcname=f"{file_name}/pred_map.png")
            files.append(pred_map_path)

            # Save the coords
            results = block_state[original_file_name].results
            coords_path = f"{tmpdir}/coords_{file_name}.csv"
            with open(coords_path, "w") as f:
                f.write("x,y,likelihood,specie,confidence\n")
                for k, v in results["species"].items():
                    for (x, y), likelihood, confidence in zip(
                        v["coords"], v["likelihood"], v["confidence"]
                    ):
                        f.write(f"{x},{y},{likelihood},{k},{confidence}\n")
            zipObj.write(coords_path, arcname=f"{file_name}/coords.csv")
            files.append(coords_path)

            # Save the knn results
            if block_state[original_file_name].knn_results is not None:
                knn_results = block_state[original_file_name].knn_results
                knn_path = f"{tmpdir}/knn_results_{file_name}.json"
                with open(knn_path, "w") as f:
                    json.dump(knn_results, f, cls=NumpyEncoder)
                zipObj.write(knn_path, arcname=f"{file_name}/knn_results.json")
                files.append(knn_path)

            # Save the physical metadata
            if block_state[original_file_name].physical_metadata is not None:
                physical_metadata = block_state[original_file_name].physical_metadata
                metadata_path = f"{tmpdir}/physical_metadata_{file_name}.json"
                with open(metadata_path, "w") as f:
                    json.dump(physical_metadata._asdict(), f, cls=NumpyEncoder)
                zipObj.write(
                    metadata_path, arcname=f"{file_name}/physical_metadata.json"
                )
                files.append(metadata_path)

    files.append(zipObj.filename)
    return gr.update(value=files[::-1], visible=True)


CSS = """
        .header {
            display: flex;
            justify-content: center;
            align-items: center;
            padding: var(--block-padding);
            border-radius: var(--block-radius);
            background: var(--button-secondary-background-hover);
        }

        img {
            width: 150px;
            margin-right: 40px;
        }

        .title {
            text-align: left;
        }

        h1 {
            font-size: 36px;
            margin-bottom: 10px;
        }

        p {
            font-size: 18px;
        }

        input {
            width: 70px;
        }

        @media (max-width: 600px) {
            h1 {
                font-size: 24px;
            }

            p {
                font-size: 14px;
            }
        }

"""


with gr.Blocks(css=CSS) as block:
    block_state = gr.State({})
    gr.HTML(
        """
        <div class="header">
            <a href="https://www.nccr-catalysis.ch/" target="_blank">
                <img src="https://www.nccr-catalysis.ch/site/assets/files/1/nccr_catalysis_logo.svg" alt="NCCR Catalysis">
            </a>
            <div class="title">
                <h1>Atom Detection</h1>
                <p>Quantitative description of metal center organization in single-atom catalysts</p>
            </div>
        </div>
        """
    )
    with gr.Row():
        with gr.Column():
            with gr.Row():
                n_species = gr.Number(
                    label="Number of species",
                    value=1,
                    precision=0,
                    visible=True,
                )
                threshold = gr.Slider(
                    minimum=0.0,
                    maximum=1.0,
                    value=0.8,
                    label="Threshold",
                    visible=True,
                )
                architecture = gr.Dropdown(
                    label="Architecture",
                    choices=[
                        ModelArgs.BASICCNN,
                        # ModelArgs.RESNET18,
                    ],
                    value=ModelArgs.BASICCNN,
                    visible=False,
                )
            files = gr.Files(
                label="Images",
                file_types=[".tif", ".tiff"],
                type="file",
                interactive=True,
            )
            button = gr.Button(value="Run")
        with gr.Column():
            with gr.Tab("Masked prediction") as masked_tab:
                masked_prediction_gallery = gr.Gallery(label="Masked predictions")
            with gr.Tab("Nearest neighbors") as nn_tab:
                bokeh_plot = gr.Plot(show_label=False)
                error_html = gr.HTML(visible=False)
            export_btn = gr.Button(value="Export files", visible=False)
            exported_files = gr.File(
                label="Exported files",
                file_count="multiple",
                type="file",
                interactive=False,
                visible=False,
            )
    button.click(
        batch_fn,
        inputs=[files, n_species, threshold, architecture, block_state],
        outputs=[
            masked_prediction_gallery,
            block_state,
            export_btn,
            bokeh_plot,
            error_html,
        ],
    )
    export_btn.click(
        batch_export_files, [masked_prediction_gallery, block_state], [exported_files]
    )
    with gr.Accordion(label="How to ✨", open=True):
        gr.HTML(
            """
            <div style="font-size: 14px;">
            <ol>
                <li>Select one or multiple microscopy images as <b>.tiff files</b> πŸ“·πŸ”¬</li>
                <li>Upload individual or multiple .tif images for processing πŸ“€πŸ”’</li>
                <li>Export the output files. The generated zip archive will contain:
                    <ul>
                        <li>An image with overlayed atomic positions πŸŒŸπŸ”</li>
                        <li>A table of atomic positions (in px) along with their probability πŸ“ŠπŸ’Ž</li>
                        <li>Physical metadata of the respective images πŸ“„πŸ”</li>
                        <li>JSON-formatted plot data πŸ“ŠπŸ“</li>
                    </ul>
                </li>
            </ol>
            <details style="padding: 5px; border-radius: 5px; background: var(--button-secondary-background-hover); font-size: 14px;">
            <summary>Note</summary>
            <ul style="padding-left: 10px;">
            <li>
            Structural descriptors beyond pixel-wise atom detections are available as outputs only if images present an embedded real-space calibration (e.g., inΒ <a href="https://imagej.nih.gov/ij/docs/guide/146-30.html#sub:Set-Scale...">nm px-1</a>) πŸ“·πŸ”¬
            </li>
            <li>
            32-bit images will be processed correctly, but appear as mostly white in the image preview window
            </li>
            </ul>
            </details>
            </div>
     """
        )
    with gr.Accordion(label="Disclaimer and License", open=False):
        gr.HTML(
            """
            <div class="acknowledgments">
                <h3>Disclaimer</h3>
                <p>NCCR licenses the Atom Detection Web-App utilisation β€œas is” with no express or implied warranty of any kind. NCCR specifically disclaims all express or implied warranties to the fullest extent allowed by applicable law, including without limitation all implied warranties of merchantability, title or fitness for any particular purpose or non-infringement. No oral or written information or advice given by the authors shall create or form the basis of any warranty of any kind.</p>
                <h3>License</h3>
                <p>Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the β€œSoftware”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
<br>
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
<br>
The software is provided β€œas is”, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software.</p>
            </div>
            """
        )
    gr.HTML(
        """
            <div style="background-color: var(--secondary-100); border-radius: 5px; padding: 10px;">
                <p style='font-size: 14px; color: black'>To reference the use of this web app in a publication, please refer to the Atom Detection web app and the development described in this publication: K. Rossi et al. Adv. Mater. 2023, <a href="https://doi.org/10.1002/adma.202307991">doi:10.1002/adma.202307991</a>.</p>
            </div>
            """
    )

block.launch(
    share=False,
    show_error=True,
    server_name="0.0.0.0",
    enable_queue=True,
)