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 added models |
---|
168 | plugins=[] |
---|
169 | ## Event owner (guiframe) |
---|
170 | event_owner = None |
---|
171 | |
---|
172 | def _getModelList(self): |
---|
173 | """ |
---|
174 | Fill up lists of models available by default |
---|
175 | for the current application |
---|
176 | |
---|
177 | :return: the next free event ID following the new menu events |
---|
178 | |
---|
179 | """ |
---|
180 | ## form factor |
---|
181 | from sans.models.SphereModel import SphereModel |
---|
182 | self.shape_list.append(SphereModel) |
---|
183 | self.multiplication_factor.append(SphereModel) |
---|
184 | |
---|
185 | from sans.models.FuzzySphereModel import FuzzySphereModel |
---|
186 | self.shape_list.append(FuzzySphereModel) |
---|
187 | self.multiplication_factor.append(FuzzySphereModel) |
---|
188 | |
---|
189 | from sans.models.CoreShellModel import CoreShellModel |
---|
190 | self.shape_list.append(CoreShellModel) |
---|
191 | self.multiplication_factor.append(CoreShellModel) |
---|
192 | |
---|
193 | from sans.models.CoreFourShellModel import CoreFourShellModel |
---|
194 | self.shape_list.append(CoreFourShellModel) |
---|
195 | self.multiplication_factor.append(CoreFourShellModel) |
---|
196 | |
---|
197 | from sans.models.VesicleModel import VesicleModel |
---|
198 | self.shape_list.append(VesicleModel) |
---|
199 | self.multiplication_factor.append(VesicleModel) |
---|
200 | |
---|
201 | from sans.models.MultiShellModel import MultiShellModel |
---|
202 | self.shape_list.append(MultiShellModel) |
---|
203 | self.multiplication_factor.append(MultiShellModel) |
---|
204 | |
---|
205 | from sans.models.BinaryHSModel import BinaryHSModel |
---|
206 | self.shape_list.append(BinaryHSModel) |
---|
207 | |
---|
208 | from sans.models.CylinderModel import CylinderModel |
---|
209 | self.shape_list.append(CylinderModel) |
---|
210 | self.multiplication_factor.append(CylinderModel) |
---|
211 | |
---|
212 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
213 | self.shape_list.append(CoreShellCylinderModel) |
---|
214 | self.multiplication_factor.append(CoreShellCylinderModel) |
---|
215 | |
---|
216 | from sans.models.HollowCylinderModel import HollowCylinderModel |
---|
217 | self.shape_list.append(HollowCylinderModel) |
---|
218 | self.multiplication_factor.append(HollowCylinderModel) |
---|
219 | |
---|
220 | from sans.models.FlexibleCylinderModel import FlexibleCylinderModel |
---|
221 | self.shape_list.append(FlexibleCylinderModel) |
---|
222 | |
---|
223 | from sans.models.FlexCylEllipXModel import FlexCylEllipXModel |
---|
224 | self.shape_list.append(FlexCylEllipXModel) |
---|
225 | |
---|
226 | from sans.models.StackedDisksModel import StackedDisksModel |
---|
227 | self.shape_list.append(StackedDisksModel) |
---|
228 | self.multiplication_factor.append(StackedDisksModel) |
---|
229 | |
---|
230 | from sans.models.ParallelepipedModel import ParallelepipedModel |
---|
231 | self.shape_list.append(ParallelepipedModel) |
---|
232 | self.multiplication_factor.append(ParallelepipedModel) |
---|
233 | |
---|
234 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
235 | self.shape_list.append(EllipticalCylinderModel) |
---|
236 | self.multiplication_factor.append(EllipticalCylinderModel) |
---|
237 | |
---|
238 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
239 | self.shape_list.append(EllipsoidModel) |
---|
240 | self.multiplication_factor.append(EllipsoidModel) |
---|
241 | |
---|
242 | from sans.models.CoreShellEllipsoidModel import CoreShellEllipsoidModel |
---|
243 | self.shape_list.append(CoreShellEllipsoidModel) |
---|
244 | self.multiplication_factor.append(CoreShellEllipsoidModel) |
---|
245 | |
---|
246 | from sans.models.TriaxialEllipsoidModel import TriaxialEllipsoidModel |
---|
247 | self.shape_list.append(TriaxialEllipsoidModel) |
---|
248 | self.multiplication_factor.append(TriaxialEllipsoidModel) |
---|
249 | |
---|
250 | from sans.models.LamellarModel import LamellarModel |
---|
251 | self.shape_list.append(LamellarModel) |
---|
252 | |
---|
253 | from sans.models.LamellarFFHGModel import LamellarFFHGModel |
---|
254 | self.shape_list.append(LamellarFFHGModel) |
---|
255 | |
---|
256 | from sans.models.LamellarPSModel import LamellarPSModel |
---|
257 | self.shape_list.append(LamellarPSModel) |
---|
258 | |
---|
259 | from sans.models.LamellarPSHGModel import LamellarPSHGModel |
---|
260 | self.shape_list.append(LamellarPSHGModel) |
---|
261 | |
---|
262 | ## Structure factor |
---|
263 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
264 | self.struct_list.append(SquareWellStructure) |
---|
265 | |
---|
266 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
267 | self.struct_list.append(HardsphereStructure) |
---|
268 | |
---|
269 | from sans.models.StickyHSStructure import StickyHSStructure |
---|
270 | self.struct_list.append(StickyHSStructure) |
---|
271 | |
---|
272 | from sans.models.HayterMSAStructure import HayterMSAStructure |
---|
273 | self.struct_list.append(HayterMSAStructure) |
---|
274 | |
---|
275 | |
---|
276 | ##shape-independent models |
---|
277 | |
---|
278 | from sans.models.PowerLawAbsModel import PowerLawAbsModel |
---|
279 | self.shape_indep_list.append( PowerLawAbsModel ) |
---|
280 | |
---|
281 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
282 | self.shape_indep_list.append(BEPolyelectrolyte ) |
---|
283 | self.form_factor_dict[str(wx.NewId())] = [SphereModel] |
---|
284 | |
---|
285 | from sans.models.DABModel import DABModel |
---|
286 | self.shape_indep_list.append(DABModel ) |
---|
287 | |
---|
288 | from sans.models.DebyeModel import DebyeModel |
---|
289 | self.shape_indep_list.append(DebyeModel ) |
---|
290 | |
---|
291 | from sans.models.GuinierModel import GuinierModel |
---|
292 | self.shape_indep_list.append(GuinierModel ) |
---|
293 | |
---|
294 | from sans.models.FractalModel import FractalModel |
---|
295 | self.shape_indep_list.append(FractalModel ) |
---|
296 | |
---|
297 | from sans.models.LorentzModel import LorentzModel |
---|
298 | self.shape_indep_list.append( LorentzModel) |
---|
299 | |
---|
300 | from sans.models.PeakGaussModel import PeakGaussModel |
---|
301 | self.shape_indep_list.append(PeakGaussModel) |
---|
302 | |
---|
303 | from sans.models.PeakLorentzModel import PeakLorentzModel |
---|
304 | self.shape_indep_list.append(PeakLorentzModel) |
---|
305 | |
---|
306 | from sans.models.Poly_GaussCoil import Poly_GaussCoil |
---|
307 | self.shape_indep_list.append(Poly_GaussCoil) |
---|
308 | |
---|
309 | from sans.models.PorodModel import PorodModel |
---|
310 | self.shape_indep_list.append(PorodModel ) |
---|
311 | |
---|
312 | #FractalModel (a c-model)will be used. |
---|
313 | #from sans.models.FractalAbsModel import FractalAbsModel |
---|
314 | #self.shape_indep_list.append(FractalAbsModel) |
---|
315 | |
---|
316 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
317 | self.shape_indep_list.append(TeubnerStreyModel ) |
---|
318 | |
---|
319 | from sans.models.LineModel import LineModel |
---|
320 | self.shape_indep_list.append(LineModel) |
---|
321 | |
---|
322 | #Looking for plugins |
---|
323 | self.plugins = findModels() |
---|
324 | |
---|
325 | return 0 |
---|
326 | |
---|
327 | def get_model_list(self): |
---|
328 | """ |
---|
329 | :return: dictionary of models for fitpanel use |
---|
330 | |
---|
331 | """ |
---|
332 | self._getModelList() |
---|
333 | self.model_combobox.set_list("Shapes", self.shape_list) |
---|
334 | self.model_combobox.set_list("Shape-Independent", self.shape_indep_list) |
---|
335 | self.model_combobox.set_list("Structure Factors", self.struct_list) |
---|
336 | self.model_combobox.set_list("Customized Models", self.plugins) |
---|
337 | self.model_combobox.set_list("P(Q)*S(Q)", self.multiplication_factor) |
---|
338 | self.model_combobox.set_list("multiplication", self.multiplication_factor) |
---|
339 | return self.model_combobox |
---|
340 | |
---|