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 as np 
 16  import struct, logging 
 17  from fabioimage import fabioimage 
 18  from fabioutils import next_filename, previous_filename 
 19  
 
 20  GE_HEADER_INFO = [
 
 21      # Name, length in bytes, format for struct (None means string)
 
 22      ('ImageFormat', 10, None),
 
 23      ('VersionOfStandardHeader', 2, '=H'),
 
 24      ('StandardHeaderSizeInBytes', 4, '=L'),
 
 25      ('VersionOfUserHeader', 2, '=H'),
 
 26      ('UserHeaderSizeInBytes', 4, '=L'),
 
 27      ('NumberOfFrames', 2, '=H'),
 
 28      ('NumberOfRowsInFrame', 2, '=H'),
 
 29      ('NumberOfColsInFrame', 2, '=H'),
 
 30      ('ImageDepthInBits', 2, '=H'),
 
 31      ('AcquisitionDate', 20, None),
 
 32      ('AcquisitionTime', 20, None),
 
 33      ('DUTID', 20, None),
 
 34      ('Operator', 50, None),
 
 35      ('DetectorSignature', 20, None),
 
 36      ('TestSystemName', 20, None),
 
 37      ('TestStationRevision', 20, None),
 
 38      ('CoreBundleRevision', 20, None),
 
 39      ('AcquisitionName', 40, None),
 
 40      ('AcquisitionParameterRevision', 20, None),
 
 41      ('OriginalNumberOfRows', 2, '=H'),
 
 42      ('OriginalNumberOfColumns', 2, '=H'),
 
 43      ('RowNumberUpperLeftPointArchiveROI', 2, '=H'),
 
 44      ('ColNumberUpperLeftPointArchiveROI', 2, '=H'),
 
 45      ('Swapped', 2, '=H'),
 
 46      ('Reordered', 2, '=H'),
 
 47      ('HorizontalFlipped', 2, '=H'),
 
 48      ('VerticalFlipped', 2, '=H'),
 
 49      ('WindowValueDesired', 2, '=H'),
 
 50      ('LevelValueDesired', 2, '=H'),
 
 51      ('AcquisitionMode', 2, '=H'),
 
 52      ('AcquisitionType', 2, '=H'),
 
 53      ('UserAcquisitionCoffFileName1', 100, None),
 
 54      ('UserAcquisitionCoffFileName2', 100, None),
 
 55      ('FramesBeforeExpose', 2, '=H'),
 
 56      ('FramesDuringExpose', 2, '=H'),
 
 57      ('FramesAfterExpose', 2, '=H'),
 
 58      ('IntervalBetweenFrames', 2, '=H'),
 
 59      ('ExposeTimeDelayInMicrosecs', 8, '=d'),
 
 60      ('TimeBetweenFramesInMicrosecs', 8, '=d'),
 
 61      ('FramesToSkipExpose', 2, '=H'),
 
 62      ('ExposureMode', 2, '=H'),
 
 63      ('PrepPresetTimeInMicrosecs', 8, '=d'),
 
 64      ('ExposePresetTimeInMicrosecs', 8, '=d'),
 
 65      ('AcquisitionFrameRateInFps', 4, '=f'),
 
 66      ('FOVSelect', 2, '=H'),
 
 67      ('ExpertMode', 2, '=H'),
 
 68      ('SetVCommon1', 8, '=d'),
 
 69      ('SetVCommon2', 8, '=d'),
 
 70      ('SetAREF', 8, '=d'),
 
 71      ('SetAREFTrim', 4, '=L'),
 
 72      ('SetSpareVoltageSource', 8, '=d'),
 
 73      ('SetCompensationVoltageSource', 8, '=d'),
 
 74      ('SetRowOffVoltage', 8, '=d'),
 
 75      ('SetRowOnVoltage', 8, '=d'),
 
 76      ('StoreCompensationVoltage', 4, '=L'),
 
 77      ('RampSelection', 2, '=H'),
 
 78      ('TimingMode', 2, '=H'),
 
 79      ('Bandwidth', 2, '=H'),
 
 80      ('ARCIntegrator', 2, '=H'),
 
 81      ('ARCPostIntegrator', 2, '=H'),
 
 82      ('NumberOfRows', 4, '=L'),
 
 83      ('RowEnable', 2, '=H'),
 
 84      ('EnableStretch', 2, '=H'),
 
 85      ('CompEnable', 2, '=H'),
 
 86      ('CompStretch', 2, '=H'),
 
 87      ('LeftEvenTristate', 2, '=H'),
 
 88      ('RightOddTristate', 2, '=H'),
 
 89      ('TestModeSelect', 4, '=L'),
 
 90      ('AnalogTestSource', 4, '=L'),
 
 91      ('VCommonSelect', 4, '=L'),
 
 92      ('DRCColumnSum', 4, '=L'),
 
 93      ('TestPatternFrameDelta', 4, '=L'),
 
 94      ('TestPatternRowDelta', 4, '=L'),
 
 95      ('TestPatternColumnDelta', 4, '=L'),
 
 96      ('DetectorHorizontalFlip', 2, '=H'),
 
 97      ('DetectorVerticalFlip', 2, '=H'),
 
 98      ('DFNAutoScrubOnOff', 2, '=H'),
 
 99      ('FiberChannelTimeOutInMicrosecs', 4, '=L'),
 
100      ('DFNAutoScrubDelayInMicrosecs', 4, '=L'),
 
101      ('StoreAECROI', 2, '=H'),
 
102      ('TestPatternSaturationValue', 2, '=H'),
 
103      ('TestPatternSeed', 4, '=L'),
 
104      ('ExposureTimeInMillisecs', 4, '=f'),
 
105      ('FrameRateInFps', 4, '=f'),
 
106      ('kVp', 4, '=f'),
 
107      ('mA', 4, '=f'),
 
108      ('mAs', 4, '=f'),
 
109      ('FocalSpotInMM', 4, '=f'),
 
110      ('GeneratorType', 20, None),
 
111      ('StrobeIntensityInFtL', 4, '=f'),
 
112      ('NDFilterSelection', 2, '=H'),
 
113      ('RefRegTemp1', 8, '=d'),
 
114      ('RefRegTemp2', 8, '=d'),
 
115      ('RefRegTemp3', 8, '=d'),
 
116      ('Humidity1', 4, '=f'),
 
117      ('Humidity2', 4, '=f'),
 
118      ('DetectorControlTemp', 8, '=d'),
 
119      ('DoseValueInmR', 8, '=d'),
 
120      ('TargetLevelROIRow0', 2, '=H'),
 
121      ('TargetLevelROICol0', 2, '=H'),
 
122      ('TargetLevelROIRow1', 2, '=H'),
 
123      ('TargetLevelROICol1', 2, '=H'),
 
124      ('FrameNumberForTargetLevelROI', 2, '=H'),
 
125      ('PercentRangeForTargetLevel', 2, '=H'),
 
126      ('TargetValue', 2, '=H'),
 
127      ('ComputedMedianValue', 2, '=H'),
 
128      ('LoadZero', 2, '=H'),
 
129      ('MaxLUTOut', 2, '=H'),
 
130      ('MinLUTOut', 2, '=H'),
 
131      ('MaxLinear', 2, '=H'),
 
132      ('Reserved', 2, '=H'),
 
133      ('ElectronsPerCount', 2, '=H'),
 
134      ('ModeGain', 2, '=H'),
 
135      ('TemperatureInDegC', 8, '=d'),
 
136      ('LineRepaired', 2, '=H'),
 
137      ('LineRepairFileName', 100, None),
 
138      ('CurrentLongitudinalInMM', 4, '=f'),
 
139      ('CurrentTransverseInMM', 4, '=f'),
 
140      ('CurrentCircularInMM', 4, '=f'),
 
141      ('CurrentFilterSelection', 4, '=L'),
 
142      ('DisableScrubAck', 2, '=H'),
 
143      ('ScanModeSelect', 2, '=H'),
 
144      ('DetectorAppSwVersion', 20, None),
 
145      ('DetectorNIOSVersion', 20, None),
 
146      ('DetectorPeripheralSetVersion', 20, None),
 
147      ('DetectorPhysicalAddress', 20, None),
 
148      ('PowerDown', 2, '=H'),
 
149      ('InitialVoltageLevel_VCOMMON', 8, '=d'),
 
150      ('FinalVoltageLevel_VCOMMON', 8, '=d'),
 
151      ('DmrCollimatorSpotSize', 10, None),
 
152      ('DmrTrack', 5, None),
 
153      ('DmrFilter', 5, None),
 
154      ('FilterCarousel', 2, '=H'),
 
155      ('Phantom', 20, None),
 
156      ('SetEnableHighTime', 2, '=H'),
 
157      ('SetEnableLowTime', 2, '=H'),
 
158      ('SetCompHighTime', 2, '=H'),
 
159      ('SetCompLowTime', 2, '=H'),
 
160      ('SetSyncLowTime', 2, '=H'),
 
161      ('SetConvertLowTime', 2, '=H'),
 
162      ('SetSyncHighTime', 2, '=H'),
 
163      ('SetEOLTime', 2, '=H'),
 
164      ('SetRampOffsetTime', 2, '=H'),
 
165      ('FOVStartingValue', 2, '=H'),
 
166      ('ColumnBinning', 2, '=H'),
 
167      ('RowBinning', 2, '=H'),
 
168      ('BorderColumns64', 2, '=H'),
 
169      ('BorderRows64', 2, '=H'),
 
170      ('FETOffRows64', 2, '=H'),
 
171      ('FOVStartColumn128', 2, '=H'),
 
172      ('FOVStartRow128', 2, '=H'),
 
173      ('NumberOfColumns128', 2, '=H'),
 
174      ('NumberOfRows128', 2, '=H'),
 
175      ('VFPAquisition', 2000, None),
 
176      ('Comment', 200, None)
 
177      ] 
178  
 
179  
 
180 -class GEimage(fabioimage):
181 182 _need_a_seek_to_read = True 183
184 - def _readheader(self, infile):
185 """ Read a GE image header """ 186 187 infile.seek(0) 188 189 self.header = {} 190 for name, nbytes, format in GE_HEADER_INFO: 191 if format is None: 192 self.header[ name ] = infile.read(nbytes) 193 else: 194 self.header[ name ] = struct.unpack(format, 195 infile.read(nbytes))[0]
196
197 - def read(self, fname, frame=0):
198 """ 199 Read in header into self.header and 200 the data into self.data 201 """ 202 self.header = {} 203 self.resetvals() 204 infile = self._open(fname, "rb") 205 self.sequencefilename = fname 206 self._readheader(infile) 207 self.nframes = self.header['NumberOfFrames'] 208 self._readframe(infile, frame) 209 infile.close() 210 return self
211
212 - def _makeframename(self):
213 """ The thing to be printed for the user to represent a frame inside 214 a file """ 215 self.filename = "%s$%04d" % (self.sequencefilename, 216 self.currentframe)
217
218 - def _readframe(self, filepointer, img_num):
219 """ 220 # Load only one image from the sequence 221 # Note: the first image in the sequence 0 222 # raises an exception if you give an invalid image 223 # otherwise fills in self.data 224 """ 225 if(img_num > self.nframes or img_num < 0): 226 raise Exception("Bad image number") 227 imgstart = self.header['StandardHeaderSizeInBytes'] + \ 228 self.header['UserHeaderSizeInBytes'] + \ 229 img_num * self.header['NumberOfRowsInFrame'] * \ 230 self.header['NumberOfColsInFrame'] * \ 231 self.header['ImageDepthInBits'] / 8 232 # whence = 0 means seek from start of file 233 filepointer.seek(imgstart, 0) 234 235 self.bpp = self.header['ImageDepthInBits'] / 8 # hopefully 2 236 imglength = self.header['NumberOfRowsInFrame'] * \ 237 self.header['NumberOfColsInFrame'] * self.bpp 238 if self.bpp != 2: 239 logging.warning("Using uint16 for GE but seems to be wrong") 240 241 # Guessing it is always unsigned int? 242 self.data = np.fromstring(filepointer.read(imglength), np.uint16) 243 self.data.shape = (self.header['NumberOfRowsInFrame'], 244 self.header['NumberOfColsInFrame']) 245 self.dim2 , self.dim1 = self.data.shape 246 self.currentframe = int(img_num) 247 self._makeframename()
248 249
250 - def write(self, fname, force_type=np.uint16):
251 """ Not yet implemented""" 252 raise Exception("Write is not implemented")
253
254 - def getframe(self, num):
255 """ 256 Returns a frame as a new fabioimage object 257 """ 258 if num < 0 or num > self.nframes: 259 raise Exception("Requested frame number is out of range") 260 # Do a deep copy of the header to make a new one 261 newheader = {} 262 for k in self.header.keys(): 263 newheader[k] = self.header[k] 264 frame = GEimage(header=newheader) 265 frame.nframes = self.nframes 266 frame.sequencefilename = self.sequencefilename 267 infile = frame._open(self.sequencefilename, "rb") 268 frame._readframe(infile, num) 269 infile.close() 270 return frame
271
272 - def next(self):
273 """ 274 Get the next image in a series as a fabio image 275 """ 276 if self.currentframe < (self.nframes - 1) and self.nframes > 1: 277 return self.getframe(self.currentframe + 1) 278 else: 279 newobj = GEimage() 280 newobj.read(next_filename( 281 self.sequencefilename)) 282 return newobj
283
284 - def previous(self):
285 """ 286 Get the previous image in a series as a fabio image 287 """ 288 if self.currentframe > 0: 289 return self.getframe(self.currentframe - 1) 290 else: 291 newobj = GEimage() 292 newobj.read(previous_filename( 293 self.sequencefilename)) 294 return newobj
295 296
297 -def demo():
298 import sys, time 299 300 if len(sys.argv) < 2: 301 print "USAGE: GE_script.py <GEaSi_raw_image_file>" 302 sys.exit() 303 304 image_file = sys.argv[1] 305 306 print "init read_GEaSi_data class and load header.." 307 sequence1 = GEimage() 308 sequence1.read(image_file) 309 310 print "TimeBetweenFramesInMicrosecs = ", 311 print sequence1.header['TimeBetweenFramesInMicrosecs'] 312 print "AcquisitionTime = ", 313 print sequence1.header['AcquisitionTime'] 314 315 316 print "Mean = ", sequence1.data.ravel().mean() 317 318 while 1: 319 start = time.time() 320 try: 321 sequence1 = sequence1.next() 322 print sequence1.currentframe, sequence1.data.ravel().mean(), \ 323 time.time() - start 324 except: 325 raise 326 break
327 328 329 330 331 if __name__ == '__main__': 332 demo() 333