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