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 | |
---|
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 None 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 None |
---|
47 | if model.__name__!="Model": |
---|
48 | msg= "Plugin %s class name must be Model \n"%str(name) |
---|
49 | log(msg) |
---|
50 | return None |
---|
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 None |
---|
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 None |
---|
68 | else: |
---|
69 | msg="Plugin %s needs a method called function \n"%str(name) |
---|
70 | log(msg) |
---|
71 | return None |
---|
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 | except: |
---|
96 | msg="Error accessing Model" |
---|
97 | msg+="in %s\n %s %s\n" % (name, |
---|
98 | str(sys.exc_type), sys.exc_value) |
---|
99 | log(msg) |
---|
100 | except: |
---|
101 | msg="Error accessing Model" |
---|
102 | msg +=" in %s\n %s %s \n" %(name, |
---|
103 | str(sys.exc_type), sys.exc_value) |
---|
104 | log(msg) |
---|
105 | finally: |
---|
106 | |
---|
107 | if not file==None: |
---|
108 | file.close() |
---|
109 | except: |
---|
110 | # Don't deal with bad plug-in imports. Just skip. |
---|
111 | pass |
---|
112 | return plugins |
---|
113 | |
---|
114 | class ModelList(object): |
---|
115 | """ |
---|
116 | Contains dictionary of model and their type |
---|
117 | """ |
---|
118 | def __init__(self): |
---|
119 | self.mydict={} |
---|
120 | |
---|
121 | def set_list(self, name, mylist): |
---|
122 | """ |
---|
123 | @param name: the type of the list |
---|
124 | @param mylist: the list to add |
---|
125 | """ |
---|
126 | if name not in self.mydict.keys(): |
---|
127 | self.mydict[name] = mylist |
---|
128 | |
---|
129 | |
---|
130 | def get_list(self): |
---|
131 | """ |
---|
132 | return all the list stored in a dictionary object |
---|
133 | """ |
---|
134 | return self.mydict |
---|
135 | |
---|
136 | class ModelManager: |
---|
137 | ## external dict for models |
---|
138 | model_combobox = ModelList() |
---|
139 | ## Dictionary of form models |
---|
140 | form_factor_dict = {} |
---|
141 | ## dictionary of other |
---|
142 | struct_factor_dict = {} |
---|
143 | ##list of form factors |
---|
144 | shape_list =[] |
---|
145 | ## independent shape model list |
---|
146 | shape_indep_list = [] |
---|
147 | ##list of structure factors |
---|
148 | struct_list= [] |
---|
149 | ##list of model allowing multiplication |
---|
150 | multiplication_factor=[] |
---|
151 | ## list of added models |
---|
152 | plugins=[] |
---|
153 | ## Event owner (guiframe) |
---|
154 | event_owner = None |
---|
155 | |
---|
156 | def _getModelList(self): |
---|
157 | """ |
---|
158 | List of models we want to make available by default |
---|
159 | for this application |
---|
160 | |
---|
161 | @return: the next free event ID following the new menu events |
---|
162 | """ |
---|
163 | ## form factor |
---|
164 | from sans.models.SphereModel import SphereModel |
---|
165 | self.shape_list.append(SphereModel) |
---|
166 | self.multiplication_factor.append(SphereModel) |
---|
167 | |
---|
168 | from sans.models.FuzzySphereModel import FuzzySphereModel |
---|
169 | self.shape_list.append(FuzzySphereModel) |
---|
170 | self.multiplication_factor.append(FuzzySphereModel) |
---|
171 | |
---|
172 | from sans.models.CoreShellModel import CoreShellModel |
---|
173 | self.shape_list.append(CoreShellModel) |
---|
174 | self.multiplication_factor.append(CoreShellModel) |
---|
175 | |
---|
176 | from sans.models.CoreFourShellModel import CoreFourShellModel |
---|
177 | self.shape_list.append(CoreFourShellModel) |
---|
178 | self.multiplication_factor.append(CoreFourShellModel) |
---|
179 | |
---|
180 | from sans.models.VesicleModel import VesicleModel |
---|
181 | self.shape_list.append(VesicleModel) |
---|
182 | self.multiplication_factor.append(VesicleModel) |
---|
183 | |
---|
184 | from sans.models.MultiShellModel import MultiShellModel |
---|
185 | self.shape_list.append(MultiShellModel) |
---|
186 | self.multiplication_factor.append(MultiShellModel) |
---|
187 | |
---|
188 | from sans.models.BinaryHSModel import BinaryHSModel |
---|
189 | self.shape_list.append(BinaryHSModel) |
---|
190 | |
---|
191 | from sans.models.CylinderModel import CylinderModel |
---|
192 | self.shape_list.append(CylinderModel) |
---|
193 | self.multiplication_factor.append(CylinderModel) |
---|
194 | |
---|
195 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
196 | self.shape_list.append(CoreShellCylinderModel) |
---|
197 | self.multiplication_factor.append(CoreShellCylinderModel) |
---|
198 | |
---|
199 | from sans.models.HollowCylinderModel import HollowCylinderModel |
---|
200 | self.shape_list.append(HollowCylinderModel) |
---|
201 | self.multiplication_factor.append(HollowCylinderModel) |
---|
202 | |
---|
203 | from sans.models.FlexibleCylinderModel import FlexibleCylinderModel |
---|
204 | self.shape_list.append(FlexibleCylinderModel) |
---|
205 | |
---|
206 | from sans.models.FlexCylEllipXModel import FlexCylEllipXModel |
---|
207 | self.shape_list.append(FlexCylEllipXModel) |
---|
208 | |
---|
209 | from sans.models.StackedDisksModel import StackedDisksModel |
---|
210 | self.shape_list.append(StackedDisksModel) |
---|
211 | self.multiplication_factor.append(StackedDisksModel) |
---|
212 | |
---|
213 | from sans.models.ParallelepipedModel import ParallelepipedModel |
---|
214 | self.shape_list.append(ParallelepipedModel) |
---|
215 | self.multiplication_factor.append(ParallelepipedModel) |
---|
216 | |
---|
217 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
218 | self.shape_list.append(EllipticalCylinderModel) |
---|
219 | self.multiplication_factor.append(EllipticalCylinderModel) |
---|
220 | |
---|
221 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
222 | self.shape_list.append(EllipsoidModel) |
---|
223 | self.multiplication_factor.append(EllipsoidModel) |
---|
224 | |
---|
225 | from sans.models.CoreShellEllipsoidModel import CoreShellEllipsoidModel |
---|
226 | self.shape_list.append(CoreShellEllipsoidModel) |
---|
227 | self.multiplication_factor.append(CoreShellEllipsoidModel) |
---|
228 | |
---|
229 | from sans.models.TriaxialEllipsoidModel import TriaxialEllipsoidModel |
---|
230 | self.shape_list.append(TriaxialEllipsoidModel) |
---|
231 | self.multiplication_factor.append(TriaxialEllipsoidModel) |
---|
232 | |
---|
233 | from sans.models.LamellarModel import LamellarModel |
---|
234 | self.shape_list.append(LamellarModel) |
---|
235 | |
---|
236 | from sans.models.LamellarFFHGModel import LamellarFFHGModel |
---|
237 | self.shape_list.append(LamellarFFHGModel) |
---|
238 | |
---|
239 | from sans.models.LamellarPSModel import LamellarPSModel |
---|
240 | self.shape_list.append(LamellarPSModel) |
---|
241 | |
---|
242 | from sans.models.LamellarPSHGModel import LamellarPSHGModel |
---|
243 | self.shape_list.append(LamellarPSHGModel) |
---|
244 | |
---|
245 | ## Structure factor |
---|
246 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
247 | self.struct_list.append(SquareWellStructure) |
---|
248 | |
---|
249 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
250 | self.struct_list.append(HardsphereStructure) |
---|
251 | |
---|
252 | from sans.models.StickyHSStructure import StickyHSStructure |
---|
253 | self.struct_list.append(StickyHSStructure) |
---|
254 | |
---|
255 | from sans.models.HayterMSAStructure import HayterMSAStructure |
---|
256 | self.struct_list.append(HayterMSAStructure) |
---|
257 | |
---|
258 | |
---|
259 | ##shape-independent models |
---|
260 | |
---|
261 | from sans.models.PowerLawAbsModel import PowerLawAbsModel |
---|
262 | self.shape_indep_list.append( PowerLawAbsModel ) |
---|
263 | |
---|
264 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
265 | self.shape_indep_list.append(BEPolyelectrolyte ) |
---|
266 | self.form_factor_dict[str(wx.NewId())] = [SphereModel] |
---|
267 | |
---|
268 | from sans.models.DABModel import DABModel |
---|
269 | self.shape_indep_list.append(DABModel ) |
---|
270 | |
---|
271 | from sans.models.DebyeModel import DebyeModel |
---|
272 | self.shape_indep_list.append(DebyeModel ) |
---|
273 | |
---|
274 | from sans.models.GuinierModel import GuinierModel |
---|
275 | self.shape_indep_list.append(GuinierModel ) |
---|
276 | |
---|
277 | from sans.models.FractalModel import FractalModel |
---|
278 | self.shape_indep_list.append(FractalModel ) |
---|
279 | |
---|
280 | from sans.models.LorentzModel import LorentzModel |
---|
281 | self.shape_indep_list.append( LorentzModel) |
---|
282 | |
---|
283 | from sans.models.PeakGaussModel import PeakGaussModel |
---|
284 | self.shape_indep_list.append(PeakGaussModel) |
---|
285 | |
---|
286 | from sans.models.PeakLorentzModel import PeakLorentzModel |
---|
287 | self.shape_indep_list.append(PeakLorentzModel) |
---|
288 | |
---|
289 | from sans.models.Poly_GaussCoil import Poly_GaussCoil |
---|
290 | self.shape_indep_list.append(Poly_GaussCoil) |
---|
291 | |
---|
292 | from sans.models.PorodModel import PorodModel |
---|
293 | self.shape_indep_list.append(PorodModel ) |
---|
294 | |
---|
295 | |
---|
296 | #FractalModel (a c-model)will be used. |
---|
297 | #from sans.models.FractalAbsModel import FractalAbsModel |
---|
298 | #self.shape_indep_list.append(FractalAbsModel) |
---|
299 | |
---|
300 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
301 | self.shape_indep_list.append(TeubnerStreyModel ) |
---|
302 | |
---|
303 | from sans.models.LineModel import LineModel |
---|
304 | self.shape_indep_list.append(LineModel) |
---|
305 | |
---|
306 | |
---|
307 | #Looking for plugins |
---|
308 | self.plugins = findModels() |
---|
309 | |
---|
310 | return 0 |
---|
311 | |
---|
312 | |
---|
313 | def populate_menu(self, modelmenu, event_owner): |
---|
314 | """ |
---|
315 | Populate a menu with our models |
---|
316 | |
---|
317 | @param id: first menu event ID to use when binding the menu events |
---|
318 | @param modelmenu: wx.Menu object to populate |
---|
319 | @param event_owner: wx object to bind the menu events to |
---|
320 | @return: the next free event ID following the new menu events |
---|
321 | """ |
---|
322 | ## Fill model lists |
---|
323 | self._getModelList() |
---|
324 | ## store reference to model menu of guiframe |
---|
325 | self.modelmenu = modelmenu |
---|
326 | ## guiframe reference |
---|
327 | self.event_owner = event_owner |
---|
328 | |
---|
329 | |
---|
330 | shape_submenu = wx.Menu() |
---|
331 | shape_indep_submenu = wx.Menu() |
---|
332 | structure_factor = wx.Menu() |
---|
333 | added_models = wx.Menu() |
---|
334 | multip_models = wx.Menu() |
---|
335 | ## create menu with shape |
---|
336 | self._fill_simple_menu( menuinfo = ["Shapes",shape_submenu," simple shape"], |
---|
337 | list1 = self.shape_list ) |
---|
338 | |
---|
339 | self._fill_simple_menu( menuinfo = ["Shape-Independent",shape_indep_submenu, |
---|
340 | "List of shape-independent models"], |
---|
341 | list1 = self.shape_indep_list ) |
---|
342 | |
---|
343 | self._fill_simple_menu( menuinfo= ["Structure Factors",structure_factor, |
---|
344 | "List of Structure factors models" ], |
---|
345 | list1= self.struct_list ) |
---|
346 | |
---|
347 | self._fill_plugin_menu( menuinfo = ["Customized Models", added_models, |
---|
348 | "List of additional models"], |
---|
349 | list1= self.plugins ) |
---|
350 | |
---|
351 | self._fill_menu(menuinfo=["P(Q)*S(Q)",multip_models, |
---|
352 | "mulplication of 2 models"], |
---|
353 | list1 = self.multiplication_factor , |
---|
354 | list2 = self.struct_list) |
---|
355 | |
---|
356 | |
---|
357 | return 0 |
---|
358 | |
---|
359 | def _fill_plugin_menu(self,menuinfo, list1): |
---|
360 | """ |
---|
361 | fill the plugin menu with costumized models |
---|
362 | """ |
---|
363 | if len(list1)==0: |
---|
364 | id = wx.NewId() |
---|
365 | msg= "No model available check plugins.log for errors to fix problem" |
---|
366 | menuinfo[1].Append(int(id),"Empty",msg) |
---|
367 | self._fill_simple_menu( menuinfo,list1) |
---|
368 | |
---|
369 | |
---|
370 | def _fill_simple_menu(self,menuinfo, list1): |
---|
371 | """ |
---|
372 | Fill the menu with list item |
---|
373 | @param modelmenu: the menu to fill |
---|
374 | @param menuinfo: submenu item for the first column of this modelmenu |
---|
375 | with info.Should be a list : |
---|
376 | [name(string) , menu(wx.menu), help(string)] |
---|
377 | @param list1: contains item (form factor )to fill modelmenu second column |
---|
378 | """ |
---|
379 | if len(list1)>0: |
---|
380 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
381 | |
---|
382 | for item in list1: |
---|
383 | try: |
---|
384 | id = wx.NewId() |
---|
385 | struct_factor=item() |
---|
386 | struct_name = struct_factor.__class__.__name__ |
---|
387 | if hasattr(struct_factor, "name"): |
---|
388 | struct_name = struct_factor.name |
---|
389 | |
---|
390 | menuinfo[1].Append(int(id),struct_name,struct_name) |
---|
391 | if not item in self.struct_factor_dict.itervalues(): |
---|
392 | self.struct_factor_dict[str(id)]= item |
---|
393 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
394 | except: |
---|
395 | msg= "Error Occured: %s"%sys.exc_value |
---|
396 | wx.PostEvent(self.event_owner, StatusEvent(status=msg)) |
---|
397 | |
---|
398 | id = wx.NewId() |
---|
399 | self.modelmenu.AppendMenu(id, menuinfo[0],menuinfo[1],menuinfo[2]) |
---|
400 | |
---|
401 | |
---|
402 | |
---|
403 | def _fill_menu(self,menuinfo, list1,list2 ): |
---|
404 | """ |
---|
405 | Fill the menu with list item |
---|
406 | @param menuinfo: submenu item for the first column of this modelmenu |
---|
407 | with info.Should be a list : |
---|
408 | [name(string) , menu(wx.menu), help(string)] |
---|
409 | @param list1: contains item (form factor )to fill modelmenu second column |
---|
410 | @param list2: contains item (Structure factor )to fill modelmenu third column |
---|
411 | """ |
---|
412 | if len(list1)>0: |
---|
413 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
414 | |
---|
415 | for item in list1: |
---|
416 | form_factor= item() |
---|
417 | form_name = form_factor.__class__.__name__ |
---|
418 | if hasattr(form_factor, "name"): |
---|
419 | form_name = form_factor.name |
---|
420 | ### store form factor to return to other users |
---|
421 | newmenu= wx.Menu() |
---|
422 | if len(list2)>0: |
---|
423 | for model in list2: |
---|
424 | id = wx.NewId() |
---|
425 | struct_factor = model() |
---|
426 | name = struct_factor.__class__.__name__ |
---|
427 | if hasattr(struct_factor, "name"): |
---|
428 | name = struct_factor.name |
---|
429 | newmenu.Append(id,name, name) |
---|
430 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
431 | ## save form_fact and struct_fact |
---|
432 | self.form_factor_dict[int(id)] = [form_factor,struct_factor] |
---|
433 | |
---|
434 | form_id= wx.NewId() |
---|
435 | menuinfo[1].AppendMenu(int(form_id), form_name,newmenu,menuinfo[2]) |
---|
436 | id=wx.NewId() |
---|
437 | self.modelmenu.AppendMenu(id,menuinfo[0],menuinfo[1], menuinfo[2]) |
---|
438 | |
---|
439 | |
---|
440 | |
---|
441 | |
---|
442 | def _on_model(self, evt): |
---|
443 | """ |
---|
444 | React to a model menu event |
---|
445 | @param event: wx menu event |
---|
446 | """ |
---|
447 | if int(evt.GetId()) in self.form_factor_dict.keys(): |
---|
448 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
449 | model1, model2 = self.form_factor_dict[int(evt.GetId())] |
---|
450 | model = MultiplicationModel(model1, model2) |
---|
451 | |
---|
452 | else: |
---|
453 | model= self.struct_factor_dict[str(evt.GetId())]() |
---|
454 | |
---|
455 | evt = ModelEvent( model= model ) |
---|
456 | wx.PostEvent(self.event_owner, evt) |
---|
457 | |
---|
458 | def get_model_list(self): |
---|
459 | """ @ return dictionary of models for fitpanel use """ |
---|
460 | self.model_combobox.set_list("multiplication", self.multiplication_factor) |
---|
461 | return self.model_combobox |
---|
462 | |
---|
463 | |
---|
464 | |
---|
465 | |
---|
466 | |
---|
467 | |
---|