diff --git a/panoptica/README.md b/panoptica/README.md index 869bb44..57f1867 100644 --- a/panoptica/README.md +++ b/panoptica/README.md @@ -1,61 +1,59 @@ # Panoptica Tutorials -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: +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. -**1. Instance Approximator:** instance approximation algorithms in panoptic segmentation evaluation. Available now: connected components algorithm. +## Notebooks +### Semantic: Spine example -**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. +semantic_figure -![workflow_figure](https://github.com/BrainLesion/panoptica/blob/main/examples/figures/workflow.png) +`example_spine_semantic.ipynb` -## Use Cases +[![nbviewer](https://raw.githubusercontent.com/jupyter/design/master/logos/Badges/nbviewer_badge.svg)](https://nbviewer.jupyter.org/github/BrainLesion/tutorials/blob/main/panoptica/example_spine_semantic.ipynb) + + Open In Colab + -### Semantic Segmentation Input +### Matched Instance: Spine example -semantic_figure +matched_instance_figure -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. +`example_spine_matched_instance.ipynb` -[Jupyter Notebook Example](https://github.com/BrainLesion/tutorials/tree/main/panoptica/example_spine_semantic.ipynb) +[![nbviewer](https://raw.githubusercontent.com/jupyter/design/master/logos/Badges/nbviewer_badge.svg)](https://nbviewer.jupyter.org/github/BrainLesion/tutorials/blob/main/panoptica/example_spine_matched_instance.ipynb) + + Open In Colab + -### Unmatched Instances Input +### Unmatched Instance: Spine example unmatched_instance_figure -It is a common issue that instance segementation outputs have good segmentations with mismatched labels. +`example_spine_unmatched_instance.ipynb` -For this case **modules [2-3]** can be utilized to match the instances and report panoptic metrics. +[![nbviewer](https://raw.githubusercontent.com/jupyter/design/master/logos/Badges/nbviewer_badge.svg)](https://nbviewer.jupyter.org/github/BrainLesion/tutorials/blob/main/panoptica/example_spine_unmatched_instance.ipynb) + + Open In Colab + -[Jupyter Notebook Example](https://github.com/BrainLesion/tutorials/tree/main/panoptica/example_spine_unmatched_instance.ipynb) - -### Matched Instances Input - -matched_instance_figure -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. +### Matching: Spine example +`example_spine_matching_algorithm.ipynb` -[Jupyter Notebook Example](https://github.com/BrainLesion/tutorials/tree/main/panoptica/example_spine_matched_instance.ipynb) +[![nbviewer](https://raw.githubusercontent.com/jupyter/design/master/logos/Badges/nbviewer_badge.svg)](https://nbviewer.jupyter.org/github/BrainLesion/tutorials/blob/main/panoptica/example_spine_matching_algorithm.ipynb) + + Open In Colab + -## Citation -If you use panoptica in your research, please cite it to support the development! +### Save/Load Configs example: +`example_config.ipynb` -Kofler, F., Möller, H., Buchner, J. A., de la Rosa, E., Ezhov, I., Rosier, M., Mekki, I., Shit, S., Negwer, M., Al-Maskari, R., Ertürk, A., Vinayahalingam, S., Isensee, F., Pati, S., Rueckert, D., Kirschke, J. S., Ehrlich, S. K., Reinke, A., Menze, B., Wiestler, B., & Piraud, M. (2023). _Panoptica -- instance-wise evaluation of 3D semantic and instance segmentation maps._ [arXiv preprint arXiv:2312.02608](https://arxiv.org/abs/2312.02608). +[![nbviewer](https://raw.githubusercontent.com/jupyter/design/master/logos/Badges/nbviewer_badge.svg)](https://nbviewer.jupyter.org/github/BrainLesion/tutorials/blob/main/panoptica/example_config.ipynb) + + Open In Colab + -``` -@misc{kofler2023panoptica, - title={Panoptica -- instance-wise evaluation of 3D semantic and instance segmentation maps}, - author={Florian Kofler and Hendrik Möller and Josef A. Buchner and Ezequiel de la Rosa and Ivan Ezhov and Marcel Rosier and Isra Mekki and Suprosanna Shit and Moritz Negwer and Rami Al-Maskari and Ali Ertürk and Shankeeth Vinayahalingam and Fabian Isensee and Sarthak Pati and Daniel Rueckert and Jan S. Kirschke and Stefan K. Ehrlich and Annika Reinke and Bjoern Menze and Benedikt Wiestler and Marie Piraud}, - year={2023}, - eprint={2312.02608}, - archivePrefix={arXiv}, - primaryClass={cs.CV} -} -``` diff --git a/panoptica/example_config.ipynb b/panoptica/example_config.ipynb index 5734fd2..296b684 100644 --- a/panoptica/example_config.ipynb +++ b/panoptica/example_config.ipynb @@ -16,39 +16,69 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 10, + "metadata": {}, + "outputs": [], + "source": [ + "!pip install panoptica auxiliary rich numpy > /dev/null" + ] + }, + { + "cell_type": "markdown", "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)) (1.0.0.post2.dev0+2f7d01f)\n", - "Requirement already satisfied: auxiliary in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 2)) (0.0.42)\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: ruamel.yaml<0.19.0,>=0.18.6 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (0.18.6)\n", - "Requirement already satisfied: scikit-image<0.23.0,>=0.22.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (0.22.0)\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.10.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (17.0.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", - "Requirement already satisfied: ruamel.yaml.clib>=0.2.7 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from ruamel.yaml<0.19.0,>=0.18.6->panoptica->-r requirements.txt (line 1)) (0.2.8)\n", - "Requirement already satisfied: networkx>=2.8 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (3.1)\n", - "Requirement already satisfied: imageio>=2.27 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (2.31.3)\n", - "Requirement already satisfied: lazy_loader>=0.3 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (0.3)\n" - ] - } - ], "source": [ - "!pip install -r requirements.txt" + "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" ] }, { @@ -60,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 10, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -90,7 +120,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -102,14 +132,14 @@ " 106, 107, 108, 202, 203, 204, 205, 206, 207, 208], dtype=uint8))" ] }, - "execution_count": 11, + "execution_count": 13, "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", + "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)" @@ -124,7 +154,7 @@ }, { "cell_type": "code", - "execution_count": 12, + "execution_count": 14, "metadata": {}, "outputs": [ { @@ -156,7 +186,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -202,14 +232,14 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "Saved config into /DATA/NAS/ongoing_projects/hendrik/panoptica/repo/panoptica/test_config.yaml\n" + "Saved config into /home/marcelrosier/miniconda3/envs/tutorials/lib/python3.10/site-packages/panoptica/test_config.yaml\n" ] } ], @@ -238,14 +268,14 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": 17, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ - "\n" + "\n" ] } ], @@ -256,7 +286,7 @@ }, { "cell_type": "code", - "execution_count": 16, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -267,9 +297,9 @@ "-- Got MatchedInstancePair, will evaluate instances\n", "\n", "+++ MATCHING +++\n", - "Number of instances in reference (num_ref_instances): 1\n", - "Number of instances in prediction (num_pred_instances): 1\n", - "True Positives (tp): 1\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", @@ -278,12 +308,12 @@ "Global Binary Dice (global_bin_dsc): 0.9744370224078394\n", "\n", "+++ INSTANCE +++\n", - "Segmentation Quality IoU (sq): 0.9501484001456879 +- 0.0\n", - "Panoptic Quality IoU (pq): 0.9501484001456879\n", - "Segmentation Quality Dsc (sq_dsc): 0.9744370224078394 +- 0.0\n", - "Panoptic Quality Dsc (pq_dsc): 0.9744370224078394\n", - "Segmentation Quality ASSD (sq_assd): 0.1499152780072207 +- 0.0\n", - "Segmentation Quality Relative Volume Difference (sq_rvd): -0.0006011763989268869 +- 0.0\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" ] } @@ -293,13 +323,6 @@ "result, debug_data = evaluator.evaluate(pred_masks, ref_masks)[\"ungrouped\"]\n", "print(result) # yields same results as the evaluator object manually constructed" ] - }, - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [] } ], "metadata": { @@ -318,7 +341,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.6" + "version": "3.10.14" } }, "nbformat": 4, diff --git a/panoptica/example_spine_matched_instance.ipynb b/panoptica/example_spine_matched_instance.ipynb index e20a019..8c8be35 100644 --- a/panoptica/example_spine_matched_instance.ipynb +++ b/panoptica/example_spine_matched_instance.ipynb @@ -16,39 +16,69 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 10, "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)) (1.0.0.post2.dev0+2f7d01f)\n", - "Requirement already satisfied: auxiliary in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 2)) (0.0.42)\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: ruamel.yaml<0.19.0,>=0.18.6 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (0.18.6)\n", - "Requirement already satisfied: scikit-image<0.23.0,>=0.22.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (0.22.0)\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.10.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (17.0.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", - "Requirement already satisfied: ruamel.yaml.clib>=0.2.7 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from ruamel.yaml<0.19.0,>=0.18.6->panoptica->-r requirements.txt (line 1)) (0.2.8)\n", - "Requirement already satisfied: networkx>=2.8 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (3.1)\n", - "Requirement already satisfied: imageio>=2.27 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (2.31.3)\n", - "Requirement already satisfied: lazy_loader>=0.3 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (0.3)\n" - ] - } - ], + "outputs": [], "source": [ - "!pip install -r requirements.txt" + "!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" ] }, { @@ -60,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -90,7 +120,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -102,14 +132,14 @@ " 106, 107, 108, 202, 203, 204, 205, 206, 207, 208], dtype=uint8))" ] }, - "execution_count": 3, + "execution_count": 13, "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", + "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)" @@ -124,74 +154,9 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 14, "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", @@ -221,7 +186,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 15, "metadata": {}, "outputs": [ { @@ -258,7 +223,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -282,7 +247,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -348,7 +313,7 @@ }, { "cell_type": "code", - "execution_count": 11, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -397,7 +362,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.6" + "version": "3.10.14" } }, "nbformat": 4, diff --git a/panoptica/example_spine_matching_algorithm.ipynb b/panoptica/example_spine_matching_algorithm.ipynb index b623947..7f89851 100644 --- a/panoptica/example_spine_matching_algorithm.ipynb +++ b/panoptica/example_spine_matching_algorithm.ipynb @@ -18,44 +18,36 @@ "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)) (1.0.0.post2.dev0+2f7d01f)\n", - "Requirement already satisfied: auxiliary in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 2)) (0.0.42)\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: ruamel.yaml<0.19.0,>=0.18.6 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (0.18.6)\n", - "Requirement already satisfied: scikit-image<0.23.0,>=0.22.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (0.22.0)\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.10.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (17.0.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", - "Requirement already satisfied: ruamel.yaml.clib>=0.2.7 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from ruamel.yaml<0.19.0,>=0.18.6->panoptica->-r requirements.txt (line 1)) (0.2.8)\n", - "Requirement already satisfied: networkx>=2.8 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (3.1)\n", - "Requirement already satisfied: imageio>=2.27 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (2.31.3)\n", - "Requirement already satisfied: lazy_loader>=0.3 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (0.3)\n" - ] - } - ], + "outputs": [], "source": [ - "!pip install -r requirements.txt" + "!pip install panoptica auxiliary rich numpy > /dev/null" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Setup Imports" + "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." ] }, { @@ -63,6 +55,53 @@ "execution_count": 2, "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": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No module named 'pandas'\n", + "OPTIONAL PACKAGE MISSING\n" + ] + } + ], "source": [ "import numpy as np\n", "from auxiliary.nifti.io import read_nifti\n", @@ -90,7 +129,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [ { @@ -102,14 +141,14 @@ " 107, 108, 109, 203, 204, 205, 206, 207, 208, 209], dtype=uint8))" ] }, - "execution_count": 3, + "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "ref_masks = read_nifti(\"./spine_seg/unmatched_instance/ref.nii.gz\")\n", - "pred_masks = read_nifti(\"./spine_seg/unmatched_instance/pred.nii.gz\")\n", + "ref_masks = read_nifti(f\"{BASE_PATH}/spine_seg/unmatched_instance/ref.nii.gz\")\n", + "pred_masks = read_nifti(f\"{BASE_PATH}/spine_seg/unmatched_instance/pred.nii.gz\")\n", "\n", "# labels are unmatched instances\n", "pred_masks[pred_masks == 27] = 26 # For later\n", @@ -125,7 +164,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -152,7 +191,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -197,7 +236,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -253,7 +292,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.6" + "version": "3.10.14" } }, "nbformat": 4, diff --git a/panoptica/example_spine_semantic.ipynb b/panoptica/example_spine_semantic.ipynb index 44bafff..7015c3c 100644 --- a/panoptica/example_spine_semantic.ipynb +++ b/panoptica/example_spine_semantic.ipynb @@ -16,46 +16,38 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 10, "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)) (1.0.0.post2.dev0+2f7d01f)\n", - "Requirement already satisfied: auxiliary in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 2)) (0.0.42)\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: ruamel.yaml<0.19.0,>=0.18.6 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (0.18.6)\n", - "Requirement already satisfied: scikit-image<0.23.0,>=0.22.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (0.22.0)\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.10.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (17.0.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", - "Requirement already satisfied: ruamel.yaml.clib>=0.2.7 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from ruamel.yaml<0.19.0,>=0.18.6->panoptica->-r requirements.txt (line 1)) (0.2.8)\n", - "Requirement already satisfied: networkx>=2.8 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (3.1)\n", - "Requirement already satisfied: imageio>=2.27 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (2.31.3)\n", - "Requirement already satisfied: lazy_loader>=0.3 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (0.3)\n" - ] - } - ], + "outputs": [], "source": [ - "!pip install -r requirements.txt" + "!pip install panoptica auxiliary rich numpy > /dev/null" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "## Setup Imports" + "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." ] }, { @@ -63,6 +55,53 @@ "execution_count": 2, "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": 3, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "No module named 'pandas'\n", + "OPTIONAL PACKAGE MISSING\n" + ] + } + ], "source": [ "from auxiliary.nifti.io import read_nifti\n", "from rich import print as pprint\n", @@ -92,12 +131,12 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 4, "metadata": {}, "outputs": [], "source": [ - "ref_masks = read_nifti(\"./spine_seg/semantic/ref.nii.gz\")\n", - "pred_masks = read_nifti(\"./spine_seg/semantic/pred.nii.gz\")" + "ref_masks = read_nifti(f\"{BASE_PATH}/spine_seg/semantic/ref.nii.gz\")\n", + "pred_masks = read_nifti(f\"{BASE_PATH}/spine_seg/semantic/pred.nii.gz\")" ] }, { @@ -123,7 +162,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 5, "metadata": {}, "outputs": [], "source": [ @@ -144,7 +183,7 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 6, "metadata": {}, "outputs": [ { @@ -231,10 +270,10 @@ "+++ INSTANCE +++\n", "Segmentation Quality IoU (sq): 0.7940127477906024 +- 0.11547745015679488\n", "Panoptic Quality IoU (pq): 0.6586696657808406\n", - "Segmentation Quality Dsc (sq_dsc): 0.8802182546605446 +- 0.07728416427007168\n", + "Segmentation Quality Dsc (sq_dsc): 0.8802182546605446 +- 0.07728416427007166\n", "Panoptic Quality Dsc (pq_dsc): 0.7301810521615881\n", - "Segmentation Quality ASSD (sq_assd): 0.2057371092494465 +- 0.1398348236766068\n", - "Segmentation Quality Relative Volume Difference (sq_rvd): 0.011340219860617232 +- 0.1217805112447998\n", + "Segmentation Quality ASSD (sq_assd): 0.20573710924944655 +- 0.13983482367660682\n", + "Segmentation Quality Relative Volume Difference (sq_rvd): 0.01134021986061723 +- 0.1217805112447998\n", "\n" ] } @@ -249,7 +288,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 7, "metadata": {}, "outputs": [ { @@ -273,7 +312,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 8, "metadata": {}, "outputs": [ { @@ -293,11 +332,11 @@ " 'sq_std': 0.11547745015679488,\n", " 'pq': 0.6586696657808406,\n", " 'sq_dsc': 0.8802182546605446,\n", - " 'sq_dsc_std': 0.07728416427007168,\n", + " 'sq_dsc_std': 0.07728416427007166,\n", " 'pq_dsc': 0.7301810521615881,\n", - " 'sq_assd': 0.2057371092494465,\n", - " 'sq_assd_std': 0.1398348236766068,\n", - " 'sq_rvd': 0.011340219860617232,\n", + " 'sq_assd': 0.20573710924944655,\n", + " 'sq_assd_std': 0.13983482367660682,\n", + " 'sq_rvd': 0.01134021986061723,\n", " 'sq_rvd_std': 0.1217805112447998,\n", " 'global_bin_dsc': 0.9731641527805414\n", "}\n", @@ -318,11 +357,11 @@ " \u001b[32m'sq_std'\u001b[0m: \u001b[1;36m0.11547745015679488\u001b[0m,\n", " \u001b[32m'pq'\u001b[0m: \u001b[1;36m0.6586696657808406\u001b[0m,\n", " \u001b[32m'sq_dsc'\u001b[0m: \u001b[1;36m0.8802182546605446\u001b[0m,\n", - " \u001b[32m'sq_dsc_std'\u001b[0m: \u001b[1;36m0.07728416427007168\u001b[0m,\n", + " \u001b[32m'sq_dsc_std'\u001b[0m: \u001b[1;36m0.07728416427007166\u001b[0m,\n", " \u001b[32m'pq_dsc'\u001b[0m: \u001b[1;36m0.7301810521615881\u001b[0m,\n", - " \u001b[32m'sq_assd'\u001b[0m: \u001b[1;36m0.2057371092494465\u001b[0m,\n", - " \u001b[32m'sq_assd_std'\u001b[0m: \u001b[1;36m0.1398348236766068\u001b[0m,\n", - " \u001b[32m'sq_rvd'\u001b[0m: \u001b[1;36m0.011340219860617232\u001b[0m,\n", + " \u001b[32m'sq_assd'\u001b[0m: \u001b[1;36m0.20573710924944655\u001b[0m,\n", + " \u001b[32m'sq_assd_std'\u001b[0m: \u001b[1;36m0.13983482367660682\u001b[0m,\n", + " \u001b[32m'sq_rvd'\u001b[0m: \u001b[1;36m0.01134021986061723\u001b[0m,\n", " \u001b[32m'sq_rvd_std'\u001b[0m: \u001b[1;36m0.1217805112447998\u001b[0m,\n", " \u001b[32m'global_bin_dsc'\u001b[0m: \u001b[1;36m0.9731641527805414\u001b[0m\n", "\u001b[1m}\u001b[0m\n" @@ -339,7 +378,7 @@ }, { "cell_type": "code", - "execution_count": 8, + "execution_count": 9, "metadata": {}, "outputs": [ { @@ -361,11 +400,11 @@ " 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87]\n", "\n", "InputType.MATCHED_INSTANCE\n", - "Prediction array shape = (170, 512, 17) unique_values= [ 0 1 2 4 5 6 9 10 12 13 14 15 16 17 18 19 20 21\n", - " 22 23 24 25 26 27 28 29 30 32 33 34 35 36 37 38 39 41\n", - " 42 43 44 45 49 50 51 52 54 55 56 57 58 60 61 62 63 64\n", - " 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82\n", - " 83 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103]\n", + "Prediction array shape = (170, 512, 17) unique_values= [ 0 1 2 3 4 6 7 8 9 10 12 13 14 15 16 17 18 19\n", + " 20 22 23 25 26 31 33 34 35 38 40 41 42 44 45 46 47 48\n", + " 49 50 51 52 53 54 55 56 57 58 59 60 61 63 64 65 66 67\n", + " 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85\n", + " 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103]\n", "Reference array shape = (170, 512, 17) unique_values= [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23\n", " 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47\n", " 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71\n", @@ -416,7 +455,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.6" + "version": "3.10.14" } }, "nbformat": 4, diff --git a/panoptica/example_spine_unmatched_instance.ipynb b/panoptica/example_spine_unmatched_instance.ipynb index d994a5a..b385bda 100644 --- a/panoptica/example_spine_unmatched_instance.ipynb +++ b/panoptica/example_spine_unmatched_instance.ipynb @@ -16,39 +16,69 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": 10, "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)) (1.0.0.post2.dev0+2f7d01f)\n", - "Requirement already satisfied: auxiliary in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from -r requirements.txt (line 2)) (0.0.42)\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: ruamel.yaml<0.19.0,>=0.18.6 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (0.18.6)\n", - "Requirement already satisfied: scikit-image<0.23.0,>=0.22.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from panoptica->-r requirements.txt (line 1)) (0.22.0)\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.10.0 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from auxiliary->-r requirements.txt (line 2)) (17.0.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", - "Requirement already satisfied: ruamel.yaml.clib>=0.2.7 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from ruamel.yaml<0.19.0,>=0.18.6->panoptica->-r requirements.txt (line 1)) (0.2.8)\n", - "Requirement already satisfied: networkx>=2.8 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (3.1)\n", - "Requirement already satisfied: imageio>=2.27 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (2.31.3)\n", - "Requirement already satisfied: lazy_loader>=0.3 in /opt/anaconda3/envs/seg11panoptdev/lib/python3.11/site-packages (from scikit-image<0.23.0,>=0.22.0->panoptica->-r requirements.txt (line 1)) (0.3)\n" - ] - } - ], + "outputs": [], "source": [ - "!pip install -r requirements.txt" + "!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" ] }, { @@ -60,7 +90,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": 12, "metadata": {}, "outputs": [], "source": [ @@ -90,7 +120,7 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": 13, "metadata": {}, "outputs": [ { @@ -102,14 +132,14 @@ " 107, 108, 109, 203, 204, 205, 206, 207, 208, 209], dtype=uint8))" ] }, - "execution_count": 3, + "execution_count": 13, "metadata": {}, "output_type": "execute_result" } ], "source": [ - "ref_masks = read_nifti(\"./spine_seg/unmatched_instance/ref.nii.gz\")\n", - "pred_masks = read_nifti(\"./spine_seg/unmatched_instance/pred.nii.gz\")\n", + "ref_masks = read_nifti(f\"{BASE_PATH}/spine_seg/unmatched_instance/ref.nii.gz\")\n", + "pred_masks = read_nifti(f\"{BASE_PATH}/spine_seg/unmatched_instance/pred.nii.gz\")\n", "\n", "# labels are unmatching\n", "pred_masks[pred_masks == 27] = 26 # For later\n", @@ -125,7 +155,7 @@ }, { "cell_type": "code", - "execution_count": 4, + "execution_count": 14, "metadata": {}, "outputs": [], "source": [ @@ -159,74 +189,9 @@ }, { "cell_type": "code", - "execution_count": 5, + "execution_count": 15, "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", @@ -334,7 +299,7 @@ }, { "cell_type": "code", - "execution_count": 6, + "execution_count": 16, "metadata": {}, "outputs": [ { @@ -359,7 +324,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": 17, "metadata": {}, "outputs": [ { @@ -425,7 +390,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": 18, "metadata": {}, "outputs": [ { @@ -522,7 +487,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.6" + "version": "3.10.14" } }, "nbformat": 4, diff --git a/panoptica/requirements.txt b/panoptica/requirements.txt deleted file mode 100644 index 21d8918..0000000 --- a/panoptica/requirements.txt +++ /dev/null @@ -1,4 +0,0 @@ -panoptica -auxiliary -rich -numpy \ No newline at end of file