Example #1
0
class xbbtools_help(Toplevel):
    def __init__(self, *args):
        Toplevel.__init__(self)
        self.tid = ScrolledText(self)
        self.tid.pack(fill = BOTH, expand = 1)
        self.Styles()
        self.Show()

    def Styles(self):
        for c in ['red', 'blue', 'magenta', 'yellow', 'green', 'red4', 'green4', 'blue4']:
            self.tid.tag_configure(c, foreground = c)

        self.tid.tag_config('underline', underline =1)
        self.tid.tag_config('italic', font = ('Courier', 6, 'italic'))
        self.tid.tag_config('bold', font = ('Courier', 8, 'bold'))
        self.tid.tag_config('title', font = ('Courier', 12, 'bold'))
        self.tid.tag_config('small', font = ('Courier', 6, ''))
        self.tid.tag_config('highlight', background = 'gray')
        

    def Show(self):
        t = self.tid
        t.insert(END, "XBBtools Help\n", 'title')
        t.insert(END, """
Copyright 2001 by Thomas Sicheritz-Ponten.  All rights reserved.
This code is part of the Biopython distribution and governed by its
license.  Please see the LICENSE file that should have been included
as part of this package.\n
""", 'italic')
        t.insert(END, '[email protected]\n\n', 'blue')
        t.insert(END, '* Goto Field\n', 'bold')
        t.insert(END, '\tinserting one position moves cursor to position\n')
        t.insert(END, "\tinserting two positions, sperated by ':' ")
        t.insert(END, 'highlights', 'highlight')
        t.insert(END, ' selected range\n')
        t.insert(END, '\n')
        t.insert(END, '* Search\n', 'bold')
        t.insert(END, '\tambiguous dna values are\n')
        t.insert(END, """
                A: A
                C: C
                G: G
                T: T
                M: AC
                R: AG
                W: AT
                S: CG
                Y: CT
                K: GT
                V: ACG
                H: ACT
                D: AGT
                B: CGT
                X: GATC
                N: GATC

                """, 'small')
    def __init__(self, diffData):
        self.diffData = diffData
        self.listOptions = diffData.keys()
        #tk.Tk.__init__(self, *args, **kwargs)
        self.root = tk.Tk()
        lb = tk.Listbox(self.root)

        for option in self.listOptions:
           lb.insert("end", option)

        lb.bind("<Double-Button-1>", self.OnDouble)
        lb.bind("<<ListboxSelect>>", self.OnDouble)
        lb.pack(side="top", fill="both", expand=True)

        text = ScrolledText(self.root, height=500, width=150, bg="black")
        text.tag_configure("minus", foreground="red")
        text.tag_configure("plus", foreground="green")
        text.tag_configure("normal", foreground="grey")
        text.pack()

        self.textbox = text
Example #3
0
    def __init__(self, diffFilename):
        self.diffFilename = diffFilename

        #tk.Tk.__init__(self, *args, **kwargs)
        print "Loading diff tool"

        self.loadData()

        self.root = tk.Tk()
        lb = tk.Listbox(self.root)

        lb.bind("<Double-Button-1>", self.OnDouble)
        lb.bind("<<ListboxSelect>>", self.OnDouble)
        lb.pack(side="top", fill="both", expand=True)

        def reloadCallback():
            numOptions = len(self.listOptions)
            self.lb.delete(0, last = numOptions)
            print "Reloading"
            self.loadData()
            self.loadGui()
            self.loadFileIntoTextbox() # Reload the currently displayed file

        b = tk.Button(self.root, text="Reload", command=reloadCallback)
        b.pack()

        text = ScrolledText(self.root, height=500, width=150, bg="black")
        text.tag_configure("minus", foreground="red")
        text.tag_configure("plus", foreground="green")
        text.tag_configure("normal", foreground="grey")
        text.pack()

        self.lb = lb
        self.textbox = text

        self.loadGui()
Example #4
0
class ClustererGui(ttk.Frame):
    """GUI to open/save xml/text-files and visualize clustering."""
    def __init__(self, master=None):
        """Init GUI - get auto-split-sentences-option and standard test-file-folder from config-file."""
        ttk.Frame.__init__(self, master)
        self.grid(sticky=tk.N + tk.S + tk.E + tk.W)

        self.createWidgets()
        self.filepath = None
        self.xml_filepath = None
        self.filename = None
        self.article_id = None
        self.extraction = None
        self.author_no = None
        self.correct = None
        self.result = None
        self.colors = []

        config = ConfigParser.ConfigParser()
        config.read("config.cfg")
        params = dict(config.items("params"))
        article_dir = params['test_file_dir']
        self.auto_split_sentences = bool(int(params['auto_split_sentences']))
        self.show_knee_point = bool(int(params['show_knee_point']))
        self.show_knee_point = False  # currently not supported in GUI-mode
        self.last_dir = article_dir

    def createWidgets(self):
        """Organize GUI."""
        top = self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)

        self.rowconfigure(0, weight=1)
        self.rowconfigure(1, weight=0)
        self.columnconfigure(0, weight=1)
        self.columnconfigure(1, weight=0)

        left_frame = ttk.Frame(self, relief="raised", borderwidth=1)
        left_frame.grid(row=0, column=0, sticky=tk.N + tk.S + tk.E + tk.W)
        left_frame.rowconfigure(0, weight=0)
        left_frame.rowconfigure(1, weight=1)
        left_frame.columnconfigure(0, weight=1)

        buttons_topleft = ttk.Frame(left_frame)
        buttons_topleft.grid(row=0, column=0)

        self.choose_file_btn = ttk.Button(buttons_topleft,
                                          text='choose file...',
                                          command=self.choose_file)
        self.choose_file_btn.grid(row=0, column=0)

        self.save_file_btn = ttk.Button(buttons_topleft,
                                        text='save file...',
                                        command=self.save_file)
        self.save_file_btn.grid(row=0, column=1)

        self.extract_feat_btn = ttk.Button(
            buttons_topleft,
            text='process',
            command=self.start_featureextr_thread)
        self.extract_feat_btn.grid(row=0, column=2)

        right_frame = ttk.Frame(self)
        right_frame.grid(row=0, column=1, sticky=tk.N + tk.S + tk.E + tk.W)
        right_frame.rowconfigure(0, weight=1)
        right_frame.rowconfigure(1, weight=1)

        self.distr_entry = ScrolledText(right_frame, width=30, height=30)
        self.distr_entry.grid(row=0, column=0, columnspan=2, sticky=tk.N)

        self.test_entry = ScrolledText(right_frame, width=30)
        self.test_entry.grid(row=1, column=0, columnspan=2, sticky=tk.N)

        self.scrolledText = ScrolledText(left_frame, undo=True, wrap=tk.WORD)
        self.scrolledText['font'] = ('Helvetica', '12')
        self.scrolledText.tag_configure('lines',
                                        background="#dddddd",
                                        foreground="black",
                                        font=('Helvetica', 9))
        self.scrolledText.tag_configure('blanks',
                                        background="#ffffff",
                                        foreground="black",
                                        font=('Helvetica', 9))
        self.scrolledText.grid(row=1,
                               column=0,
                               sticky=tk.N + tk.S + tk.E + tk.W)

        status_bar = ttk.Frame(self)
        status_bar.grid(row=1, column=0, columnspan=2, sticky=tk.W)
        status_bar.columnconfigure(0, weight=1, minsize=100)
        status_bar.columnconfigure(1, weight=1)

        self.status = tk.StringVar()
        self.status.set("ready")
        self.status_label = ttk.Label(status_bar, textvariable=self.status)
        self.status_label.grid(row=0, column=1, padx=10)

        self.progressbar = ttk.Progressbar(status_bar,
                                           mode='indeterminate',
                                           length=200)
        self.progressbar.grid(row=0, column=0, padx=3)

    def choose_file(self):
        """Choose text or xml file dialog."""
        self.filepath = askopenfilename(initialdir=self.last_dir,
                                        filetypes=(("text and xml files",
                                                    ("*.txt", "*.xml")), ))
        if self.filepath:
            ext = os.path.splitext(self.filepath)[1]

            if ext == ".xml":
                '''save raw-text of xml-file to a new file and print it'''
                self.xml_filepath = self.filepath
                self.filepath = self.create_text_fromXML()

            base = os.path.split(self.filepath)[0]
            self.article_id = os.path.split(base)[1]
            self.filename = os.path.split(self.filepath)[1]
            self.scrolledText.delete(1.0, tk.END)
            self.print_raw_text()
            self.scrolledText.edit_reset()

    def create_text_fromXML(self):
        """Create text-file out of given xml-file."""
        new_filepath = os.path.splitext(self.filepath)[0] + ".txt"
        with codecs.open(self.filepath, 'r', 'UTF-8') as xml_file:
            xml_tree = etree.parse(xml_file)

        with codecs.open(new_filepath, 'w', 'UTF-8') as newFile:
            first_entry = True
            for entry in xml_tree.getroot():
                if entry.text is not None:
                    if not first_entry:
                        newFile.write("\n\n")
                    else:
                        first_entry = False
                    newFile.write(entry.text)
        return new_filepath

    def save_file(self):
        """Save text-file-dialog."""
        text = self.scrolledText.get("0.0", tk.END)
        if self.filepath is None:
            name = asksaveasfilename(initialdir=self.last_dir,
                                     defaultextension=".txt")
            if name:
                self.filepath = name
            else:
                return
        try:
            with codecs.open(self.filepath, 'w', 'UTF-8') as newFile:
                newFile.write(text.strip())
            self.scrolledText.edit_reset()
            base = os.path.split(self.filepath)[0]
            self.article_id = os.path.split(base)[1]
            self.filename = os.path.split(self.filepath)[1]
            return True
        except Exception:  # as e:
            raise

    def start_featureextr_thread(self):
        """Start thread for feature extraction."""
        self.distr_entry.delete(1.0, tk.END)
        self.status.set("processing...")
        if self.filepath is None or self.article_id is None:
            tkMessageBox.showwarning("Save File",
                                     "Save file for feature extraction.")
            return
        try:
            self.scrolledText.edit_undo()
            self.scrolledText.edit_redo()

            tkMessageBox.showwarning("File changed",
                                     "File was changed, please save.")
            return
        except tk.TclError:
            self.extraction = clusterer.Clusterer(self.article_id,
                                                  self.filepath,
                                                  self.xml_filepath,
                                                  self.auto_split_sentences,
                                                  self.show_knee_point)

            self.ftr_extr_thread = threading.Thread(
                target=self.extract_features)
            self.ftr_extr_thread.daemon = True
            self.progressbar.start()
            self.ftr_extr_thread.start()
            self.after(1000, self.check_feat_thread)

    def check_feat_thread(self):
        """Check if feature extraction thread is still working - if not: visualize cluster-results."""
        if self.ftr_extr_thread.is_alive():
            self.after(1000, self.check_feat_thread)
        else:
            self.status.set("ready")

            # generate author-colormap
            self.colors = [None] * len(set(self.clusters))
            for k in set(self.clusters):
                temp_color = plt.cm.spectral(
                    np.float(k) / (np.max(self.clusters) + 1))
                if k == 0:
                    temp_color = plt.cm.spectral(0.05)
                self.colors[k] = self.convert_to_hex(temp_color)
            self.configure_colors()

            self.progressbar.stop()
            self.print_author_distr()
            self.print_text()
            if self.correct is not None and self.author_no is not None:
                self.test_entry.delete(1.0, tk.END)
                s = "authors found: {}".format(len(set(self.clusters)))
                s += "\n believe-score: {:.4f}".format(self.believe_score)
                s += "\n\n true number of authors: {}".format(self.author_no)
                s += "\n precision: {:.4f}".format(self.scores[0])
                s += "\n recall: {:.4f}".format(self.scores[1])
                s += "\n f1-score: {:.4f}".format(self.scores[2])
                s += "\n adjusted-rand-index: {:.4f}".format(self.scores[3])
                self.test_entry.insert(tk.INSERT, s)
            else:
                self.test_entry.delete(1.0, tk.END)
                s = "authors found: {}".format(len(set(self.clusters)))
                s += "\n believe-score: {:.4f}".format(self.believe_score)
                self.test_entry.insert(tk.INSERT, s)

    def extract_features(self):
        """Start feature extraction."""
        self.clusters, self.result, self.author_no, self.believe_score, self.scores = self.extraction.calc_cluster(
        )

        if self.result is not None:
            c = Counter(self.result)
            self.correct = c[True] / sum(c.values()) * 100

    def print_text(self):
        """Print raw text with specified author-colors."""
        self.scrolledText.delete(1.0, tk.END)
        f = open(self.filepath)

        line_number = 0
        actual_line_number = 0
        for line in f:
            actual_line_number += 1
            if line.strip():
                s = str(line_number) + ' ' + str(
                    self.clusters[line_number]) + ' ' + line
                s = line
                line_cluster = str(line_number) + ' ' + str(
                    self.clusters[line_number]) + ' '
                line_cluster = ('{:^' + str(14 - len(line_cluster)) +
                                '}').format(line_cluster)
                self.scrolledText.insert(tk.INSERT, line_cluster, 'lines')
                try:
                    self.scrolledText.insert(tk.INSERT, s,
                                             str(self.clusters[line_number]))
                    # if self.result[line_number]:
                    #     # correct assignment - print text foreground in white
                    #     self.scrolledText.insert(tk.INSERT, s, str(self.clusters[line_number]))
                    # else:
                    #     # false assignment - print text foreground in black
                    #     self.scrolledText.insert(tk.INSERT, s, str(self.clusters[line_number]*10**2))
                except IndexError:
                    self.scrolledText.insert(tk.INSERT, s)
                except TypeError:
                    self.scrolledText.insert(tk.INSERT, s,
                                             str(self.clusters[line_number]))
                line_number += 1
            else:
                s = line
                self.scrolledText.insert(tk.INSERT, s, 'blanks')
        f.close()

    def print_raw_text(self):
        """Print raw text."""
        f = open(self.filepath)
        for line in f:
            self.scrolledText.insert(tk.INSERT, line)
        f.close()

    def get_distribution(self, l=None):
        """Return Counter with author distribution in percent."""
        if l is None:
            l = self.clusters
        counter = Counter(l)
        sum_counter = sum(counter.values())

        for key in counter.iterkeys():
            counter[key] = counter[key] / sum_counter * 100
        return counter

    def print_author_distr(self):
        """Print author distribution with specified author-colors."""
        self.distr_entry.delete(1.0, tk.END)
        distr = self.get_distribution(self.clusters)

        for index, count in distr.most_common():
            author_i = "author " + str(index) + "{:>20}%\n".format(
                locale.format(u'%.2f', count))
            self.distr_entry.insert(tk.INSERT, author_i, str(index))

    def convert_to_hex(self, col):
        """Convert inter-tuple to hex-coded string."""
        red = int(col[0] * 255)
        green = int(col[1] * 255)
        blue = int(col[2] * 255)
        return '#{r:02x}{g:02x}{b:02x}'.format(r=red, g=green, b=blue)

    def configure_colors(self):
        """Configure author-specific colors for author-distribution and cluster-results."""
        for i, c in enumerate(self.colors):
            self.scrolledText.tag_configure(str(i),
                                            background=c,
                                            foreground="white")
            self.distr_entry.tag_configure(str(i),
                                           background=c,
                                           foreground="white")
Example #5
0
class PixivDownloadFrame(Frame):
    def __init__(self, root, api, search_handler):
        Frame.__init__(self, root)
        if not search_handler or not api:
            raise PixivError(
                'You must set  authPixivApi and search_handler for the DownloadFrame'
            )
        self.api = api
        self.search_handler = search_handler
        self.downloader = IllustrationDownloader(api)
        self.queue = PixivQueue(self.downloader,
                                callback=self.download_callback)
        self.task_text = ScrolledText(self,
                                      height=20,
                                      width=30,
                                      bg='light gray')
        self.print_text = ScrolledText(self,
                                       height=20,
                                       width=40,
                                       bg='light gray')
        self.root = root
        self.frames = [
            DownloadFrame(self, 'By Url or Id', self.queue, self.api,
                          self.task_text),
            SearchFrame(self, 'By Search', self.queue, self.api,
                        self.search_handler),
            RankingFrame(self, 'By Ranking', self.queue, self.api),
            RelatedFrame(self, 'By Related', self.queue, self.api)
        ]
        self.switch_menu = None
        self.init_ui()

    def init_ui(self):
        self.root.title("Pixiv Downloader")
        self.root.resizable(width=False, height=False)
        menubar = Menu(self)
        self.root.config(menu=menubar)
        self.switch_menu = Menu(menubar, tearoff=0)
        for frame in self.frames:
            frame.init_ui()
            frame.set_frames(self.frames)
            self.switch_menu.add_command(label=frame.name,
                                         command=frame.switch)
        menubar.add_cascade(label="Download Mode Switch",
                            menu=self.switch_menu)
        # 公共组件
        # text1.bind("<Key>", lambda e: "break")
        self.task_text.insert(END, 'Download Completed:\n')
        self.print_text.tag_configure('info', foreground='#3A98FE')
        self.print_text.tag_configure('warning', foreground='#ff9900')
        self.print_text.tag_configure('error', foreground='#FF2D21')
        quote = "Console Log:\n"
        self.print_text.insert(END, quote, 'info')
        self.task_text.grid(row=3, column=0, sticky=W)
        self.print_text.grid(row=3, column=1, sticky=W)

        banner = Label(self, text="Power by imn5100", width=30, height=5)
        banner.grid(row=4, columnspan=2)

        self.grid()
        self.frames[0].grid(row=1, columnspan=2)
        self.queue.run()

    def download_callback(self, msg=None):
        """
        任务完成回调
        :param msg: 回调消息
        :return: none
        """
        if msg:
            self.task_text.insert(END, msg)
        else:
            self.task_text.insert(END, "A work Done\n")
Example #6
0
class TextDemo(Demo):
   label = 'Text widget displaying source with cheap syntax highlighting:\n'+\
         '(Move mouse over text and watch indent-structure highlighting.)'
   font = ('Courier', 10, 'normal')
   bold = ('Courier', 10, 'bold')
   Highlights = {'#.*': Options(foreground='red'),
              r'\'.*?\'': Options(foreground='yellow'),
              r'\bdef\b\s.*:':Options(foreground='blue', spacing1=2),
              r'\bclass\b\s.*\n':Options(background='pink', spacing1=5),
              r'\b(class|def|for|in|import|from|break|continue)\b':
               Options(font=bold)
              }
   
   def __init__(self, master):
      Demo.__init__(self, master)
      self.text = ScrolledText(self, width=80, height=20,
                         font=self.font, background='gray65',
                         spacing1=1, spacing2=1, tabs='24')
      self.text.pack(side=TOP, expand=YES, fill=BOTH)

      content = open(sys.argv[0], 'r').read()
      self.text.insert(AtEnd(), content)

      reg = re.compile('([\t ]*).*\n')
      pos = 0
      indentTags = []
      while 1:
         match = reg.search(content, pos)
         if not match: break
         indent = match.end(1)-match.start(1)
         if match.end(0)-match.start(0) == 1:
            indent = len(indentTags)
         tagb = 'Tagb%08d' % match.start(0)
         tagc = 'Tage%08d' % match.start(0)
         self.text.tag_configure(tagc, background='', relief=FLAT, borderwidth=2)
         self.text.tag_add(tagb, Char( match.start(0)), Char(match.end(0)))
         self.text.tag_bind(tagb, '<Enter>',
                        lambda e,self=self,tagc=tagc: self.Enter(tagc))
         self.text.tag_bind(tagb, '<Leave>',
                        lambda e,self=self,tagc=tagc: self.Leave(tagc))
         del indentTags[indent:]
         indentTags.extend( (indent-len(indentTags))*[None] )
         indentTags.append(tagc)
         for tag in indentTags:
            if tag:
               self.text.tag_add(tag, Char(match.start(0)),
                             Char(match.end(0)))
         pos = match.end(0)

      for key,kw in self.Highlights.items():
         self.text.tag_configure(key, cnf=kw)
         reg = re.compile(key)
         pos = 0
         while 1:
            match = reg.search(content, pos)
            if not match: break
            self.text.tag_add(key, Char(match.start(0)),Char(match.end(0)))
            pos = match.end(0)

   def Enter(self, tag):
      self.text.tag_raise(tag)
      self.text.tag_configure(tag, background='gray80', relief=RAISED)

   def Leave(self, tag):
      self.text.tag_configure(tag, background='', relief=FLAT)
Example #7
0
class xbbtools_help(Toplevel):
    def __init__(self, *args):
        Toplevel.__init__(self)
        self.tid = ScrolledText(self)
        self.tid.pack(fill=BOTH, expand=1)
        self.Styles()
        self.Show()

    def Styles(self):
        for c in [
                'red', 'blue', 'magenta', 'yellow', 'green', 'red4', 'green4',
                'blue4'
        ]:
            self.tid.tag_configure(c, foreground=c)

        self.tid.tag_config('underline', underline=1)
        self.tid.tag_config('italic', font=('Courier', 6, 'italic'))
        self.tid.tag_config('bold', font=('Courier', 8, 'bold'))
        self.tid.tag_config('title', font=('Courier', 12, 'bold'))
        self.tid.tag_config('small', font=('Courier', 6, ''))
        self.tid.tag_config('highlight', background='gray')

    def Show(self):
        t = self.tid
        t.insert(END, "XBBtools Help\n", 'title')
        t.insert(
            END, """
Copyright 2001 by Thomas Sicheritz-Ponten.  All rights reserved.
This code is part of the Biopython distribution and governed by its
license.  Please see the LICENSE file that should have been included
as part of this package.\n
""", 'italic')
        t.insert(END, '[email protected]\n\n', 'blue')
        t.insert(END, '* Goto Field\n', 'bold')
        t.insert(END, '\tinserting one position moves cursor to position\n')
        t.insert(END, "\tinserting two positions, sperated by ':' ")
        t.insert(END, 'highlights', 'highlight')
        t.insert(END, ' selected range\n')
        t.insert(END, '\n')
        t.insert(END, '* Search\n', 'bold')
        t.insert(END, '\tambiguous dna values are\n')
        t.insert(
            END, """
                A: A
                C: C
                G: G
                T: T
                M: AC
                R: AG
                W: AT
                S: CG
                Y: CT
                K: GT
                V: ACG
                H: ACT
                D: AGT
                B: CGT
                X: GATC
                N: GATC

                """, 'small')
Example #8
0
class LogReader(object):

    def __init__(self, root, **kwargs):
        self.root = root
        self.filenameLog = kwargs.get('filenameLog', None)

        self.ClearDisplay = kwargs.get('ClearDisplay', False)
        ''' By default don't display Filenames and directory '''

        mainstyle = ttk.Style()
#         mainstyle.theme_use(mainstyle.theme_names()[0])
        mainstyle.configure('My.TFrame',
                            background='gray50',
                            foreground='gray97',
                            font="Monospace 12")

        Btnstyle = ttk.Style()
        Btnstyle.configure('My.TButton',
                           background='gray50',
                           foreground='gray97',
                           font="Monospace 12")
        root.title("Log reader v0.1")

        Chkstyle = ttk.Style()
        Chkstyle.configure('My.TCheckbutton',
                           background='gray50',
                           foreground='gray97',
                           font="Monospace 12")

        Chkstyle = ttk.Style()
        Chkstyle.configure('My.TLabel',
                           background='gray50',
                           foreground='gray97',
                           font="Monospace 12")

        root.title("Libretto v0.1")

        self.initParam()

        #======================================================================
        # Main Frame
        #======================================================================
        self.f0 = ttk.Frame(self.root, style='My.TFrame')
        self.f0.pack(expand=True, fill='both')
        LabelUP = ttk.Label(
            self.f0, text="Please select the log file then click on read",
            style='My.TLabel')
        LabelUP.pack(side="top")

        self.fDisp = ttk.Frame(self.f0, style='My.TFrame')
        self.fDisp.pack(expand=True, fill='both', side="right")

        self.fParam = ttk.Frame(self.f0, style='My.TFrame')
        self.fParam.pack(side="left")

        #======================================================================
        # Frame fDisp
        #======================================================================
        # Display stdout
        self.customFont = tkFont.Font(
            family="Helvetica", size=12)
        self.text_area = ScrolledText(
            self.fDisp, font=self.customFont, undo=True, background='gray20', foreground="gray92")
        self.text_area.pack(expand=True, fill='both')
        self.text_area.configure(state='normal')
        self.text_area.yview(END)
        self.text_area.after(0)

        self.text_area.tag_configure("comment",
                                     font="Helvetica 12",
                                     foreground="gray60")
        self.text_area.tag_configure("subsubTitle",
                                     font="Helvetica 12 bold")
        self.text_area.tag_configure("subTitle",
                                     font="Helvetica 14 bold")
        self.text_area.tag_configure("Title",
                                     font="Helvetica 16 bold")
        self.text_area.tag_configure("warn",
                                     font="Helvetica 12 bold",
                                     foreground="red")
        self.text_area.tag_configure("File",
                                     font="Helvetica 10",
                                     foreground="DarkSlateGray1")
        self.text_area.tag_configure("FileOther",
                                     font="Helvetica 10",
                                     foreground="plum")
        self.text_area.tag_configure("Directory",
                                     font="Helvetica 10",
                                     foreground="goldenrod1")
        self.text_area.tag_configure("validate",
                                     font="Helvetica 12 bold",
                                     foreground="OliveDrab1")
        self.text_area.tag_configure("param",
                                     font="Helvetica 12",
                                     foreground="wheat")

        # stdout redirection
        sys.stdout = StdoutRedirector(self.text_area)

        #======================================================================
        # Parameters
        #======================================================================

        # File Selection button
        SelectLogFileBtn = ttk.Button(
            self.fParam, text="Select the log file", style='My.TButton')
        SelectLogFileBtn.grid(row=0, column=0)
        SelectLogFileBtn.configure(command=lambda: self.set_LogFile())
        Label = ttk.Label(self.fParam, text="", style='My.TLabel')
        Label.grid(row=1, column=0)

        # Refresh button
        SelectLogFileBtn = ttk.Button(
            self.fParam, text="Refresh", style='My.TButton')
        SelectLogFileBtn.grid(row=2, column=0)
        SelectLogFileBtn.configure(command=lambda: self.RefreshLog())

        Label = ttk.Label(self.fParam, text="", style='My.TLabel')
        Label.grid(row=9, column=0)

        # Display Files button
        ChkBtnDispFilenames = ttk.Checkbutton(
            self.fParam, text="Display the filenames", style='My.TCheckbutton')
        ChkBtnDispFilenames.grid(row=10, column=0)
        ChkBtnDispFilenames.configure(variable=self.Entry_DispFilenames,
                                      command=lambda: self.set_DispFilenames())

        # Display Comments button
        ChkBtnDispCom = ttk.Checkbutton(
            self.fParam, text="Display the comments", style='My.TCheckbutton')
        ChkBtnDispCom.grid(row=11, column=0)
        ChkBtnDispCom.configure(variable=self.Entry_DispComment,
                                command=lambda: self.set_DispComment())

        # Display Directories button
        ChkBtnDispDir = ttk.Checkbutton(
            self.fParam, text="Display the directories", style='My.TCheckbutton')
        ChkBtnDispDir.grid(row=12, column=0)
        ChkBtnDispDir.configure(variable=self.Entry_DispDir,
                                command=lambda: self.set_DispDir())

        # Display Warnings button
        ChkBtnDispWarn = ttk.Checkbutton(
            self.fParam, text="Display the warnings", style='My.TCheckbutton')
        ChkBtnDispWarn.grid(row=13, column=0)
        ChkBtnDispWarn.configure(variable=self.Entry_DispWarn,
                                 command=lambda: self.set_DispWarn())

        # Display Warnings button
        ChkBtnDispParam = ttk.Checkbutton(
            self.fParam, text="Display the parameters", style='My.TCheckbutton')
        ChkBtnDispParam.grid(row=14, column=0)
        ChkBtnDispParam.configure(variable=self.Entry_DispParam,
                                  command=lambda: self.set_DispParam())

        self.RefreshLog()
        if self.ClearDisplay:
            self.UpdateAllDisp()

    def printExample(self):
        return ["=========================================",
                "= Welcome in the Log Reader        ",
                "=========================================",
                "", "=== This is a big title ! ===",
                "    ~ A little bit different of this sub-Title",
                "        *  And guess what, now a sub-sub-Title ! ",
                "            # right now I am commenting my example",
                "            p_Dodo is a parameter",
                "            BlablablaAudyIsTheBestBlablabla...",
                "            X_something is a most of the time the filename of " +
                " an MRI image or created",
                "            Z_something is the filename of a file which is not an " +
                "mri image (it can be some stats or a matrix)",
                "            D_anotherthing is a directory",
                "    > Well this task seems to be a success",
                "    !! But this is a message you should read"]

    def initParam(self):
        self.logExtension = ("*.txt", "*.log")

        self.LogMessage = None
        self.listHideBal = []
        global DefaultFileNameMRIImage
        global DefaultFileNameMRIMasks
        global DefaultFileNameOther
        global DefaultFileNameMatrix
        global DefaultDirectory
        global DefaultName
        global DefaultWarning
        global DefaultTitle
        global DefaultSubTitle
        global DefaultSubSubTitle
        global DefaultComment
        global DefaultValidate
        global DefaultParameter

        self.DefaultFileNameMRIImage = DefaultFileNameMRIImage
        self.DefaultFileNameMRIMasks = DefaultFileNameMRIMasks
        self.DefaultFileNameOther = DefaultFileNameOther
        self.DefaultFileNameMatrix = DefaultFileNameMatrix
        self.DefaultDirectory = DefaultDirectory
        self.DefaultName = DefaultName
        self.DefaultWarning = DefaultWarning
        self.DefaultTitle = DefaultTitle
        self.DefaultSubTitle = DefaultSubTitle
        self.DefaultSubSubTitle = DefaultSubSubTitle
        self.DefaultComment = DefaultComment
        self.DefaultValidate = DefaultValidate
        self.DefaultParameter = DefaultParameter

        self.DispWarn = True
        self.Entry_DispWarn = IntVar(value=1)
        self.DispParam = True
        self.Entry_DispParam = IntVar(value=1)

        if self.ClearDisplay:
            self.DispFilenames = False
            self.Entry_DispFilenames = IntVar(value=0)
            self.DispComment = False
            self.Entry_DispComment = IntVar(value=0)
            self.DispDir = False
            self.Entry_DispDir = IntVar(value=0)
        else:
            self.DispFilenames = True
            self.Entry_DispFilenames = IntVar(value=1)
            self.DispComment = True
            self.Entry_DispComment = IntVar(value=1)
            self.DispDir = True
            self.Entry_DispDir = IntVar(value=1)

    def DispOrHide(self, DispSomething, listHideBal):
        if DispSomething:
            # update the balise to hide list
            listHideBal = [e for e in self.listHideBal if e not in listHideBal]
        else:
            listHideBal = listHideBal + self.listHideBal
        self.listHideBal = listHideBal
        self.resetDisp()
        self.watchLog(bal=self.listHideBal)

    def set_DispFilenames(self):
        self.DispFilenames = self.Entry_DispFilenames.get()
        listbal0 = [self.DefaultFileNameMRIImage,
                    self.DefaultFileNameMatrix,
                    self.DefaultFileNameMRIMasks,
                    self.DefaultFileNameOther]
        self.DispOrHide(self.DispFilenames, listbal0)

    def set_DispDir(self):
        self.DispDir = self.Entry_DispDir.get()
        listbal0 = [self.DefaultDirectory]
        self.DispOrHide(self.DispDir, listbal0)

    def set_DispComment(self):
        self.DispComment = self.Entry_DispComment.get()
        listbal0 = [self.DefaultComment]
        self.DispOrHide(self.DispComment, listbal0)

    def set_DispWarn(self):
        self.DispWarn = self.Entry_DispWarn.get()
        listbal0 = [self.DefaultWarning]
        self.DispOrHide(self.DispWarn, listbal0)

    def set_DispParam(self):
        self.DispParam = self.Entry_DispParam.get()
        listbal0 = [self.DefaultParameter]
        self.DispOrHide(self.DispParam, listbal0)

    def UpdateAllDisp(self):
        self.set_DispComment()
        self.set_DispDir()
        self.set_DispFilenames()

    def set_LogFile(self):
        self.filenameLog = self.OpenFile(
            self.OriginalDirectory, self.logExtension)
        self.RefreshLog()

    def filterLog(self, str0, bal):
        ''' delete the lines which contain a specific balise '''
        if type(bal) != list:
            if type(bal) != str:
                if not bal:
                    return str0
            else:
                bal = [bal]
        try:
            str1 = DeleteTabulation(str0)
        except:
            str1 = str0

        for e in bal:
            if str1[0:len(e)] == e:
                return None
        return str0

    def loadLog(self):
        ''' load a log '''
        if self.filenameLog:
            with open(self.filenameLog) as f:
                self.LogMessage = f.read().splitlines()
        else:
            self.LogMessage = self.printExample()

    def watchLog(self, bal=None):
        ''' display the log '''
        for line in self.LogMessage:
            l = self.filterLog(line, bal)
            if l:
                print l

    def resetDisp(self):
        ''' '''
        self.text_area.delete('1.0', END)

    def RefreshLog(self):
        self.loadLog()
        self.resetDisp()
        self.watchLog(bal=self.listHideBal)

    def OpenFile(self, initialdir0="", filetype='*.*'):
        if filetype != '*.*':
            filetypes0 = (("Files", filetype),
                          ("All Files", "*.*"))
            name = askopenfilename(initialdir=initialdir0,
                                   filetypes=filetypes0,
                                   title="Choose a file.")
        else:
            name = askopenfilename(initialdir=initialdir0,
                                   title="Choose a file.")
        return name

    def close(self):
        print "close"
        exit()
class ClustererGui(ttk.Frame):
    """GUI to open/save xml/text-files and visualize clustering."""

    def __init__(self, master=None):
        """Init GUI - get auto-split-sentences-option and standard test-file-folder from config-file."""
        ttk.Frame.__init__(self, master)
        self.grid(sticky=tk.N+tk.S+tk.E+tk.W)

        self.createWidgets()
        self.filepath = None
        self.xml_filepath = None
        self.filename = None
        self.article_id = None
        self.extraction = None
        self.author_no = None
        self.correct = None
        self.result = None
        self.colors = []

        config = ConfigParser.ConfigParser()
        config.read("config.cfg")
        params = dict(config.items("params"))
        article_dir = params['test_file_dir']
        self.auto_split_sentences = bool(int(params['auto_split_sentences']))
        self.show_knee_point = bool(int(params['show_knee_point']))
        self.show_knee_point = False # currently not supported in GUI-mode
        self.last_dir = article_dir


    def createWidgets(self):
        """Organize GUI."""
        top=self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)

        self.rowconfigure(0, weight=1)
        self.rowconfigure(1, weight=0)
        self.columnconfigure(0, weight=1)
        self.columnconfigure(1, weight=0)

        left_frame = ttk.Frame(self, relief="raised", borderwidth=1)
        left_frame.grid(row=0, column=0, sticky=tk.N+tk.S+tk.E+tk.W)
        left_frame.rowconfigure(0, weight=0)
        left_frame.rowconfigure(1, weight=1)
        left_frame.columnconfigure(0, weight=1)
        
        buttons_topleft = ttk.Frame(left_frame)
        buttons_topleft.grid(row=0, column=0)

        self.choose_file_btn = ttk.Button(buttons_topleft, text='choose file...',
            command=self.choose_file)
        self.choose_file_btn.grid(row=0, column=0)

        self.save_file_btn = ttk.Button(buttons_topleft, text='save file...',
            command=self.save_file)
        self.save_file_btn.grid(row=0, column=1)
        
        self.extract_feat_btn = ttk.Button(buttons_topleft, text='process',
            command=self.start_featureextr_thread)
        self.extract_feat_btn.grid(row=0, column=2)

        right_frame = ttk.Frame(self)
        right_frame.grid(row=0, column=1, sticky=tk.N+tk.S+tk.E+tk.W)
        right_frame.rowconfigure(0, weight=1)
        right_frame.rowconfigure(1, weight=1)
        
        self.distr_entry = ScrolledText(right_frame, width=30, height=30)
        self.distr_entry.grid(row=0, column=0, columnspan=2, sticky=tk.N)

        self.test_entry = ScrolledText(right_frame, width=30)
        self.test_entry.grid(row=1, column=0, columnspan=2, sticky=tk.N)

        self.scrolledText = ScrolledText(left_frame, undo=True, wrap=tk.WORD)
        self.scrolledText['font'] = ('Helvetica', '12')
        self.scrolledText.tag_configure('lines', background="#dddddd", foreground="black", font=('Helvetica', 9))
        self.scrolledText.tag_configure('blanks', background="#ffffff", foreground="black", font=('Helvetica', 9))        
        self.scrolledText.grid(row=1, column=0, sticky=tk.N+tk.S+tk.E+tk.W)

        status_bar = ttk.Frame(self)
        status_bar.grid(row=1, column=0, columnspan=2, sticky=tk.W)
        status_bar.columnconfigure(0, weight=1, minsize=100)
        status_bar.columnconfigure(1, weight=1)

        self.status = tk.StringVar()
        self.status.set("ready")
        self.status_label = ttk.Label(status_bar, textvariable=self.status)
        self.status_label.grid(row=0, column=1, padx=10)

        self.progressbar = ttk.Progressbar(status_bar, mode='indeterminate', length=200)
        self.progressbar.grid(row=0, column=0, padx=3)
    
    def choose_file(self):
        """Choose text or xml file dialog."""
        self.filepath = askopenfilename(initialdir=self.last_dir, filetypes=(("text and xml files", ("*.txt","*.xml")),))
        if self.filepath:
            ext = os.path.splitext(self.filepath)[1]

            if ext == ".xml":
                '''save raw-text of xml-file to a new file and print it'''
                self.xml_filepath = self.filepath
                self.filepath = self.create_text_fromXML()

            base = os.path.split(self.filepath)[0]
            self.article_id = os.path.split(base)[1]
            self.filename = os.path.split(self.filepath)[1]
            self.scrolledText.delete(1.0, tk.END)
            self.print_raw_text()
            self.scrolledText.edit_reset()

    def create_text_fromXML(self):
        """Create text-file out of given xml-file."""
        new_filepath = os.path.splitext(self.filepath)[0] + ".txt"
        with codecs.open(self.filepath, 'r', 'UTF-8') as xml_file:
            xml_tree = etree.parse(xml_file)
        
        with codecs.open(new_filepath, 'w', 'UTF-8') as newFile:
            first_entry = True
            for entry in xml_tree.getroot():
                if entry.text is not None:
                    if not first_entry:
                        newFile.write("\n\n")
                    else:
                        first_entry = False
                    newFile.write(entry.text)
        return new_filepath



    def save_file(self):
        """Save text-file-dialog."""
        text = self.scrolledText.get("0.0", tk.END)
        if self.filepath is None:
            name = asksaveasfilename(initialdir=self.last_dir, defaultextension=".txt")
            if name:
                self.filepath = name
            else:
                return
        try:
            with codecs.open(self.filepath, 'w', 'UTF-8') as newFile:
                newFile.write(text.strip())
            self.scrolledText.edit_reset()
            base = os.path.split(self.filepath)[0]
            self.article_id = os.path.split(base)[1]
            self.filename = os.path.split(self.filepath)[1]
            return True
        except Exception:# as e:
            raise


    def start_featureextr_thread(self):
        """Start thread for feature extraction."""
        self.distr_entry.delete(1.0, tk.END)
        self.status.set("processing...")
        if self.filepath is None or self.article_id is None:
            tkMessageBox.showwarning(
                "Save File",
                "Save file for feature extraction.")
            return
        try:
            self.scrolledText.edit_undo()
            self.scrolledText.edit_redo()

            tkMessageBox.showwarning(
                "File changed",
                "File was changed, please save.")
            return
        except tk.TclError:
            self.extraction = clusterer.Clusterer(self.article_id, self.filepath, self.xml_filepath, self.auto_split_sentences, self.show_knee_point)

            self.ftr_extr_thread = threading.Thread(target=self.extract_features)
            self.ftr_extr_thread.daemon = True
            self.progressbar.start()
            self.ftr_extr_thread.start()
            self.after(1000, self.check_feat_thread)

    def check_feat_thread(self):
        """Check if feature extraction thread is still working - if not: visualize cluster-results."""
        if self.ftr_extr_thread.is_alive():
            self.after(1000, self.check_feat_thread)
        else:
            self.status.set("ready")

            # generate author-colormap
            self.colors = [None]*len(set(self.clusters))
            for k in set(self.clusters):  
                temp_color = plt.cm.spectral(np.float(k) / (np.max(self.clusters) + 1))
                if k == 0:
                    temp_color = plt.cm.spectral(0.05)
                self.colors[k] = self.convert_to_hex(temp_color)
            self.configure_colors()

            self.progressbar.stop()
            self.print_author_distr()
            self.print_text()
            if self.correct is not None and self.author_no is not None:
                self.test_entry.delete(1.0, tk.END)
                s = "authors found: {}".format(len(set(self.clusters)))
                s += "\n believe-score: {:.4f}".format(self.believe_score)
                s += "\n\n true number of authors: {}".format(self.author_no)
                s += "\n precision: {:.4f}".format(self.scores[0])
                s += "\n recall: {:.4f}".format(self.scores[1])
                s += "\n f1-score: {:.4f}".format(self.scores[2])
                s += "\n adjusted-rand-index: {:.4f}".format(self.scores[3])
                self.test_entry.insert(tk.INSERT, s)
            else:
                self.test_entry.delete(1.0, tk.END)
                s = "authors found: {}".format(len(set(self.clusters)))
                s += "\n believe-score: {:.4f}".format(self.believe_score)
                self.test_entry.insert(tk.INSERT, s)

    def extract_features(self):
        """Start feature extraction."""
        self.clusters, self.result, self.author_no, self.believe_score, self.scores = self.extraction.calc_cluster()

        if self.result is not None:
            c = Counter(self.result)
            self.correct = c[True] / sum(c.values()) * 100

    def print_text(self):
        """Print raw text with specified author-colors."""
        self.scrolledText.delete(1.0, tk.END)
        f = open(self.filepath)

        line_number = 0
        actual_line_number = 0
        for line in f:
            actual_line_number += 1
            if line.strip():
                s = str(line_number) + ' '+str(self.clusters[line_number]) + ' '+line
                s = line
                line_cluster = str(line_number) + ' '+str(self.clusters[line_number])+ ' '
                line_cluster = ('{:^'+str(14-len(line_cluster))+'}').format(line_cluster)
                self.scrolledText.insert(tk.INSERT, line_cluster, 'lines')
                try:
                    self.scrolledText.insert(tk.INSERT, s, str(self.clusters[line_number]))
                    # if self.result[line_number]:
                    #     # correct assignment - print text foreground in white
                    #     self.scrolledText.insert(tk.INSERT, s, str(self.clusters[line_number]))
                    # else:
                    #     # false assignment - print text foreground in black
                    #     self.scrolledText.insert(tk.INSERT, s, str(self.clusters[line_number]*10**2))
                except IndexError:
                    self.scrolledText.insert(tk.INSERT, s)
                except TypeError:
                        self.scrolledText.insert(tk.INSERT, s, str(self.clusters[line_number]))
                line_number += 1
            else:
                s = line
                self.scrolledText.insert(tk.INSERT, s, 'blanks')
        f.close()

    def print_raw_text(self):
        """Print raw text."""
        f = open(self.filepath)
        for line in f:
            self.scrolledText.insert(tk.INSERT, line)
        f.close()

    def get_distribution(self, l=None):
        """Return Counter with author distribution in percent."""
        if l is None:
            l = self.clusters
        counter = Counter(l)
        sum_counter = sum(counter.values())

        for key in counter.iterkeys():
            counter[key] = counter[key] / sum_counter * 100
        return counter

    def print_author_distr(self):
        """Print author distribution with specified author-colors."""
        self.distr_entry.delete(1.0, tk.END)
        distr = self.get_distribution(self.clusters)

        for index, count in distr.most_common():
            author_i = "author "+str(index)+"{:>20}%\n".format(locale.format(u'%.2f',count))
            self.distr_entry.insert(tk.INSERT, author_i, str(index))

    def convert_to_hex(self, col):
        """Convert inter-tuple to hex-coded string."""
        red = int(col[0]*255)
        green = int(col[1]*255)
        blue = int(col[2]*255)
        return '#{r:02x}{g:02x}{b:02x}'.format(r=red,g=green,b=blue)

    def configure_colors(self):
        """Configure author-specific colors for author-distribution and cluster-results."""
        for i,c in enumerate(self.colors):
            self.scrolledText.tag_configure(str(i), background=c, foreground="white")            
            self.distr_entry.tag_configure(str(i), background=c, foreground="white")
Example #10
0
class TextDemo(Demo):
	label = 'Text widget displaying source with cheap syntax highlighting:\n'+\
			'(Move mouse over text and watch indent-structure highlighting.)'
	font = ('Courier', 10, 'normal')
	bold = ('Courier', 10, 'bold')
	Highlights = {'#.*': Options(foreground='red'),
				  r'\'.*?\'': Options(foreground='yellow'),
				  r'\bdef\b\s.*:':Options(foreground='blue', spacing1=2),
				  r'\bclass\b\s.*\n':Options(background='pink', spacing1=5),
				  r'\b(class|def|for|in|import|from|break|continue)\b':
				   Options(font=bold)
				  }
	
	def __init__(self, master):
		Demo.__init__(self, master)
		self.text = ScrolledText(self, width=80, height=20,
								 font=self.font, background='gray65',
								 spacing1=1, spacing2=1, tabs='24')
		self.text.pack(side=TOP, expand=YES, fill=BOTH)

		content = open(sys.argv[0], 'r').read()
		self.text.insert(AtEnd(), content)

		reg = re.compile('([\t ]*).*\n')
		pos = 0
		indentTags = []
		while 1:
			match = reg.search(content, pos)
			if not match: break
			indent = match.end(1)-match.start(1)
			if match.end(0)-match.start(0) == 1:
				indent = len(indentTags)
			tagb = 'Tagb%08d' % match.start(0)
			tagc = 'Tage%08d' % match.start(0)
			self.text.tag_configure(tagc, background='', relief=FLAT, borderwidth=2)
			self.text.tag_add(tagb, Char( match.start(0)), Char(match.end(0)))
			self.text.tag_bind(tagb, '<Enter>',
							   lambda e,self=self,tagc=tagc: self.Enter(tagc))
			self.text.tag_bind(tagb, '<Leave>',
							   lambda e,self=self,tagc=tagc: self.Leave(tagc))
			del indentTags[indent:]
			indentTags.extend( (indent-len(indentTags))*[None] )
			indentTags.append(tagc)
			for tag in indentTags:
				if tag:
					self.text.tag_add(tag, Char(match.start(0)),
									  Char(match.end(0)))
			pos = match.end(0)

		for key,kw in self.Highlights.items():
			self.text.tag_configure(key, cnf=kw)
			reg = re.compile(key)
			pos = 0
			while 1:
				match = reg.search(content, pos)
				if not match: break
				self.text.tag_add(key, Char(match.start(0)),Char(match.end(0)))
				pos = match.end(0)

	def Enter(self, tag):
		self.text.tag_raise(tag)
		self.text.tag_configure(tag, background='gray80', relief=RAISED)

	def Leave(self, tag):
		self.text.tag_configure(tag, background='', relief=FLAT)