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