MGH is just a generic volume-based format so I think thickness data will be stored in a 1xNumVerticesx1x1 volume. Here is a python function which reads in the volume data. It's basically a translation of the readmgh function from the Freesurfer matlab toolbox. You can modify it to return more parts of the file data if necessary...
--------------- import struct import numpy
def read_mgh(filename): fp = open(filename,'rb') intsize = struct.calcsize('>i') shortsize = struct.calcsize('>h') floatsize = struct.calcsize('>f') charsize = struct.calcsize('>b')
v = struct.unpack('>i',fp.read(intsize))[0] ndim1 = struct.unpack('>i',fp.read(intsize))[0] ndim2 = struct.unpack('>i',fp.read(intsize))[0] ndim3 = struct.unpack('>i',fp.read(intsize))[0] nframes = struct.unpack('>i',fp.read(intsize))[0] vtype = struct.unpack('>i',fp.read(intsize))[0] dof = struct.unpack('>i',fp.read(intsize))[0]
UNUSED_SPACE_SIZE = 256 USED_SPACE_SIZE = (3*4) + (4*3*4) # space for ras transform unused_space_size = UNUSED_SPACE_SIZE - 2
ras_good_flag = struct.unpack('>h',fp.read(shortsize))[0] if ras_good_flag: # We read these in but don't process them # as we just want to move to the volume data delta = struct.unpack('>fff',fp.read(floatsize*3)) Mdc = struct.unpack('>fffffffff',fp.read(floatsize*9)) Pxyz_c = struct.unpack('>fff',fp.read(floatsize*3))
unused_space_size = unused_space_size - USED_SPACE_SIZE
for i in range(unused_space_size): struct.unpack('>b',fp.read(charsize))[0]
nv = ndim1 * ndim2 * ndim3 * nframes vol = numpy.fromstring(fp.read(floatsize*nv),dtype=numpy.float32).byteswap()
nvert = max([ndim1,ndim2,ndim3]) vol = numpy.reshape(vol,(ndim1,ndim2,ndim3,nframes),order='F') vol = numpy.squeeze(vol) fp.close()
return vol
-------------- Padraig.
Yaroslav Halchenko wrote:
actually I need thickness data out of it. Is .mgh format documented somewhere?
On Thu, 24 Apr 2008, Pádraig Kitterick wrote:
How much detail do you need frmo the mgh file? I wrote a quick Python script which reads in at least the volume data but I think I just passed over most of the other stuff in there...
Yaroslav Halchenko wrote:
Does anyone know any handy module to at least read in .mgh files from Python? Or should I simply try mri_convert them into .minc and use netcdf module within python?
Thanks in advance for the ideas!