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