{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Imports"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"from pathlib import Path\n",
"import pandas as pd\n",
"import json\n",
"import nibabel as nib\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import matplotlib.animation as animation\n",
"from IPython.display import Video\n",
"import glob\n",
"from src.plots import *\n",
"from make_pooled_datasets import *"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Configurations"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"configFile = Path('configs/3T_NIST_T1maps.json')\n",
"data_folder_name = '3T_NIST_T1maps'\n",
"output_gif_folder = Path(\"plots/01-wholedataset_gif_NIST/\")\n",
"output_gif_name = 'NIST.gif'"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Download datasets"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"if not Path(data_folder_name).exists():\n",
" make_pooled_dataset(configFile, data_folder_name)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Create and display GIF"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"
"
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fig = plt.figure()\n",
"\n",
"#Set background color to white\n",
"fig.set_facecolor(\"w\")\n",
"\n",
"# Set scalebar range\n",
"vmin = 0\n",
"vmax = 3000\n",
"\n",
"with open(configFile) as json_file:\n",
" configJson = json.load(json_file)\n",
"\n",
"ims = []\n",
"\n",
"for dataset_name in configJson:\n",
" for key1 in configJson[dataset_name]:\n",
" if key1 == 'datasets':\n",
" for key2 in configJson[dataset_name][key1]: \n",
" \n",
" # Load T1 image data\n",
" t1_file = configJson[dataset_name]['datasets'][key2]['imagePath']\n",
" t1 = nib.load(Path(data_folder_name) / t1_file)\n",
" t1_volume = t1.get_fdata() \n",
"\n",
" # Handle 2D vs 3D volume case\n",
" dims = t1_volume.shape\n",
" if (len(dims) == 2) or (np.min(dims) == 1):\n",
" im = plt.imshow(np.rot90(t1_volume), cmap=parula_map, animated=True, vmin = vmin, vmax = vmax)\n",
" else:\n",
" index_smallest_dim = np.argmin(dims)\n",
" numberOfSlices = dims[index_smallest_dim]\n",
" midSlice = int(np.round(numberOfSlices/2))\n",
"\n",
" if index_smallest_dim == 0:\n",
" im = plt.imshow(np.rot90(np.squeeze(t1_volume[midSlice,:,:])), cmap=parula_map, animated=True, vmin = vmin, vmax = vmax)\n",
" elif index_smallest_dim == 1:\n",
" im = plt.imshow(np.rot90(np.squeeze(t1_volume[:,midSlice,:])), cmap=parula_map, animated=True, vmin = vmin, vmax = vmax)\n",
" elif index_smallest_dim == 2:\n",
" im = plt.imshow(np.rot90(np.squeeze(t1_volume[:,:,midSlice])), cmap=parula_map, animated=True, vmin = vmin, vmax = vmax)\n",
" \n",
" # Add filename text to image\n",
" ttl = plt.text(0, 280, Path(t1_file).name, fontsize=6, horizontalalignment='left', verticalalignment='top')\n",
" \n",
" ims.append([im, ttl])\n",
"\n",
"# Save all images to GIF\n",
"plt.colorbar()\n",
"plt.clim(vmin,vmax)\n",
"\n",
"ani = animation.ArtistAnimation(fig, ims, interval=500, blit=False,repeat_delay=1000)\n",
"\n",
"output_gif_folder.mkdir(parents=True, exist_ok=True)\n",
"ani.save(output_gif_folder / output_gif_name, dpi=100, writer='imagemagick')\n",
"\n",
"plt.close()\n",
"\n",
"show_gif(output_gif_folder / output_gif_name)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [conda env:rrsg_analysis]",
"language": "python",
"name": "conda-env-rrsg_analysis-py"
},
"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.7"
}
},
"nbformat": 4,
"nbformat_minor": 4
}