def make_new_pairs_plot(self, MDSfile): args = {'i': MDSfile, 'o': self.displayJPEG, 'rc': self.responseCol} checkboxes = [('pres', self.chkPresence), ('absn', self.chkAbsence), ('bgd', self.chkBackground)] for arg, checkbox in checkboxes: args[arg] = str(checkbox.checkState() == QtCore.Qt.Checked).upper() kwarg_args = [('p', 'numPlots'), ('m', 'minCor'), ('core', 'corsWithHighest')] for arg, kwarg_key in kwarg_args: if kwarg_key in self.kwargs: args[arg] = str(self.kwargs[kwarg_key]) if os.path.exists( os.path.join(self.outputDir, "Predictor_Correlation.jpg")): os.remove(os.path.join(self.outputDir, "Predictor_Correlation.jpg")) utils.run_R_script('PairsExplore.r', args, self.module, new_r_path=self.kwargs['r_path']) if os.path.exists(os.path.join(self.outputDir, "devinfo.csv")): self.loadDeviances() if os.path.exists(os.path.join(self.displayJPEG)): return os.path.join(self.displayJPEG) else: writetolog("Missing output from R processing: " + self.displayJPEG) raise Exception, "Missing output from R processing"
def runR(self, MDSfile): # # program = os.path.join(self.rPath, "i386", "Rterm.exe") # script = os.path.join(utils.getModelsPath(), "PairsExplore.r") args = "i=" + '"' + MDSfile + '"' + " o=" + '"' + self.displayJPEG + '"' + " m=" + str( self.lineEdit.text()) args += " rc=" + self.responseCol if self.chkPresence.checkState() == QtCore.Qt.Checked: args += " pres=TRUE" else: args += " pres=FALSE" if self.chkAbsence.checkState() == QtCore.Qt.Checked: args += " absn=TRUE" else: args += " absn=FALSE" if self.chkBackground.checkState() == QtCore.Qt.Checked: args += " bgd=TRUE" else: args += " bgd=FALSE" # command = program + " --vanilla -f " + script + " --args " + args # writetolog(" " + command, False, False) if os.path.exists( os.path.join(self.outputDir, "Predictor_Correlation.jpg")): os.remove(os.path.join(self.outputDir, "Predictor_Correlation.jpg")) utils.runRScript('PairsExplore.r', args) # p = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE) # # writetolog(" Calculating covariate correlation in r R ") # ret = p.communicate() # if ret[1]: # msg = "An error was encountered in the R script for this module. The R error message is below - \n" # msg += ret[1] # writetolog(msg) # raise Exception, msg # else: # writetolog(" Finished in R. ") # del(ret) if os.path.exists(os.path.join(self.displayJPEG)): return os.path.join(self.displayJPEG) else: writetolog("Missing output from R processing: " + self.displayJPEG) raise Exception, "Missing output from R processing"
def runR(self, MDSfile): # # program = os.path.join(self.rPath, "i386", "Rterm.exe") # script = os.path.join(utils.getModelsPath(), "PairsExplore.r") args = "i=" + '"' + MDSfile + '"' + " o=" + '"' + self.displayJPEG + '"' + " m=" + str(self.lineEdit.text()) args += " rc=" + self.responseCol if self.chkPresence.checkState() == QtCore.Qt.Checked: args += " pres=TRUE" else: args += " pres=FALSE" if self.chkAbsence.checkState() == QtCore.Qt.Checked: args += " absn=TRUE" else: args += " absn=FALSE" if self.chkBackground.checkState() == QtCore.Qt.Checked: args += " bgd=TRUE" else: args += " bgd=FALSE" # command = program + " --vanilla -f " + script + " --args " + args # writetolog(" " + command, False, False) if os.path.exists(os.path.join(self.outputDir, "Predictor_Correlation.jpg")): os.remove(os.path.join(self.outputDir, "Predictor_Correlation.jpg")) utils.runRScript('PairsExplore.r', args) # p = subprocess.Popen(command, stderr=subprocess.PIPE, stdout=subprocess.PIPE) # # writetolog(" Calculating covariate correlation in r R ") # ret = p.communicate() # if ret[1]: # msg = "An error was encountered in the R script for this module. The R error message is below - \n" # msg += ret[1] # writetolog(msg) # raise Exception, msg # else: # writetolog(" Finished in R. ") # del(ret) if os.path.exists(os.path.join(self.displayJPEG)): return os.path.join(self.displayJPEG) else: writetolog("Missing output from R processing: " + self.displayJPEG) raise Exception, "Missing output from R processing"
def PopulateTreeview(self): ''' Reads in the input MDS and populates the treeview widget with the items in covariate columns. Sets the check state to be the same as the 0/1 include flag. ''' writetolog(" PopulateTreeview inputMDS = " + self.inputMDS, False, False) self.treeview.setColumnCount(1) #If an outputMDS already exists then the user has run this module before. #We need to pull and apply their previous selections from that output file csvfile = open(self.inputMDS, "r") #print "MDS", self.inputMDS reader = csv.reader(csvfile) header = reader.next() #store the header header2 = reader.next() #the 2nd line of the mds with use/don't use header3 = reader.next() #the 3rd line of the mds with the path self.responseCol = header[2] headerList = [] n = 0 for i in range(0, len(header)): headerList.append([header[i], header2[i], header3[i]]) #headerList.sort() for item in headerList[3:]: child_item = QtGui.QTreeWidgetItem([ item[0], ]) child_item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled) checked = True if int(item[1]) == 0: checked = False if not checked: child_item.setCheckState(0, QtCore.Qt.Unchecked) else: child_item.setCheckState(0, QtCore.Qt.Checked) self.treeview.addTopLevelItem(child_item) #self.tree_items[file] = child_item n += 1 csvfile.close() #update the tree view label to show how many covariates there are self.label_2.setText(_fromUtf8("Covariates (n=" + str(n) + ")"))
def PopulateTreeview(self): ''' Reads in the input MDS and populates the treeview widget with the items in covariate columns. Sets the check state to be the same as the 0/1 include flag. ''' writetolog(" PopulateTreeview inputMDS = " + self.inputMDS, False, False) self.treeview.setColumnCount(1) #If an outputMDS already exists then the user has run this module before. #We need to pull and apply their previous selections from that output file csvfile = open(self.inputMDS, "r") #print "MDS", self.inputMDS reader = csv.reader(csvfile) header = reader.next() #store the header header2 = reader.next() #the 2nd line of the mds with use/don't use header3 = reader.next() #the 3rd line of the mds with the path self.responseCol = header[2] headerList = [] n = 0 for i in range(0, len(header)): headerList.append([header[i], header2[i], header3[i]]) #headerList.sort() for item in headerList[3:]: child_item = QtGui.QTreeWidgetItem([item[0],]) child_item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled) checked = True if int(item[1]) == 0: checked = False if not checked: child_item.setCheckState(0, QtCore.Qt.Unchecked) else: child_item.setCheckState(0, QtCore.Qt.Checked) self.treeview.addTopLevelItem(child_item) #self.tree_items[file] = child_item n += 1 csvfile.close() #update the tree view label to show how many covariates there are self.label_2.setText(_fromUtf8("Covariates (n=" + str(n) + ")"))
def makeNewCovariatePlot(self, output_dir, covariate): output_fname = os.path.join(output_dir, covariate + ".jpg") args = { "i": self.inputMDS, "o": output_dir, "rc": self.responseCol, "p": covariate } self.saveExploreOptions(args) if os.path.exists(output_fname): os.remove(output_fname) utils.run_R_script('Predictor.inspection.r', args) if os.path.exists(output_fname): return output_fname else: writetolog("Missing output from R processing: " + self.displayJPEG) raise Exception, "Missing output from R processing"
def runR(self): # #create a finalModelEvaluation folder eval_type = os.path.split(self.findModel.csv_file)[1] eval_type = os.path.splitext(eval_type)[0] eval_type = eval_type.replace("AcrossModel", "") finalFolder = os.path.join(self.session_folder, "FinalModelEvaluation_" + eval_type) if os.path.exists(finalFolder): msg = "The folder " + "'FinalModelEvaluation_" + eval_type + "' already exists in this session folder." msg += "\nThis indicates that the final evaluation metrics have already been calculated." msg += "\nThese final statistics are only valid for the first time run." msg += "\nThe previous run of statistics will be displayed now." msg += "\nIf you really want to re-run these metrics, manually delete the previously created folder 'FinalModelEvaluation_" + eval_type + "'." msg += "But from a statistical standpoint this is not advised!" msgbox = QtGui.QMessageBox(self) msgbox.setText(msg) msgbox.exec_() output_disp = os.path.join(finalFolder, "FinalEvaluationBinom.jpg") self.load_picture(output_disp) return None else: os.mkdir(finalFolder) treeviewIter = QtGui.QTreeWidgetItemIterator(self.treeview) checked_count = 0 while treeviewIter.value(): item = treeviewIter.value() # col_index = header.index(item.text(0)) if item.checkState(0) == QtCore.Qt.Checked: checked_count += 1 origWS = os.path.join(self.session_folder, str(item.text(0)), "modelWorkspace") outfolder = os.path.join(finalFolder, "evaluate_holdout_" + str(item.text(0))) os.mkdir(outfolder) args = {"ws":origWS, "o":outfolder, 'mpt':"FALSE", 'mbt':"FALSE", 'mes':"FALSE", "pmt":"FALSE"} utils.run_R_script("EvaluateNewData.r", args) writetolog("Finished running R for: " + str(item.text(0)) , False, True) treeviewIter += 1 if checked_count == 1: utils.run_R_script("EvaluateNewData.r", args) writetolog("Finished running R for: " + str(item.text(0)) , False, True) if checked_count == 0: msg = "No models selected for final evaluation/map production." msgbox = QtGui.QMessageBox(self) msgbox.setText(msg) msgbox.exec_() else: output_disp = os.path.join(finalFolder, "FinalEvaluationBinom.jpg") self.display_jpeg = output_disp self.load_picture(output_disp)
def compute(self): """ compute() -> None Dispatch the display event to the spreadsheet with images and labels """ model_workspaces = self.get_input_list("ModelWorkspaces") if len(model_workspaces) < 1 or len(model_workspaces) > 4: raise RuntimeError('Between 1 and 4 ModelWorkspaces must be supplied!') #TODO add in check to make sure all models finished successfully #if still running raise module suspended workspaces = [] for model_workspace in model_workspaces: rel_workspace = utils.get_relative_path(model_workspace, self) rel_workspace = os.path.join(rel_workspace, 'modelWorkspace') workspaces.append(os.path.normpath(rel_workspace)) self.location = utils.get_curve_sheet_location(self) # find the next available port port = 5678 while port_occupied('127.0.0.1', port): print "port ({}) occupied trying next".format(port) port += 1 if port > 6000: raise RuntimeError('Unable to find unoccupied port!') # launch the Shiny app args = {} args['port'] = str(port) args['wsList'] = ",".join(workspaces) script = "ResponseCurveShinyApp.r" cmd = utils.gen_R_cmd(script, args) if self.has_input('run_name_info'): runinfo = self.force_get_input('run_name_info') subfolder = runinfo.contents.get('subfolder', "") runname = runinfo.contents.get('runname', "") else: subfolders = [] runnames = [] for outdname in workspaces: _subfolder, _runname = utils.get_previous_run_info(os.path.split(outdname)[0]) subfolders.append(_subfolder) runnames.append(_runname) if all(x == subfolders[0] for x in subfolders): subfolder = subfolders[0] else: subfolder = '' if all(x == runnames[0] for x in runnames): runname = runnames[0] else: runname = '' outdname = os.path.join(utils.getrootdir(), subfolder, "ResponseCurveExplorerOutput") if not os.path.exists(outdname): os.makedirs(outdname) outdname = utils.mknextdir(prefix="ResponseCurveExplorerOutput", skipSequence=False, directory=os.path.join(utils.getrootdir(), subfolder)) outfname = utils.mknextfile("ResponseCurveExplorer_stdout", suffix=".txt", directory=outdname, runname=runname) errfname = utils.mknextfile("ResponseCurveExplorer_stderr", suffix=".txt", directory=outdname, runname=runname) utils.writetolog("\nStarting processing of " + script , True) utils.writetolog(" command used: \n" + utilities.convert_list_to_cmd_str(cmd), False, False) stdErrFile = open(errfname, 'a') stdErrFile.seek(0, os.SEEK_END) stdOutFile = open(outfname, 'a') stdOutFile.seek(0, os.SEEK_END) p = subprocess.Popen(cmd, stderr=stdErrFile, stdout=stdOutFile) start_time = time.clock() CUTOFF_SECONDS = 300 # 5min while True: with open(outfname) as stdout_f: stdout = stdout_f.read() try: listening = stdout.split("Listening on ") url = listening[-1].strip() except: pass with open(errfname) as stderr_f: stderr = stderr_f.read() if "Error" in stderr: raise RuntimeError("Running Shiny App resulted in an Error:\n{}".format(stderr)) else: listening = stderr.split("Listening on ") url = listening[-1].strip() if url.startswith("http:"): break time.sleep(5) if time.clock() - start_time > CUTOFF_SECONDS: msg = "Shiny App taking longer than expected to load!.\n" msg += "The R kernel generating the Shiny app will continue to run in the background.\n" msg += "Manually monitor the contents of {\n}\tfor the url which you can then open in a browser.".format(errfname) raise RuntimeError(msg) self.cellWidget = self.displayAndWait(responseCurveExplorerWidger, (p, url, None))