def createPsuadeInFile(data, filename, includePDF=True): outf = open(filename, 'w') outf.write('PSUADE\n') #Write inputs outf.write('INPUT\n') outf.write(' dimension = %d\n' % data.getNumInputs()) names = data.getInputNames() mins = data.getInputMins() maxs = data.getInputMaxs() indices = list(range(data.getNumInputs())) for i, name, minimum, maximum in zip(indices, names, mins, maxs): outf.write(' variable %d %s = %e %e\n' % (i + 1, name, minimum, maximum)) distributions = data.getInputDistributions() for i, dist in zip(indices, distributions): distType = dist.getDistributionType() distParams = dist.getParameterValues() if distType == Distribution.SAMPLE: outf.write(' PDF %d %c' % (i + 1, Distribution.getPsuadeName(distType))) if distParams[0] is not None: filename = distParams[0] if platform.system() == 'Windows': import win32api filename = win32api.GetShortPathName(filename) outf.write(' %s' % filename) if distParams[1] is not None: outf.write(' %d' % distParams[1]) outf.write('\n') elif includePDF: # write out PDF info for all non-uniform PDF types if distType != Distribution.UNIFORM: outf.write(' PDF %d %c' % (i + 1, Distribution.getPsuadeName(distType))) if distParams[0] is not None: outf.write(' %e' % distParams[0]) if distParams[1] is not None: outf.write(' %e' % distParams[1]) outf.write('\n') outf.write('END\n') #Write outputs outf.write('OUTPUT\n') if data.getNumOutputs() == 0: outf.write(' dimension = 1\n') names = ['ghostOuput'] indices = list(range(1)) for i, name in zip(indices, names): outf.write(' variable %d %s\n' % (i + 1, name)) else: outf.write(' dimension = %d\n' % data.getNumOutputs()) names = data.getOutputNames() indices = list(range(data.getNumOutputs())) for i, name in zip(indices, names): outf.write(' variable %d %s\n' % (i + 1, name)) outf.write('END\n') #Write Method outf.write('METHOD\n') outf.write(' sampling = %s\n' % SamplingMethods.getPsuadeName(data.getSampleMethod())) outf.write(' num_samples = %d\n' % data.getNumSamples()) if data.getSampleMethod() != SamplingMethods.GMOAT: outf.write(' randomize\n') outf.write('END\n') #Write Application outf.write('APPLICATION\n') driverString = data.getDriverName() if driverString is None: driverString = 'NONE' elif platform.system() == 'Windows': if os.path.exists(driverString): import win32api driverString = win32api.GetShortPathName(driverString) else: driverString = 'NONE' driverString = 'NONE' outf.write(' driver = %s\n' % driverString) outf.write(' save_frequency = 1\n') outf.write('END\n') #Write Analysis outf.write('ANALYSIS\n') outf.write(' diagnostics 2\n') outf.write('END\n') outf.write('END\n') outf.close()
def generateSamples(self): # self.setModal(False) # Gather all info into SampleData object if isinstance(self.model, Model): model = copy.deepcopy(self.model) # runData = SampleData(self.model) else: model = copy.deepcopy(self.model.model) # runData = copy.deepcopy(self.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) runData.setModelName( self.session.flowsheet.results.incrimentSetName("UQ_Ensemble")) 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 ## print runData.getNumSamples() 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: # Common.removeArchive(self.currentArchive) self.currentArchiveData.removeArchiveFolder() pass # Common.archiveFile('psuadeMetisInfo', self.runData.getID()) 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)