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