def run(self): try: result = "0" # grise self.disalbledButtonsCalibration() self.ui.resetCalibrationPushButton.setDisabled(True) self.actualizeOutFiles() with open("fonctions_apprentissage.r", "r", encoding="utf-8") as apprentissageRopen: apprentissage = "".join(apprentissageRopen.readlines()) apprentissage = SignatureTranslatedAnonymousPackage(apprentissage, "apprentissage") if args.debug: print("{}\n{}".format(apprentissage, dir(apprentissage))) # test if Rdata file already exist, if yes remove file if user say yes, or stop analyse if os.path.exists(self.calibrationFilesOut["RData"]): reply = QMessageBox.question(self, 'WARNING', 'File will be overwritten.\nDo you still want to proceed?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: for key, path in self.calibrationFilesOut.items(): os.remove(path) reloadCalibration = True elif reply == QMessageBox.No: reloadCalibration = False else: reloadCalibration = True if reloadCalibration: self.ui.statusbar.showMessage(str("Running calibration, please waiting ...."),9600) #doivent être (dans cet ordre) le nom (relatif) des sous-répertoires fond, limbe, lésions result , good = apprentissage.apprentissage(self.calibrationInOutPath,"background", "leaf", "lesion").r_repr().replace('"','').replace("c(","").replace(")","").split(",") self.calibrationFileOpenLineEdit.setText(self.calibrationFilesOut["RData"]) #print(result, good) if result == "1" and os.path.exists(self.calibrationFilesOut["RData"]): print(result, self.calibrationFilesOut["RData"]) self.infoDialogue(status = "new") self.ui.statusbar.showMessage(str("FINISH, files were product on : %s" % self.calibrationInOutPath),9600) self.ui.resetCalibrationPushButton.setEnabled(True) elif result == "0" and os.path.exists(self.calibrationFilesOut["RData"]): self.infoDialogue(status = "already") print(result, self.calibrationFilesOut["RData"]) self.calibrationFileOpenLineEdit.setText("") self.ui.resetCalibrationPushButton.setEnabled(True) self.resetLoadFolder() self.enableButtonsCalibration() elif result == "0" and not os.path.exists(self.calibrationFilesOut["RData"]): self.displayError(typeError = "ERROR:", message = "Error when running R code....") self.resetLoadFolder() except Exception as e: self.displayError(typeError = "ERROR:", message = "Error when running R code....\n"+e) self.resetLoadFolder() self.ui.resetCalibrationPushButton.setEnabled(True)
def run(self): try: result = "0" # grise self.disalbledButtonsCalibration() self.ui.resetCalibrationPushButton.setDisabled(True) with open("fonctions_apprentissage.r", "r", encoding="utf-8") as apprentissageRopen: apprentissage = "".join(apprentissageRopen.readlines()) apprentissage = SignatureTranslatedAnonymousPackage(apprentissage, "apprentissage") self.CalibrationOutPath = "/".join(str(self.dicoFoldersCalibration["leaf"]).split("/")[:-1]) self.CalibrationBasename = self.CalibrationOutPath.split("/")[-1] self.actualizeOutFiles() if debug: print("{}\n{}".format(apprentissage, dir(apprentissage))) # test if Rdata file already exist, if yes remove file if user say yes, or stop analyse if os.path.exists(self.CalibrationFilesOut["RData"]): reply = QMessageBox.question(self, 'Warning', 'File will be overwritten.\nDo you still want to proceed?', QMessageBox.Yes | QMessageBox.No, QMessageBox.No) if reply == QMessageBox.Yes: for key, path in self.CalibrationFilesOut.items(): os.remove(path) reloadCalibration = True elif reply == QMessageBox.No: reloadCalibration = False else: reloadCalibration = True if reloadCalibration: self.ui.statusbar.showMessage(str("Running Calibration, please waiting ...."),9600) #result , self.CalibrationFilesOut["RData"] = apprentissage.apprentissage(self.dicoObjectOpenLineEditCalibration["leaf"],self.dicoObjectOpenLineEditCalibration["symptom"],self.dicoObjectOpenLineEditCalibration["background"]).r_repr().replace('"','').replace("c(","").replace(")","").split(",") result , good = apprentissage.apprentissage(self.CalibrationOutPath).r_repr().replace('"','').replace("c(","").replace(")","").split(",") self.calibrationFileOpenLineEdit.setText(self.CalibrationFilesOut["RData"]) if result == "1" and os.path.exists(self.CalibrationFilesOut["RData"]): print(result, self.CalibrationFilesOut["RData"]) self.infoDialogue(status = "new") self.ui.statusbar.showMessage(str("FINISH, files were product on : %s" % self.CalibrationOutPath),9600) self.ui.resetCalibrationPushButton.setEnabled(True) elif result == "0" and os.path.exists(self.CalibrationFilesOut["RData"]): self.infoDialogue(status = "already") print(result, self.CalibrationFilesOut["RData"]) self.calibrationFileOpenLineEdit.setText("") self.ui.resetCalibrationPushButton.setEnabled(True) self.resetLoadFolder() self.enableButtonsCalibration() elif result == "0" and not os.path.exists(self.CalibrationFilesOut["RData"]): self.displayError(error = "Error when running R code....") except Exception as e: self.displayError(error = e)
from rpy2.robjects.packages import importr from rpy2.robjects.packages import SignatureTranslatedAnonymousPackage # import R's "base" package base = importr('base') with open("fonctions_apprentissage.r", "r", encoding="utf-8") as apprentissageRopen: apprentissage = "".join(apprentissageRopen.readlines()) print(apprentissage) apprentissage = SignatureTranslatedAnonymousPackage(apprentissage, "apprentissage") path_sample = "/media/sebastien/Bayer/ScriptsSEB/scripts/GUI/EBimage/AnalyseImagesV2/Samples/5583" print(dir(apprentissage)) print(apprentissage.apprentissage(path_sample))
def run(self): print("RUN") try: warning = "" # initialise le nombre d'erreur val = 0 # initialise le nombre d'erreur txtInfo = "" # import R's "base" package base = importr('base') with open("fonctions_apprentissage.r", "r", encoding="utf-8") as apprentissageRopen: apprentissage = "".join(apprentissageRopen.readlines()) print(apprentissage) apprentissage = SignatureTranslatedAnonymousPackage(apprentissage, "apprentissage") #path_sample = "/media/sebastien/Bayer/ScriptsSEB/scripts/GUI/EBimage/AnalyseImagesV2/Samples/5583" path_sample = "/".join(str(self.dicoFoldersCallibration["leaf"]).split("/")[:-1]) print(path_sample) print(dir(apprentissage)) result , pathRdataFile= apprentissage.apprentissage(path_sample).r_repr().replace('"','').replace("c(","").replace(")","").split(",") print(result, pathRdataFile) if result == "1": reply = QMessageBox.question(parent=self, title='Attention', text='File will be overwritten.\nDo you still want to proceed?', buttons=QMessageBox.Yes | QMessageBox.No, defaultButton=QMessageBox.No) if reply == QMessageBox.Yes: print("OK") self.callibrationFileOpenLineEdit.setText(pathRdataFile) else: print("BAD") ##grise les boutons pour pas relancer job #self.ui.frameRun.show() #self.ui.runPushButton.setDisabled(True) #self.ui.loadMatriceFilePushButton.setDisabled(True) #self.ui.loadOrderFilePushButton.setDisabled(True) #self.ui.PCAlineEdit.setDisabled(True) #self.ui.DAlineEdit.setDisabled(True) #self.ui.popMinLineEdit.setDisabled(True) #self.ui.popMaxLineEdit.setDisabled(True) #self.ui.rmOldCheckBox.setDisabled(True) #self.ui.expertCheckBox.setDisabled(True) #self.ui.expertFrame.setDisabled(True) #self.ui.graphTypeComboBox.setDisabled(True) #if self.expertMode == "True": #self.EBimagefix = str(self.ui.EBimagefixPlainTextEdit.toPlainText().toUtf8()) #self.EBimagechange = str(self.ui.EBimagechangePlainTextEdit.toPlainText().toUtf8()) #"""to run programme""" ## création du pathout #if os.path.isdir(self.pathFileOut): #if self.rmOld == "True": #shutil.rmtree(str(self.pathFileOut)) #os.mkdir(self.pathFileOut) #else: #warning += "Warnnig folder "+self.pathFileOut+" already exist,\nPlease remove/rename before run new analysis or use checkbox" #raise Exception(warning) #else: #os.mkdir(self.pathFileOut) ################################################ ## code commun mode graphique ou interface ################################################ ## charge l'ordre a refaire #self.orderList = loadInListCol(self.orderPathFile, 0) ## copie de la matrice dans un dico #self.dicoMatrice = loadInDictLine(self.matricePathFile) ## Comptage du nombre d'individus, de markers et ncode: #self.nbindParam = len(self.dicoMatrice.keys())-1 #if self.nbindParam != len(self.orderList): #txtInfo += "WARNING: More individu in Matrice file (%s) than Order label file (%s)!!!\n" % (self.nbindParam, len(self.orderList)) #fileMat = open(self.matricePathFile,"r") #header = fileMat.readline() #self.nbmarkParam = len(header.split("\t")) #header = " \t"+"\t".join(header.split("\t")[1:]) #nbcode = fileMat.readline().split("\t")[1] #while nbcode == "-9": #nbcode = fileMat.readline().split("\t")[1] #self.ncodeParam = len(nbcode) #fileMat.close() ## ouverture du nouveau fichier trier #with open(self.pathFileOut+self.basename+"_Reorder.tab","w") as reorderMatriceFile: #reorderMatriceFile.write(header) #for ind in self.orderList: #if ind not in self.dicoMatrice.keys(): #error = "ERROR: The individu %s define in label file was not in the matrice file !!! Exit programme" % ind #raise Exception(error) #line = self.dicoMatrice[ind].split("\t")[0]+"\t"+"\t".join(self.dicoMatrice[ind].split("\t")[1:]).replace("999","-9") #reorderMatriceFile.write(line) #txtInfo += "Nb individus: %i\tNb markers: %i\tncodeParam: %i\tGraph type: %s\n" % (self.nbindParam,int(self.nbmarkParam)-1,self.ncodeParam, self.graphType) #if args.cmdMode: #pass #else: #self.ui.runningPlainTextEdit.setPlainText(txtInfo) ##ouverture du script R #Rscript = open(self.pathFileOut+self.basename+"_R_EBimage.R","w") #Rscript.write(installPackageR) ## Ajout du path du fichier matrice dans EBimagefix ## modifie Script R pour adapter aux parametres rentrés #dictToReplace = { #"**MAKERS**" : str(self.nbmarkParam), #"**NCODE**" : str(self.ncodeParam), #"**INDIV**" : str(self.nbindParam), #"**PATHTOFILE**": str(reorderMatriceFile.name), #"**current_dir**" : str(self.pathFileOut), #"**GRAPH**" : str(self.graphType) #} #EBimagefixModif = replace_all(dictToReplace, self.EBimagefix) ##print(EBimagefixModif) #Rscript.write(EBimagefixModif) #for pop in range(int(self.popMinValue),int(self.popMaxValue)+1): ##print(pop) #popstr=str(pop) #EBimagechange2 = self.EBimagechange.replace("**pop**",popstr).replace("**current_dir**",str(self.pathFileOut)).replace("**PCARETAIN**",str(self.PCAvalue)).replace("**DARETAIN**",str(self.DAvalue)) ##print(EBimagechange2) #Rscript.write(EBimagechange2) #Rscript.close() #self.ui.statusbar.showMessage(str("FINISH, script product on : %s" % self.pathFileOut),9600) #txtInfo += "FINISH, script product on :\n %s" % (self.pathFileOut) #if args.cmdMode: #print(txtInfo) #else: #self.ui.runningPlainTextEdit.setPlainText(txtInfo) ## si des erreurs: except Exception as e: self.displayError(error = e)
def run(self): print("RUN") try: warning = "" # initialise le nombre d'erreur val = 0 # initialise le nombre d'erreur txtInfo = "" # import R's "base" package base = importr('base') with open("fonctions_apprentissage.r", "r", encoding="utf-8") as apprentissageRopen: apprentissage = "".join(apprentissageRopen.readlines()) print(apprentissage) apprentissage = SignatureTranslatedAnonymousPackage( apprentissage, "apprentissage") #path_sample = "/media/sebastien/Bayer/ScriptsSEB/scripts/GUI/EBimage/AnalyseImagesV2/Samples/5583" path_sample = "/".join( str(self.dicoFoldersCallibration["leaf"]).split("/")[:-1]) print(path_sample) print(dir(apprentissage)) result, pathRdataFile = apprentissage.apprentissage( path_sample).r_repr().replace('"', '').replace("c(", "").replace( ")", "").split(",") print(result, pathRdataFile) if result == "1": reply = QMessageBox.question( parent=self, title='Attention', text= 'File will be overwritten.\nDo you still want to proceed?', buttons=QMessageBox.Yes | QMessageBox.No, defaultButton=QMessageBox.No) if reply == QMessageBox.Yes: print("OK") self.callibrationFileOpenLineEdit.setText(pathRdataFile) else: print("BAD") ##grise les boutons pour pas relancer job #self.ui.frameRun.show() #self.ui.runPushButton.setDisabled(True) #self.ui.loadMatriceFilePushButton.setDisabled(True) #self.ui.loadOrderFilePushButton.setDisabled(True) #self.ui.PCAlineEdit.setDisabled(True) #self.ui.DAlineEdit.setDisabled(True) #self.ui.popMinLineEdit.setDisabled(True) #self.ui.popMaxLineEdit.setDisabled(True) #self.ui.rmOldCheckBox.setDisabled(True) #self.ui.expertCheckBox.setDisabled(True) #self.ui.expertFrame.setDisabled(True) #self.ui.graphTypeComboBox.setDisabled(True) #if self.expertMode == "True": #self.EBimagefix = str(self.ui.EBimagefixPlainTextEdit.toPlainText().toUtf8()) #self.EBimagechange = str(self.ui.EBimagechangePlainTextEdit.toPlainText().toUtf8()) #"""to run programme""" ## création du pathout #if os.path.isdir(self.pathFileOut): #if self.rmOld == "True": #shutil.rmtree(str(self.pathFileOut)) #os.mkdir(self.pathFileOut) #else: #warning += "Warnnig folder "+self.pathFileOut+" already exist,\nPlease remove/rename before run new analysis or use checkbox" #raise Exception(warning) #else: #os.mkdir(self.pathFileOut) ################################################ ## code commun mode graphique ou interface ################################################ ## charge l'ordre a refaire #self.orderList = loadInListCol(self.orderPathFile, 0) ## copie de la matrice dans un dico #self.dicoMatrice = loadInDictLine(self.matricePathFile) ## Comptage du nombre d'individus, de markers et ncode: #self.nbindParam = len(self.dicoMatrice.keys())-1 #if self.nbindParam != len(self.orderList): #txtInfo += "WARNING: More individu in Matrice file (%s) than Order label file (%s)!!!\n" % (self.nbindParam, len(self.orderList)) #fileMat = open(self.matricePathFile,"r") #header = fileMat.readline() #self.nbmarkParam = len(header.split("\t")) #header = " \t"+"\t".join(header.split("\t")[1:]) #nbcode = fileMat.readline().split("\t")[1] #while nbcode == "-9": #nbcode = fileMat.readline().split("\t")[1] #self.ncodeParam = len(nbcode) #fileMat.close() ## ouverture du nouveau fichier trier #with open(self.pathFileOut+self.basename+"_Reorder.tab","w") as reorderMatriceFile: #reorderMatriceFile.write(header) #for ind in self.orderList: #if ind not in self.dicoMatrice.keys(): #error = "ERROR: The individu %s define in label file was not in the matrice file !!! Exit programme" % ind #raise Exception(error) #line = self.dicoMatrice[ind].split("\t")[0]+"\t"+"\t".join(self.dicoMatrice[ind].split("\t")[1:]).replace("999","-9") #reorderMatriceFile.write(line) #txtInfo += "Nb individus: %i\tNb markers: %i\tncodeParam: %i\tGraph type: %s\n" % (self.nbindParam,int(self.nbmarkParam)-1,self.ncodeParam, self.graphType) #if args.cmdMode: #pass #else: #self.ui.runningPlainTextEdit.setPlainText(txtInfo) ##ouverture du script R #Rscript = open(self.pathFileOut+self.basename+"_R_EBimage.R","w") #Rscript.write(installPackageR) ## Ajout du path du fichier matrice dans EBimagefix ## modifie Script R pour adapter aux parametres rentrés #dictToReplace = { #"**MAKERS**" : str(self.nbmarkParam), #"**NCODE**" : str(self.ncodeParam), #"**INDIV**" : str(self.nbindParam), #"**PATHTOFILE**": str(reorderMatriceFile.name), #"**current_dir**" : str(self.pathFileOut), #"**GRAPH**" : str(self.graphType) #} #EBimagefixModif = replace_all(dictToReplace, self.EBimagefix) ##print(EBimagefixModif) #Rscript.write(EBimagefixModif) #for pop in range(int(self.popMinValue),int(self.popMaxValue)+1): ##print(pop) #popstr=str(pop) #EBimagechange2 = self.EBimagechange.replace("**pop**",popstr).replace("**current_dir**",str(self.pathFileOut)).replace("**PCARETAIN**",str(self.PCAvalue)).replace("**DARETAIN**",str(self.DAvalue)) ##print(EBimagechange2) #Rscript.write(EBimagechange2) #Rscript.close() #self.ui.statusbar.showMessage(str("FINISH, script product on : %s" % self.pathFileOut),9600) #txtInfo += "FINISH, script product on :\n %s" % (self.pathFileOut) #if args.cmdMode: #print(txtInfo) #else: #self.ui.runningPlainTextEdit.setPlainText(txtInfo) ## si des erreurs: except Exception as e: self.displayError(error=e)