コード例 #1
0
ファイル: Tab3.py プロジェクト: ttlion/sdtDBNsGUI_code
    def __init__(self, mainFrame, width):
        self.framePredictSpecific = ttk.Frame(mainFrame, width=width)
        self.framePredictSpecific.grid(row=1, column=1, rowspan=5)

        self.frameInfResults = ttk.Frame(self.framePredictSpecific,
                                         width=width)
        self.frameInfResults.grid(row=1, column=3, rowspan=27)

        self.frameErrors = ttk.Frame(self.framePredictSpecific, width=width)
        self.frameErrors.grid(row=6, column=1, rowspan=3)

        self.widthLeft = 30
        self.widthCenter = 15
        self.widthInput = 4

        self.staticObsGiven = False  # By default put as not having static observations

        # Present filename of DBN being used
        self.showDBN = ElemTwoPresent(self.framePredictSpecific,
                                      "sdtDBN being used: ",
                                      "No file yet selected", self.widthLeft,
                                      1, 1)

        # Create a Tkinter variable for available ids
        self.idTab3TkVar = StringVar(self.framePredictSpecific)
        self.idTab3Choices = ['Inference observations not given!']

        self.idTab3 = ElemTwoSelect(self.framePredictSpecific, "Desired id: ",
                                    self.widthLeft, self.idTab3TkVar,
                                    self.idTab3Choices, self.idTab3Choices[0],
                                    2, 1)

        # Create a Tkinter variable for available attributes
        self.attTab3TkVar = StringVar(self.framePredictSpecific)
        self.attTab3Choices = ['There is not an sdtDBN learned!']

        self.attTab3 = ElemTwoSelect(self.framePredictSpecific,
                                     "Desired attribute: ", self.widthLeft,
                                     self.attTab3TkVar, self.attTab3Choices,
                                     self.attTab3Choices[0], 3, 1)

        # Create a Tkinter variable for timesteps
        self.timestepTab3 = ElemTwoInput(self.framePredictSpecific,
                                         "Desired timestep: ", self.widthLeft,
                                         self.widthInput, 4, 1, 1)

        self.makeInfTab3 = ttk.Button(self.framePredictSpecific,
                                      text="Make inference",
                                      command=self.onSubmit)
        self.makeInfTab3.grid(row=5,
                              column=1,
                              columnspan=2,
                              sticky=N + S + E + W)
コード例 #2
0
    def __init__(self, mainFrame, width):
        self.framePredictProgres = ttk.Frame(mainFrame, width=width)
        self.framePredictProgres.grid(row=1, column=1, rowspan=2)

        self.frameInfResults = ttk.Frame(self.framePredictProgres, width=width)
        self.frameInfResults.grid(row=1, column=3, rowspan=27)

        self.frameErrors = ttk.Frame(self.framePredictProgres, width=width)
        self.frameErrors.grid(row=7, column=1, rowspan=3)

        self.widthLeft = 30
        self.widthCenter = 15
        self.widthInput = 5

        self.staticObsGiven = False # By default put as not having static observations

        # Present filename of DBN being used
        self.showDBN = ElemTwoPresent(self.framePredictProgres, "sdtDBN being used: ", "No file yet selected", self.widthLeft, 1, 1)

        # Create a Tkinter variable for available ids
        self.idTab4TkVar = StringVar(self.framePredictProgres)
        self.idTab4Choices = [ 'Inference observations not given!' ]

        self.idTab4 = ElemTwoSelect(self.framePredictProgres, "Desired id: ", self.widthLeft, self.idTab4TkVar, self.idTab4Choices, self.idTab4Choices[0], 2, 1 )

        # Create a Tkinter variable for available attributes
        self.attTab4TkVar = StringVar(self.framePredictProgres)
        self.attTab4Choices = [ 'There is not an sdtDBN learned!' ]

        self.attTab4 = ElemTwoSelect(self.framePredictProgres, "Desired attribute: ", self.widthLeft, self.attTab4TkVar, self.attTab4Choices, self.attTab4Choices[0], 3, 1 )

        # Create a Tkinter variable for timesteps
        self.timestepTab4 = ElemTwoInput(self.framePredictProgres, "Maximum timestep: ", self.widthLeft, self.widthInput, 4, 1, 1)

        # Create a Tkinter variable for the modes
        self.modeTab4TkVar = StringVar(self.framePredictProgres)
        self.modeTab4Choices = [ 'Distribution', 'Most Probable', 'Random Estimation using probability distributions' ]

        self.modeTab4 = ElemTwoSelect(self.framePredictProgres, "Desired estimation mode: ", self.widthLeft, self.modeTab4TkVar, self.modeTab4Choices, self.modeTab4Choices[0], 5, 1 )

        self.modesDict = {
            'Distribution' : 'distrib',
            'Most Probable' : 'mostProb',
            'Random Estimation using probability distributions' : 'distrSampl'
        }

        # Button to submit, making inference
        self.makeInfTab4 = ttk.Button(self.framePredictProgres, text = "Make inference", command = self.onSubmit)
        self.makeInfTab4.grid(row=6, column=1, columnspan=2, sticky = N+S+E+W)
コード例 #3
0
    def __init__(self, mainFrame, width):

        self.frameInfoToImg = ttk.Frame(mainFrame, width=width)
        self.frameInfoToImg.grid(row=1, column=1, rowspan=10)

        self.frameErrors = ttk.Frame(self.frameInfoToImg, width=width)
        self.frameErrors.grid(row=4, column=1, columnspan=2)

        self.framePresentImg = ttk.Frame(self.frameInfoToImg, width=width)
        self.framePresentImg.grid(row=1, column=3, rowspan=40)

        self.widthLeft = 30
        self.widthCenter = 15
        self.widthInput = 25

        # Present filename of DBN being used
        self.showDBN = ElemTwoPresent(self.frameInfoToImg, "sdtDBN being used: ", "No file yet selected", self.widthLeft, 1, 1)

        # Create a Tkinter variable for output filename
        self.outputPathImg = ElemTwoInput(self.frameInfoToImg, "Image filename: ", self.widthLeft, self.widthInput, 2, 1, 'imgNameExample')

        # Button to submit, making inference
        self.makeInfTab5 = ttk.Button(self.frameInfoToImg, text = "Create Image", command = self.onSubmit)
        self.makeInfTab5.grid(row=3, column=1, columnspan=2, sticky = N+S+E+W)
コード例 #4
0
class Tab1_2:

    def __init__(self, mainFrame, width):

        self.frameInfoToImg = ttk.Frame(mainFrame, width=width)
        self.frameInfoToImg.grid(row=1, column=1, rowspan=10)

        self.frameErrors = ttk.Frame(self.frameInfoToImg, width=width)
        self.frameErrors.grid(row=4, column=1, columnspan=2)

        self.framePresentImg = ttk.Frame(self.frameInfoToImg, width=width)
        self.framePresentImg.grid(row=1, column=3, rowspan=40)

        self.widthLeft = 30
        self.widthCenter = 15
        self.widthInput = 25

        # Present filename of DBN being used
        self.showDBN = ElemTwoPresent(self.frameInfoToImg, "sdtDBN being used: ", "No file yet selected", self.widthLeft, 1, 1)

        # Create a Tkinter variable for output filename
        self.outputPathImg = ElemTwoInput(self.frameInfoToImg, "Image filename: ", self.widthLeft, self.widthInput, 2, 1, 'imgNameExample')

        # Button to submit, making inference
        self.makeInfTab5 = ttk.Button(self.frameInfoToImg, text = "Create Image", command = self.onSubmit)
        self.makeInfTab5.grid(row=3, column=1, columnspan=2, sticky = N+S+E+W)

    def onSubmit(self):

        for widget in self.frameErrors.winfo_children():
            widget.destroy()

        for widget in self.framePresentImg.winfo_children():
            widget.destroy()

        if( self.checkArgs() == False ):
            return

        p = subprocess.run(['java', '-jar', 'sdtDBN_v0_0_1.jar', '-fromFile', self.showDBN.messageRight, '-d'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, encoding='utf-8')

        dotSourceCode = p.stdout
        
        imgFileName = self.outputPathImg.entry.get()

        with open("auxiliar.dot", 'w', newline='') as file:
            file.write(dotSourceCode)

        p = subprocess.run(['dot', '-Tpng', 'auxiliar.dot' , '-o', imgFileName + '.png'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, encoding='utf-8')

        os.remove('auxiliar.dot')

        self.img = PhotoImage(file = imgFileName + '.png')
        
        labelToPresentImg = ttk.Label(self.framePresentImg, image = self.img)
        labelToPresentImg.grid(row=1, column=1, rowspan=40)

        printInfo = ttk.Label(self.frameErrors, text="Image of DBN saved at " + imgFileName + ".png", style="ok.TLabel")
        printInfo.grid(row=1, column=1, columnspan=2)

        return

    def checkArgs(self):
        if(self.showDBN.messageRight == "No file yet selected" ):
            printInfo = ttk.Label(self.frameErrors, text="no sdtDBN file was given!", style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False
        if(self.outputPathImg.entry.get() == ''):
            printInfo = ttk.Label(self.frameErrors, text="An image filename must be specified!", style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False
        return True
    
    def setDBNFile(self, dbnFilename):
        self.showDBN.destroy()
        self.showDBN = ElemTwoPresent(self.frameInfoToImg, "sdtDBN being used: ", dbnFilename, self.widthLeft, 1, 1)
        return
コード例 #5
0
 def setDBNFile(self, dbnFilename):
     self.showDBN.destroy()
     self.showDBN = ElemTwoPresent(self.frameInfoToImg, "sdtDBN being used: ", dbnFilename, self.widthLeft, 1, 1)
     return
コード例 #6
0
class Tab4:

    def __init__(self, mainFrame, width):
        self.framePredictProgres = ttk.Frame(mainFrame, width=width)
        self.framePredictProgres.grid(row=1, column=1, rowspan=2)

        self.frameInfResults = ttk.Frame(self.framePredictProgres, width=width)
        self.frameInfResults.grid(row=1, column=3, rowspan=27)

        self.frameErrors = ttk.Frame(self.framePredictProgres, width=width)
        self.frameErrors.grid(row=7, column=1, rowspan=3)

        self.widthLeft = 30
        self.widthCenter = 15
        self.widthInput = 5

        self.staticObsGiven = False # By default put as not having static observations

        # Present filename of DBN being used
        self.showDBN = ElemTwoPresent(self.framePredictProgres, "sdtDBN being used: ", "No file yet selected", self.widthLeft, 1, 1)

        # Create a Tkinter variable for available ids
        self.idTab4TkVar = StringVar(self.framePredictProgres)
        self.idTab4Choices = [ 'Inference observations not given!' ]

        self.idTab4 = ElemTwoSelect(self.framePredictProgres, "Desired id: ", self.widthLeft, self.idTab4TkVar, self.idTab4Choices, self.idTab4Choices[0], 2, 1 )

        # Create a Tkinter variable for available attributes
        self.attTab4TkVar = StringVar(self.framePredictProgres)
        self.attTab4Choices = [ 'There is not an sdtDBN learned!' ]

        self.attTab4 = ElemTwoSelect(self.framePredictProgres, "Desired attribute: ", self.widthLeft, self.attTab4TkVar, self.attTab4Choices, self.attTab4Choices[0], 3, 1 )

        # Create a Tkinter variable for timesteps
        self.timestepTab4 = ElemTwoInput(self.framePredictProgres, "Maximum timestep: ", self.widthLeft, self.widthInput, 4, 1, 1)

        # Create a Tkinter variable for the modes
        self.modeTab4TkVar = StringVar(self.framePredictProgres)
        self.modeTab4Choices = [ 'Distribution', 'Most Probable', 'Random Estimation using probability distributions' ]

        self.modeTab4 = ElemTwoSelect(self.framePredictProgres, "Desired estimation mode: ", self.widthLeft, self.modeTab4TkVar, self.modeTab4Choices, self.modeTab4Choices[0], 5, 1 )

        self.modesDict = {
            'Distribution' : 'distrib',
            'Most Probable' : 'mostProb',
            'Random Estimation using probability distributions' : 'distrSampl'
        }

        # Button to submit, making inference
        self.makeInfTab4 = ttk.Button(self.framePredictProgres, text = "Make inference", command = self.onSubmit)
        self.makeInfTab4.grid(row=6, column=1, columnspan=2, sticky = N+S+E+W)

    def changeAttOptions(self, newOptionsList):

        self.attTab4.destroy()

        self.attTab4Choices = ['all'] + newOptionsList

        self.attTab4 = ElemTwoSelect(self.framePredictProgres, "Desired attribute: ", self.widthLeft, self.attTab4TkVar, self.attTab4Choices, self.attTab4Choices[0], 3, 1 )

    def changeIdsOptions(self, newOptionsList):

        self.idTab4.destroy()

        self.idTab4Choices = newOptionsList

        self.idTab4 = ElemTwoSelect(self.framePredictProgres, "Desired id: ", self.widthLeft, self.idTab4TkVar, self.idTab4Choices, self.idTab4Choices[0], 2, 1 )

    def getInfSpecs(self, dynObsInfFilename, staticObsInfFilename):

        self.dynObsInfFilename = dynObsInfFilename
        self.staticObsInfFilename = staticObsInfFilename
        
        dynInfFile = open(self.dynObsInfFilename)
        reader = csv.reader(dynInfFile, delimiter=',')
        next(reader) # skip 1st line
        idList = []
        for row in reader:
            idList.append(row[0])
        dynInfFile.close()

        self.changeIdsOptions(idList)

        if(staticObsInfFilename == "Not yet selected!" or staticObsInfFilename == ''):
            self.staticObsGiven = False
        else:
            self.staticObsGiven = True

        return

    def onSubmit(self):

        if(self.checkArgs() == False):
            return

        desiredID = self.idTab4TkVar.get()

        auxDynInfFilename = 'auxDynInf.csv'
        auxInfVarFilename = 'auxInfVar.csv'

        dynInfFile = open(self.dynObsInfFilename)
        reader = csv.reader(dynInfFile, delimiter=',')
        firstLine = next(reader)
        desiredLineDyn = []
        for row in reader:
            if(row[0] == desiredID ):
                desiredLineDyn = row
                break
        dynInfFile.close()

        with open(auxDynInfFilename, 'w', newline='') as file:
            writer = csv.writer(file)
            writer.writerow(firstLine)
            writer.writerow(desiredLineDyn)

        if(self.staticObsGiven == True):
            auxStaticInfFilename = 'auxStaticInf.csv'
            staticInfFile = open(self.staticObsInfFilename)
            reader = csv.reader(staticInfFile, delimiter=',')
            firstLine = next(reader)
            desiredLineStatic = []
            for row in reader:
                if(row[0] == desiredID ):
                    desiredLineStatic = row
                    break
            staticInfFile.close()

            with open(auxStaticInfFilename, 'w', newline='') as file:
                writer = csv.writer(file)
                writer.writerow(firstLine)
                writer.writerow(desiredLineStatic)

        if(self.modeTab4TkVar.get() == 'Distribution'):
            if(self.attTab4TkVar.get() != 'all'):
                desiredAtt = self.attTab4TkVar.get()
                maxTimestep = int(self.timestepTab4.entry.get())
                with open(auxInfVarFilename, 'w', newline='') as file:
                    writer = csv.writer(file)
                    i=0
                    while (i<=maxTimestep):
                        infAttInList = [desiredAtt, i ]
                        writer.writerow(infAttInList)
                        i+=1
            else:
                maxTimestep = int(self.timestepTab4.entry.get())
                with open(auxInfVarFilename, 'w', newline='') as file:
                    writer = csv.writer(file)
                    for att in self.attTab4Choices:
                        if(att == 'all'):
                            continue
                        i=0
                        while (i<=maxTimestep):
                            infAttInList = [att, i]
                            writer.writerow(infAttInList)
                            i+=1

        infCmdArgs = ['-obs', auxDynInfFilename, '-infFmt', self.modesDict.get(self.modeTab4TkVar.get())]

        if(self.staticObsGiven == True):
            infCmdArgs = infCmdArgs + ['-obsStatic', auxStaticInfFilename]

        if(self.modeTab4TkVar.get() == 'Distribution'):
            infCmdArgs = infCmdArgs + ['-inf', auxInfVarFilename]
        else:
            infCmdArgs = infCmdArgs + ['-t', self.timestepTab4.entry.get()]
            if(self.attTab4TkVar.get() != 'all'):
                infCmdArgs = infCmdArgs + ['-tf', 'auxFile.csv']

        p = subprocess.run(['java', '-jar', 'sdtDBN_v0_0_1.jar', '-fromFile', self.showDBN.messageRight] + infCmdArgs , stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE, encoding='utf-8')
        
        self.infResult = p.stdout

        if(self.modeTab4TkVar.get() != 'Distribution' and self.attTab4TkVar.get() != 'all'):
            
            attToKeep = self.attTab4TkVar.get()
            with open('auxFile.csv', 'r', newline='') as file:
                reader = csv.DictReader(file)

                header = [reader.fieldnames[0]]
                for elem in reader.fieldnames:
                    if (attToKeep == elem.split('__')[0]):
                        header = header + [ elem ]
                string = ','.join(header)
                
                for row in reader:
                    newLine = [ row[header[0]] ]
                    for key in row:
                        if (attToKeep == key.split('__')[0]):
                            newLine = newLine + [ row[key] ]
                    string = string + "\n" + (','.join(newLine))

            self.infResult = self.infResult + string
            os.remove('auxFile.csv')

        os.remove(auxDynInfFilename)
        
        if(self.modeTab4TkVar.get() == 'Distribution'):
            os.remove(auxInfVarFilename)
            
        if(self.staticObsGiven == True):
            os.remove(auxStaticInfFilename)

        for widget in self.frameInfResults.winfo_children():
             widget.destroy()

        textInfo = scrolledtext.ScrolledText(self.frameInfResults, height=33, width=45)
        textInfo.grid(row=1, column=4, rowspan=33, padx=7)
        textInfo.insert(END, self.infResult)

        return

    def checkArgs(self):
        for widget in self.frameErrors.winfo_children():
            widget.destroy()

        if (self.showDBN.messageRight == "No file yet selected" ):
            printInfo = ttk.Label(self.frameErrors, text="No DBN was selected!", style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False
        
        if(self.idTab4TkVar.get() == 'Inference observations not given!'):
            printInfo = ttk.Label(self.frameErrors, text="Inference observations not given!", style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False

        if(self.attTab4TkVar.get() == 'There is not an sdtDBN learned!'):
            printInfo = ttk.Label(self.frameErrors, text="No attribute was specified!", style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False
        
        if(self.is_integer(self.timestepTab4.entry.get()) == False):
            printInfo = ttk.Label(self.frameErrors, text = "Timestep is not an integer!", style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False

        if(int(self.timestepTab4.entry.get()) < 1):
            printInfo = ttk.Label(self.frameErrors, text = "Timestep must be > 0", style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False

        return True
    
    def is_integer(self, value: str) -> bool:
        try:
            int(value)
            return True
        except ValueError:
            return False

    def setDBNFile(self, dbnFilename):
        self.showDBN.destroy()
        self.showDBN = ElemTwoPresent(self.framePredictProgres, "sdtDBN being used: ", dbnFilename, self.widthLeft, 1, 1)
        return
コード例 #7
0
    def __init__(self, mainFrame, width):

        self.framePredictMany = ttk.Frame(mainFrame, width=width)
        self.framePredictMany.grid(row=1, column=1, rowspan=10)

        self.frameErrors = ttk.Frame(self.framePredictMany, width=width)
        self.frameErrors.grid(row=7, column=1, rowspan=3)

        self.frameInfResults = ttk.Frame(self.framePredictMany, width=width)
        self.frameInfResults.grid(row=1, column=4, rowspan=27)

        self.dynObsInfFilename = ''
        self.staticObsInfFilename = ''

        self.widthLeft = 30
        self.widthCenter = 15
        self.widthInput = 4

        self.staticObsGiven = False  # By default put as not having static observations

        # Present filename of DBN being used
        self.showDBN = ElemTwoPresent(self.framePredictMany,
                                      "sdtDBN being used: ",
                                      "No file yet selected", self.widthLeft,
                                      1, 1)

        # Define desired mode
        self.desiredModeTab5TkVar = StringVar(self.framePredictMany)
        self.desiredModeTab5Choices = [
            'Attribute inference', 'Progression until timestep'
        ]

        self.desiredModeTab5 = ElemTwoSelect(self.framePredictMany,
                                             "Desired mode: ", self.widthLeft,
                                             self.desiredModeTab5TkVar,
                                             self.desiredModeTab5Choices,
                                             self.desiredModeTab5Choices[0], 2,
                                             1)

        self.onlyCSVfiles = [("csv files", "*.csv")]

        # This is selected by default
        self.timestepOrVarsToInf_Tab5 = ElemThree(
            self.framePredictMany, 4, 1, "File with vars to inference: ",
            "Not yet selected!", self.widthLeft, self.widthCenter,
            self.onlyCSVfiles)

        # When this var changes, it should be created the proper box
        self.desiredModeTab5TkVar.trace("w", self.createProperBox)

        # Create a Tkinter variable for the modes
        self.estimModeTab5TkVar = StringVar(self.framePredictMany)
        self.estimModeTab5Choices = [
            'Distribution', 'Most Probable',
            'Random Estimation using probability distributions'
        ]

        # By default, Distribution is presented also because it is presented att inference as the mode
        self.estimModeTab5 = ElemTwoSelect(self.framePredictMany,
                                           "Desired estimation mode: ",
                                           self.widthLeft,
                                           self.estimModeTab5TkVar,
                                           self.estimModeTab5Choices,
                                           self.estimModeTab5Choices[0], 3, 1)

        self.estimModesDict = {
            'Distribution': 'distrib',
            'Most Probable': 'mostProb',
            'Random Estimation using probability distributions': 'distrSampl'
        }

        # Create a Tkinter variable for output filename
        self.outputPathTab5 = ElemTwoInput(self.framePredictMany,
                                           "Output filename: ", self.widthLeft,
                                           25, 5, 1, 'exampleOut.csv')

        # Button to submit, making inference
        self.makeInfTab5 = ttk.Button(self.framePredictMany,
                                      text="Make inference",
                                      command=self.onSubmit)
        self.makeInfTab5.grid(row=6,
                              column=1,
                              columnspan=3,
                              sticky=N + S + E + W)
コード例 #8
0
class Tab5:
    def __init__(self, mainFrame, width):

        self.framePredictMany = ttk.Frame(mainFrame, width=width)
        self.framePredictMany.grid(row=1, column=1, rowspan=10)

        self.frameErrors = ttk.Frame(self.framePredictMany, width=width)
        self.frameErrors.grid(row=7, column=1, rowspan=3)

        self.frameInfResults = ttk.Frame(self.framePredictMany, width=width)
        self.frameInfResults.grid(row=1, column=4, rowspan=27)

        self.dynObsInfFilename = ''
        self.staticObsInfFilename = ''

        self.widthLeft = 30
        self.widthCenter = 15
        self.widthInput = 4

        self.staticObsGiven = False  # By default put as not having static observations

        # Present filename of DBN being used
        self.showDBN = ElemTwoPresent(self.framePredictMany,
                                      "sdtDBN being used: ",
                                      "No file yet selected", self.widthLeft,
                                      1, 1)

        # Define desired mode
        self.desiredModeTab5TkVar = StringVar(self.framePredictMany)
        self.desiredModeTab5Choices = [
            'Attribute inference', 'Progression until timestep'
        ]

        self.desiredModeTab5 = ElemTwoSelect(self.framePredictMany,
                                             "Desired mode: ", self.widthLeft,
                                             self.desiredModeTab5TkVar,
                                             self.desiredModeTab5Choices,
                                             self.desiredModeTab5Choices[0], 2,
                                             1)

        self.onlyCSVfiles = [("csv files", "*.csv")]

        # This is selected by default
        self.timestepOrVarsToInf_Tab5 = ElemThree(
            self.framePredictMany, 4, 1, "File with vars to inference: ",
            "Not yet selected!", self.widthLeft, self.widthCenter,
            self.onlyCSVfiles)

        # When this var changes, it should be created the proper box
        self.desiredModeTab5TkVar.trace("w", self.createProperBox)

        # Create a Tkinter variable for the modes
        self.estimModeTab5TkVar = StringVar(self.framePredictMany)
        self.estimModeTab5Choices = [
            'Distribution', 'Most Probable',
            'Random Estimation using probability distributions'
        ]

        # By default, Distribution is presented also because it is presented att inference as the mode
        self.estimModeTab5 = ElemTwoSelect(self.framePredictMany,
                                           "Desired estimation mode: ",
                                           self.widthLeft,
                                           self.estimModeTab5TkVar,
                                           self.estimModeTab5Choices,
                                           self.estimModeTab5Choices[0], 3, 1)

        self.estimModesDict = {
            'Distribution': 'distrib',
            'Most Probable': 'mostProb',
            'Random Estimation using probability distributions': 'distrSampl'
        }

        # Create a Tkinter variable for output filename
        self.outputPathTab5 = ElemTwoInput(self.framePredictMany,
                                           "Output filename: ", self.widthLeft,
                                           25, 5, 1, 'exampleOut.csv')

        # Button to submit, making inference
        self.makeInfTab5 = ttk.Button(self.framePredictMany,
                                      text="Make inference",
                                      command=self.onSubmit)
        self.makeInfTab5.grid(row=6,
                              column=1,
                              columnspan=3,
                              sticky=N + S + E + W)

    def createProperBox(self, *args):

        self.timestepOrVarsToInf_Tab5.destroy()
        self.estimModeTab5.destroy()

        if (self.desiredModeTab5TkVar.get() == 'Attribute inference'):
            # Create variable to insert file with vars to inference
            self.timestepOrVarsToInf_Tab5 = ElemThree(
                self.framePredictMany, 4, 1, "File with vars to inference: ",
                "Not yet selected!", self.widthLeft, self.widthCenter,
                self.onlyCSVfiles)

            self.estimModeTab5Choices = [
                'Distribution', 'Most Probable',
                'Random Estimation using probability distributions'
            ]

        else:
            # Create a Tkinter variable for timesteps
            self.timestepOrVarsToInf_Tab5 = ElemTwoInput(
                self.framePredictMany, "Maximum timestep: ", self.widthLeft,
                self.widthInput, 4, 1, 1)

            self.estimModeTab5Choices = [
                'Most Probable',
                'Random Estimation using probability distributions'
            ]

        # Create estimModeTab5 with proper Choices
        self.estimModeTab5 = ElemTwoSelect(self.framePredictMany,
                                           "Desired estimation mode: ",
                                           self.widthLeft,
                                           self.estimModeTab5TkVar,
                                           self.estimModeTab5Choices,
                                           self.estimModeTab5Choices[0], 3, 1)

        return

    def getInfSpecs(self, dynObsInfFilename, staticObsInfFilename):
        self.dynObsInfFilename = dynObsInfFilename
        self.staticObsInfFilename = staticObsInfFilename

        if (staticObsInfFilename == "Not yet selected!"
                or staticObsInfFilename == ''):
            self.staticObsGiven = False
        else:
            self.staticObsGiven = True

        return

    def onSubmit(self):

        if (self.checkArgs() == False):
            return

        infCmdArgs = [
            '-obs', self.dynObsInfFilename, '-infFmt',
            self.estimModesDict.get(self.estimModeTab5TkVar.get())
        ]

        if (self.staticObsGiven == True):
            infCmdArgs = infCmdArgs + ['-obsStatic', self.staticObsInfFilename]

        if (self.desiredModeTab5TkVar.get() == 'Attribute inference'):
            infCmdArgs = infCmdArgs + [
                '-inf', self.timestepOrVarsToInf_Tab5.FileName, '-outInf',
                self.outputPathTab5.entry.get()
            ]
        else:
            infCmdArgs = infCmdArgs + [
                '-t',
                self.timestepOrVarsToInf_Tab5.entry.get(), '-tf',
                self.outputPathTab5.entry.get()
            ]

        p = subprocess.run([
            'java', '-jar', 'sdtDBN_v0_0_1.jar', '-fromFile',
            self.showDBN.messageRight
        ] + infCmdArgs,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE,
                           stdin=subprocess.PIPE,
                           encoding='utf-8')

        p.stdout

        printInfo = ttk.Label(
            self.frameErrors,
            text="Desired prediction is also in the defined output file!",
            style="ok.TLabel")
        printInfo.grid(row=1, column=1, columnspan=2)

        with open(self.outputPathTab5.entry.get(), 'r') as file:
            outputFileData = file.read()
            textInfo = scrolledtext.ScrolledText(self.frameInfResults,
                                                 height=33,
                                                 width=45)
            textInfo.grid(row=1, column=1, rowspan=33, padx=7)
            textInfo.insert(END, outputFileData)

        return

    def checkArgs(self):
        for widget in self.frameErrors.winfo_children():
            widget.destroy()

        for widget in self.frameInfResults.winfo_children():
            widget.destroy()

        if (self.showDBN.messageRight == "No file yet selected"):
            printInfo = ttk.Label(self.frameErrors,
                                  text="No DBN was selected!",
                                  style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False

        if (self.dynObsInfFilename == ''):
            printInfo = ttk.Label(
                self.frameErrors,
                text="No file with dynamic observations was given!",
                style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False

        if (self.desiredModeTab5TkVar.get() == 'Attribute inference'):
            if (self.timestepOrVarsToInf_Tab5.FileName == "Not yet selected!"
                    or self.timestepOrVarsToInf_Tab5.FileName == ''):
                printInfo = ttk.Label(
                    self.frameErrors,
                    text="No file with variables to inference was given",
                    style="notok.TLabel")
                printInfo.grid(row=1, column=1, columnspan=3)
                return False
        else:
            if (self.is_integer(
                    self.timestepOrVarsToInf_Tab5.entry.get()) == False):
                printInfo = ttk.Label(
                    self.frameErrors,
                    text="Maximum timestep is not an integer!",
                    style="notok.TLabel")
                printInfo.grid(row=1, column=1, columnspan=2)
                return False

            if (int(self.timestepOrVarsToInf_Tab5.entry.get()) < 1):
                printInfo = ttk.Label(self.frameErrors,
                                      text="Maximum timestep must be > 0",
                                      style="notok.TLabel")
                printInfo.grid(row=1, column=1, columnspan=2)
                return False

        if (self.outputPathTab5.entry.get() == ''):
            printInfo = ttk.Label(self.frameErrors,
                                  text="Output filename not given!",
                                  style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False

        return True

    def is_integer(self, value: str) -> bool:
        try:
            int(value)
            return True
        except ValueError:
            return False

    def setDBNFile(self, dbnFilename):
        self.showDBN.destroy()
        self.showDBN = ElemTwoPresent(self.framePredictMany,
                                      "sdtDBN being used: ", dbnFilename,
                                      self.widthLeft, 1, 1)
        return
コード例 #9
0
ファイル: Tab3.py プロジェクト: ttlion/sdtDBNsGUI_code
class Tab3:
    def __init__(self, mainFrame, width):
        self.framePredictSpecific = ttk.Frame(mainFrame, width=width)
        self.framePredictSpecific.grid(row=1, column=1, rowspan=5)

        self.frameInfResults = ttk.Frame(self.framePredictSpecific,
                                         width=width)
        self.frameInfResults.grid(row=1, column=3, rowspan=27)

        self.frameErrors = ttk.Frame(self.framePredictSpecific, width=width)
        self.frameErrors.grid(row=6, column=1, rowspan=3)

        self.widthLeft = 30
        self.widthCenter = 15
        self.widthInput = 4

        self.staticObsGiven = False  # By default put as not having static observations

        # Present filename of DBN being used
        self.showDBN = ElemTwoPresent(self.framePredictSpecific,
                                      "sdtDBN being used: ",
                                      "No file yet selected", self.widthLeft,
                                      1, 1)

        # Create a Tkinter variable for available ids
        self.idTab3TkVar = StringVar(self.framePredictSpecific)
        self.idTab3Choices = ['Inference observations not given!']

        self.idTab3 = ElemTwoSelect(self.framePredictSpecific, "Desired id: ",
                                    self.widthLeft, self.idTab3TkVar,
                                    self.idTab3Choices, self.idTab3Choices[0],
                                    2, 1)

        # Create a Tkinter variable for available attributes
        self.attTab3TkVar = StringVar(self.framePredictSpecific)
        self.attTab3Choices = ['There is not an sdtDBN learned!']

        self.attTab3 = ElemTwoSelect(self.framePredictSpecific,
                                     "Desired attribute: ", self.widthLeft,
                                     self.attTab3TkVar, self.attTab3Choices,
                                     self.attTab3Choices[0], 3, 1)

        # Create a Tkinter variable for timesteps
        self.timestepTab3 = ElemTwoInput(self.framePredictSpecific,
                                         "Desired timestep: ", self.widthLeft,
                                         self.widthInput, 4, 1, 1)

        self.makeInfTab3 = ttk.Button(self.framePredictSpecific,
                                      text="Make inference",
                                      command=self.onSubmit)
        self.makeInfTab3.grid(row=5,
                              column=1,
                              columnspan=2,
                              sticky=N + S + E + W)

    def onSubmit(self):

        if (self.checkArgs() == False):
            return

        desiredID = self.idTab3TkVar.get()

        auxDynInfFilename = 'auxDynInf.csv'
        auxInfVarFilename = 'auxInfVar.csv'

        dynInfFile = open(self.dynObsInfFilename)
        reader = csv.reader(dynInfFile, delimiter=',')
        firstLine = next(reader)
        desiredLineDyn = []
        for row in reader:
            if (row[0] == desiredID):
                desiredLineDyn = row
                break
        dynInfFile.close()

        with open(auxDynInfFilename, 'w', newline='') as file:
            writer = csv.writer(file)
            writer.writerow(firstLine)
            writer.writerow(desiredLineDyn)

        if (self.staticObsGiven == True):
            auxStaticInfFilename = 'auxStaticInf.csv'
            staticInfFile = open(self.staticObsInfFilename)
            reader = csv.reader(staticInfFile, delimiter=',')
            firstLine = next(reader)
            desiredLineStatic = []
            for row in reader:
                if (row[0] == desiredID):
                    desiredLineStatic = row
                    break
            staticInfFile.close()

            with open(auxStaticInfFilename, 'w', newline='') as file:
                writer = csv.writer(file)
                writer.writerow(firstLine)
                writer.writerow(desiredLineStatic)

        infAttInList = [self.attTab3TkVar.get(), self.timestepTab3.entry.get()]

        with open(auxInfVarFilename, 'w', newline='') as file:
            writer = csv.writer(file)
            writer.writerow(infAttInList)

        if (self.staticObsGiven == True):
            infCmdArgs = [
                '-obs', auxDynInfFilename, '-obsStatic', auxStaticInfFilename,
                '-inf', auxInfVarFilename, '-infFmt', 'distrib'
            ]
        else:
            infCmdArgs = [
                '-obs', auxDynInfFilename, '-inf', auxInfVarFilename,
                '-infFmt', 'distrib'
            ]

        p = subprocess.run([
            'java', '-jar', 'sdtDBN_v0_0_1.jar', '-fromFile',
            self.showDBN.messageRight
        ] + infCmdArgs,
                           stdout=subprocess.PIPE,
                           stderr=subprocess.PIPE,
                           stdin=subprocess.PIPE,
                           encoding='utf-8')

        self.infResult = p.stdout

        os.remove(auxDynInfFilename)
        os.remove(auxInfVarFilename)
        if (self.staticObsGiven == True):
            os.remove(auxStaticInfFilename)

        for widget in self.frameInfResults.winfo_children():
            widget.destroy()

        textInfo = scrolledtext.ScrolledText(self.frameInfResults,
                                             height=33,
                                             width=45)
        textInfo.grid(row=1, column=4, rowspan=33, padx=7)
        textInfo.insert(END, self.infResult)

        return

    def changeAttOptions(self, newOptionsList):

        self.attTab3.destroy()

        self.attTab3Choices = newOptionsList

        self.attTab3 = ElemTwoSelect(self.framePredictSpecific,
                                     "Desired attribute: ", self.widthLeft,
                                     self.attTab3TkVar, self.attTab3Choices,
                                     self.attTab3Choices[0], 3, 1)

    def changeIdsOptions(self, newOptionsList):

        self.idTab3.destroy()

        self.idTab3Choices = newOptionsList

        self.idTab3 = ElemTwoSelect(self.framePredictSpecific, "Desired id: ",
                                    self.widthLeft, self.idTab3TkVar,
                                    self.idTab3Choices, self.idTab3Choices[0],
                                    2, 1)

    def getInfSpecs(self, dynObsInfFilename, staticObsInfFilename):

        self.dynObsInfFilename = dynObsInfFilename
        self.staticObsInfFilename = staticObsInfFilename

        dynInfFile = open(self.dynObsInfFilename)
        reader = csv.reader(dynInfFile, delimiter=',')
        next(reader)  # skip 1st line
        idList = []
        for row in reader:
            idList.append(row[0])
        dynInfFile.close()

        self.changeIdsOptions(idList)

        if (staticObsInfFilename == "Not yet selected!"
                or staticObsInfFilename == ''):
            self.staticObsGiven = False
        else:
            self.staticObsGiven = True

        return

    def setDBNFile(self, dbnFilename):
        self.showDBN.destroy()
        self.showDBN = ElemTwoPresent(self.framePredictSpecific,
                                      "sdtDBN being used: ", dbnFilename,
                                      self.widthLeft, 1, 1)
        return

    def checkArgs(self):
        for widget in self.frameErrors.winfo_children():
            widget.destroy()

        if (self.showDBN.messageRight == "No file yet selected"):
            printInfo = ttk.Label(self.frameErrors,
                                  text="No DBN was selected!",
                                  style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False

        if (self.idTab3TkVar.get() == 'Inference observations not given!'):
            printInfo = ttk.Label(self.frameErrors,
                                  text="Inference observations not given!",
                                  style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False

        if (self.attTab3TkVar.get() == 'There is not an sdtDBN learned!'):
            printInfo = ttk.Label(self.frameErrors,
                                  text="No attribute was specified!",
                                  style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False

        if (self.is_integer(self.timestepTab3.entry.get()) == False):
            printInfo = ttk.Label(self.frameErrors,
                                  text="Timestep is not an integer!",
                                  style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False

        if (int(self.timestepTab3.entry.get()) < 1):
            printInfo = ttk.Label(self.frameErrors,
                                  text="Timestep must be > 0",
                                  style="notok.TLabel")
            printInfo.grid(row=1, column=1, columnspan=2)
            return False

        return True

    def is_integer(self, value: str) -> bool:
        try:
            int(value)
            return True
        except ValueError:
            return False