1 | |
---|
2 | """ |
---|
3 | This module collects models from sans.models packages and orders theses models |
---|
4 | by categories that GUI application understands and uses. |
---|
5 | |
---|
6 | """ |
---|
7 | import wx |
---|
8 | import wx.lib.newevent |
---|
9 | import imp |
---|
10 | import os,sys,math |
---|
11 | import os.path |
---|
12 | |
---|
13 | (ModelEvent, EVT_MODEL) = wx.lib.newevent.NewEvent() |
---|
14 | from sans.guicomm.events import StatusEvent |
---|
15 | # Time is needed by the log method |
---|
16 | import time |
---|
17 | |
---|
18 | # Explicitly import from the pluginmodel module so that py2exe |
---|
19 | # places it in the distribution. The Model1DPlugin class is used |
---|
20 | # as the base class of plug-in models. |
---|
21 | from sans.models.pluginmodel import Model1DPlugin |
---|
22 | |
---|
23 | def log(message): |
---|
24 | """ |
---|
25 | """ |
---|
26 | out = open("plugins.log", 'a') |
---|
27 | out.write("%10g: %s\n" % (time.clock(), message)) |
---|
28 | out.close() |
---|
29 | |
---|
30 | def findModels(): |
---|
31 | """ |
---|
32 | """ |
---|
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 _check_plugin(model, name): |
---|
39 | """ |
---|
40 | Do some checking before model adding plugins in the list |
---|
41 | |
---|
42 | :param model: class model to add into the plugin list |
---|
43 | :param name: name of the module plugin |
---|
44 | |
---|
45 | :return model: model if valid model or None if not valid |
---|
46 | |
---|
47 | """ |
---|
48 | #Check is the plugin is of type Model1DPlugin |
---|
49 | if not issubclass(model, Model1DPlugin): |
---|
50 | msg= "Plugin %s must be of type Model1DPlugin \n"%str(name) |
---|
51 | log(msg) |
---|
52 | return None |
---|
53 | if model.__name__!="Model": |
---|
54 | msg= "Plugin %s class name must be Model \n"%str(name) |
---|
55 | log(msg) |
---|
56 | return None |
---|
57 | try: |
---|
58 | new_instance= model() |
---|
59 | except: |
---|
60 | msg="Plugin %s error in __init__ \n\t: %s %s\n"%(str(name), |
---|
61 | str(sys.exc_type),sys.exc_value) |
---|
62 | log(msg) |
---|
63 | return None |
---|
64 | |
---|
65 | new_instance= model() |
---|
66 | if hasattr(new_instance,"function"): |
---|
67 | try: |
---|
68 | value=new_instance.function() |
---|
69 | except: |
---|
70 | msg="Plugin %s: error writing function \n\t :%s %s\n "%(str(name), |
---|
71 | str(sys.exc_type),sys.exc_value) |
---|
72 | log(msg) |
---|
73 | return None |
---|
74 | else: |
---|
75 | msg="Plugin %s needs a method called function \n"%str(name) |
---|
76 | log(msg) |
---|
77 | return None |
---|
78 | return model |
---|
79 | |
---|
80 | |
---|
81 | def _findModels(dir): |
---|
82 | """ |
---|
83 | """ |
---|
84 | # List of plugin objects |
---|
85 | plugins = [] |
---|
86 | # Go through files in plug-in directory |
---|
87 | try: |
---|
88 | list = os.listdir(dir) |
---|
89 | for item in list: |
---|
90 | toks = os.path.splitext(os.path.basename(item)) |
---|
91 | if toks[1]=='.py' and not toks[0]=='__init__': |
---|
92 | name = toks[0] |
---|
93 | |
---|
94 | path = [os.path.abspath(dir)] |
---|
95 | file = None |
---|
96 | try: |
---|
97 | (file, path, info) = imp.find_module(name, path) |
---|
98 | module = imp.load_module( name, file, item, info ) |
---|
99 | if hasattr(module, "Model"): |
---|
100 | try: |
---|
101 | if _check_plugin(module.Model, name)!=None: |
---|
102 | plugins.append(module.Model) |
---|
103 | except: |
---|
104 | msg="Error accessing Model" |
---|
105 | msg+="in %s\n %s %s\n" % (name, |
---|
106 | str(sys.exc_type), sys.exc_value) |
---|
107 | log(msg) |
---|
108 | except: |
---|
109 | msg="Error accessing Model" |
---|
110 | msg +=" in %s\n %s %s \n" %(name, |
---|
111 | str(sys.exc_type), sys.exc_value) |
---|
112 | log(msg) |
---|
113 | finally: |
---|
114 | |
---|
115 | if not file==None: |
---|
116 | file.close() |
---|
117 | except: |
---|
118 | # Don't deal with bad plug-in imports. Just skip. |
---|
119 | pass |
---|
120 | return plugins |
---|
121 | |
---|
122 | class ModelList(object): |
---|
123 | """ |
---|
124 | Contains dictionary of model and their type |
---|
125 | |
---|
126 | """ |
---|
127 | def __init__(self): |
---|
128 | self.mydict={} |
---|
129 | |
---|
130 | def set_list(self, name, mylist): |
---|
131 | """ |
---|
132 | |
---|
133 | :param name: the type of the list |
---|
134 | :param mylist: the list to add |
---|
135 | |
---|
136 | """ |
---|
137 | if name not in self.mydict.keys(): |
---|
138 | self.mydict[name] = mylist |
---|
139 | |
---|
140 | |
---|
141 | def get_list(self): |
---|
142 | """ |
---|
143 | return all the list stored in a dictionary object |
---|
144 | """ |
---|
145 | return self.mydict |
---|
146 | |
---|
147 | class ModelManager: |
---|
148 | """ |
---|
149 | ModelManager collected model classes object of available into a |
---|
150 | dictionary of models' names and models' classes. |
---|
151 | |
---|
152 | """ |
---|
153 | ## external dict for models |
---|
154 | model_combobox = ModelList() |
---|
155 | ## Dictionary of form models |
---|
156 | form_factor_dict = {} |
---|
157 | ## dictionary of other |
---|
158 | struct_factor_dict = {} |
---|
159 | ##list of form factors |
---|
160 | shape_list =[] |
---|
161 | ## independent shape model list |
---|
162 | shape_indep_list = [] |
---|
163 | ##list of structure factors |
---|
164 | struct_list= [] |
---|
165 | ##list of model allowing multiplication |
---|
166 | multiplication_factor=[] |
---|
167 | ##list of multifunctional shapes |
---|
168 | multi_func_list =[] |
---|
169 | ## list of added models |
---|
170 | plugins=[] |
---|
171 | ## Event owner (guiframe) |
---|
172 | event_owner = None |
---|
173 | |
---|
174 | def _getModelList(self): |
---|
175 | """ |
---|
176 | Fill up lists of models available by default |
---|
177 | for the current application |
---|
178 | |
---|
179 | :return: the next free event ID following the new menu events |
---|
180 | |
---|
181 | """ |
---|
182 | ## form factor |
---|
183 | from sans.models.SphereModel import SphereModel |
---|
184 | self.shape_list.append(SphereModel) |
---|
185 | self.multiplication_factor.append(SphereModel) |
---|
186 | |
---|
187 | from sans.models.SphereExpShellModel import SphereExpShellModel |
---|
188 | self.shape_list.append(SphereExpShellModel) |
---|
189 | self.multiplication_factor.append(SphereExpShellModel) |
---|
190 | self.multi_func_list.append(SphereExpShellModel) |
---|
191 | |
---|
192 | from sans.models.FuzzySphereModel import FuzzySphereModel |
---|
193 | self.shape_list.append(FuzzySphereModel) |
---|
194 | self.multiplication_factor.append(FuzzySphereModel) |
---|
195 | |
---|
196 | from sans.models.CoreShellModel import CoreShellModel |
---|
197 | self.shape_list.append(CoreShellModel) |
---|
198 | self.multiplication_factor.append(CoreShellModel) |
---|
199 | |
---|
200 | from sans.models.CoreMultiShellModel import CoreMultiShellModel |
---|
201 | self.shape_list.append(CoreMultiShellModel) |
---|
202 | self.multiplication_factor.append(CoreMultiShellModel) |
---|
203 | self.multi_func_list.append(CoreMultiShellModel) |
---|
204 | |
---|
205 | from sans.models.VesicleModel import VesicleModel |
---|
206 | self.shape_list.append(VesicleModel) |
---|
207 | self.multiplication_factor.append(VesicleModel) |
---|
208 | |
---|
209 | from sans.models.MultiShellModel import MultiShellModel |
---|
210 | self.shape_list.append(MultiShellModel) |
---|
211 | self.multiplication_factor.append(MultiShellModel) |
---|
212 | |
---|
213 | from sans.models.BinaryHSModel import BinaryHSModel |
---|
214 | self.shape_list.append(BinaryHSModel) |
---|
215 | |
---|
216 | from sans.models.CylinderModel import CylinderModel |
---|
217 | self.shape_list.append(CylinderModel) |
---|
218 | self.multiplication_factor.append(CylinderModel) |
---|
219 | |
---|
220 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
221 | self.shape_list.append(CoreShellCylinderModel) |
---|
222 | self.multiplication_factor.append(CoreShellCylinderModel) |
---|
223 | |
---|
224 | from sans.models.HollowCylinderModel import HollowCylinderModel |
---|
225 | self.shape_list.append(HollowCylinderModel) |
---|
226 | self.multiplication_factor.append(HollowCylinderModel) |
---|
227 | |
---|
228 | from sans.models.FlexibleCylinderModel import FlexibleCylinderModel |
---|
229 | self.shape_list.append(FlexibleCylinderModel) |
---|
230 | |
---|
231 | from sans.models.FlexCylEllipXModel import FlexCylEllipXModel |
---|
232 | self.shape_list.append(FlexCylEllipXModel) |
---|
233 | |
---|
234 | from sans.models.StackedDisksModel import StackedDisksModel |
---|
235 | self.shape_list.append(StackedDisksModel) |
---|
236 | self.multiplication_factor.append(StackedDisksModel) |
---|
237 | |
---|
238 | from sans.models.ParallelepipedModel import ParallelepipedModel |
---|
239 | self.shape_list.append(ParallelepipedModel) |
---|
240 | self.multiplication_factor.append(ParallelepipedModel) |
---|
241 | |
---|
242 | from sans.models.CSParallelepipedModel import CSParallelepipedModel |
---|
243 | self.shape_list.append(CSParallelepipedModel) |
---|
244 | self.multiplication_factor.append(CSParallelepipedModel) |
---|
245 | |
---|
246 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
247 | self.shape_list.append(EllipticalCylinderModel) |
---|
248 | self.multiplication_factor.append(EllipticalCylinderModel) |
---|
249 | |
---|
250 | from sans.models.BarBellModel import BarBellModel |
---|
251 | self.shape_list.append(BarBellModel) |
---|
252 | # not implemeted yet! |
---|
253 | #self.multiplication_factor.append(BarBellModel) |
---|
254 | |
---|
255 | from sans.models.CappedCylinderModel import CappedCylinderModel |
---|
256 | self.shape_list.append(CappedCylinderModel) |
---|
257 | # not implemeted yet! |
---|
258 | #self.multiplication_factor.append(CappedCylinderModel) |
---|
259 | |
---|
260 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
261 | self.shape_list.append(EllipsoidModel) |
---|
262 | self.multiplication_factor.append(EllipsoidModel) |
---|
263 | |
---|
264 | from sans.models.CoreShellEllipsoidModel import CoreShellEllipsoidModel |
---|
265 | self.shape_list.append(CoreShellEllipsoidModel) |
---|
266 | self.multiplication_factor.append(CoreShellEllipsoidModel) |
---|
267 | |
---|
268 | from sans.models.TriaxialEllipsoidModel import TriaxialEllipsoidModel |
---|
269 | self.shape_list.append(TriaxialEllipsoidModel) |
---|
270 | self.multiplication_factor.append(TriaxialEllipsoidModel) |
---|
271 | |
---|
272 | from sans.models.LamellarModel import LamellarModel |
---|
273 | self.shape_list.append(LamellarModel) |
---|
274 | |
---|
275 | from sans.models.LamellarFFHGModel import LamellarFFHGModel |
---|
276 | self.shape_list.append(LamellarFFHGModel) |
---|
277 | |
---|
278 | from sans.models.LamellarPSModel import LamellarPSModel |
---|
279 | self.shape_list.append(LamellarPSModel) |
---|
280 | |
---|
281 | from sans.models.LamellarPSHGModel import LamellarPSHGModel |
---|
282 | self.shape_list.append(LamellarPSHGModel) |
---|
283 | |
---|
284 | from sans.models.LamellarPCrystalModel import LamellarPCrystalModel |
---|
285 | self.shape_list.append(LamellarPCrystalModel) |
---|
286 | |
---|
287 | from sans.models.SCCrystalModel import SCCrystalModel |
---|
288 | self.shape_list.append(SCCrystalModel) |
---|
289 | |
---|
290 | from sans.models.FCCrystalModel import FCCrystalModel |
---|
291 | self.shape_list.append(FCCrystalModel) |
---|
292 | |
---|
293 | from sans.models.BCCrystalModel import BCCrystalModel |
---|
294 | self.shape_list.append(BCCrystalModel) |
---|
295 | |
---|
296 | ## Structure factor |
---|
297 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
298 | self.struct_list.append(SquareWellStructure) |
---|
299 | |
---|
300 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
301 | self.struct_list.append(HardsphereStructure) |
---|
302 | |
---|
303 | from sans.models.StickyHSStructure import StickyHSStructure |
---|
304 | self.struct_list.append(StickyHSStructure) |
---|
305 | |
---|
306 | from sans.models.HayterMSAStructure import HayterMSAStructure |
---|
307 | self.struct_list.append(HayterMSAStructure) |
---|
308 | |
---|
309 | |
---|
310 | ##shape-independent models |
---|
311 | |
---|
312 | from sans.models.PowerLawAbsModel import PowerLawAbsModel |
---|
313 | self.shape_indep_list.append( PowerLawAbsModel ) |
---|
314 | |
---|
315 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
316 | self.shape_indep_list.append(BEPolyelectrolyte ) |
---|
317 | self.form_factor_dict[str(wx.NewId())] = [SphereModel] |
---|
318 | |
---|
319 | from sans.models.BroadPeakModel import BroadPeakModel |
---|
320 | self.shape_indep_list.append(BroadPeakModel) |
---|
321 | |
---|
322 | from sans.models.CorrLengthModel import CorrLengthModel |
---|
323 | self.shape_indep_list.append(CorrLengthModel) |
---|
324 | |
---|
325 | from sans.models.DABModel import DABModel |
---|
326 | self.shape_indep_list.append(DABModel ) |
---|
327 | |
---|
328 | from sans.models.DebyeModel import DebyeModel |
---|
329 | self.shape_indep_list.append(DebyeModel ) |
---|
330 | |
---|
331 | #FractalModel (a c-model)is now being used instead of FractalAbsModel. |
---|
332 | from sans.models.FractalModel import FractalModel |
---|
333 | self.shape_indep_list.append(FractalModel ) |
---|
334 | |
---|
335 | from sans.models.FractalCoreShellModel import FractalCoreShellModel |
---|
336 | self.shape_indep_list.append(FractalCoreShellModel ) |
---|
337 | |
---|
338 | from sans.models.GaussLorentzGelModel import GaussLorentzGelModel |
---|
339 | self.shape_indep_list.append(GaussLorentzGelModel) |
---|
340 | |
---|
341 | from sans.models.GuinierModel import GuinierModel |
---|
342 | self.shape_indep_list.append(GuinierModel ) |
---|
343 | |
---|
344 | from sans.models.GuinierPorodModel import GuinierPorodModel |
---|
345 | self.shape_indep_list.append(GuinierPorodModel ) |
---|
346 | |
---|
347 | from sans.models.LorentzModel import LorentzModel |
---|
348 | self.shape_indep_list.append( LorentzModel) |
---|
349 | |
---|
350 | from sans.models.PeakGaussModel import PeakGaussModel |
---|
351 | self.shape_indep_list.append(PeakGaussModel) |
---|
352 | |
---|
353 | from sans.models.PeakLorentzModel import PeakLorentzModel |
---|
354 | self.shape_indep_list.append(PeakLorentzModel) |
---|
355 | |
---|
356 | from sans.models.Poly_GaussCoil import Poly_GaussCoil |
---|
357 | self.shape_indep_list.append(Poly_GaussCoil) |
---|
358 | |
---|
359 | from sans.models.PolymerExclVolume import PolymerExclVolume |
---|
360 | self.shape_indep_list.append(PolymerExclVolume) |
---|
361 | |
---|
362 | from sans.models.PorodModel import PorodModel |
---|
363 | self.shape_indep_list.append(PorodModel ) |
---|
364 | |
---|
365 | from sans.models.RPA10Model import RPA10Model |
---|
366 | self.shape_indep_list.append(RPA10Model) |
---|
367 | self.multi_func_list.append(RPA10Model) |
---|
368 | |
---|
369 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
370 | self.shape_indep_list.append(TeubnerStreyModel ) |
---|
371 | |
---|
372 | from sans.models.TwoLorentzianModel import TwoLorentzianModel |
---|
373 | self.shape_indep_list.append(TwoLorentzianModel ) |
---|
374 | |
---|
375 | from sans.models.TwoPowerLawModel import TwoPowerLawModel |
---|
376 | self.shape_indep_list.append(TwoPowerLawModel ) |
---|
377 | |
---|
378 | from sans.models.UnifiedPowerRgModel import UnifiedPowerRgModel |
---|
379 | self.shape_indep_list.append(UnifiedPowerRgModel ) |
---|
380 | self.multi_func_list.append(UnifiedPowerRgModel) |
---|
381 | |
---|
382 | from sans.models.LineModel import LineModel |
---|
383 | self.shape_indep_list.append(LineModel) |
---|
384 | |
---|
385 | from sans.models.ReflectivityModel import ReflectivityModel |
---|
386 | self.multi_func_list.append(ReflectivityModel) |
---|
387 | |
---|
388 | #Looking for plugins |
---|
389 | self.plugins = findModels() |
---|
390 | self._get_multifunc_models() |
---|
391 | self.plugins.append(ReflectivityModel) |
---|
392 | return 0 |
---|
393 | |
---|
394 | def get_model_list(self): |
---|
395 | """ |
---|
396 | :return: dictionary of models for fitpanel use |
---|
397 | |
---|
398 | """ |
---|
399 | self._getModelList() |
---|
400 | self.model_combobox.set_list("Shapes", self.shape_list) |
---|
401 | self.model_combobox.set_list("Shape-Independent", self.shape_indep_list) |
---|
402 | self.model_combobox.set_list("Structure Factors", self.struct_list) |
---|
403 | self.model_combobox.set_list("Customized Models", self.plugins) |
---|
404 | self.model_combobox.set_list("P(Q)*S(Q)", self.multiplication_factor) |
---|
405 | self.model_combobox.set_list("multiplication", self.multiplication_factor) |
---|
406 | self.model_combobox.set_list("Multi-Functions", self.multi_func_list) |
---|
407 | return self.model_combobox |
---|
408 | |
---|
409 | def _get_multifunc_models(self): |
---|
410 | """ |
---|
411 | Get the multifunctional models |
---|
412 | """ |
---|
413 | for item in self.plugins: |
---|
414 | try: |
---|
415 | # check the multiplicity if any |
---|
416 | if item.multiplicity_info[0] > 1: |
---|
417 | self.multi_func_list.append(item) |
---|
418 | except: |
---|
419 | # pass to other items |
---|
420 | pass |
---|