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