Changeset 49ab5d7 in sasview for src/sas/perspectives/calculator/model_editor.py
- Timestamp:
- Mar 4, 2015 3:28:39 PM (10 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:
- 2f732b0
- Parents:
- 76aed53
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/perspectives/calculator/model_editor.py
r79492222 r49ab5d7 28 28 _BOX_WIDTH = 55 29 29 30 30 31 31 def _compileFile(path): 32 32 """ … … 40 40 _, value, _ = sys.exc_info() 41 41 return value 42 42 43 43 def _deleteFile(path): 44 44 """ … … 50 50 raise 51 51 52 52 53 53 class TextDialog(wx.Dialog): 54 54 """ 55 Dialog for easy custom sum models 55 Dialog for easy custom sum models 56 56 """ 57 def __init__(self, parent=None, base=None, id=None, title='', 57 def __init__(self, parent=None, base=None, id=None, title='', 58 58 model_list=[], plugin_dir=None): 59 59 """ 60 Dialog window popup when selecting 'Easy Custom Sum/Multiply' 60 Dialog window popup when selecting 'Easy Custom Sum/Multiply' 61 61 on the menu 62 62 """ 63 wx.Dialog.__init__(self, parent=parent, id=id, 63 wx.Dialog.__init__(self, parent=parent, id=id, 64 64 title=title, size=(PNL_WIDTH, PNL_HITE)) 65 65 self.parent = base … … 102 102 self.good_name = True 103 103 self.fill_oprator_combox() 104 104 105 105 def _layout_name(self): 106 106 """ … … 110 110 self.name_hsizer = wx.BoxSizer(wx.HORIZONTAL) 111 111 #title name [string] 112 name_txt = wx.StaticText(self, -1, 'Function Name : ') 113 self.name_tcl = wx.TextCtrl(self, -1, size=(PANEL_WIDTH *3/5, -1))112 name_txt = wx.StaticText(self, -1, 'Function Name : ') 113 self.name_tcl = wx.TextCtrl(self, -1, size=(PANEL_WIDTH * 3 / 5, -1)) 114 114 self.name_tcl.Bind(wx.EVT_TEXT_ENTER, self.on_change_name) 115 115 self.name_tcl.SetValue('') … … 117 117 hint_name = "Unique Sum/Multiply Model Function Name." 118 118 self.name_tcl.SetToolTipString(hint_name) 119 self.name_hsizer.AddMany([(name_txt, 0, wx.LEFT |wx.TOP, 10),120 (self.name_tcl, -1, 121 wx.EXPAND |wx.RIGHT|wx.TOP|wx.BOTTOM, 10)])122 self.name_sizer.AddMany([(self.name_hsizer, -1, 123 wx.LEFT |wx.TOP, 10)])124 125 119 self.name_hsizer.AddMany([(name_txt, 0, wx.LEFT | wx.TOP, 10), 120 (self.name_tcl, -1, 121 wx.EXPAND | wx.RIGHT | wx.TOP | wx.BOTTOM, 10)]) 122 self.name_sizer.AddMany([(self.name_hsizer, -1, 123 wx.LEFT | wx.TOP, 10)]) 124 125 126 126 def _layout_description(self): 127 127 """ … … 130 130 self.desc_sizer = wx.BoxSizer(wx.HORIZONTAL) 131 131 #title name [string] 132 desc_txt = wx.StaticText(self, -1, 'Description (optional) : ') 133 self.desc_tcl = wx.TextCtrl(self, -1, size=(PANEL_WIDTH *3/5, -1))132 desc_txt = wx.StaticText(self, -1, 'Description (optional) : ') 133 self.desc_tcl = wx.TextCtrl(self, -1, size=(PANEL_WIDTH * 3 / 5, -1)) 134 134 self.desc_tcl.SetValue('') 135 135 #self.name_tcl.SetFont(self.font) 136 136 hint_desc = "Write a short description of this model function." 137 137 self.desc_tcl.SetToolTipString(hint_desc) 138 self.desc_sizer.AddMany([(desc_txt, 0, wx.LEFT |wx.TOP, 10),139 (self.desc_tcl, -1, 140 wx.EXPAND |wx.RIGHT|wx.TOP|wx.BOTTOM, 10)])141 138 self.desc_sizer.AddMany([(desc_txt, 0, wx.LEFT | wx.TOP, 10), 139 (self.desc_tcl, -1, 140 wx.EXPAND | wx.RIGHT | wx.TOP | wx.BOTTOM, 10)]) 141 142 142 def _build_sizer(self): 143 143 """ … … 145 145 """ 146 146 box_width = 195 # combobox width 147 vbox 147 vbox = wx.BoxSizer(wx.VERTICAL) 148 148 self.sizer = wx.GridBagSizer(1, 3) 149 149 self._layout_name() 150 150 self._layout_description() 151 152 153 sum_description = wx.StaticBox(self, -1, 'Select', 154 size=(PNL_WIDTH -30, 70))151 152 153 sum_description = wx.StaticBox(self, -1, 'Select', 154 size=(PNL_WIDTH - 30, 70)) 155 155 sum_box = wx.StaticBoxSizer(sum_description, wx.VERTICAL) 156 156 model1_box = wx.BoxSizer(wx.HORIZONTAL) 157 157 model2_box = wx.BoxSizer(wx.HORIZONTAL) 158 158 model_vbox = wx.BoxSizer(wx.VERTICAL) 159 self.model1 = 159 self.model1 = wx.ComboBox(self, -1, style=wx.CB_READONLY) 160 160 wx.EVT_COMBOBOX(self.model1, -1, self.on_model1) 161 self.model1.SetMinSize((box_width *5/6, -1))161 self.model1.SetMinSize((box_width * 5 / 6, -1)) 162 162 self.model1.SetToolTipString("model1") 163 164 self.operator_cbox = wx.ComboBox(self, -1, size=(50, -1), 163 164 self.operator_cbox = wx.ComboBox(self, -1, size=(50, -1), 165 165 style=wx.CB_READONLY) 166 166 wx.EVT_COMBOBOX(self.operator_cbox, -1, self.on_select_operator) 167 167 operation_tip = "Add: +, Multiply: * " 168 168 self.operator_cbox.SetToolTipString(operation_tip) 169 170 self.model2 = 169 170 self.model2 = wx.ComboBox(self, -1, style=wx.CB_READONLY) 171 171 wx.EVT_COMBOBOX(self.model2, -1, self.on_model2) 172 self.model2.SetMinSize((box_width *5/6, -1))172 self.model2.SetMinSize((box_width * 5 / 6, -1)) 173 173 self.model2.SetToolTipString("model2") 174 174 self._set_model_list() 175 175 176 176 # Buttons on the bottom 177 177 self.static_line_1 = wx.StaticLine(self, -1) 178 self.okButton = wx.Button(self, wx.ID_OK, 'Apply', size=(box_width/2, 25))178 self.okButton = wx.Button(self, wx.ID_OK, 'Apply', size=(box_width / 2, 25)) 179 179 self.okButton.Bind(wx.EVT_BUTTON, self.check_name) 180 self.closeButton = wx.Button(self, wx.ID_CANCEL, 'Close',181 size=(box_width /2, 25))180 self.closeButton = wx.Button(self, wx.ID_CANCEL, 'Close', 181 size=(box_width / 2, 25)) 182 182 # Intro 183 self.explanation = " custom model = %s %s "% (self.factor, '*')184 self.explanation += "(model1 %s model2)\n"% self.operator183 self.explanation = " custom model = %s %s " % (self.factor, '*') 184 self.explanation += "(model1 %s model2)\n" % self.operator 185 185 #explanation += " Note: This will overwrite the previous sum model.\n" 186 186 model_string = " Model%s (p%s):" … … 196 196 self.explanationctr = wx.StaticText(self, -1, self.explanation) 197 197 self.sizer.Add(self.explanationctr , (iy, ix), 198 (1, 1), wx.LEFT |wx.EXPAND|wx.ADJUST_MINSIZE, 15)199 model1_box.Add(wx.StaticText(self, -1, model_string % (1, 1)), -1, 0)200 model1_box.Add((box_width -15, 10))201 model1_box.Add(wx.StaticText(self, -1, model_string % (2, 2)), -1, 0)198 (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) 199 model1_box.Add(wx.StaticText(self, -1, model_string % (1, 1)), -1, 0) 200 model1_box.Add((box_width - 15, 10)) 201 model1_box.Add(wx.StaticText(self, -1, model_string % (2, 2)), -1, 0) 202 202 model2_box.Add(self.model1, -1, 0) 203 203 model2_box.Add((15, 10)) … … 211 211 ix = 0 212 212 self.sizer.Add(sum_box, (iy, ix), 213 (1, 1), wx.LEFT |wx.EXPAND|wx.ADJUST_MINSIZE, 15)213 (1, 1), wx.LEFT | wx.EXPAND | wx.ADJUST_MINSIZE, 15) 214 214 vbox.Add((10, 10)) 215 215 vbox.Add(self.static_line_1, 0, wx.EXPAND, 10) 216 vbox.Add(self.msg_sizer, 0, 217 wx.LEFT |wx.RIGHT|wx.ADJUST_MINSIZE|wx.BOTTOM, 10)216 vbox.Add(self.msg_sizer, 0, 217 wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE | wx.BOTTOM, 10) 218 218 sizer_button = wx.BoxSizer(wx.HORIZONTAL) 219 sizer_button.Add((20, 20), 1, wx.EXPAND |wx.ADJUST_MINSIZE, 0)220 sizer_button.Add(self.okButton, 0, 221 wx.LEFT |wx.RIGHT|wx.ADJUST_MINSIZE, 0)219 sizer_button.Add((20, 20), 1, wx.EXPAND | wx.ADJUST_MINSIZE, 0) 220 sizer_button.Add(self.okButton, 0, 221 wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE, 0) 222 222 sizer_button.Add(self.closeButton, 0, 223 wx.LEFT |wx.RIGHT|wx.ADJUST_MINSIZE, 10)224 vbox.Add(sizer_button, 0, wx.EXPAND |wx.BOTTOM|wx.TOP, 10)225 223 wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE, 10) 224 vbox.Add(sizer_button, 0, wx.EXPAND | wx.BOTTOM | wx.TOP, 10) 225 226 226 self.SetSizer(vbox) 227 227 self.Centre() 228 228 229 229 def on_change_name(self, event=None): 230 230 """ … … 235 235 self.name_tcl.SetBackgroundColour('white') 236 236 self.Refresh() 237 237 238 238 def check_name(self, event=None): 239 239 """ … … 264 264 info = 'Error' 265 265 msg = "Name exists already." 266 wx.MessageBox(msg, info) 266 wx.MessageBox(msg, info) 267 267 self._notes = msg 268 268 color = 'red' … … 278 278 self.on_apply(self.fname) 279 279 return self.good_name 280 280 281 281 def on_apply(self, path): 282 282 """ … … 295 295 color = 'blue' 296 296 except: 297 msg = "Easy Custom Sum/Multipy: Error occurred..."297 msg = "Easy Custom Sum/Multipy: Error occurred..." 298 298 info = 'Error' 299 299 color = 'red' … … 301 301 self._msg_box.SetForegroundColour(color) 302 302 if self.parent.parent != None: 303 from sas.guiframe.events import StatusEvent 304 wx.PostEvent(self.parent.parent, StatusEvent(status = msg,303 from sas.guiframe.events import StatusEvent 304 wx.PostEvent(self.parent.parent, StatusEvent(status=msg, 305 305 info=info)) 306 306 else: 307 307 raise 308 308 309 309 def _set_model_list(self): 310 310 """ … … 328 328 list.sort() 329 329 for idx in range(len(list)): 330 self.model1.Append(str(list[idx]), idx) 330 self.model1.Append(str(list[idx]), idx) 331 331 self.model2.Append(str(list[idx]), idx) 332 332 self.model1.SetStringSelection(self.model1_string) 333 333 self.model2.SetStringSelection(self.model2_string) 334 334 335 335 def update_cm_list(self): 336 336 """ … … 344 344 name = str(c_name.split('.')[0]) 345 345 cm_list.append(name) 346 self.cm_list = cm_list 347 346 self.cm_list = cm_list 347 348 348 def on_model1(self, event): 349 349 """ … … 358 358 else: 359 359 self.is_p1_custom = False 360 360 361 361 def on_model2(self, event): 362 362 """ … … 371 371 else: 372 372 self.is_p2_custom = False 373 373 374 374 def on_select_operator(self, event=None): 375 375 """ … … 379 379 if event != None: 380 380 event.Skip() 381 name = '' 381 name = '' 382 382 item = event.GetEventObject() 383 383 text = item.GetValue() … … 393 393 self.factor = factor 394 394 self.operator = text 395 self.explanation = " Custom Model = %s %s (model1 %s model2)\n" % \395 self.explanation = " Custom Model = %s %s (model1 %s model2)\n" % \ 396 396 (self.factor, f_oper, self.operator) 397 397 self.explanationctr.SetLabel(self.explanation) 398 self.name = name + M_NAME 398 self.name = name + M_NAME 399 399 self.sizer.Layout() 400 400 401 401 def fill_oprator_combox(self): 402 402 """ 403 403 fill the current combobox with the operator 404 """ 404 """ 405 405 operator_list = [' +', ' *'] 406 406 for oper in operator_list: … … 408 408 self.operator_cbox.SetClientData(pos, str(oper.strip())) 409 409 self.operator_cbox.SetSelection(0) 410 410 411 411 def getText(self): 412 412 """ … … 414 414 """ 415 415 return [self.model1_name, self.model2_name] 416 416 417 417 def write_string(self, fname, name1, name2): 418 418 """ 419 419 Write and Save file 420 420 """ 421 self.fname = fname 421 self.fname = fname 422 422 description = self.desc_tcl.GetValue().lstrip().rstrip() 423 423 if description == '': … … 435 435 path = self.fname 436 436 try: 437 out_f = open(path,'w')437 out_f = open(path, 'w') 438 438 except : 439 439 raise … … 488 488 #else: 489 489 # msg = "Name exists already." 490 490 491 491 def compile_file(self, path): 492 492 """ … … 495 495 path = self.fname 496 496 _compileFile(path) 497 497 498 498 def delete_file(self, path): 499 499 """ … … 535 535 #self.bt_apply.Disable() 536 536 537 537 538 538 def _define_structure(self): 539 539 """ 540 define initial sizer 540 define initial sizer 541 541 """ 542 542 #w, h = self.parent.GetSize() … … 550 550 self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) 551 551 self.msg_sizer = wx.BoxSizer(wx.HORIZONTAL) 552 552 553 553 def _layout_name(self): 554 554 """ … … 556 556 """ 557 557 #title name [string] 558 name_txt = wx.StaticText(self, -1, 'Function Name : ') 558 name_txt = wx.StaticText(self, -1, 'Function Name : ') 559 559 overwrite_cb = wx.CheckBox(self, -1, "Overwrite?", (10, 10)) 560 560 overwrite_cb.SetValue(False) … … 562 562 wx.EVT_CHECKBOX(self, overwrite_cb.GetId(), self.on_over_cb) 563 563 #overwrite_cb.Show(False) 564 self.name_tcl = wx.TextCtrl(self, -1, size=(PANEL_WIDTH *3/5, -1))564 self.name_tcl = wx.TextCtrl(self, -1, size=(PANEL_WIDTH * 3 / 5, -1)) 565 565 self.name_tcl.Bind(wx.EVT_TEXT_ENTER, self.on_change_name) 566 566 self.name_tcl.SetValue('MyFunction') … … 568 568 hint_name = "Unique Model Function Name." 569 569 self.name_tcl.SetToolTipString(hint_name) 570 self.name_hsizer.AddMany([(self.name_tcl, 0, wx.LEFT |wx.TOP, 0),570 self.name_hsizer.AddMany([(self.name_tcl, 0, wx.LEFT | wx.TOP, 0), 571 571 (overwrite_cb, 0, wx.LEFT, 20)]) 572 self.name_sizer.AddMany([(name_txt, 0, wx.LEFT |wx.TOP, 10),573 (self.name_hsizer, 0, 574 wx.LEFT |wx.TOP|wx.BOTTOM, 10)])575 576 572 self.name_sizer.AddMany([(name_txt, 0, wx.LEFT | wx.TOP, 10), 573 (self.name_hsizer, 0, 574 wx.LEFT | wx.TOP | wx.BOTTOM, 10)]) 575 576 577 577 def _layout_description(self): 578 578 """ … … 580 580 """ 581 581 #title name [string] 582 desc_txt = wx.StaticText(self, -1, 'Description (optional) : ') 583 self.desc_tcl = wx.TextCtrl(self, -1, size=(PANEL_WIDTH *3/5, -1))582 desc_txt = wx.StaticText(self, -1, 'Description (optional) : ') 583 self.desc_tcl = wx.TextCtrl(self, -1, size=(PANEL_WIDTH * 3 / 5, -1)) 584 584 self.desc_tcl.SetValue('') 585 585 #self.name_tcl.SetFont(self.font) 586 586 hint_desc = "Write a short description of the model function." 587 587 self.desc_tcl.SetToolTipString(hint_desc) 588 self.desc_sizer.AddMany([(desc_txt, 0, wx.LEFT |wx.TOP, 10),589 (self.desc_tcl, 0, 590 wx.LEFT |wx.TOP|wx.BOTTOM, 10)])588 self.desc_sizer.AddMany([(desc_txt, 0, wx.LEFT | wx.TOP, 10), 589 (self.desc_tcl, 0, 590 wx.LEFT | wx.TOP | wx.BOTTOM, 10)]) 591 591 def _layout_param(self): 592 592 """ 593 593 Do the layout for parameter related widgets 594 594 """ 595 param_txt = wx.StaticText(self, -1, 'Fit Parameters (if any): ') 596 595 param_txt = wx.StaticText(self, -1, 'Fit Parameters (if any): ') 596 597 597 param_tip = "#Set the parameters and initial values.\n" 598 598 param_tip += "#Example:\n" 599 599 param_tip += "A = 1\nB = 1" 600 600 #param_txt.SetToolTipString(param_tip) 601 id = wx.NewId()602 self.param_tcl = EditWindow(self, id, wx.DefaultPosition, 603 wx.DefaultSize, wx.CLIP_CHILDREN |wx.SUNKEN_BORDER)601 id = wx.NewId() 602 self.param_tcl = EditWindow(self, id, wx.DefaultPosition, 603 wx.DefaultSize, wx.CLIP_CHILDREN | wx.SUNKEN_BORDER) 604 604 self.param_tcl.setDisplayLineNumbers(True) 605 605 self.param_tcl.SetToolTipString(param_tip) 606 606 607 607 self.param_sizer.AddMany([(param_txt, 0, wx.LEFT, 10), 608 (self.param_tcl, 1, wx.EXPAND |wx.ALL, 10)])609 608 (self.param_tcl, 1, wx.EXPAND | wx.ALL, 10)]) 609 610 610 def _layout_function(self): 611 611 """ 612 612 Do the layout for function related widgets 613 613 """ 614 function_txt = wx.StaticText(self, -1, 'Function(x) : ') 614 function_txt = wx.StaticText(self, -1, 'Function(x) : ') 615 615 hint_function = "#Example:\n" 616 616 hint_function += "if x <= 0:\n" … … 621 621 math_txt = wx.StaticText(self, -1, '*Useful math functions: ') 622 622 math_combo = self._fill_math_combo() 623 624 id = wx.NewId()625 self.function_tcl = EditWindow(self, id, wx.DefaultPosition, 626 wx.DefaultSize, wx.CLIP_CHILDREN |wx.SUNKEN_BORDER)623 624 id = wx.NewId() 625 self.function_tcl = EditWindow(self, id, wx.DefaultPosition, 626 wx.DefaultSize, wx.CLIP_CHILDREN | wx.SUNKEN_BORDER) 627 627 self.function_tcl.setDisplayLineNumbers(True) 628 628 self.function_tcl.SetToolTipString(hint_function) 629 629 630 630 self.func_horizon_sizer.Add(function_txt) 631 631 self.func_horizon_sizer.Add(math_txt, 0, wx.LEFT, 250) … … 633 633 634 634 self.function_sizer.Add(self.func_horizon_sizer, 0, wx.LEFT, 10) 635 self.function_sizer.Add( self.function_tcl, 1, wx.EXPAND|wx.ALL, 10)636 635 self.function_sizer.Add(self.function_tcl, 1, wx.EXPAND | wx.ALL, 10) 636 637 637 def _layout_msg(self): 638 638 """ 639 639 Layout msg 640 640 """ 641 self._msg_box = wx.StaticText(self, -1, self._notes, 641 self._msg_box = wx.StaticText(self, -1, self._notes, 642 642 size=(PANEL_WIDTH, -1)) 643 self.msg_sizer.Add(self._msg_box, 0, wx.LEFT, 10) 644 645 def _layout_button(self): 643 self.msg_sizer.Add(self._msg_box, 0, wx.LEFT, 10) 644 645 def _layout_button(self): 646 646 """ 647 647 Do the layout for the button widgets 648 """ 648 """ 649 649 self.bt_apply = wx.Button(self, -1, "Apply", size=(_BOX_WIDTH, -1)) 650 650 self.bt_apply.SetToolTipString("Save changes into the imported data.") 651 651 self.bt_apply.Bind(wx.EVT_BUTTON, self.on_click_apply) 652 652 653 653 self.bt_close = wx.Button(self, -1, 'Close', size=(_BOX_WIDTH, -1)) 654 654 self.bt_close.Bind(wx.EVT_BUTTON, self.on_close) 655 655 self.bt_close.SetToolTipString("Close this panel.") 656 657 self.button_sizer.AddMany([(self.bt_apply, 0, 656 657 self.button_sizer.AddMany([(self.bt_apply, 0, 658 658 wx.LEFT, EDITOR_WIDTH * 0.8), 659 (self.bt_close, 0, 660 wx.LEFT |wx.BOTTOM, 15)])661 659 (self.bt_close, 0, 660 wx.LEFT | wx.BOTTOM, 15)]) 661 662 662 def _do_layout(self): 663 663 """ … … 671 671 self._layout_msg() 672 672 self._layout_button() 673 self.main_sizer.AddMany([(self.name_sizer, 0, 674 wx.EXPAND |wx.ALL, 5),675 (wx.StaticLine(self), 0, 676 wx.ALL |wx.EXPAND, 5),677 (self.desc_sizer, 0, 678 wx.EXPAND |wx.ALL, 5),679 (wx.StaticLine(self), 0, 680 wx.ALL |wx.EXPAND, 5),673 self.main_sizer.AddMany([(self.name_sizer, 0, 674 wx.EXPAND | wx.ALL, 5), 675 (wx.StaticLine(self), 0, 676 wx.ALL | wx.EXPAND, 5), 677 (self.desc_sizer, 0, 678 wx.EXPAND | wx.ALL, 5), 679 (wx.StaticLine(self), 0, 680 wx.ALL | wx.EXPAND, 5), 681 681 (self.param_sizer, 1, 682 wx.EXPAND |wx.ALL, 5),683 (wx.StaticLine(self), 0, 684 wx.ALL |wx.EXPAND, 5),682 wx.EXPAND | wx.ALL, 5), 683 (wx.StaticLine(self), 0, 684 wx.ALL | wx.EXPAND, 5), 685 685 (self.function_sizer, 2, 686 wx.EXPAND |wx.ALL, 5),687 (wx.StaticLine(self), 0, 688 wx.ALL |wx.EXPAND, 5),689 (self.msg_sizer, 0, 690 wx.EXPAND |wx.ALL, 5),686 wx.EXPAND | wx.ALL, 5), 687 (wx.StaticLine(self), 0, 688 wx.ALL | wx.EXPAND, 5), 689 (self.msg_sizer, 0, 690 wx.EXPAND | wx.ALL, 5), 691 691 (self.button_sizer, 0, 692 wx.EXPAND |wx.ALL, 5)])692 wx.EXPAND | wx.ALL, 5)]) 693 693 self.SetSizer(self.main_sizer) 694 694 self.SetAutoLayout(True) … … 698 698 Fill up the math combo box 699 699 """ 700 self.math_combo = wx.ComboBox(self, -1, size=(100, -1), 701 style=wx.CB_READONLY) 700 self.math_combo = wx.ComboBox(self, -1, size=(100, -1), 701 style=wx.CB_READONLY) 702 702 for item in dir(math): 703 703 if item.count("_") < 1: 704 704 try: 705 exec "float(math.%s)" % item705 exec "float(math.%s)" % item 706 706 self.math_combo.Append(str(item)) 707 707 except: 708 self.math_combo.Append(str(item) + "()" 708 self.math_combo.Append(str(item) + "()") 709 709 self.math_combo.Bind(wx.EVT_COMBOBOX, self._on_math_select) 710 710 self.math_combo.SetSelection(0) 711 711 return self.math_combo 712 712 713 713 def _on_math_select(self, event): 714 714 """ … … 716 716 """ 717 717 event.Skip() 718 label = self.math_combo.GetLabel() 718 label = self.math_combo.GetLabel() 719 719 self.function_tcl.SetFocus() 720 720 # Put the text at the cursor position 721 721 pos = self.function_tcl.GetCurrentPos() 722 self.function_tcl.InsertText(pos, label) 722 self.function_tcl.InsertText(pos, label) 723 723 # Put the cursor at appropriate postion 724 724 length = len(label) … … 726 726 length -= 1 727 727 f_pos = pos + length 728 self.function_tcl.GotoPos(f_pos) 728 self.function_tcl.GotoPos(f_pos) 729 729 730 730 def get_notes(self): … … 733 733 """ 734 734 return self._notes 735 735 736 736 def on_change_name(self, event=None): 737 737 """ … … 742 742 self.name_tcl.SetBackgroundColour('white') 743 743 self.Refresh() 744 744 745 745 def check_name(self): 746 746 """ … … 766 766 self._notes += "to %s. \n" % str(s_title) 767 767 return True 768 768 769 769 def on_over_cb(self, event): 770 770 """ … … 775 775 cb = event.GetEventObject() 776 776 self.overwrite_name = cb.GetValue() 777 777 778 778 def on_click_apply(self, event): 779 """ 779 """ 780 780 Changes are saved in data object imported to edit 781 781 """ … … 791 791 func_str = self.function_tcl.GetText() 792 792 # No input for the model function 793 if func_str.lstrip().rstrip(): 793 if func_str.lstrip().rstrip(): 794 794 if func_str.count('return') > 0: 795 self.write_file(self.fname, description, param_str, 795 self.write_file(self.fname, description, param_str, 796 796 func_str) 797 797 tr_msg = _compileFile(self.fname) … … 800 800 if msg: 801 801 msg.replace(" ", "\n") 802 msg += 802 msg += "\nCompiling Failed" 803 803 else: 804 804 msg = "Error: The func(x) must 'return' a value at least.\n" … … 811 811 if self.base != None and not msg: 812 812 self.base.update_custom_combo() 813 Model 814 exec "from %s import Model" % name813 Model = None 814 exec "from %s import Model" % name 815 815 try: 816 Model().run(0.01) 816 Model().run(0.01) 817 817 except: 818 818 msg = "Error " 819 819 _, value, _ = sys.exc_info() 820 msg += "in %s:\n%s\n" % (name, 820 msg += "in %s:\n%s\n" % (name, value) 821 821 if msg: 822 822 info = 'Error' 823 color = 'red' 823 color = 'red' 824 824 try: 825 825 # try to remove pyc file if exists … … 843 843 # Send msg to the top window 844 844 if self.base != None: 845 from sas.guiframe.events import StatusEvent 846 wx.PostEvent(self.base.parent, StatusEvent(status = msg,845 from sas.guiframe.events import StatusEvent 846 wx.PostEvent(self.base.parent, StatusEvent(status=msg, 847 847 info=info)) 848 848 self.warning = msg 849 849 850 851 def write_file(self, fname, desc_str, param_str, func_str): 850 851 def write_file(self, fname, desc_str, param_str, func_str): 852 852 """ 853 853 Write content in file 854 854 855 855 :param fname: full file path 856 856 :param desc_str: content of the description strings 857 :param param_str: content of params; Strings 857 :param param_str: content of params; Strings 858 858 :param func_str: content of func; Strings 859 """ 859 """ 860 860 try: 861 out_f = open(fname,'w')861 out_f = open(fname, 'w') 862 862 except : 863 863 raise … … 890 890 des0 = self.name + "\\n" 891 891 desc = str(desc_str.lstrip().rstrip().replace('\"', '')) 892 out_f.write(line % (des0 + desc) + "\n")892 out_f.write(line % (des0 + desc) + "\n") 893 893 elif line.count("def function(self, x=0.0%s):"): 894 894 if self.is_2d: 895 895 y_str = ', y=0.0' 896 out_f.write(line % y_str + "\n")896 out_f.write(line % y_str + "\n") 897 897 else: 898 out_f.write(line % '' + "\n")898 out_f.write(line % '' + "\n") 899 899 elif line.count("#function here"): 900 900 for func_line in func_str.split('\n'): … … 906 906 if self.is_2d: 907 907 dep_var = 'z' 908 out_f.write(spaces + 'return %s' % dep_var + "\n")908 out_f.write(spaces + 'return %s' % dep_var + "\n") 909 909 elif line.count("#import scipy?"): 910 910 if has_scipy: … … 921 921 for line in line_test: 922 922 out_f.write(line + "\n") 923 924 out_f.close() 925 926 def set_param_helper(self, line): 923 924 out_f.close() 925 926 def set_param_helper(self, line): 927 927 """ 928 928 Get string in line to define the params dictionary 929 929 930 930 :param line: one line of string got from the param_str 931 931 """ … … 941 941 except: 942 942 value = 1.0 # default 943 params_str += spaces + "self.params['%s'] = %s\n" % (name, value)944 943 params_str += spaces + "self.params['%s'] = %s\n" % (name, value) 944 945 945 return params_str 946 946 947 def set_function_helper(self, line): 947 def set_function_helper(self, line): 948 948 """ 949 949 Get string in line to define the local params 950 950 951 951 :param line: one line of string got from the param_str 952 952 """ … … 957 957 for item in items: 958 958 name = item.split("=")[0].lstrip().rstrip() 959 params_str += spaces + "%s = self.params['%s']\n" % (name, name)959 params_str += spaces + "%s = self.params['%s']\n" % (name, name) 960 960 return params_str 961 961 962 962 def get_warning(self): 963 963 """ 964 Get the warning msg 964 Get the warning msg 965 965 """ 966 966 return self.warning 967 967 968 968 def on_close(self, event): 969 969 """ … … 972 972 self.parent.Show(False)#Close() 973 973 event.Skip() 974 974 975 975 class EditorWindow(wx.Frame): 976 976 """ 977 977 Editor Window 978 978 """ 979 def __init__(self, parent, base, path, title, 979 def __init__(self, parent, base, path, title, 980 980 size=(EDITOR_WIDTH, EDITOR_HEIGTH), *args, **kwds): 981 981 """ … … 986 986 wx.Frame.__init__(self, parent=None, *args, **kwds) 987 987 self.parent = parent 988 self.panel = EditorPanel(parent=self, base=parent, 988 self.panel = EditorPanel(parent=self, base=parent, 989 989 path=path, title=title) 990 990 self.Show(True) 991 991 wx.EVT_CLOSE(self, self.OnClose) 992 993 def OnClose(self, event): 992 993 def OnClose(self, event): 994 994 """ 995 995 On close event … … 1009 1009 #import scipy? 1010 1010 class Model(Model1DPlugin): 1011 name = "" 1011 name = "" 1012 1012 def __init__(self): 1013 Model1DPlugin.__init__(self, name=self.name) 1014 #set name same as file name 1015 self.name = self.get_fname() 1013 Model1DPlugin.__init__(self, name=self.name) 1014 #set name same as file name 1015 self.name = self.get_fname() 1016 1016 #self.params here 1017 1017 self.description = "%s" … … 1064 1064 return name 1065 1065 ###################################################################### 1066 ## THIS IS FOR TEST. DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!! 1067 if __name__ == "__main__": 1068 m= Model() 1066 ## THIS IS FOR TEST. DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!! 1067 if __name__ == "__main__": 1068 m= Model() 1069 1069 out1 = m.runXY(0.0) 1070 1070 out2 = m.runXY(0.01) … … 1085 1085 from sas.models.pluginmodel import Model1DPlugin 1086 1086 # User can change the name of the model (only with single functional model) 1087 #P1_model: 1087 #P1_model: 1088 1088 #from sas.models.%s import %s as P1 1089 #from %s import Model as P1 1090 1091 #P2_model: 1089 #from %s import Model as P1 1090 1091 #P2_model: 1092 1092 #from sas.models.%s import %s as P2 1093 #from %s import Model as P2 1093 #from %s import Model as P2 1094 1094 import os 1095 1095 import sys … … 1119 1119 self.magnetic_params = [] 1120 1120 # non-fittable parameters 1121 self.non_fittable = p_model1.non_fittable 1122 self.non_fittable += p_model2.non_fittable 1123 1124 ##models 1121 self.non_fittable = p_model1.non_fittable 1122 self.non_fittable += p_model2.non_fittable 1123 1124 ##models 1125 1125 self.p_model1= p_model1 1126 1126 self.p_model2= p_model2 1127 1128 1127 1128 1129 1129 ## dispersion 1130 1130 self._set_dispersion() … … 1133 1133 ## New parameter:scaling_factor 1134 1134 self.params['scale_factor'] = %s 1135 1135 1136 1136 ## Parameter details [units, min, max] 1137 1137 self._set_details() 1138 1138 self.details['scale_factor'] = ['', None, None] 1139 1139 1140 1140 1141 1141 #list of parameter that can be fitted 1142 self._set_fixed_params() 1142 self._set_fixed_params() 1143 1143 ## parameters with orientation 1144 1144 for item in self.p_model1.orientation_params: … … 1146 1146 if not new_item in self.orientation_params: 1147 1147 self.orientation_params.append(new_item) 1148 1148 1149 1149 for item in self.p_model2.orientation_params: 1150 1150 new_item = "p2_" + item … … 1156 1156 if not new_item in self.magnetic_params: 1157 1157 self.magnetic_params.append(new_item) 1158 1158 1159 1159 for item in self.p_model2.magnetic_params: 1160 1160 new_item = "p2_" + item … … 1172 1172 multiplicity2 = 1 1173 1173 ## functional multiplicity of the model 1174 self.multiplicity1 = multiplicity1 1175 self.multiplicity2 = multiplicity2 1176 self.multiplicity_info = [] 1177 1174 self.multiplicity1 = multiplicity1 1175 self.multiplicity2 = multiplicity2 1176 self.multiplicity_info = [] 1177 1178 1178 def _clone(self, obj): 1179 1179 obj.params = copy.deepcopy(self.params) … … 1185 1185 #obj = copy.deepcopy(self) 1186 1186 return obj 1187 1187 1188 1188 def _get_name(self, name1, name2): 1189 1189 p1_name = self._get_upper_name(name1) … … 1197 1197 name += p2_name 1198 1198 return name 1199 1199 1200 1200 def _get_upper_name(self, name=None): 1201 1201 if name == None: … … 1207 1207 upper_name += str_name[index] 1208 1208 return upper_name 1209 1209 1210 1210 def _set_dispersion(self): 1211 ##set dispersion only from p_model 1211 ##set dispersion only from p_model 1212 1212 for name , value in self.p_model1.dispersion.iteritems(): 1213 1213 #if name.lower() not in self.p_model1.orientation_params: 1214 1214 new_name = "p1_" + name 1215 self.dispersion[new_name]= value 1215 self.dispersion[new_name]= value 1216 1216 for name , value in self.p_model2.dispersion.iteritems(): 1217 1217 #if name.lower() not in self.p_model2.orientation_params: 1218 1218 new_name = "p2_" + name 1219 self.dispersion[new_name]= value 1220 1221 def function(self, x=0.0): 1219 self.dispersion[new_name]= value 1220 1221 def function(self, x=0.0): 1222 1222 return 0 1223 1223 1224 1224 def getProfile(self): 1225 1225 try: … … 1228 1228 x = None 1229 1229 y = None 1230 1230 1231 1231 return x, y 1232 1232 1233 1233 def _set_params(self): 1234 1234 for name , value in self.p_model1.params.iteritems(): … … 1237 1237 new_name = "p1_" + name 1238 1238 self.params[new_name]= value 1239 1239 1240 1240 for name , value in self.p_model2.params.iteritems(): 1241 1241 # No 2D-supported … … 1243 1243 new_name = "p2_" + name 1244 1244 self.params[new_name]= value 1245 1245 1246 1246 # Set "scale" as initializing 1247 1247 self._set_scale_factor() 1248 1249 1248 1249 1250 1250 def _set_details(self): 1251 1251 for name ,detail in self.p_model1.details.iteritems(): … … 1253 1253 #if new_name not in self.orientation_params: 1254 1254 self.details[new_name]= detail 1255 1255 1256 1256 for name ,detail in self.p_model2.details.iteritems(): 1257 1257 new_name = "p2_" + name 1258 1258 #if new_name not in self.orientation_params: 1259 1259 self.details[new_name]= detail 1260 1260 1261 1261 def _set_scale_factor(self): 1262 1262 pass 1263 1264 1263 1264 1265 1265 def setParam(self, name, value): 1266 1266 # set param to this (p1, p2) model 1267 1267 self._setParamHelper(name, value) 1268 1269 ## setParam to p model 1268 1269 ## setParam to p model 1270 1270 model_pre = '' 1271 1271 new_name = '' … … 1284 1284 else: 1285 1285 raise ValueError, "Model does not contain parameter %s" % name 1286 1286 1287 1287 def getParam(self, name): 1288 1288 # Look for dispersion parameters … … 1300 1300 if item.lower()==name.lower(): 1301 1301 return self.params[item] 1302 return 1302 return 1303 1303 #raise ValueError, "Model does not contain parameter %s" % name 1304 1304 1305 1305 def _setParamHelper(self, name, value): 1306 1306 # Look for dispersion parameters … … 1319 1319 self.params[item] = value 1320 1320 return 1321 1321 1322 1322 raise ValueError, "Model does not contain parameter %s" % name 1323 1324 1323 1324 1325 1325 def _set_fixed_params(self): 1326 1326 for item in self.p_model1.fixed: … … 1332 1332 1333 1333 self.fixed.sort() 1334 1335 1334 1335 1336 1336 def run(self, x = 0.0): 1337 1337 self._set_scale_factor() 1338 1338 return self.params['scale_factor'] %s \ 1339 1339 (self.p_model1.run(x) %s self.p_model2.run(x)) 1340 1340 1341 1341 def runXY(self, x = 0.0): 1342 1342 self._set_scale_factor() 1343 1343 return self.params['scale_factor'] %s \ 1344 1344 (self.p_model1.runXY(x) %s self.p_model2.runXY(x)) 1345 1346 ## Now (May27,10) directly uses the model eval function 1345 1346 ## Now (May27,10) directly uses the model eval function 1347 1347 ## instead of the for-loop in Base Component. 1348 1348 def evalDistribution(self, x = []): … … 1366 1366 return value 1367 1367 except: 1368 raise 1368 raise 1369 1369 1370 1370 def fill_description(self, p_model1, p_model2): … … 1373 1373 description += "%s and %s. "% ( p_model1.name, p_model2.name ) 1374 1374 self.description += description 1375 1375 1376 1376 def get_fname(self): 1377 1377 path = sys._getframe().f_code.co_filename 1378 1378 basename = os.path.basename(path) 1379 1379 name, _ = os.path.splitext(basename) 1380 return name 1381 1382 if __name__ == "__main__": 1383 m1= Model() 1384 #m1.setParam("p1_scale", 25) 1380 return name 1381 1382 if __name__ == "__main__": 1383 m1= Model() 1384 #m1.setParam("p1_scale", 25) 1385 1385 #m1.setParam("p1_length", 1000) 1386 #m1.setParam("p2_scale", 100) 1387 #m1.setParam("p2_rg", 100) 1386 #m1.setParam("p2_scale", 100) 1387 #m1.setParam("p2_rg", 100) 1388 1388 out1 = m1.runXY(0.01) 1389 1389 1390 1390 m2= Model() 1391 #m2.p_model1.setParam("scale", 25) 1392 #m2.p_model1.setParam("length", 1000) 1391 #m2.p_model1.setParam("scale", 25) 1392 #m2.p_model1.setParam("length", 1000) 1393 1393 #m2.p_model2.setParam("scale", 100) 1394 1394 #m2.p_model2.setParam("rg", 100) … … 1401 1401 print "===> Simple Test: Failed!" 1402 1402 """ 1403 1403 1404 1404 #if __name__ == "__main__": 1405 1405 # app = wx.PySimpleApp() … … 1411 1411 from sas.perspectives.fitting import models 1412 1412 dir_path = models.find_plugins_dir() 1413 app 1413 app = wx.App() 1414 1414 window = EditorWindow(parent=None, base=None, path=dir_path, title="Editor") 1415 app.MainLoop() 1415 app.MainLoop()
Note: See TracChangeset
for help on using the changeset viewer.