Changes in / [6eaf6df1:5863b16] in sasview
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/sasgui/perspectives/calculator/model_editor.py
rcf92b1f r5876f7e 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 polydispersity749 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)])763 747 764 748 def _layout_function(self): … … 962 946 description = self.desc_tcl.GetValue() 963 947 param_str = self.param_tcl.GetText() 964 pd_param_str = self.pd_param_tcl.GetText()965 948 func_str = self.function_tcl.GetText() 966 949 # No input for the model function 967 950 if func_str.lstrip().rstrip(): 968 951 if func_str.count('return') > 0: 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 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 989 966 else: 990 967 msg = "Error: The func(x) must 'return' a value at least.\n" … … 994 971 else: 995 972 msg = "Name exists already." 996 997 973 # Prepare the messagebox 998 974 if self.base != None and not msg: 999 975 self.base.update_custom_combo() 1000 # Passed exception in import test as it will fail for sasmodels.sasview_model class1001 # Should add similar test for new style?1002 976 Model = None 1003 977 try: 1004 978 exec "from %s import Model" % name 1005 979 except: 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 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 1028 998 # Prepare the messagebox 1029 999 if msg: … … 1044 1014 self.warning = msg 1045 1015 1046 def write_file(self, fname, name, desc_str, param_str, pd_param_str, func_str): 1016 1017 def write_file(self, fname, desc_str, param_str, func_str): 1047 1018 """ 1048 1019 Write content in file … … 1051 1022 :param desc_str: content of the description strings 1052 1023 :param param_str: content of params; Strings 1053 :param pd_param_str: content of params requiring polydispersity; Strings1054 1024 :param func_str: content of func; Strings 1055 1025 """ … … 1062 1032 1063 1033 has_scipy = func_str.count("scipy.") 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 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 1085 1041 # write function here 1086 1042 for line in lines: 1087 1043 # The location where to put the strings is 1088 1044 # hard-coded in the template as shown below. 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') 1045 if line.count("#self.params here"): 1098 1046 for param_line in param_str.split('\n'): 1099 1047 p_line = param_line.lstrip().rstrip() 1100 1048 if p_line: 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)) 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") 1138 1089 1139 1090 out_f.close() 1140 1091 1141 def get_param_helper(self, line):1092 def set_param_helper(self, line): 1142 1093 """ 1143 1094 Get string in line to define the params dictionary … … 1145 1096 :param line: one line of string got from the param_str 1146 1097 """ 1098 params_str = '' 1099 spaces = ' '#8spaces 1147 1100 items = line.split(";") 1148 1101 for item in items: … … 1153 1106 except: 1154 1107 value = 1.0 # default 1155 1156 return name, value 1108 params_str += spaces + "self.params['%s'] = %s\n" % (name, value) 1109 1110 return params_str 1157 1111 1158 1112 def set_function_helper(self, line): … … 1231 1185 1232 1186 ## Templates for custom models 1233 #CUSTOM_TEMPLATE = """1234 #from sas.models.pluginmodel import Model1DPlugin1235 #from math import *1236 #import os1237 #import sys1238 #import numpy1239 ##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 name1245 # self.name = self.get_fname()1246 # #self.params here1247 # self.description = "%s"1248 # self.set_details()1249 # def function(self, x=0.0%s):1250 # #local params here1251 # #function here1252 #"""1253 1254 1187 CUSTOM_TEMPLATE = """ 1188 from sas.models.pluginmodel import Model1DPlugin 1255 1189 from math import * 1256 1190 import os 1257 1191 import sys 1258 1192 import numpy 1259 1260 #name 1261 1262 #title 1263 1264 #description 1265 1266 #parameters 1267 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 1268 1206 """ 1269 1270 1207 CUSTOM_2D_TEMP = """ 1271 1208 def run(self, x=0.0, y=0.0):
Note: See TracChangeset
for help on using the changeset viewer.