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