549 lines (548 with data), 16.0 kB
{
"cells": [
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Using matplotlib backend: TkAgg\n"
]
}
],
"source": [
"import nibabel as nib\n",
"import os\n",
"from nilearn.image import crop_img\n",
"from nilearn.image import new_img_like, resample_to_img\n",
"import numpy as np\n",
"\n",
"import sys\n",
"sys.path.append('..')\n",
"\n",
"from scipy.ndimage.filters import gaussian_filter, gaussian_laplace, laplace, maximum_filter, minimum_filter, prewitt, sobel\n",
"\n",
"from fetal_net.postprocess import postprocess_prediction\n",
"\n",
"%matplotlib\n",
"\n",
"def vod(mask1, mask2, verbose=False):\n",
" mask1, mask2 = mask1.flatten(), mask2.flatten()\n",
" intersection = np.sum((mask1 + mask2) > 1)\n",
" union = np.sum((mask1+mask2) > 0)\n",
" if verbose:\n",
" print('intersection\\t', intersection)\n",
" print('union\\t\\t', union)\n",
" return 1 - (intersection + 1) / (union + 1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!ls ../../Datasets/fetus/78"
]
},
{
"cell_type": "code",
"execution_count": 163,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"222 260 41_2\r\n"
]
}
],
"source": [
"!ls ../../predictions/unet96_all/predictions/val/"
]
},
{
"cell_type": "code",
"execution_count": 184,
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"src_dir = '../../predictions/unet96_all/predictions/val/'\n",
"subject_id = '222'\n",
"vol = nib.load(os.path.join(src_dir, subject_id, 'data_volume.nii.gz'))\n",
"truth = nib.load(os.path.join(src_dir, subject_id, 'truth.nii.gz'))\n",
"pred = nib.load(os.path.join(src_dir, subject_id, 'prediction.nii.gz'))"
]
},
{
"cell_type": "code",
"execution_count": 185,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"thres\t\t: 0.1412296536763702\n",
"pp_0_01\t\t: 0.1259709038930662\n",
"pp_05_05\t: 0.13838074797935984\n",
"pp_1_05\t\t: 0.15213493934900457\n"
]
}
],
"source": [
"print('thres\\t\\t: {}'.format(vod(truth.get_data(), pred.get_data() > 0.5)))\n",
"print('pp_0_01\\t\\t: {}'.format(vod(truth.get_data(), postprocess_prediction(pred.get_data(), gaussian_std=0, threshold=0.1))))\n",
"print('pp_05_05\\t: {}'.format(vod(truth.get_data(), postprocess_prediction(pred.get_data(), gaussian_std=0.5, threshold=0.5))))\n",
"print('pp_1_05\\t\\t: {}'.format(vod(truth.get_data(), postprocess_prediction(pred.get_data(), gaussian_std=1, threshold=0.5))))"
]
},
{
"cell_type": "code",
"execution_count": 166,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"4.408514612614356"
]
},
"execution_count": 166,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.max(vol.get_data())"
]
},
{
"cell_type": "code",
"execution_count": 186,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<OrthoSlicer3D: (256, 256, 52)>"
]
},
"execution_count": 186,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 388, in process\n",
" proxy(*args, **kwargs)\n",
" File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 228, in __call__\n",
" return mtd(*args, **kwargs)\n",
"TypeError: _cleanup() takes 1 positional argument but 2 were given\n"
]
}
],
"source": [
"new_img_like(vol, data=vol.get_data()+pred.get_data()*3).orthoview()"
]
},
{
"cell_type": "code",
"execution_count": 99,
"metadata": {
"collapsed": true
},
"outputs": [
{
"data": {
"text/plain": [
"<OrthoSlicer3D: (256, 256, 108)>"
]
},
"execution_count": 99,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 388, in process\n",
" proxy(*args, **kwargs)\n",
" File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 228, in __call__\n",
" return mtd(*args, **kwargs)\n",
"TypeError: _cleanup() takes 1 positional argument but 2 were given\n"
]
}
],
"source": [
"new_img_like(vol, data=pred.get_data()*2).orthoview()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"outputs": [
{
"ename": "NameError",
"evalue": "name 'postprocess_prediction' is not defined",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\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",
"\u001b[0;31mNameError\u001b[0m: name 'postprocess_prediction' is not defined"
],
"output_type": "error"
}
],
"source": [
"new_img_like(vol, data=truth.get_data()-(postprocess_prediction(pred.get_data(), threshold=0.5, gaussian_std=0.75))).orthoview()"
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[5162475 5280 3609 3332 4608 4416 3130 2749 3290\n",
" 181063] [0.11921013 0.19536062 0.27151111 0.3476616 0.42381208 0.49996257\n",
" 0.57611306 0.65226355 0.72841404 0.80456452 0.88071501]\n"
]
}
],
"source": [
"from matplotlib import pyplot as plt\n",
"a, bins = np.histogram(pred.get_data())\n",
"plt.hist(pred.get_data().reshape([-1]), bins=100) \n",
"plt.title(\"histogram\") \n",
"plt.show()\n",
"print(a,bins)"
]
},
{
"cell_type": "code",
"execution_count": 206,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"intersection\t 0\n",
"union\t\t 645376\n",
"1: 0.9999984505180692\n"
]
}
],
"source": [
"print('1: {}'.format(vod(vol.get_data()>0.5, vol.get_data()>0.5, verbose=True)))"
]
},
{
"cell_type": "code",
"execution_count": 209,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"intersection\t 295947\n",
"union\t\t 338601\n",
"1: 0.1259709038930662\n"
]
}
],
"source": [
"print('1: {}'.format(vod(truth.get_data(), postprocess_prediction(pred.get_data(), gaussian_std=0.1, threshold=0.1), verbose=True)))\n",
"#print('2: {}'.format(vod(truth.get_data(), postprocess_prediction(pred2.get_data(), gaussian_std=1, threshold=0.5))))\n",
"#print('3: {}'.format(vod(truth.get_data(), postprocess_prediction(pred3.get_data(), gaussian_std=1, threshold=0.5))))"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<OrthoSlicer3D: (256, 256, 52)>"
]
},
"execution_count": 77,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 388, in process\n",
" proxy(*args, **kwargs)\n",
" File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 228, in __call__\n",
" return mtd(*args, **kwargs)\n",
"TypeError: _cleanup() takes 1 positional argument but 2 were given\n"
]
}
],
"source": [
"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()"
]
},
{
"cell_type": "code",
"execution_count": 218,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0 - 0.14122965\n",
"0.33 - 0.14118736\n",
"0.67 - 0.14025291\n",
"1.0 - 0.15213494\n",
"1.3 - 0.1559348\n",
"1.7 - 0.15859626\n",
"2.0 - 0.16103441\n",
"2.3 - 0.16361388\n",
"2.7 - 0.16607572\n",
"3.0 - 0.16784486\n"
]
}
],
"source": [
"for i in range(10):\n",
" print('{:.2} - {:.8}'.format(i/3, vod(truth.get_data(), gaussian_filter(pred.get_data(), i/3) > 0.5)))"
]
},
{
"cell_type": "code",
"execution_count": 220,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.0 - 0.9065614372518576\n",
"0.05 - 0.14814359689532586\n",
"0.1 - 0.13409773457672813\n",
"0.15 - 0.1278775639714016\n",
"0.2 - 0.12535605723218368\n",
"0.25 - 0.12583704184158218\n",
"0.3 - 0.12729425517299553\n",
"0.35 - 0.12959742000942132\n",
"0.4 - 0.1326181465043882\n",
"0.45 - 0.13660837921286184\n",
"0.5 - 0.14120474323651855\n",
"0.55 - 0.14619835062322617\n",
"0.6 - 0.1525275180080523\n",
"0.65 - 0.15985079129981627\n",
"0.7 - 0.16676434855339228\n",
"0.75 - 0.1763829533391864\n",
"0.8 - 0.18880411182394718\n",
"0.85 - 0.20404091332511465\n",
"0.9 - 0.2253708992666924\n",
"0.95 - 0.2582048951764738\n",
"1.0 - 0.9999968056833283\n"
]
}
],
"source": [
"for i in range(0,21):\n",
" threshold = i * 5 / 100\n",
" print('{} - {}'.format(threshold, vod(truth.get_data(), gaussian_filter(pred.get_data(), 0.33) > threshold)))"
]
},
{
"cell_type": "code",
"execution_count": 112,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0 - 0.8936488796427577\n"
]
}
],
"source": [
"for i in range(0,1):\n",
" print('{} - {}'.format(i, vod(truth.get_data(), sobel(pred.get_data()) > 0.4)))"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5230235351692842"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"from scipy.ndimage.morphology import binary_fill_holes\n",
"vod(truth.get_data(), binary_fill_holes(gaussian_filter(pred.get_data(), 1) > 0.5))"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"intersection\t 175895\n",
"union\t\t 196316\n"
]
},
{
"data": {
"text/plain": [
"0.10402053821115853"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"vod(truth.get_data(), postprocess_prediction(pred.get_data(), gaussian_std=1, threshold=0.5, connected_component=True), verbose=True)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(array([3125052, 11203, 7935, 8868, 112831, 40350, 2922,\n",
" 2352, 2914, 588549]),\n",
" array([0.11920303, 0.19536243, 0.27152183, 0.34768123, 0.42384063,\n",
" 0.50000003, 0.57615943, 0.65231883, 0.72847823, 0.80463763,\n",
" 0.88079703]))"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.histogram(pred.get_data())"
]
},
{
"cell_type": "code",
"execution_count": 178,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/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",
" vdata = self._data[idx].ravel()\n"
]
},
{
"data": {
"text/plain": [
"<OrthoSlicer3D: (256, 256, 108, 2)>"
]
},
"execution_count": 178,
"metadata": {},
"output_type": "execute_result"
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"Traceback (most recent call last):\n",
" File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 388, in process\n",
" proxy(*args, **kwargs)\n",
" File \"/home/galdude33/anaconda3/envs/keras/lib/python3.6/site-packages/matplotlib/cbook/__init__.py\", line 228, in __call__\n",
" return mtd(*args, **kwargs)\n",
"TypeError: _cleanup() takes 1 positional argument but 2 were given\n"
]
}
],
"source": [
"rep1 = (vol.get_data())\n",
"rep2 = postprocess_prediction(pred.get_data(), fill_holes=True, gaussian_std=0.5, threshold=0.5)\n",
"rep3 = (truth.get_data())\n",
"rep = np.concatenate([np.expand_dims(rep1+3*rep2, -1), np.expand_dims(rep1+3*rep3, -1)], axis=-1)\n",
"new_img_like(vol, data=rep).orthoview()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.6"
}
},
"nbformat": 4,
"nbformat_minor": 1
}