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