def onProcessStackFinished(self): _logger.debug("Stack proccessed") self._status = "Stack Fitting finished" if self.fixedLenghtOutput: self._status = "Writing output files" nParameters = len(self._parameters) datalist = [None] * (2 * len(self._sigmas.keys()) + 1) labels = [] for i in range(nParameters): parameter = self._parameters[i] datalist[2 * i] = self._images[parameter] datalist[2 * i + 1] = self._sigmas[parameter] labels.append(parameter) labels.append('s(%s)' % parameter) datalist[-1] = self._images['chisq'] labels.append('chisq') filenames = self.getOutputFileNames() csvName = filenames['csv'] edfName = filenames['edf'] ArraySave.save2DArrayListAsASCII(datalist, csvName, labels=labels, csv=True, csvseparator=";") ArraySave.save2DArrayListAsEDF(datalist, edfName, labels=labels, dtype=numpy.float32)
def saveStackAsSimplestHDF5(self): filename = self._getOutputHDF5Filename() if not len(filename): return view = self._getCroppedView() ArraySave.save3DArrayAsHDF5(view, filename, labels = None, dtype=None, mode='simplest')
def onProcessStackFinished(self): _logger.debug("Stack proccessed") self._status = "Stack Fitting finished" if self.fixedLenghtOutput: self._status = "Writing output files" nParameters = len(self._parameters) datalist = [None] * (2*len(self._sigmas.keys())+1) labels = [] for i in range(nParameters): parameter = self._parameters[i] datalist[2*i] = self._images[parameter] datalist[2*i + 1] = self._sigmas[parameter] labels.append(parameter) labels.append('s(%s)' % parameter) datalist[-1] = self._images['chisq'] labels.append('chisq') filenames = self.getOutputFileNames() csvName = filenames['csv'] edfName = filenames['edf'] ArraySave.save2DArrayListAsASCII(datalist, csvName, labels=labels, csv=True, csvseparator=";") ArraySave.save2DArrayListAsEDF(datalist, edfName, labels = labels, dtype=numpy.float32)
def saveAllSpectra(self): fltrs = ['Raw ASCII *.txt', '","-separated CSV *.csv', '";"-separated CSV *.csv', '"tab"-separated CSV *.csv', 'OMNIC CSV *.csv'] message = "Enter file name to be used as root" fileList, fileFilter = PyMcaFileDialogs.getFileList(parent=self, filetypelist=fltrs, message=message, currentdir=None, mode="SAVE", getfilter=True, single=True, currentfilter=None, native=None) if not len(fileList): return fileroot = fileList[0] dirname = os.path.dirname(fileroot) root, ext = os.path.splitext(os.path.basename(fileroot)) if ext not in [".txt", ".csv"]: root = root + ext ext = "" # get appropriate extensions and separators filterused = fileFilter.split() if filterused[0].startswith("Raw"): csv = False ext = "txt" csvseparator = " " elif filterused[0].startswith("OMNIC"): # extension is csv but saved as ASCII csv = False ext = "csv" csvseparator = "," else: csv = True ext = "csv" if "," in filterused[0]: csvseparator = "," elif ";" in filterused[0]: csvseparator = ";" elif "OMNIC" in filterused[0]: csvseparator = "," else: csvseparator = "\t" nSpectra = len(self.spectrumList) n = int(numpy.log10(nSpectra)) + 1 fmt = "_%" + "0%dd" % n + ".%s" for index in range(nSpectra): legend = self.spectrumNames[index] x = self.xValues[index] y = self.spectrumList[index] filename = os.path.join(dirname, root + fmt % (index, ext)) ArraySave.saveXY(x, y, filename, ylabel=legend, csv=csv, csvseparator=csvseparator)
def saveStackAsSimpleHDF5(self): filename = self._getOutputHDF5Filename() if not len(filename): return ArraySave.save3DArrayAsHDF5(self._stack.data, filename, labels=None, dtype=None, mode='simple')
def saveStackAsNeXusPlus(self): filename = self._getOutputHDF5Filename() if not len(filename): return ArraySave.save3DArrayAsHDF5(self._stack.data, filename, labels=None, dtype=None, mode='nexus+')
def saveStackAsSimplestHDF5(self): filename = self._getOutputHDF5Filename() if not len(filename): return view = self._getCroppedView() ArraySave.save3DArrayAsHDF5(view, filename, labels=None, dtype=None, mode='simplest')
def saveStackAsNeXus(self, dtype=None, interpretation=None, compression=False): mcaIndex = self._stack.info.get('McaIndex', -1) if interpretation is None: if mcaIndex in [0]: interpretation = "image" else: interpretation = "spectrum" if interpretation not in ["spectrum", "image"]: raise ValueError("Unknown data interpretation %s" % interpretation) filename = self._getOutputHDF5Filename() if not len(filename): return # get only the seen stack portion view = self._getCroppedView() # the current graph axis is saved axes = [None] * len(self._stack.data.shape) labels = [None] * len(self._stack.data.shape) try: xLabel = qt.safe_str(self.mcaWidget.graph.getGraphXLabel()) except: xLabel = None try: xData, y, legend, info = self.mcaWidget.getActiveCurve()[:4] except: xData = self._mcaData0.x[0] xLabel = 'Channels' if interpretation == 'image': labels[0] = xLabel axes[0] = xData else: labels[-1] = xLabel axes[-1] = xData try: ArraySave.save3DArrayAsHDF5(view, filename, axes=axes, labels=labels, dtype=dtype, mode='nexus', mcaindex=mcaIndex, interpretation=interpretation, compression=compression) except: msg = qt.QMessageBox(self) msg.setIcon(qt.QMessageBox.Critical) msg.setWindowTitle("Save error") msg.setText("An error has occured while saving the data:") msg.setInformativeText(qt.safe_str(sys.exc_info()[1])) msg.setDetailedText(traceback.format_exc()) msg.exec_()
def _saveSlot(self): if self.outputDir is None: if self.inputDir is not None: self.outputDir = self.inputDir else: self.outputDir = PyMcaDirs.outputDir wdir = self.outputDir format_list = ['";"-separated CSV *.csv', '","-separated CSV *.csv', '"tab"-separated CSV *.csv'] if self.outputFilter is None: self.outputFilter = format_list[0] outfile, filterused = PyMcaFileDialogs.getFileList(self, filetypelist=format_list, mode="SAVE", message="Output File Selection", currentdir=wdir, currentfilter=self.outputFilter, getfilter=True, single=True) if len(outfile): outputFile = qt.safe_str(outfile[0]) else: return self.outputFilter = qt.safe_str(filterused) filterused = self.outputFilter.split() try: self.outputDir = os.path.dirname(outputFile) PyMcaDirs.outputDir = os.path.dirname(outputFile) except: self.outputDir = "." if not outputFile.endswith('.csv'): outputFile += '.csv' #always overwrite if "," in filterused[0]: csv = "," elif ";" in filterused[0]: csv = ";" else: csv = "\t" ddict = self.getTransmissionTable() x = ddict["energy"] y = ddict["transmission"] try: ArraySave.saveXY(x, y, outputFile, xlabel="Energy", ylabel="Transmission", csv=True, csvseparator=csv) except IOError: msg = qt.QMessageBox(self) msg.setIcon(qt.QMessageBox.Critical) msg.setText("Input Output Error: %s" % (sys.exc_info()[1])) msg.exec() return
def finishHDF5File(self, hdf): #add final date toplevelEntry = hdf["entry_000"] toplevelEntry['end_time'] = numpy.string_( ArraySave.getDate().encode('utf-8')) hdf.flush() hdf.close()
def saveStackAsMonochromaticTiffImages(self, dtype=None): if dtype is None: dtype = self._stack.data.dtype if dtype in [numpy.uint32, numpy.uint64]: dtype = numpy.float32 elif dtype in [numpy.int32, numpy.int64]: dtype = numpy.float32 mcaIndex = self._stack.info.get('McaIndex', -1) filename = self._getOutputTiffFilename() if not len(filename): return ArraySave.save3DArrayAsMonochromaticTiff(self._stack.data, filename, labels = None, dtype=dtype, mcaindex=mcaIndex)
def initializeHDF5File(self, fname): # for the time being overwriting if os.path.exists(fname): os.remove(fname) hdf = h5py.File(fname, "w") entryName = "entry_000" nxEntry = hdf.require_group(entryName) if "NX_class" not in nxEntry.attrs: nxEntry.attrs["NX_class"] = "NXentry".encode("utf-8") nxEntry["title"] = numpy.string_("PyMca saved 3D Array".encode("utf-8")) nxEntry["start_time"] = numpy.string_(ArraySave.getDate().encode("utf-8")) alignmentGroup = nxEntry.require_group("Alignment") dataGroup = nxEntry.require_group("Data") dataGroup.attrs["NX_class"] = "NXdata".encode("utf-8") return hdf
def initializeHDF5File(self, fname): #for the time being overwriting if os.path.exists(fname): os.remove(fname) hdf = h5py.File(fname, 'w') entryName = "entry_000" nxEntry = hdf.require_group(entryName) if 'NX_class' not in nxEntry.attrs: nxEntry.attrs['NX_class'] = 'NXentry'.encode('utf-8') nxEntry['title'] = numpy.string_("PyMca saved 3D Array".encode('utf-8')) nxEntry['start_time'] = numpy.string_(ArraySave.getDate().encode('utf-8')) alignmentGroup = nxEntry.require_group('Alignment') dataGroup = nxEntry.require_group('Data') dataGroup.attrs['NX_class'] = 'NXdata'.encode('utf-8') return hdf
def _handleSaveIcon(self, event): """Handle save icon events. Get current active image and save it as a file. """ if event['event'] == 'iconClicked' and event['key'] == 'save': imageData = self.getActiveImage() if imageData is None: qt.QMessageBox.information(self, "No Data", "No image to be saved") return data, legend, info, pixmap = imageData imageList = [data] labels = ['value'] # Copied from MaskImageWidget.saveImageList filename = self.getOutputFileName() if not len(filename): return # Add PNG and JPG adapted from PlotWindow.defaultSaveAction if 'WIDGET' in self._saveFilter.upper(): fformat = self._saveFilter[-3:].upper() pixmap = qt.QPixmap.grabWidget(self._imagePlot) # Use the following instead to grab the image + histograms # pixmap = qt.QPixmap.grabWidget(self) if not pixmap.save(filename, fformat): msg = qt.QMessageBox(self) msg.setIcon(qt.QMessageBox.Critical) msg.setInformativeText(str(sys.exc_info()[1])) msg.setDetailedText(traceback.format_exc()) msg.exec_() return if filename.lower().endswith(".edf"): ArraySave.save2DArrayListAsEDF(imageList, filename, labels) elif filename.lower().endswith(".csv"): if "," in self._saveFilter: csvseparator = "," elif ";" in self._saveFilter: csvseparator = ";" else: csvseparator = "\t" ArraySave.save2DArrayListAsASCII(imageList, filename, labels, csv=True, csvseparator=csvseparator) else: ArraySave.save2DArrayListAsASCII(imageList, filename, labels, csv=False)
def saveImageList(self, filename, imageList, labels, csvseparator=None): if not imageList: qt.QMessageBox.information( self, "No Data", "Image list is empty.\nNothing to be saved") return if filename.lower().endswith(".edf"): ArraySave.save2DArrayListAsEDF(imageList, filename, labels) elif filename.lower().endswith(".tif"): ArraySave.save2DArrayListAsMonochromaticTiff(imageList, filename, labels) elif filename.lower().endswith(".csv"): assert csvseparator is not None ArraySave.save2DArrayListAsASCII(imageList, filename, labels, csv=True, csvseparator=csvseparator) else: ArraySave.save2DArrayListAsASCII(imageList, filename, labels, csv=False)
def saveImageList(self, filename, imageList, labels, csvseparator=None): if not imageList: qt.QMessageBox.information( self, "No Data", "Image list is empty.\nNothing to be saved") return if filename.lower().endswith(".edf"): ArraySave.save2DArrayListAsEDF(imageList, filename, labels) elif filename.lower().endswith(".tif"): ArraySave.save2DArrayListAsMonochromaticTiff( imageList, filename, labels) elif filename.lower().endswith(".csv"): assert csvseparator is not None ArraySave.save2DArrayListAsASCII(imageList, filename, labels, csv=True, csvseparator=csvseparator) else: ArraySave.save2DArrayListAsASCII(imageList, filename, labels, csv=False)
def _threadFinished(self): result = self.thread.result self.thread = None if type(result) == type((1, )): #if we receive a tuple there was an error if len(result): if result[0] == "Exception": # somehow this exception is not caught raise Exception(result[1], result[2]) #, result[3]) return if 'concentrations' in result: imageNames = result['names'] images = numpy.concatenate( (result['parameters'], result['concentrations']), axis=0) else: images = result['parameters'] imageNames = result['names'] nImages = images.shape[0] self._widget = StackPluginResultsWindow.StackPluginResultsWindow(\ usetab=False) self._widget.buildAndConnectImageButtonBox(replace=True, multiple=True) qt = StackPluginResultsWindow.qt self._widget.sigMaskImageWidgetSignal.connect(self.mySlot) self._widget.setStackPluginResults(images, image_names=imageNames) self._showWidget() # save to output directory parameters = self.configurationWidget.getParameters() outputDir = parameters["output_dir"] if outputDir in [None, ""]: if DEBUG: print("Nothing to be saved") return if parameters["file_root"] is None: fileRoot = "" else: fileRoot = parameters["file_root"].replace(" ", "") if fileRoot in [None, ""]: fileRoot = "images" if not os.path.exists(outputDir): os.mkdir(outputDir) imagesDir = os.path.join(outputDir, "IMAGES") if not os.path.exists(imagesDir): os.mkdir(imagesDir) imageList = [None] * (nImages + len(result['uncertainties'])) fileImageNames = [None] * (nImages + len(result['uncertainties'])) j = 0 for i in range(nImages): name = imageNames[i].replace(" ", "-") fileImageNames[j] = name imageList[j] = images[i] j += 1 if not imageNames[i].startswith("C("): # fitted parameter fileImageNames[j] = "s(%s)" % name imageList[j] = result['uncertainties'][i] j += 1 fileName = os.path.join(imagesDir, fileRoot + ".edf") ArraySave.save2DArrayListAsEDF(imageList, fileName, labels=fileImageNames) fileName = os.path.join(imagesDir, fileRoot + ".csv") ArraySave.save2DArrayListAsASCII(imageList, fileName, csv=True, labels=fileImageNames) if parameters["tiff"]: i = 0 for i in range(len(fileImageNames)): label = fileImageNames[i] if label.startswith("s("): continue elif label.startswith("C("): mass_fraction = "_" + label[2:-1] + "_mass_fraction" else: mass_fraction = "_" + label fileName = os.path.join(imagesDir, fileRoot + mass_fraction + ".tif") ArraySave.save2DArrayListAsMonochromaticTiff( [imageList[i]], fileName, labels=[label], dtype=numpy.float32)
def loadFileList(self, filelist, fileindex=0, shape=None): if type(filelist) == type(''): filelist = [filelist] self.__keyList = [] self.sourceName = filelist self.__indexedStack = True self.sourceType = SOURCE_TYPE self.info = {} self.nbFiles = len(filelist) # read first file # get information tempInstance = SpecFileDataSource.SpecFileDataSource(filelist[0]) keylist = tempInstance.getSourceInfo()['KeyList'] nscans = len(keylist) # that is the number of scans nmca = 0 numberofdetectors = 0 for key in keylist: info = tempInstance.getKeyInfo(key) numberofmca = info['NbMca'] if numberofmca > 0: numberofdetectors = info['NbMcaDet'] scantype = info["ScanType"] if numberofmca: nmca += numberofmca if numberofdetectors == 0: raise ValueError("No MCA found in file %s" % filelist[0]) if (nscans > 1) and ((nmca // numberofdetectors) == nscans): SLOW_METHOD = True else: SLOW_METHOD = False # get last mca of first point key = "%s.1.%s" % (keylist[-1], numberofmca) dataObject = tempInstance._getMcaData(key) self.info.update(dataObject.info) arrRet = dataObject.data self.onBegin(self.nbFiles * nmca // numberofdetectors) self.incrProgressBar = 0 if info['NbMcaDet'] > 1: # Should I generate a map for each mca and not just for the last one as I am doing? iterlist = range(info['NbMcaDet'], info['NbMca'] + 1, info['NbMcaDet']) else: iterlist = [1] if SLOW_METHOD and shape is None: self.data = numpy.zeros( (self.nbFiles, nmca // numberofdetectors, arrRet.shape[0]), arrRet.dtype.char) nTimes = self.nbFiles * (nmca // numberofdetectors) filecounter = 0 for key in ["McaLiveTime", "McaElapsedTime"]: if key in dataObject.info: self.info[key] = numpy.zeros((nTimes, ), numpy.float32) # positioners key = "MotorNames" positioners = None if key in dataObject.info: positioners = {} for mne in dataObject.info[key]: positioners[mne] = numpy.zeros((nTimes, ), numpy.float32) nTimes = -1 for tempFileName in filelist: tempInstance = SpecFileDataSource.SpecFileDataSource( tempFileName) mca_number = -1 for keyindex in keylist: info = tempInstance.getKeyInfo(keyindex) numberofmca = info['NbMca'] if numberofmca <= 0: continue # the positioners are for all the mca in the scan # 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] if positioners and "MotorNames" in dataObject.info: for mne in positioners: if mne in dataObject.info["MotorNames"]: mneIdx = \ dataObject.info["MotorNames"].index(mne) positioners[mne][nTimes] = \ dataObject.info["MotorValues"][mneIdx] self.onProgress(self.incrProgressBar) filecounter += 1 if positioners: self.info["positioners"] = positioners elif shape is None and (self.nbFiles == 1) and (iterlist == [1]): # it can only be here if there is one file # it can only be here if there is only one scan # it can only be here if there is only one detector self.data = numpy.zeros((1, 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 # prevent problems if the scan number is different # scan = tempInstance.select(keylist[-1]) scan = tempInstance[-1] iterationList = range(scan.nbmca()) for i in iterationList: # mcadata = scan_obj.mca(i) self.data[0, i, :] = scan.mca(i + 1)[:] self.incrProgressBar += 1 self.onProgress(self.incrProgressBar) 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, arrRet.shape[0]), arrRet.dtype.char) filecounter = 0 for tempFileName in filelist: tempInstance = specfile.Specfile(tempFileName) # it can only be here if there is one scan per file # prevent problems if the scan number is different # scan = tempInstance.select(keylist[-1]) scan = tempInstance[-1] for i in iterlist: # mcadata = scan_obj.mca(i) self.data[filecounter, 0, :] = scan.mca(i)[:] self.incrProgressBar += 1 self.onProgress(self.incrProgressBar) filecounter += 1 except MemoryError: qtflag = False if ('PyQt4.QtCore' in sys.modules) or \ ('PySide' in sys.modules) or \ ('PyMca5.PyMcaGui.PyMcaQt' in sys.modules): qtflag = True hdf5done = False if HDF5 and qtflag: from PyMca5.PyMcaGui import PyMcaQt as qt from PyMca5.PyMcaIO import ArraySave msg = qt.QMessageBox.information( \ None, "Memory error\n", "Do you want to convert your data to HDF5?\n", qt.QMessageBox.Yes,qt.QMessageBox.No) if msg != qt.QMessageBox.No: hdf5file = qt.QFileDialog.getSaveFileName( \ None, "Please select output file name", os.path.dirname(filelist[0]), "HDF5 files *.h5") if not len(hdf5file): raise IOError("Invalid output file") hdf5file = qt.safe_str(hdf5file) if not hdf5file.endswith(".h5"): hdf5file += ".h5" # get the final shape from PyMca5.RGBCorrelatorWidget import ImageShapeDialog stackImageShape = self.nbFiles,\ int(numberofmca/numberofdetectors) dialog = ImageShapeDialog(None, shape=stackImageShape) dialog.setModal(True) ret = dialog.exec() if ret: stackImageShape = dialog.getImageShape() dialog.close() del dialog hdf, self.data = ArraySave.getHDF5FileInstanceAndBuffer( \ hdf5file, (stackImageShape[0], stackImageShape[1], arrRet.shape[0]), compression=None, interpretation="spectrum") nRow = 0 nCol = 0 for tempFileName in filelist: tempInstance = specfile.Specfile(tempFileName) # it can only be here if there is one scan per file # prevent problems if the scan number is different # scan = tempInstance.select(keylist[-1]) scan = tempInstance[-1] nRow = int(self.incrProgressBar / stackImageShape[1]) nCol = self.incrProgressBar % stackImageShape[1] for i in iterlist: # mcadata = scan_obj.mca(i) self.data[nRow, nCol, :] = scan.mca(i)[:] self.incrProgressBar += 1 self.onProgress(self.incrProgressBar) hdf5done = True hdf.flush() self.onEnd() self.info["SourceType"] = "HDF5Stack1D" self.info["McaIndex"] = 2 self.info["FileIndex"] = 0 self.info["SourceName"] = [hdf5file] self.info["NumberOfFiles"] = 1 self.info["Size"] = 1 return else: raise else: # time information not read self._cleanupTimeInfo() sampling_order = 1 s0 = shape[0] s1 = shape[1] MEMORY_ERROR = False try: self.data = numpy.zeros((shape[0], shape[1], arrRet.shape[0]), arrRet.dtype.char) except MemoryError: try: self.data = numpy.zeros( (shape[0], shape[1], arrRet.shape[0]), numpy.float32) except MemoryError: MEMORY_ERROR = True while MEMORY_ERROR: try: for i in range(5): print("\7") sampling_order += 1 _logger.warning( "**************************************************") _logger.warning( " Memory error!, attempting %dx%d sub-sampling ", sampling_order, sampling_order) _logger.warning( "**************************************************") s0 = int(shape[0] / sampling_order) s1 = int(shape[1] / sampling_order) #if shape[0] % sampling_order: # s0 = s0 + 1 #if shape[1] % sampling_order: # s1 = s1 + 1 self.data = numpy.zeros((s0, s1, arrRet.shape[0]), numpy.float32) MEMORY_ERROR = False except MemoryError: pass filecounter = 0 for j in range(s0): filecounter = (j * sampling_order) * shape[1] for k in range(s1): tempFileName = filelist[filecounter] tempInstance = specfile.Specfile(tempFileName) if tempInstance is None: if not os.path.exists(tempFileName): _logger.error("File %s does not exists", tempFileName) raise IOError("File %s does not exists" % tempFileName) scan = tempInstance.select(keylist[-1]) for i in iterlist: # sum the present mcas self.data[j, k, :] += scan.mca(i)[:] self.incrProgressBar += 1 self.onProgress(self.incrProgressBar) filecounter += sampling_order self.nbFiles = s0 * s1 self.onEnd() """ # Scan types # ---------- #SF_EMPTY = 0 # empty scan #SF_SCAN = 1 # non-empty scan #SF_MESH = 2 # mesh scan #SF_MCA = 4 # single mca #SF_NMCA = 8 # multi mca (more than 1 mca per acq) case = None if scantype == (SpecFileDataSource.SF_MESH + \ SpecFileDataSource.SF_MCA): # SINGLE MESH + SINGLE MCA # nfiles = 1 # nscans = 1 # nmca = 1 # there is a danger if it can be considered an indexed file ... pass elif scantype == (SpecFileDataSource.SF_MESH + \ SpecFileDataSource.SF_NMCA): # SINGLE MESH + MULTIPLE MCA # nfiles = 1 # nscans = 1 # nmca > 1 # there is a danger if it can be considered an indexed file ... #for the time being I take last mca pass elif scantype == (SpecFileDataSource.SF_SCAN+ \ SpecFileDataSource.SF_MCA): #Assumed scans containing always 1 detector pass elif scantype == (SpecFileDataSource.SF_MCA): #Assumed scans containing always 1 detector pass elif scantype == (SpecFileDataSource.SF_SCAN+ \ SpecFileDataSource.SF_NMCA): #Assumed scans containing the same number of detectors #for the time being I take last mca pass elif scantype == (SpecFileDataSource.SF_NMCA): #Assumed scans containing the same number of detectors #for the time being I take last mca pass else: raise ValueError, "Unhandled scan type = %s" % scantype """ self.__nFiles = self.nbFiles self.__nImagesPerFile = 1 shape = self.data.shape for i in range(len(shape)): key = 'Dim_%d' % (i + 1, ) self.info[key] = shape[i] self.info["SourceType"] = SOURCE_TYPE self.info["SourceName"] = self.sourceName self.info["Size"] = self.__nFiles * self.__nImagesPerFile self.info["NumberOfFiles"] = self.__nFiles * 1 self.info["FileIndex"] = fileindex
def finishHDF5File(self, hdf): #add final date toplevelEntry = hdf["entry_000"] toplevelEntry['end_time'] = numpy.string_(ArraySave.getDate().encode('utf-8')) hdf.flush() hdf.close()
def _threadFinished(self): result = self.thread.result self.thread = None if type(result) == type((1,)): #if we receive a tuple there was an error if len(result): if result[0] == "Exception": # somehow this exception is not caught raise Exception(result[1], result[2])#, result[3]) return imageNames = result['names'] images = result["images"] nImages = images.shape[0] self._widget = StackPluginResultsWindow.StackPluginResultsWindow(\ usetab=False) self._widget.buildAndConnectImageButtonBox(replace=True, multiple=True) qt = StackPluginResultsWindow.qt self._widget.sigMaskImageWidgetSignal.connect(self.mySlot) self._widget.setStackPluginResults(images, image_names=imageNames) self._showWidget() # save to output directory parameters = self.configurationWidget.getParameters() outputDir = parameters["output_dir"] if outputDir in [None, ""]: if DEBUG: print("Nothing to be saved") return if parameters["file_root"] is None: fileRoot = "" else: fileRoot = parameters["file_root"].replace(" ","") if fileRoot in [None, ""]: fileRoot = "images" if not os.path.exists(outputDir): os.mkdir(outputDir) imagesDir = os.path.join(outputDir, "IMAGES") if not os.path.exists(imagesDir): os.mkdir(imagesDir) imageList = [None] * (nImages) fileImageNames = [None] * (nImages) j = 0 for i in range(nImages): name = imageNames[i].replace(" ","-") fileImageNames[j] = name imageList[j] = images[i] j += 1 fileName = os.path.join(imagesDir, fileRoot+".edf") ArraySave.save2DArrayListAsEDF(imageList, fileName, labels=fileImageNames) fileName = os.path.join(imagesDir, fileRoot+".csv") ArraySave.save2DArrayListAsASCII(imageList, fileName, csv=True, labels=fileImageNames) if parameters["tiff"]: i = 0 for i in range(len(fileImageNames)): label = fileImageNames[i] mass_fraction = "_" + label fileName = os.path.join(imagesDir, fileRoot + mass_fraction + ".tif") ArraySave.save2DArrayListAsMonochromaticTiff([imageList[i]], fileName, labels=[label], dtype=numpy.float32)
def saveStackAsSimplestHDF5(self): filename = self._getOutputHDF5Filename() if not len(filename): return ArraySave.save3DArrayAsHDF5(self._stack.data, filename, labels = None, dtype=None, mode='simplest')
def saveStackAsNeXusPlus(self): filename = self._getOutputHDF5Filename() if not len(filename): return ArraySave.save3DArrayAsHDF5(self._stack.data, filename, labels = None, dtype=None, mode='nexus+')
def loadFileList(self, filelist, fileindex=0, shape=None): if type(filelist) == type(''): filelist = [filelist] self.__keyList = [] self.sourceName = filelist self.__indexedStack = True self.sourceType = SOURCE_TYPE self.info = {} self.nbFiles=len(filelist) #read first file #get information tempInstance=SpecFileDataSource.SpecFileDataSource(filelist[0]) keylist = tempInstance.getSourceInfo()['KeyList'] nscans = len(keylist) #that is the number of scans nmca = 0 numberofdetectors = 0 for key in keylist: info = tempInstance.getKeyInfo(key) numberofmca = info['NbMca'] if numberofmca > 0: numberofdetectors = info['NbMcaDet'] scantype = info["ScanType"] if numberofmca: nmca += numberofmca if numberofdetectors == 0: raise ValueError("No MCA found in file %s" % filelist[0]) if (nscans > 1) and ((nmca/numberofdetectors) == nscans): SLOW_METHOD = True else: SLOW_METHOD = False #get last mca of first point key = "%s.1.%s" % (keylist[-1], numberofmca) dataObject = tempInstance._getMcaData(key) self.info.update(dataObject.info) arrRet = dataObject.data self.onBegin(self.nbFiles*nmca/numberofdetectors) self.incrProgressBar= 0 if info['NbMcaDet'] > 1: #Should I generate a map for each mca and not just for the last one as I am doing? iterlist = range(info['NbMcaDet'],info['NbMca']+1,info['NbMcaDet']) else: iterlist = [1] if SLOW_METHOD and shape is None: self.data = numpy.zeros((self.nbFiles, nmca/numberofdetectors, arrRet.shape[0]), arrRet.dtype.char) filecounter = 0 for tempFileName in filelist: tempInstance=SpecFileDataSource.SpecFileDataSource(tempFileName) mca_number = -1 for keyindex in keylist: info = tempInstance.getKeyInfo(keyindex) numberofmca = info['NbMca'] if numberofmca <= 0: continue key = "%s.1.%s" % (keyindex, numberofmca) dataObject = tempInstance._getMcaData(key) arrRet = dataObject.data mca_number += 1 for i in iterlist: #mcadata = scan_obj.mca(i) self.data[filecounter, mca_number, :] = arrRet[:] self.incrProgressBar += 1 self.onProgress(self.incrProgressBar) filecounter += 1 elif shape is None and (self.nbFiles == 1) and (iterlist == [1]): #it can only be here if there is one file #it can only be here if there is only one scan #it can only be here if there is only one detector self.data = numpy.zeros((1, numberofmca, arrRet.shape[0]), arrRet.dtype.char) for tempFileName in filelist: tempInstance=specfile.Specfile(tempFileName) #it can only be here if there is one scan per file #prevent problems if the scan number is different #scan = tempInstance.select(keylist[-1]) scan = tempInstance[-1] iterationList = range(scan.nbmca()) for i in iterationList: #mcadata = scan_obj.mca(i) self.data[0, i, :] = scan.mca(i+1)[:] self.incrProgressBar += 1 self.onProgress(self.incrProgressBar) filecounter = 1 elif shape is None: #it can only be here if there is one scan per file try: self.data = numpy.zeros((self.nbFiles, numberofmca/numberofdetectors, arrRet.shape[0]), arrRet.dtype.char) filecounter = 0 for tempFileName in filelist: tempInstance=specfile.Specfile(tempFileName) #it can only be here if there is one scan per file #prevent problems if the scan number is different #scan = tempInstance.select(keylist[-1]) scan = tempInstance[-1] for i in iterlist: #mcadata = scan_obj.mca(i) self.data[filecounter, 0, :] = scan.mca(i)[:] self.incrProgressBar += 1 self.onProgress(self.incrProgressBar) filecounter += 1 except MemoryError: qtflag = False if ('PyQt4.QtCore' in sys.modules) or\ ('PySide' in sys.modules) or\ ('PyMca5.PyMcaGui.PyMcaQt' in sys.modules): qtflag = True hdf5done = False if HDF5 and qtflag: from PyMca5.PyMcaGui import PyMcaQt as qt from PyMca5.PyMcaIO import ArraySave msg=qt.QMessageBox.information( None, "Memory error\n", "Do you want to convert your data to HDF5?\n", qt.QMessageBox.Yes,qt.QMessageBox.No) if msg != qt.QMessageBox.No: hdf5file = qt.QFileDialog.getSaveFileName(None, "Please select output file name", os.path.dirname(filelist[0]), "HDF5 files *.h5") if not len(hdf5file): raise IOError("Invalid output file") hdf5file = qt.safe_str(hdf5file) if not hdf5file.endswith(".h5"): hdf5file += ".h5" #get the final shape from PyMca5.RGBCorrelatorWidget import ImageShapeDialog stackImageShape = self.nbFiles,\ int(numberofmca/numberofdetectors) dialog = ImageShapeDialog(None, shape =stackImageShape) dialog.setModal(True) ret = dialog.exec_() if ret: stackImageShape= dialog.getImageShape() dialog.close() del dialog hdf, self.data = ArraySave.getHDF5FileInstanceAndBuffer(hdf5file, (stackImageShape[0], stackImageShape[1], arrRet.shape[0]), compression=None, interpretation="spectrum") nRow = 0 nCol = 0 for tempFileName in filelist: tempInstance=specfile.Specfile(tempFileName) #it can only be here if there is one scan per file #prevent problems if the scan number is different #scan = tempInstance.select(keylist[-1]) scan = tempInstance[-1] nRow = int(self.incrProgressBar/stackImageShape[1]) nCol = self.incrProgressBar%stackImageShape[1] for i in iterlist: #mcadata = scan_obj.mca(i) self.data[nRow, nCol, :] = scan.mca(i)[:] self.incrProgressBar += 1 self.onProgress(self.incrProgressBar) hdf5done = True hdf.flush() self.onEnd() self.info["SourceType"] = "HDF5Stack1D" self.info["McaIndex"] = 2 self.info["FileIndex"] = 0 self.info["SourceName"] = [hdf5file] self.info["NumberOfFiles"] = 1 self.info["Size"] = 1 return else: raise else: sampling_order = 1 s0 = shape[0] s1 = shape[1] MEMORY_ERROR = False try: self.data = numpy.zeros((shape[0], shape[1], arrRet.shape[0]), arrRet.dtype.char) except MemoryError: try: self.data = numpy.zeros((shape[0], shape[1], arrRet.shape[0]), numpy.float32) except MemoryError: MEMORY_ERROR = True while MEMORY_ERROR: try: for i in range(5): print("\7") sampling_order += 1 print("**************************************************") print(" Memory error!, attempting %dx%d sub-sampling " %\ (sampling_order, sampling_order)) print("**************************************************") s0 = int(shape[0]/sampling_order) s1 = int(shape[1]/sampling_order) #if shape[0] % sampling_order: # s0 = s0 + 1 #if shape[1] % sampling_order: # s1 = s1 + 1 self.data = numpy.zeros((s0, s1, arrRet.shape[0]), numpy.float32) MEMORY_ERROR = False except MemoryError: pass filecounter = 0 for j in range(s0): filecounter = (j * sampling_order) * shape[1] for k in range(s1): tempFileName = filelist[filecounter] tempInstance=specfile.Specfile(tempFileName) if tempInstance is None: if not os.path.exists(tempFileName): print("File %s does not exists" % tempFileName) raise IOError(\ "File %s does not exists" % tempFileName) scan = tempInstance.select(keylist[-1]) for i in iterlist: #sum the present mcas self.data[j, k, :] += scan.mca(i)[:] self.incrProgressBar += 1 self.onProgress(self.incrProgressBar) filecounter += sampling_order self.nbFiles = s0 * s1 self.onEnd() """ # Scan types # ---------- #SF_EMPTY = 0 # empty scan #SF_SCAN = 1 # non-empty scan #SF_MESH = 2 # mesh scan #SF_MCA = 4 # single mca #SF_NMCA = 8 # multi mca (more than 1 mca per acq) case = None if scantype == (SpecFileDataSource.SF_MESH + \ SpecFileDataSource.SF_MCA): # SINGLE MESH + SINGLE MCA # nfiles = 1 # nscans = 1 # nmca = 1 # there is a danger if it can be considered an indexed file ... pass elif scantype == (SpecFileDataSource.SF_MESH + \ SpecFileDataSource.SF_NMCA): # SINGLE MESH + MULTIPLE MCA # nfiles = 1 # nscans = 1 # nmca > 1 # there is a danger if it can be considered an indexed file ... #for the time being I take last mca pass elif scantype == (SpecFileDataSource.SF_SCAN+ \ SpecFileDataSource.SF_MCA): #Assumed scans containing always 1 detector pass elif scantype == (SpecFileDataSource.SF_MCA): #Assumed scans containing always 1 detector pass elif scantype == (SpecFileDataSource.SF_SCAN+ \ SpecFileDataSource.SF_NMCA): #Assumed scans containing the same number of detectors #for the time being I take last mca pass elif scantype == (SpecFileDataSource.SF_NMCA): #Assumed scans containing the same number of detectors #for the time being I take last mca pass else: raise ValueError, "Unhandled scan type = %s" % scantype """ self.__nFiles = self.nbFiles self.__nImagesPerFile = 1 shape = self.data.shape for i in range(len(shape)): key = 'Dim_%d' % (i+1,) self.info[key] = shape[i] self.info["SourceType"] = SOURCE_TYPE self.info["SourceName"] = self.sourceName self.info["Size"] = self.__nFiles * self.__nImagesPerFile self.info["NumberOfFiles"] = self.__nFiles * 1 self.info["FileIndex"] = fileindex
def threadFinished(self): result = self.thread.result self.thread = None if type(result) == type((1,)): #if we receive a tuple there was an error if len(result): if result[0] == "Exception": # somehow this exception is not caught raise Exception(result[1], result[2]) return if 'concentrations' in result: imageNames = result['names'] images = numpy.concatenate((result['parameters'], result['concentrations']), axis=0) else: images = result['parameters'] imageNames = result['names'] nImages = images.shape[0] self._widget = StackPluginResultsWindow.StackPluginResultsWindow(\ usetab=False) self._widget.buildAndConnectImageButtonBox() qt = StackPluginResultsWindow.qt self._widget.sigMaskImageWidgetSignal.connect(self.mySlot) self._widget.setStackPluginResults(images, image_names=imageNames) self._showWidget() # save to output directory parameters = self.configurationWidget.getParameters() outputDir = parameters["output_dir"] if outputDir in [None, ""]: if DEBUG: print("Nothing to be saved") return if parameters["file_root"] is None: fileRoot = "" else: fileRoot = parameters["file_root"].replace(" ","") if fileRoot in [None, ""]: fileRoot = "images" if not os.path.exists(outputDir): os.mkdir(outputDir) imagesDir = os.path.join(outputDir, "IMAGES") if not os.path.exists(imagesDir): os.mkdir(imagesDir) imageList = [None] * (nImages + len(result['uncertainties'])) fileImageNames = [None] * (nImages + len(result['uncertainties'])) j = 0 for i in range(nImages): name = imageNames[i].replace(" ","-") fileImageNames[j] = name imageList[j] = images[i] j += 1 if not imageNames[i].startswith("C("): # fitted parameter fileImageNames[j] = "s(%s)" % name imageList[j] = result['uncertainties'][i] j += 1 fileName = os.path.join(imagesDir, fileRoot+".edf") ArraySave.save2DArrayListAsEDF(imageList, fileName, labels=fileImageNames) fileName = os.path.join(imagesDir, fileRoot+".csv") ArraySave.save2DArrayListAsASCII(imageList, fileName, csv=True, labels=fileImageNames)