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