fabio 0.1.1__zip

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (67) hide show
  1. Library/Python/2.7/site-packages/fabio/GEimage.py +334 -0
  2. Library/Python/2.7/site-packages/fabio/GEimage.pyc +0 -0
  3. Library/Python/2.7/site-packages/fabio/GEimage_old.py +501 -0
  4. Library/Python/2.7/site-packages/fabio/GEimage_old.pyc +0 -0
  5. Library/Python/2.7/site-packages/fabio/HiPiCimage.py +106 -0
  6. Library/Python/2.7/site-packages/fabio/HiPiCimage.pyc +0 -0
  7. Library/Python/2.7/site-packages/fabio/OXDimage.py +442 -0
  8. Library/Python/2.7/site-packages/fabio/OXDimage.pyc +0 -0
  9. Library/Python/2.7/site-packages/fabio/TiffIO.py +1197 -0
  10. Library/Python/2.7/site-packages/fabio/TiffIO.pyc +0 -0
  11. Library/Python/2.7/site-packages/fabio/__init__.py +10 -0
  12. Library/Python/2.7/site-packages/fabio/__init__.pyc +0 -0
  13. Library/Python/2.7/site-packages/fabio/adscimage.py +137 -0
  14. Library/Python/2.7/site-packages/fabio/adscimage.pyc +0 -0
  15. Library/Python/2.7/site-packages/fabio/binaryimage.py +87 -0
  16. Library/Python/2.7/site-packages/fabio/binaryimage.pyc +0 -0
  17. Library/Python/2.7/site-packages/fabio/bruker100image.py +96 -0
  18. Library/Python/2.7/site-packages/fabio/bruker100image.pyc +0 -0
  19. Library/Python/2.7/site-packages/fabio/brukerimage.py +195 -0
  20. Library/Python/2.7/site-packages/fabio/brukerimage.pyc +0 -0
  21. Library/Python/2.7/site-packages/fabio/byte_offset.so +0 -0
  22. Library/Python/2.7/site-packages/fabio/cbfimage.py +758 -0
  23. Library/Python/2.7/site-packages/fabio/cbfimage.pyc +0 -0
  24. Library/Python/2.7/site-packages/fabio/cf_io.so +0 -0
  25. Library/Python/2.7/site-packages/fabio/compression.py +388 -0
  26. Library/Python/2.7/site-packages/fabio/compression.pyc +0 -0
  27. Library/Python/2.7/site-packages/fabio/converters.py +54 -0
  28. Library/Python/2.7/site-packages/fabio/converters.pyc +0 -0
  29. Library/Python/2.7/site-packages/fabio/datIO.py +60 -0
  30. Library/Python/2.7/site-packages/fabio/datIO.pyc +0 -0
  31. Library/Python/2.7/site-packages/fabio/dm3image.py +219 -0
  32. Library/Python/2.7/site-packages/fabio/dm3image.pyc +0 -0
  33. Library/Python/2.7/site-packages/fabio/edfimage.py +924 -0
  34. Library/Python/2.7/site-packages/fabio/edfimage.pyc +0 -0
  35. Library/Python/2.7/site-packages/fabio/fabioimage.py +556 -0
  36. Library/Python/2.7/site-packages/fabio/fabioimage.pyc +0 -0
  37. Library/Python/2.7/site-packages/fabio/fabioutils.py +491 -0
  38. Library/Python/2.7/site-packages/fabio/fabioutils.pyc +0 -0
  39. Library/Python/2.7/site-packages/fabio/file_series.py +313 -0
  40. Library/Python/2.7/site-packages/fabio/file_series.pyc +0 -0
  41. Library/Python/2.7/site-packages/fabio/fit2dmaskimage.py +94 -0
  42. Library/Python/2.7/site-packages/fabio/fit2dmaskimage.pyc +0 -0
  43. Library/Python/2.7/site-packages/fabio/fit2dspreadsheetimage.py +85 -0
  44. Library/Python/2.7/site-packages/fabio/fit2dspreadsheetimage.pyc +0 -0
  45. Library/Python/2.7/site-packages/fabio/kcdimage.py +131 -0
  46. Library/Python/2.7/site-packages/fabio/kcdimage.pyc +0 -0
  47. Library/Python/2.7/site-packages/fabio/mar345_IO.so +0 -0
  48. Library/Python/2.7/site-packages/fabio/mar345image.py +320 -0
  49. Library/Python/2.7/site-packages/fabio/mar345image.pyc +0 -0
  50. Library/Python/2.7/site-packages/fabio/marccdimage.py +309 -0
  51. Library/Python/2.7/site-packages/fabio/marccdimage.pyc +0 -0
  52. Library/Python/2.7/site-packages/fabio/openimage.py +162 -0
  53. Library/Python/2.7/site-packages/fabio/openimage.pyc +0 -0
  54. Library/Python/2.7/site-packages/fabio/pilatusimage.py +81 -0
  55. Library/Python/2.7/site-packages/fabio/pilatusimage.pyc +0 -0
  56. Library/Python/2.7/site-packages/fabio/pnmimage.py +165 -0
  57. Library/Python/2.7/site-packages/fabio/pnmimage.pyc +0 -0
  58. Library/Python/2.7/site-packages/fabio/readbytestream.py +86 -0
  59. Library/Python/2.7/site-packages/fabio/readbytestream.pyc +0 -0
  60. Library/Python/2.7/site-packages/fabio/tifimage.py +283 -0
  61. Library/Python/2.7/site-packages/fabio/tifimage.pyc +0 -0
  62. Library/Python/2.7/site-packages/fabio/xsdimage.py +134 -0
  63. Library/Python/2.7/site-packages/fabio/xsdimage.pyc +0 -0
  64. Library/Python/2.7/site-packages/fabio-0.1.1-py2.7.egg-info/PKG-INFO +11 -0
  65. Library/Python/2.7/site-packages/fabio-0.1.1-py2.7.egg-info/SOURCES.txt +114 -0
  66. Library/Python/2.7/site-packages/fabio-0.1.1-py2.7.egg-info/dependency_links.txt +1 -0
  67. Library/Python/2.7/site-packages/fabio-0.1.1-py2.7.egg-info/top_level.txt +4 -0
@@ -0,0 +1,491 @@
1
+ #!/usr/bin/env python
2
+ #coding: utf8
3
+
4
+ """
5
+ General purpose utilities functions for fabio
6
+ """
7
+ from __future__ import with_statement
8
+ import re, os, logging, threading, sys
9
+ import StringIO as stringIO
10
+ logger = logging.getLogger("fabioutils")
11
+ from compression import bz2, gzip
12
+
13
+
14
+ FILETYPES = {
15
+ # extension NNNimage fabioclass
16
+ # type consistency - always use a list if one case is
17
+ 'edf' : ['edf'],
18
+ 'cor' : ['edf'],
19
+ 'pnm' : ['pnm'],
20
+ 'pgm' : ['pnm'],
21
+ 'pbm' : ['pnm'],
22
+ 'tif' : ['tif'],
23
+ 'tiff' : ['tif'],
24
+ 'img' : ['adsc', 'OXD', 'HiPiC'],
25
+ 'mccd' : ['marccd'],
26
+ 'mar2300': ['mar345'],
27
+ 'sfrm' : ['bruker100'],
28
+ 'msk' : ['fit2dmask'],
29
+ 'spr' : ['fit2dspreadsheet'],
30
+ 'dm3' : ['dm3'],
31
+ 'kcd' : ['kcd'],
32
+ 'cbf' : ['cbf'],
33
+ 'xml' : ["xsd"],
34
+ 'xsd' : ["xsd"],
35
+ }
36
+
37
+ # Add bzipped and gzipped
38
+ for key in FILETYPES.keys():
39
+ FILETYPES[key + ".bz2"] = FILETYPES[key]
40
+ FILETYPES[key + ".gz"] = FILETYPES[key]
41
+
42
+
43
+ # Compressors
44
+
45
+ COMPRESSORS = {}
46
+
47
+
48
+ dictAscii = {None:[chr(i) for i in range(32, 127)],
49
+ }
50
+
51
+ try:
52
+ lines = os.popen("gzip -h 2>&1").read()
53
+ # Looking for "usage"
54
+ if "sage" in lines:
55
+ COMPRESSORS['.gz'] = 'gzip -dc '
56
+ else:
57
+ COMPRESSORS['.gz'] = None
58
+ except:
59
+ COMPRESSORS['.gz'] = None
60
+
61
+ try:
62
+ lines = os.popen("bzip2 -h 2>&1").read()
63
+ # Looking for "usage"
64
+ if "sage" in lines:
65
+ COMPRESSORS['.bz2'] = 'bzip2 -dc '
66
+ else:
67
+ COMPRESSORS['.bz2'] = None
68
+ except:
69
+ COMPRESSORS['.bz2'] = None
70
+
71
+
72
+ def getnum(name):
73
+ """
74
+ # try to figure out a file number
75
+ # guess it starts at the back
76
+ """
77
+ stem , num, post_num = numstem(name)
78
+ try:
79
+ return int(num)
80
+ except ValueError:
81
+ return None
82
+
83
+ class filename_object:
84
+ """
85
+ The 'meaning' of a filename
86
+ """
87
+ def __init__(self, stem,
88
+ num=None,
89
+ directory=None,
90
+ format=None,
91
+ extension=None,
92
+ postnum=None,
93
+ digits=4):
94
+ self.stem = stem
95
+ self.num = num
96
+ self.format = format
97
+ self.extension = extension
98
+ self.digits = digits
99
+ self.postnum = postnum
100
+ self.directory = directory
101
+
102
+ def str(self):
103
+ """ Return a string representation """
104
+ fmt = "stem %s, num %s format %s extension %s " + \
105
+ "postnum = %s digits %s dir %s"
106
+ return fmt % tuple([str(x) for x in [
107
+ self.stem ,
108
+ self.num ,
109
+ self.format ,
110
+ self.extension ,
111
+ self.postnum ,
112
+ self.digits ,
113
+ self.directory ] ])
114
+
115
+
116
+ def tostring(self):
117
+ """
118
+ convert yourself to a string
119
+ """
120
+ name = self.stem
121
+ if self.digits is not None and self.num is not None:
122
+ fmt = "%0" + str(self.digits) + "d"
123
+ name += fmt % self.num
124
+ if self.postnum is not None:
125
+ name += self.postnum
126
+ if self.extension is not None:
127
+ name += self.extension
128
+ if self.directory is not None:
129
+ name = os.path.join(self.directory, name)
130
+ return name
131
+
132
+
133
+ def numstem(name):
134
+ """ cant see how to do without reversing strings
135
+ Match 1 or more digits going backwards from the end of the string
136
+ """
137
+ reg = re.compile(r"^(.*?)(-?[0-9]{0,9})(\D*)$")
138
+ #reg = re.compile("""(\D*)(\d\d*)(\w*)""")
139
+ try:
140
+ res = reg.match(name).groups()
141
+ #res = reg.match(name[::-1]).groups()
142
+ #return [ r[::-1] for r in res[::-1]]
143
+ if len(res[0]) == len(res[1]) == 0: # Hack for file without number
144
+ return [res[2], '', '']
145
+ return [ r for r in res]
146
+ except AttributeError: # no digits found
147
+ return [name, "", ""]
148
+
149
+ def deconstruct_filename(filename):
150
+ """
151
+ Break up a filename to get image type and number
152
+ """
153
+ direc, name = os.path.split(filename)
154
+ direc = direc or None
155
+ parts = name.split(".")
156
+ compressed = False
157
+ stem = parts[0]
158
+ extn = ""
159
+ postnum = ""
160
+ ndigit = 4
161
+ num = None
162
+ typ = None
163
+ if parts[-1] in ["gz", "bz2"]:
164
+ extn = "." + parts[-1]
165
+ parts = parts[:-1]
166
+ compressed = True
167
+ if parts[-1] in FILETYPES.keys():
168
+ typ = FILETYPES[parts[-1]]
169
+ extn = "." + parts[-1] + extn
170
+ try:
171
+ stem, numstring, postnum = numstem(".".join(parts[:-1]))
172
+ num = int(numstring)
173
+ ndigit = len(numstring)
174
+ except Exception, err:
175
+ # There is no number - hence make num be None, not 0
176
+ logger.debug("l176: %s" % err)
177
+ num = None
178
+ stem = "".join(parts[:-1])
179
+ else:
180
+ # Probably two type left
181
+ if len(parts) == 1:
182
+ # Probably GE format stem_numb
183
+ parts2 = parts[0].split("_")
184
+ if parts2[-1].isdigit():
185
+ num = int(parts2[-1])
186
+ ndigit = len(parts2[-1])
187
+ typ = ['GE']
188
+ stem = "_".join(parts2[:-1]) + "_"
189
+ else:
190
+ try:
191
+ num = int(parts[-1])
192
+ ndigit = len(parts[-1])
193
+ typ = ['bruker']
194
+ stem = ".".join(parts[:-1]) + "."
195
+ except Exception, err:
196
+ logger.debug("l196: %s" % err)
197
+ typ = None
198
+ extn = "." + parts[-1] + extn
199
+ try:
200
+ stem , numstring, postnum = numstem(".".join(parts[:-1]))
201
+ num = int(numstring)
202
+ ndigit = len(numstring)
203
+ except Exception, err:
204
+ logger.debug("l204: %s" % err)
205
+ raise
206
+ # raise Exception("Cannot decode "+filename)
207
+ obj = filename_object(stem,
208
+ num=num,
209
+ directory=direc,
210
+ format=typ,
211
+ extension=extn,
212
+ postnum=postnum,
213
+ digits=ndigit)
214
+ return obj
215
+
216
+ def construct_filename(filename, frame):
217
+ "Try to construct the filename for a given frame"
218
+ fo = deconstruct_filename(filename)
219
+ fo.num = frame
220
+ return fo.tostring()
221
+
222
+ def next_filename(name, padding=True):
223
+ """ increment number """
224
+ obj = deconstruct_filename(name)
225
+ obj.num += 1
226
+ if not padding:
227
+ obj.digits = 0
228
+ return obj.tostring()
229
+
230
+ def previous_filename(name, padding=True):
231
+ """ decrement number """
232
+ obj = deconstruct_filename(name)
233
+ obj.num -= 1
234
+ if not padding:
235
+ obj.digits = 0
236
+ return obj.tostring()
237
+
238
+ def jump_filename(name, num, padding=True):
239
+ """ jump to number """
240
+ obj = deconstruct_filename(name)
241
+ obj.num = num
242
+ if not padding:
243
+ obj.digits = 0
244
+ return obj.tostring()
245
+
246
+
247
+ def extract_filenumber(name):
248
+ """ extract file number """
249
+ obj = deconstruct_filename(name)
250
+ return obj.num
251
+
252
+ def isAscii(name, listExcluded=None):
253
+ """
254
+ @param name: string to check
255
+ @param listExcluded: list of char or string excluded.
256
+ @return: True of False whether name is pure ascii or not
257
+ """
258
+ isascii = None
259
+ try:
260
+ name.decode("ascii")
261
+ except UnicodeDecodeError:
262
+ isascii = False
263
+ else:
264
+ if listExcluded:
265
+ isascii = not(any(bad in name for bad in listExcluded))
266
+ else:
267
+ isascii = True
268
+ return isascii
269
+
270
+ def toAscii(name, excluded=None):
271
+ """
272
+ @param name: string to check
273
+ @param excluded: tuple of char or string excluded (not list: they are mutable).
274
+ @return: the name with all non valid char removed
275
+ """
276
+ if excluded not in dictAscii:
277
+ ascii = dictAscii[None][:]
278
+ for i in excluded:
279
+ if i in ascii:
280
+ ascii.remove(i)
281
+ else:
282
+ logger.error("toAscii: % not in ascii table" % i)
283
+ dictAscii[excluded] = ascii
284
+ else:
285
+ ascii = dictAscii[excluded]
286
+ out = [i for i in str(name) if i in ascii]
287
+ return "".join(out)
288
+
289
+ def nice_int(s):
290
+ """
291
+ Workaround that int('1.0') raises an exception
292
+
293
+ @param s: string to be converted to integer
294
+ """
295
+ try:
296
+ return int(s)
297
+ except ValueError:
298
+ return int(float(s))
299
+
300
+
301
+ class StringIO(stringIO.StringIO):
302
+ """
303
+ just an interface providing the name and mode property to a StringIO
304
+
305
+ BugFix for MacOSX mainly
306
+ """
307
+ def __init__(self, data, fname=None, mode="r"):
308
+ stringIO.StringIO.__init__(self, data)
309
+ self.closed = False
310
+ if fname == None:
311
+ self.name = "fabioStream"
312
+ else:
313
+ self.name = fname
314
+ self.mode = mode
315
+ self.lock = threading.Semaphore()
316
+ self.__size = None
317
+
318
+ def getSize(self):
319
+ if self.__size is None:
320
+ logger.debug("Measuring size of %s" % self.name)
321
+ with self.lock:
322
+ pos = self.tell()
323
+ self.seek(0, os.SEEK_END)
324
+ self.__size = self.tell()
325
+ self.seek(pos)
326
+ return self.__size
327
+ def setSize(self, size):
328
+ self.__size = size
329
+ size = property(getSize, setSize)
330
+
331
+ class File(file):
332
+ """
333
+ wrapper for "file" with locking
334
+ """
335
+ def __init__(self, name, mode="rb", buffering=0):
336
+ """file(name[, mode[, buffering]]) -> file object
337
+
338
+ Open a file. The mode can be 'r', 'w' or 'a' for reading (default),
339
+ writing or appending. The file will be created if it doesn't exist
340
+ when opened for writing or appending; it will be truncated when
341
+ opened for writing. Add a 'b' to the mode for binary files.
342
+ Add a '+' to the mode to allow simultaneous reading and writing.
343
+ If the buffering argument is given, 0 means unbuffered, 1 means line
344
+ buffered, and larger numbers specify the buffer size. The preferred way
345
+ to open a file is with the builtin open() function.
346
+ Add a 'U' to mode to open the file for input with universal newline
347
+ support. Any line ending in the input file will be seen as a '\n'
348
+ in Python. Also, a file so opened gains the attribute 'newlines';
349
+ the value for this attribute is one of None (no newline read yet),
350
+ '\r', '\n', '\r\n' or a tuple containing all the newline types seen.
351
+
352
+ 'U' cannot be combined with 'w' or '+' mode.
353
+ """
354
+ file.__init__(self, name, mode, buffering)
355
+ self.lock = threading.Semaphore()
356
+ self.__size = None
357
+ def getSize(self):
358
+ if self.__size is None:
359
+ logger.debug("Measuring size of %s" % self.name)
360
+ with self.lock:
361
+ pos = self.tell()
362
+ self.seek(0, os.SEEK_END)
363
+ self.__size = self.tell()
364
+ self.seek(pos)
365
+ return self.__size
366
+ def setSize(self, size):
367
+ self.__size = size
368
+ size = property(getSize, setSize)
369
+
370
+ class UnknownCompressedFile(File):
371
+ """
372
+ wrapper for "File" with locking
373
+ """
374
+ def __init__(self, name, mode="rb", buffering=0):
375
+ logger.warning("No decompressor found for this type of file (are gzip anf bz2 installed ???")
376
+ File.__init__(self, name, mode, buffering)
377
+
378
+ if gzip is None:
379
+ GzipFile = UnknownCompressedFile
380
+ else:
381
+ class GzipFile(gzip.GzipFile):
382
+ """
383
+ Just a wrapper forgzip.GzipFile providing the correct seek capabilities for python 2.5
384
+ """
385
+ def __init__(self, filename=None, mode=None, compresslevel=9, fileobj=None):
386
+ """
387
+ Wrapper with locking for constructor for the GzipFile class.
388
+
389
+ At least one of fileobj and filename must be given a
390
+ non-trivial value.
391
+
392
+ The new class instance is based on fileobj, which can be a regular
393
+ file, a StringIO object, or any other object which simulates a file.
394
+ It defaults to None, in which case filename is opened to provide
395
+ a file object.
396
+
397
+ When fileobj is not None, the filename argument is only used to be
398
+ included in the gzip file header, which may includes the original
399
+ filename of the uncompressed file. It defaults to the filename of
400
+ fileobj, if discernible; otherwise, it defaults to the empty string,
401
+ and in this case the original filename is not included in the header.
402
+
403
+ The mode argument can be any of 'r', 'rb', 'a', 'ab', 'w', or 'wb',
404
+ depending on whether the file will be read or written. The default
405
+ is the mode of fileobj if discernible; otherwise, the default is 'rb'.
406
+ Be aware that only the 'rb', 'ab', and 'wb' values should be used
407
+ for cross-platform portability.
408
+
409
+ The compresslevel argument is an integer from 1 to 9 controlling the
410
+ level of compression; 1 is fastest and produces the least compression,
411
+ and 9 is slowest and produces the most compression. The default is 9.
412
+ """
413
+ gzip.GzipFile.__init__(self, filename, mode, compresslevel, fileobj)
414
+ self.lock = threading.Semaphore()
415
+ self.__size = None
416
+
417
+
418
+ if sys.version_info < (2, 7):
419
+ def getSize(self):
420
+ if self.__size is None:
421
+ logger.debug("Measuring size of %s" % self.name)
422
+ with open(self.filename, "rb") as f:
423
+ f.seek(-4)
424
+ self.__size = numpy.fromstring(f.read(4), dtype=numpy.uint32)
425
+ return self.__size
426
+ def setSize(self, value):
427
+ self.__size = value
428
+ size = property(getSize, setSize)
429
+ @property
430
+ def closed(self):
431
+ return self.fileobj is None
432
+
433
+ def seek(self, offset, whence=os.SEEK_SET):
434
+ """
435
+ Move to new file position.
436
+
437
+ Argument offset is a byte count. Optional argument whence defaults to
438
+ 0 (offset from start of file, offset should be >= 0); other values are 1
439
+ (move relative to current position, positive or negative), and 2 (move
440
+ relative to end of file, usually negative, although many platforms allow
441
+ seeking beyond the end of a file). If the file is opened in text mode,
442
+ only offsets returned by tell() are legal. Use of other offsets causes
443
+ undefined behavior.
444
+
445
+ This is a wrapper for seek to ensure compatibility with old python 2.5
446
+ """
447
+ if whence == os.SEEK_SET:
448
+ gzip.GzipFile.seek(self, offset)
449
+ elif whence == os.SEEK_CUR:
450
+ gzip.GzipFile.seek(self, offset + self.tell())
451
+ elif whence == os.SEEK_END:
452
+ gzip.GzipFile.seek(self, -1)
453
+ gzip.GzipFile.seek(self, offset + self.tell())
454
+
455
+ if bz2 is None:
456
+ BZ2File = UnknownCompressedFile
457
+ else:
458
+ class BZ2File(bz2.BZ2File):
459
+ "Wrapper with lock"
460
+ def __init__(self, name , mode='r', buffering=0, compresslevel=9):
461
+ """
462
+ BZ2File(name [, mode='r', buffering=0, compresslevel=9]) -> file object
463
+
464
+ Open a bz2 file. The mode can be 'r' or 'w', for reading (default) or
465
+ writing. When opened for writing, the file will be created if it doesn't
466
+ exist, and truncated otherwise. If the buffering argument is given, 0 means
467
+ unbuffered, and larger numbers specify the buffer size. If compresslevel
468
+ is given, must be a number between 1 and 9.
469
+
470
+ Add a 'U' to mode to open the file for input with universal newline
471
+ support. Any line ending in the input file will be seen as a '\n' in
472
+ Python. Also, a file so opened gains the attribute 'newlines'; the value
473
+ for this attribute is one of None (no newline read yet), '\r', '\n',
474
+ '\r\n' or a tuple containing all the newline types seen. Universal
475
+ newlines are available only when reading.
476
+ """
477
+ bz2.BZ2File.__init__(self, name , mode, buffering, compresslevel)
478
+ self.lock = threading.Semaphore()
479
+ self.__size = None
480
+ def getSize(self):
481
+ if self.__size is None:
482
+ logger.debug("Measuring size of %s" % self.name)
483
+ with self.lock:
484
+ pos = self.tell()
485
+ all = self.read()
486
+ self.__size = self.tell()
487
+ self.seek(pos)
488
+ return self.__size
489
+ def setSize(self, value):
490
+ self.__size = value
491
+ size = property(getSize, setSize)