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