1 | |
---|
2 | |
---|
3 | import sys |
---|
4 | import wx |
---|
5 | import wx.lib.newevent |
---|
6 | import numpy |
---|
7 | import copy |
---|
8 | import math |
---|
9 | import time |
---|
10 | from sans.models.dispersion_models import ArrayDispersion, GaussianDispersion |
---|
11 | from DataLoader.data_info import Data1D |
---|
12 | from sans.guicomm.events import StatusEvent |
---|
13 | from sans.guiframe.utils import format_number,check_float |
---|
14 | #from sans.guiframe.local_perspectives.plotting.Masking import ModelPanel2D as panel2D |
---|
15 | |
---|
16 | ## event to post model to fit to fitting plugins |
---|
17 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
18 | |
---|
19 | ## event to know the selected fit engine |
---|
20 | (FitterTypeEvent, EVT_FITTER_TYPE) = wx.lib.newevent.NewEvent() |
---|
21 | (FitStopEvent, EVT_FIT_STOP) = wx.lib.newevent.NewEvent() |
---|
22 | _BOX_WIDTH = 76 |
---|
23 | _DATA_BOX_WIDTH = 300 |
---|
24 | SMEAR_SIZE_L = 0.005 |
---|
25 | SMEAR_SIZE_H = 0.01 |
---|
26 | |
---|
27 | import basepage |
---|
28 | from basepage import BasicPage |
---|
29 | from basepage import PageInfoEvent |
---|
30 | from DataLoader.qsmearing import smear_selection |
---|
31 | |
---|
32 | class FitPage(BasicPage): |
---|
33 | """ |
---|
34 | FitPanel class contains fields allowing to display results when |
---|
35 | fitting a model and one data |
---|
36 | @note: For Fit to be performed the user should check at least one parameter |
---|
37 | on fit Panel window. |
---|
38 | |
---|
39 | """ |
---|
40 | |
---|
41 | def __init__(self,parent, page_info): |
---|
42 | BasicPage.__init__(self, parent, page_info) |
---|
43 | |
---|
44 | """ |
---|
45 | Initialization of the Panel |
---|
46 | """ |
---|
47 | ## total number of point: float |
---|
48 | self.npts=None |
---|
49 | ## thread for compute Chisqr |
---|
50 | self.calc_Chisqr=None |
---|
51 | ## default fitengine type |
---|
52 | self.engine_type = None |
---|
53 | ## smear default |
---|
54 | self.smearer = None |
---|
55 | self.current_smearer = None |
---|
56 | self.dxl = None |
---|
57 | self.dxw = None |
---|
58 | self.dx_min = None |
---|
59 | self.dx_max = None |
---|
60 | |
---|
61 | ## draw sizer |
---|
62 | self._fill_datainfo_sizer() |
---|
63 | |
---|
64 | # get smear info from data |
---|
65 | self._get_smear_info() |
---|
66 | |
---|
67 | self._fill_model_sizer( self.sizer1) |
---|
68 | |
---|
69 | self._get_defult_custom_smear() |
---|
70 | |
---|
71 | self._fill_range_sizer() |
---|
72 | |
---|
73 | if self.data is None: |
---|
74 | self.formfactorbox.Disable() |
---|
75 | self.structurebox.Disable() |
---|
76 | else: |
---|
77 | self.smearer = smear_selection( self.data ) |
---|
78 | if self.smearer ==None: |
---|
79 | self.enable_smearer.Disable() |
---|
80 | if self.data.__class__.__name__ =="Data2D": |
---|
81 | if self.model != None: |
---|
82 | self.smear_description_2d.Show(True) |
---|
83 | self.npts = len(self.data.data) |
---|
84 | else: |
---|
85 | self.npts = len(self.data.x) |
---|
86 | |
---|
87 | |
---|
88 | self.disp_cb_dict = {} |
---|
89 | ## to update the panel according to the fit engine type selected |
---|
90 | self.Bind(EVT_FITTER_TYPE,self._on_engine_change) |
---|
91 | self.Bind(EVT_FIT_STOP,self._on_fit_complete) |
---|
92 | |
---|
93 | |
---|
94 | def _on_fit_complete(self, event): |
---|
95 | """ |
---|
96 | When fit is complete ,reset the fit button label. |
---|
97 | """ |
---|
98 | #self.btFit.SetLabel("Fit") |
---|
99 | #self.btFit.Unbind(event=wx.EVT_BUTTON, id=self.btFit.GetId()) |
---|
100 | #self.btFit.Bind(event=wx.EVT_BUTTON, handler=self._onFit,id=self.btFit.GetId()) |
---|
101 | pass |
---|
102 | |
---|
103 | |
---|
104 | def _on_engine_change(self, event): |
---|
105 | """ |
---|
106 | get an event containing the current name of the fit engine type |
---|
107 | @param event: FitterTypeEvent containing the name of the current engine |
---|
108 | """ |
---|
109 | self.engine_type = event.type |
---|
110 | if len(self.parameters)==0: |
---|
111 | self.Layout() |
---|
112 | return |
---|
113 | if event.type =="park": |
---|
114 | self.btFit.SetLabel("Fit") |
---|
115 | |
---|
116 | for item in self.parameters: |
---|
117 | if event.type =="scipy" : |
---|
118 | item[5].SetValue("") |
---|
119 | item[5].Hide() |
---|
120 | item[6].SetValue("") |
---|
121 | item[6].Hide() |
---|
122 | self.text2_min.Hide() |
---|
123 | self.text2_max.Hide() |
---|
124 | |
---|
125 | else: |
---|
126 | item[5].Show(True) |
---|
127 | item[6].Show(True) |
---|
128 | self.text2_min.Show(True) |
---|
129 | self.text2_max.Show(True) |
---|
130 | for item in self.fittable_param: |
---|
131 | if item[5]!=None and item[6]!=None and not item in self.orientation_params_disp: |
---|
132 | if event.type =="scipy" and not item in self.orientation_params: |
---|
133 | item[5].SetValue("") |
---|
134 | item[5].Hide() |
---|
135 | item[6].SetValue("") |
---|
136 | item[6].Hide() |
---|
137 | self.text2_min.Hide() |
---|
138 | self.text2_max.Hide() |
---|
139 | self.text_disp_min.Hide() |
---|
140 | self.text_disp_max.Hide() |
---|
141 | else: |
---|
142 | item[5].Show(True) |
---|
143 | item[6].Show(True) |
---|
144 | self.text2_min.Show(True) |
---|
145 | self.text2_max.Show(True) |
---|
146 | self.text_disp_min.Show(True) |
---|
147 | self.text_disp_max.Show(True) |
---|
148 | |
---|
149 | for item in self.orientation_params: |
---|
150 | if item[5]!=None and item[6]!=None: |
---|
151 | if event.type =="scipy" or self.data.__class__.__name__ !="Data2D": |
---|
152 | item[5].SetValue("") |
---|
153 | item[5].Hide() |
---|
154 | item[6].SetValue("") |
---|
155 | item[6].Hide() |
---|
156 | else: |
---|
157 | item[5].Show(True) |
---|
158 | item[6].Show(True) |
---|
159 | |
---|
160 | for item in self.orientation_params_disp: |
---|
161 | if item[5]!=None and item[6]!=None: |
---|
162 | if event.type =="scipy" or self.data.__class__.__name__ !="Data2D": |
---|
163 | item[5].SetValue("") |
---|
164 | item[5].Hide() |
---|
165 | item[6].SetValue("") |
---|
166 | item[6].Hide() |
---|
167 | else: |
---|
168 | item[5].Show(True) |
---|
169 | item[6].Show(True) |
---|
170 | self.Layout() |
---|
171 | self.Refresh() |
---|
172 | |
---|
173 | def _fill_range_sizer(self): |
---|
174 | """ |
---|
175 | Fill the sizer containing the plotting range |
---|
176 | add access to npts |
---|
177 | """ |
---|
178 | is_2Ddata = False |
---|
179 | |
---|
180 | # Check if data is 2D |
---|
181 | if self.data.__class__.__name__ == 'Data2D': |
---|
182 | is_2Ddata = True |
---|
183 | |
---|
184 | title = "Fitting" |
---|
185 | #smear messages & titles |
---|
186 | smear_message_none = "No smearing is selected..." |
---|
187 | smear_message_dqdata = "The dQ data is being used for smearing..." |
---|
188 | smear_message_2d = "No smearing is supported for 2D model fitting..." |
---|
189 | smear_message_new_ssmear = "Please enter only the value of interest to customize smearing..." |
---|
190 | smear_message_new_psmear = "Please enter both; the dQ will be generated by interpolation..." |
---|
191 | smear_message_pinhole_min_title = "dQ_min[1/A]:" |
---|
192 | smear_message_pinhole_max_title = "dQ_max[1/A]:" |
---|
193 | smear_message_slit_height_title = "Slit height[1/A]:" |
---|
194 | smear_message_slit_width_title = "Slit width[1/A]:" |
---|
195 | #Fit button |
---|
196 | self.btFit = wx.Button(self,wx.NewId(),'Fit', size=(88,25)) |
---|
197 | self.btFit.Bind(wx.EVT_BUTTON, self._onFit,id= self.btFit.GetId()) |
---|
198 | self.btFit.SetToolTipString("Start fitting.") |
---|
199 | |
---|
200 | self._get_smear_info() |
---|
201 | |
---|
202 | #Sizers |
---|
203 | box_description_range = wx.StaticBox(self, -1,str(title)) |
---|
204 | boxsizer_range = wx.StaticBoxSizer(box_description_range, wx.VERTICAL) |
---|
205 | self.sizer_set_smearer = wx.BoxSizer(wx.VERTICAL) |
---|
206 | sizer_smearer = wx.BoxSizer(wx.HORIZONTAL) |
---|
207 | self.sizer_new_smear= wx.BoxSizer(wx.HORIZONTAL) |
---|
208 | self.sizer_set_masking = wx.BoxSizer(wx.HORIZONTAL) |
---|
209 | sizer_chi2 = wx.BoxSizer(wx.VERTICAL) |
---|
210 | smear_set_box= wx.StaticBox(self, -1,'Set Instrumental Smearing') |
---|
211 | sizer_smearer_box = wx.StaticBoxSizer(smear_set_box, wx.HORIZONTAL) |
---|
212 | sizer_smearer_box.SetMinSize((_DATA_BOX_WIDTH,85)) |
---|
213 | |
---|
214 | #textcntrl for new resolution |
---|
215 | self.smear_pinhole_max = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-20,20),style=wx.TE_PROCESS_ENTER, |
---|
216 | text_enter_callback = self.onPinholeSmear) |
---|
217 | self.smear_pinhole_min = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-20,20),style=wx.TE_PROCESS_ENTER, |
---|
218 | text_enter_callback = self.onPinholeSmear) |
---|
219 | self.smear_slit_height= self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-20,20),style=wx.TE_PROCESS_ENTER, |
---|
220 | text_enter_callback = self.onSlitSmear) |
---|
221 | self.smear_slit_width = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-20,20),style=wx.TE_PROCESS_ENTER, |
---|
222 | text_enter_callback = self.onSlitSmear) |
---|
223 | self.smear_data_left= BGTextCtrl(self, -1, size=(_BOX_WIDTH-20,20), style=0) |
---|
224 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
225 | self.smear_data_right = BGTextCtrl(self, -1, size=(_BOX_WIDTH-20,20), style=0) |
---|
226 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
227 | |
---|
228 | #set default values |
---|
229 | self.smear_pinhole_max.SetValue(str(self.dx_max)) |
---|
230 | self.smear_pinhole_min.SetValue(str(self.dx_min)) |
---|
231 | self.smear_slit_height.SetValue(str(self.dxl)) |
---|
232 | self.smear_slit_width.SetValue(str(self.dxw)) |
---|
233 | |
---|
234 | #Filling the sizer containing instruments smearing info. |
---|
235 | self.disable_smearer = wx.RadioButton(self, -1, 'None', style=wx.RB_GROUP) |
---|
236 | self.enable_smearer = wx.RadioButton(self, -1, 'Use dQ Data') |
---|
237 | #self.enable_smearer.SetToolTipString("Click to use the loaded dQ data for smearing.") |
---|
238 | self.pinhole_smearer = wx.RadioButton(self, -1, 'Custom Pinhole Smear') |
---|
239 | #self.pinhole_smearer.SetToolTipString("Click to input custom resolution for pinhole smearing.") |
---|
240 | self.slit_smearer = wx.RadioButton(self, -1, 'Custom Slit Smear') |
---|
241 | #self.slit_smearer.SetToolTipString("Click to input custom resolution for slit smearing.") |
---|
242 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.disable_smearer.GetId()) |
---|
243 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.enable_smearer.GetId()) |
---|
244 | self.Bind(wx.EVT_RADIOBUTTON, self.onPinholeSmear, id=self.pinhole_smearer.GetId()) |
---|
245 | self.Bind(wx.EVT_RADIOBUTTON, self.onSlitSmear, id=self.slit_smearer.GetId()) |
---|
246 | self.disable_smearer.SetValue(True) |
---|
247 | |
---|
248 | # add 4 types of smearing to the sizer |
---|
249 | sizer_smearer.Add( self.disable_smearer,0, wx.LEFT, 10) |
---|
250 | sizer_smearer.Add((10,10)) |
---|
251 | sizer_smearer.Add( self.enable_smearer) |
---|
252 | sizer_smearer.Add((10,10)) |
---|
253 | sizer_smearer.Add( self.pinhole_smearer ) |
---|
254 | sizer_smearer.Add((10,10)) |
---|
255 | sizer_smearer.Add( self.slit_smearer ) |
---|
256 | sizer_smearer.Add((10,10)) |
---|
257 | |
---|
258 | # StaticText for chi2, N(for fitting), Npts |
---|
259 | self.tcChi = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
260 | self.tcChi.SetToolTipString("Chi2/Npts") |
---|
261 | self.Npts_fit = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
262 | self.Npts_fit.SetToolTipString(" Npts : number of points selected for fitting") |
---|
263 | self.Npts_total = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
264 | self.Npts_total.SetValue(str(self.npts)) |
---|
265 | self.Npts_total.SetToolTipString(" Total Npts : total number of data points") |
---|
266 | box_description_1= wx.StaticText(self, -1,' Chi2/Npts') |
---|
267 | box_description_2= wx.StaticText(self, -1,' Npts') |
---|
268 | box_description_3= wx.StaticText(self, -1,' Total Npts') |
---|
269 | box_description_4= wx.StaticText(self, -1,' ') |
---|
270 | |
---|
271 | sizer_fit = wx.GridSizer(2, 4,2,6) |
---|
272 | sizer_fit.Add(box_description_1,0,0) |
---|
273 | sizer_fit.Add(box_description_2,0,0) |
---|
274 | sizer_fit.Add(box_description_3,0,0) |
---|
275 | sizer_fit.Add(box_description_4,0,0) |
---|
276 | sizer_fit.Add(self.tcChi,0,0) |
---|
277 | sizer_fit.Add(self.Npts_fit ,0,0) |
---|
278 | sizer_fit.Add(self.Npts_total,0,0) |
---|
279 | sizer_fit.Add(self.btFit,0,0) |
---|
280 | |
---|
281 | # StaticText for smear |
---|
282 | #self.tcChi = wx.StaticText(self, -1, "-", style=wx.ALIGN_LEFT) |
---|
283 | self.smear_description_none = wx.StaticText(self, -1, smear_message_none , style=wx.ALIGN_LEFT) |
---|
284 | self.smear_description_dqdata = wx.StaticText(self, -1, smear_message_dqdata , style=wx.ALIGN_LEFT) |
---|
285 | self.smear_description_type = wx.StaticText(self, -1, "Type:" , style=wx.ALIGN_LEFT) |
---|
286 | self.smear_description_smear_type = BGTextCtrl(self, -1, size=(45,20), style=0) |
---|
287 | self.smear_description_smear_type.SetValue(str(self.dq_l)) |
---|
288 | self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) |
---|
289 | self.smear_description_2d = wx.StaticText(self, -1, smear_message_2d , style=wx.ALIGN_LEFT) |
---|
290 | self.smear_message_new_s = wx.StaticText(self, -1, smear_message_new_ssmear , style=wx.ALIGN_LEFT) |
---|
291 | self.smear_message_new_p = wx.StaticText(self, -1, smear_message_new_psmear , style=wx.ALIGN_LEFT) |
---|
292 | self.smear_description_pin_min = wx.StaticText(self, -1, smear_message_pinhole_min_title , style=wx.ALIGN_LEFT) |
---|
293 | self.smear_description_pin_max = wx.StaticText(self, -1, smear_message_pinhole_max_title , style=wx.ALIGN_LEFT) |
---|
294 | self.smear_description_slit_height = wx.StaticText(self, -1, smear_message_slit_height_title , style=wx.ALIGN_LEFT) |
---|
295 | self.smear_description_slit_width = wx.StaticText(self, -1, smear_message_slit_width_title , style=wx.ALIGN_LEFT) |
---|
296 | |
---|
297 | #arrange sizers |
---|
298 | #boxsizer1.Add( self.tcChi ) |
---|
299 | self.sizer_set_smearer.Add(sizer_smearer ) |
---|
300 | self.sizer_set_smearer.Add((10,10)) |
---|
301 | self.sizer_set_smearer.Add( self.smear_description_none,0, wx.CENTER, 10 ) |
---|
302 | self.sizer_set_smearer.Add( self.smear_description_dqdata,0, wx.CENTER, 10 ) |
---|
303 | self.sizer_set_smearer.Add( self.smear_description_2d,0, wx.CENTER, 10 ) |
---|
304 | self.sizer_new_smear.Add( self.smear_description_type,0, wx.CENTER, 10 ) |
---|
305 | self.sizer_new_smear.Add( self.smear_description_smear_type,0, wx.CENTER, 10 ) |
---|
306 | self.sizer_new_smear.Add((15,-1)) |
---|
307 | self.sizer_new_smear.Add( self.smear_description_pin_min,0, wx.CENTER, 10 ) |
---|
308 | self.sizer_new_smear.Add( self.smear_description_slit_height,0, wx.CENTER, 10 ) |
---|
309 | self.sizer_new_smear.Add( self.smear_pinhole_min,0, wx.CENTER, 10 ) |
---|
310 | self.sizer_new_smear.Add( self.smear_slit_height,0, wx.CENTER, 10 ) |
---|
311 | self.sizer_new_smear.Add( self.smear_data_left,0, wx.CENTER, 10 ) |
---|
312 | self.sizer_new_smear.Add((20,-1)) |
---|
313 | self.sizer_new_smear.Add( self.smear_description_pin_max,0, wx.CENTER, 10 ) |
---|
314 | self.sizer_new_smear.Add( self.smear_description_slit_width,0, wx.CENTER, 10 ) |
---|
315 | self.sizer_new_smear.Add( self.smear_pinhole_max,0, wx.CENTER, 10 ) |
---|
316 | self.sizer_new_smear.Add( self.smear_slit_width,0, wx.CENTER, 10 ) |
---|
317 | self.sizer_new_smear.Add( self.smear_data_right,0, wx.CENTER, 10 ) |
---|
318 | |
---|
319 | self.sizer_set_smearer.Add( self.smear_message_new_s,0, wx.CENTER, 10) |
---|
320 | self.sizer_set_smearer.Add( self.smear_message_new_p,0, wx.CENTER, 10) |
---|
321 | self.sizer_set_smearer.Add((5,2)) |
---|
322 | self.sizer_set_smearer.Add( self.sizer_new_smear,0, wx.CENTER, 10 ) |
---|
323 | |
---|
324 | # add all to chi2 sizer |
---|
325 | sizer_smearer_box.Add(self.sizer_set_smearer) |
---|
326 | sizer_chi2.Add(sizer_smearer_box) |
---|
327 | sizer_chi2.Add((-1,5)) |
---|
328 | |
---|
329 | # hide all smear messages and textctrl |
---|
330 | self._hide_all_smear_info() |
---|
331 | |
---|
332 | # get smear_selection |
---|
333 | self.current_smearer= smear_selection( self.data ) |
---|
334 | |
---|
335 | # Show only the relevant smear messages, etc |
---|
336 | if self.current_smearer == None: |
---|
337 | if not is_2Ddata: |
---|
338 | self.smear_description_none.Show(True) |
---|
339 | self.enable_smearer.Disable() |
---|
340 | else: |
---|
341 | self.smear_description_2d.Show(True) |
---|
342 | self.pinhole_smearer.Disable() |
---|
343 | self.slit_smearer.Disable() |
---|
344 | self.enable_smearer.Disable() |
---|
345 | else: self._show_smear_sizer() |
---|
346 | boxsizer_range.Add(self.sizer_set_masking) |
---|
347 | |
---|
348 | #Set sizer for Fitting section |
---|
349 | self._set_range_sizer( title=title,box_sizer=boxsizer_range, object1=sizer_chi2, object= sizer_fit) |
---|
350 | |
---|
351 | def _fill_datainfo_sizer(self): |
---|
352 | """ |
---|
353 | fill sizer 0 with data info |
---|
354 | """ |
---|
355 | ## no loaded data , don't fill the sizer |
---|
356 | if self.data is None: |
---|
357 | data_min = "" |
---|
358 | data_max = "" |
---|
359 | data_name = "" |
---|
360 | else: |
---|
361 | data_name = self.data.name |
---|
362 | #set maximum range for x in linear scale |
---|
363 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
364 | # Minimum value of data |
---|
365 | data_min = min(self.data.x) |
---|
366 | # Maximum value of data |
---|
367 | data_max = max(self.data.x) |
---|
368 | else: |
---|
369 | ## Minimum value of data |
---|
370 | data_min = 0 |
---|
371 | x = max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
372 | y = max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
373 | ## Maximum value of data |
---|
374 | data_max = math.sqrt(x*x + y*y) |
---|
375 | |
---|
376 | box_description= wx.StaticBox(self, -1, 'Data') |
---|
377 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
378 | #---------------------------------------------------------- |
---|
379 | sizer_data = wx.BoxSizer(wx.HORIZONTAL) |
---|
380 | #Filling the sizer containing data related fields |
---|
381 | #self.dataSource = wx.StaticText(self, -1,str(self.data.name)) |
---|
382 | self.dataSource = BGTextCtrl(self, -1) |
---|
383 | self.dataSource.SetValue(str(data_name)) |
---|
384 | #self.dataSource.SetEditable(False) |
---|
385 | self.dataSource.SetMinSize((_DATA_BOX_WIDTH, -1)) |
---|
386 | sizer_data.Add(wx.StaticText(self, -1, 'Source Name : ')) |
---|
387 | sizer_data.Add(self.dataSource ) |
---|
388 | sizer_data.Add( (0,5) ) |
---|
389 | |
---|
390 | #---------sizer 2 draw-------------------------------- |
---|
391 | text4_3 = wx.StaticText(self, -1, 'Total Q Range (1/A)', |
---|
392 | style=wx.ALIGN_LEFT) |
---|
393 | sizer_range = wx.BoxSizer(wx.HORIZONTAL) |
---|
394 | sizer_range.Add( text4_3 ,0, wx.RIGHT, 10) |
---|
395 | self.minimum_q = BGTextCtrl(self, -1, size=(_BOX_WIDTH,20)) |
---|
396 | self.minimum_q.SetValue(str(data_min)) |
---|
397 | #self.minimum_q.SetEditable(False) |
---|
398 | self.maximum_q = BGTextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
399 | self.maximum_q.SetValue(str(data_max)) |
---|
400 | #self.maximum_q.SetEditable(False) |
---|
401 | sizer_range.Add(wx.StaticText(self, -1, "Min: "),0, wx.LEFT, 10) |
---|
402 | sizer_range.Add(self.minimum_q,0, wx.LEFT, 10) |
---|
403 | sizer_range.Add(wx.StaticText(self, -1, "Max: "),0, wx.LEFT, 10) |
---|
404 | sizer_range.Add(self.maximum_q,0, wx.LEFT, 10) |
---|
405 | |
---|
406 | ## set q range to plot |
---|
407 | self.qmin_x = data_min |
---|
408 | self.qmax_x = data_max |
---|
409 | |
---|
410 | boxsizer1.Add(sizer_data,0, wx.ALL, 10) |
---|
411 | boxsizer1.Add(sizer_range, 0 , wx.LEFT, 10) |
---|
412 | #------------------------------------------------------------ |
---|
413 | self.sizer0.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
414 | self.sizer0.Layout() |
---|
415 | |
---|
416 | def _fill_model_sizer(self, sizer): |
---|
417 | """ |
---|
418 | fill sizer containing model info |
---|
419 | """ |
---|
420 | ##Add model function Details button in fitpanel. |
---|
421 | ##The following 3 lines are for Mac. Let JHC know before modifying... |
---|
422 | title = "Model" |
---|
423 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
424 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
425 | |
---|
426 | id = wx.NewId() |
---|
427 | self.model_help =wx.Button(self,id,'Details', size=(80,23)) |
---|
428 | self.model_help.Bind(wx.EVT_BUTTON, self.on_model_help_clicked,id=id) |
---|
429 | self.model_help.SetToolTipString("Model Function Help") |
---|
430 | |
---|
431 | ## class base method to add view 2d button |
---|
432 | self._set_model_sizer(sizer=sizer, box_sizer=boxsizer1, title="Model",object=self.model_help ) |
---|
433 | |
---|
434 | |
---|
435 | def _set_sizer_dispersion(self, dispersity): |
---|
436 | """ |
---|
437 | draw sizer with gaussian dispersity parameters |
---|
438 | """ |
---|
439 | self.fittable_param=[] |
---|
440 | self.fixed_param=[] |
---|
441 | self.orientation_params_disp=[] |
---|
442 | |
---|
443 | self.sizer4_4.Clear(True) |
---|
444 | if self.model==None: |
---|
445 | ##no model is selected |
---|
446 | return |
---|
447 | if not self.enable_disp.GetValue(): |
---|
448 | ## the user didn't select dispersity display |
---|
449 | return |
---|
450 | |
---|
451 | self._reset_dispersity() |
---|
452 | |
---|
453 | # Create the dispersion objects |
---|
454 | for item in self.model.dispersion.keys(): |
---|
455 | #disp_model = GaussianDispersion() |
---|
456 | disp_model = dispersity() |
---|
457 | self._disp_obj_dict[item] = disp_model |
---|
458 | self.model.set_dispersion(item, disp_model) |
---|
459 | self.state._disp_obj_dict[item]= disp_model |
---|
460 | |
---|
461 | |
---|
462 | ix=0 |
---|
463 | iy=1 |
---|
464 | disp = wx.StaticText(self, -1, 'Names') |
---|
465 | self.sizer4_4.Add(disp,( iy, ix),(1,1), |
---|
466 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
467 | ix += 1 |
---|
468 | values = wx.StaticText(self, -1, 'Sigmas (STD)') |
---|
469 | self.sizer4_4.Add(values,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
470 | ix +=2 |
---|
471 | self.text_disp_1 = wx.StaticText(self, -1, '') |
---|
472 | self.sizer4_4.Add( self.text_disp_1,(iy, ix),(1,1),\ |
---|
473 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
474 | self.text_disp_1.Hide() |
---|
475 | |
---|
476 | |
---|
477 | ix +=1 |
---|
478 | self.text_disp_min = wx.StaticText(self, -1, 'Min') |
---|
479 | self.sizer4_4.Add(self.text_disp_min,(iy, ix),(1,1),\ |
---|
480 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
481 | self.text_disp_min.Hide() |
---|
482 | ix +=1 |
---|
483 | self.text_disp_max = wx.StaticText(self, -1, 'Max') |
---|
484 | self.sizer4_4.Add(self.text_disp_max,(iy, ix),(1,1),\ |
---|
485 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
486 | self.text_disp_max.Hide() |
---|
487 | |
---|
488 | |
---|
489 | ix += 1 |
---|
490 | npts = wx.StaticText(self, -1, 'Npts') |
---|
491 | self.sizer4_4.Add(npts,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
492 | ix += 1 |
---|
493 | nsigmas = wx.StaticText(self, -1, 'Nsigmas') |
---|
494 | self.sizer4_4.Add(nsigmas,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
495 | |
---|
496 | if self.engine_type=="park": |
---|
497 | self.text_disp_max.Show(True) |
---|
498 | self.text_disp_min.Show(True) |
---|
499 | |
---|
500 | for item in self.model.dispersion.keys(): |
---|
501 | if not item in self.model.orientation_params: |
---|
502 | if not self.disp_cb_dict.has_key(item): |
---|
503 | self.disp_cb_dict[item]= None |
---|
504 | name1=item+".width" |
---|
505 | name2=item+".npts" |
---|
506 | name3=item+".nsigmas" |
---|
507 | if not self.model.details.has_key(name1): |
---|
508 | self.model.details [name1] = ["",None,None] |
---|
509 | |
---|
510 | iy += 1 |
---|
511 | for p in self.model.dispersion[item].keys(): |
---|
512 | |
---|
513 | if p=="width": |
---|
514 | ix = 0 |
---|
515 | cb = wx.CheckBox(self, -1, name1, (10, 10)) |
---|
516 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
517 | self.sizer4_4.Add( cb,( iy, ix),(1,1), |
---|
518 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
519 | ix = 1 |
---|
520 | value= self.model.getParam(name1) |
---|
521 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
522 | style=wx.TE_PROCESS_ENTER) |
---|
523 | ctl1.SetValue(str (format_number(value))) |
---|
524 | self.sizer4_4.Add(ctl1, (iy,ix),(1,1),wx.EXPAND) |
---|
525 | ## text to show error sign |
---|
526 | ix = 2 |
---|
527 | text2=wx.StaticText(self, -1, '+/-') |
---|
528 | self.sizer4_4.Add(text2,(iy, ix),(1,1), |
---|
529 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
530 | text2.Hide() |
---|
531 | |
---|
532 | ix = 3 |
---|
533 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
534 | |
---|
535 | self.sizer4_4.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
536 | |
---|
537 | ctl2.Hide() |
---|
538 | |
---|
539 | ix = 4 |
---|
540 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
541 | text_enter_callback = self._onparamRangeEnter) |
---|
542 | |
---|
543 | self.sizer4_4.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
544 | ctl3.Hide() |
---|
545 | |
---|
546 | ix = 5 |
---|
547 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
548 | text_enter_callback = self._onparamRangeEnter) |
---|
549 | self.sizer4_4.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
550 | |
---|
551 | ctl4.Hide() |
---|
552 | |
---|
553 | if self.engine_type=="park": |
---|
554 | ctl3.Show(True) |
---|
555 | ctl4.Show(True) |
---|
556 | |
---|
557 | self.fittable_param.append([cb,name1,ctl1,text2, |
---|
558 | ctl2, ctl3, ctl4,None]) |
---|
559 | |
---|
560 | elif p=="npts": |
---|
561 | ix = 6 |
---|
562 | value= self.model.getParam(name2) |
---|
563 | Tctl = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
564 | style=wx.TE_PROCESS_ENTER) |
---|
565 | |
---|
566 | Tctl.SetValue(str (format_number(value))) |
---|
567 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
568 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
569 | self.fixed_param.append([None,name2, Tctl,None,None, |
---|
570 | None, None,None]) |
---|
571 | elif p=="nsigmas": |
---|
572 | ix = 7 |
---|
573 | value= self.model.getParam(name3) |
---|
574 | Tct2 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
575 | style=wx.TE_PROCESS_ENTER) |
---|
576 | Tct2.SetValue(str (format_number(value))) |
---|
577 | self.sizer4_4.Add(Tct2, (iy,ix),(1,1), |
---|
578 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
579 | ix +=1 |
---|
580 | self.sizer4_4.Add((20,20), (iy,ix),(1,1), |
---|
581 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
582 | |
---|
583 | self.fixed_param.append([None,name3, Tct2 |
---|
584 | ,None,None,None, None,None]) |
---|
585 | |
---|
586 | ix =0 |
---|
587 | iy +=1 |
---|
588 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
589 | for item in self.model.dispersion.keys(): |
---|
590 | if item in self.model.orientation_params: |
---|
591 | if not self.disp_cb_dict.has_key(item): |
---|
592 | self.disp_cb_dict[item]= None |
---|
593 | name1=item+".width" |
---|
594 | name2=item+".npts" |
---|
595 | name3=item+".nsigmas" |
---|
596 | if not self.model.details.has_key(name1): |
---|
597 | self.model.details [name1] = ["",None,None] |
---|
598 | |
---|
599 | |
---|
600 | iy += 1 |
---|
601 | for p in self.model.dispersion[item].keys(): |
---|
602 | |
---|
603 | if p=="width": |
---|
604 | ix = 0 |
---|
605 | cb = wx.CheckBox(self, -1, name1, (10, 10)) |
---|
606 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
607 | self.sizer4_4.Add( cb,( iy, ix),(1,1), |
---|
608 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
609 | if self.data.__class__.__name__ =="Data2D": |
---|
610 | cb.Show(True) |
---|
611 | elif cb.IsShown(): |
---|
612 | cb.Hide() |
---|
613 | ix = 1 |
---|
614 | value= self.model.getParam(name1) |
---|
615 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
616 | style=wx.TE_PROCESS_ENTER) |
---|
617 | ctl1.SetValue(str (format_number(value))) |
---|
618 | if self.data.__class__.__name__ =="Data2D": |
---|
619 | ctl1.Show(True) |
---|
620 | elif ctl1.IsShown(): |
---|
621 | ctl1.Hide() |
---|
622 | self.sizer4_4.Add(ctl1, (iy,ix),(1,1),wx.EXPAND) |
---|
623 | ## text to show error sign |
---|
624 | ix = 2 |
---|
625 | text2=wx.StaticText(self, -1, '+/-') |
---|
626 | self.sizer4_4.Add(text2,(iy, ix),(1,1), |
---|
627 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
628 | text2.Hide() |
---|
629 | |
---|
630 | ix = 3 |
---|
631 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
632 | |
---|
633 | self.sizer4_4.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
634 | ctl2.Hide() |
---|
635 | |
---|
636 | ix = 4 |
---|
637 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
638 | text_enter_callback = self._onparamRangeEnter) |
---|
639 | |
---|
640 | self.sizer4_4.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
641 | |
---|
642 | ctl3.Hide() |
---|
643 | |
---|
644 | ix = 5 |
---|
645 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
646 | text_enter_callback = self._onparamRangeEnter) |
---|
647 | self.sizer4_4.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
648 | ctl4.Hide() |
---|
649 | #if self.data.__class__.__name__ =="Data2D": |
---|
650 | #ctl4.Enable(True) |
---|
651 | #elif ctl4.Shown(): |
---|
652 | #ctl4.Hide() |
---|
653 | |
---|
654 | if self.engine_type=="park" and self.data.__class__.__name__ =="Data2D": |
---|
655 | ctl3.Show(True) |
---|
656 | ctl4.Show(True) |
---|
657 | |
---|
658 | |
---|
659 | |
---|
660 | |
---|
661 | self.fittable_param.append([cb,name1,ctl1,text2, |
---|
662 | ctl2, ctl3, ctl4,None]) |
---|
663 | self.orientation_params_disp.append([cb,name1,ctl1,text2, |
---|
664 | ctl2, ctl3, ctl4,None]) |
---|
665 | elif p=="npts": |
---|
666 | ix = 6 |
---|
667 | value= self.model.getParam(name2) |
---|
668 | Tctl = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
669 | style=wx.TE_PROCESS_ENTER) |
---|
670 | |
---|
671 | Tctl.SetValue(str (format_number(value))) |
---|
672 | if self.data.__class__.__name__ =="Data2D": |
---|
673 | Tctl.Show(True) |
---|
674 | else: |
---|
675 | Tctl.Hide() |
---|
676 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
677 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
678 | self.fixed_param.append([None,name2, Tctl,None,None, |
---|
679 | None, None,None]) |
---|
680 | self.orientation_params_disp.append([None,name2, Tctl,None,None, |
---|
681 | None, None,None]) |
---|
682 | elif p=="nsigmas": |
---|
683 | ix = 7 |
---|
684 | value= self.model.getParam(name3) |
---|
685 | Tct2 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
686 | style=wx.TE_PROCESS_ENTER) |
---|
687 | Tct2.SetValue(str (format_number(value))) |
---|
688 | if self.data.__class__.__name__ =="Data2D": |
---|
689 | Tct2.Show(True) |
---|
690 | else: |
---|
691 | Tct2.Hide() |
---|
692 | self.sizer4_4.Add(Tct2, (iy,ix),(1,1), |
---|
693 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
694 | ix +=1 |
---|
695 | |
---|
696 | self.fixed_param.append([None,name3, Tct2 |
---|
697 | ,None,None, None, None,None]) |
---|
698 | |
---|
699 | self.orientation_params_disp.append([None,name3, Tct2 |
---|
700 | ,None,None, None, None,None]) |
---|
701 | |
---|
702 | |
---|
703 | self.state.disp_cb_dict = copy.deepcopy(self.disp_cb_dict) |
---|
704 | |
---|
705 | self.state.model = self.model.clone() |
---|
706 | ## save state into |
---|
707 | self.state.cb1 = self.cb1.GetValue() |
---|
708 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
709 | self._copy_parameters_state(self.orientation_params_disp, |
---|
710 | self.state.orientation_params_disp) |
---|
711 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
712 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
713 | |
---|
714 | |
---|
715 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
716 | " Selected Distribution: Gaussian")) |
---|
717 | #Fill the list of fittable parameters |
---|
718 | self.select_all_param(event=None) |
---|
719 | |
---|
720 | self.Layout() |
---|
721 | |
---|
722 | |
---|
723 | def _onFit(self, event): |
---|
724 | """ |
---|
725 | Allow to fit |
---|
726 | """ |
---|
727 | #make sure all parameter values are updated. |
---|
728 | if self.check_invalid_panel(): |
---|
729 | return |
---|
730 | flag = self._update_paramv_on_fit() |
---|
731 | |
---|
732 | if not flag: |
---|
733 | msg= "Fitting range or parameters are invalid" |
---|
734 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
735 | return |
---|
736 | |
---|
737 | if len(self.param_toFit) <= 0: |
---|
738 | msg= "Select at least one parameter to fit" |
---|
739 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
740 | return |
---|
741 | |
---|
742 | |
---|
743 | self.select_param(event =None) |
---|
744 | |
---|
745 | #Clear errors if exist from previous fitting |
---|
746 | #self._clear_Err_on_Fit() |
---|
747 | |
---|
748 | # Remove or do not allow fitting on the Q=0 point, especially when y(q=0)=None at x[0]. |
---|
749 | self.qmin_x = float(self.qmin.GetValue()) |
---|
750 | self.qmax_x = float( self.qmax.GetValue()) |
---|
751 | self.manager._reset_schedule_problem( value=0) |
---|
752 | self.manager.schedule_for_fit( value=1,page=self,fitproblem =None) |
---|
753 | self.manager.set_fit_range(page= self,qmin= self.qmin_x, qmax= self.qmax_x) |
---|
754 | |
---|
755 | #single fit |
---|
756 | self.manager.onFit() |
---|
757 | ## allow stopping the fit |
---|
758 | #if self.engine_type=="scipy": |
---|
759 | # self.btFit.SetLabel("Stop") |
---|
760 | # self.btFit.Unbind(event=wx.EVT_BUTTON, id= self.btFit.GetId()) |
---|
761 | # self.btFit.Bind(event= wx.EVT_BUTTON, handler=self._StopFit, id=self.btFit.GetId()) |
---|
762 | #else: |
---|
763 | # self.btFit.SetLabel("Fit") |
---|
764 | # self.btFit.Bind(event= wx.EVT_BUTTON, handler=self._onFit, id=self.btFit.GetId()) |
---|
765 | |
---|
766 | |
---|
767 | |
---|
768 | def _StopFit(self, event): |
---|
769 | """ |
---|
770 | Stop fit |
---|
771 | """ |
---|
772 | self.btFit.SetLabel("Fit") |
---|
773 | if self.engine_type=="scipy": |
---|
774 | self.manager.stop_fit() |
---|
775 | self.btFit.Unbind(event=wx.EVT_BUTTON, id=self.btFit.GetId()) |
---|
776 | self.btFit.Bind(event=wx.EVT_BUTTON, handler=self._onFit,id=self.btFit.GetId()) |
---|
777 | |
---|
778 | |
---|
779 | def _on_select_model(self, event): |
---|
780 | """ |
---|
781 | call back for model selection |
---|
782 | """ |
---|
783 | self._on_select_model_helper() |
---|
784 | self.set_model_param_sizer(self.model) |
---|
785 | |
---|
786 | self.enable_disp.SetValue(False) |
---|
787 | self.disable_disp.SetValue(True) |
---|
788 | try: |
---|
789 | self.set_dispers_sizer() |
---|
790 | except: |
---|
791 | pass |
---|
792 | self.btFit.SetFocus() |
---|
793 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
794 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
795 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
796 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
797 | |
---|
798 | self.state.structurecombobox = self.structurebox.GetCurrentSelection() |
---|
799 | self.state.formfactorcombobox = self.formfactorbox.GetCurrentSelection() |
---|
800 | |
---|
801 | if self.model != None: |
---|
802 | try: |
---|
803 | temp_smear= None |
---|
804 | if self.enable_smearer.GetValue(): |
---|
805 | temp_smear= self.smearer |
---|
806 | self.compute_chisqr(temp_smear) |
---|
807 | except: |
---|
808 | ## error occured on chisqr computation |
---|
809 | pass |
---|
810 | if self.data is not None and self.data.__class__.__name__ !="Data2D": |
---|
811 | ## set smearing value whether or not the data contain the smearing info |
---|
812 | evt = ModelEventbox(model=self.model, |
---|
813 | smearer=temp_smear, |
---|
814 | qmin= float(self.qmin_x), |
---|
815 | qmax= float(self.qmax_x)) |
---|
816 | else: |
---|
817 | evt = ModelEventbox(model=self.model) |
---|
818 | wx.PostEvent(self.event_owner, evt) |
---|
819 | |
---|
820 | if event !=None: |
---|
821 | #self._undo.Enable(True) |
---|
822 | ## post state to fit panel |
---|
823 | event = PageInfoEvent(page = self) |
---|
824 | wx.PostEvent(self.parent, event) |
---|
825 | |
---|
826 | |
---|
827 | def _onparamEnter(self,event): |
---|
828 | """ |
---|
829 | when enter value on panel redraw model according to changed |
---|
830 | """ |
---|
831 | #default flag |
---|
832 | flag = False |
---|
833 | self.fitrange = True |
---|
834 | #get event object |
---|
835 | tcrtl= event.GetEventObject() |
---|
836 | |
---|
837 | #Clear msg if previously shown. |
---|
838 | msg= "" |
---|
839 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
840 | |
---|
841 | if check_float(tcrtl): |
---|
842 | flag = self._onparamEnter_helper() |
---|
843 | if self.fitrange: |
---|
844 | temp_smearer = None |
---|
845 | if not self.disable_smearer.GetValue(): |
---|
846 | temp_smearer= self.current_smearer |
---|
847 | ## set smearing value whether or not the data contain the smearing info |
---|
848 | if self.slit_smearer.GetValue(): |
---|
849 | flag1 = self.update_slit_smear() |
---|
850 | flag = flag or flag1 |
---|
851 | elif self.pinhole_smearer.GetValue(): |
---|
852 | flag1 = self.update_pinhole_smear() |
---|
853 | flag = flag or flag1 |
---|
854 | elif self.data.__class__.__name__ !="Data2D": |
---|
855 | self.manager.set_smearer(smearer=temp_smearer, qmin= float(self.qmin_x), |
---|
856 | qmax= float(self.qmax_x)) |
---|
857 | if flag: |
---|
858 | self.compute_chisqr(smearer= temp_smearer) |
---|
859 | |
---|
860 | ## new state posted |
---|
861 | if self.state_change: |
---|
862 | #self._undo.Enable(True) |
---|
863 | event = PageInfoEvent(page = self) |
---|
864 | wx.PostEvent(self.parent, event) |
---|
865 | self.state_change= False |
---|
866 | else: |
---|
867 | return # invalid fit range: do nothing here: msg already displayed in validate |
---|
868 | else: |
---|
869 | self.save_current_state() |
---|
870 | msg= "Cannot Plot :Must enter a number!!! " |
---|
871 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
872 | |
---|
873 | self.save_current_state() |
---|
874 | return |
---|
875 | |
---|
876 | def _onparamRangeEnter(self, event): |
---|
877 | """ |
---|
878 | Check validity of value enter in the parameters range field |
---|
879 | """ |
---|
880 | if self.check_invalid_panel(): |
---|
881 | return |
---|
882 | tcrtl= event.GetEventObject() |
---|
883 | #Clear msg if previously shown. |
---|
884 | msg= "" |
---|
885 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
886 | # Flag to register when a parameter has changed. |
---|
887 | is_modified = False |
---|
888 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
889 | try: |
---|
890 | value = float(tcrtl.GetValue()) |
---|
891 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
892 | self._check_value_enter(self.fittable_param ,is_modified) |
---|
893 | self._check_value_enter(self.parameters ,is_modified) |
---|
894 | except: |
---|
895 | tcrtl.SetBackgroundColour("pink") |
---|
896 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
897 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
898 | return |
---|
899 | else: |
---|
900 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
901 | |
---|
902 | #self._undo.Enable(True) |
---|
903 | self.save_current_state() |
---|
904 | event = PageInfoEvent(page = self) |
---|
905 | wx.PostEvent(self.parent, event) |
---|
906 | self.state_change= False |
---|
907 | |
---|
908 | |
---|
909 | def _onQrangeEnter(self, event): |
---|
910 | """ |
---|
911 | Check validity of value enter in the Q range field |
---|
912 | """ |
---|
913 | if self.check_invalid_panel(): |
---|
914 | return |
---|
915 | tcrtl= event.GetEventObject() |
---|
916 | #Clear msg if previously shown. |
---|
917 | msg= "" |
---|
918 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
919 | # Flag to register when a parameter has changed. |
---|
920 | is_modified = False |
---|
921 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
922 | try: |
---|
923 | value = float(tcrtl.GetValue()) |
---|
924 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
925 | |
---|
926 | # If qmin and qmax have been modified, update qmin and qmax |
---|
927 | if self._validate_qrange( self.qmin, self.qmax): |
---|
928 | tempmin = float(self.qmin.GetValue()) |
---|
929 | if tempmin != self.qmin_x: |
---|
930 | self.qmin_x = tempmin |
---|
931 | tempmax = float(self.qmax.GetValue()) |
---|
932 | if tempmax != self.qmax_x: |
---|
933 | self.qmax_x = tempmax |
---|
934 | else: |
---|
935 | tcrtl.SetBackgroundColour("pink") |
---|
936 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
937 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
938 | return |
---|
939 | |
---|
940 | except: |
---|
941 | tcrtl.SetBackgroundColour("pink") |
---|
942 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
943 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
944 | return |
---|
945 | #Check if # of points for theory model are valid(>0). |
---|
946 | # check for 2d |
---|
947 | if self.data.__class__.__name__ =="Data2D": |
---|
948 | # set mask |
---|
949 | radius= numpy.sqrt( self.data.qx_data*self.data.qx_data + self.data.qy_data*self.data.qy_data ) |
---|
950 | index_data = ((self.qmin <= radius)&(radius<= self.qmax)) |
---|
951 | index_data = (index_data)&(self.data.mask) |
---|
952 | index_data = (index_data)&(numpy.isfinite(self.data.data)) |
---|
953 | if len(index_data[index_data]) < 10: |
---|
954 | msg= "Cannot Plot :No or too little npts in that data range!!! " |
---|
955 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
956 | return |
---|
957 | else: |
---|
958 | self.data.mask = index_data |
---|
959 | self.Npts_fit.Setvalue(str(len(self.data.mask))) |
---|
960 | else: |
---|
961 | index_data = ((self.qmin <= self.data.x)&(self.data.x <= self.qmax)) |
---|
962 | self.Npts_fit.SetValue(str(len(self.data.x[index_data]))) |
---|
963 | |
---|
964 | else: |
---|
965 | tcrtl.SetBackgroundColour("pink") |
---|
966 | msg= "Model Error:wrong value entered!!!" |
---|
967 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
968 | |
---|
969 | self.compute_chisqr() |
---|
970 | #self._undo.Enable(True) |
---|
971 | self.save_current_state() |
---|
972 | event = PageInfoEvent(page = self) |
---|
973 | wx.PostEvent(self.parent, event) |
---|
974 | self.state_change= False |
---|
975 | self._draw_model() |
---|
976 | return |
---|
977 | def _clear_Err_on_Fit(self): |
---|
978 | """ |
---|
979 | hide the error text control shown |
---|
980 | after fitting |
---|
981 | """ |
---|
982 | |
---|
983 | if hasattr(self,"text2_3"): |
---|
984 | self.text2_3.Hide() |
---|
985 | |
---|
986 | if len(self.parameters)>0: |
---|
987 | for item in self.parameters: |
---|
988 | #Skip t ifhe angle parameters if 1D data |
---|
989 | if self.data.__class__.__name__ !="Data2D": |
---|
990 | if item in self.orientation_params: |
---|
991 | continue |
---|
992 | if item in self.param_toFit: |
---|
993 | continue |
---|
994 | ## hide statictext +/- |
---|
995 | if item[3]!=None and item[3].IsShown(): |
---|
996 | item[3].Hide() |
---|
997 | ## hide textcrtl for error after fit |
---|
998 | if item[4]!=None and item[4].IsShown(): |
---|
999 | item[4].Hide() |
---|
1000 | |
---|
1001 | if len(self.fittable_param)>0: |
---|
1002 | for item in self.fittable_param: |
---|
1003 | #Skip t ifhe angle parameters if 1D data |
---|
1004 | if self.data.__class__.__name__ !="Data2D": |
---|
1005 | if item in self.orientation_params: |
---|
1006 | continue |
---|
1007 | if item in self.param_toFit: |
---|
1008 | continue |
---|
1009 | ## hide statictext +/- |
---|
1010 | if item[3]!=None and item[3].IsShown(): |
---|
1011 | item[3].Hide() |
---|
1012 | ## hide textcrtl for error after fit |
---|
1013 | if item[4]!=None and item[4].IsShown(): |
---|
1014 | item[4].Hide() |
---|
1015 | return |
---|
1016 | |
---|
1017 | def _get_defult_custom_smear(self): |
---|
1018 | """ |
---|
1019 | Get the defult values for custum smearing. |
---|
1020 | """ |
---|
1021 | |
---|
1022 | # get the default values |
---|
1023 | if self.dxl == None: self.dxl = 0.0 |
---|
1024 | if self.dxw == None: self.dxw = "" |
---|
1025 | if self.dx_min == None: self.dx_min = SMEAR_SIZE_L |
---|
1026 | if self.dx_max == None: self.dx_max = SMEAR_SIZE_H |
---|
1027 | |
---|
1028 | def _get_smear_info(self): |
---|
1029 | """ |
---|
1030 | Get the smear info from data. |
---|
1031 | @return: self.smear_type, self.dq_l and self.dq_r, |
---|
1032 | respectively the type of the smear, dq_min and dq_max for pinhole smear data |
---|
1033 | while dxl and dxw for slit smear |
---|
1034 | """ |
---|
1035 | |
---|
1036 | # default |
---|
1037 | self.smear_type = None |
---|
1038 | self.dq_l = None |
---|
1039 | self.dq_r = None |
---|
1040 | |
---|
1041 | if self.data is None or\ |
---|
1042 | self.data.__class__.__name__ == 'Data2D': |
---|
1043 | return |
---|
1044 | |
---|
1045 | data = self.data |
---|
1046 | |
---|
1047 | # check if it is pinhole smear and get min max if it is. |
---|
1048 | if data.dx != None and all(data.dx !=0): |
---|
1049 | self.smear_type = "Pinhole" |
---|
1050 | self.dq_l = numpy.min(data.dx) |
---|
1051 | self.dq_r = numpy.max(data.dx) |
---|
1052 | |
---|
1053 | # check if it is slit smear and get min max if it is. |
---|
1054 | elif data.dxl != None or data.dxw != None: |
---|
1055 | self.smear_type = "Slit" |
---|
1056 | if data.dxl != None and all(data.dxl !=0): |
---|
1057 | self.dq_l = data.dxl[0] |
---|
1058 | if data.dxw != None and all(data.dxw !=0): |
---|
1059 | self.dq_r = data.dxw[0] |
---|
1060 | |
---|
1061 | #return self.smear_type,self.dq_l,self.dq_r |
---|
1062 | |
---|
1063 | def _show_smear_sizer(self): |
---|
1064 | """ |
---|
1065 | Show only the sizers depending on smear selection |
---|
1066 | """ |
---|
1067 | if self.disable_smearer.GetValue(): |
---|
1068 | self.smear_description_none.Show(True) |
---|
1069 | elif self.enable_smearer.GetValue(): |
---|
1070 | self.smear_description_dqdata.Show(True) |
---|
1071 | if self.smear_type != None: |
---|
1072 | self.smear_description_type.Show(True) |
---|
1073 | self.smear_description_smear_type.Show(True) |
---|
1074 | if self.smear_type == 'Slit': |
---|
1075 | self.smear_description_slit_height.Show(True) |
---|
1076 | self.smear_description_slit_width.Show(True) |
---|
1077 | elif self.smear_type == 'Pinhole': |
---|
1078 | self.smear_description_pin_min.Show(True) |
---|
1079 | self.smear_description_pin_max.Show(True) |
---|
1080 | self.smear_data_left.Show(True) |
---|
1081 | self.smear_data_right.Show(True) |
---|
1082 | elif self.pinhole_smearer.GetValue(): |
---|
1083 | self.smear_message_new_p.Show(True) |
---|
1084 | self.smear_description_pin_min.Show(True) |
---|
1085 | self.smear_pinhole_min.Show(True) |
---|
1086 | self.smear_description_pin_max.Show(True) |
---|
1087 | self.smear_pinhole_max.Show(True) |
---|
1088 | elif self.slit_smearer.GetValue(): |
---|
1089 | self.smear_message_new_s.Show(True) |
---|
1090 | self.smear_description_slit_height.Show(True) |
---|
1091 | self.smear_slit_height.Show(True) |
---|
1092 | self.smear_description_slit_width.Show(True) |
---|
1093 | self.smear_slit_width.Show(True) |
---|
1094 | |
---|
1095 | def _hide_all_smear_info(self): |
---|
1096 | """ |
---|
1097 | Hide all smearing messages in the set_smearer sizer |
---|
1098 | """ |
---|
1099 | self.smear_description_none.Hide() |
---|
1100 | self.smear_description_dqdata.Hide() |
---|
1101 | self.smear_description_type.Hide() |
---|
1102 | self.smear_description_smear_type.Hide() |
---|
1103 | self.smear_description_2d.Hide() |
---|
1104 | self.smear_data_left.Hide() |
---|
1105 | self.smear_data_right.Hide() |
---|
1106 | self.smear_description_pin_min.Hide() |
---|
1107 | self.smear_pinhole_min.Hide() |
---|
1108 | self.smear_description_pin_max.Hide() |
---|
1109 | self.smear_pinhole_max.Hide() |
---|
1110 | self.smear_description_slit_height.Hide() |
---|
1111 | self.smear_slit_height.Hide() |
---|
1112 | self.smear_description_slit_width.Hide() |
---|
1113 | self.smear_slit_width.Hide() |
---|
1114 | self.smear_message_new_p.Hide() |
---|
1115 | self.smear_message_new_s.Hide() |
---|
1116 | |
---|
1117 | def _onMask(self, event): |
---|
1118 | """ |
---|
1119 | Build a panel to allow to edit Mask |
---|
1120 | """ |
---|
1121 | |
---|
1122 | from sans.guiframe.local_perspectives.plotting.masking import MaskPanel as MaskDialog |
---|
1123 | |
---|
1124 | self.panel = MaskDialog(self, data=self.data,id =-1 ) |
---|
1125 | self.panel.Bind(wx.EVT_CLOSE, self._draw_masked_model) |
---|
1126 | self.panel.ShowModal() |
---|
1127 | #wx.PostEvent(self.parent, event) |
---|
1128 | |
---|
1129 | def _draw_masked_model(self,event): |
---|
1130 | event.Skip() |
---|
1131 | |
---|
1132 | is_valid_qrange = self._update_paramv_on_fit() |
---|
1133 | if is_valid_qrange: |
---|
1134 | self._draw_model() |
---|
1135 | self.panel.Destroy() # frame |
---|
1136 | self.set_npts2fit() |
---|
1137 | else: |
---|
1138 | msg = ' Please consider your Q range.' |
---|
1139 | self.panel.ShowMessage(msg) |
---|
1140 | |
---|
1141 | def set_data(self, data): |
---|
1142 | """ |
---|
1143 | reset the current data |
---|
1144 | """ |
---|
1145 | self.data = data |
---|
1146 | |
---|
1147 | |
---|
1148 | if self.data is None: |
---|
1149 | data_min = "" |
---|
1150 | data_max = "" |
---|
1151 | data_name = "" |
---|
1152 | self.formfactorbox.Disable() |
---|
1153 | self.structurebox.Disable() |
---|
1154 | else: |
---|
1155 | self.smearer = smear_selection( self.data ) |
---|
1156 | self.disable_smearer.SetValue(True) |
---|
1157 | if self.smearer == None: |
---|
1158 | self.enable_smearer.Disable() |
---|
1159 | else: |
---|
1160 | self.enable_smearer.Enable() |
---|
1161 | |
---|
1162 | # more disables for 2D |
---|
1163 | if self.data.__class__.__name__ =="Data2D": |
---|
1164 | if self.model != None: |
---|
1165 | self.smear_description_2d.Show(True) |
---|
1166 | self.smear_description_none.Hide() |
---|
1167 | self.pinhole_smearer.Disable() |
---|
1168 | self.slit_smearer.Disable() |
---|
1169 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
1170 | |
---|
1171 | |
---|
1172 | self.formfactorbox.Enable() |
---|
1173 | self.structurebox.Enable() |
---|
1174 | data_name = self.data.name |
---|
1175 | #set maximum range for x in linear scale |
---|
1176 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
1177 | # Minimum value of data |
---|
1178 | data_min = min(self.data.x) |
---|
1179 | # Maximum value of data |
---|
1180 | data_max = max(self.data.x) |
---|
1181 | #number of total data points |
---|
1182 | self.Npts_total.SetValue(str(len(self.data.x))) |
---|
1183 | #default:number of data points selected to fit |
---|
1184 | self.Npts_fit.SetValue(str(len(self.data.x))) |
---|
1185 | self.btEditMask.Disable() |
---|
1186 | self.EditMask_title.Disable() |
---|
1187 | else: |
---|
1188 | ## Minimum value of data |
---|
1189 | data_min = 0 |
---|
1190 | x = max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
1191 | y = max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
1192 | ## Maximum value of data |
---|
1193 | data_max = math.sqrt(x*x + y*y) |
---|
1194 | #number of total data points |
---|
1195 | self.Npts_total.SetValue(str(len(self.data.data))) |
---|
1196 | #default:number of data points selected to fit |
---|
1197 | self.Npts_fit.SetValue(str(len(self.data.data))) |
---|
1198 | self.btEditMask.Enable() |
---|
1199 | self.EditMask_title.Enable() |
---|
1200 | |
---|
1201 | |
---|
1202 | self.dataSource.SetValue(data_name) |
---|
1203 | self.qmin_x = data_min |
---|
1204 | self.qmax_x = data_max |
---|
1205 | self.minimum_q.SetValue(str(data_min)) |
---|
1206 | self.maximum_q.SetValue(str(data_max)) |
---|
1207 | self.qmin.SetValue(str(data_min)) |
---|
1208 | self.qmax.SetValue(str(data_max)) |
---|
1209 | self.qmin.SetBackgroundColour("white") |
---|
1210 | self.qmax.SetBackgroundColour("white") |
---|
1211 | self.state.data = data |
---|
1212 | self.state.qmin = self.qmin_x |
---|
1213 | self.state.qmax = self.qmax_x |
---|
1214 | |
---|
1215 | |
---|
1216 | def reset_page(self, state,first=False): |
---|
1217 | """ |
---|
1218 | reset the state |
---|
1219 | """ |
---|
1220 | self.reset_page_helper(state) |
---|
1221 | import sans.guiframe.gui_manager |
---|
1222 | evt = ModelEventbox(model=state.model) |
---|
1223 | wx.PostEvent(self.event_owner, evt) |
---|
1224 | |
---|
1225 | if self.engine_type != None: |
---|
1226 | self.manager._on_change_engine(engine=self.engine_type) |
---|
1227 | |
---|
1228 | self.select_param(event = None) |
---|
1229 | #Save state_fit |
---|
1230 | self.save_current_state_fit() |
---|
1231 | self._lay_out() |
---|
1232 | self.Refresh() |
---|
1233 | |
---|
1234 | def get_range(self): |
---|
1235 | """ |
---|
1236 | return the fitting range |
---|
1237 | """ |
---|
1238 | return float(self.qmin_x) , float(self.qmax_x) |
---|
1239 | |
---|
1240 | def get_npts2fit(self): |
---|
1241 | """ |
---|
1242 | return numbers of data points within qrange |
---|
1243 | Note: This is for Park where chi2 is not normalized by Npts of fit |
---|
1244 | """ |
---|
1245 | npts2fit = 0 |
---|
1246 | qmin,qmax = self.get_range() |
---|
1247 | if self.data.__class__.__name__ =="Data2D": |
---|
1248 | radius= numpy.sqrt( self.data.qx_data*self.data.qx_data + self.data.qy_data*self.data.qy_data ) |
---|
1249 | index_data = (self.qmin_x <= radius)&(radius<= self.qmax_x) |
---|
1250 | index_data= (index_data)&(self.data.mask) |
---|
1251 | index_data = (index_data)&(numpy.isfinite(self.data.data)) |
---|
1252 | npts2fit = len(self.data.data[index_data]) |
---|
1253 | else: |
---|
1254 | for qx in self.data.x: |
---|
1255 | if qx >= qmin and qx <= qmax: |
---|
1256 | npts2fit += 1 |
---|
1257 | return npts2fit |
---|
1258 | |
---|
1259 | def set_npts2fit(self): |
---|
1260 | """ |
---|
1261 | setValue Npts for fitting |
---|
1262 | """ |
---|
1263 | self.Npts_fit.SetValue(str(self.get_npts2fit())) |
---|
1264 | |
---|
1265 | |
---|
1266 | def get_chi2(self): |
---|
1267 | """ |
---|
1268 | return the current chi2 |
---|
1269 | """ |
---|
1270 | return self.tcChi.GetLabel() |
---|
1271 | |
---|
1272 | def get_param_list(self): |
---|
1273 | """ |
---|
1274 | @return self.param_toFit: list containing references to TextCtrl |
---|
1275 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
1276 | @raise: if return an empty list of parameter fit will nnote work |
---|
1277 | properly so raise ValueError,"missing parameter to fit" |
---|
1278 | """ |
---|
1279 | if self.param_toFit !=[]: |
---|
1280 | return self.param_toFit |
---|
1281 | else: |
---|
1282 | raise ValueError,"missing parameter to fit" |
---|
1283 | |
---|
1284 | def onsetValues(self,chisqr,p_name, out,cov): |
---|
1285 | """ |
---|
1286 | Build the panel from the fit result |
---|
1287 | @param chisqr:Value of the goodness of fit metric |
---|
1288 | @p_name: the name of parameters |
---|
1289 | @param out:list of parameter with the best value found during fitting |
---|
1290 | @param cov:Covariance matrix |
---|
1291 | |
---|
1292 | """ |
---|
1293 | if out == None or not numpy.isfinite(chisqr): |
---|
1294 | raise ValueError,"Fit error occured..." |
---|
1295 | |
---|
1296 | is_modified = False |
---|
1297 | has_error = False |
---|
1298 | |
---|
1299 | #Hide textctrl boxes of errors. |
---|
1300 | self._clear_Err_on_Fit() |
---|
1301 | |
---|
1302 | #Check if chi2 is finite |
---|
1303 | if chisqr != None or numpy.isfinite(chisqr): |
---|
1304 | #format chi2 |
---|
1305 | if self.engine_type == "park": |
---|
1306 | npt_fit = float(self.get_npts2fit()) |
---|
1307 | if npt_fit > 0: |
---|
1308 | chisqr =chisqr/npt_fit |
---|
1309 | chi2 = format_number(chisqr) |
---|
1310 | self.tcChi.SetLabel(chi2) |
---|
1311 | self.tcChi.Refresh() |
---|
1312 | else: |
---|
1313 | self.tcChi.SetLabel("-") |
---|
1314 | |
---|
1315 | #Hide error title |
---|
1316 | if self.text2_3.IsShown(): |
---|
1317 | self.text2_3.Hide() |
---|
1318 | |
---|
1319 | try: |
---|
1320 | n = self.disp_box.GetCurrentSelection() |
---|
1321 | dispersity= self.disp_box.GetClientData(n) |
---|
1322 | if dispersity !=None and self.enable_disp.GetValue(): |
---|
1323 | name= dispersity.__name__ |
---|
1324 | if name == "GaussianDispersion": |
---|
1325 | if hasattr(self,"text_disp_1" ): |
---|
1326 | if self.text_disp_1 !=None: |
---|
1327 | self.text_disp_1.Hide() |
---|
1328 | except: |
---|
1329 | dispersty = None |
---|
1330 | pass |
---|
1331 | #set the panel when fit result are float not list |
---|
1332 | if out.__class__== numpy.float64: |
---|
1333 | self.param_toFit[0][2].SetValue(format_number(out)) |
---|
1334 | |
---|
1335 | if self.param_toFit[0][4].IsShown: |
---|
1336 | self.param_toFit[0][4].Hide() |
---|
1337 | if cov !=None : |
---|
1338 | self.text2_3.Show(True) |
---|
1339 | try: |
---|
1340 | if dispersity !=None: |
---|
1341 | name= dispersity.__name__ |
---|
1342 | if name == "GaussianDispersion" and self.enable_disp.GetValue(): |
---|
1343 | if hasattr(self,"text_disp_1" ): |
---|
1344 | if self.text_disp_1 !=None: |
---|
1345 | self.text_disp_1.Show(True) |
---|
1346 | except: |
---|
1347 | pass |
---|
1348 | |
---|
1349 | if cov[0]==None or not numpy.isfinite(cov[0]): |
---|
1350 | if self.param_toFit[0][3].IsShown: |
---|
1351 | self.param_toFit[0][3].Hide() |
---|
1352 | else: |
---|
1353 | self.param_toFit[0][3].Show(True) |
---|
1354 | self.param_toFit[0][4].Show(True) |
---|
1355 | self.param_toFit[0][4].SetValue(format_number(cov[0])) |
---|
1356 | has_error = True |
---|
1357 | else: |
---|
1358 | |
---|
1359 | i = 0 |
---|
1360 | #Set the panel when fit result are list |
---|
1361 | for item in self.param_toFit: |
---|
1362 | if len(item)>5 and item != None: |
---|
1363 | ## reset error value to initial state |
---|
1364 | item[3].Hide() |
---|
1365 | item[4].Hide() |
---|
1366 | |
---|
1367 | for ind in range(len(out)): |
---|
1368 | |
---|
1369 | if item[1] == p_name[ind]: |
---|
1370 | break |
---|
1371 | if len(out)<=len(self.param_toFit) and out[ind] !=None: |
---|
1372 | val_out = format_number(out[ind]) |
---|
1373 | item[2].SetValue(val_out) |
---|
1374 | |
---|
1375 | if(cov !=None): |
---|
1376 | |
---|
1377 | try: |
---|
1378 | if dispersity !=None: |
---|
1379 | name= dispersity.__name__ |
---|
1380 | if name == "GaussianDispersion" and self.enable_disp.GetValue(): |
---|
1381 | if hasattr(self,"text_disp_1" ): |
---|
1382 | if self.text_disp_1!=None: |
---|
1383 | if not self.text_disp_1.IsShown(): |
---|
1384 | self.text_disp_1.Show(True) |
---|
1385 | except: |
---|
1386 | pass |
---|
1387 | |
---|
1388 | if cov[ind]!=None : |
---|
1389 | if numpy.isfinite(float(cov[ind])): |
---|
1390 | val_err = format_number(cov[ind]) |
---|
1391 | item[3].Show(True) |
---|
1392 | item[4].Show(True) |
---|
1393 | item[4].SetValue(val_err) |
---|
1394 | |
---|
1395 | has_error = True |
---|
1396 | i += 1 |
---|
1397 | #Show error title when any errors displayed |
---|
1398 | if has_error: |
---|
1399 | if not self.text2_3.IsShown(): |
---|
1400 | self.text2_3.Show(True) |
---|
1401 | |
---|
1402 | ## save current state |
---|
1403 | self.save_current_state() |
---|
1404 | #plot model |
---|
1405 | self._draw_model() |
---|
1406 | self._lay_out() |
---|
1407 | #PostStatusEvent |
---|
1408 | msg = "Fit completed! " |
---|
1409 | wx.PostEvent(self.manager.parent, StatusEvent(status=msg)) |
---|
1410 | |
---|
1411 | def onPinholeSmear(self, event): |
---|
1412 | """ |
---|
1413 | Create a custom pinhole smear object that will change the way residuals |
---|
1414 | are compute when fitting |
---|
1415 | """ |
---|
1416 | |
---|
1417 | if self.check_invalid_panel(): |
---|
1418 | return |
---|
1419 | if self.model ==None: |
---|
1420 | msg="Need model and data to smear plot" |
---|
1421 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
1422 | "Smear: %s"%msg)) |
---|
1423 | return |
---|
1424 | |
---|
1425 | # msg default |
---|
1426 | msg = None |
---|
1427 | if event != None: |
---|
1428 | tcrtl= event.GetEventObject() |
---|
1429 | # event case of radio button |
---|
1430 | if tcrtl.GetValue()== True: |
---|
1431 | self.dx_min = 0.0 |
---|
1432 | self.dx_max = 0.0 |
---|
1433 | is_new_pinhole = True |
---|
1434 | else: |
---|
1435 | is_new_pinhole = self._is_changed_pinhole() |
---|
1436 | else: |
---|
1437 | is_new_pinhole = self._is_changed_pinhole() |
---|
1438 | # if any value is changed |
---|
1439 | if is_new_pinhole: |
---|
1440 | msg = self._set_pinhole_smear() |
---|
1441 | # hide all silt sizer |
---|
1442 | self._hide_all_smear_info() |
---|
1443 | |
---|
1444 | ##Calculate chi2 |
---|
1445 | self.compute_chisqr(smearer= self.current_smearer) |
---|
1446 | # show relevant slit sizers |
---|
1447 | self._show_smear_sizer() |
---|
1448 | self.sizer_set_smearer.Layout() |
---|
1449 | self.Layout() |
---|
1450 | |
---|
1451 | if event != None: |
---|
1452 | event.Skip() |
---|
1453 | #self._undo.Enable(True) |
---|
1454 | self.save_current_state() |
---|
1455 | event = PageInfoEvent(page = self) |
---|
1456 | wx.PostEvent(self.parent, event) |
---|
1457 | |
---|
1458 | def _is_changed_pinhole(self): |
---|
1459 | """ |
---|
1460 | check if any of pinhole smear is changed |
---|
1461 | return: True or False |
---|
1462 | """ |
---|
1463 | # get the values |
---|
1464 | pin_min = self.smear_pinhole_min.GetValue() |
---|
1465 | pin_max = self.smear_pinhole_max.GetValue() |
---|
1466 | |
---|
1467 | # Check changes in slit width |
---|
1468 | try: |
---|
1469 | dx_min = float(pin_min) |
---|
1470 | except: |
---|
1471 | return True |
---|
1472 | if self.dx_min != dx_min: |
---|
1473 | return True |
---|
1474 | |
---|
1475 | # Check changes in slit heigth |
---|
1476 | try: |
---|
1477 | dx_max = float(pin_max) |
---|
1478 | except: |
---|
1479 | return True |
---|
1480 | if self.dx_max != dx_max: |
---|
1481 | return True |
---|
1482 | return False |
---|
1483 | |
---|
1484 | def _set_pinhole_smear(self): |
---|
1485 | """ |
---|
1486 | Set custom pinhole smear |
---|
1487 | return: msg |
---|
1488 | """ |
---|
1489 | # copy data |
---|
1490 | data = copy.deepcopy(self.data) |
---|
1491 | data_len = len(data.x) |
---|
1492 | data.dx = None |
---|
1493 | data.dxl = None |
---|
1494 | data.dxw = None |
---|
1495 | msg = None |
---|
1496 | |
---|
1497 | # make sure once more if it is smearer |
---|
1498 | data = copy.deepcopy(self.data) |
---|
1499 | len_data = len(data.x) |
---|
1500 | data.dx = numpy.zeros(len_data) |
---|
1501 | get_pin_min = self.smear_pinhole_min |
---|
1502 | get_pin_max = self.smear_pinhole_max |
---|
1503 | |
---|
1504 | if self._validate_qrange(get_pin_min, get_pin_max ): |
---|
1505 | if len_data < 2: len_data = 2 |
---|
1506 | self.dx_min = float(get_pin_min.GetValue()) |
---|
1507 | self.dx_max = float(get_pin_max.GetValue()) |
---|
1508 | if self.dx_min != None and self.dx_max != None: |
---|
1509 | if self.dx_min == self.dx_max: |
---|
1510 | data.dx[data.dx==0] = self.dx_min |
---|
1511 | else: |
---|
1512 | step = math.fabs(self.dx_max - self.dx_min)/(len_data-1) |
---|
1513 | data.dx = numpy.arange(self.dx_min,self.dx_max+step/1.1,step) |
---|
1514 | elif self.dx_min != None: |
---|
1515 | data.dx[data.dx==0] = self.dx_min |
---|
1516 | elif self.dx_max != None: |
---|
1517 | data.dx[data.dx==0] = self.dx_max |
---|
1518 | self.current_smearer = smear_selection(data) |
---|
1519 | |
---|
1520 | else: |
---|
1521 | get_pin_min.SetBackgroundColour("pink") |
---|
1522 | get_pin_max.SetBackgroundColour("pink") |
---|
1523 | msg= "Model Error:wrong value entered!!!" |
---|
1524 | wx.PostEvent(self.manager.parent, StatusEvent(status = msg )) |
---|
1525 | ## set smearing value whether or not the data contain the smearing info |
---|
1526 | self.manager.set_smearer(smearer=self.current_smearer, qmin= float(self.qmin_x),qmax= float(self.qmax_x)) |
---|
1527 | |
---|
1528 | return msg |
---|
1529 | |
---|
1530 | def update_pinhole_smear(self): |
---|
1531 | """ |
---|
1532 | called by kill_focus on pinhole TextCntrl |
---|
1533 | to update the changes |
---|
1534 | return: msg: False when wrong value was entered |
---|
1535 | """ |
---|
1536 | # msg default |
---|
1537 | msg = None |
---|
1538 | # check if any value is changed |
---|
1539 | if self._is_changed_pinhole(): |
---|
1540 | msg = self._set_pinhole_smear() |
---|
1541 | #self._undo.Enable(True) |
---|
1542 | self.save_current_state() |
---|
1543 | |
---|
1544 | if msg != None: |
---|
1545 | return False |
---|
1546 | else: |
---|
1547 | return True |
---|
1548 | |
---|
1549 | def onSlitSmear(self, event): |
---|
1550 | """ |
---|
1551 | Create a custom slit smear object that will change the way residuals |
---|
1552 | are compute when fitting |
---|
1553 | """ |
---|
1554 | |
---|
1555 | |
---|
1556 | if self.check_invalid_panel(): |
---|
1557 | return |
---|
1558 | if self.model ==None: |
---|
1559 | msg="Need model and data to smear plot" |
---|
1560 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
1561 | "Smear: %s"%msg)) |
---|
1562 | return |
---|
1563 | # msg default |
---|
1564 | msg = None |
---|
1565 | # for event given |
---|
1566 | if event != None: |
---|
1567 | tcrtl= event.GetEventObject() |
---|
1568 | # event case of radio button |
---|
1569 | if tcrtl.GetValue(): |
---|
1570 | self.dxl = 0.0 |
---|
1571 | self.dxw = 0.0 |
---|
1572 | is_new_slit = True |
---|
1573 | else: |
---|
1574 | is_new_slit = self._is_changed_slit() |
---|
1575 | else: |
---|
1576 | is_new_slit = self._is_changed_slit() |
---|
1577 | |
---|
1578 | # if any value is changed |
---|
1579 | if is_new_slit: |
---|
1580 | msg = self._set_slit_smear() |
---|
1581 | # hide all silt sizer |
---|
1582 | self._hide_all_smear_info() |
---|
1583 | ##Calculate chi2 |
---|
1584 | self.compute_chisqr(smearer= self.current_smearer) |
---|
1585 | # show relevant slit sizers |
---|
1586 | self._show_smear_sizer() |
---|
1587 | self.sizer_set_smearer.Layout() |
---|
1588 | self.Layout() |
---|
1589 | |
---|
1590 | if event != None: |
---|
1591 | event.Skip() |
---|
1592 | #self._undo.Enable(True) |
---|
1593 | self.save_current_state() |
---|
1594 | event = PageInfoEvent(page = self) |
---|
1595 | wx.PostEvent(self.parent, event) |
---|
1596 | if msg != None: |
---|
1597 | wx.PostEvent(self.manager.parent, StatusEvent(status = msg)) |
---|
1598 | |
---|
1599 | |
---|
1600 | def _is_changed_slit(self): |
---|
1601 | """ |
---|
1602 | check if any of slit lengths is changed |
---|
1603 | return: True or False |
---|
1604 | """ |
---|
1605 | # get the values |
---|
1606 | width = self.smear_slit_width.GetValue() |
---|
1607 | height = self.smear_slit_height.GetValue() |
---|
1608 | |
---|
1609 | # check and change the box bg color if it was pink but it should be white now |
---|
1610 | # because this is the case that _set_slit_smear() will not handle |
---|
1611 | if height.lstrip().rstrip()=="": |
---|
1612 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
1613 | if width.lstrip().rstrip()=="": |
---|
1614 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
1615 | |
---|
1616 | # Check changes in slit width |
---|
1617 | if width == "": |
---|
1618 | dxw = 0.0 |
---|
1619 | else: |
---|
1620 | try: |
---|
1621 | dxw = float(width) |
---|
1622 | except: |
---|
1623 | return True |
---|
1624 | if self.dxw != dxw: |
---|
1625 | return True |
---|
1626 | |
---|
1627 | # Check changes in slit heigth |
---|
1628 | if height == "": |
---|
1629 | dxl = 0.0 |
---|
1630 | else: |
---|
1631 | try: |
---|
1632 | dxl = float(height) |
---|
1633 | except: |
---|
1634 | return True |
---|
1635 | if self.dxl != dxl: |
---|
1636 | return True |
---|
1637 | return False |
---|
1638 | |
---|
1639 | def _set_slit_smear(self): |
---|
1640 | """ |
---|
1641 | Set custom slit smear |
---|
1642 | return: msg |
---|
1643 | """ |
---|
1644 | temp_smearer = None |
---|
1645 | # make sure once more if it is smearer |
---|
1646 | data = copy.deepcopy(self.data) |
---|
1647 | data_len = len(data.x) |
---|
1648 | data.dx = None |
---|
1649 | data.dxl = None |
---|
1650 | data.dxw = None |
---|
1651 | msg = None |
---|
1652 | |
---|
1653 | try: |
---|
1654 | self.dxl = float(self.smear_slit_height.GetValue()) |
---|
1655 | data.dxl = self.dxl* numpy.ones(data_len) |
---|
1656 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
1657 | except: |
---|
1658 | data.dxl = numpy.zeros(data_len) |
---|
1659 | if self.smear_slit_height.GetValue().lstrip().rstrip()!="": |
---|
1660 | self.smear_slit_height.SetBackgroundColour("pink") |
---|
1661 | msg = "Wrong value entered... " |
---|
1662 | else: |
---|
1663 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
1664 | try: |
---|
1665 | self.dxw = float(self.smear_slit_width.GetValue()) |
---|
1666 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
1667 | data.dxw = self.dxw* numpy.ones(data_len) |
---|
1668 | except: |
---|
1669 | data.dxw = numpy.zeros(data_len) |
---|
1670 | if self.smear_slit_width.GetValue().lstrip().rstrip()!="": |
---|
1671 | self.smear_slit_width.SetBackgroundColour("pink") |
---|
1672 | msg = "Wrong Fit value entered... " |
---|
1673 | else: |
---|
1674 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
1675 | |
---|
1676 | self.current_smearer = smear_selection(data) |
---|
1677 | #temp_smearer = self.current_smearer |
---|
1678 | ## set smearing value whether or not the data contain the smearing info |
---|
1679 | self.manager.set_smearer(smearer=self.current_smearer, qmin= float(self.qmin_x), qmax= float(self.qmax_x)) |
---|
1680 | |
---|
1681 | return msg |
---|
1682 | |
---|
1683 | |
---|
1684 | def update_slit_smear(self): |
---|
1685 | """ |
---|
1686 | called by kill_focus on pinhole TextCntrl |
---|
1687 | to update the changes |
---|
1688 | return: msg: False when wrong value was entered |
---|
1689 | """ |
---|
1690 | # msg default |
---|
1691 | msg = None |
---|
1692 | # check if any value is changed |
---|
1693 | if self._is_changed_slit(): |
---|
1694 | msg = self._set_slit_smear() |
---|
1695 | #self._undo.Enable(True) |
---|
1696 | self.save_current_state() |
---|
1697 | |
---|
1698 | if msg != None: |
---|
1699 | return False |
---|
1700 | else: |
---|
1701 | return True |
---|
1702 | |
---|
1703 | |
---|
1704 | def onSmear(self, event): |
---|
1705 | """ |
---|
1706 | Create a smear object that will change the way residuals |
---|
1707 | are compute when fitting |
---|
1708 | """ |
---|
1709 | if self.check_invalid_panel(): |
---|
1710 | return |
---|
1711 | if self.model ==None: |
---|
1712 | msg="Need model and data to smear plot" |
---|
1713 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
1714 | "Smear: %s"%msg)) |
---|
1715 | return |
---|
1716 | temp_smearer = None |
---|
1717 | self._get_smear_info() |
---|
1718 | |
---|
1719 | #renew smear sizer |
---|
1720 | if self.smear_type != None: |
---|
1721 | self.smear_description_smear_type.SetValue(str(self.smear_type)) |
---|
1722 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
1723 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
1724 | |
---|
1725 | self._hide_all_smear_info() |
---|
1726 | |
---|
1727 | data = copy.deepcopy(self.data) |
---|
1728 | # make sure once more if it is smearer |
---|
1729 | self.current_smearer = smear_selection(data) |
---|
1730 | |
---|
1731 | if self.enable_smearer.GetValue(): |
---|
1732 | temp_smearer= self.current_smearer |
---|
1733 | if hasattr(self.data,"dxl"): |
---|
1734 | |
---|
1735 | msg= ": Resolution smearing parameters" |
---|
1736 | if hasattr(self.data,"dxw"): |
---|
1737 | msg= ": Slit smearing parameters" |
---|
1738 | if self.smearer ==None: |
---|
1739 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
1740 | "Data contains no smearing information")) |
---|
1741 | else: |
---|
1742 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
1743 | "Data contains smearing information %s"%msg)) |
---|
1744 | self.smear_description_dqdata.Show(True) |
---|
1745 | self.smear_data_left.Show(True) |
---|
1746 | self.smear_data_right.Show(True) |
---|
1747 | elif self.disable_smearer.GetValue(): |
---|
1748 | self.smear_description_none.Show(True) |
---|
1749 | |
---|
1750 | self._show_smear_sizer() |
---|
1751 | self.sizer_set_smearer.Layout() |
---|
1752 | self.Layout() |
---|
1753 | ## set smearing value whether or not the data contain the smearing info |
---|
1754 | self.manager.set_smearer(smearer=temp_smearer, qmin= float(self.qmin_x), |
---|
1755 | qmax= float(self.qmax_x)) |
---|
1756 | ##Calculate chi2 |
---|
1757 | self.compute_chisqr(smearer= temp_smearer) |
---|
1758 | |
---|
1759 | self.state.enable_smearer= self.enable_smearer.GetValue() |
---|
1760 | self.state.disable_smearer=self.disable_smearer.GetValue() |
---|
1761 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
1762 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
1763 | |
---|
1764 | def complete_chisqr(self, output, elapsed=None): |
---|
1765 | """ |
---|
1766 | print result chisqr |
---|
1767 | """ |
---|
1768 | try: |
---|
1769 | if output ==None: |
---|
1770 | output= "-" |
---|
1771 | |
---|
1772 | self.tcChi.SetLabel(str(format_number(output))) |
---|
1773 | |
---|
1774 | self.state.tcChi =self.tcChi |
---|
1775 | |
---|
1776 | except: |
---|
1777 | pass |
---|
1778 | |
---|
1779 | |
---|
1780 | def compute_chisqr1D(self, smearer=None): |
---|
1781 | """ |
---|
1782 | Compute chisqr for 1D |
---|
1783 | """ |
---|
1784 | try: |
---|
1785 | self.qmin_x = float(self.qmin.GetValue()) |
---|
1786 | self.qmax_x = float(self.qmax.GetValue()) |
---|
1787 | ##return residuals within self.qmin_x and self.qmax_x |
---|
1788 | from gui_thread import CalcChisqr1D |
---|
1789 | ## If a thread is already started, stop it |
---|
1790 | if self.calc_Chisqr!= None and self.calc_Chisqr.isrunning(): |
---|
1791 | self.calc_Chisqr.stop() |
---|
1792 | |
---|
1793 | self.calc_Chisqr= CalcChisqr1D( data1d= self.data, |
---|
1794 | model= self.model, |
---|
1795 | smearer=smearer, |
---|
1796 | qmin=self.qmin_x, |
---|
1797 | qmax=self.qmax_x, |
---|
1798 | completefn = self.complete_chisqr, |
---|
1799 | updatefn = None) |
---|
1800 | |
---|
1801 | self.calc_Chisqr.queue() |
---|
1802 | |
---|
1803 | except: |
---|
1804 | raise ValueError," Could not compute Chisqr for %s Model 2D: "%self.model.name |
---|
1805 | |
---|
1806 | |
---|
1807 | |
---|
1808 | |
---|
1809 | |
---|
1810 | def compute_chisqr2D(self): |
---|
1811 | """ |
---|
1812 | compute chi square given a model and data 2D and set the value |
---|
1813 | to the tcChi txtcrl |
---|
1814 | """ |
---|
1815 | try: |
---|
1816 | self.qmin_x = float(self.qmin.GetValue()) |
---|
1817 | self.qmax_x = float(self.qmax.GetValue()) |
---|
1818 | |
---|
1819 | ##return residuals within self.qmin_x and self.qmax_x |
---|
1820 | from gui_thread import CalcChisqr2D |
---|
1821 | ## If a thread is already started, stop it |
---|
1822 | if self.calc_Chisqr!= None and self.calc_Chisqr.isrunning(): |
---|
1823 | self.calc_Chisqr.stop() |
---|
1824 | |
---|
1825 | self.calc_Chisqr= CalcChisqr2D( data2d= self.data, |
---|
1826 | model= self.model, |
---|
1827 | qmin= self.qmin_x, |
---|
1828 | qmax = self.qmax_x, |
---|
1829 | completefn = self.complete_chisqr, |
---|
1830 | updatefn = None) |
---|
1831 | |
---|
1832 | self.calc_Chisqr.queue() |
---|
1833 | |
---|
1834 | except: |
---|
1835 | raise |
---|
1836 | |
---|
1837 | |
---|
1838 | def compute_chisqr(self , smearer=None): |
---|
1839 | """ |
---|
1840 | compute chi square given a model and data 1D and set the value |
---|
1841 | to the tcChi txtcrl |
---|
1842 | """ |
---|
1843 | flag = self._validate_qrange( self.qmin, self.qmax) |
---|
1844 | if flag== True: |
---|
1845 | try: |
---|
1846 | if hasattr(self.data,"data"): |
---|
1847 | self.compute_chisqr2D() |
---|
1848 | return |
---|
1849 | else: |
---|
1850 | self.compute_chisqr1D(smearer=smearer) |
---|
1851 | return |
---|
1852 | except: |
---|
1853 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
1854 | "Chisqr Error: %s"% sys.exc_value)) |
---|
1855 | return |
---|
1856 | |
---|
1857 | |
---|
1858 | def select_all_param(self,event): |
---|
1859 | """ |
---|
1860 | set to true or false all checkBox given the main checkbox value cb1 |
---|
1861 | """ |
---|
1862 | |
---|
1863 | self.param_toFit=[] |
---|
1864 | if self.parameters !=[]: |
---|
1865 | if self.cb1.GetValue(): |
---|
1866 | for item in self.parameters: |
---|
1867 | ## for data2D select all to fit |
---|
1868 | if self.data.__class__.__name__=="Data2D": |
---|
1869 | item[0].SetValue(True) |
---|
1870 | self.param_toFit.append(item ) |
---|
1871 | else: |
---|
1872 | ## for 1D all parameters except orientation |
---|
1873 | if not item in self.orientation_params: |
---|
1874 | item[0].SetValue(True) |
---|
1875 | self.param_toFit.append(item ) |
---|
1876 | #if len(self.fittable_param)>0: |
---|
1877 | for item in self.fittable_param: |
---|
1878 | if self.data.__class__.__name__=="Data2D": |
---|
1879 | item[0].SetValue(True) |
---|
1880 | self.param_toFit.append(item ) |
---|
1881 | else: |
---|
1882 | ## for 1D all parameters except orientation |
---|
1883 | if not item in self.orientation_params_disp: |
---|
1884 | item[0].SetValue(True) |
---|
1885 | self.param_toFit.append(item ) |
---|
1886 | else: |
---|
1887 | for item in self.parameters: |
---|
1888 | item[0].SetValue(False) |
---|
1889 | for item in self.fittable_param: |
---|
1890 | item[0].SetValue(False) |
---|
1891 | self.param_toFit=[] |
---|
1892 | |
---|
1893 | self.save_current_state_fit() |
---|
1894 | |
---|
1895 | if event !=None: |
---|
1896 | #self._undo.Enable(True) |
---|
1897 | ## post state to fit panel |
---|
1898 | event = PageInfoEvent(page = self) |
---|
1899 | wx.PostEvent(self.parent, event) |
---|
1900 | |
---|
1901 | |
---|
1902 | |
---|
1903 | def select_param(self,event): |
---|
1904 | """ |
---|
1905 | Select TextCtrl checked for fitting purpose and stores them |
---|
1906 | in self.param_toFit=[] list |
---|
1907 | """ |
---|
1908 | self.param_toFit=[] |
---|
1909 | for item in self.parameters: |
---|
1910 | #Skip t ifhe angle parameters if 1D data |
---|
1911 | if self.data.__class__.__name__ !="Data2D": |
---|
1912 | if item in self.orientation_params: |
---|
1913 | continue |
---|
1914 | #Select parameters to fit for list of primary parameters |
---|
1915 | if item[0].GetValue(): |
---|
1916 | if not (item in self.param_toFit): |
---|
1917 | self.param_toFit.append(item ) |
---|
1918 | else: |
---|
1919 | #remove parameters from the fitting list |
---|
1920 | if item in self.param_toFit: |
---|
1921 | self.param_toFit.remove(item) |
---|
1922 | |
---|
1923 | #Select parameters to fit for list of fittable parameters with dispersion |
---|
1924 | for item in self.fittable_param: |
---|
1925 | #Skip t ifhe angle parameters if 1D data |
---|
1926 | if self.data.__class__.__name__ !="Data2D": |
---|
1927 | if item in self.orientation_params: |
---|
1928 | continue |
---|
1929 | if item[0].GetValue(): |
---|
1930 | if not (item in self.param_toFit): |
---|
1931 | self.param_toFit.append(item) |
---|
1932 | else: |
---|
1933 | #remove parameters from the fitting list |
---|
1934 | if item in self.param_toFit: |
---|
1935 | self.param_toFit.remove(item) |
---|
1936 | |
---|
1937 | #Calculate num. of angle parameters |
---|
1938 | if self.data.__class__.__name__ =="Data2D": |
---|
1939 | len_orient_para = 0 |
---|
1940 | else: |
---|
1941 | len_orient_para = len(self.orientation_params) #assume even len |
---|
1942 | #Total num. of angle parameters |
---|
1943 | if len(self.fittable_param) > 0: |
---|
1944 | len_orient_para *= 2 |
---|
1945 | #Set the value of checkbox that selected every checkbox or not |
---|
1946 | if len(self.parameters)+len(self.fittable_param)-len_orient_para ==len(self.param_toFit): |
---|
1947 | self.cb1.SetValue(True) |
---|
1948 | else: |
---|
1949 | self.cb1.SetValue(False) |
---|
1950 | self.save_current_state_fit() |
---|
1951 | if event !=None: |
---|
1952 | #self._undo.Enable(True) |
---|
1953 | ## post state to fit panel |
---|
1954 | event = PageInfoEvent(page = self) |
---|
1955 | wx.PostEvent(self.parent, event) |
---|
1956 | |
---|
1957 | |
---|
1958 | |
---|
1959 | def set_model_param_sizer(self, model): |
---|
1960 | """ |
---|
1961 | Build the panel from the model content |
---|
1962 | @param model: the model selected in combo box for fitting purpose |
---|
1963 | """ |
---|
1964 | self.sizer3.Clear(True) |
---|
1965 | self.parameters = [] |
---|
1966 | self.param_toFit=[] |
---|
1967 | self.fittable_param=[] |
---|
1968 | self.fixed_param=[] |
---|
1969 | self.orientation_params=[] |
---|
1970 | self.orientation_params_disp=[] |
---|
1971 | |
---|
1972 | if model ==None: |
---|
1973 | self.sizer3.Layout() |
---|
1974 | self.SetScrollbars(20,20,25,65) |
---|
1975 | return |
---|
1976 | ## the panel is drawn using the current value of the fit engine |
---|
1977 | if self.engine_type==None and self.manager !=None: |
---|
1978 | self.engine_type= self.manager._return_engine_type() |
---|
1979 | |
---|
1980 | |
---|
1981 | box_description= wx.StaticBox(self, -1,str("Model Parameters")) |
---|
1982 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
1983 | sizer = wx.GridBagSizer(5,5) |
---|
1984 | ## save the current model |
---|
1985 | self.model = model |
---|
1986 | |
---|
1987 | keys = self.model.getParamList() |
---|
1988 | #list of dispersion paramaters |
---|
1989 | self.disp_list=self.model.getDispParamList() |
---|
1990 | |
---|
1991 | keys.sort() |
---|
1992 | |
---|
1993 | iy = 0 |
---|
1994 | ix = 0 |
---|
1995 | select_text = "Select All" |
---|
1996 | self.cb1 = wx.CheckBox(self, -1,str(select_text), (10, 10)) |
---|
1997 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
1998 | self.cb1.SetToolTipString("To check/uncheck all the boxes below.") |
---|
1999 | #self.cb1.SetValue(True) |
---|
2000 | |
---|
2001 | sizer.Add(self.cb1,(iy, ix),(1,1),\ |
---|
2002 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2003 | ix +=1 |
---|
2004 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
2005 | sizer.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
2006 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2007 | ix +=2 |
---|
2008 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
2009 | sizer.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
2010 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2011 | self.text2_3.Hide() |
---|
2012 | ix +=1 |
---|
2013 | self.text2_min = wx.StaticText(self, -1, 'Min') |
---|
2014 | sizer.Add(self.text2_min,(iy, ix),(1,1),\ |
---|
2015 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2016 | self.text2_min.Hide() |
---|
2017 | ix +=1 |
---|
2018 | self.text2_max = wx.StaticText(self, -1, 'Max') |
---|
2019 | sizer.Add(self.text2_max,(iy, ix),(1,1),\ |
---|
2020 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2021 | self.text2_max.Hide() |
---|
2022 | ix += 1 |
---|
2023 | self.text2_4 = wx.StaticText(self, -1, '[Units]') |
---|
2024 | sizer.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
2025 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2026 | self.text2_4.Hide() |
---|
2027 | if self.engine_type=="park": |
---|
2028 | self.text2_max.Show(True) |
---|
2029 | self.text2_min.Show(True) |
---|
2030 | |
---|
2031 | for item in keys: |
---|
2032 | if not item in self.disp_list and not item in self.model.orientation_params: |
---|
2033 | |
---|
2034 | ##prepare a spot to store errors |
---|
2035 | if not self.model.details.has_key(item): |
---|
2036 | self.model.details [item] = ["",None,None] |
---|
2037 | |
---|
2038 | iy += 1 |
---|
2039 | ix = 0 |
---|
2040 | ## add parameters name with checkbox for selecting to fit |
---|
2041 | cb = wx.CheckBox(self, -1, item ) |
---|
2042 | cb.SetToolTipString(" Check/uncheck to fit/fix the parameter.") |
---|
2043 | #cb.SetValue(True) |
---|
2044 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
2045 | |
---|
2046 | sizer.Add( cb,( iy, ix),(1,1), |
---|
2047 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2048 | |
---|
2049 | ## add parameter value |
---|
2050 | ix += 1 |
---|
2051 | value= self.model.getParam(item) |
---|
2052 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
2053 | style=wx.TE_PROCESS_ENTER) |
---|
2054 | |
---|
2055 | ctl1.SetValue(format_number(value)) |
---|
2056 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
2057 | ## text to show error sign |
---|
2058 | ix += 1 |
---|
2059 | text2=wx.StaticText(self, -1, '+/-') |
---|
2060 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
2061 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2062 | text2.Hide() |
---|
2063 | ix += 1 |
---|
2064 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
2065 | sizer.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2066 | ctl2.Hide() |
---|
2067 | |
---|
2068 | ix += 1 |
---|
2069 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
2070 | text_enter_callback = self._onparamRangeEnter) |
---|
2071 | |
---|
2072 | sizer.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2073 | ctl3.Hide() |
---|
2074 | |
---|
2075 | ix += 1 |
---|
2076 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
2077 | text_enter_callback = self._onparamRangeEnter) |
---|
2078 | sizer.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2079 | |
---|
2080 | ctl4.Hide() |
---|
2081 | |
---|
2082 | if self.engine_type=="park": |
---|
2083 | ctl3.Show(True) |
---|
2084 | ctl4.Show(True) |
---|
2085 | ix +=1 |
---|
2086 | # Units |
---|
2087 | if self.model.details.has_key(item): |
---|
2088 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
2089 | else: |
---|
2090 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
2091 | sizer.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2092 | |
---|
2093 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
2094 | self.parameters.append([cb,item, ctl1, |
---|
2095 | text2,ctl2, ctl3, ctl4,units]) |
---|
2096 | |
---|
2097 | iy+=1 |
---|
2098 | sizer.Add((10,10),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2099 | |
---|
2100 | # type can be either Guassian or Array |
---|
2101 | if len(self.model.dispersion.values())>0: |
---|
2102 | type= self.model.dispersion.values()[0]["type"] |
---|
2103 | else: |
---|
2104 | type = "Gaussian" |
---|
2105 | |
---|
2106 | iy += 1 |
---|
2107 | ix = 0 |
---|
2108 | #Add tile for orientational angle |
---|
2109 | for item in keys: |
---|
2110 | if item in self.model.orientation_params: |
---|
2111 | orient_angle = wx.StaticText(self, -1, '[For 2D only]:') |
---|
2112 | sizer.Add(orient_angle,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2113 | if not self.data.__class__.__name__ =="Data2D": |
---|
2114 | orient_angle.Hide() |
---|
2115 | else: |
---|
2116 | orient_angle.Show(True) |
---|
2117 | break |
---|
2118 | |
---|
2119 | #For Gaussian only |
---|
2120 | if type.lower() != "array": |
---|
2121 | for item in self.model.orientation_params: |
---|
2122 | if not item in self.disp_list: |
---|
2123 | ##prepare a spot to store min max |
---|
2124 | if not self.model.details.has_key(item): |
---|
2125 | self.model.details [item] = ["",None,None] |
---|
2126 | |
---|
2127 | iy += 1 |
---|
2128 | ix = 0 |
---|
2129 | ## add parameters name with checkbox for selecting to fit |
---|
2130 | cb = wx.CheckBox(self, -1, item ) |
---|
2131 | cb.SetValue(False) |
---|
2132 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
2133 | if self.data.__class__.__name__ =="Data2D": |
---|
2134 | cb.Show(True) |
---|
2135 | else: |
---|
2136 | cb.Hide() |
---|
2137 | sizer.Add( cb,( iy, ix),(1,1), |
---|
2138 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2139 | |
---|
2140 | ## add parameter value |
---|
2141 | ix += 1 |
---|
2142 | value= self.model.getParam(item) |
---|
2143 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
2144 | style=wx.TE_PROCESS_ENTER) |
---|
2145 | |
---|
2146 | ctl1.SetValue(format_number(value)) |
---|
2147 | if self.data.__class__.__name__ =="Data2D": |
---|
2148 | ctl1.Show(True) |
---|
2149 | else: |
---|
2150 | ctl1.Hide() |
---|
2151 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
2152 | ## text to show error sign |
---|
2153 | ix += 1 |
---|
2154 | text2=wx.StaticText(self, -1, '+/-') |
---|
2155 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
2156 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2157 | text2.Hide() |
---|
2158 | ix += 1 |
---|
2159 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
2160 | |
---|
2161 | sizer.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2162 | ctl2.Hide() |
---|
2163 | |
---|
2164 | |
---|
2165 | ix += 1 |
---|
2166 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
2167 | text_enter_callback = self._onparamRangeEnter) |
---|
2168 | |
---|
2169 | sizer.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2170 | ctl3.Hide() |
---|
2171 | |
---|
2172 | ix += 1 |
---|
2173 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
2174 | text_enter_callback = self._onparamRangeEnter) |
---|
2175 | sizer.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2176 | |
---|
2177 | ctl4.Hide() |
---|
2178 | |
---|
2179 | if self.engine_type =="park" and self.data.__class__.__name__ =="Data2D": |
---|
2180 | ctl3.Show(True) |
---|
2181 | ctl4.Show(True) |
---|
2182 | |
---|
2183 | ix +=1 |
---|
2184 | # Units |
---|
2185 | if self.model.details.has_key(item): |
---|
2186 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
2187 | else: |
---|
2188 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
2189 | if self.data.__class__.__name__ =="Data2D": |
---|
2190 | units.Show(True) |
---|
2191 | |
---|
2192 | else: |
---|
2193 | units.Hide() |
---|
2194 | |
---|
2195 | sizer.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2196 | |
---|
2197 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
2198 | self.parameters.append([cb,item, ctl1, |
---|
2199 | text2,ctl2, ctl3, ctl4,units]) |
---|
2200 | self.orientation_params.append([cb,item, ctl1, |
---|
2201 | text2,ctl2, ctl3, ctl4,units]) |
---|
2202 | |
---|
2203 | iy+=1 |
---|
2204 | |
---|
2205 | #Display units text on panel |
---|
2206 | for item in keys: |
---|
2207 | if self.model.details.has_key(item): |
---|
2208 | self.text2_4.Show() |
---|
2209 | #Fill the list of fittable parameters |
---|
2210 | self.select_all_param(event=None) |
---|
2211 | |
---|
2212 | self.save_current_state_fit() |
---|
2213 | boxsizer1.Add(sizer) |
---|
2214 | self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
2215 | self.sizer3.Layout() |
---|
2216 | self.Layout() |
---|
2217 | self.Refresh() |
---|
2218 | self.SetScrollbars(20,20,25,65) |
---|
2219 | |
---|
2220 | class BGTextCtrl(wx.TextCtrl): |
---|
2221 | """ |
---|
2222 | Text control used to display outputs. |
---|
2223 | No editing allowed. The background is |
---|
2224 | grayed out. User can't select text. |
---|
2225 | """ |
---|
2226 | def __init__(self, *args, **kwds): |
---|
2227 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
2228 | self.SetEditable(False) |
---|
2229 | self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) |
---|
2230 | |
---|
2231 | # Bind to mouse event to avoid text highlighting |
---|
2232 | # The event will be skipped once the call-back |
---|
2233 | # is called. |
---|
2234 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
2235 | |
---|
2236 | def _click(self, event): |
---|
2237 | """ |
---|
2238 | Prevent further handling of the mouse event |
---|
2239 | by not calling Skip(). |
---|
2240 | """ |
---|
2241 | pass |
---|
2242 | |
---|
2243 | class HelpWindow(wx.Frame): |
---|
2244 | def __init__(self, parent, id, title): |
---|
2245 | wx.Frame.__init__(self, parent, id, title, size=(570, 400)) |
---|
2246 | |
---|
2247 | from sans.models.CylinderModel import CylinderModel |
---|
2248 | model = CylinderModel() |
---|
2249 | |
---|
2250 | from danse.common.plottools.plottables import Data1D |
---|
2251 | data= Data1D(x=[1,2], y=[3,4], dy=[0.1, 0,1]) |
---|
2252 | |
---|
2253 | from fitpanel import PageInfo |
---|
2254 | myinfo = PageInfo(self, model, data=data ) |
---|
2255 | |
---|
2256 | ## add data |
---|
2257 | |
---|
2258 | from models import ModelList |
---|
2259 | mylist= ModelList() |
---|
2260 | |
---|
2261 | from sans.models.SphereModel import SphereModel |
---|
2262 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
2263 | from sans.models.DebyeModel import DebyeModel |
---|
2264 | from sans.models.LineModel import LineModel |
---|
2265 | name= "shapes" |
---|
2266 | list1= [SphereModel] |
---|
2267 | mylist.set_list( name, list1) |
---|
2268 | |
---|
2269 | name= "Shape-independent" |
---|
2270 | list1= [DebyeModel] |
---|
2271 | mylist.set_list( name, list1) |
---|
2272 | |
---|
2273 | name= "Structure Factors" |
---|
2274 | list1= [SquareWellStructure] |
---|
2275 | mylist.set_list( name, list1) |
---|
2276 | |
---|
2277 | name= "Added models" |
---|
2278 | list1= [LineModel] |
---|
2279 | mylist.set_list( name, list1) |
---|
2280 | |
---|
2281 | myinfo.model_list_box = mylist.get_list() |
---|
2282 | |
---|
2283 | self.page = FitPage(self, myinfo) |
---|
2284 | |
---|
2285 | self.Centre() |
---|
2286 | self.Show(True) |
---|
2287 | |
---|
2288 | if __name__=="__main__": |
---|
2289 | app = wx.App() |
---|
2290 | HelpWindow(None, -1, 'HelpWindow') |
---|
2291 | app.MainLoop() |
---|
2292 | |
---|