File: knn_editing_display.py

package info (click to toggle)
gamera 1:3.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 15,912 kB
  • sloc: xml: 122,324; cpp: 50,730; python: 35,044; ansic: 258; makefile: 114; sh: 101
file content (114 lines) | stat: -rw-r--r-- 4,593 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#
# GUI dialog to execute editing algorithms on a kNN Classifier
# Copyright (C) 2007, 2008 Colin Baumgarten
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

from gamera.knn_editing import AlgoRegistry
import wx

class EditingDialog(object):
    """Dialog to apply any of the editing algorithms known to the 
*gamera.knn_editing.AlgoRegistry* to a given *kNNInteractive* Classifier. Lets 
the user select one of the algorithms, tune its parameters and then returns the 
edited result"""
    def __init__(self):
        self.selection = 0
        self.panel = None
        self._createDialog()

    def _createDialog(self):
        self.dlg = wx.Dialog(None, wx.NewId(), "Create edited Classifier")
        self.sizer = wx.FlexGridSizer(rows = 0, cols = 1)
        self.dlg.SetSizer(self.sizer)

        choiceSizer = wx.FlexGridSizer(rows = 1, cols = 0)
        statText = wx.StaticText(self.dlg, wx.NewId(), "Select an Editing Algorithm")
        choiceSizer.Add(statText, flag = wx.ALIGN_CENTER_VERTICAL | wx.RIGHT, border = 10)
        choiceId = wx.NewId()
        choiceControl = wx.Choice(self.dlg, choiceId, choices = self._algoNames())
        wx.EVT_CHOICE(self.dlg, choiceId, self._choiceCallback)
        choiceSizer.Add(choiceControl, flag = wx.ALIGN_RIGHT)
        choiceSizer.AddGrowableCol(0)

        self.sizer.Add(choiceSizer, flag = wx.EXPAND | wx.ALL, border = 10)
        self.sizer.Add(wx.StaticLine(self.dlg), flag = wx.EXPAND)

        self._replaceArgs()
        
        buttons = self.dlg.CreateButtonSizer(wx.OK | wx.CANCEL)
        #buttons.Realize() # does not work on Windows
        self.sizer.Add(buttons, flag = wx.ALL, border = 10)

        self.sizer.Fit(self.dlg)
        self.sizer.SetSizeHints(self.dlg)

    def _choiceCallback(self, event):
        """called, if the user selects an algorithm from the dropdown, so that 
the list of parameters can be updated""" 
        if event.GetSelection() != self.selection:
            self.selection = event.GetSelection()
            self._replaceArgs()
            self.sizer.Fit(self.dlg)
            self.sizer.SetSizeHints(self.dlg)
            
    def _replaceArgs(self):
        """replace (or initially create) the parameter input controls, according
to the just selected algorithm"""
        panel = wx.Panel(self.dlg)

        self._selected().args.window = panel
        help = self._selected().args._create_help_display(self._selected().doc)
        help.SetInitialSize(wx.Size(50, 200))        
        panelSizer = wx.FlexGridSizer(rows = 0, cols = 1)
        panelSizer.AddGrowableCol(0)
        panel.SetSizer(panelSizer)
        
        argControls = self._selected().args._create_controls(None, panel)
        panelSizer.Add(argControls, flag = wx.EXPAND)
        
        panelSizer.Add(help, flag = wx.EXPAND | wx.ALL, border = 10)
        panel.Layout()

        if self.panel == None:
            self.sizer.Add(panel, flag = wx.EXPAND)
        else:
            self.sizer.Replace(self.panel, panel)
            self.panel.Destroy()
            self.dlg.Layout()

        self.panel = panel

    def _algoNames(self):
        """Returns a list of all editing algorithm names as known to the 
*AlgoRegistry*"""        
        return [alg.name for alg in AlgoRegistry.algorithms]

    def _selected(self):
        """ Returns the metadata of the currently selected algorithm as a
*AlgoData* object"""
        return AlgoRegistry.algorithms[self.selection]

    def show(self, classifier):
        """Shows the dialog. In case the user clicks *OK*, applies the 
selected algorithm to the given *classifier* and returns the resulting one. 
Otherwise returns *None*""" 
        if self.dlg.ShowModal() == wx.ID_OK:
            args = self._selected().args.get_args()
            args.insert(0, classifier)
            args = tuple(args)
            return self._selected().callable(*args)
        else:
            return None