示例#1
0
class AddManually(Frame):

    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller

        self.place(relx=0.0, rely=0.0, relheight=0.62, relwidth=0.72)
        self.configure(relief=GROOVE)
        self.configure(borderwidth="2")
        self.configure(relief=GROOVE)
        self.configure(width=125)

        self.label_top = Label(self)
        self.label_top.place(relx=0.4, rely=0.03, height=21, width=112)
        self.label_top.configure(text="Add items manually")

        self.name = Entry(self, fg='grey')
        self.name.place(relx=0.05, rely=0.31, relheight=0.08, relwidth=0.29)
        self.name.insert(0, "Input name here")
        self.name.bind('<Button-1>', lambda event: greytext(self.name))

        self.link = Entry(self, fg='grey')
        self.link.place(relx=0.65, rely=0.31, relheight=0.08, relwidth=0.29)
        self.link.insert(0, "Input link here")
        self.link.bind('<Button-1>', lambda event: greytext(self.link))

        self.add_btn = Button(self, command=self.send_data)
        self.add_btn.place(relx=0.42, rely=0.44, height=34, width=100)
        self.add_btn.configure(text="Add item")

        self.back = Button(self, command=lambda: controller.show_frame('Main'))
        self.back.place(relx=0.42, rely=0.64, height=34, width=100)
        self.back.configure(text="Go back")

        name_label = Label(self)
        name_label.place(relx=0.05, rely=0.22, height=21, width=38)
        name_label.configure(text="Name")

        link_label = Label(self)
        link_label.place(relx=0.65, rely=0.22, height=21, width=28)
        link_label.configure(text="Link")

    def send_data(self):
        if self.link.cget('fg') == 'grey' or self.name.cget('fg') == 'grey':
            return
        link = self.link.get()
        if link.strip() != '':
            name = self.name.get()
            self.controller.add_item(link, name)
            print("Item added")
示例#2
0
class Main(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.controller = controller
        self.db_set = False

        self.configure(relief=GROOVE)
        self.configure(borderwidth="2")

        # Manual link adding
        self.manual_btn = Button(self)
        self.manual_btn.place(relx=0.07, rely=0.81, height=45, width=130)
        self.manual_btn.configure(activebackground="#d9d9d9")
        self.manual_btn.configure(highlightbackground="#d9d9d9")
        self.manual_btn.configure(pady="0")
        self.manual_btn.configure(text="Add manually")
        self.manual_btn.configure(width=130)

        self.file_btn = Button(self)
        self.file_btn.place(relx=0.67, rely=0.81, height=45, width=150)
        self.file_btn.configure(activebackground="#d9d9d9")
        self.file_btn.configure(highlightbackground="#d9d9d9")
        self.file_btn.configure(pady="0")
        self.file_btn.configure(text="Add from file")

        self.label = Label(self)
        self.label.place(relx=0.08, rely=0.0, height=61, width=484)
        self.label.configure(text="Create new playlists and add content to them")
        self.label.configure(width=485)

        self.listbox = Listbox(self)
        self.listbox.place(relx=0.38, rely=0.22, relheight=0.31, relwidth=0.17)
        self.listbox.configure(background="white")
        self.listbox.configure(disabledforeground="#a3a3a3")
        self.listbox.configure(foreground="#000000")
        self.listbox.configure(selectmode=SINGLE)
        self.listbox.configure(width=105)
        for name, value in config.configparser.items('Playlists'):
            if os.path.isdir(value):
                self.listbox.insert('end', name)
            else:
                config.remove_value('Playlists', name)
        self.listbox.bind('<<ListboxSelect>>', self.onselect)

        self.label_name = Label(self)
        self.label_name.place(relx=0.7, rely=0.22, height=31, width=84)
        self.label_name.configure(foreground="#000000")
        self.label_name.configure(text="Name")
        self.label_name.configure(width=85)

        self.entry = Entry(self)
        self.entry.place(relx=0.63, rely=0.31, relheight=0.08, relwidth=0.29)
        self.entry.configure(background="white")
        self.entry.configure(foreground="#000000")
        self.entry.configure(insertbackground="black")
        self.entry.configure(takefocus="0")
        self.entry.configure(width=175)

        self.change_name = Button(self)
        self.change_name.place(relx=0.7, rely=0.42, height=34, width=97)
        self.change_name.configure(activebackground="#d9d9d9")
        self.change_name.configure(highlightbackground="#d9d9d9")
        self.change_name.configure(highlightcolor="black")
        self.change_name.configure(pady="0")
        self.change_name.configure(text="Rename")
        self.change_name.configure(width=100)

        self.new_playlist = Button(self, command=self.new_database)
        self.new_playlist.place(relx=0.08, rely=0.28, height=54, width=107)
        self.new_playlist.configure(activebackground="#d9d9d9")
        self.new_playlist.configure(highlightbackground="#d9d9d9")
        self.new_playlist.configure(highlightcolor="black")
        self.new_playlist.configure(pady="0")
        self.new_playlist.configure(text="Create new playlist")
        self.new_playlist.configure(width=105)

        self.db_name = Entry(self)
        self.db_name.place(relx=0.07, rely=0.44, relheight=0.08, relwidth=0.22)
        self.db_name.configure(fg='grey')
        self.db_name.configure(width=135)
        self.db_name.insert(0, "Input database name here")
        self.db_name.bind('<Button-1>', lambda event: greytext(self.db_name))

    def onselect(self, event):
        w = event.widget
        index = int(w.curselection()[0])
        value = w.get(index)
        set_database(config.configparser.get('Playlists', value))
        if not database.check_integrity():
            messagebox.showwarning('Integrity check failed', 'You might be missing some entries in your list')
        if not self.db_set:
            self.manual_btn.configure(command=lambda: self.controller.show_frame('AddManually'))
            self.file_btn.configure(command=lambda: self.controller.show_frame('ReadFromFile'))
            self.db_set = True

    def new_database(self):
        name = self.db_name.get()
        names = config.configparser.options('Playlists')
        print(name, names)
        if name.strip() == '' or self.db_name.cget('fg') == 'grey':
            messagebox.showerror('Invalid name', "Please input a valid name for the database")
            return
        if name in names:
            messagebox.showerror('Name already in use', "Please select another name")
            return
        path = '../playlists/%s/' % name
        if set_database(path, create=True) is False:
            return
        config.set_value('Playlists', name, path)
        self.listbox.insert('end', name)
示例#3
0
def get_text_field(field: tk.Entry) -> str:
    if field.cget('fg') != 'grey':
        return field.get()
    return ""
示例#4
0
class GUI(Tk):
    def __init__(self):
        Tk.__init__(self)
        self.user_frame = Frame()
        self.back_button = Button(self.user_frame,
                                  text="Back",
                                  command=self.back_to_home_gui)
        self.segmentation_button = Button(self.user_frame,
                                          text="Segmentation",
                                          command=self.segmentation_gui,
                                          height=2,
                                          width=10)
        self.analysis_button = Button(self.user_frame,
                                      text="Analysis",
                                      command=self.analysis_gui,
                                      height=2,
                                      width=10)
        self.progress = Progressbar(self.user_frame,
                                    orient=HORIZONTAL,
                                    length=200,
                                    mode='indeterminate')
        self.progress_label = Label(self.user_frame)
        # output folder
        self.output_path = StringVar("")
        self.output_label = Label(self.user_frame,
                                  text="please select "
                                  "output path")
        self.output_input = Entry(self.user_frame,
                                  textvariable=self.output_path,
                                  width=40)
        self.select_output = Button(self.user_frame,
                                    text=" ",
                                    command=self.select_output_folder)

        # original image
        self.file_path = StringVar("")
        self.file_label = Label(self.user_frame,
                                text="please select the CT "
                                "scan")
        self.file_input = Entry(self.user_frame,
                                textvariable=self.file_path,
                                width=40)
        self.select_file = Button(
            self.user_frame,
            text=" ",
            command=lambda: self.select_nifti_file(self.file_path))
        # seeds
        self.seeds_path = StringVar("")
        self.seeds_label = Label(self.user_frame,
                                 text="please select the "
                                 "marking")
        self.seeds_input = Entry(self.user_frame,
                                 textvariable=self.seeds_path,
                                 width=40)
        self.select_seeds = Button(
            self.user_frame,
            text=" ",
            command=lambda: self.select_nifti_file(self.seeds_path))

        # run buttons
        self.segmentation_run = Button(self.user_frame,
                                       text="Run "
                                       "Segmentation",
                                       command=self.run_segmentation)
        self.analysis_run = Button(self.user_frame,
                                   text="Run Analysis",
                                   command=self.run_analysis)

        self.default_background = self.output_input.cget("background")
        self.scaphoid = None
        self.radius = None
        self.initilize_gui()

    def __del__(self):
        del self.user_frame
        del self.back_button
        del self.segmentation_button
        del self.analysis_button
        del self.progress
        del self.progress_label
        del self.output_path
        del self.output_label
        del self.output_input
        del self.select_output
        del self.file_path
        del self.file_label
        del self.file_input
        del self.select_file
        del self.seeds_path
        del self.seeds_label
        del self.seeds_input
        del self.select_seeds
        del self.segmentation_run
        del self.analysis_run
        del self.default_background
        del self.scaphoid
        del self.radius

    def select_output_folder(self):
        """Select an output folder"""
        path = filedialog.askdirectory()
        self.output_path.set(path)
        self.output_input.config(background=self.default_background)
        del path

    def select_nifti_file(self, var):
        """Select a nifti file"""
        input_path = filedialog.askopenfilename()
        var.set(input_path)
        self.user_frame.grid_slaves(
            int(str(var)[-1]) + 1,
            1)[0].config(background=self.default_background)
        del input_path

    def back_to_home_gui(self):
        """forgets the other gui and reload the home gui"""
        self.forget_non_home_gui()
        self.seeds_path.set("")
        self.initilize_gui()

    def initilize_gui(self):
        """Initial GUI"""
        self.title("Scaphoid Fracture Segmentation and analysis")
        self.user_frame.grid()
        self.segmentation_button.grid(row=0,
                                      column=0,
                                      padx=(100, 50),
                                      pady=(30, 30))
        self.analysis_button.grid(row=0,
                                  column=1,
                                  padx=(50, 100),
                                  pady=(30, 30))

    def segmentation_gui(self):
        """Initial GUI of the segmentation"""
        self.segmentation_button.grid_forget()
        self.analysis_button.grid_forget()
        self.back_button.grid(row=0, pady=(2, 2))
        self.title("Scaphoid Fracture Segmentation")
        self.seeds_label.config(text="please select the marking")
        self.output_label.grid(row=1, column=0)
        self.output_input.grid(row=1, column=1)
        self.select_output.grid(row=1, column=2)
        self.file_label.grid(row=2, column=0)
        self.file_input.grid(row=2, column=1)
        self.select_file.grid(row=2, column=2)
        self.seeds_label.grid(row=3, column=0)
        self.seeds_input.grid(row=3, column=1)
        self.select_seeds.grid(row=3, column=2)
        self.segmentation_run.grid(row=4, columnspan=3, sticky=N + S + E + W)

    def analysis_gui(self):
        """Initial the analysis GUI"""
        self.segmentation_button.grid_forget()
        self.analysis_button.grid_forget()
        self.back_button.grid(row=0, pady=(2, 2))
        self.title("Scaphoid Fracture analysis")
        self.seeds_label.config(text="please select the segmentation")
        self.output_label.grid(row=1, column=0)
        self.output_input.grid(row=1, column=1)
        self.select_output.grid(row=1, column=2)
        self.file_label.grid(row=2, column=0)
        self.file_input.grid(row=2, column=1)
        self.select_file.grid(row=2, column=2)
        self.seeds_label.grid(row=3, column=0)
        self.seeds_input.grid(row=3, column=1)
        self.select_seeds.grid(row=3, column=2)
        self.analysis_run.grid(row=4, columnspan=3, sticky=N + S + E + W)

    def forget_non_home_gui(self):
        """Forgets the grid of segmentation GUI"""
        collect()
        self.back_button.grid_forget()
        self.output_label.grid_forget()
        self.output_input.grid_forget()
        self.select_output.grid_forget()
        self.file_label.grid_forget()
        self.file_input.grid_forget()
        self.select_file.grid_forget()
        self.seeds_label.grid_forget()
        self.seeds_input.grid_forget()
        self.select_seeds.grid_forget()
        self.segmentation_run.grid_forget()
        self.analysis_run.grid_forget()

    def validate_data(self):
        """This function make sure that the data the user selected is valid"""
        valid = True
        if not exists(self.output_path.get()):
            valid = False
            self.output_input.config(background="tomato")
        file_path = self.file_path.get()
        seeds_path = self.seeds_path.get()
        if not (file_path.endswith(".nii.gz") and exists(file_path)):
            valid = False
            self.file_input.config(background="tomato")
        if not (seeds_path.endswith(".nii.gz") and exists(seeds_path)):
            valid = False
            self.seeds_input.config(background="tomato")
        return valid

    def run_segmentation(self):
        """Run the segmentation algorithm while updating the progressbar"""
        def threaded_prog():
            self.progress_label.grid(row=5, column=0)
            self.progress.grid(row=5, column=1, columnspan=2)
            self.progress.start()
            self.progress_label.config(text="Running Segmentation")
            self.segmentation_process()
            self.progress.stop()
            self.progress_label.grid_forget()
            self.progress.grid_forget()
            self.back_to_home_gui()

        if self.validate_data():
            Thread(target=threaded_prog).start()
        else:
            messagebox.showinfo("Error with the input", "Error with the input")

    def segmentation_process(self):
        """Creates the segmentation of the scaphoid and the fracture"""
        self.progress_label.config(text="Getting seeds")
        scaphoid_seeds, fracture_seeds = generate_scaphoid_seeds(
            self.seeds_path.get())
        self.progress_label.config(text="Isolating The Scaphoid")
        self.scaphoid = Scaphoid(self.file_path.get(), scaphoid_seeds,
                                 fracture_seeds, 6)
        self.scaphoid.region_growing_from_input(SCAPHOID_COLOR)
        self.progress_label.config(text="Isolating The Fracture")
        self.scaphoid.segment_fracture_region_growing_mean(
            FRACTURE_COLOR, SCAPHOID_COLOR)

        self.progress_label.config(text="Saving Files")
        save_scaphoid_segmentations(self.scaphoid, self.output_path.get())
        self.progress_label.config(text="Finishing")
        self.scaphoid = None
        del scaphoid_seeds, fracture_seeds
        messagebox.showinfo("Process Finished Successfully",
                            "Process Finished Successfully")

    def run_analysis(self):
        """runs the analysis algorithm while updating the progress bar"""
        def threaded_prog():
            self.progress_label.grid(row=5, column=0)
            self.progress.grid(row=5, column=1, columnspan=2)
            self.progress.start()
            self.progress_label.config(text="Running Analysis")
            self.analysis_process()
            self.progress.stop()
            self.progress_label.grid_forget()
            self.progress.grid_forget()
            self.back_to_home_gui()

        if self.validate_data():
            Thread(target=threaded_prog).start()
        else:
            messagebox.showinfo("Error with the input", "Error with the input")

    def analysis_process(self):
        """The main analysis process"""
        self.progress_label.config(text="Getting the fracture")
        self.scaphoid = Scaphoid(self.file_path.get(), [], [], 1)
        self.scaphoid.load_bone_fracture(self.seeds_path.get())
        self.scaphoid.load_fracture_from_bone_fracture()
        self.progress_label.config(text="Getting seeds")
        radius_seeds, capitate_seg = generate_analysis_seeds(
            self.seeds_path.get())
        self.progress_label.config(text="Isolating The Radius")
        self.radius = Radius(self.file_path.get(), radius_seeds, 6)
        self.radius.region_growing_from_input(RADIUS_COLOR)
        self.progress_label.config(text="Getting PCA from radius")
        pca = self.radius.extract_PCA_components()
        self.progress_label.config(text="Dividing the bone")
        self.scaphoid.divide_bone_into_quarters(pca)
        self.progress_label.config(text="Dividing the fracture")
        self.scaphoid.divide_fracture_into_quarters(pca)
        self.progress_label.config(text="Getting geometric information")
        geo_features = self.scaphoid.get_geometric_features()
        geo_features["Angle between Radius and Capitate"] = \
            str(create_direction_vector_for_2_points_cloud(
                capitate_seg, pca[0]))
        self.progress_label.config(text="Saving Files")
        save_analysis_segmentation(self.scaphoid, self.output_path.get())

        file_name = str(
            abspath(self.scaphoid.get_original_path()).split("\\")[-1].split(
                ".")[0])
        save_geometric_features(geo_features, self.output_path.get(),
                                file_name)
        self.progress_label.config(text="Finishing")
        self.scaphoid = None
        self.radius = None
        del pca, geo_features
        messagebox.showinfo("Process Finished Successfully",
                            "Process Finished Successfully")