An open API service indexing awesome lists of open source software.

https://github.com/casperdcl/brainweb

BrainWeb: Multimodal models of 20 normal brains
https://github.com/casperdcl/brainweb

fdg mri neuroimaging pet-mr volume-rendering

Last synced: 6 months ago
JSON representation

BrainWeb: Multimodal models of 20 normal brains

Awesome Lists containing this project

README

          

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# BrainWeb-based multimodal models of 20 normal brains\n",
"\n",
"This project was initially inspired by \"[BrainWeb: 20 Anatomical Models of 20 Normal Brains][RawData].\"\n",
"\n",
"However there are a number of generally useful tools, image processing & display functions included in this project. For example, this includes `volshow()` for interactive comparison of multiple 3D volumes, `get_file()` for caching data URLs, and `register()` for image coregistration.\n",
"\n",
"[![PyPI]][PyPI-target]|[![CI]][CI-target]|[![Quality]][Quality-target]|[![DOI]][DOI-target]|[![LICENCE]][LICENCE-target]\n",
"-|-|-|-|-\n",
"\n",
"**Download and Preprocessing for PET-MR Simulations**\n",
"\n",
"This notebook will not re-download/re-process files if they already exist.\n",
"\n",
"- Output data\n",
" + `~/.brainweb/subject_*.npz`: dtype(shape): `float32(127, 344, 344)`\n",
"\n",
"- [Raw data source][RawData]\n",
" + `~/.brainweb/subject_*.bin.gz`: dtype(shape): `uint16(362, 434, 362)`\n",
"\n",
"- Install\n",
" + `pip install brainweb`\n",
"\n",
"----\n",
"\n",
"- Author: Casper da Costa-Luis <>\n",
"- Date: 2017-2020\n",
"- Licence: [MPLv2.0](https://www.mozilla.org/MPL/2.0)\n",
"\n",
"[RawData]: http://brainweb.bic.mni.mcgill.ca/brainweb/anatomic_normal_20.html\n",
"[PyPI]: https://img.shields.io/pypi/v/brainweb.svg\n",
"[PyPI-target]: https://pypi.org/project/brainweb\n",
"[CI]: https://travis-ci.org/casperdcl/brainweb.svg?branch=master\n",
"[CI-target]: https://travis-ci.org/casperdcl/brainweb\n",
"[Quality]: https://api.codacy.com/project/badge/Grade/cdad13693b0141199c31d5b44c7ab185\n",
"[Quality-target]: https://www.codacy.com/app/casper-dcl/brainweb\n",
"[DOI]: https://zenodo.org/badge/DOI/10.5281/zenodo.3269888.svg\n",
"[DOI-target]: https://doi.org/10.5281/zenodo.3269888\n",
"[LICENCE]: https://img.shields.io/pypi/l/brainweb.svg?label=licence\n",
"[LICENCE-target]: https://www.mozilla.org/MPL/2.0"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from __future__ import print_function, division\n",
"%matplotlib notebook\n",
"import brainweb\n",
"from brainweb import volshow\n",
"import numpy as np\n",
"from os import path\n",
"from tqdm.auto import tqdm\n",
"import logging\n",
"logging.basicConfig(level=logging.INFO)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Raw Data"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# download\n",
"files = brainweb.get_files()\n",
"\n",
"# read last file\n",
"data = brainweb.load_file(files[-1])\n",
"\n",
"# show last subject\n",
"print(files[-1])\n",
"volshow(data, cmaps=['gist_ncar']);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Transform\n",
"\n",
"

$\\ifcsname bm\\endcsname\\else\\newcommand{\\bm}[1]{\\mathbf{#1}}\\fi$
\n",
"Convert raw image data:\n",
"\n",
"- Siemens Biograph mMR resolution (~2mm) & dimensions (127, 344, 344)\n",
"- PET/T1/T2/uMap intensities\n",
" + PET defaults to FDG intensity ratios; could use e.g. Amyloid instead\n",
"- randomised structure for PET/T1/T2\n",
" + $\\bm{\\theta} \\circ (\\bm{1} + \\gamma[2G_\\sigma(\\bm{\\rho}) - \\bm{1}])$\n",
" * $\\bm{\\rho} = rand(127, 344, 344) \\in [0, 1)$\n",
" * Gaussian smoothing $\\sigma = 1$\n",
" * $\\gamma = \\left\\{\\matrix{1 & \\text{for PET}\\\\ 0.75 & \\text{for MR}}\\right.$\n",
" * $\\bm{\\theta}$ is the PET or MR piecewise constant phantom"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# show region probability masks\n",
"PetClass = brainweb.FDG\n",
"label_probs = brainweb.get_label_probabilities(files[-1], labels=PetClass.all_labels)\n",
"volshow(label_probs[brainweb.trim_zeros_ROI(label_probs)], titles=PetClass.all_labels, frameon=False);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"brainweb.seed(1337)\n",
"\n",
"for f in tqdm(files, desc=\"mMR ground truths\", unit=\"subject\"):\n",
" vol = brainweb.get_mmr_fromfile(\n",
" f,\n",
" petNoise=1, t1Noise=0.75, t2Noise=0.75,\n",
" petSigma=1, t1Sigma=1, t2Sigma=1,\n",
" PetClass=PetClass)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# show last subject\n",
"print(files[-1])\n",
"volshow([vol['PET' ][:, 100:-100, 100:-100],\n",
" vol['uMap'][:, 100:-100, 100:-100],\n",
" vol['T1' ][:, 100:-100, 100:-100],\n",
" vol['T2' ][:, 100:-100, 100:-100]],\n",
" cmaps=['hot', 'bone', 'Greys_r', 'Greys_r'],\n",
" titles=[\"PET\", \"uMap\", \"T1\", \"T2\"],\n",
" frameon=False);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# add some lesions\n",
"brainweb.seed(1337)\n",
"im3d = brainweb.add_lesions(vol['PET'])\n",
"volshow(im3d[:, 100:-100, 100:-100], cmaps=['hot']);"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# bonus: use brute-force registration to transform\n",
"#!pip install -U 'brainweb[register]'\n",
"reg = brainweb.register(\n",
" data[:, ::-1], target=vol['PET'],\n",
" src_resolution=brainweb.Res.brainweb,\n",
" target_resolution=brainweb.Res.mMR)\n",
"\n",
"volshow({\n",
" \"PET\": vol['PET'][:, 100:-100, 100:-100],\n",
" \"RawReg\": reg[ :, 100:-100, 100:-100],\n",
" \"T1\": vol['T1' ][:, 100:-100, 100:-100],\n",
"}, cmaps=['hot', 'gist_ncar', 'Greys_r'], ncols=3, tight_layout=5, figsize=(9.5, 3.5), frameon=False);"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython"
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}