def plot():
     try:
         plt.close()
     except:
         pass
     plt.ioff()
     days_passed = len(AVG_GENE_TIME[0, 0, :])
     g_idx = gene_var.get()
     for s in range(len(SPECIES_LIST)):
         if species_checkbox_vars[s].get() == 1:
             plt.plot(range(days_passed),
                      AVG_GENE_TIME[g_idx, s],
                      label=SPECIES_LIST[s].name,
                      color=SPECIES_LIST[s].color)
     plt.legend(frameon=False)
     plt.xlabel('Days passed')
     plt.ylabel('Average ' + data.neaten_string(GENES[g_idx]))
     plt.show()
 def plot():
     try:
         plt.close()
     except:
         pass
     plt.ioff()
     days_passed = len(TRAIT_PROP_TIME[0, 0, :])
     tr_idx = trait_var.get()
     for s in range(len(SPECIES_LIST)):
         if species_checkbox_vars[s].get() == 1:
             plt.plot(range(days_passed),
                      TRAIT_PROP_TIME[tr_idx, s],
                      label=SPECIES_LIST[s].name,
                      color=SPECIES_LIST[s].color)
     plt.legend(frameon=False)
     plt.xlabel('Days passed')
     plt.ylabel('Proportion with ' +
                data.neaten_string(TRAITS[tr_idx] + ' / %'))
     plt.show()
    def edit_menu(species):
        '''Edit or add a species menu'''
        def change_color():
            global species_color
            species_color = get_color()

        def ok2():
            '''ok to edit menu'''
            commands = ["global SPECIES_LIST"]
            command_start = "SPECIES_LIST[" + str(species.index) + "]."

            # profile
            species.diet = diet.get()
            species.name = name_entry.get()
            species.color = species_color

            # genes
            for g in range(len(GENES)):
                command = (command_start + GENES[g] + ' = ' +
                           str(gene_entries[g].get()))
                commands.append(command)
                command = (command_start + GENES[g] + '_std' + ' = ' +
                           str(std_entries[g].get()))
                commands.append(command)
            execute(commands)  # cannot use exec in local local code

            # traits
            species.traits = []
            t = -1
            for trait, prob in species_traits:
                t += 1
                prob = float(trait_prob_entry[t].get())
                species.traits.append((trait, prob))

            global SPECIES_LIST
            SPECIES_LIST = data.organise_species_list(SPECIES_LIST)
            update_topframe()  # updates lower level (wildlife menu) root
            root.destroy()

        root = tk.Tk()
        if species.name:
            root.title("Edit " + species.name)
        else:
            root.title("Add Species")
        root.geometry('%dx%d+%d+%d' % (400, 600, 950, 150))

        topFrame = tk.Frame(root)
        topFrame.pack(side=tk.TOP)
        bottomFrame = tk.Frame(root)
        bottomFrame.pack(side=tk.BOTTOM)

        # Notebook
        notebook = ttk.Notebook(topFrame, width=300, height=500)
        profile_page = tk.Frame(notebook)
        genetic_page = tk.Frame(notebook)
        trait_page = tk.Frame(notebook)
        notebook.add(profile_page, text="Profile")
        notebook.add(genetic_page, text="Genetics")
        notebook.add(trait_page, text="Traits")
        notebook.pack()

        ok_button = tk.Button(root, text="Ok", command=ok2)
        ok_button.pack(side=tk.BOTTOM)

        # Genetics #

        tk.Label(genetic_page, text="Mean").grid(row=0, column=1)
        tk.Label(genetic_page, text="Standard Dev.").grid(row=0, column=2)

        # Gne Entries
        gene_entries = []  # list of tk entries mean values
        std_entries = []  # list of tk entries std values
        g = -1
        for gene in GENES:  # mechanics global variable containing list of gene strings
            g += 1  # Genetics page
            tk.Label(genetic_page,
                     text=data.neaten_string(gene)).grid(row=g + 1, column=0)
            gene_entries.append(tk.Entry(genetic_page, width=4))
            gene_entries[g].grid(row=g + 1, column=1)
            gene_value = eval("species." + gene)
            std_entries.append(tk.Entry(genetic_page, width=4))
            std_entries[g].grid(row=g + 1, column=2)
            std_value = eval("species." + gene + "_std")
            if gene_value != None:
                gene_entries[g].insert(0, str(gene_value))
            if std_value != None:
                std_entries[g].insert(0, str(std_value))

        # Profile #

        # Name
        tk.Label(profile_page, text="Name").grid(row=1, column=0)
        name_entry = tk.Entry(profile_page, width=10)
        if species.name:
            name_entry.insert(5, species.name)
        name_entry.grid(row=2, column=0)

        # Diet
        tk.Label(profile_page, text="Diet").grid(row=1, column=1)
        diet = tk.StringVar(profile_page)
        diet.set(species.diet)  # set the default option
        diet_listbox = tk.OptionMenu(profile_page, diet, *data.DIET_ORDER)
        diet_listbox.grid(row=2, column=1)

        # Colour
        global species_color
        species_color = species.color
        tk.Button(profile_page, text='Colour',
                  command=change_color).grid(row=3, column=0)

        # Traits #

        def update_traitpage():
            def add_trait():
                trait = trait_var.get()
                if trait == 'None':
                    return None
                for t in range(
                        len(species_traits)):  # if trait not in species_traits
                    if species_traits[t][0] == trait:
                        return None  # species already has trait
                species_traits.append(
                    (trait,
                     0))  # will only reach here if species does not have trait
                update_traitpage()

            for widget in trait_page.winfo_children():
                widget.destroy()  # prevents overlapping

            available_traits = TRAITS[:]  # indexing clones list, removes attachment between 2
            for trait, prob in species_traits:
                available_traits.remove(trait)
            if len(available_traits) == 0:
                available_traits.append('None')

            # add traits
            trait_var = tk.StringVar(trait_page)
            trait_listbox = tk.OptionMenu(trait_page, trait_var,
                                          *available_traits)
            trait_listbox.grid(row=0, column=1)
            trait_listbox.config(width=10)
            add_trait_button = tk.Button(trait_page,
                                         text="Add",
                                         command=add_trait)
            add_trait_button.grid(row=0, column=0)

            # Existing traits
            tk.Label(trait_page, text="Trait").grid(row=1, column=1)
            tk.Label(trait_page, text="Fraction").grid(row=1, column=2)

            for entry in trait_prob_entry:
                trait_prob_entry.pop(0)  # empty without reassignment
            t = -1
            for trait, prob in species_traits:
                t += 1
                tk.Label(trait_page, text=trait).grid(row=t + 2, column=1)
                entry = tk.Entry(trait_page, width=4)
                entry.insert(0, str(prob))
                trait_prob_entry.append(entry)
                trait_prob_entry[t].grid(row=t + 2, column=2)

        species_traits = species.traits[:]  # copy to be loaded into data in ok2 function (will only contain 0 for prob)
        trait_prob_entry = []  # to be filled with tkinter entries
        update_traitpage()

        root.mainloop()
    def plot_traits():
        def cancel():
            root.destroy()

        def plot():
            try:
                plt.close()
            except:
                pass
            plt.ioff()
            days_passed = len(TRAIT_PROP_TIME[0, 0, :])
            tr_idx = trait_var.get()
            for s in range(len(SPECIES_LIST)):
                if species_checkbox_vars[s].get() == 1:
                    plt.plot(range(days_passed),
                             TRAIT_PROP_TIME[tr_idx, s],
                             label=SPECIES_LIST[s].name,
                             color=SPECIES_LIST[s].color)
            plt.legend(frameon=False)
            plt.xlabel('Days passed')
            plt.ylabel('Proportion with ' +
                       data.neaten_string(TRAITS[tr_idx] + ' / %'))
            plt.show()

        root = tk.Tk()
        root.title("Plot traits")
        root.geometry('%dx%d+%d+%d' % (400, 600, 950, 150))
        topFrame = tk.Frame(root)
        topFrame.pack(side=tk.TOP)
        bottomFrame = tk.Frame(root)
        bottomFrame.pack(side=tk.BOTTOM)

        # Radiobutton for which gene to plot
        tk.Label(topFrame, text="Trait").grid(row=1, column=1)
        trait_var = tk.IntVar(topFrame)
        for tr in range(len(TRAITS)):
            tk.Radiobutton(topFrame,
                           text=data.neaten_string(TRAITS[tr]),
                           variable=trait_var,
                           value=tr).grid(row=2 + tr, column=1)
        trait_var.set(TRAITS[0])

        # Checkbox for which species to plot
        tk.Label(topFrame, text="Species").grid(row=1, column=3)
        species_checkbox_vars = []
        s = -1
        for species in SPECIES_LIST:
            s += 1
            species_checkbox_vars.append(tk.IntVar(topFrame))
            checkbutton = tk.Checkbutton(topFrame,
                                         text=species.name,
                                         variable=species_checkbox_vars[s],
                                         anchor=tk.W,
                                         onvalue=1,
                                         offvalue=0,
                                         height=1,
                                         width=5)
            checkbutton.grid(row=2 + s, column=3)
            species_checkbox_vars[-1].set(1)

        tk.Button(bottomFrame, text='Cancel',
                  command=cancel).pack(side=tk.LEFT)
        tk.Button(bottomFrame, text='Plot', command=plot).pack(side=tk.RIGHT)

        root.mainloop()