"/Users/ndcn0236/miniconda3/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
" from ._conv import register_converters as _register_converters\n"
"Info: Time to color volume data is 0.581 seconds.\n",
"\n",
"\n",
"Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/T1w.nii.gz was 1.06257 seconds.\n",
"\n",
"\n",
"Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.L.inflated.32k_fs_LR.surf.gii was 0.111264 seconds.\n",
"\n",
"\n",
"Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.L.midthickness.32k_fs_LR.surf.gii was 0.019432 seconds.\n",
"\n",
"\n",
"Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.L.pial.32k_fs_LR.surf.gii was 0.019506 seconds.\n",
"\n",
"\n",
"Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.L.very_inflated.32k_fs_LR.surf.gii was 0.019134 seconds.\n",
"\n",
"\n",
"Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.L.white.32k_fs_LR.surf.gii was 0.018743 seconds.\n",
"\n",
"\n",
"Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.R.inflated.32k_fs_LR.surf.gii was 0.087767 seconds.\n",
"\n",
"\n",
"Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.R.midthickness.32k_fs_LR.surf.gii was 0.019677 seconds.\n",
"\n",
"\n",
"Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.R.pial.32k_fs_LR.surf.gii was 0.019088 seconds.\n",
"\n",
"\n",
"Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.R.very_inflated.32k_fs_LR.surf.gii was 0.019656 seconds.\n",
"\n",
"\n",
"Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.R.white.32k_fs_LR.surf.gii was 0.018397 seconds.\n",
"\n",
"\n",
"Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/labels.plabel.nii was 0.651135 seconds.\n",
/Users/ndcn0236/miniconda3/lib/python3.6/site-packages/h5py/__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
from ._conv import register_converters as _register_converters
%% Cell type:markdown id: tags:
# Nibabel: loading MRI volumes
## Image file formats beyond NIFTI
Nibabel can also read:
- ANALYZE (plain, SPM99, SPM2 and later)
- Freesurfer MGH/MGZ format
- MINC1 and MINC2
- limited support for [Dicom](http://nipy.org/nibabel/dicom/dicom.html#dicom)
- Philips PAR/REC
You can get the data and affine for all these formats using:
> ```
> img = nib.load(<filename>)
> affine = img.affine
> data = img.get_data() # repeated calls of get_data() will return a cached version
> ```
Other metadata is available through `img.header`, but will be format-specific.
## Acessing part of the data
Running `nibabel.load` will only read in the header, not the full data.
You can exploit this using the `[dataobj](http://nipy.org/nibabel/nibabel_images.html#array-proxies-and-proxy-images)` object:
%% Cell type:code id: tags:
```python
```
img = nib.load('100307/T1w.nii.gz')
zslice = img.dataobj[:, :, 50] # only loads the slice with k=50
plt.imshow(slice.T, cmap='gray')
```
%% Cell type:markdown id: tags:
## Surfaces in nibabel
Nibabel supports surfaces in both [Freesurfer](http://nipy.org/nibabel/reference/nibabel.freesurfer.html#module-nibabel.freesurfer)
and [GIFTI](http://nipy.org/nibabel/reference/nibabel.gifti.html#module-nibabel.gifti) formats
Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/T1w.nii.gz was 1.06257 seconds.
Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.L.inflated.32k_fs_LR.surf.gii was 0.111264 seconds.
Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.L.midthickness.32k_fs_LR.surf.gii was 0.019432 seconds.
Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.L.pial.32k_fs_LR.surf.gii was 0.019506 seconds.
Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.L.very_inflated.32k_fs_LR.surf.gii was 0.019134 seconds.
Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.L.white.32k_fs_LR.surf.gii was 0.018743 seconds.
Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.R.inflated.32k_fs_LR.surf.gii was 0.087767 seconds.
Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.R.midthickness.32k_fs_LR.surf.gii was 0.019677 seconds.
Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.R.pial.32k_fs_LR.surf.gii was 0.019088 seconds.
Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.R.very_inflated.32k_fs_LR.surf.gii was 0.019656 seconds.
Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/100307/fsaverage_LR32k/100307.R.white.32k_fs_LR.surf.gii was 0.018397 seconds.
Info: Time to read /Users/ndcn0236/Work/projects/pytreat-2018-practicals/talks/nibabel_cifti/labels.plabel.nii was 0.651135 seconds.
Most of these packages have or are in thr progress of dropping support for python2.
So use python3!
## [Numpy](http://www.numpy.org/): arrays
This is the main library underlying (nearly) all of the scientific python ecosystem.
See the tutorial in the beginner session or [the official numpy tutorial](https://docs.scipy.org/doc/numpy-dev/user/quickstart.html) for usage details.
The usual nickname of numpy is np:
%% Cell type:code id: tags:
```
import numpy as np
```
%% Cell type:markdown id: tags:
Numpy includes support for:
- N-dimensional arrays with various datatypes
- basic functions (e.g., polynomials)
- masked arrays
- matrices
- structured/record array
- basic functions (e.g., sin, log, arctan, polynomials)
- basic linear algebra
- random number generation
## [Scipy](https://scipy.org/scipylib/index.html): most general scientific tools
At the top level this module includes all of the basic functionality from numpy.
You could import this as, but you might as well import numpy directly.
%% Cell type:code id: tags:
```
import scipy as sp
```
%% Cell type:markdown id: tags:
The main strength in scipy lies in its sub-packages: