{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# BrainWeb-based multimodal models of 20 normal brains\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",
    "- [Raw data source](http://brainweb.bic.mni.mcgill.ca/brainweb/anatomic_normal_20.html)\n",
    "  + `~/.brainweb/subject_*.bin.gz`: dtype(shape): `uint16(362, 434, 362)`\n",
    "- Prerequisites\n",
    "  + Python: [requirements.txt](requirements.txt) (e.g. `pip install -r requirements.txt`)\n",
    "\n",
    "----\n",
    "\n",
    "- Author: Casper da Costa-Luis <<casper.dcl@physics.org>>\n",
    "- Date: 2017-19\n",
    "- Licence: [MPLv2.0](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",
    "<div style=\"visibility: hidden\">$\\ifcsname bm\\endcsname\\else\\newcommand{\\bm}[1]{\\mathbf{#1}}\\fi$</div>\n",
    "Convert raw image data:\n",
    "\n",
    "- Siemens Biograph mMR resolution (~2mm) & dimensions (127, 344, 344)\n",
    "- PET/T1/T2/uMap intensities\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": [
    "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)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# show last subject\n",
    "print(f)\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\"]);"
   ]
  },
  {
   "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']);"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython"
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}