Skip to content

Examples

Sebastiano Ferraris edited this page Aug 14, 2017 · 24 revisions

Conversion Examples

A dataset of MR Bruker images where to test the code can be found here (thanks to Mikaël Naveau).

In the following lines we will convert the images accessing bruker2nifti via

  • Python command shell,
  • Command Line Utility (CLI) to integrate it in a bash script,
  • Graphical User Interface (GUI).

All you need:

  • The dataset provided downloaded in a <data_folder>.
  • A python release with bruker2nifti installed.
  • A <destination_folder> where you want to store the converted data.

Converting via Python command shell

Start a Python session, import os and import the class converter of bruker2nifti:

>>> import os
>>> from bruker2nifti.converter import Bruker2Nifti

Save the folder link to the Bruker study and where you want the study to be converted to NifTi:

>>> pfo_study_in = os.path.join(<data_folder>, 'raw', 'McGill_Orientation', 'a20130329_APM_DEV_Orient.j71')
>>> pfo_study_out = <destination_folder>

Instantiate a converter. If you do not want to keep the input study name stored in the visu_pars parameter file of each experiment (or scan, see this wiki-page for nomenclatures), and you rather prefer to change it to my_study you can change it there with the optional input variable study_name:

>>> bru = Bruker2Nifti(pfo_study_in, pfo_study_out, study_name='my_study')

Access the attributes of the instance bru to change the conversion options (the one shown here are the default one):

>>> bru.verbose = 1
>>> bru.correct_slope = True
>>> bru.get_acqp = False
>>> bru.get_method = False
>>> bru.get_reco = False
>>> bru.nifti_version = 1
>>> bru.qform_code = 1
>>> bru.sform_code = 2
>>> bru.save_human_readable = True
>>> bru.save_b0_if_dwi = True

Where verbose is the amount of output that will be visualised at conversion time (0 for minimal output, 1 for a fair amount, 2 for extra amount for debugging).

If the attribute correct_slope is True, the grayscale intensity of each image will be corrected for the slope parameter stored in visu_pars (see this wiki-page). By default the slope correction factor is saved in an external file to make easier applying the slope correction after the conversion.

All the data to fill the header of a nifti image should be in the Bruker parameter file visu_pars. Nonetheless the user convert in a readable and Python-editable format also the Bruker parameter files called acqp, reco and method (see ParaVision manuals for further informations), setting the attributes get_acqp, get_method and get_reco to True.

There are two NifTi version: NifTi-1 and NifTi-2. The user can chose the output version changing the attribute of the attribute nifti_version.

Q-form and S-form of the output NifTi can be changed via the class attributes qform_code and sform_code.

Bruker parameter files are stored as dictionary in external files with the nifti image. To have also a human readable format where all the parameter are stored in alphabetical order in a .txt file, the attribute save_human_readable must stay True.

Converted images can be very large, in particular when DWI and slope corrected. If the scan is a DWI, by default also the first time-point (first four-dimensional point) can be saved in another image. To turn off this behaviour set the attribute save_b0_if_dwi to False

To check that the list of scans and the scans names automatically selected filled makes some sense, you can ask them before the conversion with:

>>> print(bru.scans_list)
>>> print(bru.list_new_name_each_scan)
>>> print(bru.list_new_nifti_file_names)

Finally you can call the conversion method with

bru.convert()

All in one Python module:

import os

from bruker2nifti.converter import Bruker2Nifti

if __name__ == '__main__':
    root_dir = '/path/to/bruker2nifti/repo'
    pfo_study_in = os.path.join(<data_folder>, 'raw', 'McGill_Orientation', 'a20130329_APM_DEV_Orient.j71')
    pfo_study_out = <destination_folder>

    # instantiate a converter
    bru = Bruker2Nifti(pfo_study_in, pfo_study_out, study_name='my_study')
    # select the options (attributes) you may want to change - the one shown below are the default one:
    bru.verbose = 2
    bru.correct_slope = False
    bru.get_acqp = False
    bru.get_method = False
    bru.get_reco = False
    bru.nifti_version = 1
    bru.qform_code = 1
    bru.sform_code = 2
    bru.save_human_readable = True
    bru.save_b0_if_dwi = True
    # Check that the list of scans and the scans names automatically selected makes some sense:
    print(bru.scans_list)
    print(bru.list_new_name_each_scan)
    print(bru.list_new_nifti_file_names)
    # call the function convert, to convert the study:
    bru.convert()

Converting via Command Line Utility

Type

bruker2nii -h

for help and see the options of the parser. To convert the study of the example above type:

bruker2nifti -i <data_folder>/raw/McGill_Orientation/a20130329_APM_DEV_Orient.j71 -o <destination_folder> -study_name my_study -nifti_version 1 -qform_code 1 -sform_code 2 -save_human_readable True -correct_slope True -verbose 1

Via the CLI the user can also convert only a single scan invoking bruker2nifti.

Converting via Graphical User Interface

Check the next wiki-page to convert the data_set examples with the GUI.