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