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