<commit_before># -*- coding: utf-8 -*-
"""
Graphical user interface for SNN toolbox.

Features
--------

    - Allows setting parameters and what tools to use during an experiment.
    - Performs basic checks that specified parameters are valid.
    - Preferences can be saved and reloaded.
    - Tooltips explain the functionality.
    - Automatically recognizes result plots and allows displaying them in a
      separate window.

Created on Mon Apr 11 10:25:53 2016

@author: rbodo
"""

from __future__ import with_statement

import os
import sys
import webbrowser
import json
import threading
from textwrap import dedent
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.gridspec as gridspec

import snntoolbox
from snntoolbox.config import settings, pyNN_settings, update_setup
from snntoolbox.config import model_libs
from snntoolbox.config import simulators, simulators_pyNN
from snntoolbox.gui.tooltip import ToolTip
from snntoolbox.core.pipeline import test_full

if sys.version_info[0] < 3:
    import Tkinter as tk
    import tkFileDialog as filedialog
    import tkMessageBox as messagebox
    import tkFont as font
    from Queue import Queue
else:
    import tkinter as tk
    from tkinter import filedialog, messagebox, font
    from queue import Queue


class SNNToolboxGUI():
    """Main Class of SNN Toolbox."""

    def __init__(self, root):
        """init method of SNNToolboxGUI."""
        self.initialized = False
        self.root = root
        self.default_path_to_pref = os.path.join(snntoolbox._dir,
                                                 'preferences')
        self.define_style()
        self.declare_parameter_vars()
        self.load_settings()
        self.main_container = tk.Frame(root, bg='white')
        self.main_container.pack(side='top', fill='both', expand=True)
        self.globalparams_widgets()
        self.cellparams_widgets()
        self.simparams_widgets()
        self.tools_widgets()
        self.graph_widgets()
        self.top_level_menu()
        self.toggle_state_pyNN(self.settings['simulator'].get())
        self.toggle_percentile_state()
        self.toggle_poisson_input_state()
        self.initialized = True

    def define_style(self):
        """Defien apperance style."""
        self.padx = 10
        self.pady = 5
        fontFamily = 'clearlyu devagari'
        self.header_font = (fontFamily, '11', 'bold')
        font.nametofont('TkDefaultFont').configure(family=fontFamily, size=11)
        font.nametofont('TkMenuFont').configure(family=fontFamily, size=11,
                                                weight=font.BOLD)
        font.nametofont('TkTextFont').configure(family=fontFamily, size=11)
        self.kwargs = {'fill': 'both', 'expand': True,
                       'padx': self.padx, 'pady': self.pady}

    def initialize_thread(self):
        """separate thread for conversion process."""
        self.res_queue = Queue()
        # Create thread for performing the conversion in the background.
        # Make it a daemon so it is killed when the main application is closed.
        self.process_thread = threading.Thread(target=test_full,
                                               args=(self.res_queue,),
                                               name='conversion process',
                                               daemon=True)

    def globalparams_widgets(self):
        """Global parameters widgets."""
        # Create a container for individual parameter widgets
        self.globalparams_frame = tk.LabelFrame(self.main_container,
                                                labelanchor='nw',
                                                text="Global parameters",
                                                relief='raised',
                                                borderwidth='3', bg='white')

        self.globalparams_frame.pack(side='left', fill=None, expand=False)
        tip = dedent("""\
              Specify general properties of your model and the steps to
              include in your experiment.""")
        ToolTip(self.globalparams_frame, text=tip, wraplength=750, delay=1499)

        # Dataset
        dataset_frame = tk.Frame(self.globalparams_frame, bg='white')
        dataset_frame.pack(**self.kwargs)
        tk.Button(dataset_frame, text="Load dataset",
                  command=self.set_dataset,
                  font=self.header_font).pack(side='top')
        self.dataset_entry = tk.Entry(
            dataset_frame, textvariable=self.settings['dataset_path'],
            width=20, validate='focusout', bg='white',<commit_msg>python 2 has no daemon parameter in threading<commit_after># -*- coding: utf-8 -*-
"""
Graphical user interface for SNN toolbox.

Features
--------

    - Allows setting parameters and what tools to use during an experiment.
    - Performs basic checks that specified parameters are valid.
    - Preferences can be saved and reloaded.
    - Tooltips explain the functionality.
    - Automatically recognizes result plots and allows displaying them in a
      separate window.

Created on Mon Apr 11 10:25:53 2016

@author: rbodo
"""

from __future__ import with_statement

import os
import sys
import webbrowser
import json
import threading
from textwrap import dedent
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.backends.backend_tkagg import NavigationToolbar2TkAgg
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import matplotlib.gridspec as gridspec

import snntoolbox
from snntoolbox.config import settings, pyNN_settings, update_setup
from snntoolbox.config import model_libs
from snntoolbox.config import simulators, simulators_pyNN
from snntoolbox.gui.tooltip import ToolTip
from snntoolbox.core.pipeline import test_full

if sys.version_info[0] < 3:
    import Tkinter as tk
    import tkFileDialog as filedialog
    import tkMessageBox as messagebox
    import tkFont as font
    from Queue import Queue
else:
    import tkinter as tk
    from tkinter import filedialog, messagebox, font
    from queue import Queue


class SNNToolboxGUI():
    """Main Class of SNN Toolbox."""

    def __init__(self, root):
        """init method of SNNToolboxGUI."""
        self.initialized = False
        self.root = root
        self.default_path_to_pref = os.path.join(snntoolbox._dir,
                                                 'preferences')
        self.define_style()
        self.declare_parameter_vars()
        self.load_settings()
        self.main_container = tk.Frame(root, bg='white')
        self.main_container.pack(side='top', fill='both', expand=True)
        self.globalparams_widgets()
        self.cellparams_widgets()
        self.simparams_widgets()
        self.tools_widgets()
        self.graph_widgets()
        self.top_level_menu()
        self.toggle_state_pyNN(self.settings['simulator'].get())
        self.toggle_percentile_state()
        self.toggle_poisson_input_state()
        self.initialized = True

    def define_style(self):
        """Defien apperance style."""
        self.padx = 10
        self.pady = 5
        fontFamily = 'clearlyu devagari'
        self.header_font = (fontFamily, '11', 'bold')
        font.nametofont('TkDefaultFont').configure(family=fontFamily, size=11)
        font.nametofont('TkMenuFont').configure(family=fontFamily, size=11,
                                                weight=font.BOLD)
        font.nametofont('TkTextFont').configure(family=fontFamily, size=11)
        self.kwargs = {'fill': 'both', 'expand': True,
                       'padx': self.padx, 'pady': self.pady}

    def initialize_thread(self):
        """separate thread for conversion process."""
        self.res_queue = Queue()
        # Create thread for performing the conversion in the background.
        # Make it a daemon so it is killed when the main application is closed.
        if sys.version_info[0] < 3:
            self.process_thread = threading.Thread(target=test_full,
                                                   args=(self.res_queue,),
                                                   name='conversion process')
        else:
            self.process_thread = threading.Thread(target=test_full,
                                                   args=(self.res_queue,),
                                                   name='conversion process',
                                                   daemon=True)

    def globalparams_widgets(self):
        """Global parameters widgets."""
        # Create a container for individual parameter widgets
        self.globalparams_frame = tk.LabelFrame(self.main_container,
                                                labelanchor='nw',
                                                text="Global parameters",
                                                relief='raised',
                                                borderwidth='3', bg='white')

        self.globalparams_frame.pack(side='left', fill=None, expand=False)
        tip = dedent("""\
              Specify general properties of your model and the steps to
              include in your experiment.""")
        ToolTip(self.globalparams_frame, text=tip, wraplength=750, delay=1499)

        # Dataset
        dataset_frame = tk.Frame(self.globalparams_frame, bg='white')
        dataset_frame.pack(**self.kwargs)
        tk.Button(dataset_frame, text="Load dataset",
                  command=self.set_dataset,
                  font=self.header_font).pack(side='top')
        self.dataset_entry = tk.Entry(
            dataset_frame, textvariable=self.settings['dataset_path'],
            width=20, validate='focusout', bg='white',
{'fraction_non_alphanumeric': 0.0866301481030635, 'fraction_numerical': 0.007810914992899168, 'mean_word_length': 2.199935086011035, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 40, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3475564', 'n_tokens_mistral': 2648, 'n_tokens_neox': 2540, 'n_words': 679}