{ "cells": [ { "cell_type": "markdown", "id": "e5c34bf1", "metadata": { "colab_type": "text", "id": "view-in-github" }, "source": [ "\n", "\"Open " ] }, { "cell_type": "markdown", "id": "1b937be1", "metadata": {}, "source": [ "## Basic Nipype\n", "\n", "Author: Steffen Bollmann" ] }, { "cell_type": "markdown", "id": "7hrfYzlFU6pN", "metadata": { "id": "7hrfYzlFU6pN" }, "source": [ "## Setup Neurodesk" ] }, { "cell_type": "code", "execution_count": 1, "id": "DwyOSXw4U3Kn", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "DwyOSXw4U3Kn", "outputId": "9596fece-8f39-4b8a-b188-b9af0d12fdcd" }, "outputs": [], "source": [ "import os\n", "import sys\n", "IN_COLAB = 'google.colab' in sys.modules\n", "\n", "if IN_COLAB:\n", " os.environ[\"LD_PRELOAD\"] = \"\";\n", " os.environ[\"APPTAINER_BINDPATH\"] = \"/content,/tmp,/cvmfs\"\n", " os.environ[\"MPLCONFIGDIR\"] = \"/content/matplotlib-mpldir\"\n", " os.environ[\"LMOD_CMD\"] = \"/usr/share/lmod/lmod/libexec/lmod\"\n", "\n", " !curl -J -O https://raw.githubusercontent.com/NeuroDesk/neurocommand/main/googlecolab_setup.sh\n", " !chmod +x googlecolab_setup.sh\n", " !./googlecolab_setup.sh\n", "\n", " os.environ[\"MODULEPATH\"] = ':'.join(map(str, list(map(lambda x: os.path.join(os.path.abspath('/cvmfs/neurodesk.ardc.edu.au/neurodesk-modules/'), x),os.listdir('/cvmfs/neurodesk.ardc.edu.au/neurodesk-modules/')))))\n", "\n", " from google.colab import output\n", " output.enable_custom_widget_manager()\n", " !pip install ipyniivue" ] }, { "cell_type": "code", "execution_count": 2, "id": "7dc1211f-1450-4d97-b35f-3d93d5865d57", "metadata": { "tags": [] }, "outputs": [], "source": [ "%%bash\n", "# Output CPU information:\n", "cat /proc/cpuinfo | grep 'vendor' | uniq\n", "cat /proc/cpuinfo | grep 'model name' | uniq" ] }, { "cell_type": "markdown", "id": "52d527e8-c988-4f60-9d12-cc39e248ad11", "metadata": { "id": "52d527e8-c988-4f60-9d12-cc39e248ad11" }, "source": [ "# Demonstrating the module system in Python and Nipype" ] }, { "cell_type": "code", "execution_count": 3, "id": "00345266-eb39-40c9-a29a-32cda10ebba3", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "00345266-eb39-40c9-a29a-32cda10ebba3", "outputId": "86874c10-4735-471f-e569-23a6c9f944df" }, "outputs": [ { "data": { "text/plain": [ "['fsl/6.0.4']" ] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# we can use lmod to load fsl in a specific version\n", "import lmod\n", "await lmod.load('fsl/6.0.4')\n", "await lmod.list()" ] }, { "cell_type": "code", "execution_count": null, "id": "16aa3e1e-b238-44f1-94a6-aaf0843922ff", "metadata": {}, "outputs": [], "source": [ "from nipype.interfaces.fsl.base import Info\n", "print(Info.version())\n", "# if the FSL version is changed using lmod above, the kernel of the notebook needs to be restarted!" ] }, { "cell_type": "code", "execution_count": null, "id": "2e447e8b-a6fd-4365-9da3-3de21a4654b2", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "2e447e8b-a6fd-4365-9da3-3de21a4654b2", "outputId": "d5667809-043f-408a-9d46-bbc291775cc2" }, "outputs": [], "source": [ "!bet" ] }, { "cell_type": "markdown", "id": "f09a1ba3-c01d-48df-8894-6c566907d6be", "metadata": { "id": "f09a1ba3-c01d-48df-8894-6c566907d6be" }, "source": [ "## Load afni and SPM as well" ] }, { "cell_type": "code", "execution_count": null, "id": "67839715-b05f-4da5-9d8f-511ce48e8b2b", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "67839715-b05f-4da5-9d8f-511ce48e8b2b", "outputId": "db381252-d613-4959-b99f-f22c71eb5d26" }, "outputs": [], "source": [ "await lmod.load('afni/22.3.06')\n", "await lmod.load('spm12/r7771')\n", "await lmod.list()" ] }, { "cell_type": "markdown", "id": "11961012-a84e-40d5-a2df-086ede86d163", "metadata": { "id": "11961012-a84e-40d5-a2df-086ede86d163" }, "source": [ "## download test data" ] }, { "cell_type": "code", "execution_count": null, "id": "b2513194-a19e-43e5-80a8-9dccc4d47bb9", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "b2513194-a19e-43e5-80a8-9dccc4d47bb9", "outputId": "57372e2a-1225-4c7b-e6c4-548fe1852411" }, "outputs": [], "source": [ "%%bash\n", "if [ -f ./sub-01_ses-01_7T_T1w_defaced.nii ]; then\n", " echo \"nii Output file exists, not downloading or unpacking again\"\n", "else\n", " if [ ! -f ./sub-01_ses-01_7T_T1w_defaced.nii.gz ]; then\n", " echo \"nii.gz does not exist. So, it needs to be downloaded.\"\n", " osfURL=\"osfstorage/TOMCAT_DIB/sub-01/ses-01_7T/anat/sub-01_ses-01_7T_T1w_defaced.nii.gz\"\n", " echo \"downloading now ...\"\n", " osf -p bt4ez fetch $osfURL ./sub-01_ses-01_7T_T1w_defaced.nii.gz\n", " fi\n", "\n", " if [ -f ./sub-01_ses-01_7T_T1w_defaced.nii.gz ]; then\n", " echo \"nii.gz exists. So, it needs to be unpacked and deleted\"\n", " echo \"unpacking now ...\"\n", " gunzip ./sub-01_ses-01_7T_T1w_defaced.nii.gz\n", " fi\n", "fi" ] }, { "cell_type": "code", "execution_count": null, "id": "37f99b61-55a6-4016-a697-bf065a083c7f", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "37f99b61-55a6-4016-a697-bf065a083c7f", "outputId": "791c495f-3ccd-4b6a-af64-0f3ba732663f" }, "outputs": [], "source": [ "%ls " ] }, { "cell_type": "markdown", "id": "1cdb6e67-4927-4c2a-bc10-dfbfafe0b7af", "metadata": { "id": "1cdb6e67-4927-4c2a-bc10-dfbfafe0b7af" }, "source": [ "## run nipype pipeline" ] }, { "cell_type": "code", "execution_count": null, "id": "46d3e618", "metadata": {}, "outputs": [], "source": [ "%%capture\n", "!pip install nipype" ] }, { "cell_type": "code", "execution_count": null, "id": "9687c577-73df-4b05-8bb0-8fcfc0a220a9", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "9687c577-73df-4b05-8bb0-8fcfc0a220a9", "outputId": "5fb0c02a-4250-4276-982b-7bf15b54d6e9" }, "outputs": [], "source": [ "from nipype.interfaces import fsl\n", "from nipype.interfaces import afni\n", "\n", "btr = fsl.BET()\n", "btr.inputs.in_file = './sub-01_ses-01_7T_T1w_defaced.nii'\n", "btr.inputs.frac = 0.4\n", "btr.inputs.out_file = './sub-01_ses-01_7T_T1w_defaced_brain.nii'\n", "res = btr.run() \n", "\n", "edge3 = afni.Edge3()\n", "edge3.inputs.in_file = './sub-01_ses-01_7T_T1w_defaced.nii'\n", "edge3.inputs.out_file = './sub-01_ses-01_7T_T1w_defaced_edges.nii'\n", "edge3.inputs.datum = 'byte'\n", "res = edge3.run()" ] }, { "cell_type": "code", "execution_count": null, "id": "0bd12d00-3cf5-4f0e-b6f5-90007825d40c", "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "0bd12d00-3cf5-4f0e-b6f5-90007825d40c", "outputId": "2920bcc1-2441-4904-b157-43231bfb2f66" }, "outputs": [], "source": [ "%ls" ] }, { "cell_type": "code", "execution_count": null, "id": "5adcb468-1c1c-414f-8150-e9d6d9964688", "metadata": { "id": "5adcb468-1c1c-414f-8150-e9d6d9964688" }, "outputs": [], "source": [ "# View 3D data\n", "import matplotlib.pyplot as plt\n", "\n", "def view_slices_3d(image_3d, slice_nbr, vmin, vmax, title=''):\n", "# print('Matrix size: {}'.format(image_3d.shape))\n", " fig = plt.figure(figsize=(15, 4))\n", " plt.suptitle(title, fontsize=10)\n", "\n", " plt.subplot(131)\n", " plt.imshow(np.take(image_3d, slice_nbr, 2), vmin=vmin, vmax=vmax, cmap='gray')\n", " plt.title('Axial');\n", "\n", " plt.subplot(132)\n", " image_rot = ndimage.rotate(np.take(image_3d, slice_nbr, 1),90)\n", " plt.imshow(image_rot, vmin=vmin, vmax=vmax, cmap='gray')\n", " plt.title('Coronal');\n", "\n", " plt.subplot(133)\n", " image_rot = ndimage.rotate(np.take(image_3d, slice_nbr, 0),90)\n", " plt.imshow(image_rot, vmin=vmin, vmax=vmax, cmap='gray')\n", " plt.title('Sagittal');\n", " cbar=plt.colorbar()\n", "\n", "def get_figure():\n", " \"\"\"\n", " Returns figure and axis objects to plot on. \n", " \"\"\"\n", " fig, ax = plt.subplots(1)\n", " plt.tick_params(top=False, right=False, which='both') \n", " ax.spines['top'].set_visible(False)\n", " ax.spines['right'].set_visible(False)\n", " return fig, ax" ] }, { "cell_type": "code", "execution_count": null, "id": "40eaeca2-a3bd-476a-8666-95a3d5fb1620", "metadata": { "id": "40eaeca2-a3bd-476a-8666-95a3d5fb1620" }, "outputs": [], "source": [ "import nibabel as nib\n", "from matplotlib import transforms\n", "from scipy import ndimage\n", "import numpy as np\n", "\n", "# load data\n", "brain_full = nib.load('./sub-01_ses-01_7T_T1w_defaced.nii').get_fdata()\n", "brain = nib.load('./sub-01_ses-01_7T_T1w_defaced_brain.nii.gz').get_fdata()\n", "edges = nib.load('./sub-01_ses-01_7T_T1w_defaced_edges.nii').get_fdata()" ] }, { "cell_type": "code", "execution_count": null, "id": "baa81544-e316-4293-ad30-231dfdf60d29", "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 1000 }, "id": "baa81544-e316-4293-ad30-231dfdf60d29", "outputId": "bd35b551-1d7e-4779-fed1-129f7d1a84c3" }, "outputs": [], "source": [ "view_slices_3d(brain_full, slice_nbr=230, vmin=0, vmax=4000, title='Brain and Skull')\n", "view_slices_3d(brain, slice_nbr=230, vmin=0, vmax=4000, title='Brain Extracted')\n", "view_slices_3d(edges, slice_nbr=230, vmin=0, vmax=1000, title='Edges')" ] }, { "cell_type": "code", "execution_count": null, "id": "ea0da756-e384-4a79-88e6-9023b574593e", "metadata": { "scrolled": true, "tags": [] }, "outputs": [], "source": [ "from ipyniivue import AnyNiivue\n", "\n", "nv = AnyNiivue()\n", "nv.load_volumes([{\"path\": \"./sub-01_ses-01_7T_T1w_defaced_brain.nii.gz\"}])\n", "nv" ] }, { "cell_type": "markdown", "id": "071d46a9-635b-45f6-b36e-b849de23c3cc", "metadata": {}, "source": [ "SPM can also be used in such a workflow, but unfortunately, this will trigger a warning \"stty: 'standard input': Inappropriate ioctl for device\", which you can ignore (or help us to find out where it comes from):" ] }, { "cell_type": "code", "execution_count": null, "id": "ab80f10b-518a-42b1-b4e6-f14d6a41fd3b", "metadata": { "scrolled": true }, "outputs": [], "source": [ "import nipype.interfaces.spm as spm\n", "\n", "norm12 = spm.Normalize12()\n", "norm12.inputs.image_to_align = './sub-01_ses-01_7T_T1w_defaced.nii'\n", "norm12.run()" ] }, { "cell_type": "code", "execution_count": null, "id": "3c7c7b21-bc25-4e72-b9f9-821b5c2b2289", "metadata": {}, "outputs": [], "source": [ "brain_full = nib.load('./wsub-01_ses-01_7T_T1w_defaced.nii').get_fdata()\n", "view_slices_3d(brain_full, slice_nbr=50, vmin=0, vmax=4000, title='Brain normalized to MNI space')" ] }, { "cell_type": "code", "execution_count": null, "id": "a1364e2c-5424-4f47-af66-90147b536143", "metadata": {}, "outputs": [], "source": [ "nv.load_volumes([{\"path\": \"./wsub-01_ses-01_7T_T1w_defaced.nii\"}])\n", "nv" ] } ], "metadata": { "colab": { "include_colab_link": true, "provenance": [] }, "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.11.6" } }, "nbformat": 4, "nbformat_minor": 5 }