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