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 | import os |
---|
9 | from sans.guicomm.events import StatusEvent |
---|
10 | from inversion_state import InversionState |
---|
11 | |
---|
12 | class InversionDlg(wx.Dialog): |
---|
13 | def __init__(self, parent, id, title, plots, file=False, pars=True): |
---|
14 | |
---|
15 | # Estimate size |
---|
16 | nplots = len(plots) |
---|
17 | # y size for data set only |
---|
18 | ysize = 110 + nplots*20 |
---|
19 | # y size including parameters |
---|
20 | if pars: |
---|
21 | ysize += 90 |
---|
22 | |
---|
23 | wx.Dialog.__init__(self, parent, id, title, size=(250, ysize)) |
---|
24 | self.SetTitle(title) |
---|
25 | |
---|
26 | # Data set |
---|
27 | self.datasets = InversionPanel(self, -1, plots) |
---|
28 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
29 | |
---|
30 | vbox.Add(self.datasets) |
---|
31 | |
---|
32 | # Parameters |
---|
33 | self.pars_flag = False |
---|
34 | if pars==True: |
---|
35 | self.pars_flag = True |
---|
36 | self.pars = ParsDialog(self, -1, file=file) |
---|
37 | vbox.Add(self.pars) |
---|
38 | |
---|
39 | static_line = wx.StaticLine(self, -1) |
---|
40 | vbox.Add(static_line, 0, wx.EXPAND, 0) |
---|
41 | |
---|
42 | button_OK = wx.Button(self, wx.ID_OK, "OK") |
---|
43 | button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
---|
44 | |
---|
45 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
46 | sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
47 | sizer_button.Add(button_OK, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10) |
---|
48 | sizer_button.Add(button_Cancel, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
49 | vbox.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10) |
---|
50 | |
---|
51 | self.SetSizer(vbox) |
---|
52 | self.SetAutoLayout(True) |
---|
53 | |
---|
54 | self.Layout() |
---|
55 | self.Centre() |
---|
56 | |
---|
57 | def get_content(self): |
---|
58 | dataset = self.datasets.get_selected() |
---|
59 | if self.pars_flag: |
---|
60 | nfunc, alpha, dmax, file = self.pars.getContent() |
---|
61 | return dataset, nfunc, alpha, dmax |
---|
62 | else: |
---|
63 | return dataset |
---|
64 | |
---|
65 | def set_content(self, dataset, nfunc, alpha, dmax): |
---|
66 | if not dataset==None and dataset in self.datasets.radio_buttons.keys(): |
---|
67 | self.datasets.radio_buttons[dataset].SetValue(True) |
---|
68 | if self.pars_flag: |
---|
69 | self.pars.setContent(nfunc, alpha, dmax, None) |
---|
70 | |
---|
71 | class InversionPanel(wx.Panel): |
---|
72 | |
---|
73 | def __init__(self, parent, id = -1, plots = None, **kwargs): |
---|
74 | wx.Panel.__init__(self, parent, id = id, **kwargs) |
---|
75 | |
---|
76 | self.plots = plots |
---|
77 | self.radio_buttons = {} |
---|
78 | |
---|
79 | self._do_layout() |
---|
80 | |
---|
81 | def _do_layout(self): |
---|
82 | panel = wx.Panel(self, -1) |
---|
83 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
84 | |
---|
85 | ysize = 30+20*len(self.plots) |
---|
86 | wx.StaticBox(panel, -1, 'Choose a data set', (5, 5), (230, ysize)) |
---|
87 | ypos = 30 |
---|
88 | self.radio_buttons = {} |
---|
89 | for item in self.plots.keys(): |
---|
90 | self.radio_buttons[self.plots[item].name] = wx.RadioButton(panel, -1, self.plots[item].name, (15, ypos)) |
---|
91 | ypos += 20 |
---|
92 | |
---|
93 | vbox.Add(panel) |
---|
94 | |
---|
95 | self.SetSizer(vbox) |
---|
96 | |
---|
97 | def get_selected(self): |
---|
98 | for item in self.radio_buttons: |
---|
99 | if self.radio_buttons[item].GetValue(): |
---|
100 | return item |
---|
101 | return None |
---|
102 | |
---|
103 | class InversionControl(wx.Panel): |
---|
104 | window_name = 'pr_control' |
---|
105 | window_caption = "P(r) control panel" |
---|
106 | CENTER_PANE = True |
---|
107 | |
---|
108 | # Figure of merit parameters [default] |
---|
109 | |
---|
110 | ## Oscillation parameters (sin function = 1.1) |
---|
111 | oscillation_max = 1.5 |
---|
112 | |
---|
113 | def __init__(self, parent, id = -1, plots = None, standalone=False, **kwargs): |
---|
114 | wx.Panel.__init__(self, parent, id = id, **kwargs) |
---|
115 | |
---|
116 | self.plots = plots |
---|
117 | self.radio_buttons = {} |
---|
118 | |
---|
119 | ## Data file TextCtrl |
---|
120 | self.data_file = None |
---|
121 | self.plot_data = None |
---|
122 | self.nfunc_ctl = None |
---|
123 | self.alpha_ctl = None |
---|
124 | self.dmax_ctl = None |
---|
125 | self.time_ctl = None |
---|
126 | self.chi2_ctl = None |
---|
127 | self.osc_ctl = None |
---|
128 | self.file_radio = None |
---|
129 | self.plot_radio = None |
---|
130 | self.label_sugg = None |
---|
131 | self.qmin_ctl = None |
---|
132 | self.qmax_ctl = None |
---|
133 | self.swidth_ctl = None |
---|
134 | self.sheight_ctl = None |
---|
135 | |
---|
136 | self.rg_ctl = None |
---|
137 | self.iq0_ctl = None |
---|
138 | self.bck_chk = None |
---|
139 | self.bck_ctl = None |
---|
140 | |
---|
141 | # TextCtrl for fraction of positive P(r) |
---|
142 | self.pos_ctl = None |
---|
143 | |
---|
144 | # TextCtrl for fraction of 1 sigma positive P(r) |
---|
145 | self.pos_err_ctl = None |
---|
146 | |
---|
147 | ## Estimates |
---|
148 | self.alpha_estimate_ctl = None |
---|
149 | self.nterms_estimate_ctl = None |
---|
150 | |
---|
151 | ## D_max distance explorator |
---|
152 | self.distance_explorator_ctl = None |
---|
153 | |
---|
154 | ## Data manager |
---|
155 | self.manager = None |
---|
156 | |
---|
157 | ## Standalone flage |
---|
158 | self.standalone = standalone |
---|
159 | |
---|
160 | ## Default file location for save |
---|
161 | self._default_save_location = os.getcwd() |
---|
162 | |
---|
163 | self._do_layout() |
---|
164 | |
---|
165 | def __setattr__(self, name, value): |
---|
166 | """ |
---|
167 | Allow direct hooks to text boxes |
---|
168 | """ |
---|
169 | if name=='nfunc': |
---|
170 | self.nfunc_ctl.SetValue(str(int(value))) |
---|
171 | elif name=='d_max': |
---|
172 | self.dmax_ctl.SetValue(str(value)) |
---|
173 | elif name=='alpha': |
---|
174 | self.alpha_ctl.SetValue(str(value)) |
---|
175 | elif name=='chi2': |
---|
176 | self.chi2_ctl.SetValue("%-5.2g" % value) |
---|
177 | elif name=='bck': |
---|
178 | self.bck_ctl.SetValue("%-5.2g" % value) |
---|
179 | elif name=='q_min': |
---|
180 | self.qmin_ctl.SetValue("%-5.2g" % value) |
---|
181 | elif name=='q_max': |
---|
182 | self.qmax_ctl.SetValue("%-5.2g" % value) |
---|
183 | elif name=='elapsed': |
---|
184 | self.time_ctl.SetValue("%-5.2g" % value) |
---|
185 | elif name=='rg': |
---|
186 | self.rg_ctl.SetValue("%-5.2g" % value) |
---|
187 | elif name=='iq0': |
---|
188 | self.iq0_ctl.SetValue("%-5.2g" % value) |
---|
189 | elif name=='oscillation': |
---|
190 | self.osc_ctl.SetValue("%-5.2g" % value) |
---|
191 | elif name=='slit_width': |
---|
192 | self.swidth_ctl.SetValue("%-5.2g" % value) |
---|
193 | elif name=='slit_height': |
---|
194 | self.sheight_ctl.SetValue("%-5.2g" % value) |
---|
195 | elif name=='positive': |
---|
196 | self.pos_ctl.SetValue("%-5.2g" % value) |
---|
197 | elif name=='pos_err': |
---|
198 | self.pos_err_ctl.SetValue("%-5.2g" % value) |
---|
199 | elif name=='alpha_estimate': |
---|
200 | self.alpha_estimate_ctl.SetToolTipString("Click to accept value.") |
---|
201 | self.alpha_estimate_ctl.Enable(True) |
---|
202 | self.alpha_estimate_ctl.SetLabel("%-3.1g" % value) |
---|
203 | #self.alpha_estimate_ctl.Show() |
---|
204 | #self.label_sugg.Show() |
---|
205 | elif name=='nterms_estimate': |
---|
206 | self.nterms_estimate_ctl.SetToolTipString("Click to accept value.") |
---|
207 | self.nterms_estimate_ctl.Enable(True) |
---|
208 | self.nterms_estimate_ctl.SetLabel("%-g" % value) |
---|
209 | elif name=='plotname': |
---|
210 | self.plot_data.SetValue(str(value)) |
---|
211 | self._on_pars_changed(None) |
---|
212 | elif name=='datafile': |
---|
213 | self.plot_data.SetValue(str(value)) |
---|
214 | self._on_pars_changed(None) |
---|
215 | else: |
---|
216 | wx.Panel.__setattr__(self, name, value) |
---|
217 | |
---|
218 | def __getattr__(self, name): |
---|
219 | """ |
---|
220 | Allow direct hooks to text boxes |
---|
221 | """ |
---|
222 | if name=='nfunc': |
---|
223 | try: |
---|
224 | return int(self.nfunc_ctl.GetValue()) |
---|
225 | except: |
---|
226 | return -1 |
---|
227 | elif name=='d_max': |
---|
228 | try: |
---|
229 | return self.dmax_ctl.GetValue() |
---|
230 | except: |
---|
231 | return -1.0 |
---|
232 | elif name=='alpha': |
---|
233 | try: |
---|
234 | return self.alpha_ctl.GetValue() |
---|
235 | except: |
---|
236 | return -1.0 |
---|
237 | elif name=='chi2': |
---|
238 | try: |
---|
239 | return float(self.chi2_ctl.GetValue()) |
---|
240 | except: |
---|
241 | return None |
---|
242 | elif name=='bck': |
---|
243 | try: |
---|
244 | return float(self.bck_ctl.GetValue()) |
---|
245 | except: |
---|
246 | return None |
---|
247 | elif name=='q_min': |
---|
248 | try: |
---|
249 | return float(self.qmin_ctl.GetValue()) |
---|
250 | except: |
---|
251 | return 0.0 |
---|
252 | elif name=='q_max': |
---|
253 | try: |
---|
254 | return float(self.qmax_ctl.GetValue()) |
---|
255 | except: |
---|
256 | return 0.0 |
---|
257 | elif name=='elapsed': |
---|
258 | try: |
---|
259 | return float(self.time_ctl.GetValue()) |
---|
260 | except: |
---|
261 | return None |
---|
262 | elif name=='rg': |
---|
263 | try: |
---|
264 | return float(self.rg_ctl.GetValue()) |
---|
265 | except: |
---|
266 | return None |
---|
267 | elif name=='iq0': |
---|
268 | try: |
---|
269 | return float(self.iq0_ctl.GetValue()) |
---|
270 | except: |
---|
271 | return None |
---|
272 | elif name=='oscillation': |
---|
273 | try: |
---|
274 | return float(self.osc_ctl.GetValue()) |
---|
275 | except: |
---|
276 | return None |
---|
277 | elif name=='slit_width': |
---|
278 | try: |
---|
279 | return float(self.swidth_ctl.GetValue()) |
---|
280 | except: |
---|
281 | return None |
---|
282 | elif name=='slit_height': |
---|
283 | try: |
---|
284 | return float(self.sheight_ctl.GetValue()) |
---|
285 | except: |
---|
286 | return None |
---|
287 | elif name=='pos': |
---|
288 | try: |
---|
289 | return float(self.pos_ctl.GetValue()) |
---|
290 | except: |
---|
291 | return None |
---|
292 | elif name=='pos_err': |
---|
293 | try: |
---|
294 | return float(self.pos_err_ctl.GetValue()) |
---|
295 | except: |
---|
296 | return None |
---|
297 | elif name=='alpha_estimate': |
---|
298 | try: |
---|
299 | return float(self.alpha_estimate_ctl.GetLabel()) |
---|
300 | except: |
---|
301 | return None |
---|
302 | elif name=='nterms_estimate': |
---|
303 | try: |
---|
304 | return int(self.nterms_estimate_ctl.GetLabel()) |
---|
305 | except: |
---|
306 | return None |
---|
307 | elif name=='plotname': |
---|
308 | return self.plot_data.GetValue() |
---|
309 | elif name=='datafile': |
---|
310 | return self.plot_data.GetValue() |
---|
311 | else: |
---|
312 | return wx.Panel.__getattribute__(self, name) |
---|
313 | #return wx.Panel.__getattr__(self, name) |
---|
314 | |
---|
315 | def _save_state(self, evt=None): |
---|
316 | """ |
---|
317 | Method used to create a memento of the current state |
---|
318 | |
---|
319 | @return: state object |
---|
320 | """ |
---|
321 | # Ask the user the location of the file to write to. |
---|
322 | path = None |
---|
323 | dlg = wx.FileDialog(self, "Choose a file", self._default_save_location, "", "*.prv", wx.SAVE) |
---|
324 | if dlg.ShowModal() == wx.ID_OK: |
---|
325 | path = dlg.GetPath() |
---|
326 | self._default_save_location = os.path.dirname(path) |
---|
327 | else: |
---|
328 | return None |
---|
329 | |
---|
330 | dlg.Destroy() |
---|
331 | |
---|
332 | # Construct the state object |
---|
333 | state = InversionState() |
---|
334 | |
---|
335 | # Read the panel's parameters |
---|
336 | flag, alpha, dmax, nfunc, qmin, \ |
---|
337 | qmax, height, width = self._read_pars() |
---|
338 | |
---|
339 | state.nfunc = nfunc |
---|
340 | state.d_max = dmax |
---|
341 | state.alpha = alpha |
---|
342 | state.qmin = qmin |
---|
343 | state.qmax = qmax |
---|
344 | state.width = width |
---|
345 | state.height = height |
---|
346 | |
---|
347 | # Data file |
---|
348 | state.file = self.plot_data.GetValue() |
---|
349 | |
---|
350 | # Background evaluation checkbox |
---|
351 | state.estimate_bck = self.bck_chk.IsChecked() |
---|
352 | |
---|
353 | # Estimates |
---|
354 | state.nterms_estimate = self.nterms_estimate |
---|
355 | state.alpha_estimate = self.alpha_estimate |
---|
356 | |
---|
357 | # Read the output values |
---|
358 | state.chi2 = self.chi2 |
---|
359 | state.elapsed = self.elapsed |
---|
360 | state.osc = self.oscillation |
---|
361 | state.pos = self.pos |
---|
362 | state.pos_err = self.pos_err |
---|
363 | state.rg = self.rg |
---|
364 | state.iq0 = self.iq0 |
---|
365 | state.bck = self.bck |
---|
366 | |
---|
367 | self.manager.save_data(filepath=path, prstate=state) |
---|
368 | |
---|
369 | return state |
---|
370 | |
---|
371 | def set_state(self, state): |
---|
372 | """ |
---|
373 | Set the state of the panel and inversion problem to |
---|
374 | the state passed as a parameter. |
---|
375 | Execute the inversion immediately after filling the |
---|
376 | controls. |
---|
377 | |
---|
378 | @param state: InversionState object |
---|
379 | """ |
---|
380 | if state.nfunc is not None: |
---|
381 | self.nfunc = state.nfunc |
---|
382 | if state.d_max is not None: |
---|
383 | self.d_max = state.d_max |
---|
384 | if state.alpha is not None: |
---|
385 | self.alpha = state.alpha |
---|
386 | if state.qmin is not None: |
---|
387 | self.q_min = state.qmin |
---|
388 | if state.qmax is not None: |
---|
389 | self.q_max = state.qmax |
---|
390 | if state.width is not None: |
---|
391 | self.slit_width = state.width |
---|
392 | if state.height is not None: |
---|
393 | self.slit_height = state.height |
---|
394 | |
---|
395 | # Data file |
---|
396 | self.plot_data.SetValue(str(state.file)) |
---|
397 | |
---|
398 | # Background evaluation checkbox |
---|
399 | self.bck_chk.SetValue(state.estimate_bck) |
---|
400 | |
---|
401 | # Estimates |
---|
402 | if state.nterms_estimate is not None: |
---|
403 | self.nterms_estimate = state.nterms_estimate |
---|
404 | if state.alpha_estimate is not None: |
---|
405 | self.alpha_estimate = state.alpha_estimate |
---|
406 | |
---|
407 | |
---|
408 | # Read the output values |
---|
409 | if state.chi2 is not None: |
---|
410 | self.chi2 = state.chi2 |
---|
411 | if state.elapsed is not None: |
---|
412 | self.elapsed = state.elapsed |
---|
413 | if state.osc is not None: |
---|
414 | self.oscillation = state.osc |
---|
415 | if state.pos is not None: |
---|
416 | self.positive = state.pos |
---|
417 | if state.pos_err is not None: |
---|
418 | self.pos_err = state.pos_err |
---|
419 | if state.rg is not None: |
---|
420 | self.rg = state.rg |
---|
421 | if state.iq0 is not None: |
---|
422 | self.iq0 = state.iq0 |
---|
423 | if state.bck is not None: |
---|
424 | self.bck = state.bck |
---|
425 | |
---|
426 | # Perform inversion |
---|
427 | if self.standalone == False: |
---|
428 | self._on_invert(None) |
---|
429 | |
---|
430 | def set_manager(self, manager): |
---|
431 | self.manager = manager |
---|
432 | |
---|
433 | def _do_layout(self): |
---|
434 | vbox = wx.GridBagSizer(0,0) |
---|
435 | iy_vb = 0 |
---|
436 | |
---|
437 | # ----- I(q) data ----- |
---|
438 | databox = wx.StaticBox(self, -1, "I(q) data source") |
---|
439 | |
---|
440 | boxsizer1 = wx.StaticBoxSizer(databox, wx.VERTICAL) |
---|
441 | boxsizer1.SetMinSize((320,50)) |
---|
442 | pars_sizer = wx.GridBagSizer(5,5) |
---|
443 | |
---|
444 | iy = 0 |
---|
445 | self.file_radio = wx.StaticText(self, -1, "Data:") |
---|
446 | pars_sizer.Add(self.file_radio, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
447 | |
---|
448 | self.plot_data = wx.TextCtrl(self, -1, size=(220,20)) |
---|
449 | self.plot_data.SetEditable(False) |
---|
450 | pars_sizer.Add(self.plot_data, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
451 | |
---|
452 | self.bck_chk = wx.CheckBox(self, -1, "Estimate background level") |
---|
453 | self.bck_chk.SetToolTipString("Check box to let the fit estimate the constant background level.") |
---|
454 | self.bck_chk.Bind(wx.EVT_CHECKBOX, self._on_pars_changed) |
---|
455 | iy += 1 |
---|
456 | pars_sizer.Add(self.bck_chk, (iy,0), (1,2), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
457 | boxsizer1.Add(pars_sizer, 0, wx.EXPAND) |
---|
458 | vbox.Add(boxsizer1, (iy_vb,0), (1,1), wx.RIGHT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
459 | |
---|
460 | # ----- Add slit parameters ----- |
---|
461 | if True: |
---|
462 | sbox = wx.StaticBox(self, -1, "Slit parameters") |
---|
463 | sboxsizer = wx.StaticBoxSizer(sbox, wx.VERTICAL) |
---|
464 | sboxsizer.SetMinSize((320,20)) |
---|
465 | |
---|
466 | sizer_slit = wx.GridBagSizer(5,5) |
---|
467 | |
---|
468 | label_sheight = wx.StaticText(self, -1, "Height", size=(40,20)) |
---|
469 | label_swidth = wx.StaticText(self, -1, "Width", size=(40,20)) |
---|
470 | #label_sunits1 = wx.StaticText(self, -1, "[A^(-1)]") |
---|
471 | label_sunits2 = wx.StaticText(self, -1, "[A^(-1)]", size=(55,20)) |
---|
472 | self.sheight_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
473 | self.swidth_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
474 | self.sheight_ctl.SetToolTipString("Enter slit height in units of Q or leave blank.") |
---|
475 | self.swidth_ctl.SetToolTipString("Enter slit width in units of Q or leave blank.") |
---|
476 | #self.sheight_ctl.Bind(wx.EVT_TEXT, self._on_pars_changed) |
---|
477 | #self.swidth_ctl.Bind(wx.EVT_TEXT, self._on_pars_changed) |
---|
478 | |
---|
479 | iy = 0 |
---|
480 | sizer_slit.Add(label_sheight, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 5) |
---|
481 | sizer_slit.Add(self.sheight_ctl, (iy,1), (1,1), wx.LEFT|wx.EXPAND, 5) |
---|
482 | #sizer_slit.Add(label_sunits1, (iy,2), (1,1), wx.LEFT|wx.EXPAND, 10) |
---|
483 | sizer_slit.Add(label_swidth, (iy,2), (1,1), wx.LEFT|wx.EXPAND, 5) |
---|
484 | sizer_slit.Add(self.swidth_ctl, (iy,3), (1,1), wx.LEFT|wx.EXPAND, 5) |
---|
485 | sizer_slit.Add(label_sunits2, (iy,4), (1,1), wx.LEFT|wx.EXPAND, 5) |
---|
486 | |
---|
487 | sboxsizer.Add(sizer_slit, wx.TOP, 15) |
---|
488 | iy_vb += 1 |
---|
489 | vbox.Add(sboxsizer, (iy_vb,0), (1,1), wx.RIGHT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
490 | |
---|
491 | |
---|
492 | # ----- Q range ----- |
---|
493 | qbox = wx.StaticBox(self, -1, "Q range") |
---|
494 | qboxsizer = wx.StaticBoxSizer(qbox, wx.VERTICAL) |
---|
495 | qboxsizer.SetMinSize((320,20)) |
---|
496 | |
---|
497 | sizer_q = wx.GridBagSizer(5,5) |
---|
498 | |
---|
499 | label_qmin = wx.StaticText(self, -1, "Q min", size=(40,20)) |
---|
500 | label_qmax = wx.StaticText(self, -1, "Q max", size=(40,20)) |
---|
501 | #label_qunits1 = wx.StaticText(self, -1, "[A^(-1)]") |
---|
502 | label_qunits2 = wx.StaticText(self, -1, "[A^(-1)]", size=(55,20)) |
---|
503 | self.qmin_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
504 | self.qmax_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
505 | self.qmin_ctl.SetToolTipString("Select a lower bound for Q or leave blank.") |
---|
506 | self.qmax_ctl.SetToolTipString("Select an upper bound for Q or leave blank.") |
---|
507 | self.qmin_ctl.Bind(wx.EVT_TEXT, self._on_pars_changed) |
---|
508 | self.qmax_ctl.Bind(wx.EVT_TEXT, self._on_pars_changed) |
---|
509 | |
---|
510 | iy = 0 |
---|
511 | sizer_q.Add(label_qmin, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 5) |
---|
512 | sizer_q.Add(self.qmin_ctl, (iy,1), (1,1), wx.LEFT|wx.EXPAND, 5) |
---|
513 | #sizer_q.Add(label_qunits1, (iy,2), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
514 | sizer_q.Add(label_qmax, (iy,2), (1,1), wx.LEFT|wx.EXPAND, 5) |
---|
515 | sizer_q.Add(self.qmax_ctl, (iy,3), (1,1), wx.LEFT|wx.EXPAND, 5) |
---|
516 | sizer_q.Add(label_qunits2, (iy,4), (1,1), wx.LEFT|wx.EXPAND, 5) |
---|
517 | qboxsizer.Add(sizer_q, wx.TOP, 15) |
---|
518 | |
---|
519 | iy_vb += 1 |
---|
520 | vbox.Add(qboxsizer, (iy_vb,0), (1,1), wx.RIGHT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
521 | |
---|
522 | |
---|
523 | |
---|
524 | |
---|
525 | # ----- Parameters ----- |
---|
526 | parsbox = wx.StaticBox(self, -1, "Parameters") |
---|
527 | boxsizer2 = wx.StaticBoxSizer(parsbox, wx.VERTICAL) |
---|
528 | boxsizer2.SetMinSize((320,50)) |
---|
529 | |
---|
530 | explanation = "P(r) is found by fitting a set of base functions to I(Q). " |
---|
531 | explanation += "The minimization involves a regularization term to ensure " |
---|
532 | explanation += "a smooth P(r). The regularization constant gives the size of that " |
---|
533 | explanation += "term. The suggested value is the value above which the " |
---|
534 | explanation += "output P(r) will have only one peak." |
---|
535 | label_explain = wx.StaticText(self, -1, explanation, size=(280,80)) |
---|
536 | boxsizer2.Add(label_explain, wx.LEFT|wx.BOTTOM, 5) |
---|
537 | |
---|
538 | |
---|
539 | |
---|
540 | label_nfunc = wx.StaticText(self, -1, "Number of terms") |
---|
541 | label_nfunc.SetMinSize((120,20)) |
---|
542 | label_alpha = wx.StaticText(self, -1, "Regularization constant") |
---|
543 | label_dmax = wx.StaticText(self, -1, "Max distance [A]") |
---|
544 | self.label_sugg = wx.StaticText(self, -1, "Suggested value") |
---|
545 | #self.label_sugg.Hide() |
---|
546 | |
---|
547 | self.nfunc_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
548 | self.nfunc_ctl.SetToolTipString("Number of terms in the expansion.") |
---|
549 | self.alpha_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
550 | self.alpha_ctl.SetToolTipString("Control parameter for the size of the regularization term.") |
---|
551 | self.dmax_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
552 | self.dmax_ctl.SetToolTipString("Maximum distance between any two points in the system.") |
---|
553 | id = wx.NewId() |
---|
554 | self.alpha_estimate_ctl = wx.Button(self, id, "") |
---|
555 | #self.alpha_estimate_ctl.Hide() |
---|
556 | self.Bind(wx.EVT_BUTTON, self._on_accept_alpha, id = id) |
---|
557 | self.alpha_estimate_ctl.Enable(False) |
---|
558 | #self.alpha_estimate_ctl.SetBackgroundColour('#ffdf85') |
---|
559 | #self.alpha_estimate_ctl.SetBackgroundColour(self.GetBackgroundColour()) |
---|
560 | self.alpha_estimate_ctl.SetToolTipString("Waiting for estimate...") |
---|
561 | |
---|
562 | id = wx.NewId() |
---|
563 | self.nterms_estimate_ctl = wx.Button(self, id, "") |
---|
564 | #self.nterms_estimate_ctl.Hide() |
---|
565 | self.Bind(wx.EVT_BUTTON, self._on_accept_nterms, id = id) |
---|
566 | self.nterms_estimate_ctl.Enable(False) |
---|
567 | #self.nterms_estimate_ctl.SetBackgroundColour('#ffdf85') |
---|
568 | #self.nterms_estimate_ctl.SetBackgroundColour(self.GetBackgroundColour()) |
---|
569 | self.nterms_estimate_ctl.SetToolTipString("Waiting for estimate...") |
---|
570 | |
---|
571 | self.nfunc_ctl.Bind(wx.EVT_TEXT, self._read_pars) |
---|
572 | self.alpha_ctl.Bind(wx.EVT_TEXT, self._read_pars) |
---|
573 | self.dmax_ctl.Bind(wx.EVT_TEXT, self._on_pars_changed) |
---|
574 | |
---|
575 | # Distance explorator |
---|
576 | id = wx.NewId() |
---|
577 | self.distance_explorator_ctl = wx.Button(self, id, "Explore") |
---|
578 | self.Bind(wx.EVT_BUTTON, self._on_explore, id = id) |
---|
579 | |
---|
580 | |
---|
581 | sizer_params = wx.GridBagSizer(5,5) |
---|
582 | |
---|
583 | iy = 0 |
---|
584 | sizer_params.Add(self.label_sugg, (iy,2), (1,1), wx.LEFT, 15) |
---|
585 | iy += 1 |
---|
586 | sizer_params.Add(label_nfunc, (iy,0), (1,1), wx.LEFT, 15) |
---|
587 | sizer_params.Add(self.nfunc_ctl, (iy,1), (1,1), wx.RIGHT, 0) |
---|
588 | sizer_params.Add(self.nterms_estimate_ctl, (iy,2), (1,1), wx.LEFT, 15) |
---|
589 | iy += 1 |
---|
590 | sizer_params.Add(label_alpha, (iy,0), (1,1), wx.LEFT, 15) |
---|
591 | sizer_params.Add(self.alpha_ctl, (iy,1), (1,1), wx.RIGHT, 0) |
---|
592 | sizer_params.Add(self.alpha_estimate_ctl, (iy,2), (1,1), wx.LEFT, 15) |
---|
593 | iy += 1 |
---|
594 | sizer_params.Add(label_dmax, (iy,0), (1,1), wx.LEFT, 15) |
---|
595 | sizer_params.Add(self.dmax_ctl, (iy,1), (1,1), wx.RIGHT, 0) |
---|
596 | sizer_params.Add(self.distance_explorator_ctl, (iy,2), (1,1), wx.LEFT, 15) |
---|
597 | |
---|
598 | boxsizer2.Add(sizer_params, 0) |
---|
599 | |
---|
600 | iy_vb += 1 |
---|
601 | vbox.Add(boxsizer2, (iy_vb,0), (1,1), wx.RIGHT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
602 | |
---|
603 | |
---|
604 | # ----- Results ----- |
---|
605 | resbox = wx.StaticBox(self, -1, "Outputs") |
---|
606 | ressizer = wx.StaticBoxSizer(resbox, wx.VERTICAL) |
---|
607 | ressizer.SetMinSize((320,50)) |
---|
608 | |
---|
609 | label_rg = wx.StaticText(self, -1, "Rg") |
---|
610 | label_rg_unit = wx.StaticText(self, -1, "[A]") |
---|
611 | label_iq0 = wx.StaticText(self, -1, "I(Q=0)") |
---|
612 | label_iq0_unit = wx.StaticText(self, -1, "[A^(-1)]") |
---|
613 | label_bck = wx.StaticText(self, -1, "Background") |
---|
614 | label_bck_unit = wx.StaticText(self, -1, "[A^(-1)]") |
---|
615 | self.rg_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
616 | self.rg_ctl.SetEditable(False) |
---|
617 | self.rg_ctl.SetToolTipString("Radius of gyration for the computed P(r).") |
---|
618 | self.iq0_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
619 | self.iq0_ctl.SetEditable(False) |
---|
620 | self.iq0_ctl.SetToolTipString("Scattering intensity at Q=0 for the computed P(r).") |
---|
621 | self.bck_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
622 | self.bck_ctl.SetEditable(False) |
---|
623 | self.bck_ctl.SetToolTipString("Value of estimated constant background.") |
---|
624 | |
---|
625 | label_time = wx.StaticText(self, -1, "Computation time") |
---|
626 | label_time_unit = wx.StaticText(self, -1, "secs") |
---|
627 | label_time.SetMinSize((120,20)) |
---|
628 | label_chi2 = wx.StaticText(self, -1, "Chi2/dof") |
---|
629 | label_osc = wx.StaticText(self, -1, "Oscillations") |
---|
630 | label_pos = wx.StaticText(self, -1, "Positive fraction") |
---|
631 | label_pos_err = wx.StaticText(self, -1, "1-sigma positive fraction") |
---|
632 | |
---|
633 | self.time_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
634 | self.time_ctl.SetEditable(False) |
---|
635 | self.time_ctl.SetToolTipString("Computation time for the last inversion, in seconds.") |
---|
636 | |
---|
637 | self.chi2_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
638 | self.chi2_ctl.SetEditable(False) |
---|
639 | self.chi2_ctl.SetToolTipString("Chi^2 over degrees of freedom.") |
---|
640 | |
---|
641 | # Oscillation parameter |
---|
642 | self.osc_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
643 | self.osc_ctl.SetEditable(False) |
---|
644 | self.osc_ctl.SetToolTipString("Oscillation parameter. P(r) for a sphere has an oscillation parameter of 1.1.") |
---|
645 | |
---|
646 | # Positive fraction figure of merit |
---|
647 | self.pos_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
648 | self.pos_ctl.SetEditable(False) |
---|
649 | self.pos_ctl.SetToolTipString("Fraction of P(r) that is positive. Theoretically, P(r) is defined positive.") |
---|
650 | |
---|
651 | # 1-simga positive fraction figure of merit |
---|
652 | self.pos_err_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
653 | self.pos_err_ctl.SetEditable(False) |
---|
654 | message = "Fraction of P(r) that is at least 1 standard deviation greater than zero.\n" |
---|
655 | message += "This figure of merit tells you about the size of the P(r) errors.\n" |
---|
656 | message += "If it is close to 1 and the other figures of merit are bad, consider changing " |
---|
657 | message += "the maximum distance." |
---|
658 | self.pos_err_ctl.SetToolTipString(message) |
---|
659 | |
---|
660 | sizer_res = wx.GridBagSizer(5,5) |
---|
661 | |
---|
662 | iy = 0 |
---|
663 | sizer_res.Add(label_rg, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
664 | sizer_res.Add(self.rg_ctl, (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
665 | sizer_res.Add(label_rg_unit, (iy,2), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
666 | iy += 1 |
---|
667 | sizer_res.Add(label_iq0, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
668 | sizer_res.Add(self.iq0_ctl, (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
669 | sizer_res.Add(label_iq0_unit, (iy,2), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
670 | iy += 1 |
---|
671 | sizer_res.Add(label_bck, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
672 | sizer_res.Add(self.bck_ctl, (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
673 | sizer_res.Add(label_bck_unit, (iy,2), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
674 | iy += 1 |
---|
675 | sizer_res.Add(label_time, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
676 | sizer_res.Add(self.time_ctl, (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
677 | sizer_res.Add(label_time_unit, (iy,2), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
678 | iy += 1 |
---|
679 | sizer_res.Add(label_chi2, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
680 | sizer_res.Add(self.chi2_ctl, (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
681 | iy += 1 |
---|
682 | sizer_res.Add(label_osc, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
683 | sizer_res.Add(self.osc_ctl, (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
684 | |
---|
685 | iy += 1 |
---|
686 | sizer_res.Add(label_pos, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
687 | sizer_res.Add(self.pos_ctl, (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
688 | |
---|
689 | iy += 1 |
---|
690 | sizer_res.Add(label_pos_err, (iy,0), (1,1), wx.LEFT|wx.EXPAND, 15) |
---|
691 | sizer_res.Add(self.pos_err_ctl, (iy,1), (1,1), wx.RIGHT|wx.EXPAND, 15) |
---|
692 | |
---|
693 | ressizer.Add(sizer_res, 0) |
---|
694 | iy_vb += 1 |
---|
695 | vbox.Add(ressizer, (iy_vb,0), (1,1), wx.RIGHT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
696 | |
---|
697 | # ----- Buttons ----- |
---|
698 | id = wx.NewId() |
---|
699 | button_OK = wx.Button(self, id, "Compute") |
---|
700 | button_OK.SetToolTipString("Perform P(r) inversion.") |
---|
701 | self.Bind(wx.EVT_BUTTON, self._on_invert, id = id) |
---|
702 | |
---|
703 | id = wx.NewId() |
---|
704 | button_Reset = wx.Button(self, id, "Reset") |
---|
705 | button_Reset.SetToolTipString("Reset inversion parameters to default.") |
---|
706 | self.Bind(wx.EVT_BUTTON, self._on_reset, id = id) |
---|
707 | #button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
---|
708 | |
---|
709 | id = wx.NewId() |
---|
710 | button_Save = wx.Button(self, id, "Save") |
---|
711 | button_Save.SetToolTipString("Save the current P(r) work to file.") |
---|
712 | self.Bind(wx.EVT_BUTTON, self._save_state, id = id) |
---|
713 | |
---|
714 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
715 | sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
716 | sizer_button.Add(button_Save, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10) |
---|
717 | sizer_button.Add(button_Reset, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10) |
---|
718 | sizer_button.Add(button_OK, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10) |
---|
719 | #sizer_button.Add(button_Cancel, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
720 | iy_vb += 1 |
---|
721 | vbox.Add(sizer_button, (iy_vb,0), (1,1), wx.EXPAND|wx.BOTTOM|wx.TOP, 10) |
---|
722 | |
---|
723 | self.Bind(wx.EVT_TEXT_ENTER, self._on_invert) |
---|
724 | |
---|
725 | self.SetSizer(vbox) |
---|
726 | |
---|
727 | def _on_accept_alpha(self, evt): |
---|
728 | """ |
---|
729 | User has accepted the estimated alpha, |
---|
730 | set it as part of the input parameters |
---|
731 | """ |
---|
732 | try: |
---|
733 | alpha = self.alpha_estimate_ctl.GetLabel() |
---|
734 | tmp = float(alpha) |
---|
735 | self.alpha_ctl.SetValue(alpha) |
---|
736 | except: |
---|
737 | # No estimate or bad estimate, either do nothing |
---|
738 | import sys |
---|
739 | print "InversionControl._on_accept_alpha: %s" % sys.exc_value |
---|
740 | pass |
---|
741 | |
---|
742 | def _on_accept_nterms(self, evt): |
---|
743 | """ |
---|
744 | User has accepted the estimated number of terms, |
---|
745 | set it as part of the input parameters |
---|
746 | """ |
---|
747 | try: |
---|
748 | nterms = self.nterms_estimate_ctl.GetLabel() |
---|
749 | tmp = float(nterms) |
---|
750 | self.nfunc_ctl.SetValue(nterms) |
---|
751 | except: |
---|
752 | # No estimate or bad estimate, either do nothing |
---|
753 | import sys |
---|
754 | print "InversionControl._on_accept_nterms: %s" % sys.exc_value |
---|
755 | pass |
---|
756 | |
---|
757 | def _on_reset(self, evt): |
---|
758 | """ |
---|
759 | Resets inversion parameters |
---|
760 | """ |
---|
761 | self.nfunc = self.manager.DEFAULT_NFUNC |
---|
762 | self.d_max = self.manager.DEFAULT_DMAX |
---|
763 | self.alpha = self.manager.DEFAULT_ALPHA |
---|
764 | self.qmin_ctl.SetValue("") |
---|
765 | self.qmax_ctl.SetValue("") |
---|
766 | self.time_ctl.SetValue("") |
---|
767 | self.rg_ctl.SetValue("") |
---|
768 | self.iq0_ctl.SetValue("") |
---|
769 | self.bck_ctl.SetValue("") |
---|
770 | self.chi2_ctl.SetValue("") |
---|
771 | self.osc_ctl.SetValue("") |
---|
772 | self.pos_ctl.SetValue("") |
---|
773 | self.pos_err_ctl.SetValue("") |
---|
774 | self.alpha_estimate_ctl.Enable(False) |
---|
775 | self.alpha_estimate_ctl.SetLabel("") |
---|
776 | self.nterms_estimate_ctl.Enable(False) |
---|
777 | self.nterms_estimate_ctl.SetLabel("") |
---|
778 | self._on_pars_changed() |
---|
779 | |
---|
780 | def _on_pars_changed(self, evt=None): |
---|
781 | """ |
---|
782 | Called when an input parameter has changed |
---|
783 | We will estimate the alpha parameter behind the |
---|
784 | scenes. |
---|
785 | """ |
---|
786 | flag, alpha, dmax, nfunc, qmin, qmax, height, width = self._read_pars() |
---|
787 | has_bck = self.bck_chk.IsChecked() |
---|
788 | |
---|
789 | # If the pars are valid, estimate alpha |
---|
790 | if flag: |
---|
791 | self.nterms_estimate_ctl.Enable(False) |
---|
792 | self.alpha_estimate_ctl.Enable(False) |
---|
793 | |
---|
794 | dataset = self.plot_data.GetValue() |
---|
795 | self.manager.estimate_plot_inversion(alpha=alpha, nfunc=nfunc, |
---|
796 | d_max=dmax, |
---|
797 | q_min=qmin, q_max=qmax, |
---|
798 | bck=has_bck, |
---|
799 | height=height, |
---|
800 | width=width) |
---|
801 | |
---|
802 | def _read_pars(self, evt=None): |
---|
803 | alpha = 0 |
---|
804 | nfunc = 5 |
---|
805 | dmax = 120 |
---|
806 | qmin = 0 |
---|
807 | qmax = 0 |
---|
808 | height = 0 |
---|
809 | width = 0 |
---|
810 | |
---|
811 | flag = True |
---|
812 | |
---|
813 | |
---|
814 | # Read slit height |
---|
815 | try: |
---|
816 | height_str = self.sheight_ctl.GetValue() |
---|
817 | if len(height_str.lstrip().rstrip())==0: |
---|
818 | height = 0 |
---|
819 | else: |
---|
820 | height = float(height_str) |
---|
821 | self.sheight_ctl.SetBackgroundColour(wx.WHITE) |
---|
822 | self.sheight_ctl.Refresh() |
---|
823 | except: |
---|
824 | flag = False |
---|
825 | self.sheight_ctl.SetBackgroundColour("pink") |
---|
826 | self.sheight_ctl.Refresh() |
---|
827 | |
---|
828 | # Read slit width |
---|
829 | try: |
---|
830 | width_str = self.swidth_ctl.GetValue() |
---|
831 | if len(width_str.lstrip().rstrip())==0: |
---|
832 | width = 0 |
---|
833 | else: |
---|
834 | width = float(width_str) |
---|
835 | self.swidth_ctl.SetBackgroundColour(wx.WHITE) |
---|
836 | self.swidth_ctl.Refresh() |
---|
837 | except: |
---|
838 | flag = False |
---|
839 | self.swidth_ctl.SetBackgroundColour("pink") |
---|
840 | self.swidth_ctl.Refresh() |
---|
841 | |
---|
842 | # Read alpha |
---|
843 | try: |
---|
844 | alpha = float(self.alpha_ctl.GetValue()) |
---|
845 | self.alpha_ctl.SetBackgroundColour(wx.WHITE) |
---|
846 | self.alpha_ctl.Refresh() |
---|
847 | except: |
---|
848 | flag = False |
---|
849 | self.alpha_ctl.SetBackgroundColour("pink") |
---|
850 | self.alpha_ctl.Refresh() |
---|
851 | |
---|
852 | # Read d_max |
---|
853 | try: |
---|
854 | dmax = float(self.dmax_ctl.GetValue()) |
---|
855 | self.dmax_ctl.SetBackgroundColour(wx.WHITE) |
---|
856 | self.dmax_ctl.Refresh() |
---|
857 | except: |
---|
858 | flag = False |
---|
859 | self.dmax_ctl.SetBackgroundColour("pink") |
---|
860 | self.dmax_ctl.Refresh() |
---|
861 | |
---|
862 | # Read nfunc |
---|
863 | try: |
---|
864 | nfunc = int(self.nfunc_ctl.GetValue()) |
---|
865 | npts = self.manager.get_npts() |
---|
866 | if npts>0 and nfunc>npts: |
---|
867 | message = "Number of function terms should be smaller than the number of points" |
---|
868 | wx.PostEvent(self.manager.parent, StatusEvent(status=message)) |
---|
869 | raise ValueError, message |
---|
870 | self.nfunc_ctl.SetBackgroundColour(wx.WHITE) |
---|
871 | self.nfunc_ctl.Refresh() |
---|
872 | except: |
---|
873 | flag = False |
---|
874 | self.nfunc_ctl.SetBackgroundColour("pink") |
---|
875 | self.nfunc_ctl.Refresh() |
---|
876 | |
---|
877 | # Read qmin |
---|
878 | try: |
---|
879 | qmin_str = self.qmin_ctl.GetValue() |
---|
880 | if len(qmin_str.lstrip().rstrip())==0: |
---|
881 | qmin = None |
---|
882 | else: |
---|
883 | qmin = float(qmin_str) |
---|
884 | self.qmin_ctl.SetBackgroundColour(wx.WHITE) |
---|
885 | self.qmin_ctl.Refresh() |
---|
886 | except: |
---|
887 | flag = False |
---|
888 | self.qmin_ctl.SetBackgroundColour("pink") |
---|
889 | self.qmin_ctl.Refresh() |
---|
890 | |
---|
891 | # Read qmax |
---|
892 | try: |
---|
893 | qmax_str = self.qmax_ctl.GetValue() |
---|
894 | if len(qmax_str.lstrip().rstrip())==0: |
---|
895 | qmax = None |
---|
896 | else: |
---|
897 | qmax = float(qmax_str) |
---|
898 | self.qmax_ctl.SetBackgroundColour(wx.WHITE) |
---|
899 | self.qmax_ctl.Refresh() |
---|
900 | except: |
---|
901 | flag = False |
---|
902 | self.qmax_ctl.SetBackgroundColour("pink") |
---|
903 | self.qmax_ctl.Refresh() |
---|
904 | |
---|
905 | return flag, alpha, dmax, nfunc, qmin, qmax, height, width |
---|
906 | |
---|
907 | def _on_explore(self, evt): |
---|
908 | """ |
---|
909 | Invoke the d_max exploration dialog |
---|
910 | """ |
---|
911 | from explore_dialog import ExploreDialog |
---|
912 | if self.manager._last_pr is not None: |
---|
913 | pr = self.manager._create_plot_pr() |
---|
914 | dialog = ExploreDialog(pr, 10, None, -1, "") |
---|
915 | dialog.ShowModal() |
---|
916 | else: |
---|
917 | message = "No data to analyze. Please load a data set to proceed." |
---|
918 | wx.PostEvent(self.manager.parent, StatusEvent(status=message)) |
---|
919 | |
---|
920 | def _on_invert(self, evt): |
---|
921 | """ |
---|
922 | Perform inversion |
---|
923 | @param silent: when True, there will be no output for the user |
---|
924 | """ |
---|
925 | # Get the data from the form |
---|
926 | # Push it to the manager |
---|
927 | |
---|
928 | flag, alpha, dmax, nfunc, qmin, qmax, height, width = self._read_pars() |
---|
929 | has_bck = self.bck_chk.IsChecked() |
---|
930 | |
---|
931 | if flag: |
---|
932 | dataset = self.plot_data.GetValue() |
---|
933 | if len(dataset.strip())==0: |
---|
934 | message = "No data to invert. Select a data set before proceeding with P(r) inversion." |
---|
935 | wx.PostEvent(self.manager.parent, StatusEvent(status=message)) |
---|
936 | else: |
---|
937 | self.manager.setup_plot_inversion(alpha=alpha, nfunc=nfunc, |
---|
938 | d_max=dmax, |
---|
939 | q_min=qmin, q_max=qmax, |
---|
940 | bck=has_bck, |
---|
941 | height=height, |
---|
942 | width=width) |
---|
943 | else: |
---|
944 | message = "The P(r) form contains invalid values: please submit it again." |
---|
945 | wx.PostEvent(self.parent, StatusEvent(status=message)) |
---|
946 | |
---|
947 | def _change_file(self, evt=None, filepath=None): |
---|
948 | """ |
---|
949 | Choose a new input file for I(q) |
---|
950 | """ |
---|
951 | import os |
---|
952 | if not self.manager==None: |
---|
953 | path = self.manager.choose_file(path=filepath) |
---|
954 | |
---|
955 | if path and os.path.isfile(path): |
---|
956 | self.plot_data.SetValue(str(path)) |
---|
957 | self.manager.show_data(path, reset=True) |
---|
958 | self._on_pars_changed(None) |
---|
959 | |
---|
960 | # Perform inversion |
---|
961 | if self.standalone == True: |
---|
962 | self._on_invert(None) |
---|
963 | |
---|
964 | class HelpDialog(wx.Dialog): |
---|
965 | def __init__(self, parent, id): |
---|
966 | from sans.pr.invertor import help |
---|
967 | wx.Dialog.__init__(self, parent, id, size=(400, 420)) |
---|
968 | self.SetTitle("P(r) help") |
---|
969 | |
---|
970 | |
---|
971 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
972 | |
---|
973 | explanation = help() |
---|
974 | |
---|
975 | label_explain = wx.StaticText(self, -1, explanation, size=(350,320)) |
---|
976 | |
---|
977 | vbox.Add(label_explain, 0, wx.ALL|wx.EXPAND, 15) |
---|
978 | |
---|
979 | |
---|
980 | static_line = wx.StaticLine(self, -1) |
---|
981 | vbox.Add(static_line, 0, wx.EXPAND, 0) |
---|
982 | |
---|
983 | button_OK = wx.Button(self, wx.ID_OK, "OK") |
---|
984 | #button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
---|
985 | |
---|
986 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
987 | sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
988 | sizer_button.Add(button_OK, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
989 | #sizer_button.Add(button_Cancel, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
990 | vbox.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10) |
---|
991 | |
---|
992 | self.SetSizer(vbox) |
---|
993 | self.SetAutoLayout(True) |
---|
994 | |
---|
995 | self.Layout() |
---|
996 | self.Centre() |
---|
997 | |
---|
998 | class PrDistDialog(wx.Dialog): |
---|
999 | """ |
---|
1000 | Property dialog to let the user change the number |
---|
1001 | of points on the P(r) plot. |
---|
1002 | """ |
---|
1003 | def __init__(self, parent, id): |
---|
1004 | from sans.pr.invertor import help |
---|
1005 | wx.Dialog.__init__(self, parent, id, size=(250, 120)) |
---|
1006 | self.SetTitle("P(r) distribution") |
---|
1007 | |
---|
1008 | |
---|
1009 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
1010 | |
---|
1011 | label_npts = wx.StaticText(self, -1, "Number of points") |
---|
1012 | self.npts_ctl = wx.TextCtrl(self, -1, size=(100,20)) |
---|
1013 | |
---|
1014 | pars_sizer = wx.GridBagSizer(5,5) |
---|
1015 | iy = 0 |
---|
1016 | pars_sizer.Add(label_npts, (iy,0), (1,1), wx.LEFT, 15) |
---|
1017 | pars_sizer.Add(self.npts_ctl, (iy,1), (1,1), wx.RIGHT, 0) |
---|
1018 | |
---|
1019 | vbox.Add(pars_sizer, 0, wx.ALL|wx.EXPAND, 15) |
---|
1020 | |
---|
1021 | |
---|
1022 | static_line = wx.StaticLine(self, -1) |
---|
1023 | vbox.Add(static_line, 0, wx.EXPAND, 0) |
---|
1024 | |
---|
1025 | button_OK = wx.Button(self, wx.ID_OK, "OK") |
---|
1026 | self.Bind(wx.EVT_BUTTON, self._checkValues, button_OK) |
---|
1027 | button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel") |
---|
1028 | |
---|
1029 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
1030 | sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1031 | sizer_button.Add(button_OK, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
1032 | sizer_button.Add(button_Cancel, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10) |
---|
1033 | vbox.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10) |
---|
1034 | |
---|
1035 | self.SetSizer(vbox) |
---|
1036 | self.SetAutoLayout(True) |
---|
1037 | |
---|
1038 | self.Layout() |
---|
1039 | self.Centre() |
---|
1040 | |
---|
1041 | def _checkValues(self, event): |
---|
1042 | """ |
---|
1043 | Check the dialog content. |
---|
1044 | """ |
---|
1045 | flag = True |
---|
1046 | try: |
---|
1047 | int(self.npts_ctl.GetValue()) |
---|
1048 | self.npts_ctl.SetBackgroundColour(wx.WHITE) |
---|
1049 | self.npts_ctl.Refresh() |
---|
1050 | except: |
---|
1051 | flag = False |
---|
1052 | self.npts_ctl.SetBackgroundColour("pink") |
---|
1053 | self.npts_ctl.Refresh() |
---|
1054 | if flag: |
---|
1055 | event.Skip(True) |
---|
1056 | |
---|
1057 | def get_content(self): |
---|
1058 | """ |
---|
1059 | Return the content of the dialog. |
---|
1060 | At this point the values have already been |
---|
1061 | checked. |
---|
1062 | """ |
---|
1063 | value = int(self.npts_ctl.GetValue()) |
---|
1064 | return value |
---|
1065 | |
---|
1066 | def set_content(self, npts): |
---|
1067 | """ |
---|
1068 | Initialize the content of the dialog. |
---|
1069 | """ |
---|
1070 | self.npts_ctl.SetValue("%i" % npts) |
---|
1071 | |
---|
1072 | |
---|
1073 | class ParsDialog(wx.Panel): |
---|
1074 | """ |
---|
1075 | Dialog box to let the user edit detector settings |
---|
1076 | """ |
---|
1077 | |
---|
1078 | def __init__(self, parent, id = id, file=True, **kwargs): |
---|
1079 | |
---|
1080 | wx.Panel.__init__(self, parent, id = id, **kwargs) |
---|
1081 | self.file = file |
---|
1082 | |
---|
1083 | self.label_nfunc = wx.StaticText(self, -1, "Number of terms") |
---|
1084 | self.label_alpha = wx.StaticText(self, -1, "Regularization constant") |
---|
1085 | self.label_dmax = wx.StaticText(self, -1, "Max distance [A]") |
---|
1086 | |
---|
1087 | # Npts, q max |
---|
1088 | self.nfunc_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
1089 | self.alpha_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
1090 | self.dmax_ctl = wx.TextCtrl(self, -1, size=(60,20)) |
---|
1091 | |
---|
1092 | self.label_file = None |
---|
1093 | self.file_ctl = None |
---|
1094 | |
---|
1095 | self.static_line_3 = wx.StaticLine(self, -1) |
---|
1096 | |
---|
1097 | |
---|
1098 | |
---|
1099 | self.__do_layout() |
---|
1100 | |
---|
1101 | self.Fit() |
---|
1102 | |
---|
1103 | def _load_file(self, evt): |
---|
1104 | import os |
---|
1105 | path = None |
---|
1106 | dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.txt", wx.OPEN) |
---|
1107 | if dlg.ShowModal() == wx.ID_OK: |
---|
1108 | path = dlg.GetPath() |
---|
1109 | mypath = os.path.basename(path) |
---|
1110 | dlg.Destroy() |
---|
1111 | |
---|
1112 | if path and os.path.isfile(path): |
---|
1113 | self.file_ctl.SetValue(str(path)) |
---|
1114 | |
---|
1115 | |
---|
1116 | def checkValues(self, event): |
---|
1117 | flag = True |
---|
1118 | try: |
---|
1119 | float(self.alpha_ctl.GetValue()) |
---|
1120 | self.alpha_ctl.SetBackgroundColour(wx.WHITE) |
---|
1121 | self.alpha_ctl.Refresh() |
---|
1122 | except: |
---|
1123 | flag = False |
---|
1124 | self.alpha_ctl.SetBackgroundColour("pink") |
---|
1125 | self.alpha_ctl.Refresh() |
---|
1126 | |
---|
1127 | try: |
---|
1128 | float(self.dmax_ctl.GetValue()) |
---|
1129 | self.dmax_ctl.SetBackgroundColour(wx.WHITE) |
---|
1130 | self.dmax_ctl.Refresh() |
---|
1131 | except: |
---|
1132 | flag = False |
---|
1133 | self.dmax_ctl.SetBackgroundColour("pink") |
---|
1134 | self.dmax_ctl.Refresh() |
---|
1135 | |
---|
1136 | try: |
---|
1137 | int(self.nfunc_ctl.GetValue()) |
---|
1138 | self.nfunc_ctl.SetBackgroundColour(wx.WHITE) |
---|
1139 | self.nfunc_ctl.Refresh() |
---|
1140 | except: |
---|
1141 | flag = False |
---|
1142 | self.nfunc_ctl.SetBackgroundColour("pink") |
---|
1143 | self.nfunc_ctl.Refresh() |
---|
1144 | |
---|
1145 | if flag: |
---|
1146 | event.Skip(True) |
---|
1147 | |
---|
1148 | def setContent(self, nfunc, alpha, dmax, file): |
---|
1149 | self.nfunc_ctl.SetValue(str(nfunc)) |
---|
1150 | self.alpha_ctl.SetValue(str(alpha)) |
---|
1151 | self.dmax_ctl.SetValue(str(dmax)) |
---|
1152 | if self.file: |
---|
1153 | self.file_ctl.SetValue(str(file)) |
---|
1154 | |
---|
1155 | def getContent(self): |
---|
1156 | nfunc = int(self.nfunc_ctl.GetValue()) |
---|
1157 | alpha = float(self.alpha_ctl.GetValue()) |
---|
1158 | dmax = float(self.dmax_ctl.GetValue()) |
---|
1159 | file = None |
---|
1160 | if self.file: |
---|
1161 | file = self.file_ctl.GetValue() |
---|
1162 | return nfunc, alpha, dmax, file |
---|
1163 | |
---|
1164 | |
---|
1165 | def __do_layout(self): |
---|
1166 | sizer_main = wx.BoxSizer(wx.VERTICAL) |
---|
1167 | sizer_params = wx.GridBagSizer(5,5) |
---|
1168 | |
---|
1169 | iy = 0 |
---|
1170 | sizer_params.Add(self.label_nfunc, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1171 | sizer_params.Add(self.nfunc_ctl, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1172 | iy += 1 |
---|
1173 | sizer_params.Add(self.label_alpha, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1174 | sizer_params.Add(self.alpha_ctl, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1175 | iy += 1 |
---|
1176 | sizer_params.Add(self.label_dmax, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1177 | sizer_params.Add(self.dmax_ctl, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1178 | iy += 1 |
---|
1179 | if self.file: |
---|
1180 | self.label_file = wx.StaticText(self, -1, "Input file") |
---|
1181 | self.file_ctl = wx.TextCtrl(self, -1, size=(120,20)) |
---|
1182 | sizer_params.Add(self.label_file, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
1183 | sizer_params.Add(self.file_ctl, (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
1184 | |
---|
1185 | sizer_main.Add(sizer_params, 0, wx.EXPAND|wx.ALL, 10) |
---|
1186 | |
---|
1187 | |
---|
1188 | if self.file: |
---|
1189 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
1190 | self.button_load = wx.Button(self, 1, "Choose file") |
---|
1191 | self.Bind(wx.EVT_BUTTON, self._load_file, id = 1) |
---|
1192 | sizer_button.Add(self.button_load, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10) |
---|
1193 | |
---|
1194 | |
---|
1195 | sizer_main.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10) |
---|
1196 | self.SetAutoLayout(True) |
---|
1197 | self.SetSizer(sizer_main) |
---|
1198 | self.Layout() |
---|
1199 | self.Centre() |
---|
1200 | # end wxGlade |
---|
1201 | |
---|
1202 | |
---|
1203 | # end of class DialogAbout |
---|
1204 | |
---|
1205 | ##### testing code ############################################################ |
---|
1206 | class TestPlot: |
---|
1207 | def __init__(self, text): |
---|
1208 | self.name = text |
---|
1209 | |
---|
1210 | class MyApp(wx.App): |
---|
1211 | def OnInit(self): |
---|
1212 | wx.InitAllImageHandlers() |
---|
1213 | dialog = PrDistDialog(None, -1) |
---|
1214 | if dialog.ShowModal() == wx.ID_OK: |
---|
1215 | pass |
---|
1216 | dialog.Destroy() |
---|
1217 | |
---|
1218 | return 1 |
---|
1219 | |
---|
1220 | # end of class MyApp |
---|
1221 | |
---|
1222 | if __name__ == "__main__": |
---|
1223 | app = MyApp(0) |
---|
1224 | app.MainLoop() |
---|
1225 | |
---|
1226 | ##### end of testing code ##################################################### |
---|