Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save thewtex/079c59ea71a1da59bea5e14cb6b73d1f to your computer and use it in GitHub Desktop.
Save thewtex/079c59ea71a1da59bea5e14cb6b73d1f to your computer and use it in GitHub Desktop.
ITKDaskSkimageLocalMeanResizeComparison.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import itk\n",
"from itkwidgets import view\n",
"import dask.array as da\n",
"import skimage.transform\n",
"import numpy as np\n",
"from functools import reduce"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 596 ms, sys: 5.7 s, total: 6.29 s\n",
"Wall time: 13.5 s\n"
]
}
],
"source": [
"# Available https://data.kitware.com/#folder/5ac69d1b8d777f0685792255, both huc01resa.mhd, huc01resa.raw required\n",
"%time image = itk.imread('huc01resa.mhd')"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Image (0x55a9a14b1ed0)\n",
" RTTI typeinfo: itk::Image<unsigned char, 3u>\n",
" Reference Count: 1\n",
" Modified Time: 349\n",
" Debug: Off\n",
" Object Name: \n",
" Observers: \n",
" none\n",
" Source: (none)\n",
" Source output name: (none)\n",
" Release Data: Off\n",
" Data Released: False\n",
" Global Release Data: Off\n",
" PipelineMTime: 179\n",
" UpdateMTime: 348\n",
" RealTimeStamp: 0 seconds \n",
" LargestPossibleRegion: \n",
" Dimension: 3\n",
" Index: [0, 0, 0]\n",
" Size: [2048, 2048, 1600]\n",
" BufferedRegion: \n",
" Dimension: 3\n",
" Index: [0, 0, 0]\n",
" Size: [2048, 2048, 1600]\n",
" RequestedRegion: \n",
" Dimension: 3\n",
" Index: [0, 0, 0]\n",
" Size: [2048, 2048, 1600]\n",
" Spacing: [1, 1, 1]\n",
" Origin: [0, 0, 0]\n",
" Direction: \n",
"1 0 0\n",
"0 1 0\n",
"0 0 1\n",
"\n",
" IndexToPointMatrix: \n",
"1 0 0\n",
"0 1 0\n",
"0 0 1\n",
"\n",
" PointToIndexMatrix: \n",
"1 0 0\n",
"0 1 0\n",
"0 0 1\n",
"\n",
" Inverse Direction: \n",
"1 0 0\n",
"0 1 0\n",
"0 0 1\n",
"\n",
" PixelContainer: \n",
" ImportImageContainer (0x55a9a3bece50)\n",
" RTTI typeinfo: itk::ImportImageContainer<unsigned long, unsigned char>\n",
" Reference Count: 1\n",
" Modified Time: 345\n",
" Debug: Off\n",
" Object Name: \n",
" Observers: \n",
" none\n",
" Pointer: 0x7ff769752010\n",
" Container manages memory: true\n",
" Size: 6710886400\n",
" Capacity: 6710886400\n",
"\n"
]
}
],
"source": [
"print(image)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"shrink_factors = (10, 10, 10)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"#view(image)"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 loop, best of 3: 1.86 s per loop\n"
]
}
],
"source": [
"%timeit shrunk = itk.BinShrinkImageFilter(image, ShrinkFactors=shrink_factors)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Image (0x55a9a422a5e0)\n",
" RTTI typeinfo: itk::Image<unsigned char, 3u>\n",
" Reference Count: 1\n",
" Modified Time: 476\n",
" Debug: Off\n",
" Object Name: \n",
" Observers: \n",
" none\n",
" Source: (none)\n",
" Source output name: (none)\n",
" Release Data: Off\n",
" Data Released: False\n",
" Global Release Data: Off\n",
" PipelineMTime: 465\n",
" UpdateMTime: 475\n",
" RealTimeStamp: 0 seconds \n",
" LargestPossibleRegion: \n",
" Dimension: 3\n",
" Index: [0, 0, 0]\n",
" Size: [204, 204, 160]\n",
" BufferedRegion: \n",
" Dimension: 3\n",
" Index: [0, 0, 0]\n",
" Size: [204, 204, 160]\n",
" RequestedRegion: \n",
" Dimension: 3\n",
" Index: [0, 0, 0]\n",
" Size: [204, 204, 160]\n",
" Spacing: [10, 10, 10]\n",
" Origin: [4.5, 4.5, 4.5]\n",
" Direction: \n",
"1 0 0\n",
"0 1 0\n",
"0 0 1\n",
"\n",
" IndexToPointMatrix: \n",
"10 0 0\n",
"0 10 0\n",
"0 0 10\n",
"\n",
" PointToIndexMatrix: \n",
"0.1 0 0\n",
"0 0.1 0\n",
"0 0 0.1\n",
"\n",
" Inverse Direction: \n",
"1 0 0\n",
"0 1 0\n",
"0 0 1\n",
"\n",
" PixelContainer: \n",
" ImportImageContainer (0x55a9a42a70f0)\n",
" RTTI typeinfo: itk::ImportImageContainer<unsigned long, unsigned char>\n",
" Reference Count: 1\n",
" Modified Time: 473\n",
" Debug: Off\n",
" Object Name: \n",
" Observers: \n",
" none\n",
" Pointer: 0x55a9a44b9460\n",
" Container manages memory: true\n",
" Size: 6658560\n",
" Capacity: 6658560\n",
"\n"
]
}
],
"source": [
"shrunk = itk.BinShrinkImageFilter(image, ShrinkFactors=shrink_factors)\n",
"print(shrunk)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('shrinking with: ', [2, 2, 1])\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "d159660490e54eb2993ac0596e33feb1",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Vmlld2VyKGdyYWRpZW50X29wYWNpdHk9MC4yMiwgcmVuZGVyZWRfaW1hZ2U9PGl0a0ltYWdlUHl0aG9uLml0a0ltYWdlVUMzOyBwcm94eSBvZiA8U3dpZyBPYmplY3Qgb2YgdHlwZSAnaXRrSW3igKY=\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"view(shrunk)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"image_array = itk.GetArrayViewFromImage(image)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 loop, best of 3: 44.4 s per loop\n"
]
}
],
"source": [
"%timeit skshrunk = skimage.transform.downscale_local_mean(image_array, shrink_factors[::-1])"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('shrinking with: ', [2, 2, 1])\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "a989561a9e0145b4ac8166e7fb3b17ca",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Vmlld2VyKGdyYWRpZW50X29wYWNpdHk9MC4yMiwgcmVuZGVyZWRfaW1hZ2U9PGl0a0ltYWdlUHl0aG9uLml0a0ltYWdlVUMzOyBwcm94eSBvZiA8U3dpZyBPYmplY3Qgb2YgdHlwZSAnaXRrSW3igKY=\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"skshrunk = skimage.transform.downscale_local_mean(image_array, shrink_factors[::-1])\n",
"view(skshrunk.astype(image_array.dtype))"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 loop, best of 3: 10.2 s per loop\n"
]
}
],
"source": [
"chunks = (256, 256, 256)\n",
"%timeit da.from_array(image_array, chunks)"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1 loop, best of 3: 3.09 s per loop\n"
]
}
],
"source": [
"da_image = da.from_array(image_array, chunks)\n",
"def dask_shrink(da_image):\n",
" dashrunk = da.coarsen(np.mean,\n",
" da_image,\n",
" {index: factor for index, factor in enumerate(shrink_factors[::-1])},\n",
" trim_excess=True)\n",
" return dashrunk.compute().astype(image_array.dtype)\n",
"%timeit da_shrunk = dask_shrink(da_image)"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"('shrinking with: ', [2, 2, 1])\n"
]
},
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "3a2da51cb85a46528bfe405c7a783505",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"Vmlld2VyKGdyYWRpZW50X29wYWNpdHk9MC4yMiwgcmVuZGVyZWRfaW1hZ2U9PGl0a0ltYWdlUHl0aG9uLml0a0ltYWdlVUMzOyBwcm94eSBvZiA8U3dpZyBPYmplY3Qgb2YgdHlwZSAnaXRrSW3igKY=\n"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"da_shrunk = dask_shrink(da_image)\n",
"view(da_shrunk)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment