[0277d084] | 1 | |
---|
[74755ff] | 2 | """ |
---|
| 3 | This module collects models from sans.models packages and orders theses models |
---|
| 4 | by categories that GUI application understands and uses. |
---|
[0277d084] | 5 | |
---|
[74755ff] | 6 | """ |
---|
[0277d084] | 7 | import wx |
---|
| 8 | import wx.lib.newevent |
---|
| 9 | import imp |
---|
| 10 | import os,sys,math |
---|
| 11 | import os.path |
---|
| 12 | |
---|
| 13 | (ModelEvent, EVT_MODEL) = wx.lib.newevent.NewEvent() |
---|
| 14 | from sans.guicomm.events import StatusEvent |
---|
| 15 | # Time is needed by the log method |
---|
| 16 | import time |
---|
| 17 | |
---|
| 18 | # Explicitly import from the pluginmodel module so that py2exe |
---|
| 19 | # places it in the distribution. The Model1DPlugin class is used |
---|
| 20 | # as the base class of plug-in models. |
---|
| 21 | from sans.models.pluginmodel import Model1DPlugin |
---|
| 22 | |
---|
| 23 | def log(message): |
---|
[74755ff] | 24 | """ |
---|
| 25 | """ |
---|
[0277d084] | 26 | out = open("plugins.log", 'a') |
---|
| 27 | out.write("%10g: %s\n" % (time.clock(), message)) |
---|
| 28 | out.close() |
---|
| 29 | |
---|
| 30 | def findModels(): |
---|
[74755ff] | 31 | """ |
---|
| 32 | """ |
---|
[0277d084] | 33 | log("looking for models in: %s/plugins" % os.getcwd()) |
---|
| 34 | if os.path.isdir('plugins'): |
---|
| 35 | return _findModels('plugins') |
---|
| 36 | return [] |
---|
| 37 | |
---|
| 38 | def _check_plugin(model, name): |
---|
| 39 | """ |
---|
[74755ff] | 40 | Do some checking before model adding plugins in the list |
---|
| 41 | |
---|
| 42 | :param model: class model to add into the plugin list |
---|
| 43 | :param name: name of the module plugin |
---|
| 44 | |
---|
| 45 | :return model: model if valid model or None if not valid |
---|
| 46 | |
---|
[0277d084] | 47 | """ |
---|
| 48 | #Check is the plugin is of type Model1DPlugin |
---|
| 49 | if not issubclass(model, Model1DPlugin): |
---|
| 50 | msg= "Plugin %s must be of type Model1DPlugin \n"%str(name) |
---|
| 51 | log(msg) |
---|
| 52 | return None |
---|
| 53 | if model.__name__!="Model": |
---|
| 54 | msg= "Plugin %s class name must be Model \n"%str(name) |
---|
| 55 | log(msg) |
---|
| 56 | return None |
---|
| 57 | try: |
---|
| 58 | new_instance= model() |
---|
| 59 | except: |
---|
| 60 | msg="Plugin %s error in __init__ \n\t: %s %s\n"%(str(name), |
---|
| 61 | str(sys.exc_type),sys.exc_value) |
---|
| 62 | log(msg) |
---|
| 63 | return None |
---|
| 64 | |
---|
| 65 | new_instance= model() |
---|
| 66 | if hasattr(new_instance,"function"): |
---|
| 67 | try: |
---|
| 68 | value=new_instance.function() |
---|
| 69 | except: |
---|
| 70 | msg="Plugin %s: error writing function \n\t :%s %s\n "%(str(name), |
---|
| 71 | str(sys.exc_type),sys.exc_value) |
---|
| 72 | log(msg) |
---|
| 73 | return None |
---|
| 74 | else: |
---|
| 75 | msg="Plugin %s needs a method called function \n"%str(name) |
---|
| 76 | log(msg) |
---|
| 77 | return None |
---|
| 78 | return model |
---|
| 79 | |
---|
| 80 | |
---|
| 81 | def _findModels(dir): |
---|
[74755ff] | 82 | """ |
---|
| 83 | """ |
---|
[0277d084] | 84 | # List of plugin objects |
---|
| 85 | plugins = [] |
---|
| 86 | # Go through files in plug-in directory |
---|
| 87 | try: |
---|
| 88 | list = os.listdir(dir) |
---|
| 89 | for item in list: |
---|
| 90 | toks = os.path.splitext(os.path.basename(item)) |
---|
| 91 | if toks[1]=='.py' and not toks[0]=='__init__': |
---|
| 92 | name = toks[0] |
---|
| 93 | |
---|
| 94 | path = [os.path.abspath(dir)] |
---|
| 95 | file = None |
---|
| 96 | try: |
---|
| 97 | (file, path, info) = imp.find_module(name, path) |
---|
| 98 | module = imp.load_module( name, file, item, info ) |
---|
| 99 | if hasattr(module, "Model"): |
---|
| 100 | try: |
---|
| 101 | if _check_plugin(module.Model, name)!=None: |
---|
| 102 | plugins.append(module.Model) |
---|
| 103 | except: |
---|
| 104 | msg="Error accessing Model" |
---|
| 105 | msg+="in %s\n %s %s\n" % (name, |
---|
| 106 | str(sys.exc_type), sys.exc_value) |
---|
| 107 | log(msg) |
---|
| 108 | except: |
---|
| 109 | msg="Error accessing Model" |
---|
| 110 | msg +=" in %s\n %s %s \n" %(name, |
---|
| 111 | str(sys.exc_type), sys.exc_value) |
---|
| 112 | log(msg) |
---|
| 113 | finally: |
---|
| 114 | |
---|
| 115 | if not file==None: |
---|
| 116 | file.close() |
---|
| 117 | except: |
---|
| 118 | # Don't deal with bad plug-in imports. Just skip. |
---|
| 119 | pass |
---|
| 120 | return plugins |
---|
| 121 | |
---|
| 122 | class ModelList(object): |
---|
| 123 | """ |
---|
[74755ff] | 124 | Contains dictionary of model and their type |
---|
| 125 | |
---|
[0277d084] | 126 | """ |
---|
| 127 | def __init__(self): |
---|
| 128 | self.mydict={} |
---|
| 129 | |
---|
| 130 | def set_list(self, name, mylist): |
---|
| 131 | """ |
---|
[74755ff] | 132 | |
---|
| 133 | :param name: the type of the list |
---|
| 134 | :param mylist: the list to add |
---|
| 135 | |
---|
[0277d084] | 136 | """ |
---|
| 137 | if name not in self.mydict.keys(): |
---|
| 138 | self.mydict[name] = mylist |
---|
| 139 | |
---|
| 140 | |
---|
| 141 | def get_list(self): |
---|
| 142 | """ |
---|
[74755ff] | 143 | return all the list stored in a dictionary object |
---|
[0277d084] | 144 | """ |
---|
| 145 | return self.mydict |
---|
| 146 | |
---|
| 147 | class ModelManager: |
---|
[74755ff] | 148 | """ |
---|
| 149 | ModelManager collected model classes object of available into a |
---|
| 150 | dictionary of models' names and models' classes. |
---|
| 151 | |
---|
| 152 | """ |
---|
[0277d084] | 153 | ## external dict for models |
---|
| 154 | model_combobox = ModelList() |
---|
| 155 | ## Dictionary of form models |
---|
| 156 | form_factor_dict = {} |
---|
| 157 | ## dictionary of other |
---|
| 158 | struct_factor_dict = {} |
---|
| 159 | ##list of form factors |
---|
| 160 | shape_list =[] |
---|
| 161 | ## independent shape model list |
---|
| 162 | shape_indep_list = [] |
---|
| 163 | ##list of structure factors |
---|
| 164 | struct_list= [] |
---|
| 165 | ##list of model allowing multiplication |
---|
| 166 | multiplication_factor=[] |
---|
[e87f9fc] | 167 | ##list of multifunctional shapes |
---|
| 168 | multi_func_list =[] |
---|
[0277d084] | 169 | ## list of added models |
---|
| 170 | plugins=[] |
---|
| 171 | ## Event owner (guiframe) |
---|
| 172 | event_owner = None |
---|
| 173 | |
---|
| 174 | def _getModelList(self): |
---|
| 175 | """ |
---|
[74755ff] | 176 | Fill up lists of models available by default |
---|
| 177 | for the current application |
---|
| 178 | |
---|
| 179 | :return: the next free event ID following the new menu events |
---|
[0277d084] | 180 | |
---|
| 181 | """ |
---|
| 182 | ## form factor |
---|
| 183 | from sans.models.SphereModel import SphereModel |
---|
| 184 | self.shape_list.append(SphereModel) |
---|
| 185 | self.multiplication_factor.append(SphereModel) |
---|
[1a395a6] | 186 | |
---|
| 187 | from sans.models.BinaryHSModel import BinaryHSModel |
---|
| 188 | self.shape_list.append(BinaryHSModel) |
---|
[e87f9fc] | 189 | |
---|
[ce07fa8] | 190 | from sans.models.FuzzySphereModel import FuzzySphereModel |
---|
| 191 | self.shape_list.append(FuzzySphereModel) |
---|
| 192 | self.multiplication_factor.append(FuzzySphereModel) |
---|
| 193 | |
---|
[0277d084] | 194 | from sans.models.CoreShellModel import CoreShellModel |
---|
| 195 | self.shape_list.append(CoreShellModel) |
---|
| 196 | self.multiplication_factor.append(CoreShellModel) |
---|
| 197 | |
---|
[4523b68] | 198 | from sans.models.CoreMultiShellModel import CoreMultiShellModel |
---|
| 199 | self.shape_list.append(CoreMultiShellModel) |
---|
| 200 | self.multiplication_factor.append(CoreMultiShellModel) |
---|
[e87f9fc] | 201 | self.multi_func_list.append(CoreMultiShellModel) |
---|
[4523b68] | 202 | |
---|
[0277d084] | 203 | from sans.models.VesicleModel import VesicleModel |
---|
| 204 | self.shape_list.append(VesicleModel) |
---|
| 205 | self.multiplication_factor.append(VesicleModel) |
---|
| 206 | |
---|
| 207 | from sans.models.MultiShellModel import MultiShellModel |
---|
| 208 | self.shape_list.append(MultiShellModel) |
---|
| 209 | self.multiplication_factor.append(MultiShellModel) |
---|
| 210 | |
---|
[1a395a6] | 211 | from sans.models.OnionExpShellModel import OnionExpShellModel |
---|
| 212 | self.shape_list.append(OnionExpShellModel) |
---|
| 213 | self.multiplication_factor.append(OnionExpShellModel) |
---|
| 214 | self.multi_func_list.append(OnionExpShellModel) |
---|
| 215 | |
---|
| 216 | from sans.models.SphericalSLDModel import SphericalSLDModel |
---|
| 217 | self.shape_list.append(SphericalSLDModel) |
---|
| 218 | self.multiplication_factor.append(SphericalSLDModel) |
---|
| 219 | self.multi_func_list.append(SphericalSLDModel) |
---|
| 220 | |
---|
[0277d084] | 221 | |
---|
| 222 | from sans.models.CylinderModel import CylinderModel |
---|
| 223 | self.shape_list.append(CylinderModel) |
---|
| 224 | self.multiplication_factor.append(CylinderModel) |
---|
| 225 | |
---|
| 226 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
| 227 | self.shape_list.append(CoreShellCylinderModel) |
---|
| 228 | self.multiplication_factor.append(CoreShellCylinderModel) |
---|
| 229 | |
---|
| 230 | from sans.models.HollowCylinderModel import HollowCylinderModel |
---|
| 231 | self.shape_list.append(HollowCylinderModel) |
---|
| 232 | self.multiplication_factor.append(HollowCylinderModel) |
---|
| 233 | |
---|
| 234 | from sans.models.FlexibleCylinderModel import FlexibleCylinderModel |
---|
| 235 | self.shape_list.append(FlexibleCylinderModel) |
---|
| 236 | |
---|
[ce07fa8] | 237 | from sans.models.FlexCylEllipXModel import FlexCylEllipXModel |
---|
| 238 | self.shape_list.append(FlexCylEllipXModel) |
---|
[0277d084] | 239 | |
---|
| 240 | from sans.models.StackedDisksModel import StackedDisksModel |
---|
| 241 | self.shape_list.append(StackedDisksModel) |
---|
| 242 | self.multiplication_factor.append(StackedDisksModel) |
---|
| 243 | |
---|
| 244 | from sans.models.ParallelepipedModel import ParallelepipedModel |
---|
| 245 | self.shape_list.append(ParallelepipedModel) |
---|
| 246 | self.multiplication_factor.append(ParallelepipedModel) |
---|
| 247 | |
---|
[fb59ed9] | 248 | from sans.models.CSParallelepipedModel import CSParallelepipedModel |
---|
| 249 | self.shape_list.append(CSParallelepipedModel) |
---|
| 250 | self.multiplication_factor.append(CSParallelepipedModel) |
---|
| 251 | |
---|
[0277d084] | 252 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
| 253 | self.shape_list.append(EllipticalCylinderModel) |
---|
| 254 | self.multiplication_factor.append(EllipticalCylinderModel) |
---|
[fb59ed9] | 255 | |
---|
| 256 | from sans.models.BarBellModel import BarBellModel |
---|
| 257 | self.shape_list.append(BarBellModel) |
---|
| 258 | # not implemeted yet! |
---|
| 259 | #self.multiplication_factor.append(BarBellModel) |
---|
| 260 | |
---|
| 261 | from sans.models.CappedCylinderModel import CappedCylinderModel |
---|
| 262 | self.shape_list.append(CappedCylinderModel) |
---|
| 263 | # not implemeted yet! |
---|
| 264 | #self.multiplication_factor.append(CappedCylinderModel) |
---|
| 265 | |
---|
[0277d084] | 266 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
| 267 | self.shape_list.append(EllipsoidModel) |
---|
| 268 | self.multiplication_factor.append(EllipsoidModel) |
---|
| 269 | |
---|
| 270 | from sans.models.CoreShellEllipsoidModel import CoreShellEllipsoidModel |
---|
| 271 | self.shape_list.append(CoreShellEllipsoidModel) |
---|
| 272 | self.multiplication_factor.append(CoreShellEllipsoidModel) |
---|
| 273 | |
---|
| 274 | from sans.models.TriaxialEllipsoidModel import TriaxialEllipsoidModel |
---|
| 275 | self.shape_list.append(TriaxialEllipsoidModel) |
---|
| 276 | self.multiplication_factor.append(TriaxialEllipsoidModel) |
---|
| 277 | |
---|
| 278 | from sans.models.LamellarModel import LamellarModel |
---|
| 279 | self.shape_list.append(LamellarModel) |
---|
| 280 | |
---|
| 281 | from sans.models.LamellarFFHGModel import LamellarFFHGModel |
---|
| 282 | self.shape_list.append(LamellarFFHGModel) |
---|
| 283 | |
---|
| 284 | from sans.models.LamellarPSModel import LamellarPSModel |
---|
| 285 | self.shape_list.append(LamellarPSModel) |
---|
| 286 | |
---|
| 287 | from sans.models.LamellarPSHGModel import LamellarPSHGModel |
---|
| 288 | self.shape_list.append(LamellarPSHGModel) |
---|
[fb59ed9] | 289 | |
---|
| 290 | from sans.models.LamellarPCrystalModel import LamellarPCrystalModel |
---|
| 291 | self.shape_list.append(LamellarPCrystalModel) |
---|
| 292 | |
---|
| 293 | from sans.models.SCCrystalModel import SCCrystalModel |
---|
| 294 | self.shape_list.append(SCCrystalModel) |
---|
| 295 | |
---|
| 296 | from sans.models.FCCrystalModel import FCCrystalModel |
---|
| 297 | self.shape_list.append(FCCrystalModel) |
---|
| 298 | |
---|
| 299 | from sans.models.BCCrystalModel import BCCrystalModel |
---|
| 300 | self.shape_list.append(BCCrystalModel) |
---|
[0277d084] | 301 | |
---|
| 302 | ## Structure factor |
---|
| 303 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
| 304 | self.struct_list.append(SquareWellStructure) |
---|
| 305 | |
---|
| 306 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
| 307 | self.struct_list.append(HardsphereStructure) |
---|
| 308 | |
---|
| 309 | from sans.models.StickyHSStructure import StickyHSStructure |
---|
| 310 | self.struct_list.append(StickyHSStructure) |
---|
| 311 | |
---|
| 312 | from sans.models.HayterMSAStructure import HayterMSAStructure |
---|
| 313 | self.struct_list.append(HayterMSAStructure) |
---|
| 314 | |
---|
| 315 | |
---|
| 316 | ##shape-independent models |
---|
[ce07fa8] | 317 | |
---|
| 318 | from sans.models.PowerLawAbsModel import PowerLawAbsModel |
---|
| 319 | self.shape_indep_list.append( PowerLawAbsModel ) |
---|
| 320 | |
---|
[0277d084] | 321 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
| 322 | self.shape_indep_list.append(BEPolyelectrolyte ) |
---|
| 323 | self.form_factor_dict[str(wx.NewId())] = [SphereModel] |
---|
[fb59ed9] | 324 | |
---|
| 325 | from sans.models.BroadPeakModel import BroadPeakModel |
---|
| 326 | self.shape_indep_list.append(BroadPeakModel) |
---|
| 327 | |
---|
| 328 | from sans.models.CorrLengthModel import CorrLengthModel |
---|
| 329 | self.shape_indep_list.append(CorrLengthModel) |
---|
| 330 | |
---|
[0277d084] | 331 | from sans.models.DABModel import DABModel |
---|
| 332 | self.shape_indep_list.append(DABModel ) |
---|
| 333 | |
---|
[ce07fa8] | 334 | from sans.models.DebyeModel import DebyeModel |
---|
| 335 | self.shape_indep_list.append(DebyeModel ) |
---|
| 336 | |
---|
[fb59ed9] | 337 | #FractalModel (a c-model)is now being used instead of FractalAbsModel. |
---|
[ce07fa8] | 338 | from sans.models.FractalModel import FractalModel |
---|
| 339 | self.shape_indep_list.append(FractalModel ) |
---|
[0277d084] | 340 | |
---|
[fb59ed9] | 341 | from sans.models.FractalCoreShellModel import FractalCoreShellModel |
---|
| 342 | self.shape_indep_list.append(FractalCoreShellModel ) |
---|
| 343 | |
---|
| 344 | from sans.models.GaussLorentzGelModel import GaussLorentzGelModel |
---|
| 345 | self.shape_indep_list.append(GaussLorentzGelModel) |
---|
| 346 | |
---|
| 347 | from sans.models.GuinierModel import GuinierModel |
---|
| 348 | self.shape_indep_list.append(GuinierModel ) |
---|
| 349 | |
---|
| 350 | from sans.models.GuinierPorodModel import GuinierPorodModel |
---|
| 351 | self.shape_indep_list.append(GuinierPorodModel ) |
---|
| 352 | |
---|
[ce07fa8] | 353 | from sans.models.LorentzModel import LorentzModel |
---|
| 354 | self.shape_indep_list.append( LorentzModel) |
---|
[0277d084] | 355 | |
---|
| 356 | from sans.models.PeakGaussModel import PeakGaussModel |
---|
| 357 | self.shape_indep_list.append(PeakGaussModel) |
---|
| 358 | |
---|
| 359 | from sans.models.PeakLorentzModel import PeakLorentzModel |
---|
| 360 | self.shape_indep_list.append(PeakLorentzModel) |
---|
| 361 | |
---|
[ce07fa8] | 362 | from sans.models.Poly_GaussCoil import Poly_GaussCoil |
---|
| 363 | self.shape_indep_list.append(Poly_GaussCoil) |
---|
[fb59ed9] | 364 | |
---|
| 365 | from sans.models.PolymerExclVolume import PolymerExclVolume |
---|
| 366 | self.shape_indep_list.append(PolymerExclVolume) |
---|
| 367 | |
---|
[ce07fa8] | 368 | from sans.models.PorodModel import PorodModel |
---|
[fb59ed9] | 369 | self.shape_indep_list.append(PorodModel ) |
---|
[0277d084] | 370 | |
---|
[fb59ed9] | 371 | from sans.models.RPA10Model import RPA10Model |
---|
| 372 | self.shape_indep_list.append(RPA10Model) |
---|
| 373 | self.multi_func_list.append(RPA10Model) |
---|
[0277d084] | 374 | |
---|
| 375 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
| 376 | self.shape_indep_list.append(TeubnerStreyModel ) |
---|
| 377 | |
---|
[fb59ed9] | 378 | from sans.models.TwoLorentzianModel import TwoLorentzianModel |
---|
| 379 | self.shape_indep_list.append(TwoLorentzianModel ) |
---|
| 380 | |
---|
| 381 | from sans.models.TwoPowerLawModel import TwoPowerLawModel |
---|
| 382 | self.shape_indep_list.append(TwoPowerLawModel ) |
---|
| 383 | |
---|
| 384 | from sans.models.UnifiedPowerRgModel import UnifiedPowerRgModel |
---|
| 385 | self.shape_indep_list.append(UnifiedPowerRgModel ) |
---|
| 386 | self.multi_func_list.append(UnifiedPowerRgModel) |
---|
| 387 | |
---|
[0277d084] | 388 | from sans.models.LineModel import LineModel |
---|
| 389 | self.shape_indep_list.append(LineModel) |
---|
[fb59ed9] | 390 | |
---|
| 391 | from sans.models.ReflectivityModel import ReflectivityModel |
---|
| 392 | self.multi_func_list.append(ReflectivityModel) |
---|
[1a395a6] | 393 | |
---|
[1cc23fd] | 394 | from sans.models.ReflectivityIIModel import ReflectivityIIModel |
---|
| 395 | self.multi_func_list.append(ReflectivityIIModel) |
---|
| 396 | |
---|
[0277d084] | 397 | #Looking for plugins |
---|
| 398 | self.plugins = findModels() |
---|
[fb59ed9] | 399 | self._get_multifunc_models() |
---|
| 400 | self.plugins.append(ReflectivityModel) |
---|
[1cc23fd] | 401 | self.plugins.append(ReflectivityIIModel) |
---|
[0277d084] | 402 | return 0 |
---|
| 403 | |
---|
| 404 | def get_model_list(self): |
---|
[74755ff] | 405 | """ |
---|
| 406 | :return: dictionary of models for fitpanel use |
---|
| 407 | |
---|
| 408 | """ |
---|
[0277d084] | 409 | self._getModelList() |
---|
| 410 | self.model_combobox.set_list("Shapes", self.shape_list) |
---|
| 411 | self.model_combobox.set_list("Shape-Independent", self.shape_indep_list) |
---|
| 412 | self.model_combobox.set_list("Structure Factors", self.struct_list) |
---|
| 413 | self.model_combobox.set_list("Customized Models", self.plugins) |
---|
| 414 | self.model_combobox.set_list("P(Q)*S(Q)", self.multiplication_factor) |
---|
| 415 | self.model_combobox.set_list("multiplication", self.multiplication_factor) |
---|
[e87f9fc] | 416 | self.model_combobox.set_list("Multi-Functions", self.multi_func_list) |
---|
[0277d084] | 417 | return self.model_combobox |
---|
[fb59ed9] | 418 | |
---|
| 419 | def _get_multifunc_models(self): |
---|
| 420 | """ |
---|
| 421 | Get the multifunctional models |
---|
| 422 | """ |
---|
| 423 | for item in self.plugins: |
---|
| 424 | try: |
---|
| 425 | # check the multiplicity if any |
---|
| 426 | if item.multiplicity_info[0] > 1: |
---|
| 427 | self.multi_func_list.append(item) |
---|
| 428 | except: |
---|
| 429 | # pass to other items |
---|
| 430 | pass |
---|