nibabel_001
NiBabel
Installation
Installation Testing
-
Testing as usual with
import nibabel;print(success);nibabel.test() -
import nibabel; print('Success!');nibabel.test()
Note: Before running advanced tests, please update all submodules of nibabel, by running git submodule update --init
I am running a simpple test, not the advanced one it ran for 15 minutes and gave me long listing. In the end it gave me error about Freesurfer other wise the rest test is passed.
-- Docs: https://docs.pytest.org/en/latest/warnings.html
=========================== short test summary info ============================
FAILED freesurfer/tests/test_io.py::test_geometry - assert False
FAILED freesurfer/tests/test_io.py::test_write_annot_fill_ctab - assert False
FAILED streamlines/tests/test_streamlines.py::TestLoadSave::test_save_complex_file
FAILED streamlines/tests/test_streamlines.py::TestLoadSave::test_save_tractogram_file
FAILED streamlines/tests/test_tck.py::TestTCK::test_load_file_with_wrong_information
FAILED streamlines/tests/test_trk.py::TestTRK::test_load_file_with_wrong_information
FAILED streamlines/tests/test_trk.py::TestTRK::test_load_trk_version_1 - Asse...
FAILED tests/test_deprecated.py::test_futurewarning_mixin - IndexError: pop f...
FAILED tests/test_nifti1.py::test_extension_io - assert 0 == 1
FAILED tests/test_parrec.py::test_truncated_load - assert 0 == 1
FAILED tests/test_parrec.py::test_truncations - assert 0 == 1
FAILED tests/test_parrec.py::test_ADC_map - assert 0 == 2
FAILED tests/test_testing.py::test_clear_and_catch_warnings - assert 1 == 2
= 13 failed, 4648 passed, 105 skipped, 6 xfailed, 1 warning in 829.54s (0:13:49) =
What is this pakcage ?
- It provides read write acees to commonly used Neuroimaging files which includes following format Description
| type | Description |
|---|---|
| Gifti | NifTI |
Start working
-
Import and use its exposed version
nibabel.(__version__) -
Reading a nifti file.
nibabel.load(<filename>) -
A file that is read is used in a variabel say
anat_img = nibabel.load('sample.nii.gz'). This object knows the fileshapeand image affine ( array matrix) shape. Another attribute isdataobjthat gives you the detail of where this object is pointing to. -
The ouput of
nibabelloaded object is an instance ofnibabel.nifti1.Nifti1Imagewhich get read in a memory. When it is printed it gives you the address as well for example0x7fdbdc86b2e0.. -
One can see that the super class is of
nibabel.nifti1. This class exposes number of ...file -
The
nibabelattributedataobjis an object that point to an image array that get loaded.,it is ofnibabel.arrayproxy.ArrayProxy ojbect. -
Array proxies and proxy imagesare the techniquesnibabeluses to load an image from disk, an arrayproxyis not the array itself but something that represents the array and can provide the array when we load it. It allows us to create the image object withou immediately loading all the array data from sik. -
Proxy is used rightly because images with an
proxy objectlike this one are calledproxy imagesbecause theimage datais the proxy points to the array data on disk. -
To check if it is a proxy,
nib.is_proxy(anat_img) -
Image shape and affine shape can be found out using
numpy objectand to do so you need to get an object that points to an image. -
And it is done using
img_data = anat_img.get_fdata()). This method returns a numpy array object. Itsshapeattribute will give the same result asnibiabelobject. -
Following is done and shown below
import nibable as nb
print(nb.__version__)
anat_img = nibabel.load('sample.nii.gz')
anat_img.shape # ()
anat_img.affine.sahpe #
anat_img.header #
file_data = anat_img.get_fdata()
file_data.shape
file_data.affine.shape ?
Commonly used functions
-
nibable.load()
-
nibable.shape # a loaded object represents the image that knows its shape.
-
nibable.affine.shape #
Working directory
- There is not need to have or set a working directory but it is better to set a data directroy do avoid platfrom specific details.
Nibabel Images and its image object
-
It is composed of 3 items:
-
an N-D array containing the image data;
- a (4, 4) affine matrix mapping array coordinates to coordinates in some
RAS+ world coordinate space (Coordinate systems and affines); - image metadata in the form of a header.
The word affine is used frequently in image transformation, subsquently the word
affine arrayrepresents an array that is accessed by another array in a loop. Another term is affine transformation like linear transformation and it is used to correct image transoformation in image related works.
Comments