def run(self): ''' This function overloads the Thread class function, and is called when you run start() to start a new thread. ''' try: #Get options and show some information about settings adir = self.acossoDir self.setupWorkingDir() rscriptExe = self.dat.foqusSettings.rScriptPath xdata = self.options["Input Data File"].value ydata = self.options["Output Data File"].value cv = self.options["CV"].value order = self.options["Order"].value modelFile = self.options["Model File"].value driverFile = self.options["FOQUS Model (for UQ)"].value if self.checkNumVars(): return self.msgQueue.put("------------------------------------") self.msgQueue.put("Starting ACOSSO\n") self.msgQueue.put("Model File: " + str(modelFile)) self.msgQueue.put("Py File (UQ): " + str(driverFile)) self.msgQueue.put("RScript Path: " + str(rscriptExe)) self.msgQueue.put("Sub-directory: " + str(adir)) self.msgQueue.put("X data file: " + str(xdata)) self.msgQueue.put("Y data file: " + str(ydata)) self.msgQueue.put("Order: " + str(order)) self.msgQueue.put("CV: " + str(cv)) self.msgQueue.put("------------------------------------") # Run the R script if self.options["Use Flowsheet Data"].value == "Yes": self.msgQueue.put("Exporting Data...") if len(self.input) < 1: self.msgQueue.put( " Must select at least 2 input variables") return self.msgQueue.put(" Inputs: {0}".format( json.dumps(self.input))) self.dat.flowsheet.results.exportVarsCSV( xdata, inputs = self.input, outputs = [], flat = True) if len(self.output) < 1: self.msgQueue.put( " Must select an output variable") return self.msgQueue.put(" Output: {0}".format( json.dumps(self.output))) self.dat.flowsheet.results.exportVarsCSV( ydata, inputs = [], outputs = self.output, flat = True) self.msgQueue.put("Running ACOSSO...") rscriptFile = os.path.basename(rscriptExe) process = subprocess.Popen([ rscriptFile, 'acosso_fit.R', os.path.abspath(xdata), os.path.abspath(ydata), modelFile, str(order), cv], #need to add categorical arg executable=rscriptExe, cwd=adir, stdout=subprocess.PIPE, stderr=subprocess.PIPE) line = process.stdout.readline() while process.poll() == None or line != b'': if line == b'': time.sleep(0.2) if line != b'': self.msgQueue.put(line.decode("utf-8").rstrip()) line = process.stdout.readline() if self.stop.isSet(): self.msgQueue.put("**terminated by user**") process.kill() break self.msgQueue.put( "Process completed code: {0}".format(process.poll())) line = process.stderr.readline() while line != b'': self.msgQueue.put(line.decode("utf-8").rstrip()) line = process.stderr.readline() modelFile2 = os.path.join(adir, modelFile) driverFile2 = os.path.join(adir, driverFile) rfile = os.path.join(adir, 'acosso_pred.R') acossoData = { 'outputNames': self.output, # assume univariate 'modelNames': [modelFile2], 'rscriptPath': rscriptExe, 'rfile': rfile} SurrogateParser.writeAcossoDriver(acossoData, driverFile2) self.msgQueue.put( "Wrote Python driver file: {0}".format(driverFile2)) self.result = "Done, see Python driver file: {0}".format(driverFile2) self.driverFile = driverFile2 self.writePlugin() # added by BN, 2/4/2016 except Exception: self.ex = sys.exc_info() logging.getLogger("foqus." + __name__).exception( "Exception in ACOSSO Thread")
def run(self): ''' This function overloads the Thread class function, and is called when you run start() to start a new thread. ''' alamoExe = self.dat.foqusSettings.alamo_path try: assert(os.path.isfile(alamoExe)) except AssertionError: msg = "The ALAMO executable was not found. Check the ALAMO exec "\ "path in settings and that ALAMO is installed." self.msgQueue.put(msg) raise AssertionError(msg) try: #Setup dictionaries to convert between foqus and ALAMO #variable names. Also setup a list of input and output #variable names for ALAMO. I'm going to keep the dicts #for now in case using the FOQUS variables names in ALAMO #doesn't work out for some reason. uq_file = self.options[ "FOQUS Model (for UQ)"].value self.xList = self.input self.zList = self.output self.xi = {} self.zi = {} cn = self.graph.input.compoundNames(sort=True) for v in self.xList: self.xi[v] = cn.index(v) cn = self.graph.output.compoundNames(sort=False) for v in self.zList: self.zi[v] = cn.index(v) #Get options and show some information about settings adaptive = self.options['SAMPLER'].value alamoDir = self.alamoDir alamoDirFull = os.path.abspath(alamoDir) self.setupWorkingDir() adpexe = os.path.join(alamoDirFull, 'foqusALAMOClient.py') self.writeAlamoInputFile(adaptiveExe=adpexe) if self.checkNumVars(): return alamoInput = self.options["Input File"].value alamoOutput = alamoInput.rsplit('.', 1)[0] + '.lst' self.msgQueue.put("------------------------------------") self.msgQueue.put("Starting ALAMO\n") self.msgQueue.put("Exec File Path: " + alamoExe) self.msgQueue.put("Sub-directory: " + alamoDir) self.msgQueue.put("Input File Name: " + alamoInput) self.msgQueue.put("Output File Name: " + alamoOutput) self.msgQueue.put("SAMPLER: " + str(adaptive)) self.msgQueue.put("UQ Driver File: " + uq_file) self.msgQueue.put("------------------------------------") # If using adaptive sampleing open a network socket to listen if adaptive: listener = listen.foqusListener(self.dat) listener.setInputs(self.input) listener.setOutputs(self.output) listener.failValue = self.options["PRESET"].value address = listener.address listener.start() #Run Alamo process = subprocess.Popen([ alamoExe, alamoInput], cwd=alamoDir, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, stdin = None, creationflags=win32process.CREATE_NO_WINDOW) line = process.stdout.readline() while process.poll() == None or line != b'': if line == b'': time.sleep(0.2) if line != b'': self.msgQueue.put(line.decode("utf-8").rstrip()) line = process.stdout.readline() if self.stop.isSet(): self.msgQueue.put("**terminated by user**") process.kill() break if adaptive: # stop the listener conn = Client(address) conn.send(['quit']) conn.close() alamoOutput = os.path.join(alamoDir, alamoOutput) if self.options['FUNFORM'].value != "Fortran": self.msgQueue.put("MUST USE FORTRAN FORM to make UQ and flowsheet plugins") self.msgQueue.put("Skipping plugin creation") return res = SurrogateParser.parseAlamo(alamoOutput) self.result = res #self.msgQueue.put(str(res)) self.writePlugin() SurrogateParser.writeAlamoDriver( self.result, uq_file, ii=self.ii, oi=self.oi, inputNames=self.xList2, outputNames=self.zList2) self.driverFile = uq_file except Exception: self.ex = sys.exc_info() logging.getLogger("foqus." + __name__).error( "Exception in ALAMO Thread", exc_info=sys.exc_info())
def run(self): ''' This function overloads the Thread class function, and is called when you run start() to start a new thread. ''' try: #Get options and show some information about settings adir = self.bssanovaDir self.setupWorkingDir() rscriptExe = self.dat.foqusSettings.rScriptPath xdata = self.options["Input Data File"].value ydata = self.options["Output Data File"].value burnin = self.options["Burn In"].value mcmcit = self.options["MCMC Iterations"].value recint = self.options["Record Interval"].value nterms = self.options["nterms"].value order = self.options["order"].value prior = self.options["priorprob"].value modelFile = self.options["Model File"].value driverFile = self.options["FOQUS Model (for UQ)"].value bte = json.dumps([burnin, mcmcit, recint]) if self.checkNumVars(): return self.msgQueue.put("------------------------------------") self.msgQueue.put("Starting BSS-ANOVA\n") self.msgQueue.put("Model File: " + str(modelFile)) self.msgQueue.put("Py File (UQ): " + str(driverFile)) self.msgQueue.put("RScript Path: " + str(rscriptExe)) self.msgQueue.put("Sub-directory: " + str(adir)) self.msgQueue.put("X data file: " + str(xdata)) self.msgQueue.put("Y data file: " + str(ydata)) self.msgQueue.put("Burn In: " + str(burnin)) self.msgQueue.put("MCMC Iterations: " + str(mcmcit)) self.msgQueue.put("Record Interval: " + str(recint)) self.msgQueue.put("N. Terms: " + str(nterms)) self.msgQueue.put("Order: " + str(order)) self.msgQueue.put("Prior Prob.: " + str(prior)) self.msgQueue.put("bte: " + str(bte)) self.msgQueue.put("------------------------------------") # Run the R script if self.options["Use Flowsheet Data"].value == "Yes": self.msgQueue.put("Exporting Data...") if len(self.input) < 1: self.msgQueue.put( " Must select at least 2 input variables") return self.msgQueue.put(" Inputs: {0}".format( json.dumps(self.input))) self.dat.flowsheet.results.exportVarsCVS(xdata, inputs=self.input, outputs=[], flat=True) if len(self.output) < 1: self.msgQueue.put(" Must select an output variable") return self.msgQueue.put(" Output: {0}".format( json.dumps(self.output))) self.dat.flowsheet.results.exportVarsCVS(ydata, inputs=[], outputs=self.output, flat=True) self.msgQueue.put("Running BSS-ANOVA...") rscriptFile = os.path.basename(rscriptExe) process = subprocess.Popen([ rscriptFile, 'bssanova_fit.R', os.path.abspath(xdata), os.path.abspath(ydata), modelFile, bte, 'auto', str(nterms), str(order), str(prior) ], executable=rscriptExe, cwd=adir, stdout=subprocess.PIPE, stderr=subprocess.PIPE) line = process.stdout.readline() while process.poll() == None or line != '': if line == '': time.sleep(0.2) if line != '': self.msgQueue.put(line.rstrip()) line = process.stdout.readline() if self.stop.isSet(): self.msgQueue.put("**terminated by user**") process.kill() break self.msgQueue.put("Process completed code: {0}".format( process.poll())) line = process.stderr.readline() while line != '': self.msgQueue.put(line.rstrip()) line = process.stderr.readline() modelFile2 = os.path.join(adir, modelFile) driverFile2 = os.path.join(adir, driverFile) rfile = os.path.join(adir, 'bssanova_pred.R') bssAnovaData = { 'outputNames': self.output, # assume univariate 'modelNames': [modelFile2], 'rscriptPath': rscriptExe, 'rfile': rfile } SurrogateParser.writeBssAnovaDriver(bssAnovaData, driverFile2) self.msgQueue.put( "Wrote Python driver file: {0}".format(driverFile2)) self.result = "Done, see Python driver file: {0}".format( driverFile2) self.driverFile = driverFile2 self.writePlugin() # added by BN, 2/4/2016 except Exception: self.ex = sys.exc_info() logging.getLogger("foqus." + __name__).exception( "Exception in BSS-ANOVA Thread")