Changeset d970df9 in sasview for calculatorview/src
- Timestamp:
- Jun 15, 2012 3:23:58 PM (12 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:
- 4a47244
- Parents:
- d7b49576
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
calculatorview/src/sans/perspectives/calculator/model_editor.py
rfdef956 rd970df9 1 2 1 ################################################################################ 3 2 #This software was developed by the University of Tennessee as part of the … … 9 8 #copyright 2009, University of Tennessee 10 9 ################################################################################ 11 12 13 10 import wx 14 11 import sys … … 40 37 return '' 41 38 except: 42 type, value, traceback= sys.exc_info()39 _, value, _ = sys.exc_info() 43 40 return value 44 41 … … 57 54 Dialog for easy custom sum models 58 55 """ 59 def __init__(self, parent=None, base=None, id=None, title='', model_list=[], plugin_dir=None): 60 """ 61 Dialog window popup when selecting 'Easy Custom Sum/Multiply' on the menu 56 def __init__(self, parent=None, base=None, id=None, title='', 57 model_list=[], plugin_dir=None): 58 """ 59 Dialog window popup when selecting 'Easy Custom Sum/Multiply' 60 on the menu 62 61 """ 63 62 wx.Dialog.__init__(self, parent=parent, id=id, … … 75 74 self.model2_string = "CylinderModel" 76 75 self.name = 'Sum' + M_NAME 76 self.factor = 'scale_factor' 77 77 self._notes = '' 78 78 self.operator = '+' … … 81 81 self.explanationctr = None 82 82 self.sizer = None 83 self.name_sizer = None 84 self.name_hsizer = None 85 self.desc_sizer = None 86 self.desc_tcl = None 87 self.model1 = None 88 self.model2 = None 89 self.static_line_1 = None 90 self.okButton = None 91 self.closeButton = None 83 92 self._msg_box = None 84 93 self.msg_sizer = None 94 self.fname = None 95 self.cm_list = None 96 self.is_p1_custom = False 97 self.is_p2_custom = False 85 98 self._build_sizer() 86 99 self.model1_name = str(self.model1.GetValue()) … … 123 136 self.desc_tcl.SetToolTipString(hint_desc) 124 137 self.desc_sizer.AddMany([(desc_txt, 0, wx.LEFT|wx.TOP, 10), 125 126 138 (self.desc_tcl, -1, 139 wx.EXPAND|wx.RIGHT|wx.TOP|wx.BOTTOM, 10)]) 127 140 128 141 def _build_sizer(self): … … 137 150 138 151 139 sum_description = wx.StaticBox(self, -1, 'Select',152 sum_description = wx.StaticBox(self, -1, 'Select', 140 153 size=(PNL_WIDTH-30, 70)) 141 154 sum_box = wx.StaticBoxSizer(sum_description, wx.VERTICAL) … … 167 180 size=(box_width/2, 25)) 168 181 # Intro 169 self.explanation = " custom model = scale_factor * (model1 %s model2)\n"\170 182 self.explanation = " custom model = %s %s "% (self.factor, '*') 183 self.explanation += "(model1 %s model2)\n"% self.operator 171 184 #explanation += " Note: This will overwrite the previous sum model.\n" 172 185 model_string = " Model%s (p%s):" … … 183 196 self.sizer.Add(self.explanationctr , (iy, ix), 184 197 (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 185 model1_box.Add(wx.StaticText(self, -1, model_string% (1, 1)), -1, 0)186 model1_box.Add((box_width-15, 10))198 model1_box.Add(wx.StaticText(self, -1, model_string% (1, 1)), -1, 0) 199 model1_box.Add((box_width-15, 10)) 187 200 model1_box.Add(wx.StaticText(self, -1, model_string% (2, 2)), -1, 0) 188 201 model2_box.Add(self.model1, -1, 0) 189 model2_box.Add((15, 10))202 model2_box.Add((15, 10)) 190 203 model2_box.Add(self.operator_cbox, 0, 0) 191 model2_box.Add((15, 10))204 model2_box.Add((15, 10)) 192 205 model2_box.Add(self.model2, -1, 0) 193 206 model_vbox.Add(model1_box, -1, 0) … … 198 211 self.sizer.Add(sum_box, (iy, ix), 199 212 (1, 1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) 200 vbox.Add((10, 10))213 vbox.Add((10, 10)) 201 214 vbox.Add(self.static_line_1, 0, wx.EXPAND, 10) 202 215 vbox.Add(self.msg_sizer, 0, … … 226 239 Check name if exist already 227 240 """ 241 mname = M_NAME 228 242 self.on_change_name(None) 229 243 list_fnames = os.listdir(self.plugin_dir) 230 244 # fake existing regular model name list 245 m_list = [model + ".py" for model in self.model_list] 246 list_fnames.append(m_list) 231 247 # function/file name 232 248 title = self.name_tcl.GetValue().lstrip().rstrip() … … 234 250 text = self.operator_cbox.GetLabel().strip() 235 251 if text == '+': 236 name = 'Sum'252 mname = 'Sum' 237 253 else: 238 name = 'Multi'239 self.name = name +M_NAME240 title = self.name254 mname = 'Multi' 255 mname += M_NAME 256 title = mname 241 257 self.name = title 242 258 t_fname = title + '.py' 243 259 if not self.overwrite_name: 244 if t_fname in list_fnames and title != self.name:260 if t_fname in list_fnames and title != mname: 245 261 self.name_tcl.SetBackgroundColour('pink') 246 262 self.good_name = False … … 295 311 """ 296 312 # list of model names 313 cm_list = [] 314 # models 297 315 list = self.model_list 316 # custom models 317 al_list = os.listdir(self.plugin_dir) 318 for c_name in al_list: 319 if c_name.split('.')[-1] == 'py' and \ 320 c_name.split('.')[0] != '__init__': 321 name = str(c_name.split('.')[0]) 322 cm_list.append(name) 323 if name not in list: 324 list.append(name) 325 self.cm_list = cm_list 298 326 if len(list) > 1: 299 327 list.sort() 300 328 for idx in range(len(list)): 301 self.model1.Append( list[idx],idx)302 self.model2.Append( list[idx],idx)329 self.model1.Append(str(list[idx]), idx) 330 self.model2.Append(str(list[idx]), idx) 303 331 self.model1.SetStringSelection(self.model1_string) 304 332 self.model2.SetStringSelection(self.model2_string) 305 333 334 def update_cm_list(self): 335 """ 336 Update custom model list 337 """ 338 cm_list = [] 339 al_list = os.listdir(self.plugin_dir) 340 for c_name in al_list: 341 if c_name.split('.')[-1] == 'py' and \ 342 c_name.split('.')[0] != '__init__': 343 name = str(c_name.split('.')[0]) 344 cm_list.append(name) 345 self.cm_list = cm_list 346 306 347 def on_model1(self, event): 307 348 """ … … 309 350 """ 310 351 event.Skip() 352 self.update_cm_list() 311 353 self.model1_name = str(self.model1.GetValue()) 312 354 self.model1_string = self.model1_name 355 if self.model1_name in self.cm_list: 356 self.is_p1_custom = True 357 else: 358 self.is_p1_custom = False 313 359 314 360 def on_model2(self, event): … … 317 363 """ 318 364 event.Skip() 365 self.update_cm_list() 319 366 self.model2_name = str(self.model2.GetValue()) 320 367 self.model2_string = self.model2_name 368 if self.model2_name in self.cm_list: 369 self.is_p2_custom = True 370 else: 371 self.is_p2_custom = False 321 372 322 373 def on_select_operator(self, event=None): … … 328 379 if text == '+': 329 380 name = 'Sum' 381 factor = 'scale_factor' 382 f_oper = '*' 330 383 else: 331 384 name = 'Multi' 385 factor = 'BackGround' 386 f_oper = '+' 387 self.factor = str(factor) 332 388 self.operator = str(text) 333 self.explanation = " custom model = scale_factor * " 389 self.explanation = " custom model = %s %s "% (self.factor, 390 f_oper) 334 391 self.explanation += "(model1 %s model2)\n"% self.operator 335 392 self.explanationctr.SetLabel(self.explanation) … … 362 419 description = name1 + self.operator + name2 363 420 name = self.name_tcl.GetValue().lstrip().rstrip() 364 if name == '': 365 text = self.operator_cbox.GetLabel().strip() 366 if text == '+': 367 name = 'Sum' 368 else: 369 name = 'Multi' 370 self.name = name + M_NAME 421 text = self.operator_cbox.GetLabel().strip() 422 if text == '+': 423 factor = 'scale_factor' 424 f_oper = '*' 425 default_val = '1.0' 426 else: 427 factor = 'BackGround' 428 f_oper = '+' 429 default_val = '0.0' 371 430 path = self.fname 372 431 try: … … 376 435 lines = SUM_TEMPLATE.split('\n') 377 436 for line in lines: 378 if line.count("import %s as P1"): 379 out_f.write(line % (name1, name1) + "\n") 380 elif line.count("import %s as P2"): 381 out_f.write(line % (name2, name2) + "\n") 382 elif line.count("self.description = '%s'"): 383 out_f.write(line % description + "\n") 384 elif line.count("run") and line.count("%s"): 385 out_f.write(line % self.operator) 386 elif line.count("evalDistribution") and line.count("%s"): 387 out_f.write(line % self.operator) 388 else: 389 out_f.write(line + "\n") 437 try: 438 if line.count("scale_factor"): 439 line = line.replace('scale_factor', factor) 440 #print "scale_factor", line 441 if line.count("= %s"): 442 out_f.write(line % (default_val) + "\n") 443 elif line.count("import Model as P1"): 444 if self.is_p1_custom: 445 line = line.replace('#', '') 446 out_f.write(line % name1 + "\n") 447 else: 448 out_f.write(line + "\n") 449 elif line.count("import %s as P1"): 450 if not self.is_p1_custom: 451 line = line.replace('#', '') 452 out_f.write(line % (name1, name1) + "\n") 453 else: 454 out_f.write(line + "\n") 455 elif line.count("import Model as P2"): 456 if self.is_p2_custom: 457 line = line.replace('#', '') 458 out_f.write(line % name2 + "\n") 459 else: 460 out_f.write(line + "\n") 461 elif line.count("import %s as P2"): 462 if not self.is_p2_custom: 463 line = line.replace('#', '') 464 out_f.write(line % (name2, name2) + "\n") 465 else: 466 out_f.write(line + "\n") 467 elif line.count("self.description = '%s'"): 468 out_f.write(line % description + "\n") 469 #elif line.count("run") and line.count("%s"): 470 # out_f.write(line % self.operator + "\n") 471 #elif line.count("evalDistribution") and line.count("%s"): 472 # out_f.write(line % self.operator + "\n") 473 elif line.count("return") and line.count("%s") == 2: 474 #print "line return", line 475 out_f.write(line % (f_oper, self.operator) + "\n") 476 elif line.count("out2")and line.count("%s"): 477 out_f.write(line % self.operator + "\n") 478 else: 479 out_f.write(line + "\n") 480 except: 481 raise 390 482 out_f.close() 391 483 #else: … … 413 505 kwds['name'] = title 414 506 kwds["size"] = (EDITOR_WIDTH, EDITOR_HEIGTH) 415 kwds["style"] = wx.FULL_REPAINT_ON_RESIZE507 kwds["style"] = wx.FULL_REPAINT_ON_RESIZE 416 508 wx.ScrolledWindow.__init__(self, parent, *args, **kwds) 417 509 #self.SetupScrolling() … … 814 906 815 907 class EditorWindow(wx.Frame): 908 """ 909 Editor Window 910 """ 816 911 def __init__(self, parent, base, path, title, 817 912 size=(EDITOR_WIDTH, EDITOR_HEIGTH), *args, **kwds): 913 """ 914 Init 915 """ 818 916 kwds["title"] = title 819 917 kwds["size"] = size … … 919 1017 # User can change the name of the model (only with single functional model) 920 1018 #P1_model: 921 from sans.models.%s import %s as P1 1019 #from sans.models.%s import %s as P1 1020 #from %s import Model as P1 922 1021 923 1022 #P2_model: 924 from sans.models.%s import %s as P2 1023 #from sans.models.%s import %s as P2 1024 #from %s import Model as P2 925 1025 import os 926 1026 import sys … … 961 1061 ## Define parameters 962 1062 self._set_params() 963 ## New parameter: Scalingfactor964 self.params['scale_factor'] = 11063 ## New parameter:scaling_factor 1064 self.params['scale_factor'] = %s 965 1065 966 1066 ## Parameter details [units, min, max] … … 1152 1252 def run(self, x = 0.0): 1153 1253 self._set_scale_factor() 1154 return self.params['scale_factor'] *\1254 return self.params['scale_factor'] %s \ 1155 1255 (self.p_model1.run(x) %s self.p_model2.run(x)) 1156 1256 1157 1257 def runXY(self, x = 0.0): 1158 1258 self._set_scale_factor() 1159 return self.params['scale_factor'] *\1259 return self.params['scale_factor'] %s \ 1160 1260 (self.p_model1.runXY(x) %s self.p_model2.runXY(x)) 1161 1261 … … 1164 1264 def evalDistribution(self, x = []): 1165 1265 self._set_scale_factor() 1166 return self.params['scale_factor'] *\1266 return self.params['scale_factor'] %s \ 1167 1267 (self.p_model1.evalDistribution(x) %s \ 1168 1268 self.p_model2.evalDistribution(x))
Note: See TracChangeset
for help on using the changeset viewer.