Exemple #1
0
 def getData(self):
     if self.loadSamplesRadio.isChecked():
         returnData = self.loadData
     elif self.flowsheetDataRadio.isChecked():
         self.session.flowsheet.results.exportPSUADE("temp/psuadeData2.tmp")
         returnData = LocalExecutionModule.readSampleFromPsuadeFile("temp/psuadeData2.tmp")
     else:
         returnData = self.runData
     return returnData
Exemple #2
0
 def showAdaptiveRefineSchemes(self):
     if self.adaptiveRefineRadio.isChecked():
         foundLibs = LocalExecutionModule.getPsuadeInstalledModules();
         foundMETIS = foundLibs['METIS']
         self.schemesList.clear()
         self.schemesList.addItem(SamplingMethods.getFullName(SamplingMethods.METIS))
         if not foundMETIS:
             item = self.schemesList.item(0)
             text = item.text()
             item.setText(text + ' (Not installed)')
             flags = item.flags()
             item.setFlags(flags & ~Qt.ItemIsEnabled)
Exemple #3
0
    def showAllSchemes(self):
        if self.chooseSchemeRadio.isChecked():
            foundLibs = LocalExecutionModule.getPsuadeInstalledModules();
            foundMETIS = foundLibs['METIS']

            self.schemesList.clear()
            self.schemesList.addItems(SamplingMethods.fullNames[:-2]) # Remove GMETIS
            if not foundMETIS:
                item = self.schemesList.item(SamplingMethods.METIS)
                text = item.text()
                item.setText(text + ' (Not installed)')
                flags = item.flags()
                item.setFlags(flags & ~Qt.ItemIsEnabled)
Exemple #4
0
    def __init__(self,
                 model,
                 session,
                 viewOnly=False,
                 returnDataSignal=None,
                 parent=None):
        super(SimSetup, self).__init__(parent)

        self.setupUi(self)
        self.viewOnly = viewOnly
        self.returnDataSignal = returnDataSignal
        self.sampleFileSet = (
            set()
        )  # Contains which inputs are of the distribution "Sample from File"

        self.fsDataBrowser = dataBrowserFrame(session, self)
        self.dataViewWidget.setLayout(QStackedLayout(self.dataViewWidget))
        self.dataViewWidget.layout().addWidget(self.fsDataBrowser)
        self.fsDataBrowser.refreshContents()

        self.fileLoaded = False
        self.samplesGenerated = False

        self.currentArchiveData = None

        self.model = model
        self.session = session

        if isinstance(model, Model):
            data = SampleData(model)
            dists = []
            for i in range(model.getNumInputs()):
                dists = dists + ["U"]
            data.setInputDistributions(dists)
        else:
            data = model
            self.currentArchiveData = data

        self.ignoreDistributionCheck = False

        if model.getRunType() == Model.EMULATOR:
            self.flowsheetDataRadio.setHidden(True)

        if viewOnly:
            self.loadSamplesRadio.setEnabled(False)
            self.flowsheetDataRadio.setEnabled(False)
        self.loadSamplesRadio.clicked.connect(self.setPage)
        self.flowsheetDataRadio.clicked.connect(self.setPage)
        self.chooseSchemeRadio.clicked.connect(self.setPage)
        self.setPage()

        self.browseButton.clicked.connect(self.loadSampleFile)

        # self.cancelButton.clicked.connect(self.reject)
        self.cancelButton.clicked.connect(self.cancel)
        self.previewButton.clicked.connect(self.preview)
        self.previewButton.setEnabled(False)
        # self.doneButton.clicked.connect(self.accept)
        self.doneButton.clicked.connect(self.doneClicked)
        self.doneButton.setEnabled(False)
        if viewOnly:
            self.cancelButton.setText("OK")
            self.doneButton.setHidden(True)
            self.samplingTabs.setTabEnabled(1, False)

        # Make sure appropriate option is visible
        # self.chooseSchemeRadio.setChecked(True)
        # self.showChooseSchemePage()
        if not isinstance(model, Model):
            if model.getInputData() is not None:
                self.previewButton.setEnabled(True)
                if not viewOnly:
                    self.doneButton.setEnabled(True)
                self.runData = model

        # Make sure distribution tab is showing
        self.samplingTabs.setCurrentIndex(0)
        self.samplingTabs.currentChanged[int].connect(self.checkDists)

        # Set up distributions table
        self.distTable.init(model, InputPriorTable.SIMSETUP, viewOnly=viewOnly)

        self.allFixedButton.clicked.connect(self.makeAllFixed)
        self.allVariableButton.clicked.connect(self.makeAllVariable)
        if viewOnly or model.getRunType() == Model.EMULATOR:
            self.allFixedButton.setHidden(True)
            self.allVariableButton.setHidden(True)

        # Set up sampling schemes tab
        self.generateSamplesButton.setEnabled(False)
        self.generateStatusText.setText("")
        self.allSchemesRadio.setChecked(True)

        foundLibs = LocalExecutionModule.getPsuadeInstalledModules()
        foundMETIS = foundLibs["METIS"]

        self.schemesList.clear()
        self.schemesList.addItems(SamplingMethods.fullNames[:-2]
                                  )  # Leave out GMETIS and FULL Factorial
        if not foundMETIS:
            item = self.schemesList.item(SamplingMethods.METIS)
            text = item.text()
            item.setText(text + " (Not installed)")
            flags = item.flags()
            item.setFlags(flags & ~Qt.ItemIsEnabled)

        if isinstance(model, SampleData):
            self.schemesList.setCurrentRow(model.getSampleMethod())
            self.generateSamplesButton.setEnabled(True)

        # Change lists of schemes displayed
        self.allSchemesRadio.toggled.connect(self.showAllSchemes)
        self.paramScreenRadio.toggled.connect(self.showParamScreenSchemes)
        self.adaptiveRefineRadio.toggled.connect(
            self.showAdaptiveRefineSchemes)
        self.otherRadio.toggled.connect(self.showOtherSchemes)

        self.schemesList.currentItemChanged.connect(
            self.handleGenerateSamplesButton)
        self.generateSamplesButton.clicked.connect(self.generateSamples)
Exemple #5
0
    def checkDists(self, tabIndex):
        if tabIndex == 0 or self.ignoreDistributionCheck:
            self.ignoreDistributionCheck = False
            return

        showMessage = False
        if self.distTable.getNumVariables() == 0:
            showMessage = True
            message = "All inputs are fixed! One needs to be variable."
        else:
            valid, error = self.distTable.checkValidInputs()
            if not valid:
                showMessage = True
                message = (
                    "Distribution settings not correct or entirely filled out! %s"
                    % error)
            else:
                rowsToWarnAboutMass = []
                for row in range(self.distTable.rowCount()):
                    for col in [3, 4]:
                        item = self.distTable.item(row, col)
                        if col == 3:
                            minVal = float(item.text())
                        else:
                            maxVal = float(item.text())

                    #### Get distribution parameters
                    for col in [6, 7]:
                        cellTable = self.distTable.cellWidget(row, col)
                        if isinstance(cellTable, QComboBox):
                            continue
                        item = None
                        if cellTable is not None:
                            item = cellTable.item(0, 1)
                        if item is not None and item.text():
                            if col == 6:
                                distParam1 = float(item.text())
                            else:
                                distParam2 = float(item.text())
                        else:
                            if col == 6:
                                distParam1 = None
                            else:
                                distParam2 = None

                    #### Check mass and warn if below 50%
                    # Only collect those that are not fixed to generate inputs
                    combobox = self.distTable.cellWidget(row, 5)
                    dist = combobox.currentIndex()
                    # Create file for psuade input
                    if dist not in [Distribution.UNIFORM, Distribution.SAMPLE]:
                        f = tempfile.SpooledTemporaryFile()
                        for i in range(2):
                            f.write(b"cdf_lookup\n")
                            distNum = dist
                            if dist == Distribution.BETA:
                                distNum = 4
                            elif dist == Distribution.WEIBULL:
                                distNum = 5
                            elif dist == Distribution.GAMMA:
                                distNum = 6
                            elif dist == Distribution.EXPONENTIAL:
                                distNum = 7
                            f.write(b"%d\n" %
                                    distNum)  # Number of distribution
                            f.write(b"%f\n" % distParam1)  # Parameter 1
                            if distParam2 is not None:
                                f.write(b"%f\n" % distParam2)  # Parameter 2
                            if i == 0:
                                val = minVal
                            else:
                                val = maxVal
                            f.write(b"%f\n" % val)  # Min or max value
                        f.write(b"quit\n")
                        f.seek(0)

                        # invoke psuade
                        psuadePath = LocalExecutionModule.getPsuadePath()
                        if psuadePath is None:
                            return
                        p = subprocess.Popen(
                            psuadePath,
                            stdin=f,
                            stdout=subprocess.PIPE,
                            stderr=subprocess.PIPE,
                            shell=True,
                        )
                        f.close()

                        # process error
                        out, error = p.communicate()
                        if error:
                            Common.showError(error, out)
                            return None

                        # parse output
                        lines = out.splitlines()
                        vals = []
                        for line in lines:
                            if "Cumulative probability = " in line.decode(
                                    "utf-8"):
                                words = line.split()
                                vals.append(float(words[-1]))

                        mass = vals[1] - vals[0]
                        if mass < 0.5:
                            rowsToWarnAboutMass.append(row)

                if len(rowsToWarnAboutMass) > 0:
                    self.samplingTabs.setCurrentIndex(0)
                    for row in rowsToWarnAboutMass:
                        msgbox = QMessageBox()
                        msgbox.setWindowTitle("UQ/Opt GUI Warning")
                        msgbox.setText(
                            "Regarding input " +
                            self.model.getInputNames()[row] +
                            ": Min/max range is narrow for its distribution. "
                            +
                            "This could cause sample generation to take more time.  Continue?"
                        )
                        msgbox.setIcon(QMessageBox.Warning)
                        msgbox.setStandardButtons(QMessageBox.Yes
                                                  | QMessageBox.No)
                        msgbox.setDefaultButton(QMessageBox.Yes)
                        ret = msgbox.exec_()
                        if ret != QMessageBox.Yes:
                            self.distTable.selectRow(row)
                            return
                    self.ignoreDistributionCheck = True
                    self.samplingTabs.setCurrentIndex(1)

        if showMessage:
            self.samplingTabs.setCurrentIndex(0)
            msgbox = QMessageBox()
            msgbox.setWindowTitle("UQ/Opt GUI Warning")
            msgbox.setText(message)
            msgbox.setIcon(QMessageBox.Warning)
            msgbox.exec_()
            return
Exemple #6
0
    def loadSampleFile(self):
        if platform.system() == "Windows":
            allFiles = "*.*"
        else:
            allFiles = "*"
        sampleFile, _ = QFileDialog.getOpenFileName(
            self,
            "Load Sample file",
            "",
            "Psuade Files (*.dat *.psuade *.filtered);;Psuade Simple File (*.smp);;CSV (Comma delimited) (*.csv);;All files (%s)"
            % allFiles,
        )
        if not sampleFile:  # Cancelled
            return
        fromSimpleFile = False
        try:
            if sampleFile.endswith(".csv"):
                data = LocalExecutionModule.readSampleFromCsvFile(
                    sampleFile, False)
                inputNames = data.getInputNames()
                if len(inputNames[0]) == 0:
                    fromSimpleFile = True
            else:
                data = LocalExecutionModule.readSampleFromPsuadeFile(
                    sampleFile)
            inputData = data.getInputData()
            numSamples = data.getNumSamples()
            numInputs = data.getNumInputs()
            numLoadVariableInputs = data.getInputTypes().count(Model.VARIABLE)
        except:
            import traceback

            traceback.print_exc()
            try:
                (
                    inputData,
                    _,
                    numInputs,
                    _,
                ) = LocalExecutionModule.readDataFromSimpleFile(sampleFile)
                numSamples = inputData.shape[0]
                numLoadVariableInputs = numInputs
                fromSimpleFile = True
            except:
                QMessageBox.critical(
                    self,
                    "Incorrect format",
                    "File does not have the correct format! Please consult the users manual about the format.",
                )
                return

        # Check compatibility with model.
        # For now, we just check number of inputs ok.
        modelInputTypes = self.model.getInputTypes()
        numModelVariableInputs = modelInputTypes.count(Model.VARIABLE)
        compatible = True
        if fromSimpleFile and numLoadVariableInputs != numModelVariableInputs:
            prompt = (
                "This file does not have the same number of variable inputs (%d) as the setup model (%d)!"
                % (numLoadVariableInputs, numModelVariableInputs))
            compatible = False
        elif not fromSimpleFile:  # Check if input names match
            if numLoadVariableInputs < numModelVariableInputs:
                prompt = (
                    "This file does not have as many variable inputs (%d) as the setup model (%d)!"
                    % (numLoadVariableInputs, numModelVariableInputs))
                compatible = False
            else:
                loadInputNames = data.getInputNames()
                loadInputTypes = data.getInputTypes()
                loadVariableNames = set([
                    loadInputNames[modelIndex]
                    for modelIndex in range(len(loadInputNames))
                    if loadInputTypes[modelIndex] == Model.VARIABLE
                ])
                modelInputNames = self.model.getInputNames()
                modelVariableNames = set([
                    modelInputNames[modelIndex]
                    for modelIndex in range(len(modelInputNames))
                    if modelInputTypes[modelIndex] == Model.VARIABLE
                ])
                if not loadVariableNames <= modelVariableNames:
                    compatible = False
                    prompt = "This file does not have the same variable names as the setup model!"

        if not compatible:
            QMessageBox.warning(self, self.tr("UQ GUI"), self.tr(prompt),
                                QMessageBox.Ok, QMessageBox.Ok)
            return

        # UI indications of success
        self.sampleFileNameEdit.setText(sampleFile)
        self.fileLoaded = True
        self.previewButton.setEnabled(True)
        self.doneButton.setEnabled(True)

        # Create loadData object
        self.loadData = SampleData(self.model)
        self.loadData.setFromFile(False)
        if fromSimpleFile:
            self.loadData.setSampleMethod(SamplingMethods.MC)
        else:
            self.loadData.setSampleMethod(data.getSampleMethod())
        self.loadData.setNumSamples(numSamples)
        resultData = numpy.zeros([numSamples, self.model.getNumInputs()])
        modelDefaults = self.model.getInputDefaults()
        loadIndex = 0
        if fromSimpleFile:
            for modelIndex, modelInType in enumerate(modelInputTypes):
                if modelInType == Model.FIXED:
                    resultData[:, modelIndex] = [modelDefaults[modelIndex]
                                                 ] * numSamples
                else:
                    inDataCol = inputData[:, loadIndex]
                    resultData[:, modelIndex] = inDataCol
                    loadIndex += 1
        else:  # full file
            # Create mapping between model names and load names
            loadDefaults = self.loadData.getInputDefaults()
            for modelIndex, (modelInName, modelInType) in enumerate(
                    zip(modelInputNames, modelInputTypes)):
                if modelInName in loadInputNames:
                    loadIndex = loadInputNames.index(modelInName)
                if modelInType == Model.FIXED:
                    if modelInName in loadInputNames:
                        value = loadDefaults[loadIndex]
                    else:
                        value = modelDefaults[modelIndex]
                    resultData[:, modelIndex] = [value] * numSamples
                else:  # Variable
                    if modelInName not in loadInputNames:
                        resultData[:,
                                   modelIndex] = [modelDefaults[modelIndex]
                                                  ] * numSamples
                    elif loadInputTypes[loadIndex] == Model.FIXED:
                        resultData[:, modelIndex] = [loadDefaults[loadIndex]
                                                     ] * numSamples
                    else:
                        inDataCol = inputData[:, loadIndex]
                        resultData[:, modelIndex] = inDataCol

            self.loadData.setInputDistributions(data.getInputDistributions())

        dists = []
        for modelIndex in range(self.model.getNumInputs()):
            d = Distribution(Distribution.SAMPLE)
            d.setParameterValues(sampleFile, modelIndex + 1)
            dists.append(d)
            self.loadData.setInputDistributions(dists)

        self.loadData.setInputData(resultData)
        self.loadData.setRunState([False] * numSamples)
Exemple #7
0
        # self.setModal(False)
        dialog = Preview(previewData, self)
        # dialog.exec_()
        dialog.show()
        # self.setModal(True)

    ### Return data
    def getData(self):
        if self.loadSamplesRadio.isChecked():
            returnData = self.loadData
        elif self.flowsheetDataRadio.isChecked():
            self.session.flowsheet.results.exportPSUADE("temp/psuadeData2.tmp")
            returnData = LocalExecutionModule.readSampleFromPsuadeFile(
                "temp/psuadeData2.tmp")
        else:
            returnData = self.runData
        return returnData


if __name__ == "__main__":
    app = QApplication(sys.argv)
    fileName = "C:\\Users\\ou3.THE-LAB\\Documents\CCSI\\pt6_optimize\\sim-based_optimize\\trunk\\examples\\UQ\\lptau5k_10inputs_4outputs.filtered"
    #    model = LocalExecutionModule.readSampleFromPsuadeFile(fileName, True)
    model = LocalExecutionModule.readSampleFromPsuadeFile(fileName, False)
    # WHY the missing `session` parameter when instantiating SimSetup seems to be a real error
    # together with the hard-coded `fileName`, this suggests that this portion of the code is not being run
    form = SimSetup(model)  # TODO pylint: disable=no-value-for-parameter
    result = form.exec_()
#    sys.exit(result)
Exemple #8
0
    def ouu(self,
            fname,
            y,
            outputsAsConstraint,
            outputsAsDerivative,
            xtable,
            phi,
            x3sample=None,
            x4sample=None,
            useRS=False,
            useBobyqa=True,
            driver=None,
            optDriver=None,
            auxDriver=None,
            ensOptDriver=None,
            plotSignal=None,
            endFunction=None):

        # Function to execute after inference has finished.
        # Function would enable button again and such things.
        self.endFunction = endFunction

        # read data, assumes data already have fixed variables written to file
        data = LocalExecutionModule.readSampleFromPsuadeFile(fname)
        if optDriver == None and ensOptDriver == None and \
               data.getOptDriverName() == None and \
               data.getEnsembleOptDriverName() == None:
            Common.showError('Model file does not have any drivers set!', \
                             showDeveloperHelpMessage = False)
            self.hadError = True
            if endFunction is not None:
                endFunction()
            return
        if driver != None:
            data.setDriverName(driver)
        if optDriver != None:
            data.setOptDriverName(optDriver)
        if auxDriver != None:
            data.setAuxDriverName(auxDriver)
        if ensOptDriver != None:
            data.setEnsembleOptDriverName(ensOptDriver)
        else:
            ensOptDriver = data.getEnsembleOptDriverName()

        # Remove file that tells OUU to stop
        if os.path.exists(OUU.stopFile):
            os.remove(OUU.stopFile)

        # process input table
        dname = OUU.dname
        deleteFiles = True
        if x3sample is not None:
            deleteFiles = not x3sample['file'].startswith(dname)
        #Common.initFolder(dname, deleteFiles = deleteFiles)
        if platform.system() == 'Windows':
            import win32api
            dname = win32api.GetShortPathName(dname)
        fnameOUU = Common.getLocalFileName(dname, fname, '.ouudat')
        p = RSAnalyzer.parsePrior(data, xtable)
        if p is not None:
            inputLB = p['inputLB']
            inputUB = p['inputUB']
            dist = p['dist']

        init_input = []
        vartypes = []
        for e in xtable:
            t = e['type']
            if t == u'Opt: Primary Continuous (Z1)':
                vartypes.append(1)
            elif t == u'Opt: Primary Continuous (Z1c)':
                vartypes.append(1)
            elif t == u'Opt: Primary Discrete (Z1d)':
                vartypes.append(1)
            elif t == u'Opt: Recourse (Z2)':
                vartypes.append(2)
            elif t == u'UQ: Discrete (Z3)':
                vartypes.append(3)
            elif t == u'UQ: Continuous (Z4)':
                vartypes.append(4)
            if t != u'Fixed':
                init_input.append(e['value'])
        M1 = vartypes.count(1)
        M2 = vartypes.count(2)
        M3 = vartypes.count(3)
        M4 = vartypes.count(4)

        # check arguments
        if M1 < 1:
            error = 'OUU: In function ouu(), number of Z1 (design opt) '
            error = error + 'must be at least 1.'
        if M3 > 0:
            if x3sample == None:
                error = 'OUU: In function ouu(), "x3sample" is undefined.'
                Common.showError(error)
                return None

        if M4 > 0:
            if x4sample == None:
                error = 'OUU: In function ouu(), "x4sample" is undefined.'
                Common.showError(error)
                return None
            loadcs = 'file' in x4sample
            if loadcs:
                N = 0  # number of samples in x4sample['file']

                ### TO DO for Jeremy: check sample size in GUI
                with open(x4sample['file']) as f:
                    header = f.readline()
                    header = header.split()
                    N = int(header[0])

                Nmin = M4 + 1  # minimum number of samples
                if N < Nmin:
                    error = 'OUU: In function ouu(), "x4sample file" requires '
                    error = error + 'at least %d samples.' % Nmin
                    Common.showError(error)
                    return None
                if useRS:
                    Nrs = 'nsamplesRS' in x4sample
                    if not Nrs:
                        error = 'OUU: In function ouu(), "x4sample nsamplesRS" is '
                        error = error + 'required for setting up response surface.'
                        Common.showError(error)
                        return None
                    Nrs = x4sample['nsamplesRS']
                    Nrs = min(max(Nrs, Nmin),
                              N)  ### TO DO for Jeremy: check in GUI

        # TO DO: remove randSeed
        ouuFile = OUU.writeOUUdata(fnameOUU,
                                   y,
                                   outputsAsConstraint,
                                   outputsAsDerivative,
                                   data,
                                   xtable,
                                   randSeed=41491431,
                                   inputLowerBounds=inputLB,
                                   inputUpperBounds=inputUB,
                                   inputPDF=dist,
                                   useEnsOptDriver=(ensOptDriver != None),
                                   init_input=init_input)
        if (ouuFile == None):
            return None

        # write script
        f = OUU.writescript(vartypes,
                            fnameOUU,
                            outputsAsConstraint,
                            phi,
                            x3sample,
                            x4sample,
                            useRS,
                            useBobyqa,
                            useEnsOptDriver=(ensOptDriver != None))

        # delete previous history file
        if os.path.exists(OUU.hfile):
            os.remove(OUU.hfile)

        self.textDialog = Common.textDialog()
        self.thread = psuadeThread(self, f, self.finishOUU, self.textDialog,
                                   plotSignal)
        self.thread.start()