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