Creating a header file

100 views
Skip to first unread message

Jimmy

unread,
Aug 26, 2010, 8:25:06 PM8/26/10
to NiPy Users
I need to create a nifti header file with a specified x,y,z and voxel
size. To get this done quickly inside my existing pipeline, I created
a nipype interface for the FSL createheader tool since that's what I
was using before nipy. I'm sure there is probably a better way - maybe
using nibabel? Can anyone point me in the right direction?

Satrajit Ghosh

unread,
Aug 26, 2010, 10:26:39 PM8/26/10
to nipy-user
hi jimmy,

please do send us the code for your interface or send a pull request if you are using github. nibabel can indeed create a header, but matthew will be a better person to talk about the validity of the following steps.

----
from nibabel import Nifti1Header
import numpy as np

hdr = Nifti1Header()
hdr.set_qform(np.eye(4))
hdr.set_data_shape((12,24,48))
hdr.set_zooms((3,3,3))
hdr.set_xyzt_units(xyz='mm')

f = open('test.hdr','w')
hdr.write_to(f)
f.close()
-----

when i run mri_info on this from freesurfer i get:

INFO: using NIfTI-1 qform
Volume information for test.hdr
          type: nii
    dimensions: 12 x 24 x 48
   voxel sizes: 3.0000, 3.0000, 3.0000
          type: FLOAT (3)
           fov: 12.000
           dof: 0
        xstart: -6.0, xend: 6.0
        ystart: -12.0, yend: 12.0
        zstart: -24.0, zend: 24.0
            TR: 0.00 msec, TE: 0.00 msec, TI: 0.00 msec, flip angle: 0.00 degrees
       nframes: 1
       PhEncDir: UNKNOWN
ras xform present
    xform info: x_r =   1.0000, y_r =   0.0000, z_r =   0.0000, c_r =    18.0000
              : x_a =   0.0000, y_a =   1.0000, z_a =   0.0000, c_a =    36.0000
              : x_s =   0.0000, y_s =   0.0000, z_s =   1.0000, c_s =    72.0000
Orientation   : RAS
Primary Slice Direction: axial

voxel to ras transform:
                3.0000   0.0000   0.0000     0.0000
                0.0000   3.0000   0.0000     0.0000
                0.0000   0.0000   3.0000     0.0000
                0.0000   0.0000   0.0000     1.0000

voxel-to-ras determinant 27

ras to voxel transform:
                0.3333  -0.0000  -0.0000    -0.0000
               -0.0000   0.3333  -0.0000    -0.0000
               -0.0000  -0.0000   0.3333    -0.0000
                0.0000   0.0000   0.0000     1.0000

cheers,

satra

Jimmy

unread,
Aug 27, 2010, 4:13:52 PM8/27/10
to NiPy Users
Thanks Satra, that worked for me.

I'm not using github yet (haven't made the switch from SVN), but my
code is pretty short. I just threw the following in nipype/interfaces/
fsl/utils.py :

class CreateHeaderInputSpec(FSLCommandInputSpec):
x_size = traits.Int(argstr="%d", position=0, desc="x size",
mandatory=True)
y_size = traits.Int(argstr="%d", position=1, desc="y size",
mandatory=True)
z_size = traits.Int(argstr="%d", position=2, desc="z size",
mandatory=True)
t_size = traits.Int(argstr="%d", position=3, desc="t size",
mandatory=True)
voxel_x_size = traits.Float(argstr="%f", position=4, desc="voxel x
size", mandatory=True)
voxel_y_size = traits.Float(argstr="%f", position=5, desc="voxel y
size", mandatory=True)
voxel_z_size = traits.Float(argstr="%f", position=6, desc="voxel z
size", mandatory=True)
tr = traits.Float(argstr="%f", position=7, desc="repetition time
(TR)", mandatory=True)
x_origin = traits.Int(argstr="%d", position=8, desc="x origin",
mandatory=True)
y_origin = traits.Int(argstr="%d", position=9, desc="y origin",
mandatory=True)
z_origin = traits.Int(argstr="%d", position=10, desc="z origin",
mandatory=True)
datatype = traits.Int(argstr="%d", position=11, desc="data type",
mandatory=True)
out_file = File(argstr="%s", position=12, desc="output file",
genfile=True)

class CreateHeaderOutputSpec(TraitedSpec):
out_file = File(exists=True)

class CreateHeader(FSLCommand):
""" Use fslcreatehd to create a header and image
"""

_cmd = 'fslcreatehd'
input_spec = CreateHeaderInputSpec
output_spec = CreateHeaderOutputSpec

def _list_outputs(self):
outputs = self._outputs().get()
outputs['out_file'] = self.inputs.out_file
if not isdefined(outputs['out_file']):
outputs['out_file'] = self._gen_fname('header.nii.gz')
return outputs

def _gen_filename(self, name):
if name == 'out_file':
return self._list_outputs()[name]
return None


and of course, added CreateHeader to the import statement in nipype/
interfaces/fsl/__init__.py

I'm not sure how correct this code is, but it seems to work.

- Jimmy
Reply all
Reply to author
Forward
0 new messages