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

Source Code for Module fabio.file_series

  1   
  2   
  3   
  4  #!/usr/bin/env python 
  5  """ 
  6   
  7  Authors: Henning O. Sorensen & Erik Knudsen 
  8           Center for Fundamental Research: Metal Structures in Four Dimensions 
  9           Risoe National Laboratory 
 10           Frederiksborgvej 399 
 11           DK-4000 Roskilde 
 12           email:erik.knudsen@risoe.dk 
 13   
 14          + Jon Wright, ESRF 
 15  """ 
 16  from fabio import filename_object 
 17  import fabio 
 18  from fabio.openimage import openimage 
 19           
 20   
21 -def new_file_series0( first_object, first = None, last = None, step = 1 ):
22 """ 23 Created from a fabio image 24 first and last are file numbers 25 """ 26 im = first_object 27 nimages = 0 28 # for counting images 29 if None in (first, last): 30 step = 0 31 total = 1 32 else: 33 total = last - first 34 35 yield im 36 while nimages < total: 37 nimages += step 38 try: 39 newim = im.next() 40 im = newim 41 except: 42 import traceback 43 traceback.print_exc() 44 45 # Skip bad images 46 print "Got a problem here" 47 try: 48 im.filename = fabio.next_filename( im.filename ) 49 except: 50 # KE: This will not work and will throw an exception 51 # fabio.next_filename doesn't understand %nnnn on the end 52 im.filename = fabio.next_filename( im.sequencefilename ) 53 yield None 54 yield im
55 56 57
58 -def new_file_series( first_object, nimages = 0, step = 1, traceback = False ):
59 """ 60 A generator function that creates a file series starting from a a fabioimage. 61 Iterates through all images in a file (if more than 1), then proceeds to 62 the next file as determined by fabio.next_filename. 63 64 first_object: the starting fabioimage, which will be the first one yielded 65 in the sequence 66 nimages: the maximum number of images to consider 67 step: step size, will yield the first and every step'th image until nimages 68 is reached. (e.g. nimages = 5, step = 2 will yield 3 images (0, 2, 4) 69 traceback: if True causes it to print a traceback in the event of an 70 exception (missing image, etc.). Otherwise the calling routine can handle 71 the exception as it chooses 72 yields: the next fabioimage in the series. 73 In the event there is an exception, it yields the sys.exec_info for the 74 exception instead. sys.exec_info is a tuple: 75 ( exceptionType, exceptionValue, exceptionTraceback ) 76 from which all the exception information can be obtained. 77 Suggested usage: 78 for obj in new_file_series( ... ): 79 if not isinstance( obj, fabio.fabioimage.fabioimage ): 80 # deal with errors like missing images, non readable files, etc 81 # e.g. 82 traceback.print_exception(obj[0], obj[1], obj[2]) 83 """ 84 im = first_object 85 nprocessed = 0 86 abort = False 87 if nimages > 0: 88 yield im 89 nprocessed += 1 90 while nprocessed < nimages: 91 try: 92 newim = im.next() 93 im = newim 94 retVal = im 95 except Exception, ex: 96 import sys 97 retVal = sys.exc_info() 98 if(traceback): 99 import traceback 100 traceback.print_exc() 101 # Skip bad images 102 print "Got a problem here: next() failed" 103 # Skip bad images 104 try: 105 im.filename = fabio.next_filename( im.filename ) 106 except: 107 pass 108 if nprocessed % step == 0: 109 yield retVal 110 # Avoid cyclic references with exc_info ? 111 retVal = None 112 if abort: break 113 nprocessed += 1
114 115 116
117 -class file_series(list):
118 """ 119 represents a series of files to iterate 120 has an idea of a current position to do next and prev 121 122 You also get from the list python superclass: 123 append 124 count 125 extend 126 insert 127 pop 128 remove 129 reverse 130 sort 131 """
132 - def __init__(self, list_of_strings):
133 """ 134 arg should be a list of strings which are filenames 135 """ 136 super(file_series, self).__init__( list_of_strings ) 137 # track current position in list 138 self._current = 0
139 140 141 # methods which return a filename 142
143 - def first(self):
144 """ first image in series """ 145 return self[0]
146
147 - def last(self):
148 """ last in series """ 149 return self[-1]
150
151 - def previous(self):
152 """ prev in a sequence""" 153 self._current -= 1 154 return self[self._current]
155
156 - def current(self):
157 """ current position in a sequence """ 158 return self[self._current]
159
160 - def next(self):
161 """ next in a sequence """ 162 self._current += 1 163 return self[self._current]
164
165 - def jump(self, num):
166 """ goto a position in sequence """ 167 assert num < len(self) and num > 0, "num out of range" 168 self._current = num 169 return self[self._current]
170
171 - def len(self):
172 """ number of files""" 173 return len(self)
174 175 176 # Methods which return a fabioimage 177
178 - def first_image(self):
179 """ first image in a sequence """ 180 return openimage(self.first())
181
182 - def last_image(self):
183 """ last image in a sequence """ 184 return openimage(self.last())
185
186 - def next_image(self):
187 """ Return the next image """ 188 return openimage(self.next())
189
190 - def previous_image(self):
191 """ Return the previous image """ 192 return openimage(self.previous())
193
194 - def jump_image(self, num):
195 """ jump to and read image """ 196 return openimage(self.jump(num))
197
198 - def current_image(self):
199 """ current image in sequence """ 200 return openimage(self.current())
201 202 # methods which return a file_object 203
204 - def first_object(self):
205 """ first image in a sequence """ 206 return filename_object(self.first())
207
208 - def last_object(self):
209 """ last image in a sequence """ 210 return filename_object(self.last())
211
212 - def next_object(self):
213 """ Return the next image """ 214 return filename_object(self.next())
215
216 - def previous_object(self):
217 """ Return the previous image """ 218 return filename_object(self.previous())
219
220 - def jump_object(self, num):
221 """ jump to and read image """ 222 return filename_object(self.jump(num))
223
224 - def current_object(self):
225 """ current image in sequence """ 226 return filename_object(self.current())
227 228 229 230
231 -class numbered_file_series(file_series):
232 """ 233 mydata0001.edf = "mydata" + 0001 + ".edf" 234 mydata0002.edf = "mydata" + 0002 + ".edf" 235 mydata0003.edf = "mydata" + 0003 + ".edf" 236 """
237 - def __init__(self, stem, first, last, extension, 238 digits = 4, padding='Y', step = 1):
239 """ 240 stem - first part of the name 241 step - in case of every nth file 242 padding - possibility for specifying that numbers are not padded 243 with zeroes up to digits 244 """ 245 if padding == 'Y': 246 fmt = "%s%0"+str(digits)+"d%s" 247 else: 248 fmt = "%s%i%s" 249 250 super(numbered_file_series, self).__init__( 251 [ fmt % ( stem, i, extension ) for i in range(first, 252 last + 1, 253 step) ] )
254 255
256 -class filename_series:
257 """ Much like the others, but created from a string filename """
258 - def __init__(self, filename):
259 """ create from a filename (String)""" 260 self.obj = filename_object(filename)
261
262 - def next(self):
263 """ increment number """ 264 self.obj.num += 1 265 return self.obj.tostring()
266
267 - def previous(self):
268 """ decrement number """ 269 self.obj.num -= 1 270 return self.obj.tostring()
271
272 - def current(self):
273 """ return current filename string""" 274 return self.obj.tostring()
275
276 - def jump(self, num):
277 """ jump to a specific number """ 278 self.obj.num = num 279 return self.obj.tostring()
280 281 # image methods
282 - def next_image(self):
283 """ returns the next image as a fabioimage """ 284 return openimage(self.next())
285 - def prev_image(self):
286 """ returns the previos image as a fabioimage """ 287 return openimage(self.previous())
288 - def current_image(self):
289 """ returns the current image as a fabioimage""" 290 return openimage(self.current())
291 - def jump_image(self, num):
292 """ returns the image number as a fabioimage""" 293 return openimage(self.jump(num))
294 # object methods
295 - def next_object(self):
296 """ returns the next filename as a fabio.filename_object""" 297 self.obj.num += 1 298 return self.obj
299 - def previous_object(self):
300 """ returns the previous filename as a fabio.filename_object""" 301 self.obj.num -= 1 302 return self.obj
303 - def current_object(self):
304 """ returns the current filename as a fabio.filename_object""" 305 return self.obj
306 - def jump_object(self, num):
307 """ returns the filename num as a fabio.filename_object""" 308 self.obj.num = num 309 return self.obj
310