[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 | |
---|
[07a93a1] | 15 | #TODO: FitPage1D inherits from ModelPage but doesn't call its __init__ method! |
---|
| 16 | #TODO: refactor this class to have an __init__ that deals with data only, then calls a UI builder method. |
---|
[1fbb343] | 17 | |
---|
[d23544dc] | 18 | class FitPage1D(ModelPage): |
---|
[d89f09b] | 19 | """ |
---|
[fbc3e04] | 20 | FitPanel class contains fields allowing to display results when |
---|
| 21 | fitting a model and one data |
---|
[d89f09b] | 22 | @note: For Fit to be performed the user should check at least one parameter |
---|
| 23 | on fit Panel window. |
---|
| 24 | """ |
---|
| 25 | ## Internal name for the AUI manager |
---|
| 26 | window_name = "Fit page" |
---|
| 27 | ## Title to appear on top of the window |
---|
| 28 | window_caption = "Fit Page" |
---|
[22e8d49] | 29 | |
---|
[10c43a5] | 30 | name=None |
---|
[d89f09b] | 31 | |
---|
[fbc3e04] | 32 | def __init__(self, parent,data, *args, **kwargs): |
---|
| 33 | wx.ScrolledWindow.__init__(self, parent, *args, **kwargs) |
---|
| 34 | |
---|
[d89f09b] | 35 | """ |
---|
| 36 | Initialization of the Panel |
---|
| 37 | """ |
---|
[76dab10] | 38 | #TODO: remove this once the inheritence is cleaned up |
---|
| 39 | ## Data member to store the dispersion object created |
---|
[fbc3e04] | 40 | self._disp_obj_dict = {} |
---|
| 41 | |
---|
| 42 | #Data used for fitting |
---|
| 43 | self.data = data |
---|
| 44 | # flag to allow data2D plot |
---|
| 45 | self.enable2D=False |
---|
| 46 | #fit page manager |
---|
| 47 | self.manager = None |
---|
| 48 | #Store the parent of this panel parent |
---|
| 49 | # For this application fitpanel is the parent |
---|
| 50 | self.parent = parent |
---|
| 51 | # Event_owner is the owner of model event |
---|
| 52 | self.event_owner = None |
---|
[d89f09b] | 53 | #panel interface |
---|
| 54 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
[ed2ea6a] | 55 | |
---|
[26bf293] | 56 | self.sizer9 = wx.GridBagSizer(5,5) |
---|
| 57 | self.sizer8 = wx.GridBagSizer(5,5) |
---|
| 58 | self.sizer7 = wx.GridBagSizer(5,5) |
---|
[08b9c6c8] | 59 | self.sizer6 = wx.GridBagSizer(5,5) |
---|
[5cab7d3] | 60 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
[d89f09b] | 61 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
| 62 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
| 63 | self.sizer2 = wx.GridBagSizer(5,5) |
---|
| 64 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
[26bf293] | 65 | # Add layer |
---|
| 66 | #data info layer |
---|
| 67 | self.vbox.Add(self.sizer1) |
---|
| 68 | #data range |
---|
| 69 | self.vbox.Add(self.sizer2) |
---|
| 70 | #instrument smearing selection layer |
---|
| 71 | self.vbox.Add(self.sizer3) |
---|
| 72 | #model selection |
---|
| 73 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
| 74 | self.vbox.Add(self.sizer4) |
---|
| 75 | #model paramaters layer |
---|
| 76 | self.vbox.Add(self.sizer5) |
---|
| 77 | #polydispersion selected |
---|
| 78 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
| 79 | self.vbox.Add(self.sizer6) |
---|
| 80 | #combox box for type of dispersion |
---|
| 81 | self.vbox.Add(self.sizer7) |
---|
| 82 | #dispersion parameters layer |
---|
| 83 | self.vbox.Add(self.sizer8) |
---|
| 84 | #fit info layer |
---|
| 85 | self.vbox.Add(wx.StaticLine(self, -1), 0, wx.EXPAND, 0) |
---|
| 86 | self.vbox.Add(self.sizer9) |
---|
| 87 | #---------sizer 1 draw-------------------------------- |
---|
[22e8d49] | 88 | #Filling the sizer containing data related fields |
---|
[fbc3e04] | 89 | self.DataSource =wx.StaticText(self, -1,str(data.name)) |
---|
[26bf293] | 90 | ix = 0 |
---|
| 91 | iy = 1 |
---|
| 92 | self.sizer1.Add(wx.StaticText(self, -1, 'Data Source Name : '),(iy,ix),\ |
---|
| 93 | (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 94 | ix += 1 |
---|
| 95 | self.sizer1.Add(self.DataSource,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[d89f09b] | 96 | |
---|
[26bf293] | 97 | #---------sizer 2 draw-------------------------------- |
---|
| 98 | #set maximum range for x in linear scale |
---|
[8aac6e1] | 99 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
[ef8b580] | 100 | ix = 0 |
---|
| 101 | iy = 1 |
---|
[2268d10] | 102 | # Minimum value of data |
---|
[fbc3e04] | 103 | self.data_min = wx.StaticText(self, -1,str(format_number(numpy.min(data.x)))) |
---|
[2268d10] | 104 | # Maximum value of data |
---|
[fbc3e04] | 105 | self.data_max = wx.StaticText(self, -1,str(format_number(numpy.max(data.x)))) |
---|
[8aac6e1] | 106 | self.text4_3 = wx.StaticText(self, -1, 'Maximum Data Range(Linear)', style=wx.ALIGN_LEFT) |
---|
| 107 | self.sizer2.Add(self.text4_3,(iy,ix),(1,1),\ |
---|
[26bf293] | 108 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[8aac6e1] | 109 | ix += 2 |
---|
| 110 | self.sizer2.Add(wx.StaticText(self, -1, 'Min :'),(iy, ix),(1,1),\ |
---|
[26bf293] | 111 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[8aac6e1] | 112 | ix += 1 |
---|
| 113 | self.sizer2.Add(self.data_min,(iy, ix),(1,1),\ |
---|
[26bf293] | 114 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[8aac6e1] | 115 | ix += 1 |
---|
| 116 | self.sizer2.Add(wx.StaticText(self, -1, 'Max : '),(iy, ix),(1,1),\ |
---|
[26bf293] | 117 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[8aac6e1] | 118 | ix += 1 |
---|
| 119 | self.sizer2.Add(self.data_max,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[26bf293] | 120 | |
---|
| 121 | #----sizer 3 draw-------------------------------- |
---|
[22e8d49] | 122 | #Filling the sizer containing instruments smearing info. |
---|
[ef8b580] | 123 | self.disable_smearer = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) |
---|
| 124 | self.enable_smearer = wx.RadioButton(self, -1, 'Yes', (10, 30)) |
---|
| 125 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.disable_smearer.GetId()) |
---|
| 126 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.enable_smearer.GetId()) |
---|
[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 |
---|
[ef8b580] | 132 | self.sizer3.Add(self.enable_smearer,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 133 | ix += 1 |
---|
| 134 | self.sizer3.Add(self.disable_smearer,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[26bf293] | 135 | ix =0 |
---|
| 136 | iy+=1 |
---|
| 137 | self.sizer3.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 138 | |
---|
| 139 | #------------------ sizer 4 draw------------------------ |
---|
[22e8d49] | 140 | # This sizer contains model list and chisqr value |
---|
| 141 | #filling sizer4 |
---|
[26bf293] | 142 | self.modelbox = wx.ComboBox(self, -1) |
---|
[94999eb] | 143 | self.tcChi = wx.StaticText(self, -1, str(0), style=wx.ALIGN_LEFT) |
---|
| 144 | self.tcChi.Hide() |
---|
| 145 | self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT) |
---|
| 146 | self.text1_1.Hide() |
---|
[08b9c6c8] | 147 | ix = 0 |
---|
[26bf293] | 148 | iy = 1 |
---|
| 149 | self.sizer4.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
[d89f09b] | 150 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 151 | ix += 1 |
---|
[26bf293] | 152 | self.sizer4.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[94999eb] | 153 | ix += 1 |
---|
| 154 | self.sizer4.Add(self.text1_1,(iy,ix),(1,1),\ |
---|
| 155 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 156 | ix += 1 |
---|
| 157 | self.sizer4.Add(self.tcChi,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[26bf293] | 158 | #----------sizer6------------------------------------------------- |
---|
[22e8d49] | 159 | #Sizer containing info on polydispersity |
---|
[26bf293] | 160 | self.disable_disp = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) |
---|
| 161 | self.enable_disp = wx.RadioButton(self, -1, 'Yes', (10, 30)) |
---|
[fbc3e04] | 162 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.disable_disp.GetId()) |
---|
| 163 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.enable_disp.GetId()) |
---|
[26bf293] | 164 | ix= 0 |
---|
| 165 | iy=1 |
---|
| 166 | self.sizer6.Add(wx.StaticText(self,-1,'Polydispersity: '),(iy,ix),(1,1)\ |
---|
| 167 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[9d31a8b] | 168 | ix += 1 |
---|
[26bf293] | 169 | self.sizer6.Add(self.enable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 170 | ix += 1 |
---|
| 171 | self.sizer6.Add(self.disable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 172 | ix =0 |
---|
| 173 | iy+=1 |
---|
| 174 | self.sizer6.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 175 | #---------sizer 9 draw---------------------------------------- |
---|
[94999eb] | 176 | |
---|
[26bf293] | 177 | |
---|
| 178 | id = wx.NewId() |
---|
| 179 | self.btFit =wx.Button(self,id,'Fit') |
---|
| 180 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
| 181 | self.btFit.SetToolTipString("Perform fit.") |
---|
[22e8d49] | 182 | ## Q range |
---|
[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)) |
---|
[22e8d49] | 194 | |
---|
| 195 | self.qmin_x= 0 |
---|
[4cb07c0] | 196 | self.qmax_x= math.sqrt(math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2) |
---|
| 197 | +math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2))#self.data.xmax |
---|
[44999f3] | 198 | |
---|
[888e62c] | 199 | self.num_points= 100 |
---|
[22e8d49] | 200 | # The minimum fitting range |
---|
[26bf293] | 201 | self.qmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 202 | self.qmin.SetValue(str(format_number(self.qmin_x))) |
---|
| 203 | self.qmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
| 204 | self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 205 | self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
[4cb07c0] | 206 | self.qmin.Enable() |
---|
[22e8d49] | 207 | #The maximum fitting range |
---|
[26bf293] | 208 | self.qmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 209 | self.qmax.SetValue(str(format_number(self.qmax_x))) |
---|
| 210 | self.qmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
| 211 | self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 212 | self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
[4cb07c0] | 213 | self.qmax.Enable() |
---|
[22e8d49] | 214 | #The number of points to plots |
---|
[26bf293] | 215 | self.npts = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 216 | self.npts.SetValue(format_number(self.num_points)) |
---|
| 217 | self.npts.SetToolTipString("Number of point to plot.") |
---|
| 218 | self.npts.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 219 | self.npts.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 220 | self.npts.Disable() |
---|
| 221 | self.npts.Hide() |
---|
| 222 | ix = 0 |
---|
| 223 | iy = 1 |
---|
| 224 | self.sizer9.Add(wx.StaticText(self, -1, 'Fitting Range'),(iy, ix),(1,1),\ |
---|
[d89f09b] | 225 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[26bf293] | 226 | ix += 1 |
---|
| 227 | self.sizer9.Add(wx.StaticText(self, -1, 'Min'),(iy, ix),(1,1),\ |
---|
[d89f09b] | 228 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[26bf293] | 229 | ix += 1 |
---|
| 230 | self.sizer9.Add(wx.StaticText(self, -1, 'Max'),(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) |
---|
[22e8d49] | 245 | |
---|
[26bf293] | 246 | ix =0 |
---|
| 247 | iy+=1 |
---|
| 248 | self.sizer9.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[fbc3e04] | 249 | |
---|
| 250 | # Contains link between model ,all its parameters, and panel organization |
---|
| 251 | self.parameters=[] |
---|
| 252 | # Contains list of parameters that cannot be fitted and reference to |
---|
| 253 | #panel objects |
---|
| 254 | self.fixed_param=[] |
---|
| 255 | # Contains list of parameters with dispersity and reference to |
---|
| 256 | #panel objects |
---|
| 257 | self.fittable_param=[] |
---|
| 258 | #list of dispersion paramaters |
---|
| 259 | self.disp_list=[] |
---|
| 260 | #contains link between a model and selected parameters to fit |
---|
| 261 | self.param_toFit=[] |
---|
| 262 | # model on which the fit would be performed |
---|
| 263 | self.model=None |
---|
| 264 | |
---|
| 265 | self.back_up_model= None |
---|
| 266 | #dictionary of model name and model class |
---|
| 267 | self.model_list_box={} |
---|
| 268 | |
---|
| 269 | if self.model == None: |
---|
| 270 | self.qmin.Disable() |
---|
| 271 | self.qmax.Disable() |
---|
| 272 | else: |
---|
| 273 | self.qmin.Enable() |
---|
| 274 | self.qmax.Enable() |
---|
| 275 | |
---|
[22e8d49] | 276 | |
---|
[f39511b] | 277 | self.vbox.Layout() |
---|
| 278 | self.vbox.Fit(self) |
---|
| 279 | self.SetSizer(self.vbox) |
---|
| 280 | self.SetScrollbars(20,20,55,40) |
---|
| 281 | |
---|
[fbc3e04] | 282 | self.Centre() |
---|
| 283 | self.Layout() |
---|
| 284 | self.GrandParent.GetSizer().Layout() |
---|
| 285 | |
---|
[26bf293] | 286 | def compute_chisqr2D(self): |
---|
[22e8d49] | 287 | """ |
---|
| 288 | compute chi square given a model and data 2D and set the value |
---|
| 289 | to the tcChi txtcrl |
---|
[442895f] | 290 | """ |
---|
| 291 | flag=self.checkFitRange() |
---|
[26bf293] | 292 | res=[] |
---|
[442895f] | 293 | if flag== True: |
---|
[948add7] | 294 | try: |
---|
[26bf293] | 295 | self.qmin_x = float(self.qmin.GetValue()) |
---|
| 296 | self.qmax_x = float(self.qmax.GetValue()) |
---|
| 297 | for i in range(len(self.data.x_bins)): |
---|
[22e8d49] | 298 | for j in range(len(self.data.y_bins)): |
---|
| 299 | #Check the range containing data between self.qmin_x and self.qmax_x |
---|
[fbc3e04] | 300 | if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)>=math.pow(self.qmin_x,2): |
---|
| 301 | if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)<=math.pow(self.qmax_x,2): |
---|
| 302 | chisqrji=(self.data.data[j][i]- self.model.runXY(\ |
---|
| 303 | [self.data.y_bins[j],self.data.x_bins[i]]))\ |
---|
| 304 | /self.data.err_data[j][i] |
---|
[22e8d49] | 305 | #Vector containing residuals |
---|
| 306 | res.append( math.pow(chisqrji,2) ) |
---|
| 307 | # compute sum of residual |
---|
[948add7] | 308 | sum=0 |
---|
| 309 | for item in res: |
---|
| 310 | if numpy.isfinite(item): |
---|
| 311 | sum +=item |
---|
[1309e919] | 312 | self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res)))) |
---|
[948add7] | 313 | except: |
---|
[fbc3e04] | 314 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 315 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
[22e8d49] | 316 | return |
---|
| 317 | |
---|
[f3113c9] | 318 | |
---|
[26bf293] | 319 | def compute_chisqr(self): |
---|
[22e8d49] | 320 | """ |
---|
| 321 | compute chi square given a model and data 1D and set the value |
---|
| 322 | to the tcChi txtcrl |
---|
[26bf293] | 323 | """ |
---|
[d89f09b] | 324 | flag=self.checkFitRange() |
---|
[26bf293] | 325 | if flag== True: |
---|
| 326 | try: |
---|
| 327 | if hasattr(self.data,"data"): |
---|
| 328 | self.compute_chisqr2D() |
---|
| 329 | return |
---|
| 330 | else: |
---|
| 331 | self.qmin_x = float(self.qmin.GetValue()) |
---|
| 332 | self.qmax_x = float(self.qmax.GetValue()) |
---|
[fbc3e04] | 333 | # return residuals within self.qmin_x and self.qmax_x |
---|
[26bf293] | 334 | x,y,dy = [numpy.asarray(v) for v in (self.data.x,self.data.y,self.data.dy)] |
---|
| 335 | if self.qmin_x==None and self.qmax_x==None: |
---|
| 336 | fx =numpy.asarray([self.model.run(v) for v in x]) |
---|
[8f274b11] | 337 | temp=(y - fx)/dy |
---|
| 338 | res= temp*temp |
---|
[26bf293] | 339 | else: |
---|
| 340 | idx = (x>= self.qmin_x) & (x <=self.qmax_x) |
---|
| 341 | fx = numpy.asarray([self.model.run(item)for item in x[idx ]]) |
---|
[8f274b11] | 342 | temp=(y[idx] - fx)/dy[idx] |
---|
| 343 | res= temp*temp |
---|
[22e8d49] | 344 | #sum of residuals |
---|
[26bf293] | 345 | sum=0 |
---|
| 346 | for item in res: |
---|
| 347 | if numpy.isfinite(item): |
---|
[8f274b11] | 348 | sum +=item |
---|
[1309e919] | 349 | self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res)))) |
---|
[26bf293] | 350 | except: |
---|
[fbc3e04] | 351 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 352 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
[22e8d49] | 353 | return |
---|
[26bf293] | 354 | |
---|
[d89f09b] | 355 | def _on_select_model(self,event): |
---|
| 356 | """ |
---|
| 357 | react when a model is selected from page's combo box |
---|
| 358 | post an event to its owner to draw an appropriate theory |
---|
| 359 | """ |
---|
[44bbf6a] | 360 | self.btFit.SetFocus() |
---|
[26bf293] | 361 | self.disable_disp.SetValue(True) |
---|
| 362 | self.sizer8.Clear(True) |
---|
| 363 | self.sizer7.Clear(True) |
---|
| 364 | self.vbox.Layout() |
---|
| 365 | self.SetScrollbars(20,20,55,40) |
---|
| 366 | self.Layout() |
---|
| 367 | self.parent.GetSizer().Layout() |
---|
[72637e8] | 368 | |
---|
[d89f09b] | 369 | for item in self.model_list_box.itervalues(): |
---|
[00561739] | 370 | name = item.__name__ |
---|
| 371 | if hasattr(item, "name"): |
---|
| 372 | name = item.name |
---|
[22e8d49] | 373 | |
---|
[72637e8] | 374 | if name == None: |
---|
| 375 | self.qmin.Disable() |
---|
| 376 | self.qmax.Disable() |
---|
| 377 | else: |
---|
| 378 | self.qmin.Enable() |
---|
| 379 | self.qmax.Enable() |
---|
| 380 | |
---|
[00561739] | 381 | if name ==event.GetString(): |
---|
| 382 | try: |
---|
[fbc3e04] | 383 | self.model=item() |
---|
| 384 | self.back_up_model= self.model.clone() |
---|
| 385 | evt = ModelEventbox(model=self.model,name=name) |
---|
| 386 | wx.PostEvent(self.event_owner, evt) |
---|
| 387 | self.text1_1.Show() |
---|
[44999f3] | 388 | self.compute_chisqr() |
---|
[94999eb] | 389 | self.tcChi.Show() |
---|
[00561739] | 390 | except: |
---|
[fbc3e04] | 391 | raise #ValueError,"model.name is not equal to model class name" |
---|
[22e8d49] | 392 | break |
---|
[fbc3e04] | 393 | |
---|
[22e8d49] | 394 | |
---|
[26bf293] | 395 | def onFit(self,event): |
---|
| 396 | """ signal for fitting""" |
---|
| 397 | |
---|
| 398 | flag=self.checkFitRange() |
---|
| 399 | self.set_manager(self.manager) |
---|
| 400 | |
---|
| 401 | self.qmin_x=float(self.qmin.GetValue()) |
---|
| 402 | self.qmax_x =float( self.qmax.GetValue()) |
---|
| 403 | if len(self.param_toFit) >0 and flag==True: |
---|
| 404 | self.manager.schedule_for_fit( value=1,fitproblem =None) |
---|
[3e8e627] | 405 | if hasattr(self.data, "data"): |
---|
[22e8d49] | 406 | #single fit for Data2D |
---|
[4cb07c0] | 407 | self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x, |
---|
[e191da5] | 408 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
| 409 | xmin=self.data.xmin,xmax=self.data.xmax) |
---|
[3e8e627] | 410 | else: |
---|
[22e8d49] | 411 | #single fit for Data1D |
---|
| 412 | self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x) |
---|
| 413 | |
---|
[51d47b5] | 414 | self.vbox.Layout() |
---|
| 415 | self.SetScrollbars(20,20,55,40) |
---|
| 416 | self.Layout() |
---|
| 417 | self.parent.GetSizer().Layout() |
---|
[26bf293] | 418 | else: |
---|
[fbc3e04] | 419 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
| 420 | "Select at least one parameter to fit ")) |
---|
[26bf293] | 421 | |
---|
[fbc3e04] | 422 | |
---|
| 423 | def _onTextEnter(self,event): |
---|
| 424 | """ |
---|
| 425 | set a flag to determine if the fitting range entered by the user is valid |
---|
| 426 | """ |
---|
| 427 | |
---|
| 428 | try: |
---|
| 429 | flag=self.checkFitRange() |
---|
| 430 | if flag==True and self.model!=None: |
---|
| 431 | self.manager.redraw_model(float(self.xmin.GetValue())\ |
---|
| 432 | ,float(self.xmax.GetValue())) |
---|
| 433 | except: |
---|
| 434 | |
---|
| 435 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 436 | "Drawing Error:wrong value entered %s"% sys.exc_value)) |
---|
| 437 | |
---|
| 438 | |
---|
[d89f09b] | 439 | |
---|
| 440 | def get_param_list(self): |
---|
| 441 | """ |
---|
| 442 | @return self.param_toFit: list containing references to TextCtrl |
---|
| 443 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
| 444 | @raise: if return an empty list of parameter fit will nnote work |
---|
| 445 | properly so raise ValueError,"missing parameter to fit" |
---|
| 446 | """ |
---|
| 447 | if self.param_toFit !=[]: |
---|
| 448 | return self.param_toFit |
---|
| 449 | else: |
---|
| 450 | raise ValueError,"missing parameter to fit" |
---|
| 451 | |
---|
[26bf293] | 452 | |
---|
[d89f09b] | 453 | def _onparamEnter(self,event): |
---|
| 454 | """ |
---|
| 455 | when enter value on panel redraw model according to changed |
---|
| 456 | """ |
---|
[e9b4cc4] | 457 | self.set_model() |
---|
[22e8d49] | 458 | try: |
---|
| 459 | self.compute_chisqr() |
---|
| 460 | except: |
---|
| 461 | pass |
---|
[0550752] | 462 | |
---|
[e9b4cc4] | 463 | def set_model(self): |
---|
[c77f72c] | 464 | """ |
---|
| 465 | Hide panel object related to the previous fit and set |
---|
| 466 | values entered by the used inside the model |
---|
| 467 | """ |
---|
[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 | pass |
---|
[e9b4cc4] | 478 | self.set_model_parameter() |
---|
| 479 | |
---|
[26bf293] | 480 | |
---|
[d89f09b] | 481 | def select_all_param(self,event): |
---|
| 482 | """ |
---|
| 483 | set to true or false all checkBox given the main checkbox value cb1 |
---|
| 484 | """ |
---|
[94999eb] | 485 | self.select_all_param_helper() |
---|
[d89f09b] | 486 | |
---|
| 487 | |
---|
| 488 | def select_param(self,event): |
---|
| 489 | """ |
---|
| 490 | Select TextCtrl checked for fitting purpose and stores them |
---|
| 491 | in self.param_toFit=[] list |
---|
| 492 | """ |
---|
| 493 | self.param_toFit=[] |
---|
| 494 | for item in self.parameters: |
---|
[c77f72c] | 495 | #Select parameters to fit for list of primary parameters |
---|
[d89f09b] | 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: |
---|
[c77f72c] | 501 | #remove parameters from the fitting list |
---|
[d89f09b] | 502 | if item in self.param_toFit: |
---|
| 503 | self.param_toFit.remove(item) |
---|
[c77f72c] | 504 | #Select parameters to fit for list of fittable parameters with dispersion |
---|
[6b44403] | 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: |
---|
[c77f72c] | 511 | #remove parameters from the fitting list |
---|
[6b44403] | 512 | if item in self.param_toFit: |
---|
| 513 | self.param_toFit.remove(item) |
---|
[fbc3e04] | 514 | #Set the value of checkbox that selected every checkbox or not |
---|
[6b44403] | 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) |
---|
[22e8d49] | 519 | |
---|
| 520 | |
---|
[d89f09b] | 521 | def onsetValues(self,chisqr, out,cov): |
---|
| 522 | """ |
---|
| 523 | Build the panel from the fit result |
---|
| 524 | @param chisqr:Value of the goodness of fit metric |
---|
| 525 | @param out:list of parameter with the best value found during fitting |
---|
| 526 | @param cov:Covariance matrix |
---|
| 527 | |
---|
| 528 | """ |
---|
[26bf293] | 529 | self.tcChi.SetLabel(format_number(chisqr)) |
---|
[d89f09b] | 530 | params = {} |
---|
| 531 | is_modified = False |
---|
| 532 | has_error = False |
---|
[c77f72c] | 533 | #set the panel when fit result are float not list |
---|
[d89f09b] | 534 | if out.__class__==numpy.float64: |
---|
| 535 | self.param_toFit[0][1].SetValue(format_number(out)) |
---|
| 536 | self.param_toFit[0][1].Refresh() |
---|
| 537 | if cov !=None : |
---|
| 538 | self.text2_3.Show() |
---|
| 539 | self.param_toFit[0][2].Show() |
---|
| 540 | self.param_toFit[0][3].Clear() |
---|
| 541 | self.param_toFit[0][3].SetValue(format_number(cov[0])) |
---|
| 542 | self.param_toFit[0][3].Show() |
---|
| 543 | else: |
---|
| 544 | i=0 |
---|
[d63ef2f] | 545 | j=0 |
---|
[c77f72c] | 546 | #Set the panel when fit result are list |
---|
[d89f09b] | 547 | for item in self.param_toFit: |
---|
| 548 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
[442895f] | 549 | item[1].SetValue(format_number(self.model.getParam(item[0].GetLabelText()))) |
---|
[d63ef2f] | 550 | item[1].Refresh() |
---|
| 551 | if(cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
[d89f09b] | 552 | self.text2_3.Show() |
---|
| 553 | item[2].Show() |
---|
| 554 | item[3].Clear() |
---|
[d63ef2f] | 555 | for j in range(len(out)): |
---|
[c77f72c] | 556 | if out[j]==self.model.getParam(item[0].GetLabelText()): |
---|
[d63ef2f] | 557 | break |
---|
| 558 | item[3].SetValue(format_number(cov[j])) |
---|
[d89f09b] | 559 | item[3].Show() |
---|
| 560 | i+=1 |
---|
| 561 | |
---|
| 562 | self.vbox.Layout() |
---|
[0aac36f] | 563 | self.SetScrollbars(20,20,55,40) |
---|
| 564 | self.Layout() |
---|
[d89f09b] | 565 | self.GrandParent.GetSizer().Layout() |
---|
[a92d51b] | 566 | |
---|
| 567 | |
---|
[55e13ab] | 568 | def onSmear(self, event): |
---|
[c77f72c] | 569 | """ |
---|
| 570 | Create a smear object that will change the way residuals |
---|
| 571 | are compute when fitting |
---|
| 572 | """ |
---|
[ef8b580] | 573 | smear =None |
---|
[cce33b3] | 574 | msg="" |
---|
[ef8b580] | 575 | if self.enable_smearer.GetValue(): |
---|
[55e13ab] | 576 | from DataLoader.qsmearing import smear_selection |
---|
| 577 | smear =smear_selection( self.data ) |
---|
[5150250] | 578 | if hasattr(self.data,"dx"): |
---|
[cce33b3] | 579 | msg= ": Resolution smearing parameters" |
---|
[5150250] | 580 | if hasattr(self.data,"dxl"): |
---|
| 581 | msg= ": Slit_l smearing parameters" |
---|
[cce33b3] | 582 | if hasattr(self.data,"dxw"): |
---|
[5150250] | 583 | msg= ": Slit_w smearing parameters" |
---|
[ef8b580] | 584 | if smear ==None: |
---|
| 585 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
| 586 | "Data contains no smearing information")) |
---|
| 587 | else: |
---|
| 588 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
[cce33b3] | 589 | "Data contains smearing information %s"%msg)) |
---|
[ef8b580] | 590 | self.manager.set_smearer(smear) |
---|
| 591 | |
---|
| 592 | |
---|
| 593 | |
---|
[08b9c6c8] | 594 | |
---|