[f3d51f6] | 1 | #!/usr/bin/env python |
---|
| 2 | |
---|
| 3 | # version |
---|
| 4 | __id__ = "$Id: aboutdialog.py 1193 2007-05-03 17:29:59Z dmitriy $" |
---|
| 5 | __revision__ = "$Revision: 1193 $" |
---|
| 6 | |
---|
| 7 | import wx |
---|
| 8 | from sans.guicomm.events import StatusEvent |
---|
| 9 | |
---|
| 10 | class InversionDlg(wx.Dialog): |
---|
| 11 | def __init__(self, parent, id, title, plots, file=False, pars=True): |
---|
| 12 | |
---|
| 13 | # Estimate size |
---|
| 14 | nplots = len(plots) |
---|
| 15 | # y size for data set only |
---|
| 16 | ysize = 110 + nplots*20 |
---|
| 17 | # y size including parameters |
---|
| 18 | if pars: |
---|
| 19 | ysize += 90 |
---|
| 20 | |
---|
| 21 | wx.Dialog.__init__(self, parent, id, title, size=(250, ysize)) |
---|
| 22 | self.SetTitle(title) |
---|
| 23 | |
---|
| 24 | # Data set |
---|
| 25 | self.datasets = InversionPanel(self, -1, plots) |
---|
| 26 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 27 | |
---|
| 28 | vbox.Add(self.datasets) |
---|
| 29 | |
---|
| 30 | # Parameters |
---|
| 31 | self.pars_flag = False |
---|
| 32 | if pars==True: |
---|
| 33 | self.pars_flag = True |
---|
| 34 | self.pars = ParsDialog(self, -1, file=file) |
---|
| 35 | vbox.Add(self.pars) |
---|
| 36 | |
---|
| 37 | static_line = wx.StaticLine(self, -1) |
---|
| 38 | vbox.Add(static_line, 0, wx.EXPAND, 0) |
---|
| 39 | |
---|
| 40 | button_OK = wx.Button(self, wx.ID_OK, "OK") |
---|
| 41 | button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
---|
| 42 | |
---|
| 43 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 44 | sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 45 | sizer_button.Add(button_OK, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10) |
---|
| 46 | sizer_button.Add(button_Cancel, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
| 47 | vbox.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10) |
---|
| 48 | |
---|
| 49 | self.SetSizer(vbox) |
---|
| 50 | self.SetAutoLayout(True) |
---|
| 51 | |
---|
| 52 | self.Layout() |
---|
| 53 | self.Centre() |
---|
| 54 | |
---|
| 55 | def get_content(self): |
---|
| 56 | dataset = self.datasets.get_selected() |
---|
| 57 | if self.pars_flag: |
---|
| 58 | nfunc, alpha, dmax, file = self.pars.getContent() |
---|
| 59 | return dataset, nfunc, alpha, dmax |
---|
| 60 | else: |
---|
| 61 | return dataset |
---|
| 62 | |
---|
| 63 | def set_content(self, dataset, nfunc, alpha, dmax): |
---|
| 64 | if not dataset==None and dataset in self.datasets.radio_buttons.keys(): |
---|
| 65 | self.datasets.radio_buttons[dataset].SetValue(True) |
---|
| 66 | if self.pars_flag: |
---|
| 67 | self.pars.setContent(nfunc, alpha, dmax, None) |
---|
| 68 | |
---|
| 69 | class InversionPanel(wx.Panel): |
---|
| 70 | |
---|
| 71 | def __init__(self, parent, id = -1, plots = None, **kwargs): |
---|
| 72 | wx.Panel.__init__(self, parent, id = id, **kwargs) |
---|
| 73 | |
---|
| 74 | self.plots = plots |
---|
| 75 | self.radio_buttons = {} |
---|
| 76 | |
---|
| 77 | self._do_layout() |
---|
| 78 | |
---|
| 79 | def _do_layout(self): |
---|
| 80 | panel = wx.Panel(self, -1) |
---|
| 81 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 82 | |
---|
| 83 | ysize = 30+20*len(self.plots) |
---|
| 84 | wx.StaticBox(panel, -1, 'Choose a data set', (5, 5), (230, ysize)) |
---|
| 85 | ypos = 30 |
---|
| 86 | self.radio_buttons = {} |
---|
| 87 | for item in self.plots.keys(): |
---|
| 88 | self.radio_buttons[self.plots[item].name] = wx.RadioButton(panel, -1, self.plots[item].name, (15, ypos)) |
---|
| 89 | ypos += 20 |
---|
| 90 | |
---|
| 91 | vbox.Add(panel) |
---|
| 92 | |
---|
| 93 | self.SetSizer(vbox) |
---|
| 94 | |
---|
| 95 | def get_selected(self): |
---|
| 96 | for item in self.radio_buttons: |
---|
| 97 | if self.radio_buttons[item].GetValue(): |
---|
| 98 | return item |
---|
| 99 | return None |
---|
| 100 | |
---|
| 101 | class InversionControl(wx.Panel): |
---|
| 102 | window_name = 'pr_control' |
---|
| 103 | window_caption = "P(r) control panel" |
---|
| 104 | CENTER_PANE = True |
---|
| 105 | |
---|
| 106 | # Figure of merit parameters [default] |
---|
[dfb58f8] | 107 | |
---|
| 108 | ## Oscillation parameters (sin function = 1.1) |
---|
[f3d51f6] | 109 | oscillation_max = 1.5 |
---|
| 110 | |
---|
[dfb58f8] | 111 | ## Fraction of P(r) that is positive |
---|
| 112 | positive = 1.0 |
---|
| 113 | |
---|
| 114 | ## Fraction of P(r) that is greater than zero by more than 1 sigma |
---|
| 115 | pos_err = 1.0 |
---|
| 116 | |
---|
[f3d51f6] | 117 | def __init__(self, parent, id = -1, plots = None, **kwargs): |
---|
| 118 | wx.Panel.__init__(self, parent, id = id, **kwargs) |
---|
| 119 | |
---|
| 120 | self.plots = plots |
---|
| 121 | self.radio_buttons = {} |
---|
| 122 | |
---|
| 123 | ## Data file TextCtrl |
---|
| 124 | self.data_file = None |
---|
| 125 | self.plot_data = None |
---|
| 126 | self.nfunc_ctl = None |
---|
| 127 | self.alpha_ctl = None |
---|
| 128 | self.dmax_ctl = None |
---|
| 129 | self.time_ctl = None |
---|
| 130 | self.chi2_ctl = None |
---|
| 131 | self.osc_ctl = None |
---|
| 132 | self.file_radio = None |
---|
| 133 | self.plot_radio = None |
---|
[634f1cf] | 134 | self.label_sugg = None |
---|
| 135 | self.qmin_ctl = None |
---|
| 136 | self.qmax_ctl = None |
---|
[f3d51f6] | 137 | |
---|
[dfb58f8] | 138 | # TextCtrl for fraction of positive P(r) |
---|
| 139 | self.pos_ctl = None |
---|
| 140 | |
---|
| 141 | # TextCtrl for fraction of 1 sigma positive P(r) |
---|
| 142 | self.pos_err_ctl = None |
---|
| 143 | |
---|
[f3d51f6] | 144 | ## Estimates |
---|
| 145 | self.alpha_estimate_ctl = None |
---|
| 146 | |
---|
| 147 | ## Data manager |
---|
| 148 | self.manager = None |
---|
| 149 | |
---|
| 150 | self._do_layout() |
---|
| 151 | |
---|
| 152 | def __setattr__(self, name, value): |
---|
| 153 | """ |
---|
| 154 | Allow direct hooks to text boxes |
---|
| 155 | """ |
---|
| 156 | if name=='nfunc': |
---|
| 157 | self.nfunc_ctl.SetValue(str(value)) |
---|
| 158 | elif name=='d_max': |
---|
| 159 | self.dmax_ctl.SetValue(str(value)) |
---|
| 160 | elif name=='alpha': |
---|
| 161 | self.alpha_ctl.SetValue(str(value)) |
---|
| 162 | elif name=='chi2': |
---|
| 163 | self.chi2_ctl.SetValue("%-5.3g" % value) |
---|
[634f1cf] | 164 | elif name=='q_min': |
---|
| 165 | self.qmin_ctl.SetValue("%-5.3g" % value) |
---|
| 166 | elif name=='q_max': |
---|
| 167 | self.qmax_ctl.SetValue("%-5.3g" % value) |
---|
[f3d51f6] | 168 | elif name=='elapsed': |
---|
| 169 | self.time_ctl.SetValue("%-5.2g" % value) |
---|
| 170 | elif name=='oscillation': |
---|
| 171 | self.osc_ctl.SetValue("%-5.2g" % value) |
---|
[dfb58f8] | 172 | elif name=='positive': |
---|
| 173 | self.pos_ctl.SetValue("%-5.2g" % value) |
---|
| 174 | elif name=='pos_err': |
---|
| 175 | self.pos_err_ctl.SetValue("%-5.2g" % value) |
---|
[f3d51f6] | 176 | elif name=='alpha_estimate': |
---|
[634f1cf] | 177 | self.alpha_estimate_ctl.SetToolTipString("Click to accept value.") |
---|
| 178 | self.alpha_estimate_ctl.Enable(True) |
---|
| 179 | self.alpha_estimate_ctl.SetLabel("%-3.1g" % value) |
---|
| 180 | #self.alpha_estimate_ctl.Show() |
---|
| 181 | #self.label_sugg.Show() |
---|
[f3d51f6] | 182 | elif name=='plotname': |
---|
| 183 | self.plot_data.SetValue(str(value)) |
---|
| 184 | self.plot_radio.SetValue(True) |
---|
| 185 | self._on_pars_changed(None) |
---|
| 186 | else: |
---|
| 187 | wx.Panel.__setattr__(self, name, value) |
---|
| 188 | |
---|
| 189 | def __getattr__(self, name): |
---|
| 190 | """ |
---|
| 191 | Allow direct hooks to text boxes |
---|
| 192 | """ |
---|
| 193 | if name=='nfunc': |
---|
| 194 | int(self.nfunc_ctl.GetValue()) |
---|
| 195 | elif name=='d_max': |
---|
| 196 | self.dmax_ctl.GetValue() |
---|
| 197 | elif name=='alpha': |
---|
| 198 | self.alpha_ctl.GetValue() |
---|
| 199 | elif name=='chi2': |
---|
| 200 | self.chi2_ctl.GetValue() |
---|
[634f1cf] | 201 | elif name=='q_min': |
---|
| 202 | self.qmin_ctl.GetValue() |
---|
| 203 | elif name=='q_max': |
---|
| 204 | self.qmax_ctl.GetValue() |
---|
[f3d51f6] | 205 | elif name=='elapsed': |
---|
| 206 | self.time_ctl.GetValue() |
---|
| 207 | elif name=='oscillation': |
---|
| 208 | self.osc_ctl.GetValue() |
---|
[dfb58f8] | 209 | elif name=='pos': |
---|
| 210 | self.pos_ctl.GetValue() |
---|
| 211 | elif name=='pos_err': |
---|
| 212 | self.pos_err_ctl.GetValue() |
---|
[f3d51f6] | 213 | elif name=='alpha_estimate': |
---|
| 214 | self.alpha_estimate_ctl.GetValue() |
---|
| 215 | elif name=='plotname': |
---|
| 216 | self.plot_data.GetValue() |
---|
| 217 | else: |
---|
| 218 | wx.Panel.__getattr__(self, name) |
---|
| 219 | |
---|
| 220 | def set_manager(self, manager): |
---|
| 221 | self.manager = manager |
---|
| 222 | # Get data |
---|
| 223 | |
---|
| 224 | # Push data to form |
---|
| 225 | |
---|
| 226 | |
---|
| 227 | def _do_layout(self): |
---|
| 228 | #panel = wx.Panel(self, -1) |
---|
| 229 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 230 | |
---|
| 231 | # ----- I(q) data ----- |
---|
| 232 | databox = wx.StaticBox(self, -1, "I(q) data") |
---|
| 233 | |
---|
| 234 | boxsizer1 = wx.StaticBoxSizer(databox, wx.VERTICAL) |
---|
| 235 | boxsizer1.SetMinSize((320,50)) |
---|
| 236 | pars_sizer = wx.GridBagSizer(5,5) |
---|
| 237 | |
---|
| 238 | iy = 0 |
---|
| 239 | self.file_radio = wx.RadioButton(self, -1, "File data:") |
---|
| 240 | self.data_file = wx.TextCtrl(self, -1, size=(100,20)) |
---|
| 241 | self.data_file.SetEditable(False) |
---|
| 242 | self.data_file.SetValue("") |
---|
| 243 | id = wx.NewId() |
---|
| 244 | choose_button = wx.Button(self, id, "Choose file") |
---|
| 245 | self.Bind(wx.EVT_BUTTON, self._change_file, id = id) |
---|
| 246 | pars_sizer.Add(self.file_radio, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 247 | pars_sizer.Add(self.data_file, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 248 | pars_sizer.Add(choose_button, (iy,3), (1,1), wx.RIGHT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 249 | |
---|
| 250 | iy += 1 |
---|
| 251 | self.plot_radio = wx.RadioButton(self, -1, "Plot data:") |
---|
| 252 | self.plot_data = wx.TextCtrl(self, -1, size=(100,20)) |
---|
| 253 | self.plot_data.SetEditable(False) |
---|
| 254 | pars_sizer.Add(self.plot_radio, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 255 | pars_sizer.Add(self.plot_data, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 256 | |
---|
| 257 | boxsizer1.Add(pars_sizer, 0, wx.EXPAND) |
---|
| 258 | vbox.Add(boxsizer1) |
---|
| 259 | |
---|
| 260 | # ----- Parameters ----- |
---|
| 261 | parsbox = wx.StaticBox(self, -1, "Parameters") |
---|
| 262 | boxsizer2 = wx.StaticBoxSizer(parsbox, wx.VERTICAL) |
---|
| 263 | boxsizer2.SetMinSize((320,50)) |
---|
| 264 | |
---|
[32dffae4] | 265 | explanation = "P(r) is found by fitting a set of base functions to I(Q). " |
---|
| 266 | explanation += "The minimization involves a regularization term to ensure " |
---|
| 267 | explanation += "a smooth P(r). The alpha parameter gives the size of that " |
---|
[119a11d] | 268 | explanation += "term. The suggested value is the value above which the " |
---|
[32dffae4] | 269 | explanation += "output P(r) will have only one peak." |
---|
| 270 | label_explain = wx.StaticText(self, -1, explanation, size=(280,80)) |
---|
| 271 | boxsizer2.Add(label_explain, wx.LEFT|wx.BOTTOM, 5) |
---|
| 272 | |
---|
| 273 | |
---|
| 274 | |
---|
[f3d51f6] | 275 | label_nfunc = wx.StaticText(self, -1, "Number of terms") |
---|
| 276 | label_nfunc.SetMinSize((120,20)) |
---|
| 277 | label_alpha = wx.StaticText(self, -1, "Regularization constant") |
---|
| 278 | label_dmax = wx.StaticText(self, -1, "Max distance [A]") |
---|
[634f1cf] | 279 | self.label_sugg = wx.StaticText(self, -1, "Suggested value") |
---|
| 280 | #self.label_sugg.Hide() |
---|
[f3d51f6] | 281 | |
---|
| 282 | self.nfunc_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
[32dffae4] | 283 | self.nfunc_ctl.SetToolTipString("Number of terms in the expansion.") |
---|
[f3d51f6] | 284 | self.alpha_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
[32dffae4] | 285 | self.alpha_ctl.SetToolTipString("Control parameter for the size of the regularization term.") |
---|
[f3d51f6] | 286 | self.dmax_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
[32dffae4] | 287 | self.dmax_ctl.SetToolTipString("Maximum distance between any two points in the system.") |
---|
[634f1cf] | 288 | id = wx.NewId() |
---|
| 289 | self.alpha_estimate_ctl = wx.Button(self, id, "") |
---|
| 290 | #self.alpha_estimate_ctl.Hide() |
---|
| 291 | self.Bind(wx.EVT_BUTTON, self._on_accept_alpha, id = id) |
---|
[f3d51f6] | 292 | self.alpha_estimate_ctl.Enable(False) |
---|
[634f1cf] | 293 | #self.alpha_estimate_ctl.SetBackgroundColour('#ffdf85') |
---|
| 294 | #self.alpha_estimate_ctl.SetBackgroundColour(self.GetBackgroundColour()) |
---|
| 295 | self.alpha_estimate_ctl.SetToolTipString("Waiting for estimate...") |
---|
[f3d51f6] | 296 | |
---|
| 297 | # EVT_TEXT would trigger an event for each character entered |
---|
| 298 | self.nfunc_ctl.Bind(wx.EVT_KILL_FOCUS, self._on_pars_changed) |
---|
[634f1cf] | 299 | self.alpha_ctl.Bind(wx.EVT_KILL_FOCUS, self._read_pars) |
---|
[f3d51f6] | 300 | self.dmax_ctl.Bind(wx.EVT_KILL_FOCUS, self._on_pars_changed) |
---|
| 301 | self.Bind(wx.EVT_TEXT_ENTER, self._on_pars_changed) |
---|
| 302 | |
---|
| 303 | sizer_params = wx.GridBagSizer(5,5) |
---|
| 304 | |
---|
| 305 | iy = 0 |
---|
[634f1cf] | 306 | sizer_params.Add(self.label_sugg, (iy,2), (1,1), wx.LEFT, 15) |
---|
[f3d51f6] | 307 | iy += 1 |
---|
[32dffae4] | 308 | sizer_params.Add(label_nfunc, (iy,0), (1,1), wx.LEFT, 15) |
---|
| 309 | sizer_params.Add(self.nfunc_ctl, (iy,1), (1,1), wx.RIGHT, 0) |
---|
[f3d51f6] | 310 | iy += 1 |
---|
[32dffae4] | 311 | sizer_params.Add(label_alpha, (iy,0), (1,1), wx.LEFT, 15) |
---|
| 312 | sizer_params.Add(self.alpha_ctl, (iy,1), (1,1), wx.RIGHT, 0) |
---|
| 313 | sizer_params.Add(self.alpha_estimate_ctl, (iy,2), (1,1), wx.LEFT, 15) |
---|
[f3d51f6] | 314 | iy += 1 |
---|
[32dffae4] | 315 | sizer_params.Add(label_dmax, (iy,0), (1,1), wx.LEFT, 15) |
---|
| 316 | sizer_params.Add(self.dmax_ctl, (iy,1), (1,1), wx.RIGHT, 0) |
---|
[f3d51f6] | 317 | |
---|
| 318 | boxsizer2.Add(sizer_params, 0) |
---|
[634f1cf] | 319 | |
---|
[f3d51f6] | 320 | vbox.Add(boxsizer2) |
---|
| 321 | |
---|
[634f1cf] | 322 | # ----- Q range ----- |
---|
| 323 | qbox = wx.StaticBox(self, -1, "Q range") |
---|
| 324 | qboxsizer = wx.StaticBoxSizer(qbox, wx.VERTICAL) |
---|
| 325 | qboxsizer.SetMinSize((320,20)) |
---|
| 326 | |
---|
| 327 | sizer_q = wx.GridBagSizer(5,5) |
---|
| 328 | |
---|
| 329 | label_qmin = wx.StaticText(self, -1, "Q min") |
---|
| 330 | label_qmax = wx.StaticText(self, -1, "Q max") |
---|
| 331 | self.qmin_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
| 332 | self.qmax_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
| 333 | |
---|
| 334 | iy = 0 |
---|
| 335 | sizer_q.Add(label_qmin, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
| 336 | sizer_q.Add(self.qmin_ctl, (iy,1), (1,1), wx.LEFT|wx.EXPAND, 10) |
---|
| 337 | sizer_q.Add(label_qmax, (iy,2), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
| 338 | sizer_q.Add(self.qmax_ctl, (iy,3), (1,1), wx.LEFT|wx.EXPAND, 10) |
---|
| 339 | qboxsizer.Add(sizer_q, wx.TOP, 15) |
---|
| 340 | vbox.Add(qboxsizer) |
---|
| 341 | |
---|
| 342 | |
---|
| 343 | |
---|
[f3d51f6] | 344 | # ----- Results ----- |
---|
| 345 | resbox = wx.StaticBox(self, -1, "Outputs") |
---|
| 346 | ressizer = wx.StaticBoxSizer(resbox, wx.VERTICAL) |
---|
| 347 | ressizer.SetMinSize((320,50)) |
---|
| 348 | |
---|
| 349 | label_time = wx.StaticText(self, -1, "Computation time") |
---|
| 350 | label_time_unit = wx.StaticText(self, -1, "secs") |
---|
| 351 | label_time.SetMinSize((120,20)) |
---|
| 352 | label_chi2 = wx.StaticText(self, -1, "Chi2/dof") |
---|
| 353 | label_osc = wx.StaticText(self, -1, "Oscillations") |
---|
[dfb58f8] | 354 | label_pos = wx.StaticText(self, -1, "Positive fraction") |
---|
| 355 | label_pos_err = wx.StaticText(self, -1, "1-sigma positive fraction") |
---|
[f3d51f6] | 356 | |
---|
| 357 | self.time_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
| 358 | self.time_ctl.SetEditable(False) |
---|
[32dffae4] | 359 | self.time_ctl.SetToolTipString("Computation time for the last inversion, in seconds.") |
---|
| 360 | |
---|
[f3d51f6] | 361 | self.chi2_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
| 362 | self.chi2_ctl.SetEditable(False) |
---|
[32dffae4] | 363 | self.chi2_ctl.SetToolTipString("Chi^2 over degrees of freedom.") |
---|
| 364 | |
---|
[dfb58f8] | 365 | # Oscillation parameter |
---|
[f3d51f6] | 366 | self.osc_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
| 367 | self.osc_ctl.SetEditable(False) |
---|
[32dffae4] | 368 | self.osc_ctl.SetToolTipString("Oscillation parameter. P(r) for a sphere has an oscillation parameter of 1.1.") |
---|
[f3d51f6] | 369 | |
---|
[dfb58f8] | 370 | # Positive fraction figure of merit |
---|
| 371 | self.pos_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
| 372 | self.pos_ctl.SetEditable(False) |
---|
| 373 | self.pos_ctl.SetToolTipString("Fraction of P(r) that is positive. Theoretically, P(r) is defined positive.") |
---|
| 374 | |
---|
| 375 | # 1-simga positive fraction figure of merit |
---|
| 376 | self.pos_err_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
| 377 | self.pos_err_ctl.SetEditable(False) |
---|
| 378 | message = "Fraction of P(r) that is at least 1 standard deviation greater than zero.\n" |
---|
| 379 | message += "This figure of merit tells you about the size of the P(r) errors.\n" |
---|
| 380 | message += "If it is close to 1 and the other figures of merit are bad, consider changing " |
---|
| 381 | message += "the maximum distance." |
---|
| 382 | self.pos_err_ctl.SetToolTipString(message) |
---|
| 383 | |
---|
[f3d51f6] | 384 | sizer_res = wx.GridBagSizer(5,5) |
---|
| 385 | |
---|
| 386 | iy = 0 |
---|
| 387 | sizer_res.Add(label_time, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
| 388 | sizer_res.Add(self.time_ctl, (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
| 389 | sizer_res.Add(label_time_unit, (iy,2), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
| 390 | iy += 1 |
---|
| 391 | sizer_res.Add(label_chi2, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
| 392 | sizer_res.Add(self.chi2_ctl, (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
| 393 | iy += 1 |
---|
| 394 | sizer_res.Add(label_osc, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
| 395 | sizer_res.Add(self.osc_ctl, (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
| 396 | |
---|
[dfb58f8] | 397 | iy += 1 |
---|
| 398 | sizer_res.Add(label_pos, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
| 399 | sizer_res.Add(self.pos_ctl, (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
| 400 | |
---|
| 401 | iy += 1 |
---|
| 402 | sizer_res.Add(label_pos_err, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
| 403 | sizer_res.Add(self.pos_err_ctl, (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
| 404 | |
---|
[f3d51f6] | 405 | ressizer.Add(sizer_res, 0) |
---|
| 406 | vbox.Add(ressizer) |
---|
| 407 | |
---|
| 408 | # ----- Buttons ----- |
---|
| 409 | static_line = wx.StaticLine(self, -1) |
---|
| 410 | vbox.Add(static_line, 0, wx.EXPAND|wx.TOP, 10) |
---|
| 411 | |
---|
| 412 | id = wx.NewId() |
---|
| 413 | button_OK = wx.Button(self, id, "Compute") |
---|
[32dffae4] | 414 | button_OK.SetToolTipString("Perform P(r) inversion.") |
---|
[f3d51f6] | 415 | self.Bind(wx.EVT_BUTTON, self._on_invert, id = id) |
---|
| 416 | #button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
---|
| 417 | |
---|
| 418 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 419 | sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 420 | sizer_button.Add(button_OK, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10) |
---|
| 421 | #sizer_button.Add(button_Cancel, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
| 422 | vbox.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10) |
---|
| 423 | |
---|
| 424 | |
---|
| 425 | self.SetSizer(vbox) |
---|
| 426 | |
---|
[634f1cf] | 427 | |
---|
| 428 | |
---|
| 429 | def _on_accept_alpha(self, evt): |
---|
| 430 | """ |
---|
| 431 | User has accepted the estimated alpha, |
---|
| 432 | set it as part of the input parameters |
---|
| 433 | """ |
---|
| 434 | try: |
---|
| 435 | alpha = self.alpha_estimate_ctl.GetLabel() |
---|
| 436 | tmp = float(alpha) |
---|
| 437 | self.alpha_ctl.SetValue(alpha) |
---|
| 438 | except: |
---|
| 439 | # No estimate or bad estimate, either do nothing |
---|
| 440 | import sys |
---|
| 441 | print "InversionControl._on_accept_alpha: %s" % sys.exc_value |
---|
| 442 | pass |
---|
| 443 | |
---|
| 444 | |
---|
[f3d51f6] | 445 | def _on_pars_changed(self, evt): |
---|
| 446 | """ |
---|
| 447 | Called when an input parameter has changed |
---|
| 448 | We will estimate the alpha parameter behind the |
---|
| 449 | scenes. |
---|
| 450 | """ |
---|
[634f1cf] | 451 | flag, alpha, dmax, nfunc, qmin, qmax = self._read_pars() |
---|
[f3d51f6] | 452 | |
---|
| 453 | # If the pars are valid, estimate alpha |
---|
| 454 | if flag: |
---|
| 455 | if self.plot_radio.GetValue(): |
---|
| 456 | dataset = self.plot_data.GetValue() |
---|
| 457 | self.manager.estimate_plot_inversion(alpha=alpha, nfunc=nfunc, |
---|
[634f1cf] | 458 | d_max=dmax, |
---|
| 459 | q_min=qmin, q_max=qmax) |
---|
[f3d51f6] | 460 | else: |
---|
| 461 | path = self.data_file.GetValue() |
---|
| 462 | self.manager.estimate_file_inversion(alpha=alpha, nfunc=nfunc, |
---|
[634f1cf] | 463 | d_max=dmax, path=path, |
---|
| 464 | q_min=qmin, q_max=qmax) |
---|
[f3d51f6] | 465 | |
---|
| 466 | |
---|
[634f1cf] | 467 | def _read_pars(self, evt=None): |
---|
[f3d51f6] | 468 | alpha = 0 |
---|
| 469 | nfunc = 5 |
---|
| 470 | dmax = 120 |
---|
| 471 | |
---|
| 472 | flag = True |
---|
| 473 | |
---|
| 474 | # Read alpha |
---|
| 475 | try: |
---|
| 476 | alpha = float(self.alpha_ctl.GetValue()) |
---|
| 477 | self.alpha_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 478 | self.alpha_ctl.Refresh() |
---|
| 479 | except: |
---|
| 480 | flag = False |
---|
| 481 | self.alpha_ctl.SetBackgroundColour("pink") |
---|
| 482 | self.alpha_ctl.Refresh() |
---|
| 483 | |
---|
| 484 | # Read d_max |
---|
| 485 | try: |
---|
| 486 | dmax = float(self.dmax_ctl.GetValue()) |
---|
| 487 | self.dmax_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 488 | self.dmax_ctl.Refresh() |
---|
| 489 | except: |
---|
| 490 | flag = False |
---|
| 491 | self.dmax_ctl.SetBackgroundColour("pink") |
---|
| 492 | self.dmax_ctl.Refresh() |
---|
| 493 | |
---|
| 494 | # Read nfunc |
---|
| 495 | try: |
---|
| 496 | nfunc = int(self.nfunc_ctl.GetValue()) |
---|
| 497 | self.nfunc_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 498 | self.nfunc_ctl.Refresh() |
---|
| 499 | except: |
---|
| 500 | flag = False |
---|
| 501 | self.nfunc_ctl.SetBackgroundColour("pink") |
---|
| 502 | self.nfunc_ctl.Refresh() |
---|
| 503 | |
---|
[634f1cf] | 504 | # Read qmin |
---|
| 505 | try: |
---|
| 506 | qmin_str = self.qmin_ctl.GetValue() |
---|
| 507 | if len(qmin_str.lstrip().rstrip())==0: |
---|
| 508 | qmin = None |
---|
| 509 | else: |
---|
| 510 | qmin = float(qmin_str) |
---|
| 511 | self.qmin_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 512 | self.qmin_ctl.Refresh() |
---|
| 513 | except: |
---|
| 514 | flag = False |
---|
| 515 | self.qmin_ctl.SetBackgroundColour("pink") |
---|
| 516 | self.qmin_ctl.Refresh() |
---|
| 517 | |
---|
| 518 | # Read qmax |
---|
| 519 | try: |
---|
| 520 | qmax_str = self.qmax_ctl.GetValue() |
---|
| 521 | if len(qmax_str.lstrip().rstrip())==0: |
---|
| 522 | qmax = None |
---|
| 523 | else: |
---|
| 524 | qmax = float(qmax_str) |
---|
| 525 | self.qmax_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 526 | self.qmax_ctl.Refresh() |
---|
| 527 | except: |
---|
| 528 | flag = False |
---|
| 529 | self.qmax_ctl.SetBackgroundColour("pink") |
---|
| 530 | self.qmax_ctl.Refresh() |
---|
| 531 | |
---|
| 532 | return flag, alpha, dmax, nfunc, qmin, qmax |
---|
[f3d51f6] | 533 | |
---|
| 534 | def _on_invert(self, evt): |
---|
| 535 | """ |
---|
| 536 | Perform inversion |
---|
| 537 | @param silent: when True, there will be no output for the user |
---|
| 538 | """ |
---|
| 539 | # Get the data from the form |
---|
| 540 | # Push it to the manager |
---|
| 541 | |
---|
[634f1cf] | 542 | flag, alpha, dmax, nfunc, qmin, qmax = self._read_pars() |
---|
[f3d51f6] | 543 | |
---|
| 544 | if flag: |
---|
| 545 | if self.plot_radio.GetValue(): |
---|
| 546 | dataset = self.plot_data.GetValue() |
---|
| 547 | self.manager.setup_plot_inversion(alpha=alpha, nfunc=nfunc, |
---|
[634f1cf] | 548 | d_max=dmax, |
---|
| 549 | q_min=qmin, q_max=qmax) |
---|
[f3d51f6] | 550 | else: |
---|
| 551 | path = self.data_file.GetValue() |
---|
| 552 | self.manager.setup_file_inversion(alpha=alpha, nfunc=nfunc, |
---|
[634f1cf] | 553 | d_max=dmax, path=path, |
---|
| 554 | q_min=qmin, q_max=qmax |
---|
| 555 | ) |
---|
[f3d51f6] | 556 | |
---|
| 557 | else: |
---|
| 558 | message = "The P(r) form contains invalid values: please submit it again." |
---|
| 559 | wx.PostEvent(self.parent, StatusEvent(status=message)) |
---|
| 560 | |
---|
| 561 | def _change_file(self, evt): |
---|
| 562 | """ |
---|
| 563 | Choose a new input file for I(q) |
---|
| 564 | """ |
---|
| 565 | import os |
---|
| 566 | if not self.manager==None: |
---|
| 567 | path = self.manager.choose_file() |
---|
| 568 | |
---|
| 569 | if path and os.path.isfile(path): |
---|
| 570 | self.data_file.SetValue(str(path)) |
---|
| 571 | self.file_radio.SetValue(True) |
---|
| 572 | self._on_pars_changed(None) |
---|
| 573 | |
---|
| 574 | |
---|
[119a11d] | 575 | |
---|
| 576 | |
---|
| 577 | class HelpDialog(wx.Dialog): |
---|
| 578 | def __init__(self, parent, id): |
---|
| 579 | from sans.pr.invertor import help |
---|
| 580 | wx.Dialog.__init__(self, parent, id, size=(400, 400)) |
---|
| 581 | self.SetTitle("P(r) help") |
---|
| 582 | |
---|
| 583 | |
---|
| 584 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
| 585 | |
---|
| 586 | explanation = help() |
---|
| 587 | |
---|
| 588 | label_explain = wx.StaticText(self, -1, explanation, size=(350,300)) |
---|
| 589 | |
---|
| 590 | vbox.Add(label_explain, 0, wx.ALL|wx.EXPAND, 15) |
---|
| 591 | |
---|
| 592 | |
---|
| 593 | static_line = wx.StaticLine(self, -1) |
---|
| 594 | vbox.Add(static_line, 0, wx.EXPAND, 0) |
---|
| 595 | |
---|
| 596 | button_OK = wx.Button(self, wx.ID_OK, "OK") |
---|
| 597 | #button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
---|
| 598 | |
---|
| 599 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 600 | sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 601 | sizer_button.Add(button_OK, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
| 602 | #sizer_button.Add(button_Cancel, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
| 603 | vbox.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10) |
---|
| 604 | |
---|
| 605 | self.SetSizer(vbox) |
---|
| 606 | self.SetAutoLayout(True) |
---|
| 607 | |
---|
| 608 | self.Layout() |
---|
| 609 | self.Centre() |
---|
| 610 | |
---|
| 611 | |
---|
[f3d51f6] | 612 | class ParsDialog(wx.Panel): |
---|
| 613 | """ |
---|
| 614 | Dialog box to let the user edit detector settings |
---|
| 615 | """ |
---|
| 616 | |
---|
| 617 | def __init__(self, parent, id = id, file=True, **kwargs): |
---|
| 618 | |
---|
| 619 | wx.Panel.__init__(self, parent, id = id, **kwargs) |
---|
| 620 | self.file = file |
---|
| 621 | |
---|
| 622 | self.label_nfunc = wx.StaticText(self, -1, "Number of terms") |
---|
| 623 | self.label_alpha = wx.StaticText(self, -1, "Regularization constant") |
---|
| 624 | self.label_dmax = wx.StaticText(self, -1, "Max distance [A]") |
---|
| 625 | |
---|
| 626 | # Npts, q max |
---|
| 627 | self.nfunc_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
| 628 | self.alpha_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
| 629 | self.dmax_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
| 630 | |
---|
| 631 | self.label_file = None |
---|
| 632 | self.file_ctl = None |
---|
| 633 | |
---|
| 634 | self.static_line_3 = wx.StaticLine(self, -1) |
---|
| 635 | |
---|
| 636 | |
---|
| 637 | |
---|
| 638 | self.__do_layout() |
---|
| 639 | |
---|
| 640 | self.Fit() |
---|
| 641 | |
---|
| 642 | def _load_file(self, evt): |
---|
| 643 | import os |
---|
| 644 | path = None |
---|
| 645 | dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.txt", wx.OPEN) |
---|
| 646 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 647 | path = dlg.GetPath() |
---|
| 648 | mypath = os.path.basename(path) |
---|
| 649 | dlg.Destroy() |
---|
| 650 | |
---|
| 651 | if path and os.path.isfile(path): |
---|
| 652 | self.file_ctl.SetValue(str(path)) |
---|
| 653 | |
---|
| 654 | |
---|
| 655 | def checkValues(self, event): |
---|
| 656 | flag = True |
---|
| 657 | try: |
---|
| 658 | float(self.alpha_ctl.GetValue()) |
---|
| 659 | self.alpha_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 660 | self.alpha_ctl.Refresh() |
---|
| 661 | except: |
---|
| 662 | flag = False |
---|
| 663 | self.alpha_ctl.SetBackgroundColour("pink") |
---|
| 664 | self.alpha_ctl.Refresh() |
---|
| 665 | |
---|
| 666 | try: |
---|
| 667 | float(self.dmax_ctl.GetValue()) |
---|
| 668 | self.dmax_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 669 | self.dmax_ctl.Refresh() |
---|
| 670 | except: |
---|
| 671 | flag = False |
---|
| 672 | self.dmax_ctl.SetBackgroundColour("pink") |
---|
| 673 | self.dmax_ctl.Refresh() |
---|
| 674 | |
---|
| 675 | try: |
---|
| 676 | int(self.nfunc_ctl.GetValue()) |
---|
| 677 | self.nfunc_ctl.SetBackgroundColour(wx.WHITE) |
---|
| 678 | self.nfunc_ctl.Refresh() |
---|
| 679 | except: |
---|
| 680 | flag = False |
---|
| 681 | self.nfunc_ctl.SetBackgroundColour("pink") |
---|
| 682 | self.nfunc_ctl.Refresh() |
---|
| 683 | |
---|
| 684 | if flag: |
---|
| 685 | event.Skip(True) |
---|
| 686 | |
---|
| 687 | def setContent(self, nfunc, alpha, dmax, file): |
---|
| 688 | self.nfunc_ctl.SetValue(str(nfunc)) |
---|
| 689 | self.alpha_ctl.SetValue(str(alpha)) |
---|
| 690 | self.dmax_ctl.SetValue(str(dmax)) |
---|
| 691 | if self.file: |
---|
| 692 | self.file_ctl.SetValue(str(file)) |
---|
| 693 | |
---|
| 694 | def getContent(self): |
---|
| 695 | nfunc = int(self.nfunc_ctl.GetValue()) |
---|
| 696 | alpha = float(self.alpha_ctl.GetValue()) |
---|
| 697 | dmax = float(self.dmax_ctl.GetValue()) |
---|
| 698 | file = None |
---|
| 699 | if self.file: |
---|
| 700 | file = self.file_ctl.GetValue() |
---|
| 701 | return nfunc, alpha, dmax, file |
---|
| 702 | |
---|
| 703 | |
---|
| 704 | def __do_layout(self): |
---|
| 705 | sizer_main = wx.BoxSizer(wx.VERTICAL) |
---|
| 706 | sizer_params = wx.GridBagSizer(5,5) |
---|
| 707 | |
---|
| 708 | iy = 0 |
---|
| 709 | sizer_params.Add(self.label_nfunc, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 710 | sizer_params.Add(self.nfunc_ctl, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 711 | iy += 1 |
---|
| 712 | sizer_params.Add(self.label_alpha, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 713 | sizer_params.Add(self.alpha_ctl, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 714 | iy += 1 |
---|
| 715 | sizer_params.Add(self.label_dmax, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 716 | sizer_params.Add(self.dmax_ctl, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 717 | iy += 1 |
---|
| 718 | if self.file: |
---|
| 719 | self.label_file = wx.StaticText(self, -1, "Input file") |
---|
| 720 | self.file_ctl = wx.TextCtrl(self, -1, size=(120,20)) |
---|
| 721 | sizer_params.Add(self.label_file, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 722 | sizer_params.Add(self.file_ctl, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 723 | |
---|
| 724 | sizer_main.Add(sizer_params, 0, wx.EXPAND|wx.ALL, 10) |
---|
| 725 | |
---|
| 726 | |
---|
| 727 | if self.file: |
---|
| 728 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 729 | self.button_load = wx.Button(self, 1, "Choose file") |
---|
| 730 | self.Bind(wx.EVT_BUTTON, self._load_file, id = 1) |
---|
| 731 | sizer_button.Add(self.button_load, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10) |
---|
| 732 | |
---|
| 733 | |
---|
| 734 | sizer_main.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10) |
---|
| 735 | self.SetAutoLayout(True) |
---|
| 736 | self.SetSizer(sizer_main) |
---|
| 737 | self.Layout() |
---|
| 738 | self.Centre() |
---|
| 739 | # end wxGlade |
---|
| 740 | |
---|
| 741 | |
---|
| 742 | # end of class DialogAbout |
---|
| 743 | |
---|
| 744 | ##### testing code ############################################################ |
---|
| 745 | class TestPlot: |
---|
| 746 | def __init__(self, text): |
---|
| 747 | self.name = text |
---|
| 748 | |
---|
| 749 | class MyApp(wx.App): |
---|
| 750 | def OnInit(self): |
---|
| 751 | wx.InitAllImageHandlers() |
---|
[119a11d] | 752 | dialog = HelpDialog(None, -1) |
---|
[f3d51f6] | 753 | if dialog.ShowModal() == wx.ID_OK: |
---|
[119a11d] | 754 | pass |
---|
[f3d51f6] | 755 | dialog.Destroy() |
---|
| 756 | |
---|
| 757 | return 1 |
---|
| 758 | |
---|
| 759 | # end of class MyApp |
---|
| 760 | |
---|
| 761 | if __name__ == "__main__": |
---|
| 762 | app = MyApp(0) |
---|
| 763 | app.MainLoop() |
---|
| 764 | |
---|
| 765 | ##### end of testing code ##################################################### |
---|