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