1 | import sys |
---|
2 | import wx |
---|
3 | import wx.lib |
---|
4 | import numpy,math |
---|
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 | from modelpage import format_number |
---|
12 | from fitpage1D import FitPage1D |
---|
13 | |
---|
14 | class FitPage2D(FitPage1D): |
---|
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 | |
---|
28 | def __init__(self, parent,data, *args, **kwargs): |
---|
29 | wx.ScrolledWindow.__init__(self, parent, *args, **kwargs) |
---|
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) |
---|
38 | self.sizer6 = wx.GridBagSizer(5,5) |
---|
39 | self.sizer5 = wx.GridBagSizer(5,5) |
---|
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) |
---|
44 | |
---|
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)) |
---|
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.") |
---|
55 | self.static_line_1 = wx.StaticLine(self, -1) |
---|
56 | |
---|
57 | self.vbox.Add(self.sizer3) |
---|
58 | self.vbox.Add(self.sizer2) |
---|
59 | self.vbox.Add(self.static_line_1, 0, wx.EXPAND, 0) |
---|
60 | self.vbox.Add(self.sizer5) |
---|
61 | self.vbox.Add(self.sizer6) |
---|
62 | self.vbox.Add(self.sizer4) |
---|
63 | self.vbox.Add(self.sizer1) |
---|
64 | ## Q range |
---|
65 | self.qmin= 0.001 |
---|
66 | self.qmax= 0.1 |
---|
67 | |
---|
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.") |
---|
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) |
---|
86 | |
---|
87 | ix = 0 |
---|
88 | iy = 1 |
---|
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 |
---|
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) |
---|
97 | #self.text4_1.Hide() |
---|
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) |
---|
102 | #self.text4_2.Hide() |
---|
103 | |
---|
104 | #self.text4_3.Hide() |
---|
105 | ix = 0 |
---|
106 | iy += 1 |
---|
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) |
---|
110 | ix += 1 |
---|
111 | self.xmin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
112 | self.xmin.SetValue(format_number(data.xmin)) |
---|
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) |
---|
116 | self.xmin.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
117 | |
---|
118 | ix += 2 |
---|
119 | self.xmax = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
120 | self.xmax.SetValue(format_number(data.xmax)) |
---|
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) |
---|
124 | self.xmax.Bind(wx.EVT_TEXT_ENTER, self._onTextEnter) |
---|
125 | |
---|
126 | iy +=1 |
---|
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 |
---|
132 | self.ymin = wx.TextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
133 | self.ymin.SetValue(format_number(data.ymin)) |
---|
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)) |
---|
141 | self.ymax.SetValue(format_number(data.ymax)) |
---|
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 | |
---|
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) |
---|
159 | ix+= 1 |
---|
160 | self.sizer1.Add( self.btClose,(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
161 | ix= 1 |
---|
162 | iy+=1 |
---|
163 | self.sizer1.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
164 | # contains link between model ,all its parameters, and panel organization |
---|
165 | self.parameters=[] |
---|
166 | self.fixed_param=[] |
---|
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 |
---|
172 | |
---|
173 | |
---|
174 | #dictionary of model name and model class |
---|
175 | self.model_list_box={} |
---|
176 | |
---|
177 | self.data = data |
---|
178 | self.vbox.Layout() |
---|
179 | self.vbox.Fit(self) |
---|
180 | self.SetSizer(self.vbox) |
---|
181 | self.SetScrollbars(20,20,55,40) |
---|
182 | self.Centre() |
---|
183 | |
---|
184 | |
---|
185 | |
---|
186 | def compute_chisqr(self): |
---|
187 | """ @param fn: function that return model value |
---|
188 | @return residuals |
---|
189 | """ |
---|
190 | flag=self.checkFitRange() |
---|
191 | res=[] |
---|
192 | if flag== True: |
---|
193 | try: |
---|
194 | xmin = float(self.xmin.GetValue()) |
---|
195 | xmax = float(self.xmax.GetValue()) |
---|
196 | ymin = float(self.ymin.GetValue()) |
---|
197 | ymax = float(self.ymax.GetValue()) |
---|
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: |
---|
202 | print "data error ",self.data.err_data[j][i] |
---|
203 | res.append( (self.data.data[j][i]- self.model.runXY(\ |
---|
204 | [self.data.x_bins[i],self.data.y_bins[j]]))\ |
---|
205 | /self.data.err_data[j][i] ) |
---|
206 | sum=0 |
---|
207 | |
---|
208 | for item in res: |
---|
209 | if numpy.isfinite(item): |
---|
210 | sum +=item |
---|
211 | print "chisqr : sum 2D", xmin, xmax, ymin, ymax,sum |
---|
212 | #print res |
---|
213 | self.tcChi.SetValue(format_number(math.fabs(sum))) |
---|
214 | except: |
---|
215 | wx.PostEvent(self.parent.GrandParent, StatusEvent(status=\ |
---|
216 | "Chisqr cannot be compute: %s"% sys.exc_value)) |
---|
217 | |
---|
218 | |
---|