1 | import sys |
---|
2 | import wx |
---|
3 | import wx.lib |
---|
4 | import numpy |
---|
5 | import copy |
---|
6 | |
---|
7 | from sans.guicomm.events import StatusEvent |
---|
8 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
9 | _BOX_WIDTH = 80 |
---|
10 | |
---|
11 | def format_number(value, high=False): |
---|
12 | """ |
---|
13 | Return a float in a standardized, human-readable formatted string |
---|
14 | """ |
---|
15 | try: |
---|
16 | value = float(value) |
---|
17 | except: |
---|
18 | print "returning 0" |
---|
19 | return "0" |
---|
20 | |
---|
21 | if high: |
---|
22 | return "%-6.4g" % value |
---|
23 | else: |
---|
24 | return "%-5.3g" % value |
---|
25 | |
---|
26 | |
---|
27 | class ModelPage(wx.Panel): |
---|
28 | """ |
---|
29 | FitPanel class contains fields allowing to display results when |
---|
30 | fitting a model and one data |
---|
31 | @note: For Fit to be performed the user should check at least one parameter |
---|
32 | on fit Panel window. |
---|
33 | |
---|
34 | """ |
---|
35 | ## Internal name for the AUI manager |
---|
36 | window_name = "Fit page" |
---|
37 | ## Title to appear on top of the window |
---|
38 | window_caption = "Fit Page" |
---|
39 | |
---|
40 | |
---|
41 | def __init__(self, parent,model, *args, **kwargs): |
---|
42 | wx.Panel.__init__(self, parent, *args, **kwargs) |
---|
43 | """ |
---|
44 | Initialization of the Panel |
---|
45 | """ |
---|
46 | self.manager = None |
---|
47 | self.parent = parent |
---|
48 | self.event_owner=None |
---|
49 | #panel interface |
---|
50 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
51 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
52 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
53 | self.sizer2 = wx.GridBagSizer(5,5) |
---|
54 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
55 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
56 | self.static_line_1 = wx.StaticLine(self, -1) |
---|
57 | self.modelbox = wx.ComboBox(self, -1) |
---|
58 | id = wx.NewId() |
---|
59 | self.vbox.Add(self.sizer3) |
---|
60 | self.vbox.Add(self.sizer1) |
---|
61 | self.vbox.Add(self.sizer2) |
---|
62 | self.vbox.Add(self.static_line_1, 0, wx.EXPAND, 0) |
---|
63 | self.vbox.Add(self.sizer5) |
---|
64 | self.vbox.Add(self.sizer4) |
---|
65 | |
---|
66 | id = wx.NewId() |
---|
67 | self.btClose =wx.Button(self,id,'Close') |
---|
68 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
69 | self.btClose.SetToolTipString("Close page.") |
---|
70 | |
---|
71 | ix = 12 |
---|
72 | iy = 1 |
---|
73 | self.sizer4.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
74 | ix = 0 |
---|
75 | iy = 1 |
---|
76 | self.sizer3.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
77 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
78 | ix += 1 |
---|
79 | self.sizer3.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
80 | # contains link between model ,all its parameters, and panel organization |
---|
81 | self.parameters=[] |
---|
82 | #contains link between a model and selected parameters to fit |
---|
83 | self.param_toFit=[] |
---|
84 | # model on which the fit would be performed |
---|
85 | self.model=model |
---|
86 | try: |
---|
87 | print"init modelpage",model.name |
---|
88 | self.set_panel(model) |
---|
89 | except: |
---|
90 | raise |
---|
91 | # preview selected model name |
---|
92 | self.prevmodel_name=model.__class__.__name__ |
---|
93 | self.modelbox.SetValue(self.prevmodel_name) |
---|
94 | # flag to check if the user has selected a new model in the combox box |
---|
95 | self.model_hasChanged=False |
---|
96 | #dictionary of model name and model class |
---|
97 | self.model_list_box={} |
---|
98 | # Data1D to make a deep comparison between 2 Data1D for checking data |
---|
99 | #change |
---|
100 | self.vbox.Layout() |
---|
101 | self.vbox.Fit(self) |
---|
102 | self.SetSizer(self.vbox) |
---|
103 | self.Centre() |
---|
104 | |
---|
105 | |
---|
106 | def onClose(self,event): |
---|
107 | """ close the page associated with this panel""" |
---|
108 | self.GrandParent.onClose() |
---|
109 | |
---|
110 | def set_owner(self,owner): |
---|
111 | """ |
---|
112 | set owner of fitpage |
---|
113 | @param owner: the class responsible of plotting |
---|
114 | """ |
---|
115 | self.event_owner=owner |
---|
116 | |
---|
117 | |
---|
118 | def set_manager(self, manager): |
---|
119 | """ |
---|
120 | set panel manager |
---|
121 | @param manager: instance of plugin fitting |
---|
122 | """ |
---|
123 | self.manager = manager |
---|
124 | |
---|
125 | def populate_box(self, dict): |
---|
126 | """ |
---|
127 | Populate each combox box of each page |
---|
128 | @param page: the page to populate |
---|
129 | """ |
---|
130 | id=0 |
---|
131 | self.model_list_box=dict |
---|
132 | list_name=[] |
---|
133 | for item in self.model_list_box.itervalues(): |
---|
134 | name = item.__name__ |
---|
135 | model=item() |
---|
136 | if hasattr(model, "name"): |
---|
137 | name = model.name |
---|
138 | list_name.append(name) |
---|
139 | list_name.sort() |
---|
140 | for name in list_name: |
---|
141 | self.modelbox.Insert(name,int(id)) |
---|
142 | id+=1 |
---|
143 | wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) |
---|
144 | return 0 |
---|
145 | |
---|
146 | def set_page(self, model,description=None): |
---|
147 | #print " modelpage: set_page was called",model |
---|
148 | self.model=model |
---|
149 | name = self.model.__class__.__name__ |
---|
150 | if hasattr(self.model, "name"): |
---|
151 | name = self.model.name |
---|
152 | |
---|
153 | |
---|
154 | self.modelbox.SetValue(name) |
---|
155 | self.set_panel(self.model) |
---|
156 | self.manager.draw_model(self.model) |
---|
157 | |
---|
158 | def _on_select_model(self,event): |
---|
159 | """ |
---|
160 | react when a model is selected from page's combo box |
---|
161 | post an event to its owner to draw an appropriate theory |
---|
162 | """ |
---|
163 | #print "modelpage: self.model_list_box ",self.model_list_box |
---|
164 | for item in self.model_list_box.itervalues(): |
---|
165 | name = item.__name__ |
---|
166 | items=item() |
---|
167 | if hasattr(items, "name"): |
---|
168 | name = items.name |
---|
169 | print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
170 | if name ==event.GetString(): |
---|
171 | model=items |
---|
172 | print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
173 | self.manager.draw_model(model) |
---|
174 | self.set_panel(model) |
---|
175 | def set_model_name(self,name): |
---|
176 | """ |
---|
177 | set model name. set also self.model_hasChanged to true is the model |
---|
178 | type has changed or false if it didn't |
---|
179 | @param name: model 's name |
---|
180 | """ |
---|
181 | self.model_hasChanged=False |
---|
182 | if (name != self.prevmodel_name): |
---|
183 | self.model_hasChanged=True |
---|
184 | |
---|
185 | self.prevmodel_name=self.modelbox.GetValue() |
---|
186 | |
---|
187 | |
---|
188 | def get_model_box(self): |
---|
189 | """ return reference to combox box self.model""" |
---|
190 | return self.modelbox |
---|
191 | |
---|
192 | |
---|
193 | def get_param_list(self): |
---|
194 | """ |
---|
195 | @return self.param_toFit: list containing references to TextCtrl |
---|
196 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
197 | @raise: if return an empty list of parameter fit will nnote work |
---|
198 | properly so raise ValueError,"missing parameter to fit" |
---|
199 | """ |
---|
200 | if self.param_toFit !=[]: |
---|
201 | return self.param_toFit |
---|
202 | else: |
---|
203 | raise ValueError,"missing parameter to fit" |
---|
204 | |
---|
205 | |
---|
206 | def set_panel(self,model): |
---|
207 | """ |
---|
208 | Build the panel from the model content |
---|
209 | @param model: the model selected in combo box for fitting purpose |
---|
210 | """ |
---|
211 | |
---|
212 | self.sizer2.Clear(True) |
---|
213 | self.sizer1.Clear(True) |
---|
214 | self.sizer5.Clear(True) |
---|
215 | self.parameters = [] |
---|
216 | self.param_toFit=[] |
---|
217 | self.model = model |
---|
218 | keys = self.model.getParamList() |
---|
219 | keys.sort() |
---|
220 | description=None |
---|
221 | if hasattr(self.model,'description'): |
---|
222 | description =model.description |
---|
223 | disp_list=self.model.getDispParamList() |
---|
224 | ip=0 |
---|
225 | iq=1 |
---|
226 | if len(disp_list)>0: |
---|
227 | disp = wx.StaticText(self, -1, 'Dispersion') |
---|
228 | self.sizer5.Add(disp,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
229 | ip += 1 |
---|
230 | values = wx.StaticText(self, -1, 'Values') |
---|
231 | self.sizer5.Add(values,( iq, ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
232 | |
---|
233 | disp_list.sort() |
---|
234 | print "went here",self.model.name,model.description |
---|
235 | iy = 1 |
---|
236 | ix = 0 |
---|
237 | self.cb0 = wx.StaticText(self, -1,'Model Description:') |
---|
238 | self.sizer1.Add(self.cb0,(iy, ix),(1,1),\ |
---|
239 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
240 | iy += 1 |
---|
241 | |
---|
242 | self.cb01 = wx.StaticText(self, -1,str(description),style=wx.ALIGN_LEFT) |
---|
243 | self.cb01.Wrap(400) |
---|
244 | #self.cb01 = wx.StaticText(self, -1,str(description),(45, 25),style=wx.ALIGN_LEFT) |
---|
245 | |
---|
246 | self.sizer1.Add(self.cb01,(iy, ix),(1,1),\ |
---|
247 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
248 | ix = 0 |
---|
249 | iy = 1 |
---|
250 | self.cb1 = wx.StaticText(self, -1,'Parameters') |
---|
251 | self.sizer2.Add(self.cb1,(iy, ix),(1,1),\ |
---|
252 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
253 | ix +=1 |
---|
254 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
255 | self.sizer2.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
256 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
257 | ix +=1 |
---|
258 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
259 | self.sizer2.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
260 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
261 | self.text2_4.Hide() |
---|
262 | for item in keys: |
---|
263 | if not item in disp_list: |
---|
264 | iy += 1 |
---|
265 | ix = 0 |
---|
266 | cb=wx.StaticText(self, -1, item) |
---|
267 | self.sizer2.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
268 | ix += 1 |
---|
269 | value= self.model.getParam(item) |
---|
270 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
271 | ctl1.SetValue(str (format_number(value))) |
---|
272 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
273 | ctl1.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
274 | self.sizer2.Add(ctl1, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
275 | ix +=1 |
---|
276 | |
---|
277 | # Units |
---|
278 | try: |
---|
279 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
280 | except: |
---|
281 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
282 | self.sizer2.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
283 | else: |
---|
284 | ip = 0 |
---|
285 | iq += 1 |
---|
286 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
287 | cb.SetValue(False) |
---|
288 | self.sizer5.Add( cb,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
289 | wx.EVT_CHECKBOX(self, cb.GetId(), self._on_select_model) |
---|
290 | |
---|
291 | ip += 1 |
---|
292 | value= self.model.getParam(item) |
---|
293 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
294 | ctl1.SetValue(str (format_number(value))) |
---|
295 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
296 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
297 | self.sizer5.Add(ctl1, (iq,ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
298 | #save data |
---|
299 | self.parameters.append([cb,ctl1]) |
---|
300 | iy+=1 |
---|
301 | self.sizer2.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
302 | |
---|
303 | #Display units text on panel |
---|
304 | for item in keys: |
---|
305 | if self.model.details[item][0]!='': |
---|
306 | self.text2_4.Show() |
---|
307 | break |
---|
308 | else: |
---|
309 | self.text2_4.Hide() |
---|
310 | self.vbox.Layout() |
---|
311 | self.GrandParent.GetSizer().Layout() |
---|
312 | |
---|
313 | |
---|
314 | def _onparamEnter(self,event): |
---|
315 | """ |
---|
316 | when enter value on panel redraw model according to changed |
---|
317 | """ |
---|
318 | self.set_model_parameter() |
---|
319 | |
---|
320 | def set_model_parameter(self): |
---|
321 | if len(self.parameters) !=0 and self.model !=None: |
---|
322 | for item in self.parameters: |
---|
323 | try: |
---|
324 | name=str(item[0].GetLabelText()) |
---|
325 | value= float(item[1].GetValue()) |
---|
326 | self.model.setParam(name,value) |
---|
327 | except: |
---|
328 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
329 | "Model Drawing Error:wrong value entered : %s"% sys.exc_value)) |
---|
330 | self.manager.draw_model(self.model) |
---|
331 | |
---|