def applyChanges(self): """ Update the node with the settings in the forms """ if self.nodeName not in self.dat.flowsheet.nodes: return # don't apply if node was deleted already gr = self.dat.flowsheet # shorter name for graph gr.nodes[self.nodeName] = self.node self.node.x = float(self.xBox.text()) self.node.y = float(self.yBox.text()) self.node.z = float(self.zBox.text()) self.node.pythonCode = self.pyCode.toPlainText() if self.postRadio.isChecked(): self.node.scriptMode = "post" elif self.preRadio.isChecked(): self.node.scriptMode = "pre" else: self.node.scriptMode = "total" table = self.inputVarTable for row in range(table.rowCount()): name = gh.getCellText(table, row, self.ivCols["Name"]) var = self.node.inVars[name] var.desc = gh.getCellText(table, row, self.ivCols["Description"]) var.unit = gh.getCellText(table, row, self.ivCols["Unit"]) var.setType(gh.getCellText(table, row, self.ivCols["Type"])) var.min = gh.getCellJSON(table, row, self.ivCols["Min"]) var.max = gh.getCellJSON(table, row, self.ivCols["Max"]) var.value = gh.getCellJSON(table, row, self.ivCols["Value"]) var.tags = gh.getCellJSON(table, row, self.ivCols["Tags"]) var.default = gh.getCellJSON(table, row, self.ivCols["Default"]) distName = gh.getCellText(table, row, self.ivCols["Distribution"]) var.dist.setDistributionType(distName) val = gh.getCellJSON(table, row, self.ivCols["Param 1"]) var.dist.firstParamValue = val var.dist.secondParamValue = gh.getCellJSON(table, row, self.ivCols["Param 2"]) table = self.outputVarTable for row in range(self.outputVarTable.rowCount()): name = gh.getCellText(table, row, self.ovCols["Name"]) var = self.node.outVars[name] var.value = gh.getCellJSON(table, row, self.ovCols["Value"]) var.desc = gh.getCellText(table, row, self.ovCols["Description"]) var.unit = gh.getCellText(table, row, self.ovCols["Unit"]) var.setType(gh.getCellText(table, row, self.ovCols["Type"])) var.tags = gh.getCellJSON(table, row, self.ovCols["Tags"]) table = self.simSettingsTable for row in range(table.rowCount()): name = gh.getCellText(table, row, 0) if self.node.options[name].dtype == bool: val = gh.isChecked(table, row, 1) elif len(self.node.options[name].validValues) > 0: val = gh.cellPulldownJSON(table, row, 1) else: val = gh.getCellJSON(table, row, 1) self.node.options[name].value = val self.redrawFlowsheet.emit()
def scaleHelper(self, ind=0): ''' If a descision variable has a none scale type set the scale to lineae. For other variables set the scale type to none and disable the scale selection. ''' table = self.varForm typeCol = self.vtCols["Type"] scaleCol = self.vtCols["Scale"] for row in range(table.rowCount()): vtype = gh.getCellText(table, row, typeCol) stype = gh.getCellText(table, row, scaleCol) if vtype == 'Decision': if stype == 'None': gh.cellPulldownSetText(table,row,scaleCol, "Linear") table.cellWidget(row, scaleCol).setEnabled(True) else: gh.cellPulldownSetText(table, row, scaleCol, "None") table.cellWidget(row, scaleCol).setEnabled(False)
def delOutput(self): ''' Delete selected output variable ''' table = self.outputVarTable if table.currentRow() < 0: return name = gh.getCellText(table, table.currentRow(), self.ivCols["Name"]) self.applyChanges() del (self.node.outVars[name]) self.updateOutputVariables()
def delInput(self): ''' Delete selected variable ''' table = self.inputVarTable if table.currentRow() < 0: return name = gh.getCellText(table, table.currentRow(), self.ivCols["Name"]) self.applyChanges() del self.node.gr.input[self.node.name][name] self.updateInputVariables()
def accept(self): ''' If the okay button is press, use the simulation name and sinter configuration file path from the dialog to attempt to upload simulation files. The Turbine configuration file is a global setting stored in self.dat.turbineConfFile. I'm assuming you will want to use the same gateway for a session ''' simName = self.simNameEdit.currentText() sinterConfigPath = self.files[0][1] if len(self.files) > 2: # First update from table in case user edited resource name for i in range(2, len(self.files)): self.files[i][0] = gh.getCellText(self.fileTable, i, 0) self.files[i][1] = gh.getCellText(self.fileTable, i, 1) other = self.files[2:] else: other = [] tcfgs = [] for i in range(self.tableWidget.rowCount()): tcfgs.append(gh.getCellText(self.tableWidget, i, 1)) for tcfg in tcfgs: self.waiting.emit() try: # try to upload config, simulation, and any extra files self.turb.updateSettings(altConfig=tcfg) self.turb.uploadSimulation(simName, sinterConfigPath, update=True, otherResources=other) except Exception as e: QMessageBox.information(self, "Error", str(e)) self.notwaiting.emit() self.turb.updateSettings() return finally: self.notwaiting.emit() # If uploaded to a Turbine gatway other that the current, # make sure the turbine version is set back to proper value. self.turb.updateSettings() self.done(QDialog.Accepted)
def applyChanges(self, usePrev=False): if usePrev: fltrName = self.prevFilter else: fltrName = self.selectFilterBox.currentText() if fltrName == None or fltrName == "": return r = self.dat.flowsheet.results r.filters[fltrName] = dataFilter() fltr = r.filters[fltrName] if self.sortCheck.isChecked(): fltr.sortTerm = self.sortTermEdit.text() else: fltr.sortTerm = None table = self.ruleTable for row in range(table.rowCount()): term1 = gh.getCellText(table, row, 0) term2 = gh.getCellText(table, row, 2) op = gh.getCellText(table, row, 1) if term1 == "" and term2 == "" and op in self.copList: # operator that operates on rules. op = self.copDict[op] fltr.fstack.append([op]) elif term1 != "" and term2 != "" and op in self.ropList: # rule rule = dataFilterRule() try: rule.term1 = json.loads(term1) except ValueError: rule.term1 = term1 try: rule.term2 = json.loads(term2) except ValueError: rule.term2 = term2 rule.op = self.ropDict[op] fltr.fstack.append([dataFilter.DF_RULE, rule]) else: #something wrong print "message box here" return 1 return 0
def applyChanges(self, tool=None): ''' Save surrogate model setup in foqus session ''' if self.blockapply: return if tool is None: tool = self.toolSelectBox.currentText() pg = self.dat.surrogateMethods.plugins[tool].\ surrogateMethod(self.dat) inputs = [] outputs = [] dataFilter = self.dataBrowser.filterSelectBox.currentText() if dataFilter == '': dataFilter = None # inputs and input var options for i in range(self.inputTable.rowCount()): c = gh.isCellChecked(self.inputTable, i, 0) var = self.dat.flowsheet.input.get(c[1]) var.setMin( gh.getCellJSON(self.inputTable, i, self.inputCols['Min'])) var.setMax( gh.getCellJSON(self.inputTable, i, self.inputCols['Max'])) if c[0]: inputs.append(c[1]) for item in pg.inputCols: col = self.inputCols[item[0]] val = gh.getCellJSON(self.inputTable, i, col) pg.setInputVarOption(item[0], c[1], val) # outputs and output var options for i in range(self.outputTable.rowCount()): c = gh.isCellChecked(self.outputTable, i, 0) if c[0]: outputs.append(c[1]) for item in pg.outputCols: col = self.outputCols[item[0]] val = gh.getCellJSON(self.outputTable, i, col) pg.setOutputVarOption(item[0], c[1], val) pg.input = inputs pg.output = outputs pg.dataFilter = dataFilter for i in range(self.optTable[tool].rowCount()): optName = gh.getCellText(self.optTable[tool], i, 0) if pg.options[optName].dtype == bool: optValue = gh.isChecked(self.optTable[tool], i, 1) elif len(pg.options[optName].validValues) > 0: optValue = gh.cellPulldownJSON(self.optTable[tool], i, 1) else: optValue = gh.getCellJSON(self.optTable[tool], i, 1) pg.options[optName].value = optValue pg.saveInSession() self.dat.surrogateCurrent = pg.name
def delInput(self): ''' Delete selected variable ''' table = self.inputVarTable if table.currentRow() < 0: return name = gh.getCellText(table, table.currentRow(), self.ivCols["Name"]) self.applyChanges() for vname in self.node.gr.input_vectorlist[self.node.name].keys(): if vname in name: for i in range(len(self.node.gr.input_vectorlist[self.node.name][vname].vector)): del self.node.gr.input[self.node.name][vname+'_{0}'.format(i)] del self.node.gr.input_vectorlist[self.node.name][vname] break else: continue if name in self.node.gr.input[self.node.name].keys(): del self.node.gr.input[self.node.name][name] self.updateInputVariables()
def delOutput(self): """ Delete selected output variable """ table = self.outputVarTable if table.currentRow() < 0: return name = gh.getCellText(table, table.currentRow(), self.ivCols["Name"]) self.applyChanges() for vname in self.node.gr.output_vectorlist[self.node.name].keys(): if vname in name: for i in range( len(self.node.gr.output_vectorlist[self.node.name] [vname].vector)): del self.node.gr.output[self.node.name][vname + "_{0}".format(i)] del self.node.gr.output_vectorlist[self.node.name][vname] break else: continue if name in self.node.gr.output[self.node.name].keys(): del self.node.gr.output[self.node.name][name] # del(self.node.outVars[name]) self.updateOutputVariables()
def selectAllOutputs(self): table = self.outputTable for row in range(table.rowCount()): if gh.getCellText(table, row, 0) != 'graph.error': gh.setCellChecked(table, row, 0, check=True)
def refreshContents(self): ''' Update the contents of the surrogate for to reflect the current state of the foqus session. ''' self.ivGeneralButton1.hide() self.ivGeneralButton2.hide() self.ovGeneralButton1.hide() self.ovGeneralButton2.hide() self.dataBrowser.refreshContents() inputNames = self.dat.flowsheet.input.compoundNames() self.dat.flowsheet.markConnectedInputs() delList = [] for i in range(len(inputNames)): if self.dat.flowsheet.input.get(inputNames[i]).con == True: delList.append(inputNames[i]) for n in delList: inputNames.remove(n) outputNames = self.dat.flowsheet.output.compoundNames() if self.dat.surrogateCurrent is not None: i = self.toolSelectBox.findText(self.dat.surrogateCurrent) if i >= 0: self.blockapply = True self.toolSelectBox.setCurrentIndex(i) self.blockapply = False tool = self.toolSelectBox.currentText() if tool == '': return pg = self.dat.surrogateMethods.plugins[tool].\ surrogateMethod(self.dat) pg.loadFromSession() pg.updateOptions() for i, btn in enumerate(pg.inputVarButtons): # this is because I am beeing lazy there are two # preexisting buttons if i == 0: self.ivGeneralButton1.setText(btn[0]) self.ivGeneralButton1.show() elif i == 1: self.ivGeneralButton2.setText(btn[0]) self.ivGeneralButton2.show() for i, btn in enumerate(pg.outputVarButtons): if i == 0: self.ovGeneralButton1.setText(btn[0]) self.ovGeneralButton1.show() elif i == 1: self.ovGeneralButton2.setText(btn[0]) self.ovGeneralButton2.show() for i in range(self.optTable[tool].rowCount()): oname = gh.getCellText(self.optTable[tool], i, 0) if oname in pg.options: if pg.options[oname].dtype == bool: gh.setCellChecked(self.optTable[tool], i, 1, pg.options[oname].value) elif len(pg.options[oname].validValues) > 0: gh.cellPulldownSetItemsJSON(self.optTable[tool], i, 1, pg.options[oname].validValues) gh.cellPulldownSetJSON(self.optTable[tool], i, 1, pg.options[oname].value) else: gh.setCellJSON(self.optTable[tool], i, 1, pg.options[oname].value) self.inputTable.setRowCount(0) self.inputTable.setColumnCount(3 + len(pg.inputCols)) for i, item in enumerate(pg.inputCols): gh.setColHeaderIntem(self.inputTable, i + 3, item[0]) self.outputTable.setRowCount(0) self.outputTable.setColumnCount(1 + len(pg.outputCols)) for i, item in enumerate(pg.outputCols): gh.setColHeaderIntem(self.outputTable, i + 1, item[0]) self.inputTable.setRowCount(len(inputNames)) self.outputTable.setRowCount(len(outputNames)) self.inputCols = gh.colIndexes(self.inputTable) self.outputCols = gh.colIndexes(self.outputTable) for i in range(len(inputNames)): var = self.dat.flowsheet.input.get(inputNames[i]) gh.setTableItem(self.inputTable, row=i, col=self.inputCols['Name'], text=inputNames[i], check=inputNames[i] in pg.input, editable=False) gh.setTableItem(self.inputTable, row=i, col=self.inputCols['Max'], text=var.max, jsonEnc=True) gh.setTableItem(self.inputTable, row=i, col=self.inputCols['Min'], text=var.min, jsonEnc=True) for item in pg.inputCols: val = pg.getInputVarOption(item[0], inputNames[i]) gh.setTableItem(self.inputTable, row=i, col=self.inputCols[item[0]], text=val, jsonEnc=True) for i in range(len(outputNames)): var = self.dat.flowsheet.output.get(outputNames[i]) gh.setTableItem(self.outputTable, row=i, col=self.outputCols['Name'], text=outputNames[i], check=outputNames[i] in pg.output, editable=False) for item in pg.outputCols: val = pg.getOutputVarOption(item[0], outputNames[i]) gh.setTableItem(self.outputTable, row=i, col=self.outputCols[item[0]], text=val, jsonEnc=True) self.inputTable.resizeColumnsToContents() self.outputTable.resizeColumnsToContents()
def _save(self, file="test.json"): d = {} d["application"] = {} d["application"]["name"] = self.application_name.currentText() d["application"]["version"] = self.application_version.text() d["application"][ "constraint"] = self.application_constraint.currentText() d["filetype"] = self.filetype.currentText() d["filetype-version"] = float(self.filetype_version.currentText()) d["title"] = self.title.text() d["author"] = self.author.text() d["date"] = self.date.text() d["description"] = self.description.text() d["config-version"] = self.config_version.text() d["model"] = {} d["model"]["file"] = self.model_file.text() d["model"]["DigestValue"] = self.model_digest_value.text() d["input-files"] = [] tbl = self.input_files_table for row in range(tbl.rowCount()): d2 = {} d2["file"] = gh.getCellText(tbl, row, 0) d2["DigestValue"] = gh.getCellText(tbl, row, 1) d2["SignatureMethodAlgorithm"] = gh.getCellText(tbl, row, 2) d["input-files"].append(d2) d["settings"] = {} tbl = self.settings_table for row in range(tbl.rowCount()): sname = tbl.verticalHeaderItem(row).text() d["settings"][sname] = {} d["settings"][sname]["type"] = gh.getCellText(tbl, row, 0) d["settings"][sname]["default"] = gh.getCellText(tbl, row, 1) d["settings"][sname]["description"] = gh.getCellText(tbl, row, 2) d["inputs"] = {} tbl = self.inputs_table for row in range(tbl.rowCount()): name = gh.getCellText(tbl, row, 0) d["inputs"][name] = {} d["inputs"][name]["path"] = [gh.getCellText(tbl, row, 1)] d["inputs"][name]["type"] = gh.getCellText(tbl, row, 2) d["inputs"][name]["default"] = gh.getCellText(tbl, row, 3) d["inputs"][name]["max"] = gh.getCellText(tbl, row, 4) d["inputs"][name]["min"] = gh.getCellText(tbl, row, 5) d["inputs"][name]["units"] = gh.getCellText(tbl, row, 6) d["inputs"][name]["description"] = gh.getCellText(tbl, row, 7) if d["inputs"][name]["units"] == "": d["inputs"][name]["units"] = None if d["inputs"][name]["type"] in ["int", "double"]: if d["inputs"][name]["default"] == "": d["inputs"][name]["default"] = 0 if d["inputs"][name]["min"] == "": d["inputs"][name]["min"] = 0 if d["inputs"][name]["max"] == "": d["inputs"][name]["max"] = 0 if d["inputs"][name]["type"] == "int": try: d["inputs"][name]["default"] = int( d["inputs"][name]["default"]) d["inputs"][name]["min"] = int(d["inputs"][name]["min"]) d["inputs"][name]["max"] = int(d["inputs"][name]["max"]) except: _log.exception("Error converting to int") elif d["inputs"][name]["type"] == "double": try: d["inputs"][name]["default"] = float( d["inputs"][name]["default"]) d["inputs"][name]["min"] = float(d["inputs"][name]["min"]) d["inputs"][name]["max"] = float(d["inputs"][name]["max"]) except: _log.exception("Error converting to double") elif d["inputs"][name]["type"] == "string": del d["inputs"][name]["min"] del d["inputs"][name]["max"] d["outputs"] = {} tbl = self.outputs_table for row in range(tbl.rowCount()): name = gh.getCellText(tbl, row, 0) d["outputs"][name] = {} d["outputs"][name]["path"] = [gh.getCellText(tbl, row, 1)] d["outputs"][name]["type"] = gh.getCellText(tbl, row, 2) d["outputs"][name]["default"] = gh.getCellText(tbl, row, 3) d["outputs"][name]["units"] = gh.getCellText(tbl, row, 4) d["outputs"][name]["description"] = gh.getCellText(tbl, row, 5) if d["outputs"][name]["units"] == "": d["outputs"][name]["units"] = None if d["outputs"][name]["type"] in ["int", "double"]: if d["outputs"][name]["default"] == "": d["outputs"][name]["default"] = 0 if d["outputs"][name]["type"] == "int": try: d["outputs"][name]["default"] = int( d["outputs"][name]["default"]) except: _log.exception("Error converting to int") elif d["outputs"][name]["type"] == "double": try: d["outputs"][name]["default"] = float( d["outputs"][name]["default"]) except: _log.exception("Error converting to double") with open(file, "w") as fp: json.dump(d, fp, indent=2)
def applyChanges(self): ''' Use information stored in this form to update the optimization options ''' # # Delete optimization settings and rebuild them from the form if self.running: return prob = self.dat.optProblem gr = self.dat.flowsheet prob.solver = self.solverBox.currentText() # Get solver options s = self.lastSolver prob.solverOptions[s] = {} opts = self.methods[s].opt().options for row in range(self.solverOptionTable.rowCount()): settingName = self.solverOptionTable.item(row, 0).text() if opts[settingName].dtype == bool: setting = gh.isChecked(self.solverOptionTable, row, 1) elif len(opts[settingName].validValues)>0: setting = \ gh.cellPulldownJSON(self.solverOptionTable,row,1) else: setting = gh.getCellJSON(self.solverOptionTable, row, 1) prob.solverOptions[s][settingName] = setting # Get objective functions prob.obj = [] table = self.fTable for row in range(self.fTable.rowCount()): pc = gh.getCellText(table, row, self.ofCols["Expression"]) ps = gh.getCellJSON(table, row, self.ofCols["Penalty Scale"]) fv = gh.getCellJSON( table, row, self.ofCols["Value for Failure"]) prob.obj.append( optimObj(pc, ps, fv) ) # Get constraints prob.g = [] table = self.gTable for row in range(self.gTable.rowCount()): pc = gh.getCellText(table, row, self.icCols["Expression"]) ps = gh.getCellJSON( table, row, self.icCols["Penalty Factor"]) pf = gh.getCellText(table, row, self.icCols["Form"] ) prob.g.append( optimInEq(pc, ps, pf) ) # Get decision variables prob.v = [] prob.vs = [] table = self.varForm for row in range(self.varForm.rowCount()): name = gh.getCellText(table, row, self.vtCols["Variable"]) gr.x[name].scaling = gh.getCellText( table, row, self.vtCols["Scale"]) gr.x[name].min = gh.getCellJSON( table, row, self.vtCols["Min"]) gr.x[name].max = gh.getCellJSON( table, row, self.vtCols["Max"]) gr.x[name].value = gh.getCellJSON( table, row, self.vtCols["Value"]) vt = gh.cellPulldownValue(table, row, self.vtCols["Type"]) if vt == "Decision": prob.v.append(name) gr.x[name].optVar=True elif vt == "Sample": prob.vs.append(name) else: gr.x[name].optVar=False # Get sample information table = self.sampleTable ci = gh.colIndexes(table) n = prob.numSamples() for row in range(table.rowCount()): if row < n: #re-set sample data for name in ci: if name in prob.samp: prob.samp[name][row] = \ gh.getCellJSON(table, row, ci[name]) else: prob.addSampleVar(name) prob.samp[name][row] = \ gh.getCellJSON(table, row, ci[name]) else: s = ci.copy() for name in s: s[name] = gh.getCellJSON(table, row, ci[name]) prob.addSample(s) # Get objective type if self.objTypeCombo.currentIndex() == 0: prob.objtype = prob.OBJ_TYPE_EVAL elif self.objTypeCombo.currentIndex() == 1: prob.objtype = prob.OBJ_TYPE_CUST # Get custom code prob.custpy = self.customCodeEdit.toPlainText() self.updateSampleVarsTable()