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