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 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
|
#!/usr/bin/python
# Copyright (C) 2006-2011, University of Maryland
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/ or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Author: Nikunj Patel, James Krycka, Paul Kienzle
"""
This module implements the FitControl class which presents a pop-up dialog box
for the user to control fitting options.
"""
# TODO: reset button sets values to factory settings for current optimizer
# ==============================================================================
import wx
import wx.lib.newevent
from .. import options
from .input_list import InputListPanel
from .utilities import phoenix
(FitterChangedEvent, EVT_FITTER_CHANGED) = wx.lib.newevent.NewCommandEvent()
class FitConfig(wx.Frame):
"""
FitControl lets the user set fitting options from a pop-up dialog box.
"""
def __init__(
self,
parent=None,
id=wx.ID_ANY,
title="Fit Options",
pos=wx.DefaultPosition,
size=wx.DefaultSize, # dialog box size will be calculated
style=wx.DEFAULT_DIALOG_STYLE,
name="",
config=None,
help=None,
fontsize=None,
):
wx.Frame.__init__(self, parent, id, title, pos, size, style, name)
self.config = config
self.help = help
pairs = [(config.names[id], id) for id in config.active_ids]
self.active_ids = [id for _, id in sorted(pairs)]
# Set the font for this window and all child windows (widgets) from the
# parent window, or from the system defaults if no parent is given.
# A dialog box does not inherit font info from its parent, so we will
# explicitly get it from the parent and apply it to the dialog box.
if parent is not None:
font = parent.GetFont()
self.SetFont(font)
# If the caller specifies a font size, override the default value.
if fontsize is not None:
font = self.GetFont()
font.SetPointSize(fontsize)
self.SetFont(font)
# Section 1
self.panel1 = wx.Panel(self, -1)
static_box1 = wx.StaticBox(self.panel1, -1, "Fit Algorithms")
rows = (len(self.active_ids) + 1) // 2
flexsizer = wx.FlexGridSizer(rows, 2, hgap=20, vgap=10)
self.fitter_button = {}
for fitter in self.active_ids:
button = wx.RadioButton(self.panel1, -1, label=config.names[fitter], name=fitter)
self.fitter_button[fitter] = button
self.Bind(wx.EVT_RADIOBUTTON, self.OnRadio, id=button.GetId())
flexsizer.Add(button, 0, 0)
fit_hsizer = wx.StaticBoxSizer(static_box1, orient=wx.VERTICAL)
fit_hsizer.Add(flexsizer, 0, wx.ALL, 5)
self.panel1.SetSizer(fit_hsizer)
self.vbox = wx.BoxSizer(wx.VERTICAL)
self.vbox.Add(self.panel1, 0, wx.ALL, 10)
# Section 2
# Create list of all panels for later use in hiding and showing panels.
self.fitter_panel = {}
for fitter in self.active_ids:
items = [
(options.FIT_FIELDS[field][0], field, config.values[fitter][field], options.FIT_FIELDS[field][1])
for field, default in config.settings[fitter]
]
# print fitter, items
panel = ParameterPanel(self, items, config.names[fitter])
self.fitter_panel[fitter] = panel
self.vbox.Add(panel, 1, wx.EXPAND | wx.ALL, 10)
panel.Hide()
# Make the current panel active
self.fitter_button[config.selected_id].SetValue(True)
self.fitter_panel[config.selected_id].Show()
# Section 3
# Create the button controls (Reset, Apply) and bind their events.
# apply_btn = wx.Button(self, wx.ID_APPLY, "Apply")
# apply_btn.SetToolTip(wx.ToolTip("Accept new options for the optimizer"))
# apply_btn.SetDefault()
# reset_btn = wx.Button(self, wx.ID_ANY, "Reset")
# reset_btn.SetToolTip(wx.ToolTip("Restore default options for the optimizer"))
accept_btn = wx.Button(self, wx.ID_OK)
accept_btn.SetToolTip(wx.ToolTip("Accept new options for the optimizer"))
accept_btn.SetDefault()
cancel_btn = wx.Button(self, wx.ID_CANCEL)
cancel_btn.SetToolTip(wx.ToolTip("Restore default options for the optimizer"))
if help is not None:
help_btn = wx.Button(self, wx.ID_HELP, "Help")
# help_btn = wx.Button(self, wx.ID_ANY, 'Help')
help_btn.SetToolTip(wx.ToolTip("Help on the options for the optimizer"))
self.Bind(wx.EVT_BUTTON, self.OnAccept, accept_btn)
self.Bind(wx.EVT_BUTTON, self.OnCancel, cancel_btn)
if help is not None:
self.Bind(wx.EVT_BUTTON, self.OnHelp, help_btn)
self.Bind(wx.EVT_CLOSE, self.OnClose)
# Create the button sizer that will put the buttons in a row, right
# justified, and with a fixed amount of space between them. This
# emulates the Windows convention for placing a set of buttons at the
# bottom right of the window.
btn_sizer = wx.BoxSizer(wx.HORIZONTAL)
btn_sizer.Add((10, 20), 1) # stretchable whitespace
btn_sizer.Add(accept_btn, 0)
btn_sizer.Add((10, 20), 0) # non-stretchable whitespace
btn_sizer.Add(cancel_btn, 0)
btn_sizer.Add((10, 20), 0) # non-stretchable whitespace
if help is not None:
btn_sizer.Add((10, 20), 0) # non-stretchable whitespace
btn_sizer.Add(help_btn, 0)
# Add the button sizer to the main sizer.
self.vbox.Add(btn_sizer, 0, wx.EXPAND | wx.ALL, 10)
# Finalize the sizer and establish the dimensions of the dialog box.
# The minimum width is explicitly set because the sizer is not able to
# take into consideration the width of the enclosing frame's title.
self.SetSizer(self.vbox)
# self.vbox.SetMinSize((size[0], -1))
self.vbox.Fit(self)
self.Centre()
def OnRadio(self, event):
button = event.GetEventObject()
for panel in self.fitter_panel.values():
panel.Hide()
self.fitter_panel[button.Name].Show()
self.vbox.Layout()
def OnCancel(self, event):
"""
Restore options for the selected fitter to the default values.
"""
fitter = self._get_fitter()
panel = self.fitter_panel[fitter]
panel.Parameters = dict(self.config.settings[fitter])
self.Hide()
def OnAccept(self, event):
"""
Save the current fitter and options to the fit config.
"""
fitter = self._get_fitter()
options = self.fitter_panel[fitter].Parameters
self.config.selected_id = fitter
self.config.values[fitter] = options
self.Hide()
# Signal a change in fitter
event = FitterChangedEvent(self.Id, config=self.config)
wx.PostEvent(self, event)
def OnHelp(self, event):
"""
Provide help on the selected fitter.
"""
if self.help is not None:
self.help(self._get_fitter())
def OnClose(self, event):
"""
Don't close the window, just hide it.
"""
if event.CanVeto():
self.Hide()
event.Veto()
else:
event.Skip()
def _get_fitter(self):
"""
Returns the currently selected algorithm, or None if no algorithm is
selected.
"""
for button in self.fitter_button.values():
if button.Value:
return button.Name
else:
return None
def _get_options(self):
fitter = self._get_fitter()
options = self.fitter_panel[fitter].Parameters
return fitter, options
class ParameterPanel(wx.Panel):
def __init__(self, parent, parameters, fitter_name):
wx.Panel.__init__(self, parent=parent, id=wx.ID_ANY)
self.fields = []
itemlist = []
sbox = wx.StaticBox(self, wx.ID_ANY, fitter_name + " Fitting Parameters")
for parameter in parameters:
label, field, curr_value, datatype = parameter
if not label.endswith(":"):
label += ":"
if hasattr(datatype, "choices"):
extra = [str(v) for v in datatype.choices]
mode = "CRE"
else:
mode = "RE"
extra = None
sub_list = [label, curr_value, datatype, mode, extra]
itemlist.append(sub_list)
self.fields.append(field)
# Set the same minimum height for each panel. The y-size value should
# be sufficient to display at least 6 input fields without the need for
# a scroll bar. Adjust the size.y value if the maximum number of
# number of input parameters across fitters changes.
self.fit_params = InputListPanel(parent=self, itemlist=itemlist, align=True, size=(-1, 220))
sbox_sizer = wx.StaticBoxSizer(sbox, wx.VERTICAL)
sbox_sizer.Add(self.fit_params, 1, wx.EXPAND | wx.ALL, 5)
self.SetSizer(sbox_sizer)
sbox_sizer.Fit(self)
@property
def Parameters(self):
values = self.fit_params.GetResults()
return dict(zip(self.fields, values))
@Parameters.setter
def Parameters(self, parameters):
values = [parameters[k] for k in self.fields]
self.fit_params.update_items_in_panel(values)
_fit_config_frame = None
def show_fit_config(parent, help=None):
global _fit_config_frame
if _fit_config_frame is None:
_fit_config_frame = FitConfig(parent=parent, config=options.FIT_CONFIG, help=help)
_fit_config_frame.Show()
_fit_config_frame.Raise()
return _fit_config_frame
if __name__ == "__main__":
opts = options.getopts()
def _help(algo):
print("asking for help with " + algo)
app = wx.App()
top = wx.Frame(None)
text = wx.TextCtrl(top, wx.ID_ANY, "some text")
button = wx.Button(top, wx.ID_ANY, "Options...")
button.Bind(wx.EVT_BUTTON, lambda ev: show_fit_config(top, help=_help))
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(text)
sizer.Add(button)
sizer.Fit(top)
top.SetSizer(sizer)
top.Show()
app.MainLoop()
|