source: sasview/prview/perspectives/pr/inversion_panel.py @ 660b1e6

ESS_GUIESS_GUI_DocsESS_GUI_batch_fittingESS_GUI_bumps_abstractionESS_GUI_iss1116ESS_GUI_iss879ESS_GUI_iss959ESS_GUI_openclESS_GUI_orderingESS_GUI_sync_sascalccostrafo411magnetic_scattrelease-4.1.1release-4.1.2release-4.2.2release_4.0.1ticket-1009ticket-1094-headlessticket-1242-2d-resolutionticket-1243ticket-1249ticket885unittest-saveload
Last change on this file since 660b1e6 was 660b1e6, checked in by Mathieu Doucet <doucetm@…>, 16 years ago

Loaded data is now shown before hitting the inversion button.

  • Property mode set to 100644
File size: 28.0 KB
Line 
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
7import wx
8from sans.guicomm.events import StatusEvent   
9
10class 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
69class 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
101class 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]
107   
108    ## Oscillation parameters (sin function = 1.1)
109    oscillation_max = 1.5
110   
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   
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
134        self.label_sugg = None
135        self.qmin_ctl = None
136        self.qmax_ctl = None
137       
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       
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)
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)
168        elif name=='elapsed':
169            self.time_ctl.SetValue("%-5.2g" % value)
170        elif name=='oscillation':
171            self.osc_ctl.SetValue("%-5.2g" % value)
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)
176        elif name=='alpha_estimate':
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()
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()
201        elif name=='q_min':
202            self.qmin_ctl.GetValue()
203        elif name=='q_max':
204            self.qmax_ctl.GetValue()
205        elif name=='elapsed':
206            self.time_ctl.GetValue()
207        elif name=='oscillation':
208            self.osc_ctl.GetValue()
209        elif name=='pos':
210            self.pos_ctl.GetValue()
211        elif name=='pos_err':
212            self.pos_err_ctl.GetValue()
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       
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 " 
268        explanation += "term. The suggested value is the value above which the "
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       
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]")
279        self.label_sugg  = wx.StaticText(self, -1, "Suggested value")
280        #self.label_sugg.Hide()
281       
282        self.nfunc_ctl = wx.TextCtrl(self, -1, size=(60,20))
283        self.nfunc_ctl.SetToolTipString("Number of terms in the expansion.")
284        self.alpha_ctl = wx.TextCtrl(self, -1, size=(60,20))
285        self.alpha_ctl.SetToolTipString("Control parameter for the size of the regularization term.")
286        self.dmax_ctl  = wx.TextCtrl(self, -1, size=(60,20))
287        self.dmax_ctl.SetToolTipString("Maximum distance between any two points in the system.")
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)   
292        self.alpha_estimate_ctl.Enable(False)
293        #self.alpha_estimate_ctl.SetBackgroundColour('#ffdf85')
294        #self.alpha_estimate_ctl.SetBackgroundColour(self.GetBackgroundColour())
295        self.alpha_estimate_ctl.SetToolTipString("Waiting for estimate...")
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)
299        self.alpha_ctl.Bind(wx.EVT_KILL_FOCUS, self._read_pars)
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
306        sizer_params.Add(self.label_sugg,       (iy,2), (1,1), wx.LEFT, 15)
307        iy += 1
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)
310        iy += 1
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)
314        iy += 1
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)
317
318        boxsizer2.Add(sizer_params, 0)
319       
320        vbox.Add(boxsizer2)
321
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
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")
354        label_pos = wx.StaticText(self, -1, "Positive fraction")
355        label_pos_err = wx.StaticText(self, -1, "1-sigma positive fraction")
356       
357        self.time_ctl = wx.TextCtrl(self, -1, size=(60,20))
358        self.time_ctl.SetEditable(False)
359        self.time_ctl.SetToolTipString("Computation time for the last inversion, in seconds.")
360       
361        self.chi2_ctl = wx.TextCtrl(self, -1, size=(60,20))
362        self.chi2_ctl.SetEditable(False)
363        self.chi2_ctl.SetToolTipString("Chi^2 over degrees of freedom.")
364       
365        # Oscillation parameter
366        self.osc_ctl = wx.TextCtrl(self, -1, size=(60,20))
367        self.osc_ctl.SetEditable(False)
368        self.osc_ctl.SetToolTipString("Oscillation parameter. P(r) for a sphere has an oscillation parameter of 1.1.")
369       
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       
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
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
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")
414        button_OK.SetToolTipString("Perform P(r) inversion.")
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       
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       
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        """
451        flag, alpha, dmax, nfunc, qmin, qmax = self._read_pars()
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, 
458                                                     d_max=dmax,
459                                                     q_min=qmin, q_max=qmax)
460            else:
461                path = self.data_file.GetValue()
462                self.manager.estimate_file_inversion(alpha=alpha, nfunc=nfunc, 
463                                                     d_max=dmax, path=path,
464                                                     q_min=qmin, q_max=qmax)
465       
466       
467    def _read_pars(self, evt=None):   
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       
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
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       
542        flag, alpha, dmax, nfunc, qmin, qmax = self._read_pars()
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, 
548                                                  d_max=dmax,
549                                                  q_min=qmin, q_max=qmax)
550            else:
551                path = self.data_file.GetValue()
552                self.manager.setup_file_inversion(alpha=alpha, nfunc=nfunc, 
553                                                  d_max=dmax, path=path,
554                                                  q_min=qmin, q_max=qmax
555                                                  )
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                self.manager.show_data(path)
574       
575
576
577
578class HelpDialog(wx.Dialog):
579    def __init__(self, parent, id):
580        from sans.pr.invertor import help
581        wx.Dialog.__init__(self, parent, id, size=(400, 420))
582        self.SetTitle("P(r) help") 
583       
584
585        vbox = wx.BoxSizer(wx.VERTICAL)
586
587        explanation = help()
588           
589        label_explain = wx.StaticText(self, -1, explanation, size=(350,320))
590           
591        vbox.Add(label_explain, 0, wx.ALL|wx.EXPAND, 15)
592
593
594        static_line = wx.StaticLine(self, -1)
595        vbox.Add(static_line, 0, wx.EXPAND, 0)
596       
597        button_OK = wx.Button(self, wx.ID_OK, "OK")
598        #button_Cancel = wx.Button(self, wx.ID_CANCEL, "Cancel")
599       
600        sizer_button = wx.BoxSizer(wx.HORIZONTAL)
601        sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0)
602        sizer_button.Add(button_OK, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10)
603        #sizer_button.Add(button_Cancel, 0, wx.LEFT|wx.RIGHT|wx.ADJUST_MINSIZE, 10)       
604        vbox.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
605
606        self.SetSizer(vbox)
607        self.SetAutoLayout(True)
608       
609        self.Layout()
610        self.Centre()
611
612
613class ParsDialog(wx.Panel):
614    """
615        Dialog box to let the user edit detector settings
616    """
617   
618    def __init__(self, parent, id = id, file=True, **kwargs):
619
620        wx.Panel.__init__(self, parent, id = id, **kwargs)
621        self.file = file
622       
623        self.label_nfunc = wx.StaticText(self, -1, "Number of terms")
624        self.label_alpha = wx.StaticText(self, -1, "Regularization constant")
625        self.label_dmax  = wx.StaticText(self, -1, "Max distance [A]")
626       
627        # Npts, q max
628        self.nfunc_ctl = wx.TextCtrl(self, -1, size=(60,20))
629        self.alpha_ctl = wx.TextCtrl(self, -1, size=(60,20))
630        self.dmax_ctl  = wx.TextCtrl(self, -1, size=(60,20))
631
632        self.label_file = None
633        self.file_ctl   = None
634
635        self.static_line_3 = wx.StaticLine(self, -1)
636       
637       
638
639        self.__do_layout()
640
641        self.Fit()
642       
643    def _load_file(self, evt):
644        import os
645        path = None
646        dlg = wx.FileDialog(self, "Choose a file", os.getcwd(), "", "*.txt", wx.OPEN)
647        if dlg.ShowModal() == wx.ID_OK:
648            path = dlg.GetPath()
649            mypath = os.path.basename(path)
650        dlg.Destroy()
651       
652        if path and os.path.isfile(path):
653            self.file_ctl.SetValue(str(path))
654
655       
656    def checkValues(self, event):
657        flag = True
658        try:
659            float(self.alpha_ctl.GetValue())
660            self.alpha_ctl.SetBackgroundColour(wx.WHITE)
661            self.alpha_ctl.Refresh()
662        except:
663            flag = False
664            self.alpha_ctl.SetBackgroundColour("pink")
665            self.alpha_ctl.Refresh()
666           
667        try:
668            float(self.dmax_ctl.GetValue())
669            self.dmax_ctl.SetBackgroundColour(wx.WHITE)
670            self.dmax_ctl.Refresh()
671        except:
672            flag = False
673            self.dmax_ctl.SetBackgroundColour("pink")
674            self.dmax_ctl.Refresh()
675           
676        try:
677            int(self.nfunc_ctl.GetValue())
678            self.nfunc_ctl.SetBackgroundColour(wx.WHITE)
679            self.nfunc_ctl.Refresh()
680        except:
681            flag = False
682            self.nfunc_ctl.SetBackgroundColour("pink")
683            self.nfunc_ctl.Refresh()
684       
685        if flag:
686            event.Skip(True)
687   
688    def setContent(self, nfunc, alpha, dmax, file):
689        self.nfunc_ctl.SetValue(str(nfunc))
690        self.alpha_ctl.SetValue(str(alpha))
691        self.dmax_ctl.SetValue(str(dmax))
692        if self.file:
693            self.file_ctl.SetValue(str(file))
694
695    def getContent(self):
696        nfunc = int(self.nfunc_ctl.GetValue())
697        alpha = float(self.alpha_ctl.GetValue())
698        dmax = float(self.dmax_ctl.GetValue())
699        file = None
700        if self.file:
701            file = self.file_ctl.GetValue()
702        return nfunc, alpha, dmax, file
703
704
705    def __do_layout(self):
706        sizer_main = wx.BoxSizer(wx.VERTICAL)
707        sizer_params = wx.GridBagSizer(5,5)
708
709        iy = 0
710        sizer_params.Add(self.label_nfunc, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
711        sizer_params.Add(self.nfunc_ctl,   (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
712        iy += 1
713        sizer_params.Add(self.label_alpha, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
714        sizer_params.Add(self.alpha_ctl,   (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
715        iy += 1
716        sizer_params.Add(self.label_dmax, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
717        sizer_params.Add(self.dmax_ctl,   (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
718        iy += 1
719        if self.file:
720            self.label_file  = wx.StaticText(self, -1, "Input file")
721            self.file_ctl  = wx.TextCtrl(self, -1, size=(120,20))
722            sizer_params.Add(self.label_file, (iy,0), (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
723            sizer_params.Add(self.file_ctl,   (iy,1), (1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0)
724
725        sizer_main.Add(sizer_params, 0, wx.EXPAND|wx.ALL, 10)
726       
727       
728        if self.file:
729            sizer_button = wx.BoxSizer(wx.HORIZONTAL)
730            self.button_load = wx.Button(self, 1, "Choose file")
731            self.Bind(wx.EVT_BUTTON, self._load_file, id = 1)       
732            sizer_button.Add(self.button_load, 0, wx.LEFT|wx.ADJUST_MINSIZE, 10)
733       
734       
735            sizer_main.Add(sizer_button, 0, wx.EXPAND|wx.BOTTOM|wx.TOP, 10)
736        self.SetAutoLayout(True)
737        self.SetSizer(sizer_main)
738        self.Layout()
739        self.Centre()
740        # end wxGlade
741
742
743# end of class DialogAbout
744
745##### testing code ############################################################
746class TestPlot:
747    def __init__(self, text):
748        self.name = text
749   
750class MyApp(wx.App):
751    def OnInit(self):
752        wx.InitAllImageHandlers()
753        dialog = HelpDialog(None, -1)
754        if dialog.ShowModal() == wx.ID_OK:
755            pass
756        dialog.Destroy()
757       
758        return 1
759
760# end of class MyApp
761
762if __name__ == "__main__":
763    app = MyApp(0)
764    app.MainLoop()
765   
766##### end of testing code #####################################################   
Note: See TracBrowser for help on using the repository browser.