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 added models |
---|
106 | plugins=[] |
---|
107 | ## Event owner (guiframe) |
---|
108 | event_owner = None |
---|
109 | |
---|
110 | def _getModelList(self): |
---|
111 | """ |
---|
112 | List of models we want to make available by default |
---|
113 | for this application |
---|
114 | |
---|
115 | @param id: first event ID to register the menu events with |
---|
116 | @return: the next free event ID following the new menu events |
---|
117 | """ |
---|
118 | ## form factor |
---|
119 | from sans.models.SphereModel import SphereModel |
---|
120 | self.shape_list.append(SphereModel) |
---|
121 | |
---|
122 | from sans.models.CylinderModel import CylinderModel |
---|
123 | self.shape_list.append(CylinderModel) |
---|
124 | |
---|
125 | from sans.models.CoreShellModel import CoreShellModel |
---|
126 | self.shape_list.append(CoreShellModel) |
---|
127 | |
---|
128 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
129 | self.shape_list.append(CoreShellCylinderModel) |
---|
130 | |
---|
131 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
132 | self.shape_list.append(EllipticalCylinderModel) |
---|
133 | |
---|
134 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
135 | self.shape_list.append(EllipsoidModel) |
---|
136 | |
---|
137 | from sans.models.LineModel import LineModel |
---|
138 | self.shape_list.append(LineModel) |
---|
139 | |
---|
140 | ## Structure factor |
---|
141 | from sans.models.NoStructure import NoStructure |
---|
142 | self.struct_list.append(NoStructure) |
---|
143 | |
---|
144 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
145 | self.struct_list.append(SquareWellStructure) |
---|
146 | |
---|
147 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
148 | self.struct_list.append(HardsphereStructure) |
---|
149 | |
---|
150 | from sans.models.StickyHSStructure import StickyHSStructure |
---|
151 | self.struct_list.append(StickyHSStructure) |
---|
152 | |
---|
153 | from sans.models.HayterMSAStructure import HayterMSAStructure |
---|
154 | self.struct_list.append(HayterMSAStructure) |
---|
155 | |
---|
156 | |
---|
157 | ##shape-independent models |
---|
158 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
159 | self.shape_indep_list.append(BEPolyelectrolyte ) |
---|
160 | self.form_factor_dict[str(wx.NewId())] = [SphereModel] |
---|
161 | from sans.models.DABModel import DABModel |
---|
162 | self.shape_indep_list.append(DABModel ) |
---|
163 | |
---|
164 | from sans.models.GuinierModel import GuinierModel |
---|
165 | self.shape_indep_list.append(GuinierModel ) |
---|
166 | |
---|
167 | from sans.models.DebyeModel import DebyeModel |
---|
168 | self.shape_indep_list.append(DebyeModel ) |
---|
169 | |
---|
170 | from sans.models.PorodModel import PorodModel |
---|
171 | self.shape_indep_list.append(PorodModel ) |
---|
172 | |
---|
173 | from sans.models.FractalModel import FractalModel |
---|
174 | class FractalAbsModel(FractalModel): |
---|
175 | def _Fractal(self, x): |
---|
176 | return FractalModel._Fractal(self, math.fabs(x)) |
---|
177 | self.shape_indep_list.append(FractalAbsModel) |
---|
178 | |
---|
179 | from sans.models.LorentzModel import LorentzModel |
---|
180 | self.shape_indep_list.append( LorentzModel) |
---|
181 | |
---|
182 | from sans.models.PowerLawModel import PowerLawModel |
---|
183 | class PowerLawAbsModel(PowerLawModel): |
---|
184 | def _PowerLaw(self, x): |
---|
185 | try: |
---|
186 | return PowerLawModel._PowerLaw(self, math.fabs(x)) |
---|
187 | except: |
---|
188 | print sys.exc_value |
---|
189 | self.shape_indep_list.append( PowerLawAbsModel ) |
---|
190 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
191 | self.shape_indep_list.append(TeubnerStreyModel ) |
---|
192 | |
---|
193 | #Looking for plugins |
---|
194 | self.plugins = findModels() |
---|
195 | |
---|
196 | return 0 |
---|
197 | |
---|
198 | |
---|
199 | def populate_menu(self, modelmenu, event_owner): |
---|
200 | """ |
---|
201 | Populate a menu with our models |
---|
202 | |
---|
203 | @param id: first menu event ID to use when binding the menu events |
---|
204 | @param modelmenu: wx.Menu object to populate |
---|
205 | @param event_owner: wx object to bind the menu events to |
---|
206 | @return: the next free event ID following the new menu events |
---|
207 | """ |
---|
208 | ## Fill model lists |
---|
209 | self._getModelList() |
---|
210 | ## store reference to model menu of guiframe |
---|
211 | self.modelmenu = modelmenu |
---|
212 | ## guiframe reference |
---|
213 | self.event_owner = event_owner |
---|
214 | |
---|
215 | |
---|
216 | shape_submenu = wx.Menu() |
---|
217 | shape_indep_submenu = wx.Menu() |
---|
218 | structure_factor = wx.Menu() |
---|
219 | added_models = wx.Menu() |
---|
220 | ## create menu with shape |
---|
221 | self._fill_menu( menuinfo = ["shapes",shape_submenu," simple shape"], |
---|
222 | list1 = self.shape_list, |
---|
223 | list2 = self.struct_list ) |
---|
224 | self._fill_menu( menuinfo = ["Shape-independent",shape_indep_submenu, |
---|
225 | "List of shape-independent models"], |
---|
226 | list1 = self.shape_indep_list, |
---|
227 | list2 = self.struct_list ) |
---|
228 | |
---|
229 | self._fill_simple_menu( menuinfo= ["Structure Factors",structure_factor, |
---|
230 | "List of Structure factors models" ], |
---|
231 | list1= self.struct_list ) |
---|
232 | |
---|
233 | self._fill_simple_menu( menuinfo = ["Added models", added_models, |
---|
234 | "List of additional models"], |
---|
235 | list1= self.plugins) |
---|
236 | return 0 |
---|
237 | |
---|
238 | def _fill_simple_menu(self,menuinfo, list1): |
---|
239 | """ |
---|
240 | Fill the menu with list item |
---|
241 | @param modelmenu: the menu to fill |
---|
242 | @param menuinfo: submenu item for the first column of this modelmenu |
---|
243 | with info.Should be a list : |
---|
244 | [name(string) , menu(wx.menu), help(string)] |
---|
245 | @param list1: contains item (form factor )to fill modelmenu second column |
---|
246 | """ |
---|
247 | if len(list1)>0: |
---|
248 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
249 | |
---|
250 | for item in list1: |
---|
251 | id = wx.NewId() |
---|
252 | struct_factor=item() |
---|
253 | struct_name = struct_factor.__class__.__name__ |
---|
254 | if hasattr(struct_factor, "name"): |
---|
255 | struct_name = struct_factor.name |
---|
256 | |
---|
257 | menuinfo[1].Append(int(id),struct_name,struct_name) |
---|
258 | if not item in self.struct_factor_dict.itervalues(): |
---|
259 | self.struct_factor_dict[str(id)]= item |
---|
260 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
261 | |
---|
262 | id = wx.NewId() |
---|
263 | self.modelmenu.AppendMenu(id, menuinfo[0],menuinfo[1],menuinfo[2]) |
---|
264 | |
---|
265 | |
---|
266 | |
---|
267 | def _fill_menu(self,menuinfo, list1,list2 ): |
---|
268 | """ |
---|
269 | Fill the menu with list item |
---|
270 | @param menuinfo: submenu item for the first column of this modelmenu |
---|
271 | with info.Should be a list : |
---|
272 | [name(string) , menu(wx.menu), help(string)] |
---|
273 | @param list1: contains item (form factor )to fill modelmenu second column |
---|
274 | @param list2: contains item (Structure factor )to fill modelmenu third column |
---|
275 | """ |
---|
276 | if len(list1)>0: |
---|
277 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
278 | |
---|
279 | for item in list1: |
---|
280 | form_factor= item() |
---|
281 | form_name = form_factor.__class__.__name__ |
---|
282 | if hasattr(form_factor, "name"): |
---|
283 | form_name = form_factor.name |
---|
284 | ### store form factor to return to other users |
---|
285 | newmenu= wx.Menu() |
---|
286 | if len(list2)>0: |
---|
287 | for model in list2: |
---|
288 | id = wx.NewId() |
---|
289 | struct_factor = model() |
---|
290 | name = struct_factor.__class__.__name__ |
---|
291 | if hasattr(struct_factor, "name"): |
---|
292 | name = struct_factor.name |
---|
293 | newmenu.Append(id,name, name) |
---|
294 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
295 | ## save form_fact and struct_fact |
---|
296 | self.form_factor_dict[int(id)] = [form_factor,struct_factor] |
---|
297 | |
---|
298 | form_id= wx.NewId() |
---|
299 | menuinfo[1].AppendMenu(int(form_id), form_name,newmenu,menuinfo[2]) |
---|
300 | id=wx.NewId() |
---|
301 | self.modelmenu.AppendMenu(id,menuinfo[0],menuinfo[1], menuinfo[2]) |
---|
302 | |
---|
303 | |
---|
304 | |
---|
305 | |
---|
306 | def _on_model(self, evt): |
---|
307 | """ |
---|
308 | React to a model menu event |
---|
309 | @param event: wx menu event |
---|
310 | """ |
---|
311 | if int(evt.GetId()) in self.form_factor_dict.keys(): |
---|
312 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
313 | model1, model2 = self.form_factor_dict[int(evt.GetId())] |
---|
314 | model = MultiplicationModel(model1, model2) |
---|
315 | |
---|
316 | else: |
---|
317 | model= self.struct_factor_dict[str(evt.GetId())]() |
---|
318 | |
---|
319 | evt = ModelEvent( model= model ) |
---|
320 | wx.PostEvent(self.event_owner, evt) |
---|
321 | |
---|
322 | def get_model_list(self): |
---|
323 | """ @ return dictionary of models for fitpanel use """ |
---|
324 | return self.model_combobox |
---|
325 | |
---|
326 | |
---|
327 | def get_form_struct(self): |
---|
328 | """ retunr list of form structures""" |
---|
329 | return self.struct_list |
---|
330 | |
---|
331 | |
---|
332 | |
---|
333 | |
---|