source: sasview/invariantview/perspectives/invariant/invariant_panel.py @ 4e74e13

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 4e74e13 was 4e74e13, checked in by Gervaise Alina <gervyh@…>, 14 years ago

send the proper msg to the status bar

  • Property mode set to 100644
File size: 44.3 KB
Line 
1"""
2    This module provide GUI for the neutron scattering length density calculator
3    @author: Gervaise B. Alina
4"""
5
6import wx
7import sys
8
9from sans.invariant import invariant
10from sans.guiframe.utils import format_number, check_float
11from sans.guicomm.events import NewPlotEvent, StatusEvent
12from invariant_details import InvariantDetailsPanel, InvariantContainer
13from invariant_widgets import OutputTextCtrl, InvTextCtrl
14# The minimum q-value to be used when extrapolating
15Q_MINIMUM  = 1e-5
16# The maximum q-value to be used when extrapolating
17Q_MAXIMUM  = 10
18# the maximum value to plot the theory data
19Q_MAXIMUM_PLOT = 2
20# the number of points to consider during fit
21NPTS = 10
22#Default value for background
23BACKGROUND = 0.0
24#default value for the scale
25SCALE = 1.0
26#default value of the contrast
27CONTRAST = 1.0
28#default value of the power used for power law
29POWER = 4.0
30#Invariant panel size
31_BOX_WIDTH = 76
32
33if sys.platform.count("win32")>0:
34    _STATICBOX_WIDTH = 450
35    PANEL_WIDTH = 500
36    PANEL_HEIGHT = 700
37    FONT_VARIANT = 0
38else:
39    _STATICBOX_WIDTH = 480
40    PANEL_WIDTH = 530
41    PANEL_HEIGHT = 700
42    FONT_VARIANT = 1
43
44
45class InvariantPanel(wx.ScrolledWindow):
46    """
47        Provides the Invariant GUI.
48    """
49    ## Internal nickname for the window, used by the AUI manager
50    window_name = "Invariant"
51    ## Name to appear on the window title bar
52    window_caption = "Invariant"
53    ## Flag to tell the AUI manager to put this panel in the center pane
54    CENTER_PANE = True
55    def __init__(self, parent, data=None, manager=None):
56        wx.ScrolledWindow.__init__(self, parent, style= wx.FULL_REPAINT_ON_RESIZE )
57        #Font size
58        self.SetWindowVariant(variant=FONT_VARIANT)
59        #Object that receive status event
60        self.parent = parent
61        #plug-in using this panel
62        self._manager = manager
63        #Data uses for computation
64        self._data = data
65        #container of invariant value
66        self.inv_container = None
67        #Draw the panel
68        self._do_layout()
69        self.reset_panel()
70        if self.parent is not None:
71            msg = ""
72            wx.PostEvent(self.parent, StatusEvent(status= msg))
73       
74    def err_check_on_data(self):
75        """
76            Check if data is valid for further computation
77        """
78        flag = False
79        #edit the panel
80        if self._data is not None:
81            if len(self._data.x[self._data.x==0]) > 0:
82                flag = True
83                msg = "Invariant: one of your q-values is zero. Delete that entry before proceeding"
84                wx.PostEvent(self.parent, StatusEvent(status= msg, type="stop")) 
85        return flag
86   
87    def set_data(self, data):
88        """
89            Set the data
90        """
91        self._data = data
92        #edit the panel
93        if self._data is not None:
94            self.err_check_on_data()
95            data_name = self._data.name
96            data_qmin = min (self._data.x)
97            data_qmax = max (self._data.x)
98            self.data_name_tcl.SetValue(str(data_name))
99            self.data_min_tcl.SetLabel(str(data_qmin))
100            self.data_max_tcl.SetLabel(str(data_qmax))
101            self.hint_msg_txt.SetLabel('')
102            self.reset_panel()
103            self.compute_invariant(event=None)
104           
105    def set_message(self):
106        """
107            Display warning message if available
108        """
109        if self.inv_container is not None:
110            if self.inv_container.existing_warning:
111                msg = "Warning! Computations on invariant require your "
112                msg += "attention.\n Please click on Details button."
113                self.hint_msg_txt.SetForegroundColour("red")
114            else:
115                msg = "For more information, click on Details button."
116                self.hint_msg_txt.SetForegroundColour("black")
117            self.hint_msg_txt.SetLabel(msg)
118        self.data_name_boxsizer.Layout()
119       
120    def set_manager(self, manager):
121        """
122            set value for the manager
123        """
124        self._manager = manager
125   
126    def get_background(self):
127        """
128            @return the background textcrtl value as a float
129        """
130        background = self.background_tcl.GetValue().lstrip().rstrip()
131        if background == "":
132            raise ValueError, "Need a background"
133        if check_float(self.background_tcl):
134            return float(background)
135        else:
136            raise ValueError, "Receive invalid value for background : %s"%(background)
137   
138    def get_scale(self):
139        """
140            @return the scale textcrtl value as a float
141        """
142        scale = self.scale_tcl.GetValue().lstrip().rstrip()
143        if scale == "":
144            raise ValueError, "Need a background"
145        if check_float(self.scale_tcl):
146            return float(scale)
147        else:
148            raise ValueError, "Receive invalid value for background : %s"%(scale)
149       
150    def get_contrast(self):
151        """
152            @return the contrast textcrtl value as a float
153        """
154        par_str = self.contrast_tcl.GetValue().strip()
155        contrast = None
156        if par_str !="" and check_float(self.contrast_tcl):
157            contrast = float(par_str)
158        return contrast
159   
160    def get_extrapolation_type(self, low_q, high_q):
161        """
162        """
163        extrapolation = None
164        if low_q  and not high_q:
165            extrapolation = "low"
166        elif not low_q  and high_q:
167            extrapolation = "high"
168        elif low_q and high_q:
169            extrapolation = "both"
170        return extrapolation
171           
172    def get_porod_const(self):
173        """
174            @return the porod constant textcrtl value as a float
175        """
176        par_str = self.porod_constant_tcl.GetValue().strip()
177        porod_const = None
178        if par_str !="" and check_float(self.porod_constant_tcl):
179            porod_const = float(par_str)
180        return porod_const
181   
182    def get_volume(self, inv, contrast, extrapolation):
183        """
184        """
185        if contrast is not None:
186            try:
187                v, dv = inv.get_volume_fraction_with_error(contrast=contrast, 
188                                                           extrapolation=extrapolation)
189                self.volume_tcl.SetValue(format_number(v))
190                self.volume_err_tcl.SetValue(format_number(dv))
191            except:
192                msg= "Error occurred computing volume fraction: %s"%sys.exc_value
193                wx.PostEvent(self.parent, StatusEvent(status= msg, type="stop"))
194               
195    def get_surface(self, inv, contrast, porod_const, extrapolation):
196        """
197        """
198        if contrast is not None and porod_const is not None:
199            try:
200                s, ds = inv.get_surface_with_error(contrast=contrast,
201                                        porod_const=porod_const,
202                                        extrapolation=extrapolation)
203                self.surface_tcl.SetValue(format_number(s))
204                self.surface_err_tcl.SetValue(format_number(ds))
205            except:
206                msg = "Error occurred computing specific surface: %s"%sys.exc_value
207                wx.PostEvent(self.parent, StatusEvent(status= msg, type="stop"))
208               
209    def get_total_qstar(self, inv, extrapolation):
210        """
211        """
212        try:
213            qstar_total, qstar_total_err = inv.get_qstar_with_error(extrapolation)
214            self.invariant_total_tcl.SetValue(format_number(qstar_total))
215            self.invariant_total_err_tcl.SetValue(format_number(qstar_total_err))
216            self.inv_container.qstar_total = qstar_total
217            self.inv_container.qstar_total_err = qstar_total_err
218         
219        except:
220            msg= "Error occurred computing invariant using extrapolation: %s"%sys.exc_value
221            wx.PostEvent(self.parent, StatusEvent(status= msg, type="stop")) 
222           
223    def get_low_qstar(self, inv, npts_low, low_q=False):
224        """
225        """
226        if low_q:
227            try: 
228                qstar_low, qstar_low_err = inv.get_qstar_low()
229                self.inv_container.qstar_low = qstar_low
230                self.inv_container.qstar_low_err = qstar_low_err
231                extrapolated_data = inv.get_extra_data_low(npts_in=npts_low) 
232                power_low = inv.get_extrapolation_power(range='low') 
233                if self.power_law_low.GetValue():
234                    self.power_low_tcl.SetValue(format_number(power_low))
235                self._manager.plot_theory(data=extrapolated_data,
236                                           name="Low-Q extrapolation")
237            except:
238                msg= "Error occurred computing low-Q invariant: %s"%sys.exc_value
239                wx.PostEvent(self.parent, StatusEvent(status= msg, type="stop"))
240        else:
241            self._manager.plot_theory(name="Low-Q extrapolation")
242           
243    def get_high_qstar(self, inv, high_q=False):
244        """
245        """
246        if high_q:
247            try: 
248                qstar_high, qstar_high_err = inv.get_qstar_high()
249                self.inv_container.qstar_high = qstar_high
250                self.inv_container.qstar_high_err = qstar_high_err
251                power_high = inv.get_extrapolation_power(range='high') 
252                self.power_high_tcl.SetValue(format_number(power_high))
253                high_out_data = inv.get_extra_data_high(q_end=Q_MAXIMUM_PLOT)
254                self._manager.plot_theory(data=high_out_data,
255                                           name="High-Q extrapolation")
256            except:
257                msg= "Error occurred computing high-Q invariant: %s"%sys.exc_value
258                wx.PostEvent(self.parent, StatusEvent(status= msg, type="stop"))
259        else:
260            self._manager.plot_theory(name="High-Q extrapolation")
261           
262    def get_qstar(self, inv):
263        """
264        """
265        qstar, qstar_err = inv.get_qstar_with_error()
266        self.inv_container.qstar = qstar
267        self.inv_container.qstar_err = qstar_err
268             
269    def set_extrapolation_low(self, inv, low_q=False):
270        """
271            @return float value necessary to compute invariant a low q
272        """
273        #get funtion
274        if self.guinier.GetValue():
275            function_low = "guinier"
276        # get the function
277        power_low = None #2.0/3.0
278        if self.power_law_low.GetValue():
279            function_low = "power_law"
280            if self.fit_enable_low.GetValue():
281                #set value of power_low to none to allow fitting
282                power_low = None
283            else:
284                power_low = self.power_low_tcl.GetValue().lstrip().rstrip()
285                if check_float(self.power_low_tcl):
286                    power_low = float(power_low)
287                else:
288                    if low_q :
289                        #Raise error only when qstar at low q is requested
290                        msg = "Expect float for power at low q , got %s"%(power_low)
291                        raise ValueError, msg
292       
293        #Get the number of points to extrapolated
294        npts_low = self.npts_low_tcl.GetValue().lstrip().rstrip()   
295        if check_float(self.npts_low_tcl):
296            npts_low = float(npts_low)
297        else:
298            if low_q:
299                msg = "Expect float for number of points at low q , got %s"%(npts_low)
300                raise ValueError, msg
301        #Set the invariant calculator
302        inv.set_extrapolation(range="low", npts=npts_low,
303                                   function=function_low, power=power_low)   
304        return inv, npts_low 
305       
306    def set_extrapolation_high(self, inv, high_q=False):
307        """
308            @return float value necessary to compute invariant a high q
309        """
310        power_high = None
311        #if self.power_law_high.GetValue():
312        function_high = "power_law"
313        if self.fit_enable_high.GetValue():
314            #set value of power_high to none to allow fitting
315            power_high = None
316        else:
317            power_high = self.power_high_tcl.GetValue().lstrip().rstrip()
318            if check_float(self.power_high_tcl):
319                power_high = float(power_high)
320            else:
321                if high_q :
322                    #Raise error only when qstar at high q is requested
323                    msg = "Expect float for power at high q , got %s"%(power_high)
324                    raise ValueError, msg
325                         
326        npts_high = self.npts_high_tcl.GetValue().lstrip().rstrip()   
327        if check_float(self.npts_high_tcl):
328            npts_high = float(npts_high)
329        else:
330            if high_q:
331                msg = "Expect float for number of points at high q , got %s"%(npts_high)
332                raise ValueError, msg
333        inv.set_extrapolation(range="high", npts=npts_high,
334                                   function=function_high, power=power_high)
335        return inv, npts_high
336   
337    def display_details(self, event):
338        """
339            open another panel for more details on invariant calculation
340        """
341        panel = InvariantDetailsPanel(parent=self, 
342                                           qstar_container=self.inv_container)
343        panel.ShowModal()
344        panel.Destroy()
345        self.button_calculate.SetFocus()
346       
347    def compute_invariant(self, event=None):
348        """
349            compute invariant
350        """
351        msg= ""
352        wx.PostEvent(self.parent, StatusEvent(status= msg))
353        if self._data is None or self.err_check_on_data():
354            return
355   
356        #clear outputs textctrl
357        self._reset_output()
358        try:
359            background = self.get_background()
360            scale = self.get_scale()
361        except:
362            msg= "Invariant Error: %s"%(sys.exc_value)
363            wx.PostEvent(self.parent, StatusEvent(status= msg, type="stop"))
364            return
365       
366        low_q = self.enable_low_cbox.GetValue()
367        high_q = self.enable_high_cbox.GetValue() 
368        #set invariant calculator
369        inv = invariant.InvariantCalculator(data=self._data,
370                                            background=background,
371                                            scale=scale)
372        try:
373            inv, npts_low = self.set_extrapolation_low(inv=inv, low_q=low_q)
374            inv, npts_high = self.set_extrapolation_high(inv=inv, high_q=high_q)
375        except:
376            raise
377            #msg= "Error occurred computing invariant: %s"%sys.exc_value
378            #wx.PostEvent(self.parent, StatusEvent(status= msg, type="stop"))
379            return
380        #check the type of extrapolation
381        extrapolation = self.get_extrapolation_type(low_q=low_q, high_q=high_q)
382       
383        #Compute invariant
384        try:
385            self.get_qstar(inv=inv)
386        except:
387            msg= "Error occurred computing invariant: %s"%sys.exc_value
388            wx.PostEvent(self.parent, StatusEvent(status= msg, type="stop"))
389            return
390        #Compute qstar extrapolated to low q range
391        self.get_low_qstar(inv=inv, npts_low=npts_low, low_q=low_q)
392        #Compute qstar extrapolated to high q range
393        self.get_high_qstar(inv=inv, high_q=high_q)
394        #Compute qstar extrapolated to total q range and set value to txtcrtl
395        self.get_total_qstar(inv=inv, extrapolation=extrapolation)
396        # Parse additional parameters
397        porod_const = self.get_porod_const()       
398        contrast = self.get_contrast()
399        try:
400            #Compute volume and set value to txtcrtl
401            self.get_volume(inv=inv, contrast=contrast, extrapolation=extrapolation)
402            #compute surface and set value to txtcrtl
403        except:
404            msg = "Error occurred computing invariant: %s"%sys.exc_value
405            wx.PostEvent(self.parent, StatusEvent(status=msg))
406        try:
407            self.get_surface(inv=inv, contrast=contrast, porod_const=porod_const, 
408                                    extrapolation=extrapolation)
409        except:
410            msg = "Error occurred computing invariant: %s"%sys.exc_value
411            wx.PostEvent(self.parent, StatusEvent(status= msg))
412           
413        #compute percentage of each invariant
414        self.inv_container.compute_percentage()
415        #display a message
416        self.set_message()
417        #enable the button_ok for more details
418        self.button_details.Enable()
419        self.button_details.SetFocus()
420   
421    def reset_panel(self):
422        """
423            set the panel at its initial state.
424        """
425        self.background_tcl.SetValue(str(BACKGROUND))
426        self.scale_tcl.SetValue(str(SCALE)) 
427        self.contrast_tcl.SetValue(str(CONTRAST))
428        self.porod_constant_tcl.SetValue('') 
429        self.npts_low_tcl.SetValue(str(NPTS))
430        self.enable_low_cbox.SetValue(False)
431        self.fix_enable_low.SetValue(True)
432        self.power_low_tcl.SetValue(str(POWER))
433        self.guinier.SetValue(True)
434        self.power_low_tcl.Disable()
435        self.enable_high_cbox.SetValue(False)
436        self.fix_enable_high.SetValue(True)
437        self.power_high_tcl.SetValue(str(POWER))
438        self.npts_high_tcl.SetValue(str(NPTS))
439        self.button_details.Disable()
440        #Change the state of txtcrtl to enable/disable
441        self._enable_low_q_section()
442        #Change the state of txtcrtl to enable/disable
443        self._enable_high_q_section()
444        self._reset_output()
445        self.button_calculate.SetFocus()
446       
447    def _reset_output(self):
448        """
449            clear outputs textcrtl
450        """
451        self.invariant_total_tcl.Clear()
452        self.invariant_total_err_tcl.Clear()
453        self.volume_tcl.Clear()
454        self.volume_err_tcl.Clear()
455        self.surface_tcl.Clear()
456        self.surface_err_tcl.Clear()
457        #prepare a new container to put result of invariant
458        self.inv_container = InvariantContainer()
459       
460    def _define_structure(self):
461        """
462            Define main sizers needed for this panel
463        """
464        ## Box sizers must be defined first before defining buttons/textctrls (MAC).
465        self.main_sizer = wx.BoxSizer(wx.VERTICAL)
466        #Sizer related to outputs
467        outputs_box = wx.StaticBox(self, -1, "Outputs")
468        self.outputs_sizer = wx.StaticBoxSizer(outputs_box, wx.VERTICAL)
469        self.outputs_sizer.SetMinSize((PANEL_WIDTH,-1))
470        #Sizer related to data
471        data_name_box = wx.StaticBox(self, -1, "I(q) Data Source")
472        self.data_name_boxsizer = wx.StaticBoxSizer(data_name_box, wx.VERTICAL)
473        self.data_name_boxsizer.SetMinSize((PANEL_WIDTH,-1))
474        self.hint_msg_sizer = wx.BoxSizer(wx.HORIZONTAL)
475        self.data_name_sizer = wx.BoxSizer(wx.HORIZONTAL)
476        self.data_range_sizer = wx.BoxSizer(wx.HORIZONTAL)
477        #Sizer related to background and scale
478        self.bkg_scale_sizer = wx.BoxSizer(wx.HORIZONTAL) 
479        #Sizer related to contrast and porod constant
480        self.contrast_porod_sizer = wx.BoxSizer(wx.HORIZONTAL) 
481        #Sizer related to inputs
482        inputs_box = wx.StaticBox(self, -1, "Customized Inputs")
483        self.inputs_sizer = wx.StaticBoxSizer(inputs_box, wx.VERTICAL)
484        #Sizer related to extrapolation
485        extrapolation_box = wx.StaticBox(self, -1, "Extrapolation")
486        self.extrapolation_sizer = wx.StaticBoxSizer(extrapolation_box,
487                                                        wx.VERTICAL)
488        self.extrapolation_sizer.SetMinSize((PANEL_WIDTH,-1))
489        self.extrapolation_range_sizer = wx.BoxSizer(wx.HORIZONTAL)
490        self.extrapolation_low_high_sizer = wx.BoxSizer(wx.HORIZONTAL)
491        #Sizer related to extrapolation at low q range
492        low_q_box = wx.StaticBox(self, -1, "Low Q")
493        self.low_extrapolation_sizer = wx.StaticBoxSizer(low_q_box, wx.VERTICAL)
494        self.low_q_sizer = wx.GridBagSizer(5,5)
495        #Sizer related to extrapolation at low q range
496        high_q_box = wx.StaticBox(self, -1, "High Q")
497        self.high_extrapolation_sizer = wx.StaticBoxSizer(high_q_box, wx.VERTICAL)
498        self.high_q_sizer = wx.GridBagSizer(5,5)
499        #sizer to define outputs
500        self.volume_surface_sizer = wx.GridBagSizer(5,5)
501        #Sizer related to invariant output
502        self.invariant_sizer = wx.GridBagSizer(5, 5)
503        #Sizer related to button
504        self.button_sizer = wx.BoxSizer(wx.HORIZONTAL)
505       
506    def _layout_data_name(self):
507        """
508            Draw widgets related to data's name
509        """
510        #Sizer hint
511        hint_msg = "Load Data then right click to add the data on this panel! "
512        self.hint_msg_txt = wx.StaticText(self, -1, hint_msg) 
513        self.hint_msg_txt.SetForegroundColour("red")
514        self.hint_msg_sizer.Add(self.hint_msg_txt)
515        #Data name [string]
516        data_name_txt = wx.StaticText(self, -1, 'Data : ') 
517       
518        self.data_name_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH*5, 20), style=0) 
519        self.data_name_tcl.SetToolTipString("Data's name.")
520        self.data_name_sizer.AddMany([(data_name_txt, 0, wx.LEFT|wx.RIGHT, 10),
521                                       (self.data_name_tcl, 0, wx.EXPAND)])
522        #Data range [string]
523        data_range_txt = wx.StaticText(self, -1, 'Total Q Range (1/A): ') 
524        data_min_txt = wx.StaticText(self, -1, 'Min : ') 
525        self.data_min_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0)
526        self.data_min_tcl.SetToolTipString("The minimum value of q range.")
527        data_max_txt = wx.StaticText(self, -1, 'Max : ') 
528        self.data_max_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0) 
529        self.data_max_tcl.SetToolTipString("The maximum value of q range.")
530        self.data_range_sizer.AddMany([(data_range_txt, 0, wx.RIGHT, 10),
531                                       (data_min_txt, 0, wx.RIGHT, 10),
532                                       (self.data_min_tcl, 0, wx.RIGHT, 10),
533                                       (data_max_txt, 0, wx.RIGHT, 10),
534                                       (self.data_max_tcl, 0, wx.RIGHT, 10)])
535        self.data_name_boxsizer.AddMany([(self.hint_msg_sizer, 0 , wx.ALL, 10),
536                                         (self.data_name_sizer, 0 , wx.RIGHT, 10),
537                                         (self.data_range_sizer, 0 , wx.ALL, 10)])
538   
539    def _layout_bkg_scale(self):
540        """
541            Draw widgets related to background and scale
542        """
543        background_txt = wx.StaticText(self, -1, 'Background : ') 
544        self.background_tcl = InvTextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0) 
545        background_hint_txt = "background"
546        self.background_tcl.SetToolTipString(background_hint_txt)
547        background_unit_txt = wx.StaticText(self, -1, '[1/cm]') 
548        scale_txt = wx.StaticText(self, -1, 'Scale : ') 
549        self.scale_tcl = InvTextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0)
550        scale_hint_txt = "Scale"
551        self.scale_tcl.SetToolTipString(scale_hint_txt)
552        self.bkg_scale_sizer.AddMany([(background_txt, 0, wx.LEFT, 10),
553                                       (self.background_tcl, 0, wx.LEFT, 5),
554                                       (background_unit_txt, 0, wx.LEFT, 10),
555                                       (scale_txt, 0, wx.LEFT, 70),
556                                       (self.scale_tcl, 0, wx.LEFT, 40)])
557 
558    def _layout_contrast_porod(self):
559        """
560            Draw widgets related to porod constant and contrast
561        """
562        contrast_txt = wx.StaticText(self, -1, 'Contrast : ') 
563        self.contrast_tcl = InvTextCtrl(self, -1, size=(_BOX_WIDTH, 20), style=0)
564        contrast_hint_txt = "Contrast"
565        self.contrast_tcl.SetToolTipString(contrast_hint_txt)
566        contrast_unit_txt = wx.StaticText(self, -1, '[1/A^(2)]') 
567        porod_const_txt = wx.StaticText(self, -1, 'Porod Constant:') 
568        self.porod_constant_tcl = InvTextCtrl(self, -1, 
569                                              size=(_BOX_WIDTH, 20), style=0) 
570        porod_const_hint_txt = "Porod Constant"
571        self.porod_constant_tcl.SetToolTipString(porod_const_hint_txt)
572        optional_txt = wx.StaticText(self, -1, '(Optional)') 
573        self.contrast_porod_sizer.AddMany([(contrast_txt, 0, wx.LEFT, 10),
574                                           (self.contrast_tcl, 0, wx.LEFT, 20),
575                                           (contrast_unit_txt, 0, wx.LEFT, 10),
576                                           (porod_const_txt, 0, wx.LEFT, 50),
577                                       (self.porod_constant_tcl, 0, wx.LEFT, 0),
578                                       (optional_txt, 0, wx.LEFT, 10)])
579       
580    def _enable_fit_power_law_low(self, event=None):
581        """
582            Enable and disable the power value editing
583        """
584        if self.fix_enable_low.IsEnabled():
585            if self.fix_enable_low.GetValue():
586                self.power_low_tcl.Enable()
587            else:
588                self.power_low_tcl.Disable()
589           
590    def _enable_low_q_section(self, event=None):
591        """
592            Disable or enable some button if the user enable low q extrapolation
593        """
594        if self.enable_low_cbox.GetValue():
595            self.npts_low_tcl.Enable()
596            self.fix_enable_low.Enable()
597            self.fit_enable_low.Enable()
598            self.guinier.Enable()
599            self.power_law_low.Enable()
600
601        else:
602            self.npts_low_tcl.Disable()
603            self.fix_enable_low.Disable()
604            self.fit_enable_low.Disable()
605            self.guinier.Disable()
606            self.power_law_low.Disable()
607        self._enable_power_law_low()
608        self._enable_fit_power_law_low()
609        self.button_calculate.SetFocus()
610   
611    def _enable_power_law_low(self, event=None):
612        """
613            Enable editing power law section at low q range
614        """
615        if self.guinier.GetValue():
616            self.fix_enable_low.Disable()
617            self.fit_enable_low.Disable()
618            self.power_low_tcl.Disable()
619        else:
620            self.fix_enable_low.Enable()
621            self.fit_enable_low.Enable()
622            self.power_low_tcl.Enable()
623        self._enable_fit_power_law_low()
624           
625    def _layout_extrapolation_low(self):
626        """
627            Draw widgets related to extrapolation at low q range
628        """
629        self.enable_low_cbox = wx.CheckBox(self, -1, "Enable Extrapolate Low Q")
630        wx.EVT_CHECKBOX(self, self.enable_low_cbox.GetId(),
631                                         self._enable_low_q_section)
632        self.fix_enable_low = wx.RadioButton(self, -1, 'Fix',
633                                         (10, 10),style=wx.RB_GROUP)
634        self.fit_enable_low = wx.RadioButton(self, -1, 'Fit', (10, 10))
635        self.Bind(wx.EVT_RADIOBUTTON, self._enable_fit_power_law_low,
636                                     id=self.fix_enable_low.GetId())
637        self.Bind(wx.EVT_RADIOBUTTON, self._enable_fit_power_law_low, 
638                                        id=self.fit_enable_low.GetId())
639        self.guinier = wx.RadioButton(self, -1, 'Guinier',
640                                         (10, 10),style=wx.RB_GROUP)
641        self.power_law_low = wx.RadioButton(self, -1, 'Power Law', (10, 10))
642        self.Bind(wx.EVT_RADIOBUTTON, self._enable_power_law_low,
643                                     id=self.guinier.GetId())
644        self.Bind(wx.EVT_RADIOBUTTON, self._enable_power_law_low, 
645                                        id=self.power_law_low.GetId())
646       
647        npts_low_txt = wx.StaticText(self, -1, 'Npts')
648        self.npts_low_tcl = InvTextCtrl(self, -1, size=(_BOX_WIDTH*2/3, -1))
649        msg_hint = "Number of Q points to consider"
650        msg_hint +="while extrapolating the low-Q region"
651        self.npts_low_tcl.SetToolTipString(msg_hint)
652        power_txt = wx.StaticText(self, -1, 'Power')
653        self.power_low_tcl = InvTextCtrl(self, -1, size=(_BOX_WIDTH*2/3, -1))
654       
655        power_hint_txt = "Exponent to apply to the Power_law function."
656        self.power_low_tcl.SetToolTipString(power_hint_txt)
657        iy = 0
658        ix = 0
659        self.low_q_sizer.Add(self.enable_low_cbox,(iy, ix),(1,5),
660                            wx.TOP|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
661        iy += 1
662        ix = 0
663        self.low_q_sizer.Add(npts_low_txt,(iy, ix),(1,1),
664                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
665        ix += 1
666        self.low_q_sizer.Add(self.npts_low_tcl, (iy, ix), (1,1),
667                            wx.EXPAND|wx.ADJUST_MINSIZE, 0)
668        iy += 1
669        ix = 0
670        self.low_q_sizer.Add(self.guinier,(iy, ix),(1,2),
671                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
672        iy += 1
673        ix = 0
674        self.low_q_sizer.Add(self.power_law_low,(iy, ix),(1,2),
675                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
676       
677        # Parameter controls for power law
678        ix = 1
679        iy += 1
680        self.low_q_sizer.Add(self.fix_enable_low,(iy, ix),(1,1),
681                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0)
682        ix += 1
683        self.low_q_sizer.Add(self.fit_enable_low,(iy, ix),(1,1),
684                           wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0)
685        ix = 1
686        iy += 1
687        self.low_q_sizer.Add(power_txt,(iy, ix),(1,1),
688                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0)
689        ix += 1
690        self.low_q_sizer.Add(self.power_low_tcl, (iy, ix), (1,1),
691                            wx.EXPAND|wx.ADJUST_MINSIZE, 0)
692        self.low_extrapolation_sizer.AddMany([(self.low_q_sizer, 0,
693                                                wx.BOTTOM|wx.RIGHT, 15)])
694       
695    def _enable_fit_power_law_high(self, event=None):
696        """
697            Enable and disable the power value editing
698        """
699        if self.fix_enable_high.IsEnabled():
700            if self.fix_enable_high.GetValue():
701                self.power_high_tcl.Enable()
702            else:
703                self.power_high_tcl.Disable()
704       
705    def _enable_high_q_section(self, event=None):
706        """
707            Disable or enable some button if the user enable high q extrapolation
708        """
709        if self.enable_high_cbox.GetValue():
710            self.npts_high_tcl.Enable()
711            self.power_law_high.Enable()
712            self.power_high_tcl.Enable()
713            self.fix_enable_high.Enable()
714            self.fit_enable_high.Enable()
715        else:
716            self.npts_high_tcl.Disable()
717            self.power_law_high.Disable()
718            self.power_high_tcl.Disable()
719            self.fix_enable_high.Disable()
720            self.fit_enable_high.Disable()
721        self._enable_fit_power_law_high()
722        self.button_calculate.SetFocus()
723 
724    def _layout_extrapolation_high(self):
725        """
726            Draw widgets related to extrapolation at high q range
727        """
728        self.enable_high_cbox = wx.CheckBox(self, -1, "Enable Extrapolate high-Q")
729        wx.EVT_CHECKBOX(self, self.enable_high_cbox.GetId(),
730                                         self._enable_high_q_section)
731     
732        self.fix_enable_high = wx.RadioButton(self, -1, 'Fix',
733                                         (10, 10),style=wx.RB_GROUP)
734        self.fit_enable_high = wx.RadioButton(self, -1, 'Fit', (10, 10))
735        self.Bind(wx.EVT_RADIOBUTTON, self._enable_fit_power_law_high,
736                                     id=self.fix_enable_high.GetId())
737        self.Bind(wx.EVT_RADIOBUTTON, self._enable_fit_power_law_high, 
738                                        id=self.fit_enable_high.GetId())
739       
740        self.power_law_high = wx.StaticText(self, -1, 'Power Law')
741        msg_hint ="Check to extrapolate data at high-Q"
742        self.power_law_high.SetToolTipString(msg_hint)
743        npts_high_txt = wx.StaticText(self, -1, 'Npts')
744        self.npts_high_tcl = InvTextCtrl(self, -1, size=(_BOX_WIDTH*2/3, -1))
745        msg_hint = "Number of Q points to consider"
746        msg_hint += "while extrapolating the high-Q region"
747        self.npts_high_tcl.SetToolTipString(msg_hint)
748        power_txt = wx.StaticText(self, -1, 'Power')
749        self.power_high_tcl = InvTextCtrl(self, -1, size=(_BOX_WIDTH*2/3, -1))
750        power_hint_txt = "Exponent to apply to the Power_law function."
751        self.power_high_tcl.SetToolTipString(power_hint_txt)
752        iy = 0
753        ix = 0
754        self.high_q_sizer.Add(self.enable_high_cbox,(iy, ix),(1,5),
755                            wx.TOP|wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
756        iy += 1
757        ix = 0
758        self.high_q_sizer.Add(npts_high_txt,(iy, ix),(1,1),
759                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
760        ix += 1
761        self.high_q_sizer.Add(self.npts_high_tcl, (iy, ix), (1,1),
762                            wx.EXPAND|wx.ADJUST_MINSIZE, 0)
763        iy += 2
764        ix = 0
765        self.high_q_sizer.Add(self.power_law_high,(iy, ix),(1,2),
766                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
767       
768        # Parameter controls for power law
769        ix = 1
770        iy += 1
771        self.high_q_sizer.Add(self.fix_enable_high,(iy, ix),(1,1),
772                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0)
773        ix += 1
774        self.high_q_sizer.Add(self.fit_enable_high,(iy, ix),(1,1),
775                           wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0)
776        ix = 1
777        iy += 1
778        self.high_q_sizer.Add(power_txt,(iy, ix),(1,1),
779                            wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
780        ix += 1
781        self.high_q_sizer.Add(self.power_high_tcl, (iy, ix), (1,1),
782                            wx.EXPAND|wx.ADJUST_MINSIZE, 0)
783        self.high_extrapolation_sizer.AddMany([(self.high_q_sizer, 0, 
784                                                wx.BOTTOM|wx.RIGHT, 10)])
785       
786    def _layout_extrapolation(self):
787        """
788            Draw widgets related to extrapolation
789        """
790        extra_hint = "Extrapolation Maximum Q Range [1/A]: "
791        extra_hint_txt = wx.StaticText(self, -1, extra_hint)
792        #Extrapolation range [string]
793        extrapolation_min_txt = wx.StaticText(self, -1, 'Min : ') 
794        self.extrapolation_min_tcl = OutputTextCtrl(self, -1, 
795                                                size=(_BOX_WIDTH, 20), style=0)
796        self.extrapolation_min_tcl.SetValue(str(Q_MINIMUM))
797        self.extrapolation_min_tcl.SetToolTipString("The minimum extrapolated q value.")
798        extrapolation_max_txt = wx.StaticText(self, -1, 'Max : ') 
799        self.extrapolation_max_tcl = OutputTextCtrl(self, -1,
800                                                  size=(_BOX_WIDTH, 20), style=0) 
801        self.extrapolation_max_tcl.SetValue(str(Q_MAXIMUM))
802        self.extrapolation_max_tcl.SetToolTipString("The maximum extrapolated q value.")
803        self.extrapolation_range_sizer.AddMany([(extra_hint_txt, 0, wx.LEFT, 10),
804                                                (extrapolation_min_txt, 0, wx.LEFT, 10),
805                                                (self.extrapolation_min_tcl,
806                                                            0, wx.LEFT, 10),
807                                                (extrapolation_max_txt, 0, wx.LEFT, 10),
808                                                (self.extrapolation_max_tcl,
809                                                            0, wx.LEFT, 10),
810                                                ])
811        self._layout_extrapolation_low()
812        self._layout_extrapolation_high()
813        self.extrapolation_low_high_sizer.AddMany([(self.low_extrapolation_sizer,
814                                                     0, wx.ALL, 10),
815                                                   (self.high_extrapolation_sizer,
816                                                    0, wx.ALL, 10)])
817        self.extrapolation_sizer.AddMany([(self.extrapolation_range_sizer, 0,
818                                            wx.RIGHT, 10),
819                                        (self.extrapolation_low_high_sizer, 0,
820                                           wx.ALL, 10)])
821       
822    def _layout_volume_surface_sizer(self):
823        """
824            Draw widgets related to volume and surface
825        """
826        unit_volume = ''
827        unit_surface = ''
828        uncertainty = "+/-" 
829        volume_txt = wx.StaticText(self, -1, 'Volume Fraction')
830        self.volume_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1))
831        self.volume_tcl.SetToolTipString("Volume fraction.")
832        self.volume_err_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1))
833        self.volume_err_tcl.SetToolTipString("Uncertainty on the volume fraction.")
834        volume_units_txt = wx.StaticText(self, -1, unit_volume)
835       
836        surface_txt = wx.StaticText(self, -1, 'Specific Surface')
837        self.surface_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1))
838        self.surface_tcl.SetToolTipString("Specific surface value.")
839        self.surface_err_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1))
840        self.surface_err_tcl.SetToolTipString("Uncertainty on the specific surface.")
841        surface_units_txt = wx.StaticText(self, -1, unit_surface)
842        iy = 0
843        ix = 0
844        self.volume_surface_sizer.Add(volume_txt, (iy, ix), (1,1),
845                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
846        ix += 1
847        self.volume_surface_sizer.Add(self.volume_tcl, (iy, ix), (1,1),
848                            wx.EXPAND|wx.ADJUST_MINSIZE, 10)
849        ix += 1
850        self.volume_surface_sizer.Add(wx.StaticText(self, -1, uncertainty),
851                         (iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 10) 
852        ix += 1
853        self.volume_surface_sizer.Add(self.volume_err_tcl, (iy, ix), (1,1),
854                            wx.EXPAND|wx.ADJUST_MINSIZE, 10) 
855        ix += 1
856        self.volume_surface_sizer.Add(volume_units_txt, (iy, ix), (1,1),
857                             wx.EXPAND|wx.ADJUST_MINSIZE, 10)
858        iy += 1
859        ix = 0
860        self.volume_surface_sizer.Add(surface_txt, (iy, ix), (1,1),
861                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
862        ix += 1
863        self.volume_surface_sizer.Add(self.surface_tcl, (iy, ix), (1,1),
864                            wx.EXPAND|wx.ADJUST_MINSIZE, 0)
865        ix += 1
866        self.volume_surface_sizer.Add(wx.StaticText(self, -1, uncertainty),
867                         (iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
868        ix += 1
869        self.volume_surface_sizer.Add(self.surface_err_tcl, (iy, ix), (1,1),
870                            wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
871        ix += 1
872        self.volume_surface_sizer.Add(surface_units_txt, (iy, ix), (1,1),
873                            wx.EXPAND|wx.ADJUST_MINSIZE, 10)
874       
875    def _layout_invariant_sizer(self):
876        """
877            Draw widgets related to invariant
878        """
879        uncertainty = "+/-" 
880        unit_invariant = '[1/(cm * A)]'
881        invariant_total_txt = wx.StaticText(self, -1, 'Invariant Total')
882        self.invariant_total_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1))
883        msg_hint = "Total invariant, including extrapolated regions."
884        self.invariant_total_tcl.SetToolTipString(msg_hint)
885        self.invariant_total_err_tcl = OutputTextCtrl(self, -1, size=(_BOX_WIDTH,-1))
886        self.invariant_total_err_tcl.SetToolTipString("Uncertainty on invariant.")
887        invariant_total_units_txt = wx.StaticText(self, -1, unit_invariant)
888   
889        #Invariant total
890        iy = 0
891        ix = 0
892        self.invariant_sizer.Add(invariant_total_txt, (iy, ix), (1,1),
893                             wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15)
894        ix += 1
895        self.invariant_sizer.Add(self.invariant_total_tcl, (iy, ix), (1,1),
896                           wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5)
897        ix += 1
898        self.invariant_sizer.Add( wx.StaticText(self, -1, uncertainty),
899                         (iy, ix),(1,1),wx.EXPAND|wx.ADJUST_MINSIZE, 10) 
900        ix += 1
901        self.invariant_sizer.Add(self.invariant_total_err_tcl, (iy, ix), (1,1),
902                             wx.EXPAND|wx.ADJUST_MINSIZE, 10)
903        ix += 1
904        self.invariant_sizer.Add(invariant_total_units_txt,(iy, ix), (1,1),
905                          wx.EXPAND|wx.ADJUST_MINSIZE, 10)
906 
907    def _layout_inputs_sizer(self):
908        """
909            Draw widgets related to inputs
910        """
911        self._layout_bkg_scale()
912        self._layout_contrast_porod()
913        self.inputs_sizer.AddMany([(self.bkg_scale_sizer, 0, wx.ALL, 5),
914                                    (self.contrast_porod_sizer, 0, wx.ALL, 5)])
915       
916    def _layout_outputs_sizer(self):
917        """
918            Draw widgets related to outputs
919        """
920        self._layout_volume_surface_sizer()
921        self._layout_invariant_sizer()
922        static_line = wx.StaticLine(self, -1)
923        self.outputs_sizer.AddMany([(self.volume_surface_sizer, 0, wx.ALL, 10),
924                                    (static_line, 0, wx.EXPAND, 0),
925                                    (self.invariant_sizer, 0, wx.ALL, 10)])
926    def _layout_button(self): 
927        """
928            Do the layout for the button widgets
929        """ 
930        #compute button
931        id = wx.NewId()
932        self.button_calculate = wx.Button(self, id, "Compute")
933        self.button_calculate.SetToolTipString("Compute invariant")
934        self.Bind(wx.EVT_BUTTON, self.compute_invariant, id=id)   
935        #detail button
936        id = wx.NewId()
937        self.button_details = wx.Button(self, id, "Details?")
938        self.button_details.SetToolTipString("Give Details on Computation")
939        self.Bind(wx.EVT_BUTTON, self.display_details, id=id)
940        details = "Details on Invariant Total Calculations"
941        details_txt = wx.StaticText(self, -1, details)
942        self.button_sizer.AddMany([((10,10), 0 , wx.LEFT,0),
943                                   (details_txt, 0 , 
944                                    wx.RIGHT|wx.BOTTOM|wx.TOP, 10),
945                                   (self.button_details, 0 , wx.ALL, 10),
946                        (self.button_calculate, 0 , wx.RIGHT|wx.TOP|wx.BOTTOM, 10)])
947       
948    def _do_layout(self):
949        """
950            Draw window content
951        """
952        self._define_structure()
953        self._layout_data_name()
954        self._layout_extrapolation()
955        self._layout_inputs_sizer()
956        self._layout_outputs_sizer()
957        self._layout_button()
958        self.main_sizer.AddMany([(self.data_name_boxsizer, 1, wx.ALL, 10),
959                                  (self.outputs_sizer, 0,
960                                  wx.LEFT|wx.RIGHT|wx.BOTTOM, 10),
961                                  (self.button_sizer, 0,
962                                  wx.LEFT|wx.RIGHT|wx.BOTTOM, 10),
963                                 (self.inputs_sizer, 0,
964                                  wx.LEFT|wx.RIGHT|wx.BOTTOM, 10),
965                                  (self.extrapolation_sizer, 0,
966                                  wx.LEFT|wx.RIGHT|wx.BOTTOM, 10)])
967        self.SetSizer(self.main_sizer)
968        self.SetScrollbars(20,20,25,65)
969        self.SetAutoLayout(True)
970   
971class InvariantDialog(wx.Dialog):
972    def __init__(self, parent=None, id=1,graph=None,
973                 data=None, title="Invariant",base=None):
974        wx.Dialog.__init__(self, parent, id, title, size=(PANEL_WIDTH,
975                                                             PANEL_HEIGHT))
976        self.panel = InvariantPanel(self)
977        self.Centre()
978        self.Show(True)
979       
980class InvariantWindow(wx.Frame):
981    def __init__(self, parent=None, id=1,graph=None, 
982                 data=None, title="Invariant",base=None):
983       
984        wx.Frame.__init__(self, parent, id, title, size=(PANEL_WIDTH +100,
985                                                             PANEL_HEIGHT+100))
986       
987        self.panel = InvariantPanel(self)
988        self.Centre()
989        self.Show(True)
990       
991class MyApp(wx.App):
992    def OnInit(self):
993        wx.InitAllImageHandlers()
994        frame = InvariantWindow()
995        frame.Show(True)
996        self.SetTopWindow(frame)
997       
998        return True
999     
1000# end of class MyApp
1001
1002if __name__ == "__main__":
1003    app = MyApp(0)
1004    app.MainLoop()
Note: See TracBrowser for help on using the repository browser.