diff --git a/getting_started/05_nifti.ipynb b/getting_started/05_nifti.ipynb index 85b1c32fcd6cd5937234c6ac0cbd6dde5a93b057..9d8551e0970ce313c6801143e06d5b4859db3b2d 100644 --- a/getting_started/05_nifti.ipynb +++ b/getting_started/05_nifti.ipynb @@ -12,10 +12,10 @@ "\n", "\n", "Building upon `nibabel`, the\n", - "[`fsl.data`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.html#module-fsl.data)\n", - "package contains a number of FSL-specific classes and functions which you may\n", - "find useful. This is covered in a different practical\n", - "(`advanced_topics/08_fslpy.ipynb`).\n", + "[`fslpy`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/) library\n", + "contains a number of FSL-specific classes and functions which you may find\n", + "useful. But let's start with `nibabel` - `fslpy` is introduced in a different\n", + "practical (`advanced_topics/08_fslpy.ipynb`).\n", "\n", "\n", "## Contents\n", @@ -50,8 +50,8 @@ "# display header object\n", "imhdr = imobj.header\n", "\n", - "# extract data (as an numpy array)\n", - "imdat = imobj.get_data().astype(float)\n", + "# extract data (as a numpy array)\n", + "imdat = imobj.get_fdata()\n", "print(imdat.shape)" ] }, @@ -59,26 +59,28 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "> Make sure you use the full filename, including the .nii.gz extension.\n", - "\n", + "> Make sure you use the full filename, including the `.nii.gz` extension.\n", + "> `fslpy` provides FSL-like automatic file suffix detection though.\n", "\n", - "> We use the expandvars() function above to insert the FSLDIR\n", + "> We use the `expandvars()` function above to insert the FSLDIR\n", "> environmental variable into our string. This function is\n", "> discussed more fully in the file management practical.\n", "\n", - "Reading the data off the disk is not done until `get_data()` is called.\n", + "Reading the data off the disk is not done until `get_fdata()` is called.\n", "\n", "> Pitfall:\n", ">\n", - "> The option `mmap=False`is necessary as turns off memory mapping,\n", - "> which otherwise would be invoked for uncompressed NIfTI files but not for\n", - "> compressed files. Since some functionality behaves differently on memory\n", - "> mapped objects, it is advisable to turn this off.\n", + "> The option `mmap=False` disables memory mapping, which would otherwise be\n", + "> invoked for uncompressed NIfTI files but not for compressed files. Since\n", + "> some functionality behaves differently on memory mapped objects, it is\n", + "> advisable to turn this off unless you specifically want it.\n", "\n", "Once the data is read into a numpy array then it is easily manipulated.\n", "\n", - "> We recommend converting it to float at the start to avoid problems with\n", - "> integer arithmetic and overflow, though this is not compulsory.\n", + "> The `get_fdata` method will return floating point data, regardless of the\n", + "> underlying image data type. If you want the image data in the type that it\n", + "> is stored (e.g. integer ROI labels), then use\n", + "> `imdat = np.asanyarray(imobj.dataobj)` instead.\n", "\n", "---\n", "\n", @@ -155,6 +157,7 @@ "<a class=\"anchor\" id=\"writing-images\"></a>\n", "## Writing images\n", "\n", + "\n", "If you have created a modified image by making or modifying a numpy array then\n", "you need to put this into a NIfTI image object in order to save it to a file.\n", "The easiest way to do this is to copy all the header info from an existing\n", @@ -190,8 +193,9 @@ "> creating an entirely separate image, like a simulation.\n", "\n", "If the voxel size of the image is different, then extra modifications will be\n", - "required. Take a look at the `fslpy` practical for more advanced image\n", - "manipulation options (`advanced_topics/08_fslpy.ipynb`).\n", + "required. Take a look at the `fslpy` practical for some extra image\n", + "manipulation options, including cropping and resampling\n", + "(`advanced_topics/08_fslpy.ipynb`).\n", "\n", "---\n", "\n", @@ -202,7 +206,10 @@ "\n", "Write some code to read in a 4D fMRI image (you can find one\n", "[here](http://www.fmrib.ox.ac.uk/~mark/files/av.nii.gz) if you don't have one\n", - "handy), calculate the tSNR and then save the 3D result." + "handy), calculate the tSNR and then save the 3D result.\n", + "\n", + "> The tSNR of a time series signal is simply its mean divided by its standard\n", + "> deviation." ] }, { diff --git a/getting_started/05_nifti.md b/getting_started/05_nifti.md index f20063e3b7d66ec3eb0d628ee5751a7836d692c0..67139c538b3f79da2dd24057f8e82acaae4cebe7 100644 --- a/getting_started/05_nifti.md +++ b/getting_started/05_nifti.md @@ -6,10 +6,10 @@ MINC, MGH). `nibabel` is included within the FSL python environment. Building upon `nibabel`, the -[`fsl.data`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/fsl.data.html#module-fsl.data) -package contains a number of FSL-specific classes and functions which you may -find useful. This is covered in a different practical -(`advanced_topics/08_fslpy.ipynb`). +[`fslpy`](https://users.fmrib.ox.ac.uk/~paulmc/fsleyes/fslpy/latest/) library +contains a number of FSL-specific classes and functions which you may find +useful. But let's start with `nibabel` - `fslpy` is introduced in a different +practical (`advanced_topics/08_fslpy.ipynb`). ## Contents @@ -38,31 +38,33 @@ imobj = nib.load(filename, mmap=False) # display header object imhdr = imobj.header -# extract data (as an numpy array) -imdat = imobj.get_data().astype(float) +# extract data (as a numpy array) +imdat = imobj.get_fdata() print(imdat.shape) ``` -> Make sure you use the full filename, including the .nii.gz extension. - +> Make sure you use the full filename, including the `.nii.gz` extension. +> `fslpy` provides FSL-like automatic file suffix detection though. -> We use the expandvars() function above to insert the FSLDIR +> We use the `expandvars()` function above to insert the FSLDIR > environmental variable into our string. This function is > discussed more fully in the file management practical. -Reading the data off the disk is not done until `get_data()` is called. +Reading the data off the disk is not done until `get_fdata()` is called. > Pitfall: > -> The option `mmap=False`is necessary as turns off memory mapping, -> which otherwise would be invoked for uncompressed NIfTI files but not for -> compressed files. Since some functionality behaves differently on memory -> mapped objects, it is advisable to turn this off. +> The option `mmap=False` disables memory mapping, which would otherwise be +> invoked for uncompressed NIfTI files but not for compressed files. Since +> some functionality behaves differently on memory mapped objects, it is +> advisable to turn this off unless you specifically want it. Once the data is read into a numpy array then it is easily manipulated. -> We recommend converting it to float at the start to avoid problems with -> integer arithmetic and overflow, though this is not compulsory. +> The `get_fdata` method will return floating point data, regardless of the +> underlying image data type. If you want the image data in the type that it +> is stored (e.g. integer ROI labels), then use +> `imdat = np.asanyarray(imobj.dataobj)` instead. --- @@ -109,6 +111,7 @@ print(affine, code) <a class="anchor" id="writing-images"></a> ## Writing images + If you have created a modified image by making or modifying a numpy array then you need to put this into a NIfTI image object in order to save it to a file. The easiest way to do this is to copy all the header info from an existing @@ -134,8 +137,9 @@ where `newdata` is the numpy array (the above is a random example only) and > creating an entirely separate image, like a simulation. If the voxel size of the image is different, then extra modifications will be -required. Take a look at the `fslpy` practical for more advanced image -manipulation options (`advanced_topics/08_fslpy.ipynb`). +required. Take a look at the `fslpy` practical for some extra image +manipulation options, including cropping and resampling +(`advanced_topics/08_fslpy.ipynb`). --- @@ -148,6 +152,9 @@ Write some code to read in a 4D fMRI image (you can find one [here](http://www.fmrib.ox.ac.uk/~mark/files/av.nii.gz) if you don't have one handy), calculate the tSNR and then save the 3D result. +> The tSNR of a time series signal is simply its mean divided by its standard +> deviation. + ``` # Calculate tSNR ```