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 | if self.data is None: |
---|
934 | msg = "Please select a Data first..." |
---|
935 | wx.MessageBox(msg, 'Info') |
---|
936 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
937 | "Fit: %s" % msg)) |
---|
938 | return |
---|
939 | if self.model is None: |
---|
940 | msg = "Please select a Model first..." |
---|
941 | wx.MessageBox(msg, 'Info') |
---|
942 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
943 | "Fit: %s" % msg)) |
---|
944 | return |
---|
945 | |
---|
946 | flag = self._update_paramv_on_fit() |
---|
947 | |
---|
948 | if not flag: |
---|
949 | msg= "Fitting range or parameters are invalid" |
---|
950 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
951 | return |
---|
952 | |
---|
953 | if len(self.param_toFit) <= 0: |
---|
954 | msg= "Select at least one parameter to fit" |
---|
955 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
956 | return |
---|
957 | |
---|
958 | self.select_param(event =None) |
---|
959 | |
---|
960 | #Clear errors if exist from previous fitting |
---|
961 | #self._clear_Err_on_Fit() |
---|
962 | |
---|
963 | # Remove or do not allow fitting on the Q=0 point, especially |
---|
964 | # when y(q=0)=None at x[0]. |
---|
965 | self.qmin_x = float(self.qmin_tcrl.GetValue()) |
---|
966 | self.qmax_x = float( self.qmax.GetValue()) |
---|
967 | self._manager._reset_schedule_problem(id=self.id, value=0) |
---|
968 | self._manager.schedule_for_fit(id=id,value=1, fitproblem =None) |
---|
969 | self._manager.set_fit_range(id=self.id,qmin= self.qmin_x, |
---|
970 | qmax= self.qmax_x) |
---|
971 | |
---|
972 | #single fit |
---|
973 | self._manager.onFit() |
---|
974 | ## allow stopping the fit |
---|
975 | #if self.engine_type=="scipy": |
---|
976 | # self.btFit.SetLabel("Stop") |
---|
977 | # self.btFit.Unbind(event=wx.EVT_BUTTON, id= self.btFit.GetId()) |
---|
978 | # self.btFit.Bind(event= wx.EVT_BUTTON, |
---|
979 | # handler=self._StopFit, id=self.btFit.GetId()) |
---|
980 | #else: |
---|
981 | # self.btFit.SetLabel("Fit") |
---|
982 | # self.btFit.Bind(event= wx.EVT_BUTTON, |
---|
983 | # handler=self._onFit, id=self.btFit.GetId()) |
---|
984 | |
---|
985 | def _StopFit(self, event): |
---|
986 | """ |
---|
987 | Stop fit |
---|
988 | """ |
---|
989 | self.btFit.SetLabel("Fit") |
---|
990 | if self.engine_type=="scipy": |
---|
991 | self._manager.stop_fit() |
---|
992 | self.btFit.Unbind(event=wx.EVT_BUTTON, id=self.btFit.GetId()) |
---|
993 | self.btFit.Bind(event=wx.EVT_BUTTON, handler=self._onFit, |
---|
994 | id=self.btFit.GetId()) |
---|
995 | |
---|
996 | |
---|
997 | def _on_select_model(self, event=None): |
---|
998 | """ |
---|
999 | call back for model selection |
---|
1000 | """ |
---|
1001 | self._on_select_model_helper() |
---|
1002 | self.set_model_param_sizer(self.model) |
---|
1003 | |
---|
1004 | if self.model is None: |
---|
1005 | self._set_bookmark_flag(False) |
---|
1006 | self._set_save_flag(False) |
---|
1007 | self.enable_disp.SetValue(False) |
---|
1008 | self.disable_disp.SetValue(True) |
---|
1009 | try: |
---|
1010 | self.set_dispers_sizer() |
---|
1011 | except: |
---|
1012 | pass |
---|
1013 | self.btFit.SetFocus() |
---|
1014 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
1015 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
1016 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
1017 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
1018 | |
---|
1019 | self.state.structurecombobox = self.structurebox.GetCurrentSelection() |
---|
1020 | self.state.formfactorcombobox = self.formfactorbox.GetCurrentSelection() |
---|
1021 | |
---|
1022 | if self.model != None: |
---|
1023 | self._set_bookmark_flag(True) |
---|
1024 | self._set_save_flag(True) |
---|
1025 | # Reset smearer, model and data |
---|
1026 | self._set_smear(self.data) |
---|
1027 | try: |
---|
1028 | # update smearer sizer |
---|
1029 | self.onSmear(None) |
---|
1030 | temp_smear = None |
---|
1031 | if self.enable_smearer.GetValue(): |
---|
1032 | # Set the smearer environments |
---|
1033 | temp_smear = self.smearer |
---|
1034 | except: |
---|
1035 | ## error occured on chisqr computation |
---|
1036 | pass |
---|
1037 | ## event to post model to fit to fitting plugins |
---|
1038 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
1039 | |
---|
1040 | ## set smearing value whether or not |
---|
1041 | # the data contain the smearing info |
---|
1042 | evt = ModelEventbox(model=self.model, |
---|
1043 | smearer=temp_smear, |
---|
1044 | qmin=float(self.qmin_x), |
---|
1045 | id=self.id, |
---|
1046 | qmax=float(self.qmax_x)) |
---|
1047 | |
---|
1048 | self._manager._on_model_panel(evt=evt) |
---|
1049 | self.state.model = self.model.clone() |
---|
1050 | self.state.model.name = self.model.name |
---|
1051 | if event is not None: |
---|
1052 | self._draw_model() |
---|
1053 | if event != None: |
---|
1054 | |
---|
1055 | ## post state to fit panel |
---|
1056 | event = PageInfoEvent(page = self) |
---|
1057 | wx.PostEvent(self.parent, event) |
---|
1058 | # |
---|
1059 | |
---|
1060 | |
---|
1061 | def _onparamEnter(self,event): |
---|
1062 | """ |
---|
1063 | when enter value on panel redraw model according to changed |
---|
1064 | """ |
---|
1065 | if self.model ==None: |
---|
1066 | msg="Please select a Model first..." |
---|
1067 | wx.MessageBox(msg, 'Info') |
---|
1068 | #wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
1069 | # "Parameters: %s"%msg)) |
---|
1070 | return |
---|
1071 | |
---|
1072 | #default flag |
---|
1073 | flag = False |
---|
1074 | self.fitrange = True |
---|
1075 | #get event object |
---|
1076 | tcrtl = event.GetEventObject() |
---|
1077 | |
---|
1078 | wx.PostEvent(self._manager.parent, StatusEvent(status=" \ |
---|
1079 | updating ... ",type="update")) |
---|
1080 | #Clear msg if previously shown. |
---|
1081 | msg= "" |
---|
1082 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1083 | |
---|
1084 | if check_float(tcrtl): |
---|
1085 | flag = self._onparamEnter_helper() |
---|
1086 | self.set_npts2fit() |
---|
1087 | if self.fitrange: |
---|
1088 | temp_smearer = None |
---|
1089 | if not self.disable_smearer.GetValue(): |
---|
1090 | temp_smearer= self.current_smearer |
---|
1091 | ## set smearing value whether or not |
---|
1092 | # the data contain the smearing info |
---|
1093 | if self.slit_smearer.GetValue(): |
---|
1094 | flag1 = self.update_slit_smear() |
---|
1095 | flag = flag or flag1 |
---|
1096 | elif self.pinhole_smearer.GetValue(): |
---|
1097 | flag1 = self.update_pinhole_smear() |
---|
1098 | flag = flag or flag1 |
---|
1099 | elif self.data.__class__.__name__ !="Data2D": |
---|
1100 | self._manager.set_smearer(smearer=temp_smearer, |
---|
1101 | id=self.id, |
---|
1102 | qmin= float(self.qmin_x), |
---|
1103 | qmax= float(self.qmax_x), |
---|
1104 | draw=True) |
---|
1105 | if flag: |
---|
1106 | #self.compute_chisqr(smearer= temp_smearer) |
---|
1107 | |
---|
1108 | ## new state posted |
---|
1109 | if self.state_change: |
---|
1110 | #self._undo.Enable(True) |
---|
1111 | event = PageInfoEvent(page = self) |
---|
1112 | wx.PostEvent(self.parent, event) |
---|
1113 | self.state_change= False |
---|
1114 | else: |
---|
1115 | # invalid fit range: do nothing here: |
---|
1116 | # msg already displayed in validate |
---|
1117 | return |
---|
1118 | else: |
---|
1119 | self.save_current_state() |
---|
1120 | msg= "Cannot Plot :Must enter a number!!! " |
---|
1121 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1122 | |
---|
1123 | self.save_current_state() |
---|
1124 | return |
---|
1125 | |
---|
1126 | def _onparamRangeEnter(self, event): |
---|
1127 | """ |
---|
1128 | Check validity of value enter in the parameters range field |
---|
1129 | """ |
---|
1130 | #if self.check_invalid_panel(): |
---|
1131 | # return |
---|
1132 | tcrtl= event.GetEventObject() |
---|
1133 | #Clear msg if previously shown. |
---|
1134 | msg= "" |
---|
1135 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1136 | # Flag to register when a parameter has changed. |
---|
1137 | is_modified = False |
---|
1138 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
1139 | try: |
---|
1140 | value = float(tcrtl.GetValue()) |
---|
1141 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1142 | self._check_value_enter(self.fittable_param ,is_modified) |
---|
1143 | self._check_value_enter(self.parameters ,is_modified) |
---|
1144 | except: |
---|
1145 | tcrtl.SetBackgroundColour("pink") |
---|
1146 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1147 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1148 | return |
---|
1149 | else: |
---|
1150 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1151 | |
---|
1152 | #self._undo.Enable(True) |
---|
1153 | self.save_current_state() |
---|
1154 | event = PageInfoEvent(page = self) |
---|
1155 | wx.PostEvent(self.parent, event) |
---|
1156 | self.state_change= False |
---|
1157 | |
---|
1158 | |
---|
1159 | def _onQrangeEnter(self, event): |
---|
1160 | """ |
---|
1161 | Check validity of value enter in the Q range field |
---|
1162 | """ |
---|
1163 | #if self.check_invalid_panel(): |
---|
1164 | # return |
---|
1165 | tcrtl= event.GetEventObject() |
---|
1166 | #Clear msg if previously shown. |
---|
1167 | msg= "" |
---|
1168 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1169 | # Flag to register when a parameter has changed. |
---|
1170 | is_modified = False |
---|
1171 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
1172 | try: |
---|
1173 | value = float(tcrtl.GetValue()) |
---|
1174 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1175 | |
---|
1176 | # If qmin and qmax have been modified, update qmin and qmax |
---|
1177 | if self._validate_qrange( self.qmin_tcrl, self.qmax): |
---|
1178 | tempmin = float(self.qmin_tcrl.GetValue()) |
---|
1179 | if tempmin != self.qmin_x: |
---|
1180 | self.qmin_x = tempmin |
---|
1181 | tempmax = float(self.qmax.GetValue()) |
---|
1182 | if tempmax != self.qmax_x: |
---|
1183 | self.qmax_x = tempmax |
---|
1184 | else: |
---|
1185 | tcrtl.SetBackgroundColour("pink") |
---|
1186 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1187 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1188 | return |
---|
1189 | |
---|
1190 | except: |
---|
1191 | tcrtl.SetBackgroundColour("pink") |
---|
1192 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1193 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1194 | return |
---|
1195 | #Check if # of points for theory model are valid(>0). |
---|
1196 | # check for 2d |
---|
1197 | if self.data.__class__.__name__ =="Data2D": |
---|
1198 | # set mask |
---|
1199 | radius= numpy.sqrt( self.data.qx_data*self.data.qx_data + |
---|
1200 | self.data.qy_data*self.data.qy_data ) |
---|
1201 | index_data = ((self.qmin_x <= radius)& \ |
---|
1202 | (radius<= self.qmax_x)) |
---|
1203 | index_data = (index_data)&(self.data.mask) |
---|
1204 | index_data = (index_data)&(numpy.isfinite(self.data.data)) |
---|
1205 | if len(index_data[index_data]) < 10: |
---|
1206 | msg = "Cannot Plot :No or too little npts in" |
---|
1207 | msg += " that data range!!! " |
---|
1208 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1209 | return |
---|
1210 | else: |
---|
1211 | self.data.mask = index_data |
---|
1212 | #self.Npts_fit.SetValue(str(len(self.data.mask))) |
---|
1213 | self.set_npts2fit() |
---|
1214 | else: |
---|
1215 | if self.data is not None: |
---|
1216 | |
---|
1217 | index_data = ((self.qmin_x <= self.data.x)& \ |
---|
1218 | (self.data.x <= self.qmax_x)) |
---|
1219 | self.Npts_fit.SetValue(str(len(self.data.x[index_data]))) |
---|
1220 | |
---|
1221 | |
---|
1222 | else: |
---|
1223 | tcrtl.SetBackgroundColour("pink") |
---|
1224 | msg= "Model Error:wrong value entered!!!" |
---|
1225 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1226 | |
---|
1227 | self._draw_model() |
---|
1228 | ##update chi2 |
---|
1229 | #self.compute_chisqr(smearer= temp_smearer) |
---|
1230 | #self._undo.Enable(True) |
---|
1231 | self.save_current_state() |
---|
1232 | event = PageInfoEvent(page = self) |
---|
1233 | wx.PostEvent(self.parent, event) |
---|
1234 | self.state_change= False |
---|
1235 | return |
---|
1236 | |
---|
1237 | def _clear_Err_on_Fit(self): |
---|
1238 | """ |
---|
1239 | hide the error text control shown |
---|
1240 | after fitting |
---|
1241 | """ |
---|
1242 | |
---|
1243 | if hasattr(self,"text2_3"): |
---|
1244 | self.text2_3.Hide() |
---|
1245 | |
---|
1246 | if len(self.parameters)>0: |
---|
1247 | for item in self.parameters: |
---|
1248 | #Skip t ifhe angle parameters if 1D data |
---|
1249 | if self.data.__class__.__name__ !="Data2D": |
---|
1250 | if item in self.orientation_params: |
---|
1251 | continue |
---|
1252 | if item in self.param_toFit: |
---|
1253 | continue |
---|
1254 | ## hide statictext +/- |
---|
1255 | if item[3]!=None and item[3].IsShown(): |
---|
1256 | item[3].Hide() |
---|
1257 | ## hide textcrtl for error after fit |
---|
1258 | if item[4]!=None and item[4].IsShown(): |
---|
1259 | item[4].Hide() |
---|
1260 | |
---|
1261 | if len(self.fittable_param)>0: |
---|
1262 | for item in self.fittable_param: |
---|
1263 | #Skip t ifhe angle parameters if 1D data |
---|
1264 | if self.data.__class__.__name__ !="Data2D": |
---|
1265 | if item in self.orientation_params: |
---|
1266 | continue |
---|
1267 | if item in self.param_toFit: |
---|
1268 | continue |
---|
1269 | ## hide statictext +/- |
---|
1270 | if item[3]!=None and item[3].IsShown(): |
---|
1271 | item[3].Hide() |
---|
1272 | ## hide textcrtl for error after fit |
---|
1273 | if item[4]!=None and item[4].IsShown(): |
---|
1274 | item[4].Hide() |
---|
1275 | return |
---|
1276 | |
---|
1277 | def _get_defult_custom_smear(self): |
---|
1278 | """ |
---|
1279 | Get the defult values for custum smearing. |
---|
1280 | """ |
---|
1281 | # get the default values |
---|
1282 | if self.dxl == None: self.dxl = 0.0 |
---|
1283 | if self.dxw == None: self.dxw = "" |
---|
1284 | if self.dx_min == None: self.dx_min = SMEAR_SIZE_L |
---|
1285 | if self.dx_max == None: self.dx_max = SMEAR_SIZE_H |
---|
1286 | |
---|
1287 | def _get_smear_info(self): |
---|
1288 | """ |
---|
1289 | Get the smear info from data. |
---|
1290 | |
---|
1291 | :return: self.smear_type, self.dq_l and self.dq_r, |
---|
1292 | respectively the type of the smear, dq_min and |
---|
1293 | dq_max for pinhole smear data |
---|
1294 | while dxl and dxw for slit smear |
---|
1295 | |
---|
1296 | """ |
---|
1297 | |
---|
1298 | # default |
---|
1299 | self.smear_type = None |
---|
1300 | self.dq_l = None |
---|
1301 | self.dq_r = None |
---|
1302 | data = self.data |
---|
1303 | if self.data is None: |
---|
1304 | return |
---|
1305 | elif self.data.__class__.__name__ == 'Data2D': |
---|
1306 | if data.dqx_data == None or data.dqy_data ==None: |
---|
1307 | return |
---|
1308 | elif self.smearer != None and (data.dqx_data.any()!=0) and \ |
---|
1309 | (data.dqx_data.any()!=0): |
---|
1310 | self.smear_type = "Pinhole2d" |
---|
1311 | self.dq_l = format_number(numpy.average(data.dqx_data)) |
---|
1312 | self.dq_r = format_number(numpy.average(data.dqy_data)) |
---|
1313 | return |
---|
1314 | else: |
---|
1315 | return |
---|
1316 | # check if it is pinhole smear and get min max if it is. |
---|
1317 | if data.dx != None and all(data.dx !=0): |
---|
1318 | self.smear_type = "Pinhole" |
---|
1319 | self.dq_l = data.dx[0] |
---|
1320 | self.dq_r = data.dx[-1] |
---|
1321 | |
---|
1322 | # check if it is slit smear and get min max if it is. |
---|
1323 | elif data.dxl != None or data.dxw != None: |
---|
1324 | self.smear_type = "Slit" |
---|
1325 | if data.dxl != None and all(data.dxl !=0): |
---|
1326 | self.dq_l = data.dxl[0] |
---|
1327 | if data.dxw != None and all(data.dxw !=0): |
---|
1328 | self.dq_r = data.dxw[0] |
---|
1329 | #return self.smear_type,self.dq_l,self.dq_r |
---|
1330 | |
---|
1331 | def _show_smear_sizer(self): |
---|
1332 | """ |
---|
1333 | Show only the sizers depending on smear selection |
---|
1334 | """ |
---|
1335 | # smear disabled |
---|
1336 | if self.disable_smearer.GetValue(): |
---|
1337 | self.smear_description_none.Show(True) |
---|
1338 | # 2Dsmear |
---|
1339 | elif self._is_2D(): |
---|
1340 | self.smear_description_accuracy_type.Show(True) |
---|
1341 | self.smear_accuracy.Show(True) |
---|
1342 | self.smear_description_accuracy_type.Show(True) |
---|
1343 | self.smear_description_2d.Show(True) |
---|
1344 | self.smear_description_2d_x.Show(True) |
---|
1345 | self.smear_description_2d_y.Show(True) |
---|
1346 | if self.pinhole_smearer.GetValue(): |
---|
1347 | self.smear_pinhole_min.Show(True) |
---|
1348 | self.smear_pinhole_max.Show(True) |
---|
1349 | # smear from data |
---|
1350 | elif self.enable_smearer.GetValue(): |
---|
1351 | |
---|
1352 | self.smear_description_dqdata.Show(True) |
---|
1353 | if self.smear_type != None: |
---|
1354 | self.smear_description_smear_type.Show(True) |
---|
1355 | if self.smear_type == 'Slit': |
---|
1356 | self.smear_description_slit_height.Show(True) |
---|
1357 | self.smear_description_slit_width.Show(True) |
---|
1358 | elif self.smear_type == 'Pinhole': |
---|
1359 | self.smear_description_pin_min.Show(True) |
---|
1360 | self.smear_description_pin_max.Show(True) |
---|
1361 | self.smear_description_smear_type.Show(True) |
---|
1362 | self.smear_description_type.Show(True) |
---|
1363 | self.smear_data_left.Show(True) |
---|
1364 | self.smear_data_right.Show(True) |
---|
1365 | # custom pinhole smear |
---|
1366 | elif self.pinhole_smearer.GetValue(): |
---|
1367 | if self.smear_type == 'Pinhole': |
---|
1368 | self.smear_message_new_p.Show(True) |
---|
1369 | self.smear_description_pin_min.Show(True) |
---|
1370 | self.smear_description_pin_max.Show(True) |
---|
1371 | |
---|
1372 | self.smear_pinhole_min.Show(True) |
---|
1373 | self.smear_pinhole_max.Show(True) |
---|
1374 | # custom slit smear |
---|
1375 | elif self.slit_smearer.GetValue(): |
---|
1376 | self.smear_message_new_s.Show(True) |
---|
1377 | self.smear_description_slit_height.Show(True) |
---|
1378 | self.smear_slit_height.Show(True) |
---|
1379 | self.smear_description_slit_width.Show(True) |
---|
1380 | self.smear_slit_width.Show(True) |
---|
1381 | |
---|
1382 | def _hide_all_smear_info(self): |
---|
1383 | """ |
---|
1384 | Hide all smearing messages in the set_smearer sizer |
---|
1385 | """ |
---|
1386 | self.smear_description_none.Hide() |
---|
1387 | self.smear_description_dqdata.Hide() |
---|
1388 | self.smear_description_type.Hide() |
---|
1389 | self.smear_description_smear_type.Hide() |
---|
1390 | self.smear_description_accuracy_type.Hide() |
---|
1391 | self.smear_description_2d_x.Hide() |
---|
1392 | self.smear_description_2d_y.Hide() |
---|
1393 | self.smear_description_2d.Hide() |
---|
1394 | |
---|
1395 | self.smear_accuracy.Hide() |
---|
1396 | self.smear_data_left.Hide() |
---|
1397 | self.smear_data_right.Hide() |
---|
1398 | self.smear_description_pin_min.Hide() |
---|
1399 | self.smear_pinhole_min.Hide() |
---|
1400 | self.smear_description_pin_max.Hide() |
---|
1401 | self.smear_pinhole_max.Hide() |
---|
1402 | self.smear_description_slit_height.Hide() |
---|
1403 | self.smear_slit_height.Hide() |
---|
1404 | self.smear_description_slit_width.Hide() |
---|
1405 | self.smear_slit_width.Hide() |
---|
1406 | self.smear_message_new_p.Hide() |
---|
1407 | self.smear_message_new_s.Hide() |
---|
1408 | |
---|
1409 | def _set_accuracy_list(self): |
---|
1410 | """ |
---|
1411 | Set the list of an accuracy in 2D custum smear: Xhigh, High, Med, or Low |
---|
1412 | """ |
---|
1413 | # list of accuracy choices |
---|
1414 | list = ['Low','Med','High','Xhigh'] |
---|
1415 | for idx in range(len(list)): |
---|
1416 | self.smear_accuracy.Append(list[idx],idx) |
---|
1417 | |
---|
1418 | def _set_fun_box_list(self,fun_box): |
---|
1419 | """ |
---|
1420 | Set the list of func for multifunctional models |
---|
1421 | """ |
---|
1422 | # Check if it is multi_functional model |
---|
1423 | if self.model.__class__ not in self.model_list_box["Multi-Functions"]: |
---|
1424 | return None |
---|
1425 | # Get the func name list |
---|
1426 | list = self.model.fun_list |
---|
1427 | if len(list) == 0: |
---|
1428 | return None |
---|
1429 | # build function (combo)box |
---|
1430 | ind = 0 |
---|
1431 | while(ind < len(list)): |
---|
1432 | for key, val in list.iteritems(): |
---|
1433 | if (val == ind): |
---|
1434 | fun_box.Append(key,val) |
---|
1435 | break |
---|
1436 | ind += 1 |
---|
1437 | |
---|
1438 | def _on_select_accuracy(self,event): |
---|
1439 | """ |
---|
1440 | Select an accuracy in 2D custom smear: Xhigh, High, Med, or Low |
---|
1441 | """ |
---|
1442 | #event.Skip() |
---|
1443 | # Check if the accuracy is same as before |
---|
1444 | #self.smear2d_accuracy = event.GetEventObject().GetValue() |
---|
1445 | self.smear2d_accuracy = self.smear_accuracy.GetValue() |
---|
1446 | if self.pinhole_smearer.GetValue(): |
---|
1447 | self.onPinholeSmear(event=None) |
---|
1448 | else: |
---|
1449 | self.onSmear(event=None) |
---|
1450 | if self.current_smearer != None: |
---|
1451 | self.current_smearer.set_accuracy(accuracy = |
---|
1452 | self.smear2d_accuracy) |
---|
1453 | event.Skip() |
---|
1454 | |
---|
1455 | def _on_fun_box(self,event): |
---|
1456 | """ |
---|
1457 | Select an func: Erf,Rparabola,LParabola |
---|
1458 | """ |
---|
1459 | fun_val = None |
---|
1460 | fun_box = event.GetEventObject() |
---|
1461 | name = fun_box.Name |
---|
1462 | value = fun_box.GetValue() |
---|
1463 | if self.model.fun_list.has_key(value): |
---|
1464 | fun_val = self.model.fun_list[value] |
---|
1465 | |
---|
1466 | self.model.setParam(name,fun_val) |
---|
1467 | # save state |
---|
1468 | self._copy_parameters_state(self.str_parameters, |
---|
1469 | self.state.str_parameters) |
---|
1470 | # update params |
---|
1471 | self._update_paramv_on_fit() |
---|
1472 | # draw |
---|
1473 | self._draw_model() |
---|
1474 | self.Refresh() |
---|
1475 | # get ready for new event |
---|
1476 | event.Skip() |
---|
1477 | |
---|
1478 | def _onMask(self, event): |
---|
1479 | """ |
---|
1480 | Build a panel to allow to edit Mask |
---|
1481 | """ |
---|
1482 | |
---|
1483 | from sans.guiframe.local_perspectives.plotting.masking \ |
---|
1484 | import MaskPanel as MaskDialog |
---|
1485 | |
---|
1486 | self.panel = MaskDialog(self, data=self.data,id =-1 ) |
---|
1487 | self.panel.Bind(wx.EVT_CLOSE, self._draw_masked_model) |
---|
1488 | self.panel.ShowModal() |
---|
1489 | #wx.PostEvent(self.parent, event) |
---|
1490 | |
---|
1491 | def _draw_masked_model(self,event): |
---|
1492 | """ |
---|
1493 | Draw model image w/mask |
---|
1494 | """ |
---|
1495 | event.Skip() |
---|
1496 | |
---|
1497 | is_valid_qrange = self._update_paramv_on_fit() |
---|
1498 | |
---|
1499 | if is_valid_qrange: |
---|
1500 | # try re draw the model plot if it exists |
---|
1501 | self._draw_model() |
---|
1502 | self.panel.Destroy() # frame |
---|
1503 | self.set_npts2fit() |
---|
1504 | elif self.model == None: |
---|
1505 | self.panel.Destroy() |
---|
1506 | self.set_npts2fit() |
---|
1507 | msg= "No model is found on updating MASK in the model plot... " |
---|
1508 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1509 | else: |
---|
1510 | msg = ' Please consider your Q range, too.' |
---|
1511 | self.panel.ShowMessage(msg) |
---|
1512 | |
---|
1513 | def _set_smear(self, data): |
---|
1514 | """ |
---|
1515 | """ |
---|
1516 | if data is None: |
---|
1517 | return |
---|
1518 | self.smearer = smear_selection(self.data, self.model) |
---|
1519 | self.disable_smearer.SetValue(True) |
---|
1520 | if self.smearer == None: |
---|
1521 | self.enable_smearer.Disable() |
---|
1522 | else: |
---|
1523 | self.enable_smearer.Enable() |
---|
1524 | |
---|
1525 | def get_view_mode(self): |
---|
1526 | """ |
---|
1527 | return True if the panel allow 2D or False if 1D |
---|
1528 | """ |
---|
1529 | return self.enable2D |
---|
1530 | |
---|
1531 | def set_data(self, data): |
---|
1532 | """ |
---|
1533 | reset the current data |
---|
1534 | """ |
---|
1535 | flag = False |
---|
1536 | if self.data is None and data is not None: |
---|
1537 | self.window_name = str(data.name) |
---|
1538 | ## Title to appear on top of the window |
---|
1539 | self.window_caption = str(data.name) |
---|
1540 | flag = True |
---|
1541 | self.data = data |
---|
1542 | if self.data is None: |
---|
1543 | data_min = "" |
---|
1544 | data_max = "" |
---|
1545 | data_name = "" |
---|
1546 | self._set_bookmark_flag(False) |
---|
1547 | self._set_save_flag(False) |
---|
1548 | else: |
---|
1549 | self._set_bookmark_flag(True) |
---|
1550 | self._set_save_flag(True) |
---|
1551 | self._set_smear(data) |
---|
1552 | # more disables for 2D |
---|
1553 | if self.data.__class__.__name__ =="Data2D": |
---|
1554 | self.slit_smearer.Disable() |
---|
1555 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
1556 | |
---|
1557 | |
---|
1558 | self.formfactorbox.Enable() |
---|
1559 | self.structurebox.Enable() |
---|
1560 | data_name = self.data.name |
---|
1561 | #set maximum range for x in linear scale |
---|
1562 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
1563 | # Minimum value of data |
---|
1564 | data_min = min(self.data.x) |
---|
1565 | # Maximum value of data |
---|
1566 | data_max = max(self.data.x) |
---|
1567 | #number of total data points |
---|
1568 | self.Npts_total.SetValue(str(len(self.data.x))) |
---|
1569 | #default:number of data points selected to fit |
---|
1570 | self.Npts_fit.SetValue(str(len(self.data.x))) |
---|
1571 | self.btEditMask.Disable() |
---|
1572 | self.EditMask_title.Disable() |
---|
1573 | else: |
---|
1574 | |
---|
1575 | ## Minimum value of data |
---|
1576 | data_min = 0 |
---|
1577 | x = max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
1578 | y = max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
1579 | ## Maximum value of data |
---|
1580 | data_max = math.sqrt(x*x + y*y) |
---|
1581 | #number of total data points |
---|
1582 | self.Npts_total.SetValue(str(len(self.data.data))) |
---|
1583 | #default:number of data points selected to fit |
---|
1584 | self.Npts_fit.SetValue(str(len(self.data.data))) |
---|
1585 | self.btEditMask.Enable() |
---|
1586 | self.EditMask_title.Enable() |
---|
1587 | self.dataSource.SetValue(data_name) |
---|
1588 | self.qmin_x = data_min |
---|
1589 | self.qmax_x = data_max |
---|
1590 | self.minimum_q.SetValue(str(data_min)) |
---|
1591 | self.maximum_q.SetValue(str(data_max)) |
---|
1592 | self.qmin_tcrl.SetValue(str(data_min)) |
---|
1593 | self.qmax.SetValue(str(data_max)) |
---|
1594 | self.qmin_tcrl.SetBackgroundColour("white") |
---|
1595 | self.qmax.SetBackgroundColour("white") |
---|
1596 | self.state.data = data |
---|
1597 | self.state.qmin = self.qmin_x |
---|
1598 | self.state.qmax = self.qmax_x |
---|
1599 | |
---|
1600 | #update model plot with new data information |
---|
1601 | if flag: |
---|
1602 | #set model view button |
---|
1603 | if self.data.__class__.__name__ == "Data2D": |
---|
1604 | self.enable2D = True |
---|
1605 | self.model_view.SetLabel("2D Mode") |
---|
1606 | else: |
---|
1607 | self.enable2D = False |
---|
1608 | self.model_view.SetLabel("1D Mode") |
---|
1609 | |
---|
1610 | self.model_view.Disable() |
---|
1611 | self._draw_model() |
---|
1612 | |
---|
1613 | def reset_page(self, state,first=False): |
---|
1614 | """ |
---|
1615 | reset the state |
---|
1616 | """ |
---|
1617 | self.reset_page_helper(state) |
---|
1618 | #import sans.guiframe.gui_manager |
---|
1619 | #evt = ModelEventbox(model=state.model) |
---|
1620 | #wx.PostEvent(self.event_owner, evt) |
---|
1621 | |
---|
1622 | if self.engine_type != None: |
---|
1623 | self._manager._on_change_engine(engine=self.engine_type) |
---|
1624 | |
---|
1625 | self.select_param(event = None) |
---|
1626 | #Save state_fit |
---|
1627 | self.save_current_state_fit() |
---|
1628 | self._lay_out() |
---|
1629 | self.Refresh() |
---|
1630 | |
---|
1631 | def get_range(self): |
---|
1632 | """ |
---|
1633 | return the fitting range |
---|
1634 | """ |
---|
1635 | return float(self.qmin_x) , float(self.qmax_x) |
---|
1636 | |
---|
1637 | def get_npts2fit(self): |
---|
1638 | """ |
---|
1639 | return numbers of data points within qrange |
---|
1640 | |
---|
1641 | :Note: This is for Park where chi2 is not normalized by Npts of fit |
---|
1642 | |
---|
1643 | """ |
---|
1644 | if self.data is None: |
---|
1645 | return |
---|
1646 | npts2fit = 0 |
---|
1647 | qmin,qmax = self.get_range() |
---|
1648 | if self.data.__class__.__name__ =="Data2D": |
---|
1649 | radius= numpy.sqrt( self.data.qx_data*self.data.qx_data + |
---|
1650 | self.data.qy_data*self.data.qy_data ) |
---|
1651 | index_data = (self.qmin_x <= radius)&(radius<= self.qmax_x) |
---|
1652 | index_data= (index_data)&(self.data.mask) |
---|
1653 | index_data = (index_data)&(numpy.isfinite(self.data.data)) |
---|
1654 | npts2fit = len(self.data.data[index_data]) |
---|
1655 | else: |
---|
1656 | for qx in self.data.x: |
---|
1657 | if qx >= qmin and qx <= qmax: |
---|
1658 | npts2fit += 1 |
---|
1659 | return npts2fit |
---|
1660 | |
---|
1661 | def set_npts2fit(self): |
---|
1662 | """ |
---|
1663 | setValue Npts for fitting |
---|
1664 | """ |
---|
1665 | self.Npts_fit.SetValue(str(self.get_npts2fit())) |
---|
1666 | |
---|
1667 | def get_chi2(self): |
---|
1668 | """ |
---|
1669 | return the current chi2 |
---|
1670 | """ |
---|
1671 | return self.tcChi.GetValue() |
---|
1672 | |
---|
1673 | def get_param_list(self): |
---|
1674 | """ |
---|
1675 | :return self.param_toFit: list containing references to TextCtrl |
---|
1676 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
1677 | |
---|
1678 | :raise: if return an empty list of parameter fit will nnote work |
---|
1679 | properly so raise ValueError,"missing parameter to fit" |
---|
1680 | """ |
---|
1681 | if self.param_toFit !=[]: |
---|
1682 | return self.param_toFit |
---|
1683 | else: |
---|
1684 | raise ValueError,"missing parameter to fit" |
---|
1685 | |
---|
1686 | def onsetValues(self,chisqr,p_name, out,cov): |
---|
1687 | """ |
---|
1688 | Build the panel from the fit result |
---|
1689 | |
---|
1690 | :param chisqr: Value of the goodness of fit metric |
---|
1691 | :param p_name: the name of parameters |
---|
1692 | :param out: list of parameter with the best value found during fitting |
---|
1693 | :param cov: Covariance matrix |
---|
1694 | |
---|
1695 | """ |
---|
1696 | if out == None or not numpy.isfinite(chisqr): |
---|
1697 | raise ValueError,"Fit error occured..." |
---|
1698 | |
---|
1699 | is_modified = False |
---|
1700 | has_error = False |
---|
1701 | |
---|
1702 | #Hide textctrl boxes of errors. |
---|
1703 | self._clear_Err_on_Fit() |
---|
1704 | |
---|
1705 | #Check if chi2 is finite |
---|
1706 | if chisqr != None or numpy.isfinite(chisqr): |
---|
1707 | #format chi2 |
---|
1708 | if self.engine_type == "park": |
---|
1709 | npt_fit = float(self.get_npts2fit()) |
---|
1710 | if npt_fit > 0: |
---|
1711 | chisqr =chisqr/npt_fit |
---|
1712 | chi2 = format_number(chisqr) |
---|
1713 | self.tcChi.SetValue(chi2) |
---|
1714 | self.tcChi.Refresh() |
---|
1715 | else: |
---|
1716 | self.tcChi.SetValue("-") |
---|
1717 | |
---|
1718 | #Hide error title |
---|
1719 | if self.text2_3.IsShown(): |
---|
1720 | self.text2_3.Hide() |
---|
1721 | |
---|
1722 | try: |
---|
1723 | n = self.disp_box.GetCurrentSelection() |
---|
1724 | dispersity= self.disp_box.GetClientData(n) |
---|
1725 | if dispersity !=None and self.enable_disp.GetValue(): |
---|
1726 | name= dispersity.__name__ |
---|
1727 | if name == "GaussianDispersion": |
---|
1728 | if hasattr(self,"text_disp_1" ): |
---|
1729 | if self.text_disp_1 !=None: |
---|
1730 | self.text_disp_1.Hide() |
---|
1731 | except: |
---|
1732 | dispersty = None |
---|
1733 | pass |
---|
1734 | #set the panel when fit result are float not list |
---|
1735 | if out.__class__== numpy.float64: |
---|
1736 | self.param_toFit[0][2].SetValue(format_number(out)) |
---|
1737 | |
---|
1738 | if self.param_toFit[0][4].IsShown: |
---|
1739 | self.param_toFit[0][4].Hide() |
---|
1740 | if cov !=None : |
---|
1741 | self.text2_3.Show(True) |
---|
1742 | try: |
---|
1743 | if dispersity !=None: |
---|
1744 | name= dispersity.__name__ |
---|
1745 | if name == "GaussianDispersion" and \ |
---|
1746 | self.enable_disp.GetValue(): |
---|
1747 | if hasattr(self,"text_disp_1" ): |
---|
1748 | if self.text_disp_1 !=None: |
---|
1749 | self.text_disp_1.Show(True) |
---|
1750 | except: |
---|
1751 | pass |
---|
1752 | |
---|
1753 | if cov[0]==None or not numpy.isfinite(cov[0]): |
---|
1754 | if self.param_toFit[0][3].IsShown: |
---|
1755 | self.param_toFit[0][3].Hide() |
---|
1756 | else: |
---|
1757 | self.param_toFit[0][3].Show(True) |
---|
1758 | self.param_toFit[0][4].Show(True) |
---|
1759 | self.param_toFit[0][4].SetValue(format_number(cov[0])) |
---|
1760 | has_error = True |
---|
1761 | else: |
---|
1762 | |
---|
1763 | i = 0 |
---|
1764 | #Set the panel when fit result are list |
---|
1765 | for item in self.param_toFit: |
---|
1766 | if len(item)>5 and item != None: |
---|
1767 | ## reset error value to initial state |
---|
1768 | item[3].Hide() |
---|
1769 | item[4].Hide() |
---|
1770 | |
---|
1771 | for ind in range(len(out)): |
---|
1772 | |
---|
1773 | if item[1] == p_name[ind]: |
---|
1774 | break |
---|
1775 | if len(out)<=len(self.param_toFit) and out[ind] !=None: |
---|
1776 | val_out = format_number(out[ind]) |
---|
1777 | item[2].SetValue(val_out) |
---|
1778 | |
---|
1779 | if(cov !=None): |
---|
1780 | |
---|
1781 | try: |
---|
1782 | if dispersity !=None: |
---|
1783 | name= dispersity.__name__ |
---|
1784 | if name == "GaussianDispersion" and \ |
---|
1785 | self.enable_disp.GetValue(): |
---|
1786 | if hasattr(self,"text_disp_1" ): |
---|
1787 | if self.text_disp_1!=None: |
---|
1788 | if not self.text_disp_1.IsShown(): |
---|
1789 | self.text_disp_1.Show(True) |
---|
1790 | except: |
---|
1791 | pass |
---|
1792 | |
---|
1793 | if cov[ind]!=None : |
---|
1794 | if numpy.isfinite(float(cov[ind])): |
---|
1795 | val_err = format_number(cov[ind]) |
---|
1796 | item[3].Show(True) |
---|
1797 | item[4].Show(True) |
---|
1798 | item[4].SetValue(val_err) |
---|
1799 | |
---|
1800 | has_error = True |
---|
1801 | i += 1 |
---|
1802 | #Show error title when any errors displayed |
---|
1803 | if has_error: |
---|
1804 | if not self.text2_3.IsShown(): |
---|
1805 | self.text2_3.Show(True) |
---|
1806 | |
---|
1807 | ## save current state |
---|
1808 | self.save_current_state() |
---|
1809 | #plot model |
---|
1810 | self._draw_model() |
---|
1811 | self._lay_out() |
---|
1812 | #PostStatusEvent |
---|
1813 | msg = "Fit completed! " |
---|
1814 | wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
1815 | |
---|
1816 | def onPinholeSmear(self, event): |
---|
1817 | """ |
---|
1818 | Create a custom pinhole smear object that will change the way residuals |
---|
1819 | are compute when fitting |
---|
1820 | |
---|
1821 | :Note: accuracy is given by strings'High','Med', 'Low' FOR 2d, |
---|
1822 | None for 1D |
---|
1823 | |
---|
1824 | """ |
---|
1825 | |
---|
1826 | #if self.check_invalid_panel(): |
---|
1827 | # return |
---|
1828 | if self.model ==None: |
---|
1829 | self.disable_smearer.SetValue(True) |
---|
1830 | msg="Please select a Model first..." |
---|
1831 | wx.MessageBox(msg, 'Info') |
---|
1832 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
1833 | "Smear: %s"%msg)) |
---|
1834 | return |
---|
1835 | |
---|
1836 | # Need update param values |
---|
1837 | self._update_paramv_on_fit() |
---|
1838 | |
---|
1839 | # msg default |
---|
1840 | msg = None |
---|
1841 | if event != None: |
---|
1842 | tcrtl= event.GetEventObject() |
---|
1843 | # event case of radio button |
---|
1844 | if tcrtl.GetValue()== True: |
---|
1845 | self.dx_min = 0.0 |
---|
1846 | self.dx_max = 0.0 |
---|
1847 | is_new_pinhole = True |
---|
1848 | else: |
---|
1849 | is_new_pinhole = self._is_changed_pinhole() |
---|
1850 | else: |
---|
1851 | is_new_pinhole = True |
---|
1852 | # if any value is changed |
---|
1853 | if is_new_pinhole: |
---|
1854 | msg = self._set_pinhole_smear() |
---|
1855 | # hide all silt sizer |
---|
1856 | self._hide_all_smear_info() |
---|
1857 | |
---|
1858 | ##Calculate chi2 |
---|
1859 | temp_smearer = self.current_smearer |
---|
1860 | #self.compute_chisqr(smearer= temp_smearer) |
---|
1861 | |
---|
1862 | # show relevant slit sizers |
---|
1863 | self._show_smear_sizer() |
---|
1864 | |
---|
1865 | self.sizer_set_smearer.Layout() |
---|
1866 | self.Layout() |
---|
1867 | |
---|
1868 | if event != None: |
---|
1869 | event.Skip() |
---|
1870 | #self._undo.Enable(True) |
---|
1871 | self.save_current_state() |
---|
1872 | event = PageInfoEvent(page = self) |
---|
1873 | wx.PostEvent(self.parent, event) |
---|
1874 | |
---|
1875 | def _is_changed_pinhole(self): |
---|
1876 | """ |
---|
1877 | check if any of pinhole smear is changed |
---|
1878 | |
---|
1879 | :return: True or False |
---|
1880 | |
---|
1881 | """ |
---|
1882 | # get the values |
---|
1883 | pin_min = self.smear_pinhole_min.GetValue() |
---|
1884 | pin_max = self.smear_pinhole_max.GetValue() |
---|
1885 | |
---|
1886 | # Check changes in slit width |
---|
1887 | try: |
---|
1888 | dx_min = float(pin_min) |
---|
1889 | except: |
---|
1890 | return True |
---|
1891 | if self.dx_min != dx_min: |
---|
1892 | return True |
---|
1893 | |
---|
1894 | # Check changes in slit heigth |
---|
1895 | try: |
---|
1896 | dx_max = float(pin_max) |
---|
1897 | except: |
---|
1898 | return True |
---|
1899 | if self.dx_max != dx_max: |
---|
1900 | return True |
---|
1901 | return False |
---|
1902 | |
---|
1903 | def _set_pinhole_smear(self): |
---|
1904 | """ |
---|
1905 | Set custom pinhole smear |
---|
1906 | |
---|
1907 | :return: msg |
---|
1908 | |
---|
1909 | """ |
---|
1910 | # copy data |
---|
1911 | data = copy.deepcopy(self.data) |
---|
1912 | if self._is_2D(): |
---|
1913 | self.smear_type = 'Pinhole2d' |
---|
1914 | len_data = len(data.data) |
---|
1915 | data.dqx_data = numpy.zeros(len_data) |
---|
1916 | data.dqy_data = numpy.zeros(len_data) |
---|
1917 | else: |
---|
1918 | self.smear_type = 'Pinhole' |
---|
1919 | len_data = len(data.x) |
---|
1920 | data.dx = numpy.zeros(len_data) |
---|
1921 | data.dxl = None |
---|
1922 | data.dxw = None |
---|
1923 | msg = None |
---|
1924 | |
---|
1925 | get_pin_min = self.smear_pinhole_min |
---|
1926 | get_pin_max = self.smear_pinhole_max |
---|
1927 | |
---|
1928 | if not check_float(get_pin_min): |
---|
1929 | get_pin_min.SetBackgroundColour("pink") |
---|
1930 | msg= "Model Error:wrong value entered!!!" |
---|
1931 | elif not check_float(get_pin_max ): |
---|
1932 | get_pin_max.SetBackgroundColour("pink") |
---|
1933 | msg= "Model Error:wrong value entered!!!" |
---|
1934 | else: |
---|
1935 | if len_data < 2: len_data = 2 |
---|
1936 | self.dx_min = float(get_pin_min.GetValue()) |
---|
1937 | self.dx_max = float(get_pin_max.GetValue()) |
---|
1938 | if self.dx_min < 0: |
---|
1939 | get_pin_min.SetBackgroundColour("pink") |
---|
1940 | msg= "Model Error:This value can not be negative!!!" |
---|
1941 | elif self.dx_max <0: |
---|
1942 | get_pin_max.SetBackgroundColour("pink") |
---|
1943 | msg= "Model Error:This value can not be negative!!!" |
---|
1944 | elif self.dx_min != None and self.dx_max != None: |
---|
1945 | if self._is_2D(): |
---|
1946 | data.dqx_data[data.dqx_data==0] = self.dx_min |
---|
1947 | data.dqy_data[data.dqy_data==0] = self.dx_max |
---|
1948 | elif self.dx_min == self.dx_max: |
---|
1949 | data.dx[data.dx==0] = self.dx_min |
---|
1950 | else: |
---|
1951 | step = (self.dx_max - self.dx_min)/(len_data-1) |
---|
1952 | data.dx = numpy.arange(self.dx_min,self.dx_max+step/1.1, |
---|
1953 | step) |
---|
1954 | elif self.dx_min != None: |
---|
1955 | if self._is_2D(): data.dqx_data[data.dqx_data==0] = self.dx_min |
---|
1956 | else: data.dx[data.dx==0] = self.dx_min |
---|
1957 | elif self.dx_max != None: |
---|
1958 | if self._is_2D(): data.dqy_data[data.dqy_data==0] = self.dx_max |
---|
1959 | else: data.dx[data.dx==0] = self.dx_max |
---|
1960 | self.current_smearer = smear_selection(data, self.model) |
---|
1961 | # 2D need to set accuracy |
---|
1962 | if self._is_2D(): |
---|
1963 | self.current_smearer.set_accuracy(accuracy = \ |
---|
1964 | self.smear2d_accuracy) |
---|
1965 | |
---|
1966 | if msg != None: |
---|
1967 | wx.PostEvent(self._manager.parent, StatusEvent(status = msg )) |
---|
1968 | else: |
---|
1969 | get_pin_min.SetBackgroundColour("white") |
---|
1970 | get_pin_max.SetBackgroundColour("white") |
---|
1971 | ## set smearing value whether or not the data contain the smearing info |
---|
1972 | self._manager.set_smearer(smearer=self.current_smearer, |
---|
1973 | qmin=float(self.qmin_x), |
---|
1974 | qmax= float(self.qmax_x), |
---|
1975 | id=self.id) |
---|
1976 | return msg |
---|
1977 | |
---|
1978 | def update_pinhole_smear(self): |
---|
1979 | """ |
---|
1980 | called by kill_focus on pinhole TextCntrl |
---|
1981 | to update the changes |
---|
1982 | |
---|
1983 | :return: False when wrong value was entered |
---|
1984 | |
---|
1985 | """ |
---|
1986 | # msg default |
---|
1987 | msg = None |
---|
1988 | # check if any value is changed |
---|
1989 | if self._is_changed_pinhole(): |
---|
1990 | msg = self._set_pinhole_smear() |
---|
1991 | #self._undo.Enable(True) |
---|
1992 | self.save_current_state() |
---|
1993 | |
---|
1994 | if msg != None: |
---|
1995 | return False |
---|
1996 | else: |
---|
1997 | return True |
---|
1998 | |
---|
1999 | def onSlitSmear(self, event): |
---|
2000 | """ |
---|
2001 | Create a custom slit smear object that will change the way residuals |
---|
2002 | are compute when fitting |
---|
2003 | """ |
---|
2004 | |
---|
2005 | #if self.check_invalid_panel(): |
---|
2006 | # return |
---|
2007 | |
---|
2008 | if self.model ==None: |
---|
2009 | self.disable_smearer.SetValue(True) |
---|
2010 | |
---|
2011 | msg="Please select a Model first..." |
---|
2012 | wx.MessageBox(msg, 'Info') |
---|
2013 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2014 | "Smear: %s"%msg)) |
---|
2015 | return |
---|
2016 | |
---|
2017 | # Need update param values |
---|
2018 | self._update_paramv_on_fit() |
---|
2019 | |
---|
2020 | # msg default |
---|
2021 | msg = None |
---|
2022 | # for event given |
---|
2023 | if event != None: |
---|
2024 | tcrtl= event.GetEventObject() |
---|
2025 | # event case of radio button |
---|
2026 | if tcrtl.GetValue(): |
---|
2027 | self.dxl = 0.0 |
---|
2028 | self.dxw = 0.0 |
---|
2029 | is_new_slit = True |
---|
2030 | else: |
---|
2031 | is_new_slit = self._is_changed_slit() |
---|
2032 | else: |
---|
2033 | is_new_slit = True |
---|
2034 | |
---|
2035 | # if any value is changed |
---|
2036 | if is_new_slit: |
---|
2037 | msg = self._set_slit_smear() |
---|
2038 | |
---|
2039 | # hide all silt sizer |
---|
2040 | self._hide_all_smear_info() |
---|
2041 | ##Calculate chi2 |
---|
2042 | #self.compute_chisqr(smearer= self.current_smearer) |
---|
2043 | # show relevant slit sizers |
---|
2044 | self._show_smear_sizer() |
---|
2045 | self.sizer_set_smearer.Layout() |
---|
2046 | self.Layout() |
---|
2047 | |
---|
2048 | if event != None: |
---|
2049 | event.Skip() |
---|
2050 | #self._undo.Enable(True) |
---|
2051 | self.save_current_state() |
---|
2052 | event = PageInfoEvent(page = self) |
---|
2053 | wx.PostEvent(self.parent, event) |
---|
2054 | if msg != None: |
---|
2055 | wx.PostEvent(self._manager.parent, StatusEvent(status = msg)) |
---|
2056 | |
---|
2057 | def _is_changed_slit(self): |
---|
2058 | """ |
---|
2059 | check if any of slit lengths is changed |
---|
2060 | |
---|
2061 | :return: True or False |
---|
2062 | |
---|
2063 | """ |
---|
2064 | # get the values |
---|
2065 | width = self.smear_slit_width.GetValue() |
---|
2066 | height = self.smear_slit_height.GetValue() |
---|
2067 | |
---|
2068 | # check and change the box bg color if it was pink |
---|
2069 | # but it should be white now |
---|
2070 | # because this is the case that _set_slit_smear() will not handle |
---|
2071 | if height.lstrip().rstrip()=="": |
---|
2072 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2073 | if width.lstrip().rstrip()=="": |
---|
2074 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2075 | |
---|
2076 | # Check changes in slit width |
---|
2077 | if width == "": |
---|
2078 | dxw = 0.0 |
---|
2079 | else: |
---|
2080 | try: |
---|
2081 | dxw = float(width) |
---|
2082 | except: |
---|
2083 | return True |
---|
2084 | if self.dxw != dxw: |
---|
2085 | return True |
---|
2086 | |
---|
2087 | # Check changes in slit heigth |
---|
2088 | if height == "": |
---|
2089 | dxl = 0.0 |
---|
2090 | else: |
---|
2091 | try: |
---|
2092 | dxl = float(height) |
---|
2093 | except: |
---|
2094 | return True |
---|
2095 | if self.dxl != dxl: |
---|
2096 | return True |
---|
2097 | |
---|
2098 | return False |
---|
2099 | |
---|
2100 | def _set_slit_smear(self): |
---|
2101 | """ |
---|
2102 | Set custom slit smear |
---|
2103 | |
---|
2104 | :return: message to inform the user about the validity |
---|
2105 | of the values entered for slit smear |
---|
2106 | """ |
---|
2107 | if self.data.__class__.__name__ == "Data2D": |
---|
2108 | return |
---|
2109 | temp_smearer = None |
---|
2110 | # make sure once more if it is smearer |
---|
2111 | data = copy.deepcopy(self.data) |
---|
2112 | data_len = len(data.x) |
---|
2113 | data.dx = None |
---|
2114 | data.dxl = None |
---|
2115 | data.dxw = None |
---|
2116 | msg = None |
---|
2117 | |
---|
2118 | try: |
---|
2119 | self.dxl = float(self.smear_slit_height.GetValue()) |
---|
2120 | data.dxl = self.dxl* numpy.ones(data_len) |
---|
2121 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2122 | except: |
---|
2123 | data.dxl = numpy.zeros(data_len) |
---|
2124 | if self.smear_slit_height.GetValue().lstrip().rstrip()!="": |
---|
2125 | self.smear_slit_height.SetBackgroundColour("pink") |
---|
2126 | msg = "Wrong value entered... " |
---|
2127 | else: |
---|
2128 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2129 | try: |
---|
2130 | self.dxw = float(self.smear_slit_width.GetValue()) |
---|
2131 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2132 | data.dxw = self.dxw* numpy.ones(data_len) |
---|
2133 | except: |
---|
2134 | data.dxw = numpy.zeros(data_len) |
---|
2135 | if self.smear_slit_width.GetValue().lstrip().rstrip()!="": |
---|
2136 | self.smear_slit_width.SetBackgroundColour("pink") |
---|
2137 | msg = "Wrong Fit value entered... " |
---|
2138 | else: |
---|
2139 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2140 | |
---|
2141 | self.current_smearer = smear_selection(data, self.model) |
---|
2142 | #temp_smearer = self.current_smearer |
---|
2143 | ## set smearing value whether or not the data contain the smearing info |
---|
2144 | self._manager.set_smearer(smearer=self.current_smearer, |
---|
2145 | qmin=float(self.qmin_x), |
---|
2146 | qmax= float(self.qmax_x), |
---|
2147 | id=self.id) |
---|
2148 | return msg |
---|
2149 | |
---|
2150 | def update_slit_smear(self): |
---|
2151 | """ |
---|
2152 | called by kill_focus on pinhole TextCntrl |
---|
2153 | to update the changes |
---|
2154 | |
---|
2155 | :return: False when wrong value was entered |
---|
2156 | |
---|
2157 | """ |
---|
2158 | # msg default |
---|
2159 | msg = None |
---|
2160 | # check if any value is changed |
---|
2161 | if self._is_changed_slit(): |
---|
2162 | msg = self._set_slit_smear() |
---|
2163 | #self._undo.Enable(True) |
---|
2164 | self.save_current_state() |
---|
2165 | |
---|
2166 | if msg != None: |
---|
2167 | return False |
---|
2168 | else: |
---|
2169 | return True |
---|
2170 | |
---|
2171 | def onSmear(self, event): |
---|
2172 | """ |
---|
2173 | Create a smear object that will change the way residuals |
---|
2174 | are compute when fitting |
---|
2175 | """ |
---|
2176 | if event != None: |
---|
2177 | event.Skip() |
---|
2178 | if self.data is None: |
---|
2179 | return |
---|
2180 | |
---|
2181 | if self.model == None: |
---|
2182 | self.disable_smearer.SetValue(True) |
---|
2183 | msg="Please select a Model first..." |
---|
2184 | wx.MessageBox(msg, 'Info') |
---|
2185 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2186 | "Smear: %s"%msg)) |
---|
2187 | return |
---|
2188 | |
---|
2189 | # Need update param values |
---|
2190 | self._update_paramv_on_fit() |
---|
2191 | |
---|
2192 | temp_smearer = None |
---|
2193 | self._get_smear_info() |
---|
2194 | |
---|
2195 | #renew smear sizer |
---|
2196 | if self.smear_type != None: |
---|
2197 | self.smear_description_smear_type.SetValue(str(self.smear_type)) |
---|
2198 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
2199 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
2200 | |
---|
2201 | self._hide_all_smear_info() |
---|
2202 | |
---|
2203 | data = copy.deepcopy(self.data) |
---|
2204 | # make sure once more if it is smearer |
---|
2205 | self.current_smearer = smear_selection(data, self.model) |
---|
2206 | |
---|
2207 | if self.enable_smearer.GetValue(): |
---|
2208 | if hasattr(self.data,"dxl"): |
---|
2209 | |
---|
2210 | msg= ": Resolution smearing parameters" |
---|
2211 | if hasattr(self.data,"dxw"): |
---|
2212 | msg= ": Slit smearing parameters" |
---|
2213 | if self.smearer ==None: |
---|
2214 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2215 | "Data contains no smearing information")) |
---|
2216 | else: |
---|
2217 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2218 | "Data contains smearing information")) |
---|
2219 | |
---|
2220 | #self.smear_description_dqdata.Show(True) |
---|
2221 | self.smear_data_left.Show(True) |
---|
2222 | self.smear_data_right.Show(True) |
---|
2223 | temp_smearer= self.current_smearer |
---|
2224 | elif self.disable_smearer.GetValue(): |
---|
2225 | self.smear_description_none.Show(True) |
---|
2226 | |
---|
2227 | self._show_smear_sizer() |
---|
2228 | |
---|
2229 | self.sizer_set_smearer.Layout() |
---|
2230 | self.Layout() |
---|
2231 | ## set smearing value whether or not the data contain the smearing info |
---|
2232 | self._manager.set_smearer(id=self.id, smearer=temp_smearer, qmin= float(self.qmin_x), |
---|
2233 | qmax= float(self.qmax_x), draw=True) |
---|
2234 | |
---|
2235 | ##Calculate chi2 |
---|
2236 | #self.compute_chisqr(smearer= temp_smearer) |
---|
2237 | |
---|
2238 | self.state.enable_smearer= self.enable_smearer.GetValue() |
---|
2239 | self.state.disable_smearer=self.disable_smearer.GetValue() |
---|
2240 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
2241 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
2242 | |
---|
2243 | def on_complete_chisqr(self, event): |
---|
2244 | """ |
---|
2245 | print result chisqr |
---|
2246 | |
---|
2247 | :event: activated by fitting/ complete after draw |
---|
2248 | |
---|
2249 | """ |
---|
2250 | try: |
---|
2251 | if event ==None: |
---|
2252 | output= "-" |
---|
2253 | else: |
---|
2254 | output = event.output |
---|
2255 | self.tcChi.SetValue(str(format_number(output))) |
---|
2256 | |
---|
2257 | self.state.tcChi = self.tcChi.GetValue() |
---|
2258 | except: |
---|
2259 | pass |
---|
2260 | |
---|
2261 | |
---|
2262 | def select_all_param(self,event): |
---|
2263 | """ |
---|
2264 | set to true or false all checkBox given the main checkbox value cb1 |
---|
2265 | """ |
---|
2266 | self.param_toFit=[] |
---|
2267 | if self.parameters !=[]: |
---|
2268 | if self.cb1.GetValue(): |
---|
2269 | for item in self.parameters: |
---|
2270 | ## for data2D select all to fit |
---|
2271 | if self.data.__class__.__name__=="Data2D": |
---|
2272 | item[0].SetValue(True) |
---|
2273 | self.param_toFit.append(item ) |
---|
2274 | else: |
---|
2275 | ## for 1D all parameters except orientation |
---|
2276 | if not item in self.orientation_params: |
---|
2277 | item[0].SetValue(True) |
---|
2278 | self.param_toFit.append(item ) |
---|
2279 | #if len(self.fittable_param)>0: |
---|
2280 | for item in self.fittable_param: |
---|
2281 | if self.data.__class__.__name__=="Data2D": |
---|
2282 | item[0].SetValue(True) |
---|
2283 | self.param_toFit.append(item ) |
---|
2284 | else: |
---|
2285 | ## for 1D all parameters except orientation |
---|
2286 | if not item in self.orientation_params_disp: |
---|
2287 | item[0].SetValue(True) |
---|
2288 | self.param_toFit.append(item ) |
---|
2289 | else: |
---|
2290 | for item in self.parameters: |
---|
2291 | item[0].SetValue(False) |
---|
2292 | for item in self.fittable_param: |
---|
2293 | item[0].SetValue(False) |
---|
2294 | self.param_toFit=[] |
---|
2295 | |
---|
2296 | self.save_current_state_fit() |
---|
2297 | |
---|
2298 | if event !=None: |
---|
2299 | #self._undo.Enable(True) |
---|
2300 | ## post state to fit panel |
---|
2301 | event = PageInfoEvent(page = self) |
---|
2302 | wx.PostEvent(self.parent, event) |
---|
2303 | |
---|
2304 | def select_param(self,event): |
---|
2305 | """ |
---|
2306 | Select TextCtrl checked for fitting purpose and stores them |
---|
2307 | in self.param_toFit=[] list |
---|
2308 | """ |
---|
2309 | self.param_toFit=[] |
---|
2310 | for item in self.parameters: |
---|
2311 | #Skip t ifhe angle parameters if 1D data |
---|
2312 | if self.data.__class__.__name__ !="Data2D": |
---|
2313 | if item in self.orientation_params: |
---|
2314 | continue |
---|
2315 | #Select parameters to fit for list of primary parameters |
---|
2316 | if item[0].GetValue(): |
---|
2317 | if not (item in self.param_toFit): |
---|
2318 | self.param_toFit.append(item ) |
---|
2319 | else: |
---|
2320 | #remove parameters from the fitting list |
---|
2321 | if item in self.param_toFit: |
---|
2322 | self.param_toFit.remove(item) |
---|
2323 | |
---|
2324 | #Select parameters to fit for list of fittable parameters |
---|
2325 | # with dispersion |
---|
2326 | for item in self.fittable_param: |
---|
2327 | #Skip t ifhe angle parameters if 1D data |
---|
2328 | if self.data.__class__.__name__ !="Data2D": |
---|
2329 | if item in self.orientation_params: |
---|
2330 | continue |
---|
2331 | if item[0].GetValue(): |
---|
2332 | if not (item in self.param_toFit): |
---|
2333 | self.param_toFit.append(item) |
---|
2334 | else: |
---|
2335 | #remove parameters from the fitting list |
---|
2336 | if item in self.param_toFit: |
---|
2337 | self.param_toFit.remove(item) |
---|
2338 | |
---|
2339 | #Calculate num. of angle parameters |
---|
2340 | if self.data.__class__.__name__ =="Data2D": |
---|
2341 | len_orient_para = 0 |
---|
2342 | else: |
---|
2343 | len_orient_para = len(self.orientation_params) #assume even len |
---|
2344 | #Total num. of angle parameters |
---|
2345 | if len(self.fittable_param) > 0: |
---|
2346 | len_orient_para *= 2 |
---|
2347 | #Set the value of checkbox that selected every checkbox or not |
---|
2348 | if len(self.parameters)+len(self.fittable_param)-len_orient_para ==\ |
---|
2349 | len(self.param_toFit): |
---|
2350 | self.cb1.SetValue(True) |
---|
2351 | else: |
---|
2352 | self.cb1.SetValue(False) |
---|
2353 | self.save_current_state_fit() |
---|
2354 | if event !=None: |
---|
2355 | #self._undo.Enable(True) |
---|
2356 | ## post state to fit panel |
---|
2357 | event = PageInfoEvent(page = self) |
---|
2358 | wx.PostEvent(self.parent, event) |
---|
2359 | |
---|
2360 | def set_model_param_sizer(self, model): |
---|
2361 | """ |
---|
2362 | Build the panel from the model content |
---|
2363 | |
---|
2364 | :param model: the model selected in combo box for fitting purpose |
---|
2365 | |
---|
2366 | """ |
---|
2367 | self.sizer3.Clear(True) |
---|
2368 | self.parameters = [] |
---|
2369 | self.str_parameters = [] |
---|
2370 | self.param_toFit=[] |
---|
2371 | self.fittable_param=[] |
---|
2372 | self.fixed_param=[] |
---|
2373 | self.orientation_params=[] |
---|
2374 | self.orientation_params_disp=[] |
---|
2375 | |
---|
2376 | if model ==None: |
---|
2377 | self.sizer3.Layout() |
---|
2378 | self.SetupScrolling() |
---|
2379 | return |
---|
2380 | ## the panel is drawn using the current value of the fit engine |
---|
2381 | if self.engine_type==None and self._manager !=None: |
---|
2382 | self.engine_type= self._manager._return_engine_type() |
---|
2383 | |
---|
2384 | box_description= wx.StaticBox(self, -1,str("Model Parameters")) |
---|
2385 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
2386 | sizer = wx.GridBagSizer(5,5) |
---|
2387 | ## save the current model |
---|
2388 | self.model = model |
---|
2389 | |
---|
2390 | keys = self.model.getParamList() |
---|
2391 | #list of dispersion paramaters |
---|
2392 | self.disp_list=self.model.getDispParamList() |
---|
2393 | |
---|
2394 | def custom_compare(a,b): |
---|
2395 | """ |
---|
2396 | Custom compare to order, first by alphabets then second by number. |
---|
2397 | """ |
---|
2398 | # number at the last digit |
---|
2399 | a_last = a[len(a)-1] |
---|
2400 | b_last = b[len(b)-1] |
---|
2401 | # default |
---|
2402 | num_a = None |
---|
2403 | num_b = None |
---|
2404 | # split the names |
---|
2405 | a2 = a.lower().split('_') |
---|
2406 | b2 = b.lower().split('_') |
---|
2407 | # check length of a2, b2 |
---|
2408 | len_a2 = len(a2) |
---|
2409 | len_b2 = len(b2) |
---|
2410 | # check if it contains a int number(<10) |
---|
2411 | try: |
---|
2412 | num_a = int(a_last) |
---|
2413 | except: pass |
---|
2414 | try: |
---|
2415 | num_b = int(b_last) |
---|
2416 | except: pass |
---|
2417 | # Put 'scale' near the top; happens |
---|
2418 | # when numbered param name exists |
---|
2419 | if a == 'scale': |
---|
2420 | return -1 |
---|
2421 | # both have a number |
---|
2422 | if num_a != None and num_b != None: |
---|
2423 | if num_a > num_b: return -1 |
---|
2424 | # same number |
---|
2425 | elif num_a == num_b: |
---|
2426 | # different last names |
---|
2427 | if a2[len_a2-1] != b2[len_b2-1] and num_a != 0: |
---|
2428 | return -cmp(a2[len_a2-1], b2[len_b2-1]) |
---|
2429 | else: |
---|
2430 | return cmp(a, b) |
---|
2431 | else: return 1 |
---|
2432 | # one of them has a number |
---|
2433 | elif num_a != None: return 1 |
---|
2434 | elif num_b != None: return -1 |
---|
2435 | # no numbers |
---|
2436 | else: return cmp(a.lower(), b.lower()) |
---|
2437 | |
---|
2438 | |
---|
2439 | keys.sort(custom_compare) |
---|
2440 | |
---|
2441 | iy = 0 |
---|
2442 | ix = 0 |
---|
2443 | select_text = "Select All" |
---|
2444 | self.cb1 = wx.CheckBox(self, -1,str(select_text), (10, 10)) |
---|
2445 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
2446 | self.cb1.SetToolTipString("To check/uncheck all the boxes below.") |
---|
2447 | #self.cb1.SetValue(True) |
---|
2448 | |
---|
2449 | sizer.Add(self.cb1,(iy, ix),(1,1),\ |
---|
2450 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2451 | ix +=1 |
---|
2452 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
2453 | sizer.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
2454 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2455 | ix +=2 |
---|
2456 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
2457 | sizer.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
2458 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2459 | self.text2_3.Hide() |
---|
2460 | ix +=1 |
---|
2461 | self.text2_min = wx.StaticText(self, -1, 'Min') |
---|
2462 | sizer.Add(self.text2_min,(iy, ix),(1,1),\ |
---|
2463 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2464 | self.text2_min.Hide() |
---|
2465 | ix +=1 |
---|
2466 | self.text2_max = wx.StaticText(self, -1, 'Max') |
---|
2467 | sizer.Add(self.text2_max,(iy, ix),(1,1),\ |
---|
2468 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2469 | self.text2_max.Hide() |
---|
2470 | ix += 1 |
---|
2471 | self.text2_4 = wx.StaticText(self, -1, '[Units]') |
---|
2472 | sizer.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
2473 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2474 | self.text2_4.Hide() |
---|
2475 | if self.engine_type=="park": |
---|
2476 | self.text2_max.Show(True) |
---|
2477 | self.text2_min.Show(True) |
---|
2478 | |
---|
2479 | for item in keys: |
---|
2480 | if not item in self.disp_list and not item in \ |
---|
2481 | self.model.orientation_params: |
---|
2482 | |
---|
2483 | ##prepare a spot to store errors |
---|
2484 | if not self.model.details.has_key(item): |
---|
2485 | self.model.details [item] = ["",None,None] |
---|
2486 | |
---|
2487 | iy += 1 |
---|
2488 | ix = 0 |
---|
2489 | if self.model.__class__ in \ |
---|
2490 | self.model_list_box["Multi-Functions"] \ |
---|
2491 | and item in self.model.non_fittable: |
---|
2492 | non_fittable_name = wx.StaticText(self, -1, item ) |
---|
2493 | sizer.Add(non_fittable_name,(iy, ix),(1,1),\ |
---|
2494 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 21) |
---|
2495 | ## add parameter value |
---|
2496 | ix += 1 |
---|
2497 | value= self.model.getParam(item) |
---|
2498 | if len(self.model.fun_list) > 0: |
---|
2499 | num = item.split('_')[1][5:7] |
---|
2500 | fun_box = wx.ComboBox(self, -1,size=(100,-1), |
---|
2501 | style=wx.CB_READONLY, name = '%s'% item) |
---|
2502 | self._set_fun_box_list(fun_box) |
---|
2503 | fun_box.SetSelection(0) |
---|
2504 | #self.fun_box.SetToolTipString("A function |
---|
2505 | # describing the interface") |
---|
2506 | wx.EVT_COMBOBOX(fun_box,-1, self._on_fun_box) |
---|
2507 | else: |
---|
2508 | fun_box = self.ModelTextCtrl(self, -1, |
---|
2509 | size=(_BOX_WIDTH,20), |
---|
2510 | style=wx.TE_PROCESS_ENTER, name ='%s'% item) |
---|
2511 | fun_box.SetToolTipString("Hit 'Enter' after typing.") |
---|
2512 | fun_box.SetValue(format_number(value)) |
---|
2513 | sizer.Add(fun_box, (iy,ix),(1,1), wx.EXPAND) |
---|
2514 | self.str_parameters.append([None,item, fun_box, |
---|
2515 | None,None,None,None,None]) |
---|
2516 | |
---|
2517 | |
---|
2518 | else: |
---|
2519 | ## add parameters name with checkbox for selecting to fit |
---|
2520 | cb = wx.CheckBox(self, -1, item ) |
---|
2521 | cb.SetToolTipString(" Check for fitting.") |
---|
2522 | #cb.SetValue(True) |
---|
2523 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
2524 | |
---|
2525 | sizer.Add( cb,( iy, ix),(1,1), |
---|
2526 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2527 | |
---|
2528 | ## add parameter value |
---|
2529 | ix += 1 |
---|
2530 | value= self.model.getParam(item) |
---|
2531 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
2532 | style=wx.TE_PROCESS_ENTER) |
---|
2533 | ctl1.SetToolTipString("Hit 'Enter' after typing.") |
---|
2534 | ctl1.SetValue(format_number(value)) |
---|
2535 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
2536 | ## text to show error sign |
---|
2537 | ix += 1 |
---|
2538 | text2=wx.StaticText(self, -1, '+/-') |
---|
2539 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
2540 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2541 | text2.Hide() |
---|
2542 | ix += 1 |
---|
2543 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
2544 | sizer.Add(ctl2, (iy,ix),(1,1), |
---|
2545 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2546 | ctl2.Hide() |
---|
2547 | |
---|
2548 | ix += 1 |
---|
2549 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
2550 | style=wx.TE_PROCESS_ENTER, |
---|
2551 | text_enter_callback = self._onparamRangeEnter) |
---|
2552 | |
---|
2553 | sizer.Add(ctl3, (iy,ix),(1,1), |
---|
2554 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2555 | ctl3.Hide() |
---|
2556 | |
---|
2557 | ix += 1 |
---|
2558 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
2559 | style=wx.TE_PROCESS_ENTER, |
---|
2560 | text_enter_callback = self._onparamRangeEnter) |
---|
2561 | sizer.Add(ctl4, (iy,ix),(1,1), |
---|
2562 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2563 | |
---|
2564 | ctl4.Hide() |
---|
2565 | |
---|
2566 | if self.engine_type=="park": |
---|
2567 | ctl3.Show(True) |
---|
2568 | ctl4.Show(True) |
---|
2569 | ix +=1 |
---|
2570 | # Units |
---|
2571 | if self.model.details.has_key(item): |
---|
2572 | units = wx.StaticText(self, -1, |
---|
2573 | self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
2574 | else: |
---|
2575 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
2576 | sizer.Add(units, (iy,ix),(1,1), |
---|
2577 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2578 | |
---|
2579 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
2580 | self.parameters.append([cb,item, ctl1, |
---|
2581 | text2,ctl2, ctl3, ctl4,units]) |
---|
2582 | |
---|
2583 | iy+=1 |
---|
2584 | sizer.Add((10,10),(iy,ix),(1,1), |
---|
2585 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2586 | |
---|
2587 | # type can be either Guassian or Array |
---|
2588 | if len(self.model.dispersion.values())>0: |
---|
2589 | type= self.model.dispersion.values()[0]["type"] |
---|
2590 | else: |
---|
2591 | type = "Gaussian" |
---|
2592 | |
---|
2593 | iy += 1 |
---|
2594 | ix = 0 |
---|
2595 | #Add tile for orientational angle |
---|
2596 | for item in keys: |
---|
2597 | if item in self.model.orientation_params: |
---|
2598 | orient_angle = wx.StaticText(self, -1, '[For 2D only]:') |
---|
2599 | sizer.Add(orient_angle,(iy, ix),(1,1), |
---|
2600 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2601 | if not self.data.__class__.__name__ =="Data2D": |
---|
2602 | orient_angle.Hide() |
---|
2603 | else: |
---|
2604 | orient_angle.Show(True) |
---|
2605 | break |
---|
2606 | |
---|
2607 | #For Gaussian only |
---|
2608 | if type.lower() != "array": |
---|
2609 | for item in self.model.orientation_params: |
---|
2610 | if not item in self.disp_list: |
---|
2611 | ##prepare a spot to store min max |
---|
2612 | if not self.model.details.has_key(item): |
---|
2613 | self.model.details [item] = ["",None,None] |
---|
2614 | |
---|
2615 | iy += 1 |
---|
2616 | ix = 0 |
---|
2617 | ## add parameters name with checkbox for selecting to fit |
---|
2618 | cb = wx.CheckBox(self, -1, item ) |
---|
2619 | cb.SetValue(False) |
---|
2620 | cb.SetToolTipString("Check for fitting") |
---|
2621 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
2622 | if self.data.__class__.__name__ =="Data2D": |
---|
2623 | cb.Show(True) |
---|
2624 | else: |
---|
2625 | cb.Hide() |
---|
2626 | sizer.Add( cb,( iy, ix),(1,1), |
---|
2627 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2628 | |
---|
2629 | ## add parameter value |
---|
2630 | ix += 1 |
---|
2631 | value= self.model.getParam(item) |
---|
2632 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
2633 | style=wx.TE_PROCESS_ENTER) |
---|
2634 | ctl1.SetToolTipString("Hit 'Enter' after typing.") |
---|
2635 | ctl1.SetValue(format_number(value)) |
---|
2636 | if self.data.__class__.__name__ =="Data2D": |
---|
2637 | ctl1.Show(True) |
---|
2638 | else: |
---|
2639 | ctl1.Hide() |
---|
2640 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
2641 | ## text to show error sign |
---|
2642 | ix += 1 |
---|
2643 | text2=wx.StaticText(self, -1, '+/-') |
---|
2644 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
2645 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2646 | text2.Hide() |
---|
2647 | ix += 1 |
---|
2648 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
2649 | sizer.Add(ctl2, (iy,ix),(1,1), |
---|
2650 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2651 | ctl2.Hide() |
---|
2652 | |
---|
2653 | |
---|
2654 | ix += 1 |
---|
2655 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
2656 | style=wx.TE_PROCESS_ENTER, |
---|
2657 | text_enter_callback = self._onparamRangeEnter) |
---|
2658 | |
---|
2659 | sizer.Add(ctl3, (iy,ix),(1,1), |
---|
2660 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2661 | ctl3.Hide() |
---|
2662 | |
---|
2663 | ix += 1 |
---|
2664 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
2665 | style=wx.TE_PROCESS_ENTER, |
---|
2666 | text_enter_callback = self._onparamRangeEnter) |
---|
2667 | sizer.Add(ctl4, (iy,ix),(1,1), |
---|
2668 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2669 | |
---|
2670 | ctl4.Hide() |
---|
2671 | |
---|
2672 | if self.engine_type =="park" and \ |
---|
2673 | self.data.__class__.__name__ =="Data2D": |
---|
2674 | ctl3.Show(True) |
---|
2675 | ctl4.Show(True) |
---|
2676 | |
---|
2677 | ix +=1 |
---|
2678 | # Units |
---|
2679 | if self.model.details.has_key(item): |
---|
2680 | units = wx.StaticText(self, -1, |
---|
2681 | self.model.details[item][0], |
---|
2682 | style=wx.ALIGN_LEFT) |
---|
2683 | else: |
---|
2684 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
2685 | if self.data.__class__.__name__ =="Data2D": |
---|
2686 | units.Show(True) |
---|
2687 | |
---|
2688 | else: |
---|
2689 | units.Hide() |
---|
2690 | |
---|
2691 | sizer.Add(units, (iy,ix),(1,1), |
---|
2692 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2693 | |
---|
2694 | ##[cb state, name, value, "+/-", error of fit,min,max,units] |
---|
2695 | self.parameters.append([cb,item, ctl1, |
---|
2696 | text2,ctl2, ctl3, ctl4,units]) |
---|
2697 | self.orientation_params.append([cb,item, ctl1, |
---|
2698 | text2,ctl2, ctl3, ctl4,units]) |
---|
2699 | |
---|
2700 | iy+=1 |
---|
2701 | |
---|
2702 | #Display units text on panel |
---|
2703 | for item in keys: |
---|
2704 | if self.model.details.has_key(item): |
---|
2705 | self.text2_4.Show() |
---|
2706 | #Fill the list of fittable parameters |
---|
2707 | self.select_all_param(event=None) |
---|
2708 | |
---|
2709 | self.save_current_state_fit() |
---|
2710 | boxsizer1.Add(sizer) |
---|
2711 | self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
2712 | self.sizer3.Layout() |
---|
2713 | self.Layout() |
---|
2714 | self.Refresh() |
---|
2715 | |
---|
2716 | def _onModel2D(self, event): |
---|
2717 | """ |
---|
2718 | toggle view of model from 1D to 2D or 2D from 1D |
---|
2719 | """ |
---|
2720 | if self.model_view.GetLabelText() == "Switch to 2D": |
---|
2721 | self.model_view.SetLabel("Switch to 1D") |
---|
2722 | self.enable2D = True |
---|
2723 | else: |
---|
2724 | self.model_view.SetLabel("Switch to 2D") |
---|
2725 | self.enable2D = False |
---|
2726 | self._draw_model() |
---|
2727 | |
---|
2728 | class BGTextCtrl(wx.TextCtrl): |
---|
2729 | """ |
---|
2730 | Text control used to display outputs. |
---|
2731 | No editing allowed. The background is |
---|
2732 | grayed out. User can't select text. |
---|
2733 | """ |
---|
2734 | def __init__(self, *args, **kwds): |
---|
2735 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
2736 | self.SetEditable(False) |
---|
2737 | self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) |
---|
2738 | |
---|
2739 | # Bind to mouse event to avoid text highlighting |
---|
2740 | # The event will be skipped once the call-back |
---|
2741 | # is called. |
---|
2742 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
2743 | |
---|
2744 | def _click(self, event): |
---|
2745 | """ |
---|
2746 | Prevent further handling of the mouse event |
---|
2747 | by not calling Skip(). |
---|
2748 | """ |
---|
2749 | pass |
---|
2750 | |
---|