def load_header(self, filestem): try: fp = open(filestem+".nii", 'rb') except exceptions.IOError: try: fp = open(filestem+".hdr", 'rb') except exceptions.IOError: raise IOError("no NIFTI file found with this name: %s"%filestem) self.filetype = 'dual' else: self.filetype = 'single' byte_order = byteorders[sys.byteorder] self.swapped = False reported_length = struct_unpack(fp, byte_order, field_formats[0:1])[0] if reported_length != HEADER_SIZE: byte_order = byteorders['swapped'] self.swapped = True fp.seek(0) # unpack all header values values = struct_unpack(fp, byte_order, field_formats) fp.close() # now load values into self hd_vals = dict() map(hd_vals.__setitem__, struct_fields.keys(), values) # sanity check? why not if (self.filetype == 'single' and hd_vals['magic'] != 'n+1\x00') \ or (self.filetype == 'dual' and hd_vals['magic'] != 'ni1\x00'): raise ValueError("Got file %s, but magic string is incorrect: %s"%\ (filestem, hd_vals['magic'])) #self.hdr = hd_vals self.__dict__.update(hd_vals)
def dumpHeader(filename): # Determine byte order of the header. The first header element is the # header size. It should always be 384. If it is not then you know # you read it in the wrong byte order. byte_order = byteorders[sys.byteorder] reported_length = struct_unpack(file(filename, "rb"), byte_order, field_formats[0:1])[0] if reported_length != HEADER_SIZE: byte_order = byteorders["swapped"] # unpack all header values values = struct_unpack(file(filename, "rb"), byte_order, field_formats) for field, val in zip(struct_fields.keys(), values): print field, " -- ", val
def dumpHeader(filename): # Determine byte order of the header. The first header element is the # header size. It should always be 384. If it is not then you know # you read it in the wrong byte order. byte_order = byteorders[sys.byteorder] reported_length = struct_unpack(file(filename, 'rb'), byte_order, field_formats[0:1])[0] if reported_length != HEADER_SIZE: byte_order = byteorders['swapped'] # unpack all header values values = struct_unpack(file(filename, 'rb'), byte_order, field_formats) for field, val in zip(struct_fields.keys(), values): print field, " -- ", val
def load_header(self, filename): "Load Analyze7.5 header from the given filename" # Determine byte order of the header. The first header element is the # header size. It should always be 384. If it is not then you know # you read it in the wrong byte order. byte_order = byteorders[sys.byteorder] self.swapped = False reported_length = struct_unpack(file(filename, "rb"), byte_order, field_formats[0:1])[0] if reported_length != HEADER_SIZE: byte_order = byteorders["swapped"] self.swapped = True # unpack all header values values = struct_unpack(file(filename, "rb"), byte_order, field_formats) # now load values into self hd_vals = dict() map(hd_vals.__setitem__, struct_fields.keys(), values) self.__dict__.update(hd_vals)
def load_header(self, filename): "Load Analyze7.5 header from the given filename" # Determine byte order of the header. The first header element is the # header size. It should always be 384. If it is not then you know # you read it in the wrong byte order. byte_order = byteorders[sys.byteorder] self.swapped = False reported_length = struct_unpack(file(filename, 'rb'), byte_order, field_formats[0:1])[0] if reported_length != HEADER_SIZE: byte_order = byteorders['swapped'] self.swapped = True # unpack all header values values = struct_unpack(file(filename, 'rb'), byte_order, field_formats) # now load values into self hd_vals = dict() map(hd_vals.__setitem__, struct_fields.keys(), values) self.__dict__.update(hd_vals)