[78ef36]: / docs / evaluation / index.html

Download this file

564 lines (410 with data), 35.7 kB

  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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="generator" content="Docutils 0.18.1: http://docutils.sourceforge.net/" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Evaluation &mdash; slideflow 3.0.0 documentation</title>
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<!-- <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> -->
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
<link rel="index" title="Index" href="../genindex/" />
<link rel="search" title="Search" href="../search/" />
<link rel="next" title="Layer Activations" href="../posthoc/" />
<link rel="prev" title="Training" href="../training/" />
<script src="../_static/js/modernizr.min.js"></script>
<!-- Preload the theme fonts -->
<link rel="preload" href="../_static/fonts/FreightSans/freight-sans-book.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../_static/fonts/FreightSans/freight-sans-medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../_static/fonts/IBMPlexMono/IBMPlexMono-Medium.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../_static/fonts/FreightSans/freight-sans-bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../_static/fonts/FreightSans/freight-sans-medium-italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="../_static/fonts/IBMPlexMono/IBMPlexMono-SemiBold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<!-- Preload the katex fonts -->
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Math-Italic.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Main-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Main-Bold.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size1-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size4-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size2-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Size3-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="preload" href="https://cdn.jsdelivr.net/npm/katex@0.10.0/dist/fonts/KaTeX_Caligraphic-Regular.woff2" as="font" type="font/woff2" crossorigin="anonymous">
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.15.2/css/all.css" integrity="sha384-vSIIfh2YWi9wW0r9iZe7RJPrKwp6bG+s9QZMoITbCckVJqGCCRhc+ccxNcdpHuYu" crossorigin="anonymous">
<script defer data-domain="slideflow.dev" src="https://plausible.io/js/script.js"></script>
</head>
<div class="container-fluid header-holder tutorials-header" id="header-holder">
<div class="container">
<div class="header-container">
<a class="header-logo" href="https://slideflow.dev" aria-label="Slideflow"></a>
<div class="main-menu">
<ul>
<li class="active">
<a href="https://slideflow.dev">Docs</a>
</li>
<li>
<a href="https://slideflow.dev/tutorial1/">Tutorials</a>
</li>
<li>
<a href="https://github.com/slideflow/slideflow">GitHub</a>
</li>
</ul>
</div>
<a class="main-menu-open-button" href="#" data-behavior="open-mobile-menu"></a>
</div>
</div>
</div>
<body class="pytorch-body">
<div class="table-of-contents-link-wrapper">
<span>Table of Contents</span>
<a href="#" class="toggle-table-of-contents" data-behavior="toggle-table-of-contents"></a>
</div>
<nav data-toggle="wy-nav-shift" class="pytorch-left-menu" id="pytorch-left-menu">
<div class="pytorch-side-scroll">
<div class="pytorch-menu pytorch-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<div class="pytorch-left-menu-search">
<div class="version">
3.0
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="../search/" method="get">
<input type="text" name="q" placeholder="Search Docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<p class="caption" role="heading"><span class="caption-text">Introduction</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="../installation/">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../overview/">Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="../quickstart/">Quickstart</a></li>
<li class="toctree-l1"><a class="reference internal" href="../project_setup/">Setting up a Project</a></li>
<li class="toctree-l1"><a class="reference internal" href="../datasets_and_val/">Datasets</a></li>
<li class="toctree-l1"><a class="reference internal" href="../slide_processing/">Slide Processing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../training/">Training</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Evaluation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../posthoc/">Layer Activations</a></li>
<li class="toctree-l1"><a class="reference internal" href="../uq/">Uncertainty Quantification</a></li>
<li class="toctree-l1"><a class="reference internal" href="../features/">Generating Features</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mil/">Multiple-Instance Learning (MIL)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../ssl/">Self-Supervised Learning (SSL)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stylegan/">Generative Networks (GANs)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../saliency/">Saliency Maps</a></li>
<li class="toctree-l1"><a class="reference internal" href="../segmentation/">Tissue Segmentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../cellseg/">Cell Segmentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../custom_loops/">Custom Training Loops</a></li>
<li class="toctree-l1"><a class="reference internal" href="../studio/">Slideflow Studio: Live Visualization</a></li>
<li class="toctree-l1"><a class="reference internal" href="../troubleshooting/">Troubleshooting</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Developer Notes</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tfrecords/">TFRecords: Reading and Writing</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dataloaders/">Dataloaders: Sampling and Augmentation</a></li>
<li class="toctree-l1"><a class="reference internal" href="../custom_extractors/">Custom Feature Extractors</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tile_labels/">Strong Supervision with Tile Labels</a></li>
<li class="toctree-l1"><a class="reference internal" href="../plugins/">Creating a Slideflow Plugin</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../slideflow/">slideflow</a></li>
<li class="toctree-l1"><a class="reference internal" href="../project/">slideflow.Project</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dataset/">slideflow.Dataset</a></li>
<li class="toctree-l1"><a class="reference internal" href="../dataset_features/">slideflow.DatasetFeatures</a></li>
<li class="toctree-l1"><a class="reference internal" href="../heatmap/">slideflow.Heatmap</a></li>
<li class="toctree-l1"><a class="reference internal" href="../model_params/">slideflow.ModelParams</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mosaic/">slideflow.Mosaic</a></li>
<li class="toctree-l1"><a class="reference internal" href="../slidemap/">slideflow.SlideMap</a></li>
<li class="toctree-l1"><a class="reference internal" href="../biscuit/">slideflow.biscuit</a></li>
<li class="toctree-l1"><a class="reference internal" href="../slideflow_cellseg/">slideflow.cellseg</a></li>
<li class="toctree-l1"><a class="reference internal" href="../io/">slideflow.io</a></li>
<li class="toctree-l1"><a class="reference internal" href="../io_tensorflow/">slideflow.io.tensorflow</a></li>
<li class="toctree-l1"><a class="reference internal" href="../io_torch/">slideflow.io.torch</a></li>
<li class="toctree-l1"><a class="reference internal" href="../gan/">slideflow.gan</a></li>
<li class="toctree-l1"><a class="reference internal" href="../grad/">slideflow.grad</a></li>
<li class="toctree-l1"><a class="reference internal" href="../mil_module/">slideflow.mil</a></li>
<li class="toctree-l1"><a class="reference internal" href="../model/">slideflow.model</a></li>
<li class="toctree-l1"><a class="reference internal" href="../model_tensorflow/">slideflow.model.tensorflow</a></li>
<li class="toctree-l1"><a class="reference internal" href="../model_torch/">slideflow.model.torch</a></li>
<li class="toctree-l1"><a class="reference internal" href="../norm/">slideflow.norm</a></li>
<li class="toctree-l1"><a class="reference internal" href="../simclr/">slideflow.simclr</a></li>
<li class="toctree-l1"><a class="reference internal" href="../slide/">slideflow.slide</a></li>
<li class="toctree-l1"><a class="reference internal" href="../slide_qc/">slideflow.slide.qc</a></li>
<li class="toctree-l1"><a class="reference internal" href="../stats/">slideflow.stats</a></li>
<li class="toctree-l1"><a class="reference internal" href="../util/">slideflow.util</a></li>
<li class="toctree-l1"><a class="reference internal" href="../studio_module/">slideflow.studio</a></li>
</ul>
<p class="caption" role="heading"><span class="caption-text">Tutorials</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../tutorial1/">Tutorial 1: Model training (simple)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial2/">Tutorial 2: Model training (advanced)</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial3/">Tutorial 3: Using a custom architecture</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial4/">Tutorial 4: Model evaluation &amp; heatmaps</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial5/">Tutorial 5: Creating a mosaic map</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial6/">Tutorial 6: Custom slide filtering</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial7/">Tutorial 7: Training with custom augmentations</a></li>
<li class="toctree-l1"><a class="reference internal" href="../tutorial8/">Tutorial 8: Multiple-Instance Learning</a></li>
</ul>
</div>
</div>
</nav>
<div class="pytorch-container">
<div class="pytorch-page-level-bar" id="pytorch-page-level-bar">
<div class="pytorch-breadcrumbs-wrapper">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="pytorch-breadcrumbs">
<li>
<a href="../">
Docs
</a> &gt;
</li>
<li>Evaluation</li>
<li class="pytorch-breadcrumbs-aside">
<a href="../_sources/evaluation.rst.txt" rel="nofollow"><img src="../_static/images/view-page-source-icon.svg"></a>
</li>
</ul>
</div>
</div>
<div class="pytorch-shortcuts-wrapper" id="pytorch-shortcuts-wrapper">
Shortcuts
</div>
</div>
<section data-toggle="wy-nav-shift" id="pytorch-content-wrap" class="pytorch-content-wrap">
<div class="pytorch-content-left">
<div class="rst-content">
<div role="main" class="main-content" itemscope="itemscope" itemtype="http://schema.org/Article">
<article itemprop="articleBody" id="pytorch-article" class="pytorch-article">
<section id="evaluation">
<span id="id1"></span><h1>Evaluation<a class="headerlink" href="#evaluation" title="Permalink to this heading"></a></h1>
<p>Slideflow includes several tools for evaluating trained models. In the next sections, we’ll review how to evaluate a model on a held-out test set, generate predictions without ground-truth labels, and visualize predictions with heatmaps.</p>
<section id="evaluating-a-test-set">
<h2>Evaluating a test set<a class="headerlink" href="#evaluating-a-test-set" title="Permalink to this heading"></a></h2>
<p>The <a class="reference internal" href="../project/#slideflow.Project.evaluate" title="slideflow.Project.evaluate"><code class="xref py py-meth docutils literal notranslate"><span class="pre">slideflow.Project.evaluate()</span></code></a> provides an easy interface for evaluating model performance on a held-out test set. Locate the saved model to evaluate (which will be in the project <code class="docutils literal notranslate"><span class="pre">models/</span></code> folder). <a class="reference internal" href="../training/#training-with-project"><span class="std std-ref">As with training</span></a>, the dataset to evaluate can be specified using either the <code class="docutils literal notranslate"><span class="pre">filters</span></code> or <code class="docutils literal notranslate"><span class="pre">dataset</span></code> arguments. If neither is provided, all slides in the project will be evaluated.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Method 1: specifying filters</span>
<span class="n">P</span><span class="o">.</span><span class="n">evaluate</span><span class="p">(</span>
<span class="n">model</span><span class="o">=</span><span class="s2">&quot;/path/to/trained_model_epoch1&quot;</span><span class="p">,</span>
<span class="n">outcomes</span><span class="o">=</span><span class="s2">&quot;tumor_type&quot;</span><span class="p">,</span>
<span class="n">filters</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;dataset&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;test&quot;</span><span class="p">]}</span>
<span class="p">)</span>
<span class="c1"># Method 2: specify a dataset</span>
<span class="n">dataset</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">dataset</span><span class="p">(</span><span class="n">tile_px</span><span class="o">=</span><span class="mi">299</span><span class="p">,</span> <span class="n">tile_um</span><span class="o">=</span><span class="s1">&#39;10x&#39;</span><span class="p">)</span>
<span class="n">test_dataset</span> <span class="o">=</span> <span class="n">dataset</span><span class="o">.</span><span class="n">filter</span><span class="p">({</span><span class="s2">&quot;dataset&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;test&quot;</span><span class="p">]})</span>
<span class="n">P</span><span class="o">.</span><span class="n">evaluate</span><span class="p">(</span>
<span class="n">model</span><span class="o">=</span><span class="s2">&quot;/path/to/trained_model_epoch1&quot;</span><span class="p">,</span>
<span class="n">outcomes</span><span class="o">=</span><span class="s2">&quot;tumor_type&quot;</span><span class="p">,</span>
<span class="n">dataset</span><span class="o">=</span><span class="n">test_dataset</span>
<span class="p">)</span>
</pre></div>
</div>
<p>Results are returned from the <code class="docutils literal notranslate"><span class="pre">Project.evaluate()</span></code> function as a dictionary and saved in the project evaluation directory. Tile-, slide-, and patient- level predictions are also saved in the corresponding project evaluation folder, <code class="docutils literal notranslate"><span class="pre">eval/</span></code>.</p>
</section>
<section id="generating-predictions">
<h2>Generating predictions<a class="headerlink" href="#generating-predictions" title="Permalink to this heading"></a></h2>
<section id="for-a-dataset">
<h3>For a dataset<a class="headerlink" href="#for-a-dataset" title="Permalink to this heading"></a></h3>
<p><a class="reference internal" href="../project/#slideflow.Project.predict" title="slideflow.Project.predict"><code class="xref py py-meth docutils literal notranslate"><span class="pre">slideflow.Project.predict()</span></code></a> provides an interface for generating model predictions on an entire dataset. As above, locate the saved model from which to generate predictions, and specify the dataset with either <code class="docutils literal notranslate"><span class="pre">filters</span></code> or <code class="docutils literal notranslate"><span class="pre">dataset</span></code> arguments.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">dfs</span> <span class="o">=</span> <span class="n">P</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span>
<span class="n">model</span><span class="o">=</span><span class="s2">&quot;/path/to/trained_model_epoch1&quot;</span><span class="p">,</span>
<span class="n">filters</span><span class="o">=</span><span class="p">{</span><span class="s2">&quot;dataset&quot;</span><span class="p">:</span> <span class="p">[</span><span class="s2">&quot;test&quot;</span><span class="p">]}</span>
<span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">dfs</span><span class="p">[</span><span class="s1">&#39;patient&#39;</span><span class="p">])</span>
</pre></div>
</div>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span> patient ... cohort-y_pred1
0 TCGA-05-4244-01Z-00-DX1... ... 0.032608
1 TCGA-05-4245-01Z-00-DX1... ... 0.216634
2 TCGA-05-4249-01Z-00-DX1... ... 0.000858
3 TCGA-05-4250-01Z-00-DX1... ... 0.015915
4 TCGA-05-4382-01Z-00-DX1... ... 0.020700
.. ... ... ...
936 TCGA-O2-A52S-01Z-00-DX1... ... 0.983500
937 TCGA-O2-A52V-01Z-00-DX1... ... 0.773328
938 TCGA-O2-A52W-01Z-00-DX1... ... 0.858558
939 TCGA-S2-AA1A-01Z-00-DX1... ... 0.000212
940 TCGA-XC-AA0X-01Z-00-DX1... ... 0.632612
</pre></div>
</div>
<p>Results are returned as a dictionary of pandas DataFrames (with the keys <code class="docutils literal notranslate"><span class="pre">'tile'</span></code>, <code class="docutils literal notranslate"><span class="pre">'slide'</span></code>, and <code class="docutils literal notranslate"><span class="pre">'patient'</span></code> for each level of prediction) and saved in the project evaluation directory, <code class="docutils literal notranslate"><span class="pre">eval/</span></code>.</p>
</section>
<section id="for-a-single-slide">
<h3>For a single slide<a class="headerlink" href="#for-a-single-slide" title="Permalink to this heading"></a></h3>
<p>You can also generate predictions for a single slide with either <a class="reference internal" href="../slide/#slideflow.slide.predict" title="slideflow.slide.predict"><code class="xref py py-func docutils literal notranslate"><span class="pre">slideflow.slide.predict()</span></code></a> or <a class="reference internal" href="../slide/#slideflow.WSI.predict" title="slideflow.WSI.predict"><code class="xref py py-meth docutils literal notranslate"><span class="pre">slideflow.WSI.predict()</span></code></a>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">slideflow</span> <span class="k">as</span> <span class="nn">sf</span>
<span class="n">slide</span> <span class="o">=</span> <span class="s1">&#39;/path/to/slide.svs&#39;</span>
<span class="n">model</span> <span class="o">=</span> <span class="s1">&#39;/path/to/model_epoch1&#39;</span>
<span class="n">sf</span><span class="o">.</span><span class="n">slide</span><span class="o">.</span><span class="n">predict</span><span class="p">(</span><span class="n">slide</span><span class="p">,</span> <span class="n">model</span><span class="p">)</span>
</pre></div>
</div>
<div class="sphx-glr-script-out highlight-none notranslate"><div class="highlight"><pre><span></span>array([0.84378019, 0.15622007])
</pre></div>
</div>
<p>The returned array has the shape <code class="docutils literal notranslate"><span class="pre">(num_classes,)</span></code>, indicating the whole-slide prediction for each outcome category. If the model was trained with uncertainty quantification, this function will return two arrays; the first with predictions, the second with estimated uncertainty.</p>
</section>
</section>
<section id="heatmaps">
<span id="generate-heatmaps"></span><h2>Heatmaps<a class="headerlink" href="#heatmaps" title="Permalink to this heading"></a></h2>
<section id="id2">
<h3>For a dataset<a class="headerlink" href="#id2" title="Permalink to this heading"></a></h3>
<p>Predictive heatmaps can be created for an entire dataset using <a class="reference internal" href="../project/#slideflow.Project.generate_heatmaps" title="slideflow.Project.generate_heatmaps"><code class="xref py py-meth docutils literal notranslate"><span class="pre">slideflow.Project.generate_heatmaps()</span></code></a>. Heatmaps will be saved and exported in the project directory. See the linked API documentation for arguments and customization.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">P</span><span class="o">.</span><span class="n">generate_heatmaps</span><span class="p">(</span><span class="n">model</span><span class="o">=</span><span class="s2">&quot;/path/to/trained_model_epoch1&quot;</span><span class="p">)</span>
</pre></div>
</div>
</section>
<section id="id3">
<h3>For a single slide<a class="headerlink" href="#id3" title="Permalink to this heading"></a></h3>
<p><a class="reference internal" href="../heatmap/#slideflow.Heatmap" title="slideflow.Heatmap"><code class="xref py py-class docutils literal notranslate"><span class="pre">slideflow.Heatmap</span></code></a> provides more granular control for calculating and displaying a heatmap for a given slide. The required arguments are:</p>
<ul class="simple">
<li><p><code class="docutils literal notranslate"><span class="pre">slide</span></code>: Either a path to a slide, or a <code class="xref py py-class docutils literal notranslate"><span class="pre">slideflow.WSI</span></code> object.</p></li>
<li><p><code class="docutils literal notranslate"><span class="pre">model</span></code>: Path to a saved Slideflow model.</p></li>
</ul>
<p>Additional keyword arguments can be used to customize and optimize the heatmap. In this example, we’ll increase the batch size to 64 and allow multiprocessing by setting <code class="docutils literal notranslate"><span class="pre">num_processes</span></code> equal to our CPU core count, 16.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">heatmap</span> <span class="o">=</span> <span class="n">sf</span><span class="o">.</span><span class="n">Heatmap</span><span class="p">(</span>
<span class="n">slide</span><span class="o">=</span><span class="s1">&#39;/path/to/slide.svs&#39;</span><span class="p">,</span>
<span class="n">model</span><span class="o">=</span><span class="s1">&#39;/path/to/model&#39;</span>
<span class="n">batch_size</span><span class="o">=</span><span class="mi">64</span><span class="p">,</span>
<span class="n">num_processes</span><span class="o">=</span><span class="mi">16</span>
<span class="p">)</span>
</pre></div>
</div>
<p>If <code class="docutils literal notranslate"><span class="pre">slide</span></code> is a <code class="xref py py-class docutils literal notranslate"><span class="pre">slideflow.WSI</span></code>, the heatmap will be calculated only within non-masked areas and ROIs, if applicable.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">slideflow.slide</span> <span class="kn">import</span> <span class="n">qc</span>
<span class="c1"># Prepare the slide</span>
<span class="n">wsi</span> <span class="o">=</span> <span class="n">sf</span><span class="o">.</span><span class="n">WSI</span><span class="p">(</span><span class="s1">&#39;slide.svs&#39;</span><span class="p">,</span> <span class="n">tile_px</span><span class="o">=</span><span class="mi">299</span><span class="p">,</span> <span class="n">tile_um</span><span class="o">=</span><span class="mi">302</span><span class="p">,</span> <span class="n">rois</span><span class="o">=</span><span class="s1">&#39;/path&#39;</span><span class="p">)</span>
<span class="n">wsi</span><span class="o">.</span><span class="n">qc</span><span class="p">([</span><span class="n">qc</span><span class="o">.</span><span class="n">Otsu</span><span class="p">(),</span> <span class="n">qc</span><span class="o">.</span><span class="n">Gaussian</span><span class="p">()])</span>
<span class="c1"># Generate a heatmap</span>
<span class="n">heatmap</span> <span class="o">=</span> <span class="n">sf</span><span class="o">.</span><span class="n">Heatmap</span><span class="p">(</span>
<span class="n">slide</span><span class="o">=</span><span class="n">wsi</span><span class="p">,</span>
<span class="n">model</span><span class="o">=</span><span class="s1">&#39;/path/to/model&#39;</span>
<span class="n">batch_size</span><span class="o">=</span><span class="mi">64</span><span class="p">,</span>
<span class="n">num_processes</span><span class="o">=</span><span class="mi">16</span>
<span class="p">)</span>
</pre></div>
</div>
<p>If <code class="docutils literal notranslate"><span class="pre">slide</span></code> is a path to a slide, Regions of Interest can be provided through the optional <code class="docutils literal notranslate"><span class="pre">roi_dir</span></code> or <code class="docutils literal notranslate"><span class="pre">rois</span></code> arguments.</p>
<p>Once generated, heatmaps can be rendered and displayed (ie. in a Jupyter notebook) with <a class="reference internal" href="../heatmap/#slideflow.Heatmap.plot" title="slideflow.Heatmap.plot"><code class="xref py py-meth docutils literal notranslate"><span class="pre">slideflow.Heatmap.plot()</span></code></a>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">heatmap</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">class_idx</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">cmap</span><span class="o">=</span><span class="s1">&#39;inferno&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Insets showing zoomed-in portions of the heatmap can be added with <a class="reference internal" href="../heatmap/#slideflow.Heatmap.add_inset" title="slideflow.Heatmap.add_inset"><code class="xref py py-meth docutils literal notranslate"><span class="pre">slideflow.Heatmap.add_inset()</span></code></a>:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">heatmap</span><span class="o">.</span><span class="n">add_inset</span><span class="p">(</span><span class="n">zoom</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="p">(</span><span class="mi">10000</span><span class="p">,</span> <span class="mi">10500</span><span class="p">),</span> <span class="n">y</span><span class="o">=</span><span class="p">(</span><span class="mi">2500</span><span class="p">,</span> <span class="mi">3000</span><span class="p">),</span> <span class="n">loc</span><span class="o">=</span><span class="mi">1</span><span class="p">,</span> <span class="n">axes</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="n">heatmap</span><span class="o">.</span><span class="n">add_inset</span><span class="p">(</span><span class="n">zoom</span><span class="o">=</span><span class="mi">20</span><span class="p">,</span> <span class="n">x</span><span class="o">=</span><span class="p">(</span><span class="mi">12000</span><span class="p">,</span> <span class="mi">12500</span><span class="p">),</span> <span class="n">y</span><span class="o">=</span><span class="p">(</span><span class="mi">7500</span><span class="p">,</span> <span class="mi">8000</span><span class="p">),</span> <span class="n">loc</span><span class="o">=</span><span class="mi">3</span><span class="p">,</span> <span class="n">axes</span><span class="o">=</span><span class="kc">False</span><span class="p">)</span>
<span class="n">heatmap</span><span class="o">.</span><span class="n">plot</span><span class="p">(</span><span class="n">class_idx</span><span class="o">=</span><span class="mi">0</span><span class="p">,</span> <span class="n">mpp</span><span class="o">=</span><span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
<img alt="../_images/heatmap_inset.jpg" src="../_images/heatmap_inset.jpg" />
<div class="line-block">
<div class="line"><br /></div>
</div>
<p>Save rendered heatmaps for each outcome category with <a class="reference internal" href="../heatmap/#slideflow.Heatmap.save" title="slideflow.Heatmap.save"><code class="xref py py-meth docutils literal notranslate"><span class="pre">slideflow.Heatmap.save()</span></code></a>. The spatial map of predictions, as calculated across the input slide, can be accessed through <code class="docutils literal notranslate"><span class="pre">Heatmap.predictions</span></code>. You can save the numpy array with calculated predictions (and uncertainty, if applicable) as an *.npz file using <a class="reference internal" href="../heatmap/#slideflow.Heatmap.save_npz" title="slideflow.Heatmap.save_npz"><code class="xref py py-meth docutils literal notranslate"><span class="pre">slideflow.Heatmap.save_npz()</span></code></a>.</p>
</section>
</section>
</section>
</article>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="../posthoc/" class="btn btn-neutral float-right" title="Layer Activations" accesskey="n" rel="next">Next <img src="../_static/images/chevron-right-orange.svg" class="next-page"></a>
<a href="../training/" class="btn btn-neutral" title="Training" accesskey="p" rel="prev"><img src="../_static/images/chevron-right-orange.svg" class="previous-page"> Previous</a>
</div>
<hr>
<div role="contentinfo">
<p>
&copy; Copyright 2023, James M Dolezal.
</p>
</div>
<div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</div>
</footer>
</div>
</div>
<div class="pytorch-content-right" id="pytorch-content-right">
<div class="pytorch-right-menu" id="pytorch-right-menu">
<div class="pytorch-side-scroll" id="pytorch-side-scroll-right">
<ul>
<li><a class="reference internal" href="#">Evaluation</a><ul>
<li><a class="reference internal" href="#evaluating-a-test-set">Evaluating a test set</a></li>
<li><a class="reference internal" href="#generating-predictions">Generating predictions</a><ul>
<li><a class="reference internal" href="#for-a-dataset">For a dataset</a></li>
<li><a class="reference internal" href="#for-a-single-slide">For a single slide</a></li>
</ul>
</li>
<li><a class="reference internal" href="#heatmaps">Heatmaps</a><ul>
<li><a class="reference internal" href="#id2">For a dataset</a></li>
<li><a class="reference internal" href="#id3">For a single slide</a></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
</div>
</div>
</section>
</div>
<script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
<script data-url_root="../" id="documentation_options" src="../_static/documentation_options.js"></script>
<script src="../_static/doctools.js"></script>
<script src="../_static/sphinx_highlight.js"></script>
<script type="text/javascript" src="../_static/js/vendor/jquery-3.6.3.min.js"></script>
<script type="text/javascript" src="../_static/js/vendor/popper.min.js"></script>
<script type="text/javascript" src="../_static/js/vendor/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/list.js/1.5.0/list.min.js"></script>
<script type="text/javascript" src="../_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
<!-- Begin Footer -->
<!-- End Footer -->
<!-- Begin Mobile Menu -->
<div class="mobile-main-menu">
<div class="container-fluid">
<div class="container">
<div class="mobile-main-menu-header-container">
<a class="header-logo" href="https://pytorch.org/" aria-label="PyTorch"></a>
<a class="main-menu-close-button" href="#" data-behavior="close-mobile-menu"></a>
</div>
</div>
</div>
<div class="mobile-main-menu-links-container">
<div class="main-menu">
<ul>
<li>
<a href="https://slideflow.dev">Docs</a>
</li>
<li>
<a href="https://slideflow.dev/tutorial1/">Tutorials</a>
</li>
<li>
<a href="https://github.com/slideflow/slideflow">Github</a>
</li>
</ul>
</div>
</div>
</div>
<!-- End Mobile Menu -->
<script script type="text/javascript">
var collapsedSections = [];
</script>
<script type="text/javascript" src="../_static/js/vendor/anchor.min.js"></script>
<script type="text/javascript">
$(document).ready(function() {
mobileMenu.bind();
mobileTOC.bind();
pytorchAnchors.bind();
sideMenus.bind();
scrollToAnchor.bind();
highlightNavigation.bind();
mainMenuDropdown.bind();
filterTags.bind();
// Add class to links that have code blocks, since we cannot create links in code blocks
$("article.pytorch-article a span.pre").each(function(e) {
$(this).closest("a").addClass("has-code");
});
})
</script>
</body>
</html>