1 | #TODO: add comments to document this module |
---|
2 | #TODO: clean-up the exception handling. |
---|
3 | |
---|
4 | #TODO: clean-up the FractalAbsModel and PowerLawAbsModel menu items. Those |
---|
5 | # model definitions do not belong here. They belong with the rest of the |
---|
6 | # models. |
---|
7 | |
---|
8 | import wx |
---|
9 | import wx.lib.newevent |
---|
10 | import imp |
---|
11 | import os,sys,math |
---|
12 | import os.path |
---|
13 | from sans.models.pluginmodel import Model1DPlugin |
---|
14 | (ModelEvent, EVT_MODEL) = wx.lib.newevent.NewEvent() |
---|
15 | from sans.guicomm.events import StatusEvent |
---|
16 | # Time is needed by the log method |
---|
17 | import time |
---|
18 | |
---|
19 | # Explicitly import from the pluginmodel module so that py2exe |
---|
20 | # places it in the distribution. The Model1DPlugin class is used |
---|
21 | # as the base class of plug-in models. |
---|
22 | from sans.models.pluginmodel import Model1DPlugin |
---|
23 | |
---|
24 | def log(message): |
---|
25 | out = open("plugins.log", 'a') |
---|
26 | out.write("%10g: %s\n" % (time.clock(), message)) |
---|
27 | out.close() |
---|
28 | |
---|
29 | def findModels(): |
---|
30 | log("looking for models in: %s/plugins" % os.getcwd()) |
---|
31 | if os.path.isdir('plugins'): |
---|
32 | return _findModels('plugins') |
---|
33 | return [] |
---|
34 | |
---|
35 | def _check_plugin(model, name): |
---|
36 | """ |
---|
37 | Do some checking before model adding plugins in the list |
---|
38 | @param model: class model to add into the plugin list |
---|
39 | @param name:name of the module plugin |
---|
40 | @return model: model if valid model or nothing if not valid |
---|
41 | """ |
---|
42 | #Check is the plugin is of type Model1DPlugin |
---|
43 | if not issubclass(model, Model1DPlugin): |
---|
44 | msg= "Plugin %s must be of type Model1DPlugin \n"%str(name) |
---|
45 | log(msg) |
---|
46 | return |
---|
47 | if model.__name__!="Model": |
---|
48 | msg= "Plugin %s class name must be Model \n"%str(name) |
---|
49 | log(msg) |
---|
50 | return |
---|
51 | try: |
---|
52 | new_instance= model() |
---|
53 | except: |
---|
54 | msg="Plugin %s error in __init__ \n\t: %s %s\n"%(str(name), |
---|
55 | str(sys.exc_type),sys.exc_value) |
---|
56 | log(msg) |
---|
57 | return |
---|
58 | |
---|
59 | new_instance= model() |
---|
60 | if hasattr(new_instance,"function"): |
---|
61 | try: |
---|
62 | value=new_instance.function() |
---|
63 | except: |
---|
64 | msg="Plugin %s: error writing function \n\t :%s %s\n "%(str(name), |
---|
65 | str(sys.exc_type),sys.exc_value) |
---|
66 | log(msg) |
---|
67 | return |
---|
68 | else: |
---|
69 | msg="Plugin %s needs a method called function \n"%str(name) |
---|
70 | log(msg) |
---|
71 | return |
---|
72 | return model |
---|
73 | |
---|
74 | |
---|
75 | def _findModels(dir): |
---|
76 | # List of plugin objects |
---|
77 | plugins = [] |
---|
78 | # Go through files in plug-in directory |
---|
79 | try: |
---|
80 | list = os.listdir(dir) |
---|
81 | for item in list: |
---|
82 | toks = os.path.splitext(os.path.basename(item)) |
---|
83 | if toks[1]=='.py' and not toks[0]=='__init__': |
---|
84 | name = toks[0] |
---|
85 | |
---|
86 | path = [os.path.abspath(dir)] |
---|
87 | file = None |
---|
88 | try: |
---|
89 | (file, path, info) = imp.find_module(name, path) |
---|
90 | module = imp.load_module( name, file, item, info ) |
---|
91 | if hasattr(module, "Model"): |
---|
92 | try: |
---|
93 | if _check_plugin(module.Model, name)!=None: |
---|
94 | #plugins.append(module.Model) |
---|
95 | plugins.append(module.Model) |
---|
96 | except: |
---|
97 | msg="Error accessing Model" |
---|
98 | msg+="in %s\n %s %s\n" % (name, |
---|
99 | str(sys.exc_type), sys.exc_value) |
---|
100 | log(msg) |
---|
101 | except: |
---|
102 | msg="Error accessing Model" |
---|
103 | msg +=" in %s\n %s %s \n" %(name, |
---|
104 | str(sys.exc_type), sys.exc_value) |
---|
105 | log(msg) |
---|
106 | finally: |
---|
107 | |
---|
108 | if not file==None: |
---|
109 | file.close() |
---|
110 | except: |
---|
111 | # Don't deal with bad plug-in imports. Just skip. |
---|
112 | pass |
---|
113 | return plugins |
---|
114 | |
---|
115 | class ModelList(object): |
---|
116 | """ |
---|
117 | Contains dictionary of model and their type |
---|
118 | """ |
---|
119 | def __init__(self): |
---|
120 | self.mydict={} |
---|
121 | |
---|
122 | def set_list(self, name, mylist): |
---|
123 | """ |
---|
124 | @param name: the type of the list |
---|
125 | @param mylist: the list to add |
---|
126 | """ |
---|
127 | if name not in self.mydict.keys(): |
---|
128 | self.mydict[name] = mylist |
---|
129 | |
---|
130 | |
---|
131 | def get_list(self): |
---|
132 | """ |
---|
133 | return all the list stored in a dictionary object |
---|
134 | """ |
---|
135 | return self.mydict |
---|
136 | |
---|
137 | class ModelManager: |
---|
138 | ## external dict for models |
---|
139 | model_combobox = ModelList() |
---|
140 | ## Dictionary of form models |
---|
141 | form_factor_dict = {} |
---|
142 | ## dictionary of other |
---|
143 | struct_factor_dict = {} |
---|
144 | ##list of form factors |
---|
145 | shape_list =[] |
---|
146 | ## independent shape model list |
---|
147 | shape_indep_list = [] |
---|
148 | ##list of structure factors |
---|
149 | struct_list= [] |
---|
150 | ##list of model allowing multiplication |
---|
151 | multiplication_factor=[] |
---|
152 | ## list of added models |
---|
153 | plugins=[] |
---|
154 | ## Event owner (guiframe) |
---|
155 | event_owner = None |
---|
156 | |
---|
157 | def _getModelList(self): |
---|
158 | """ |
---|
159 | List of models we want to make available by default |
---|
160 | for this application |
---|
161 | |
---|
162 | @return: the next free event ID following the new menu events |
---|
163 | """ |
---|
164 | ## form factor |
---|
165 | from sans.models.SphereModel import SphereModel |
---|
166 | self.shape_list.append(SphereModel) |
---|
167 | self.multiplication_factor.append(SphereModel) |
---|
168 | |
---|
169 | from sans.models.CoreShellModel import CoreShellModel |
---|
170 | self.shape_list.append(CoreShellModel) |
---|
171 | self.multiplication_factor.append(CoreShellModel) |
---|
172 | |
---|
173 | from sans.models.VesicleModel import VesicleModel |
---|
174 | self.shape_list.append(VesicleModel) |
---|
175 | self.multiplication_factor.append(VesicleModel) |
---|
176 | |
---|
177 | from sans.models.MultiShellModel import MultiShellModel |
---|
178 | self.shape_list.append(MultiShellModel) |
---|
179 | self.multiplication_factor.append(MultiShellModel) |
---|
180 | |
---|
181 | from sans.models.BinaryHSModel import BinaryHSModel |
---|
182 | self.shape_list.append(BinaryHSModel) |
---|
183 | |
---|
184 | from sans.models.CylinderModel import CylinderModel |
---|
185 | self.shape_list.append(CylinderModel) |
---|
186 | self.multiplication_factor.append(CylinderModel) |
---|
187 | |
---|
188 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
189 | self.shape_list.append(CoreShellCylinderModel) |
---|
190 | self.multiplication_factor.append(CoreShellCylinderModel) |
---|
191 | |
---|
192 | from sans.models.HollowCylinderModel import HollowCylinderModel |
---|
193 | self.shape_list.append(HollowCylinderModel) |
---|
194 | self.multiplication_factor.append(HollowCylinderModel) |
---|
195 | |
---|
196 | from sans.models.FlexibleCylinderModel import FlexibleCylinderModel |
---|
197 | self.shape_list.append(FlexibleCylinderModel) |
---|
198 | |
---|
199 | |
---|
200 | from sans.models.StackedDisksModel import StackedDisksModel |
---|
201 | self.shape_list.append(StackedDisksModel) |
---|
202 | self.multiplication_factor.append(StackedDisksModel) |
---|
203 | |
---|
204 | from sans.models.ParallelepipedModel import ParallelepipedModel |
---|
205 | self.shape_list.append(ParallelepipedModel) |
---|
206 | self.multiplication_factor.append(ParallelepipedModel) |
---|
207 | |
---|
208 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
209 | self.shape_list.append(EllipticalCylinderModel) |
---|
210 | self.multiplication_factor.append(EllipticalCylinderModel) |
---|
211 | |
---|
212 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
213 | self.shape_list.append(EllipsoidModel) |
---|
214 | self.multiplication_factor.append(EllipsoidModel) |
---|
215 | |
---|
216 | from sans.models.CoreShellEllipsoidModel import CoreShellEllipsoidModel |
---|
217 | self.shape_list.append(CoreShellEllipsoidModel) |
---|
218 | self.multiplication_factor.append(CoreShellEllipsoidModel) |
---|
219 | |
---|
220 | from sans.models.TriaxialEllipsoidModel import TriaxialEllipsoidModel |
---|
221 | self.shape_list.append(TriaxialEllipsoidModel) |
---|
222 | self.multiplication_factor.append(TriaxialEllipsoidModel) |
---|
223 | |
---|
224 | from sans.models.LamellarModel import LamellarModel |
---|
225 | self.shape_list.append(LamellarModel) |
---|
226 | |
---|
227 | from sans.models.LamellarFFHGModel import LamellarFFHGModel |
---|
228 | self.shape_list.append(LamellarFFHGModel) |
---|
229 | |
---|
230 | from sans.models.LamellarPSModel import LamellarPSModel |
---|
231 | self.shape_list.append(LamellarPSModel) |
---|
232 | |
---|
233 | from sans.models.LamellarPSHGModel import LamellarPSHGModel |
---|
234 | self.shape_list.append(LamellarPSHGModel) |
---|
235 | |
---|
236 | ## Structure factor |
---|
237 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
238 | self.struct_list.append(SquareWellStructure) |
---|
239 | |
---|
240 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
241 | self.struct_list.append(HardsphereStructure) |
---|
242 | |
---|
243 | from sans.models.StickyHSStructure import StickyHSStructure |
---|
244 | self.struct_list.append(StickyHSStructure) |
---|
245 | |
---|
246 | from sans.models.HayterMSAStructure import HayterMSAStructure |
---|
247 | self.struct_list.append(HayterMSAStructure) |
---|
248 | |
---|
249 | |
---|
250 | ##shape-independent models |
---|
251 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
252 | self.shape_indep_list.append(BEPolyelectrolyte ) |
---|
253 | self.form_factor_dict[str(wx.NewId())] = [SphereModel] |
---|
254 | from sans.models.DABModel import DABModel |
---|
255 | self.shape_indep_list.append(DABModel ) |
---|
256 | |
---|
257 | from sans.models.GuinierModel import GuinierModel |
---|
258 | self.shape_indep_list.append(GuinierModel ) |
---|
259 | |
---|
260 | from sans.models.DebyeModel import DebyeModel |
---|
261 | self.shape_indep_list.append(DebyeModel ) |
---|
262 | |
---|
263 | from sans.models.PorodModel import PorodModel |
---|
264 | self.shape_indep_list.append(PorodModel ) |
---|
265 | |
---|
266 | from sans.models.PeakGaussModel import PeakGaussModel |
---|
267 | self.shape_indep_list.append(PeakGaussModel) |
---|
268 | |
---|
269 | from sans.models.PeakLorentzModel import PeakLorentzModel |
---|
270 | self.shape_indep_list.append(PeakLorentzModel) |
---|
271 | |
---|
272 | from sans.models.FractalModel import FractalModel |
---|
273 | class FractalAbsModel(FractalModel): |
---|
274 | def _Fractal(self, x): |
---|
275 | return FractalModel._Fractal(self, math.fabs(x)) |
---|
276 | self.shape_indep_list.append(FractalAbsModel) |
---|
277 | |
---|
278 | from sans.models.LorentzModel import LorentzModel |
---|
279 | self.shape_indep_list.append( LorentzModel) |
---|
280 | |
---|
281 | from sans.models.PowerLawModel import PowerLawModel |
---|
282 | class PowerLawAbsModel(PowerLawModel): |
---|
283 | def _PowerLaw(self, x): |
---|
284 | try: |
---|
285 | return PowerLawModel._PowerLaw(self, math.fabs(x)) |
---|
286 | except: |
---|
287 | print sys.exc_value |
---|
288 | self.shape_indep_list.append( PowerLawAbsModel ) |
---|
289 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
290 | self.shape_indep_list.append(TeubnerStreyModel ) |
---|
291 | |
---|
292 | from sans.models.LineModel import LineModel |
---|
293 | self.shape_indep_list.append(LineModel) |
---|
294 | |
---|
295 | #Looking for plugins |
---|
296 | self.plugins = findModels() |
---|
297 | |
---|
298 | return 0 |
---|
299 | |
---|
300 | |
---|
301 | def populate_menu(self, modelmenu, event_owner): |
---|
302 | """ |
---|
303 | Populate a menu with our models |
---|
304 | |
---|
305 | @param id: first menu event ID to use when binding the menu events |
---|
306 | @param modelmenu: wx.Menu object to populate |
---|
307 | @param event_owner: wx object to bind the menu events to |
---|
308 | @return: the next free event ID following the new menu events |
---|
309 | """ |
---|
310 | ## Fill model lists |
---|
311 | self._getModelList() |
---|
312 | ## store reference to model menu of guiframe |
---|
313 | self.modelmenu = modelmenu |
---|
314 | ## guiframe reference |
---|
315 | self.event_owner = event_owner |
---|
316 | |
---|
317 | |
---|
318 | shape_submenu = wx.Menu() |
---|
319 | shape_indep_submenu = wx.Menu() |
---|
320 | structure_factor = wx.Menu() |
---|
321 | added_models = wx.Menu() |
---|
322 | multip_models = wx.Menu() |
---|
323 | ## create menu with shape |
---|
324 | self._fill_simple_menu( menuinfo = ["Shapes",shape_submenu," simple shape"], |
---|
325 | list1 = self.shape_list ) |
---|
326 | |
---|
327 | self._fill_simple_menu( menuinfo = ["Shape-Independent",shape_indep_submenu, |
---|
328 | "List of shape-independent models"], |
---|
329 | list1 = self.shape_indep_list ) |
---|
330 | |
---|
331 | self._fill_simple_menu( menuinfo= ["Structure Factors",structure_factor, |
---|
332 | "List of Structure factors models" ], |
---|
333 | list1= self.struct_list ) |
---|
334 | |
---|
335 | self._fill_plugin_menu( menuinfo = ["Customized Models", added_models, |
---|
336 | "List of additional models"], |
---|
337 | list1= self.plugins ) |
---|
338 | |
---|
339 | self._fill_menu(menuinfo=["P(Q)*S(Q)",multip_models, |
---|
340 | "mulplication of 2 models"], |
---|
341 | list1 = self.multiplication_factor , |
---|
342 | list2 = self.struct_list) |
---|
343 | |
---|
344 | |
---|
345 | return 0 |
---|
346 | |
---|
347 | def _fill_plugin_menu(self,menuinfo, list1): |
---|
348 | """ |
---|
349 | fill the plugin menu with costumized models |
---|
350 | """ |
---|
351 | if len(list1)==0: |
---|
352 | id = wx.NewId() |
---|
353 | msg= "No model available check plugins.log for errors to fix problem" |
---|
354 | menuinfo[1].Append(int(id),"Empty",msg) |
---|
355 | self._fill_simple_menu( menuinfo,list1) |
---|
356 | |
---|
357 | |
---|
358 | def _fill_simple_menu(self,menuinfo, list1): |
---|
359 | """ |
---|
360 | Fill the menu with list item |
---|
361 | @param modelmenu: the menu to fill |
---|
362 | @param menuinfo: submenu item for the first column of this modelmenu |
---|
363 | with info.Should be a list : |
---|
364 | [name(string) , menu(wx.menu), help(string)] |
---|
365 | @param list1: contains item (form factor )to fill modelmenu second column |
---|
366 | """ |
---|
367 | if len(list1)>0: |
---|
368 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
369 | |
---|
370 | for item in list1: |
---|
371 | try: |
---|
372 | id = wx.NewId() |
---|
373 | struct_factor=item() |
---|
374 | struct_name = struct_factor.__class__.__name__ |
---|
375 | if hasattr(struct_factor, "name"): |
---|
376 | struct_name = struct_factor.name |
---|
377 | |
---|
378 | menuinfo[1].Append(int(id),struct_name,struct_name) |
---|
379 | if not item in self.struct_factor_dict.itervalues(): |
---|
380 | self.struct_factor_dict[str(id)]= item |
---|
381 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
382 | except: |
---|
383 | msg= "Error Occured: %s"%sys.exc_value |
---|
384 | wx.PostEvent(self.event_owner, StatusEvent(status=msg)) |
---|
385 | |
---|
386 | id = wx.NewId() |
---|
387 | self.modelmenu.AppendMenu(id, menuinfo[0],menuinfo[1],menuinfo[2]) |
---|
388 | |
---|
389 | |
---|
390 | |
---|
391 | def _fill_menu(self,menuinfo, list1,list2 ): |
---|
392 | """ |
---|
393 | Fill the menu with list item |
---|
394 | @param menuinfo: submenu item for the first column of this modelmenu |
---|
395 | with info.Should be a list : |
---|
396 | [name(string) , menu(wx.menu), help(string)] |
---|
397 | @param list1: contains item (form factor )to fill modelmenu second column |
---|
398 | @param list2: contains item (Structure factor )to fill modelmenu third column |
---|
399 | """ |
---|
400 | if len(list1)>0: |
---|
401 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
402 | |
---|
403 | for item in list1: |
---|
404 | form_factor= item() |
---|
405 | form_name = form_factor.__class__.__name__ |
---|
406 | if hasattr(form_factor, "name"): |
---|
407 | form_name = form_factor.name |
---|
408 | ### store form factor to return to other users |
---|
409 | newmenu= wx.Menu() |
---|
410 | if len(list2)>0: |
---|
411 | for model in list2: |
---|
412 | id = wx.NewId() |
---|
413 | struct_factor = model() |
---|
414 | name = struct_factor.__class__.__name__ |
---|
415 | if hasattr(struct_factor, "name"): |
---|
416 | name = struct_factor.name |
---|
417 | newmenu.Append(id,name, name) |
---|
418 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
419 | ## save form_fact and struct_fact |
---|
420 | self.form_factor_dict[int(id)] = [form_factor,struct_factor] |
---|
421 | |
---|
422 | form_id= wx.NewId() |
---|
423 | menuinfo[1].AppendMenu(int(form_id), form_name,newmenu,menuinfo[2]) |
---|
424 | id=wx.NewId() |
---|
425 | self.modelmenu.AppendMenu(id,menuinfo[0],menuinfo[1], menuinfo[2]) |
---|
426 | |
---|
427 | |
---|
428 | |
---|
429 | |
---|
430 | def _on_model(self, evt): |
---|
431 | """ |
---|
432 | React to a model menu event |
---|
433 | @param event: wx menu event |
---|
434 | """ |
---|
435 | if int(evt.GetId()) in self.form_factor_dict.keys(): |
---|
436 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
437 | model1, model2 = self.form_factor_dict[int(evt.GetId())] |
---|
438 | model = MultiplicationModel(model1, model2) |
---|
439 | |
---|
440 | else: |
---|
441 | model= self.struct_factor_dict[str(evt.GetId())]() |
---|
442 | |
---|
443 | evt = ModelEvent( model= model ) |
---|
444 | wx.PostEvent(self.event_owner, evt) |
---|
445 | |
---|
446 | def get_model_list(self): |
---|
447 | """ @ return dictionary of models for fitpanel use """ |
---|
448 | self.model_combobox.set_list("multiplication", self.multiplication_factor) |
---|
449 | return self.model_combobox |
---|
450 | |
---|
451 | |
---|
452 | |
---|
453 | |
---|
454 | |
---|
455 | |
---|