{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Reviewing data\n",
    "\n",
    "Author: Martin Horvat, March 2025"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "from scipy import io\n",
    "import matplotlib.pyplot as plt\n",
    "import pandas as pd"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "suv_filename = \"../data/suv_percentilesSLOthenUWM.mat\"\n",
    "flags_filename=\"../data/flags_combined.mat\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "Loading all data\n",
    "\"\"\"\n",
    "suv_dict = io.loadmat(suv_filename)\n",
    "flags_dict = io.loadmat(flags_filename)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['__header__', '__version__', '__globals__', 'bowel_SUVperc_COMBINED', 'lung_SUVperc_COMBINED', 'thyroid_SUVperc_COMBINED'])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# what is suv dict\n",
    "suv_dict.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "dict_keys(['__header__', '__version__', '__globals__', 'days', 'flags', 'patients'])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "flags_dict.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>suv_name</th>\n",
       "      <th>flag_idx</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>organ</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>bowel</th>\n",
       "      <td>bowel_SUVperc_COMBINED</td>\n",
       "      <td>1</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>lung</th>\n",
       "      <td>lung_SUVperc_COMBINED</td>\n",
       "      <td>3</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>thyroid</th>\n",
       "      <td>thyroid_SUVperc_COMBINED</td>\n",
       "      <td>5</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                         suv_name  flag_idx\n",
       "organ                                      \n",
       "bowel      bowel_SUVperc_COMBINED         1\n",
       "lung        lung_SUVperc_COMBINED         3\n",
       "thyroid  thyroid_SUVperc_COMBINED         5"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\"\"\"\n",
    "    Present all data options\n",
    "\"\"\"\n",
    "\n",
    "choices = pd.DataFrame({\"suv_name\":['bowel_SUVperc_COMBINED','lung_SUVperc_COMBINED', 'thyroid_SUVperc_COMBINED'], \n",
    "                        \"flag_idx\":[1, 3, 5]}, \n",
    "                       index=[\"bowel\",\"lung\", \"thyroid\"])\n",
    "choices.index.name =\"organ\"\n",
    "choices"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "    Extracting only data (percentiles) associated to lungs\n",
    "\"\"\"\n",
    "Npatients = 58\n",
    "choice = choices.loc[\"lung\"]\n",
    "lung_suv = suv_dict[choice[\"suv_name\"]][:Npatients,:]\n",
    "\n",
    "# legend: 0 == NC, 1 == AE\n",
    "flags = flags_dict['flags'][:Npatients,choice[\"flag_idx\"]]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "    Getting data we are interested in:\n",
    "    for each patient we get maximal the pth percentail SUV \n",
    "\"\"\"\n",
    "\n",
    "p = 95  # percentile of interest\n",
    "X = np.nanmax(lung_suv[:,:,p], axis=1)  # taking SUV max and ignoring NaNs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "    Get data points associated to both groups\n",
    "\"\"\"\n",
    "\n",
    "X_NC = X[flags == 0]\n",
    "X_AE = X[flags == 1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<matplotlib.legend.Legend at 0x7dec77fdff50>"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    },
    {
     "data": {
      "image/png": "[encoded data, 15584 characters]",
      "text/plain": [
       "<Figure size 640x480 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "\"\"\"\n",
    "    Plotting histograms of data\n",
    "\"\"\"\n",
    "plt.hist(X_NC, density = True, label = \"NC\")\n",
    "plt.hist(X_AE, density = True, label = \"AE\")\n",
    "plt.legend()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "base",
   "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.12.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
