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