{
 "cells": [
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Introduction"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('/home/gpuvmadm/PyTomography/src')\n",
    "import numpy as np\n",
    "from pytomography.io import get_SPECT_recon_algorithm_simind\n",
    "from pytomography.metadata import PSFMeta\n",
    "from pytomography.priors import RelativeDifferencePrior\n",
    "import matplotlib.pyplot as plt\n",
    "import torch\n",
    "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. Create OSEM reconstruction network with output data from SIMIND, using\n",
    "    * attenuation correction\n",
    "    * PSF modeling\n",
    "    * Relative difference prior"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "osem_net = get_SPECT_recon_algorithm_simind(projections_header = '/home/gpuvmadm/PyTomography/test_files/quantitative/body1t2ew6_tot_w2.hdr',\n",
    "                        scatter_headers = ['/home/gpuvmadm/PyTomography/test_files/quantitative/body1t2ew6_tot_w1.hdr',\n",
    "                                           '/home/gpuvmadm/PyTomography/test_files/quantitative/body1t2ew6_tot_w3.hdr'],\n",
    "                        CT_header = '/home/gpuvmadm/PyTomography/test_files/quantitative/body1.hct',\n",
    "                        psf_meta=PSFMeta(collimator_slope=0.03013, collimator_intercept=0.1967),\n",
    "                        prior = RelativeDifferencePrior(beta=1, gamma=5),\n",
    "                        device=device)"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "2. Reconstruct object, specifying number of iterations and subsets."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "reconstructed_object = osem_net(n_iters=10, n_subsets=8)\n",
    "reconstructed_object = reconstructed_object[0].cpu().numpy()"
   ]
  },
  {
   "attachments": {},
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "3. Plot maximum intensity projections of the reconstructed object."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 72692 characters]",
      "text/plain": [
       "<Figure size 1200x1000 with 4 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "plt.subplots(1,2,figsize=(12,10))\n",
    "plt.subplot(121)\n",
    "plt.pcolormesh(reconstructed_object.max(axis=0).T, cmap='nipy_spectral')\n",
    "plt.colorbar()\n",
    "plt.axis('off')\n",
    "plt.subplot(122)\n",
    "plt.pcolormesh(reconstructed_object.max(axis=1).T, cmap='nipy_spectral')\n",
    "plt.colorbar()\n",
    "plt.axis('off')\n",
    "plt.savefig('../images/sample_MIP.png', dpi=300, transparent=True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "torch",
   "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.9.16"
  },
  "vscode": {
   "interpreter": {
    "hash": "324f5aa1485c6e81c0125c86ad1340c603dab08bc11babd97bd730cfafa2ec4a"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
