[33afff7] | 1 | #TODO: add comments to document this module |
---|
| 2 | #TODO: clean-up the exception handling. |
---|
[e7b1ccf] | 3 | |
---|
[33afff7] | 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 | |
---|
[d89f09b] | 8 | import wx |
---|
[bb18ef1] | 9 | import wx.lib.newevent |
---|
[b30f001] | 10 | import imp |
---|
[442895f] | 11 | import os,sys,math |
---|
[d89f09b] | 12 | import os.path |
---|
| 13 | |
---|
| 14 | (ModelEvent, EVT_MODEL) = wx.lib.newevent.NewEvent() |
---|
[e7b1ccf] | 15 | from sans.guicomm.events import StatusEvent |
---|
[33afff7] | 16 | # Time is needed by the log method |
---|
| 17 | import time |
---|
[2dbb681] | 18 | |
---|
[33afff7] | 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 |
---|
[00561739] | 23 | |
---|
[b30f001] | 24 | def log(message): |
---|
| 25 | out = open("plugins.log", 'a') |
---|
| 26 | out.write("%10g: %s\n" % (time.clock(), message)) |
---|
| 27 | out.close() |
---|
| 28 | |
---|
[aa92772] | 29 | def findModels(): |
---|
[33afff7] | 30 | log("looking for models in: %s/plugins" % os.getcwd()) |
---|
[b30f001] | 31 | if os.path.isdir('plugins'): |
---|
| 32 | return _findModels('plugins') |
---|
| 33 | return [] |
---|
[aa92772] | 34 | |
---|
[1c66bc5] | 35 | def _findModels(dir): |
---|
| 36 | # List of plugin objects |
---|
| 37 | plugins = [] |
---|
| 38 | # Go through files in plug-in directory |
---|
| 39 | try: |
---|
| 40 | list = os.listdir(dir) |
---|
| 41 | for item in list: |
---|
| 42 | toks = os.path.splitext(os.path.basename(item)) |
---|
| 43 | if toks[1]=='.py' and not toks[0]=='__init__': |
---|
| 44 | name = toks[0] |
---|
| 45 | |
---|
| 46 | path = [os.path.abspath(dir)] |
---|
| 47 | file = None |
---|
| 48 | try: |
---|
| 49 | (file, path, info) = imp.find_module(name, path) |
---|
| 50 | module = imp.load_module( name, file, item, info ) |
---|
| 51 | if hasattr(module, "Model"): |
---|
| 52 | try: |
---|
| 53 | plugins.append(module.Model) |
---|
| 54 | except: |
---|
| 55 | log("Error accessing Model in %s\n %s" % (name, sys.exc_value)) |
---|
| 56 | except: |
---|
[b30f001] | 57 | log("Error accessing Model in %s\n %s" % (name, sys.exc_value)) |
---|
[1c66bc5] | 58 | finally: |
---|
[a92d51b] | 59 | |
---|
[1c66bc5] | 60 | if not file==None: |
---|
| 61 | file.close() |
---|
| 62 | except: |
---|
[33afff7] | 63 | # Don't deal with bad plug-in imports. Just skip. |
---|
[1c66bc5] | 64 | pass |
---|
| 65 | return plugins |
---|
[bb18ef1] | 66 | |
---|
| 67 | class ModelList(object): |
---|
| 68 | """ |
---|
| 69 | Contains dictionary of model and their type |
---|
| 70 | """ |
---|
| 71 | def __init__(self): |
---|
| 72 | self.mydict={} |
---|
| 73 | |
---|
| 74 | def set_list(self, name, mylist): |
---|
| 75 | """ |
---|
| 76 | @param name: the type of the list |
---|
| 77 | @param mylist: the list to add |
---|
| 78 | """ |
---|
| 79 | if name not in self.mydict.keys(): |
---|
| 80 | self.mydict[name] = mylist |
---|
| 81 | |
---|
| 82 | |
---|
| 83 | def get_list(self): |
---|
| 84 | """ |
---|
| 85 | return all the list stored in a dictionary object |
---|
| 86 | """ |
---|
| 87 | return self.mydict |
---|
| 88 | |
---|
[d89f09b] | 89 | class ModelManager: |
---|
[bb18ef1] | 90 | ## external dict for models |
---|
| 91 | model_combobox = ModelList() |
---|
| 92 | ## Dictionary of form models |
---|
| 93 | form_factor_dict = {} |
---|
| 94 | ## dictionary of other |
---|
| 95 | struct_factor_dict = {} |
---|
| 96 | ##list of form factors |
---|
| 97 | shape_list =[] |
---|
| 98 | ## independent shape model list |
---|
| 99 | shape_indep_list = [] |
---|
| 100 | ##list of structure factors |
---|
| 101 | struct_list= [] |
---|
[376916c] | 102 | ##list of model allowing multiplication |
---|
| 103 | multiplication_factor=[] |
---|
[bb18ef1] | 104 | ## list of added models |
---|
[b30f001] | 105 | plugins=[] |
---|
[bb18ef1] | 106 | ## Event owner (guiframe) |
---|
[d89f09b] | 107 | event_owner = None |
---|
| 108 | |
---|
| 109 | def _getModelList(self): |
---|
| 110 | """ |
---|
| 111 | List of models we want to make available by default |
---|
| 112 | for this application |
---|
[e7b1ccf] | 113 | |
---|
[d89f09b] | 114 | @return: the next free event ID following the new menu events |
---|
| 115 | """ |
---|
[bb18ef1] | 116 | ## form factor |
---|
[442895f] | 117 | from sans.models.SphereModel import SphereModel |
---|
[bb18ef1] | 118 | self.shape_list.append(SphereModel) |
---|
[376916c] | 119 | self.multiplication_factor.append(SphereModel) |
---|
[442895f] | 120 | |
---|
[d89f09b] | 121 | from sans.models.CylinderModel import CylinderModel |
---|
[bb18ef1] | 122 | self.shape_list.append(CylinderModel) |
---|
[376916c] | 123 | self.multiplication_factor.append(CylinderModel) |
---|
| 124 | |
---|
[e65050e] | 125 | from sans.models.ParallelepipedModel import ParallelepipedModel |
---|
| 126 | self.shape_list.append(ParallelepipedModel) |
---|
[2b687dc] | 127 | #self.multiplication_factor.append(ParallelepipedModel) |
---|
[3b9e023] | 128 | |
---|
[442895f] | 129 | from sans.models.CoreShellModel import CoreShellModel |
---|
[bb18ef1] | 130 | self.shape_list.append(CoreShellModel) |
---|
[442895f] | 131 | |
---|
| 132 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
[bb18ef1] | 133 | self.shape_list.append(CoreShellCylinderModel) |
---|
[442895f] | 134 | |
---|
[cee6867] | 135 | from sans.models.MultiShellModel import MultiShellModel |
---|
| 136 | self.shape_list.append(MultiShellModel) |
---|
| 137 | |
---|
| 138 | from sans.models.BinaryHSModel import BinaryHSModel |
---|
| 139 | self.shape_list.append(BinaryHSModel) |
---|
| 140 | |
---|
| 141 | from sans.models.VesicleModel import VesicleModel |
---|
| 142 | self.shape_list.append(VesicleModel) |
---|
| 143 | |
---|
| 144 | from sans.models.HollowCylinderModel import HollowCylinderModel |
---|
| 145 | self.shape_list.append(HollowCylinderModel) |
---|
| 146 | |
---|
[442895f] | 147 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
[bb18ef1] | 148 | self.shape_list.append(EllipticalCylinderModel) |
---|
[442895f] | 149 | |
---|
| 150 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
[bb18ef1] | 151 | self.shape_list.append(EllipsoidModel) |
---|
[376916c] | 152 | self.multiplication_factor.append(EllipsoidModel) |
---|
[bb18ef1] | 153 | |
---|
[e65050e] | 154 | from sans.models.TriaxialEllipsoidModel import TriaxialEllipsoidModel |
---|
| 155 | self.shape_list.append(TriaxialEllipsoidModel) |
---|
[7a69683] | 156 | |
---|
[e65050e] | 157 | from sans.models.FlexibleCylinderModel import FlexibleCylinderModel |
---|
| 158 | self.shape_list.append(FlexibleCylinderModel) |
---|
| 159 | |
---|
| 160 | from sans.models.StackedDisksModel import StackedDisksModel |
---|
| 161 | self.shape_list.append(StackedDisksModel) |
---|
| 162 | |
---|
| 163 | from sans.models.LamellarModel import LamellarModel |
---|
| 164 | self.shape_list.append(LamellarModel) |
---|
| 165 | |
---|
| 166 | from sans.models.LamellarFFHGModel import LamellarFFHGModel |
---|
| 167 | self.shape_list.append(LamellarFFHGModel) |
---|
| 168 | |
---|
| 169 | from sans.models.LamellarPSModel import LamellarPSModel |
---|
| 170 | self.shape_list.append(LamellarPSModel) |
---|
[7a69683] | 171 | |
---|
[e65050e] | 172 | from sans.models.LamellarPSHGModel import LamellarPSHGModel |
---|
| 173 | self.shape_list.append(LamellarPSHGModel) |
---|
[7a69683] | 174 | |
---|
[e65050e] | 175 | from sans.models.OblateModel import OblateModel |
---|
| 176 | self.shape_list.append(OblateModel) |
---|
| 177 | |
---|
| 178 | from sans.models.ProlateModel import ProlateModel |
---|
| 179 | self.shape_list.append(ProlateModel) |
---|
[7a69683] | 180 | |
---|
[376916c] | 181 | ## Structure factor |
---|
[8346667] | 182 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
[bb18ef1] | 183 | self.struct_list.append(SquareWellStructure) |
---|
[8346667] | 184 | |
---|
| 185 | from sans.models.HardsphereStructure import HardsphereStructure |
---|
[bb18ef1] | 186 | self.struct_list.append(HardsphereStructure) |
---|
| 187 | |
---|
[8346667] | 188 | from sans.models.StickyHSStructure import StickyHSStructure |
---|
[bb18ef1] | 189 | self.struct_list.append(StickyHSStructure) |
---|
[8346667] | 190 | |
---|
| 191 | from sans.models.HayterMSAStructure import HayterMSAStructure |
---|
[bb18ef1] | 192 | self.struct_list.append(HayterMSAStructure) |
---|
[43eb424] | 193 | |
---|
| 194 | |
---|
[bb18ef1] | 195 | ##shape-independent models |
---|
[442895f] | 196 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
[bb18ef1] | 197 | self.shape_indep_list.append(BEPolyelectrolyte ) |
---|
| 198 | self.form_factor_dict[str(wx.NewId())] = [SphereModel] |
---|
[442895f] | 199 | from sans.models.DABModel import DABModel |
---|
[bb18ef1] | 200 | self.shape_indep_list.append(DABModel ) |
---|
[442895f] | 201 | |
---|
[f39511b] | 202 | from sans.models.GuinierModel import GuinierModel |
---|
[bb18ef1] | 203 | self.shape_indep_list.append(GuinierModel ) |
---|
[f39511b] | 204 | |
---|
[442895f] | 205 | from sans.models.DebyeModel import DebyeModel |
---|
[bb18ef1] | 206 | self.shape_indep_list.append(DebyeModel ) |
---|
| 207 | |
---|
| 208 | from sans.models.PorodModel import PorodModel |
---|
| 209 | self.shape_indep_list.append(PorodModel ) |
---|
[442895f] | 210 | |
---|
[376916c] | 211 | from sans.models.LineModel import LineModel |
---|
| 212 | self.shape_indep_list.append(LineModel) |
---|
| 213 | |
---|
[cee6867] | 214 | from sans.models.PeakGaussModel import PeakGaussModel |
---|
| 215 | self.shape_indep_list.append(PeakGaussModel) |
---|
| 216 | |
---|
| 217 | from sans.models.PeakLorentzModel import PeakLorentzModel |
---|
| 218 | self.shape_indep_list.append(PeakLorentzModel) |
---|
| 219 | |
---|
[442895f] | 220 | from sans.models.FractalModel import FractalModel |
---|
| 221 | class FractalAbsModel(FractalModel): |
---|
| 222 | def _Fractal(self, x): |
---|
| 223 | return FractalModel._Fractal(self, math.fabs(x)) |
---|
[bb18ef1] | 224 | self.shape_indep_list.append(FractalAbsModel) |
---|
[442895f] | 225 | |
---|
| 226 | from sans.models.LorentzModel import LorentzModel |
---|
[bb18ef1] | 227 | self.shape_indep_list.append( LorentzModel) |
---|
[442895f] | 228 | |
---|
| 229 | from sans.models.PowerLawModel import PowerLawModel |
---|
| 230 | class PowerLawAbsModel(PowerLawModel): |
---|
| 231 | def _PowerLaw(self, x): |
---|
| 232 | try: |
---|
| 233 | return PowerLawModel._PowerLaw(self, math.fabs(x)) |
---|
| 234 | except: |
---|
| 235 | print sys.exc_value |
---|
[bb18ef1] | 236 | self.shape_indep_list.append( PowerLawAbsModel ) |
---|
[442895f] | 237 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
[bb18ef1] | 238 | self.shape_indep_list.append(TeubnerStreyModel ) |
---|
[2dbb681] | 239 | |
---|
[49b7efa] | 240 | #Looking for plugins |
---|
| 241 | self.plugins = findModels() |
---|
| 242 | |
---|
[d89f09b] | 243 | return 0 |
---|
| 244 | |
---|
| 245 | |
---|
| 246 | def populate_menu(self, modelmenu, event_owner): |
---|
| 247 | """ |
---|
| 248 | Populate a menu with our models |
---|
| 249 | |
---|
| 250 | @param id: first menu event ID to use when binding the menu events |
---|
| 251 | @param modelmenu: wx.Menu object to populate |
---|
| 252 | @param event_owner: wx object to bind the menu events to |
---|
| 253 | @return: the next free event ID following the new menu events |
---|
| 254 | """ |
---|
[bb18ef1] | 255 | ## Fill model lists |
---|
[d89f09b] | 256 | self._getModelList() |
---|
[bb18ef1] | 257 | ## store reference to model menu of guiframe |
---|
| 258 | self.modelmenu = modelmenu |
---|
| 259 | ## guiframe reference |
---|
[d89f09b] | 260 | self.event_owner = event_owner |
---|
[bb18ef1] | 261 | |
---|
| 262 | |
---|
| 263 | shape_submenu = wx.Menu() |
---|
| 264 | shape_indep_submenu = wx.Menu() |
---|
| 265 | structure_factor = wx.Menu() |
---|
[b30f001] | 266 | added_models = wx.Menu() |
---|
[376916c] | 267 | multip_models = wx.Menu() |
---|
[bb18ef1] | 268 | ## create menu with shape |
---|
[376916c] | 269 | self._fill_simple_menu( menuinfo = ["Shapes",shape_submenu," simple shape"], |
---|
| 270 | list1 = self.shape_list ) |
---|
| 271 | |
---|
| 272 | self._fill_simple_menu( menuinfo = ["Shape-Independent",shape_indep_submenu, |
---|
[bb18ef1] | 273 | "List of shape-independent models"], |
---|
[376916c] | 274 | list1 = self.shape_indep_list ) |
---|
[bb18ef1] | 275 | |
---|
| 276 | self._fill_simple_menu( menuinfo= ["Structure Factors",structure_factor, |
---|
| 277 | "List of Structure factors models" ], |
---|
| 278 | list1= self.struct_list ) |
---|
| 279 | |
---|
[376916c] | 280 | self._fill_simple_menu( menuinfo = ["Customized Models", added_models, |
---|
| 281 | "List of additional models"], |
---|
| 282 | list1= self.plugins ) |
---|
| 283 | |
---|
| 284 | self._fill_menu(menuinfo=["P(Q)*S(Q)",multip_models, |
---|
| 285 | "mulplication of 2 models"], |
---|
| 286 | list1 = self.multiplication_factor , |
---|
| 287 | list2 = self.struct_list) |
---|
| 288 | |
---|
[c77d859] | 289 | |
---|
[d89f09b] | 290 | return 0 |
---|
| 291 | |
---|
[bb18ef1] | 292 | def _fill_simple_menu(self,menuinfo, list1): |
---|
| 293 | """ |
---|
| 294 | Fill the menu with list item |
---|
| 295 | @param modelmenu: the menu to fill |
---|
| 296 | @param menuinfo: submenu item for the first column of this modelmenu |
---|
| 297 | with info.Should be a list : |
---|
| 298 | [name(string) , menu(wx.menu), help(string)] |
---|
| 299 | @param list1: contains item (form factor )to fill modelmenu second column |
---|
| 300 | """ |
---|
| 301 | if len(list1)>0: |
---|
| 302 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
[e7b1ccf] | 303 | |
---|
[bb18ef1] | 304 | for item in list1: |
---|
[e7b1ccf] | 305 | try: |
---|
| 306 | id = wx.NewId() |
---|
| 307 | struct_factor=item() |
---|
| 308 | struct_name = struct_factor.__class__.__name__ |
---|
| 309 | if hasattr(struct_factor, "name"): |
---|
| 310 | struct_name = struct_factor.name |
---|
| 311 | |
---|
| 312 | menuinfo[1].Append(int(id),struct_name,struct_name) |
---|
| 313 | if not item in self.struct_factor_dict.itervalues(): |
---|
| 314 | self.struct_factor_dict[str(id)]= item |
---|
| 315 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
| 316 | except: |
---|
| 317 | msg= "Error Occured: %s"%sys.exc_value |
---|
| 318 | wx.PostEvent(self.event_owner, StatusEvent(status=msg)) |
---|
[bb18ef1] | 319 | |
---|
| 320 | id = wx.NewId() |
---|
| 321 | self.modelmenu.AppendMenu(id, menuinfo[0],menuinfo[1],menuinfo[2]) |
---|
| 322 | |
---|
| 323 | |
---|
| 324 | |
---|
| 325 | def _fill_menu(self,menuinfo, list1,list2 ): |
---|
| 326 | """ |
---|
| 327 | Fill the menu with list item |
---|
| 328 | @param menuinfo: submenu item for the first column of this modelmenu |
---|
| 329 | with info.Should be a list : |
---|
| 330 | [name(string) , menu(wx.menu), help(string)] |
---|
| 331 | @param list1: contains item (form factor )to fill modelmenu second column |
---|
| 332 | @param list2: contains item (Structure factor )to fill modelmenu third column |
---|
| 333 | """ |
---|
| 334 | if len(list1)>0: |
---|
| 335 | self.model_combobox.set_list(menuinfo[0],list1) |
---|
| 336 | |
---|
| 337 | for item in list1: |
---|
| 338 | form_factor= item() |
---|
| 339 | form_name = form_factor.__class__.__name__ |
---|
| 340 | if hasattr(form_factor, "name"): |
---|
| 341 | form_name = form_factor.name |
---|
| 342 | ### store form factor to return to other users |
---|
| 343 | newmenu= wx.Menu() |
---|
| 344 | if len(list2)>0: |
---|
| 345 | for model in list2: |
---|
| 346 | id = wx.NewId() |
---|
| 347 | struct_factor = model() |
---|
| 348 | name = struct_factor.__class__.__name__ |
---|
| 349 | if hasattr(struct_factor, "name"): |
---|
| 350 | name = struct_factor.name |
---|
| 351 | newmenu.Append(id,name, name) |
---|
| 352 | wx.EVT_MENU(self.event_owner, int(id), self._on_model) |
---|
| 353 | ## save form_fact and struct_fact |
---|
| 354 | self.form_factor_dict[int(id)] = [form_factor,struct_factor] |
---|
| 355 | |
---|
| 356 | form_id= wx.NewId() |
---|
| 357 | menuinfo[1].AppendMenu(int(form_id), form_name,newmenu,menuinfo[2]) |
---|
| 358 | id=wx.NewId() |
---|
| 359 | self.modelmenu.AppendMenu(id,menuinfo[0],menuinfo[1], menuinfo[2]) |
---|
| 360 | |
---|
| 361 | |
---|
| 362 | |
---|
| 363 | |
---|
[d89f09b] | 364 | def _on_model(self, evt): |
---|
| 365 | """ |
---|
| 366 | React to a model menu event |
---|
| 367 | @param event: wx menu event |
---|
| 368 | """ |
---|
[bb18ef1] | 369 | if int(evt.GetId()) in self.form_factor_dict.keys(): |
---|
| 370 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
| 371 | model1, model2 = self.form_factor_dict[int(evt.GetId())] |
---|
| 372 | model = MultiplicationModel(model1, model2) |
---|
| 373 | |
---|
| 374 | else: |
---|
| 375 | model= self.struct_factor_dict[str(evt.GetId())]() |
---|
[d89f09b] | 376 | |
---|
[bb18ef1] | 377 | evt = ModelEvent( model= model ) |
---|
| 378 | wx.PostEvent(self.event_owner, evt) |
---|
[d89f09b] | 379 | |
---|
| 380 | def get_model_list(self): |
---|
| 381 | """ @ return dictionary of models for fitpanel use """ |
---|
[376916c] | 382 | self.model_combobox.set_list("multiplication", self.multiplication_factor) |
---|
[bb18ef1] | 383 | return self.model_combobox |
---|
[d89f09b] | 384 | |
---|
[376916c] | 385 | |
---|
[bb18ef1] | 386 | |
---|
| 387 | |
---|
[d89f09b] | 388 | |
---|
[bb18ef1] | 389 | |
---|