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

Source Code for Module fabio.file_series

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