Skip to content

Commit

Permalink
How to run -> Readme, major edits
Browse files Browse the repository at this point in the history
  • Loading branch information
relyativist committed Sep 7, 2020
1 parent 746ac93 commit 275ecf1
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 12 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ This course is specifically aimed at MSc and PhD students with basic knowledge o
* Before seminar you are to install Docker https://www.docker.com/
* SEMINAR 1 (04.09) EEG analysis, Machine Learning in EEG
* SEMINAR 2 (04.09) MRI data analysis, sources, databases, tools
* Before seminar, please, do the following:
1) Download data from link https://drive.google.com/drive/folders/1P0ZhS1EoDY6fncnJb7foNFPjY5uoN6r0?usp=sharing
2) Clone repository to your local machine
3) Run docker locally and ensure it working with command `docker run hello-world`
4) In terminal: `cd to NEUROML/seminar2`
5) Type command `docker build -t neuroml/seminar2:0.0.1` and wait for successfull build
6) Run `docker run --rm -it -v /directory/to/downloaded/data:/workspace/data -p 8080:8080 neuroml/seminar2:0.0.1
7) Open browser (preferebly Chrome) -> localhost:8080
* Before the seminar you are to install FreeSurfer https://surfer.nmr.mgh.harvard.edu/
* SEMINAR 3 (04.09) Machine Learning for structural MRI data analysis
* Before the seminar you are to get an account and granted access here https://db.humanconnectome.org/data/projects/HCP_1200
Expand Down
31 changes: 28 additions & 3 deletions seminar2/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,30 @@ RUN apt-get update -qq \
grep \
less \
wget \
bzip2 \
make \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/*

ENV PATH="/opt/dcm2niix-latest/bin:$PATH"
RUN apt-get update -qq \
&& apt-get install -y -q --no-install-recommends \
cmake \
g++ \
gcc \
git \
make \
pigz \
zlib1g-dev \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/* \
&& git clone https://github.com/rordenlab/dcm2niix /tmp/dcm2niix \
&& mkdir /tmp/dcm2niix/build \
&& cd /tmp/dcm2niix/build \
&& cmake -DCMAKE_INSTALL_PREFIX:PATH=/opt/dcm2niix-latest .. \
&& make \
&& make install \
&& rm -rf /tmp/dcm2niix

ENV ANTSPATH="/opt/ants-2.2.0" \
PATH="/opt/ants-2.2.0:$PATH"
RUN echo "Downloading ANTs ..." \
Expand Down Expand Up @@ -102,6 +121,7 @@ RUN export PATH="/opt/miniconda-latest/bin:$PATH" \
'nilearn' \
'traits' \
'nipype' \
'seaborn' \
&& sync && conda clean --all && sync

RUN wget https://ftpmirror.gnu.org/parallel/parallel-20190222.tar.bz2 \
Expand All @@ -120,8 +140,6 @@ COPY ./entrypoint.sh /entrypoint.sh
RUN chmod +x /workspace/multiproc.sh \
/entrypoint.sh



RUN "/neurodocker/startup.sh"

#RUN "./${FREESURFER_HOME}/SetUpFreeSurfer.sh"
Expand All @@ -142,6 +160,13 @@ RUN echo '{ \
\n ] \
\n ], \
\n [ \
\n "dcm2niix", \
\n { \
\n "version": "latest", \
\n "method": "source" \
\n } \
\n ], \
\n [ \
\n "ants", \
\n { \
\n "version": "2.2.0" \
Expand Down
4 changes: 2 additions & 2 deletions seminar2/entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#! /bin/bash

printf 'Neurodocker startup - success'
printf 'Neurodocker startup - success\n'

source $FREESURFER_HOME/SetUpFreeSurfer.sh

printf 'FREESURFER - check'
printf 'FREESURFER - check\n'

./opt/miniconda-latest/envs/neuro/bin/jupyter-lab --ip=0.0.0.0 --port=8080 --no-browser --allow-root --notebook-dir='/workspace' --NotebookApp.token=''
57 changes: 55 additions & 2 deletions seminar2/seminar2_1_mri.ipynb
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"PLAN:\n",
"\n",
"1) MRI data formats\n",
"2) accessing data voxel and meta data\n",
"3) transformations\n",
"4) preprocessing\n",
"5) visualization\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
Expand Down Expand Up @@ -428,7 +443,45 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
"source": [
"*Voxel size* and *Pixel spacing* are important as we for preprocessing we can use only isomorphic voxel"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Assume that you recieved new dicom from hospital and you need to convert to convinient NIFTI, let's review the tool distinctive from simple on above:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!dcm2niix -o /workspace -f %i_%n_%p -z y /workspace/data/DICOM"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from nilearn.input_data import NiftiMasker\n",
"from nilearn import plotting\n",
"dcm_to_nii_convert = './1010_BRAIN_MR_T1_AX.nii.gz'\n",
"plot_convert = plotting.view_img(dcm_to_nii_convert,\n",
" bg_img=False,\n",
" cmap='gray',\n",
" symmetric_cmap=False,\n",
" threshold=0,\n",
" title='Drag mouse to go through slices')\n",
"plot_convert"
]
}
],
"metadata": {
Expand All @@ -452,4 +505,4 @@
},
"nbformat": 4,
"nbformat_minor": 4
}
}
106 changes: 105 additions & 1 deletion seminar2/seminar2_2_transform.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
"warnings.filterwarnings('ignore')\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"./assets/affine_1.png\">"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -88,7 +95,7 @@
"mni_space_template = nib.load('/workspace/data/mni_icbm152_t1_tal_nlin_sym_09a.nii') # Full template\n",
"\n",
"nilearn.plotting.plot_anat(nii_file,\n",
" title='Nifti T1 Native Space')\n",
" title='Nifti T1 Native Space', annotate=True)\n",
"\n",
"nilearn.plotting.plot_anat(mni_space_template,\n",
" title='Nifti MNI152 Space')\n",
Expand Down Expand Up @@ -154,6 +161,103 @@
"\"\"\".format(nii_file.shape, mni_space_template.shape))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can think of the image affine as a combination of a series of transformations to go from voxel coordinates to mm coordinates in terms of the magnet isocenter. Here is the EPI affine broken down into a series of transformations, with the results shown on the localizer image:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"./assets/affine_2.png\">"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from nibabel.affines import from_matvec, to_matvec, apply_affine\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"reflect = np.array([[1, 0, 0],\n",
" [0, 1, 0],\n",
" [0, 0, 1]])\n",
"\n",
"translate_affine = from_matvec(reflect, [0, 0, 0]) # reflect + shift 10 x direction\n",
"translate_affine"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"transformed = np.dot(nii_file.get_qform(), translate_affine)\n",
"#nii_file.affine = transformed\n",
"print(transformed)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"header_info = nii_file.header\n",
"vox_data = np.array(nii_file.get_fdata())\n",
"transformed_nii = nib.Nifti1Image(vox_data, transformed, header_info)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"tranfsorm_image = nilearn.plotting.plot_anat(transformed_nii,\n",
" title='Space displacement', annotate=True)\n",
"tranfsorm_image.add_overlay(nii_file, threshold=0.1)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"transform_image = nilearn.plotting.plot_anat(transformed_nii,\n",
" title='Space displacement', annotate=True)\n",
"transform_image.add_overlay(nii_file,threshold=0.8e3, colorbar=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"How about rotation?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cos_gamma = np.cos(0.3)\n",
"sin_gamma = np.sin(0.3)\n",
"rotation_affine = np.array([[1, 0, 0, 0],\n",
" [0, cos_gamma, -sin_gamma, 0],\n",
" [0, sin_gamma, cos_gamma, 0],\n",
" [0, 0, 0, 1]])"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
Loading

0 comments on commit 275ecf1

Please sign in to comment.