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