[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 |
---|
[8aac6e1] | 111 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
| 112 | self.text4_3 = wx.StaticText(self, -1, 'Maximum Data Range(Linear)', style=wx.ALIGN_LEFT) |
---|
| 113 | self.sizer2.Add(self.text4_3,(iy,ix),(1,1),\ |
---|
[26bf293] | 114 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[8aac6e1] | 115 | ix += 2 |
---|
| 116 | self.sizer2.Add(wx.StaticText(self, -1, 'Min :'),(iy, ix),(1,1),\ |
---|
[26bf293] | 117 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[8aac6e1] | 118 | ix += 1 |
---|
| 119 | self.sizer2.Add(self.data_min,(iy, ix),(1,1),\ |
---|
[26bf293] | 120 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[8aac6e1] | 121 | ix += 1 |
---|
| 122 | self.sizer2.Add(wx.StaticText(self, -1, 'Max : '),(iy, ix),(1,1),\ |
---|
[26bf293] | 123 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[8aac6e1] | 124 | ix += 1 |
---|
| 125 | self.sizer2.Add(self.data_max,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[26bf293] | 126 | |
---|
| 127 | #----sizer 3 draw-------------------------------- |
---|
[d89f09b] | 128 | ix = 0 |
---|
[26bf293] | 129 | iy = 1 |
---|
[32d802f] | 130 | self.sizer3.Add(wx.StaticText(self,-1,'Instrument Smearing'),(iy,ix),(1,1)\ |
---|
[08b9c6c8] | 131 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 132 | ix += 1 |
---|
| 133 | self.sizer3.Add(self.smearer_box,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[26bf293] | 134 | ix =0 |
---|
| 135 | iy+=1 |
---|
| 136 | self.sizer3.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 137 | |
---|
| 138 | #------------------ sizer 4 draw------------------------ |
---|
| 139 | self.modelbox = wx.ComboBox(self, -1) |
---|
[94999eb] | 140 | self.tcChi = wx.StaticText(self, -1, str(0), style=wx.ALIGN_LEFT) |
---|
| 141 | self.tcChi.Hide() |
---|
| 142 | self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT) |
---|
| 143 | self.text1_1.Hide() |
---|
[26bf293] | 144 | #filling sizer2 |
---|
[08b9c6c8] | 145 | ix = 0 |
---|
[26bf293] | 146 | iy = 1 |
---|
| 147 | self.sizer4.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
[d89f09b] | 148 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 149 | ix += 1 |
---|
[26bf293] | 150 | self.sizer4.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[94999eb] | 151 | ix += 1 |
---|
| 152 | self.sizer4.Add(self.text1_1,(iy,ix),(1,1),\ |
---|
| 153 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 154 | ix += 1 |
---|
| 155 | self.sizer4.Add(self.tcChi,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[26bf293] | 156 | #----------sizer6------------------------------------------------- |
---|
| 157 | self.disable_disp = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) |
---|
| 158 | self.enable_disp = wx.RadioButton(self, -1, 'Yes', (10, 30)) |
---|
| 159 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.disable_disp.GetId()) |
---|
| 160 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.enable_disp.GetId()) |
---|
| 161 | ix= 0 |
---|
| 162 | iy=1 |
---|
| 163 | self.sizer6.Add(wx.StaticText(self,-1,'Polydispersity: '),(iy,ix),(1,1)\ |
---|
| 164 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[9d31a8b] | 165 | ix += 1 |
---|
[26bf293] | 166 | self.sizer6.Add(self.enable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 167 | ix += 1 |
---|
| 168 | self.sizer6.Add(self.disable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 169 | ix =0 |
---|
| 170 | iy+=1 |
---|
| 171 | self.sizer6.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 172 | |
---|
| 173 | |
---|
| 174 | #---------sizer 9 draw---------------------------------------- |
---|
[94999eb] | 175 | |
---|
[26bf293] | 176 | |
---|
| 177 | id = wx.NewId() |
---|
| 178 | self.btFit =wx.Button(self,id,'Fit') |
---|
| 179 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
| 180 | self.btFit.SetToolTipString("Perform fit.") |
---|
| 181 | ## Q range |
---|
[3e8e627] | 182 | #print "self.data fitpage1D" , self.data,hasattr(self.data,"data") |
---|
[44999f3] | 183 | # Reversed to the codes; Need to think carefully about consistency in q between 2D plot and fitting |
---|
[26bf293] | 184 | if not hasattr(self.data,"data"): |
---|
| 185 | self.qmin_x= numpy.min(self.data.x) |
---|
| 186 | self.qmax_x= numpy.max(self.data.x) |
---|
[3e8e627] | 187 | self.num_points= len(self.data.x) |
---|
[26bf293] | 188 | else: |
---|
[44999f3] | 189 | # Reversed to the codes; Need to think carefully about consistency in q between 2D plot and fitting |
---|
[c4550b0] | 190 | radius1= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymin, 2)) |
---|
| 191 | radius2= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymin, 2)) |
---|
| 192 | radius3= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymax, 2)) |
---|
| 193 | radius4= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymax, 2)) |
---|
[3e8e627] | 194 | #self.qmin_x = 0 |
---|
| 195 | #self.qmax_x = max(radius1, radius2, radius3, radius4) |
---|
[4cb07c0] | 196 | self.qmin_x= 0#self.data.xmin |
---|
| 197 | self.qmax_x= math.sqrt(math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2) |
---|
| 198 | +math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2))#self.data.xmax |
---|
[888e62c] | 199 | print "data2D range",self.qmax_x |
---|
[44999f3] | 200 | |
---|
[888e62c] | 201 | self.num_points= 100 |
---|
[26bf293] | 202 | |
---|
| 203 | |
---|
| 204 | |
---|
| 205 | self.qmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 206 | self.qmin.SetValue(str(format_number(self.qmin_x))) |
---|
| 207 | self.qmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
| 208 | self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 209 | self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
[4cb07c0] | 210 | self.qmin.Enable() |
---|
[26bf293] | 211 | |
---|
| 212 | self.qmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 213 | self.qmax.SetValue(str(format_number(self.qmax_x))) |
---|
| 214 | self.qmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
| 215 | self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 216 | self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
[4cb07c0] | 217 | self.qmax.Enable() |
---|
[26bf293] | 218 | |
---|
| 219 | self.npts = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 220 | self.npts.SetValue(format_number(self.num_points)) |
---|
| 221 | self.npts.SetToolTipString("Number of point to plot.") |
---|
| 222 | self.npts.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 223 | self.npts.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 224 | self.npts.Disable() |
---|
| 225 | self.npts.Hide() |
---|
| 226 | ix = 0 |
---|
| 227 | iy = 1 |
---|
| 228 | self.sizer9.Add(wx.StaticText(self, -1, 'Fitting Range'),(iy, ix),(1,1),\ |
---|
[d89f09b] | 229 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[26bf293] | 230 | ix += 1 |
---|
| 231 | self.sizer9.Add(wx.StaticText(self, -1, 'Min'),(iy, ix),(1,1),\ |
---|
[d89f09b] | 232 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[26bf293] | 233 | ix += 1 |
---|
| 234 | self.sizer9.Add(wx.StaticText(self, -1, 'Max'),(iy, ix),(1,1),\ |
---|
| 235 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 236 | #ix += 1 |
---|
| 237 | #self.sizer9.Add(wx.StaticText(self, -1, 'Npts'),(iy, ix),(1,1),\ |
---|
| 238 | # wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[d89f09b] | 239 | ix = 0 |
---|
| 240 | iy += 1 |
---|
[8aac6e1] | 241 | self.sizer9.Add(wx.StaticText(self, -1, 'Q range'),(iy, ix),(1,1),\ |
---|
[9d31a8b] | 242 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[d89f09b] | 243 | ix += 1 |
---|
[26bf293] | 244 | self.sizer9.Add(self.qmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 245 | ix += 1 |
---|
| 246 | self.sizer9.Add(self.qmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 247 | ix += 1 |
---|
| 248 | self.sizer9.Add(self.npts,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[94999eb] | 249 | |
---|
[d89f09b] | 250 | ix += 1 |
---|
[26bf293] | 251 | self.sizer9.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 252 | ix =0 |
---|
| 253 | iy+=1 |
---|
| 254 | self.sizer9.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 255 | #----------sizer 10 draw------------------------------------------------------ |
---|
| 256 | id = wx.NewId() |
---|
| 257 | self.btClose =wx.Button(self,id,'Close') |
---|
| 258 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
| 259 | self.btClose.SetToolTipString("Close page.") |
---|
| 260 | |
---|
| 261 | ix= 3 |
---|
| 262 | iy= 1 |
---|
| 263 | self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 264 | ix +=1 |
---|
| 265 | self.sizer10.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 266 | ix =0 |
---|
[2dbb681] | 267 | iy+=1 |
---|
[26bf293] | 268 | self.sizer10.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 269 | |
---|
[d89f09b] | 270 | # contains link between model ,all its parameters, and panel organization |
---|
| 271 | self.parameters=[] |
---|
[08b9c6c8] | 272 | self.fixed_param=[] |
---|
[6b44403] | 273 | self.fittable_param=[] |
---|
[26bf293] | 274 | #list of dispersion paramaters |
---|
[7a0fb4e] | 275 | self.disp_list=[] |
---|
[d89f09b] | 276 | #contains link between a model and selected parameters to fit |
---|
| 277 | self.param_toFit=[] |
---|
| 278 | # model on which the fit would be performed |
---|
| 279 | self.model=None |
---|
| 280 | #dictionary of model name and model class |
---|
[72637e8] | 281 | self.model_list_box={} |
---|
| 282 | |
---|
| 283 | if self.model == None: |
---|
| 284 | self.qmin.Disable() |
---|
| 285 | self.qmax.Disable() |
---|
| 286 | else: |
---|
| 287 | self.qmin.Enable() |
---|
| 288 | self.qmax.Enable() |
---|
| 289 | |
---|
[32673ac] | 290 | |
---|
[f39511b] | 291 | self.vbox.Layout() |
---|
| 292 | self.vbox.Fit(self) |
---|
| 293 | self.SetSizer(self.vbox) |
---|
| 294 | self.SetScrollbars(20,20,55,40) |
---|
| 295 | |
---|
[d89f09b] | 296 | self.Centre() |
---|
[b5847bd8] | 297 | self.Layout() |
---|
| 298 | self.GrandParent.GetSizer().Layout() |
---|
[26bf293] | 299 | |
---|
| 300 | def compute_chisqr2D(self): |
---|
[442895f] | 301 | """ @param fn: function that return model value |
---|
| 302 | @return residuals |
---|
| 303 | """ |
---|
| 304 | flag=self.checkFitRange() |
---|
[26bf293] | 305 | res=[] |
---|
[442895f] | 306 | if flag== True: |
---|
[948add7] | 307 | try: |
---|
[fd25fb0] | 308 | print "compute",self.data.err_data |
---|
[26bf293] | 309 | self.qmin_x = float(self.qmin.GetValue()) |
---|
| 310 | self.qmax_x = float(self.qmax.GetValue()) |
---|
[bcd6d51] | 311 | |
---|
[26bf293] | 312 | for i in range(len(self.data.x_bins)): |
---|
| 313 | if self.data.x_bins[i]>= self.qmin_x and self.data.x_bins[i]<= self.qmax_x: |
---|
| 314 | for j in range(len(self.data.y_bins)): |
---|
| 315 | if self.data.y_bins[j]>= self.qmin_x and self.data.y_bins[j]<= self.qmax_x: |
---|
| 316 | res.append( (self.data.data[j][i]- self.model.runXY(\ |
---|
[888e62c] | 317 | [self.data.x_bins[i],self.data.y_bins[j]]))\ |
---|
[26bf293] | 318 | /self.data.err_data[j][i] ) |
---|
[948add7] | 319 | sum=0 |
---|
[26bf293] | 320 | |
---|
[948add7] | 321 | for item in res: |
---|
| 322 | if numpy.isfinite(item): |
---|
| 323 | sum +=item |
---|
[26bf293] | 324 | #print "chisqr : sum 2D", xmin, xmax, ymin, ymax,sum |
---|
| 325 | #print res |
---|
[44999f3] | 326 | self.tcChi.SetLabel(format_number(math.fabs(sum))) |
---|
[948add7] | 327 | except: |
---|
[fd25fb0] | 328 | raise |
---|
[948add7] | 329 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 330 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
[26bf293] | 331 | |
---|
| 332 | |
---|
| 333 | def compute_chisqr(self): |
---|
| 334 | """ @param fn: function that return model value |
---|
| 335 | @return residuals |
---|
| 336 | """ |
---|
| 337 | |
---|
[d89f09b] | 338 | flag=self.checkFitRange() |
---|
[94999eb] | 339 | print "flag", flag |
---|
[26bf293] | 340 | if flag== True: |
---|
| 341 | try: |
---|
| 342 | if hasattr(self.data,"data"): |
---|
| 343 | self.compute_chisqr2D() |
---|
| 344 | return |
---|
| 345 | else: |
---|
| 346 | self.qmin_x = float(self.qmin.GetValue()) |
---|
| 347 | self.qmax_x = float(self.qmax.GetValue()) |
---|
[3e8e627] | 348 | #print "self.qmin_x, self.qmax_x",self.qmin_x,self.qmax_x |
---|
[26bf293] | 349 | x,y,dy = [numpy.asarray(v) for v in (self.data.x,self.data.y,self.data.dy)] |
---|
| 350 | if self.qmin_x==None and self.qmax_x==None: |
---|
| 351 | fx =numpy.asarray([self.model.run(v) for v in x]) |
---|
| 352 | res=(y - fx)/dy |
---|
| 353 | else: |
---|
| 354 | idx = (x>= self.qmin_x) & (x <=self.qmax_x) |
---|
| 355 | fx = numpy.asarray([self.model.run(item)for item in x[idx ]]) |
---|
| 356 | res= (y[idx] - fx)/dy[idx] |
---|
| 357 | |
---|
| 358 | sum=0 |
---|
| 359 | for item in res: |
---|
| 360 | if numpy.isfinite(item): |
---|
| 361 | sum +=item |
---|
| 362 | self.tcChi.SetLabel(format_number(math.fabs(sum))) |
---|
| 363 | except: |
---|
| 364 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 365 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
| 366 | |
---|
[d89f09b] | 367 | def _on_select_model(self,event): |
---|
| 368 | """ |
---|
| 369 | react when a model is selected from page's combo box |
---|
| 370 | post an event to its owner to draw an appropriate theory |
---|
| 371 | """ |
---|
[44bbf6a] | 372 | self.btFit.SetFocus() |
---|
[26bf293] | 373 | self.disable_disp.SetValue(True) |
---|
| 374 | self.sizer8.Clear(True) |
---|
| 375 | self.sizer7.Clear(True) |
---|
| 376 | self.vbox.Layout() |
---|
| 377 | self.SetScrollbars(20,20,55,40) |
---|
| 378 | self.Layout() |
---|
| 379 | self.parent.GetSizer().Layout() |
---|
[72637e8] | 380 | |
---|
[d89f09b] | 381 | for item in self.model_list_box.itervalues(): |
---|
[00561739] | 382 | name = item.__name__ |
---|
| 383 | if hasattr(item, "name"): |
---|
| 384 | name = item.name |
---|
| 385 | #print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
[72637e8] | 386 | if name == None: |
---|
| 387 | self.qmin.Disable() |
---|
| 388 | self.qmax.Disable() |
---|
| 389 | else: |
---|
| 390 | self.qmin.Enable() |
---|
| 391 | self.qmax.Enable() |
---|
| 392 | |
---|
[00561739] | 393 | if name ==event.GetString(): |
---|
| 394 | try: |
---|
[f39511b] | 395 | self.model=item() |
---|
| 396 | evt = ModelEventbox(model=self.model,name=name) |
---|
[d89f09b] | 397 | wx.PostEvent(self.event_owner, evt) |
---|
[94999eb] | 398 | self.text1_1.Show() |
---|
[44999f3] | 399 | self.compute_chisqr() |
---|
[94999eb] | 400 | self.tcChi.Show() |
---|
[00561739] | 401 | except: |
---|
| 402 | raise #ValueError,"model.name is not equal to model class name" |
---|
[26bf293] | 403 | break |
---|
| 404 | def onFit(self,event): |
---|
| 405 | """ signal for fitting""" |
---|
| 406 | |
---|
| 407 | flag=self.checkFitRange() |
---|
| 408 | self.set_manager(self.manager) |
---|
| 409 | |
---|
| 410 | self.qmin_x=float(self.qmin.GetValue()) |
---|
| 411 | self.qmax_x =float( self.qmax.GetValue()) |
---|
| 412 | if len(self.param_toFit) >0 and flag==True: |
---|
| 413 | self.manager.schedule_for_fit( value=1,fitproblem =None) |
---|
[3e8e627] | 414 | if hasattr(self.data, "data"): |
---|
[4cb07c0] | 415 | self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x, |
---|
[e191da5] | 416 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
| 417 | xmin=self.data.xmin,xmax=self.data.xmax) |
---|
| 418 | #self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x, |
---|
| 419 | #ymin=self.data.ymin, ymax=self.data.ymax) |
---|
[3e8e627] | 420 | else: |
---|
| 421 | self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x) |
---|
[26bf293] | 422 | else: |
---|
[80f3169] | 423 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
[26bf293] | 424 | "Select at least on parameter to fit ")) |
---|
| 425 | |
---|
| 426 | |
---|
[d89f09b] | 427 | def _onTextEnter(self,event): |
---|
| 428 | """ |
---|
| 429 | set a flag to determine if the fitting range entered by the user is valid |
---|
| 430 | """ |
---|
[442895f] | 431 | |
---|
[d89f09b] | 432 | try: |
---|
| 433 | flag=self.checkFitRange() |
---|
| 434 | if flag==True and self.model!=None: |
---|
[f39511b] | 435 | #print"fit page",self.xmin.GetValue(),self.xmax.GetValue() |
---|
[6f73a08] | 436 | self.manager.redraw_model(float(self.xmin.GetValue())\ |
---|
[d89f09b] | 437 | ,float(self.xmax.GetValue())) |
---|
| 438 | except: |
---|
[6f73a08] | 439 | |
---|
[d89f09b] | 440 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 441 | "Drawing Error:wrong value entered %s"% sys.exc_value)) |
---|
| 442 | |
---|
[26bf293] | 443 | |
---|
[d89f09b] | 444 | |
---|
| 445 | def get_param_list(self): |
---|
| 446 | """ |
---|
| 447 | @return self.param_toFit: list containing references to TextCtrl |
---|
| 448 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
| 449 | @raise: if return an empty list of parameter fit will nnote work |
---|
| 450 | properly so raise ValueError,"missing parameter to fit" |
---|
| 451 | """ |
---|
| 452 | if self.param_toFit !=[]: |
---|
| 453 | return self.param_toFit |
---|
| 454 | else: |
---|
| 455 | raise ValueError,"missing parameter to fit" |
---|
| 456 | |
---|
[26bf293] | 457 | |
---|
[d89f09b] | 458 | def _onparamEnter(self,event): |
---|
| 459 | """ |
---|
| 460 | when enter value on panel redraw model according to changed |
---|
| 461 | """ |
---|
[e9b4cc4] | 462 | self.set_model() |
---|
[442895f] | 463 | self.compute_chisqr() |
---|
[0550752] | 464 | |
---|
[e9b4cc4] | 465 | def set_model(self): |
---|
[d89f09b] | 466 | if len(self.parameters) !=0 and self.model !=None: |
---|
[d15c0202] | 467 | # Flag to register when a parameter has changed. |
---|
[d89f09b] | 468 | for item in self.parameters: |
---|
| 469 | try: |
---|
[d23544dc] | 470 | self.text2_3.Hide() |
---|
[0550752] | 471 | item[2].Hide() |
---|
| 472 | item[3].Clear() |
---|
| 473 | item[3].Hide() |
---|
[08b9c6c8] | 474 | except: |
---|
[26bf293] | 475 | #enter dispersion value |
---|
| 476 | pass |
---|
[e9b4cc4] | 477 | self.set_model_parameter() |
---|
| 478 | |
---|
[26bf293] | 479 | |
---|
[d89f09b] | 480 | def select_all_param(self,event): |
---|
| 481 | """ |
---|
| 482 | set to true or false all checkBox given the main checkbox value cb1 |
---|
| 483 | """ |
---|
[94999eb] | 484 | self.select_all_param_helper() |
---|
[d89f09b] | 485 | |
---|
| 486 | |
---|
| 487 | def select_param(self,event): |
---|
| 488 | """ |
---|
| 489 | Select TextCtrl checked for fitting purpose and stores them |
---|
| 490 | in self.param_toFit=[] list |
---|
| 491 | """ |
---|
| 492 | self.param_toFit=[] |
---|
| 493 | for item in self.parameters: |
---|
| 494 | if item[0].GetValue()==True: |
---|
| 495 | list= [item[0],item[1],item[2],item[3]] |
---|
[89032bf] | 496 | if not (list in self.param_toFit): |
---|
| 497 | self.param_toFit.append(list ) |
---|
[d89f09b] | 498 | else: |
---|
| 499 | if item in self.param_toFit: |
---|
| 500 | self.param_toFit.remove(item) |
---|
[6b44403] | 501 | |
---|
| 502 | for item in self.fittable_param: |
---|
| 503 | if item[0].GetValue()==True: |
---|
| 504 | list= [item[0],item[1],item[2],item[3]] |
---|
| 505 | if not (list in self.param_toFit): |
---|
| 506 | self.param_toFit.append(list ) |
---|
| 507 | else: |
---|
| 508 | if item in self.param_toFit: |
---|
| 509 | self.param_toFit.remove(item) |
---|
| 510 | |
---|
| 511 | |
---|
| 512 | if len(self.parameters)+len(self.fittable_param) ==len(self.param_toFit): |
---|
[d89f09b] | 513 | self.cb1.SetValue(True) |
---|
| 514 | else: |
---|
| 515 | self.cb1.SetValue(False) |
---|
[4cb07c0] | 516 | """ |
---|
[8d93ec4] | 517 | #qmax, qmin Input enabled all the time |
---|
| 518 | self.qmin.Enable() |
---|
[4cb07c0] | 519 | self.qmax.Enable() |
---|
| 520 | """ |
---|
[d89f09b] | 521 | |
---|
| 522 | def onsetValues(self,chisqr, out,cov): |
---|
| 523 | """ |
---|
| 524 | Build the panel from the fit result |
---|
| 525 | @param chisqr:Value of the goodness of fit metric |
---|
| 526 | @param out:list of parameter with the best value found during fitting |
---|
| 527 | @param cov:Covariance matrix |
---|
| 528 | |
---|
| 529 | """ |
---|
| 530 | #print "fitting : onsetvalues out",out |
---|
[26bf293] | 531 | self.tcChi.SetLabel(format_number(chisqr)) |
---|
[d89f09b] | 532 | params = {} |
---|
| 533 | is_modified = False |
---|
| 534 | has_error = False |
---|
| 535 | if out.__class__==numpy.float64: |
---|
| 536 | self.param_toFit[0][1].SetValue(format_number(out)) |
---|
| 537 | self.param_toFit[0][1].Refresh() |
---|
| 538 | if cov !=None : |
---|
| 539 | self.text2_3.Show() |
---|
| 540 | self.param_toFit[0][2].Show() |
---|
| 541 | self.param_toFit[0][3].Clear() |
---|
| 542 | self.param_toFit[0][3].SetValue(format_number(cov[0])) |
---|
| 543 | self.param_toFit[0][3].Show() |
---|
| 544 | #out is a list : set parameters and errors in TextCtrl |
---|
| 545 | else: |
---|
| 546 | i=0 |
---|
[4cb07c0] | 547 | #print "fitpage: list param model",list |
---|
[d89f09b] | 548 | #for item in self.param_toFit: |
---|
| 549 | # print "fitpage: list display",item[0].GetLabelText() |
---|
| 550 | for item in self.param_toFit: |
---|
| 551 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
[442895f] | 552 | #item[1].SetValue(format_number(out[i])) |
---|
| 553 | item[1].SetValue(format_number(self.model.getParam(item[0].GetLabelText()))) |
---|
[d89f09b] | 554 | item[1].Refresh() |
---|
| 555 | if (cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
| 556 | self.text2_3.Show() |
---|
| 557 | item[2].Show() |
---|
| 558 | item[3].Clear() |
---|
| 559 | item[3].SetValue(format_number(cov[i])) |
---|
| 560 | item[3].Show() |
---|
| 561 | i+=1 |
---|
| 562 | |
---|
| 563 | self.vbox.Layout() |
---|
[0aac36f] | 564 | self.SetScrollbars(20,20,55,40) |
---|
| 565 | self.Layout() |
---|
[d89f09b] | 566 | self.GrandParent.GetSizer().Layout() |
---|
[a92d51b] | 567 | |
---|
| 568 | |
---|
[55e13ab] | 569 | def onSmear(self, event): |
---|
[08b9c6c8] | 570 | if event.GetString()=="None": |
---|
| 571 | self.manager.set_smearer(None) |
---|
| 572 | |
---|
| 573 | |
---|
[32d802f] | 574 | if event.GetString()=="Gaussian Resolution": |
---|
[55e13ab] | 575 | from DataLoader.qsmearing import smear_selection |
---|
| 576 | smear =smear_selection( self.data ) |
---|
[08b9c6c8] | 577 | self.manager.set_smearer(smear) |
---|
[32d802f] | 578 | # print "on smearing" |
---|
[08b9c6c8] | 579 | |
---|