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