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 | |
---|
229 | self.stored_plugins = {} |
---|
230 | self._getModelList() |
---|
231 | |
---|
232 | |
---|
233 | def findModels(self): |
---|
234 | """ |
---|
235 | find plugin model in directory of plugin .recompile all file |
---|
236 | in the directory if file were modified |
---|
237 | """ |
---|
238 | temp = {} |
---|
239 | if self.is_changed(): |
---|
240 | return _findModels(dir) |
---|
241 | logging.info("pluging model : %s\n" % str(temp)) |
---|
242 | return temp |
---|
243 | |
---|
244 | def _getModelList(self): |
---|
245 | """ |
---|
246 | List of models we want to make available by default |
---|
247 | for this application |
---|
248 | |
---|
249 | :return: the next free event ID following the new menu events |
---|
250 | |
---|
251 | """ |
---|
252 | from sans.models.SphereModel import SphereModel |
---|
253 | self.shape_list.append(SphereModel) |
---|
254 | self.multiplication_factor.append(SphereModel) |
---|
255 | |
---|
256 | from sans.models.BinaryHSModel import BinaryHSModel |
---|
257 | self.shape_list.append(BinaryHSModel) |
---|
258 | |
---|
259 | from sans.models.FuzzySphereModel import FuzzySphereModel |
---|
260 | self.shape_list.append(FuzzySphereModel) |
---|
261 | self.multiplication_factor.append(FuzzySphereModel) |
---|
262 | |
---|
263 | from sans.models.CoreShellModel import CoreShellModel |
---|
264 | self.shape_list.append(CoreShellModel) |
---|
265 | self.multiplication_factor.append(CoreShellModel) |
---|
266 | |
---|
267 | from sans.models.CoreMultiShellModel import CoreMultiShellModel |
---|
268 | self.shape_list.append(CoreMultiShellModel) |
---|
269 | self.multiplication_factor.append(CoreMultiShellModel) |
---|
270 | self.multi_func_list.append(CoreMultiShellModel) |
---|
271 | |
---|
272 | from sans.models.VesicleModel import VesicleModel |
---|
273 | self.shape_list.append(VesicleModel) |
---|
274 | self.multiplication_factor.append(VesicleModel) |
---|
275 | |
---|
276 | from sans.models.MultiShellModel import MultiShellModel |
---|
277 | self.shape_list.append(MultiShellModel) |
---|
278 | self.multiplication_factor.append(MultiShellModel) |
---|
279 | |
---|
280 | from sans.models.OnionExpShellModel import OnionExpShellModel |
---|
281 | self.shape_list.append(OnionExpShellModel) |
---|
282 | self.multiplication_factor.append(OnionExpShellModel) |
---|
283 | self.multi_func_list.append(OnionExpShellModel) |
---|
284 | |
---|
285 | from sans.models.SphericalSLDModel import SphericalSLDModel |
---|
286 | self.shape_list.append(SphericalSLDModel) |
---|
287 | self.multiplication_factor.append(SphericalSLDModel) |
---|
288 | self.multi_func_list.append(SphericalSLDModel) |
---|
289 | |
---|
290 | from sans.models.PearlNecklaceModel import PearlNecklaceModel |
---|
291 | self.shape_list.append(PearlNecklaceModel) |
---|
292 | #self.multiplication_factor.append(PearlNecklaceModel) |
---|
293 | |
---|
294 | from sans.models.CylinderModel import CylinderModel |
---|
295 | self.shape_list.append(CylinderModel) |
---|
296 | self.multiplication_factor.append(CylinderModel) |
---|
297 | |
---|
298 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
299 | self.shape_list.append(CoreShellCylinderModel) |
---|
300 | self.multiplication_factor.append(CoreShellCylinderModel) |
---|
301 | |
---|
302 | from sans.models.HollowCylinderModel import HollowCylinderModel |
---|
303 | self.shape_list.append(HollowCylinderModel) |
---|
304 | self.multiplication_factor.append(HollowCylinderModel) |
---|
305 | |
---|
306 | from sans.models.FlexibleCylinderModel import FlexibleCylinderModel |
---|
307 | self.shape_list.append(FlexibleCylinderModel) |
---|
308 | |
---|
309 | from sans.models.FlexCylEllipXModel import FlexCylEllipXModel |
---|
310 | self.shape_list.append(FlexCylEllipXModel) |
---|
311 | |
---|
312 | from sans.models.StackedDisksModel import StackedDisksModel |
---|
313 | self.shape_list.append(StackedDisksModel) |
---|
314 | self.multiplication_factor.append(StackedDisksModel) |
---|
315 | |
---|
316 | from sans.models.ParallelepipedModel import ParallelepipedModel |
---|
317 | self.shape_list.append(ParallelepipedModel) |
---|
318 | self.multiplication_factor.append(ParallelepipedModel) |
---|
319 | |
---|
320 | from sans.models.CSParallelepipedModel import CSParallelepipedModel |
---|
321 | self.shape_list.append(CSParallelepipedModel) |
---|
322 | self.multiplication_factor.append(CSParallelepipedModel) |
---|
323 | |
---|
324 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
325 | self.shape_list.append(EllipticalCylinderModel) |
---|
326 | self.multiplication_factor.append(EllipticalCylinderModel) |
---|
327 | |
---|
328 | from sans.models.BarBellModel import BarBellModel |
---|
329 | self.shape_list.append(BarBellModel) |
---|
330 | # not implemeted yet! |
---|
331 | #self.multiplication_factor.append(BarBellModel) |
---|
332 | |
---|
333 | from sans.models.CappedCylinderModel import CappedCylinderModel |
---|
334 | self.shape_list.append(CappedCylinderModel) |
---|
335 | # not implemeted yet! |
---|
336 | #self.multiplication_factor.append(CappedCylinderModel) |
---|
337 | |
---|
338 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
339 | self.shape_list.append(EllipsoidModel) |
---|
340 | self.multiplication_factor.append(EllipsoidModel) |
---|
341 | |
---|
342 | from sans.models.CoreShellEllipsoidModel import CoreShellEllipsoidModel |
---|
343 | self.shape_list.append(CoreShellEllipsoidModel) |
---|
344 | self.multiplication_factor.append(CoreShellEllipsoidModel) |
---|
345 | |
---|
346 | from sans.models.TriaxialEllipsoidModel import TriaxialEllipsoidModel |
---|
347 | self.shape_list.append(TriaxialEllipsoidModel) |
---|
348 | self.multiplication_factor.append(TriaxialEllipsoidModel) |
---|
349 | |
---|
350 | from sans.models.LamellarModel import LamellarModel |
---|
351 | self.shape_list.append(LamellarModel) |
---|
352 | |
---|
353 | from sans.models.LamellarFFHGModel import LamellarFFHGModel |
---|
354 | self.shape_list.append(LamellarFFHGModel) |
---|
355 | |
---|
356 | from sans.models.LamellarPSModel import LamellarPSModel |
---|
357 | self.shape_list.append(LamellarPSModel) |
---|
358 | |
---|
359 | from sans.models.LamellarPSHGModel import LamellarPSHGModel |
---|
360 | self.shape_list.append(LamellarPSHGModel) |
---|
361 | |
---|
362 | from sans.models.LamellarPCrystalModel import LamellarPCrystalModel |
---|
363 | self.shape_list.append(LamellarPCrystalModel) |
---|
364 | |
---|
365 | from sans.models.SCCrystalModel import SCCrystalModel |
---|
366 | self.shape_list.append(SCCrystalModel) |
---|
367 | |
---|
368 | from sans.models.FCCrystalModel import FCCrystalModel |
---|
369 | self.shape_list.append(FCCrystalModel) |
---|
370 | |
---|
371 | from sans.models.BCCrystalModel import BCCrystalModel |
---|
372 | self.shape_list.append(BCCrystalModel) |
---|
373 | |
---|
374 | ## Structure factor |
---|
375 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
376 | self.struct_list.append(SquareWellStructure) |
---|
377 | |
---|
378 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
379 | self.struct_list.append(HardsphereStructure) |
---|
380 | |
---|
381 | from sans.models.StickyHSStructure import StickyHSStructure |
---|
382 | self.struct_list.append(StickyHSStructure) |
---|
383 | |
---|
384 | from sans.models.HayterMSAStructure import HayterMSAStructure |
---|
385 | self.struct_list.append(HayterMSAStructure) |
---|
386 | |
---|
387 | ##shape-independent models |
---|
388 | from sans.models.PowerLawAbsModel import PowerLawAbsModel |
---|
389 | self.shape_indep_list.append( PowerLawAbsModel ) |
---|
390 | |
---|
391 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
392 | self.shape_indep_list.append(BEPolyelectrolyte ) |
---|
393 | self.form_factor_dict[str(wx.NewId())] = [SphereModel] |
---|
394 | |
---|
395 | from sans.models.BroadPeakModel import BroadPeakModel |
---|
396 | self.shape_indep_list.append(BroadPeakModel) |
---|
397 | |
---|
398 | from sans.models.CorrLengthModel import CorrLengthModel |
---|
399 | self.shape_indep_list.append(CorrLengthModel) |
---|
400 | |
---|
401 | from sans.models.DABModel import DABModel |
---|
402 | self.shape_indep_list.append(DABModel ) |
---|
403 | |
---|
404 | from sans.models.DebyeModel import DebyeModel |
---|
405 | self.shape_indep_list.append(DebyeModel ) |
---|
406 | |
---|
407 | #FractalModel (a c-model)is now being used instead of FractalAbsModel. |
---|
408 | from sans.models.FractalModel import FractalModel |
---|
409 | self.shape_indep_list.append(FractalModel ) |
---|
410 | |
---|
411 | from sans.models.FractalCoreShellModel import FractalCoreShellModel |
---|
412 | self.shape_indep_list.append(FractalCoreShellModel ) |
---|
413 | |
---|
414 | from sans.models.GaussLorentzGelModel import GaussLorentzGelModel |
---|
415 | self.shape_indep_list.append(GaussLorentzGelModel) |
---|
416 | |
---|
417 | from sans.models.GuinierModel import GuinierModel |
---|
418 | self.shape_indep_list.append(GuinierModel ) |
---|
419 | |
---|
420 | from sans.models.GuinierPorodModel import GuinierPorodModel |
---|
421 | self.shape_indep_list.append(GuinierPorodModel ) |
---|
422 | |
---|
423 | from sans.models.LorentzModel import LorentzModel |
---|
424 | self.shape_indep_list.append( LorentzModel) |
---|
425 | |
---|
426 | from sans.models.PeakGaussModel import PeakGaussModel |
---|
427 | self.shape_indep_list.append(PeakGaussModel) |
---|
428 | |
---|
429 | from sans.models.PeakLorentzModel import PeakLorentzModel |
---|
430 | self.shape_indep_list.append(PeakLorentzModel) |
---|
431 | |
---|
432 | from sans.models.Poly_GaussCoil import Poly_GaussCoil |
---|
433 | self.shape_indep_list.append(Poly_GaussCoil) |
---|
434 | |
---|
435 | from sans.models.PolymerExclVolume import PolymerExclVolume |
---|
436 | self.shape_indep_list.append(PolymerExclVolume) |
---|
437 | |
---|
438 | from sans.models.PorodModel import PorodModel |
---|
439 | self.shape_indep_list.append(PorodModel ) |
---|
440 | |
---|
441 | from sans.models.RPA10Model import RPA10Model |
---|
442 | self.shape_indep_list.append(RPA10Model) |
---|
443 | self.multi_func_list.append(RPA10Model) |
---|
444 | |
---|
445 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
446 | self.shape_indep_list.append(TeubnerStreyModel ) |
---|
447 | |
---|
448 | from sans.models.TwoLorentzianModel import TwoLorentzianModel |
---|
449 | self.shape_indep_list.append(TwoLorentzianModel ) |
---|
450 | |
---|
451 | from sans.models.TwoPowerLawModel import TwoPowerLawModel |
---|
452 | self.shape_indep_list.append(TwoPowerLawModel ) |
---|
453 | |
---|
454 | from sans.models.UnifiedPowerRgModel import UnifiedPowerRgModel |
---|
455 | self.shape_indep_list.append(UnifiedPowerRgModel ) |
---|
456 | self.multi_func_list.append(UnifiedPowerRgModel) |
---|
457 | |
---|
458 | from sans.models.LineModel import LineModel |
---|
459 | self.shape_indep_list.append(LineModel) |
---|
460 | |
---|
461 | from sans.models.ReflectivityModel import ReflectivityModel |
---|
462 | self.multi_func_list.append(ReflectivityModel) |
---|
463 | |
---|
464 | from sans.models.ReflectivityIIModel import ReflectivityIIModel |
---|
465 | self.multi_func_list.append(ReflectivityIIModel) |
---|
466 | |
---|
467 | #Looking for plugins |
---|
468 | self.stored_plugins = self.findModels() |
---|
469 | self.plugins = self.stored_plugins.values() |
---|
470 | self.plugins.append(ReflectivityModel) |
---|
471 | self.plugins.append(ReflectivityIIModel) |
---|
472 | self._get_multifunc_models() |
---|
473 | |
---|
474 | return 0 |
---|
475 | |
---|
476 | def is_changed(self): |
---|
477 | """ |
---|
478 | check the last time the plugin dir has changed and return true |
---|
479 | is the directory was modified else return false |
---|
480 | """ |
---|
481 | is_modified = False |
---|
482 | plugin_dir = find_plugins_dir() |
---|
483 | if os.path.isdir(plugin_dir): |
---|
484 | temp = os.path.getmtime(plugin_dir) |
---|
485 | if self.last_time_dir_modified != temp: |
---|
486 | is_modified = True |
---|
487 | self.last_time_dir_modified = temp |
---|
488 | |
---|
489 | return is_modified |
---|
490 | |
---|
491 | def update(self): |
---|
492 | """ |
---|
493 | return a dictionary of model if |
---|
494 | new models were added else return empty dictionary |
---|
495 | """ |
---|
496 | new_plugins = self.findModels() |
---|
497 | if len(new_plugins) > 0: |
---|
498 | for name, plug in new_plugins.iteritems(): |
---|
499 | if name not in self.stored_plugins.keys(): |
---|
500 | self.stored_plugins[name] = plug |
---|
501 | self.plugins.append(plug) |
---|
502 | self.model_combobox.set_list("Customized Models", self.plugins) |
---|
503 | return self.model_combobox.get_list() |
---|
504 | else: |
---|
505 | return {} |
---|
506 | |
---|
507 | def pulgins_reset(self): |
---|
508 | """ |
---|
509 | return a dictionary of model |
---|
510 | """ |
---|
511 | self.plugins = [] |
---|
512 | new_plugins = _findModels(dir) |
---|
513 | for name, plug in new_plugins.iteritems(): |
---|
514 | for stored_name, stored_plug in self.stored_plugins.iteritems(): |
---|
515 | if name == stored_name: |
---|
516 | del self.stored_plugins[name] |
---|
517 | break |
---|
518 | self.stored_plugins[name] = plug |
---|
519 | self.plugins.append(plug) |
---|
520 | from sans.models.ReflectivityModel import ReflectivityModel |
---|
521 | from sans.models.ReflectivityIIModel import ReflectivityIIModel |
---|
522 | self.plugins.append(ReflectivityModel) |
---|
523 | self.plugins.append(ReflectivityIIModel) |
---|
524 | self.model_combobox.reset_list("Customized Models", self.plugins) |
---|
525 | return self.model_combobox.get_list() |
---|
526 | |
---|
527 | def populate_menu(self, modelmenu, event_owner): |
---|
528 | """ |
---|
529 | Populate a menu with our models |
---|
530 | |
---|
531 | :param id: first menu event ID to use when binding the menu events |
---|
532 | :param modelmenu: wx.Menu object to populate |
---|
533 | :param event_owner: wx object to bind the menu events to |
---|
534 | |
---|
535 | :return: the next free event ID following the new menu events |
---|
536 | |
---|
537 | """ |
---|
538 | ## Fill model lists |
---|
539 | self._getModelList() |
---|
540 | ## store reference to model menu of guiframe |
---|
541 | self.modelmenu = modelmenu |
---|
542 | ## guiframe reference |
---|
543 | self.event_owner = event_owner |
---|
544 | |
---|
545 | shape_submenu = wx.Menu() |
---|
546 | shape_indep_submenu = wx.Menu() |
---|
547 | structure_factor = wx.Menu() |
---|
548 | added_models = wx.Menu() |
---|
549 | multip_models = wx.Menu() |
---|
550 | ## create menu with shape |
---|
551 | self._fill_simple_menu(menuinfo=["Shapes",shape_submenu," simple shape"], |
---|
552 | list1=self.shape_list) |
---|
553 | |
---|
554 | self._fill_simple_menu(menuinfo=["Shape-Independent",shape_indep_submenu, |
---|
555 | "List of shape-independent models"], |
---|
556 | list1=self.shape_indep_list ) |
---|
557 | |
---|
558 | self._fill_simple_menu(menuinfo=["Structure Factors",structure_factor, |
---|
559 | "List of Structure factors models" ], |
---|
560 | list1=self.struct_list) |
---|
561 | |
---|
562 | self._fill_plugin_menu(menuinfo=["Customized Models", added_models, |
---|
563 | "List of additional models"], |
---|
564 | list1=self.plugins) |
---|
565 | |
---|
566 | self._fill_menu(menuinfo=["P(Q)*S(Q)",multip_models, |
---|
567 | "mulplication of 2 models"], |
---|
568 | list1=self.multiplication_factor , |
---|
569 | list2= self.struct_list) |
---|
570 | return 0 |
---|
571 | |
---|
572 | def _fill_plugin_menu(self, menuinfo, list1): |
---|
573 | """ |
---|
574 | fill the plugin menu with costumized models |
---|
575 | """ |
---|
576 | if len(list1)==0: |
---|
577 | id = wx.NewId() |
---|
578 | msg= "No model available check plugins.log for errors to fix problem" |
---|
579 | menuinfo[1].Append(int(id),"Empty",msg) |
---|
580 | self._fill_simple_menu( menuinfo,list1) |
---|
581 | |
---|
582 | def _fill_simple_menu(self, menuinfo, list1): |
---|
583 | """ |
---|
584 | Fill the menu with list item |
---|
585 | |
---|
586 | :param modelmenu: the menu to fill |
---|
587 | :param menuinfo: submenu item for the first column of this modelmenu |
---|
588 | with info.Should be a list : |
---|
589 | [name(string) , menu(wx.menu), help(string)] |
---|
590 | :param list1: contains item (form factor )to fill modelmenu second column |
---|
591 | |
---|
592 | """ |
---|
593 | if len(list1)>0: |
---|
594 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
595 | |
---|
596 | for item in list1: |
---|
597 | try: |
---|
598 | id = wx.NewId() |
---|
599 | struct_factor=item() |
---|
600 | struct_name = struct_factor.__class__.__name__ |
---|
601 | if hasattr(struct_factor, "name"): |
---|
602 | struct_name = struct_factor.name |
---|
603 | |
---|
604 | menuinfo[1].Append(int(id),struct_name,struct_name) |
---|
605 | if not item in self.struct_factor_dict.itervalues(): |
---|
606 | self.struct_factor_dict[str(id)]= item |
---|
607 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
608 | except: |
---|
609 | msg= "Error Occured: %s"%sys.exc_value |
---|
610 | wx.PostEvent(self.event_owner, StatusEvent(status=msg)) |
---|
611 | |
---|
612 | id = wx.NewId() |
---|
613 | self.modelmenu.AppendMenu(id, menuinfo[0],menuinfo[1],menuinfo[2]) |
---|
614 | |
---|
615 | def _fill_menu(self, menuinfo, list1, list2): |
---|
616 | """ |
---|
617 | Fill the menu with list item |
---|
618 | |
---|
619 | :param menuinfo: submenu item for the first column of this modelmenu |
---|
620 | with info.Should be a list : |
---|
621 | [name(string) , menu(wx.menu), help(string)] |
---|
622 | :param list1: contains item (form factor )to fill modelmenu second column |
---|
623 | :param list2: contains item (Structure factor )to fill modelmenu |
---|
624 | third column |
---|
625 | |
---|
626 | """ |
---|
627 | if len(list1)>0: |
---|
628 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
629 | |
---|
630 | for item in list1: |
---|
631 | form_factor= item() |
---|
632 | form_name = form_factor.__class__.__name__ |
---|
633 | if hasattr(form_factor, "name"): |
---|
634 | form_name = form_factor.name |
---|
635 | ### store form factor to return to other users |
---|
636 | newmenu= wx.Menu() |
---|
637 | if len(list2)>0: |
---|
638 | for model in list2: |
---|
639 | id = wx.NewId() |
---|
640 | struct_factor = model() |
---|
641 | name = struct_factor.__class__.__name__ |
---|
642 | if hasattr(struct_factor, "name"): |
---|
643 | name = struct_factor.name |
---|
644 | newmenu.Append(id,name, name) |
---|
645 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
646 | ## save form_fact and struct_fact |
---|
647 | self.form_factor_dict[int(id)] = [form_factor,struct_factor] |
---|
648 | |
---|
649 | form_id= wx.NewId() |
---|
650 | menuinfo[1].AppendMenu(int(form_id), form_name,newmenu,menuinfo[2]) |
---|
651 | id=wx.NewId() |
---|
652 | self.modelmenu.AppendMenu(id,menuinfo[0],menuinfo[1], menuinfo[2]) |
---|
653 | |
---|
654 | def _on_model(self, evt): |
---|
655 | """ |
---|
656 | React to a model menu event |
---|
657 | |
---|
658 | :param event: wx menu event |
---|
659 | |
---|
660 | """ |
---|
661 | if int(evt.GetId()) in self.form_factor_dict.keys(): |
---|
662 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
663 | model1, model2 = self.form_factor_dict[int(evt.GetId())] |
---|
664 | model = MultiplicationModel(model1, model2) |
---|
665 | else: |
---|
666 | model= self.struct_factor_dict[str(evt.GetId())]() |
---|
667 | evt = ModelEvent(model=model) |
---|
668 | wx.PostEvent(self.event_owner, evt) |
---|
669 | |
---|
670 | def _get_multifunc_models(self): |
---|
671 | """ |
---|
672 | Get the multifunctional models |
---|
673 | """ |
---|
674 | for item in self.plugins: |
---|
675 | try: |
---|
676 | # check the multiplicity if any |
---|
677 | if item.multiplicity_info[0] > 1: |
---|
678 | self.multi_func_list.append(item) |
---|
679 | except: |
---|
680 | # pass to other items |
---|
681 | pass |
---|
682 | |
---|
683 | def get_model_list(self): |
---|
684 | """ |
---|
685 | return dictionary of models for fitpanel use |
---|
686 | |
---|
687 | """ |
---|
688 | self.model_combobox.set_list("Shapes", self.shape_list) |
---|
689 | self.model_combobox.set_list("Shape-Independent", self.shape_indep_list) |
---|
690 | self.model_combobox.set_list("Structure Factors", self.struct_list) |
---|
691 | self.model_combobox.set_list("Customized Models", self.plugins) |
---|
692 | self.model_combobox.set_list("P(Q)*S(Q)", self.multiplication_factor) |
---|
693 | self.model_combobox.set_list("multiplication", self.multiplication_factor) |
---|
694 | self.model_combobox.set_list("Multi-Functions", self.multi_func_list) |
---|
695 | return self.model_combobox.get_list() |
---|
696 | |
---|
697 | |
---|
698 | |
---|
699 | class ModelManager(object): |
---|
700 | """ |
---|
701 | implement model |
---|
702 | """ |
---|
703 | __modelmanager = ModelManagerBase() |
---|
704 | |
---|
705 | def findModels(self): |
---|
706 | return self.__modelmanager.findModels() |
---|
707 | |
---|
708 | def _getModelList(self): |
---|
709 | return self.__modelmanager._getModelList() |
---|
710 | |
---|
711 | def is_changed(self): |
---|
712 | return self.__modelmanager.is_changed() |
---|
713 | |
---|
714 | def update(self): |
---|
715 | return self.__modelmanager.update() |
---|
716 | |
---|
717 | def pulgins_reset(self): |
---|
718 | return self.__modelmanager.pulgins_reset() |
---|
719 | |
---|
720 | def populate_menu(self, modelmenu, event_owner): |
---|
721 | return self.__modelmanager.populate_menu(modelmenu, event_owner) |
---|
722 | |
---|
723 | def _on_model(self, evt): |
---|
724 | return self.__modelmanager._on_model(evt) |
---|
725 | |
---|
726 | def _get_multifunc_models(self): |
---|
727 | return self.__modelmanager._get_multifunc_models() |
---|
728 | |
---|
729 | def get_model_list(self): |
---|
730 | return self.__modelmanager.get_model_list() |
---|
731 | |
---|
732 | |
---|
733 | |
---|