[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 | |
---|
[d23544dc] | 11 | from modelpage import format_number |
---|
| 12 | from fitpage1D import FitPage1D |
---|
[d89f09b] | 13 | |
---|
[d23544dc] | 14 | class FitPage2D(FitPage1D): |
---|
[d89f09b] | 15 | """ |
---|
| 16 | FitPanel class contains fields allowing to display results when |
---|
| 17 | fitting a model and one data |
---|
| 18 | @note: For Fit to be performed the user should check at least one parameter |
---|
| 19 | on fit Panel window. |
---|
| 20 | |
---|
| 21 | """ |
---|
| 22 | ## Internal name for the AUI manager |
---|
| 23 | window_name = "Fit page" |
---|
| 24 | ## Title to appear on top of the window |
---|
| 25 | window_caption = "Fit Page" |
---|
| 26 | |
---|
| 27 | |
---|
[9d31a8b] | 28 | def __init__(self, parent,data, *args, **kwargs): |
---|
[f39511b] | 29 | wx.ScrolledWindow.__init__(self, parent, *args, **kwargs) |
---|
[d89f09b] | 30 | """ |
---|
| 31 | Initialization of the Panel |
---|
| 32 | """ |
---|
| 33 | self.manager = None |
---|
| 34 | self.parent = parent |
---|
| 35 | self.event_owner=None |
---|
| 36 | #panel interface |
---|
| 37 | self.vbox = wx.BoxSizer(wx.VERTICAL) |
---|
[d23544dc] | 38 | self.sizer6 = wx.GridBagSizer(5,5) |
---|
[44bbf6a] | 39 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
[d89f09b] | 40 | self.sizer4 = wx.GridBagSizer(5,5) |
---|
| 41 | self.sizer3 = wx.GridBagSizer(5,5) |
---|
| 42 | self.sizer2 = wx.GridBagSizer(5,5) |
---|
| 43 | self.sizer1 = wx.GridBagSizer(5,5) |
---|
[d23544dc] | 44 | |
---|
[44bbf6a] | 45 | #self.DataSource = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 46 | #self.DataSource.SetToolTipString("name of data to fit") |
---|
| 47 | #self.DataSource.SetValue(str(data.name)) |
---|
| 48 | #self.DataSource.Disable() |
---|
| 49 | self.DataSource =wx.StaticText(self, -1,str(data.name)) |
---|
[d89f09b] | 50 | self.modelbox = wx.ComboBox(self, -1) |
---|
| 51 | id = wx.NewId() |
---|
| 52 | self.btFit =wx.Button(self,id,'Fit') |
---|
| 53 | self.btFit.Bind(wx.EVT_BUTTON, self.onFit,id=id) |
---|
| 54 | self.btFit.SetToolTipString("Perform fit.") |
---|
[44bbf6a] | 55 | self.static_line_1 = wx.StaticLine(self, -1) |
---|
| 56 | |
---|
[d89f09b] | 57 | self.vbox.Add(self.sizer3) |
---|
| 58 | self.vbox.Add(self.sizer2) |
---|
[44bbf6a] | 59 | self.vbox.Add(self.static_line_1, 0, wx.EXPAND, 0) |
---|
| 60 | self.vbox.Add(self.sizer5) |
---|
[d23544dc] | 61 | self.vbox.Add(self.sizer6) |
---|
[d89f09b] | 62 | self.vbox.Add(self.sizer4) |
---|
| 63 | self.vbox.Add(self.sizer1) |
---|
[d15c0202] | 64 | ## Q range |
---|
| 65 | self.qmin= 0.001 |
---|
| 66 | self.qmax= 0.1 |
---|
[d89f09b] | 67 | |
---|
[442895f] | 68 | id = wx.NewId() |
---|
| 69 | self.btClose =wx.Button(self,id,'Close') |
---|
| 70 | self.btClose.Bind(wx.EVT_BUTTON, self.onClose,id=id) |
---|
| 71 | self.btClose.SetToolTipString("Close page.") |
---|
[d89f09b] | 72 | ix = 0 |
---|
| 73 | iy = 1 |
---|
| 74 | self.sizer3.Add(wx.StaticText(self, -1, 'Data Source'),(iy,ix),\ |
---|
| 75 | (1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 76 | ix += 1 |
---|
| 77 | self.sizer3.Add(self.DataSource,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 78 | ix += 1 |
---|
| 79 | self.sizer3.Add((20,20),(iy,ix),(1,1),wx.RIGHT|wx.EXPAND|wx.ADJUST_MINSIZE,0) |
---|
| 80 | ix = 0 |
---|
| 81 | iy += 1 |
---|
| 82 | self.sizer3.Add(wx.StaticText(self,-1,'Model'),(iy,ix),(1,1)\ |
---|
| 83 | , wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 84 | ix += 1 |
---|
| 85 | self.sizer3.Add(self.modelbox,(iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[9d31a8b] | 86 | |
---|
| 87 | ix = 0 |
---|
[d89f09b] | 88 | iy = 1 |
---|
[9d31a8b] | 89 | #set maximum range for x in linear scale |
---|
| 90 | self.text4_3 = wx.StaticText(self, -1, 'Maximum Data\n Range (Linear)', style=wx.ALIGN_LEFT) |
---|
| 91 | self.sizer4.Add(self.text4_3,(iy,ix),(1,1),\ |
---|
| 92 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 93 | ix += 1 |
---|
[d89f09b] | 94 | self.text4_1 = wx.StaticText(self, -1, 'Min') |
---|
| 95 | self.sizer4.Add(self.text4_1,(iy, ix),(1,1),\ |
---|
| 96 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[e1a310f] | 97 | #self.text4_1.Hide() |
---|
[d89f09b] | 98 | ix += 2 |
---|
| 99 | self.text4_2 = wx.StaticText(self, -1, 'Max') |
---|
| 100 | self.sizer4.Add(self.text4_2,(iy, ix),(1,1),\ |
---|
| 101 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[e1a310f] | 102 | #self.text4_2.Hide() |
---|
[9d31a8b] | 103 | |
---|
| 104 | #self.text4_3.Hide() |
---|
[d89f09b] | 105 | ix = 0 |
---|
| 106 | iy += 1 |
---|
[9d31a8b] | 107 | self.text4_4 = wx.StaticText(self, -1, 'x range') |
---|
| 108 | self.sizer4.Add(self.text4_4,(iy, ix),(1,1),\ |
---|
| 109 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
[d89f09b] | 110 | ix += 1 |
---|
| 111 | self.xmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
[9d31a8b] | 112 | self.xmin.SetValue(format_number(data.xmin)) |
---|
[d89f09b] | 113 | self.xmin.SetToolTipString("Minimun value of x in linear scale.") |
---|
| 114 | self.sizer4.Add(self.xmin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 115 | self.xmin.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
[55fd102] | 116 | self.xmin.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
[e1a310f] | 117 | |
---|
[d89f09b] | 118 | ix += 2 |
---|
| 119 | self.xmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
[9d31a8b] | 120 | self.xmax.SetValue(format_number(data.xmax)) |
---|
[d89f09b] | 121 | self.xmax.SetToolTipString("Maximum value of x in linear scale.") |
---|
| 122 | self.sizer4.Add(self.xmax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 123 | self.xmax.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
[55fd102] | 124 | self.xmax.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
[e1a310f] | 125 | |
---|
| 126 | iy +=1 |
---|
[9d31a8b] | 127 | ix = 0 |
---|
| 128 | self.text4_5 = wx.StaticText(self, -1, 'y range') |
---|
| 129 | self.sizer4.Add(self.text4_5,(iy, ix),(1,1),\ |
---|
| 130 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 131 | ix += 1 |
---|
[e1a310f] | 132 | self.ymin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
[9d31a8b] | 133 | self.ymin.SetValue(format_number(data.ymin)) |
---|
[e1a310f] | 134 | self.ymin.SetToolTipString("Minimun value of y in linear scale.") |
---|
| 135 | self.sizer4.Add(self.ymin,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 136 | self.ymin.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
| 137 | self.ymin.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
| 138 | |
---|
| 139 | ix += 2 |
---|
| 140 | self.ymax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
[9d31a8b] | 141 | self.ymax.SetValue(format_number(data.ymax)) |
---|
[e1a310f] | 142 | self.ymax.SetToolTipString("Maximum value of y in linear scale.") |
---|
| 143 | self.sizer4.Add(self.ymax,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 144 | self.ymax.Bind(wx.EVT_KILL_FOCUS, self._onTextEnter) |
---|
| 145 | self.ymax.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
| 146 | |
---|
[d89f09b] | 147 | #Set chisqr result into TextCtrl |
---|
| 148 | ix = 0 |
---|
| 149 | iy = 1 |
---|
| 150 | self.text1_1 = wx.StaticText(self, -1, 'Chi2/dof', style=wx.ALIGN_LEFT) |
---|
| 151 | self.sizer1.Add(self.text1_1,(iy,ix),(1,1),\ |
---|
| 152 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
| 153 | ix += 1 |
---|
| 154 | self.tcChi = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
| 155 | self.tcChi.SetToolTipString("Chi^2 over degrees of freedom.") |
---|
| 156 | self.sizer1.Add(self.tcChi,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
| 157 | ix +=2 |
---|
| 158 | self.sizer1.Add(self.btFit,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[2dbb681] | 159 | ix+= 1 |
---|
[442895f] | 160 | self.sizer1.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[2dbb681] | 161 | ix= 1 |
---|
| 162 | iy+=1 |
---|
| 163 | self.sizer1.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
[d89f09b] | 164 | # contains link between model ,all its parameters, and panel organization |
---|
| 165 | self.parameters=[] |
---|
[d23544dc] | 166 | self.fixed_param=[] |
---|
[d89f09b] | 167 | #contains link between a model and selected parameters to fit |
---|
| 168 | self.param_toFit=[] |
---|
| 169 | # model on which the fit would be performed |
---|
| 170 | self.model=None |
---|
| 171 | # preview selected model name |
---|
[9d31a8b] | 172 | |
---|
[6f73a08] | 173 | |
---|
[d89f09b] | 174 | #dictionary of model name and model class |
---|
| 175 | self.model_list_box={} |
---|
[9d31a8b] | 176 | |
---|
| 177 | self.data = data |
---|
[d89f09b] | 178 | self.vbox.Layout() |
---|
| 179 | self.vbox.Fit(self) |
---|
| 180 | self.SetSizer(self.vbox) |
---|
[f39511b] | 181 | self.SetScrollbars(20,20,55,40) |
---|
[d89f09b] | 182 | self.Centre() |
---|
| 183 | |
---|
| 184 | |
---|
[442895f] | 185 | |
---|
| 186 | def compute_chisqr(self): |
---|
| 187 | """ @param fn: function that return model value |
---|
| 188 | @return residuals |
---|
| 189 | """ |
---|
| 190 | flag=self.checkFitRange() |
---|
[9d31a8b] | 191 | res=[] |
---|
[442895f] | 192 | if flag== True: |
---|
[948add7] | 193 | try: |
---|
[9d31a8b] | 194 | xmin = float(self.xmin.GetValue()) |
---|
| 195 | xmax = float(self.xmax.GetValue()) |
---|
| 196 | ymin = float(self.ymin.GetValue()) |
---|
| 197 | ymax = float(self.ymax.GetValue()) |
---|
[d23544dc] | 198 | for i in range(len(self.data.x_bins)): |
---|
| 199 | if self.data.x_bins[i]>= xmin and self.data.x_bins[i]<= xmax: |
---|
| 200 | for j in range(len(self.data.y_bins)): |
---|
| 201 | if self.data.y_bins[j]>= ymin and self.data.y_bins[j]<= ymax: |
---|
[57668f8] | 202 | res.append( (self.data.data[j][i]- self.model.runXY(\ |
---|
[d23544dc] | 203 | [self.data.x_bins[i],self.data.y_bins[j]]))\ |
---|
[57668f8] | 204 | /self.data.err_data[j][i] ) |
---|
[948add7] | 205 | sum=0 |
---|
[9d31a8b] | 206 | |
---|
[948add7] | 207 | for item in res: |
---|
| 208 | if numpy.isfinite(item): |
---|
| 209 | sum +=item |
---|
[e9b4cc4] | 210 | #print "chisqr : sum 2D", xmin, xmax, ymin, ymax,sum |
---|
[d23544dc] | 211 | #print res |
---|
[948add7] | 212 | self.tcChi.SetValue(format_number(math.fabs(sum))) |
---|
| 213 | except: |
---|
| 214 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
| 215 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
[d89f09b] | 216 | |
---|
[d23544dc] | 217 | |
---|