Utilities

File format conversion

The following functions facilitate the integration of binary executables that operate on images in the Analyze 7.5 file format with workflows based on the newer NIfTI-1 format.

sbl.core.io.converters.analyze_to_nifti(src_path, dst_path, header=None, dtype=None)

Save the Analyze image at src_path as a NIfTI image to dst_path.

The canonical image orientation used by NiBabel is LPI.

  • 1st (x) axis increases Left to Right
  • 2nd (y) axis increases back to front (Posterior to Anterior)
  • 3rd (z) axis increases bottom to top (Inferior to Superior)

Analyze images are assumed to be in stored in the radiological convention (corresponding to RPI), with the fastest-changing (x) axis increasing from right to left.

Parameters:
  • src_path – Source image file path
  • dst_path – Destination image file path
  • header – (optional) Override source header with header
  • dtype – (optional) Override source data type with NumPy data type
Returns:

The original Analyze header loaded from src_path

sbl.core.io.converters.nifti_to_analyze(src_path, dst_path, header=None, dtype=None)

Save the NIfTI image at src_path as an Analyze image to dst_path.

Parameters:
  • src_path – Source image file path
  • dst_path – Destination image file path
  • header – (optional) Override source header with header
  • dtype – (optional) Override source data type with NumPy data type
Returns:

The original NIfTI header loaded from src_path

sbl.sabre.utils.converters.run_with_analyze(self, func, input_names, output_names, output_transforms={}, working_dir=None, dtype=None, remove=True)

Run func (usually lambda: _run_interface(self, runtime)) of a Nipype CommandLine interface with inputs listed in input_names converted to temporary files in the Analyze 7.5 format before execution, and outputs listed in output_names converted back to NIfTI.

Parameters:
  • func – Parametrized function (lambda) to evaluate
  • input_names – List of names of inputs to convert
  • output_names – List of names of outputs to convert
  • output_transforms – (optional) {'output_name': transform_func} dictionary to account for transformations applied by the evaluated function (interface) func to the output filenames
  • working_dir – (optional) Store converted temporary images in working_dir instead of temporary directory
  • remove – (optional) Remove temporary (Analyze 7.5) files after running func (default: True)
  • dtype – (optional) Override source data type with NumPy data type

Example usage in a class extending nipype.interfaces.base.CommandLine:

def _run_interface(self, runtime):
    runtime = run_with_analyze(self, 
        lambda: super(self.__class__, self)._run_interface(runtime),
        input_names=["input_image", "mask_image"],
        output_names=["output_image"]
    )
    if runtime.stderr:
        self.raise_exception(runtime)

    return runtime

Table Of Contents

Previous topic

Automated Image Registration (AIR)

This Page