diff options
author | Picca Frédéric-Emmanuel <picca@debian.org> | 2018-08-02 20:54:28 +0200 |
---|---|---|
committer | Picca Frédéric-Emmanuel <picca@debian.org> | 2018-08-02 20:54:28 +0200 |
commit | 0c737c7331790e9825a69d65b26d08075aded68a (patch) | |
tree | cad7520f2899b8bd84abca0526f32da32eb6b90b /PyMca5/PyMcaIO | |
parent | a9695cd9edb7b272e0b2dcd20882c2580dd9fb61 (diff) |
New upstream version 5.3.2+dfsg
Diffstat (limited to 'PyMca5/PyMcaIO')
-rw-r--r-- | PyMca5/PyMcaIO/EdfFile.py | 51 | ||||
-rw-r--r-- | PyMca5/PyMcaIO/HDF5Stack1D.py | 22 | ||||
-rw-r--r-- | PyMca5/PyMcaIO/MarCCD.py | 8 | ||||
-rw-r--r-- | PyMca5/PyMcaIO/PilatusCBF.py | 13 | ||||
-rw-r--r-- | PyMca5/PyMcaIO/SpecFileStack.py | 34 | ||||
-rw-r--r-- | PyMca5/PyMcaIO/TiffIO.py | 84 |
6 files changed, 141 insertions, 71 deletions
diff --git a/PyMca5/PyMcaIO/EdfFile.py b/PyMca5/PyMcaIO/EdfFile.py index 418771f..481ca6e 100644 --- a/PyMca5/PyMcaIO/EdfFile.py +++ b/PyMca5/PyMcaIO/EdfFile.py @@ -2,7 +2,7 @@ # # The PyMca X-Ray Fluorescence Toolkit # -# Copyright (c) 2004-2014 European Synchrotron Radiation Facility +# Copyright (c) 2004-2018 European Synchrotron Radiation Facility # # This file is part of the PyMca X-ray Fluorescence Toolkit developed at # the ESRF by the Software group. @@ -443,7 +443,7 @@ class EdfFile(object): self.Images[Index].DataType = 'UnsignedShort' try: self.__data = numpy.reshape( - numpy.fromstring(binary, numpy.uint16), + numpy.array(numpy.frombuffer(binary, numpy.uint16)), (self.Images[Index].Dim2, self.Images[Index].Dim1)) except ValueError: raise IOError('Size spec in ADSC-header does not match ' + \ @@ -636,14 +636,14 @@ class EdfFile(object): sizeToRead = self.Images[Index].Dim1 * \ self.Images[Index].Dim2 * \ self.Images[Index].Dim3 * datasize - Data = numpy.fromstring(self.File.read(sizeToRead), - datatype) + Data = numpy.array(numpy.frombuffer(self.File.read(sizeToRead), + datatype)) Data = numpy.reshape(Data, (self.Images[Index].Dim3, self.Images[Index].Dim2, self.Images[Index].Dim1)) elif self.Images[Index].NumDim == 2: sizeToRead = self.Images[Index].Dim1 * \ self.Images[Index].Dim2 * datasize - Data = numpy.fromstring(self.File.read(sizeToRead), - datatype) + Data = numpy.array(numpy.frombuffer(self.File.read(sizeToRead), + datatype)) #print "datatype = ",datatype #print "Data.type = ", Data.dtype.char #print "self.Images[Index].DataType ", self.Images[Index].DataType @@ -654,8 +654,8 @@ class EdfFile(object): Data = numpy.reshape(Data, (self.Images[Index].Dim2, self.Images[Index].Dim1)) elif self.Images[Index].NumDim == 1: sizeToRead = self.Images[Index].Dim1 * datasize - Data = numpy.fromstring(self.File.read(sizeToRead), - datatype) + Data = numpy.array(numpy.frombuffer(self.File.read(sizeToRead), + datatype)) elif self.ADSC or self.MARCCD or self.PILATUS_CBF or self.SPE: return self.__data[Pos[1]:(Pos[1] + Size[1]), Pos[0]:(Pos[0] + Size[0])] @@ -664,9 +664,9 @@ class EdfFile(object): return data[Pos[1]:(Pos[1] + Size[1]), Pos[0]:(Pos[0] + Size[0])] elif fastedf and CAN_USE_FASTEDF: - type = self.__GetDefaultNumpyType__(self.Images[Index].DataType, index=Index) - size_pixel = self.__GetSizeNumpyType__(type) - Data = numpy.array([], type) + type_ = self.__GetDefaultNumpyType__(self.Images[Index].DataType, index=Index) + size_pixel = self.__GetSizeNumpyType__(type_) + Data = numpy.array([], type_) if self.Images[Index].NumDim == 1: if Pos == None: Pos = (0,) if Size == None: Size = (0,) @@ -674,7 +674,7 @@ class EdfFile(object): Size = list(Size) if Size[0] == 0:Size[0] = sizex - Pos[0] self.File.seek((Pos[0] * size_pixel) + self.Images[Index].DataPosition, 0) - Data = numpy.fromstring(self.File.read(Size[0] * size_pixel), type) + Data = numpy.array(numpy.frombuffer(self.File.read(Size[0] * size_pixel), type_)) elif self.Images[Index].NumDim == 2: if Pos == None: Pos = (0, 0) if Size == None: Size = (0, 0) @@ -682,7 +682,7 @@ class EdfFile(object): sizex, sizey = self.Images[Index].Dim1, self.Images[Index].Dim2 if Size[0] == 0:Size[0] = sizex - Pos[0] if Size[1] == 0:Size[1] = sizey - Pos[1] - Data = numpy.zeros([Size[1], Size[0]], type) + Data = numpy.zeros([Size[1], Size[0]], type_) self.File.seek((((Pos[1] * sizex) + Pos[0]) * size_pixel) + self.Images[Index].DataPosition, 0) extended_fread(Data, Size[0] * size_pixel , numpy.array([Size[1]]), numpy.array([sizex * size_pixel]) , self.File) @@ -695,7 +695,7 @@ class EdfFile(object): if Size[0] == 0:Size[0] = sizex - Pos[0] if Size[1] == 0:Size[1] = sizey - Pos[1] if Size[2] == 0:Size[2] = sizez - Pos[2] - Data = numpy.zeros([Size[2], Size[1], Size[0]], type) + Data = numpy.zeros([Size[2], Size[1], Size[0]], type_) self.File.seek(((((Pos[2] * sizey + Pos[1]) * sizex) + Pos[0]) * size_pixel) + self.Images[Index].DataPosition, 0) extended_fread(Data, Size[0] * size_pixel , numpy.array([Size[2], Size[1]]), numpy.array([ sizey * sizex * size_pixel , sizex * size_pixel]) , self.File) @@ -703,9 +703,9 @@ class EdfFile(object): else: if fastedf: print("I could not use fast routines") - type = self.__GetDefaultNumpyType__(self.Images[Index].DataType, index=Index) - size_pixel = self.__GetSizeNumpyType__(type) - Data = numpy.array([], type) + type_ = self.__GetDefaultNumpyType__(self.Images[Index].DataType, index=Index) + size_pixel = self.__GetSizeNumpyType__(type_) + Data = numpy.array([], type_) if self.Images[Index].NumDim == 1: if Pos == None: Pos = (0,) if Size == None: Size = (0,) @@ -713,7 +713,7 @@ class EdfFile(object): Size = list(Size) if Size[0] == 0:Size[0] = sizex - Pos[0] self.File.seek((Pos[0] * size_pixel) + self.Images[Index].DataPosition, 0) - Data = numpy.fromstring(self.File.read(Size[0] * size_pixel), type) + Data = numpy.array(numpy.frombuffer(self.File.read(Size[0] * size_pixel), type_)) elif self.Images[Index].NumDim == 2: if Pos == None: Pos = (0, 0) if Size == None: Size = (0, 0) @@ -723,11 +723,11 @@ class EdfFile(object): if Size[1] == 0:Size[1] = sizey - Pos[1] #print len(range(Pos[1],Pos[1]+Size[1])), "LECTURES OF ", Size[0], "POINTS" #print "sizex = ", sizex, "sizey = ", sizey - Data = numpy.zeros((Size[1], Size[0]), type) + Data = numpy.zeros((Size[1], Size[0]), type_) dataindex = 0 for y in range(Pos[1], Pos[1] + Size[1]): self.File.seek((((y * sizex) + Pos[0]) * size_pixel) + self.Images[Index].DataPosition, 0) - line = numpy.fromstring(self.File.read(Size[0] * size_pixel), type) + line = numpy.array(numpy.frombuffer(self.File.read(Size[0] * size_pixel), type_)) Data[dataindex, :] = line[:] #Data=numpy.concatenate((Data,line)) dataindex += 1 @@ -745,7 +745,7 @@ class EdfFile(object): for z in range(Pos[2], Pos[2] + Size[2]): for y in range(Pos[1], Pos[1] + Size[1]): self.File.seek(((((z * sizey + y) * sizex) + Pos[0]) * size_pixel) + self.Images[Index].DataPosition, 0) - line = numpy.fromstring(self.File.read(Size[0] * size_pixel), type) + line = numpy.array(numpy.frombuffer(self.File.read(Size[0] * size_pixel), type_)) Data = numpy.concatenate((Data, line)) Data = numpy.reshape(Data, (Size[2], Size[1], Size[0])) @@ -776,9 +776,10 @@ class EdfFile(object): size_img = size_row * self.Images[Index].Dim2 offset = offset + (Position[2] * size_img) self.File.seek(self.Images[Index].DataPosition + offset, 0) - Data = numpy.fromstring(self.File.read(size_pixel), - self.__GetDefaultNumpyType__(self.Images[Index].DataType, - index=Index)) + Data = numpy.array(numpy.frombuffer( + self.File.read(size_pixel), + self.__GetDefaultNumpyType__(self.Images[Index].DataType, + index=Index))) if self.SysByteOrder.upper() != self.Images[Index].ByteOrder.upper(): Data = Data.byteswap() Data = self.__SetDataType__ (Data, "DoubleValue") @@ -931,7 +932,7 @@ class EdfFile(object): for i in Header.keys(): StrHeader = StrHeader + ("%s = %s ;\n" % (i, Header[i])) self.Images[Index].Header[i] = Header[i] - newsize = (((len(StrHeader) + 1) / HEADER_BLOCK_SIZE) + 1) * HEADER_BLOCK_SIZE - 2 + newsize = (((len(StrHeader) + 1) // HEADER_BLOCK_SIZE) + 1) * HEADER_BLOCK_SIZE - 2 newsize = int(newsize) StrHeader = StrHeader.ljust(newsize) StrHeader = StrHeader + "}\n" diff --git a/PyMca5/PyMcaIO/HDF5Stack1D.py b/PyMca5/PyMcaIO/HDF5Stack1D.py index 2c41858..041bf1e 100644 --- a/PyMca5/PyMcaIO/HDF5Stack1D.py +++ b/PyMca5/PyMcaIO/HDF5Stack1D.py @@ -256,7 +256,10 @@ class HDF5Stack1D(DataObject.DataObject): if self.__dtype in [numpy.int16, numpy.uint16]: self.__dtype = numpy.float32 elif self.__dtype in [numpy.int32, numpy.uint32]: - self.__dtype = numpy.float64 + if mSelection: + self.__dtype = numpy.float32 + else: + self.__dtype = numpy.float64 elif self.__dtype not in [numpy.float16, numpy.float32, numpy.float64]: # Some datasets form CLS (origin APS?) arrive as data format @@ -266,7 +269,10 @@ class HDF5Stack1D(DataObject.DataObject): if ("%s" % self.__dtype).endswith("2"): self.__dtype = numpy.float32 else: - self.__dtype = numpy.float64 + if mSelection: + self.__dtype = numpy.float32 + else: + self.__dtype = numpy.float64 # figure out the shape of the stack shape = yDataset.shape @@ -289,9 +295,14 @@ class HDF5Stack1D(DataObject.DataObject): bytefactor = 8 neededMegaBytes = nFiles * dim0 * dim1 * (mcaDim * bytefactor/(1024*1024.)) - physicalMemory = PhysicalMemory.getPhysicalMemoryOrNone() + physicalMemory = None + if hasattr(PhysicalMemory, "getAvailablePhysicalMemoryOrNone"): + physicalMemory = PhysicalMemory.getAvailablePhysicalMemoryOrNone() + if not physicalMemory: + physicalMemory = PhysicalMemory.getPhysicalMemoryOrNone() if physicalMemory is None: - # 5 Gigabytes should be a good compromise + # 6 Gigabytes of available memory + # should be a good compromise in 2018 physicalMemory = 6000 else: physicalMemory /= (1024*1024.) @@ -300,6 +311,7 @@ class HDF5Stack1D(DataObject.DataObject): if self.__dtype0 is None: if (bytefactor == 8) and (neededMegaBytes < (2*physicalMemory)): # try reading as float32 + print("Forcing the use of float32 data") self.__dtype = numpy.float32 else: raise MemoryError("Force dynamic loading") @@ -317,6 +329,8 @@ class HDF5Stack1D(DataObject.DataObject): # some versions report ValueError instead of MemoryError if (nFiles == 1) and (len(shape) == 3): print("Attempting dynamic loading") + if mSelection is not None: + print("Ignoring monitor") self.data = yDataset if mSelection is not None: mdtype = tmpHdf[mpath].dtype diff --git a/PyMca5/PyMcaIO/MarCCD.py b/PyMca5/PyMcaIO/MarCCD.py index fc37031..f2d0c8e 100644 --- a/PyMca5/PyMcaIO/MarCCD.py +++ b/PyMca5/PyMcaIO/MarCCD.py @@ -64,11 +64,11 @@ class MarCCD(object): depth = info["depth"] fd.seek(4096) if depth == 1: - data = numpy.fromstring(fd.read(nbytes), numpy.uint8) + data = numpy.array(numpy.frombuffer(fd.read(nbytes), numpy.uint8)) elif depth == 2: - data = numpy.fromstring(fd.read(nbytes), numpy.uint16) + data = numpy.array(numpy.frombuffer(fd.read(nbytes), numpy.uint16)) elif depth == 4: - data = numpy.fromstring(fd.read(nbytes), numpy.uint32) + data = numpy.array(numpy.frombuffer(fd.read(nbytes), numpy.uint32)) if swap: data = data.byteswap() data.shape = info["nfast"], info["nslow"] @@ -155,7 +155,7 @@ class MccdHeader(object): if 0: self.__format = struct.unpack("256I", self.raw[0:256*4]) else: - self.__format = numpy.fromstring(self.raw[0:256*4], numpy.uint32) + self.__format = numpy.array(numpy.frombuffer(self.raw[0:256*4], numpy.uint32)) def __unpack_gonio(self): idx= 640 diff --git a/PyMca5/PyMcaIO/PilatusCBF.py b/PyMca5/PyMcaIO/PilatusCBF.py index 5c5ebba..9c6f260 100644 --- a/PyMca5/PyMcaIO/PilatusCBF.py +++ b/PyMca5/PyMcaIO/PilatusCBF.py @@ -173,24 +173,27 @@ class PilatusCBF(object): # lns = len(stream) idx = stream.find(key16) if idx == -1: - listnpa.append(np.fromstring(stream, dtype="int8")) + listnpa.append(np.array(np.frombuffer(stream, dtype="int8"))) break - listnpa.append(np.fromstring(stream[:idx], dtype="int8")) + listnpa.append(np.array(np.frombuffer(stream[:idx], dtype="int8"))) # position += listnpa[-1].size if stream[idx + 1:idx + 3] == key32: if stream[idx + 3:idx + 7] == key64: - listnpa.append(np.fromstring(stream[idx + 7:idx + 15], dtype="int64")) + listnpa.append(np.array(np.frombuffer(stream[idx + 7:idx + 15], + dtype="int64"))) # position += 1 # print "loop64 x=%4i y=%4i in idx %4i lns %4i value=%s" % ((position % 2463), (position // 2463), idx, lns, listnpa[-1]) shift = 15 else: #32 bit int - listnpa.append(np.fromstring(stream[idx + 3:idx + 7], dtype="int32")) + listnpa.append(np.array(np.frombuffer(stream[idx + 3:idx + 7], + dtype="int32"))) # position += 1 # print "loop32 x=%4i y=%4i in idx %4i lns %4i value=%s" % ((position % 2463), (position // 2463), idx, lns, listnpa[-1]) shift = 7 else: #int16 - listnpa.append(np.fromstring(stream[idx + 1:idx + 3], dtype="int16")) + listnpa.append(np.array(np.frombuffer(stream[idx + 1:idx + 3], + dtype="int16"))) # position += 1 # print "loop16 x=%4i y=%4i in idx %4i lns %4i value=%s" % ((position % 2463), (position // 2463), idx, lns, listnpa[-1]) shift = 3 diff --git a/PyMca5/PyMcaIO/SpecFileStack.py b/PyMca5/PyMcaIO/SpecFileStack.py index 01e86bc..1b51b11 100644 --- a/PyMca5/PyMcaIO/SpecFileStack.py +++ b/PyMca5/PyMcaIO/SpecFileStack.py @@ -2,7 +2,7 @@ # # The PyMca X-Ray Fluorescence Toolkit # -# Copyright (c) 2004-2017 European Synchrotron Radiation Facility +# Copyright (c) 2004-2018 European Synchrotron Radiation Facility # # This file is part of the PyMca X-ray Fluorescence Toolkit developed at # the ESRF by the Software group. @@ -110,10 +110,19 @@ class SpecFileStack(DataObject.DataObject): iterlist = [1] if SLOW_METHOD and shape is None: self.data = numpy.zeros((self.nbFiles, - nmca / numberofdetectors, + nmca // numberofdetectors, arrRet.shape[0]), arrRet.dtype.char) filecounter = 0 + if "McaLiveTime" in info: + nTimes = self.nbFiles * (nmca // numberofdetectors) + self.info["McaLiveTime"] = numpy.zeros((nTimes,), + numpy.float32) + if "McaElapsedTime" in info: + nTimes = self.nbFiles * (nmca // numberofdetectors) + self.info["McaElapsedTime"] = numpy.zeros((nTimes,), + numpy.float32) + nTimes = -1 for tempFileName in filelist: tempInstance = SpecFileDataSource.SpecFileDataSource(tempFileName) mca_number = -1 @@ -122,16 +131,22 @@ class SpecFileStack(DataObject.DataObject): numberofmca = info['NbMca'] if numberofmca <= 0: continue + # only the last mca is read key = "%s.1.%s" % (keyindex, numberofmca) dataObject = tempInstance._getMcaData(key) arrRet = dataObject.data mca_number += 1 + nTimes += 1 for i in iterlist: # mcadata = scan_obj.mca(i) self.data[filecounter, mca_number, :] = arrRet[:] self.incrProgressBar += 1 + for timeKey in ["McaElapsedTime", "McaLiveTime"]: + if timeKey in dataObject.info: + self.info[timeKey][nTimes] = \ + dataObject.info[timeKey] self.onProgress(self.incrProgressBar) filecounter += 1 elif shape is None and (self.nbFiles == 1) and (iterlist == [1]): @@ -142,6 +157,9 @@ class SpecFileStack(DataObject.DataObject): numberofmca, arrRet.shape[0]), arrRet.dtype.char) + # when reading fast we do not read the time information + # therefore we have to remove it from the info + self._cleanupTimeInfo() for tempFileName in filelist: tempInstance = specfile.Specfile(tempFileName) # it can only be here if there is one scan per file @@ -159,9 +177,12 @@ class SpecFileStack(DataObject.DataObject): filecounter = 1 elif shape is None: # it can only be here if there is one scan per file + # when reading fast we do not read the time information + # therefore we have to remove it from the info + self._cleanupTimeInfo() try: self.data = numpy.zeros((self.nbFiles, - numberofmca / numberofdetectors, + numberofmca // numberofdetectors, arrRet.shape[0]), arrRet.dtype.char) filecounter = 0 @@ -254,6 +275,8 @@ class SpecFileStack(DataObject.DataObject): else: raise else: + # time information not read + self._cleanupTimeInfo() sampling_order = 1 s0 = shape[0] s1 = shape[1] @@ -381,6 +404,11 @@ class SpecFileStack(DataObject.DataObject): self.info["NumberOfFiles"] = self.__nFiles * 1 self.info["FileIndex"] = fileindex + def _cleanupTimeInfo(self): + for timeKey in ["McaElapsedTime", "McaLiveTime"]: + if timeKey in self.info: + del self.info[timeKey] + def onBegin(self, n): pass diff --git a/PyMca5/PyMcaIO/TiffIO.py b/PyMca5/PyMcaIO/TiffIO.py index 0abf418..c564135 100644 --- a/PyMca5/PyMcaIO/TiffIO.py +++ b/PyMca5/PyMcaIO/TiffIO.py @@ -30,6 +30,7 @@ __author__ = "V.A. Sole - ESRF Data Analysis" __contact__ = "sole@esrf.fr" __license__ = "MIT" __copyright__ = "European Synchrotron Radiation Facility, Grenoble, France" +__date__ = "25/06/2018" import sys import os @@ -45,6 +46,7 @@ TAG_ID = { 256:"NumberOfColumns", # S or L ImageWidth 259:"Compression", # SHORT (1 - NoCompression, ... 262:"PhotometricInterpretation", # SHORT (0 - WhiteIsZero, 1 -BlackIsZero, 2 - RGB, 3 - Palette color 270:"ImageDescription", # ASCII + 272:"Model", # ASCII 273:"StripOffsets", # S or L, for each strip, the byte offset of the strip 277:"SamplesPerPixel", # SHORT (>=3) only for RGB images 278:"RowsPerStrip", # S or L, number of rows in each back may be not for the last @@ -62,6 +64,7 @@ TAG_BITS_PER_SAMPLE = 258 TAG_PHOTOMETRIC_INTERPRETATION = 262 TAG_COMPRESSION = 259 TAG_IMAGE_DESCRIPTION = 270 +TAG_MODEL = 272 TAG_STRIP_OFFSETS = 273 TAG_SAMPLES_PER_PIXEL = 277 TAG_ROWS_PER_STRIP = 278 @@ -151,7 +154,7 @@ class TiffIO(object): else: raise IOError("File is not a Mar CCD file, nor a TIFF file") a = fd.read(2) - fortyTwo = struct.unpack(self._structChar+"H",a)[0] + fortyTwo = struct.unpack(self._structChar + "H", a)[0] if fortyTwo != 42: raise IOError("Invalid TIFF version %d" % fortyTwo) else: @@ -184,7 +187,7 @@ class TiffIO(object): if self._access is None: # we do not own the file # open in read mode - newFile = open(fileName,'rb') + newFile = open(fileName, 'rb') else: newFile = open(fileName, self._access) self.fd = newFile @@ -272,7 +275,13 @@ class TiffIO(object): ftype, vfmt = FIELD_TYPE[fieldType] if ftype not in ['ASCII', 'RATIONAL', 'SRATIONAL']: vfmt = st + vfmt - actualValue = struct.unpack(vfmt, valueOffset[0: struct.calcsize(vfmt)])[0] + data = valueOffset[0: struct.calcsize(vfmt)] + if struct.calcsize(vfmt) > len(data): + # I do not see how I can enter here + # Add a 0 padding to have the expected size + print("WARNING: Data at tag id '%s' is smaller than expected", tagID) + data = data + b"\x00" * (struct.calcsize(vfmt) - len(data)) + actualValue = struct.unpack(vfmt, data)[0] valueOffsetList.append(actualValue) else: valueOffsetList.append(valueOffset) @@ -294,8 +303,6 @@ class TiffIO(object): # print("valueOffset = %s" % valueOffset) return tagIDList, fieldTypeList, nValuesList, valueOffsetList - - def _readIFDEntry(self, tag, tagIDList, fieldTypeList, nValuesList, valueOffsetList): fd = self.fd st = self._structChar @@ -374,13 +381,15 @@ class TiffIO(object): if nValuesList[idx] != 1: # this happens with RGB and friends, nBits is not a single value nBits = self._readIFDEntry(TAG_BITS_PER_SAMPLE, - tagIDList, fieldTypeList, nValuesList, valueOffsetList) + tagIDList, fieldTypeList, nValuesList, + valueOffsetList) if TAG_COLORMAP in tagIDList: idx = tagIDList.index(TAG_COLORMAP) tmpColormap = self._readIFDEntry(TAG_COLORMAP, - tagIDList, fieldTypeList, nValuesList, valueOffsetList) + tagIDList, fieldTypeList, nValuesList, + valueOffsetList) if max(tmpColormap) > 255: tmpColormap = numpy.array(tmpColormap, dtype=numpy.uint16) tmpColormap = (tmpColormap / 256.).astype(numpy.uint8) @@ -423,9 +432,15 @@ class TiffIO(object): imageDescription = self._readIFDEntry(TAG_IMAGE_DESCRIPTION, tagIDList, fieldTypeList, nValuesList, valueOffsetList) if type(imageDescription) in [type([1]), type((1,))]: - imageDescription =helpString.join(imageDescription) + imageDescription = helpString.join(imageDescription) else: - imageDescription = "%d/%d" % (nImage+1, len(self._IFD)) + imageDescription = "%d/%d" % (nImage + 1, len(self._IFD)) + + if TAG_MODEL in tagIDList: + model = self._readIFDEntry(TAG_MODEL, + tagIDList, fieldTypeList, nValuesList, valueOffsetList) + else: + model = None defaultSoftware = "Unknown Software" @@ -454,12 +469,15 @@ class TiffIO(object): date = self._readIFDEntry(TAG_DATE, tagIDList, fieldTypeList, nValuesList, valueOffsetList) if type(date) in [type([1]), type((1,))]: - date =helpString.join(date) + date = helpString.join(date) else: date = "Unknown Date" stripOffsets = self._readIFDEntry(TAG_STRIP_OFFSETS, - tagIDList, fieldTypeList, nValuesList, valueOffsetList) + tagIDList, + fieldTypeList, + nValuesList, + valueOffsetList) if TAG_ROWS_PER_STRIP in tagIDList: rowsPerStrip = self._readIFDEntry(TAG_ROWS_PER_STRIP, tagIDList, fieldTypeList, nValuesList, valueOffsetList)[0] @@ -511,6 +529,8 @@ class TiffIO(object): info["colormap"] = colormap info["sampleFormat"] = sampleFormat info["photometricInterpretation"] = interpretation + if model is not None: + info["model"] = model infoDict = {} testString = 'PyMca' @@ -663,22 +683,24 @@ class TiffIO(object): rowStart = 0 if len(stripOffsets) == 1: - bytesPerRow = int(stripByteCounts[0]/rowsPerStrip) + bytesPerRow = int(stripByteCounts[0] / rowsPerStrip) + nBytes = stripByteCounts[0] if nRows == rowsPerStrip: - actualBytesPerRow = int(image.nbytes/nRows) + actualBytesPerRow = int(image.nbytes / nRows) if actualBytesPerRow != bytesPerRow: print("Warning: Bogus StripByteCounts information") bytesPerRow = actualBytesPerRow + nBytes = (rowMax-rowMin+1) * bytesPerRow fd.seek(stripOffsets[0] + rowMin * bytesPerRow) - nBytes = (rowMax-rowMin+1) * bytesPerRow if self._swap: - readout = numpy.fromstring(fd.read(nBytes), dtype).byteswap() + readout = numpy.array(numpy.frombuffer(fd.read(nBytes), dtype)).byteswap() else: - readout = numpy.fromstring(fd.read(nBytes), dtype) + readout = numpy.array(numpy.frombuffer(fd.read(nBytes), dtype)) if hasattr(nBits, 'index'): readout.shape = -1, nColumns, len(nBits) elif info['colormap'] is not None: readout = colormap[readout] + readout.shape = -1, nColumns, 3 else: readout.shape = -1, nColumns image[rowMin:rowMax+1, :] = readout @@ -686,7 +708,7 @@ class TiffIO(object): for i in range(len(stripOffsets)): # the amount of rows nRowsToRead = rowsPerStrip - rowEnd = int(min(rowStart+nRowsToRead, nRows)) + rowEnd = int(min(rowStart + nRowsToRead, nRows)) if rowEnd < rowMin: rowStart += nRowsToRead continue @@ -707,25 +729,27 @@ class TiffIO(object): # intermediate buffer tmpBuffer = fd.read(nBytes) while readBytes < nBytes: - n = struct.unpack('b', tmpBuffer[readBytes:(readBytes+1)])[0] + n = struct.unpack('b', + tmpBuffer[readBytes:(readBytes + 1)])[0] readBytes += 1 if n >= 0: # should I prevent reading more than the # length of the chain? Let's python raise # the exception... bufferBytes += tmpBuffer[readBytes:\ - readBytes+(n+1)] - readBytes += (n+1) + readBytes + (n + 1)] + readBytes += (n + 1) elif n > -128: - bufferBytes += (-n+1) * tmpBuffer[readBytes:(readBytes+1)] + bufferBytes += (-n + 1) * \ + tmpBuffer[readBytes:(readBytes + 1)] readBytes += 1 else: # if read -128 ignore the byte continue if self._swap: - readout = numpy.fromstring(bufferBytes, dtype).byteswap() + readout = numpy.array(numpy.frombuffer(bufferBytes, dtype)).byteswap() else: - readout = numpy.fromstring(bufferBytes, dtype) + readout = numpy.array(numpy.frombuffer(bufferBytes, dtype)) if hasattr(nBits, 'index'): readout.shape = -1, nColumns, len(nBits) elif info['colormap'] is not None: @@ -738,9 +762,9 @@ class TiffIO(object): if 1: # use numpy if self._swap: - readout = numpy.fromstring(fd.read(nBytes), dtype).byteswap() + readout = numpy.array(numpy.frombuffer(fd.read(nBytes), dtype)).byteswap() else: - readout = numpy.fromstring(fd.read(nBytes), dtype) + readout = numpy.array(numpy.frombuffer(fd.read(nBytes), dtype)) if hasattr(nBits, 'index'): readout.shape = -1, nColumns, len(nBits) elif colormap is not None: @@ -773,8 +797,8 @@ class TiffIO(object): image[:, :, 1] * 0.587 + \ image[:, :, 2] * 0.299).astype(numpy.float32) - if (rowMin == 0) and (rowMax == (nRows-1)): - self._imageDataCacheIndex.insert(0,nImage) + if (rowMin == 0) and (rowMax == (nRows - 1)): + self._imageDataCacheIndex.insert(0, nImage) self._imageDataCache.insert(0, image) if len(self._imageDataCacheIndex) > self._maxImageCacheLength: self._imageDataCacheIndex = self._imageDataCacheIndex[:self._maxImageCacheLength] @@ -1034,7 +1058,7 @@ class TiffIO(object): bitsPerSample * nChannels / 8) if descriptionLength > 4: - stripOffsets0 = endOfFile + dateLength + descriptionLength +\ + stripOffsets0 = endOfFile + dateLength + descriptionLength + \ 2 + 12 * nDirectoryEntries + 4 else: stripOffsets0 = endOfFile + dateLength + \ @@ -1063,7 +1087,7 @@ class TiffIO(object): value = stripOffsets0 + i * stripByteCounts stripOffsets.append(value) if i == 0: - stripOffsetsString = struct.pack(fmt, value) + stripOffsetsString = struct.pack(fmt, value) stripByteCountsString = struct.pack(fmt, stripByteCounts) else: stripOffsetsString += struct.pack(fmt, value) @@ -1269,7 +1293,7 @@ if __name__ == "__main__": tif = None if os.path.exists(filename): print("Testing image appending") - tif = TiffIO(filename, mode = 'rb+') + tif = TiffIO(filename, mode='rb+') tif.writeImage((data * 2).astype(dtype), info={'Title': '2nd'}) tif = None tif = TiffIO(filename) |