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