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