def dataImputation(fname, y, rsMethodName, eval_fname): rsIndex = ResponseSurfaces.getEnumValue(rsMethodName) # write script f = tempfile.SpooledTemporaryFile(mode="wt") if platform.system() == 'Windows': import win32api fname = win32api.GetShortPathName(fname) f.write('load %s\n' % fname) # load data cmd = 'rscreate' f.write('%s\n' % cmd) f.write('%d\n' % y) # select output f.write('%d\n' % rsIndex) # select response surface cmd = 'rseval' f.write('%s\n' % cmd) f.write('y\n') # data taken from register f.write('%s\n' % eval_fname) f.write('y\n') # do fuzzy evaluation f.write('y\n') # write data to file f.write('quit\n') f.seek(0) # invoke psuade out, error = Common.invokePsuade(f) f.close() if error: return None outfile = 'eval_sample' assert(os.path.exists(outfile)) return outfile
def rseval(rsdata, pdata, cdata, rstypes): # rsdata: SampleData containing the RS training data # pdata: SampleData containing the sample representing the prior over uncertain variables # cdata: SampleData containing the candidate set # rstypes: dictionary with output index as key and string denote RS types as value; possible values: ['MARS', # 'linear', 'quadratic', 'cubic'] # convert the data into psuade files cfile = os.path.join(dname, 'CandidateSet') pfile = os.path.join(dname, 'PriorSample') rsfile = os.path.join(dname, 'RSTrainData') cfile = writeSample(cfile, cdata) pfile = writeSample(pfile, pdata) y = 1 rsfile = RSAnalyzer.writeRSdata(rsfile, y, rsdata) cmd = 'odoeu_rseval' inputNames = rsdata.getInputNames() priorNames = pdata.getInputNames() priorIndices = [] for name in priorNames: priorIndices.append(inputNames.index(name) + 1) rs_list = ['MARS', 'linear', 'quadratic', 'cubic', 'GP3'] # extract the indices of RS types for outputs rs_idx = [ResponseSurfaces.getEnumValue(s) for s in rs_list] rsdict = dict(zip(rs_list, rs_idx)) # write script f = tempfile.SpooledTemporaryFile(mode='wt') if platform.system() == 'Windows': import win32api cfile = win32api.GetShortPathName(cfile) pfile = win32api.GetShortPathName(pfile) rsfile = win32api.GetShortPathName(rsfile) f.write('load %s\n' % rsfile) f.write('%s\n' % cmd) f.write('y\n') for i in priorIndices: f.write( '%d\n' % i ) # specify random variables, should be consistent with vars in prior f.write('0\n') # 0 to proceed f.write('%s\n' % pfile) # file containing the prior sample (psuade sample format) f.write('%s\n' % cfile) # file containing the candidate set (psuade sample format) for rs in rstypes: # for each output, specify RS index f.write('%d\n' % rsdict[rstypes[rs]]) f.write('quit\n') f.seek(0) # invoke psuade out, error = Common.invokePsuade(f) if error: return None outfile = 'odoeu_rseval.out' assert (os.path.exists(outfile)) return outfile
def __init__(self, info, parent=None): super(AnalysisInfoDialog, self).__init__(parent) self.setWindowTitle('Analysis Additional Info') self.resize(400, 400) self.gridLayout = QGridLayout(self) self.table = QTableWidget() self.table.setRowCount(len(list(info.keys()))) self.table.setColumnCount(1) boldFont = QFont() boldFont.setWeight(75) boldFont.setBold(True) totalHeight = 20 totalWidth = 50 row = 0 for key in list(info.keys()): if key in ('xprior', 'xtable', 'ytable', 'obsTable'): continue #item = QTableWidgetItem(key) #self.table.setItem(row, 0, item) if isinstance(info[key], (list, tuple)): strings = [] for strItem in info[key]: if isinstance(strItem, dict): ks = list(strItem.keys()) for k in ks: if strItem[k] is None: del strItem[k] strings.append(str(strItem)) item = QTableWidgetItem('\n'.join(strings)) else: item = QTableWidgetItem(str(info[key])) self.table.setItem(row, 0, item) row += 1 if row > 0: label = QLabel('Additional Info') label.setFont(boldFont) self.gridLayout.addWidget(label) self.gridLayout.addWidget(self.table) self.table.horizontalHeader().setHidden(True) self.table.setRowCount(row) self.table.setVerticalHeaderLabels([key for key in list(info.keys()) if key not in ('xprior', 'xtable', 'ytable', 'obsTable')]) #self.table.verticalHeader().setHidden(True) self.table.setWordWrap(True) self.table.resizeColumnsToContents() self.table.resizeRowsToContents() self.show() # Needed for headers to be correctly sized width = 2 + self.table.verticalHeader().width() + self.table.columnWidth(0) if self.table.verticalScrollBar().isVisible(): width += self.table.verticalScrollBar().width() self.table.setMinimumWidth(width) maxHeight = 3 for i in range(row): maxHeight += self.table.rowHeight(i) self.table.setMinimumHeight(maxHeight) totalHeight += maxHeight + 20 if width + 20 > totalWidth: totalWidth = width + 20 # Show table for key in info: if key in ('xprior', 'xtable', 'ytable', 'obsTable'): #self.resize(800, 400) values = info[key] for d in values: if d is not None and 'type' in d and d['type'] == 'Design': keys = list(d.keys()) for k in keys: if k not in ('name', 'type'): del d[k] if key == 'ytable': labelString = 'Outputs' elif key == 'obsTable': labelString = 'Experiments' else: labelString = 'Inputs' label = QLabel(labelString) label.setFont(boldFont) self.gridLayout.addWidget(label) table = QTableWidget() self.gridLayout.addWidget(table) table.setRowCount(len([1 for i in range(len(values)) if values[i] is not None])) # column headers if key == 'obsTable': inputInfo = info['xtable'] designVars = [] for d in inputInfo: if 'type' in d and d['type'] == 'Design': designVars.append(d['name']) columnHeaders = [name + ' Value' for name in designVars] outputInfo = info['ytable'] outputVars = [d['name'] for d in outputInfo if d is not None] for out in outputVars: columnHeaders.append(out + ' Mean') columnHeaders.append(out + ' Std Dev') table.setColumnCount(len(columnHeaders)) table.setHorizontalHeaderLabels(columnHeaders) table.setVerticalHeaderLabels(['Experiment ' + str(num) for num in range(1, len(values) + 1)]) else: columnSet = set() if key == 'ytable': valuesKeys = ['name', 'rsIndex', 'legendreOrder', 'marsBases', 'marsInteractions', 'userRegressionFile'] columnHeaders = ['Output Name', 'RS Type', 'Legendre Order', 'MARS # Basis Functions', 'MARS Deg. of Interaction', 'User Regression File'] else: #xprior, xtable valuesKeys = ['name', 'type', 'value', 'pdf', 'param1', 'param2', 'min', 'max'] columnHeaders = ['Input Name', 'Type', 'Fixed Value', 'PDF', 'PDF Param 1', 'PDF Param 2', 'Min', 'Max'] for d in values: if d is not None: d2 = copy.deepcopy(d) for key in d2: if d2[key] is None: del d[key] columnSet = columnSet.union(set(d.keys())) table.setColumnCount(len(columnSet)) usedColumns = [] usedHeaders = [] columnIndices = {} for i, (valuesKey, header) in enumerate(zip(valuesKeys, columnHeaders)): if valuesKey in columnSet: usedColumns.append(valuesKey) usedHeaders.append(header) columnIndices[valuesKey] = i table.setHorizontalHeaderLabels(usedHeaders) table.verticalHeader().setHidden(True) table.setWordWrap(True) r = 0 for i in range(len(values)): if key == 'obsTable': for c, string in enumerate(values[r][1:]): item = QTableWidgetItem(string) table.setItem(r, c, item) r += 1 elif values[r] is not None: for c, colName in enumerate(usedColumns): if colName in values[r]: if colName == 'pdf': string = Distribution.getFullName(values[r][colName]) elif colName == 'rsIndex': string = ResponseSurfaces.getFullName(values[r][colName]) else: string = str(values[r][colName]) item = QTableWidgetItem(string) table.setItem(r, c, item) r += 1 table.resizeColumnsToContents() QtCore.QCoreApplication.processEvents() width = 2 if key == 'obsTable': width += table.verticalHeader().width() for i in range(table.columnCount()): width += table.columnWidth(i) if table.verticalScrollBar().isVisible(): width += table.verticalScrollBar().width() if width > 800: width = 800 table.setMinimumWidth(width) maxHeight = 3 + table.horizontalHeader().height() for i in range(table.rowCount()): maxHeight += table.rowHeight(i) maxHeight = min([maxHeight, 400]) table.setMinimumHeight(maxHeight) totalHeight += maxHeight + 20 if width + 20 > totalWidth: totalWidth = width + 20 self.okButton = QPushButton(self) self.okButton.setText('OK') self.okButton.clicked.connect(self.close) self.gridLayout.addWidget(self.okButton) self.show() self.resize(totalWidth, totalHeight)
def odoeu(cdata, cfile, pdata, rsdata, rstypes, opt, nd, max_iters=100, edata=None): # cdata: SampleData containing the original candidate set # cfile: PSUADE sample file containing the original candidates with the mean/std of the selected output # cdata: SampleData containing the candidate set # pdata: SampleData containing the sample representing the prior over uncertain variables # rsdata: SampleData containing the RS training data # rstypes: dictionary with output index as key and string denote RS types as value; possible values: ['MARS', # 'linear', 'quadratic', 'cubic'] # nd: int denoting design size # max_iters: int denoting maximum number of iterations for the optimization routine [default: 100] # edata: SampleData containing the evaluation set # parse params opts = ['G', 'I', 'D', 'A'] assert (opt in opts) optdict = dict(zip(opts, range(1, len(opts) + 1))) opt_index = optdict[opt] cmd = 'odoeu_optns' # TO DO for Pedro: check in GUI? # maximum iterations should be in range [100, 1000] assert (99 < max_iters < 1001) # initialize constants ncand = cdata.getNumSamples() nOutputs = rsdata.getNumOutputs() # extract the indices of random variables inputNames = rsdata.getInputNames() priorNames = pdata.getInputNames() priorIndices = [] for name in priorNames: priorIndices.append(inputNames.index(name) + 1) rs_list = ['MARS', 'linear', 'quadratic', 'cubic', 'GP3'] # TO DO for Pedro: check in GUI? # this checks to see if user is trying to pass an unsupported RS for rs in rstypes: assert (rstypes[rs] in rs_list) assert (len(rstypes) == nOutputs) # extract the indices of RS types for outputs rs_idx = [ResponseSurfaces.getEnumValue(s) for s in rs_list] rsdict = dict(zip(rs_list, rs_idx)) # convert the data into psuade files pfile = os.path.join(dname, 'PriorSample') rsfile = os.path.join(dname, 'RSTrainData') pfile = writeSample(pfile, pdata) efile = os.path.join(dname, 'EvaluationSet') if edata is None: efile = writeSample(efile, cdata) else: efile = writeSample(efile, edata) y = 1 rsfile = RSAnalyzer.writeRSdata(rsfile, y, rsdata) # write script f = tempfile.SpooledTemporaryFile(mode='wt') if platform.system() == 'Windows': import win32api pfile = win32api.GetShortPathName(pfile) rsfile = win32api.GetShortPathName(rsfile) efile = win32api.GetShortPathName(efile) f.write('%s\n' % cmd) f.write('y\n') f.write('%d\n' % opt_index) # choose G, I, D, A f.write('%d\n' % ncand) # size of the candidate set f.write('%d\n' % nd) # design size f.write( '%d\n' % max_iters) # max number of iterations, must be greater or equal to 100 f.write('n\n') # no initial guess f.write('%s\n' % rsfile) # file containing RS training data (psuade format) for i in priorIndices: f.write( '%d\n' % i ) # specify random variables, should be consistent with vars in prior f.write('0\n') # 0 to proceed f.write('%s\n' % pfile) # file containing the prior sample (psuade sample format) f.write('%s\n' % cfile) # file containing the candidate set (psuade sample format) f.write('%s\n' % efile ) # ... evaluate the optimality values on the (same) candidate set for rs in rstypes: # for each output, specify RS index f.write('%d\n' % rsdict[rstypes[rs]]) # TO DO: as we add more RS, may need to port more code from RSAnalyzer.py to handle different RS types f.write('quit\n') f.seek(0) # invoke psuade out, error = Common.invokePsuade(f) if error: return None # parse output best_indices = None best_optval = None m = re.findall(cmd + r' best selection = (\d+ \d+)', out) if m: best_indices = [i for i in m[0].split()] s = r'\s*'.join([i for i in best_indices]) best_optvals = re.findall(s + r'\s*===> output = (\S*)', out) best_indices = [int(i) for i in best_indices] best_optval = float(best_optvals[0]) return best_indices, best_optval
def __init__(self, info, parent=None): super(AnalysisInfoDialog, self).__init__(parent) self.setWindowTitle("Analysis Additional Info") self.resize(400, 400) self.gridLayout = QGridLayout(self) self.table = QTableWidget() self.table.setRowCount(len(list(info.keys()))) self.table.setColumnCount(1) boldFont = QFont() boldFont.setWeight(75) boldFont.setBold(True) totalHeight = 20 totalWidth = 50 row = 0 for key in list(info.keys()): if key in ("xprior", "xtable", "ytable", "obsTable"): continue # item = QTableWidgetItem(key) # self.table.setItem(row, 0, item) if isinstance(info[key], (list, tuple)): strings = [] for strItem in info[key]: if isinstance(strItem, dict): ks = list(strItem.keys()) for k in ks: if strItem[k] is None: del strItem[k] strings.append(str(strItem)) item = QTableWidgetItem("\n".join(strings)) else: item = QTableWidgetItem(str(info[key])) self.table.setItem(row, 0, item) row += 1 if row > 0: label = QLabel("Additional Info") label.setFont(boldFont) self.gridLayout.addWidget(label) self.gridLayout.addWidget(self.table) self.table.horizontalHeader().setHidden(True) self.table.setRowCount(row) self.table.setVerticalHeaderLabels( [ key for key in list(info.keys()) if key not in ("xprior", "xtable", "ytable", "obsTable") ] ) # self.table.verticalHeader().setHidden(True) self.table.setWordWrap(True) self.table.resizeColumnsToContents() self.table.resizeRowsToContents() self.show() # Needed for headers to be correctly sized width = 2 + self.table.verticalHeader().width() + self.table.columnWidth(0) if self.table.verticalScrollBar().isVisible(): width += self.table.verticalScrollBar().width() self.table.setMinimumWidth(width) maxHeight = 3 for i in range(row): maxHeight += self.table.rowHeight(i) self.table.setMinimumHeight(maxHeight) totalHeight += maxHeight + 20 if width + 20 > totalWidth: totalWidth = width + 20 # Show table for key in info: if key in ("xprior", "xtable", "ytable", "obsTable"): # self.resize(800, 400) values = info[key] for d in values: if d is not None and "type" in d and d["type"] == "Design": keys = list(d.keys()) for k in keys: if k not in ("name", "type"): del d[k] if key == "ytable": labelString = "Outputs" elif key == "obsTable": labelString = "Experiments" else: labelString = "Inputs" label = QLabel(labelString) label.setFont(boldFont) self.gridLayout.addWidget(label) table = QTableWidget() self.gridLayout.addWidget(table) table.setRowCount( len([1 for i in range(len(values)) if values[i] is not None]) ) # column headers if key == "obsTable": inputInfo = info["xtable"] designVars = [] for d in inputInfo: if "type" in d and d["type"] == "Design": designVars.append(d["name"]) columnHeaders = [name + " Value" for name in designVars] outputInfo = info["ytable"] outputVars = [d["name"] for d in outputInfo if d is not None] for out in outputVars: columnHeaders.append(out + " Mean") columnHeaders.append(out + " Std Dev") table.setColumnCount(len(columnHeaders)) table.setHorizontalHeaderLabels(columnHeaders) table.setVerticalHeaderLabels( ["Experiment " + str(num) for num in range(1, len(values) + 1)] ) else: columnSet = set() if key == "ytable": valuesKeys = [ "name", "rsIndex", "legendreOrder", "marsBases", "marsInteractions", "userRegressionFile", ] columnHeaders = [ "Output Name", "RS Type", "Legendre Order", "MARS # Basis Functions", "MARS Deg. of Interaction", "User Regression File", ] else: # xprior, xtable valuesKeys = [ "name", "type", "value", "pdf", "param1", "param2", "min", "max", ] columnHeaders = [ "Input Name", "Type", "Fixed Value", "PDF", "PDF Param 1", "PDF Param 2", "Min", "Max", ] for d in values: if d is not None: d2 = copy.deepcopy(d) for key in d2: if d2[key] is None: del d[key] columnSet = columnSet.union(set(d.keys())) table.setColumnCount(len(columnSet)) usedColumns = [] usedHeaders = [] columnIndices = {} for i, (valuesKey, header) in enumerate( zip(valuesKeys, columnHeaders) ): if valuesKey in columnSet: usedColumns.append(valuesKey) usedHeaders.append(header) columnIndices[valuesKey] = i table.setHorizontalHeaderLabels(usedHeaders) table.verticalHeader().setHidden(True) table.setWordWrap(True) r = 0 for i in range(len(values)): if key == "obsTable": for c, string in enumerate(values[r][1:]): item = QTableWidgetItem(string) table.setItem(r, c, item) r += 1 elif values[r] is not None: for c, colName in enumerate(usedColumns): if colName in values[r]: if colName == "pdf": string = Distribution.getFullName( values[r][colName] ) elif colName == "rsIndex": string = ResponseSurfaces.getFullName( values[r][colName] ) else: string = str(values[r][colName]) item = QTableWidgetItem(string) table.setItem(r, c, item) r += 1 table.resizeColumnsToContents() QtCore.QCoreApplication.processEvents() width = 2 if key == "obsTable": width += table.verticalHeader().width() for i in range(table.columnCount()): width += table.columnWidth(i) if table.verticalScrollBar().isVisible(): width += table.verticalScrollBar().width() if width > 800: width = 800 table.setMinimumWidth(width) maxHeight = 3 + table.horizontalHeader().height() for i in range(table.rowCount()): maxHeight += table.rowHeight(i) maxHeight = min([maxHeight, 400]) table.setMinimumHeight(maxHeight) totalHeight += maxHeight + 20 if width + 20 > totalWidth: totalWidth = width + 20 self.okButton = QPushButton(self) self.okButton.setText("OK") self.okButton.clicked.connect(self.close) self.gridLayout.addWidget(self.okButton) self.show() self.resize(totalWidth, totalHeight)