Changeset cf92b1f in sasview for src/sas/sasgui
- Timestamp:
- Mar 21, 2016 10:22:54 AM (9 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 4ec9fa2
- Parents:
- 25427d9
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sasgui/perspectives/calculator/model_editor.py
r5876f7e rcf92b1f 745 745 self.param_sizer.AddMany([(param_txt, 0, wx.LEFT, 10), 746 746 (self.param_tcl, 1, wx.EXPAND | wx.ALL, 10)]) 747 748 # Parameters with polydispersity 749 pd_param_txt = wx.StaticText(self, -1, 'Fit Parameters requiring polydispersity (if any): ') 750 751 pd_param_tip = "#Set the parameters and initial values.\n" 752 pd_param_tip += "#Example:\n" 753 pd_param_tip += "C = 2\nD = 2" 754 newid = wx.NewId() 755 self.pd_param_tcl = EditWindow(self, newid, wx.DefaultPosition, 756 wx.DefaultSize, 757 wx.CLIP_CHILDREN | wx.SUNKEN_BORDER) 758 self.pd_param_tcl.setDisplayLineNumbers(True) 759 self.pd_param_tcl.SetToolTipString(pd_param_tip) 760 761 self.param_sizer.AddMany([(pd_param_txt, 0, wx.LEFT, 10), 762 (self.pd_param_tcl, 1, wx.EXPAND | wx.ALL, 10)]) 747 763 748 764 def _layout_function(self): … … 946 962 description = self.desc_tcl.GetValue() 947 963 param_str = self.param_tcl.GetText() 964 pd_param_str = self.pd_param_tcl.GetText() 948 965 func_str = self.function_tcl.GetText() 949 966 # No input for the model function 950 967 if func_str.lstrip().rstrip(): 951 968 if func_str.count('return') > 0: 952 self.write_file(self.fname, description, param_str, 953 func_str) 954 tr_msg = _compile_file(self.fname) 955 msg = str(tr_msg.__str__()) 956 # Compile error 957 if msg: 958 msg.replace(" ", "\n") 959 msg += "\nCompiling Failed" 960 try: 961 # try to remove pyc file if exists 962 _delete_file(self.fname) 963 _delete_file(self.fname + "c") 964 except: 965 pass 969 self.write_file(self.fname, name, description, param_str, 970 pd_param_str, func_str) 971 # Modified compiling test, as it will fail for sasmodels.sasview_model class 972 # Should add a test to check that the class is correctly built 973 # by sasview_model.make_class_from_file? 974 # try: 975 # tr_msg = _compile_file(self.fname) 976 # msg = str(tr_msg.__str__()) 977 # # Compile error 978 # if msg: 979 # msg.replace(" ", "\n") 980 # msg += "\nCompiling Failed" 981 # try: 982 # # try to remove pyc file if exists 983 # _delete_file(self.fname) 984 # _delete_file(self.fname + "c") 985 # except: 986 # pass 987 # except: 988 # pass 966 989 else: 967 990 msg = "Error: The func(x) must 'return' a value at least.\n" … … 971 994 else: 972 995 msg = "Name exists already." 996 973 997 # Prepare the messagebox 974 998 if self.base != None and not msg: 975 999 self.base.update_custom_combo() 1000 # Passed exception in import test as it will fail for sasmodels.sasview_model class 1001 # Should add similar test for new style? 976 1002 Model = None 977 1003 try: 978 1004 exec "from %s import Model" % name 979 1005 except: 980 msg = 'new model fails to import in python' 981 try: 982 # try to remove pyc file if exists 983 _delete_file(self.fname + "c") 984 except: 985 pass 986 if self.base != None and not msg: 987 try: 988 Model().run(0.01) 989 except: 990 msg = "new model fails on run method:" 991 _, value, _ = sys.exc_info() 992 msg += "in %s:\n%s\n" % (name, value) 993 try: 994 # try to remove pyc file if exists 995 _delete_file(self.fname + "c") 996 except: 997 pass 1006 pass 1007 # except: 1008 # msg = 'new model fails to import in python' 1009 # try: 1010 # # try to remove pyc file if exists 1011 # _delete_file(self.fname + "c") 1012 # except: 1013 # pass 1014 # 1015 # And also need to test if model runs 1016 # if self.base != None and not msg: 1017 # try: 1018 # Model().run(0.01) 1019 # except: 1020 # msg = "new model fails on run method:" 1021 # _, value, _ = sys.exc_info() 1022 # msg += "in %s:\n%s\n" % (name, value) 1023 # try: 1024 # # try to remove pyc file if exists 1025 # _delete_file(self.fname + "c") 1026 # except: 1027 # pass 998 1028 # Prepare the messagebox 999 1029 if msg: … … 1014 1044 self.warning = msg 1015 1045 1016 1017 def write_file(self, fname, desc_str, param_str, func_str): 1046 def write_file(self, fname, name, desc_str, param_str, pd_param_str, func_str): 1018 1047 """ 1019 1048 Write content in file … … 1022 1051 :param desc_str: content of the description strings 1023 1052 :param param_str: content of params; Strings 1053 :param pd_param_str: content of params requiring polydispersity; Strings 1024 1054 :param func_str: content of func; Strings 1025 1055 """ … … 1032 1062 1033 1063 has_scipy = func_str.count("scipy.") 1034 self.is_2d = func_str.count("#self.ndim = 2") 1035 line_2d = '' 1036 if self.is_2d: 1037 line_2d = CUSTOM_2D_TEMP.split('\n') 1038 line_test = TEST_TEMPLATE.split('\n') 1039 local_params = '' 1040 spaces = ' '#8spaces 1064 if has_scipy: 1065 lines.insert(0, 'import scipy') 1066 1067 # Think about 2D later 1068 #self.is_2d = func_str.count("#self.ndim = 2") 1069 #line_2d = '' 1070 #if self.is_2d: 1071 # line_2d = CUSTOM_2D_TEMP.split('\n') 1072 1073 # Also think about test later 1074 #line_test = TEST_TEMPLATE.split('\n') 1075 #local_params = '' 1076 #spaces = ' '#8spaces 1077 spaces4 = ' '*4 1078 spaces13 = ' '*13 1079 spaces16 = ' '*16 1080 param_names = [] # to store parameter names 1081 has_scipy = func_str.count("scipy.") 1082 if has_scipy: 1083 lines.insert(0, 'import scipy') 1084 1041 1085 # write function here 1042 1086 for line in lines: 1043 1087 # The location where to put the strings is 1044 1088 # hard-coded in the template as shown below. 1045 if line.count("#self.params here"): 1089 out_f.write(line + '\n') 1090 if line.count('#name'): 1091 out_f.write('name = "%s" \n' % name) 1092 elif line.count('#title'): 1093 out_f.write('title = "User model for %s"\n' % name) 1094 elif line.count('#description'): 1095 out_f.write('description = "%s"\n' % desc_str) 1096 elif line.count('#parameters'): 1097 out_f.write('parameters = [ \n') 1046 1098 for param_line in param_str.split('\n'): 1047 1099 p_line = param_line.lstrip().rstrip() 1048 1100 if p_line: 1049 p0_line = self.set_param_helper(p_line) 1050 local_params += self.set_function_helper(p_line) 1051 out_f.write(p0_line) 1052 elif line.count("#local params here"): 1053 if local_params: 1054 out_f.write(local_params) 1055 elif line.count("self.description = "): 1056 des0 = self.name + "\\n" 1057 desc = str(desc_str.lstrip().rstrip().replace('\"', '')) 1058 out_f.write(line % (des0 + desc) + "\n") 1059 elif line.count("def function(self, x=0.0%s):"): 1060 if self.is_2d: 1061 y_str = ', y=0.0' 1062 out_f.write(line % y_str + "\n") 1063 else: 1064 out_f.write(line % '' + "\n") 1065 elif line.count("#function here"): 1066 for func_line in func_str.split('\n'): 1067 f_line = func_line.rstrip() 1068 if f_line: 1069 out_f.write(spaces + f_line + "\n") 1070 if not func_str: 1071 dep_var = 'y' 1072 if self.is_2d: 1073 dep_var = 'z' 1074 out_f.write(spaces + 'return %s' % dep_var + "\n") 1075 elif line.count("#import scipy?"): 1076 if has_scipy: 1077 out_f.write("import scipy" + "\n") 1078 #elif line.count("name = "): 1079 # out_f.write(line % self.name + "\n") 1080 elif line: 1081 out_f.write(line + "\n") 1082 # run string for 2d 1083 if line_2d: 1084 for line in line_2d: 1085 out_f.write(line + "\n") 1086 # Test strins 1087 for line in line_test: 1088 out_f.write(line + "\n") 1101 pname, pvalue = self.get_param_helper(p_line) 1102 param_names.append(pname) 1103 out_f.write("%s['%s', '', %s, [-numpy.inf, numpy.inf], '', ''],\n" % (spaces16, pname, pvalue)) 1104 for param_line in pd_param_str.split('\n'): 1105 p_line = param_line.lstrip().rstrip() 1106 if p_line: 1107 pname, pvalue = self.get_param_helper(p_line) 1108 param_names.append(pname) 1109 out_f.write("%s['%s', '', %s, [-numpy.inf, numpy.inf], 'volume', ''],\n" % (spaces16, pname, pvalue)) 1110 out_f.write('%s]\n' % spaces13) 1111 1112 # No form_volume or ER available in simple model editor 1113 out_f.write('def form_volume(*arg): \n') 1114 out_f.write(' return 1.0 \n') 1115 out_f.write('\n') 1116 out_f.write('def ER(*arg): \n') 1117 out_f.write(' return 1.0 \n') 1118 1119 # function to compute 1120 out_f.write('\n') 1121 out_f.write('def Iq(x ') 1122 for name in param_names: 1123 out_f.write(', %s' % name) 1124 out_f.write('):\n') 1125 for func_line in func_str.split('\n'): 1126 out_f.write('%s%s\n' % (spaces4, func_line)) 1127 1128 Iqxy_string = 'return Iq(numpy.sqrt(x**2+y**2) ' 1129 1130 out_f.write('\n') 1131 out_f.write('def Iqxy(x, y ') 1132 for name in param_names: 1133 out_f.write(', %s' % name) 1134 Iqxy_string += ', ' + name 1135 out_f.write('):\n') 1136 Iqxy_string += ')' 1137 out_f.write('%s%s\n' % (spaces4, Iqxy_string)) 1089 1138 1090 1139 out_f.close() 1091 1140 1092 def set_param_helper(self, line):1141 def get_param_helper(self, line): 1093 1142 """ 1094 1143 Get string in line to define the params dictionary … … 1096 1145 :param line: one line of string got from the param_str 1097 1146 """ 1098 params_str = ''1099 spaces = ' '#8spaces1100 1147 items = line.split(";") 1101 1148 for item in items: … … 1106 1153 except: 1107 1154 value = 1.0 # default 1108 params_str += spaces + "self.params['%s'] = %s\n" % (name, value) 1109 1110 return params_str 1155 1156 return name, value 1111 1157 1112 1158 def set_function_helper(self, line): … … 1185 1231 1186 1232 ## Templates for custom models 1233 #CUSTOM_TEMPLATE = """ 1234 #from sas.models.pluginmodel import Model1DPlugin 1235 #from math import * 1236 #import os 1237 #import sys 1238 #import numpy 1239 ##import scipy? 1240 #class Model(Model1DPlugin): 1241 # name = "" 1242 # def __init__(self): 1243 # Model1DPlugin.__init__(self, name=self.name) 1244 # #set name same as file name 1245 # self.name = self.get_fname() 1246 # #self.params here 1247 # self.description = "%s" 1248 # self.set_details() 1249 # def function(self, x=0.0%s): 1250 # #local params here 1251 # #function here 1252 #""" 1253 1187 1254 CUSTOM_TEMPLATE = """ 1188 from sas.models.pluginmodel import Model1DPlugin1189 1255 from math import * 1190 1256 import os 1191 1257 import sys 1192 1258 import numpy 1193 #import scipy? 1194 class Model(Model1DPlugin): 1195 name = "" 1196 def __init__(self): 1197 Model1DPlugin.__init__(self, name=self.name) 1198 #set name same as file name 1199 self.name = self.get_fname() 1200 #self.params here 1201 self.description = "%s" 1202 self.set_details() 1203 def function(self, x=0.0%s): 1204 #local params here 1205 #function here 1259 1260 #name 1261 1262 #title 1263 1264 #description 1265 1266 #parameters 1267 1206 1268 """ 1269 1207 1270 CUSTOM_2D_TEMP = """ 1208 1271 def run(self, x=0.0, y=0.0):
Note: See TracChangeset
for help on using the changeset viewer.