[7b1f4e3] | 1 | ''' |
---|
| 2 | This module provides three model editor classes: the composite model editor, |
---|
| 3 | the easy editor which provides a simple interface with tooltip help to enter |
---|
[2d50115] | 4 | the parameters of the model and their default value and a panel to input a |
---|
| 5 | function of y (usually the intensity). It also provides a drop down of |
---|
[7b1f4e3] | 6 | standard available math functions. Finally a full python editor panel for |
---|
| 7 | complete customizatin is provided. |
---|
| 8 | |
---|
[2d50115] | 9 | :TODO the writiong of the file and name checking (and maybe some other |
---|
[7b1f4e3] | 10 | funtions?) should be moved to a computational module which could be called |
---|
| 11 | fropm a python script. Basically one just needs to pass the name, |
---|
[2d50115] | 12 | description text and function text (or in the case of the composite editor |
---|
[7b1f4e3] | 13 | the names of the first and second model and the operator to be used). |
---|
| 14 | ''' |
---|
| 15 | |
---|
[6f140f2] | 16 | ################################################################################ |
---|
| 17 | #This software was developed by the University of Tennessee as part of the |
---|
| 18 | #Distributed Data Analysis of Neutron Scattering Experiments (DANSE) |
---|
[18ac46b] | 19 | #project funded by the US National Science Foundation. |
---|
[6f140f2] | 20 | # |
---|
| 21 | #See the license text in license.txt |
---|
| 22 | # |
---|
| 23 | #copyright 2009, University of Tennessee |
---|
| 24 | ################################################################################ |
---|
| 25 | import wx |
---|
| 26 | import sys |
---|
| 27 | import os |
---|
[318b5bbb] | 28 | import math |
---|
[7b1f4e3] | 29 | import re |
---|
[6f140f2] | 30 | from wx.py.editwindow import EditWindow |
---|
| 31 | |
---|
| 32 | if sys.platform.count("win32") > 0: |
---|
| 33 | FONT_VARIANT = 0 |
---|
[fdef956] | 34 | PNL_WIDTH = 450 |
---|
[18ac46b] | 35 | PNL_HEIGHT = 320 |
---|
[6f140f2] | 36 | else: |
---|
| 37 | FONT_VARIANT = 1 |
---|
[fdef956] | 38 | PNL_WIDTH = 590 |
---|
[18ac46b] | 39 | PNL_HEIGHT = 350 |
---|
[fdef956] | 40 | M_NAME = 'Model' |
---|
[6f140f2] | 41 | EDITOR_WIDTH = 800 |
---|
[318b5bbb] | 42 | EDITOR_HEIGTH = 735 |
---|
[6f140f2] | 43 | PANEL_WIDTH = 500 |
---|
[fdef956] | 44 | _BOX_WIDTH = 55 |
---|
[6f140f2] | 45 | |
---|
[49ab5d7] | 46 | |
---|
[18ac46b] | 47 | def _compile_file(path): |
---|
[6f140f2] | 48 | """ |
---|
| 49 | Compile the file in the path |
---|
| 50 | """ |
---|
| 51 | try: |
---|
| 52 | import py_compile |
---|
| 53 | py_compile.compile(file=path, doraise=True) |
---|
| 54 | return '' |
---|
| 55 | except: |
---|
[d970df9] | 56 | _, value, _ = sys.exc_info() |
---|
[6f140f2] | 57 | return value |
---|
[49ab5d7] | 58 | |
---|
[18ac46b] | 59 | def _delete_file(path): |
---|
[6f140f2] | 60 | """ |
---|
| 61 | Delete file in the path |
---|
| 62 | """ |
---|
| 63 | try: |
---|
| 64 | os.remove(path) |
---|
| 65 | except: |
---|
| 66 | raise |
---|
| 67 | |
---|
[49ab5d7] | 68 | |
---|
[6f140f2] | 69 | class TextDialog(wx.Dialog): |
---|
| 70 | """ |
---|
[7b1f4e3] | 71 | Dialog for easy custom composite models. Provides a wx.Dialog panel |
---|
| 72 | to choose two existing models (including pre-existing custom models which |
---|
[18ac46b] | 73 | may themselves be composite models) as well as an operation on those models |
---|
[2d50115] | 74 | (add or multiply) the resulting model will add a scale parameter for summed |
---|
[7b1f4e3] | 75 | models and a background parameter for a multiplied model. |
---|
[2d50115] | 76 | |
---|
| 77 | The user also gives a brief help for the model in a description box and |
---|
[7b1f4e3] | 78 | must provide a unique name which is verified as unique before the new |
---|
| 79 | model is saved. |
---|
[2d50115] | 80 | |
---|
[7b1f4e3] | 81 | This Dialog pops up for the user when they press 'Sum|Multi(p1,p2)' under |
---|
| 82 | 'Edit Custom Model' under 'Fitting' menu. This is currently called as |
---|
| 83 | a Modal Dialog. |
---|
[2d50115] | 84 | |
---|
| 85 | :TODO the build in compiler currently balks at when it tries to import |
---|
| 86 | a model whose name contains spaces or symbols (such as + ... underscore |
---|
[18ac46b] | 87 | should be fine). Have fixed so the editor cannot save such a file name |
---|
| 88 | but if a file is dropped in the plugin directory from outside this class |
---|
| 89 | will create a file that cannot be compiled. Should add the check to |
---|
| 90 | the write method or to the on_modelx method. |
---|
| 91 | - PDB:April 5, 2015 |
---|
[6f140f2] | 92 | """ |
---|
[49ab5d7] | 93 | def __init__(self, parent=None, base=None, id=None, title='', |
---|
[d970df9] | 94 | model_list=[], plugin_dir=None): |
---|
[6f140f2] | 95 | """ |
---|
[18ac46b] | 96 | This class is run when instatiated. The __init__ initializes and |
---|
[7b1f4e3] | 97 | calls the internal methods necessary. On exiting the wx.Dialog |
---|
| 98 | window should be destroyed. |
---|
[6f140f2] | 99 | """ |
---|
[49ab5d7] | 100 | wx.Dialog.__init__(self, parent=parent, id=id, |
---|
[18ac46b] | 101 | title=title, size=(PNL_WIDTH, PNL_HEIGHT)) |
---|
[796c4d4] | 102 | self.parent = base |
---|
[6f140f2] | 103 | #Font |
---|
| 104 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
| 105 | # default |
---|
[796c4d4] | 106 | self.overwrite_name = False |
---|
| 107 | self.plugin_dir = plugin_dir |
---|
[6f140f2] | 108 | self.model_list = model_list |
---|
| 109 | self.model1_string = "SphereModel" |
---|
| 110 | self.model2_string = "CylinderModel" |
---|
[fdef956] | 111 | self.name = 'Sum' + M_NAME |
---|
[d970df9] | 112 | self.factor = 'scale_factor' |
---|
[41a8cb3] | 113 | self._notes = '' |
---|
[7b1f4e3] | 114 | self._operator = '+' |
---|
| 115 | self._operator_choice = None |
---|
[fdef956] | 116 | self.explanation = '' |
---|
| 117 | self.explanationctr = None |
---|
[7b1f4e3] | 118 | self.type = None |
---|
[d970df9] | 119 | self.name_sizer = None |
---|
[7b1f4e3] | 120 | self.name_tcl = None |
---|
[d970df9] | 121 | self.desc_sizer = None |
---|
| 122 | self.desc_tcl = None |
---|
[2d50115] | 123 | self._selection_box = None |
---|
[d970df9] | 124 | self.model1 = None |
---|
| 125 | self.model2 = None |
---|
| 126 | self.static_line_1 = None |
---|
[18ac46b] | 127 | self.ok_button = None |
---|
| 128 | self.close_button = None |
---|
[41a8cb3] | 129 | self._msg_box = None |
---|
| 130 | self.msg_sizer = None |
---|
[d970df9] | 131 | self.fname = None |
---|
| 132 | self.cm_list = None |
---|
| 133 | self.is_p1_custom = False |
---|
| 134 | self.is_p2_custom = False |
---|
[6f140f2] | 135 | self._build_sizer() |
---|
| 136 | self.model1_name = str(self.model1.GetValue()) |
---|
| 137 | self.model2_name = str(self.model2.GetValue()) |
---|
[796c4d4] | 138 | self.good_name = True |
---|
[fdef956] | 139 | self.fill_oprator_combox() |
---|
[49ab5d7] | 140 | |
---|
[796c4d4] | 141 | def _layout_name(self): |
---|
| 142 | """ |
---|
| 143 | Do the layout for file/function name related widgets |
---|
| 144 | """ |
---|
[7b1f4e3] | 145 | #container for new model name input |
---|
| 146 | self.name_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 147 | |
---|
| 148 | #set up label and input box with tool tip and event handling |
---|
[2d50115] | 149 | name_txt = wx.StaticText(self, -1, 'Function Name : ') |
---|
| 150 | self.name_tcl = wx.TextCtrl(self, -1, value='MySumFunction') |
---|
[796c4d4] | 151 | self.name_tcl.Bind(wx.EVT_TEXT_ENTER, self.on_change_name) |
---|
[0008f54] | 152 | hint_name = "Unique Sum/Multiply Model Function Name." |
---|
[796c4d4] | 153 | self.name_tcl.SetToolTipString(hint_name) |
---|
[7b1f4e3] | 154 | |
---|
[2d50115] | 155 | self.name_sizer.AddMany([(name_txt, 0, wx.LEFT | wx.TOP, 10), |
---|
| 156 | (self.name_tcl, -1, |
---|
| 157 | wx.EXPAND | wx.RIGHT | wx.TOP | wx.BOTTOM, |
---|
| 158 | 10)]) |
---|
[49ab5d7] | 159 | |
---|
| 160 | |
---|
[796c4d4] | 161 | def _layout_description(self): |
---|
| 162 | """ |
---|
| 163 | Do the layout for description related widgets |
---|
| 164 | """ |
---|
[7b1f4e3] | 165 | #container for new model description input |
---|
[796c4d4] | 166 | self.desc_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
[7b1f4e3] | 167 | |
---|
| 168 | #set up description label and input box with tool tip and event handling |
---|
[49ab5d7] | 169 | desc_txt = wx.StaticText(self, -1, 'Description (optional) : ') |
---|
[7b1f4e3] | 170 | self.desc_tcl = wx.TextCtrl(self, -1) |
---|
[fdef956] | 171 | hint_desc = "Write a short description of this model function." |
---|
[796c4d4] | 172 | self.desc_tcl.SetToolTipString(hint_desc) |
---|
[7b1f4e3] | 173 | |
---|
[49ab5d7] | 174 | self.desc_sizer.AddMany([(desc_txt, 0, wx.LEFT | wx.TOP, 10), |
---|
[18ac46b] | 175 | (self.desc_tcl, -1, |
---|
| 176 | wx.EXPAND | wx.RIGHT | wx.TOP | wx.BOTTOM, |
---|
| 177 | 10)]) |
---|
[49ab5d7] | 178 | |
---|
[7b1f4e3] | 179 | |
---|
| 180 | def _layout_model_selection(self): |
---|
[6f140f2] | 181 | """ |
---|
[7b1f4e3] | 182 | Do the layout for model selection related widgets |
---|
[6f140f2] | 183 | """ |
---|
[fdef956] | 184 | box_width = 195 # combobox width |
---|
[49ab5d7] | 185 | |
---|
[7b1f4e3] | 186 | #First set up main sizer for the selection |
---|
| 187 | selection_box_title = wx.StaticBox(self, -1, 'Select', |
---|
[2d50115] | 188 | size=(PNL_WIDTH - 30, 70)) |
---|
| 189 | self._selection_box = wx.StaticBoxSizer(selection_box_title, |
---|
[18ac46b] | 190 | wx.VERTICAL) |
---|
[7b1f4e3] | 191 | |
---|
| 192 | #Next create the help labels for the model selection |
---|
| 193 | select_help_box = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 194 | model_string = " Model%s (p%s):" |
---|
[18ac46b] | 195 | select_help_box.Add(wx.StaticText(self, -1, model_string % (1, 1)), |
---|
| 196 | 0, 0) |
---|
[2d50115] | 197 | select_help_box.Add((box_width - 25, 10), 0, 0) |
---|
[18ac46b] | 198 | select_help_box.Add(wx.StaticText(self, -1, model_string % (2, 2)), |
---|
| 199 | 0, 0) |
---|
[7b1f4e3] | 200 | self._selection_box.Add(select_help_box, 0, 0) |
---|
| 201 | |
---|
| 202 | #Next create the actual selection box with 3 combo boxes |
---|
| 203 | selection_box_choose = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 204 | |
---|
[49ab5d7] | 205 | self.model1 = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
[6f140f2] | 206 | wx.EVT_COMBOBOX(self.model1, -1, self.on_model1) |
---|
[49ab5d7] | 207 | self.model1.SetMinSize((box_width * 5 / 6, -1)) |
---|
[6f140f2] | 208 | self.model1.SetToolTipString("model1") |
---|
[49ab5d7] | 209 | |
---|
[18ac46b] | 210 | self._operator_choice = wx.ComboBox(self, -1, size=(50, -1), |
---|
| 211 | style=wx.CB_READONLY) |
---|
[7b1f4e3] | 212 | wx.EVT_COMBOBOX(self._operator_choice, -1, self.on_select_operator) |
---|
[fdef956] | 213 | operation_tip = "Add: +, Multiply: * " |
---|
[7b1f4e3] | 214 | self._operator_choice.SetToolTipString(operation_tip) |
---|
[49ab5d7] | 215 | |
---|
| 216 | self.model2 = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
[6f140f2] | 217 | wx.EVT_COMBOBOX(self.model2, -1, self.on_model2) |
---|
[49ab5d7] | 218 | self.model2.SetMinSize((box_width * 5 / 6, -1)) |
---|
[6f140f2] | 219 | self.model2.SetToolTipString("model2") |
---|
| 220 | self._set_model_list() |
---|
[49ab5d7] | 221 | |
---|
[7b1f4e3] | 222 | selection_box_choose.Add(self.model1, 0, 0) |
---|
| 223 | selection_box_choose.Add((15, 10)) |
---|
| 224 | selection_box_choose.Add(self._operator_choice, 0, 0) |
---|
| 225 | selection_box_choose.Add((15, 10)) |
---|
| 226 | selection_box_choose.Add(self.model2, 0, 0) |
---|
[18ac46b] | 227 | # add some space between labels and selection |
---|
[2d50115] | 228 | self._selection_box.Add((20, 5), 0, 0) |
---|
[7b1f4e3] | 229 | self._selection_box.Add(selection_box_choose, 0, 0) |
---|
| 230 | |
---|
| 231 | def _build_sizer(self): |
---|
| 232 | """ |
---|
| 233 | Build GUI with calls to _layout_name, _layout Description |
---|
[2d50115] | 234 | and _layout_model_selection which each build a their portion of the |
---|
[7b1f4e3] | 235 | GUI. |
---|
| 236 | """ |
---|
| 237 | mainsizer = wx.BoxSizer(wx.VERTICAL) # create main sizer for dialog |
---|
| 238 | |
---|
| 239 | # build fromm top by calling _layout_name and _layout_description |
---|
| 240 | # and adding to main sizer |
---|
| 241 | self._layout_name() |
---|
[2d50115] | 242 | mainsizer.Add(self.name_sizer, 0, wx.EXPAND) |
---|
[7b1f4e3] | 243 | self._layout_description() |
---|
[2d50115] | 244 | mainsizer.Add(self.desc_sizer, 0, wx.EXPAND) |
---|
[7b1f4e3] | 245 | |
---|
| 246 | # Add an explanation of dialog (short help) |
---|
| 247 | self.explanationctr = wx.StaticText(self, -1, self.explanation) |
---|
| 248 | self.fill_explanation_helpstring(self._operator) |
---|
[2d50115] | 249 | mainsizer.Add(self.explanationctr, 0, wx.LEFT | wx.EXPAND, 15) |
---|
[7b1f4e3] | 250 | |
---|
| 251 | # Add the selection box stuff with border and labels built |
---|
| 252 | # by _layout_model_selection |
---|
| 253 | self._layout_model_selection() |
---|
| 254 | mainsizer.Add(self._selection_box, 0, wx.LEFT, 15) |
---|
| 255 | |
---|
| 256 | # Add a space and horizontal line before the notification |
---|
| 257 | #messages and the buttons at the bottom |
---|
| 258 | mainsizer.Add((10, 10)) |
---|
[6f140f2] | 259 | self.static_line_1 = wx.StaticLine(self, -1) |
---|
[7b1f4e3] | 260 | mainsizer.Add(self.static_line_1, 0, wx.EXPAND, 10) |
---|
| 261 | |
---|
| 262 | # Add action status notification line (null at startup) |
---|
[41a8cb3] | 263 | self._msg_box = wx.StaticText(self, -1, self._notes) |
---|
| 264 | self.msg_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 265 | self.msg_sizer.Add(self._msg_box, 0, wx.LEFT, 0) |
---|
[7b1f4e3] | 266 | mainsizer.Add(self.msg_sizer, 0, |
---|
[2d50115] | 267 | wx.LEFT | wx.RIGHT | wx.ADJUST_MINSIZE | wx.BOTTOM, 10) |
---|
[7b1f4e3] | 268 | |
---|
| 269 | # Finally add the buttons (apply and close) on the bottom |
---|
[2d50115] | 270 | # Eventually need to add help here |
---|
[18ac46b] | 271 | self.ok_button = wx.Button(self, wx.ID_OK, 'Apply') |
---|
| 272 | self.ok_button.Bind(wx.EVT_BUTTON, self.check_name) |
---|
| 273 | self.close_button = wx.Button(self, wx.ID_CANCEL, 'Close') |
---|
[6f140f2] | 274 | sizer_button = wx.BoxSizer(wx.HORIZONTAL) |
---|
[7b1f4e3] | 275 | sizer_button.AddMany([((20, 20), 1, 0), |
---|
[18ac46b] | 276 | (self.ok_button, 0, 0), |
---|
[2d50115] | 277 | (self.close_button, 0, wx.LEFT | wx.RIGHT, 10)]) |
---|
[7b1f4e3] | 278 | mainsizer.Add(sizer_button, 0, wx.EXPAND | wx.BOTTOM | wx.TOP, 10) |
---|
| 279 | |
---|
| 280 | self.SetSizer(mainsizer) |
---|
[6f140f2] | 281 | self.Centre() |
---|
[49ab5d7] | 282 | |
---|
[796c4d4] | 283 | def on_change_name(self, event=None): |
---|
| 284 | """ |
---|
| 285 | Change name |
---|
| 286 | """ |
---|
| 287 | if event is not None: |
---|
| 288 | event.Skip() |
---|
| 289 | self.name_tcl.SetBackgroundColour('white') |
---|
| 290 | self.Refresh() |
---|
[49ab5d7] | 291 | |
---|
[796c4d4] | 292 | def check_name(self, event=None): |
---|
| 293 | """ |
---|
[2d50115] | 294 | Check that proposed new model name is a valid Python module name |
---|
[439420f9] | 295 | and that it does not already exist. If not show error message and |
---|
| 296 | pink background in text box else call on_apply |
---|
[2d50115] | 297 | |
---|
| 298 | :TODO this should be separated out from the GUI code. For that we |
---|
| 299 | need to pass it the name (or if we want to keep the default name |
---|
[439420f9] | 300 | option also need to pass the self._operator attribute) We just need |
---|
[2d50115] | 301 | the function to return an error code that the name is good or if |
---|
| 302 | not why (not a valid name, name exists already). The rest of the |
---|
[439420f9] | 303 | error handling should be done in this module. so on_apply would then |
---|
| 304 | start by checking the name and then either raise errors or do the |
---|
| 305 | deed. |
---|
[796c4d4] | 306 | """ |
---|
[439420f9] | 307 | #Get the function/file name |
---|
[d970df9] | 308 | mname = M_NAME |
---|
[796c4d4] | 309 | self.on_change_name(None) |
---|
| 310 | title = self.name_tcl.GetValue().lstrip().rstrip() |
---|
| 311 | if title == '': |
---|
[7b1f4e3] | 312 | text = self._operator |
---|
[c8654a3] | 313 | if text.count('+') > 0: |
---|
[d970df9] | 314 | mname = 'Sum' |
---|
[19b9e43] | 315 | else: |
---|
[c8654a3] | 316 | mname = 'Multi' |
---|
[d970df9] | 317 | mname += M_NAME |
---|
| 318 | title = mname |
---|
[796c4d4] | 319 | self.name = title |
---|
| 320 | t_fname = title + '.py' |
---|
[439420f9] | 321 | |
---|
| 322 | #First check if the name is a valid Python name |
---|
[2d50115] | 323 | if re.match('^[A-Za-z0-9_]*$', title): |
---|
[439420f9] | 324 | self.good_name = True |
---|
[2d50115] | 325 | else: |
---|
[439420f9] | 326 | self.good_name = False |
---|
| 327 | msg = ("%s is not a valid Python name. Only alphanumeric \n" \ |
---|
[18ac46b] | 328 | "and underscore allowed" % self.name) |
---|
[2d50115] | 329 | |
---|
[439420f9] | 330 | #Now check if the name already exists |
---|
| 331 | if not self.overwrite_name and self.good_name: |
---|
| 332 | #Create list of existing model names for comparison |
---|
| 333 | list_fnames = os.listdir(self.plugin_dir) |
---|
| 334 | # fake existing regular model name list |
---|
| 335 | m_list = [model + ".py" for model in self.model_list] |
---|
| 336 | list_fnames.append(m_list) |
---|
[d970df9] | 337 | if t_fname in list_fnames and title != mname: |
---|
[796c4d4] | 338 | self.good_name = False |
---|
| 339 | msg = "Name exists already." |
---|
[439420f9] | 340 | |
---|
| 341 | if self.good_name == False: |
---|
| 342 | self.name_tcl.SetBackgroundColour('pink') |
---|
| 343 | info = 'Error' |
---|
| 344 | wx.MessageBox(msg, info) |
---|
| 345 | self._notes = msg |
---|
| 346 | color = 'red' |
---|
| 347 | self._msg_box.SetLabel(msg) |
---|
| 348 | self._msg_box.SetForegroundColour(color) |
---|
| 349 | return self.good_name |
---|
[796c4d4] | 350 | self.fname = os.path.join(self.plugin_dir, t_fname) |
---|
[41a8cb3] | 351 | s_title = title |
---|
| 352 | if len(title) > 20: |
---|
| 353 | s_title = title[0:19] + '...' |
---|
[0008f54] | 354 | self._notes = "Model function (%s) has been set! \n" % str(s_title) |
---|
[796c4d4] | 355 | self.good_name = True |
---|
| 356 | self.on_apply(self.fname) |
---|
| 357 | return self.good_name |
---|
[49ab5d7] | 358 | |
---|
[796c4d4] | 359 | def on_apply(self, path): |
---|
| 360 | """ |
---|
[18ac46b] | 361 | This method is a misnomer - it is not bound to the apply button |
---|
| 362 | event. Instead the apply button event goes to check_name which |
---|
| 363 | then calls this method if the name of the new file is acceptable. |
---|
[2d50115] | 364 | |
---|
| 365 | :TODO this should be bound to the apply button. The first line |
---|
[18ac46b] | 366 | should call the check_name method which itself should be in another |
---|
[2d50115] | 367 | module separated from the the GUI modules. |
---|
[796c4d4] | 368 | """ |
---|
[7b1f4e3] | 369 | self.name_tcl.SetBackgroundColour('white') |
---|
[796c4d4] | 370 | try: |
---|
[18ac46b] | 371 | label = self.get_textnames() |
---|
[796c4d4] | 372 | fname = path |
---|
| 373 | name1 = label[0] |
---|
| 374 | name2 = label[1] |
---|
| 375 | self.write_string(fname, name1, name2) |
---|
| 376 | self.compile_file(fname) |
---|
| 377 | self.parent.update_custom_combo() |
---|
[cdc2ee2] | 378 | msg = self._notes |
---|
[0008f54] | 379 | info = 'Info' |
---|
[41a8cb3] | 380 | color = 'blue' |
---|
[796c4d4] | 381 | except: |
---|
[49ab5d7] | 382 | msg = "Easy Custom Sum/Multipy: Error occurred..." |
---|
[cdc2ee2] | 383 | info = 'Error' |
---|
[41a8cb3] | 384 | color = 'red' |
---|
| 385 | self._msg_box.SetLabel(msg) |
---|
| 386 | self._msg_box.SetForegroundColour(color) |
---|
[cdc2ee2] | 387 | if self.parent.parent != None: |
---|
[49ab5d7] | 388 | from sas.guiframe.events import StatusEvent |
---|
| 389 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg, |
---|
[18ac46b] | 390 | info=info)) |
---|
[cdc2ee2] | 391 | else: |
---|
[796c4d4] | 392 | raise |
---|
[49ab5d7] | 393 | |
---|
[6f140f2] | 394 | def _set_model_list(self): |
---|
| 395 | """ |
---|
| 396 | Set the list of models |
---|
| 397 | """ |
---|
| 398 | # list of model names |
---|
[18ac46b] | 399 | # get regular models |
---|
| 400 | main_list = self.model_list |
---|
| 401 | # get custom models |
---|
| 402 | self.update_cm_list() |
---|
| 403 | # add custom models to model list |
---|
| 404 | for name in self.cm_list: |
---|
| 405 | if name not in main_list: |
---|
| 406 | main_list.append(name) |
---|
| 407 | |
---|
| 408 | if len(main_list) > 1: |
---|
| 409 | main_list.sort() |
---|
| 410 | for idx in range(len(main_list)): |
---|
| 411 | self.model1.Append(str(main_list[idx]), idx) |
---|
| 412 | self.model2.Append(str(main_list[idx]), idx) |
---|
[6f140f2] | 413 | self.model1.SetStringSelection(self.model1_string) |
---|
| 414 | self.model2.SetStringSelection(self.model2_string) |
---|
[49ab5d7] | 415 | |
---|
[d970df9] | 416 | def update_cm_list(self): |
---|
| 417 | """ |
---|
| 418 | Update custom model list |
---|
| 419 | """ |
---|
| 420 | cm_list = [] |
---|
| 421 | al_list = os.listdir(self.plugin_dir) |
---|
| 422 | for c_name in al_list: |
---|
| 423 | if c_name.split('.')[-1] == 'py' and \ |
---|
| 424 | c_name.split('.')[0] != '__init__': |
---|
| 425 | name = str(c_name.split('.')[0]) |
---|
| 426 | cm_list.append(name) |
---|
[49ab5d7] | 427 | self.cm_list = cm_list |
---|
| 428 | |
---|
[6f140f2] | 429 | def on_model1(self, event): |
---|
| 430 | """ |
---|
| 431 | Set model1 |
---|
| 432 | """ |
---|
| 433 | event.Skip() |
---|
[d970df9] | 434 | self.update_cm_list() |
---|
[6f140f2] | 435 | self.model1_name = str(self.model1.GetValue()) |
---|
| 436 | self.model1_string = self.model1_name |
---|
[d970df9] | 437 | if self.model1_name in self.cm_list: |
---|
| 438 | self.is_p1_custom = True |
---|
| 439 | else: |
---|
| 440 | self.is_p1_custom = False |
---|
[49ab5d7] | 441 | |
---|
[6f140f2] | 442 | def on_model2(self, event): |
---|
| 443 | """ |
---|
| 444 | Set model2 |
---|
| 445 | """ |
---|
| 446 | event.Skip() |
---|
[d970df9] | 447 | self.update_cm_list() |
---|
[6f140f2] | 448 | self.model2_name = str(self.model2.GetValue()) |
---|
| 449 | self.model2_string = self.model2_name |
---|
[d970df9] | 450 | if self.model2_name in self.cm_list: |
---|
| 451 | self.is_p2_custom = True |
---|
| 452 | else: |
---|
| 453 | self.is_p2_custom = False |
---|
[49ab5d7] | 454 | |
---|
[fdef956] | 455 | def on_select_operator(self, event=None): |
---|
| 456 | """ |
---|
| 457 | On Select an Operator |
---|
| 458 | """ |
---|
[9e00363] | 459 | # For Mac |
---|
| 460 | if event != None: |
---|
| 461 | event.Skip() |
---|
[fdef956] | 462 | item = event.GetEventObject() |
---|
[c8654a3] | 463 | text = item.GetValue() |
---|
[7b1f4e3] | 464 | self.fill_explanation_helpstring(text) |
---|
| 465 | |
---|
[2d50115] | 466 | def fill_explanation_helpstring(self, operator): |
---|
[7b1f4e3] | 467 | """ |
---|
| 468 | Choose the equation to use depending on whether we now have |
---|
| 469 | a sum or multiply model then create the appropriate string |
---|
| 470 | """ |
---|
| 471 | |
---|
| 472 | name = '' |
---|
| 473 | |
---|
[2d50115] | 474 | if operator == '*': |
---|
[fdef956] | 475 | name = 'Multi' |
---|
[d970df9] | 476 | factor = 'BackGround' |
---|
| 477 | f_oper = '+' |
---|
[f5500b3] | 478 | else: |
---|
[848913c] | 479 | name = 'Sum' |
---|
| 480 | factor = 'scale_factor' |
---|
| 481 | f_oper = '*' |
---|
[c8654a3] | 482 | |
---|
| 483 | self.factor = factor |
---|
[2d50115] | 484 | self._operator = operator |
---|
[49ab5d7] | 485 | self.explanation = " Custom Model = %s %s (model1 %s model2)\n" % \ |
---|
[18ac46b] | 486 | (self.factor, f_oper, self._operator) |
---|
[fdef956] | 487 | self.explanationctr.SetLabel(self.explanation) |
---|
[49ab5d7] | 488 | self.name = name + M_NAME |
---|
[7b1f4e3] | 489 | |
---|
[49ab5d7] | 490 | |
---|
[fdef956] | 491 | def fill_oprator_combox(self): |
---|
| 492 | """ |
---|
| 493 | fill the current combobox with the operator |
---|
[49ab5d7] | 494 | """ |
---|
[fdef956] | 495 | operator_list = [' +', ' *'] |
---|
| 496 | for oper in operator_list: |
---|
[7b1f4e3] | 497 | pos = self._operator_choice.Append(str(oper)) |
---|
| 498 | self._operator_choice.SetClientData(pos, str(oper.strip())) |
---|
| 499 | self._operator_choice.SetSelection(0) |
---|
[49ab5d7] | 500 | |
---|
[18ac46b] | 501 | def get_textnames(self): |
---|
[6f140f2] | 502 | """ |
---|
| 503 | Returns model name string as list |
---|
| 504 | """ |
---|
| 505 | return [self.model1_name, self.model2_name] |
---|
[49ab5d7] | 506 | |
---|
[6f140f2] | 507 | def write_string(self, fname, name1, name2): |
---|
| 508 | """ |
---|
| 509 | Write and Save file |
---|
| 510 | """ |
---|
[49ab5d7] | 511 | self.fname = fname |
---|
[796c4d4] | 512 | description = self.desc_tcl.GetValue().lstrip().rstrip() |
---|
| 513 | if description == '': |
---|
[7b1f4e3] | 514 | description = name1 + self._operator + name2 |
---|
| 515 | text = self._operator_choice.GetValue() |
---|
[c8654a3] | 516 | if text.count('+') > 0: |
---|
[d970df9] | 517 | factor = 'scale_factor' |
---|
| 518 | f_oper = '*' |
---|
| 519 | default_val = '1.0' |
---|
[c8654a3] | 520 | else: |
---|
[d970df9] | 521 | factor = 'BackGround' |
---|
| 522 | f_oper = '+' |
---|
| 523 | default_val = '0.0' |
---|
[796c4d4] | 524 | path = self.fname |
---|
[6f140f2] | 525 | try: |
---|
[49ab5d7] | 526 | out_f = open(path, 'w') |
---|
[2d50115] | 527 | except: |
---|
[6f140f2] | 528 | raise |
---|
| 529 | lines = SUM_TEMPLATE.split('\n') |
---|
| 530 | for line in lines: |
---|
[d970df9] | 531 | try: |
---|
| 532 | if line.count("scale_factor"): |
---|
| 533 | line = line.replace('scale_factor', factor) |
---|
| 534 | #print "scale_factor", line |
---|
| 535 | if line.count("= %s"): |
---|
| 536 | out_f.write(line % (default_val) + "\n") |
---|
| 537 | elif line.count("import Model as P1"): |
---|
| 538 | if self.is_p1_custom: |
---|
| 539 | line = line.replace('#', '') |
---|
| 540 | out_f.write(line % name1 + "\n") |
---|
| 541 | else: |
---|
| 542 | out_f.write(line + "\n") |
---|
| 543 | elif line.count("import %s as P1"): |
---|
| 544 | if not self.is_p1_custom: |
---|
| 545 | line = line.replace('#', '') |
---|
| 546 | out_f.write(line % (name1, name1) + "\n") |
---|
| 547 | else: |
---|
| 548 | out_f.write(line + "\n") |
---|
| 549 | elif line.count("import Model as P2"): |
---|
| 550 | if self.is_p2_custom: |
---|
| 551 | line = line.replace('#', '') |
---|
| 552 | out_f.write(line % name2 + "\n") |
---|
| 553 | else: |
---|
| 554 | out_f.write(line + "\n") |
---|
| 555 | elif line.count("import %s as P2"): |
---|
| 556 | if not self.is_p2_custom: |
---|
| 557 | line = line.replace('#', '') |
---|
| 558 | out_f.write(line % (name2, name2) + "\n") |
---|
| 559 | else: |
---|
| 560 | out_f.write(line + "\n") |
---|
| 561 | elif line.count("self.description = '%s'"): |
---|
| 562 | out_f.write(line % description + "\n") |
---|
| 563 | #elif line.count("run") and line.count("%s"): |
---|
[7b1f4e3] | 564 | # out_f.write(line % self._operator + "\n") |
---|
[d970df9] | 565 | #elif line.count("evalDistribution") and line.count("%s"): |
---|
[7b1f4e3] | 566 | # out_f.write(line % self._operator + "\n") |
---|
[d970df9] | 567 | elif line.count("return") and line.count("%s") == 2: |
---|
| 568 | #print "line return", line |
---|
[7b1f4e3] | 569 | out_f.write(line % (f_oper, self._operator) + "\n") |
---|
[d970df9] | 570 | elif line.count("out2")and line.count("%s"): |
---|
[7b1f4e3] | 571 | out_f.write(line % self._operator + "\n") |
---|
[d970df9] | 572 | else: |
---|
| 573 | out_f.write(line + "\n") |
---|
| 574 | except: |
---|
| 575 | raise |
---|
[796c4d4] | 576 | out_f.close() |
---|
| 577 | #else: |
---|
| 578 | # msg = "Name exists already." |
---|
[49ab5d7] | 579 | |
---|
[6f140f2] | 580 | def compile_file(self, path): |
---|
| 581 | """ |
---|
| 582 | Compile the file in the path |
---|
| 583 | """ |
---|
[796c4d4] | 584 | path = self.fname |
---|
[18ac46b] | 585 | _compile_file(path) |
---|
[49ab5d7] | 586 | |
---|
[6f140f2] | 587 | def delete_file(self, path): |
---|
| 588 | """ |
---|
| 589 | Delete file in the path |
---|
| 590 | """ |
---|
[18ac46b] | 591 | _delete_file(path) |
---|
[6f140f2] | 592 | |
---|
| 593 | |
---|
| 594 | class EditorPanel(wx.ScrolledWindow): |
---|
| 595 | """ |
---|
| 596 | Custom model function editor |
---|
| 597 | """ |
---|
| 598 | def __init__(self, parent, base, path, title, *args, **kwds): |
---|
| 599 | kwds['name'] = title |
---|
[18ac46b] | 600 | # kwds["size"] = (EDITOR_WIDTH, EDITOR_HEIGTH) |
---|
[d970df9] | 601 | kwds["style"] = wx.FULL_REPAINT_ON_RESIZE |
---|
[6f140f2] | 602 | wx.ScrolledWindow.__init__(self, parent, *args, **kwds) |
---|
[2d50115] | 603 | self.SetScrollbars(1,1,1,1) |
---|
[6f140f2] | 604 | self.parent = parent |
---|
| 605 | self.base = base |
---|
| 606 | self.path = path |
---|
| 607 | self.font = wx.SystemSettings_GetFont(wx.SYS_SYSTEM_FONT) |
---|
| 608 | self.font.SetPointSize(10) |
---|
| 609 | self.reader = None |
---|
| 610 | self.name = 'untitled' |
---|
[796c4d4] | 611 | self.overwrite_name = False |
---|
[d83549c] | 612 | self.is_2d = False |
---|
[6f140f2] | 613 | self.fname = None |
---|
[7b1f4e3] | 614 | self.main_sizer = None |
---|
| 615 | self.name_sizer = None |
---|
| 616 | self.name_hsizer = None |
---|
[18ac46b] | 617 | self.name_tcl = None |
---|
[7b1f4e3] | 618 | self.desc_sizer = None |
---|
[18ac46b] | 619 | self.desc_tcl = None |
---|
[7b1f4e3] | 620 | self.param_sizer = None |
---|
[18ac46b] | 621 | self.param_tcl = None |
---|
[7b1f4e3] | 622 | self.function_sizer = None |
---|
| 623 | self.func_horizon_sizer = None |
---|
| 624 | self.button_sizer = None |
---|
[6f140f2] | 625 | self.param_strings = '' |
---|
| 626 | self.function_strings = '' |
---|
| 627 | self._notes = "" |
---|
[41a8cb3] | 628 | self._msg_box = None |
---|
| 629 | self.msg_sizer = None |
---|
[6f140f2] | 630 | self.warning = "" |
---|
| 631 | self._description = "New Custom Model" |
---|
[318b5bbb] | 632 | self.function_tcl = None |
---|
[18ac46b] | 633 | self.math_combo = None |
---|
| 634 | self.bt_apply = None |
---|
| 635 | self.bt_close = None |
---|
[6f140f2] | 636 | #self._default_save_location = os.getcwd() |
---|
| 637 | self._do_layout() |
---|
[18ac46b] | 638 | |
---|
[6f140f2] | 639 | |
---|
[49ab5d7] | 640 | |
---|
[6f140f2] | 641 | def _define_structure(self): |
---|
| 642 | """ |
---|
[49ab5d7] | 643 | define initial sizer |
---|
[6f140f2] | 644 | """ |
---|
| 645 | #w, h = self.parent.GetSize() |
---|
| 646 | self.main_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
| 647 | self.name_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
| 648 | self.name_hsizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 649 | self.desc_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
| 650 | self.param_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
| 651 | self.function_sizer = wx.BoxSizer(wx.VERTICAL) |
---|
[318b5bbb] | 652 | self.func_horizon_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
[6f140f2] | 653 | self.button_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
[41a8cb3] | 654 | self.msg_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
[49ab5d7] | 655 | |
---|
[6f140f2] | 656 | def _layout_name(self): |
---|
| 657 | """ |
---|
| 658 | Do the layout for file/function name related widgets |
---|
| 659 | """ |
---|
| 660 | #title name [string] |
---|
[49ab5d7] | 661 | name_txt = wx.StaticText(self, -1, 'Function Name : ') |
---|
[6f140f2] | 662 | overwrite_cb = wx.CheckBox(self, -1, "Overwrite?", (10, 10)) |
---|
[796c4d4] | 663 | overwrite_cb.SetValue(False) |
---|
[6f140f2] | 664 | overwrite_cb.SetToolTipString("Overwrite it if already exists?") |
---|
| 665 | wx.EVT_CHECKBOX(self, overwrite_cb.GetId(), self.on_over_cb) |
---|
[796c4d4] | 666 | #overwrite_cb.Show(False) |
---|
[49ab5d7] | 667 | self.name_tcl = wx.TextCtrl(self, -1, size=(PANEL_WIDTH * 3 / 5, -1)) |
---|
[6f140f2] | 668 | self.name_tcl.Bind(wx.EVT_TEXT_ENTER, self.on_change_name) |
---|
[7434020] | 669 | self.name_tcl.SetValue('MyFunction') |
---|
[6f140f2] | 670 | self.name_tcl.SetFont(self.font) |
---|
| 671 | hint_name = "Unique Model Function Name." |
---|
| 672 | self.name_tcl.SetToolTipString(hint_name) |
---|
[49ab5d7] | 673 | self.name_hsizer.AddMany([(self.name_tcl, 0, wx.LEFT | wx.TOP, 0), |
---|
[18ac46b] | 674 | (overwrite_cb, 0, wx.LEFT, 20)]) |
---|
[49ab5d7] | 675 | self.name_sizer.AddMany([(name_txt, 0, wx.LEFT | wx.TOP, 10), |
---|
[18ac46b] | 676 | (self.name_hsizer, 0, |
---|
| 677 | wx.LEFT | wx.TOP | wx.BOTTOM, 10)]) |
---|
[49ab5d7] | 678 | |
---|
| 679 | |
---|
[6f140f2] | 680 | def _layout_description(self): |
---|
| 681 | """ |
---|
| 682 | Do the layout for description related widgets |
---|
| 683 | """ |
---|
| 684 | #title name [string] |
---|
[49ab5d7] | 685 | desc_txt = wx.StaticText(self, -1, 'Description (optional) : ') |
---|
| 686 | self.desc_tcl = wx.TextCtrl(self, -1, size=(PANEL_WIDTH * 3 / 5, -1)) |
---|
[6f140f2] | 687 | self.desc_tcl.SetValue('') |
---|
| 688 | hint_desc = "Write a short description of the model function." |
---|
| 689 | self.desc_tcl.SetToolTipString(hint_desc) |
---|
[49ab5d7] | 690 | self.desc_sizer.AddMany([(desc_txt, 0, wx.LEFT | wx.TOP, 10), |
---|
[18ac46b] | 691 | (self.desc_tcl, 0, |
---|
| 692 | wx.LEFT | wx.TOP | wx.BOTTOM, 10)]) |
---|
[6f140f2] | 693 | def _layout_param(self): |
---|
| 694 | """ |
---|
| 695 | Do the layout for parameter related widgets |
---|
| 696 | """ |
---|
[49ab5d7] | 697 | param_txt = wx.StaticText(self, -1, 'Fit Parameters (if any): ') |
---|
| 698 | |
---|
[6f140f2] | 699 | param_tip = "#Set the parameters and initial values.\n" |
---|
| 700 | param_tip += "#Example:\n" |
---|
| 701 | param_tip += "A = 1\nB = 1" |
---|
| 702 | #param_txt.SetToolTipString(param_tip) |
---|
[18ac46b] | 703 | newid = wx.NewId() |
---|
| 704 | self.param_tcl = EditWindow(self, newid, wx.DefaultPosition, |
---|
| 705 | wx.DefaultSize, |
---|
| 706 | wx.CLIP_CHILDREN | wx.SUNKEN_BORDER) |
---|
[b25caad] | 707 | self.param_tcl.setDisplayLineNumbers(True) |
---|
[6f140f2] | 708 | self.param_tcl.SetToolTipString(param_tip) |
---|
[318b5bbb] | 709 | |
---|
[6f140f2] | 710 | self.param_sizer.AddMany([(param_txt, 0, wx.LEFT, 10), |
---|
[18ac46b] | 711 | (self.param_tcl, 1, wx.EXPAND | wx.ALL, 10)]) |
---|
[49ab5d7] | 712 | |
---|
[6f140f2] | 713 | def _layout_function(self): |
---|
| 714 | """ |
---|
| 715 | Do the layout for function related widgets |
---|
| 716 | """ |
---|
[49ab5d7] | 717 | function_txt = wx.StaticText(self, -1, 'Function(x) : ') |
---|
[6f140f2] | 718 | hint_function = "#Example:\n" |
---|
[e499ca0] | 719 | hint_function += "if x <= 0:\n" |
---|
[b25caad] | 720 | hint_function += " y = A + B\n" |
---|
| 721 | hint_function += "else:\n" |
---|
[e499ca0] | 722 | hint_function += " y = A + B * cos(2 * pi * x)\n" |
---|
[b25caad] | 723 | hint_function += "return y\n" |
---|
[318b5bbb] | 724 | math_txt = wx.StaticText(self, -1, '*Useful math functions: ') |
---|
| 725 | math_combo = self._fill_math_combo() |
---|
[49ab5d7] | 726 | |
---|
[18ac46b] | 727 | newid = wx.NewId() |
---|
| 728 | self.function_tcl = EditWindow(self, newid, wx.DefaultPosition, |
---|
| 729 | wx.DefaultSize, |
---|
| 730 | wx.CLIP_CHILDREN | wx.SUNKEN_BORDER) |
---|
[b25caad] | 731 | self.function_tcl.setDisplayLineNumbers(True) |
---|
[6f140f2] | 732 | self.function_tcl.SetToolTipString(hint_function) |
---|
[49ab5d7] | 733 | |
---|
[318b5bbb] | 734 | self.func_horizon_sizer.Add(function_txt) |
---|
| 735 | self.func_horizon_sizer.Add(math_txt, 0, wx.LEFT, 250) |
---|
| 736 | self.func_horizon_sizer.Add(math_combo, 0, wx.LEFT, 10) |
---|
| 737 | |
---|
| 738 | self.function_sizer.Add(self.func_horizon_sizer, 0, wx.LEFT, 10) |
---|
[49ab5d7] | 739 | self.function_sizer.Add(self.function_tcl, 1, wx.EXPAND | wx.ALL, 10) |
---|
| 740 | |
---|
[41a8cb3] | 741 | def _layout_msg(self): |
---|
| 742 | """ |
---|
| 743 | Layout msg |
---|
| 744 | """ |
---|
[49ab5d7] | 745 | self._msg_box = wx.StaticText(self, -1, self._notes, |
---|
[657e52c] | 746 | size=(PANEL_WIDTH, -1)) |
---|
[49ab5d7] | 747 | self.msg_sizer.Add(self._msg_box, 0, wx.LEFT, 10) |
---|
| 748 | |
---|
| 749 | def _layout_button(self): |
---|
[6f140f2] | 750 | """ |
---|
| 751 | Do the layout for the button widgets |
---|
[49ab5d7] | 752 | """ |
---|
[6f140f2] | 753 | self.bt_apply = wx.Button(self, -1, "Apply", size=(_BOX_WIDTH, -1)) |
---|
| 754 | self.bt_apply.SetToolTipString("Save changes into the imported data.") |
---|
| 755 | self.bt_apply.Bind(wx.EVT_BUTTON, self.on_click_apply) |
---|
[49ab5d7] | 756 | |
---|
[6f140f2] | 757 | self.bt_close = wx.Button(self, -1, 'Close', size=(_BOX_WIDTH, -1)) |
---|
| 758 | self.bt_close.Bind(wx.EVT_BUTTON, self.on_close) |
---|
| 759 | self.bt_close.SetToolTipString("Close this panel.") |
---|
[49ab5d7] | 760 | |
---|
| 761 | self.button_sizer.AddMany([(self.bt_apply, 0, |
---|
[6f140f2] | 762 | wx.LEFT, EDITOR_WIDTH * 0.8), |
---|
[49ab5d7] | 763 | (self.bt_close, 0, |
---|
| 764 | wx.LEFT | wx.BOTTOM, 15)]) |
---|
| 765 | |
---|
[6f140f2] | 766 | def _do_layout(self): |
---|
| 767 | """ |
---|
| 768 | Draw the current panel |
---|
| 769 | """ |
---|
| 770 | self._define_structure() |
---|
| 771 | self._layout_name() |
---|
| 772 | self._layout_description() |
---|
| 773 | self._layout_param() |
---|
| 774 | self._layout_function() |
---|
[41a8cb3] | 775 | self._layout_msg() |
---|
[6f140f2] | 776 | self._layout_button() |
---|
[18ac46b] | 777 | self.main_sizer.AddMany([(self.name_sizer, 0, wx.EXPAND | wx.ALL, 5), |
---|
[49ab5d7] | 778 | (wx.StaticLine(self), 0, |
---|
[18ac46b] | 779 | wx.ALL | wx.EXPAND, 5), |
---|
| 780 | (self.desc_sizer, 0, wx.EXPAND | wx.ALL, 5), |
---|
[49ab5d7] | 781 | (wx.StaticLine(self), 0, |
---|
[18ac46b] | 782 | wx.ALL | wx.EXPAND, 5), |
---|
| 783 | (self.param_sizer, 1, wx.EXPAND | wx.ALL, 5), |
---|
[49ab5d7] | 784 | (wx.StaticLine(self), 0, |
---|
[18ac46b] | 785 | wx.ALL | wx.EXPAND, 5), |
---|
| 786 | (self.function_sizer, 2, |
---|
| 787 | wx.EXPAND | wx.ALL, 5), |
---|
[49ab5d7] | 788 | (wx.StaticLine(self), 0, |
---|
[18ac46b] | 789 | wx.ALL | wx.EXPAND, 5), |
---|
| 790 | (self.msg_sizer, 0, wx.EXPAND | wx.ALL, 5), |
---|
| 791 | (self.button_sizer, 0, wx.EXPAND | wx.ALL, 5)]) |
---|
[6f140f2] | 792 | self.SetSizer(self.main_sizer) |
---|
| 793 | self.SetAutoLayout(True) |
---|
[318b5bbb] | 794 | |
---|
| 795 | def _fill_math_combo(self): |
---|
| 796 | """ |
---|
| 797 | Fill up the math combo box |
---|
| 798 | """ |
---|
[49ab5d7] | 799 | self.math_combo = wx.ComboBox(self, -1, size=(100, -1), |
---|
| 800 | style=wx.CB_READONLY) |
---|
[318b5bbb] | 801 | for item in dir(math): |
---|
| 802 | if item.count("_") < 1: |
---|
| 803 | try: |
---|
[49ab5d7] | 804 | exec "float(math.%s)" % item |
---|
[318b5bbb] | 805 | self.math_combo.Append(str(item)) |
---|
| 806 | except: |
---|
[49ab5d7] | 807 | self.math_combo.Append(str(item) + "()") |
---|
[318b5bbb] | 808 | self.math_combo.Bind(wx.EVT_COMBOBOX, self._on_math_select) |
---|
| 809 | self.math_combo.SetSelection(0) |
---|
| 810 | return self.math_combo |
---|
[49ab5d7] | 811 | |
---|
[318b5bbb] | 812 | def _on_math_select(self, event): |
---|
| 813 | """ |
---|
| 814 | On math selection on ComboBox |
---|
| 815 | """ |
---|
| 816 | event.Skip() |
---|
[2d50115] | 817 | label = self.math_combo.GetValue() |
---|
[318b5bbb] | 818 | self.function_tcl.SetFocus() |
---|
| 819 | # Put the text at the cursor position |
---|
| 820 | pos = self.function_tcl.GetCurrentPos() |
---|
[49ab5d7] | 821 | self.function_tcl.InsertText(pos, label) |
---|
[2d50115] | 822 | # Put the cursor at appropriate position |
---|
[318b5bbb] | 823 | length = len(label) |
---|
[2d50115] | 824 | print length |
---|
| 825 | if label[length-1] == ')': |
---|
[318b5bbb] | 826 | length -= 1 |
---|
| 827 | f_pos = pos + length |
---|
[49ab5d7] | 828 | self.function_tcl.GotoPos(f_pos) |
---|
[318b5bbb] | 829 | |
---|
[6f140f2] | 830 | def get_notes(self): |
---|
| 831 | """ |
---|
| 832 | return notes |
---|
| 833 | """ |
---|
| 834 | return self._notes |
---|
[49ab5d7] | 835 | |
---|
[6f140f2] | 836 | def on_change_name(self, event=None): |
---|
| 837 | """ |
---|
| 838 | Change name |
---|
| 839 | """ |
---|
| 840 | if event is not None: |
---|
| 841 | event.Skip() |
---|
| 842 | self.name_tcl.SetBackgroundColour('white') |
---|
| 843 | self.Refresh() |
---|
[49ab5d7] | 844 | |
---|
[6f140f2] | 845 | def check_name(self): |
---|
| 846 | """ |
---|
| 847 | Check name if exist already |
---|
| 848 | """ |
---|
[41a8cb3] | 849 | self._notes = '' |
---|
[6f140f2] | 850 | self.on_change_name(None) |
---|
| 851 | plugin_dir = self.path |
---|
| 852 | list_fnames = os.listdir(plugin_dir) |
---|
| 853 | # function/file name |
---|
| 854 | title = self.name_tcl.GetValue().lstrip().rstrip() |
---|
| 855 | self.name = title |
---|
| 856 | t_fname = title + '.py' |
---|
| 857 | if not self.overwrite_name: |
---|
| 858 | if t_fname in list_fnames: |
---|
| 859 | self.name_tcl.SetBackgroundColour('pink') |
---|
| 860 | return False |
---|
| 861 | self.fname = os.path.join(plugin_dir, t_fname) |
---|
[41a8cb3] | 862 | s_title = title |
---|
| 863 | if len(title) > 20: |
---|
| 864 | s_title = title[0:19] + '...' |
---|
[657e52c] | 865 | self._notes += "Model function name is set " |
---|
[41a8cb3] | 866 | self._notes += "to %s. \n" % str(s_title) |
---|
[6f140f2] | 867 | return True |
---|
[49ab5d7] | 868 | |
---|
[6f140f2] | 869 | def on_over_cb(self, event): |
---|
| 870 | """ |
---|
| 871 | Set overwrite name flag on cb event |
---|
| 872 | """ |
---|
| 873 | if event is not None: |
---|
| 874 | event.Skip() |
---|
[18ac46b] | 875 | cb_value = event.GetEventObject() |
---|
| 876 | self.overwrite_name = cb_value.GetValue() |
---|
[49ab5d7] | 877 | |
---|
[6f140f2] | 878 | def on_click_apply(self, event): |
---|
[49ab5d7] | 879 | """ |
---|
[18ac46b] | 880 | Changes are saved in data object imported to edit. |
---|
[2d50115] | 881 | |
---|
[18ac46b] | 882 | checks firs for valid name, then if it already exists then checks |
---|
| 883 | that a function was entered and finally that if entered it contains at |
---|
[2d50115] | 884 | least a return statement. If all passes writes file then tries to |
---|
[18ac46b] | 885 | compile. If compile fails or import module fails or run method fails |
---|
| 886 | tries to remove any .py and pyc files that may have been created and |
---|
| 887 | sets error message. |
---|
[2d50115] | 888 | |
---|
| 889 | :todo this code still could do with a careful going over to clean |
---|
[18ac46b] | 890 | up and simplify. the non GUI methods such as this one should be removed |
---|
| 891 | to computational code of SasView. Most of those computational methods |
---|
| 892 | would be the same for both the simple editors. |
---|
[6f140f2] | 893 | """ |
---|
| 894 | #must post event here |
---|
| 895 | event.Skip() |
---|
[18ac46b] | 896 | name = self.name_tcl.GetValue().lstrip().rstrip() |
---|
[6f140f2] | 897 | info = 'Info' |
---|
[6057915] | 898 | msg = '' |
---|
[6f140f2] | 899 | # Sort out the errors if occur |
---|
[18ac46b] | 900 | # First check for valid python name then if the name already exists |
---|
[2d50115] | 901 | if not re.match('^[A-Za-z0-9_]*$', name): |
---|
[18ac46b] | 902 | msg = "not a valid python name. Name must include only alpha \n" |
---|
| 903 | msg += "numeric or underline characters and no spaces" |
---|
| 904 | elif self.check_name(): |
---|
[6f140f2] | 905 | description = self.desc_tcl.GetValue() |
---|
| 906 | param_str = self.param_tcl.GetText() |
---|
| 907 | func_str = self.function_tcl.GetText() |
---|
| 908 | # No input for the model function |
---|
[49ab5d7] | 909 | if func_str.lstrip().rstrip(): |
---|
[0a90d92] | 910 | if func_str.count('return') > 0: |
---|
[49ab5d7] | 911 | self.write_file(self.fname, description, param_str, |
---|
[18ac46b] | 912 | func_str) |
---|
| 913 | tr_msg = _compile_file(self.fname) |
---|
[0ed400d] | 914 | msg = str(tr_msg.__str__()) |
---|
[0a90d92] | 915 | # Compile error |
---|
| 916 | if msg: |
---|
[0ed400d] | 917 | msg.replace(" ", "\n") |
---|
[49ab5d7] | 918 | msg += "\nCompiling Failed" |
---|
[2d50115] | 919 | try: |
---|
| 920 | # try to remove pyc file if exists |
---|
| 921 | _delete_file(self.fname) |
---|
| 922 | _delete_file(self.fname + "c") |
---|
| 923 | except: |
---|
| 924 | pass |
---|
[6f140f2] | 925 | else: |
---|
[0a90d92] | 926 | msg = "Error: The func(x) must 'return' a value at least.\n" |
---|
| 927 | msg += "For example: \n\nreturn 2*x" |
---|
[6f140f2] | 928 | else: |
---|
| 929 | msg = 'Error: Function is not defined.' |
---|
| 930 | else: |
---|
| 931 | msg = "Name exists already." |
---|
[18ac46b] | 932 | # Prepare the messagebox |
---|
[0ed400d] | 933 | if self.base != None and not msg: |
---|
| 934 | self.base.update_custom_combo() |
---|
[49ab5d7] | 935 | Model = None |
---|
[18ac46b] | 936 | try: |
---|
| 937 | exec "from %s import Model" % name |
---|
| 938 | except: |
---|
| 939 | msg = 'new model fails to import in python' |
---|
[2d50115] | 940 | try: |
---|
| 941 | # try to remove pyc file if exists |
---|
| 942 | _delete_file(self.fname + "c") |
---|
| 943 | except: |
---|
| 944 | pass |
---|
[18ac46b] | 945 | if self.base != None and not msg: |
---|
[0ed400d] | 946 | try: |
---|
[49ab5d7] | 947 | Model().run(0.01) |
---|
[0ed400d] | 948 | except: |
---|
[18ac46b] | 949 | msg = "new model fails on run method:" |
---|
[0ed400d] | 950 | _, value, _ = sys.exc_info() |
---|
[49ab5d7] | 951 | msg += "in %s:\n%s\n" % (name, value) |
---|
[2d50115] | 952 | try: |
---|
| 953 | # try to remove pyc file if exists |
---|
| 954 | _delete_file(self.fname + "c") |
---|
| 955 | except: |
---|
| 956 | pass |
---|
[18ac46b] | 957 | # Prepare the messagebox |
---|
[0ed400d] | 958 | if msg: |
---|
[6f140f2] | 959 | info = 'Error' |
---|
[49ab5d7] | 960 | color = 'red' |
---|
[0ed400d] | 961 | else: |
---|
[657e52c] | 962 | msg = "Successful! " |
---|
[0ed400d] | 963 | msg += " " + self._notes |
---|
[ea5fa58] | 964 | msg += " Please look for it in the Customized Models." |
---|
[0ed400d] | 965 | info = 'Info' |
---|
| 966 | color = 'blue' |
---|
[18ac46b] | 967 | self._msg_box.SetLabel(msg) |
---|
[41a8cb3] | 968 | self._msg_box.SetForegroundColour(color) |
---|
[18ac46b] | 969 | # Send msg to the top window |
---|
[6f140f2] | 970 | if self.base != None: |
---|
[18ac46b] | 971 | from sas.guiframe.events import StatusEvent |
---|
| 972 | wx.PostEvent(self.base.parent, StatusEvent(status=msg, info=info)) |
---|
[6f140f2] | 973 | self.warning = msg |
---|
| 974 | |
---|
[49ab5d7] | 975 | |
---|
| 976 | def write_file(self, fname, desc_str, param_str, func_str): |
---|
[6f140f2] | 977 | """ |
---|
| 978 | Write content in file |
---|
[49ab5d7] | 979 | |
---|
[6f140f2] | 980 | :param fname: full file path |
---|
| 981 | :param desc_str: content of the description strings |
---|
[49ab5d7] | 982 | :param param_str: content of params; Strings |
---|
[6f140f2] | 983 | :param func_str: content of func; Strings |
---|
[49ab5d7] | 984 | """ |
---|
[6f140f2] | 985 | try: |
---|
[49ab5d7] | 986 | out_f = open(fname, 'w') |
---|
[18ac46b] | 987 | except: |
---|
[6f140f2] | 988 | raise |
---|
[e499ca0] | 989 | # Prepare the content of the function |
---|
[6f140f2] | 990 | lines = CUSTOM_TEMPLATE.split('\n') |
---|
[f9feff3] | 991 | |
---|
[6f140f2] | 992 | has_scipy = func_str.count("scipy.") |
---|
[d83549c] | 993 | self.is_2d = func_str.count("#self.ndim = 2") |
---|
| 994 | line_2d = '' |
---|
| 995 | if self.is_2d: |
---|
| 996 | line_2d = CUSTOM_2D_TEMP.split('\n') |
---|
| 997 | line_test = TEST_TEMPLATE.split('\n') |
---|
[6f140f2] | 998 | local_params = '' |
---|
| 999 | spaces = ' '#8spaces |
---|
[d83549c] | 1000 | # write function here |
---|
[6f140f2] | 1001 | for line in lines: |
---|
[18ac46b] | 1002 | # The location where to put the strings is |
---|
[6f140f2] | 1003 | # hard-coded in the template as shown below. |
---|
| 1004 | if line.count("#self.params here"): |
---|
| 1005 | for param_line in param_str.split('\n'): |
---|
| 1006 | p_line = param_line.lstrip().rstrip() |
---|
| 1007 | if p_line: |
---|
| 1008 | p0_line = self.set_param_helper(p_line) |
---|
| 1009 | local_params += self.set_function_helper(p_line) |
---|
| 1010 | out_f.write(p0_line) |
---|
| 1011 | elif line.count("#local params here"): |
---|
| 1012 | if local_params: |
---|
| 1013 | out_f.write(local_params) |
---|
| 1014 | elif line.count("self.description = "): |
---|
[e499ca0] | 1015 | des0 = self.name + "\\n" |
---|
[6f140f2] | 1016 | desc = str(desc_str.lstrip().rstrip().replace('\"', '')) |
---|
[49ab5d7] | 1017 | out_f.write(line % (des0 + desc) + "\n") |
---|
[d83549c] | 1018 | elif line.count("def function(self, x=0.0%s):"): |
---|
| 1019 | if self.is_2d: |
---|
| 1020 | y_str = ', y=0.0' |
---|
[49ab5d7] | 1021 | out_f.write(line % y_str + "\n") |
---|
[d83549c] | 1022 | else: |
---|
[49ab5d7] | 1023 | out_f.write(line % '' + "\n") |
---|
[6f140f2] | 1024 | elif line.count("#function here"): |
---|
| 1025 | for func_line in func_str.split('\n'): |
---|
[46472c61] | 1026 | f_line = func_line.rstrip() |
---|
[6f140f2] | 1027 | if f_line: |
---|
| 1028 | out_f.write(spaces + f_line + "\n") |
---|
| 1029 | if not func_str: |
---|
[d83549c] | 1030 | dep_var = 'y' |
---|
| 1031 | if self.is_2d: |
---|
| 1032 | dep_var = 'z' |
---|
[49ab5d7] | 1033 | out_f.write(spaces + 'return %s' % dep_var + "\n") |
---|
[6f140f2] | 1034 | elif line.count("#import scipy?"): |
---|
| 1035 | if has_scipy: |
---|
| 1036 | out_f.write("import scipy" + "\n") |
---|
[316e231] | 1037 | #elif line.count("name = "): |
---|
| 1038 | # out_f.write(line % self.name + "\n") |
---|
[6f140f2] | 1039 | elif line: |
---|
| 1040 | out_f.write(line + "\n") |
---|
[d83549c] | 1041 | # run string for 2d |
---|
| 1042 | if line_2d: |
---|
| 1043 | for line in line_2d: |
---|
| 1044 | out_f.write(line + "\n") |
---|
| 1045 | # Test strins |
---|
| 1046 | for line in line_test: |
---|
| 1047 | out_f.write(line + "\n") |
---|
[49ab5d7] | 1048 | |
---|
| 1049 | out_f.close() |
---|
| 1050 | |
---|
| 1051 | def set_param_helper(self, line): |
---|
[6f140f2] | 1052 | """ |
---|
| 1053 | Get string in line to define the params dictionary |
---|
[49ab5d7] | 1054 | |
---|
[6f140f2] | 1055 | :param line: one line of string got from the param_str |
---|
| 1056 | """ |
---|
| 1057 | params_str = '' |
---|
| 1058 | spaces = ' '#8spaces |
---|
| 1059 | items = line.split(";") |
---|
| 1060 | for item in items: |
---|
| 1061 | name = item.split("=")[0].lstrip().rstrip() |
---|
| 1062 | try: |
---|
[e499ca0] | 1063 | value = item.split("=")[1].lstrip().rstrip() |
---|
[6f140f2] | 1064 | float(value) |
---|
| 1065 | except: |
---|
[e499ca0] | 1066 | value = 1.0 # default |
---|
[49ab5d7] | 1067 | params_str += spaces + "self.params['%s'] = %s\n" % (name, value) |
---|
| 1068 | |
---|
[6f140f2] | 1069 | return params_str |
---|
| 1070 | |
---|
[49ab5d7] | 1071 | def set_function_helper(self, line): |
---|
[6f140f2] | 1072 | """ |
---|
| 1073 | Get string in line to define the local params |
---|
[49ab5d7] | 1074 | |
---|
[6f140f2] | 1075 | :param line: one line of string got from the param_str |
---|
| 1076 | """ |
---|
| 1077 | params_str = '' |
---|
| 1078 | spaces = ' '#8spaces |
---|
| 1079 | items = line.split(";") |
---|
| 1080 | for item in items: |
---|
| 1081 | name = item.split("=")[0].lstrip().rstrip() |
---|
[49ab5d7] | 1082 | params_str += spaces + "%s = self.params['%s']\n" % (name, name) |
---|
[6f140f2] | 1083 | return params_str |
---|
[49ab5d7] | 1084 | |
---|
[6f140f2] | 1085 | def get_warning(self): |
---|
| 1086 | """ |
---|
[49ab5d7] | 1087 | Get the warning msg |
---|
[6f140f2] | 1088 | """ |
---|
| 1089 | return self.warning |
---|
[49ab5d7] | 1090 | |
---|
[6f140f2] | 1091 | def on_close(self, event): |
---|
| 1092 | """ |
---|
| 1093 | leave data as it is and close |
---|
| 1094 | """ |
---|
[6057915] | 1095 | self.parent.Show(False)#Close() |
---|
[6f140f2] | 1096 | event.Skip() |
---|
[49ab5d7] | 1097 | |
---|
[6f140f2] | 1098 | class EditorWindow(wx.Frame): |
---|
[d970df9] | 1099 | """ |
---|
| 1100 | Editor Window |
---|
| 1101 | """ |
---|
[49ab5d7] | 1102 | def __init__(self, parent, base, path, title, |
---|
[6f140f2] | 1103 | size=(EDITOR_WIDTH, EDITOR_HEIGTH), *args, **kwds): |
---|
[d970df9] | 1104 | """ |
---|
| 1105 | Init |
---|
| 1106 | """ |
---|
[6f140f2] | 1107 | kwds["title"] = title |
---|
| 1108 | kwds["size"] = size |
---|
| 1109 | wx.Frame.__init__(self, parent=None, *args, **kwds) |
---|
| 1110 | self.parent = parent |
---|
[49ab5d7] | 1111 | self.panel = EditorPanel(parent=self, base=parent, |
---|
[6f140f2] | 1112 | path=path, title=title) |
---|
| 1113 | self.Show(True) |
---|
[2d50115] | 1114 | wx.EVT_CLOSE(self, self.on_close) |
---|
[49ab5d7] | 1115 | |
---|
[2d50115] | 1116 | def on_close(self, event): |
---|
[6f140f2] | 1117 | """ |
---|
| 1118 | On close event |
---|
| 1119 | """ |
---|
[6057915] | 1120 | self.Show(False) |
---|
| 1121 | #if self.parent != None: |
---|
| 1122 | # self.parent.new_model_frame = None |
---|
[18ac46b] | 1123 | #self.Destroy() |
---|
[6f140f2] | 1124 | |
---|
| 1125 | ## Templates for custom models |
---|
| 1126 | CUSTOM_TEMPLATE = """ |
---|
[79492222] | 1127 | from sas.models.pluginmodel import Model1DPlugin |
---|
[e499ca0] | 1128 | from math import * |
---|
[316e231] | 1129 | import os |
---|
| 1130 | import sys |
---|
[6f140f2] | 1131 | import numpy |
---|
| 1132 | #import scipy? |
---|
| 1133 | class Model(Model1DPlugin): |
---|
[49ab5d7] | 1134 | name = "" |
---|
[6f140f2] | 1135 | def __init__(self): |
---|
[49ab5d7] | 1136 | Model1DPlugin.__init__(self, name=self.name) |
---|
| 1137 | #set name same as file name |
---|
| 1138 | self.name = self.get_fname() |
---|
[6f140f2] | 1139 | #self.params here |
---|
| 1140 | self.description = "%s" |
---|
| 1141 | self.set_details() |
---|
[d83549c] | 1142 | def function(self, x=0.0%s): |
---|
[6f140f2] | 1143 | #local params here |
---|
[d83549c] | 1144 | #function here |
---|
| 1145 | """ |
---|
| 1146 | CUSTOM_2D_TEMP = """ |
---|
[f505470] | 1147 | def run(self, x=0.0, y=0.0): |
---|
[d83549c] | 1148 | if x.__class__.__name__ == 'list': |
---|
[f505470] | 1149 | x_val = x[0] |
---|
| 1150 | y_val = y[0]*0.0 |
---|
[d83549c] | 1151 | return self.function(x_val, y_val) |
---|
| 1152 | elif x.__class__.__name__ == 'tuple': |
---|
| 1153 | msg = "Tuples are not allowed as input to BaseComponent models" |
---|
| 1154 | raise ValueError, msg |
---|
| 1155 | else: |
---|
[7485cfa] | 1156 | return self.function(x, 0.0) |
---|
[d83549c] | 1157 | def runXY(self, x=0.0, y=0.0): |
---|
| 1158 | if x.__class__.__name__ == 'list': |
---|
| 1159 | return self.function(x, y) |
---|
| 1160 | elif x.__class__.__name__ == 'tuple': |
---|
| 1161 | msg = "Tuples are not allowed as input to BaseComponent models" |
---|
| 1162 | raise ValueError, msg |
---|
| 1163 | else: |
---|
| 1164 | return self.function(x, y) |
---|
| 1165 | def evalDistribution(self, qdist): |
---|
| 1166 | if qdist.__class__.__name__ == 'list': |
---|
| 1167 | msg = "evalDistribution expects a list of 2 ndarrays" |
---|
| 1168 | if len(qdist)!=2: |
---|
| 1169 | raise RuntimeError, msg |
---|
| 1170 | if qdist[0].__class__.__name__ != 'ndarray': |
---|
| 1171 | raise RuntimeError, msg |
---|
| 1172 | if qdist[1].__class__.__name__ != 'ndarray': |
---|
| 1173 | raise RuntimeError, msg |
---|
| 1174 | v_model = numpy.vectorize(self.runXY, otypes=[float]) |
---|
| 1175 | iq_array = v_model(qdist[0], qdist[1]) |
---|
| 1176 | return iq_array |
---|
| 1177 | elif qdist.__class__.__name__ == 'ndarray': |
---|
| 1178 | v_model = numpy.vectorize(self.runXY, otypes=[float]) |
---|
| 1179 | iq_array = v_model(qdist) |
---|
| 1180 | return iq_array |
---|
| 1181 | """ |
---|
[7485cfa] | 1182 | TEST_TEMPLATE = """ |
---|
[316e231] | 1183 | def get_fname(self): |
---|
| 1184 | path = sys._getframe().f_code.co_filename |
---|
| 1185 | basename = os.path.basename(path) |
---|
| 1186 | name, _ = os.path.splitext(basename) |
---|
| 1187 | return name |
---|
[7485cfa] | 1188 | ###################################################################### |
---|
[49ab5d7] | 1189 | ## THIS IS FOR TEST. DO NOT MODIFY THE FOLLOWING LINES!!!!!!!!!!!!!!!! |
---|
| 1190 | if __name__ == "__main__": |
---|
| 1191 | m= Model() |
---|
[7485cfa] | 1192 | out1 = m.runXY(0.0) |
---|
| 1193 | out2 = m.runXY(0.01) |
---|
| 1194 | isfine1 = numpy.isfinite(out1) |
---|
| 1195 | isfine2 = numpy.isfinite(out2) |
---|
| 1196 | print "Testing the value at Q = 0.0:" |
---|
| 1197 | print out1, " : finite? ", isfine1 |
---|
| 1198 | print "Testing the value at Q = 0.01:" |
---|
| 1199 | print out2, " : finite? ", isfine2 |
---|
| 1200 | if isfine1 and isfine2: |
---|
| 1201 | print "===> Simple Test: Passed!" |
---|
| 1202 | else: |
---|
| 1203 | print "===> Simple Test: Failed!" |
---|
| 1204 | """ |
---|
[6f140f2] | 1205 | SUM_TEMPLATE = """ |
---|
[0008f54] | 1206 | # A sample of an experimental model function for Sum/Multiply(Pmodel1,Pmodel2) |
---|
[6f140f2] | 1207 | import copy |
---|
[79492222] | 1208 | from sas.models.pluginmodel import Model1DPlugin |
---|
[6f140f2] | 1209 | # User can change the name of the model (only with single functional model) |
---|
[49ab5d7] | 1210 | #P1_model: |
---|
[79492222] | 1211 | #from sas.models.%s import %s as P1 |
---|
[49ab5d7] | 1212 | #from %s import Model as P1 |
---|
[6f140f2] | 1213 | |
---|
[49ab5d7] | 1214 | #P2_model: |
---|
[79492222] | 1215 | #from sas.models.%s import %s as P2 |
---|
[49ab5d7] | 1216 | #from %s import Model as P2 |
---|
[316e231] | 1217 | import os |
---|
| 1218 | import sys |
---|
[6f140f2] | 1219 | |
---|
| 1220 | class Model(Model1DPlugin): |
---|
| 1221 | name = "" |
---|
| 1222 | def __init__(self): |
---|
| 1223 | Model1DPlugin.__init__(self, name='') |
---|
| 1224 | p_model1 = P1() |
---|
| 1225 | p_model2 = P2() |
---|
| 1226 | ## Setting model name model description |
---|
[796c4d4] | 1227 | self.description = '%s' |
---|
[316e231] | 1228 | self.name = self.get_fname() |
---|
[796c4d4] | 1229 | if self.name.rstrip().lstrip() == '': |
---|
| 1230 | self.name = self._get_name(p_model1.name, p_model2.name) |
---|
| 1231 | if self.description.rstrip().lstrip() == '': |
---|
| 1232 | self.description = p_model1.name |
---|
| 1233 | self.description += p_model2.name |
---|
| 1234 | self.fill_description(p_model1, p_model2) |
---|
[6f140f2] | 1235 | |
---|
| 1236 | ## Define parameters |
---|
| 1237 | self.params = {} |
---|
| 1238 | |
---|
| 1239 | ## Parameter details [units, min, max] |
---|
| 1240 | self.details = {} |
---|
[318b5bbb] | 1241 | ## Magnetic Panrameters |
---|
| 1242 | self.magnetic_params = [] |
---|
[6f140f2] | 1243 | # non-fittable parameters |
---|
[49ab5d7] | 1244 | self.non_fittable = p_model1.non_fittable |
---|
| 1245 | self.non_fittable += p_model2.non_fittable |
---|
| 1246 | |
---|
| 1247 | ##models |
---|
[6f140f2] | 1248 | self.p_model1= p_model1 |
---|
| 1249 | self.p_model2= p_model2 |
---|
[49ab5d7] | 1250 | |
---|
| 1251 | |
---|
[6f140f2] | 1252 | ## dispersion |
---|
| 1253 | self._set_dispersion() |
---|
| 1254 | ## Define parameters |
---|
| 1255 | self._set_params() |
---|
[d970df9] | 1256 | ## New parameter:scaling_factor |
---|
| 1257 | self.params['scale_factor'] = %s |
---|
[49ab5d7] | 1258 | |
---|
[6f140f2] | 1259 | ## Parameter details [units, min, max] |
---|
| 1260 | self._set_details() |
---|
| 1261 | self.details['scale_factor'] = ['', None, None] |
---|
| 1262 | |
---|
[49ab5d7] | 1263 | |
---|
[6f140f2] | 1264 | #list of parameter that can be fitted |
---|
[49ab5d7] | 1265 | self._set_fixed_params() |
---|
[6f140f2] | 1266 | ## parameters with orientation |
---|
| 1267 | for item in self.p_model1.orientation_params: |
---|
| 1268 | new_item = "p1_" + item |
---|
| 1269 | if not new_item in self.orientation_params: |
---|
| 1270 | self.orientation_params.append(new_item) |
---|
[49ab5d7] | 1271 | |
---|
[6f140f2] | 1272 | for item in self.p_model2.orientation_params: |
---|
| 1273 | new_item = "p2_" + item |
---|
| 1274 | if not new_item in self.orientation_params: |
---|
| 1275 | self.orientation_params.append(new_item) |
---|
[318b5bbb] | 1276 | ## magnetic params |
---|
| 1277 | for item in self.p_model1.magnetic_params: |
---|
| 1278 | new_item = "p1_" + item |
---|
| 1279 | if not new_item in self.magnetic_params: |
---|
| 1280 | self.magnetic_params.append(new_item) |
---|
[49ab5d7] | 1281 | |
---|
[318b5bbb] | 1282 | for item in self.p_model2.magnetic_params: |
---|
| 1283 | new_item = "p2_" + item |
---|
| 1284 | if not new_item in self.magnetic_params: |
---|
| 1285 | self.magnetic_params.append(new_item) |
---|
[6f140f2] | 1286 | # get multiplicity if model provide it, else 1. |
---|
| 1287 | try: |
---|
| 1288 | multiplicity1 = p_model1.multiplicity |
---|
| 1289 | try: |
---|
| 1290 | multiplicity2 = p_model2.multiplicity |
---|
| 1291 | except: |
---|
| 1292 | multiplicity2 = 1 |
---|
| 1293 | except: |
---|
| 1294 | multiplicity1 = 1 |
---|
| 1295 | multiplicity2 = 1 |
---|
| 1296 | ## functional multiplicity of the model |
---|
[49ab5d7] | 1297 | self.multiplicity1 = multiplicity1 |
---|
| 1298 | self.multiplicity2 = multiplicity2 |
---|
| 1299 | self.multiplicity_info = [] |
---|
| 1300 | |
---|
[6f140f2] | 1301 | def _clone(self, obj): |
---|
| 1302 | obj.params = copy.deepcopy(self.params) |
---|
| 1303 | obj.description = copy.deepcopy(self.description) |
---|
| 1304 | obj.details = copy.deepcopy(self.details) |
---|
| 1305 | obj.dispersion = copy.deepcopy(self.dispersion) |
---|
| 1306 | obj.p_model1 = self.p_model1.clone() |
---|
| 1307 | obj.p_model2 = self.p_model2.clone() |
---|
| 1308 | #obj = copy.deepcopy(self) |
---|
| 1309 | return obj |
---|
[49ab5d7] | 1310 | |
---|
[6f140f2] | 1311 | def _get_name(self, name1, name2): |
---|
[2b4c8ca] | 1312 | p1_name = self._get_upper_name(name1) |
---|
| 1313 | if not p1_name: |
---|
| 1314 | p1_name = name1 |
---|
| 1315 | name = p1_name |
---|
[30bc3045] | 1316 | name += "_and_" |
---|
[2b4c8ca] | 1317 | p2_name = self._get_upper_name(name2) |
---|
| 1318 | if not p2_name: |
---|
| 1319 | p2_name = name2 |
---|
| 1320 | name += p2_name |
---|
[6f140f2] | 1321 | return name |
---|
[49ab5d7] | 1322 | |
---|
[6f140f2] | 1323 | def _get_upper_name(self, name=None): |
---|
| 1324 | if name == None: |
---|
| 1325 | return "" |
---|
| 1326 | upper_name = "" |
---|
| 1327 | str_name = str(name) |
---|
| 1328 | for index in range(len(str_name)): |
---|
| 1329 | if str_name[index].isupper(): |
---|
| 1330 | upper_name += str_name[index] |
---|
| 1331 | return upper_name |
---|
[49ab5d7] | 1332 | |
---|
[6f140f2] | 1333 | def _set_dispersion(self): |
---|
[49ab5d7] | 1334 | ##set dispersion only from p_model |
---|
[6f140f2] | 1335 | for name , value in self.p_model1.dispersion.iteritems(): |
---|
| 1336 | #if name.lower() not in self.p_model1.orientation_params: |
---|
| 1337 | new_name = "p1_" + name |
---|
[49ab5d7] | 1338 | self.dispersion[new_name]= value |
---|
[6f140f2] | 1339 | for name , value in self.p_model2.dispersion.iteritems(): |
---|
| 1340 | #if name.lower() not in self.p_model2.orientation_params: |
---|
| 1341 | new_name = "p2_" + name |
---|
[49ab5d7] | 1342 | self.dispersion[new_name]= value |
---|
| 1343 | |
---|
| 1344 | def function(self, x=0.0): |
---|
[6f140f2] | 1345 | return 0 |
---|
[49ab5d7] | 1346 | |
---|
[6f140f2] | 1347 | def getProfile(self): |
---|
| 1348 | try: |
---|
| 1349 | x,y = self.p_model1.getProfile() |
---|
| 1350 | except: |
---|
| 1351 | x = None |
---|
| 1352 | y = None |
---|
[49ab5d7] | 1353 | |
---|
[6f140f2] | 1354 | return x, y |
---|
[49ab5d7] | 1355 | |
---|
[6f140f2] | 1356 | def _set_params(self): |
---|
| 1357 | for name , value in self.p_model1.params.iteritems(): |
---|
| 1358 | # No 2D-supported |
---|
| 1359 | #if name not in self.p_model1.orientation_params: |
---|
| 1360 | new_name = "p1_" + name |
---|
| 1361 | self.params[new_name]= value |
---|
[49ab5d7] | 1362 | |
---|
[6f140f2] | 1363 | for name , value in self.p_model2.params.iteritems(): |
---|
| 1364 | # No 2D-supported |
---|
| 1365 | #if name not in self.p_model2.orientation_params: |
---|
| 1366 | new_name = "p2_" + name |
---|
| 1367 | self.params[new_name]= value |
---|
[49ab5d7] | 1368 | |
---|
[6f140f2] | 1369 | # Set "scale" as initializing |
---|
| 1370 | self._set_scale_factor() |
---|
[49ab5d7] | 1371 | |
---|
| 1372 | |
---|
[6f140f2] | 1373 | def _set_details(self): |
---|
| 1374 | for name ,detail in self.p_model1.details.iteritems(): |
---|
| 1375 | new_name = "p1_" + name |
---|
| 1376 | #if new_name not in self.orientation_params: |
---|
| 1377 | self.details[new_name]= detail |
---|
[49ab5d7] | 1378 | |
---|
[6f140f2] | 1379 | for name ,detail in self.p_model2.details.iteritems(): |
---|
| 1380 | new_name = "p2_" + name |
---|
| 1381 | #if new_name not in self.orientation_params: |
---|
| 1382 | self.details[new_name]= detail |
---|
[49ab5d7] | 1383 | |
---|
[6f140f2] | 1384 | def _set_scale_factor(self): |
---|
| 1385 | pass |
---|
[49ab5d7] | 1386 | |
---|
| 1387 | |
---|
[6f140f2] | 1388 | def setParam(self, name, value): |
---|
[0008f54] | 1389 | # set param to this (p1, p2) model |
---|
[6f140f2] | 1390 | self._setParamHelper(name, value) |
---|
[49ab5d7] | 1391 | |
---|
| 1392 | ## setParam to p model |
---|
[be0fe41] | 1393 | model_pre = '' |
---|
| 1394 | new_name = '' |
---|
| 1395 | name_split = name.split('_', 1) |
---|
| 1396 | if len(name_split) == 2: |
---|
| 1397 | model_pre = name.split('_', 1)[0] |
---|
| 1398 | new_name = name.split('_', 1)[1] |
---|
[6f140f2] | 1399 | if model_pre == "p1": |
---|
| 1400 | if new_name in self.p_model1.getParamList(): |
---|
| 1401 | self.p_model1.setParam(new_name, value) |
---|
| 1402 | elif model_pre == "p2": |
---|
| 1403 | if new_name in self.p_model2.getParamList(): |
---|
| 1404 | self.p_model2.setParam(new_name, value) |
---|
[be0fe41] | 1405 | elif name == 'scale_factor': |
---|
[6f140f2] | 1406 | self.params['scale_factor'] = value |
---|
| 1407 | else: |
---|
| 1408 | raise ValueError, "Model does not contain parameter %s" % name |
---|
[49ab5d7] | 1409 | |
---|
[6f140f2] | 1410 | def getParam(self, name): |
---|
| 1411 | # Look for dispersion parameters |
---|
| 1412 | toks = name.split('.') |
---|
| 1413 | if len(toks)==2: |
---|
| 1414 | for item in self.dispersion.keys(): |
---|
| 1415 | # 2D not supported |
---|
| 1416 | if item.lower()==toks[0].lower(): |
---|
| 1417 | for par in self.dispersion[item]: |
---|
| 1418 | if par.lower() == toks[1].lower(): |
---|
| 1419 | return self.dispersion[item][par] |
---|
| 1420 | else: |
---|
| 1421 | # Look for standard parameter |
---|
| 1422 | for item in self.params.keys(): |
---|
| 1423 | if item.lower()==name.lower(): |
---|
| 1424 | return self.params[item] |
---|
[49ab5d7] | 1425 | return |
---|
[6f140f2] | 1426 | #raise ValueError, "Model does not contain parameter %s" % name |
---|
[49ab5d7] | 1427 | |
---|
[6f140f2] | 1428 | def _setParamHelper(self, name, value): |
---|
| 1429 | # Look for dispersion parameters |
---|
| 1430 | toks = name.split('.') |
---|
| 1431 | if len(toks)== 2: |
---|
| 1432 | for item in self.dispersion.keys(): |
---|
| 1433 | if item.lower()== toks[0].lower(): |
---|
| 1434 | for par in self.dispersion[item]: |
---|
| 1435 | if par.lower() == toks[1].lower(): |
---|
| 1436 | self.dispersion[item][par] = value |
---|
| 1437 | return |
---|
| 1438 | else: |
---|
| 1439 | # Look for standard parameter |
---|
| 1440 | for item in self.params.keys(): |
---|
| 1441 | if item.lower()== name.lower(): |
---|
| 1442 | self.params[item] = value |
---|
| 1443 | return |
---|
[49ab5d7] | 1444 | |
---|
[6f140f2] | 1445 | raise ValueError, "Model does not contain parameter %s" % name |
---|
[49ab5d7] | 1446 | |
---|
| 1447 | |
---|
[6f140f2] | 1448 | def _set_fixed_params(self): |
---|
| 1449 | for item in self.p_model1.fixed: |
---|
| 1450 | new_item = "p1" + item |
---|
| 1451 | self.fixed.append(new_item) |
---|
| 1452 | for item in self.p_model2.fixed: |
---|
| 1453 | new_item = "p2" + item |
---|
| 1454 | self.fixed.append(new_item) |
---|
| 1455 | |
---|
| 1456 | self.fixed.sort() |
---|
[49ab5d7] | 1457 | |
---|
| 1458 | |
---|
[6f140f2] | 1459 | def run(self, x = 0.0): |
---|
| 1460 | self._set_scale_factor() |
---|
[d970df9] | 1461 | return self.params['scale_factor'] %s \ |
---|
[0008f54] | 1462 | (self.p_model1.run(x) %s self.p_model2.run(x)) |
---|
[49ab5d7] | 1463 | |
---|
[6f140f2] | 1464 | def runXY(self, x = 0.0): |
---|
| 1465 | self._set_scale_factor() |
---|
[d970df9] | 1466 | return self.params['scale_factor'] %s \ |
---|
[0008f54] | 1467 | (self.p_model1.runXY(x) %s self.p_model2.runXY(x)) |
---|
[49ab5d7] | 1468 | |
---|
| 1469 | ## Now (May27,10) directly uses the model eval function |
---|
[6f140f2] | 1470 | ## instead of the for-loop in Base Component. |
---|
| 1471 | def evalDistribution(self, x = []): |
---|
| 1472 | self._set_scale_factor() |
---|
[d970df9] | 1473 | return self.params['scale_factor'] %s \ |
---|
[0008f54] | 1474 | (self.p_model1.evalDistribution(x) %s \ |
---|
[6f140f2] | 1475 | self.p_model2.evalDistribution(x)) |
---|
| 1476 | |
---|
| 1477 | def set_dispersion(self, parameter, dispersion): |
---|
| 1478 | value= None |
---|
| 1479 | new_pre = parameter.split("_", 1)[0] |
---|
| 1480 | new_parameter = parameter.split("_", 1)[1] |
---|
| 1481 | try: |
---|
| 1482 | if new_pre == 'p1' and \ |
---|
| 1483 | new_parameter in self.p_model1.dispersion.keys(): |
---|
| 1484 | value= self.p_model1.set_dispersion(new_parameter, dispersion) |
---|
| 1485 | if new_pre == 'p2' and \ |
---|
| 1486 | new_parameter in self.p_model2.dispersion.keys(): |
---|
| 1487 | value= self.p_model2.set_dispersion(new_parameter, dispersion) |
---|
| 1488 | self._set_dispersion() |
---|
| 1489 | return value |
---|
| 1490 | except: |
---|
[49ab5d7] | 1491 | raise |
---|
[6f140f2] | 1492 | |
---|
| 1493 | def fill_description(self, p_model1, p_model2): |
---|
| 1494 | description = "" |
---|
[0008f54] | 1495 | description += "This model gives the summation or multiplication of" |
---|
| 1496 | description += "%s and %s. "% ( p_model1.name, p_model2.name ) |
---|
[6f140f2] | 1497 | self.description += description |
---|
[49ab5d7] | 1498 | |
---|
[316e231] | 1499 | def get_fname(self): |
---|
| 1500 | path = sys._getframe().f_code.co_filename |
---|
| 1501 | basename = os.path.basename(path) |
---|
| 1502 | name, _ = os.path.splitext(basename) |
---|
[49ab5d7] | 1503 | return name |
---|
| 1504 | |
---|
| 1505 | if __name__ == "__main__": |
---|
| 1506 | m1= Model() |
---|
| 1507 | #m1.setParam("p1_scale", 25) |
---|
[6f140f2] | 1508 | #m1.setParam("p1_length", 1000) |
---|
[49ab5d7] | 1509 | #m1.setParam("p2_scale", 100) |
---|
| 1510 | #m1.setParam("p2_rg", 100) |
---|
[6f140f2] | 1511 | out1 = m1.runXY(0.01) |
---|
| 1512 | |
---|
| 1513 | m2= Model() |
---|
[49ab5d7] | 1514 | #m2.p_model1.setParam("scale", 25) |
---|
| 1515 | #m2.p_model1.setParam("length", 1000) |
---|
[6f140f2] | 1516 | #m2.p_model2.setParam("scale", 100) |
---|
| 1517 | #m2.p_model2.setParam("rg", 100) |
---|
[0008f54] | 1518 | out2 = m2.p_model1.runXY(0.01) %s m2.p_model2.runXY(0.01)\n |
---|
[fdef956] | 1519 | print "My name is %s."% m1.name |
---|
[6f140f2] | 1520 | print out1, " = ", out2 |
---|
| 1521 | if out1 == out2: |
---|
| 1522 | print "===> Simple Test: Passed!" |
---|
| 1523 | else: |
---|
| 1524 | print "===> Simple Test: Failed!" |
---|
| 1525 | """ |
---|
[49ab5d7] | 1526 | |
---|
[18ac46b] | 1527 | if __name__ == "__main__": |
---|
[6f140f2] | 1528 | # app = wx.PySimpleApp() |
---|
[2d50115] | 1529 | main_app = wx.App() |
---|
| 1530 | main_frame = TextDialog(id=1, model_list=["SphereModel", "CylinderModel"], |
---|
[18ac46b] | 1531 | plugin_dir='../fitting/plugin_models') |
---|
[2d50115] | 1532 | main_frame.ShowModal() |
---|
| 1533 | main_app.MainLoop() |
---|
[7b1f4e3] | 1534 | |
---|
| 1535 | #if __name__ == "__main__": |
---|
| 1536 | # from sas.perspectives.fitting import models |
---|
| 1537 | # dir_path = models.find_plugins_dir() |
---|
| 1538 | # app = wx.App() |
---|
| 1539 | # window = EditorWindow(parent=None, base=None, path=dir_path, title="Editor") |
---|
| 1540 | # app.MainLoop() |
---|