def loadDict(self, sd, filename, stopConsumers=True): ''' Load a session from a string ''' # Clear session information self.new(stopConsumers=stopConsumers) # Read metaData information metaData = sd.get("CCSIFileMetaData", None) if metaData: # Read information from meta data if it exists self.description = metaData.get("Description", "None") self.creationTime = metaData.get("CreationTime", "") self.changeLog = metaData.get("ChangeLog", {}) self.parents = metaData.get("Parents", []) self.originalFileName = metaData.get("OriginalFilename", "") self.date = metaData.get("ModificationTime", "") self.version = metaData.get("Version", "00.00") self.name = metaData.get("DisplayName", "") self.uid = metaData.get("ID", self.uid) self.confidence = metaData.get("Confidence", "experimental") else: # Older session files read data from old locations self.description = sd.get("description", "None") self.date = sd.get("date", "") #Read flowsheet self.flowsheet.loadDict(sd["flowsheet"]) #Read ID for UQ archives, should prob get rid of this and use #metadata ID fullFile = os.path.abspath(filename) pathName, baseName = os.path.split(fullFile) base, ext = os.path.splitext(baseName) self.ID = sd.get("ID", self.ID) self.archiveFolder = os.path.join( os.path.dirname(os.path.abspath(filename)), '%s_files' % self.ID) # Load the surrogate model settings self.surrogateProblem = sd.get('surrogateProblem', {}) self.surrogateCurrent = sd.get("surrogateCurrent", None) # Load the optimization problem if exists self.optProblem = oprob.problem() self.optProblem.dat = self p = sd.get('optProblem', None) if p: self.optProblem.loadDict(p) # Load UQ Stuff self.uqSimList = [] if 'uqSimList' in sd: for simDict in sd['uqSimList']: model = Model() model.loadDict(simDict['model']) sim = SampleData(model) sim.setSession(self) sim.loadDict(simDict) self.uqSimList.append(sim) self.uqFilterResultsList = [] if 'uqFilterResultsList' in sd: for filterDict in sd['uqFilterResultsList']: filterResults = Results() filterResults.loadDict(filterDict) self.uqFilterResultsList.append(filterResults) self.currentFile = None
def changeDataInSimTable(self, data, row): if data is None: return self.dat.sdoeSimList[row] = data res = Results() res.sdoe_add_result(data) self.dat.sdoeFilterResultsList[row] = res self.updateSimTableRow(row)
def addDataToSimTable(self, data): if data is None: return self.dat.sdoeSimList.append(data) res = Results() res.sdoe_add_result(data) self.dat.sdoeFilterResultsList.append(res) self.updateSimTable()
def cloneSimulation(self): # Get selected row row = self.filesTable.selectedIndexes()[0].row() sim = copy.deepcopy(self.dat.sdoeSimList[row]) # Create copy of sim sim.clearRunState() sim.turbineSession = None sim.turbineJobIds = [] self.dat.sdoeSimList.append(sim) # Add to simulation list res = Results() res.sdoe_add_result(sim) self.dat.sdoeFilterResultsList.append(sim) # Update table self.updateSimTable()
def delete(self): # check arguments if not self.activateDeleteButton(0, 0): return self.enableDelete(False) self.freeze() # get selected row row = self.filesTable.selectedIndexes()[0].row() data = self.dat.sdoeSimList[row] fname = Common.getLocalFileName(DataProcessor.dname, data.getModelName().split()[0], '.dat') data.writeToPsuade(fname) # perform deletion samples, inVars, outVars, nSamples, nInputs, nOutputs = self.getDeleteSelections( ) if samples: samplesToKeep = [i for i in range(nSamples) if i not in samples] newdata = data.getSubSample(samplesToKeep) else: newdata = copy.deepcopy(data) if inVars: newdata.deleteInputs(inVars) if outVars: newdata.deleteOutputs(outVars) newdata.setModelName(data.getModelName().split('.')[0] + '.deleted') newdata.setSession(self.dat) # add to simulation table, select new data self.dat.sdoeSimList.append(newdata) res = Results() res.sdoe_add_result(newdata) self.dat.sdoeFilterResultsList.append(res) self.updateSimTable() self.deleteTable.resizeColumnsToContents() # reset components self.unfreeze()
def loadSimulation(self): self.freeze() # Get file name if platform.system() == 'Windows': allFiles = '*.*' else: allFiles = '*' fileName, selectedFilter = QFileDialog.getOpenFileName( self, "Open Ensemble", '', "CSV (Comma delimited) (*.csv)") if len(fileName) == 0: self.unfreeze() return if fileName.endswith('.csv'): data = LocalExecutionModule.readSampleFromCsvFile(fileName, False) else: try: data = LocalExecutionModule.readSampleFromPsuadeFile(fileName) except: import traceback traceback.print_exc() QtGui.QMessageBox.critical( self, 'Incorrect format', 'File does not have the correct format! Please consult the users manual about the format.' ) logging.getLogger("foqus." + __name__).exception( "Error loading psuade file.") self.unfreeze() return data.setSession(self.dat) self.dat.sdoeSimList.append(data) res = Results() res.sdoe_add_result(data) self.dat.sdoeFilterResultsList.append(res) shutil.copy(fileName, self.dname) # Update table self.updateSimTable() self.dataTabs.setEnabled(True) self.unfreeze()
def refreshFilterData(self, updateResult=False): indexes = self.filesTable.selectedIndexes() if len(indexes) == 0: return row = indexes[0].row() data = self.dat.uqSimList[row] if updateResult or not self.dat.uqFilterResultsList: if not self.dat.uqFilterResultsList: self.dat.uqFilterResultsList = [None] * len(self.dat.uqSimList) res = Results() res.uq_add_result(data) self.dat.uqFilterResultsList[row] = res else: res = self.dat.uqFilterResultsList[row] self.filterWidget.init(self.dat) self.filterWidget.setResults(res) self.filterWidget.refreshContents()
def createFilteredEnsemble(self, indices): self.freeze() # get selected row row = self.filesTable.selectedIndexes()[0].row() data = self.dat.sdoeSimList[row] newdata = data.getSubSample(indices) newdata.setModelName(data.getModelName().split('.')[0] + '.filtered') newdata.setSession(self.dat) # add to simulation table, select new data self.dat.sdoeSimList.append(newdata) res = Results() res.sdoe_add_result(newdata) self.dat.sdoeFilterResultsList.append(res) self.updateSimTable() # reset components self.unfreeze()
def generateSamples(self): # Gather all info into SampleData object if isinstance(self.model, Model): model = copy.deepcopy(self.model) else: model = copy.deepcopy(self.model.model) # Gather distributions from distribution table types = [] modelTypes = self.model.getInputTypes() defaults = [] modelDefaults = self.model.getInputDefaults() mins = [] modelMins = self.model.getInputMins() maxs = [] modelMaxs = self.model.getInputMaxs() dists = [] selectedInputs = [] # Set sampling scheme to selected or monte carlo if adaptive if self.chooseSchemeRadio.isChecked(): scheme = self.schemesList.currentItem().text() else: scheme = "Monte Carlo" # First get parameters for the model row = 0 for inputNum in range(self.model.getNumInputs()): if modelTypes[inputNum] == Model.VARIABLE: # Type combobox = self.distTable.cellWidget(row, 1) if combobox is None: text = self.distTable.item(row, 1).text() else: text = combobox.currentText() if text == "Fixed": value = Model.FIXED else: value = Model.VARIABLE types.append(value) if value == Model.VARIABLE: selectedInputs.append(inputNum) # Defaults item = self.distTable.item(row, 2) if item is None or len(item.text()) == 0: defaults.append(None) else: defaults.append(float(item.text())) # Mins item = self.distTable.item(row, 3) mins.append(float(item.text())) # Maxs item = self.distTable.item(row, 4) maxs.append(float(item.text())) row += 1 else: # Fixed types.append(Model.FIXED) defaults.append(modelDefaults[inputNum]) mins.append(modelMins[inputNum]) maxs.append(modelMaxs[inputNum]) # Update model model.setInputTypes(types) model.setInputDefaults(defaults) model.setInputMins(mins) model.setInputMaxs(maxs) # Create SampleData object runData = SampleData(model, self.session) res = Results() res.sdoe_add_result(runData) possibleNames = [] sdoeSimList = [] for i in range(len(self.session.sdoeSimList)): sdoeSimList.append(self.session.sdoeSimList[i].getModelName()) for i in range(100): possibleNames.append("SDoE_Ensemble_%s" % str(i + 1)) for i in range(len(possibleNames)): if possibleNames[i] not in sdoeSimList: newName = possibleNames[i] break else: newName = possibleNames[i + 1] runData.setModelName(newName) runData.setFromFile(False) # Now get distributions for the SampleData object numSampleFromFile = 0 row = 0 for inputNum in range(self.model.getNumInputs()): if modelTypes[inputNum] == Model.VARIABLE: # Only collect those that are not fixed to generate inputs combobox = self.distTable.cellWidget(row, 5) dist = combobox.currentIndex() # Check non-uniform distribution and non-Monte Carlo scheme if ( False ): # dist != Distribution.UNIFORM and SamplingMethods.getEnumValue(scheme) != SamplingMethods.MC: msgbox = QMessageBox() msgbox.setWindowTitle("UQ/Opt GUI Warning") msgbox.setText( "Non-Uniform distributions are not compatible with any " + "sampling scheme other than Monte Carlo! Please change " + "all distributions back to uniform or select Monte Carlo " + "sampling scheme.") msgbox.setIcon(QMessageBox.Warning) msgbox.exec_() return if dist == Distribution.SAMPLE: numSampleFromFile += 1 dists += [self.distTable.getDistribution(row)] row += 1 else: # Fixed dist = Distribution(Distribution.UNIFORM) dists = dists + [dist] runData.setInputDistributions(dists) numSamples = int(self.numSamplesBox.value()) runData.setNumSamples(numSamples) runData.setSampleMethod(scheme) # Check number of samples scheme = runData.getSampleMethod() newNumSamples = SamplingMethods.validateSampleSize( scheme, len(selectedInputs), numSamples) if scheme == SamplingMethods.LSA: if newNumSamples != numSamples: msgbox = QMessageBox() msgbox.setWindowTitle("UQ/Opt GUI Warning") msgbox.setText( "%s scheme with %d variable inputs requires %d samples! Do you want to proceed?" % ( SamplingMethods.getPsuadeName(scheme), len(selectedInputs), newNumSamples, )) msgbox.setIcon(QMessageBox.Question) msgbox.setStandardButtons(QMessageBox.Yes | QMessageBox.No) msgbox.setDefaultButton(QMessageBox.Yes) response = msgbox.exec_() if response == QMessageBox.Yes: runData.setNumSamples(newNumSamples) else: return elif scheme == SamplingMethods.MOAT or scheme == SamplingMethods.GMOAT: if type(newNumSamples) is tuple: msgbox = QMessageBox() msgbox.setWindowTitle("UQ/Opt GUI Warning") msgbox.setText( "%s scheme with %d variable inputs cannot have %d samples! How do you want to proceed?" % ( SamplingMethods.getFullName(scheme), len(selectedInputs), numSamples, )) msgbox.setIcon(QMessageBox.Question) firstValButton = msgbox.addButton( "Change to %d samples" % newNumSamples[0], QMessageBox.AcceptRole) secondValButton = msgbox.addButton( "Change to %d samples" % newNumSamples[1], QMessageBox.AcceptRole) cancelButton = msgbox.addButton(QMessageBox.Cancel) msgbox.exec_() if msgbox.clickedButton() == firstValButton: runData.setNumSamples(int(newNumSamples[0])) elif msgbox.clickedButton() == secondValButton: runData.setNumSamples(int(newNumSamples[1])) else: return # Visual indications of processing QApplication.setOverrideCursor(QCursor(Qt.WaitCursor)) self.generateStatusText.setText("Generating...") self.generateStatusText.repaint() # Generate samples for the variable inputs selectedRunData = ExperimentalDesign.generateSamples( runData, selectedInputs, self.model.getSelectedOutputs()) if selectedRunData is None: QApplication.restoreOverrideCursor() self.generateStatusText.setText("") return selectedInputData = selectedRunData.getInputData() # Add fixed inputs back in fullInputData = [0] * runData.getNumSamples() for row in range(runData.getNumSamples()): rowData = [] selectedIndex = 0 for col in range(runData.getNumInputs()): if col in selectedInputs: rowData.append(selectedInputData[row][selectedIndex]) selectedIndex = selectedIndex + 1 else: rowData.append(defaults[col]) fullInputData[row] = rowData runData.setInputData(fullInputData) runData.setRunState([0] * runData.getNumSamples()) self.runData = runData # Handle archive of METIS file if self.runData.getSampleMethod() == SamplingMethods.METIS: if self.currentArchiveData is not None: self.currentArchiveData.removeArchiveFolder() pass self.runData.archiveFile("psuadeMetisInfo") self.currentArchiveData = self.runData # Restore cursor QApplication.restoreOverrideCursor() self.generateStatusText.setText("Done!") self.samplesGenerated = True self.previewButton.setEnabled(True) self.doneButton.setEnabled(True)