@@ -36,6 +36,7 @@ A page should open in your web browser - to access the practicals, navigate
into one of the `getting_started`, `advanced_programming`, or `applications`
directories, and click on the `.ipynb` file you are interested in.
Some practical sub-directories will also contain a `README.md` file with additional information specific to the practical. Make sure you read this as well **before running the notebooks**.
"> 1. Make sure you have the necessary software dependencies (see the [readme](README.md))\n",
"\n",
"For group ICA, `melodic` uses multi-session temporal concatenation. This will perform a single 2D ICA run on the concatenated data matrix (obtained by stacking all 2D data matrices of every single data set on top of each other).\n",
"\n",
"\n",
...
...
@@ -24,7 +27,7 @@
"\n",
"## This notebook\n",
"\n",
"This notebook will download an open fMRI dataset (~50MB) for use in the MIGP demo, regresses confounds from the data, performs spatial smoothing with 10mm FWHM, and then runs group `melodic` with `MIGP`.\n",
"This notebook will download an open fMRI dataset (~50MB) for use in the MIGP demo, regress confounds from the data, perform spatial smoothing with 10mm FWHM, and then run group `melodic` with `MIGP`.\n",
"\n",
"* [Fetch the data](#download-the-data)\n",
"* [Clean the data](#clean-the-data)\n",
...
...
@@ -62,9 +65,7 @@
"\n",
"It comprises 10 preprocessed resting-state fMRI selected from 72 patients diagnosed with schizophrenia and 74 healthy controls (6mm isotropic, TR=2s, 150 volumes).\n",
"\n",
"Create a directory in the users home directory to store the downloaded data:\n",
"\n",
"> **NOTE:** [`expanduser`](https://docs.python.org/3.7/library/os.path.html#os.path.expanduser) will expand the `~` to the be users home directory:"
"Create a directory in the current directory to store the downloaded data:"
]
},
{
...
...
@@ -73,7 +74,7 @@
"metadata": {},
"outputs": [],
"source": [
"data_dir = op.expanduser('~/nilearn_data')\n",
"data_dir = './nilearn_data'\n",
"\n",
"if not op.exists(data_dir):\n",
" os.makedirs(data_dir)"
...
...
@@ -85,7 +86,9 @@
"source": [
"Download the data (if not already downloaded):\n",
"\n",
"> **Note:** We use a method from [`nilearn`](https://nilearn.github.io/index.html) called [`fetch_cobre`](https://nilearn.github.io/modules/generated/nilearn.datasets.fetch_cobre.html) to download the fMRI data"
"> **Note:** \n",
"> 1. We use a method from [`nilearn`](https://nilearn.github.io/index.html) called [`fetch_cobre`](https://nilearn.github.io/modules/generated/nilearn.datasets.fetch_cobre.html) to download the fMRI data.\n",
"> 2. `fetch_cobre` has been deprecated in recent versions of `nilearn`. It will soon be replaced here in there near future. In the interim, the deprecation warning has been suppressed with `filterwarnings`."
]
},
{
...
...
@@ -94,6 +97,11 @@
"metadata": {},
"outputs": [],
"source": [
"# suppress deprecation warning\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\")\n",
"\n",
"# download dataset\n",
"d = datasets.fetch_cobre(data_dir=data_dir) "
]
},
...
...
%% Cell type:markdown id: tags:
# MIGP
> **Before you run this notebook**:
> 1. Make sure you have the necessary software dependencies (see the [readme](README.md))
For group ICA, `melodic` uses multi-session temporal concatenation. This will perform a single 2D ICA run on the concatenated data matrix (obtained by stacking all 2D data matrices of every single data set on top of each other).

Resulting in **high dimension** datasets!
Furthermore, with ICA we are typically only interested in a comparitively low dimension decomposition so that we can capture spatially extended networks.
Therefore the first step is to reduce the dimensionality of the data. This can be achieved in a number of ways, but `melodic`, by default, uses `MIGP`.
> MIGP is an incremental approach that aims to provide a very close approximation to full temporal concatenation followed by PCA, but without the large memory requirements *(Smith et al., 2014)*.
Essentially, MIGP stacks the datasets incrementally in the temporal dimension, and whenever the temporal dimension exceeds a specified size, a PCA-based temporal reduction is performed.
> MIGP does not increase at all in memory requirement with increasing numbers of subjects, no large matrices are ever formed, and the computation time scales linearly with the number of subjects. It is easily parallelisable, simply by applying the approach in parallel to subsets of subjects, and then combining across these with the same “concatenate and reduce” approach described above *(Smith et al., 2014)*.
## This notebook
This notebook will download an open fMRI dataset (~50MB) for use in the MIGP demo, regresses confounds from the data, performs spatial smoothing with 10mm FWHM, and then runs group `melodic` with `MIGP`.
This notebook will download an open fMRI dataset (~50MB) for use in the MIGP demo, regress confounds from the data, perform spatial smoothing with 10mm FWHM, and then run group `melodic` with `MIGP`.
*[Fetch the data](#download-the-data)
*[Clean the data](#clean-the-data)
*[Run `melodic`](#run-melodic)
*[Plot group ICs](#plot-group-ics)
Firstly we will import the necessary packages for this notebook:
%% Cell type:code id: tags:
``` python
fromnilearnimportdatasets
fromnilearnimportimage
fromnilearnimportplotting
importnibabelasnb
importnumpyasnp
importos.pathasop
importos
importglob
importmatplotlib.pyplotasplt
```
%% Cell type:markdown id: tags:
<aclass="anchor"id="download-the-data"></a>
## Fetch the data
This data is a derivative from the [COBRE](http://fcon_1000.projects.nitrc.org/indi/retro/cobre.html) sample found in the International Neuroimaging Data-sharing Initiative, originally released under Creative Commons - Attribution Non-Commercial.
It comprises 10 preprocessed resting-state fMRI selected from 72 patients diagnosed with schizophrenia and 74 healthy controls (6mm isotropic, TR=2s, 150 volumes).
Create a directory in the users home directory to store the downloaded data:
> **NOTE:** [`expanduser`](https://docs.python.org/3.7/library/os.path.html#os.path.expanduser) will expand the `~` to the be users home directory:
Create a directory in the current directory to store the downloaded data:
%% Cell type:code id: tags:
``` python
data_dir=op.expanduser('~/nilearn_data')
data_dir='./nilearn_data'
ifnotop.exists(data_dir):
os.makedirs(data_dir)
```
%% Cell type:markdown id: tags:
Download the data (if not already downloaded):
> **Note:** We use a method from [`nilearn`](https://nilearn.github.io/index.html) called [`fetch_cobre`](https://nilearn.github.io/modules/generated/nilearn.datasets.fetch_cobre.html) to download the fMRI data
> **Note:**
> 1. We use a method from [`nilearn`](https://nilearn.github.io/index.html) called [`fetch_cobre`](https://nilearn.github.io/modules/generated/nilearn.datasets.fetch_cobre.html) to download the fMRI data.
> 2. `fetch_cobre` has been deprecated in recent versions of `nilearn`. It will soon be replaced here in there near future. In the interim, the deprecation warning has been suppressed with `filterwarnings`.
%% Cell type:code id: tags:
``` python
# suppress deprecation warning
importwarnings
warnings.filterwarnings("ignore")
# download dataset
d=datasets.fetch_cobre(data_dir=data_dir)
```
%% Cell type:markdown id: tags:
<aclass="anchor"id="clean-the-data"></a>
## Clean the data
Regress confounds from the data and to spatially smooth the data with a gaussian filter of 10mm FWHM.
> **Note:**
> 1. We use [`clean_img`](https://nilearn.github.io/modules/generated/nilearn.image.clean_img.html) from the [`nilearn`](https://nilearn.github.io/index.html) package to regress confounds from the data
> 2. We use [`smooth_img`](https://nilearn.github.io/modules/generated/nilearn.image.smooth_img.html) from the [`nilearn`](https://nilearn.github.io/index.html) package to spatially smooth the data
> 3. [`zip`](https://docs.python.org/3.3/library/functions.html#zip) takes iterables and aggregates them in a tuple. Here it is used to iterate through four lists simultaneously
> 4. We use list comprehension to loop through all the filenames and append suffixes
%% Cell type:code id: tags:
``` python
# Create a list of filenames for cleaned and smoothed data
To run ```melodic``` we will need a brain mask in MNI152 space at the same resolution as the fMRI.
> **Note:**
> 1. We use [`load_mni152_brain_mask`](https://nilearn.github.io/modules/generated/nilearn.datasets.load_mni152_brain_mask.html) from the [`nilearn`](https://nilearn.github.io/index.html) package to load the MNI152 mask
> 2. We use [`resample_to_img`](https://nilearn.github.io/modules/generated/nilearn.image.resample_to_img.html) from the [`nilearn`](https://nilearn.github.io/index.html) package to resample the mask to the resolution of the fMRI
> 3. We use [`math_img`](https://nilearn.github.io/modules/generated/nilearn.image.math_img.html) from the [`nilearn`](https://nilearn.github.io/index.html) package to binarize the resample mask
> 4. The mask is plotted using [`plot_anat`](https://nilearn.github.io/modules/generated/nilearn.plotting.plot_anat.html) from the [`nilearn`](https://nilearn.github.io/index.html) package
Generate a command line string and run group ```melodic``` on the smoothed fMRI with a dimension of 10 components:
> **Note**:
> 1. Here we use python [f-strings](https://www.python.org/dev/peps/pep-0498/), formally known as literal string interpolation, which allow for easy formatting
> 2. [`op.join`](https://docs.python.org/3.7/library/os.path.html#os.path.join) will join path strings using the platform-specific directory separator
> 3. [`','.join(smooth)`](https://docs.python.org/3/library/stdtypes.html#str.join) will create a comma seprated string of all the items in the list `smooth`
> 1. Here we use the `!` operator to execute the command in the shell
> 2. The `{}` will expand the contained python variable in the shell
%% Cell type:code id: tags:
``` python
# run melodic
!{melodic_cmd}
```
%% Cell type:markdown id: tags:
<aclass="anchor"id="plot-group-ics"></a>
### Plot group ICs
Now we can load and plot the group ICs generated by ```melodic```.
This function will be used to plot ICs:
> **NOTE:**
> 1. Here we use [`plot_stat_map`](https://nilearn.github.io/modules/generated/nilearn.plotting.plot_stat_map.html) from the [`nilearn`](https://nilearn.github.io/index.html) package to plot the orthographic images
> 2. [`subplots`](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots.html) from `matplotlib.pyplot` creates a figure and multiple subplots
> 3. [`find_xyz_cut_coords`](https://nilearn.github.io/modules/generated/nilearn.plotting.find_xyz_cut_coords.html) from the [`nilearn`](https://nilearn.github.io/index.html) package will find the image coordinates of the center of the largest activation connected component
> 4. [`zip`](https://docs.python.org/3.3/library/functions.html#zip) takes iterables and aggregates them in a tuple. Here it is used to iterate through two lists simultaneously
> 5. [`iter_img`](https://nilearn.github.io/modules/generated/nilearn.image.iter_img.html) from the [`nilearn`](https://nilearn.github.io/index.html) package creates an iterator from an image that steps through each volume/time-point of the image
1.`MIGP.ipynb` will introduce MIGP, download an open fMRI dataset for use in the MIGP demo, regress confounds from the data, perform spatial smoothing, and then run group melodic ICA.
2.`matlab_MIGP.ipynb` will perform Matlab MIGP dimension reduction, run group ICA, and then plot the group ICs.
3.`python_MIGP.ipynb` will perform python MIGP dimension reduction, run group ICA, and then plot the group ICs.
The `MIGP.ipynb` notebook will need to be run first to ensure that the necessary data is downloaded and setup.
## Running the notebooks
These notebooks can be run in the `fslpython` environment using:
```
fslpython -m notebook
```
## Dependencies
These notebooks require both `FSL` and `Matlab` to be installed.
### FSL
In addition to FSL you will need to install the python package `nilearn` into the `fslpython` environment with the following command:
> **Note**: `sudo` is required if FSL is installed in an admin/root protected location.
In addition to Matlab you will need to know the location of the Matlab executable/binary. Common matlab paths are below, but they may vary on your system:
"The code for Matlab MIGP is in the `demo_MIGP_NIFTI.m` m-file in the current directory.\n",
"\n",
"We are going to run Matlab MIGP from the command line using the `-batch` option. You will need to know the path to the Matlab executable (see the [readme](README.md))\n",
"\n",
"> **IMPORTANT:** Make sure you change the `MATLAB_EXE` string to be appropriate for your system.\n",
"\n",
"> **Note**: \n",
"> 1. Here we use python [f-strings](https://www.python.org/dev/peps/pep-0498/), formally known as literal string interpolation, which allow for easy formatting\n",
"> 2. Here we use the `!` operator to execute the command in the shell\n",
"> 3. The `{}` will expand the contained python variable in the shell"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Generate Matlab MIGP command line string\n",
"\n",
"# Change the MATLAB_EXE to point to the Matlab executable on your system \n",
"> **Note**: [`expanduser`](https://docs.python.org/3.7/library/os.path.html#os.path.expanduser) will expand the `~` to the be users home directory"
"! echo {CMD}"
]
},
{
...
...
@@ -43,7 +90,8 @@
"metadata": {},
"outputs": [],
"source": [
"data_dir = op.expanduser('~/nilearn_data')"
"# Run Matlab command line string\n",
"! {CMD}"
]
},
{
...
...
%% Cell type:markdown id: tags:
## Matlab MIGP
This notebook will load the dimension reduced data from Matlab MIGP, run group ICA, and then plot the group ICs.
> **Before you run this notebook**:
> 1. Make sure you have the necessary software dependencies (see the [readme](README.md))
> 2. You must run [MIGP.ipynb](MIGP.ipynb) to ensure that the necessary data is downloaded and setup
This notebook will perform *Matlab* MIGP dimension reduction, run group ICA, and then plot the group ICs.
*[Run python `MIGP`](#run-matlab-migp)
*[Run `melodic`](#run-matlab-melodic)
*[Plot group ICs](#plot-matlab-group-ics)
Firstly we will import the necessary packages for this notebook:
%% Cell type:code id: tags:
``` python
fromnilearnimportplotting
fromnilearnimportimage
importnibabelasnb
importmatplotlib.pyplotasplt
importnumpyasnp
importos.pathasop
```
%% Cell type:markdown id: tags:
It will be necessary to know the location where the data was stored so that we can load the brainmask:
It will be necessary to know the location where the data was stored in [MIGP.ipynb](MIGP.ipynb).
%% Cell type:code id: tags:
``` python
data_dir='./nilearn_data'
```
%% Cell type:markdown id: tags:
<aclass="anchor"id="run-matlab-migp"></a>
### Run Matlab `MIGP`
The code for Matlab MIGP is in the `demo_MIGP_NIFTI.m` m-file in the current directory.
We are going to run Matlab MIGP from the command line using the `-batch` option. You will need to know the path to the Matlab executable (see the [readme](README.md))
> **Note**: [`expanduser`](https://docs.python.org/3.7/library/os.path.html#os.path.expanduser) will expand the `~` to the be users home directory
> **IMPORTANT:** Make sure you change the `MATLAB_EXE` string to be appropriate for your system.
> **Note**:
> 1. Here we use python [f-strings](https://www.python.org/dev/peps/pep-0498/), formally known as literal string interpolation, which allow for easy formatting
> 2. Here we use the `!` operator to execute the command in the shell
> 3. The `{}` will expand the contained python variable in the shell
%% Cell type:code id: tags:
``` python
# Generate Matlab MIGP command line string
# Change the MATLAB_EXE to point to the Matlab executable on your system
Generate a command line string and run group ```melodic``` on the Matlab MIGP dimension reduced data with a dimension of 10 components. We disable MIGP because it was already run separately in Matlab.
> **Note**:
> 1. Here we use python [f-strings](https://www.python.org/dev/peps/pep-0498/), formally known as literal string interpolation, which allow for easy formatting
> 2. [`op.join`](https://docs.python.org/3.7/library/os.path.html#os.path.join) will join path strings using the platform-specific directory separator
> 1. Here we use the `!` operator to execute the command in the shell
> 2. The `{}` will expand the contained python variable in the shell
%% Cell type:code id: tags:
``` python
# run melodic
!{melodic_cmd}
```
%% Cell type:markdown id: tags:
<aclass="anchor"id="plot-matlab-group-ics"></a>
### Plot group ICs
Now we can load and plot the group ICs generated by ```melodic```.
This function will be used to plot ICs:
> **NOTE:**
> 1. Here we use [`plot_stat_map`](https://nilearn.github.io/modules/generated/nilearn.plotting.plot_stat_map.html) from the [`nilearn`](https://nilearn.github.io/index.html) package to plot the orthographic images
> 2. [`subplots`](https://matplotlib.org/api/_as_gen/matplotlib.pyplot.subplots.html) from `matplotlib.pyplot` creates a figure and multiple subplots
> 3. [`find_xyz_cut_coords`](https://nilearn.github.io/modules/generated/nilearn.plotting.find_xyz_cut_coords.html) from the [`nilearn`](https://nilearn.github.io/index.html) package will find the image coordinates of the center of the largest activation connected component
> 4. [`zip`](https://docs.python.org/3.3/library/functions.html#zip) takes iterables and aggregates them in a tuple. Here it is used to iterate through two lists simultaneously
> 5. [`iter_img`](https://nilearn.github.io/modules/generated/nilearn.image.iter_img.html) from the [`nilearn`](https://nilearn.github.io/index.html) package creates an iterator from an image that steps through each volume/time-point of the image