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