[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") |
---|
[44999f3] | 182 | # Reversed to the codes; Need to think carefully about consistency in q between 2D plot and fitting |
---|
[26bf293] | 183 | if not hasattr(self.data,"data"): |
---|
| 184 | self.qmin_x= numpy.min(self.data.x) |
---|
| 185 | self.qmax_x= numpy.max(self.data.x) |
---|
| 186 | else: |
---|
[44999f3] | 187 | # Reversed to the codes; Need to think carefully about consistency in q between 2D plot and fitting |
---|
[c4550b0] | 188 | radius1= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymin, 2)) |
---|
| 189 | radius2= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymin, 2)) |
---|
| 190 | radius3= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymax, 2)) |
---|
| 191 | radius4= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymax, 2)) |
---|
| 192 | self.qmin_x =0 |
---|
| 193 | self.qmax_x= max(radius1, radius2, radius3, radius4) |
---|
| 194 | #self.qmin_x= self.data.xmin |
---|
| 195 | #self.qmax_x= self.data.xmax |
---|
[26bf293] | 196 | print "data2D range",self.qmax_x |
---|
[44999f3] | 197 | |
---|
[26bf293] | 198 | self.num_points= 100 |
---|
| 199 | |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | self.qmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 203 | self.qmin.SetValue(str(format_number(self.qmin_x))) |
---|
| 204 | self.qmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
| 205 | self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 206 | self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 207 | self.qmin.Disable() |
---|
| 208 | |
---|
| 209 | self.qmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 210 | self.qmax.SetValue(str(format_number(self.qmax_x))) |
---|
| 211 | self.qmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
| 212 | self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 213 | self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 214 | self.qmax.Disable() |
---|
| 215 | |
---|
| 216 | self.npts = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 217 | self.npts.SetValue(format_number(self.num_points)) |
---|
| 218 | self.npts.SetToolTipString("Number of point to plot.") |
---|
| 219 | self.npts.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 220 | self.npts.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 221 | self.npts.Disable() |
---|
| 222 | self.npts.Hide() |
---|
| 223 | ix = 0 |
---|
| 224 | iy = 1 |
---|
| 225 | self.sizer9.Add(wx.StaticText(self, -1, 'Fitting Range'),(iy, ix),(1,1),\ |
---|
[d89f09b] | 226 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[26bf293] | 227 | ix += 1 |
---|
| 228 | self.sizer9.Add(wx.StaticText(self, -1, 'Min'),(iy, ix),(1,1),\ |
---|
[d89f09b] | 229 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[26bf293] | 230 | ix += 1 |
---|
| 231 | self.sizer9.Add(wx.StaticText(self, -1, 'Max'),(iy, ix),(1,1),\ |
---|
| 232 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 233 | #ix += 1 |
---|
| 234 | #self.sizer9.Add(wx.StaticText(self, -1, 'Npts'),(iy, ix),(1,1),\ |
---|
| 235 | # wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[d89f09b] | 236 | ix = 0 |
---|
| 237 | iy += 1 |
---|
[e5af88b] | 238 | self.sizer9.Add(wx.StaticText(self, -1, 'Q range'),(iy, ix),(1,1),\ |
---|
[9d31a8b] | 239 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[d89f09b] | 240 | ix += 1 |
---|
[26bf293] | 241 | self.sizer9.Add(self.qmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 242 | ix += 1 |
---|
| 243 | self.sizer9.Add(self.qmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 244 | ix += 1 |
---|
| 245 | self.sizer9.Add(self.npts,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[94999eb] | 246 | |
---|
[d89f09b] | 247 | ix += 1 |
---|
[26bf293] | 248 | self.sizer9.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 249 | ix =0 |
---|
| 250 | iy+=1 |
---|
| 251 | self.sizer9.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 252 | #----------sizer 10 draw------------------------------------------------------ |
---|
| 253 | id = wx.NewId() |
---|
| 254 | self.btClose =wx.Button(self,id,'Close') |
---|
| 255 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
| 256 | self.btClose.SetToolTipString("Close page.") |
---|
| 257 | |
---|
| 258 | ix= 3 |
---|
| 259 | iy= 1 |
---|
| 260 | self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 261 | ix +=1 |
---|
| 262 | self.sizer10.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 263 | ix =0 |
---|
[2dbb681] | 264 | iy+=1 |
---|
[26bf293] | 265 | self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 266 | |
---|
[d89f09b] | 267 | # contains link between model ,all its parameters, and panel organization |
---|
| 268 | self.parameters=[] |
---|
[08b9c6c8] | 269 | self.fixed_param=[] |
---|
[6b44403] | 270 | self.fittable_param=[] |
---|
[26bf293] | 271 | #list of dispersion paramaters |
---|
[7a0fb4e] | 272 | self.disp_list=[] |
---|
[d89f09b] | 273 | #contains link between a model and selected parameters to fit |
---|
| 274 | self.param_toFit=[] |
---|
| 275 | # model on which the fit would be performed |
---|
| 276 | self.model=None |
---|
| 277 | #dictionary of model name and model class |
---|
| 278 | self.model_list_box={} |
---|
[26bf293] | 279 | |
---|
| 280 | |
---|
[32673ac] | 281 | |
---|
[f39511b] | 282 | self.vbox.Layout() |
---|
| 283 | self.vbox.Fit(self) |
---|
| 284 | self.SetSizer(self.vbox) |
---|
| 285 | self.SetScrollbars(20,20,55,40) |
---|
| 286 | |
---|
[d89f09b] | 287 | self.Centre() |
---|
[b5847bd8] | 288 | self.Layout() |
---|
| 289 | self.GrandParent.GetSizer().Layout() |
---|
[26bf293] | 290 | |
---|
| 291 | def compute_chisqr2D(self): |
---|
[442895f] | 292 | """ @param fn: function that return model value |
---|
| 293 | @return residuals |
---|
| 294 | """ |
---|
| 295 | flag=self.checkFitRange() |
---|
[26bf293] | 296 | res=[] |
---|
[442895f] | 297 | if flag== True: |
---|
[948add7] | 298 | try: |
---|
[26bf293] | 299 | self.qmin_x = float(self.qmin.GetValue()) |
---|
| 300 | self.qmax_x = float(self.qmax.GetValue()) |
---|
[bcd6d51] | 301 | |
---|
[26bf293] | 302 | for i in range(len(self.data.x_bins)): |
---|
| 303 | if self.data.x_bins[i]>= self.qmin_x and self.data.x_bins[i]<= self.qmax_x: |
---|
| 304 | for j in range(len(self.data.y_bins)): |
---|
| 305 | if self.data.y_bins[j]>= self.qmin_x and self.data.y_bins[j]<= self.qmax_x: |
---|
| 306 | res.append( (self.data.data[j][i]- self.model.runXY(\ |
---|
| 307 | [self.data.x_bins[i],self.data.y_bins[j]]))\ |
---|
| 308 | /self.data.err_data[j][i] ) |
---|
[948add7] | 309 | sum=0 |
---|
[26bf293] | 310 | |
---|
[948add7] | 311 | for item in res: |
---|
| 312 | if numpy.isfinite(item): |
---|
| 313 | sum +=item |
---|
[26bf293] | 314 | #print "chisqr : sum 2D", xmin, xmax, ymin, ymax,sum |
---|
| 315 | #print res |
---|
[44999f3] | 316 | self.tcChi.SetLabel(format_number(math.fabs(sum))) |
---|
[948add7] | 317 | except: |
---|
| 318 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 319 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
[26bf293] | 320 | |
---|
| 321 | |
---|
| 322 | def compute_chisqr(self): |
---|
| 323 | """ @param fn: function that return model value |
---|
| 324 | @return residuals |
---|
| 325 | """ |
---|
| 326 | |
---|
[d89f09b] | 327 | flag=self.checkFitRange() |
---|
[94999eb] | 328 | print "flag", flag |
---|
[26bf293] | 329 | if flag== True: |
---|
| 330 | try: |
---|
| 331 | if hasattr(self.data,"data"): |
---|
| 332 | self.compute_chisqr2D() |
---|
| 333 | return |
---|
| 334 | else: |
---|
| 335 | self.qmin_x = float(self.qmin.GetValue()) |
---|
| 336 | self.qmax_x = float(self.qmax.GetValue()) |
---|
[94999eb] | 337 | print "self.qmin_x, self.qmax_x",self.qmin_x,self.qmax_x |
---|
[26bf293] | 338 | x,y,dy = [numpy.asarray(v) for v in (self.data.x,self.data.y,self.data.dy)] |
---|
| 339 | if self.qmin_x==None and self.qmax_x==None: |
---|
| 340 | fx =numpy.asarray([self.model.run(v) for v in x]) |
---|
| 341 | res=(y - fx)/dy |
---|
| 342 | else: |
---|
| 343 | idx = (x>= self.qmin_x) & (x <=self.qmax_x) |
---|
| 344 | fx = numpy.asarray([self.model.run(item)for item in x[idx ]]) |
---|
| 345 | res= (y[idx] - fx)/dy[idx] |
---|
| 346 | |
---|
| 347 | sum=0 |
---|
| 348 | for item in res: |
---|
| 349 | if numpy.isfinite(item): |
---|
| 350 | sum +=item |
---|
| 351 | self.tcChi.SetLabel(format_number(math.fabs(sum))) |
---|
| 352 | except: |
---|
| 353 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 354 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
| 355 | |
---|
[d89f09b] | 356 | def _on_select_model(self,event): |
---|
| 357 | """ |
---|
| 358 | react when a model is selected from page's combo box |
---|
| 359 | post an event to its owner to draw an appropriate theory |
---|
| 360 | """ |
---|
[44bbf6a] | 361 | self.btFit.SetFocus() |
---|
[26bf293] | 362 | self.disable_disp.SetValue(True) |
---|
| 363 | self.sizer8.Clear(True) |
---|
| 364 | self.sizer7.Clear(True) |
---|
| 365 | self.vbox.Layout() |
---|
| 366 | self.SetScrollbars(20,20,55,40) |
---|
| 367 | self.Layout() |
---|
| 368 | self.parent.GetSizer().Layout() |
---|
[d89f09b] | 369 | for item in self.model_list_box.itervalues(): |
---|
[00561739] | 370 | name = item.__name__ |
---|
| 371 | if hasattr(item, "name"): |
---|
| 372 | name = item.name |
---|
| 373 | #print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
| 374 | if name ==event.GetString(): |
---|
| 375 | try: |
---|
[f39511b] | 376 | self.model=item() |
---|
| 377 | evt = ModelEventbox(model=self.model,name=name) |
---|
[d89f09b] | 378 | wx.PostEvent(self.event_owner, evt) |
---|
[94999eb] | 379 | self.text1_1.Show() |
---|
[44999f3] | 380 | self.compute_chisqr() |
---|
[94999eb] | 381 | self.tcChi.Show() |
---|
[00561739] | 382 | except: |
---|
| 383 | raise #ValueError,"model.name is not equal to model class name" |
---|
[26bf293] | 384 | break |
---|
| 385 | def onFit(self,event): |
---|
| 386 | """ signal for fitting""" |
---|
| 387 | |
---|
| 388 | flag=self.checkFitRange() |
---|
| 389 | self.set_manager(self.manager) |
---|
| 390 | |
---|
| 391 | self.qmin_x=float(self.qmin.GetValue()) |
---|
| 392 | self.qmax_x =float( self.qmax.GetValue()) |
---|
| 393 | if len(self.param_toFit) >0 and flag==True: |
---|
| 394 | self.manager.schedule_for_fit( value=1,fitproblem =None) |
---|
| 395 | self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x) |
---|
| 396 | else: |
---|
[80f3169] | 397 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
[26bf293] | 398 | "Select at least on parameter to fit ")) |
---|
| 399 | |
---|
| 400 | |
---|
[d89f09b] | 401 | def _onTextEnter(self,event): |
---|
| 402 | """ |
---|
| 403 | set a flag to determine if the fitting range entered by the user is valid |
---|
| 404 | """ |
---|
[442895f] | 405 | |
---|
[d89f09b] | 406 | try: |
---|
| 407 | flag=self.checkFitRange() |
---|
| 408 | if flag==True and self.model!=None: |
---|
[f39511b] | 409 | #print"fit page",self.xmin.GetValue(),self.xmax.GetValue() |
---|
[6f73a08] | 410 | self.manager.redraw_model(float(self.xmin.GetValue())\ |
---|
[d89f09b] | 411 | ,float(self.xmax.GetValue())) |
---|
| 412 | except: |
---|
[6f73a08] | 413 | |
---|
[d89f09b] | 414 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 415 | "Drawing Error:wrong value entered %s"% sys.exc_value)) |
---|
| 416 | |
---|
[26bf293] | 417 | |
---|
[d89f09b] | 418 | |
---|
| 419 | def get_param_list(self): |
---|
| 420 | """ |
---|
| 421 | @return self.param_toFit: list containing references to TextCtrl |
---|
| 422 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
| 423 | @raise: if return an empty list of parameter fit will nnote work |
---|
| 424 | properly so raise ValueError,"missing parameter to fit" |
---|
| 425 | """ |
---|
| 426 | if self.param_toFit !=[]: |
---|
| 427 | return self.param_toFit |
---|
| 428 | else: |
---|
| 429 | raise ValueError,"missing parameter to fit" |
---|
| 430 | |
---|
[26bf293] | 431 | |
---|
| 432 | |
---|
| 433 | def old_set_panel(self,model): |
---|
[d89f09b] | 434 | """ |
---|
| 435 | Build the panel from the model content |
---|
| 436 | @param model: the model selected in combo box for fitting purpose |
---|
| 437 | """ |
---|
[5cab7d3] | 438 | self.sizer5.Clear(True) |
---|
[d89f09b] | 439 | self.parameters = [] |
---|
| 440 | self.param_toFit=[] |
---|
| 441 | self.model = model |
---|
| 442 | keys = self.model.getParamList() |
---|
[44bbf6a] | 443 | #print "fitpage1D : dispersion list",self.model.getDispParamList() |
---|
[d89f09b] | 444 | keys.sort() |
---|
[08b9c6c8] | 445 | ik=0 |
---|
| 446 | im=1 |
---|
[26bf293] | 447 | |
---|
[d89f09b] | 448 | iy = 1 |
---|
| 449 | ix = 0 |
---|
[32d802f] | 450 | self.cb1 = wx.CheckBox(self, -1,"Select all", (10, 10)) |
---|
[f39511b] | 451 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
[26bf293] | 452 | self.sizer5.Add(self.cb1,(iy, ix),(1,1),\ |
---|
[d89f09b] | 453 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 454 | ix +=1 |
---|
[f39511b] | 455 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
[26bf293] | 456 | self.sizer5.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
[d89f09b] | 457 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 458 | ix +=2 |
---|
[f39511b] | 459 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
[26bf293] | 460 | self.sizer5.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
[d89f09b] | 461 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 462 | self.text2_3.Hide() |
---|
| 463 | ix +=1 |
---|
[f39511b] | 464 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
[26bf293] | 465 | self.sizer5.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
[d89f09b] | 466 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 467 | self.text2_4.Hide() |
---|
[26bf293] | 468 | disp_list=self.model.getDispParamList() |
---|
[d89f09b] | 469 | for item in keys: |
---|
[5cab7d3] | 470 | if not item in disp_list: |
---|
| 471 | iy += 1 |
---|
| 472 | ix = 0 |
---|
| 473 | |
---|
[f39511b] | 474 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
[5cab7d3] | 475 | cb.SetValue(False) |
---|
[26bf293] | 476 | self.sizer5.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[f39511b] | 477 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
[08b9c6c8] | 478 | |
---|
[5cab7d3] | 479 | ix += 1 |
---|
| 480 | value= self.model.getParam(item) |
---|
[f39511b] | 481 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
[5cab7d3] | 482 | ctl1.SetValue(str (format_number(value))) |
---|
| 483 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 484 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
[26bf293] | 485 | self.sizer5.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
[08b9c6c8] | 486 | |
---|
[5cab7d3] | 487 | ix += 1 |
---|
[f39511b] | 488 | text2=wx.StaticText(self, -1, '+/-') |
---|
[26bf293] | 489 | self.sizer5.Add(text2,(iy, ix),(1,1),\ |
---|
[5cab7d3] | 490 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 491 | text2.Hide() |
---|
| 492 | ix += 1 |
---|
[f39511b] | 493 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
[26bf293] | 494 | self.sizer5.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[5cab7d3] | 495 | ctl2.Hide() |
---|
| 496 | ix +=1 |
---|
| 497 | # Units |
---|
| 498 | try: |
---|
[f39511b] | 499 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
[5cab7d3] | 500 | except: |
---|
[f39511b] | 501 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
[26bf293] | 502 | self.sizer5.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 503 | """ |
---|
[04edd0d] | 504 | else: |
---|
[26bf293] | 505 | if not item in fixed: |
---|
[08b9c6c8] | 506 | ip = 0 |
---|
| 507 | iq += 1 |
---|
| 508 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
| 509 | cb.SetValue(False) |
---|
| 510 | self.sizer5.Add( cb,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 511 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
| 512 | |
---|
| 513 | ip += 1 |
---|
| 514 | value= self.model.getParam(item) |
---|
| 515 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 516 | ctl1.SetValue(str (format_number(value))) |
---|
| 517 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 518 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
| 519 | self.sizer5.Add(ctl1, (iq,ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 520 | else: |
---|
| 521 | ik = 0 |
---|
| 522 | text = wx.StaticText(self, -1, item, style=wx.ALIGN_LEFT) |
---|
| 523 | self.sizer6.Add(text,( im, ik),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 524 | |
---|
| 525 | ik += 1 |
---|
| 526 | value= self.model.getParam(item) |
---|
| 527 | Tctl = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 528 | Tctl.SetValue(str (format_number(value))) |
---|
| 529 | Tctl.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 530 | Tctl.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
| 531 | self.sizer6.Add(Tctl, (im,ik),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 532 | self.fixed_param.append([item, Tctl]) |
---|
| 533 | im += 1 |
---|
[04edd0d] | 534 | #save data |
---|
[26bf293] | 535 | """ |
---|
[04edd0d] | 536 | self.parameters.append([cb,ctl1,text2,ctl2]) |
---|
| 537 | |
---|
[5cab7d3] | 538 | iy+=1 |
---|
[26bf293] | 539 | self.sizer5.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[04edd0d] | 540 | |
---|
[d89f09b] | 541 | #Display units text on panel |
---|
| 542 | for item in keys: |
---|
| 543 | if self.model.details[item][0]!='': |
---|
| 544 | self.text2_4.Show() |
---|
| 545 | break |
---|
| 546 | else: |
---|
| 547 | self.text2_4.Hide() |
---|
| 548 | #Disable or enable fit button |
---|
[9d31a8b] | 549 | |
---|
| 550 | if not (len(self.param_toFit ) >0): |
---|
[d89f09b] | 551 | self.xmin.Disable() |
---|
| 552 | self.xmax.Disable() |
---|
[9d31a8b] | 553 | else: |
---|
| 554 | self.xmin.Enable() |
---|
| 555 | self.xmax.Enable() |
---|
| 556 | |
---|
[442895f] | 557 | self.compute_chisqr() |
---|
[d89f09b] | 558 | self.vbox.Layout() |
---|
[0aac36f] | 559 | self.SetScrollbars(20,20,55,40) |
---|
[dc317d1] | 560 | self.Layout() |
---|
[d89f09b] | 561 | self.GrandParent.GetSizer().Layout() |
---|
| 562 | |
---|
[bcd6d51] | 563 | |
---|
[d89f09b] | 564 | |
---|
| 565 | def _onparamEnter(self,event): |
---|
| 566 | """ |
---|
| 567 | when enter value on panel redraw model according to changed |
---|
| 568 | """ |
---|
[e9b4cc4] | 569 | self.set_model() |
---|
[442895f] | 570 | self.compute_chisqr() |
---|
[0550752] | 571 | |
---|
[e9b4cc4] | 572 | def set_model(self): |
---|
[d89f09b] | 573 | if len(self.parameters) !=0 and self.model !=None: |
---|
[d15c0202] | 574 | # Flag to register when a parameter has changed. |
---|
[d89f09b] | 575 | for item in self.parameters: |
---|
| 576 | try: |
---|
[d23544dc] | 577 | self.text2_3.Hide() |
---|
[0550752] | 578 | item[2].Hide() |
---|
| 579 | item[3].Clear() |
---|
| 580 | item[3].Hide() |
---|
[08b9c6c8] | 581 | except: |
---|
[26bf293] | 582 | #enter dispersion value |
---|
| 583 | pass |
---|
[e9b4cc4] | 584 | self.set_model_parameter() |
---|
| 585 | |
---|
[26bf293] | 586 | |
---|
[d89f09b] | 587 | def select_all_param(self,event): |
---|
| 588 | """ |
---|
| 589 | set to true or false all checkBox given the main checkbox value cb1 |
---|
| 590 | """ |
---|
[94999eb] | 591 | self.select_all_param_helper() |
---|
[d89f09b] | 592 | |
---|
| 593 | |
---|
| 594 | def select_param(self,event): |
---|
| 595 | """ |
---|
| 596 | Select TextCtrl checked for fitting purpose and stores them |
---|
| 597 | in self.param_toFit=[] list |
---|
| 598 | """ |
---|
| 599 | self.param_toFit=[] |
---|
| 600 | for item in self.parameters: |
---|
| 601 | if item[0].GetValue()==True: |
---|
| 602 | list= [item[0],item[1],item[2],item[3]] |
---|
[89032bf] | 603 | if not (list in self.param_toFit): |
---|
| 604 | self.param_toFit.append(list ) |
---|
[d89f09b] | 605 | else: |
---|
| 606 | if item in self.param_toFit: |
---|
| 607 | self.param_toFit.remove(item) |
---|
[6b44403] | 608 | |
---|
| 609 | for item in self.fittable_param: |
---|
| 610 | if item[0].GetValue()==True: |
---|
| 611 | list= [item[0],item[1],item[2],item[3]] |
---|
| 612 | if not (list in self.param_toFit): |
---|
| 613 | self.param_toFit.append(list ) |
---|
| 614 | else: |
---|
| 615 | if item in self.param_toFit: |
---|
| 616 | self.param_toFit.remove(item) |
---|
| 617 | |
---|
| 618 | |
---|
| 619 | if len(self.parameters)+len(self.fittable_param) ==len(self.param_toFit): |
---|
[d89f09b] | 620 | self.cb1.SetValue(True) |
---|
| 621 | else: |
---|
| 622 | self.cb1.SetValue(False) |
---|
[08b9c6c8] | 623 | |
---|
[9d31a8b] | 624 | if not (len(self.param_toFit ) >0): |
---|
[26bf293] | 625 | self.qmin.Disable() |
---|
| 626 | self.qmax.Disable() |
---|
[9d31a8b] | 627 | else: |
---|
[26bf293] | 628 | self.qmin.Enable() |
---|
| 629 | self.qmax.Enable() |
---|
[89032bf] | 630 | |
---|
[d89f09b] | 631 | |
---|
| 632 | |
---|
| 633 | |
---|
| 634 | def onsetValues(self,chisqr, out,cov): |
---|
| 635 | """ |
---|
| 636 | Build the panel from the fit result |
---|
| 637 | @param chisqr:Value of the goodness of fit metric |
---|
| 638 | @param out:list of parameter with the best value found during fitting |
---|
| 639 | @param cov:Covariance matrix |
---|
| 640 | |
---|
| 641 | """ |
---|
| 642 | #print "fitting : onsetvalues out",out |
---|
[26bf293] | 643 | self.tcChi.SetLabel(format_number(chisqr)) |
---|
[d89f09b] | 644 | params = {} |
---|
| 645 | is_modified = False |
---|
| 646 | has_error = False |
---|
| 647 | if out.__class__==numpy.float64: |
---|
| 648 | self.param_toFit[0][1].SetValue(format_number(out)) |
---|
| 649 | self.param_toFit[0][1].Refresh() |
---|
| 650 | if cov !=None : |
---|
| 651 | self.text2_3.Show() |
---|
| 652 | self.param_toFit[0][2].Show() |
---|
| 653 | self.param_toFit[0][3].Clear() |
---|
| 654 | self.param_toFit[0][3].SetValue(format_number(cov[0])) |
---|
| 655 | self.param_toFit[0][3].Show() |
---|
| 656 | #out is a list : set parameters and errors in TextCtrl |
---|
| 657 | else: |
---|
| 658 | i=0 |
---|
| 659 | #print "fitpage: list param model",list |
---|
| 660 | #for item in self.param_toFit: |
---|
| 661 | # print "fitpage: list display",item[0].GetLabelText() |
---|
| 662 | for item in self.param_toFit: |
---|
| 663 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
[442895f] | 664 | #item[1].SetValue(format_number(out[i])) |
---|
| 665 | item[1].SetValue(format_number(self.model.getParam(item[0].GetLabelText()))) |
---|
[d89f09b] | 666 | item[1].Refresh() |
---|
| 667 | if (cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
| 668 | self.text2_3.Show() |
---|
| 669 | item[2].Show() |
---|
| 670 | item[3].Clear() |
---|
| 671 | item[3].SetValue(format_number(cov[i])) |
---|
| 672 | item[3].Show() |
---|
| 673 | i+=1 |
---|
| 674 | |
---|
| 675 | self.vbox.Layout() |
---|
[0aac36f] | 676 | self.SetScrollbars(20,20,55,40) |
---|
| 677 | self.Layout() |
---|
[d89f09b] | 678 | self.GrandParent.GetSizer().Layout() |
---|
[a92d51b] | 679 | |
---|
| 680 | |
---|
[55e13ab] | 681 | def onSmear(self, event): |
---|
[08b9c6c8] | 682 | if event.GetString()=="None": |
---|
| 683 | self.manager.set_smearer(None) |
---|
| 684 | |
---|
| 685 | |
---|
[32d802f] | 686 | if event.GetString()=="Gaussian Resolution": |
---|
[55e13ab] | 687 | from DataLoader.qsmearing import smear_selection |
---|
| 688 | smear =smear_selection( self.data ) |
---|
[08b9c6c8] | 689 | self.manager.set_smearer(smear) |
---|
[32d802f] | 690 | # print "on smearing" |
---|
[08b9c6c8] | 691 | |
---|