示例#1
0
    def controlFun(self):
        "processes selected files, clears report, shows results in a report"

        # progressbar
        if len(self.root.fileStorage.arenafiles) > 1:
            self.stoppedProcessing = False
            self.progressWindow = ProgressWindow(self, len(self.root.fileStorage.arenafiles),
                                                 text = "controlled")

        # initialization
        controls = self.controlFrame.controlsGet() # selected controls
        self.controlReport.clear(self)  # clears report
        self.controlReport.addControls(controls) # adds selected controls to ControlReport

        self.problemOccured = False
        # processing      
        for file in self.root.fileStorage.arenafiles:
            try:
                if file in self.root.fileStorage.pairedfiles:
                    cm = CM(file, nameR = self.root.fileStorage.pairedfiles[file])
                else:
                    cm = CM(file, nameR = "auto")
            except Exception:
                self.problemOccured = True
                for control in controls:
                    self.controlReport.addFile((control[0], (file, "Failed to load!", "Problem",
                                                             9999999)))
            else:
                for control in controls:
                    try:
                        assessment = self.assessImportance(cm = cm, control = control, file = file)
                    except Exception:
                        self.problemOccured = True
                        assessment = (control[0], (file, "Failed to compute!", "Problem",
                                                   9999998))
                    self.controlReport.addFile(assessment)
                     
            if len(self.root.fileStorage.arenafiles) > 1:
                if self.stoppedProcessing:
                    return
                else:
                    self.progressWindow.addOne()
                    
        self.controlReport.updateTree(self)

        # progressbar and status
        if len(self.root.fileStorage.arenafiles) > 1:
            self.progressWindow.destroy()
            if self.problemOccured:
                self.status.set("Files were not processed successfully!")
                self.bell()
            else:
                self.status.set("Files were processed successfully.")
        else:
            if self.problemOccured:
                self.status.set("File was not processed successfully!")
                self.bell()
            else:
                self.status.set("File was processed successfully.")          
示例#2
0
    def addReflections(self, file, points):
        "add reflection points from a set in the 'points' argument for a file in the argument"
        if file in self.addedReflections:
            self.addedReflections[file].update(points)
        else:
            self.addedReflections[file] = points

        if file in self.reflections:
            self.reflections[file].update(points)
        else:
            if file in self.pairedfiles:
                cm = CM(file, nameR = self.pairedfiles[file])
            else:
                cm = CM(file, nameR = "auto")
            reflections = cm.findReflections(results = "indices")
            self.reflections[file] = set(reflections[0]) | set(reflections[1]) | points
示例#3
0
    def saveImages(self):
        "saves images for selected files"
        files = self._returnSelectedFiles()

        if len(files) > 1:
            progress = ProgressWindow(self, len(files), text = "saved")
        elif len(files) == 1:
            self.root.config(cursor = "wait")
            self.root.update()
        elif len(files) == 0:
            self.status.set("No file selected.")
            self.bell()
            return

        problems = 0
        for filename in files:
            try:
                if filename in self.fileStorage.pairedfiles:
                    cm = CM(filename, nameR = self.fileStorage.pairedfiles[filename])
                else:
                    cm = CM(filename, nameR = "auto")
                if self.removeReflectionsVar.get():
                    cm.removeReflections(points = self.fileStorage.reflections.get(filename, None))
                self.saveOneImage(cm, filename)               
            except Exception:
                problems += 0

            if len(files) > 1:
                progress.addOne()

        if len(files) > 1:
            progress.destroy()
            if problems > 1:
                self.status.set("{} problems occured!".format(problems))
                self.bell()
            elif problems == 1:
                self.status.set("One problem occured!")
                self.bell()
            else:
                self.status.set("Images were saved.")
        else:
            if problems:
                self.status.set("A problem occured!")
                self.bell()
            else:
                self.status.set("Image was saved.")
            self.root.config(cursor = "")
示例#4
0
    def postear_en_dlm(self, fecha, diario, secciones):
        resultados = Resultados()
        visu = Visualizador()
        tolkien = Escritor()

        if '-' in secciones:
            secciones = secciones.split('-')
        else:
            secciones = [secciones]

        freqs = resultados.frecuencias(fecha=fecha,
                                       diario=diario,
                                       secciones=secciones,
                                       top=20,
                                       verbos=False)
        if not bool(freqs):
            return

        path_imagen = os.getcwd() + '/imagenes/' + diario + '-todo.png'
        visu.nube(path_imagen, freqs, con_espacios=False)
        texto = tolkien.tweet_tendencias(freqs=freqs,
                                         fecha=fecha,
                                         diario=diario)

        textos_e_imagenes = [{'media': [path_imagen], 'texto': texto}]

        for s in secciones:
            freqs = resultados.frecuencias(fecha=fecha,
                                           diario=diario,
                                           secciones=s,
                                           top=20,
                                           verbos=False)
            if not bool(freqs):
                continue

            path_imagen = os.getcwd(
            ) + '/imagenes/' + diario + '-' + s + '.png'
            visu.nube(path_imagen, freqs, con_espacios=False)
            texto = tolkien.tweet_tendencias(freqs=freqs,
                                             fecha=fecha,
                                             diario=diario,
                                             secciones=s)

            textos_e_imagenes.append({'media': [path_imagen], 'texto': texto})

        cm = CM()
        cm.twittear_hilo('dicenlosmedios', textos_e_imagenes)
示例#5
0
    def addReflections(self, file, points):
        "add reflection points from a set in the 'points' argument for a file in the argument"
        if file in self.addedReflections:
            self.addedReflections[file].update(points)
        else:
            self.addedReflections[file] = points

        if file in self.reflections:
            self.reflections[file].update(points)
        else:
            if file in self.pairedfiles:
                cm = CM(file, nameR=self.pairedfiles[file])
            else:
                cm = CM(file, nameR="auto")
            reflections = cm.findReflections(results="indices")
            self.reflections[file] = set(reflections[0]) | set(
                reflections[1]) | points
示例#6
0
def main():
    from cm import CM
    import os
    import os.path
    svg = SVG(300, 300)
    cm = CM(os.path.join(os.getcwd(), "TestingFiles", "14rNO465_Arena.dat"))
    svg.drawAAPA(cm, "room", scale=3, boundary=True, sector=True, shocks=True)
    svg.save("test.svg")
    os.startfile("test.svg")
示例#7
0
    def processFun(self):
        "processes chosen files and saves the results in the save-to file"
        # files to be processed
        self.filesToProcess = [file for file in self.fileStorage if \
                               self.optionFrame.processFile(file)]

        # checking for mistakes
        try:
            self.processCheck()
        except Exception as e:
            self.bell()
            self.status.set(e)
            return

        # progressWindow
        if len(self.filesToProcess) > 1:
            self.stoppedProcessing = False
            self.progressWindow = ProgressWindow(self,
                                                 len(self.filesToProcess))

        # selected methods
        methods = []
        for method in Parameters().parameters:
            if eval("self.parametersF.%sVar.get()" %
                    (method[0].replace(" ", ""))):
                methods.append(method[0])

        # initializations
        output = self.saveToFrame.saveToVar.get()
        startTime = float(self.timeFrame.startTimeVar.get())
        time = float(self.timeFrame.timeVar.get())
        separator = optionGet("ResultSeparator", ",", "str")
        batchTime = self.selectedBatchTime if self.useBatchTimeVar.get(
        ) else None
        self.log = Log(methods,
                       startTime,
                       time,
                       self.filesToProcess,
                       self.fileStorage,
                       self.optionFrame.removeReflectionsWhere.get(),
                       output,
                       batchTime=batchTime)
        self.someProblem = False
        developer = optionGet("Developer", False, 'bool')

        # results header
        if self.useBatchTimeVar.get():
            info = [
                method[0] for method in Parameters().parameters
                if method[2] == "info"
            ]
            results = ["File"]
            for method in methods:
                if method in info:
                    results.append(method)
                else:
                    results.extend([
                        method + " ({}-{})".format(start, end)
                        for start, end in self.selectedBatchTime
                    ])
            results = separator.join(results)
        else:
            results = separator.join(["File"] + methods)
        if self.optionFrame.saveTags.get():
            results += separator + "Tags"
        if self.optionFrame.saveComments.get():
            results += separator + "Comment"

        # computing of results
        for file in self.filesToProcess:
            # loading of cm object
            if methods:
                try:
                    if file in self.fileStorage.pairedfiles:
                        cm = CM(file, nameR=self.fileStorage.pairedfiles[file])
                    else:
                        cm = CM(file, nameR="auto")

                    if self.optionFrame.removeReflections(file):
                        cm.removeReflections(
                            points=self.fileStorage.reflections.get(
                                file, None))
                except Exception as e:
                    if developer:
                        print(e)
                    filename = returnName(filename=file,
                                          allFiles=self.fileStorage.arenafiles)
                    results += "\n" + filename + "{}NA".format(
                        separator) * len(methods)
                    self.log.failedToLoad.append(file)
                    self.someProblem = True
                    continue

            result = []
            for method in Parameters().parameters:
                if method[0] in methods:
                    if self.useBatchTimeVar.get():
                        for startTime, time in self.selectedBatchTime:
                            try:
                                if method[2] == "custom":
                                    exec("from Stuff.Parameters import {}".
                                         format(method[5]))
                                result.append(eval(method[1]))
                                if method[2] == "info":
                                    break
                            except Exception as e:
                                if developer:
                                    print(e)
                                result.append("NA")
                                self.log.methodProblems[method[0]].append(file)
                                self.someProblem = True
                    else:
                        try:
                            if method[2] == "custom":
                                exec("from Stuff.Parameters import {}".format(
                                    method[5]))
                            result.append(eval(method[1]))
                        except Exception as e:
                            if developer:
                                print(e)
                            result.append("NA")
                            self.log.methodProblems[method[0]].append(file)
                            self.someProblem = True

            result = separator.join(map(str, result))
            if methods:
                result = separator + result
            filename = returnName(filename=file,
                                  allFiles=self.fileStorage.arenafiles)
            results += "\n" + filename + result

            # tag inclusion in results
            if self.optionFrame.saveTags.get():
                if file in self.fileStorage.tagged:
                    results += separator + "1"
                else:
                    results += separator + "0"

            # comment inclusion in results
            if self.optionFrame.saveComments.get():
                results += separator + self.fileStorage.comments[file]

            # progress window update
            if len(self.filesToProcess) > 1:
                if self.stoppedProcessing:
                    writeResults(output, results)
                    self.log.stopped = file
                    self.log.writeLog()
                    self.status.set("Processing stopped")
                    return
                else:
                    self.progressWindow.addOne()

        # results and log writing, ending of processing
        writeResults(output, results)
        self.log.writeLog()
        self._setStatusEndProgressWindow()

        if self.someProblem:
            ProcessingProblemDialog(self, self.log.filename)
        elif self.optionFrame.showResults.get():
            os.startfile(output)
示例#8
0
class ShowTracks(Toplevel):
    "displays Canvases with trajectory of a selected track"

    def __init__(self, root, tracks, nameA, controlled=False):
        super().__init__(root)
        self["padx"] = 4
        self["pady"] = 4
        self.title("Track images")
        self.grab_set()
        self.focus_set()
        self.resizable(FALSE, FALSE)
        self.geometry("+8+40")

        self.controlled = controlled
        self.tracks = tracks
        self.root = root
        self.fileStorage = self.root.fileStorage
        self.index = self.tracks.index(nameA)
        self.enabledManualReflectionsRemoval = False
        self.size = 6  # size of a mouse circle for reflection removal

        # number of canvases
        self.columns = ((self.winfo_screenwidth() - 515) // 290) or 1
        self.rows = ((self.winfo_screenheight() - 50) // 290) or 1
        self.numberOfCanvases = self.columns * self.rows

        # canvases
        self.canvasFrame = ttk.Frame(self)
        self.canvasFrame.grid(column=0, row=0)
        self.canvasFrame.root = self

        for canvasNumber in range(0, self.numberOfCanvases):
            exec("self.canvas{} = TrackCanvas(self.canvasFrame)".format(
                canvasNumber))
            exec("self.canvas{}.grid(column = {}, row = {}, padx = 2, pady = 2)"\
                 .format(canvasNumber, canvasNumber % self.columns, canvasNumber // self.columns))

        # fileTree (the entire right part)
        self.fileTree = FileTree(self,
                                 files=self.tracks,
                                 controlled=controlled)
        self.fileTree.grid(column=1,
                           row=0,
                           sticky=(N, S, E, W),
                           padx=3,
                           pady=3)

        # initialization of selected track
        self.drawTracks(new=True)

        # event binding
        self.bind("<Up>", lambda e: self.fileTree.previousFun())
        self.bind("<Left>", lambda e: self.fileTree.previousFun())
        self.bind("<Down>", lambda e: self.fileTree.nextFun())
        self.bind("<Right>", lambda e: self.fileTree.nextFun())
        self.bind("<Return>", lambda e: self.drawTracks(new=False))
        self.bind("<MouseWheel>", lambda e: self.mouseWheel(e))

        self.canvasFrame.bind("<3>", lambda e: self.popUp(e))

        # on exit
        self.protocol("WM_DELETE_WINDOW", lambda: self.closeFun())

    def popUp(self, event, canvas=None):
        "makes pop-up menu with options related to manual reflection removal"
        menu = Menu(self, tearoff=0)
        if not self.enabledManualReflectionsRemoval:
            label = "Enable manual reflection removal"
        else:
            label = "Disable manual reflection removal"
        menu.add_command(label=label,
                         command=self.toggleManualReflectionRemoval)
        menu.add_separator()
        if canvas:
            if canvas.addedReflections:
                menu.add_command(label="Undo manual changes",
                                 command=canvas.undoChanges)
            else:
                menu.add_command(label="Undo manual changes",
                                 command=canvas.undoChanges,
                                 state="disabled")
        for canvasNumber in range(0, self.numberOfCanvases):
            if eval("self.canvas{}.addedReflections".format(canvasNumber)):
                menu.add_command(label="Undo all manual changes",
                                 command=self.undoChanges)
                break
        else:
            menu.add_command(label="Undo all manual changes",
                             command=self.undoChanges,
                             state="disabled")
        menu.post(event.x_root, event.y_root)

    def undoChanges(self):
        "removes all manually added reflections"
        self._initializeCM(self.cm.nameA)
        reflections = self.cm.findReflections(results="indices")
        self.fileStorage.reflections[self.cm.nameA] = set(reflections[0] +
                                                          reflections[1])
        self.fileStorage.addedReflections[self.cm.nameA] = set()
        self.drawTracks(new=True)

    def mouseWheel(self, event):
        "changes size of a mouse circle used for reflection removal"
        if self.enabledManualReflectionsRemoval:
            if event.num == 5 or event.delta == -120:
                self.size -= 1
            elif event.num == 4 or event.delta == 120:
                self.size += 1

    def toggleManualReflectionRemoval(self):
        "enables or disables manual reflection removal"
        if not self.fileTree.removeReflectionsVar.get():
            self.fileTree.removeReflectionsVar.set(True)
            self.fileTree.toggleReflections()
        self.enabledManualReflectionsRemoval = not self.enabledManualReflectionsRemoval
        for canvasNumber in range(0, self.numberOfCanvases):
            exec(
                "self.canvas{}.toggleReflectionRemoval()".format(canvasNumber))

    def setTime(self):
        "redraws tracks when time is changed"
        self.drawTracks(new=False)

    def closeFun(self):
        "closes the window"
        if self.controlled:
            self.root.checkProcessing()
        else:
            self.root.root.checkProcessing()

        self.destroy()

    def _initializeCM(self, file):
        "initializes self.cm"
        if file in self.fileStorage.pairedfiles:
            self.cm = CM(file, nameR=self.fileStorage.pairedfiles[file])
        else:
            self.cm = CM(file, nameR="auto")

    def drawTracks(self, new=True):
        "draws selected track"
        if new:
            file = self.fileTree.files[self.fileTree.index]
            self._initializeCM(file)

        if self.fileTree.removeReflectionsVar.get():
            if self.cm.nameA not in self.fileStorage.reflections:
                reflections = self.cm.findReflections(results="indices")
                self.fileStorage.reflections[self.cm.nameA] = set(
                    reflections[0] + reflections[1])

            self.cm.removeReflections(
                points=self.fileStorage.reflections[self.cm.nameA],
                deleteSame=True)

        d = self.cm.getComputedDiameter()

        stopTime = int(self.fileTree.timeFrame.timeVar.get()) * 60000
        startTime = int(self.fileTree.timeFrame.startTimeVar.get()) * 60000
        timeRange = stopTime - startTime

        # draws the track divided in equal time intervals
        for canvasNumber in range(0, self.numberOfCanvases):
            rangeBegin = startTime + timeRange * canvasNumber / self.numberOfCanvases
            rangeEnd = startTime + timeRange * (canvasNumber +
                                                1) / self.numberOfCanvases
            if self.fileTree.frameVar.get() == "arena":
                selectData = [line[7:9] + [line[0]] for line in self.cm.data if \
                              rangeBegin <= line[1] <= rangeEnd]
            if self.fileTree.frameVar.get() == "room":
                selectData = [line[2:4] + [line[0]] for line in self.cm.data if \
                              rangeBegin <= line[1] <= rangeEnd]
            exec("self.canvas{}.drawTrack(d, data = selectData)".format(
                canvasNumber))
示例#9
0
 def _initializeCM(self, file):
     "initializes self.cm"
     if file in self.fileStorage.pairedfiles:
         self.cm = CM(file, nameR=self.fileStorage.pairedfiles[file])
     else:
         self.cm = CM(file, nameR="auto")
示例#10
0
class ShowTracks(Toplevel):
    "displays Canvases with trajectory of a selected track"
    def __init__(self, root, tracks, nameA, time = "auto", controlled = False):
        super().__init__(root)
        self["padx"] = 4
        self["pady"] = 4
        self.title("Track images")
        self.grab_set()
        self.focus_set()
        self.resizable(FALSE, FALSE)
        self.geometry("+8+40")

        self.controlled = controlled
        self.tracks = tracks
        self.root = root
        self.fileStorage = self.root.fileStorage
        self.index = self.tracks.index(nameA)
        self.enabledManualReflectionsRemoval = False
        self.size = 6 # size of a mouse circle for reflection removal


        # number of canvases
        self.columns = ((self.winfo_screenwidth() - 515) // 290) or 1
        self.rows = ((self.winfo_screenheight() - 50 )// 290) or 1
        self.numberOfCanvases = self.columns * self.rows
        
        # canvases
        self.canvasFrame = ttk.Frame(self)
        self.canvasFrame.grid(column = 0, row = 0)
        self.canvasFrame.root = self

        for canvasNumber in range(0, self.numberOfCanvases):
            exec("self.canvas{} = TrackCanvas(self.canvasFrame)".format(canvasNumber))
            exec("self.canvas{}.grid(column = {}, row = {}, padx = 2, pady = 2)"\
                 .format(canvasNumber, canvasNumber % self.columns, canvasNumber // self.columns))

        # fileTree (the entire right part)
        self.fileTree = FileTree(self, files = self.tracks, time = time, controlled = controlled)
        self.fileTree.grid(column = 1, row = 0, sticky = (N, S, E, W), padx = 3, pady = 3) 
        
        # initialization of selected track
        self.drawTracks(new = True)

        # event binding
        self.bind("<Up>", lambda e: self.fileTree.previousFun())
        self.bind("<Left>", lambda e: self.fileTree.previousFun())
        self.bind("<Down>", lambda e: self.fileTree.nextFun())
        self.bind("<Right>", lambda e: self.fileTree.nextFun())
        self.bind("<Return>", lambda e: self.drawTracks(new = False))
        self.bind("<MouseWheel>", lambda e: self.mouseWheel(e))

        self.canvasFrame.bind("<3>", lambda e: self.popUp(e))

        # on exit
        self.protocol("WM_DELETE_WINDOW", lambda: self.closeFun())


    def popUp(self, event, canvas = None):
        "makes pop-up menu with options related to manual reflection removal"
        menu = Menu(self, tearoff = 0)
        if not self.enabledManualReflectionsRemoval:
            label = "Enable manual reflection removal"
        else:
            label = "Disable manual reflection removal"
        menu.add_command(label = label, command = self.toggleManualReflectionRemoval)
        if canvas:
            if canvas.addedReflections:
                menu.add_command(label = "Undo manual changes", command = canvas.undoChanges)
            else:
                menu.add_command(label = "Undo manual changes", command = canvas.undoChanges,
                                 state = "disabled")
        for canvasNumber in range(0, self.numberOfCanvases):
            if eval("self.canvas{}.addedReflections".format(canvasNumber)):
                menu.add_command(label = "Undo all manual changes", command = self.undoChanges)
                break
        else:
            menu.add_command(label = "Undo all manual changes", command = self.undoChanges,
                             state = "disabled")
        menu.post(event.x_root, event.y_root)


    def undoChanges(self):
        "removes all manually added reflections"
        self._initializeCM(self.cm.nameA)
        reflections = self.cm.findReflections(results = "indices")
        self.fileStorage.reflections[self.cm.nameA] = set(reflections[0] + reflections[1])
        self.fileStorage.addedReflections[self.cm.nameA] = set()
        self.drawTracks(new = True)


    def mouseWheel(self, event):
        "changes size of a mouse circle used for reflection removal"
        if self.enabledManualReflectionsRemoval:
            if event.num == 5 or event.delta == - 120:
                self.size -= 1
            elif event.num == 4 or event.delta == 120:
                self.size += 1


    def toggleManualReflectionRemoval(self):
        "enables or disables manual reflection removal"
        self.enabledManualReflectionsRemoval = not self.enabledManualReflectionsRemoval
        for canvasNumber in range(0, self.numberOfCanvases):
            exec("self.canvas{}.toggleReflectionRemoval()".format(canvasNumber))
            

    def setTime(self):
        "redraws tracks when time is changed"
        self.drawTracks(new = False)


    def closeFun(self):
        "closes the window"
        if self.controlled:
            self.root.checkProcessing()
        else:
            self.root.root.checkProcessing()

        self.destroy()


    def _initializeCM(self, file):
        "initializes self.cm"
        if file in self.fileStorage.pairedfiles:
            self.cm = CM(file, nameR = self.fileStorage.pairedfiles[file])
        else:
            self.cm = CM(file, nameR = "auto")                     
        
                        
    def drawTracks(self, new = True):
        "draws selected track"
        if new:
            file = self.fileTree.files[self.fileTree.index]
            self._initializeCM(file)
            
        if self.fileTree.removeReflectionsVar.get():
            if self.cm.nameA not in self.fileStorage.reflections:
                reflections = self.cm.findReflections(results = "indices")
                self.fileStorage.reflections[self.cm.nameA] = set(reflections[0] + reflections[1])
                
            self.cm.removeReflections(points = self.fileStorage.reflections[self.cm.nameA],
                                      deleteSame = True)               
            
        d = self.cm.getComputedDiameter()
    
        stopTime = int(self.fileTree.timeFrame.timeVar.get()) * 60000
        startTime = int(self.fileTree.timeFrame.startTimeVar.get()) * 60000
        timeRange = stopTime - startTime
       
        # draws the track divided in equal time intervals 
        for canvasNumber in range(0, self.numberOfCanvases):
            rangeBegin = startTime + timeRange * canvasNumber / self.numberOfCanvases
            rangeEnd = startTime + timeRange * (canvasNumber + 1) / self.numberOfCanvases
            if self.fileTree.frameVar.get() == "arena":
                selectData = [line[7:9] + [line[0]] for line in self.cm.data if \
                              rangeBegin <= line[1] <= rangeEnd]
            if self.fileTree.frameVar.get() == "room":
                selectData = [line[2:4] + [line[0]] for line in self.cm.data if \
                              rangeBegin <= line[1] <= rangeEnd]
            exec("self.canvas{}.drawTrack(d, data = selectData)".format(canvasNumber))
示例#11
0
    def postear_contador(self, fecha, concepto):
        resultados = Resultados()
        tolkien = Escritor()

        terminos = {
            'cfk': [
                'CFK', 'Kirchner', 'Cristina', 'Cristina Kirchner',
                'Cristina Fernández', 'Cristina Fernández de Kirchner',
                'Cristina Fernández De Kirchner', 'Vicepresidenta'
            ],
            'dolar': ['dólar', 'US$', 'Dólar'],
            'corrupcion': ['corrupción', 'corrupto', 'Corrupción'],
            'larreta': [
                'Larreta', 'Horacio Larreta', 'Rodríguez Larreta',
                'Horacio Rodríguez', 'Horacio Rodríguez Larreta'
            ],
            'venezuela': [
                'Venezuela', 'Nicolás Maduro', 'Maduro', 'Chávez',
                'Hugo Chávez', 'Chavez', 'Hugo Chavez'
            ],
            'inseguridad': ['inseguridad', 'inseguro'],
            'miedo':
            ['miedo', 'muerte', 'muerto', 'muerta', 'asesinato', 'horror']
        }

        tabla = {}
        for medio in [
                medio for medio in tolkien.hashtags.keys()
                if medio != 'casarosada'
        ]:

            freqs = resultados.frecuencias(fecha=fecha,
                                           diario=medio,
                                           verbos=False,
                                           top=2000)

            if not bool(freqs):
                continue

            total = sum(
                [f[1] for f in freqs.items() if f[0] in terminos[concepto]])

            if total == 0:
                continue

            tabla[medio] = total

        if len(tabla) == 0:
            return

        ordenada = {
            k: v
            for k, v in sorted(
                tabla.items(), key=lambda item: item[1], reverse=True)
        }
        texto = tolkien.tweet_contador(concepto=concepto,
                                       freqs=ordenada,
                                       fecha=fecha)

        cm = CM()
        cm.twittear_texto('dicenlosmedios', texto)
示例#12
0
    def postear_en_discursoshistoricos(self, cuenta, fecha=None):
        resultados = Resultados()
        kiosco = Kiosco()
        visu = Visualizador()
        tolkien = Escritor()

        presidente, subfijopng, hashtag = self.info_cuenta(cuenta)

        # recupero frecuencias al azar
        freqs = resultados.bd.frecuencias_discursos.aggregate([{
            '$match': {
                'presidente': presidente
            }
        }, {
            '$sample': {
                'size': 1
            }
        }])

        if not bool(freqs):
            return

        cm = CM()
        for freq in freqs:
            # recupero texto del discurso
            discurso = kiosco.noticias(diario='casarosada', url=freq['url'])[0]

            # armo tweet con el discurso en imagenes
            texto = " ".join(
                re.split("\s+", discurso['texto'], flags=re.UNICODE))
            paths_imagenes = visu.texto_en_imagenes(
                texto, 'calibri.ttf', 17, 800, 600,
                os.getcwd() + "/imagenes/intro" + subfijopng)
            # TODO: la fecha esta MAL. pasar de date a string.
            tw_intro = {
                'texto':
                "Discurso del " + tolkien.separar_fecha(fecha=freq['fecha']) +
                " de " + hashtag + ". Hilo 👇",
                'media':
                paths_imagenes
            }

            # armo textos del tweet
            txt_sustantivos = tolkien.texto_tweet_sustantivos_discurso(
                freq['sustxt'])
            txt_adjetivos = tolkien.texto_tweet_adjetivos_discurso(
                freq['adjtxt'])

            # armo tweet con top 15 de terminos
            path_imagen_sustantivos = os.getcwd(
            ) + '/imagenes/sustantivos_discurso' + subfijopng + '.png'
            etiquetas_sustantivos = [
                nombre for nombre, m in freq['sustxt'].items()
            ][:15]
            data_sustantivos = [m for nombre, m in freq['sustxt'].items()][:15]
            visu.lollipop(path=path_imagen_sustantivos,
                          colormap=visu.cmap_del_dia(),
                          titulo="Frecuencia de sustantivos",
                          etiquetas=etiquetas_sustantivos,
                          unidad="cantidad de apariciones",
                          valfmt="{x:.0f}",
                          data=data_sustantivos)
            tw_terminos = {
                'texto': txt_sustantivos,
                'media': [path_imagen_sustantivos]
            }

            # armo tweet con top 15 de verbos
            path_imagen_adjetivos = os.getcwd(
            ) + '/imagenes/adjetivos_discurso' + subfijopng + '.png'
            etiquetas_adjetivos = [
                nombre for nombre, m in freq['adjtxt'].items()
            ][:15]
            data_adjetivos = [m for nombre, m in freq['adjtxt'].items()][:15]
            visu.lollipop(path=path_imagen_adjetivos,
                          colormap=visu.cmap_del_dia(),
                          titulo="Frecuencia de adjetivos",
                          etiquetas=etiquetas_adjetivos,
                          unidad="cantidad de apariciones",
                          valfmt="{x:.0f}",
                          data=data_adjetivos)
            tw_verbos = {
                'texto': txt_adjetivos,
                'media': [path_imagen_adjetivos]
            }

            # el CM twittea
            cm.twittear_hilo(cuenta, [tw_intro, tw_terminos, tw_verbos])
示例#13
0
    def postear_en_discursosdeaf(self, fecha):
        resultados = Resultados()
        kiosco = Kiosco()
        visu = Visualizador()
        tolkien = Escritor()

        # recupero frecuencias del discurso
        freqs = resultados.bd.frecuencias_discursos.aggregate([{
            '$match': {
                'presidente': 'alberto',
                'fecha': fecha
            }
        }])

        if not bool(freqs):
            return

        cm = CM()
        for freq in freqs:

            # recupero texto del discurso
            discurso = kiosco.noticias(diario='casarosada', url=freq['url'])[0]

            # armo tweet con el discurso en imagenes
            texto = " ".join(
                re.split("\s+", discurso['texto'], flags=re.UNICODE))
            paths_imagenes = visu.texto_en_imagenes(
                texto, 'calibri.ttf', 17, 800, 600,
                os.getcwd() + "/imagenes/introaf")
            tw_intro = {
                'texto':
                "Discurso del " + tolkien.separar_fecha(fecha=freq['fecha']) +
                " de #AlbertoFernández. Hilo 👇",
                'media':
                paths_imagenes
            }

            # armo textos del tweet
            txt_sustantivos = tolkien.texto_tweet_sustantivos_discurso(
                freq['sustxt'])
            txt_adjetivos = tolkien.texto_tweet_adjetivos_discurso(
                freq['adjtxt'])

            # armo tweet con top 15 de terminos
            path_imagen_sustantivos = os.getcwd(
            ) + '/imagenes/sustantivos_discursoaf.png'
            etiquetas_sustantivos = [
                nombre for nombre, m in freq['sustxt'].items()
            ][:15]
            data_sustantivos = [m for nombre, m in freq['sustxt'].items()][:15]
            visu.lollipop(path=path_imagen_sustantivos,
                          colormap=visu.cmap_del_dia(),
                          titulo="Frecuencia de sustantivos",
                          etiquetas=etiquetas_sustantivos,
                          unidad="cantidad de apariciones",
                          valfmt="{x:.0f}",
                          data=data_sustantivos)
            tw_terminos = {
                'texto': txt_sustantivos,
                'media': [path_imagen_sustantivos]
            }

            # armo tweet con top 15 de verbos
            path_imagen_adjetivos = os.getcwd(
            ) + '/imagenes/adjetivos_discursoaf.png'
            etiquetas_adjetivos = [
                nombre for nombre, m in freq['adjtxt'].items()
            ][:15]
            data_adjetivos = [m for nombre, m in freq['adjtxt'].items()][:15]
            visu.lollipop(path=path_imagen_adjetivos,
                          colormap=visu.cmap_del_dia(),
                          titulo="Frecuencia de adjetivos",
                          etiquetas=etiquetas_adjetivos,
                          unidad="cantidad de apariciones",
                          valfmt="{x:.0f}",
                          data=data_adjetivos)
            tw_verbos = {
                'texto': txt_adjetivos,
                'media': [path_imagen_adjetivos]
            }

            # el CM twittea
            cm.twittear_hilo('discursosdeaf',
                             [tw_intro, tw_terminos, tw_verbos])
示例#14
0
    def processFun(self):
        "processes chosen files and saves the results in the save-to file"
        # checking for mistakes
        try:
            self.processCheck()
        except Exception as e:
            self.bell()
            self.status.set(e)
            return                   

        # files to be processed
        self.filesToProcess = [file for file in self.root.fileStorage.arenafiles if \
                               self.optionFrame.processFile(file)]

        # progressWindow and check for number of files for processing
        if len(self.filesToProcess) > 1:
            self.stoppedProcessing = False
            self.progressWindow = ProgressWindow(self, len(self.filesToProcess))
        elif len(self.filesToProcess) == 0:
            self.bell()
            self.status.set("There is no file selected for processing!")
            return

        # selected methods          
        methods = []
        for method in Parameters().parameters:
            if eval("self.parametersF.%sVar.get()" % (method[0].replace(" ", ""))):
                methods.append(method[0])
           
        output = self.saveToFrame.saveToVar.get()
        startTime = float(self.timeFrame.startTimeVar.get())
        time = float(self.timeFrame.timeVar.get())
        separator = optionGet("ResultSeparator", ",", "str")
        
        results = separator.join(["File"] + methods)
        
        if self.optionFrame.saveTags.get():
            results += separator + "Tags"

        self.log = Log(methods, startTime, time, self.filesToProcess, self.root.fileStorage,
                       self.optionFrame.removeReflectionsWhere.get(), output)
        self.someProblem = False

        developer = optionGet("Developer", False, 'bool')
        
        for file in self.filesToProcess:
            # loading of cm object
            if methods:
                try:
                    if file in self.root.fileStorage.pairedfiles:
                        cm = CM(file, nameR = self.root.fileStorage.pairedfiles[file])
                    else:
                        cm = CM(file, nameR = "auto")

                    if self.optionFrame.removeReflections(file):
                        cm.removeReflections(points = self.root.fileStorage.reflections.get(file,
                                                                                            None))
                except Exception as e:
                    if developer:
                        print(e)   
                    filename = returnName(filename = file, allFiles =
                                          self.root.fileStorage.arenafiles) 
                    results += "\n" + filename + "{}NA".format(separator) * len(methods)
                    self.log.failedToLoad.append(file)
                    self.someProblem = True
                    continue
                    
            
            result = []
            for method in Parameters().parameters:
                if method[0] in methods:
                    try:
                        if method[2] == "custom":
                            exec("from Stuff.Parameters import {}".format(method[5]))
                        result.append(eval(method[1]))
                    except Exception as e:
                        if developer:
                            print(e)   
                        result.append("NA")
                        self.log.methodProblems[method[0]].append(file)
                        self.someProblem = True


            result = separator.join(map(str, result))
            if methods:
                result = separator + result               
            filename = returnName(filename = file, allFiles = self.root.fileStorage.arenafiles)      
            results += "\n" + filename + result
            
            if self.optionFrame.saveTags.get(): # tag inclusion in results
                if file in self.root.fileStorage.tagged:
                    results += separator + "1"
                else:
                    results += separator + "0"
            
            if len(self.filesToProcess) > 1:
                if self.stoppedProcessing:
                    writeResults(output, results)
                    self.log.stopped = file
                    self.log.writeLog()
                    self.status.set("Processing stopped")
                    return
                else:
                    self.progressWindow.addOne()


        writeResults(output, results)
        self.log.writeLog()

        # change of status bar and closing of progressWindow
        if len(self.filesToProcess) > 1:
            if self.someProblem:
                self.status.set("Files were processed.")
            else:
                self.status.set("Files were processed successfully.")
            self.progressWindow.destroy()
        else:
            if self.someProblem:
                self.status.set("File was processed.")
            else:
                self.status.set("File was processed successfully.")

        # removal of files from fileStorage if selected
        if self.optionFrame.clearFilesAfterProcessing.get():
            for file in self.filesToProcess:
                self.root.fileStorage.arenafiles.remove(file)
            self.fileStorageFrame.chosenVar.set(len(self.root.fileStorage.arenafiles))
            if not self.root.fileStorage.arenafiles:
                self.fileStorageFrame.removeFiles.state(["disabled"]) 
                self.process.state(["disabled"])

        if self.someProblem:
            ProcessingProblemDialog(self, self.log.filename)
示例#15
0
    def processFun(self):
        "processes chosen files and saves the results in the save-to file"
        # checking for mistakes
        try:
            self.processCheck()
        except Exception as e:
            self.bell()
            self.status.set(e)
            return

        # files to be processed
        self.filesToProcess = [file for file in self.root.fileStorage.arenafiles if \
                               self.optionFrame.processFile(file)]

        # progressWindow and check for number of files for processing
        if len(self.filesToProcess) > 1:
            self.stoppedProcessing = False
            self.progressWindow = ProgressWindow(self,
                                                 len(self.filesToProcess))
        elif len(self.filesToProcess) == 0:
            self.bell()
            self.status.set("There is no file selected for processing!")
            return

        # selected methods
        methods = []
        for method in Parameters().parameters:
            if eval("self.parametersF.%sVar.get()" %
                    (method[0].replace(" ", ""))):
                methods.append(method[0])

        output = self.saveToFrame.saveToVar.get()
        startTime = float(self.timeFrame.startTimeVar.get())
        time = float(self.timeFrame.timeVar.get())
        separator = optionGet("ResultSeparator", ",", "str")

        results = separator.join(["File"] + methods)

        if self.optionFrame.saveTags.get():
            results += separator + "Tags"

        self.log = Log(methods, startTime, time, self.filesToProcess,
                       self.root.fileStorage,
                       self.optionFrame.removeReflectionsWhere.get(), output)
        self.someProblem = False

        developer = optionGet("Developer", False, 'bool')

        for file in self.filesToProcess:
            # loading of cm object
            if methods:
                try:
                    if file in self.root.fileStorage.pairedfiles:
                        cm = CM(file,
                                nameR=self.root.fileStorage.pairedfiles[file])
                    else:
                        cm = CM(file, nameR="auto")

                    if self.optionFrame.removeReflections(file):
                        cm.removeReflections(
                            points=self.root.fileStorage.reflections.get(
                                file, None))
                except Exception as e:
                    if developer:
                        print(e)
                    filename = returnName(
                        filename=file,
                        allFiles=self.root.fileStorage.arenafiles)
                    results += "\n" + filename + "{}NA".format(
                        separator) * len(methods)
                    self.log.failedToLoad.append(file)
                    self.someProblem = True
                    continue

            result = []
            for method in Parameters().parameters:
                if method[0] in methods:
                    try:
                        if method[2] == "custom":
                            exec("from Stuff.Parameters import {}".format(
                                method[5]))
                        result.append(eval(method[1]))
                    except Exception as e:
                        if developer:
                            print(e)
                        result.append("NA")
                        self.log.methodProblems[method[0]].append(file)
                        self.someProblem = True

            result = separator.join(map(str, result))
            if methods:
                result = separator + result
            filename = returnName(filename=file,
                                  allFiles=self.root.fileStorage.arenafiles)
            results += "\n" + filename + result

            if self.optionFrame.saveTags.get():  # tag inclusion in results
                if file in self.root.fileStorage.tagged:
                    results += separator + "1"
                else:
                    results += separator + "0"

            if len(self.filesToProcess) > 1:
                if self.stoppedProcessing:
                    writeResults(output, results)
                    self.log.stopped = file
                    self.log.writeLog()
                    self.status.set("Processing stopped")
                    return
                else:
                    self.progressWindow.addOne()

        writeResults(output, results)
        self.log.writeLog()

        # change of status bar and closing of progressWindow
        if len(self.filesToProcess) > 1:
            if self.someProblem:
                self.status.set("Files were processed.")
            else:
                self.status.set("Files were processed successfully.")
            self.progressWindow.destroy()
        else:
            if self.someProblem:
                self.status.set("File was processed.")
            else:
                self.status.set("File was processed successfully.")

        # removal of files from fileStorage if selected
        if self.optionFrame.clearFilesAfterProcessing.get():
            for file in self.filesToProcess:
                self.root.fileStorage.arenafiles.remove(file)
            self.fileStorageFrame.chosenVar.set(
                len(self.root.fileStorage.arenafiles))
            if not self.root.fileStorage.arenafiles:
                self.fileStorageFrame.removeFiles.state(["disabled"])
                self.process.state(["disabled"])

        if self.someProblem:
            ProcessingProblemDialog(self, self.log.filename)
示例#16
0
 def _initializeCM(self, file):
     "initializes self.cm"
     if file in self.fileStorage.pairedfiles:
         self.cm = CM(file, nameR = self.fileStorage.pairedfiles[file])
     else:
         self.cm = CM(file, nameR = "auto")                     
示例#17
0
class Explorer(ttk.Frame):
    "represents ''Explore' page in the main window notebook"
    def __init__(self, root):
        super().__init__(root)
        
        self["padding"] = (10, 10, 12, 12)

        self.animate = "stop"
        self.initialized = False
        self.root = root
        self.fileStorage = self.root.fileStorage

        # variables
        self.status = StringVar()
        self.curTime = DoubleVar()
        self.speed = DoubleVar()
        self.graphTypeVar = StringVar()
        self.distanceVar = StringVar()
        self.entrancesVar = StringVar()
        self.timeVar = StringVar()
        self.totDistanceVar = StringVar()
        self.totEntrancesVar = StringVar()
        self.showTrackVar = BooleanVar()
        self.removeReflectionsVar = BooleanVar()
        self.showShocksVar = BooleanVar()
        self.saveWhatVar = StringVar()
        self.saveWhichFilesVar = StringVar()

        self.curTime.set(0)
        self.speed.set(1.0)
        self.graphTypeVar.set("SpeedGraph(self)")
        self.showTrackVar.set(False)
        self.removeReflectionsVar.set(False)
        self.showShocksVar.set(True)
        self.saveWhatVar.set("both frames")
        self.saveWhichFilesVar.set("current")
        
        
        # frames
        self.controlsLF = ttk.Labelframe(self, text = "Controls")
        self.graphLF = ttk.Labelframe(self, text = "Graph")
        self.fileStorageFrame = FileStorageFrame(self, parent = "explorer")
        self.fileFrame = FileFrame(self)
        self.parametersLF = ttk.LabelFrame(self, text = "Parameters")
        self.timeLabFrame = ttk.Labelframe(self, text = "Time")
        self.timeLabFrame.root = self
        self.timeFrame = TimeFrame(self.timeLabFrame, onChange = True)
        self.arenaFrame = ttk.LabelFrame(self, text = "Arena frame")
        self.roomFrame = ttk.LabelFrame(self, text = "Room frame")
        self.speedScaleFrame = ttk.Frame(self)
        self.speedScaleFrame.rowconfigure(0, weight = 1)
        self.optionsLF = ttk.LabelFrame(self, text = "Options")
        self.saveImagesLF = ttk.LabelFrame(self, text = "Save image(s)")
        
        # canvases
        self.arenaCanv = Canvas(self.arenaFrame, background = "white", height = 300, width = 300)
        self.roomCanv = Canvas(self.roomFrame, background = "white", height = 300, width = 300)

        # buttons
        self.playBut = ttk.Button(self.controlsLF, text = "Play", command = self.playFun,
                                  state = "disabled")
        self.pauseBut = ttk.Button(self.controlsLF, text = "Pause", command = self.pauseFun,
                                   state = "disabled")
        self.stopBut = ttk.Button(self.controlsLF, text = "Stop", command = self.stopFun,
                                  state = "disabled")
        self.saveBut = ttk.Button(self.saveImagesLF, text = "Save", command = self.saveImages,
                                  state = "disabled")

        # checkbuttons
        self.removeReflections = ttk.Checkbutton(self.optionsLF, text = "Remove reflections",
                                                 variable = self.removeReflectionsVar,
                                                 command = self.toggleReflections)
        self.showShocks = ttk.Checkbutton(self.optionsLF, text = "Show shocks",
                                          variable = self.showShocksVar,
                                          command = self.toggleShocks)

        # radiobutton
        self.showTrack = ttk.Radiobutton(self.optionsLF, text = "Show track",
                                         variable = self.showTrackVar, value = True,
                                         command = self.toggleAnimation)
        self.showAnimation = ttk.Radiobutton(self.optionsLF, text = "Show animation",
                                             variable = self.showTrackVar, value = False,
                                             command = self.toggleAnimation)
        
        
        # label
        self.statusBar = ttk.Label(self, textvariable = self.status)
        self.distance = ttk.Label(self.parametersLF, textvariable = self.distanceVar, width = 10,
                                  anchor = "e")
        self.entrances = ttk.Label(self.parametersLF, textvariable = self.entrancesVar, width = 10,
                                   anchor = "e")
        self.timePar = ttk.Label(self.parametersLF, textvariable = self.timeVar, width = 10,
                                 anchor = "e")
        self.totDistance = ttk.Label(self.parametersLF, textvariable = self.totDistanceVar,
                                     width = 10, anchor = "e")
        self.totEntrances = ttk.Label(self.parametersLF, textvariable = self.totEntrancesVar,
                                     width = 10, anchor = "e")
        self.distanceLab = ttk.Label(self.parametersLF, text = "Distance [m]", width = 11,
                                     anchor = "w")
        self.entrancesLab = ttk.Label(self.parametersLF, text = "Entrances", width = 11,
                                      anchor = "w")
        self.timeLab = ttk.Label(self.parametersLF, text = "Time", width = 11, anchor = "w")
        self.currentLab = ttk.Label(self.parametersLF, text = "Current", width = 10,
                                    anchor = "e")
        self.totalLab = ttk.Label(self.parametersLF, text = "Total", width = 10,
                                  anchor = "e")
        self.saveWhatLab = ttk.Label(self.saveImagesLF, text = "Save")
        self.saveWhichFilesLab = ttk.Label(self.saveImagesLF, text = "File(s)")

        # scales
        self.timeSc = ttk.Scale(self, orient = HORIZONTAL, from_ = 0, to = 100,
                                variable = self.curTime, command = self.changedTime)
        self.speedSc = ttk.Scale(self.speedScaleFrame, orient = VERTICAL, from_ = 50, to = 0.5,
                                 variable = self.speed, command = self.changedSpeed)
        
        # entry
        self.speedEntry = ttk.Entry(self.speedScaleFrame, textvariable = self.speed, width = 4,
                                    justify = "right", state = "readonly")
        
        # graph
        self.graph = eval(self.graphTypeVar.get())
        self.graph.bind("<Button-1>", self.graphClick)
        self.graph.bind("<Button-3>", self.graphPopUp)

        # combobox
        self.saveWhatCombo = ttk.Combobox(self.saveImagesLF, textvariable = self.saveWhatVar,
                                          justify = "center", width = 15, state = "readonly")
        self.saveWhatCombo["values"] = ("arena frame", "room frame", "both frames",
                                        "graph", "all")
        self.saveWhichFilesCombo = ttk.Combobox(self.saveImagesLF, textvariable =
                                                self.saveWhichFilesVar, justify = "center",
                                                width = 15, state = "readonly")
        self.saveWhichFilesCombo["values"] = ("current", "tagged", "untagged", "all")

        # adding to grid        
        self.controlsLF.grid(column = 0, row = 3, columnspan = 2, sticky = (N, W), pady = 2,
                             padx = 2)
        self.graphLF.grid(column = 0, row = 5, columnspan = 1, pady = 2, padx = 2, sticky = (N, W))
        self.fileStorageFrame.grid(column = 8, row = 0, pady = 2, padx = 3, sticky = (N, W))
        self.fileFrame.grid(column = 8, row = 1, rowspan = 4, padx = 3, sticky = (N, S))
        self.parametersLF.grid(column = 3, row = 3, columnspan = 3)
        self.timeLabFrame.grid(column = 2, row = 5, columnspan = 3, sticky = (W, N), pady = 2)        
        self.timeFrame.grid(column = 0, row = 0)
        self.arenaFrame.grid(column = 0, row = 0, rowspan = 2, columnspan = 3,
                             sticky = (N, S, E, W), padx = 2, pady = 2)
        self.roomFrame.grid(column = 3, row = 0, rowspan = 2, columnspan = 4,
                            sticky = (N, S, E, W), padx = 2, pady = 2)
        self.speedScaleFrame.grid(column = 7, row = 0, rowspan = 2, sticky = (N, S), padx = 4,
                                  pady = 2)
        self.optionsLF.grid(column = 5, row = 5, columnspan = 2, sticky = (N, W), pady = 2)
        self.saveImagesLF.grid(column = 8, row = 5, sticky = (S), pady = 5)
        
        self.arenaCanv.grid(column = 0, row = 0, padx = 1, pady = 1)
        self.roomCanv.grid(column = 0, row = 0, padx = 1, pady = 1)
        self.graph.grid(column = 0, row = 4, columnspan = 7, padx = 2, pady = 5, sticky = (E, W))
        
        self.playBut.grid(column = 0, row = 0, sticky = (N, S), padx = 2, pady = 2)
        self.pauseBut.grid(column = 1, row = 0, sticky = (N, S), padx = 2, pady = 2)
        self.stopBut.grid(column = 2, row = 0, sticky = (N, S), padx = 2, pady = 2)
        self.saveBut.grid(column = 1, row = 2, sticky = E)

        self.removeReflections.grid(column = 0, row = 2, padx = 2, pady = 2, sticky = (N, W))
        self.showShocks.grid(column = 0, row = 3, padx = 2, pady = 2, sticky = (N, W))

        self.showAnimation.grid(column = 0, row = 0, padx = 2, pady = 1, sticky = (N, W))
        self.showTrack.grid(column = 0, row = 1, padx = 2, pady = 1, sticky = (N, W))
        
        self.speedEntry.grid(column = 0, row = 1)

        self.statusBar.grid(column = 0, row = 6, columnspan = 7, sticky = (S, E, W), padx = 2,
                       pady = 2)
        self.distance.grid(column = 1, row = 1, sticky = E)
        self.entrances.grid(column = 1, row = 2, sticky = E)
        self.timePar.grid(column = 1, row = 3, sticky = E)
        self.totDistance.grid(column = 2, row = 1, sticky = E, padx = 4)
        self.totEntrances.grid(column = 2, row = 2, sticky = E, padx = 4)
        self.distanceLab.grid(column = 0, row = 1, sticky = E)
        self.entrancesLab.grid(column = 0, row = 2, sticky = E)
        self.timeLab.grid(column = 0, row = 3, sticky = E)
        self.currentLab.grid(column = 1, row = 0, sticky = E, padx = 2, pady = 2)
        self.totalLab.grid(column = 2, row = 0, sticky = E, padx = 4, pady = 2)
        self.saveWhatLab.grid(column = 0, row = 0, padx = 2, sticky = E)
        self.saveWhichFilesLab.grid(column = 0, row = 1, padx = 2, sticky = E)

        self.timeSc.grid(column = 0, row = 2, columnspan = 7, sticky = (E, W), pady = 4, padx = 2)
        self.speedSc.grid(column = 0, row = 0, sticky = (N, S, E, W), pady = 3)

        self.saveWhatCombo.grid(column = 1, row = 0)
        self.saveWhichFilesCombo.grid(column = 1, row = 1)


        # checkbuttons for graph
        for number, gType in enumerate(getGraphTypes()):
            ttk.Radiobutton(self.graphLF, text = gType[0], variable = self.graphTypeVar,
                            command = self.changedGraph, value = gType[1])\
                            .grid(row = number, pady = 2, padx = 1, sticky = (W))


    def saveImages(self):
        "saves images for selected files"
        files = self._returnSelectedFiles()

        if len(files) > 1:
            progress = ProgressWindow(self, len(files), text = "saved")
        elif len(files) == 1:
            self.root.config(cursor = "wait")
            self.root.update()
        elif len(files) == 0:
            self.status.set("No file selected.")
            self.bell()
            return

        problems = 0
        for filename in files:
            try:
                if filename in self.fileStorage.pairedfiles:
                    cm = CM(filename, nameR = self.fileStorage.pairedfiles[filename])
                else:
                    cm = CM(filename, nameR = "auto")
                if self.removeReflectionsVar.get():
                    cm.removeReflections(points = self.fileStorage.reflections.get(filename, None))
                self.saveOneImage(cm, filename)               
            except Exception:
                problems += 0

            if len(files) > 1:
                progress.addOne()

        if len(files) > 1:
            progress.destroy()
            if problems > 1:
                self.status.set("{} problems occured!".format(problems))
                self.bell()
            elif problems == 1:
                self.status.set("One problem occured!")
                self.bell()
            else:
                self.status.set("Images were saved.")
        else:
            if problems:
                self.status.set("A problem occured!")
                self.bell()
            else:
                self.status.set("Image was saved.")
            self.root.config(cursor = "")


    def saveOneImage(self, cm, filename):
        "saves image for one file"
        directory = optionGet("ImageDirectory", os.getcwd(), "str")
        # pridat moznost scale u vsech SVG
        
        what =  self.saveWhatVar.get()    

        if what == "both frames":
            self.svg = SVG(600, 300)
            self.saveArenaFrame(cm)
            self.saveRoomFrame(cm, origin = (300, 0))
        elif what == "arena frame":
            self.svg = SVG(300, 300) 
            self.saveArenaFrame(cm)
        elif what == "room frame":
            self.svg = SVG(300, 300)
            self.saveRoomFrame(cm)
        elif what == "graph":
            self.svg = SVG(600, 120)
            self.saveGraph(cm)
        elif what == "all":
            self.svg = SVG(600, 420)
            self.saveArenaFrame(cm)
            self.saveRoomFrame(cm, origin = (300, 0))
            self.saveGraph(cm, origin = (0, 300))
            
        self.svg.save(os.path.join(directory, os.path.splitext(os.path.basename(filename))[0] +
                                   "_" + what.replace(" ", "_") + ".svg")) # upravit
            

    def saveArenaFrame(self, cm, origin = (0, 0)):
        "saves info about arena frame in self.svg"
        self.svg.drawAAPA(cm, frame = "arena", startTime = int(self.timeFrame.startTimeVar.get()),
                         time = int(self.timeFrame.timeVar.get()), boundary = True,
                         sector = False, shocks = False, origin = origin) # dat do options


    def saveRoomFrame(self, cm, origin = (0, 0)):
        "saves info about room frame in self.svg"
        self.svg.drawAAPA(cm, frame = "room", startTime = int(self.timeFrame.startTimeVar.get()),
                         time = int(self.timeFrame.timeVar.get()), boundary = True,
                         sector = True, shocks = True, origin = origin)


    def saveGraph(self, cm, origin = (0, 0)):
        "saves info about graph in self.svg"
        size = (600, 120)
        graph = eval(self.graphTypeVar.get()[:-1] + ', cm, purpose = "svg")')
        yCoordinates, maxY, furtherText = graph.saveGraph(cm)
        points = []
        if yCoordinates:
            length = len(yCoordinates) - 1
            for count, y in enumerate(yCoordinates):       
                points.append(((count * size[0]) / length, size[1] - ((y * size[1]) / maxY)))
        self.svg.drawGraph(points, furtherText = furtherText, origin = origin, boundary = True)


    def _returnSelectedFiles(self):
        "returns files that are chosen for creating of images"
        which = self.saveWhichFilesVar.get()
        
        files = []
        if which == "all":
            files.extend(self.fileStorage.arenafiles)
        elif which == "tagged":
            files.extend(self.fileStorage.tagged)
        elif which == "untagged":
            files.extend([file for file in self.fileStorage.arenafiles if file not in
                          self.fileStorage.tagged])
        elif which == "current":
            files = [self.fileFrame.selected]

        return files
            

    def toggleAnimation(self):
        "called when checkbutton for showing tracks is toggled"
        if self.initialized:          
            self.initializeFile(self.fileFrame.selected, new = False, timeReset = False)


    def toggleReflections(self):
        "called when reflections removal is changed - just refreshes canvases"
        if self.initialized:          
            if self.removeReflectionsVar.get():
                self.cm.removeReflections(points = self.fileStorage.reflections.get(self.cm.nameA,
                                                                                    None))
                self.data = self.cm.getData()
                self.initializeFile(self.fileFrame.selected, new = False, timeReset = False)
            else:
                self.initializeFile(self.fileFrame.selected, new = True, timeReset = False)


    def toggleShocks(self):
        "called when checkbutton for showing shocks is toggled"
        if self.initialized and self.showTrackVar.get():
            self.initializeFile(self.fileFrame.selected, new = False, timeReset = False)                    
            

    def playFun(self):
        "starts animation"        
        self.timeFrame.changeState("disabled")
        self.showTrack["state"] = "disabled"
        self.saveBut.state(["disabled"])
        self.animate = "" # status of the animation - '', 'pause', 'stop'
        
        prevTime = time()
        while not self.animate:
            # finds current time
            nowTime = time()
            difTime = (nowTime - prevTime) * 1000
            playTime = difTime * self.speed.get() + (
                self.curTime.get() * (self.maxTime - self.minTime) / 100) + self.minTime

            if playTime > self.maxTime:
                self.curTime.set(100)
                self.timeVar.set(self._timeFormat(self.maxTime))  
                self.animate = "stop"
                self.timeFrame.changeState("!disabled")
                self.showTrack["state"] = "!disabled"
                self.saveBut.state(["!disabled"])
                break

            self.changedTime(playTime, unit = "ms")

            prevTime = nowTime


    def changedTime(self, value, unit = "0-100"):
        "called when time is changed"
        if not self.initialized or self.minTime > self.maxTime or self.maxTime < self.minTime:
            return
        
        if unit == "ms":
            value = value
        else:
            value = ((float(value) * (self.maxTime - self.minTime)) / 100) + self.minTime
        
        # finds current line in data and computes current distance and entrances            
        x0, y0 = self.data[0][7:9] 
        dist = 0
        entrances = 0
        prev = 0
        skip = 25 # z options
        start = self.cm.findStart(self.minTime / 60000)
        for row, content in enumerate(self.data[start:]):
            if content[1] < value:
                # distance
                if row % skip == 0:
                    x1, y1 = content[7:9]
                    diff = ((x1 - x0)**2 + (y1 - y0)**2)**0.5
                    dist += diff
                    x0, y0 = x1, y1
                # entrances
                if content[5] != 2 and prev != 2:
                    continue
                elif content[5] != 2 and prev == 2:
                    if content[5] == 0: 
                        prev = 0
                    continue
                elif content[5] == 2 and prev == 2:
                    continue
                elif content[5] == 2 and prev != 2:
                    entrances += 1
                    prev = 2
                    continue
            else:
                curLine = content
                break
        dist = dist / (self.cm.trackerResolution * 100) # conversion from pixels to metres
        self.distanceVar.set("{:.1f}".format(dist))
        self.entrancesVar.set(entrances)

        Rx, Ry = curLine[2:4]
        Ax, Ay = curLine[7:9]                              

        # changes position of 'the rat'
        self.arenaCanv.coords("ratA", (Ax + self.ld, Ay + self.ld,\
                                Ax + self.ur, Ay + self.ur))
        self.roomCanv.coords("ratR", (Rx + self.ld, Ry + self.ld,\
                                Rx + self.ur, Ry + self.ur))

        # changes color of 'the rat' in case of shock
        if curLine[6] > 0:
            self.roomCanv.itemconfigure("ratR", fill = "red", outline = "red")
            self.shock = True
        elif self.shock and curLine[6] <= 0:
            self.roomCanv.itemconfigure("ratR", fill = "black", outline = "black")
            self.shock = False       

        self.graph.changedTime(value)

        # changes position of a time scale               
        if value < self.maxTime:
            self.curTime.set(((value - self.minTime) * 100) / (self.maxTime - self.minTime))
            self.timeVar.set(self._timeFormat(value))  
        else:
            self.curTime.set(100)
            self.timeVar.set(self._timeFormat(self.maxTime))          

        self.update()

            
    def setTime(self):
        "called when maximum or minimum time is set"
        if self.initialized:
            nowTime = (self.curTime.get() / 100) * (self.maxTime - self.minTime) + self.minTime
            self.initializeFile(self.fileFrame.selected, new = False)
            if self.minTime <= nowTime <= self.maxTime:
                self.graph.changedTime(nowTime)
                self.changedTime(nowTime, unit = "ms")
            elif self.minTime > nowTime:
                self.graph.changedTime(self.minTime)
                self.changedTime(self.minTime, unit = "ms")
            else:
                self.graph.changedTime(self.maxTime)
                self.changedTime(self.maxTime, unit = "ms")
            

    def pauseFun(self):
        "pauses and unpauses animation"
        if self.animate == "pause":
            self.timeFrame.changeState("disabled")
            self.showTrack["state"] = "disabled"
            self.saveBut.state(["disabled"])
            self.playFun()
        else:
            self.animate = "pause"
            self.timeFrame.changeState("!disabled")        
            self.showTrack["state"] = "!disabled"
            self.saveBut.state(["!disabled"])
            

    def stopFun(self):
        "stops animation"
        self.animate = "stop"
        self.changedTime(0)
        self.timeFrame.changeState("!disabled")          
        self.showTrack["state"] = "!disabled"
        self.saveBut.state(["!disabled"])
        

    def initializeFile(self, filename, new = True, timeReset = True):
        "initializes canvases, graph, etc."
        if new:
            try:
                if filename in self.fileStorage.pairedfiles:
                    self.cm = CM(filename, nameR = self.fileStorage.pairedfiles[filename])
                else:
                    self.cm = CM(filename, nameR = "auto")
                if self.removeReflectionsVar.get():
                    self.cm.removeReflections(points = self.fileStorage.reflections.get(filename,
                                                                                        None))
                self.data = self.cm.getData()
            except Exception as e:
                if optionGet("Developer", False, 'bool'):
                    print(e)
                self.status.set("File failed to load!")
                self.bell()
                return

  
        # initialization of canvases
        self.arenaCanv.delete("all")
        self.roomCanv.delete("all")
        
        d = self.cm.getComputedDiameter()
        self.arenaCanv.create_oval(150 - d, 150 - d, 150 + d, 150 + d, outline = "black",\
                                   width = 2, tags = "arenaAF")
        self.roomCanv.create_oval(150 - d, 150 - d, 150 + d, 150 + d, outline = "black",\
                                  width = 2, tags = "arenaRF")

       
        # creates lines depicting the shock sector
        if self.cm.getCenterAngle():
            self.angle = self.cm.getCenterAngle()
            self.width = self.cm.getWidth()

            a1 = radians(self.angle - (self.width / 2))
            a2 = radians(self.angle + (self.width / 2))

            Sx1, Sy1 = 150 + (cos(a1) * d), 150 - (sin(a1) * d)
            Sx2, Sy2 = 150 + (cos(a2) * d), 150 - (sin(a2) * d)
            
            self.roomCanv.create_line((Sx1, Sy1, 150, 150, Sx2, Sy2), fill = "red", width = 2,\
                                       tags = "shockZone")


        # minimal and maximal time
        self.maxTime = min([self.cm.data[-1][1], eval(self.timeFrame.timeVar.get()) * 60000])
        self.minTime = max([self.cm.data[0][1], eval(self.timeFrame.startTimeVar.get()) * 60000])

        # initial position
        for line in self.data:
            if line[1] < self.minTime:
                continue                       
            else:
                curLine = line
                break
        else:
            self.status.set("Invalid start time set.")
            self.bell()
            return

        
        if self.showTrackVar.get():
            # draws tracks        
            data = [line[2:4] + line[6:9] for count, line in enumerate(self.data) if
                    self.minTime <= line[1] <= self.maxTime]
            arena = []
            room = []
            prev = [-100, -100, 0, -100, -100]
            last = [0, 0]
            for count, line in enumerate(data):
                if abs(line[0] - prev[0]) + abs(line[1] - prev[1]) > 2 or count - last[0] == 15 or\
                   line[2] > 0:
                    room.append(line[0:2])
                    last[0] = count
                if abs(line[3] - prev[3]) + abs(line[4] - prev[4]) > 2 or count - last[1] == 15:
                    arena.append(line[3:5])
                    last[1] = count
                prev = line
                    
            self.arenaCanv.create_line(([item + 150 - d for line in arena for item in line]),
                                       fill = "black", width = 2)
            self.roomCanv.create_line(([item + 150 - d for line in room for item in line]),
                                      fill = "black", width = 2)
            # draws shocks
            if self.showShocksVar.get():
                shocks = [line[2:4] for count, line in enumerate(self.data) if
                          self.minTime <= line[1] <= self.maxTime and line[6] > 0 and
                          self.data[count - 1][6] <= 0]
                self.ld = 150 - d - 4
                self.ur = 150 - d + 4
                for shock in shocks:
                    self.roomCanv.create_oval(shock[0] + self.ld, shock[1] + self.ld,
                                              shock[0] + self.ur, shock[1] + self.ur,
                                              outline = "red", width = 3)
        else:
            # makes 'the rat' (i.e. two black points)
            self.ld = 150 - d - 4 # 4 is size of the rat dot
            self.ur = 150 - d + 4
            
            Rx, Ry = curLine[2:4]
            Ax, Ay = curLine[7:9]
            self.arenaCanv.create_oval(Ax + self.ld, Ay + self.ld, Ax + self.ur, Ay + self.ur,\
                                       fill = "black", tags = "ratA")
            self.roomCanv.create_oval(Rx + self.ld, Ry + self.ld, Rx + self.ur, Ry + self.ur,\
                                       fill = "black", tags = "ratR")
            
            # shock
            if curLine[6] > 0:
                self.roomCanv.itemconfigure("ratR", fill = "red", outline = "red")
                self.shock = True   
            else:
                self.shock = False


        # initializes distance and entrances displays
        self.totDistanceVar.set(round(self.cm.getDistance(time = self.maxTime / 60000,
                                                          startTime = self.minTime / 60000), 1))
        self.totEntrancesVar.set(self.cm.getEntrances(time = self.maxTime / 60000,
                                                      startTime = self.minTime / 60000))
        if timeReset:
            self.distanceVar.set("0.0")
            self.entrancesVar.set("0")


        # drawing of the graph
        self.graph.delete("all")
        if timeReset:
            self.graph.CM_loaded(self.cm, minTime = self.minTime, maxTime = self.maxTime)
        else:
            self.graph.CM_loaded(self.cm, minTime = self.minTime, maxTime = self.maxTime,
                                 initTime = (self.curTime.get() * (self.maxTime - self.minTime) /
                                             100 + self.minTime))

    
        # changing buttons' statuses
        if not self.showTrackVar.get():
            self.playBut.state(["!disabled"])
            self.pauseBut.state(["!disabled"])
            self.stopBut.state(["!disabled"])
            self.timeSc.state(["!disabled"])
        else:
            self.playBut.state(["disabled"])
            self.pauseBut.state(["disabled"])
            self.stopBut.state(["disabled"])
            self.timeSc.state(["disabled"])


        self.initialized = True


        if new and timeReset:
            self.curTime.set(0)
            self.timeVar.set(self._timeFormat(self.minTime))  


        
    def changedGraph(self):
        "called when the graph is changed"
        self.graph.delete("all")
        drawnParameter = self.graph.drawnParameter
        self.graph = eval(self.graphTypeVar.get())
        self.graph.drawnParameter = drawnParameter
        self.graph.grid(column = 0, row = 4, columnspan = 7, padx = 2, pady = 5, sticky = (E, W))
        self.graph.bind("<Button-1>", self.graphClick)
        self.graph.bind("<Button-3>", self.graphPopUp)
        if self.initialized:
            self.graph.CM_loaded(self.cm, minTime = self.minTime, maxTime = self.maxTime,
                                 initTime = ((self.curTime.get() * (self.maxTime - self.minTime) /
                                              100) + self.minTime))
            

    def graphClick(self, event):
        "changes time when the graph is clicked upon"
        if self.initialized and not self.showTrackVar.get():
            self.graph.coords("timeMeasure", (event.x, 0, event.x, self.graph.height))
            time = (event.x * 100) / self.graph.width
            if time > 100:
                time = 100           
            self.changedTime(time)


    def graphPopUp(self, event):
        "shows menu for selection of parameter to be shown in graph"
        if not self.initialized:
            return
        
        menu = Menu(self, tearoff = 0)
        menu.add_command(label = "Show periodicity",
                         command = lambda: self.graph.drawParameter(self.cm, "periodicity"))
        menu.add_command(label = "Show mobility",
                         command = lambda: self.graph.drawParameter(self.cm, "mobility"))
        menu.add_command(label = "Show immobility",
                         command = lambda: self.graph.drawParameter(self.cm, "immobility"))
        menu.add_command(label = "Show entrances",
                         command = lambda: self.graph.drawParameter(self.cm, "entrances"))
        menu.add_command(label = "Show shocks",
                         command = lambda: self.graph.drawParameter(self.cm, "shocks"))
        menu.add_command(label = "Show bad points",
                         command = lambda: self.graph.drawParameter(self.cm, "bad points"))
        menu.add_command(label = "Show thigmotaxis",
                         command = lambda: self.graph.drawParameter(self.cm, "thigmotaxis"))
        menu.add_command(label = "Don't show anything",
                         command = lambda: self.graph.drawParameter(self.cm, None))
        menu.post(event.x_root, event.y_root)        


    def unDraw(self):
        "clears canvases, graph etc."
        if self.initialized:
            self.arenaCanv.delete("all")
            self.roomCanv.delete("all")
            self.graph.delete("all")
            self.initialized = False
            self.playBut.state(["disabled"])
            self.pauseBut.state(["disabled"])
            self.stopBut.state(["disabled"])
            self.fileFrame.previousBut.state(["disabled"])
            self.fileFrame.nextBut.state(["disabled"])
            self.fileFrame.tagBut.state(["disabled"])
            self.saveBut.state(["disabled"])
            

    def checkProcessing(self):
        "called when content of fileStorage is changed"
        if self.fileStorage.arenafiles or self.fileStorage.wrongfiles:
            self.fileStorageFrame.removeFiles.state(["!disabled"])
        else:
            self.fileStorageFrame.removeFiles.state(["disabled"])
        self.fileStorageFrame.chosenVar.set(len(self.fileStorage.arenafiles))
        self.fileStorageFrame.nonMatchingVar.set(len(self.fileStorage.wrongfiles))

        self.fileFrame.files = [] + self.fileStorage.arenafiles
        
        if not self.fileFrame.files or not self.fileFrame.selected in self.fileFrame.files:
            self.unDraw()

        if self.fileFrame.files:
            self.fileFrame.previousBut.state(["!disabled"])
            self.fileFrame.nextBut.state(["!disabled"])
            self.fileFrame.tagBut.state(["!disabled"])
            self.saveBut.state(["!disabled"])
            if not self.fileFrame.selected in self.fileFrame.files:
                self.fileFrame.selected = None
        else:
            self.fileFrame.selected = None
            
        self.fileFrame.drawTree(self.fileFrame.selected)


    def initialize(self):
        """initializes the canvases etc. if files were added to fileStorage and file from
            fileStorage is not initialized"""
        if self.fileFrame.files:
            if not self.fileFrame.selected in self.fileFrame.files:
                self.initializeFile(self.fileFrame.files[0])
                self.fileFrame.selected = self.fileFrame.files[0]


    def _timeFormat(self, time):
        "formats time from miliseconds to minutes:seconds"
        minutes = time // 60000
        seconds = (time % 60000) // 1000
        return "{:d}:{:0>2d}".format(int(minutes), int(seconds))

         
    def changedSpeed(self, value):
        "rounds speed set by scale"
        if eval(value) <= 1.0:
            self.speed.set(round(self.speed.get(), 1))
        elif eval(value) < 2.0:
            self.speed.set(round(self.speed.get() * 5, 0) / 5)
        elif eval(value) < 5.0:
            self.speed.set(round(self.speed.get() * 2, 0) / 2)
        elif eval(value) < 10.0:
            self.speed.set(round(self.speed.get(), 0))
        else:
            self.speed.set(round(self.speed.get() * 5, -1) / 5)
示例#18
0
    def initializeFile(self, filename, new = True, timeReset = True):
        "initializes canvases, graph, etc."
        if new:
            try:
                if filename in self.fileStorage.pairedfiles:
                    self.cm = CM(filename, nameR = self.fileStorage.pairedfiles[filename])
                else:
                    self.cm = CM(filename, nameR = "auto")
                if self.removeReflectionsVar.get():
                    self.cm.removeReflections(points = self.fileStorage.reflections.get(filename,
                                                                                        None))
                self.data = self.cm.getData()
            except Exception as e:
                if optionGet("Developer", False, 'bool'):
                    print(e)
                self.status.set("File failed to load!")
                self.bell()
                return

  
        # initialization of canvases
        self.arenaCanv.delete("all")
        self.roomCanv.delete("all")
        
        d = self.cm.getComputedDiameter()
        self.arenaCanv.create_oval(150 - d, 150 - d, 150 + d, 150 + d, outline = "black",\
                                   width = 2, tags = "arenaAF")
        self.roomCanv.create_oval(150 - d, 150 - d, 150 + d, 150 + d, outline = "black",\
                                  width = 2, tags = "arenaRF")

       
        # creates lines depicting the shock sector
        if self.cm.getCenterAngle():
            self.angle = self.cm.getCenterAngle()
            self.width = self.cm.getWidth()

            a1 = radians(self.angle - (self.width / 2))
            a2 = radians(self.angle + (self.width / 2))

            Sx1, Sy1 = 150 + (cos(a1) * d), 150 - (sin(a1) * d)
            Sx2, Sy2 = 150 + (cos(a2) * d), 150 - (sin(a2) * d)
            
            self.roomCanv.create_line((Sx1, Sy1, 150, 150, Sx2, Sy2), fill = "red", width = 2,\
                                       tags = "shockZone")


        # minimal and maximal time
        self.maxTime = min([self.cm.data[-1][1], eval(self.timeFrame.timeVar.get()) * 60000])
        self.minTime = max([self.cm.data[0][1], eval(self.timeFrame.startTimeVar.get()) * 60000])

        # initial position
        for line in self.data:
            if line[1] < self.minTime:
                continue                       
            else:
                curLine = line
                break
        else:
            self.status.set("Invalid start time set.")
            self.bell()
            return

        
        if self.showTrackVar.get():
            # draws tracks        
            data = [line[2:4] + line[6:9] for count, line in enumerate(self.data) if
                    self.minTime <= line[1] <= self.maxTime]
            arena = []
            room = []
            prev = [-100, -100, 0, -100, -100]
            last = [0, 0]
            for count, line in enumerate(data):
                if abs(line[0] - prev[0]) + abs(line[1] - prev[1]) > 2 or count - last[0] == 15 or\
                   line[2] > 0:
                    room.append(line[0:2])
                    last[0] = count
                if abs(line[3] - prev[3]) + abs(line[4] - prev[4]) > 2 or count - last[1] == 15:
                    arena.append(line[3:5])
                    last[1] = count
                prev = line
                    
            self.arenaCanv.create_line(([item + 150 - d for line in arena for item in line]),
                                       fill = "black", width = 2)
            self.roomCanv.create_line(([item + 150 - d for line in room for item in line]),
                                      fill = "black", width = 2)
            # draws shocks
            if self.showShocksVar.get():
                shocks = [line[2:4] for count, line in enumerate(self.data) if
                          self.minTime <= line[1] <= self.maxTime and line[6] > 0 and
                          self.data[count - 1][6] <= 0]
                self.ld = 150 - d - 4
                self.ur = 150 - d + 4
                for shock in shocks:
                    self.roomCanv.create_oval(shock[0] + self.ld, shock[1] + self.ld,
                                              shock[0] + self.ur, shock[1] + self.ur,
                                              outline = "red", width = 3)
        else:
            # makes 'the rat' (i.e. two black points)
            self.ld = 150 - d - 4 # 4 is size of the rat dot
            self.ur = 150 - d + 4
            
            Rx, Ry = curLine[2:4]
            Ax, Ay = curLine[7:9]
            self.arenaCanv.create_oval(Ax + self.ld, Ay + self.ld, Ax + self.ur, Ay + self.ur,\
                                       fill = "black", tags = "ratA")
            self.roomCanv.create_oval(Rx + self.ld, Ry + self.ld, Rx + self.ur, Ry + self.ur,\
                                       fill = "black", tags = "ratR")
            
            # shock
            if curLine[6] > 0:
                self.roomCanv.itemconfigure("ratR", fill = "red", outline = "red")
                self.shock = True   
            else:
                self.shock = False


        # initializes distance and entrances displays
        self.totDistanceVar.set(round(self.cm.getDistance(time = self.maxTime / 60000,
                                                          startTime = self.minTime / 60000), 1))
        self.totEntrancesVar.set(self.cm.getEntrances(time = self.maxTime / 60000,
                                                      startTime = self.minTime / 60000))
        if timeReset:
            self.distanceVar.set("0.0")
            self.entrancesVar.set("0")


        # drawing of the graph
        self.graph.delete("all")
        if timeReset:
            self.graph.CM_loaded(self.cm, minTime = self.minTime, maxTime = self.maxTime)
        else:
            self.graph.CM_loaded(self.cm, minTime = self.minTime, maxTime = self.maxTime,
                                 initTime = (self.curTime.get() * (self.maxTime - self.minTime) /
                                             100 + self.minTime))

    
        # changing buttons' statuses
        if not self.showTrackVar.get():
            self.playBut.state(["!disabled"])
            self.pauseBut.state(["!disabled"])
            self.stopBut.state(["!disabled"])
            self.timeSc.state(["!disabled"])
        else:
            self.playBut.state(["disabled"])
            self.pauseBut.state(["disabled"])
            self.stopBut.state(["disabled"])
            self.timeSc.state(["disabled"])


        self.initialized = True


        if new and timeReset:
            self.curTime.set(0)
            self.timeVar.set(self._timeFormat(self.minTime))  
示例#19
0
    def processFun(self):
        "processes chosen files and saves the results in the save-to file"
        # files to be processed
        self.filesToProcess = [file for file in self.fileStorage if \
                               self.optionFrame.processFile(file)]

        # checking for mistakes
        try:
            self.processCheck()
        except Exception as e:
            self.bell()
            self.status.set(e)
            return                   

        # progressWindow
        if len(self.filesToProcess) > 1:
            self.stoppedProcessing = False
            self.progressWindow = ProgressWindow(self, len(self.filesToProcess))     

        # selected methods          
        methods = []
        for method in Parameters().parameters:
            if eval("self.parametersF.%sVar.get()" % (method[0].replace(" ", ""))):
                methods.append(method[0])

        # initializations
        output = self.saveToFrame.saveToVar.get()
        startTime = float(self.timeFrame.startTimeVar.get())
        time = float(self.timeFrame.timeVar.get())
        separator = optionGet("ResultSeparator", ",", "str")
        batchTime = self.selectedBatchTime if self.useBatchTimeVar.get() else None
        self.log = Log(methods, startTime, time, self.filesToProcess, self.fileStorage,
                       self.optionFrame.removeReflectionsWhere.get(), output,
                       batchTime = batchTime)
        self.someProblem = False
        developer = optionGet("Developer", False, 'bool')

        # results header
        if self.useBatchTimeVar.get():
            info = [method[0] for method in Parameters().parameters if method[2] == "info"]
            results = ["File"]
            for method in methods:
                if method in info:
                    results.append(method)
                else:
                    results.extend([method + " ({}-{})".format(start, end) for
                                    start, end in self.selectedBatchTime])
            results = separator.join(results)
        else:
            results = separator.join(["File"] + methods)        
        if self.optionFrame.saveTags.get():
            results += separator + "Tags"
        if self.optionFrame.saveComments.get():
            results += separator + "Comment"
        
        # computing of results
        for file in self.filesToProcess:
            # loading of cm object
            if methods:
                try:
                    if file in self.fileStorage.pairedfiles:
                        cm = CM(file, nameR = self.fileStorage.pairedfiles[file])
                    else:
                        cm = CM(file, nameR = "auto")

                    if self.optionFrame.removeReflections(file):
                        cm.removeReflections(points = self.fileStorage.reflections.get(file,
                                                                                            None))
                except Exception as e:
                    if developer:
                        print(e)   
                    filename = returnName(filename = file, allFiles =
                                          self.fileStorage.arenafiles) 
                    results += "\n" + filename + "{}NA".format(separator) * len(methods)
                    self.log.failedToLoad.append(file)
                    self.someProblem = True
                    continue
                                
            result = []
            for method in Parameters().parameters:
                if method[0] in methods:
                    if self.useBatchTimeVar.get():
                        for startTime, time in self.selectedBatchTime:
                            try:
                                if method[2] == "custom":
                                    exec("from Stuff.Parameters import {}".format(method[5]))
                                result.append(eval(method[1]))
                                if method[2] == "info":
                                    break
                            except Exception as e:
                                if developer:
                                    print(e)   
                                result.append("NA")
                                self.log.methodProblems[method[0]].append(file)
                                self.someProblem = True
                    else:                          
                        try:
                            if method[2] == "custom":
                                exec("from Stuff.Parameters import {}".format(method[5]))
                            result.append(eval(method[1]))
                        except Exception as e:
                            if developer:
                                print(e)   
                            result.append("NA")
                            self.log.methodProblems[method[0]].append(file)
                            self.someProblem = True

            result = separator.join(map(str, result))
            if methods:
                result = separator + result               
            filename = returnName(filename = file, allFiles = self.fileStorage.arenafiles)      
            results += "\n" + filename + result
            
            # tag inclusion in results
            if self.optionFrame.saveTags.get(): 
                if file in self.fileStorage.tagged:
                    results += separator + "1"
                else:
                    results += separator + "0"

            # comment inclusion in results
            if self.optionFrame.saveComments.get():
                results += separator + self.fileStorage.comments[file]
                    
            # progress window update
            if len(self.filesToProcess) > 1:
                if self.stoppedProcessing:
                    writeResults(output, results)
                    self.log.stopped = file
                    self.log.writeLog()
                    self.status.set("Processing stopped")
                    return
                else:
                    self.progressWindow.addOne()

        # results and log writing, ending of processing
        writeResults(output, results)
        self.log.writeLog()
        self._setStatusEndProgressWindow()

        if self.someProblem:
            ProcessingProblemDialog(self, self.log.filename)
        elif self.optionFrame.showResults.get():
            os.startfile(output)