Skip to content

Instantly share code, notes, and snippets.

@miykael
Created October 21, 2018 20:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miykael/68cc03b6cd96b1ecef648e0f9a9ef95c to your computer and use it in GitHub Desktop.
Save miykael/68cc03b6cd96b1ecef648e0f9a9ef95c to your computer and use it in GitHub Desktop.
SPM12's SliceTiming sets TR value in header to zero
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Load dataset and verify TR value in header"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [],
"source": [
"import nibabel as nb\n",
"from nilearn.datasets import fetch_adhd"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/home/line/nilearn_data/adhd/data/0010042/0010042_rest_tshift_RPI_voreg_mni.nii.gz'"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"filename = fetch_adhd(n_subjects=1)['func'][0]\n",
"filename"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We need to unzip the file to make it accessible to SPM."
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/home/line/nilearn_data/adhd/data/0010042/0010042_rest_tshift_RPI_voreg_mni.nii'"
]
},
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"!gunzip $filename\n",
"filename = filename.replace('.gz', '')\n",
"filename"
]
},
{
"cell_type": "code",
"execution_count": 38,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3.0, 3.0, 3.0, 2.0)"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Load image and show spatial and temporal resolution\n",
"img = nb.load(filename)\n",
"img.header.get_zooms()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here we see clearly that the TR of the functional image is 2.0 seconds."
]
},
{
"cell_type": "code",
"execution_count": 39,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(61, 73, 61, 176)"
]
},
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# And how many slices does the image have?\n",
"img.header.get_data_shape()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Perform SPM12's SliceTiming correction\n",
"\n",
"We will use Nipype to run SPM's SliceTiming correction, but the underlying execution is the same and happens also if one runs SPM as a GUI.\n",
"\n",
"For this case we assume that the data was recorded in ascending order."
]
},
{
"cell_type": "code",
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"from nipype.interfaces.spm import SliceTiming"
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
"st = SliceTiming()\n",
"st.inputs.in_files = filename\n",
"st.inputs.num_slices = 61\n",
"st.inputs.time_repetition = 2.0\n",
"st.inputs.time_acquisition = 2. - 2./61.\n",
"st.inputs.slice_order = list(range(61))\n",
"st.inputs.ref_slice = int(61/2)\n",
"res = st.run()"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'/home/line/nilearn_data/adhd/data/0010042/a0010042_rest_tshift_RPI_voreg_mni.nii'"
]
},
"execution_count": 45,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"output = res.outputs.timecorrected_files\n",
"output"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And if we now again investigate the TR in the NIfTI header, we get a value of `0.0`."
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(3.0, 3.0, 3.0, 0.0)"
]
},
"execution_count": 48,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"nb.load(output).header.get_zooms()"
]
}
],
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment