{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Use Case: Matched Instances Input\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Install Dependencies" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [], "source": [ "!pip install panoptica auxiliary rich numpy > /dev/null" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If you installed the packages and requirements on your own machine, you can skip this section and start from the import section.\n", "\n", "### Setup Colab environment (optional) \n", "Otherwise you can follow and execute the tutorial on your browser.\n", "In order to start working on the notebook, click on the following button, this will open this page in the Colab environment and you will be able to execute the code on your own (*Google account required*).\n", "\n", "\n", " \"Open\n", "\n", "\n", "Now that you are visualizing the notebook in Colab, run the next cell to install the packages we will use. There are few things you should follow in order to properly set the notebook up:\n", "1. Warning: This notebook was not authored by Google. Click on 'Run anyway'.\n", "1. When the installation commands are done, there might be \"Restart runtime\" button at the end of the output. Please, click it.\n", "If you run the next cell in a Google Colab environment, it will **clone the 'tutorials' repository** in your google drive. This will create a **new folder** called \"tutorials\" in **your Google Drive**.\n", "All generated file will be created/uploaded to your Google Drive respectively.\n", "\n", "After the first execution of the next cell, you might receive some warnings and notifications, please follow these instructions:\n", " - 'Permit this notebook to access your Google Drive files?' Click on 'Yes', and select your account.\n", " - Google Drive for desktop wants to access your Google Account. Click on 'Allow'.\n", "\n", "Afterwards the \"tutorials\" folder has been created. You can navigate it through the lefthand panel in Colab. You might also have received an email that informs you about the access on your Google Drive." ] }, { "cell_type": "code", "execution_count": 11, "metadata": {}, "outputs": [], "source": [ "import sys\n", "\n", "# Check if we are in google colab currently\n", "try:\n", " import google.colab\n", "\n", " colabFlag = True\n", "except ImportError as r:\n", " colabFlag = False\n", "\n", "# Execute certain steps only if we are in a colab environment\n", "if colabFlag:\n", " # Create a folder in your Google Drive\n", " from google.colab import drive\n", "\n", " drive.mount(\"/content/drive\")\n", " # clone repository and set path\n", " !git clone https://github.com/BrainLesion/tutorials.git /content/drive/MyDrive/tutorials\n", " BASE_PATH = \"/content/drive/MyDrive/tutorials/panoptica\"\n", " sys.path.insert(0, BASE_PATH)\n", "\n", "else: # normal jupyter notebook environment\n", " BASE_PATH = \".\" # current working directory would be BraTs-Toolkit anyways if you are not in colab" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Setup Imports" ] }, { "cell_type": "code", "execution_count": 12, "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 InputType, Panoptica_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": 13, "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": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ "ref_masks = read_nifti(f\"{BASE_PATH}/spine_seg/matched_instance/ref.nii.gz\")\n", "pred_masks = read_nifti(f\"{BASE_PATH}/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": [ "## Build Evaluator object" ] }, { "cell_type": "code", "execution_count": 14, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Panoptic: Start Evaluation\n", "-- Got MatchedInstancePair, will evaluate instances\n" ] } ], "source": [ "evaluator = Panoptica_Evaluator(\n", " expected_input=InputType.MATCHED_INSTANCE,\n", " decision_metric=Metric.IOU,\n", " decision_threshold=0.5,\n", ")\n", "\n", "result, intermediate_steps_data = evaluator.evaluate(pred_masks, ref_masks)[\"ungrouped\"]" ] }, { "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": 15, "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", "\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", "Segmentation Quality Relative Volume Difference (sq_rvd): 0.0028133049062930553 +- 0.034518928495505724\n", "\n" ] } ], "source": [ "# print all results\n", "print(result)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Save Evaluator as config\n", "\n", "You can save your settings and arguments for nearly all panoptica-related objects as yaml configs" ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Saved config into /home/marcelrosier/miniconda3/envs/tutorials/lib/python3.10/site-packages/panoptica/test_config.yaml\n" ] } ], "source": [ "evaluator.save_to_config_by_name(\"test_config\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Load Configs\n", "\n", "You can simply load the same object by using its class.load_config:\n", "\n", "Notably: \n", "\n", "- load_from_config() uses an absolute path, while\n", "- load_from_config_by_name() uses some default path\n", "\n", "The same is true for saving configs:\n", "\n", "- save_to_config() saves to a given absolute path, while\n", "- save_to_config_by_name() uses a default path" ] }, { "cell_type": "code", "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n" ] } ], "source": [ "eval = Panoptica_Evaluator.load_from_config_name(\"test_config\")\n", "print(eval)" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Panoptic: Start Evaluation\n", "-- Got MatchedInstancePair, will evaluate instances\n", "\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", "\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", "Segmentation Quality Relative Volume Difference (sq_rvd): 0.0028133049062930553 +- 0.034518928495505724\n", "\n" ] } ], "source": [ "# Now use it as normal\n", "result, debug_data = evaluator.evaluate(pred_masks, ref_masks)[\"ungrouped\"]\n", "print(result) # yields same results as the evaluator object manually constructed" ] } ], "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.10.14" } }, "nbformat": 4, "nbformat_minor": 2 }