示例#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
示例#2
0
文件: SimSetup.py 项目: pn51/FOQUS
    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)
示例#3
0
文件: SimSetup.py 项目: pn51/FOQUS
        # 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)
示例#4
0
文件: OUU.py 项目: elbashandy/FOQUS
    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()