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