Skip to content

Example: use bruker2nifti in a python (Ipython) session

Sebastiano Ferraris edited this page Dec 17, 2018 · 1 revision

In this example we use the publicly available dataset bruker2nifti_qa (thanks to Mikaël Naveau).

  • <data_folder> contains the study to convert.
  • <destination_folder> is where the converted study will be stored.
  • Please see the Definitions of scan, study and experiment.

Start a Python session

Open a terminal and type python (or ipython if the interactive command to console, import os and import the class converter of bruker2nifti:

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

Links the folder path to Python variables

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, 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')

Change the converter options

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
  • 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 may want to convert in a readable and Python-editable format also some additional Bruker parameter files called acqp, reco and method (see ParaVision manuals for further informations). This can be done 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

Check

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)

Convert

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)
    # call the function convert, to convert the study:
    bru.convert()