From 80fee5355fcc7d9440e5b41aeb84613977de096f Mon Sep 17 00:00:00 2001 From: evamariie Date: Wed, 6 Mar 2024 19:13:44 +0100 Subject: [PATCH 1/2] panoptica changes V1 --- panoptica/README.md | 14 +- .../example_spine_matched_instance.ipynb | 655 +++++++++--------- panoptica/example_spine_semantic.ipynb | 64 +- .../example_spine_unmatched_instance.ipynb | 62 +- 4 files changed, 386 insertions(+), 409 deletions(-) diff --git a/panoptica/README.md b/panoptica/README.md index e63c505..869bb44 100644 --- a/panoptica/README.md +++ b/panoptica/README.md @@ -3,9 +3,11 @@ This folder contains several Jupyter notebooks to showcase different possible use cases of the [panoptica package](https://github.com/BrainLesion/panoptica). The package allows to compute instance-wise segmentation quality metrics for 2D and 3D semantic- and instance segmentation maps by providing 3 core modules: -1. Instance Approximator: instance approximation algorithms in panoptic segmentation evaluation. Available now: connected components algorithm. -1. Instance Matcher: instance matching algorithm in panoptic segmentation evaluation, to align and compare predicted instances with reference instances. -1. Instance Evaluator: Evaluation of panoptic segmentation performance by evaluating matched instance pairs and calculating various metrics like true positives, Dice score, IoU, and ASSD for each instance. +**1. Instance Approximator:** instance approximation algorithms in panoptic segmentation evaluation. Available now: connected components algorithm. + +**2. Instance Matcher:** instance matching algorithm in panoptic segmentation evaluation, to align and compare predicted instances with reference instances. + +**3. Instance Evaluator:** Evaluation of panoptic segmentation performance by evaluating matched instance pairs and calculating various metrics like true positives, Dice score, IoU, and ASSD for each instance. ![workflow_figure](https://github.com/BrainLesion/panoptica/blob/main/examples/figures/workflow.png) @@ -17,7 +19,7 @@ The package allows to compute instance-wise segmentation quality metrics for 2D Although for many biomedical segmentation problems, an instance-wise evaluation is highly relevant and desirable, they are still addressed as semantic segmentation problems due to lack of appropriate instance labels. -Modules [1-3] can be used to obtain panoptic metrics of matched instances based on a semantic segmentation input. +**Modules [1-3]** can be used to obtain panoptic metrics of matched instances based on a semantic segmentation input. [Jupyter Notebook Example](https://github.com/BrainLesion/tutorials/tree/main/panoptica/example_spine_semantic.ipynb) @@ -27,7 +29,7 @@ Modules [1-3] can be used to obtain panoptic metrics of matched instances based It is a common issue that instance segementation outputs have good segmentations with mismatched labels. -For this case modules [2-3] can be utilized to match the instances and report panoptic metrics. +For this case **modules [2-3]** can be utilized to match the instances and report panoptic metrics. [Jupyter Notebook Example](https://github.com/BrainLesion/tutorials/tree/main/panoptica/example_spine_unmatched_instance.ipynb) @@ -37,7 +39,7 @@ For this case modules [2-3] can be utilized to match the instances and report pa Ideally the input data already provides matched instances. -In this case module 3 can be used to directly report panoptic metrics without requiring any internal preprocessing. +In this case **module 3** can be used to directly report panoptic metrics without requiring any internal preprocessing. [Jupyter Notebook Example](https://github.com/BrainLesion/tutorials/tree/main/panoptica/example_spine_matched_instance.ipynb) diff --git a/panoptica/example_spine_matched_instance.ipynb b/panoptica/example_spine_matched_instance.ipynb index e0eb96e..7c22f1c 100644 --- a/panoptica/example_spine_matched_instance.ipynb +++ b/panoptica/example_spine_matched_instance.ipynb @@ -1,362 +1,357 @@ { - "cells": [ - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "# Use Case: Matched Instances Input\n" - ] - }, + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Use Case: Matched Instances Input\n" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Install Dependencies" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": { + "scrolled": true + }, + "outputs": [], + "source": [ + "!pip install -U panoptica > /dev/null #installs numpy and others\n", + "!pip install -U auxiliary > /dev/null" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Setup Imports" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [], + "source": [ + "import numpy as np\n", + "from auxiliary.nifti.io import read_nifti\n", + "from rich import print as pprint\n", + "from panoptica import MatchedInstancePair, Panoptic_Evaluator\n", + "from panoptica.metrics import Metric" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Load Data" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To demonstrate we use a reference and predicition of spine a segmentation with matched instances.\n", + "\n", + "\n", + "![matched_figure](figures/matched_instance.png)" + ] + }, + { + "cell_type": "code", + "execution_count": 3, + "metadata": {}, + "outputs": [ { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Install Dependencies" + "data": { + "text/plain": [ + "(array([ 0, 2, 3, 4, 5, 6, 7, 8, 26, 102, 103, 104, 105,\n", + " 106, 107, 108, 202, 203, 204, 205, 206, 207, 208], dtype=uint8),\n", + " array([ 0, 2, 3, 4, 5, 6, 7, 8, 26, 102, 103, 104, 105,\n", + " 106, 107, 108, 202, 203, 204, 205, 206, 207, 208], dtype=uint8))" ] - }, + }, + "execution_count": 3, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "ref_masks = read_nifti(\"./spine_seg/matched_instance/ref.nii.gz\")\n", + "pred_masks = read_nifti(\"./spine_seg/matched_instance/pred.nii.gz\")\n", + "\n", + "# labels are matching\n", + "np.unique(ref_masks), np.unique(pred_masks)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To use your own data please replace the example data with your own data.\n", + "\n", + "In ordner to successfully load your data please use NIFTI files and the following file designation within the folder \"matched_instance\": \n", + "\n", + "```panoptica/spine_seg/matched_instance/```\n", + "\n", + "- Reference data (\"ref.nii.gz\")\n", + "- Prediction data (\"pred.nii.gz\")" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Run Evaluation" + ] + }, + { + "cell_type": "code", + "execution_count": 4, + "metadata": {}, + "outputs": [ { - "cell_type": "code", - "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Requirement already satisfied: panoptica in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 1)) (0.5.20.post28.dev0+df6e9b3)\n", - "Requirement already satisfied: auxiliary in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 2)) (0.0.40)\n", - "Requirement already satisfied: rich in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 3)) (13.6.0)\n", - "Requirement already satisfied: numpy in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 4)) (1.25.2)\n", - "Requirement already satisfied: connected-components-3d<4.0.0,>=3.12.3 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (3.12.3)\n", - "Requirement already satisfied: scipy<2.0.0,>=1.7.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (1.11.2)\n", - "Requirement already satisfied: nibabel>=3.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (5.1.0)\n", - "Requirement already satisfied: path>=16.7.1 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (16.9.0)\n", - "Requirement already satisfied: pathlib>=1.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (1.0.1)\n", - "Requirement already satisfied: pillow>=10.0.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (10.0.0)\n", - "Requirement already satisfied: tifffile>=2023.8.25 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (2023.8.30)\n", - "Requirement already satisfied: markdown-it-py>=2.2.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from rich->-r requirements.txt (line 3)) (3.0.0)\n", - "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from rich->-r requirements.txt (line 3)) (2.17.2)\n", - "Requirement already satisfied: mdurl~=0.1 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from markdown-it-py>=2.2.0->rich->-r requirements.txt (line 3)) (0.1.2)\n", - "Requirement already satisfied: packaging>=17 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from nibabel>=3.0->auxiliary->-r requirements.txt (line 2)) (23.1)\n" - ] - } + "data": { + "text/html": [ + "
────────────────────────────────────────── Thank you for using panoptica ──────────────────────────────────────────\n",
+       "
\n" ], - "source": [ - "!pip install -r requirements.txt" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Setup Imports" - ] - }, - { - "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [], - "source": [ - "import numpy as np\n", - "from auxiliary.nifti.io import read_nifti\n", - "from rich import print as pprint\n", - "from panoptica import MatchedInstancePair, Panoptic_Evaluator\n", - "from panoptica.metrics import Metric" + "text/plain": [ + "\u001b[92m────────────────────────────────────────── \u001b[0mThank you for using \u001b[1mpanoptica\u001b[0m\u001b[92m ──────────────────────────────────────────\u001b[0m\n" ] + }, + "metadata": {}, + "output_type": "display_data" }, { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Load Data" - ] - }, - { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "To demonstrate we use a reference and predicition of spine a segmentation with matched instances.\n", - "\n", - "\n", - "![matched_figure](figures/matched_instance.png)" + "data": { + "text/html": [ + "
                                     Please support our development by citing                                      \n",
+       "
\n" + ], + "text/plain": [ + " Please support our development by citing \n" ] + }, + "metadata": {}, + "output_type": "display_data" }, { - "cell_type": "code", - "execution_count": 3, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "(array([ 0, 2, 3, 4, 5, 6, 7, 8, 26, 102, 103, 104, 105,\n", - " 106, 107, 108, 202, 203, 204, 205, 206, 207, 208], dtype=uint8),\n", - " array([ 0, 2, 3, 4, 5, 6, 7, 8, 26, 102, 103, 104, 105,\n", - " 106, 107, 108, 202, 203, 204, 205, 206, 207, 208], dtype=uint8))" - ] - }, - "execution_count": 3, - "metadata": {}, - "output_type": "execute_result" - } + "data": { + "text/html": [ + "
                          https://github.com/BrainLesion/panoptica#citation -- Thank you!                          \n",
+       "
\n" ], - "source": [ - "ref_masks = read_nifti(\"./spine_seg/matched_instance/ref.nii.gz\")\n", - "pred_masks = read_nifti(\"./spine_seg/matched_instance/pred.nii.gz\")\n", - "\n", - "# labels are matching\n", - "np.unique(ref_masks), np.unique(pred_masks)" + "text/plain": [ + " \u001b[4;94mhttps://github.com/BrainLesion/panoptica#citation\u001b[0m -- Thank you! \n" ] + }, + "metadata": {}, + "output_type": "display_data" }, { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Run Evaluation" + "data": { + "text/html": [ + "
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────\n",
+       "
\n" + ], + "text/plain": [ + "\u001b[92m───────────────────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n" ] + }, + "metadata": {}, + "output_type": "display_data" }, { - "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
────────────────────────────────────────── Thank you for using panoptica ──────────────────────────────────────────\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[92m────────────────────────────────────────── \u001b[0mThank you for using \u001b[1mpanoptica\u001b[0m\u001b[92m ──────────────────────────────────────────\u001b[0m\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
                                     Please support our development by citing                                      \n",
-              "
\n" - ], - "text/plain": [ - " Please support our development by citing \n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
                          https://github.com/BrainLesion/panoptica#citation -- Thank you!                          \n",
-              "
\n" - ], - "text/plain": [ - " \u001b[4;94mhttps://github.com/BrainLesion/panoptica#citation\u001b[0m -- Thank you! \n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
───────────────────────────────────────────────────────────────────────────────────────────────────────────────────\n",
-              "
\n" - ], - "text/plain": [ - "\u001b[92m───────────────────────────────────────────────────────────────────────────────────────────────────────────────────\u001b[0m\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "data": { - "text/html": [ - "
\n",
-              "
\n" - ], - "text/plain": [ - "\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Panoptic: Start Evaluation\n", - "-- Got MatchedInstancePair, will evaluate instances\n", - "-- evaluate took 2.0301928520202637 seconds to execute.\n" - ] - } + "data": { + "text/html": [ + "
\n",
+       "
\n" ], - "source": [ - "sample = MatchedInstancePair(prediction_arr=pred_masks, reference_arr=ref_masks)\n", - "\n", - "evaluator = Panoptic_Evaluator(\n", - " expected_input=MatchedInstancePair,\n", - " decision_metric=Metric.IOU,\n", - " decision_threshold=0.5,\n", - ")\n", - "\n", - "result, debug_data = evaluator.evaluate(sample)" + "text/plain": [ + "\n" ] + }, + "metadata": {}, + "output_type": "display_data" }, { - "cell_type": "markdown", - "metadata": {}, - "source": [ - "## Inspect Results\n", - "The results object allows access to individual metrics and provides helper methods for further processing" - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "Panoptic: Start Evaluation\n", + "-- Got MatchedInstancePair, will evaluate instances\n", + "-- evaluate took 2.918851613998413 seconds to execute.\n" + ] + } + ], + "source": [ + "sample = MatchedInstancePair(prediction_arr=pred_masks, reference_arr=ref_masks)\n", + "\n", + "evaluator = Panoptic_Evaluator(\n", + " expected_input=MatchedInstancePair,\n", + " decision_metric=Metric.IOU,\n", + " decision_threshold=0.5,\n", + ")\n", + "\n", + "result, debug_data = evaluator.evaluate(sample)" + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Inspect Results\n", + "The results object allows access to individual metrics and provides helper methods for further processing" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ { - "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "\n", - "+++ MATCHING +++\n", - "Number of instances in reference (num_ref_instances): 22\n", - "Number of instances in prediction (num_pred_instances): 22\n", - "True Positives (tp): 22\n", - "False Positives (fp): 0\n", - "False Negatives (fn): 0\n", - "Recognition Quality / F1-Score (rq): 1.0\n", - "\n", - "+++ GLOBAL +++\n", - "Global Binary Dice (global_bin_dsc): 0.9744370224078394\n", - "Global Binary Centerline Dice (global_bin_cldsc): 0.9637064011802574\n", - "\n", - "+++ INSTANCE +++\n", - "Segmentation Quality IoU (sq): 0.8328184295330796 +- 0.15186064004517466\n", - "Panoptic Quality IoU (pq): 0.8328184295330796\n", - "Segmentation Quality Dsc (sq_dsc): 0.900292616009954 +- 0.10253566174957332\n", - "Panoptic Quality Dsc (pq_dsc): 0.900292616009954\n", - "Segmentation Quality Assd (sq_assd): 0.250331887879225 +- 0.07696680402317076\n", - "\n" - ] - } - ], - "source": [ - "# print all results\n", - "print(result)" - ] - }, + "name": "stdout", + "output_type": "stream", + "text": [ + "\n", + "+++ MATCHING +++\n", + "Number of instances in reference (num_ref_instances): 22\n", + "Number of instances in prediction (num_pred_instances): 22\n", + "True Positives (tp): 22\n", + "False Positives (fp): 0\n", + "False Negatives (fn): 0\n", + "Recognition Quality / F1-Score (rq): 1.0\n", + "\n", + "+++ GLOBAL +++\n", + "Global Binary Dice (global_bin_dsc): 0.9744370224078394\n", + "Global Binary Centerline Dice (global_bin_cldsc): 0.9637064011802574\n", + "\n", + "+++ INSTANCE +++\n", + "Segmentation Quality IoU (sq): 0.8328184295330796 +- 0.15186064004517466\n", + "Panoptic Quality IoU (pq): 0.8328184295330796\n", + "Segmentation Quality Dsc (sq_dsc): 0.900292616009954 +- 0.10253566174957332\n", + "Panoptic Quality Dsc (pq_dsc): 0.900292616009954\n", + "Segmentation Quality Assd (sq_assd): 0.250331887879225 +- 0.07696680402317076\n", + "\n" + ] + } + ], + "source": [ + "# print all results\n", + "print(result)" + ] + }, + { + "cell_type": "code", + "execution_count": 6, + "metadata": {}, + "outputs": [ { - "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
result.pq=0.8328184295330796\n",
-              "
\n" - ], - "text/plain": [ - "result.\u001b[33mpq\u001b[0m=\u001b[1;36m0\u001b[0m\u001b[1;36m.8328184295330796\u001b[0m\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - } + "data": { + "text/html": [ + "
result.pq=0.8328184295330796\n",
+       "
\n" ], - "source": [ - "# get specific metric, e.g. pq\n", - "pprint(f\"{result.pq=}\")" + "text/plain": [ + "result.\u001b[33mpq\u001b[0m=\u001b[1;36m0\u001b[0m\u001b[1;36m.8328184295330796\u001b[0m\n" ] - }, + }, + "metadata": {}, + "output_type": "display_data" + } + ], + "source": [ + "# get specific metric, e.g. pq\n", + "pprint(f\"{result.pq=}\")" + ] + }, + { + "cell_type": "code", + "execution_count": 7, + "metadata": {}, + "outputs": [ { - "cell_type": "code", - "execution_count": 7, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "
results dict: \n",
-              "{\n",
-              "    'num_ref_instances': 22,\n",
-              "    'num_pred_instances': 22,\n",
-              "    'tp': 22,\n",
-              "    'fp': 0,\n",
-              "    'fn': 0,\n",
-              "    'rq': 1.0,\n",
-              "    'global_bin_dsc': 0.9744370224078394,\n",
-              "    'global_bin_cldsc': 0.9637064011802574,\n",
-              "    'sq': 0.8328184295330796,\n",
-              "    'sq_std': 0.15186064004517466,\n",
-              "    'pq': 0.8328184295330796,\n",
-              "    'sq_dsc': 0.900292616009954,\n",
-              "    'sq_dsc_std': 0.10253566174957332,\n",
-              "    'pq_dsc': 0.900292616009954,\n",
-              "    'sq_assd': 0.250331887879225,\n",
-              "    'sq_assd_std': 0.07696680402317076\n",
-              "}\n",
-              "
\n" - ], - "text/plain": [ - "results dict: \n", - "\u001b[1m{\u001b[0m\n", - " \u001b[32m'num_ref_instances'\u001b[0m: \u001b[1;36m22\u001b[0m,\n", - " \u001b[32m'num_pred_instances'\u001b[0m: \u001b[1;36m22\u001b[0m,\n", - " \u001b[32m'tp'\u001b[0m: \u001b[1;36m22\u001b[0m,\n", - " \u001b[32m'fp'\u001b[0m: \u001b[1;36m0\u001b[0m,\n", - " \u001b[32m'fn'\u001b[0m: \u001b[1;36m0\u001b[0m,\n", - " \u001b[32m'rq'\u001b[0m: \u001b[1;36m1.0\u001b[0m,\n", - " \u001b[32m'global_bin_dsc'\u001b[0m: \u001b[1;36m0.9744370224078394\u001b[0m,\n", - " \u001b[32m'global_bin_cldsc'\u001b[0m: \u001b[1;36m0.9637064011802574\u001b[0m,\n", - " \u001b[32m'sq'\u001b[0m: \u001b[1;36m0.8328184295330796\u001b[0m,\n", - " \u001b[32m'sq_std'\u001b[0m: \u001b[1;36m0.15186064004517466\u001b[0m,\n", - " \u001b[32m'pq'\u001b[0m: \u001b[1;36m0.8328184295330796\u001b[0m,\n", - " \u001b[32m'sq_dsc'\u001b[0m: \u001b[1;36m0.900292616009954\u001b[0m,\n", - " \u001b[32m'sq_dsc_std'\u001b[0m: \u001b[1;36m0.10253566174957332\u001b[0m,\n", - " \u001b[32m'pq_dsc'\u001b[0m: \u001b[1;36m0.900292616009954\u001b[0m,\n", - " \u001b[32m'sq_assd'\u001b[0m: \u001b[1;36m0.250331887879225\u001b[0m,\n", - " \u001b[32m'sq_assd_std'\u001b[0m: \u001b[1;36m0.07696680402317076\u001b[0m\n", - "\u001b[1m}\u001b[0m\n" - ] - }, - "metadata": {}, - "output_type": "display_data" - } + "data": { + "text/html": [ + "
results dict: \n",
+       "{\n",
+       "    'num_ref_instances': 22,\n",
+       "    'num_pred_instances': 22,\n",
+       "    'tp': 22,\n",
+       "    'fp': 0,\n",
+       "    'fn': 0,\n",
+       "    'rq': 1.0,\n",
+       "    'global_bin_dsc': 0.9744370224078394,\n",
+       "    'global_bin_cldsc': 0.9637064011802574,\n",
+       "    'sq': 0.8328184295330796,\n",
+       "    'sq_std': 0.15186064004517466,\n",
+       "    'pq': 0.8328184295330796,\n",
+       "    'sq_dsc': 0.900292616009954,\n",
+       "    'sq_dsc_std': 0.10253566174957332,\n",
+       "    'pq_dsc': 0.900292616009954,\n",
+       "    'sq_assd': 0.250331887879225,\n",
+       "    'sq_assd_std': 0.07696680402317076\n",
+       "}\n",
+       "
\n" ], - "source": [ - "# get dict for further processing, e.g. for pandas\n", - "pprint(\"results dict: \", result.to_dict())" + "text/plain": [ + "results dict: \n", + "\u001b[1m{\u001b[0m\n", + " \u001b[32m'num_ref_instances'\u001b[0m: \u001b[1;36m22\u001b[0m,\n", + " \u001b[32m'num_pred_instances'\u001b[0m: \u001b[1;36m22\u001b[0m,\n", + " \u001b[32m'tp'\u001b[0m: \u001b[1;36m22\u001b[0m,\n", + " \u001b[32m'fp'\u001b[0m: \u001b[1;36m0\u001b[0m,\n", + " \u001b[32m'fn'\u001b[0m: \u001b[1;36m0\u001b[0m,\n", + " \u001b[32m'rq'\u001b[0m: \u001b[1;36m1.0\u001b[0m,\n", + " \u001b[32m'global_bin_dsc'\u001b[0m: \u001b[1;36m0.9744370224078394\u001b[0m,\n", + " \u001b[32m'global_bin_cldsc'\u001b[0m: \u001b[1;36m0.9637064011802574\u001b[0m,\n", + " \u001b[32m'sq'\u001b[0m: \u001b[1;36m0.8328184295330796\u001b[0m,\n", + " \u001b[32m'sq_std'\u001b[0m: \u001b[1;36m0.15186064004517466\u001b[0m,\n", + " \u001b[32m'pq'\u001b[0m: \u001b[1;36m0.8328184295330796\u001b[0m,\n", + " \u001b[32m'sq_dsc'\u001b[0m: \u001b[1;36m0.900292616009954\u001b[0m,\n", + " \u001b[32m'sq_dsc_std'\u001b[0m: \u001b[1;36m0.10253566174957332\u001b[0m,\n", + " \u001b[32m'pq_dsc'\u001b[0m: \u001b[1;36m0.900292616009954\u001b[0m,\n", + " \u001b[32m'sq_assd'\u001b[0m: \u001b[1;36m0.250331887879225\u001b[0m,\n", + " \u001b[32m'sq_assd_std'\u001b[0m: \u001b[1;36m0.07696680402317076\u001b[0m\n", + "\u001b[1m}\u001b[0m\n" ] + }, + "metadata": {}, + "output_type": "display_data" } - ], - "metadata": { - "kernelspec": { - "display_name": "helm", - "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.11.6" - } + ], + "source": [ + "# get dict for further processing, e.g. for pandas\n", + "pprint(\"results dict: \", result.to_dict())" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3 (ipykernel)", + "language": "python", + "name": "python3" }, - "nbformat": 4, - "nbformat_minor": 2 + "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.10.12" + } + }, + "nbformat": 4, + "nbformat_minor": 2 } diff --git a/panoptica/example_spine_semantic.ipynb b/panoptica/example_spine_semantic.ipynb index adf36e3..b058d12 100644 --- a/panoptica/example_spine_semantic.ipynb +++ b/panoptica/example_spine_semantic.ipynb @@ -17,32 +17,13 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Requirement already satisfied: panoptica in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 1)) (0.5.20.post28.dev0+df6e9b3)\n", - "Requirement already satisfied: auxiliary in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 2)) (0.0.40)\n", - "Requirement already satisfied: rich in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 3)) (13.6.0)\n", - "Requirement already satisfied: numpy in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 4)) (1.25.2)\n", - "Requirement already satisfied: connected-components-3d<4.0.0,>=3.12.3 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (3.12.3)\n", - "Requirement already satisfied: scipy<2.0.0,>=1.7.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (1.11.2)\n", - "Requirement already satisfied: nibabel>=3.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (5.1.0)\n", - "Requirement already satisfied: path>=16.7.1 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (16.9.0)\n", - "Requirement already satisfied: pathlib>=1.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (1.0.1)\n", - "Requirement already satisfied: pillow>=10.0.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (10.0.0)\n", - "Requirement already satisfied: tifffile>=2023.8.25 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (2023.8.30)\n", - "Requirement already satisfied: markdown-it-py>=2.2.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from rich->-r requirements.txt (line 3)) (3.0.0)\n", - "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from rich->-r requirements.txt (line 3)) (2.17.2)\n", - "Requirement already satisfied: mdurl~=0.1 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from markdown-it-py>=2.2.0->rich->-r requirements.txt (line 3)) (0.1.2)\n", - "Requirement already satisfied: packaging>=17 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from nibabel>=3.0->auxiliary->-r requirements.txt (line 2)) (23.1)\n" - ] - } - ], + "metadata": { + "scrolled": true + }, + "outputs": [], "source": [ - "!pip install -r requirements.txt" + "!pip install -U panoptica > /dev/null #installs numpy and others\n", + "!pip install -U auxiliary > /dev/null" ] }, { @@ -95,6 +76,20 @@ "pred_masks = read_nifti(\"./spine_seg/semantic/pred.nii.gz\")" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To use your own data please replace the example data with your own data.\n", + "\n", + "In ordner to successfully load your data please use NIFTI files and the following file designation within the \"semantic\" folder: \n", + "\n", + "```panoptica/spine_seg/semantic/```\n", + "\n", + "- Reference data (\"ref.nii.gz\")\n", + "- Prediction data (\"pred.nii.gz\")\n" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -180,7 +175,7 @@ "-- Got SemanticPair, will approximate instances\n", "-- Got UnmatchedInstancePair, will match instances\n", "-- Got MatchedInstancePair, will evaluate instances\n", - "-- evaluate took 4.104548931121826 seconds to execute.\n" + "-- evaluate took 8.176098346710205 seconds to execute.\n" ] } ], @@ -201,7 +196,7 @@ "metadata": {}, "source": [ "## Inspect Results\n", - "The results object allows access to individual metrics and provides helper methods for further processing" + "The results object allows access to individual metrics and provides helper methods for further processing\n" ] }, { @@ -268,7 +263,9 @@ { "cell_type": "code", "execution_count": 7, - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [ { "data": { @@ -324,18 +321,11 @@ "# get dict for further processing, e.g. for pandas\n", "pprint(\"results dict: \", result.to_dict())" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "brainles", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -349,7 +339,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.6" + "version": "3.10.12" } }, "nbformat": 4, diff --git a/panoptica/example_spine_unmatched_instance.ipynb b/panoptica/example_spine_unmatched_instance.ipynb index 3ade848..2199d84 100644 --- a/panoptica/example_spine_unmatched_instance.ipynb +++ b/panoptica/example_spine_unmatched_instance.ipynb @@ -17,32 +17,13 @@ { "cell_type": "code", "execution_count": 1, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Requirement already satisfied: panoptica in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 1)) (0.5.20.post28.dev0+df6e9b3)\n", - "Requirement already satisfied: auxiliary in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 2)) (0.0.40)\n", - "Requirement already satisfied: rich in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 3)) (13.6.0)\n", - "Requirement already satisfied: numpy in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 4)) (1.25.2)\n", - "Requirement already satisfied: connected-components-3d<4.0.0,>=3.12.3 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (3.12.3)\n", - "Requirement already satisfied: scipy<2.0.0,>=1.7.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (1.11.2)\n", - "Requirement already satisfied: nibabel>=3.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (5.1.0)\n", - "Requirement already satisfied: path>=16.7.1 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (16.9.0)\n", - "Requirement already satisfied: pathlib>=1.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (1.0.1)\n", - "Requirement already satisfied: pillow>=10.0.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (10.0.0)\n", - "Requirement already satisfied: tifffile>=2023.8.25 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (2023.8.30)\n", - "Requirement already satisfied: markdown-it-py>=2.2.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from rich->-r requirements.txt (line 3)) (3.0.0)\n", - "Requirement already satisfied: pygments<3.0.0,>=2.13.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from rich->-r requirements.txt (line 3)) (2.17.2)\n", - "Requirement already satisfied: mdurl~=0.1 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from markdown-it-py>=2.2.0->rich->-r requirements.txt (line 3)) (0.1.2)\n", - "Requirement already satisfied: packaging>=17 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from nibabel>=3.0->auxiliary->-r requirements.txt (line 2)) (23.1)\n" - ] - } - ], + "metadata": { + "scrolled": true + }, + "outputs": [], "source": [ - "!pip install -r requirements.txt" + "!pip install -U panoptica > /dev/null #installs numpy and others\n", + "!pip install -U auxiliary > /dev/null" ] }, { @@ -84,7 +65,9 @@ { "cell_type": "code", "execution_count": 3, - "metadata": {}, + "metadata": { + "scrolled": true + }, "outputs": [ { "data": { @@ -108,6 +91,20 @@ "np.unique(ref_masks), np.unique(pred_masks)" ] }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "To use your own data please replace the example data with your own data.\n", + "\n", + "In ordner to successfully load your data please use NIFTI files and the following file designation within the folder \"unmatched_instance\": \n", + "\n", + "```panoptica/spine_seg/unmatched_instance/```\n", + "\n", + "- Reference data (\"ref.nii.gz\")\n", + "- Prediction data (\"pred.nii.gz\")" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -192,7 +189,7 @@ "Panoptic: Start Evaluation\n", "-- Got UnmatchedInstancePair, will match instances\n", "-- Got MatchedInstancePair, will evaluate instances\n", - "-- evaluate took 4.241225719451904 seconds to execute.\n" + "-- evaluate took 3.694295644760132 seconds to execute.\n" ] } ], @@ -334,18 +331,11 @@ "# get dict for further processing, e.g. for pandas\n", "pprint(\"results dict: \", result.to_dict())" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { "kernelspec": { - "display_name": "brainles", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -359,7 +349,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.6" + "version": "3.10.12" } }, "nbformat": 4, From afbcc98833a7bbaa06ab9b5d0ac384861f8d6257 Mon Sep 17 00:00:00 2001 From: evamariie Date: Sun, 10 Mar 2024 18:23:54 +0100 Subject: [PATCH 2/2] applies black (but should have been fine before too) --- panoptica/example_spine_matched_instance.ipynb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/panoptica/example_spine_matched_instance.ipynb b/panoptica/example_spine_matched_instance.ipynb index 7c22f1c..fbccca9 100644 --- a/panoptica/example_spine_matched_instance.ipynb +++ b/panoptica/example_spine_matched_instance.ipynb @@ -349,7 +349,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.10.12" + "version": "3.10.2" } }, "nbformat": 4,