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