Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
system_uncompress,
python_uncompress,
mode='rb'):
"""
Try to transparently handle gzip / bzip2 without always getting python
performance
"""
# assert that python modules are always OK based on performance benchmark
# Try to fix the way we are using them?
fobj = None
if self._need_a_real_file and mode[0] == "r":
fo = python_uncompress(fname, mode)
# problem when not administrator under certain flavors of windows
tmpfd, tmpfn = tempfile.mkstemp()
os.close(tmpfd)
fobj = fabioutils.File(tmpfn, "w+b", temporary=True)
fobj.write(fo.read())
fo.close()
fobj.seek(0)
elif self._need_a_seek_to_read and mode[0] == "r":
fo = python_uncompress(fname, mode)
fobj = fabioutils.BytesIO(fo.read(), fname, mode)
else:
fobj = python_uncompress(fname, mode)
return fobj
def __init__(self, data=None, header=None):
"""Set up initial values
:param data: numpy array of values
:param header: dict or ordereddict with metadata
"""
super(FabioImage, self).__init__()
self._classname = None
self._shape = None
self._dtype = None
self._file = None
if type(data) in fabioutils.StringTypes:
raise TypeError("Data should be numpy array")
self.data = self.check_data(data)
self.header = self.check_header(header)
# cache for image statistics
self._nframes = 1
self.currentframe = 0
self.filename = None
self.filenumber = None
self.resetvals()
def write(self, fname):
"""
To be overwritten - write the file
"""
if isinstance(fname, fabioutils.PathTypes):
if not isinstance(fname, fabioutils.StringTypes):
fname = str(fname)
module = sys.modules[self.__class__.__module__]
raise NotImplementedError("Writing %s format is not implemented" % module.__name__)
def load(self, filename):
"""
Loads an image using the fabio library.
:param filename: path to the image file to be loaded
:return: dictionary with image_data and image_data_fabio, None if unsuccessful
"""
try:
img_data_fabio = fabio.open(filename)
self.img_data = img_data_fabio.data[::-1]
self.filename = filename
except (IOError, fabio.fabioutils.NotGoodReader):
return None
It returns a FabioImage-class instance which can be used as a context manager to close the file
at the termination.
.. code-block:: python
with fabio.open("image.edf") as i:
print(i.nframes)
print(i.data)
:param Union[str,FilenameObject] filename: A filename or a filename
iterator.
:param Union[int,None] frame: A specific frame inside this file.
:rtype: FabioImage
"""
if isinstance(filename, fabioutils.PathTypes):
if not isinstance(filename, fabioutils.StringTypes):
filename = str(filename)
if isinstance(filename, FilenameObject):
try:
actual_filename = filename.tostring()
logger.debug("Attempting to open %s", actual_filename)
obj = _openimage(actual_filename)
logger.debug("Attempting to read frame %s from %s with reader %s", frame, actual_filename, obj.classname)
obj = obj.read(actual_filename, frame)
except Exception as ex:
# multiframe file
# logger.debug( "DEBUG: multiframe file, start # %d"%(
# filename.num)
logger.debug("Exception %s, trying name %s" % (ex, filename.stem))
obj = _openimage(filename.stem)
"""
if hasattr(fname, "read") and hasattr(fname, "write"):
# It is already something we can use
if "name" in dir(fname):
self.filename = fname.name
else:
self.filename = "stream"
try:
setattr(fname, "name", self.filename)
except AttributeError:
# cStringIO
logger.warning("Unable to set filename attribute to stream (cStringIO?) of type %s" % type(fname))
return fname
if isinstance(fname, fabioutils.PathTypes):
if not isinstance(fname, fabioutils.StringTypes):
fname = str(fname)
else:
raise TypeError("Unsupported type of fname (found %s)" % type(fname))
fileObject = None
self.filename = fname
self.filenumber = fabioutils.extract_filenumber(fname)
comp_type = os.path.splitext(fname)[-1]
if comp_type == ".gz":
fileObject = self._compressed_stream(fname,
fabioutils.COMPRESSORS['.gz'],
fabioutils.GzipFile,
mode)
elif comp_type == '.bz2':
def write(self, fname):
"""
To be overwritten - write the file
"""
if isinstance(fname, fabioutils.PathTypes):
if not isinstance(fname, fabioutils.StringTypes):
fname = str(fname)
module = sys.modules[self.__class__.__module__]
raise NotImplementedError("Writing %s format is not implemented" % module.__name__)
if not is_general_header:
# Check the full header information, if it is a standard frame
frame._check_header_mandatory_keys(filename=self.filename)
try:
# skip the data block
infile.seek(frame.blobsize - 1, os.SEEK_CUR)
data = infile.read(1)
if len(data) == 0:
self._incomplete_file = True
frame.incomplete_data = True
# Out of the file
break
except Exception as error:
if isinstance(infile, fabioutils.GzipFile):
if compression_module.is_incomplete_gz_block_exception(error):
self._incomplete_file = True
frame.incomplete_data = True
break
logger.warning("infile is %s" % infile)
logger.warning("position is %s" % infile.tell())
logger.warning("blobsize is %s" % frame.blobsize)
logger.error("It seams this error occurs under windows when reading a (large-) file over network: %s ", error)
raise Exception(error)
# PB38k20190607: _check_header_mandatory_keys is already
# done for each frame in the above loop
self.currentframe = 0
def load_fabio(self, filename):
"""
Loads an image using the fabio library.
:param filename: path to the image file to be loaded
:return: dictionary with image_data and image_data_fabio, None if unsuccessful
"""
try:
img_data_fabio = fabio.open(filename)
img_data = img_data_fabio.data[::-1]
return {"img_data_fabio": img_data_fabio, "img_data": img_data}
except (IOError, fabio.fabioutils.NotGoodReader):
return None
if isinstance(fname, fabioutils.PathTypes):
if not isinstance(fname, fabioutils.StringTypes):
fname = str(fname)
else:
raise TypeError("Unsupported type of fname (found %s)" % type(fname))
fileObject = None
self.filename = fname
self.filenumber = fabioutils.extract_filenumber(fname)
comp_type = os.path.splitext(fname)[-1]
if comp_type == ".gz":
fileObject = self._compressed_stream(fname,
fabioutils.COMPRESSORS['.gz'],
fabioutils.GzipFile,
mode)
elif comp_type == '.bz2':
fileObject = self._compressed_stream(fname,
fabioutils.COMPRESSORS['.bz2'],
fabioutils.BZ2File,
mode)
#
# Here we return the file even though it may be bzipped or gzipped
# but named incorrectly...
#
# FIXME - should we fix that or complain about the daft naming?
else:
fileObject = fabioutils.File(fname, mode)
if "name" not in dir(fileObject):
fileObject.name = fname
self._file = fileObject