Changeset b6b81a3 in sasview
 Timestamp:
 Mar 6, 2018 3:16:23 AM (5 months ago)
 Branches:
 ticket885
 Parents:
 943e17a
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

src/sas/sasgui/perspectives/calculator/model_editor.py
r2469df7 rb6b81a3 586 586 self.function_sizer = None 587 587 self.func_horizon_sizer = None 588 self.volume_sizer = None 588 589 self.button_sizer = None 589 590 self.param_strings = '' … … 597 598 #self._description = "New Plugin Model" 598 599 self.function_tcl = None 600 self.volume_tcl = None 599 601 self.math_combo = None 600 602 self.bt_apply = None … … 617 619 self.function_sizer = wx.BoxSizer(wx.VERTICAL) 618 620 self.func_horizon_sizer = wx.BoxSizer(wx.HORIZONTAL) 621 self.volume_sizer = wx.BoxSizer(wx.VERTICAL) 619 622 self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) 620 623 self.msg_sizer = wx.BoxSizer(wx.HORIZONTAL) … … 660 663 Do the layout for parameter related widgets 661 664 """ 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" 670 672 newid = wx.NewId() 671 673 self.param_tcl = EditWindow(self, newid, wx.DefaultPosition, … … 679 681 680 682 # 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 polydispersityand " + \683 pd_param_txt = wx.StaticText(self, 1, 'Volume parameters ' + \ 684 ' (can be polydisperse): ') 685 686 pd_param_tip = "# Set the volume parameters and " + \ 685 687 "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" 688 690 newid = wx.NewId() 689 691 self.pd_param_tcl = EditWindow(self, newid, wx.DefaultPosition, … … 701 703 """ 702 704 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*(snqr*cn)/qr**3 \n" 715 hint_function += "else: \n" 716 hint_function += " bes = 1.0 \n" 717 hint_function += "fq = bes * (sld_spheresld_solvent)*V \n" 718 hint_function += "return 1.0e4 * fq**2 \n" 709 719 math_txt = wx.StaticText(self, 1, '*Useful math functions: ') 710 720 math_combo = self._fill_math_combo() … … 724 734 self.function_sizer.Add(self.function_tcl, 1, wx.EXPAND  wx.ALL, 10) 725 735 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 726 757 def _layout_msg(self): 727 758 """ … … 761 792 self._layout_param() 762 793 self._layout_function() 794 self._layout_volume() 763 795 self._layout_msg() 764 796 self._layout_button() … … 772 804 (wx.StaticLine(self), 0, 773 805 wx.ALL  wx.EXPAND, 5), 774 (self.function_sizer, 2,806 (self.function_sizer, 1, 775 807 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), 776 811 (wx.StaticLine(self), 0, 777 812 wx.ALL  wx.EXPAND, 5), … … 898 933 pd_param_str = self.pd_param_tcl.GetText() 899 934 func_str = self.function_tcl.GetText() 935 volume_str = self.volume_tcl.GetText() 900 936 # No input for the model function 901 937 if func_str.lstrip().rstrip(): 902 938 if func_str.count('return') > 0: 903 939 self.write_file(self.fname, name, description, param_str, 904 pd_param_str, func_str )940 pd_param_str, func_str, volume_str) 905 941 try: 906 942 result, msg = check_model(self.fname), None … … 942 978 self.warning = msg 943 979 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): 945 981 """ 946 982 Write content in file … … 978 1014 979 1015 # Write out function definition 1016 out_f.write('\n') 980 1017 out_f.write('def Iq(%s):\n' % ', '.join(['x'] + param_names)) 981 1018 out_f.write(' """Absolute scattering"""\n') 982 if "scipy." in func_str:983 out_f.write(' import scipy')984 1019 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') 988 1023 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') 990 1026 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)}) 997 1051 998 1052 # Create place holder for Iqxy … … 1003 1057 out_f.write('# return oriented_form(x, y, args)\n') 1004 1058 out_f.write('## uncomment the following if Iqxy works for vector x, y\n') 1005 out_f.write('#I qxy.vectorized = True\n')1059 out_f.write('#I qxy.vectorized = True\n') 1006 1060 1007 1061 out_f.close() … … 1128 1182 1129 1183 CUSTOM_TEMPLATE_PD = '''\ 1130 def form_volume(%(args)s):1131 """1132 Volume of the particles used to compute absolute scattering intensity1133 and to weight polydisperse parameter contributions.1134 """1135 return 0.01136 1137 1184 def ER(%(args)s): 1138 1185 """ … … 1141 1188 Input parameters are vectors ranging over the mesh of polydispersity values. 1142 1189 """ 1143 return 0.01190 return 1.0 1144 1191 1145 1192 def VR(%(args)s):
Note: See TracChangeset
for help on using the changeset viewer.