Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def setUp(self):
""" create test files"""
if not os.path.isfile(self.testfile):
open(self.testfile, "wb").write(b"{ hello }")
if not os.path.isfile(self.testfile + ".gz"):
gzip.open(self.testfile + ".gz", "wb").write(b"{ hello }")
if not os.path.isfile(self.testfile + ".bz2"):
bz2.BZ2File(self.testfile + ".bz2", "wb").write(b"{ hello }")
self.obj = fabioimage()
def testpil(self):
for typ in self.okformats:
name = NAMES[typ]
for shape in [(10, 20), (431, 1325)]:
testdata = self.mkdata(shape, typ)
img = fabioimage(testdata, {"title":"Random data"})
pim = img.toPIL16()
for i in [ 0, 5, 6, shape[1] - 1 ]:
for j in [0, 5, 7, shape[0] - 1 ]:
errstr = name + " %d %d %f %f t=%s" % (
i, j, testdata[j, i], pim.getpixel((i, j)), typ)
er1 = img.data[j, i] - pim.getpixel((i, j))
er2 = img.data[j, i] + pim.getpixel((i, j))
# difference as % error in case of rounding
if er2 != 0.:
err = er1 / er2
else:
err = er1
self.assertAlmostEquals(err, 0, 6, errstr)
def setUp(self):
"""make test data"""
dat2 = numpy.zeros((1024, 1024), numpy.uint16)
hed = {"Title": "zeros and 100"}
self.cord = [256, 256, 790, 768]
self.obj = fabioimage(dat2, hed)
self.slic = slic = self.obj.make_slice(self.cord)
# Note - d2 is modified *after* fabioimage is made
dat2[slic] = dat2[slic] + 100
assert self.obj.maxval is None
assert self.obj.minval is None
self.npix = (slic[0].stop - slic[0].start) * \
(slic[1].stop - slic[1].start)
data = (data / data.max() * ((2 ** 32) - 1)).astype(np.uint32).copy()
self.data = data
return self
class fitsimage(fabioimage):
DESCRIPTION = "FITS file format from astronomy"
DEFAULT_EXTENTIONS = DEFAULT_EXTENSIONS = ["fits"]
def read(self, f, frame=None):
self.data = np.rot90(np.fliplr(pyfits.open(f)[2].data), 2)
return self
class gbimage(fabioimage):
DEFAULT_EXTENTIONS = DEFAULT_EXTENSIONS = ['gb']
def read(self, f, frame=None):
data = np.fromfile(f, np.float32)
if len(data) == 1475 * 1679:
data.shape = (1679, 1475)
elif len(data) == 981 * 1043:
data.shape = (1043, 981)
elif len(data) == 1475 * 195:
data.shape = (195, 1475)
self.data = data
return self
class rawimage(fabioimage):
DEFAULT_EXTENTIONS = DEFAULT_EXTENSIONS = ['raw']
def __init__(self, data=None, mask=None):
"""Constructor of the class...
:param data: 2D array or filename (discouraged)
:param mask: array with non zero for invalid data
"""
if isinstance(data, six.string_types) and os.path.isfile(data):
self.data = fabio.open(data).data.astype("float32")
elif isinstance(data, fabio.fabioimage.fabioimage):
self.data = data.data.astype("float32")
else:
try:
self.data = data.astype("float32")
except Exception as error:
logger.error("Unable to understand this type of data %s: %s", data, error)
self.mask = mask
self._cleaned_data = None
self._bilin = Bilinear(self.data)
self._blurred_data = None
self._median_data = None
self._labeled_massif = None
self._number_massif = None
self._valley_size = None
self._binned_data = None
self._reconstruct_used = None
return self.getframe(self.currentframe)
else:
raise StopIteration
def close(self):
pass
class npyimage(fabioimage):
DEFAULT_EXTENTIONS = DEFAULT_EXTENSIONS = ['npy']
def read(self, f, frame=None):
self.data = np.load(f)
return self
class hipgisaxsimage(fabioimage):
DEFAULT_EXTENTIONS = DEFAULT_EXTENSIONS = ['out']
def read(self, f, frame=None):
data = np.loadtxt(f)
data = (data / data.max() * ((2 ** 32) - 1)).astype(np.uint32).copy()
self.data = data
return self
class fitsimage(fabioimage):
DESCRIPTION = "FITS file format from astronomy"
DEFAULT_EXTENTIONS = DEFAULT_EXTENSIONS = ["fits"]
def read(self, f, frame=None):
self.data = np.rot90(np.fliplr(pyfits.open(f)[2].data), 2)
# obj.read(filename)
# except H5ReadError:
# # Skip exception and try the next H5 image class
# continue
# else:
# # If not error was thrown break out of loop
# break
# else:
# # If for loop finished without breaking raise ReadError
# raise H5ReadError('H5 format not recognized')
# return obj # return the successfully read object
@register_h5class
class ALS733H5image(fabioimage):
DEFAULT_EXTENTIONS = DEFAULT_EXTENSIONS = ['h5']
def _readheader(self, f):
fname = f.name # get filename from file object
with h5py.File(fname, 'r') as h:
self.header = dict(h.attrs)
def read(self, f, frame=None):
self.readheader(f)
# Check header for unique attributes
try:
if self.header['facility'] != 'als' or self.header['end_station'] != 'bl733':
raise H5ReadError
except KeyError:
raise H5ReadError
def read(self, f, frame=None):
self.data = np.load(f)
return self
class hipgisaxsimage(fabioimage):
DEFAULT_EXTENTIONS = DEFAULT_EXTENSIONS = ['out']
def read(self, f, frame=None):
data = np.loadtxt(f)
data = (data / data.max() * ((2 ** 32) - 1)).astype(np.uint32).copy()
self.data = data
return self
class fitsimage(fabioimage):
DESCRIPTION = "FITS file format from astronomy"
DEFAULT_EXTENTIONS = DEFAULT_EXTENSIONS = ["fits"]
def read(self, f, frame=None):
self.data = np.rot90(np.fliplr(pyfits.open(f)[2].data), 2)
return self
class gbimage(fabioimage):
DEFAULT_EXTENTIONS = DEFAULT_EXTENSIONS = ['gb']
def read(self, f, frame=None):
data = np.fromfile(f, np.float32)
if len(data) == 1475 * 1679:
data.shape = (1679, 1475)
class gbimage(fabioimage):
DEFAULT_EXTENTIONS = DEFAULT_EXTENSIONS = ['gb']
def read(self, f, frame=None):
data = np.fromfile(f, np.float32)
if len(data) == 1475 * 1679:
data.shape = (1679, 1475)
elif len(data) == 981 * 1043:
data.shape = (1043, 981)
elif len(data) == 1475 * 195:
data.shape = (195, 1475)
self.data = data
return self
class rawimage(fabioimage):
DEFAULT_EXTENTIONS = DEFAULT_EXTENSIONS = ['raw']
def read(self, f, frame=None):
with open(f, 'r') as f:
data = np.fromfile(f, dtype=np.int32)
for name, detector in detectors.ALL_DETECTORS.iteritems():
if hasattr(detector, 'MAX_SHAPE'):
# print name, detector.MAX_SHAPE, imgdata.shape[::-1]
if np.prod(detector.MAX_SHAPE) == len(data): #
detector = detector()
msg.logMessage('Detector found: ' + name, msg.INFO)
break
if hasattr(detector, 'BINNED_PIXEL_SIZE'):
# print detector.BINNED_PIXEL_SIZE.keys()
if len(data) in [np.prod(np.array(detector.MAX_SHAPE) / b) for b in
detector.BINNED_PIXEL_SIZE.keys()]:
h5classes = list()
tiffclasses = list()
def register_tiffclass(cls):
global tiffclasses
tiffclasses.append(cls)
return cls
def register_h5class(cls):
global h5classes
h5classes.append(cls)
return cls
class xicamtiffimage(fabioimage):
DEFAULT_EXTENTIONS = DEFAULT_EXTENSIONS = ['tiff', 'tif']
def read(self, filename, frame=None):
for tiff in tiffclasses:
if hasattr(tiff,'validate'): # check which class preferably based on the validate staticmethod
try:
tiff.validate(filename, frame)
except Exception as ex:
continue
try: # if there isn't one, try to read with this class
return xicamtiffimage._instantiate_read(tiff,filename,frame)
except Exception as ex:
continue
# if custom classes fail, use built-in class
return xicamtiffimage._instantiate_read(fabio.tifimage.tifimage, filename, frame)