[d89f09b] | 1 | import sys |
---|
| 2 | import wx |
---|
| 3 | import wx.lib |
---|
[442895f] | 4 | import numpy,math |
---|
[d89f09b] | 5 | import copy |
---|
| 6 | |
---|
| 7 | from sans.guicomm.events import StatusEvent |
---|
| 8 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
| 9 | _BOX_WIDTH = 80 |
---|
| 10 | |
---|
| 11 | def format_number(value, high=False): |
---|
| 12 | """ |
---|
| 13 | Return a float in a standardized, human-readable formatted string |
---|
| 14 | """ |
---|
| 15 | try: |
---|
| 16 | value = float(value) |
---|
| 17 | except: |
---|
| 18 | print "returning 0" |
---|
| 19 | return "0" |
---|
| 20 | |
---|
| 21 | if high: |
---|
| 22 | return "%-6.4g" % value |
---|
| 23 | else: |
---|
| 24 | return "%-5.3g" % value |
---|
| 25 | |
---|
| 26 | |
---|
[f39511b] | 27 | class FitPage2D(wx.ScrolledWindow): |
---|
[d89f09b] | 28 | """ |
---|
| 29 | FitPanel class contains fields allowing to display results when |
---|
| 30 | fitting a model and one data |
---|
| 31 | @note: For Fit to be performed the user should check at least one parameter |
---|
| 32 | on fit Panel window. |
---|
| 33 | |
---|
| 34 | """ |
---|
| 35 | ## Internal name for the AUI manager |
---|
| 36 | window_name = "Fit page" |
---|
| 37 | ## Title to appear on top of the window |
---|
| 38 | window_caption = "Fit Page" |
---|
| 39 | |
---|
| 40 | |
---|
[9d31a8b] | 41 | def __init__(self, parent,data, *args, **kwargs): |
---|
[f39511b] | 42 | wx.ScrolledWindow.__init__(self, parent, *args, **kwargs) |
---|
[d89f09b] | 43 | """ |
---|
| 44 | Initialization of the Panel |
---|
| 45 | """ |
---|
| 46 | self.manager = None |
---|
| 47 | self.parent = parent |
---|
| 48 | self.event_owner=None |
---|
| 49 | #panel interface |
---|
| 50 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
[44bbf6a] | 51 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
[d89f09b] | 52 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
| 53 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
| 54 | self.sizer2 = wx.GridBagSizer(5,5) |
---|
| 55 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
[44bbf6a] | 56 | #self.DataSource = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 57 | #self.DataSource.SetToolTipString("name of data to fit") |
---|
| 58 | #self.DataSource.SetValue(str(data.name)) |
---|
| 59 | #self.DataSource.Disable() |
---|
| 60 | self.DataSource =wx.StaticText(self, -1,str(data.name)) |
---|
[d89f09b] | 61 | self.modelbox = wx.ComboBox(self, -1) |
---|
| 62 | id = wx.NewId() |
---|
| 63 | self.btFit =wx.Button(self,id,'Fit') |
---|
| 64 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
| 65 | self.btFit.SetToolTipString("Perform fit.") |
---|
[44bbf6a] | 66 | self.static_line_1 = wx.StaticLine(self, -1) |
---|
| 67 | |
---|
[d89f09b] | 68 | self.vbox.Add(self.sizer3) |
---|
| 69 | self.vbox.Add(self.sizer2) |
---|
[44bbf6a] | 70 | self.vbox.Add(self.static_line_1, 0, wx.EXPAND, 0) |
---|
| 71 | self.vbox.Add(self.sizer5) |
---|
[d89f09b] | 72 | self.vbox.Add(self.sizer4) |
---|
| 73 | self.vbox.Add(self.sizer1) |
---|
[d15c0202] | 74 | ## Q range |
---|
| 75 | self.qmin= 0.001 |
---|
| 76 | self.qmax= 0.1 |
---|
[d89f09b] | 77 | |
---|
[442895f] | 78 | id = wx.NewId() |
---|
| 79 | self.btClose =wx.Button(self,id,'Close') |
---|
| 80 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
| 81 | self.btClose.SetToolTipString("Close page.") |
---|
[d89f09b] | 82 | ix = 0 |
---|
| 83 | iy = 1 |
---|
| 84 | self.sizer3.Add(wx.StaticText(self, -1, 'Data Source'),(iy,ix),\ |
---|
| 85 | (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 86 | ix += 1 |
---|
| 87 | self.sizer3.Add(self.DataSource,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 88 | ix += 1 |
---|
| 89 | self.sizer3.Add((20,20),(iy,ix),(1,1),wx.RIGHT|wx.EXPAND|wx.ADJUST_MINSIZE,0) |
---|
| 90 | ix = 0 |
---|
| 91 | iy += 1 |
---|
| 92 | self.sizer3.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
| 93 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 94 | ix += 1 |
---|
| 95 | self.sizer3.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[9d31a8b] | 96 | |
---|
| 97 | ix = 0 |
---|
[d89f09b] | 98 | iy = 1 |
---|
[9d31a8b] | 99 | #set maximum range for x in linear scale |
---|
| 100 | self.text4_3 = wx.StaticText(self, -1, 'Maximum Data\n Range (Linear)', style=wx.ALIGN_LEFT) |
---|
| 101 | self.sizer4.Add(self.text4_3,(iy,ix),(1,1),\ |
---|
| 102 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 103 | ix += 1 |
---|
[d89f09b] | 104 | self.text4_1 = wx.StaticText(self, -1, 'Min') |
---|
| 105 | self.sizer4.Add(self.text4_1,(iy, ix),(1,1),\ |
---|
| 106 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[e1a310f] | 107 | #self.text4_1.Hide() |
---|
[d89f09b] | 108 | ix += 2 |
---|
| 109 | self.text4_2 = wx.StaticText(self, -1, 'Max') |
---|
| 110 | self.sizer4.Add(self.text4_2,(iy, ix),(1,1),\ |
---|
| 111 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[e1a310f] | 112 | #self.text4_2.Hide() |
---|
[9d31a8b] | 113 | |
---|
| 114 | #self.text4_3.Hide() |
---|
[d89f09b] | 115 | ix = 0 |
---|
| 116 | iy += 1 |
---|
[9d31a8b] | 117 | self.text4_4 = wx.StaticText(self, -1, 'x range') |
---|
| 118 | self.sizer4.Add(self.text4_4,(iy, ix),(1,1),\ |
---|
| 119 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[d89f09b] | 120 | ix += 1 |
---|
| 121 | self.xmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
[9d31a8b] | 122 | self.xmin.SetValue(format_number(data.xmin)) |
---|
[d89f09b] | 123 | self.xmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
| 124 | self.sizer4.Add(self.xmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 125 | self.xmin.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
[55fd102] | 126 | self.xmin.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
[e1a310f] | 127 | |
---|
[d89f09b] | 128 | ix += 2 |
---|
| 129 | self.xmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
[9d31a8b] | 130 | self.xmax.SetValue(format_number(data.xmax)) |
---|
[d89f09b] | 131 | self.xmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
| 132 | self.sizer4.Add(self.xmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 133 | self.xmax.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
[55fd102] | 134 | self.xmax.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
[e1a310f] | 135 | |
---|
| 136 | iy +=1 |
---|
[9d31a8b] | 137 | ix = 0 |
---|
| 138 | self.text4_5 = wx.StaticText(self, -1, 'y range') |
---|
| 139 | self.sizer4.Add(self.text4_5,(iy, ix),(1,1),\ |
---|
| 140 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 141 | ix += 1 |
---|
[e1a310f] | 142 | self.ymin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
[9d31a8b] | 143 | self.ymin.SetValue(format_number(data.ymin)) |
---|
[e1a310f] | 144 | self.ymin.SetToolTipString("Minimun value of y in linear scale.") |
---|
| 145 | self.sizer4.Add(self.ymin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 146 | self.ymin.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
| 147 | self.ymin.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
| 148 | |
---|
| 149 | ix += 2 |
---|
| 150 | self.ymax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
[9d31a8b] | 151 | self.ymax.SetValue(format_number(data.ymax)) |
---|
[e1a310f] | 152 | self.ymax.SetToolTipString("Maximum value of y in linear scale.") |
---|
| 153 | self.sizer4.Add(self.ymax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 154 | self.ymax.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
| 155 | self.ymax.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
| 156 | |
---|
[d89f09b] | 157 | #Set chisqr result into TextCtrl |
---|
| 158 | ix = 0 |
---|
| 159 | iy = 1 |
---|
| 160 | self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT) |
---|
| 161 | self.sizer1.Add(self.text1_1,(iy,ix),(1,1),\ |
---|
| 162 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 163 | ix += 1 |
---|
| 164 | self.tcChi = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 165 | self.tcChi.SetToolTipString("Chi^2 over degrees of freedom.") |
---|
| 166 | self.sizer1.Add(self.tcChi,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 167 | ix +=2 |
---|
| 168 | self.sizer1.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[2dbb681] | 169 | ix+= 1 |
---|
[442895f] | 170 | self.sizer1.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[2dbb681] | 171 | ix= 1 |
---|
| 172 | iy+=1 |
---|
| 173 | self.sizer1.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[d89f09b] | 174 | # contains link between model ,all its parameters, and panel organization |
---|
| 175 | self.parameters=[] |
---|
| 176 | #contains link between a model and selected parameters to fit |
---|
| 177 | self.param_toFit=[] |
---|
| 178 | # model on which the fit would be performed |
---|
| 179 | self.model=None |
---|
| 180 | # preview selected model name |
---|
[9d31a8b] | 181 | |
---|
[6f73a08] | 182 | |
---|
[d89f09b] | 183 | #dictionary of model name and model class |
---|
| 184 | self.model_list_box={} |
---|
[9d31a8b] | 185 | |
---|
| 186 | self.data = data |
---|
[d89f09b] | 187 | self.vbox.Layout() |
---|
| 188 | self.vbox.Fit(self) |
---|
| 189 | self.SetSizer(self.vbox) |
---|
[f39511b] | 190 | self.SetScrollbars(20,20,55,40) |
---|
[d89f09b] | 191 | self.Centre() |
---|
| 192 | |
---|
| 193 | |
---|
| 194 | def set_owner(self,owner): |
---|
| 195 | """ |
---|
| 196 | set owner of fitpage |
---|
| 197 | @param owner: the class responsible of plotting |
---|
| 198 | """ |
---|
| 199 | self.event_owner=owner |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | def set_manager(self, manager): |
---|
| 203 | """ |
---|
| 204 | set panel manager |
---|
| 205 | @param manager: instance of plugin fitting |
---|
| 206 | """ |
---|
| 207 | self.manager = manager |
---|
| 208 | |
---|
| 209 | |
---|
[442895f] | 210 | def onClose(self,event): |
---|
[00561739] | 211 | """ close the page associated with this panel""" |
---|
[442895f] | 212 | self.GrandParent.onClose() |
---|
| 213 | |
---|
| 214 | |
---|
| 215 | def compute_chisqr(self): |
---|
| 216 | """ @param fn: function that return model value |
---|
| 217 | @return residuals |
---|
| 218 | """ |
---|
| 219 | flag=self.checkFitRange() |
---|
[9d31a8b] | 220 | res=[] |
---|
[442895f] | 221 | if flag== True: |
---|
[948add7] | 222 | try: |
---|
[9d31a8b] | 223 | xmin = float(self.xmin.GetValue()) |
---|
| 224 | xmax = float(self.xmax.GetValue()) |
---|
| 225 | ymin = float(self.ymin.GetValue()) |
---|
| 226 | ymax = float(self.ymax.GetValue()) |
---|
| 227 | |
---|
| 228 | for i in range(len(self.data.y_bins)): |
---|
| 229 | if self.data.y_bins[i]>= ymin and self.data.y_bins[i]<= ymax: |
---|
| 230 | for j in range(len(self.data.x_bins)): |
---|
| 231 | if self.data.x_bins[j]>= xmin and self.data.x_bins[j]<= xmax: |
---|
[57668f8] | 232 | res.append( (self.data.data[j][i]- self.model.runXY(\ |
---|
[9d31a8b] | 233 | [self.data.x_bins[j],self.data.y_bins[i]]))\ |
---|
[57668f8] | 234 | /self.data.err_data[j][i] ) |
---|
[948add7] | 235 | sum=0 |
---|
[9d31a8b] | 236 | |
---|
[948add7] | 237 | for item in res: |
---|
| 238 | if numpy.isfinite(item): |
---|
| 239 | sum +=item |
---|
| 240 | self.tcChi.SetValue(format_number(math.fabs(sum))) |
---|
| 241 | except: |
---|
| 242 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 243 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
[442895f] | 244 | |
---|
| 245 | |
---|
[d89f09b] | 246 | def onFit(self,event): |
---|
| 247 | """ signal for fitting""" |
---|
| 248 | flag=self.checkFitRange() |
---|
| 249 | self.set_manager(self.manager) |
---|
| 250 | |
---|
| 251 | qmin=float(self.xmin.GetValue()) |
---|
| 252 | qmax =float( self.xmax.GetValue()) |
---|
| 253 | if len(self.param_toFit) >0 and flag==True: |
---|
[948add7] | 254 | self.manager.schedule_for_fit( value=1,fitproblem =None) |
---|
[d89f09b] | 255 | self.manager._on_single_fit(qmin=qmin,qmax=qmax) |
---|
| 256 | else: |
---|
[e1a310f] | 257 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
[d89f09b] | 258 | "Select at least on parameter to fit ")) |
---|
| 259 | def populate_box(self, dict): |
---|
| 260 | """ |
---|
| 261 | Populate each combox box of each page |
---|
| 262 | @param page: the page to populate |
---|
| 263 | """ |
---|
| 264 | id=0 |
---|
| 265 | self.model_list_box=dict |
---|
[49b7efa] | 266 | list_name=[] |
---|
| 267 | for item in self.model_list_box.itervalues(): |
---|
[00561739] | 268 | name = item.__name__ |
---|
[d89f09b] | 269 | if hasattr(item, "name"): |
---|
| 270 | name = item.name |
---|
[49b7efa] | 271 | list_name.append(name) |
---|
| 272 | list_name.sort() |
---|
| 273 | for name in list_name: |
---|
[d89f09b] | 274 | self.modelbox.Insert(name,int(id)) |
---|
| 275 | id+=1 |
---|
[49b7efa] | 276 | wx.EVT_COMBOBOX(self.modelbox,-1, self._on_select_model) |
---|
[d89f09b] | 277 | return 0 |
---|
| 278 | |
---|
| 279 | |
---|
| 280 | def _on_select_model(self,event): |
---|
| 281 | """ |
---|
| 282 | react when a model is selected from page's combo box |
---|
| 283 | post an event to its owner to draw an appropriate theory |
---|
| 284 | """ |
---|
[d171299] | 285 | self.btFit.SetFocus() |
---|
[d89f09b] | 286 | for item in self.model_list_box.itervalues(): |
---|
[00561739] | 287 | name = item.__name__ |
---|
| 288 | if hasattr(item, "name"): |
---|
| 289 | name = item.name |
---|
| 290 | #print "fitpage: _on_select_model model name",name ,event.GetString() |
---|
| 291 | if name ==event.GetString(): |
---|
| 292 | try: |
---|
| 293 | evt = ModelEventbox(model=item(),name=name) |
---|
[d89f09b] | 294 | wx.PostEvent(self.event_owner, evt) |
---|
[00561739] | 295 | except: |
---|
| 296 | raise #ValueError,"model.name is not equal to model class name" |
---|
| 297 | break |
---|
[d89f09b] | 298 | |
---|
| 299 | def _onTextEnter(self,event): |
---|
| 300 | """ |
---|
| 301 | set a flag to determine if the fitting range entered by the user is valid |
---|
| 302 | """ |
---|
[442895f] | 303 | |
---|
[d89f09b] | 304 | try: |
---|
| 305 | flag=self.checkFitRange() |
---|
| 306 | if flag==True and self.model!=None: |
---|
[6f73a08] | 307 | print"fit page",self.xmin.GetValue(),self.xmax.GetValue() |
---|
| 308 | self.manager.redraw_model(float(self.xmin.GetValue())\ |
---|
[d89f09b] | 309 | ,float(self.xmax.GetValue())) |
---|
| 310 | except: |
---|
[6f73a08] | 311 | |
---|
[d89f09b] | 312 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 313 | "Drawing Error:wrong value entered %s"% sys.exc_value)) |
---|
| 314 | |
---|
| 315 | def checkFitRange(self): |
---|
| 316 | """ |
---|
| 317 | Check the validity of fitting range |
---|
| 318 | @note: xmin should always be less than xmax or else each control box |
---|
| 319 | background is colored in pink. |
---|
| 320 | """ |
---|
| 321 | |
---|
| 322 | flag = True |
---|
| 323 | valueMin = self.xmin.GetValue() |
---|
| 324 | valueMax = self.xmax.GetValue() |
---|
| 325 | # Check for possible values entered |
---|
[6f73a08] | 326 | print "fitpage: checkfitrange:",valueMin,valueMax |
---|
[d89f09b] | 327 | try: |
---|
| 328 | if (float(valueMax)> float(valueMin)): |
---|
| 329 | self.xmax.SetBackgroundColour(wx.WHITE) |
---|
| 330 | self.xmin.SetBackgroundColour(wx.WHITE) |
---|
| 331 | else: |
---|
| 332 | flag = False |
---|
| 333 | self.xmin.SetBackgroundColour("pink") |
---|
| 334 | self.xmax.SetBackgroundColour("pink") |
---|
| 335 | except: |
---|
| 336 | flag = False |
---|
| 337 | self.xmin.SetBackgroundColour("pink") |
---|
| 338 | self.xmax.SetBackgroundColour("pink") |
---|
| 339 | |
---|
| 340 | self.xmin.Refresh() |
---|
| 341 | self.xmax.Refresh() |
---|
| 342 | return flag |
---|
| 343 | |
---|
[9d31a8b] | 344 | |
---|
[d89f09b] | 345 | def get_model_box(self): |
---|
| 346 | """ return reference to combox box self.model""" |
---|
| 347 | return self.modelbox |
---|
| 348 | |
---|
| 349 | |
---|
| 350 | def get_param_list(self): |
---|
| 351 | """ |
---|
| 352 | @return self.param_toFit: list containing references to TextCtrl |
---|
| 353 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
| 354 | @raise: if return an empty list of parameter fit will nnote work |
---|
| 355 | properly so raise ValueError,"missing parameter to fit" |
---|
| 356 | """ |
---|
| 357 | if self.param_toFit !=[]: |
---|
| 358 | return self.param_toFit |
---|
| 359 | else: |
---|
| 360 | raise ValueError,"missing parameter to fit" |
---|
| 361 | |
---|
| 362 | |
---|
| 363 | def set_panel(self,model): |
---|
| 364 | """ |
---|
| 365 | Build the panel from the model content |
---|
| 366 | @param model: the model selected in combo box for fitting purpose |
---|
| 367 | """ |
---|
| 368 | self.sizer2.Clear(True) |
---|
[d171299] | 369 | self.sizer5.Clear(True) |
---|
[d89f09b] | 370 | self.parameters = [] |
---|
| 371 | self.param_toFit=[] |
---|
| 372 | self.model = model |
---|
| 373 | keys = self.model.getParamList() |
---|
[04edd0d] | 374 | #print "fitpage1D : dispersion list",self.model.getDispParamList() |
---|
[d89f09b] | 375 | keys.sort() |
---|
[44bbf6a] | 376 | disp_list=self.model.getDispParamList() |
---|
[04edd0d] | 377 | ip=0 |
---|
| 378 | iq=1 |
---|
| 379 | if len(disp_list)>0: |
---|
| 380 | disp = wx.StaticText(self, -1, 'Dispersion') |
---|
| 381 | self.sizer5.Add(disp,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 382 | ip += 1 |
---|
| 383 | values = wx.StaticText(self, -1, 'Values') |
---|
| 384 | self.sizer5.Add(values,( iq, ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 385 | |
---|
[44bbf6a] | 386 | disp_list.sort() |
---|
[d89f09b] | 387 | iy = 1 |
---|
| 388 | ix = 0 |
---|
| 389 | self.cb1 = wx.CheckBox(self, -1,'Parameters', (10, 10)) |
---|
| 390 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
| 391 | self.sizer2.Add(self.cb1,(iy, ix),(1,1),\ |
---|
| 392 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 393 | ix +=1 |
---|
| 394 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
| 395 | self.sizer2.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
| 396 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 397 | ix +=2 |
---|
| 398 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
| 399 | self.sizer2.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
| 400 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 401 | self.text2_3.Hide() |
---|
| 402 | ix +=1 |
---|
| 403 | self.text2_4 = wx.StaticText(self, -1, 'Units') |
---|
| 404 | self.sizer2.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
| 405 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 406 | self.text2_4.Hide() |
---|
[04edd0d] | 407 | |
---|
[d89f09b] | 408 | for item in keys: |
---|
[44bbf6a] | 409 | if not item in disp_list: |
---|
| 410 | iy += 1 |
---|
| 411 | ix = 0 |
---|
| 412 | |
---|
| 413 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
| 414 | cb.SetValue(False) |
---|
| 415 | self.sizer2.Add( cb,( iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 416 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
| 417 | |
---|
| 418 | ix += 1 |
---|
| 419 | value= self.model.getParam(item) |
---|
| 420 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 421 | ctl1.SetValue(str (format_number(value))) |
---|
| 422 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 423 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
| 424 | self.sizer2.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
| 425 | ix += 1 |
---|
| 426 | text2=wx.StaticText(self, -1, '+/-') |
---|
| 427 | self.sizer2.Add(text2,(iy, ix),(1,1),\ |
---|
| 428 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 429 | text2.Hide() |
---|
| 430 | ix += 1 |
---|
| 431 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 432 | self.sizer2.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 433 | ctl2.Hide() |
---|
| 434 | ix +=1 |
---|
| 435 | # Units |
---|
| 436 | try: |
---|
| 437 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
| 438 | except: |
---|
| 439 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
| 440 | self.sizer2.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[04edd0d] | 441 | else: |
---|
| 442 | ip = 0 |
---|
| 443 | iq += 1 |
---|
| 444 | cb = wx.CheckBox(self, -1, item, (10, 10)) |
---|
| 445 | cb.SetValue(False) |
---|
| 446 | self.sizer5.Add( cb,( iq, ip),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 447 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
| 448 | |
---|
| 449 | ip += 1 |
---|
| 450 | value= self.model.getParam(item) |
---|
| 451 | ctl1 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=wx.TE_PROCESS_ENTER) |
---|
| 452 | ctl1.SetValue(str (format_number(value))) |
---|
| 453 | ctl1.Bind(wx.EVT_KILL_FOCUS, self._onparamEnter) |
---|
| 454 | ctl1.Bind(wx.EVT_TEXT_ENTER,self._onparamEnter) |
---|
| 455 | self.sizer5.Add(ctl1, (iq,ip),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 456 | |
---|
| 457 | #save data |
---|
| 458 | self.parameters.append([cb,ctl1,text2,ctl2]) |
---|
| 459 | |
---|
[44bbf6a] | 460 | iy+=1 |
---|
| 461 | self.sizer2.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[d171299] | 462 | |
---|
[d89f09b] | 463 | #Display units text on panel |
---|
| 464 | for item in keys: |
---|
| 465 | if self.model.details[item][0]!='': |
---|
| 466 | self.text2_4.Show() |
---|
| 467 | break |
---|
| 468 | else: |
---|
| 469 | self.text2_4.Hide() |
---|
| 470 | #Disable or enable fit button |
---|
[44bbf6a] | 471 | |
---|
| 472 | if not (len(self.param_toFit ) >0): |
---|
[d89f09b] | 473 | self.xmin.Disable() |
---|
| 474 | self.xmax.Disable() |
---|
[e1a310f] | 475 | self.ymin.Disable() |
---|
| 476 | self.ymax.Disable() |
---|
[44bbf6a] | 477 | else: |
---|
| 478 | self.xmin.Enable() |
---|
| 479 | self.xmax.Enable() |
---|
| 480 | self.ymin.Enable() |
---|
| 481 | self.ymax.Enable() |
---|
| 482 | |
---|
[442895f] | 483 | self.compute_chisqr() |
---|
[d89f09b] | 484 | self.vbox.Layout() |
---|
[0aac36f] | 485 | self.SetScrollbars(20,20,55,40) |
---|
| 486 | self.Layout() |
---|
| 487 | |
---|
[d89f09b] | 488 | self.GrandParent.GetSizer().Layout() |
---|
| 489 | |
---|
| 490 | |
---|
| 491 | |
---|
| 492 | def _onparamEnter(self,event): |
---|
| 493 | """ |
---|
| 494 | when enter value on panel redraw model according to changed |
---|
| 495 | """ |
---|
[1b07935d] | 496 | self.set_model_parameter() |
---|
[442895f] | 497 | self.compute_chisqr() |
---|
| 498 | |
---|
[1b07935d] | 499 | def set_model_parameter(self): |
---|
[442895f] | 500 | """ |
---|
| 501 | this method redraws the model according to parameters values changes |
---|
| 502 | and the reset model according to paramaters changes |
---|
| 503 | """ |
---|
[d15c0202] | 504 | print "went here",len(self.parameters) ,self.model |
---|
[d89f09b] | 505 | if len(self.parameters) !=0 and self.model !=None: |
---|
[d15c0202] | 506 | # Flag to register when a parameter has changed. |
---|
| 507 | is_modified = False |
---|
[d89f09b] | 508 | for item in self.parameters: |
---|
[d15c0202] | 509 | try: |
---|
| 510 | item[2].Hide() |
---|
| 511 | item[3].Clear() |
---|
| 512 | item[3].Hide() |
---|
| 513 | name=str(item[0].GetLabelText()) |
---|
| 514 | value= float(item[1].GetValue()) |
---|
| 515 | # If the value of the parameter has changed, |
---|
| 516 | # update the model and set the is_modified flag |
---|
| 517 | if value != self.model.getParam(name): |
---|
[db709e4] | 518 | self.model.setParam(name,value) |
---|
[d15c0202] | 519 | is_modified = True |
---|
[d89f09b] | 520 | except: |
---|
| 521 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
[d15c0202] | 522 | "Drawing Error:wrong value entered : %s"% sys.exc_value)) |
---|
| 523 | # Here we should check whether the boundaries have been modified. |
---|
| 524 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
| 525 | # set the is_modified flag to True |
---|
| 526 | if float(self.xmin.GetValue()) != self.qmin: |
---|
| 527 | self.qmin = float(self.xmin.GetValue()) |
---|
| 528 | is_modified = True |
---|
| 529 | if float(self.xmax.GetValue()) != self.qmax: |
---|
| 530 | self.qmax = float(self.xmax.GetValue()) |
---|
| 531 | is_modified = True |
---|
[db709e4] | 532 | |
---|
[d15c0202] | 533 | if is_modified: |
---|
| 534 | |
---|
| 535 | self.manager.redraw_model( |
---|
| 536 | qmin=self.qmin, qmax=self.qmax, |
---|
| 537 | ) |
---|
| 538 | #self.manager.draw_model(self,model,description=None, |
---|
| 539 | # enable1D=True,qmin=None,qmax=None, qstep=None) |
---|
[db709e4] | 540 | |
---|
[d89f09b] | 541 | def select_all_param(self,event): |
---|
| 542 | """ |
---|
| 543 | set to true or false all checkBox given the main checkbox value cb1 |
---|
| 544 | """ |
---|
| 545 | self.param_toFit=[] |
---|
| 546 | if self.parameters !=[]: |
---|
| 547 | if self.cb1.GetValue()==True: |
---|
| 548 | for item in self.parameters: |
---|
| 549 | item[0].SetValue(True) |
---|
| 550 | list= [item[0],item[1],item[2],item[3]] |
---|
| 551 | self.param_toFit.append(list ) |
---|
[9d31a8b] | 552 | |
---|
| 553 | if not (len(self.param_toFit ) >0): |
---|
| 554 | self.xmin.Disable() |
---|
| 555 | self.xmax.Disable() |
---|
| 556 | self.ymin.Disable() |
---|
| 557 | self.ymax.Disable() |
---|
| 558 | else: |
---|
| 559 | self.xmin.Enable() |
---|
| 560 | self.xmax.Enable() |
---|
| 561 | self.ymin.Enable() |
---|
| 562 | self.ymax.Enable() |
---|
[d89f09b] | 563 | else: |
---|
| 564 | for item in self.parameters: |
---|
| 565 | item[0].SetValue(False) |
---|
| 566 | self.param_toFit=[] |
---|
| 567 | |
---|
| 568 | self.xmin.Disable() |
---|
| 569 | self.xmax.Disable() |
---|
[e1a310f] | 570 | self.ymin.Disable() |
---|
| 571 | self.ymax.Disable() |
---|
[d89f09b] | 572 | |
---|
| 573 | |
---|
| 574 | def select_param(self,event): |
---|
| 575 | """ |
---|
| 576 | Select TextCtrl checked for fitting purpose and stores them |
---|
| 577 | in self.param_toFit=[] list |
---|
| 578 | """ |
---|
| 579 | self.param_toFit=[] |
---|
| 580 | for item in self.parameters: |
---|
| 581 | if item[0].GetValue()==True: |
---|
| 582 | list= [item[0],item[1],item[2],item[3]] |
---|
| 583 | self.param_toFit.append(list ) |
---|
| 584 | else: |
---|
| 585 | if item in self.param_toFit: |
---|
| 586 | self.param_toFit.remove(item) |
---|
| 587 | if len(self.parameters)==len(self.param_toFit): |
---|
| 588 | self.cb1.SetValue(True) |
---|
| 589 | else: |
---|
| 590 | self.cb1.SetValue(False) |
---|
[9d31a8b] | 591 | |
---|
| 592 | if not (len(self.param_toFit ) >0): |
---|
[d89f09b] | 593 | self.xmin.Disable() |
---|
| 594 | self.xmax.Disable() |
---|
[e1a310f] | 595 | self.ymin.Disable() |
---|
| 596 | self.ymax.Disable() |
---|
[9d31a8b] | 597 | else: |
---|
| 598 | self.xmin.Enable() |
---|
| 599 | self.xmax.Enable() |
---|
| 600 | self.ymin.Enable() |
---|
| 601 | self.ymax.Enable() |
---|
| 602 | |
---|
[d89f09b] | 603 | |
---|
| 604 | |
---|
| 605 | |
---|
| 606 | def onsetValues(self,chisqr, out,cov): |
---|
| 607 | """ |
---|
| 608 | Build the panel from the fit result |
---|
| 609 | @param chisqr:Value of the goodness of fit metric |
---|
| 610 | @param out:list of parameter with the best value found during fitting |
---|
| 611 | @param cov:Covariance matrix |
---|
| 612 | |
---|
| 613 | """ |
---|
| 614 | #print "fitting : onsetvalues out",out |
---|
| 615 | self.tcChi.Clear() |
---|
| 616 | self.tcChi.SetValue(format_number(chisqr)) |
---|
| 617 | params = {} |
---|
| 618 | is_modified = False |
---|
| 619 | has_error = False |
---|
| 620 | if out.__class__==numpy.float64: |
---|
| 621 | self.param_toFit[0][1].SetValue(format_number(out)) |
---|
| 622 | self.param_toFit[0][1].Refresh() |
---|
| 623 | if cov !=None : |
---|
| 624 | self.text2_3.Show() |
---|
| 625 | self.param_toFit[0][2].Show() |
---|
| 626 | self.param_toFit[0][3].Clear() |
---|
| 627 | self.param_toFit[0][3].SetValue(format_number(cov[0])) |
---|
| 628 | self.param_toFit[0][3].Show() |
---|
| 629 | #out is a list : set parameters and errors in TextCtrl |
---|
| 630 | else: |
---|
| 631 | i=0 |
---|
| 632 | #print "fitpage: list param model",list |
---|
| 633 | #for item in self.param_toFit: |
---|
| 634 | # print "fitpage: list display",item[0].GetLabelText() |
---|
| 635 | for item in self.param_toFit: |
---|
| 636 | if( out != None ) and len(out)<=len(self.param_toFit)and i < len(out): |
---|
[442895f] | 637 | #item[1].SetValue(format_number(out[i])) |
---|
| 638 | item[1].SetValue(format_number(self.model.getParam(item[0].GetLabelText()))) |
---|
[d89f09b] | 639 | item[1].Refresh() |
---|
| 640 | if (cov !=None)and len(cov)<=len(self.param_toFit)and i < len(cov): |
---|
| 641 | self.text2_3.Show() |
---|
| 642 | item[2].Show() |
---|
| 643 | item[3].Clear() |
---|
| 644 | item[3].SetValue(format_number(cov[i])) |
---|
| 645 | item[3].Show() |
---|
| 646 | i+=1 |
---|
| 647 | |
---|
| 648 | self.vbox.Layout() |
---|
[0aac36f] | 649 | self.SetScrollbars(20,20,55,40) |
---|
| 650 | self.Layout() |
---|
[d89f09b] | 651 | self.GrandParent.GetSizer().Layout() |
---|
| 652 | |
---|