Switch to unified view

a b/notebooks/ni_viewer_best.ipynb
1
{
2
 "cells": [
3
  {
4
   "cell_type": "code",
5
   "execution_count": 11,
6
   "metadata": {},
7
   "outputs": [
8
    {
9
     "name": "stdout",
10
     "output_type": "stream",
11
     "text": [
12
      "Using matplotlib backend: TkAgg\n"
13
     ]
14
    }
15
   ],
16
   "source": [
17
    "import nibabel as nib\n",
18
    "import os\n",
19
    "from nilearn.image import crop_img\n",
20
    "from nilearn.image import new_img_like, resample_to_img\n",
21
    "import numpy as np\n",
22
    "\n",
23
    "import sys\n",
24
    "sys.path.append('..')\n",
25
    "\n",
26
    "from scipy.ndimage.filters import gaussian_filter, gaussian_laplace, laplace, maximum_filter, minimum_filter, prewitt, sobel\n",
27
    "\n",
28
    "from fetal_net.postprocess import postprocess_prediction\n",
29
    "\n",
30
    "%matplotlib\n",
31
    "\n",
32
    "def vod(mask1, mask2, verbose=False):\n",
33
    "    mask1, mask2 = mask1.flatten(), mask2.flatten()\n",
34
    "    intersection = np.sum((mask1 + mask2) > 1)\n",
35
    "    union = np.sum((mask1+mask2) > 0)\n",
36
    "    if verbose:\n",
37
    "        print('intersection\\t', intersection)\n",
38
    "        print('union\\t\\t', union)\n",
39
    "    return 1 - (intersection + 1) / (union + 1)"
40
   ]
41
  },
42
  {
43
   "cell_type": "code",
44
   "execution_count": null,
45
   "metadata": {},
46
   "outputs": [],
47
   "source": [
48
    "!ls ../../Datasets/fetus/78"
49
   ]
50
  },
51
  {
52
   "cell_type": "code",
53
   "execution_count": 163,
54
   "metadata": {},
55
   "outputs": [
56
    {
57
     "name": "stdout",
58
     "output_type": "stream",
59
     "text": [
60
      "222  260  41_2\r\n"
61
     ]
62
    }
63
   ],
64
   "source": [
65
    "!ls ../../predictions/unet96_all/predictions/val/"
66
   ]
67
  },
68
  {
69
   "cell_type": "code",
70
   "execution_count": 184,
71
   "metadata": {
72
    "scrolled": true
73
   },
74
   "outputs": [],
75
   "source": [
76
    "src_dir = '../../predictions/unet96_all/predictions/val/'\n",
77
    "subject_id = '222'\n",
78
    "vol = nib.load(os.path.join(src_dir, subject_id, 'data_volume.nii.gz'))\n",
79
    "truth = nib.load(os.path.join(src_dir, subject_id, 'truth.nii.gz'))\n",
80
    "pred = nib.load(os.path.join(src_dir, subject_id, 'prediction.nii.gz'))"
81
   ]
82
  },
83
  {
84
   "cell_type": "code",
85
   "execution_count": 185,
86
   "metadata": {},
87
   "outputs": [
88
    {
89
     "name": "stdout",
90
     "output_type": "stream",
91
     "text": [
92
      "thres\t\t: 0.1412296536763702\n",
93
      "pp_0_01\t\t: 0.1259709038930662\n",
94
      "pp_05_05\t: 0.13838074797935984\n",
95
      "pp_1_05\t\t: 0.15213493934900457\n"
96
     ]
97
    }
98
   ],
99
   "source": [
100
    "print('thres\\t\\t: {}'.format(vod(truth.get_data(), pred.get_data() > 0.5)))\n",
101
    "print('pp_0_01\\t\\t: {}'.format(vod(truth.get_data(), postprocess_prediction(pred.get_data(), gaussian_std=0, threshold=0.1))))\n",
102
    "print('pp_05_05\\t: {}'.format(vod(truth.get_data(), postprocess_prediction(pred.get_data(), gaussian_std=0.5, threshold=0.5))))\n",
103
    "print('pp_1_05\\t\\t: {}'.format(vod(truth.get_data(), postprocess_prediction(pred.get_data(), gaussian_std=1, threshold=0.5))))"
104
   ]
105
  },
106
  {
107
   "cell_type": "code",
108
   "execution_count": 166,
109
   "metadata": {},
110
   "outputs": [
111
    {
112
     "data": {
113
      "text/plain": [
114
       "4.408514612614356"
115
      ]
116
     },
117
     "execution_count": 166,
118
     "metadata": {},
119
     "output_type": "execute_result"
120
    }
121
   ],
122
   "source": [
123
    "np.max(vol.get_data())"
124
   ]
125
  },
126
  {
127
   "cell_type": "code",
128
   "execution_count": 186,
129
   "metadata": {},
130
   "outputs": [
131
    {
132
     "data": {
133
      "text/plain": [
134
       "<OrthoSlicer3D: (256, 256, 52)>"
135
      ]
136
     },
137
     "execution_count": 186,
138
     "metadata": {},
139
     "output_type": "execute_result"
140
    },
141
    {
142
     "name": "stderr",
143
     "output_type": "stream",
144
     "text": [
145
      "Traceback (most recent call last):\n",
146
      "  File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 388, in process\n",
147
      "    proxy(*args, **kwargs)\n",
148
      "  File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 228, in __call__\n",
149
      "    return mtd(*args, **kwargs)\n",
150
      "TypeError: _cleanup() takes 1 positional argument but 2 were given\n"
151
     ]
152
    }
153
   ],
154
   "source": [
155
    "new_img_like(vol, data=vol.get_data()+pred.get_data()*3).orthoview()"
156
   ]
157
  },
158
  {
159
   "cell_type": "code",
160
   "execution_count": 99,
161
   "metadata": {
162
    "collapsed": true
163
   },
164
   "outputs": [
165
    {
166
     "data": {
167
      "text/plain": [
168
       "<OrthoSlicer3D: (256, 256, 108)>"
169
      ]
170
     },
171
     "execution_count": 99,
172
     "metadata": {},
173
     "output_type": "execute_result"
174
    },
175
    {
176
     "name": "stderr",
177
     "output_type": "stream",
178
     "text": [
179
      "Traceback (most recent call last):\n",
180
      "  File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 388, in process\n",
181
      "    proxy(*args, **kwargs)\n",
182
      "  File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 228, in __call__\n",
183
      "    return mtd(*args, **kwargs)\n",
184
      "TypeError: _cleanup() takes 1 positional argument but 2 were given\n"
185
     ]
186
    }
187
   ],
188
   "source": [
189
    "new_img_like(vol, data=pred.get_data()*2).orthoview()"
190
   ]
191
  },
192
  {
193
   "cell_type": "code",
194
   "execution_count": 9,
195
   "metadata": {
196
    "collapsed": true
197
   },
198
   "outputs": [
199
    {
200
     "ename": "NameError",
201
     "evalue": "name 'postprocess_prediction' is not defined",
202
     "traceback": [
203
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
204
      "\u001b[0;31mNameError\u001b[0m                                 Traceback (most recent call last)",
205
      "\u001b[0;32m<ipython-input-9-eccee40acd61>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mnew_img_like\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mvol\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdata\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mtruth\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m-\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpostprocess_prediction\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpred\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mget_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mthreshold\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.5\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mgaussian_std\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m0.75\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0morthoview\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
206
      "\u001b[0;31mNameError\u001b[0m: name 'postprocess_prediction' is not defined"
207
     ],
208
     "output_type": "error"
209
    }
210
   ],
211
   "source": [
212
    "new_img_like(vol, data=truth.get_data()-(postprocess_prediction(pred.get_data(), threshold=0.5, gaussian_std=0.75))).orthoview()"
213
   ]
214
  },
215
  {
216
   "cell_type": "code",
217
   "execution_count": 40,
218
   "metadata": {},
219
   "outputs": [
220
    {
221
     "name": "stdout",
222
     "output_type": "stream",
223
     "text": [
224
      "[5162475    5280    3609    3332    4608    4416    3130    2749    3290\n",
225
      "  181063] [0.11921013 0.19536062 0.27151111 0.3476616  0.42381208 0.49996257\n",
226
      " 0.57611306 0.65226355 0.72841404 0.80456452 0.88071501]\n"
227
     ]
228
    }
229
   ],
230
   "source": [
231
    "from matplotlib import pyplot as plt\n",
232
    "a, bins = np.histogram(pred.get_data())\n",
233
    "plt.hist(pred.get_data().reshape([-1]), bins=100) \n",
234
    "plt.title(\"histogram\") \n",
235
    "plt.show()\n",
236
    "print(a,bins)"
237
   ]
238
  },
239
  {
240
   "cell_type": "code",
241
   "execution_count": 206,
242
   "metadata": {},
243
   "outputs": [
244
    {
245
     "name": "stdout",
246
     "output_type": "stream",
247
     "text": [
248
      "intersection\t 0\n",
249
      "union\t\t 645376\n",
250
      "1: 0.9999984505180692\n"
251
     ]
252
    }
253
   ],
254
   "source": [
255
    "print('1: {}'.format(vod(vol.get_data()>0.5, vol.get_data()>0.5, verbose=True)))"
256
   ]
257
  },
258
  {
259
   "cell_type": "code",
260
   "execution_count": 209,
261
   "metadata": {},
262
   "outputs": [
263
    {
264
     "name": "stdout",
265
     "output_type": "stream",
266
     "text": [
267
      "intersection\t 295947\n",
268
      "union\t\t 338601\n",
269
      "1: 0.1259709038930662\n"
270
     ]
271
    }
272
   ],
273
   "source": [
274
    "print('1: {}'.format(vod(truth.get_data(), postprocess_prediction(pred.get_data(), gaussian_std=0.1, threshold=0.1), verbose=True)))\n",
275
    "#print('2: {}'.format(vod(truth.get_data(), postprocess_prediction(pred2.get_data(), gaussian_std=1, threshold=0.5))))\n",
276
    "#print('3: {}'.format(vod(truth.get_data(), postprocess_prediction(pred3.get_data(), gaussian_std=1, threshold=0.5))))"
277
   ]
278
  },
279
  {
280
   "cell_type": "code",
281
   "execution_count": null,
282
   "metadata": {},
283
   "outputs": [],
284
   "source": []
285
  },
286
  {
287
   "cell_type": "code",
288
   "execution_count": 77,
289
   "metadata": {},
290
   "outputs": [
291
    {
292
     "data": {
293
      "text/plain": [
294
       "<OrthoSlicer3D: (256, 256, 52)>"
295
      ]
296
     },
297
     "execution_count": 77,
298
     "metadata": {},
299
     "output_type": "execute_result"
300
    },
301
    {
302
     "name": "stderr",
303
     "output_type": "stream",
304
     "text": [
305
      "Traceback (most recent call last):\n",
306
      "  File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 388, in process\n",
307
      "    proxy(*args, **kwargs)\n",
308
      "  File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 228, in __call__\n",
309
      "    return mtd(*args, **kwargs)\n",
310
      "TypeError: _cleanup() takes 1 positional argument but 2 were given\n"
311
     ]
312
    }
313
   ],
314
   "source": [
315
    "new_img_like(truth, data=1*truth.get_data()+1*(postprocess_prediction(pred.get_data(), gaussian_std=0.5, threshold=0.5).astype(int))).orthoview()"
316
   ]
317
  },
318
  {
319
   "cell_type": "code",
320
   "execution_count": 218,
321
   "metadata": {},
322
   "outputs": [
323
    {
324
     "name": "stdout",
325
     "output_type": "stream",
326
     "text": [
327
      "0.0 - 0.14122965\n",
328
      "0.33 - 0.14118736\n",
329
      "0.67 - 0.14025291\n",
330
      "1.0 - 0.15213494\n",
331
      "1.3 - 0.1559348\n",
332
      "1.7 - 0.15859626\n",
333
      "2.0 - 0.16103441\n",
334
      "2.3 - 0.16361388\n",
335
      "2.7 - 0.16607572\n",
336
      "3.0 - 0.16784486\n"
337
     ]
338
    }
339
   ],
340
   "source": [
341
    "for i in range(10):\n",
342
    "    print('{:.2} - {:.8}'.format(i/3, vod(truth.get_data(), gaussian_filter(pred.get_data(), i/3) > 0.5)))"
343
   ]
344
  },
345
  {
346
   "cell_type": "code",
347
   "execution_count": 220,
348
   "metadata": {},
349
   "outputs": [
350
    {
351
     "name": "stdout",
352
     "output_type": "stream",
353
     "text": [
354
      "0.0 - 0.9065614372518576\n",
355
      "0.05 - 0.14814359689532586\n",
356
      "0.1 - 0.13409773457672813\n",
357
      "0.15 - 0.1278775639714016\n",
358
      "0.2 - 0.12535605723218368\n",
359
      "0.25 - 0.12583704184158218\n",
360
      "0.3 - 0.12729425517299553\n",
361
      "0.35 - 0.12959742000942132\n",
362
      "0.4 - 0.1326181465043882\n",
363
      "0.45 - 0.13660837921286184\n",
364
      "0.5 - 0.14120474323651855\n",
365
      "0.55 - 0.14619835062322617\n",
366
      "0.6 - 0.1525275180080523\n",
367
      "0.65 - 0.15985079129981627\n",
368
      "0.7 - 0.16676434855339228\n",
369
      "0.75 - 0.1763829533391864\n",
370
      "0.8 - 0.18880411182394718\n",
371
      "0.85 - 0.20404091332511465\n",
372
      "0.9 - 0.2253708992666924\n",
373
      "0.95 - 0.2582048951764738\n",
374
      "1.0 - 0.9999968056833283\n"
375
     ]
376
    }
377
   ],
378
   "source": [
379
    "for i in range(0,21):\n",
380
    "    threshold = i * 5 / 100\n",
381
    "    print('{} - {}'.format(threshold, vod(truth.get_data(), gaussian_filter(pred.get_data(), 0.33) > threshold)))"
382
   ]
383
  },
384
  {
385
   "cell_type": "code",
386
   "execution_count": 112,
387
   "metadata": {},
388
   "outputs": [
389
    {
390
     "name": "stdout",
391
     "output_type": "stream",
392
     "text": [
393
      "0 - 0.8936488796427577\n"
394
     ]
395
    }
396
   ],
397
   "source": [
398
    "for i in range(0,1):\n",
399
    "    print('{} - {}'.format(i, vod(truth.get_data(), sobel(pred.get_data()) > 0.4)))"
400
   ]
401
  },
402
  {
403
   "cell_type": "code",
404
   "execution_count": 37,
405
   "metadata": {},
406
   "outputs": [
407
    {
408
     "data": {
409
      "text/plain": [
410
       "0.5230235351692842"
411
      ]
412
     },
413
     "execution_count": 37,
414
     "metadata": {},
415
     "output_type": "execute_result"
416
    }
417
   ],
418
   "source": [
419
    "from scipy.ndimage.morphology import binary_fill_holes\n",
420
    "vod(truth.get_data(), binary_fill_holes(gaussian_filter(pred.get_data(), 1) > 0.5))"
421
   ]
422
  },
423
  {
424
   "cell_type": "code",
425
   "execution_count": 10,
426
   "metadata": {},
427
   "outputs": [
428
    {
429
     "name": "stdout",
430
     "output_type": "stream",
431
     "text": [
432
      "intersection\t 175895\n",
433
      "union\t\t 196316\n"
434
     ]
435
    },
436
    {
437
     "data": {
438
      "text/plain": [
439
       "0.10402053821115853"
440
      ]
441
     },
442
     "execution_count": 10,
443
     "metadata": {},
444
     "output_type": "execute_result"
445
    }
446
   ],
447
   "source": [
448
    "vod(truth.get_data(), postprocess_prediction(pred.get_data(), gaussian_std=1, threshold=0.5, connected_component=True), verbose=True)"
449
   ]
450
  },
451
  {
452
   "cell_type": "code",
453
   "execution_count": 14,
454
   "metadata": {},
455
   "outputs": [
456
    {
457
     "data": {
458
      "text/plain": [
459
       "(array([3125052,   11203,    7935,    8868,  112831,   40350,    2922,\n",
460
       "           2352,    2914,  588549]),\n",
461
       " array([0.11920303, 0.19536243, 0.27152183, 0.34768123, 0.42384063,\n",
462
       "        0.50000003, 0.57615943, 0.65231883, 0.72847823, 0.80463763,\n",
463
       "        0.88079703]))"
464
      ]
465
     },
466
     "execution_count": 14,
467
     "metadata": {},
468
     "output_type": "execute_result"
469
    }
470
   ],
471
   "source": [
472
    "np.histogram(pred.get_data())"
473
   ]
474
  },
475
  {
476
   "cell_type": "code",
477
   "execution_count": 178,
478
   "metadata": {},
479
   "outputs": [
480
    {
481
     "name": "stderr",
482
     "output_type": "stream",
483
     "text": [
484
      "/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/nibabel/viewers.py:416: FutureWarning: Using a non-tuple sequence for multidimensional indexing is deprecated; use `arr[tuple(seq)]` instead of `arr[seq]`. In the future this will be interpreted as an array index, `arr[np.array(seq)]`, which will result either in an error or a different result.\n",
485
      "  vdata = self._data[idx].ravel()\n"
486
     ]
487
    },
488
    {
489
     "data": {
490
      "text/plain": [
491
       "<OrthoSlicer3D: (256, 256, 108, 2)>"
492
      ]
493
     },
494
     "execution_count": 178,
495
     "metadata": {},
496
     "output_type": "execute_result"
497
    },
498
    {
499
     "name": "stderr",
500
     "output_type": "stream",
501
     "text": [
502
      "Traceback (most recent call last):\n",
503
      "  File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 388, in process\n",
504
      "    proxy(*args, **kwargs)\n",
505
      "  File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 228, in __call__\n",
506
      "    return mtd(*args, **kwargs)\n",
507
      "TypeError: _cleanup() takes 1 positional argument but 2 were given\n"
508
     ]
509
    }
510
   ],
511
   "source": [
512
    "rep1 = (vol.get_data())\n",
513
    "rep2 = postprocess_prediction(pred.get_data(), fill_holes=True, gaussian_std=0.5, threshold=0.5)\n",
514
    "rep3 = (truth.get_data())\n",
515
    "rep = np.concatenate([np.expand_dims(rep1+3*rep2, -1), np.expand_dims(rep1+3*rep3, -1)], axis=-1)\n",
516
    "new_img_like(vol, data=rep).orthoview()"
517
   ]
518
  },
519
  {
520
   "cell_type": "code",
521
   "execution_count": null,
522
   "metadata": {},
523
   "outputs": [],
524
   "source": []
525
  }
526
 ],
527
 "metadata": {
528
  "kernelspec": {
529
   "display_name": "Python 3",
530
   "language": "python",
531
   "name": "python3"
532
  },
533
  "language_info": {
534
   "codemirror_mode": {
535
    "name": "ipython",
536
    "version": 3
537
   },
538
   "file_extension": ".py",
539
   "mimetype": "text/x-python",
540
   "name": "python",
541
   "nbconvert_exporter": "python",
542
   "pygments_lexer": "ipython3",
543
   "version": "3.6.6"
544
  }
545
 },
546
 "nbformat": 4,
547
 "nbformat_minor": 1
548
}