1 | |
---|
2 | import wx |
---|
3 | import wx.lib.newevent |
---|
4 | import imp |
---|
5 | import os |
---|
6 | import sys |
---|
7 | import math |
---|
8 | import os.path |
---|
9 | # Time is needed by the log method |
---|
10 | import time |
---|
11 | import logging |
---|
12 | import py_compile |
---|
13 | import shutil |
---|
14 | from sans.guiframe.events import StatusEvent |
---|
15 | # Explicitly import from the pluginmodel module so that py2exe |
---|
16 | # places it in the distribution. The Model1DPlugin class is used |
---|
17 | # as the base class of plug-in models. |
---|
18 | from sans.models.pluginmodel import Model1DPlugin |
---|
19 | |
---|
20 | PLUGIN_DIR = 'plugins' |
---|
21 | |
---|
22 | def log(message): |
---|
23 | """ |
---|
24 | """ |
---|
25 | out = open("plugins.log", 'a') |
---|
26 | out.write("%10g: %s\n" % (time.clock(), message)) |
---|
27 | out.close() |
---|
28 | |
---|
29 | |
---|
30 | def _check_plugin(model, name): |
---|
31 | """ |
---|
32 | Do some checking before model adding plugins in the list |
---|
33 | |
---|
34 | :param model: class model to add into the plugin list |
---|
35 | :param name:name of the module plugin |
---|
36 | |
---|
37 | :return model: model if valid model or None if not valid |
---|
38 | |
---|
39 | """ |
---|
40 | #Check is the plugin is of type Model1DPlugin |
---|
41 | if not issubclass(model, Model1DPlugin): |
---|
42 | msg = "Plugin %s must be of type Model1DPlugin \n"%str(name) |
---|
43 | log(msg) |
---|
44 | return None |
---|
45 | if model.__name__!="Model": |
---|
46 | msg= "Plugin %s class name must be Model \n"%str(name) |
---|
47 | log(msg) |
---|
48 | return None |
---|
49 | try: |
---|
50 | new_instance= model() |
---|
51 | except: |
---|
52 | msg="Plugin %s error in __init__ \n\t: %s %s\n"%(str(name), |
---|
53 | str(sys.exc_type),sys.exc_value) |
---|
54 | log(msg) |
---|
55 | return None |
---|
56 | |
---|
57 | new_instance= model() |
---|
58 | if hasattr(new_instance,"function"): |
---|
59 | try: |
---|
60 | value=new_instance.function() |
---|
61 | except: |
---|
62 | msg="Plugin %s: error writing function \n\t :%s %s\n "%(str(name), |
---|
63 | str(sys.exc_type),sys.exc_value) |
---|
64 | log(msg) |
---|
65 | return None |
---|
66 | else: |
---|
67 | msg="Plugin %s needs a method called function \n"%str(name) |
---|
68 | log(msg) |
---|
69 | return None |
---|
70 | return model |
---|
71 | |
---|
72 | def find_plugins_dir(): |
---|
73 | """ |
---|
74 | Find path of the plugins directory. |
---|
75 | The plugin directory is located in the user's home directory. |
---|
76 | """ |
---|
77 | dir = os.path.join(os.path.expanduser("~"),'.sansview',PLUGIN_DIR) |
---|
78 | |
---|
79 | # If the plugin directory doesn't exist, create it |
---|
80 | if not os.path.isdir(dir): |
---|
81 | os.makedirs(dir) |
---|
82 | |
---|
83 | # Place example user models as needed |
---|
84 | if not os.path.isfile(os.path.join(dir,"testmodel.py")): |
---|
85 | shutil.copy(os.path.join(os.path.dirname(__file__),"plugin_models","testmodel.py"), dir) |
---|
86 | if not os.path.isfile(os.path.join(dir,"testmodel_2.py")): |
---|
87 | shutil.copy(os.path.join(os.path.dirname(__file__),"plugin_models","testmodel_2.py"), dir) |
---|
88 | if not os.path.isfile(os.path.join(dir,"sum_p1_p2.py")): |
---|
89 | shutil.copy(os.path.join(os.path.dirname(__file__),"plugin_models","sum_p1_p2.py"), dir) |
---|
90 | |
---|
91 | return dir |
---|
92 | |
---|
93 | class ReportProblem: |
---|
94 | def __nonzero__(self): |
---|
95 | type, value, traceback = sys.exc_info() |
---|
96 | if type is not None and issubclass(type, py_compile.PyCompileError): |
---|
97 | print "Problem with", repr(value) |
---|
98 | raise type, value, traceback |
---|
99 | return 1 |
---|
100 | |
---|
101 | report_problem = ReportProblem() |
---|
102 | |
---|
103 | def compile_file(dir): |
---|
104 | """ |
---|
105 | Compile a py file |
---|
106 | """ |
---|
107 | try: |
---|
108 | import compileall |
---|
109 | compileall.compile_dir(dir=dir, ddir=dir, force=1, quiet=report_problem) |
---|
110 | except: |
---|
111 | type, value, traceback = sys.exc_info() |
---|
112 | return value |
---|
113 | return None |
---|
114 | |
---|
115 | def _findModels(dir): |
---|
116 | """ |
---|
117 | """ |
---|
118 | # List of plugin objects |
---|
119 | plugins = {} |
---|
120 | # Go through files in plug-in directory |
---|
121 | #always recompile the folder plugin |
---|
122 | import compileall |
---|
123 | dir = find_plugins_dir() |
---|
124 | if not os.path.isdir(dir): |
---|
125 | msg = "SansView couldn't locate Model plugin folder." |
---|
126 | msg += """ "%s" does not exist""" % dir |
---|
127 | logging.warning(msg) |
---|
128 | return plugins |
---|
129 | else: |
---|
130 | log("looking for models in: %s" % str(dir)) |
---|
131 | compile_file(dir) |
---|
132 | logging.info("pluging model dir: %s\n" % str(dir)) |
---|
133 | try: |
---|
134 | list = os.listdir(dir) |
---|
135 | for item in list: |
---|
136 | toks = os.path.splitext(os.path.basename(item)) |
---|
137 | if toks[1]=='.py' and not toks[0]=='__init__': |
---|
138 | name = toks[0] |
---|
139 | |
---|
140 | path = [os.path.abspath(dir)] |
---|
141 | file = None |
---|
142 | try: |
---|
143 | (file, path, info) = imp.find_module(name, path) |
---|
144 | module = imp.load_module( name, file, item, info ) |
---|
145 | if hasattr(module, "Model"): |
---|
146 | try: |
---|
147 | if _check_plugin(module.Model, name)!=None: |
---|
148 | plugins[name] = module.Model |
---|
149 | except: |
---|
150 | msg="Error accessing Model" |
---|
151 | msg+="in %s\n %s %s\n" % (name, |
---|
152 | str(sys.exc_type), sys.exc_value) |
---|
153 | log(msg) |
---|
154 | except: |
---|
155 | msg="Error accessing Model" |
---|
156 | msg +=" in %s\n %s %s \n" %(name, |
---|
157 | str(sys.exc_type), sys.exc_value) |
---|
158 | log(msg) |
---|
159 | finally: |
---|
160 | |
---|
161 | if not file==None: |
---|
162 | file.close() |
---|
163 | except: |
---|
164 | # Don't deal with bad plug-in imports. Just skip. |
---|
165 | msg = "Could not import model plugin: %s\n" % sys.exc_value |
---|
166 | log(msg) |
---|
167 | pass |
---|
168 | return plugins |
---|
169 | |
---|
170 | class ModelList(object): |
---|
171 | """ |
---|
172 | Contains dictionary of model and their type |
---|
173 | """ |
---|
174 | def __init__(self): |
---|
175 | """ |
---|
176 | """ |
---|
177 | self.mydict = {} |
---|
178 | |
---|
179 | def set_list(self, name, mylist): |
---|
180 | """ |
---|
181 | :param name: the type of the list |
---|
182 | :param mylist: the list to add |
---|
183 | |
---|
184 | """ |
---|
185 | if name not in self.mydict.keys(): |
---|
186 | self.reset_list(name, mylist) |
---|
187 | |
---|
188 | def reset_list(self, name, mylist): |
---|
189 | """ |
---|
190 | :param name: the type of the list |
---|
191 | :param mylist: the list to add |
---|
192 | """ |
---|
193 | self.mydict[name] = mylist |
---|
194 | |
---|
195 | def get_list(self): |
---|
196 | """ |
---|
197 | return all the list stored in a dictionary object |
---|
198 | """ |
---|
199 | return self.mydict |
---|
200 | |
---|
201 | class ModelManagerBase: |
---|
202 | """ |
---|
203 | """ |
---|
204 | ## external dict for models |
---|
205 | model_combobox = ModelList() |
---|
206 | ## Dictionary of form models |
---|
207 | form_factor_dict = {} |
---|
208 | ## dictionary of other |
---|
209 | struct_factor_dict = {} |
---|
210 | ##list of form factors |
---|
211 | shape_list = [] |
---|
212 | ## independent shape model list |
---|
213 | shape_indep_list = [] |
---|
214 | ##list of structure factors |
---|
215 | struct_list = [] |
---|
216 | ##list of model allowing multiplication |
---|
217 | multiplication_factor = [] |
---|
218 | ##list of multifunctional shapes |
---|
219 | multi_func_list = [] |
---|
220 | ## list of added models |
---|
221 | plugins = [] |
---|
222 | ## Event owner (guiframe) |
---|
223 | event_owner = None |
---|
224 | last_time_dir_modified = 0 |
---|
225 | def __init__(self): |
---|
226 | """ |
---|
227 | """ |
---|
228 | self.stored_plugins = {} |
---|
229 | self._getModelList() |
---|
230 | |
---|
231 | def findModels(self): |
---|
232 | """ |
---|
233 | find plugin model in directory of plugin .recompile all file |
---|
234 | in the directory if file were modified |
---|
235 | """ |
---|
236 | temp = {} |
---|
237 | if self.is_changed(): |
---|
238 | return _findModels(dir) |
---|
239 | logging.info("pluging model : %s\n" % str(temp)) |
---|
240 | return temp |
---|
241 | |
---|
242 | def _getModelList(self): |
---|
243 | """ |
---|
244 | List of models we want to make available by default |
---|
245 | for this application |
---|
246 | |
---|
247 | :return: the next free event ID following the new menu events |
---|
248 | |
---|
249 | """ |
---|
250 | # regular model names only |
---|
251 | self.model_name_list = [] |
---|
252 | from sans.models.SphereModel import SphereModel |
---|
253 | self.shape_list.append(SphereModel) |
---|
254 | self.multiplication_factor.append(SphereModel) |
---|
255 | self.model_name_list.append(SphereModel.__name__) |
---|
256 | |
---|
257 | from sans.models.BinaryHSModel import BinaryHSModel |
---|
258 | self.shape_list.append(BinaryHSModel) |
---|
259 | self.model_name_list.append(BinaryHSModel.__name__) |
---|
260 | |
---|
261 | from sans.models.FuzzySphereModel import FuzzySphereModel |
---|
262 | self.shape_list.append(FuzzySphereModel) |
---|
263 | self.multiplication_factor.append(FuzzySphereModel) |
---|
264 | self.model_name_list.append(FuzzySphereModel.__name__) |
---|
265 | |
---|
266 | from sans.models.CoreShellModel import CoreShellModel |
---|
267 | self.shape_list.append(CoreShellModel) |
---|
268 | self.multiplication_factor.append(CoreShellModel) |
---|
269 | self.model_name_list.append(CoreShellModel.__name__) |
---|
270 | |
---|
271 | from sans.models.CoreMultiShellModel import CoreMultiShellModel |
---|
272 | self.shape_list.append(CoreMultiShellModel) |
---|
273 | self.multiplication_factor.append(CoreMultiShellModel) |
---|
274 | self.multi_func_list.append(CoreMultiShellModel) |
---|
275 | |
---|
276 | from sans.models.VesicleModel import VesicleModel |
---|
277 | self.shape_list.append(VesicleModel) |
---|
278 | self.multiplication_factor.append(VesicleModel) |
---|
279 | self.model_name_list.append(VesicleModel.__name__) |
---|
280 | |
---|
281 | from sans.models.MultiShellModel import MultiShellModel |
---|
282 | self.shape_list.append(MultiShellModel) |
---|
283 | self.multiplication_factor.append(MultiShellModel) |
---|
284 | self.model_name_list.append(MultiShellModel.__name__) |
---|
285 | |
---|
286 | from sans.models.OnionExpShellModel import OnionExpShellModel |
---|
287 | self.shape_list.append(OnionExpShellModel) |
---|
288 | self.multiplication_factor.append(OnionExpShellModel) |
---|
289 | self.multi_func_list.append(OnionExpShellModel) |
---|
290 | |
---|
291 | from sans.models.SphericalSLDModel import SphericalSLDModel |
---|
292 | self.shape_list.append(SphericalSLDModel) |
---|
293 | self.multiplication_factor.append(SphericalSLDModel) |
---|
294 | self.multi_func_list.append(SphericalSLDModel) |
---|
295 | |
---|
296 | from sans.models.PearlNecklaceModel import PearlNecklaceModel |
---|
297 | self.shape_list.append(PearlNecklaceModel) |
---|
298 | self.model_name_list.append(PearlNecklaceModel.__name__) |
---|
299 | #self.multiplication_factor.append(PearlNecklaceModel) |
---|
300 | |
---|
301 | from sans.models.CylinderModel import CylinderModel |
---|
302 | self.shape_list.append(CylinderModel) |
---|
303 | self.multiplication_factor.append(CylinderModel) |
---|
304 | self.model_name_list.append(CylinderModel.__name__) |
---|
305 | |
---|
306 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
307 | self.shape_list.append(CoreShellCylinderModel) |
---|
308 | self.multiplication_factor.append(CoreShellCylinderModel) |
---|
309 | self.model_name_list.append(CoreShellCylinderModel.__name__) |
---|
310 | |
---|
311 | from sans.models.HollowCylinderModel import HollowCylinderModel |
---|
312 | self.shape_list.append(HollowCylinderModel) |
---|
313 | self.multiplication_factor.append(HollowCylinderModel) |
---|
314 | self.model_name_list.append(HollowCylinderModel.__name__) |
---|
315 | |
---|
316 | from sans.models.FlexibleCylinderModel import FlexibleCylinderModel |
---|
317 | self.shape_list.append(FlexibleCylinderModel) |
---|
318 | self.model_name_list.append(FlexibleCylinderModel.__name__) |
---|
319 | |
---|
320 | from sans.models.FlexCylEllipXModel import FlexCylEllipXModel |
---|
321 | self.shape_list.append(FlexCylEllipXModel) |
---|
322 | self.model_name_list.append(FlexCylEllipXModel.__name__) |
---|
323 | |
---|
324 | from sans.models.StackedDisksModel import StackedDisksModel |
---|
325 | self.shape_list.append(StackedDisksModel) |
---|
326 | self.multiplication_factor.append(StackedDisksModel) |
---|
327 | self.model_name_list.append(StackedDisksModel.__name__) |
---|
328 | |
---|
329 | from sans.models.ParallelepipedModel import ParallelepipedModel |
---|
330 | self.shape_list.append(ParallelepipedModel) |
---|
331 | self.multiplication_factor.append(ParallelepipedModel) |
---|
332 | self.model_name_list.append(ParallelepipedModel.__name__) |
---|
333 | |
---|
334 | from sans.models.CSParallelepipedModel import CSParallelepipedModel |
---|
335 | self.shape_list.append(CSParallelepipedModel) |
---|
336 | self.multiplication_factor.append(CSParallelepipedModel) |
---|
337 | self.model_name_list.append(CSParallelepipedModel.__name__) |
---|
338 | |
---|
339 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
340 | self.shape_list.append(EllipticalCylinderModel) |
---|
341 | self.multiplication_factor.append(EllipticalCylinderModel) |
---|
342 | self.model_name_list.append(EllipticalCylinderModel.__name__) |
---|
343 | |
---|
344 | from sans.models.BarBellModel import BarBellModel |
---|
345 | self.shape_list.append(BarBellModel) |
---|
346 | self.model_name_list.append(BarBellModel.__name__) |
---|
347 | # not implemeted yet! |
---|
348 | #self.multiplication_factor.append(BarBellModel) |
---|
349 | |
---|
350 | from sans.models.CappedCylinderModel import CappedCylinderModel |
---|
351 | self.shape_list.append(CappedCylinderModel) |
---|
352 | self.model_name_list.append(CappedCylinderModel.__name__) |
---|
353 | # not implemeted yet! |
---|
354 | #self.multiplication_factor.append(CappedCylinderModel) |
---|
355 | |
---|
356 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
357 | self.shape_list.append(EllipsoidModel) |
---|
358 | self.multiplication_factor.append(EllipsoidModel) |
---|
359 | self.model_name_list.append(EllipsoidModel.__name__) |
---|
360 | |
---|
361 | from sans.models.CoreShellEllipsoidModel import CoreShellEllipsoidModel |
---|
362 | self.shape_list.append(CoreShellEllipsoidModel) |
---|
363 | self.multiplication_factor.append(CoreShellEllipsoidModel) |
---|
364 | self.model_name_list.append(CoreShellEllipsoidModel.__name__) |
---|
365 | |
---|
366 | from sans.models.TriaxialEllipsoidModel import TriaxialEllipsoidModel |
---|
367 | self.shape_list.append(TriaxialEllipsoidModel) |
---|
368 | self.multiplication_factor.append(TriaxialEllipsoidModel) |
---|
369 | self.model_name_list.append(TriaxialEllipsoidModel.__name__) |
---|
370 | |
---|
371 | from sans.models.LamellarModel import LamellarModel |
---|
372 | self.shape_list.append(LamellarModel) |
---|
373 | self.model_name_list.append(LamellarModel.__name__) |
---|
374 | |
---|
375 | from sans.models.LamellarFFHGModel import LamellarFFHGModel |
---|
376 | self.shape_list.append(LamellarFFHGModel) |
---|
377 | self.model_name_list.append(LamellarFFHGModel.__name__) |
---|
378 | |
---|
379 | from sans.models.LamellarPSModel import LamellarPSModel |
---|
380 | self.shape_list.append(LamellarPSModel) |
---|
381 | self.model_name_list.append(LamellarPSModel.__name__) |
---|
382 | |
---|
383 | from sans.models.LamellarPSHGModel import LamellarPSHGModel |
---|
384 | self.shape_list.append(LamellarPSHGModel) |
---|
385 | self.model_name_list.append(LamellarPSHGModel.__name__) |
---|
386 | |
---|
387 | from sans.models.LamellarPCrystalModel import LamellarPCrystalModel |
---|
388 | self.shape_list.append(LamellarPCrystalModel) |
---|
389 | self.model_name_list.append(LamellarPCrystalModel.__name__) |
---|
390 | |
---|
391 | from sans.models.SCCrystalModel import SCCrystalModel |
---|
392 | self.shape_list.append(SCCrystalModel) |
---|
393 | self.model_name_list.append(SCCrystalModel.__name__) |
---|
394 | |
---|
395 | from sans.models.FCCrystalModel import FCCrystalModel |
---|
396 | self.shape_list.append(FCCrystalModel) |
---|
397 | self.model_name_list.append(FCCrystalModel.__name__) |
---|
398 | |
---|
399 | from sans.models.BCCrystalModel import BCCrystalModel |
---|
400 | self.shape_list.append(BCCrystalModel) |
---|
401 | self.model_name_list.append(BCCrystalModel.__name__) |
---|
402 | |
---|
403 | ## Structure factor |
---|
404 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
405 | self.struct_list.append(SquareWellStructure) |
---|
406 | self.model_name_list.append(SquareWellStructure.__name__) |
---|
407 | |
---|
408 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
409 | self.struct_list.append(HardsphereStructure) |
---|
410 | self.model_name_list.append(HardsphereStructure.__name__) |
---|
411 | |
---|
412 | from sans.models.StickyHSStructure import StickyHSStructure |
---|
413 | self.struct_list.append(StickyHSStructure) |
---|
414 | self.model_name_list.append(StickyHSStructure.__name__) |
---|
415 | |
---|
416 | from sans.models.HayterMSAStructure import HayterMSAStructure |
---|
417 | self.struct_list.append(HayterMSAStructure) |
---|
418 | self.model_name_list.append(HayterMSAStructure.__name__) |
---|
419 | |
---|
420 | ##shape-independent models |
---|
421 | from sans.models.PowerLawAbsModel import PowerLawAbsModel |
---|
422 | self.shape_indep_list.append( PowerLawAbsModel ) |
---|
423 | self.model_name_list.append(PowerLawAbsModel.__name__) |
---|
424 | |
---|
425 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
426 | self.shape_indep_list.append(BEPolyelectrolyte ) |
---|
427 | self.model_name_list.append(BEPolyelectrolyte.__name__) |
---|
428 | self.form_factor_dict[str(wx.NewId())] = [SphereModel] |
---|
429 | |
---|
430 | from sans.models.BroadPeakModel import BroadPeakModel |
---|
431 | self.shape_indep_list.append(BroadPeakModel) |
---|
432 | self.model_name_list.append(BroadPeakModel.__name__) |
---|
433 | |
---|
434 | from sans.models.CorrLengthModel import CorrLengthModel |
---|
435 | self.shape_indep_list.append(CorrLengthModel) |
---|
436 | self.model_name_list.append(CorrLengthModel.__name__) |
---|
437 | |
---|
438 | from sans.models.DABModel import DABModel |
---|
439 | self.shape_indep_list.append(DABModel ) |
---|
440 | self.model_name_list.append(DABModel.__name__) |
---|
441 | |
---|
442 | from sans.models.DebyeModel import DebyeModel |
---|
443 | self.shape_indep_list.append(DebyeModel ) |
---|
444 | self.model_name_list.append(DebyeModel.__name__) |
---|
445 | |
---|
446 | #FractalModel (a c-model)is now being used instead of FractalAbsModel. |
---|
447 | from sans.models.FractalModel import FractalModel |
---|
448 | self.shape_indep_list.append(FractalModel ) |
---|
449 | self.model_name_list.append(FractalModel.__name__) |
---|
450 | |
---|
451 | from sans.models.FractalCoreShellModel import FractalCoreShellModel |
---|
452 | self.shape_indep_list.append(FractalCoreShellModel ) |
---|
453 | self.model_name_list.append(FractalCoreShellModel.__name__) |
---|
454 | |
---|
455 | from sans.models.GaussLorentzGelModel import GaussLorentzGelModel |
---|
456 | self.shape_indep_list.append(GaussLorentzGelModel) |
---|
457 | self.model_name_list.append(GaussLorentzGelModel.__name__) |
---|
458 | |
---|
459 | from sans.models.GuinierModel import GuinierModel |
---|
460 | self.shape_indep_list.append(GuinierModel ) |
---|
461 | self.model_name_list.append(GuinierModel.__name__) |
---|
462 | |
---|
463 | from sans.models.GuinierPorodModel import GuinierPorodModel |
---|
464 | self.shape_indep_list.append(GuinierPorodModel ) |
---|
465 | self.model_name_list.append(GuinierPorodModel.__name__) |
---|
466 | |
---|
467 | from sans.models.LorentzModel import LorentzModel |
---|
468 | self.shape_indep_list.append( LorentzModel) |
---|
469 | self.model_name_list.append(LorentzModel.__name__) |
---|
470 | |
---|
471 | from sans.models.PeakGaussModel import PeakGaussModel |
---|
472 | self.shape_indep_list.append(PeakGaussModel) |
---|
473 | self.model_name_list.append(PeakGaussModel.__name__) |
---|
474 | |
---|
475 | from sans.models.PeakLorentzModel import PeakLorentzModel |
---|
476 | self.shape_indep_list.append(PeakLorentzModel) |
---|
477 | self.model_name_list.append( PeakLorentzModel.__name__) |
---|
478 | |
---|
479 | from sans.models.Poly_GaussCoil import Poly_GaussCoil |
---|
480 | self.shape_indep_list.append(Poly_GaussCoil) |
---|
481 | self.model_name_list.append(Poly_GaussCoil.__name__) |
---|
482 | |
---|
483 | from sans.models.PolymerExclVolume import PolymerExclVolume |
---|
484 | self.shape_indep_list.append(PolymerExclVolume) |
---|
485 | self.model_name_list.append(PolymerExclVolume.__name__) |
---|
486 | |
---|
487 | from sans.models.PorodModel import PorodModel |
---|
488 | self.shape_indep_list.append(PorodModel ) |
---|
489 | self.model_name_list.append(PorodModel.__name__) |
---|
490 | |
---|
491 | from sans.models.RPA10Model import RPA10Model |
---|
492 | self.shape_indep_list.append(RPA10Model) |
---|
493 | self.multi_func_list.append(RPA10Model) |
---|
494 | |
---|
495 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
496 | self.shape_indep_list.append(TeubnerStreyModel ) |
---|
497 | self.model_name_list.append(TeubnerStreyModel.__name__) |
---|
498 | |
---|
499 | from sans.models.TwoLorentzianModel import TwoLorentzianModel |
---|
500 | self.shape_indep_list.append(TwoLorentzianModel ) |
---|
501 | self.model_name_list.append(TwoLorentzianModel.__name__) |
---|
502 | |
---|
503 | from sans.models.TwoPowerLawModel import TwoPowerLawModel |
---|
504 | self.shape_indep_list.append(TwoPowerLawModel ) |
---|
505 | self.model_name_list.append(TwoPowerLawModel.__name__) |
---|
506 | |
---|
507 | from sans.models.UnifiedPowerRgModel import UnifiedPowerRgModel |
---|
508 | self.shape_indep_list.append(UnifiedPowerRgModel ) |
---|
509 | self.multi_func_list.append(UnifiedPowerRgModel) |
---|
510 | |
---|
511 | from sans.models.LineModel import LineModel |
---|
512 | self.shape_indep_list.append(LineModel) |
---|
513 | self.model_name_list.append(LineModel.__name__) |
---|
514 | |
---|
515 | from sans.models.ReflectivityModel import ReflectivityModel |
---|
516 | self.multi_func_list.append(ReflectivityModel) |
---|
517 | |
---|
518 | from sans.models.ReflectivityIIModel import ReflectivityIIModel |
---|
519 | self.multi_func_list.append(ReflectivityIIModel) |
---|
520 | |
---|
521 | #Looking for plugins |
---|
522 | self.stored_plugins = self.findModels() |
---|
523 | self.plugins = self.stored_plugins.values() |
---|
524 | self.plugins.append(ReflectivityModel) |
---|
525 | self.plugins.append(ReflectivityIIModel) |
---|
526 | self._get_multifunc_models() |
---|
527 | |
---|
528 | return 0 |
---|
529 | |
---|
530 | def is_changed(self): |
---|
531 | """ |
---|
532 | check the last time the plugin dir has changed and return true |
---|
533 | is the directory was modified else return false |
---|
534 | """ |
---|
535 | is_modified = False |
---|
536 | plugin_dir = find_plugins_dir() |
---|
537 | if os.path.isdir(plugin_dir): |
---|
538 | temp = os.path.getmtime(plugin_dir) |
---|
539 | if self.last_time_dir_modified != temp: |
---|
540 | is_modified = True |
---|
541 | self.last_time_dir_modified = temp |
---|
542 | |
---|
543 | return is_modified |
---|
544 | |
---|
545 | def update(self): |
---|
546 | """ |
---|
547 | return a dictionary of model if |
---|
548 | new models were added else return empty dictionary |
---|
549 | """ |
---|
550 | new_plugins = self.findModels() |
---|
551 | if len(new_plugins) > 0: |
---|
552 | for name, plug in new_plugins.iteritems(): |
---|
553 | if name not in self.stored_plugins.keys(): |
---|
554 | self.stored_plugins[name] = plug |
---|
555 | self.plugins.append(plug) |
---|
556 | self.model_combobox.set_list("Customized Models", self.plugins) |
---|
557 | return self.model_combobox.get_list() |
---|
558 | else: |
---|
559 | return {} |
---|
560 | |
---|
561 | def pulgins_reset(self): |
---|
562 | """ |
---|
563 | return a dictionary of model |
---|
564 | """ |
---|
565 | self.plugins = [] |
---|
566 | new_plugins = _findModels(dir) |
---|
567 | for name, plug in new_plugins.iteritems(): |
---|
568 | for stored_name, stored_plug in self.stored_plugins.iteritems(): |
---|
569 | if name == stored_name: |
---|
570 | del self.stored_plugins[name] |
---|
571 | break |
---|
572 | self.stored_plugins[name] = plug |
---|
573 | self.plugins.append(plug) |
---|
574 | from sans.models.ReflectivityModel import ReflectivityModel |
---|
575 | from sans.models.ReflectivityIIModel import ReflectivityIIModel |
---|
576 | self.plugins.append(ReflectivityModel) |
---|
577 | self.plugins.append(ReflectivityIIModel) |
---|
578 | self.model_combobox.reset_list("Customized Models", self.plugins) |
---|
579 | return self.model_combobox.get_list() |
---|
580 | |
---|
581 | def populate_menu(self, modelmenu, event_owner): |
---|
582 | """ |
---|
583 | Populate a menu with our models |
---|
584 | |
---|
585 | :param id: first menu event ID to use when binding the menu events |
---|
586 | :param modelmenu: wx.Menu object to populate |
---|
587 | :param event_owner: wx object to bind the menu events to |
---|
588 | |
---|
589 | :return: the next free event ID following the new menu events |
---|
590 | |
---|
591 | """ |
---|
592 | ## Fill model lists |
---|
593 | self._getModelList() |
---|
594 | ## store reference to model menu of guiframe |
---|
595 | self.modelmenu = modelmenu |
---|
596 | ## guiframe reference |
---|
597 | self.event_owner = event_owner |
---|
598 | |
---|
599 | shape_submenu = wx.Menu() |
---|
600 | shape_indep_submenu = wx.Menu() |
---|
601 | structure_factor = wx.Menu() |
---|
602 | added_models = wx.Menu() |
---|
603 | multip_models = wx.Menu() |
---|
604 | ## create menu with shape |
---|
605 | self._fill_simple_menu(menuinfo=["Shapes",shape_submenu," simple shape"], |
---|
606 | list1=self.shape_list) |
---|
607 | |
---|
608 | self._fill_simple_menu(menuinfo=["Shape-Independent",shape_indep_submenu, |
---|
609 | "List of shape-independent models"], |
---|
610 | list1=self.shape_indep_list ) |
---|
611 | |
---|
612 | self._fill_simple_menu(menuinfo=["Structure Factors",structure_factor, |
---|
613 | "List of Structure factors models" ], |
---|
614 | list1=self.struct_list) |
---|
615 | |
---|
616 | self._fill_plugin_menu(menuinfo=["Customized Models", added_models, |
---|
617 | "List of additional models"], |
---|
618 | list1=self.plugins) |
---|
619 | |
---|
620 | self._fill_menu(menuinfo=["P(Q)*S(Q)",multip_models, |
---|
621 | "mulplication of 2 models"], |
---|
622 | list1=self.multiplication_factor , |
---|
623 | list2= self.struct_list) |
---|
624 | return 0 |
---|
625 | |
---|
626 | def _fill_plugin_menu(self, menuinfo, list1): |
---|
627 | """ |
---|
628 | fill the plugin menu with costumized models |
---|
629 | """ |
---|
630 | if len(list1)==0: |
---|
631 | id = wx.NewId() |
---|
632 | msg= "No model available check plugins.log for errors to fix problem" |
---|
633 | menuinfo[1].Append(int(id),"Empty",msg) |
---|
634 | self._fill_simple_menu( menuinfo,list1) |
---|
635 | |
---|
636 | def _fill_simple_menu(self, menuinfo, list1): |
---|
637 | """ |
---|
638 | Fill the menu with list item |
---|
639 | |
---|
640 | :param modelmenu: the menu to fill |
---|
641 | :param menuinfo: submenu item for the first column of this modelmenu |
---|
642 | with info.Should be a list : |
---|
643 | [name(string) , menu(wx.menu), help(string)] |
---|
644 | :param list1: contains item (form factor )to fill modelmenu second column |
---|
645 | |
---|
646 | """ |
---|
647 | if len(list1)>0: |
---|
648 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
649 | |
---|
650 | for item in list1: |
---|
651 | try: |
---|
652 | id = wx.NewId() |
---|
653 | struct_factor=item() |
---|
654 | struct_name = struct_factor.__class__.__name__ |
---|
655 | if hasattr(struct_factor, "name"): |
---|
656 | struct_name = struct_factor.name |
---|
657 | |
---|
658 | menuinfo[1].Append(int(id),struct_name,struct_name) |
---|
659 | if not item in self.struct_factor_dict.itervalues(): |
---|
660 | self.struct_factor_dict[str(id)]= item |
---|
661 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
662 | except: |
---|
663 | msg= "Error Occured: %s"%sys.exc_value |
---|
664 | wx.PostEvent(self.event_owner, StatusEvent(status=msg)) |
---|
665 | |
---|
666 | id = wx.NewId() |
---|
667 | self.modelmenu.AppendMenu(id, menuinfo[0],menuinfo[1],menuinfo[2]) |
---|
668 | |
---|
669 | def _fill_menu(self, menuinfo, list1, list2): |
---|
670 | """ |
---|
671 | Fill the menu with list item |
---|
672 | |
---|
673 | :param menuinfo: submenu item for the first column of this modelmenu |
---|
674 | with info.Should be a list : |
---|
675 | [name(string) , menu(wx.menu), help(string)] |
---|
676 | :param list1: contains item (form factor )to fill modelmenu second column |
---|
677 | :param list2: contains item (Structure factor )to fill modelmenu |
---|
678 | third column |
---|
679 | |
---|
680 | """ |
---|
681 | if len(list1)>0: |
---|
682 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
683 | |
---|
684 | for item in list1: |
---|
685 | form_factor= item() |
---|
686 | form_name = form_factor.__class__.__name__ |
---|
687 | if hasattr(form_factor, "name"): |
---|
688 | form_name = form_factor.name |
---|
689 | ### store form factor to return to other users |
---|
690 | newmenu= wx.Menu() |
---|
691 | if len(list2)>0: |
---|
692 | for model in list2: |
---|
693 | id = wx.NewId() |
---|
694 | struct_factor = model() |
---|
695 | name = struct_factor.__class__.__name__ |
---|
696 | if hasattr(struct_factor, "name"): |
---|
697 | name = struct_factor.name |
---|
698 | newmenu.Append(id,name, name) |
---|
699 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
700 | ## save form_fact and struct_fact |
---|
701 | self.form_factor_dict[int(id)] = [form_factor,struct_factor] |
---|
702 | |
---|
703 | form_id= wx.NewId() |
---|
704 | menuinfo[1].AppendMenu(int(form_id), form_name,newmenu,menuinfo[2]) |
---|
705 | id=wx.NewId() |
---|
706 | self.modelmenu.AppendMenu(id,menuinfo[0],menuinfo[1], menuinfo[2]) |
---|
707 | |
---|
708 | def _on_model(self, evt): |
---|
709 | """ |
---|
710 | React to a model menu event |
---|
711 | |
---|
712 | :param event: wx menu event |
---|
713 | |
---|
714 | """ |
---|
715 | if int(evt.GetId()) in self.form_factor_dict.keys(): |
---|
716 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
717 | model1, model2 = self.form_factor_dict[int(evt.GetId())] |
---|
718 | model = MultiplicationModel(model1, model2) |
---|
719 | else: |
---|
720 | model= self.struct_factor_dict[str(evt.GetId())]() |
---|
721 | evt = ModelEvent(model=model) |
---|
722 | wx.PostEvent(self.event_owner, evt) |
---|
723 | |
---|
724 | def _get_multifunc_models(self): |
---|
725 | """ |
---|
726 | Get the multifunctional models |
---|
727 | """ |
---|
728 | for item in self.plugins: |
---|
729 | try: |
---|
730 | # check the multiplicity if any |
---|
731 | if item.multiplicity_info[0] > 1: |
---|
732 | self.multi_func_list.append(item) |
---|
733 | except: |
---|
734 | # pass to other items |
---|
735 | pass |
---|
736 | |
---|
737 | def get_model_list(self): |
---|
738 | """ |
---|
739 | return dictionary of models for fitpanel use |
---|
740 | |
---|
741 | """ |
---|
742 | self.model_combobox.set_list("Shapes", self.shape_list) |
---|
743 | self.model_combobox.set_list("Shape-Independent", self.shape_indep_list) |
---|
744 | self.model_combobox.set_list("Structure Factors", self.struct_list) |
---|
745 | self.model_combobox.set_list("Customized Models", self.plugins) |
---|
746 | self.model_combobox.set_list("P(Q)*S(Q)", self.multiplication_factor) |
---|
747 | self.model_combobox.set_list("multiplication", self.multiplication_factor) |
---|
748 | self.model_combobox.set_list("Multi-Functions", self.multi_func_list) |
---|
749 | return self.model_combobox.get_list() |
---|
750 | |
---|
751 | def get_model_name_list(self): |
---|
752 | """ |
---|
753 | return regular model name list |
---|
754 | """ |
---|
755 | return self.model_name_list |
---|
756 | |
---|
757 | |
---|
758 | class ModelManager(object): |
---|
759 | """ |
---|
760 | implement model |
---|
761 | """ |
---|
762 | __modelmanager = ModelManagerBase() |
---|
763 | |
---|
764 | def findModels(self): |
---|
765 | return self.__modelmanager.findModels() |
---|
766 | |
---|
767 | def _getModelList(self): |
---|
768 | return self.__modelmanager._getModelList() |
---|
769 | |
---|
770 | def is_changed(self): |
---|
771 | return self.__modelmanager.is_changed() |
---|
772 | |
---|
773 | def update(self): |
---|
774 | return self.__modelmanager.update() |
---|
775 | |
---|
776 | def pulgins_reset(self): |
---|
777 | return self.__modelmanager.pulgins_reset() |
---|
778 | |
---|
779 | def populate_menu(self, modelmenu, event_owner): |
---|
780 | return self.__modelmanager.populate_menu(modelmenu, event_owner) |
---|
781 | |
---|
782 | def _on_model(self, evt): |
---|
783 | return self.__modelmanager._on_model(evt) |
---|
784 | |
---|
785 | def _get_multifunc_models(self): |
---|
786 | return self.__modelmanager._get_multifunc_models() |
---|
787 | |
---|
788 | def get_model_list(self): |
---|
789 | return self.__modelmanager.get_model_list() |
---|
790 | |
---|
791 | def get_model_name_list(self): |
---|
792 | return self.__modelmanager.get_model_name_list() |
---|
793 | |
---|
794 | |
---|
795 | |
---|