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