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