[33afff7] | 1 | #TODO: add comments to document this module |
---|
| 2 | #TODO: clean-up the exception handling. |
---|
| 3 | #TODO: clean-up existing comments/documentation. |
---|
| 4 | # For example, the _getModelList method advertises |
---|
| 5 | # an 'id' parameter that is not part of the method's signature. |
---|
| 6 | # It also advertises an ID as return value but it always returns zero. |
---|
| 7 | #TODO: clean-up the FractalAbsModel and PowerLawAbsModel menu items. Those |
---|
| 8 | # model definitions do not belong here. They belong with the rest of the |
---|
| 9 | # models. |
---|
| 10 | |
---|
[d89f09b] | 11 | import wx |
---|
[b30f001] | 12 | import imp |
---|
[442895f] | 13 | import os,sys,math |
---|
[d89f09b] | 14 | import os.path |
---|
| 15 | |
---|
| 16 | (ModelEvent, EVT_MODEL) = wx.lib.newevent.NewEvent() |
---|
[00561739] | 17 | |
---|
[33afff7] | 18 | # Time is needed by the log method |
---|
| 19 | import time |
---|
[2dbb681] | 20 | |
---|
[33afff7] | 21 | # Explicitly import from the pluginmodel module so that py2exe |
---|
| 22 | # places it in the distribution. The Model1DPlugin class is used |
---|
| 23 | # as the base class of plug-in models. |
---|
| 24 | from sans.models.pluginmodel import Model1DPlugin |
---|
[00561739] | 25 | |
---|
[b30f001] | 26 | def log(message): |
---|
| 27 | out = open("plugins.log", 'a') |
---|
| 28 | out.write("%10g: %s\n" % (time.clock(), message)) |
---|
| 29 | out.close() |
---|
| 30 | |
---|
[aa92772] | 31 | def findModels(): |
---|
[33afff7] | 32 | log("looking for models in: %s/plugins" % os.getcwd()) |
---|
[b30f001] | 33 | if os.path.isdir('plugins'): |
---|
| 34 | return _findModels('plugins') |
---|
| 35 | return [] |
---|
[aa92772] | 36 | |
---|
[1c66bc5] | 37 | def _findModels(dir): |
---|
| 38 | # List of plugin objects |
---|
| 39 | plugins = [] |
---|
| 40 | # Go through files in plug-in directory |
---|
| 41 | try: |
---|
| 42 | list = os.listdir(dir) |
---|
| 43 | for item in list: |
---|
| 44 | toks = os.path.splitext(os.path.basename(item)) |
---|
| 45 | if toks[1]=='.py' and not toks[0]=='__init__': |
---|
| 46 | name = toks[0] |
---|
| 47 | |
---|
| 48 | path = [os.path.abspath(dir)] |
---|
| 49 | file = None |
---|
| 50 | try: |
---|
| 51 | (file, path, info) = imp.find_module(name, path) |
---|
| 52 | module = imp.load_module( name, file, item, info ) |
---|
| 53 | if hasattr(module, "Model"): |
---|
| 54 | try: |
---|
| 55 | plugins.append(module.Model) |
---|
| 56 | except: |
---|
| 57 | log("Error accessing Model in %s\n %s" % (name, sys.exc_value)) |
---|
| 58 | except: |
---|
[b30f001] | 59 | log("Error accessing Model in %s\n %s" % (name, sys.exc_value)) |
---|
[1c66bc5] | 60 | finally: |
---|
[a92d51b] | 61 | |
---|
[1c66bc5] | 62 | if not file==None: |
---|
| 63 | file.close() |
---|
| 64 | except: |
---|
[33afff7] | 65 | # Don't deal with bad plug-in imports. Just skip. |
---|
[1c66bc5] | 66 | pass |
---|
| 67 | return plugins |
---|
[d89f09b] | 68 | class ModelManager: |
---|
| 69 | |
---|
| 70 | ## Dictionary of models |
---|
| 71 | model_list = {} |
---|
[49b7efa] | 72 | indep_model_list = {} |
---|
[d89f09b] | 73 | model_list_box = {} |
---|
[b30f001] | 74 | custom_models={} |
---|
| 75 | plugins=[] |
---|
[49b7efa] | 76 | indep_model=[] |
---|
[d89f09b] | 77 | ## Event owner |
---|
| 78 | event_owner = None |
---|
| 79 | |
---|
| 80 | def _getModelList(self): |
---|
| 81 | """ |
---|
| 82 | List of models we want to make available by default |
---|
| 83 | for this application |
---|
| 84 | |
---|
| 85 | @param id: first event ID to register the menu events with |
---|
| 86 | @return: the next free event ID following the new menu events |
---|
| 87 | """ |
---|
| 88 | self.model_list = {} |
---|
| 89 | self.model_list_box = {} |
---|
[2dbb681] | 90 | |
---|
[442895f] | 91 | |
---|
| 92 | from sans.models.SphereModel import SphereModel |
---|
[2dbb681] | 93 | self.model_list[str(wx.NewId())] = SphereModel |
---|
[442895f] | 94 | |
---|
[d89f09b] | 95 | from sans.models.CylinderModel import CylinderModel |
---|
[2dbb681] | 96 | self.model_list[str(wx.NewId())] =CylinderModel |
---|
[d89f09b] | 97 | |
---|
[442895f] | 98 | from sans.models.CoreShellModel import CoreShellModel |
---|
[2dbb681] | 99 | self.model_list[str(wx.NewId())] = CoreShellModel |
---|
[442895f] | 100 | |
---|
| 101 | from sans.models.CoreShellCylinderModel import CoreShellCylinderModel |
---|
[2dbb681] | 102 | self.model_list[str(wx.NewId())] =CoreShellCylinderModel |
---|
[442895f] | 103 | |
---|
| 104 | from sans.models.EllipticalCylinderModel import EllipticalCylinderModel |
---|
[2dbb681] | 105 | self.model_list[str(wx.NewId())] =EllipticalCylinderModel |
---|
[442895f] | 106 | |
---|
| 107 | from sans.models.EllipsoidModel import EllipsoidModel |
---|
[2dbb681] | 108 | self.model_list[str(wx.NewId())] = EllipsoidModel |
---|
[442895f] | 109 | |
---|
[55e13ab] | 110 | from sans.models.LineModel import LineModel |
---|
[2dbb681] | 111 | self.model_list[str(wx.NewId())] = LineModel |
---|
[442895f] | 112 | |
---|
| 113 | |
---|
[49b7efa] | 114 | model_info="shape-independent models" |
---|
[442895f] | 115 | |
---|
| 116 | from sans.models.BEPolyelectrolyte import BEPolyelectrolyte |
---|
[2dbb681] | 117 | self.indep_model.append(BEPolyelectrolyte ) |
---|
[442895f] | 118 | |
---|
| 119 | from sans.models.DABModel import DABModel |
---|
[2dbb681] | 120 | self.indep_model.append(DABModel ) |
---|
[442895f] | 121 | |
---|
[f39511b] | 122 | from sans.models.GuinierModel import GuinierModel |
---|
| 123 | self.indep_model.append(GuinierModel ) |
---|
| 124 | |
---|
[442895f] | 125 | from sans.models.DebyeModel import DebyeModel |
---|
[2dbb681] | 126 | self.indep_model.append(DebyeModel ) |
---|
[442895f] | 127 | |
---|
| 128 | from sans.models.FractalModel import FractalModel |
---|
| 129 | class FractalAbsModel(FractalModel): |
---|
| 130 | def _Fractal(self, x): |
---|
| 131 | return FractalModel._Fractal(self, math.fabs(x)) |
---|
[2dbb681] | 132 | self.indep_model.append(FractalAbsModel) |
---|
[442895f] | 133 | |
---|
| 134 | from sans.models.LorentzModel import LorentzModel |
---|
[2dbb681] | 135 | self.indep_model.append( LorentzModel) |
---|
[442895f] | 136 | |
---|
| 137 | from sans.models.PowerLawModel import PowerLawModel |
---|
| 138 | class PowerLawAbsModel(PowerLawModel): |
---|
| 139 | def _PowerLaw(self, x): |
---|
| 140 | try: |
---|
| 141 | return PowerLawModel._PowerLaw(self, math.fabs(x)) |
---|
| 142 | except: |
---|
| 143 | print sys.exc_value |
---|
[2dbb681] | 144 | self.indep_model.append( PowerLawAbsModel ) |
---|
[442895f] | 145 | from sans.models.TeubnerStreyModel import TeubnerStreyModel |
---|
[2dbb681] | 146 | self.indep_model.append(TeubnerStreyModel ) |
---|
| 147 | |
---|
[49b7efa] | 148 | #Looking for plugins |
---|
| 149 | self.plugins = findModels() |
---|
| 150 | |
---|
[d89f09b] | 151 | return 0 |
---|
| 152 | |
---|
| 153 | |
---|
| 154 | def populate_menu(self, modelmenu, event_owner): |
---|
| 155 | """ |
---|
| 156 | Populate a menu with our models |
---|
| 157 | |
---|
| 158 | @param id: first menu event ID to use when binding the menu events |
---|
| 159 | @param modelmenu: wx.Menu object to populate |
---|
| 160 | @param event_owner: wx object to bind the menu events to |
---|
| 161 | @return: the next free event ID following the new menu events |
---|
| 162 | """ |
---|
| 163 | self._getModelList() |
---|
| 164 | self.event_owner = event_owner |
---|
[b30f001] | 165 | shape_submenu= wx.Menu() |
---|
| 166 | indep_submenu = wx.Menu() |
---|
| 167 | added_models = wx.Menu() |
---|
[d89f09b] | 168 | for id_str,value in self.model_list.iteritems(): |
---|
[2dbb681] | 169 | item = self.model_list[id_str]() |
---|
| 170 | name = item.__class__.__name__ |
---|
[d89f09b] | 171 | if hasattr(item, "name"): |
---|
[2dbb681] | 172 | name = item.name |
---|
| 173 | self.model_list_box[name] =value |
---|
[b30f001] | 174 | shape_submenu.Append(int(id_str), name, name) |
---|
| 175 | wx.EVT_MENU(event_owner, int(id_str), self._on_model) |
---|
| 176 | modelmenu.AppendMenu(wx.NewId(), "Shapes...", shape_submenu, "List of shape-based models") |
---|
| 177 | id = wx.NewId() |
---|
[49b7efa] | 178 | if len(self.indep_model_list) == 0: |
---|
[2dbb681] | 179 | for items in self.indep_model: |
---|
[00561739] | 180 | #if item not in self.indep_model_list.values(): |
---|
| 181 | #self.indep_model_list[str(id)] = item |
---|
[2dbb681] | 182 | self.model_list[str(id)]=items |
---|
| 183 | item=items() |
---|
| 184 | name = item.__class__.__name__ |
---|
[00561739] | 185 | if hasattr(item, "name"): |
---|
[2dbb681] | 186 | name = item.name |
---|
[00561739] | 187 | indep_submenu.Append(id,name, name) |
---|
[2dbb681] | 188 | self.model_list_box[name] =items |
---|
[00561739] | 189 | wx.EVT_MENU(event_owner, int(id), self._on_model) |
---|
| 190 | id = wx.NewId() |
---|
[49b7efa] | 191 | modelmenu.AppendMenu(wx.NewId(), "Shape-independent...", indep_submenu, "List of shape-independent models") |
---|
| 192 | id = wx.NewId() |
---|
[b30f001] | 193 | if len(self.custom_models) == 0: |
---|
[2dbb681] | 194 | for items in self.plugins: |
---|
[00561739] | 195 | #if item not in self.custom_models.values(): |
---|
| 196 | #self.custom_models[str(id)] = item |
---|
[2dbb681] | 197 | self.model_list[str(id)]=items |
---|
[44bbf6a] | 198 | name = items.__name__ |
---|
| 199 | if hasattr(items, "name"): |
---|
| 200 | name = items.name |
---|
[00561739] | 201 | added_models.Append(id, name, name) |
---|
[2dbb681] | 202 | self.model_list_box[name] =items |
---|
[00561739] | 203 | wx.EVT_MENU(event_owner, int(id), self._on_model) |
---|
| 204 | id = wx.NewId() |
---|
[b30f001] | 205 | modelmenu.AppendMenu(wx.NewId(),"Added models...", added_models, "List of additional models") |
---|
[d89f09b] | 206 | return 0 |
---|
| 207 | |
---|
| 208 | def _on_model(self, evt): |
---|
| 209 | """ |
---|
| 210 | React to a model menu event |
---|
| 211 | @param event: wx menu event |
---|
| 212 | """ |
---|
| 213 | if str(evt.GetId()) in self.model_list.keys(): |
---|
| 214 | # Notify the application manager that a new model has been set |
---|
| 215 | #self.app_manager.set_model(self.model_list[str(evt.GetId())]()) |
---|
| 216 | |
---|
| 217 | #TODO: post a model event to update all panels that need |
---|
| 218 | #evt = ModelEvent(model=self.model_list[str(evt.GetId())]()) |
---|
| 219 | |
---|
[49b7efa] | 220 | model = self.model_list[str(evt.GetId())] |
---|
[2dbb681] | 221 | evt = ModelEvent(model= model ) |
---|
[d89f09b] | 222 | wx.PostEvent(self.event_owner, evt) |
---|
| 223 | |
---|
| 224 | def get_model_list(self): |
---|
| 225 | """ @ return dictionary of models for fitpanel use """ |
---|
| 226 | return self.model_list_box |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | |
---|
| 230 | |
---|