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