Skip to content

Instantly share code, notes, and snippets.

@bearpelican
Created January 27, 2020 23:59
Show Gist options
  • Save bearpelican/81e4485ff864e9f479b9e0a7d4fb21cc to your computer and use it in GitHub Desktop.
Save bearpelican/81e4485ff864e9f479b9e0a7d4fb21cc to your computer and use it in GitHub Desktop.
Simple Notebook to process svs images
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import openslide\n",
"import itertools\n",
"from fastai2.basics import *\n",
"from fastprogress import progress_bar"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# Enter path\n",
"path = Path('~/WAMRI/nuclei/').expanduser()\n",
"data_path = path/'data/test_svs'"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(Path('/data2/ashaw/WAMRI/nuclei/data/test_svs/P1C054g07.svs'), 'aperio')"
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fn = data_path.ls()[0]\n",
"fn, openslide.OpenSlide.detect_format(str(fn))"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(4, ((74256, 75067), (18564, 18766), (4641, 4691), (2320, 2345)))"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"slide = openslide.OpenSlide(str(fn))\n",
"slide.level_count, slide.level_dimensions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"slide.properties"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"out_dir = path/'data/processed'\n",
"out_dir.mkdir(parents=True, exist_ok=True)\n",
"lvl = 0 # level 0 is the max zoom\n",
"sz = (2048,2048) # tile resolution\n",
"overlap = 0 # overlap between tiles\n",
"dims = slide.level_dimensions[lvl] # overall size at zoom level"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"dimx = range(0,dims[0],sz[0]-overlap)\n",
"dimy = range(0,dims[1],sz[1]-overlap)\n",
"regions = list(itertools.product(dimx,dimy))"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"def process_region(coord):\n",
" x,y = coord\n",
" out_fn = out_dir/f'{fn.stem}_lvl{lvl}_{x}x{y}.jpg'\n",
" if out_fn.exists(): return\n",
" slide = openslide.OpenSlide(str(fn))\n",
" reg = slide.read_region((x,y), lvl, size=sz)\n",
" reg.convert('RGB').save(out_fn)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/plain": [
"(#1369) [None,None,None,None,None,None,None,None,None,None...]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"n_workers=16\n",
"parallel(process_region, regions, n_workers=n_workers)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"len(regions), len(out_dir.ls())"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.5"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment