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