[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 |
---|
[bb18ef1] | 142 | ## structure combox box |
---|
| 143 | self.structbox = wx.ComboBox(self, -1) |
---|
| 144 | # define combox box |
---|
[26bf293] | 145 | self.modelbox = wx.ComboBox(self, -1) |
---|
[bb18ef1] | 146 | |
---|
[94999eb] | 147 | self.tcChi = wx.StaticText(self, -1, str(0), style=wx.ALIGN_LEFT) |
---|
| 148 | self.tcChi.Hide() |
---|
| 149 | self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT) |
---|
| 150 | self.text1_1.Hide() |
---|
[08b9c6c8] | 151 | ix = 0 |
---|
[26bf293] | 152 | iy = 1 |
---|
| 153 | self.sizer4.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
[d89f09b] | 154 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[bb18ef1] | 155 | |
---|
[d89f09b] | 156 | ix += 1 |
---|
[26bf293] | 157 | self.sizer4.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[bb18ef1] | 158 | ix +=1 |
---|
| 159 | self.text_mult= wx.StaticText(self,-1,' x ') |
---|
| 160 | self.sizer4.Add(self.text_mult,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 161 | ix += 1 |
---|
| 162 | self.sizer4.Add(self.structbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 163 | |
---|
| 164 | |
---|
| 165 | |
---|
[94999eb] | 166 | ix += 1 |
---|
| 167 | self.sizer4.Add(self.text1_1,(iy,ix),(1,1),\ |
---|
| 168 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 169 | ix += 1 |
---|
| 170 | self.sizer4.Add(self.tcChi,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[26bf293] | 171 | #----------sizer6------------------------------------------------- |
---|
[22e8d49] | 172 | #Sizer containing info on polydispersity |
---|
[26bf293] | 173 | self.disable_disp = wx.RadioButton(self, -1, 'No', (10, 10), style=wx.RB_GROUP) |
---|
| 174 | self.enable_disp = wx.RadioButton(self, -1, 'Yes', (10, 30)) |
---|
[fbc3e04] | 175 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.disable_disp.GetId()) |
---|
| 176 | self.Bind(wx.EVT_RADIOBUTTON, self.Set_DipersParam, id=self.enable_disp.GetId()) |
---|
[26bf293] | 177 | ix= 0 |
---|
| 178 | iy=1 |
---|
| 179 | self.sizer6.Add(wx.StaticText(self,-1,'Polydispersity: '),(iy,ix),(1,1)\ |
---|
| 180 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[9d31a8b] | 181 | ix += 1 |
---|
[26bf293] | 182 | self.sizer6.Add(self.enable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 183 | ix += 1 |
---|
| 184 | self.sizer6.Add(self.disable_disp ,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 185 | ix =0 |
---|
| 186 | iy+=1 |
---|
| 187 | self.sizer6.Add((20,20),(iy,ix),(1,1),wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 188 | #---------sizer 9 draw---------------------------------------- |
---|
[94999eb] | 189 | |
---|
[26bf293] | 190 | |
---|
| 191 | id = wx.NewId() |
---|
| 192 | self.btFit =wx.Button(self,id,'Fit') |
---|
| 193 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
| 194 | self.btFit.SetToolTipString("Perform fit.") |
---|
[22e8d49] | 195 | ## Q range |
---|
[44999f3] | 196 | # Reversed to the codes; Need to think carefully about consistency in q between 2D plot and fitting |
---|
[26bf293] | 197 | if not hasattr(self.data,"data"): |
---|
| 198 | self.qmin_x= numpy.min(self.data.x) |
---|
| 199 | self.qmax_x= numpy.max(self.data.x) |
---|
[3e8e627] | 200 | self.num_points= len(self.data.x) |
---|
[26bf293] | 201 | else: |
---|
[44999f3] | 202 | # Reversed to the codes; Need to think carefully about consistency in q between 2D plot and fitting |
---|
[c4550b0] | 203 | radius1= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymin, 2)) |
---|
| 204 | radius2= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymin, 2)) |
---|
| 205 | radius3= math.sqrt(math.pow(self.data.xmin, 2)+ math.pow(self.data.ymax, 2)) |
---|
| 206 | radius4= math.sqrt(math.pow(self.data.xmax, 2)+ math.pow(self.data.ymax, 2)) |
---|
[22e8d49] | 207 | |
---|
| 208 | self.qmin_x= 0 |
---|
[4cb07c0] | 209 | self.qmax_x= math.sqrt(math.pow(max(math.fabs(self.data.xmax),math.fabs(self.data.xmin)),2) |
---|
| 210 | +math.pow(max(math.fabs(self.data.ymax),math.fabs(self.data.ymin)),2))#self.data.xmax |
---|
[44999f3] | 211 | |
---|
[888e62c] | 212 | self.num_points= 100 |
---|
[22e8d49] | 213 | # The minimum fitting range |
---|
[26bf293] | 214 | self.qmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 215 | self.qmin.SetValue(str(format_number(self.qmin_x))) |
---|
| 216 | self.qmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
| 217 | self.qmin.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 218 | self.qmin.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
[4cb07c0] | 219 | self.qmin.Enable() |
---|
[22e8d49] | 220 | #The maximum fitting range |
---|
[26bf293] | 221 | self.qmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 222 | self.qmax.SetValue(str(format_number(self.qmax_x))) |
---|
| 223 | self.qmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
| 224 | self.qmax.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 225 | self.qmax.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
[4cb07c0] | 226 | self.qmax.Enable() |
---|
[22e8d49] | 227 | #The number of points to plots |
---|
[26bf293] | 228 | self.npts = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 229 | self.npts.SetValue(format_number(self.num_points)) |
---|
| 230 | self.npts.SetToolTipString("Number of point to plot.") |
---|
| 231 | self.npts.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 232 | self.npts.Bind(wx.EVT_TEXT_ENTER, self._onparamEnter) |
---|
| 233 | self.npts.Disable() |
---|
| 234 | self.npts.Hide() |
---|
| 235 | ix = 0 |
---|
| 236 | iy = 1 |
---|
| 237 | self.sizer9.Add(wx.StaticText(self, -1, 'Fitting Range'),(iy, ix),(1,1),\ |
---|
[d89f09b] | 238 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[26bf293] | 239 | ix += 1 |
---|
| 240 | self.sizer9.Add(wx.StaticText(self, -1, 'Min'),(iy, ix),(1,1),\ |
---|
[d89f09b] | 241 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[26bf293] | 242 | ix += 1 |
---|
| 243 | self.sizer9.Add(wx.StaticText(self, -1, 'Max'),(iy, ix),(1,1),\ |
---|
| 244 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[d89f09b] | 245 | ix = 0 |
---|
| 246 | iy += 1 |
---|
[8aac6e1] | 247 | self.sizer9.Add(wx.StaticText(self, -1, 'Q range'),(iy, ix),(1,1),\ |
---|
[9d31a8b] | 248 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[d89f09b] | 249 | ix += 1 |
---|
[26bf293] | 250 | self.sizer9.Add(self.qmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 251 | ix += 1 |
---|
| 252 | self.sizer9.Add(self.qmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 253 | ix += 1 |
---|
| 254 | self.sizer9.Add(self.npts,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[94999eb] | 255 | |
---|
[d89f09b] | 256 | ix += 1 |
---|
[26bf293] | 257 | self.sizer9.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[22e8d49] | 258 | |
---|
[26bf293] | 259 | ix =0 |
---|
| 260 | iy+=1 |
---|
| 261 | self.sizer9.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[fbc3e04] | 262 | |
---|
| 263 | # Contains link between model ,all its parameters, and panel organization |
---|
| 264 | self.parameters=[] |
---|
| 265 | # Contains list of parameters that cannot be fitted and reference to |
---|
| 266 | #panel objects |
---|
| 267 | self.fixed_param=[] |
---|
| 268 | # Contains list of parameters with dispersity and reference to |
---|
| 269 | #panel objects |
---|
| 270 | self.fittable_param=[] |
---|
| 271 | #list of dispersion paramaters |
---|
| 272 | self.disp_list=[] |
---|
| 273 | #contains link between a model and selected parameters to fit |
---|
| 274 | self.param_toFit=[] |
---|
| 275 | # model on which the fit would be performed |
---|
| 276 | self.model=None |
---|
[bb18ef1] | 277 | |
---|
[fbc3e04] | 278 | self.back_up_model= None |
---|
| 279 | #dictionary of model name and model class |
---|
| 280 | self.model_list_box={} |
---|
| 281 | |
---|
| 282 | if self.model == None: |
---|
| 283 | self.qmin.Disable() |
---|
| 284 | self.qmax.Disable() |
---|
| 285 | else: |
---|
| 286 | self.qmin.Enable() |
---|
| 287 | self.qmax.Enable() |
---|
| 288 | |
---|
[22e8d49] | 289 | |
---|
[f39511b] | 290 | self.vbox.Layout() |
---|
| 291 | self.vbox.Fit(self) |
---|
| 292 | self.SetSizer(self.vbox) |
---|
| 293 | self.SetScrollbars(20,20,55,40) |
---|
| 294 | |
---|
[fbc3e04] | 295 | self.Centre() |
---|
| 296 | self.Layout() |
---|
[bb18ef1] | 297 | |
---|
| 298 | self.parent.GetSizer().Layout() |
---|
[fbc3e04] | 299 | |
---|
[26bf293] | 300 | def compute_chisqr2D(self): |
---|
[22e8d49] | 301 | """ |
---|
| 302 | compute chi square given a model and data 2D and set the value |
---|
| 303 | to the tcChi txtcrl |
---|
[442895f] | 304 | """ |
---|
[bb18ef1] | 305 | from sans.guiframe.utils import check_value |
---|
| 306 | flag = check_value( self.qmin, self.qmax) |
---|
| 307 | #flag=self.checkFitRange() |
---|
[26bf293] | 308 | res=[] |
---|
[442895f] | 309 | if flag== True: |
---|
[948add7] | 310 | try: |
---|
[26bf293] | 311 | self.qmin_x = float(self.qmin.GetValue()) |
---|
| 312 | self.qmax_x = float(self.qmax.GetValue()) |
---|
| 313 | for i in range(len(self.data.x_bins)): |
---|
[22e8d49] | 314 | for j in range(len(self.data.y_bins)): |
---|
| 315 | #Check the range containing data between self.qmin_x and self.qmax_x |
---|
[fbc3e04] | 316 | if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)>=math.pow(self.qmin_x,2): |
---|
| 317 | if math.pow(self.data.x_bins[i],2)+math.pow(self.data.y_bins[j],2)<=math.pow(self.qmax_x,2): |
---|
| 318 | chisqrji=(self.data.data[j][i]- self.model.runXY(\ |
---|
| 319 | [self.data.y_bins[j],self.data.x_bins[i]]))\ |
---|
| 320 | /self.data.err_data[j][i] |
---|
[22e8d49] | 321 | #Vector containing residuals |
---|
| 322 | res.append( math.pow(chisqrji,2) ) |
---|
| 323 | # compute sum of residual |
---|
[948add7] | 324 | sum=0 |
---|
| 325 | for item in res: |
---|
| 326 | if numpy.isfinite(item): |
---|
| 327 | sum +=item |
---|
[1309e919] | 328 | self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res)))) |
---|
[948add7] | 329 | except: |
---|
[fbc3e04] | 330 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 331 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
[22e8d49] | 332 | return |
---|
| 333 | |
---|
[f3113c9] | 334 | |
---|
[26bf293] | 335 | def compute_chisqr(self): |
---|
[22e8d49] | 336 | """ |
---|
| 337 | compute chi square given a model and data 1D and set the value |
---|
| 338 | to the tcChi txtcrl |
---|
[26bf293] | 339 | """ |
---|
[bb18ef1] | 340 | from sans.guiframe.utils import check_value |
---|
| 341 | flag = check_value( self.qmin, self.qmax) |
---|
| 342 | #flag=self.checkFitRange() |
---|
[26bf293] | 343 | if flag== True: |
---|
| 344 | try: |
---|
| 345 | if hasattr(self.data,"data"): |
---|
| 346 | self.compute_chisqr2D() |
---|
| 347 | return |
---|
| 348 | else: |
---|
| 349 | self.qmin_x = float(self.qmin.GetValue()) |
---|
| 350 | self.qmax_x = float(self.qmax.GetValue()) |
---|
[fbc3e04] | 351 | # return residuals within self.qmin_x and self.qmax_x |
---|
[26bf293] | 352 | x,y,dy = [numpy.asarray(v) for v in (self.data.x,self.data.y,self.data.dy)] |
---|
| 353 | if self.qmin_x==None and self.qmax_x==None: |
---|
| 354 | fx =numpy.asarray([self.model.run(v) for v in x]) |
---|
[8f274b11] | 355 | temp=(y - fx)/dy |
---|
| 356 | res= temp*temp |
---|
[26bf293] | 357 | else: |
---|
| 358 | idx = (x>= self.qmin_x) & (x <=self.qmax_x) |
---|
| 359 | fx = numpy.asarray([self.model.run(item)for item in x[idx ]]) |
---|
[8f274b11] | 360 | temp=(y[idx] - fx)/dy[idx] |
---|
| 361 | res= temp*temp |
---|
[22e8d49] | 362 | #sum of residuals |
---|
[26bf293] | 363 | sum=0 |
---|
| 364 | for item in res: |
---|
| 365 | if numpy.isfinite(item): |
---|
[8f274b11] | 366 | sum +=item |
---|
[1309e919] | 367 | self.tcChi.SetLabel(format_number(math.fabs(sum/ len(res)))) |
---|
[26bf293] | 368 | except: |
---|
[fbc3e04] | 369 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 370 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
[22e8d49] | 371 | return |
---|
[26bf293] | 372 | |
---|
[d89f09b] | 373 | def _on_select_model(self,event): |
---|
| 374 | """ |
---|
| 375 | react when a model is selected from page's combo box |
---|
| 376 | post an event to its owner to draw an appropriate theory |
---|
| 377 | """ |
---|
[44bbf6a] | 378 | self.btFit.SetFocus() |
---|
[26bf293] | 379 | self.disable_disp.SetValue(True) |
---|
| 380 | self.sizer8.Clear(True) |
---|
| 381 | self.sizer7.Clear(True) |
---|
| 382 | self.vbox.Layout() |
---|
| 383 | self.SetScrollbars(20,20,55,40) |
---|
| 384 | self.Layout() |
---|
| 385 | self.parent.GetSizer().Layout() |
---|
[72637e8] | 386 | |
---|
[bb18ef1] | 387 | |
---|
| 388 | name= self.modelbox.GetString(self.modelbox.GetCurrentSelection()) |
---|
| 389 | form_factor =self.modelbox.GetClientData(self.modelbox.GetCurrentSelection()) |
---|
| 390 | struct_factor =self.structbox.GetClientData(self.structbox.GetCurrentSelection()) |
---|
| 391 | |
---|
| 392 | |
---|
| 393 | if form_factor!="separator": |
---|
| 394 | if struct_factor != None and struct_factor.__name__ != "NoStructure": |
---|
| 395 | from sans.models.MultiplicationModel import MultiplicationModel |
---|
| 396 | self.model= MultiplicationModel(form_factor(),struct_factor()) |
---|
| 397 | else: |
---|
| 398 | self.model= form_factor() |
---|
| 399 | self.back_up_model= self.model.clone() |
---|
| 400 | name= self.model.name |
---|
[72637e8] | 401 | if name == None: |
---|
| 402 | self.qmin.Disable() |
---|
| 403 | self.qmax.Disable() |
---|
| 404 | else: |
---|
| 405 | self.qmin.Enable() |
---|
| 406 | self.qmax.Enable() |
---|
[bb18ef1] | 407 | evt = ModelEventbox(model=self.model,name=name) |
---|
| 408 | wx.PostEvent(self.event_owner, evt) |
---|
| 409 | self.text1_1.Show() |
---|
| 410 | self.tcChi.Show() |
---|
| 411 | self.compute_chisqr() |
---|
| 412 | |
---|
[72637e8] | 413 | |
---|
[bb18ef1] | 414 | else: |
---|
| 415 | self.model=None |
---|
| 416 | msg= " Select non - model value:%s ! Previous Model reloaded "%name |
---|
| 417 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg)) |
---|
| 418 | |
---|
| 419 | self.set_panel(self.model) |
---|
[fbc3e04] | 420 | |
---|
[22e8d49] | 421 | |
---|
[26bf293] | 422 | def onFit(self,event): |
---|
[bb18ef1] | 423 | """ |
---|
| 424 | signal for fitting .Perform single fit only |
---|
| 425 | """ |
---|
| 426 | from sans.guiframe.utils import check_value |
---|
| 427 | flag = check_value( self.qmin, self.qmax) |
---|
| 428 | |
---|
| 429 | if not flag: |
---|
| 430 | msg= "Fitting range invalid" |
---|
| 431 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
| 432 | return |
---|
| 433 | |
---|
| 434 | if len(self.param_toFit) <= 0: |
---|
| 435 | msg= "Select at least one parameter to fit" |
---|
| 436 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
| 437 | return |
---|
| 438 | |
---|
[26bf293] | 439 | self.qmin_x=float(self.qmin.GetValue()) |
---|
| 440 | self.qmax_x =float( self.qmax.GetValue()) |
---|
[bb18ef1] | 441 | |
---|
| 442 | self.manager.schedule_for_fit( value=1,fitproblem =None) |
---|
| 443 | if hasattr(self.data, "data"): |
---|
| 444 | #single fit for Data2D |
---|
| 445 | self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x, |
---|
| 446 | ymin=self.data.ymin, ymax=self.data.ymax, |
---|
| 447 | xmin=self.data.xmin,xmax=self.data.xmax) |
---|
| 448 | else: |
---|
| 449 | #single fit for Data1D |
---|
| 450 | self.manager._on_single_fit(qmin=self.qmin_x,qmax=self.qmax_x) |
---|
[22e8d49] | 451 | |
---|
[51d47b5] | 452 | self.vbox.Layout() |
---|
| 453 | self.SetScrollbars(20,20,55,40) |
---|
| 454 | self.Layout() |
---|
| 455 | self.parent.GetSizer().Layout() |
---|
[fbc3e04] | 456 | |
---|
[bb18ef1] | 457 | |
---|
| 458 | |
---|
[fbc3e04] | 459 | |
---|
[d89f09b] | 460 | |
---|
| 461 | def get_param_list(self): |
---|
| 462 | """ |
---|
| 463 | @return self.param_toFit: list containing references to TextCtrl |
---|
| 464 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
| 465 | @raise: if return an empty list of parameter fit will nnote work |
---|
| 466 | properly so raise ValueError,"missing parameter to fit" |
---|
| 467 | """ |
---|
| 468 | if self.param_toFit !=[]: |
---|
| 469 | return self.param_toFit |
---|
| 470 | else: |
---|
| 471 | raise ValueError,"missing parameter to fit" |
---|
| 472 | |
---|
[26bf293] | 473 | |
---|
[d89f09b] | 474 | def _onparamEnter(self,event): |
---|
| 475 | """ |
---|
| 476 | when enter value on panel redraw model according to changed |
---|
| 477 | """ |
---|
[e9b4cc4] | 478 | self.set_model() |
---|
[22e8d49] | 479 | try: |
---|
| 480 | self.compute_chisqr() |
---|
| 481 | except: |
---|
| 482 | pass |
---|
[0550752] | 483 | |
---|
[e9b4cc4] | 484 | def set_model(self): |
---|
[c77f72c] | 485 | """ |
---|
| 486 | Hide panel object related to the previous fit and set |
---|
| 487 | values entered by the used inside the model |
---|
| 488 | """ |
---|
[d89f09b] | 489 | if len(self.parameters) !=0 and self.model !=None: |
---|
[d15c0202] | 490 | # Flag to register when a parameter has changed. |
---|
[d89f09b] | 491 | for item in self.parameters: |
---|
| 492 | try: |
---|
[d23544dc] | 493 | self.text2_3.Hide() |
---|
[0550752] | 494 | item[2].Hide() |
---|
| 495 | item[3].Clear() |
---|
| 496 | item[3].Hide() |
---|
[08b9c6c8] | 497 | except: |
---|
[26bf293] | 498 | pass |
---|
[e9b4cc4] | 499 | self.set_model_parameter() |
---|
| 500 | |
---|
[26bf293] | 501 | |
---|
[d89f09b] | 502 | def select_all_param(self,event): |
---|
| 503 | """ |
---|
| 504 | set to true or false all checkBox given the main checkbox value cb1 |
---|
| 505 | """ |
---|
[94999eb] | 506 | self.select_all_param_helper() |
---|
[d89f09b] | 507 | |
---|
| 508 | |
---|
| 509 | def select_param(self,event): |
---|
| 510 | """ |
---|
| 511 | Select TextCtrl checked for fitting purpose and stores them |
---|
| 512 | in self.param_toFit=[] list |
---|
| 513 | """ |
---|
| 514 | self.param_toFit=[] |
---|
| 515 | for item in self.parameters: |
---|
[c77f72c] | 516 | #Select parameters to fit for list of primary parameters |
---|
[d89f09b] | 517 | if item[0].GetValue()==True: |
---|
| 518 | list= [item[0],item[1],item[2],item[3]] |
---|
[89032bf] | 519 | if not (list in self.param_toFit): |
---|
| 520 | self.param_toFit.append(list ) |
---|
[d89f09b] | 521 | else: |
---|
[c77f72c] | 522 | #remove parameters from the fitting list |
---|
[d89f09b] | 523 | if item in self.param_toFit: |
---|
| 524 | self.param_toFit.remove(item) |
---|
[c77f72c] | 525 | #Select parameters to fit for list of fittable parameters with dispersion |
---|
[6b44403] | 526 | for item in self.fittable_param: |
---|
| 527 | if item[0].GetValue()==True: |
---|
| 528 | list= [item[0],item[1],item[2],item[3]] |
---|
| 529 | if not (list in self.param_toFit): |
---|
| 530 | self.param_toFit.append(list ) |
---|
| 531 | else: |
---|
[c77f72c] | 532 | #remove parameters from the fitting list |
---|
[6b44403] | 533 | if item in self.param_toFit: |
---|
| 534 | self.param_toFit.remove(item) |
---|
[fbc3e04] | 535 | #Set the value of checkbox that selected every checkbox or not |
---|
[6b44403] | 536 | if len(self.parameters)+len(self.fittable_param) ==len(self.param_toFit): |
---|
[d89f09b] | 537 | self.cb1.SetValue(True) |
---|
| 538 | else: |
---|
| 539 | self.cb1.SetValue(False) |
---|
[22e8d49] | 540 | |
---|
| 541 | |
---|
[d89f09b] | 542 | def onsetValues(self,chisqr, out,cov): |
---|
| 543 | """ |
---|
| 544 | Build the panel from the fit result |
---|
| 545 | @param chisqr:Value of the goodness of fit metric |
---|
| 546 | @param out:list of parameter with the best value found during fitting |
---|
| 547 | @param cov:Covariance matrix |
---|
| 548 | |
---|
| 549 | """ |
---|
[26bf293] | 550 | self.tcChi.SetLabel(format_number(chisqr)) |
---|
[d89f09b] | 551 | params = {} |
---|
| 552 | is_modified = False |
---|
| 553 | has_error = False |
---|
[c77f72c] | 554 | #set the panel when fit result are float not list |
---|
[d89f09b] | 555 | if out.__class__==numpy.float64: |
---|
| 556 | self.param_toFit[0][1].SetValue(format_number(out)) |
---|
| 557 | self.param_toFit[0][1].Refresh() |
---|
| 558 | if cov !=None : |
---|
| 559 | self.text2_3.Show() |
---|
| 560 | self.param_toFit[0][2].Show() |
---|
| 561 | self.param_toFit[0][3].Clear() |
---|
| 562 | self.param_toFit[0][3].SetValue(format_number(cov[0])) |
---|
| 563 | self.param_toFit[0][3].Show() |
---|
| 564 | else: |
---|
| 565 | i=0 |
---|
[d63ef2f] | 566 | j=0 |
---|
[c77f72c] | 567 | #Set the panel when fit result are list |
---|
[d89f09b] | 568 | for item in self.param_toFit: |
---|
| 569 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
[442895f] | 570 | item[1].SetValue(format_number(self.model.getParam(item[0].GetLabelText()))) |
---|
[d63ef2f] | 571 | item[1].Refresh() |
---|
| 572 | if(cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
[d89f09b] | 573 | self.text2_3.Show() |
---|
| 574 | item[2].Show() |
---|
| 575 | item[3].Clear() |
---|
[d63ef2f] | 576 | for j in range(len(out)): |
---|
[c77f72c] | 577 | if out[j]==self.model.getParam(item[0].GetLabelText()): |
---|
[d63ef2f] | 578 | break |
---|
| 579 | item[3].SetValue(format_number(cov[j])) |
---|
[d89f09b] | 580 | item[3].Show() |
---|
| 581 | i+=1 |
---|
| 582 | |
---|
| 583 | self.vbox.Layout() |
---|
[0aac36f] | 584 | self.SetScrollbars(20,20,55,40) |
---|
| 585 | self.Layout() |
---|
[d89f09b] | 586 | self.GrandParent.GetSizer().Layout() |
---|
[a92d51b] | 587 | |
---|
| 588 | |
---|
[55e13ab] | 589 | def onSmear(self, event): |
---|
[c77f72c] | 590 | """ |
---|
| 591 | Create a smear object that will change the way residuals |
---|
| 592 | are compute when fitting |
---|
| 593 | """ |
---|
[ef8b580] | 594 | smear =None |
---|
[cce33b3] | 595 | msg="" |
---|
[ef8b580] | 596 | if self.enable_smearer.GetValue(): |
---|
[55e13ab] | 597 | from DataLoader.qsmearing import smear_selection |
---|
| 598 | smear =smear_selection( self.data ) |
---|
[5150250] | 599 | if hasattr(self.data,"dxl"): |
---|
[bb18ef1] | 600 | msg= ": Resolution smearing parameters" |
---|
[cce33b3] | 601 | if hasattr(self.data,"dxw"): |
---|
[bb18ef1] | 602 | msg= ": Slit smearing parameters" |
---|
[ef8b580] | 603 | if smear ==None: |
---|
| 604 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
| 605 | "Data contains no smearing information")) |
---|
| 606 | else: |
---|
| 607 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
[cce33b3] | 608 | "Data contains smearing information %s"%msg)) |
---|
[ef8b580] | 609 | self.manager.set_smearer(smear) |
---|
| 610 | |
---|
| 611 | |
---|
| 612 | |
---|
[08b9c6c8] | 613 | |
---|