{ "cells": [ { "cell_type": "markdown", "id": "3642dcb1-3fab-44b3-b9f5-e654523cdba6", "metadata": {}, "source": [ "# Static views\n", "In this notebook we demonstrate how to create static views of images with additional information such as shape, data type and size of the pixel array, minimum and maximum intensity and histograms." ] }, { "cell_type": "code", "execution_count": 1, "id": "be054a75-be25-427e-aeb0-be80ed203675", "metadata": {}, "outputs": [], "source": [ "from stackview import insight, jupyter_displayable_output\n", "from skimage.io import imread, imshow\n", "from skimage.filters import gaussian\n", "from skimage.measure import label" ] }, { "cell_type": "code", "execution_count": 2, "id": "16f7302b-9592-49b2-842e-08707222a1d6", "metadata": {}, "outputs": [], "source": [ "image = imread('data/Haase_MRT_tfl3d1.tif')" ] }, { "cell_type": "markdown", "id": "421dd7ac-58a7-4ce6-a45d-1ac21447dac9", "metadata": {}, "source": [ "The `insight` function turns a numpy-array into a numpy-compatible array that has an image-display in jupyter notebooks." ] }, { "cell_type": "code", "execution_count": 3, "id": "45c0dca5-0e23-4e34-840a-11d1c89fceec", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
shape(160, 160)
dtypeuint16
size50.0 kB
min0
max65535
\n", "\n", "
" ], "text/plain": [ "StackViewNDArray([[ 0, 0, 0, ..., 0, 0, 0],\n", " [ 0, 381, 1450, ..., 1373, 992, 687],\n", " [ 0, 381, 1755, ..., 1297, 305, 916],\n", " ...,\n", " [ 0, 0, 0, ..., 610, 687, 1984],\n", " [ 0, 0, 0, ..., 839, 458, 1526],\n", " [ 0, 0, 0, ..., 839, 916, 992]], dtype=uint16)" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "insight(image[60])" ] }, { "cell_type": "code", "execution_count": 4, "id": "e60c8fee-b432-46fc-903f-8b551911a9b4", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
shape(254, 256)
dtypeint32
size254.0 kB
min0
max64
\n", "\n", "
" ], "text/plain": [ "StackViewNDArray([[0, 0, 0, ..., 5, 5, 5],\n", " [0, 0, 0, ..., 5, 5, 5],\n", " [0, 0, 0, ..., 5, 5, 5],\n", " ...,\n", " [0, 0, 0, ..., 0, 0, 0],\n", " [0, 0, 0, ..., 0, 0, 0],\n", " [0, 0, 0, ..., 0, 0, 0]])" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "blobs = imread('data/blobs.tif')\n", "labels = label(blobs > 120)\n", "\n", "insight(labels)" ] }, { "cell_type": "markdown", "id": "3dba1473-5920-4e9e-b5a7-26225932d92d", "metadata": {}, "source": [ "The function `insight` returns a variable that can be post-processed using common libraries, it aims to be just a numpy-array with some extras." ] }, { "cell_type": "code", "execution_count": 5, "id": "98d442c1-4400-4c85-97a6-df1ee4f1f2bf", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "stackview._static_view.StackViewNDArray" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sv_image = insight(image[60])\n", "type(sv_image)" ] }, { "cell_type": "code", "execution_count": 6, "id": "3d8d0e6d-660e-49fc-82d4-5328964543b6", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
shape(160, 160)
dtypeuint16
size50.0 kB
min0
max65535
\n", "\n", "
" ], "text/plain": [ "StackViewNDArray([[ 0, 0, 0, ..., 0, 0, 0],\n", " [ 0, 381, 1450, ..., 1373, 992, 687],\n", " [ 0, 381, 1755, ..., 1297, 305, 916],\n", " ...,\n", " [ 0, 0, 0, ..., 610, 687, 1984],\n", " [ 0, 0, 0, ..., 839, 458, 1526],\n", " [ 0, 0, 0, ..., 839, 916, 992]], dtype=uint16)" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "sv_image" ] }, { "cell_type": "markdown", "id": "983290f0-d018-4356-997d-4fc6da0dd438", "metadata": {}, "source": [ "After processing the `insight` generated image, e.g. using the `gaussian` function from scikit-image, the result must be converted again to have the display." ] }, { "cell_type": "code", "execution_count": 7, "id": "00eb5b0a-32d4-4714-8e72-32c6c0b4727d", "metadata": {}, "outputs": [ { "data": { "text/plain": [ "numpy.ndarray" ] }, "execution_count": 7, "metadata": {}, "output_type": "execute_result" } ], "source": [ "result = gaussian(sv_image, sigma=6)\n", "type(result)" ] }, { "cell_type": "code", "execution_count": 8, "id": "417cb291-0d93-4ad1-81d2-b10f2ed99b86", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
shape(160, 160)
dtypefloat64
size200.0 kB
min0.0032266908735059745
max0.5743035671972324
\n", "\n", "
" ], "text/plain": [ "StackViewNDArray([[0.00370651, 0.00426108, 0.00481234, ..., 0.00710816,\n", " 0.00697263, 0.00685315],\n", " [0.0042273 , 0.00485844, 0.00548581, ..., 0.00816649,\n", " 0.00801763, 0.00788711],\n", " [0.00473856, 0.00544452, 0.00614624, ..., 0.00923175,\n", " 0.00907213, 0.00893312],\n", " ...,\n", " [0.00463481, 0.00552068, 0.00645748, ..., 0.01694976,\n", " 0.01634085, 0.01584955],\n", " [0.00390661, 0.00465442, 0.00544576, ..., 0.01716718,\n", " 0.01655641, 0.01606671],\n", " [0.00322669, 0.0038455 , 0.00450086, ..., 0.01736883,\n", " 0.01675237, 0.01626037]])" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "insight(result)" ] }, { "cell_type": "markdown", "id": "bc02abc0-f11a-49e8-97ef-28c54150deea", "metadata": {}, "source": [ "## Custom functions\n", "When implementing custom image processing functions, those can be annotated so that the result becomes an `insight` image automatically." ] }, { "cell_type": "code", "execution_count": 9, "id": "1a7d2c86-df68-4b36-ac06-deffd0aa2fae", "metadata": {}, "outputs": [], "source": [ "@jupyter_displayable_output\n", "def my_gaussian(image, sigma):\n", " return gaussian(image, sigma)" ] }, { "cell_type": "code", "execution_count": 10, "id": "2bccfc33-62de-443f-8a10-f9281145500e", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "
shape(160, 160)
dtypefloat64
size200.0 kB
min0.00010141543975253962
max0.8699153742799944
\n", "\n", "
" ], "text/plain": [ "StackViewNDArray([[0.00212939, 0.00344053, 0.00476471, ..., 0.00583083,\n", " 0.00560638, 0.00537486],\n", " [0.00327628, 0.00530667, 0.00735382, ..., 0.00866562,\n", " 0.00837849, 0.00809373],\n", " [0.00437608, 0.00708168, 0.00979352, ..., 0.0110469 ,\n", " 0.0107322 , 0.01043783],\n", " ...,\n", " [0.00063916, 0.0014103 , 0.00256736, ..., 0.01399779,\n", " 0.01456949, 0.01554148],\n", " [0.00028084, 0.00062272, 0.00114119, ..., 0.01403081,\n", " 0.01471743, 0.01585427],\n", " [0.00010142, 0.00022578, 0.00041603, ..., 0.01400911,\n", " 0.01458054, 0.01561444]])" ] }, "execution_count": 10, "metadata": {}, "output_type": "execute_result" } ], "source": [ "my_gaussian(image[60], 2)" ] }, { "cell_type": "markdown", "id": "6f778f54-6527-4e95-bfde-7318d8872ea3", "metadata": {}, "source": [ "It is recommended to add a library name of your library and a link to the documentation so that users can click the link right in the `insight` display." ] }, { "cell_type": "code", "execution_count": 11, "id": "243e794b-94f2-4662-a8b3-6ec0173edd12", "metadata": {}, "outputs": [], "source": [ "@jupyter_displayable_output(library_name=\"clesperanto\", help_url=\"https://github.com/clesperanto\")\n", "def cle_gaussian(image, sigma):\n", " import pyclesperanto_prototype as cle\n", " return cle.gaussian_blur(image, sigma_x=sigma, sigma_y=sigma)" ] }, { "cell_type": "code", "execution_count": 12, "id": "ed8a30c7-3d70-4c43-a8f7-aa607f9857d3", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "clesperanto made image
\n", "\n", "\n", "\n", "\n", "\n", "
shape(160, 160)
dtypefloat32
size100.0 kB
min0.000925589
max62310.082
\n", "\n", "
" ], "text/plain": [ "StackViewNDArray([[5.27978058e+01, 1.64570343e+02, 2.61079163e+02, ...,\n", " 3.16405029e+02, 2.84286743e+02, 2.40542450e+02],\n", " [1.24606766e+02, 3.96521637e+02, 6.54013428e+02, ...,\n", " 7.18867004e+02, 6.43725464e+02, 5.69837952e+02],\n", " [1.65524872e+02, 5.44341736e+02, 9.49922180e+02, ...,\n", " 9.70090820e+02, 8.74096619e+02, 8.09579529e+02],\n", " ...,\n", " [4.06006604e-01, 5.16509247e+00, 2.62547874e+01, ...,\n", " 7.71678833e+02, 9.35180176e+02, 1.20644006e+03],\n", " [3.15837264e-02, 4.01614636e-01, 2.04012704e+00, ...,\n", " 7.70590942e+02, 9.28915710e+02, 1.20594690e+03],\n", " [9.25589004e-04, 1.17663806e-02, 5.97464181e-02, ...,\n", " 7.89296997e+02, 9.06250427e+02, 1.05993542e+03]],\n", " dtype=float32)" ] }, "execution_count": 12, "metadata": {}, "output_type": "execute_result" } ], "source": [ "cle_gaussian(image[60], 1)" ] }, { "cell_type": "code", "execution_count": null, "id": "c74ba588-8a45-41a8-909f-da76c513e68f", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.9.15" }, "widgets": { "application/vnd.jupyter.widget-state+json": { "state": { "de8527c08e3543b8bd4c9747eb7500c8": { "model_module": "ipycanvas", "model_module_version": "^0.13", "model_name": "CanvasManagerModel", "state": { "_model_module_version": "^0.13", "_view_module": null, "_view_module_version": "" } } }, "version_major": 2, "version_minor": 0 } } }, "nbformat": 4, "nbformat_minor": 5 }