[d89f09b] | 1 | import sys |
---|
| 2 | import wx |
---|
| 3 | import wx.lib |
---|
[442895f] | 4 | import numpy,math |
---|
[d89f09b] | 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 | |
---|
[f39511b] | 27 | class FitPage2D(wx.ScrolledWindow): |
---|
[d89f09b] | 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 | |
---|
[9d31a8b] | 41 | def __init__(self, parent,data, *args, **kwargs): |
---|
[f39511b] | 42 | wx.ScrolledWindow.__init__(self, parent, *args, **kwargs) |
---|
[d89f09b] | 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) |
---|
[44bbf6a] | 51 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
[d89f09b] | 52 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
| 53 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
| 54 | self.sizer2 = wx.GridBagSizer(5,5) |
---|
| 55 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
[44bbf6a] | 56 | #self.DataSource = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 57 | #self.DataSource.SetToolTipString("name of data to fit") |
---|
| 58 | #self.DataSource.SetValue(str(data.name)) |
---|
| 59 | #self.DataSource.Disable() |
---|
| 60 | self.DataSource =wx.StaticText(self, -1,str(data.name)) |
---|
[d89f09b] | 61 | self.modelbox = wx.ComboBox(self, -1) |
---|
| 62 | id = wx.NewId() |
---|
| 63 | self.btFit =wx.Button(self,id,'Fit') |
---|
| 64 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
| 65 | self.btFit.SetToolTipString("Perform fit.") |
---|
[44bbf6a] | 66 | self.static_line_1 = wx.StaticLine(self, -1) |
---|
| 67 | |
---|
[d89f09b] | 68 | self.vbox.Add(self.sizer3) |
---|
| 69 | self.vbox.Add(self.sizer2) |
---|
[44bbf6a] | 70 | self.vbox.Add(self.static_line_1, 0, wx.EXPAND, 0) |
---|
| 71 | self.vbox.Add(self.sizer5) |
---|
[d89f09b] | 72 | self.vbox.Add(self.sizer4) |
---|
| 73 | self.vbox.Add(self.sizer1) |
---|
| 74 | |
---|
[442895f] | 75 | id = wx.NewId() |
---|
| 76 | self.btClose =wx.Button(self,id,'Close') |
---|
| 77 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
| 78 | self.btClose.SetToolTipString("Close page.") |
---|
[d89f09b] | 79 | ix = 0 |
---|
| 80 | iy = 1 |
---|
| 81 | self.sizer3.Add(wx.StaticText(self, -1, 'Data Source'),(iy,ix),\ |
---|
| 82 | (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 83 | ix += 1 |
---|
| 84 | self.sizer3.Add(self.DataSource,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 85 | ix += 1 |
---|
| 86 | self.sizer3.Add((20,20),(iy,ix),(1,1),wx.RIGHT|wx.EXPAND|wx.ADJUST_MINSIZE,0) |
---|
| 87 | ix = 0 |
---|
| 88 | iy += 1 |
---|
| 89 | self.sizer3.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
| 90 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 91 | ix += 1 |
---|
| 92 | self.sizer3.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[9d31a8b] | 93 | |
---|
| 94 | ix = 0 |
---|
[d89f09b] | 95 | iy = 1 |
---|
[9d31a8b] | 96 | #set maximum range for x in linear scale |
---|
| 97 | self.text4_3 = wx.StaticText(self, -1, 'Maximum Data\n Range (Linear)', style=wx.ALIGN_LEFT) |
---|
| 98 | self.sizer4.Add(self.text4_3,(iy,ix),(1,1),\ |
---|
| 99 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 100 | ix += 1 |
---|
[d89f09b] | 101 | self.text4_1 = wx.StaticText(self, -1, 'Min') |
---|
| 102 | self.sizer4.Add(self.text4_1,(iy, ix),(1,1),\ |
---|
| 103 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[e1a310f] | 104 | #self.text4_1.Hide() |
---|
[d89f09b] | 105 | ix += 2 |
---|
| 106 | self.text4_2 = wx.StaticText(self, -1, 'Max') |
---|
| 107 | self.sizer4.Add(self.text4_2,(iy, ix),(1,1),\ |
---|
| 108 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[e1a310f] | 109 | #self.text4_2.Hide() |
---|
[9d31a8b] | 110 | |
---|
| 111 | #self.text4_3.Hide() |
---|
[d89f09b] | 112 | ix = 0 |
---|
| 113 | iy += 1 |
---|
[9d31a8b] | 114 | self.text4_4 = wx.StaticText(self, -1, 'x range') |
---|
| 115 | self.sizer4.Add(self.text4_4,(iy, ix),(1,1),\ |
---|
| 116 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[d89f09b] | 117 | ix += 1 |
---|
| 118 | self.xmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
[9d31a8b] | 119 | self.xmin.SetValue(format_number(data.xmin)) |
---|
[d89f09b] | 120 | self.xmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
| 121 | self.sizer4.Add(self.xmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 122 | self.xmin.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
[55fd102] | 123 | self.xmin.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
[e1a310f] | 124 | |
---|
[d89f09b] | 125 | ix += 2 |
---|
| 126 | self.xmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
[9d31a8b] | 127 | self.xmax.SetValue(format_number(data.xmax)) |
---|
[d89f09b] | 128 | self.xmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
| 129 | self.sizer4.Add(self.xmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 130 | self.xmax.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
[55fd102] | 131 | self.xmax.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
[e1a310f] | 132 | |
---|
| 133 | iy +=1 |
---|
[9d31a8b] | 134 | ix = 0 |
---|
| 135 | self.text4_5 = wx.StaticText(self, -1, 'y range') |
---|
| 136 | self.sizer4.Add(self.text4_5,(iy, ix),(1,1),\ |
---|
| 137 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 138 | ix += 1 |
---|
[e1a310f] | 139 | self.ymin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
[9d31a8b] | 140 | self.ymin.SetValue(format_number(data.ymin)) |
---|
[e1a310f] | 141 | self.ymin.SetToolTipString("Minimun value of y in linear scale.") |
---|
| 142 | self.sizer4.Add(self.ymin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 143 | self.ymin.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
| 144 | self.ymin.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
| 145 | |
---|
| 146 | ix += 2 |
---|
| 147 | self.ymax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
[9d31a8b] | 148 | self.ymax.SetValue(format_number(data.ymax)) |
---|
[e1a310f] | 149 | self.ymax.SetToolTipString("Maximum value of y in linear scale.") |
---|
| 150 | self.sizer4.Add(self.ymax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 151 | self.ymax.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
| 152 | self.ymax.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
| 153 | |
---|
[d89f09b] | 154 | #Set chisqr result into TextCtrl |
---|
| 155 | ix = 0 |
---|
| 156 | iy = 1 |
---|
| 157 | self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT) |
---|
| 158 | self.sizer1.Add(self.text1_1,(iy,ix),(1,1),\ |
---|
| 159 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 160 | ix += 1 |
---|
| 161 | self.tcChi = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 162 | self.tcChi.SetToolTipString("Chi^2 over degrees of freedom.") |
---|
| 163 | self.sizer1.Add(self.tcChi,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 164 | ix +=2 |
---|
| 165 | self.sizer1.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[2dbb681] | 166 | ix+= 1 |
---|
[442895f] | 167 | self.sizer1.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[2dbb681] | 168 | ix= 1 |
---|
| 169 | iy+=1 |
---|
| 170 | self.sizer1.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[d89f09b] | 171 | # contains link between model ,all its parameters, and panel organization |
---|
| 172 | self.parameters=[] |
---|
| 173 | #contains link between a model and selected parameters to fit |
---|
| 174 | self.param_toFit=[] |
---|
| 175 | # model on which the fit would be performed |
---|
| 176 | self.model=None |
---|
| 177 | # preview selected model name |
---|
[9d31a8b] | 178 | |
---|
[6f73a08] | 179 | |
---|
[d89f09b] | 180 | #dictionary of model name and model class |
---|
| 181 | self.model_list_box={} |
---|
[9d31a8b] | 182 | |
---|
| 183 | self.data = data |
---|
[d89f09b] | 184 | self.vbox.Layout() |
---|
| 185 | self.vbox.Fit(self) |
---|
| 186 | self.SetSizer(self.vbox) |
---|
[f39511b] | 187 | self.SetScrollbars(20,20,55,40) |
---|
[d89f09b] | 188 | self.Centre() |
---|
| 189 | |
---|
| 190 | |
---|
| 191 | def set_owner(self,owner): |
---|
| 192 | """ |
---|
| 193 | set owner of fitpage |
---|
| 194 | @param owner: the class responsible of plotting |
---|
| 195 | """ |
---|
| 196 | self.event_owner=owner |
---|
| 197 | |
---|
| 198 | |
---|
| 199 | def set_manager(self, manager): |
---|
| 200 | """ |
---|
| 201 | set panel manager |
---|
| 202 | @param manager: instance of plugin fitting |
---|
| 203 | """ |
---|
| 204 | self.manager = manager |
---|
| 205 | |
---|
| 206 | |
---|
[442895f] | 207 | def onClose(self,event): |
---|
[00561739] | 208 | """ close the page associated with this panel""" |
---|
[442895f] | 209 | self.GrandParent.onClose() |
---|
| 210 | |
---|
| 211 | |
---|
| 212 | def compute_chisqr(self): |
---|
| 213 | """ @param fn: function that return model value |
---|
| 214 | @return residuals |
---|
| 215 | """ |
---|
| 216 | flag=self.checkFitRange() |
---|
[9d31a8b] | 217 | res=[] |
---|
[442895f] | 218 | if flag== True: |
---|
[948add7] | 219 | try: |
---|
[9d31a8b] | 220 | xmin = float(self.xmin.GetValue()) |
---|
| 221 | xmax = float(self.xmax.GetValue()) |
---|
| 222 | ymin = float(self.ymin.GetValue()) |
---|
| 223 | ymax = float(self.ymax.GetValue()) |
---|
| 224 | |
---|
| 225 | for i in range(len(self.data.y_bins)): |
---|
| 226 | if self.data.y_bins[i]>= ymin and self.data.y_bins[i]<= ymax: |
---|
| 227 | for j in range(len(self.data.x_bins)): |
---|
| 228 | if self.data.x_bins[j]>= xmin and self.data.x_bins[j]<= xmax: |
---|
[57668f8] | 229 | res.append( (self.data.data[j][i]- self.model.runXY(\ |
---|
[9d31a8b] | 230 | [self.data.x_bins[j],self.data.y_bins[i]]))\ |
---|
[57668f8] | 231 | /self.data.err_data[j][i] ) |
---|
[948add7] | 232 | sum=0 |
---|
[9d31a8b] | 233 | |
---|
[948add7] | 234 | for item in res: |
---|
| 235 | if numpy.isfinite(item): |
---|
| 236 | sum +=item |
---|
| 237 | self.tcChi.SetValue(format_number(math.fabs(sum))) |
---|
| 238 | except: |
---|
| 239 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 240 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
[442895f] | 241 | |
---|
| 242 | |
---|
[d89f09b] | 243 | def onFit(self,event): |
---|
| 244 | """ signal for fitting""" |
---|
| 245 | flag=self.checkFitRange() |
---|
| 246 | self.set_manager(self.manager) |
---|
| 247 | |
---|
| 248 | qmin=float(self.xmin.GetValue()) |
---|
| 249 | qmax =float( self.xmax.GetValue()) |
---|
| 250 | if len(self.param_toFit) >0 and flag==True: |
---|
[948add7] | 251 | self.manager.schedule_for_fit( value=1,fitproblem =None) |
---|
[d89f09b] | 252 | self.manager._on_single_fit(qmin=qmin,qmax=qmax) |
---|
| 253 | else: |
---|
[e1a310f] | 254 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
[d89f09b] | 255 | "Select at least on parameter to fit ")) |
---|
| 256 | def populate_box(self, dict): |
---|
| 257 | """ |
---|
| 258 | Populate each combox box of each page |
---|
| 259 | @param page: the page to populate |
---|
| 260 | """ |
---|
| 261 | id=0 |
---|
| 262 | self.model_list_box=dict |
---|
[49b7efa] | 263 | list_name=[] |
---|
| 264 | for item in self.model_list_box.itervalues(): |
---|
[00561739] | 265 | name = item.__name__ |
---|
[d89f09b] | 266 | if hasattr(item, "name"): |
---|
| 267 | name = item.name |
---|
[49b7efa] | 268 | list_name.append(name) |
---|
| 269 | list_name.sort() |
---|
| 270 | for name in list_name: |
---|
[d89f09b] | 271 | self.modelbox.Insert(name,int(id)) |
---|
| 272 | id+=1 |
---|
[49b7efa] | 273 | wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) |
---|
[d89f09b] | 274 | return 0 |
---|
| 275 | |
---|
| 276 | |
---|
| 277 | def _on_select_model(self,event): |
---|
| 278 | """ |
---|
| 279 | react when a model is selected from page's combo box |
---|
| 280 | post an event to its owner to draw an appropriate theory |
---|
| 281 | """ |
---|
[d171299] | 282 | self.btFit.SetFocus() |
---|
[d89f09b] | 283 | for item in self.model_list_box.itervalues(): |
---|
[00561739] | 284 | name = item.__name__ |
---|
| 285 | if hasattr(item, "name"): |
---|
| 286 | name = item.name |
---|
| 287 | #print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
| 288 | if name ==event.GetString(): |
---|
| 289 | try: |
---|
| 290 | evt = ModelEventbox(model=item(),name=name) |
---|
[d89f09b] | 291 | wx.PostEvent(self.event_owner, evt) |
---|
[00561739] | 292 | except: |
---|
| 293 | raise #ValueError,"model.name is not equal to model class name" |
---|
| 294 | break |
---|
[d89f09b] | 295 | |
---|
| 296 | def _onTextEnter(self,event): |
---|
| 297 | """ |
---|
| 298 | set a flag to determine if the fitting range entered by the user is valid |
---|
| 299 | """ |
---|
[442895f] | 300 | |
---|
[d89f09b] | 301 | try: |
---|
| 302 | flag=self.checkFitRange() |
---|
| 303 | if flag==True and self.model!=None: |
---|
[6f73a08] | 304 | print"fit page",self.xmin.GetValue(),self.xmax.GetValue() |
---|
| 305 | self.manager.redraw_model(float(self.xmin.GetValue())\ |
---|
[d89f09b] | 306 | ,float(self.xmax.GetValue())) |
---|
| 307 | except: |
---|
[6f73a08] | 308 | |
---|
[d89f09b] | 309 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 310 | "Drawing Error:wrong value entered %s"% sys.exc_value)) |
---|
| 311 | |
---|
| 312 | def checkFitRange(self): |
---|
| 313 | """ |
---|
| 314 | Check the validity of fitting range |
---|
| 315 | @note: xmin should always be less than xmax or else each control box |
---|
| 316 | background is colored in pink. |
---|
| 317 | """ |
---|
| 318 | |
---|
| 319 | flag = True |
---|
| 320 | valueMin = self.xmin.GetValue() |
---|
| 321 | valueMax = self.xmax.GetValue() |
---|
| 322 | # Check for possible values entered |
---|
[6f73a08] | 323 | print "fitpage: checkfitrange:",valueMin,valueMax |
---|
[d89f09b] | 324 | try: |
---|
| 325 | if (float(valueMax)> float(valueMin)): |
---|
| 326 | self.xmax.SetBackgroundColour(wx.WHITE) |
---|
| 327 | self.xmin.SetBackgroundColour(wx.WHITE) |
---|
| 328 | else: |
---|
| 329 | flag = False |
---|
| 330 | self.xmin.SetBackgroundColour("pink") |
---|
| 331 | self.xmax.SetBackgroundColour("pink") |
---|
| 332 | except: |
---|
| 333 | flag = False |
---|
| 334 | self.xmin.SetBackgroundColour("pink") |
---|
| 335 | self.xmax.SetBackgroundColour("pink") |
---|
| 336 | |
---|
| 337 | self.xmin.Refresh() |
---|
| 338 | self.xmax.Refresh() |
---|
| 339 | return flag |
---|
| 340 | |
---|
[9d31a8b] | 341 | |
---|
[d89f09b] | 342 | def get_model_box(self): |
---|
| 343 | """ return reference to combox box self.model""" |
---|
| 344 | return self.modelbox |
---|
| 345 | |
---|
| 346 | |
---|
| 347 | def get_param_list(self): |
---|
| 348 | """ |
---|
| 349 | @return self.param_toFit: list containing references to TextCtrl |
---|
| 350 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
| 351 | @raise: if return an empty list of parameter fit will nnote work |
---|
| 352 | properly so raise ValueError,"missing parameter to fit" |
---|
| 353 | """ |
---|
| 354 | if self.param_toFit !=[]: |
---|
| 355 | return self.param_toFit |
---|
| 356 | else: |
---|
| 357 | raise ValueError,"missing parameter to fit" |
---|
| 358 | |
---|
| 359 | |
---|
| 360 | def set_panel(self,model): |
---|
| 361 | """ |
---|
| 362 | Build the panel from the model content |
---|
| 363 | @param model: the model selected in combo box for fitting purpose |
---|
| 364 | """ |
---|
| 365 | self.sizer2.Clear(True) |
---|
[d171299] | 366 | self.sizer5.Clear(True) |
---|
[d89f09b] | 367 | self.parameters = [] |
---|
| 368 | self.param_toFit=[] |
---|
| 369 | self.model = model |
---|
| 370 | keys = self.model.getParamList() |
---|
[04edd0d] | 371 | #print "fitpage1D : dispersion list",self.model.getDispParamList() |
---|
[d89f09b] | 372 | keys.sort() |
---|
[44bbf6a] | 373 | disp_list=self.model.getDispParamList() |
---|
[04edd0d] | 374 | ip=0 |
---|
| 375 | iq=1 |
---|
| 376 | if len(disp_list)>0: |
---|
| 377 | disp = wx.StaticText(self, -1, 'Dispersion') |
---|
| 378 | self.sizer5.Add(disp,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 379 | ip += 1 |
---|
| 380 | values = wx.StaticText(self, -1, 'Values') |
---|
| 381 | self.sizer5.Add(values,( iq, ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 382 | |
---|
[44bbf6a] | 383 | disp_list.sort() |
---|
[d89f09b] | 384 | iy = 1 |
---|
| 385 | ix = 0 |
---|
| 386 | self.cb1 = wx.CheckBox(self, -1,'Parameters', (10, 10)) |
---|
| 387 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
| 388 | self.sizer2.Add(self.cb1,(iy, ix),(1,1),\ |
---|
| 389 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 390 | ix +=1 |
---|
| 391 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
| 392 | self.sizer2.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
| 393 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 394 | ix +=2 |
---|
| 395 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
| 396 | self.sizer2.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
| 397 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 398 | self.text2_3.Hide() |
---|
| 399 | ix +=1 |
---|
| 400 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
| 401 | self.sizer2.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
| 402 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 403 | self.text2_4.Hide() |
---|
[04edd0d] | 404 | |
---|
[d89f09b] | 405 | for item in keys: |
---|
[44bbf6a] | 406 | if not item in disp_list: |
---|
| 407 | iy += 1 |
---|
| 408 | ix = 0 |
---|
| 409 | |
---|
| 410 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
| 411 | cb.SetValue(False) |
---|
| 412 | self.sizer2.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 413 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
| 414 | |
---|
| 415 | ix += 1 |
---|
| 416 | value= self.model.getParam(item) |
---|
| 417 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 418 | ctl1.SetValue(str (format_number(value))) |
---|
| 419 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 420 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
| 421 | self.sizer2.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
| 422 | ix += 1 |
---|
| 423 | text2=wx.StaticText(self, -1, '+/-') |
---|
| 424 | self.sizer2.Add(text2,(iy, ix),(1,1),\ |
---|
| 425 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 426 | text2.Hide() |
---|
| 427 | ix += 1 |
---|
| 428 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 429 | self.sizer2.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 430 | ctl2.Hide() |
---|
| 431 | ix +=1 |
---|
| 432 | # Units |
---|
| 433 | try: |
---|
| 434 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
| 435 | except: |
---|
| 436 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
| 437 | self.sizer2.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[04edd0d] | 438 | else: |
---|
| 439 | ip = 0 |
---|
| 440 | iq += 1 |
---|
| 441 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
| 442 | cb.SetValue(False) |
---|
| 443 | self.sizer5.Add( cb,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 444 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
| 445 | |
---|
| 446 | ip += 1 |
---|
| 447 | value= self.model.getParam(item) |
---|
| 448 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 449 | ctl1.SetValue(str (format_number(value))) |
---|
| 450 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 451 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
| 452 | self.sizer5.Add(ctl1, (iq,ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 453 | |
---|
| 454 | #save data |
---|
| 455 | self.parameters.append([cb,ctl1,text2,ctl2]) |
---|
| 456 | |
---|
[44bbf6a] | 457 | iy+=1 |
---|
| 458 | self.sizer2.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[d171299] | 459 | |
---|
[d89f09b] | 460 | #Display units text on panel |
---|
| 461 | for item in keys: |
---|
| 462 | if self.model.details[item][0]!='': |
---|
| 463 | self.text2_4.Show() |
---|
| 464 | break |
---|
| 465 | else: |
---|
| 466 | self.text2_4.Hide() |
---|
| 467 | #Disable or enable fit button |
---|
[44bbf6a] | 468 | |
---|
| 469 | if not (len(self.param_toFit ) >0): |
---|
[d89f09b] | 470 | self.xmin.Disable() |
---|
| 471 | self.xmax.Disable() |
---|
[e1a310f] | 472 | self.ymin.Disable() |
---|
| 473 | self.ymax.Disable() |
---|
[44bbf6a] | 474 | else: |
---|
| 475 | self.xmin.Enable() |
---|
| 476 | self.xmax.Enable() |
---|
| 477 | self.ymin.Enable() |
---|
| 478 | self.ymax.Enable() |
---|
| 479 | |
---|
[442895f] | 480 | self.compute_chisqr() |
---|
[d89f09b] | 481 | self.vbox.Layout() |
---|
| 482 | self.GrandParent.GetSizer().Layout() |
---|
| 483 | |
---|
| 484 | |
---|
| 485 | |
---|
| 486 | def _onparamEnter(self,event): |
---|
| 487 | """ |
---|
| 488 | when enter value on panel redraw model according to changed |
---|
| 489 | """ |
---|
[1b07935d] | 490 | self.set_model_parameter() |
---|
[442895f] | 491 | self.compute_chisqr() |
---|
| 492 | |
---|
[1b07935d] | 493 | def set_model_parameter(self): |
---|
[442895f] | 494 | """ |
---|
| 495 | this method redraws the model according to parameters values changes |
---|
| 496 | and the reset model according to paramaters changes |
---|
| 497 | """ |
---|
[d89f09b] | 498 | if len(self.parameters) !=0 and self.model !=None: |
---|
| 499 | for item in self.parameters: |
---|
[db709e4] | 500 | |
---|
| 501 | is_modified = False |
---|
| 502 | if self.xmin.IsModified(): |
---|
| 503 | is_modified = True |
---|
| 504 | if self.xmax.IsModified(): |
---|
| 505 | is_modified = True |
---|
| 506 | |
---|
| 507 | try: |
---|
| 508 | |
---|
| 509 | if item[1].IsModified() or is_modified: |
---|
| 510 | print str(item[0].GetLabelText()),item[1].IsModified() |
---|
| 511 | item[1].SetModified(False) |
---|
| 512 | name=str(item[0].GetLabelText()) |
---|
| 513 | value= float(item[1].GetValue()) |
---|
| 514 | self.model.setParam(name,value) |
---|
| 515 | self.xmin.SetModified(False) |
---|
| 516 | self.xmax.SetModified(False) |
---|
| 517 | is_modified=False |
---|
| 518 | |
---|
| 519 | self.manager.redraw_model( |
---|
| 520 | float(self.xmin.GetValue()), |
---|
| 521 | float(self.xmax.GetValue()) ) |
---|
| 522 | |
---|
[d89f09b] | 523 | except: |
---|
| 524 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
[db709e4] | 525 | "Model Drawing Error:wrong value entered : %s"% sys.exc_value)) |
---|
| 526 | |
---|
| 527 | |
---|
[d89f09b] | 528 | def select_all_param(self,event): |
---|
| 529 | """ |
---|
| 530 | set to true or false all checkBox given the main checkbox value cb1 |
---|
| 531 | """ |
---|
| 532 | self.param_toFit=[] |
---|
| 533 | if self.parameters !=[]: |
---|
| 534 | if self.cb1.GetValue()==True: |
---|
| 535 | for item in self.parameters: |
---|
| 536 | item[0].SetValue(True) |
---|
| 537 | list= [item[0],item[1],item[2],item[3]] |
---|
| 538 | self.param_toFit.append(list ) |
---|
[9d31a8b] | 539 | |
---|
| 540 | if not (len(self.param_toFit ) >0): |
---|
| 541 | self.xmin.Disable() |
---|
| 542 | self.xmax.Disable() |
---|
| 543 | self.ymin.Disable() |
---|
| 544 | self.ymax.Disable() |
---|
| 545 | else: |
---|
| 546 | self.xmin.Enable() |
---|
| 547 | self.xmax.Enable() |
---|
| 548 | self.ymin.Enable() |
---|
| 549 | self.ymax.Enable() |
---|
[d89f09b] | 550 | else: |
---|
| 551 | for item in self.parameters: |
---|
| 552 | item[0].SetValue(False) |
---|
| 553 | self.param_toFit=[] |
---|
| 554 | |
---|
| 555 | self.xmin.Disable() |
---|
| 556 | self.xmax.Disable() |
---|
[e1a310f] | 557 | self.ymin.Disable() |
---|
| 558 | self.ymax.Disable() |
---|
[d89f09b] | 559 | |
---|
| 560 | |
---|
| 561 | def select_param(self,event): |
---|
| 562 | """ |
---|
| 563 | Select TextCtrl checked for fitting purpose and stores them |
---|
| 564 | in self.param_toFit=[] list |
---|
| 565 | """ |
---|
| 566 | self.param_toFit=[] |
---|
| 567 | for item in self.parameters: |
---|
| 568 | if item[0].GetValue()==True: |
---|
| 569 | list= [item[0],item[1],item[2],item[3]] |
---|
| 570 | self.param_toFit.append(list ) |
---|
| 571 | else: |
---|
| 572 | if item in self.param_toFit: |
---|
| 573 | self.param_toFit.remove(item) |
---|
| 574 | if len(self.parameters)==len(self.param_toFit): |
---|
| 575 | self.cb1.SetValue(True) |
---|
| 576 | else: |
---|
| 577 | self.cb1.SetValue(False) |
---|
[9d31a8b] | 578 | |
---|
| 579 | if not (len(self.param_toFit ) >0): |
---|
[d89f09b] | 580 | self.xmin.Disable() |
---|
| 581 | self.xmax.Disable() |
---|
[e1a310f] | 582 | self.ymin.Disable() |
---|
| 583 | self.ymax.Disable() |
---|
[9d31a8b] | 584 | else: |
---|
| 585 | self.xmin.Enable() |
---|
| 586 | self.xmax.Enable() |
---|
| 587 | self.ymin.Enable() |
---|
| 588 | self.ymax.Enable() |
---|
| 589 | |
---|
[d89f09b] | 590 | |
---|
| 591 | |
---|
| 592 | |
---|
| 593 | def onsetValues(self,chisqr, out,cov): |
---|
| 594 | """ |
---|
| 595 | Build the panel from the fit result |
---|
| 596 | @param chisqr:Value of the goodness of fit metric |
---|
| 597 | @param out:list of parameter with the best value found during fitting |
---|
| 598 | @param cov:Covariance matrix |
---|
| 599 | |
---|
| 600 | """ |
---|
| 601 | #print "fitting : onsetvalues out",out |
---|
| 602 | self.tcChi.Clear() |
---|
| 603 | self.tcChi.SetValue(format_number(chisqr)) |
---|
| 604 | params = {} |
---|
| 605 | is_modified = False |
---|
| 606 | has_error = False |
---|
| 607 | if out.__class__==numpy.float64: |
---|
| 608 | self.param_toFit[0][1].SetValue(format_number(out)) |
---|
| 609 | self.param_toFit[0][1].Refresh() |
---|
| 610 | if cov !=None : |
---|
| 611 | self.text2_3.Show() |
---|
| 612 | self.param_toFit[0][2].Show() |
---|
| 613 | self.param_toFit[0][3].Clear() |
---|
| 614 | self.param_toFit[0][3].SetValue(format_number(cov[0])) |
---|
| 615 | self.param_toFit[0][3].Show() |
---|
| 616 | #out is a list : set parameters and errors in TextCtrl |
---|
| 617 | else: |
---|
| 618 | i=0 |
---|
| 619 | #print "fitpage: list param model",list |
---|
| 620 | #for item in self.param_toFit: |
---|
| 621 | # print "fitpage: list display",item[0].GetLabelText() |
---|
| 622 | for item in self.param_toFit: |
---|
| 623 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
[442895f] | 624 | #item[1].SetValue(format_number(out[i])) |
---|
| 625 | item[1].SetValue(format_number(self.model.getParam(item[0].GetLabelText()))) |
---|
[d89f09b] | 626 | item[1].Refresh() |
---|
| 627 | if (cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
| 628 | self.text2_3.Show() |
---|
| 629 | item[2].Show() |
---|
| 630 | item[3].Clear() |
---|
| 631 | item[3].SetValue(format_number(cov[i])) |
---|
| 632 | item[3].Show() |
---|
| 633 | i+=1 |
---|
| 634 | |
---|
| 635 | self.vbox.Layout() |
---|
| 636 | self.GrandParent.GetSizer().Layout() |
---|
| 637 | |
---|