{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Basic statistics with pyclesperanto\n", "A common use case for image processing in the biology context is deriving statistics of segmented objects. clEsperanto offers a function for that: [statistics_of_labelled_pixels](https://clij.github.io/clij2-docs/reference_statisticsOfLabelledPixels)." ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "" ] }, "execution_count": 1, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import pyclesperanto_prototype as cle\n", "\n", "from skimage.io import imread, imsave, imshow\n", "import matplotlib\n", "import numpy as np\n", "\n", "# initialize GPU\n", "cle.select_device(\"RTX\")" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "
\n", "\n", "\n", "cle._ image
\n", "\n", "\n", "\n", "\n", "\n", "
shape(254, 256)
dtypeuint32
size254.0 kB
min0.0
max65.0
\n", "\n", "
" ], "text/plain": [ "cl.OCLArray([[ 0, 0, 0, ..., 62, 62, 62],\n", " [ 0, 0, 0, ..., 62, 62, 62],\n", " [ 0, 0, 0, ..., 62, 62, 62],\n", " ...,\n", " [ 0, 0, 0, ..., 0, 0, 0],\n", " [ 0, 0, 0, ..., 0, 0, 0],\n", " [ 0, 0, 0, ..., 0, 0, 0]], dtype=uint32)" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# load data\n", "image = imread('https://samples.fiji.sc/blobs.png')\n", "\n", "# segment the image\n", "labels = cle.voronoi_otsu_labeling(image, spot_sigma=3.5)\n", "labels" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Deriving basic statistics of labelled objects" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['label', 'original_label', 'bbox_min_x', 'bbox_min_y', 'bbox_min_z', 'bbox_max_x', 'bbox_max_y', 'bbox_max_z', 'bbox_width', 'bbox_height', 'bbox_depth', 'min_intensity', 'max_intensity', 'sum_intensity', 'area', 'mean_intensity', 'sum_intensity_times_x', 'mass_center_x', 'sum_intensity_times_y', 'mass_center_y', 'sum_intensity_times_z', 'mass_center_z', 'sum_x', 'centroid_x', 'sum_y', 'centroid_y', 'sum_z', 'centroid_z', 'sum_distance_to_centroid', 'mean_distance_to_centroid', 'sum_distance_to_mass_center', 'mean_distance_to_mass_center', 'standard_deviation_intensity', 'max_distance_to_centroid', 'max_distance_to_mass_center', 'mean_max_distance_to_centroid_ratio', 'mean_max_distance_to_mass_center_ratio'])" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "statistics = cle.statistics_of_labelled_pixels(image, labels)\n", "statistics.keys()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We can use [pandas](https://pandas.pydata.org/) to process that kind of tabular data. " ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [], "source": [ "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
labeloriginal_labelbbox_min_xbbox_min_ybbox_min_zbbox_max_xbbox_max_ybbox_max_zbbox_widthbbox_height...centroid_zsum_distance_to_centroidmean_distance_to_centroidsum_distance_to_mass_centermean_distance_to_mass_centerstandard_deviation_intensitymax_distance_to_centroidmax_distance_to_mass_centermean_max_distance_to_centroid_ratiomean_max_distance_to_mass_center_ratio
0110.021.00.01.031.00.02.011.0...0.047.9220732.52221447.9299772.5226307.9220585.2275125.2430062.0725882.078388
1220.0214.00.010.0238.00.011.025.0...0.01266.5080576.1780881268.9421396.18996133.20111112.71712312.5947902.0584242.034712
2330.082.00.018.0113.00.019.032.0...0.04528.0844738.9842954535.9794928.99996028.55646716.48918916.5125061.8353351.834731
3440.0130.00.015.0149.00.016.020.0...0.01631.8115236.1811041631.8238536.18115126.89263710.29273110.2714441.6651931.661736
4554.040.00.012.050.00.09.011.0...0.0262.2212523.361811262.2981263.36279716.0498895.4441765.4932241.6194181.633529
..................................................................
606161235.063.00.0255.083.00.021.021.0...0.02511.7014167.1153012511.7932137.11556127.74156211.91286411.9585121.6742601.680614
616262237.00.00.0255.028.00.019.029.0...0.04084.9379888.5818034091.1467298.59484729.70402015.77552915.2647101.8382531.776031
626363245.095.00.0255.0114.00.011.020.0...0.0951.9660645.408898953.4296265.41721428.63275511.00404410.8338992.0344331.999902
636464233.0137.00.0255.0166.00.023.030.0...0.04177.5727548.7764144186.1440438.79442032.46022818.67124718.1356792.1274352.062180
646565251.0193.00.0255.0208.00.05.016.0...0.0240.3777473.755902240.7496033.76171332.0770537.7789917.7596002.0711382.062784
\n", "

65 rows × 37 columns

\n", "
" ], "text/plain": [ " label original_label bbox_min_x bbox_min_y bbox_min_z bbox_max_x \\\n", "0 1 1 0.0 21.0 0.0 1.0 \n", "1 2 2 0.0 214.0 0.0 10.0 \n", "2 3 3 0.0 82.0 0.0 18.0 \n", "3 4 4 0.0 130.0 0.0 15.0 \n", "4 5 5 4.0 40.0 0.0 12.0 \n", ".. ... ... ... ... ... ... \n", "60 61 61 235.0 63.0 0.0 255.0 \n", "61 62 62 237.0 0.0 0.0 255.0 \n", "62 63 63 245.0 95.0 0.0 255.0 \n", "63 64 64 233.0 137.0 0.0 255.0 \n", "64 65 65 251.0 193.0 0.0 255.0 \n", "\n", " bbox_max_y bbox_max_z bbox_width bbox_height ... centroid_z \\\n", "0 31.0 0.0 2.0 11.0 ... 0.0 \n", "1 238.0 0.0 11.0 25.0 ... 0.0 \n", "2 113.0 0.0 19.0 32.0 ... 0.0 \n", "3 149.0 0.0 16.0 20.0 ... 0.0 \n", "4 50.0 0.0 9.0 11.0 ... 0.0 \n", ".. ... ... ... ... ... ... \n", "60 83.0 0.0 21.0 21.0 ... 0.0 \n", "61 28.0 0.0 19.0 29.0 ... 0.0 \n", "62 114.0 0.0 11.0 20.0 ... 0.0 \n", "63 166.0 0.0 23.0 30.0 ... 0.0 \n", "64 208.0 0.0 5.0 16.0 ... 0.0 \n", "\n", " sum_distance_to_centroid mean_distance_to_centroid \\\n", "0 47.922073 2.522214 \n", "1 1266.508057 6.178088 \n", "2 4528.084473 8.984295 \n", "3 1631.811523 6.181104 \n", "4 262.221252 3.361811 \n", ".. ... ... \n", "60 2511.701416 7.115301 \n", "61 4084.937988 8.581803 \n", "62 951.966064 5.408898 \n", "63 4177.572754 8.776414 \n", "64 240.377747 3.755902 \n", "\n", " sum_distance_to_mass_center mean_distance_to_mass_center \\\n", "0 47.929977 2.522630 \n", "1 1268.942139 6.189961 \n", "2 4535.979492 8.999960 \n", "3 1631.823853 6.181151 \n", "4 262.298126 3.362797 \n", ".. ... ... \n", "60 2511.793213 7.115561 \n", "61 4091.146729 8.594847 \n", "62 953.429626 5.417214 \n", "63 4186.144043 8.794420 \n", "64 240.749603 3.761713 \n", "\n", " standard_deviation_intensity max_distance_to_centroid \\\n", "0 7.922058 5.227512 \n", "1 33.201111 12.717123 \n", "2 28.556467 16.489189 \n", "3 26.892637 10.292731 \n", "4 16.049889 5.444176 \n", ".. ... ... \n", "60 27.741562 11.912864 \n", "61 29.704020 15.775529 \n", "62 28.632755 11.004044 \n", "63 32.460228 18.671247 \n", "64 32.077053 7.778991 \n", "\n", " max_distance_to_mass_center mean_max_distance_to_centroid_ratio \\\n", "0 5.243006 2.072588 \n", "1 12.594790 2.058424 \n", "2 16.512506 1.835335 \n", "3 10.271444 1.665193 \n", "4 5.493224 1.619418 \n", ".. ... ... \n", "60 11.958512 1.674260 \n", "61 15.264710 1.838253 \n", "62 10.833899 2.034433 \n", "63 18.135679 2.127435 \n", "64 7.759600 2.071138 \n", "\n", " mean_max_distance_to_mass_center_ratio \n", "0 2.078388 \n", "1 2.034712 \n", "2 1.834731 \n", "3 1.661736 \n", "4 1.633529 \n", ".. ... \n", "60 1.680614 \n", "61 1.776031 \n", "62 1.999902 \n", "63 2.062180 \n", "64 2.062784 \n", "\n", "[65 rows x 37 columns]" ] }, "execution_count": 5, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table = pd.DataFrame(statistics)\n", "table" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
labeloriginal_labelbbox_min_xbbox_min_ybbox_min_zbbox_max_xbbox_max_ybbox_max_zbbox_widthbbox_height...centroid_zsum_distance_to_centroidmean_distance_to_centroidsum_distance_to_mass_centermean_distance_to_mass_centerstandard_deviation_intensitymax_distance_to_centroidmax_distance_to_mass_centermean_max_distance_to_centroid_ratiomean_max_distance_to_mass_center_ratio
count65.0000065.0000065.00000065.00000065.065.00000065.00000065.065.00000065.000000...65.065.00000065.00000065.00000065.00000065.00000065.00000065.00000065.00000065.000000
mean33.0000033.00000119.769234109.0307690.0138.523071129.1384580.019.75384521.107693...0.02702.9187016.8625332703.7014166.86500528.89729312.06192612.0681901.7678301.767646
std18.9076718.9076778.59245377.5890580.079.09272877.8688580.06.5646567.858362...0.02297.4902342.0930092297.7114262.0929895.8401573.7881923.8119440.1797820.175480
min1.000001.000000.0000000.0000000.01.0000008.0000000.02.0000004.000000...0.047.9220732.52221447.9299772.5226307.9220584.7766954.7979671.5487671.555610
25%17.0000017.0000053.00000042.0000000.073.00000061.0000000.016.00000018.000000...0.01056.1722415.4986121056.1917725.50282626.3772569.4469079.4673421.6272721.635578
50%33.0000033.00000120.000000108.0000000.0143.000000130.0000000.020.00000021.000000...0.01781.9111336.7367421781.9422616.73864029.16578311.83521911.8086531.7021641.711959
75%49.0000049.00000189.000000166.0000000.0204.000000195.0000000.023.00000026.000000...0.04038.7448738.5114474039.0966808.50659832.46022814.07657314.0516371.8382531.834731
max65.0000065.00000251.000000250.0000000.0255.000000253.0000000.040.00000050.000000...0.012003.94921913.35255712001.02734413.34930740.01785326.36005026.9605222.2313422.207110
\n", "

8 rows × 37 columns

\n", "
" ], "text/plain": [ " label original_label bbox_min_x bbox_min_y bbox_min_z \\\n", "count 65.00000 65.00000 65.000000 65.000000 65.0 \n", "mean 33.00000 33.00000 119.769234 109.030769 0.0 \n", "std 18.90767 18.90767 78.592453 77.589058 0.0 \n", "min 1.00000 1.00000 0.000000 0.000000 0.0 \n", "25% 17.00000 17.00000 53.000000 42.000000 0.0 \n", "50% 33.00000 33.00000 120.000000 108.000000 0.0 \n", "75% 49.00000 49.00000 189.000000 166.000000 0.0 \n", "max 65.00000 65.00000 251.000000 250.000000 0.0 \n", "\n", " bbox_max_x bbox_max_y bbox_max_z bbox_width bbox_height ... \\\n", "count 65.000000 65.000000 65.0 65.000000 65.000000 ... \n", "mean 138.523071 129.138458 0.0 19.753845 21.107693 ... \n", "std 79.092728 77.868858 0.0 6.564656 7.858362 ... \n", "min 1.000000 8.000000 0.0 2.000000 4.000000 ... \n", "25% 73.000000 61.000000 0.0 16.000000 18.000000 ... \n", "50% 143.000000 130.000000 0.0 20.000000 21.000000 ... \n", "75% 204.000000 195.000000 0.0 23.000000 26.000000 ... \n", "max 255.000000 253.000000 0.0 40.000000 50.000000 ... \n", "\n", " centroid_z sum_distance_to_centroid mean_distance_to_centroid \\\n", "count 65.0 65.000000 65.000000 \n", "mean 0.0 2702.918701 6.862533 \n", "std 0.0 2297.490234 2.093009 \n", "min 0.0 47.922073 2.522214 \n", "25% 0.0 1056.172241 5.498612 \n", "50% 0.0 1781.911133 6.736742 \n", "75% 0.0 4038.744873 8.511447 \n", "max 0.0 12003.949219 13.352557 \n", "\n", " sum_distance_to_mass_center mean_distance_to_mass_center \\\n", "count 65.000000 65.000000 \n", "mean 2703.701416 6.865005 \n", "std 2297.711426 2.092989 \n", "min 47.929977 2.522630 \n", "25% 1056.191772 5.502826 \n", "50% 1781.942261 6.738640 \n", "75% 4039.096680 8.506598 \n", "max 12001.027344 13.349307 \n", "\n", " standard_deviation_intensity max_distance_to_centroid \\\n", "count 65.000000 65.000000 \n", "mean 28.897293 12.061926 \n", "std 5.840157 3.788192 \n", "min 7.922058 4.776695 \n", "25% 26.377256 9.446907 \n", "50% 29.165783 11.835219 \n", "75% 32.460228 14.076573 \n", "max 40.017853 26.360050 \n", "\n", " max_distance_to_mass_center mean_max_distance_to_centroid_ratio \\\n", "count 65.000000 65.000000 \n", "mean 12.068190 1.767830 \n", "std 3.811944 0.179782 \n", "min 4.797967 1.548767 \n", "25% 9.467342 1.627272 \n", "50% 11.808653 1.702164 \n", "75% 14.051637 1.838253 \n", "max 26.960522 2.231342 \n", "\n", " mean_max_distance_to_mass_center_ratio \n", "count 65.000000 \n", "mean 1.767646 \n", "std 0.175480 \n", "min 1.555610 \n", "25% 1.635578 \n", "50% 1.711959 \n", "75% 1.834731 \n", "max 2.207110 \n", "\n", "[8 rows x 37 columns]" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "table.describe()" ] }, { "cell_type": "code", "execution_count": null, "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.13" } }, "nbformat": 4, "nbformat_minor": 4 }