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