Changeset b6b81a3 in sasview


Ignore:
Timestamp:
Mar 6, 2018 1:16:23 AM (6 years ago)
Author:
Gonzalez, Miguel <gonzalez@…>
Branches:
ticket885
Parents:
943e17a
Message:

Work on progress on simple editor

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/sasgui/perspectives/calculator/model_editor.py

    r2469df7 rb6b81a3  
    586586        self.function_sizer = None 
    587587        self.func_horizon_sizer = None 
     588        self.volume_sizer = None 
    588589        self.button_sizer = None 
    589590        self.param_strings = '' 
     
    597598        #self._description = "New Plugin Model" 
    598599        self.function_tcl = None 
     600        self.volume_tcl = None 
    599601        self.math_combo = None 
    600602        self.bt_apply = None 
     
    617619        self.function_sizer = wx.BoxSizer(wx.VERTICAL) 
    618620        self.func_horizon_sizer = wx.BoxSizer(wx.HORIZONTAL) 
     621        self.volume_sizer = wx.BoxSizer(wx.VERTICAL) 
    619622        self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) 
    620623        self.msg_sizer = wx.BoxSizer(wx.HORIZONTAL) 
     
    660663        Do the layout for parameter related widgets 
    661664        """ 
    662         param_txt = wx.StaticText(self, -1, 'Fit Parameters NOT requiring' + \ 
    663                                   ' polydispersity (if any): ') 
    664  
    665         param_tip = "#Set the parameters NOT requiring polydispersity " + \ 
    666         "and their initial values.\n" 
    667         param_tip += "#Example:\n" 
    668         param_tip += "A = 1\nB = 1" 
    669         #param_txt.SetToolTipString(param_tip) 
     665        param_txt = wx.StaticText(self, -1, 'Non volume parameters (no polydisperse): ') 
     666 
     667        param_tip =  "# Set the parameters that do NOT enter \n" 
     668        param_tip += " in the volume calculation and their initial values.\n" 
     669        param_tip += "# Example:\n" 
     670        param_tip += "sld_sphere = 1\n" 
     671        param_tip += "sld_solvent = 6" 
    670672        newid = wx.NewId() 
    671673        self.param_tcl = EditWindow(self, newid, wx.DefaultPosition, 
     
    679681 
    680682        # Parameters with polydispersity 
    681         pd_param_txt = wx.StaticText(self, -1, 'Fit Parameters requiring ' + \ 
    682                                      'polydispersity (if any): ') 
    683  
    684         pd_param_tip = "#Set the parameters requiring polydispersity and " + \ 
     683        pd_param_txt = wx.StaticText(self, -1, 'Volume parameters ' + \ 
     684                                     ' (can be polydisperse): ') 
     685 
     686        pd_param_tip = "# Set the volume parameters and " + \ 
    685687        "their initial values.\n" 
    686         pd_param_tip += "#Example:\n" 
    687         pd_param_tip += "C = 2\nD = 2" 
     688        pd_param_tip += "# Example:\n" 
     689        pd_param_tip += "R_sphere = 100" 
    688690        newid = wx.NewId() 
    689691        self.pd_param_tcl = EditWindow(self, newid, wx.DefaultPosition, 
     
    701703        """ 
    702704        function_txt = wx.StaticText(self, -1, 'Function(x) : ') 
    703         hint_function = "#Example:\n" 
    704         hint_function += "if x <= 0:\n" 
    705         hint_function += "    y = A + B\n" 
    706         hint_function += "else:\n" 
    707         hint_function += "    y = A + B * cos(2 * pi * x)\n" 
    708         hint_function += "return y\n" 
     705        hint_function =  "# Write the function describing the form factor. \n" 
     706        hint_function += "# Mathematical functions can be selected from the list. \n" 
     707        hint_function += "# Other numpy functions can also be used by writing e.g. \n" 
     708        hint_function += "# numpy.sinh(x) or np.sinh(x). \n" 
     709        hint_function += "# Example: \n" 
     710        hint_function += "V = (4./3.)*pi*R_sphere**3 \n" 
     711        hint_function += "qr = x*R_sphere \n" 
     712        hint_function += "sn, cn = sin(qr), cos(qr) \n" 
     713        hint_function += "if qr > 0: \n" 
     714        hint_function += "   bes = 3*(sn-qr*cn)/qr**3 \n" 
     715        hint_function += "else: \n" 
     716        hint_function += "   bes = 1.0 \n" 
     717        hint_function += "fq = bes * (sld_sphere-sld_solvent)*V \n" 
     718        hint_function += "return 1.0e-4 * fq**2 \n" 
    709719        math_txt = wx.StaticText(self, -1, '*Useful math functions: ') 
    710720        math_combo = self._fill_math_combo() 
     
    724734        self.function_sizer.Add(self.function_tcl, 1, wx.EXPAND | wx.ALL, 10) 
    725735 
     736    def _layout_volume(self): 
     737        """ 
     738        Do the layout for the form_volume widget 
     739        """ 
     740        volume_txt = wx.StaticText(self, -1, 'Function defining the volume of the particle' + \ 
     741                                  ' (if left empty, V = 1.0): ') 
     742 
     743        volume_tip = "# Example: \n" 
     744        volume_tip += " V = (4./3.)*pi*R_sphere**3 \n" 
     745        volume_tip += "return V \n" 
     746        newid = wx.NewId() 
     747        self.volume_tcl = EditWindow(self, newid, wx.DefaultPosition, 
     748                                    wx.DefaultSize, 
     749                                    wx.CLIP_CHILDREN | wx.SUNKEN_BORDER) 
     750        self.volume_tcl.setDisplayLineNumbers(True) 
     751        self.volume_tcl.SetToolTipString(volume_tip) 
     752 
     753        self.volume_sizer.AddMany([(volume_txt, 0, wx.LEFT, 10), 
     754                                  (self.volume_tcl, 1, wx.EXPAND | wx.ALL, 10)]) 
     755 
     756 
    726757    def _layout_msg(self): 
    727758        """ 
     
    761792        self._layout_param() 
    762793        self._layout_function() 
     794        self._layout_volume() 
    763795        self._layout_msg() 
    764796        self._layout_button() 
     
    772804                                 (wx.StaticLine(self), 0, 
    773805                                  wx.ALL | wx.EXPAND, 5), 
    774                                  (self.function_sizer, 2, 
     806                                 (self.function_sizer, 1, 
    775807                                  wx.EXPAND | wx.ALL, 5), 
     808                                 (wx.StaticLine(self), 0, 
     809                                  wx.ALL | wx.EXPAND, 5), 
     810                                 (self.volume_sizer, 1, wx.EXPAND | wx.ALL, 5), 
    776811                                 (wx.StaticLine(self), 0, 
    777812                                  wx.ALL | wx.EXPAND, 5), 
     
    898933            pd_param_str = self.pd_param_tcl.GetText() 
    899934            func_str = self.function_tcl.GetText() 
     935            volume_str = self.volume_tcl.GetText() 
    900936            # No input for the model function 
    901937            if func_str.lstrip().rstrip(): 
    902938                if func_str.count('return') > 0: 
    903939                    self.write_file(self.fname, name, description, param_str, 
    904                                     pd_param_str, func_str) 
     940                                    pd_param_str, func_str, volume_str) 
    905941                    try: 
    906942                        result, msg = check_model(self.fname), None 
     
    942978        self.warning = msg 
    943979 
    944     def write_file(self, fname, name, desc_str, param_str, pd_param_str, func_str): 
     980    def write_file(self, fname, name, desc_str, param_str, pd_param_str, func_str, volume_str): 
    945981        """ 
    946982        Write content in file 
     
    9781014 
    9791015        # Write out function definition 
     1016        out_f.write('\n') 
    9801017        out_f.write('def Iq(%s):\n' % ', '.join(['x'] + param_names)) 
    9811018        out_f.write('    """Absolute scattering"""\n') 
    982         if "scipy." in func_str: 
    983             out_f.write('    import scipy') 
    9841019        if "numpy." in func_str: 
    985             out_f.write('    import numpy') 
    986         if "np." in func_str: 
    987             out_f.write('    import numpy as np') 
     1020            out_f.write('    import numpy\n') 
     1021        elif "np." in func_str: 
     1022            out_f.write('    import numpy as np\n') 
    9881023        for func_line in func_str.split('\n'): 
    989             out_f.write('%s%s\n' % (spaces4, func_line)) 
     1024            out_f.write('%s%s' % ('    ', func_line)) 
     1025        out_f.write('\n')     
    9901026        out_f.write('## uncomment the following if Iq works for vector x\n') 
    991         out_f.write('#Iq.vectorized = True\n') 
    992  
    993         # If polydisperse, create place holders for form_volume, ER and VR 
    994         if pd_params: 
    995             out_f.write('\n') 
    996             out_f.write(CUSTOM_TEMPLATE_PD % {'args': ', '.join(pd_params)}) 
     1027        out_f.write('#    Iq.vectorized = True\n') 
     1028         
     1029        # Write out form_volume 
     1030        out_f.write('\n') 
     1031        out_f.write('def form_volume(%s):\n' % ', '.join(pd_params)) 
     1032        out_f.write('    """ \n') 
     1033        out_f.write('    Volume of the particles used to compute absolute scattering intensity \n') 
     1034        out_f.write('    and to weight polydisperse parameter contributions. \n') 
     1035        out_f.write('    """\n') 
     1036        if volume_str.lstrip().rstrip() and volume_str.count('return'): 
     1037            if "numpy." in volume_str: 
     1038                out_f.write('    import numpy\n') 
     1039            elif "np." in volume_str: 
     1040                out_f.write('    import numpy as np\n') 
     1041            for vol_line in volume_str.split('\n'): 
     1042                out_f.write('%s%s' % ('    ', vol_line)) 
     1043        else: 
     1044            out_f.write('    return 1.0 \n') 
     1045        out_f.write('\n') 
     1046 
     1047        # If polydisperse, create place holders also for ER and VR 
     1048        #if pd_params: 
     1049        #    out_f.write('\n') 
     1050        #    out_f.write(CUSTOM_TEMPLATE_PD % {'args': ', '.join(pd_params)}) 
    9971051 
    9981052        # Create place holder for Iqxy 
     
    10031057        out_f.write('#    return oriented_form(x, y, args)\n') 
    10041058        out_f.write('## uncomment the following if Iqxy works for vector x, y\n') 
    1005         out_f.write('#Iqxy.vectorized = True\n') 
     1059        out_f.write('#I    qxy.vectorized = True\n') 
    10061060 
    10071061        out_f.close() 
     
    11281182 
    11291183CUSTOM_TEMPLATE_PD = '''\ 
    1130 def form_volume(%(args)s): 
    1131     """ 
    1132     Volume of the particles used to compute absolute scattering intensity 
    1133     and to weight polydisperse parameter contributions. 
    1134     """ 
    1135     return 0.0 
    1136  
    11371184def ER(%(args)s): 
    11381185    """ 
     
    11411188    Input parameters are vectors ranging over the mesh of polydispersity values. 
    11421189    """ 
    1143     return 0.0 
     1190    return 1.0 
    11441191 
    11451192def VR(%(args)s): 
Note: See TracChangeset for help on using the changeset viewer.