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