fabio.GEimage

Package fabio :: Module GEimage |
[hide private]
[frames] | no frames]

# Source Code for Module fabio.GEimage

      1  #!/usr/bin/env python

      2

      3  #

      4  # Reads the header from a GE a-Si Angio Detector

      5  # Using version 8001 of the header from file:

      6  #     c:\adept\core\DefaultImageInfoConfig.csv

      7  #

      8  #  Antonino Miceli

      9  #  Thu Jan  4 13:46:31 CST 2007

     10  #

     11

     12  # modifications by Jon Wright for style, pychecker and fabio

     13  #

     14

     15  import numpy
     16  import struct, logging
     17  logger = logging.getLogger("GEimage")
     18  from fabioimage import fabioimage
     19  from fabioutils import next_filename, previous_filename
     20

     21  GE_HEADER_INFO = [

     22      # Name, length in bytes, format for struct (None means string)

     23      ('ImageFormat', 10, None),

     24      ('VersionOfStandardHeader', 2, '=H'),

     25      ('StandardHeaderSizeInBytes', 4, '=L'),

     26      ('VersionOfUserHeader', 2, '=H'),

     27      ('UserHeaderSizeInBytes', 4, '=L'),

     28      ('NumberOfFrames', 2, '=H'),

     29      ('NumberOfRowsInFrame', 2, '=H'),

     30      ('NumberOfColsInFrame', 2, '=H'),

     31      ('ImageDepthInBits', 2, '=H'),

     32      ('AcquisitionDate', 20, None),

     33      ('AcquisitionTime', 20, None),

     34      ('DUTID', 20, None),

     35      ('Operator', 50, None),

     36      ('DetectorSignature', 20, None),

     37      ('TestSystemName', 20, None),

     38      ('TestStationRevision', 20, None),

     39      ('CoreBundleRevision', 20, None),

     40      ('AcquisitionName', 40, None),

     41      ('AcquisitionParameterRevision', 20, None),

     42      ('OriginalNumberOfRows', 2, '=H'),

     43      ('OriginalNumberOfColumns', 2, '=H'),

     44      ('RowNumberUpperLeftPointArchiveROI', 2, '=H'),

     45      ('ColNumberUpperLeftPointArchiveROI', 2, '=H'),

     46      ('Swapped', 2, '=H'),

     47      ('Reordered', 2, '=H'),

     48      ('HorizontalFlipped', 2, '=H'),

     49      ('VerticalFlipped', 2, '=H'),

     50      ('WindowValueDesired', 2, '=H'),

     51      ('LevelValueDesired', 2, '=H'),

     52      ('AcquisitionMode', 2, '=H'),

     53      ('AcquisitionType', 2, '=H'),

     54      ('UserAcquisitionCoffFileName1', 100, None),

     55      ('UserAcquisitionCoffFileName2', 100, None),

     56      ('FramesBeforeExpose', 2, '=H'),

     57      ('FramesDuringExpose', 2, '=H'),

     58      ('FramesAfterExpose', 2, '=H'),

     59      ('IntervalBetweenFrames', 2, '=H'),

     60      ('ExposeTimeDelayInMicrosecs', 8, '=d'),

     61      ('TimeBetweenFramesInMicrosecs', 8, '=d'),

     62      ('FramesToSkipExpose', 2, '=H'),

     63      ('ExposureMode', 2, '=H'),

     64      ('PrepPresetTimeInMicrosecs', 8, '=d'),

     65      ('ExposePresetTimeInMicrosecs', 8, '=d'),

     66      ('AcquisitionFrameRateInFps', 4, '=f'),

     67      ('FOVSelect', 2, '=H'),

     68      ('ExpertMode', 2, '=H'),

     69      ('SetVCommon1', 8, '=d'),

     70      ('SetVCommon2', 8, '=d'),

     71      ('SetAREF', 8, '=d'),

     72      ('SetAREFTrim', 4, '=L'),

     73      ('SetSpareVoltageSource', 8, '=d'),

     74      ('SetCompensationVoltageSource', 8, '=d'),

     75      ('SetRowOffVoltage', 8, '=d'),

     76      ('SetRowOnVoltage', 8, '=d'),

     77      ('StoreCompensationVoltage', 4, '=L'),

     78      ('RampSelection', 2, '=H'),

     79      ('TimingMode', 2, '=H'),

     80      ('Bandwidth', 2, '=H'),

     81      ('ARCIntegrator', 2, '=H'),

     82      ('ARCPostIntegrator', 2, '=H'),

     83      ('NumberOfRows', 4, '=L'),

     84      ('RowEnable', 2, '=H'),

     85      ('EnableStretch', 2, '=H'),

     86      ('CompEnable', 2, '=H'),

     87      ('CompStretch', 2, '=H'),

     88      ('LeftEvenTristate', 2, '=H'),

     89      ('RightOddTristate', 2, '=H'),

     90      ('TestModeSelect', 4, '=L'),

     91      ('AnalogTestSource', 4, '=L'),

     92      ('VCommonSelect', 4, '=L'),

     93      ('DRCColumnSum', 4, '=L'),

     94      ('TestPatternFrameDelta', 4, '=L'),

     95      ('TestPatternRowDelta', 4, '=L'),

     96      ('TestPatternColumnDelta', 4, '=L'),

     97      ('DetectorHorizontalFlip', 2, '=H'),

     98      ('DetectorVerticalFlip', 2, '=H'),

     99      ('DFNAutoScrubOnOff', 2, '=H'),

    100      ('FiberChannelTimeOutInMicrosecs', 4, '=L'),

    101      ('DFNAutoScrubDelayInMicrosecs', 4, '=L'),

    102      ('StoreAECROI', 2, '=H'),

    103      ('TestPatternSaturationValue', 2, '=H'),

    104      ('TestPatternSeed', 4, '=L'),

    105      ('ExposureTimeInMillisecs', 4, '=f'),

    106      ('FrameRateInFps', 4, '=f'),

    107      ('kVp', 4, '=f'),

    108      ('mA', 4, '=f'),

    109      ('mAs', 4, '=f'),

    110      ('FocalSpotInMM', 4, '=f'),

    111      ('GeneratorType', 20, None),

    112      ('StrobeIntensityInFtL', 4, '=f'),

    113      ('NDFilterSelection', 2, '=H'),

    114      ('RefRegTemp1', 8, '=d'),

    115      ('RefRegTemp2', 8, '=d'),

    116      ('RefRegTemp3', 8, '=d'),

    117      ('Humidity1', 4, '=f'),

    118      ('Humidity2', 4, '=f'),

    119      ('DetectorControlTemp', 8, '=d'),

    120      ('DoseValueInmR', 8, '=d'),

    121      ('TargetLevelROIRow0', 2, '=H'),

    122      ('TargetLevelROICol0', 2, '=H'),

    123      ('TargetLevelROIRow1', 2, '=H'),

    124      ('TargetLevelROICol1', 2, '=H'),

    125      ('FrameNumberForTargetLevelROI', 2, '=H'),

    126      ('PercentRangeForTargetLevel', 2, '=H'),

    127      ('TargetValue', 2, '=H'),

    128      ('ComputedMedianValue', 2, '=H'),

    129      ('LoadZero', 2, '=H'),

    130      ('MaxLUTOut', 2, '=H'),

    131      ('MinLUTOut', 2, '=H'),

    132      ('MaxLinear', 2, '=H'),

    133      ('Reserved', 2, '=H'),

    134      ('ElectronsPerCount', 2, '=H'),

    135      ('ModeGain', 2, '=H'),

    136      ('TemperatureInDegC', 8, '=d'),

    137      ('LineRepaired', 2, '=H'),

    138      ('LineRepairFileName', 100, None),

    139      ('CurrentLongitudinalInMM', 4, '=f'),

    140      ('CurrentTransverseInMM', 4, '=f'),

    141      ('CurrentCircularInMM', 4, '=f'),

    142      ('CurrentFilterSelection', 4, '=L'),

    143      ('DisableScrubAck', 2, '=H'),

    144      ('ScanModeSelect', 2, '=H'),

    145      ('DetectorAppSwVersion', 20, None),

    146      ('DetectorNIOSVersion', 20, None),

    147      ('DetectorPeripheralSetVersion', 20, None),

    148      ('DetectorPhysicalAddress', 20, None),

    149      ('PowerDown', 2, '=H'),

    150      ('InitialVoltageLevel_VCOMMON', 8, '=d'),

    151      ('FinalVoltageLevel_VCOMMON', 8, '=d'),

    152      ('DmrCollimatorSpotSize', 10, None),

    153      ('DmrTrack', 5, None),

    154      ('DmrFilter', 5, None),

    155      ('FilterCarousel', 2, '=H'),

    156      ('Phantom', 20, None),

    157      ('SetEnableHighTime', 2, '=H'),

    158      ('SetEnableLowTime', 2, '=H'),

    159      ('SetCompHighTime', 2, '=H'),

    160      ('SetCompLowTime', 2, '=H'),

    161      ('SetSyncLowTime', 2, '=H'),

    162      ('SetConvertLowTime', 2, '=H'),

    163      ('SetSyncHighTime', 2, '=H'),

    164      ('SetEOLTime', 2, '=H'),

    165      ('SetRampOffsetTime', 2, '=H'),

    166      ('FOVStartingValue', 2, '=H'),

    167      ('ColumnBinning', 2, '=H'),

    168      ('RowBinning', 2, '=H'),

    169      ('BorderColumns64', 2, '=H'),

    170      ('BorderRows64', 2, '=H'),

    171      ('FETOffRows64', 2, '=H'),

    172      ('FOVStartColumn128', 2, '=H'),

    173      ('FOVStartRow128', 2, '=H'),

    174      ('NumberOfColumns128', 2, '=H'),

    175      ('NumberOfRows128', 2, '=H'),

    176      ('VFPAquisition', 2000, None),

    177      ('Comment', 200, None)

    178      ]
    179

    180

    181 -class GEimage(fabioimage):
    182

    183      _need_a_seek_to_read = True
    184

    185 -    def _readheader(self, infile):
    186          """ Read a GE image header """
    187

    188          infile.seek(0)
    189

    190          self.header = {}
    191          for name, nbytes, format in GE_HEADER_INFO:
    192              if format is None:
    193                  self.header[ name ] = infile.read(nbytes)
    194              else:
    195                  self.header[ name ] = struct.unpack(format,

    196                                                       infile.read(nbytes))[0]
    197

    198 -    def read(self, fname, frame=None):
    199          """

    200          Read in header into self.header and

    201          the data   into self.data

    202          """
    203          if frame is None:
    204              frame = 0
    205          self.header = {}
    206          self.resetvals()
    207          infile = self._open(fname, "rb")
    208          self.sequencefilename = fname
    209          self._readheader(infile)
    210          self.nframes = self.header['NumberOfFrames']
    211          self._readframe(infile, frame)
    212          infile.close()
    213          return self
    214

    215 -    def _makeframename(self):
    216          """ The thing to be printed for the user to represent a frame inside

    217          a file """
    218          self.filename = "%s$%04d" % (self.sequencefilename,

    219                                     self.currentframe)
    220

    221 -    def _readframe(self, filepointer, img_num):
    222          """

    223          # Load only one image from the sequence

    224          #    Note: the first image in the sequence 0

    225          # raises an exception if you give an invalid image

    226          # otherwise fills in self.data

    227          """
    228          if(img_num > self.nframes or img_num < 0):
    229              raise Exception("Bad image number")
    230          imgstart = self.header['StandardHeaderSizeInBytes'] + \

    231                     self.header['UserHeaderSizeInBytes'] + \

    232                     img_num * self.header['NumberOfRowsInFrame'] * \

    233                     self.header['NumberOfColsInFrame'] * \

    234                     self.header['ImageDepthInBits'] / 8
    235          # whence = 0 means seek from start of file

    236          filepointer.seek(imgstart, 0)
    237

    238          self.bpp = self.header['ImageDepthInBits'] / 8 # hopefully 2
    239          imglength = self.header['NumberOfRowsInFrame'] * \

    240                      self.header['NumberOfColsInFrame'] * self.bpp
    241          if self.bpp != 2:
    242              logging.warning("Using uint16 for GE but seems to be wrong")
    243

    244          # Guessing it is always unsigned int?

    245          self.data = numpy.fromstring(filepointer.read(imglength), numpy.uint16)
    246          self.data.shape = (self.header['NumberOfRowsInFrame'],

    247                              self.header['NumberOfColsInFrame'])
    248          self.dim2 , self.dim1 = self.data.shape
    249          self.currentframe = int(img_num)
    250          self._makeframename()
    251

    252

    253 -    def write(self, fname, force_type=numpy.uint16):
    254          """ Not yet implemented"""
    255          raise Exception("Write is not implemented")
    256

    257 -    def getframe(self, num):
    258          """

    259          Returns a frame as a new fabioimage object

    260          """
    261          if num < 0 or num > self.nframes:
    262              raise Exception("Requested frame number is out of range")
    263          # Do a deep copy of the header to make a new one

    264          newheader = {}
    265          for k in self.header.keys():
    266              newheader[k] = self.header[k]
    267          frame = GEimage(header=newheader)
    268          frame.nframes = self.nframes
    269          frame.sequencefilename = self.sequencefilename
    270          infile = frame._open(self.sequencefilename, "rb")
    271          frame._readframe(infile, num)
    272          infile.close()
    273          return frame
    274

    275 -    def next(self):
    276          """

    277          Get the next image in a series as a fabio image

    278          """
    279          if self.currentframe < (self.nframes - 1) and self.nframes > 1:
    280              return self.getframe(self.currentframe + 1)
    281          else:
    282              newobj = GEimage()
    283              newobj.read(next_filename(

    284                  self.sequencefilename))
    285              return newobj
    286

    287 -    def previous(self):
    288          """

    289          Get the previous image in a series as a fabio image

    290          """
    291          if self.currentframe > 0:
    292              return self.getframe(self.currentframe - 1)
    293          else:
    294              newobj = GEimage()
    295              newobj.read(previous_filename(

    296                  self.sequencefilename))
    297              return newobj
    298

    299

    300 -def demo():
    301      import sys, time
    302

    303      if len(sys.argv) < 2:
    304          print "USAGE: GE_script.py <GEaSi_raw_image_file>"
    305          sys.exit()
    306

    307      image_file = sys.argv[1]
    308

    309      print "init read_GEaSi_data class and load header.."
    310      sequence1 = GEimage()
    311      sequence1.read(image_file)
    312

    313      print "TimeBetweenFramesInMicrosecs = ",
    314      print sequence1.header['TimeBetweenFramesInMicrosecs']
    315      print "AcquisitionTime = ",
    316      print sequence1.header['AcquisitionTime']
    317

    318

    319      print "Mean = ", sequence1.data.ravel().mean()
    320

    321      while 1:
    322          start = time.time()
    323          try:
    324              sequence1 = sequence1.next()
    325              print sequence1.currentframe, sequence1.data.ravel().mean(), \

    326                    time.time() - start
    327          except Exception, ex:
    328              raise ex
    329

    330

    331

    332

    333  if __name__ == '__main__':
    334      demo()
    335