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