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