[0277d084] | 1 | #TODO: add comments to document this module |
---|
| 2 | #TODO: clean-up the exception handling. |
---|
| 3 | |
---|
| 4 | #TODO: clean-up the FractalAbsModel and PowerLawAbsModel menu items. Those |
---|
| 5 | # model definitions do not belong here. They belong with the rest of the |
---|
| 6 | # models. |
---|
| 7 | |
---|
| 8 | import wx |
---|
| 9 | import wx.lib.newevent |
---|
| 10 | import imp |
---|
| 11 | import os,sys,math |
---|
| 12 | import os.path |
---|
| 13 | |
---|
| 14 | (ModelEvent, EVT_MODEL) = wx.lib.newevent.NewEvent() |
---|
| 15 | from sans.guicomm.events import StatusEvent |
---|
| 16 | # Time is needed by the log method |
---|
| 17 | import time |
---|
| 18 | |
---|
| 19 | # Explicitly import from the pluginmodel module so that py2exe |
---|
| 20 | # places it in the distribution. The Model1DPlugin class is used |
---|
| 21 | # as the base class of plug-in models. |
---|
| 22 | from sans.models.pluginmodel import Model1DPlugin |
---|
| 23 | |
---|
| 24 | def log(message): |
---|
| 25 | out = open("plugins.log", 'a') |
---|
| 26 | out.write("%10g: %s\n" % (time.clock(), message)) |
---|
| 27 | out.close() |
---|
| 28 | |
---|
| 29 | def findModels(): |
---|
| 30 | log("looking for models in: %s/plugins" % os.getcwd()) |
---|
| 31 | if os.path.isdir('plugins'): |
---|
| 32 | return _findModels('plugins') |
---|
| 33 | return [] |
---|
| 34 | |
---|
| 35 | def _check_plugin(model, name): |
---|
| 36 | """ |
---|
| 37 | Do some checking before model adding plugins in the list |
---|
| 38 | @param model: class model to add into the plugin list |
---|
| 39 | @param name:name of the module plugin |
---|
| 40 | @return model: model if valid model or None if not valid |
---|
| 41 | """ |
---|
| 42 | #Check is the plugin is of type Model1DPlugin |
---|
| 43 | if not issubclass(model, Model1DPlugin): |
---|
| 44 | msg= "Plugin %s must be of type Model1DPlugin \n"%str(name) |
---|
| 45 | log(msg) |
---|
| 46 | return None |
---|
| 47 | if model.__name__!="Model": |
---|
| 48 | msg= "Plugin %s class name must be Model \n"%str(name) |
---|
| 49 | log(msg) |
---|
| 50 | return None |
---|
| 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) |
---|
| 57 | return None |
---|
| 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) |
---|
| 67 | return None |
---|
| 68 | else: |
---|
| 69 | msg="Plugin %s needs a method called function \n"%str(name) |
---|
| 70 | log(msg) |
---|
| 71 | return None |
---|
| 72 | return model |
---|
| 73 | |
---|
| 74 | |
---|
| 75 | def _findModels(dir): |
---|
| 76 | # List of plugin objects |
---|
| 77 | plugins = [] |
---|
| 78 | # Go through files in plug-in directory |
---|
| 79 | try: |
---|
| 80 | list = os.listdir(dir) |
---|
| 81 | for item in list: |
---|
| 82 | toks = os.path.splitext(os.path.basename(item)) |
---|
| 83 | if toks[1]=='.py' and not toks[0]=='__init__': |
---|
| 84 | name = toks[0] |
---|
| 85 | |
---|
| 86 | path = [os.path.abspath(dir)] |
---|
| 87 | file = None |
---|
| 88 | try: |
---|
| 89 | (file, path, info) = imp.find_module(name, path) |
---|
| 90 | module = imp.load_module( name, file, item, info ) |
---|
| 91 | if hasattr(module, "Model"): |
---|
| 92 | try: |
---|
| 93 | if _check_plugin(module.Model, name)!=None: |
---|
| 94 | plugins.append(module.Model) |
---|
| 95 | except: |
---|
| 96 | msg="Error accessing Model" |
---|
| 97 | msg+="in %s\n %s %s\n" % (name, |
---|
| 98 | str(sys.exc_type), sys.exc_value) |
---|
| 99 | log(msg) |
---|
| 100 | except: |
---|
| 101 | msg="Error accessing Model" |
---|
| 102 | msg +=" in %s\n %s %s \n" %(name, |
---|
| 103 | str(sys.exc_type), sys.exc_value) |
---|
| 104 | log(msg) |
---|
| 105 | finally: |
---|
| 106 | |
---|
| 107 | if not file==None: |
---|
| 108 | file.close() |
---|
| 109 | except: |
---|
| 110 | # Don't deal with bad plug-in imports. Just skip. |
---|
| 111 | pass |
---|
| 112 | return plugins |
---|
| 113 | |
---|
| 114 | class ModelList(object): |
---|
| 115 | """ |
---|
| 116 | Contains dictionary of model and their type |
---|
| 117 | """ |
---|
| 118 | def __init__(self): |
---|
| 119 | self.mydict={} |
---|
| 120 | |
---|
| 121 | def set_list(self, name, mylist): |
---|
| 122 | """ |
---|
| 123 | @param name: the type of the list |
---|
| 124 | @param mylist: the list to add |
---|
| 125 | """ |
---|
| 126 | if name not in self.mydict.keys(): |
---|
| 127 | self.mydict[name] = mylist |
---|
| 128 | |
---|
| 129 | |
---|
| 130 | def get_list(self): |
---|
| 131 | """ |
---|
| 132 | return all the list stored in a dictionary object |
---|
| 133 | """ |
---|
| 134 | return self.mydict |
---|
| 135 | |
---|
| 136 | class ModelManager: |
---|
| 137 | ## external dict for models |
---|
| 138 | model_combobox = ModelList() |
---|
| 139 | ## Dictionary of form models |
---|
| 140 | form_factor_dict = {} |
---|
| 141 | ## dictionary of other |
---|
| 142 | struct_factor_dict = {} |
---|
| 143 | ##list of form factors |
---|
| 144 | shape_list =[] |
---|
| 145 | ## independent shape model list |
---|
| 146 | shape_indep_list = [] |
---|
| 147 | ##list of structure factors |
---|
| 148 | struct_list= [] |
---|
| 149 | ##list of model allowing multiplication |
---|
| 150 | multiplication_factor=[] |
---|
| 151 | ## list of added models |
---|
| 152 | plugins=[] |
---|
| 153 | ## Event owner (guiframe) |
---|
| 154 | event_owner = None |
---|
| 155 | |
---|
| 156 | def _getModelList(self): |
---|
| 157 | """ |
---|
| 158 | List of models we want to make available by default |
---|
| 159 | for this application |
---|
| 160 | |
---|
| 161 | @return: the next free event ID following the new menu events |
---|
| 162 | """ |
---|
| 163 | ## form factor |
---|
| 164 | from sans.models.SphereModel import SphereModel |
---|
| 165 | self.shape_list.append(SphereModel) |
---|
| 166 | self.multiplication_factor.append(SphereModel) |
---|
| 167 | |
---|
[ce07fa8] | 168 | from sans.models.FuzzySphereModel import FuzzySphereModel |
---|
| 169 | self.shape_list.append(FuzzySphereModel) |
---|
| 170 | self.multiplication_factor.append(FuzzySphereModel) |
---|
| 171 | |
---|
[0277d084] | 172 | from sans.models.CoreShellModel import CoreShellModel |
---|
| 173 | self.shape_list.append(CoreShellModel) |
---|
| 174 | self.multiplication_factor.append(CoreShellModel) |
---|
[ce07fa8] | 175 | |
---|
| 176 | from sans.models.CoreFourShellModel import CoreFourShellModel |
---|
| 177 | self.shape_list.append(CoreFourShellModel) |
---|
| 178 | self.multiplication_factor.append(CoreFourShellModel) |
---|
[0277d084] | 179 | |
---|
| 180 | from sans.models.VesicleModel import VesicleModel |
---|
| 181 | self.shape_list.append(VesicleModel) |
---|
| 182 | self.multiplication_factor.append(VesicleModel) |
---|
| 183 | |
---|
| 184 | from sans.models.MultiShellModel import MultiShellModel |
---|
| 185 | self.shape_list.append(MultiShellModel) |
---|
| 186 | self.multiplication_factor.append(MultiShellModel) |
---|
| 187 | |
---|
| 188 | from sans.models.BinaryHSModel import BinaryHSModel |
---|
| 189 | self.shape_list.append(BinaryHSModel) |
---|
| 190 | |
---|
| 191 | from sans.models.CylinderModel import CylinderModel |
---|
| 192 | self.shape_list.append(CylinderModel) |
---|
| 193 | self.multiplication_factor.append(CylinderModel) |
---|
| 194 | |
---|
| 195 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
| 196 | self.shape_list.append(CoreShellCylinderModel) |
---|
| 197 | self.multiplication_factor.append(CoreShellCylinderModel) |
---|
| 198 | |
---|
| 199 | from sans.models.HollowCylinderModel import HollowCylinderModel |
---|
| 200 | self.shape_list.append(HollowCylinderModel) |
---|
| 201 | self.multiplication_factor.append(HollowCylinderModel) |
---|
| 202 | |
---|
| 203 | from sans.models.FlexibleCylinderModel import FlexibleCylinderModel |
---|
| 204 | self.shape_list.append(FlexibleCylinderModel) |
---|
| 205 | |
---|
[ce07fa8] | 206 | from sans.models.FlexCylEllipXModel import FlexCylEllipXModel |
---|
| 207 | self.shape_list.append(FlexCylEllipXModel) |
---|
[0277d084] | 208 | |
---|
| 209 | from sans.models.StackedDisksModel import StackedDisksModel |
---|
| 210 | self.shape_list.append(StackedDisksModel) |
---|
| 211 | self.multiplication_factor.append(StackedDisksModel) |
---|
| 212 | |
---|
| 213 | from sans.models.ParallelepipedModel import ParallelepipedModel |
---|
| 214 | self.shape_list.append(ParallelepipedModel) |
---|
| 215 | self.multiplication_factor.append(ParallelepipedModel) |
---|
| 216 | |
---|
| 217 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
| 218 | self.shape_list.append(EllipticalCylinderModel) |
---|
| 219 | self.multiplication_factor.append(EllipticalCylinderModel) |
---|
| 220 | |
---|
| 221 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
| 222 | self.shape_list.append(EllipsoidModel) |
---|
| 223 | self.multiplication_factor.append(EllipsoidModel) |
---|
| 224 | |
---|
| 225 | from sans.models.CoreShellEllipsoidModel import CoreShellEllipsoidModel |
---|
| 226 | self.shape_list.append(CoreShellEllipsoidModel) |
---|
| 227 | self.multiplication_factor.append(CoreShellEllipsoidModel) |
---|
| 228 | |
---|
| 229 | from sans.models.TriaxialEllipsoidModel import TriaxialEllipsoidModel |
---|
| 230 | self.shape_list.append(TriaxialEllipsoidModel) |
---|
| 231 | self.multiplication_factor.append(TriaxialEllipsoidModel) |
---|
| 232 | |
---|
| 233 | from sans.models.LamellarModel import LamellarModel |
---|
| 234 | self.shape_list.append(LamellarModel) |
---|
| 235 | |
---|
| 236 | from sans.models.LamellarFFHGModel import LamellarFFHGModel |
---|
| 237 | self.shape_list.append(LamellarFFHGModel) |
---|
| 238 | |
---|
| 239 | from sans.models.LamellarPSModel import LamellarPSModel |
---|
| 240 | self.shape_list.append(LamellarPSModel) |
---|
| 241 | |
---|
| 242 | from sans.models.LamellarPSHGModel import LamellarPSHGModel |
---|
| 243 | self.shape_list.append(LamellarPSHGModel) |
---|
| 244 | |
---|
| 245 | ## Structure factor |
---|
| 246 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
| 247 | self.struct_list.append(SquareWellStructure) |
---|
| 248 | |
---|
| 249 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
| 250 | self.struct_list.append(HardsphereStructure) |
---|
| 251 | |
---|
| 252 | from sans.models.StickyHSStructure import StickyHSStructure |
---|
| 253 | self.struct_list.append(StickyHSStructure) |
---|
| 254 | |
---|
| 255 | from sans.models.HayterMSAStructure import HayterMSAStructure |
---|
| 256 | self.struct_list.append(HayterMSAStructure) |
---|
| 257 | |
---|
| 258 | |
---|
| 259 | ##shape-independent models |
---|
[ce07fa8] | 260 | |
---|
| 261 | from sans.models.PowerLawAbsModel import PowerLawAbsModel |
---|
| 262 | self.shape_indep_list.append( PowerLawAbsModel ) |
---|
| 263 | |
---|
[0277d084] | 264 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
| 265 | self.shape_indep_list.append(BEPolyelectrolyte ) |
---|
| 266 | self.form_factor_dict[str(wx.NewId())] = [SphereModel] |
---|
[ce07fa8] | 267 | |
---|
[0277d084] | 268 | from sans.models.DABModel import DABModel |
---|
| 269 | self.shape_indep_list.append(DABModel ) |
---|
| 270 | |
---|
[ce07fa8] | 271 | from sans.models.DebyeModel import DebyeModel |
---|
| 272 | self.shape_indep_list.append(DebyeModel ) |
---|
| 273 | |
---|
[0277d084] | 274 | from sans.models.GuinierModel import GuinierModel |
---|
| 275 | self.shape_indep_list.append(GuinierModel ) |
---|
| 276 | |
---|
[ce07fa8] | 277 | from sans.models.FractalModel import FractalModel |
---|
| 278 | self.shape_indep_list.append(FractalModel ) |
---|
[0277d084] | 279 | |
---|
[ce07fa8] | 280 | from sans.models.LorentzModel import LorentzModel |
---|
| 281 | self.shape_indep_list.append( LorentzModel) |
---|
[0277d084] | 282 | |
---|
| 283 | from sans.models.PeakGaussModel import PeakGaussModel |
---|
| 284 | self.shape_indep_list.append(PeakGaussModel) |
---|
| 285 | |
---|
| 286 | from sans.models.PeakLorentzModel import PeakLorentzModel |
---|
| 287 | self.shape_indep_list.append(PeakLorentzModel) |
---|
| 288 | |
---|
[ce07fa8] | 289 | from sans.models.Poly_GaussCoil import Poly_GaussCoil |
---|
| 290 | self.shape_indep_list.append(Poly_GaussCoil) |
---|
| 291 | |
---|
| 292 | from sans.models.PorodModel import PorodModel |
---|
| 293 | self.shape_indep_list.append(PorodModel ) |
---|
[0277d084] | 294 | |
---|
[ce07fa8] | 295 | |
---|
| 296 | #FractalModel (a c-model)will be used. |
---|
| 297 | #from sans.models.FractalAbsModel import FractalAbsModel |
---|
| 298 | #self.shape_indep_list.append(FractalAbsModel) |
---|
[0277d084] | 299 | |
---|
| 300 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
| 301 | self.shape_indep_list.append(TeubnerStreyModel ) |
---|
| 302 | |
---|
| 303 | from sans.models.LineModel import LineModel |
---|
| 304 | self.shape_indep_list.append(LineModel) |
---|
| 305 | |
---|
| 306 | #Looking for plugins |
---|
| 307 | self.plugins = findModels() |
---|
| 308 | |
---|
| 309 | return 0 |
---|
| 310 | |
---|
| 311 | |
---|
| 312 | def get_model_list(self): |
---|
| 313 | """ @ return dictionary of models for fitpanel use """ |
---|
| 314 | self._getModelList() |
---|
| 315 | self.model_combobox.set_list("Shapes", self.shape_list) |
---|
| 316 | self.model_combobox.set_list("Shape-Independent", self.shape_indep_list) |
---|
| 317 | self.model_combobox.set_list("Structure Factors", self.struct_list) |
---|
| 318 | self.model_combobox.set_list("Customized Models", self.plugins) |
---|
| 319 | self.model_combobox.set_list("P(Q)*S(Q)", self.multiplication_factor) |
---|
| 320 | self.model_combobox.set_list("multiplication", self.multiplication_factor) |
---|
| 321 | |
---|
| 322 | return self.model_combobox |
---|
| 323 | |
---|
| 324 | |
---|
| 325 | |
---|
| 326 | |
---|
| 327 | |
---|
| 328 | |
---|