Created
March 6, 2023 19:49
-
-
Save andreped/82a749f29d30f7e0f046fb43b8f3865a to your computer and use it in GitHub Desktop.
cellsens-read-magnification-python-bioformats.ipynb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"nbformat": 4, | |
"nbformat_minor": 0, | |
"metadata": { | |
"colab": { | |
"provenance": [], | |
"toc_visible": true, | |
"authorship_tag": "ABX9TyPRKBChEJozowpVTJt7TrMh", | |
"include_colab_link": true | |
}, | |
"kernelspec": { | |
"name": "python3", | |
"display_name": "Python 3" | |
}, | |
"language_info": { | |
"name": "python" | |
} | |
}, | |
"cells": [ | |
{ | |
"cell_type": "markdown", | |
"metadata": { | |
"id": "view-in-github", | |
"colab_type": "text" | |
}, | |
"source": [ | |
"<a href=\"https://colab.research.google.com/gist/andreped/82a749f29d30f7e0f046fb43b8f3865a/cellsens-read-magnification-python-bioformats.ipynb\" target=\"_parent\"><img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/></a>" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "VZNUceysGbXf", | |
"outputId": "1331f4b8-4de9-45f5-db94-a10955b68796" | |
}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", | |
"Collecting python-bioformats\n", | |
" Downloading python_bioformats-4.0.7-py3-none-any.whl (40.6 MB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m40.6/40.6 MB\u001b[0m \u001b[31m34.4 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hCollecting xmltodict\n", | |
" Downloading xmltodict-0.13.0-py2.py3-none-any.whl (10.0 kB)\n", | |
"Collecting boto3>=1.14.23\n", | |
" Downloading boto3-1.26.84-py3-none-any.whl (134 kB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m134.7/134.7 KB\u001b[0m \u001b[31m18.7 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hCollecting future>=0.18.2\n", | |
" Downloading future-0.18.3.tar.gz (840 kB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m840.9/840.9 KB\u001b[0m \u001b[31m42.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
"Collecting python-javabridge==4.0.3\n", | |
" Downloading python-javabridge-4.0.3.tar.gz (1.3 MB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m1.3/1.3 MB\u001b[0m \u001b[31m33.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25h Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
"Requirement already satisfied: numpy>=1.20.1 in /usr/local/lib/python3.8/dist-packages (from python-javabridge==4.0.3->python-bioformats) (1.22.4)\n", | |
"Collecting jmespath<2.0.0,>=0.7.1\n", | |
" Downloading jmespath-1.0.1-py3-none-any.whl (20 kB)\n", | |
"Collecting s3transfer<0.7.0,>=0.6.0\n", | |
" Downloading s3transfer-0.6.0-py3-none-any.whl (79 kB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m79.6/79.6 KB\u001b[0m \u001b[31m10.3 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hCollecting botocore<1.30.0,>=1.29.84\n", | |
" Downloading botocore-1.29.84-py3-none-any.whl (10.5 MB)\n", | |
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m10.5/10.5 MB\u001b[0m \u001b[31m92.1 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n", | |
"\u001b[?25hRequirement already satisfied: python-dateutil<3.0.0,>=2.1 in /usr/local/lib/python3.8/dist-packages (from botocore<1.30.0,>=1.29.84->boto3>=1.14.23->python-bioformats) (2.8.2)\n", | |
"Requirement already satisfied: urllib3<1.27,>=1.25.4 in /usr/local/lib/python3.8/dist-packages (from botocore<1.30.0,>=1.29.84->boto3>=1.14.23->python-bioformats) (1.26.14)\n", | |
"Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.8/dist-packages (from python-dateutil<3.0.0,>=2.1->botocore<1.30.0,>=1.29.84->boto3>=1.14.23->python-bioformats) (1.15.0)\n", | |
"Building wheels for collected packages: python-javabridge, future\n", | |
" Building wheel for python-javabridge (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
" Created wheel for python-javabridge: filename=python_javabridge-4.0.3-cp38-cp38-linux_x86_64.whl size=1895959 sha256=4bfa0abff7ae4c838762f510b851d2db7e12423310529029c31c4713b90a476c\n", | |
" Stored in directory: /root/.cache/pip/wheels/ce/f3/d8/389d390e98d568ea1dbcbd36398e73360038c289d29e78c462\n", | |
" Building wheel for future (setup.py) ... \u001b[?25l\u001b[?25hdone\n", | |
" Created wheel for future: filename=future-0.18.3-py3-none-any.whl size=492036 sha256=1d7cc417ccf2d2d22f27fb813857e3a97b9c5856400c77fa8f57de681c4df19f\n", | |
" Stored in directory: /root/.cache/pip/wheels/a0/0b/ee/e6994fadb42c1354dcccb139b0bf2795271bddfe6253ccdf11\n", | |
"Successfully built python-javabridge future\n", | |
"Installing collected packages: xmltodict, python-javabridge, jmespath, future, botocore, s3transfer, boto3, python-bioformats\n", | |
" Attempting uninstall: future\n", | |
" Found existing installation: future 0.16.0\n", | |
" Uninstalling future-0.16.0:\n", | |
" Successfully uninstalled future-0.16.0\n", | |
"Successfully installed boto3-1.26.84 botocore-1.29.84 future-0.18.3 jmespath-1.0.1 python-bioformats-4.0.7 python-javabridge-4.0.3 s3transfer-0.6.0 xmltodict-0.13.0\n" | |
] | |
} | |
], | |
"source": [ | |
"!pip install python-bioformats xmltodict" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"# download test WSI in the cellSens VSI format and uncompress\n", | |
"!wget https://openslide.cs.cmu.edu/download/openslide-testdata/Olympus/OS-2.zip\n", | |
"!unzip OS-2.zip" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "iKObWWw4GtV5", | |
"outputId": "53b8d489-e1d5-4ddb-f9fd-a5c4b2580568" | |
}, | |
"execution_count": 2, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"--2023-03-06 19:42:59-- https://openslide.cs.cmu.edu/download/openslide-testdata/Olympus/OS-2.zip\n", | |
"Resolving openslide.cs.cmu.edu (openslide.cs.cmu.edu)... 128.2.209.93\n", | |
"Connecting to openslide.cs.cmu.edu (openslide.cs.cmu.edu)|128.2.209.93|:443... connected.\n", | |
"HTTP request sent, awaiting response... 200 OK\n", | |
"Length: 309552104 (295M) [application/zip]\n", | |
"Saving to: ‘OS-2.zip’\n", | |
"\n", | |
"OS-2.zip 100%[===================>] 295.21M 2.37MB/s in 96s \n", | |
"\n", | |
"2023-03-06 19:44:36 (3.07 MB/s) - ‘OS-2.zip’ saved [309552104/309552104]\n", | |
"\n", | |
"Archive: OS-2.zip\n", | |
" inflating: OS-2.vsi \n", | |
" creating: _OS-2_/\n", | |
" creating: _OS-2_/stack10001/\n", | |
" inflating: _OS-2_/stack10001/frame_t.ets \n", | |
" creating: _OS-2_/stack1/\n", | |
" inflating: _OS-2_/stack1/frame_t.ets \n" | |
] | |
} | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"source": [ | |
"import javabridge as jb\n", | |
"import bioformats as bf\n", | |
"import xmltodict\n", | |
"\n", | |
"\n", | |
"def get_vsi_magnification(wsi_path):\n", | |
"\n", | |
" # get OME XML from VSI format\n", | |
" ome_xml = bf.get_omexml_metadata(wsi_path)\n", | |
"\n", | |
" # convert XML to human readable format (dict)\n", | |
" metadata = dict(xmltodict.parse(ome_xml))\n", | |
"\n", | |
" # extract objective information (can be more than one lens/objective)\n", | |
" curr = metadata[\"OME\"][\"Instrument\"][\"Objective\"]\n", | |
"\n", | |
" # extract magnification for each objective\n", | |
" magnifications = [eval(obj[\"@NominalMagnification\"]) for obj in curr]\n", | |
"\n", | |
" # assumption: the highest magnification value is the total magnification\n", | |
" return max(magnifications)\n", | |
"\n", | |
"\n", | |
"if __name__ == \"__main__\":\n", | |
"\n", | |
" # start virtual machine\n", | |
" jb.start_vm(class_path=bf.JARS, max_heap_size=\"4G\")\n", | |
"\n", | |
" magn = get_vsi_magnification(\"./OS-2.vsi\")\n", | |
" print(magn)\n", | |
"\n", | |
" # kill virtual machine\n", | |
" #jb.kill_vm()" | |
], | |
"metadata": { | |
"colab": { | |
"base_uri": "https://localhost:8080/" | |
}, | |
"id": "51RXGOdMHESE", | |
"outputId": "4ecf020a-05dd-4589-e278-3cc67547eee0" | |
}, | |
"execution_count": 3, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"name": "stdout", | |
"text": [ | |
"20.0\n" | |
] | |
} | |
] | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment