Skip to content

Instantly share code, notes, and snippets.

@ctralie
Last active July 1, 2020 18:15
Show Gist options
  • Save ctralie/ba83751b3e7a4fde11c9aebed273d32c to your computer and use it in GitHub Desktop.
Save ctralie/ba83751b3e7a4fde11c9aebed273d32c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import nilearn\n",
"import nilearn.masking\n",
"import nilearn.image\n",
"import matplotlib.pyplot as plt\n",
"import numpy as np\n",
"import glob"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def plot_volumes(volumes_path, prefix):\n",
" \"\"\"\n",
" Save volumes to disk as image slices\n",
" volumes_path: string\n",
" Path to voluemes that are of shape (Nx, Ny, Nz, NVolumes)\n",
" prefix: string\n",
" Prefix to use when saving frames\n",
" \"\"\"\n",
" img = nilearn.image.load_img(volumes_path)\n",
" I = img.get_fdata()\n",
" if len(I.shape) == 3:\n",
" I = I[:, :, :, None]\n",
" vmin = np.min(I)\n",
" vmax = np.max(I)\n",
" plt.figure(figsize=(6, 6))\n",
" for i in range(I.shape[-1]):\n",
" for z in range(I.shape[-2]):\n",
" Ii = I[:, :, z, i]\n",
" plt.clf()\n",
" plt.imshow(Ii, cmap='magma_r', vmin=vmin, vmax=vmax)\n",
" plt.title(\"Volume {} Slice {}\".format(i, z))\n",
" plt.colorbar()\n",
" plt.savefig(\"{}_{}_{}.png\".format(prefix, i, z))\n",
"\n",
" \n",
"def save_masked_regions(volumes_path, mask_path, prefix):\n",
" \"\"\"\n",
" Apply masks to volumes and save to disk\n",
" Parameters\n",
" ----------\n",
" volumes_path: string\n",
" File path to volumes that are of shape (Nx, Ny, Nz, NVolumes)\n",
" mask_path: string\n",
" File path to masks that are of shape (Nx, Ny, Nz)\n",
" prefix: string\n",
" Prefix to use when saving\n",
" \n",
" Returns\n",
" -------\n",
" list of nilearn.image\n",
" The masked image objects\n",
" \"\"\"\n",
" img = nilearn.image.load_img(volumes_path)\n",
" mask = nilearn.image.load_img(mask_path)\n",
" I = img.get_fdata()\n",
" M = mask.get_fdata()\n",
" if len(I) > len(M):\n",
" shape = tuple(list(M.shape) + [1])\n",
" M = np.reshape(M, shape)\n",
" vals = np.unique(M)\n",
" vals = vals[vals > 0]\n",
" imgs_masked = []\n",
" for v in vals:\n",
" Iv = np.array(I, dtype=np.int16)\n",
" Iv[np.abs(M - v) > 0] = 0\n",
" # Copy over affine and header\n",
" imgv = nilearn.image.new_img_like(img, Iv, img.affine, True)\n",
" filename = \"{}{}.nii\".format(prefix, int(v))\n",
" imgv.to_filename(filename)\n",
" imgs_masked.append(imgv)\n",
" return imgs_masked"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"maskfiles = glob.glob(\"*labels.nii\")\n",
"prefixes = [f.split(\"_labels.nii\")[0] for f in maskfiles]\n",
"datafiles = [\"{}_sdc_GR_TV.nii\".format(p) for p in prefixes]\n",
"for volume_file, mask_file, prefix in zip(datafiles, maskfiles, prefixes):\n",
" save_masked_regions(volume_file, mask_file, prefix)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"img = nilearn.image.load_img(\"JH02_PQS1.nii\")\n",
"img2 = nilearn.image.load_img(\"JH02_PQS_sdc_GR_TV.nii\")\n",
"print(img.header)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment