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 = False |
---|
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 | if not copy_flag: |
---|
1258 | self.disable_smearer.SetValue(True) |
---|
1259 | self.enable_smearer.SetValue(False) |
---|
1260 | |
---|
1261 | # more disables for 2D |
---|
1262 | self._set_smear_buttons() |
---|
1263 | |
---|
1264 | try: |
---|
1265 | # update smearer sizer |
---|
1266 | #if not self.enable_smearer.GetValue(): |
---|
1267 | # self.disable_smearer.SetValue(True) |
---|
1268 | self.onSmear(None) |
---|
1269 | temp_smear = None |
---|
1270 | if not self.disable_smearer.GetValue(): |
---|
1271 | # Set the smearer environments |
---|
1272 | temp_smear = self.current_smearer |
---|
1273 | except: |
---|
1274 | raise |
---|
1275 | ## error occured on chisqr computation |
---|
1276 | #pass |
---|
1277 | ## event to post model to fit to fitting plugins |
---|
1278 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
1279 | |
---|
1280 | ## set smearing value whether or not |
---|
1281 | # the data contain the smearing info |
---|
1282 | evt = ModelEventbox(model=self.model, |
---|
1283 | smearer=temp_smear, |
---|
1284 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
1285 | qmin=float(self.qmin_x), |
---|
1286 | uid=self.uid, |
---|
1287 | caption=self.window_caption, |
---|
1288 | qmax=float(self.qmax_x)) |
---|
1289 | |
---|
1290 | self._manager._on_model_panel(evt=evt) |
---|
1291 | self.mbox_description.SetLabel("Model [ %s ]" % str(self.model.name)) |
---|
1292 | self.mbox_description.SetForegroundColour(wx.BLUE) |
---|
1293 | self.state.model = self.model.clone() |
---|
1294 | self.state.model.name = self.model.name |
---|
1295 | |
---|
1296 | |
---|
1297 | if event != None: |
---|
1298 | ## post state to fit panel |
---|
1299 | new_event = PageInfoEvent(page = self) |
---|
1300 | wx.PostEvent(self.parent, new_event) |
---|
1301 | #update list of plugins if new plugin is available |
---|
1302 | if self.plugin_rbutton.GetValue(): |
---|
1303 | temp = self.parent.update_model_list() |
---|
1304 | if temp: |
---|
1305 | self.model_list_box = temp |
---|
1306 | current_val = self.formfactorbox.GetValue() |
---|
1307 | pos = self.formfactorbox.GetSelection() |
---|
1308 | self._show_combox_helper() |
---|
1309 | self.formfactorbox.SetSelection(pos) |
---|
1310 | self.formfactorbox.SetValue(current_val) |
---|
1311 | # when select a model only from guictr/button |
---|
1312 | if is_poly_enabled != None: |
---|
1313 | self.enable_disp.SetValue(is_poly_enabled) |
---|
1314 | self.disable_disp.SetValue(not is_poly_enabled) |
---|
1315 | self._set_dipers_Param(event=None) |
---|
1316 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
1317 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
1318 | |
---|
1319 | # Keep the previous param values |
---|
1320 | if copy_flag: |
---|
1321 | self.get_paste_params(copy_flag) |
---|
1322 | wx.CallAfter(self._onDraw, None) |
---|
1323 | |
---|
1324 | else: |
---|
1325 | self._draw_model() |
---|
1326 | |
---|
1327 | if self.batch_on: |
---|
1328 | self.slit_smearer.Enable(False) |
---|
1329 | self.pinhole_smearer.Enable(False) |
---|
1330 | self.btEditMask.Disable() |
---|
1331 | self.EditMask_title.Disable() |
---|
1332 | |
---|
1333 | self.Show(True) |
---|
1334 | self.SetupScrolling() |
---|
1335 | |
---|
1336 | def _onparamEnter(self,event): |
---|
1337 | """ |
---|
1338 | when enter value on panel redraw model according to changed |
---|
1339 | """ |
---|
1340 | if self.model ==None: |
---|
1341 | msg="Please select a Model first..." |
---|
1342 | wx.MessageBox(msg, 'Info') |
---|
1343 | #wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
1344 | # "Parameters: %s"%msg)) |
---|
1345 | return |
---|
1346 | |
---|
1347 | #default flag |
---|
1348 | flag = False |
---|
1349 | self.fitrange = True |
---|
1350 | #get event object |
---|
1351 | tcrtl = event.GetEventObject() |
---|
1352 | |
---|
1353 | #wx.PostEvent(self._manager.parent, StatusEvent(status=" \ |
---|
1354 | # updating ... ",type="update")) |
---|
1355 | #Clear msg if previously shown. |
---|
1356 | msg= "" |
---|
1357 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1358 | |
---|
1359 | if check_float(tcrtl): |
---|
1360 | flag = self._onparamEnter_helper() |
---|
1361 | self.set_npts2fit() |
---|
1362 | if self.fitrange: |
---|
1363 | temp_smearer = None |
---|
1364 | if not self.disable_smearer.GetValue(): |
---|
1365 | temp_smearer= self.current_smearer |
---|
1366 | ## set smearing value whether or not |
---|
1367 | # the data contain the smearing info |
---|
1368 | if self.slit_smearer.GetValue(): |
---|
1369 | flag1 = self.update_slit_smear() |
---|
1370 | flag = flag or flag1 |
---|
1371 | elif self.pinhole_smearer.GetValue(): |
---|
1372 | flag1 = self.update_pinhole_smear() |
---|
1373 | flag = flag or flag1 |
---|
1374 | elif self.data.__class__.__name__ != "Data2D" and \ |
---|
1375 | not self.enable2D: |
---|
1376 | self._manager.set_smearer(smearer=temp_smearer, |
---|
1377 | fid=self.data.id, |
---|
1378 | uid=self.uid, |
---|
1379 | qmin= float(self.qmin_x), |
---|
1380 | qmax= float(self.qmax_x), |
---|
1381 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
1382 | draw=True) |
---|
1383 | if flag: |
---|
1384 | #self.compute_chisqr(smearer= temp_smearer) |
---|
1385 | |
---|
1386 | ## new state posted |
---|
1387 | if self.state_change: |
---|
1388 | #self._undo.Enable(True) |
---|
1389 | event = PageInfoEvent(page = self) |
---|
1390 | wx.PostEvent(self.parent, event) |
---|
1391 | self.state_change= False |
---|
1392 | else: |
---|
1393 | # invalid fit range: do nothing here: |
---|
1394 | # msg already displayed in validate |
---|
1395 | return |
---|
1396 | else: |
---|
1397 | self.save_current_state() |
---|
1398 | msg= "Cannot Plot :Must enter a number!!! " |
---|
1399 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1400 | |
---|
1401 | self.save_current_state() |
---|
1402 | return |
---|
1403 | |
---|
1404 | def _onparamRangeEnter(self, event): |
---|
1405 | """ |
---|
1406 | Check validity of value enter in the parameters range field |
---|
1407 | """ |
---|
1408 | #if self.check_invalid_panel(): |
---|
1409 | # return |
---|
1410 | tcrtl= event.GetEventObject() |
---|
1411 | #Clear msg if previously shown. |
---|
1412 | msg= "" |
---|
1413 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1414 | # Flag to register when a parameter has changed. |
---|
1415 | is_modified = False |
---|
1416 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
1417 | try: |
---|
1418 | value = float(tcrtl.GetValue()) |
---|
1419 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1420 | self._check_value_enter(self.fittable_param ,is_modified) |
---|
1421 | self._check_value_enter(self.parameters ,is_modified) |
---|
1422 | except: |
---|
1423 | tcrtl.SetBackgroundColour("pink") |
---|
1424 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1425 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1426 | return |
---|
1427 | else: |
---|
1428 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1429 | |
---|
1430 | #self._undo.Enable(True) |
---|
1431 | self.save_current_state() |
---|
1432 | event = PageInfoEvent(page = self) |
---|
1433 | wx.PostEvent(self.parent, event) |
---|
1434 | self.state_change= False |
---|
1435 | |
---|
1436 | |
---|
1437 | def _onQrangeEnter(self, event): |
---|
1438 | """ |
---|
1439 | Check validity of value enter in the Q range field |
---|
1440 | """ |
---|
1441 | tcrtl = event.GetEventObject() |
---|
1442 | #Clear msg if previously shown. |
---|
1443 | msg= "" |
---|
1444 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1445 | # Flag to register when a parameter has changed. |
---|
1446 | is_modified = False |
---|
1447 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
1448 | try: |
---|
1449 | value = float(tcrtl.GetValue()) |
---|
1450 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1451 | |
---|
1452 | # If qmin and qmax have been modified, update qmin and qmax |
---|
1453 | if self._validate_qrange( self.qmin, self.qmax): |
---|
1454 | tempmin = float(self.qmin.GetValue()) |
---|
1455 | if tempmin != self.qmin_x: |
---|
1456 | self.qmin_x = tempmin |
---|
1457 | tempmax = float(self.qmax.GetValue()) |
---|
1458 | if tempmax != self.qmax_x: |
---|
1459 | self.qmax_x = tempmax |
---|
1460 | else: |
---|
1461 | tcrtl.SetBackgroundColour("pink") |
---|
1462 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1463 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1464 | return |
---|
1465 | |
---|
1466 | except: |
---|
1467 | tcrtl.SetBackgroundColour("pink") |
---|
1468 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1469 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1470 | return |
---|
1471 | #Check if # of points for theory model are valid(>0). |
---|
1472 | # check for 2d |
---|
1473 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
1474 | self.enable2D: |
---|
1475 | # set mask |
---|
1476 | radius= numpy.sqrt(self.data.qx_data*self.data.qx_data + |
---|
1477 | self.data.qy_data*self.data.qy_data ) |
---|
1478 | index_data = ((self.qmin_x <= radius)& \ |
---|
1479 | (radius<= self.qmax_x)) |
---|
1480 | index_data = (index_data)&(self.data.mask) |
---|
1481 | index_data = (index_data)&(numpy.isfinite(self.data.data)) |
---|
1482 | if len(index_data[index_data]) < 10: |
---|
1483 | msg = "Cannot Plot :No or too little npts in" |
---|
1484 | msg += " that data range!!! " |
---|
1485 | wx.PostEvent(self.parent.parent, |
---|
1486 | StatusEvent(status=msg)) |
---|
1487 | return |
---|
1488 | else: |
---|
1489 | #self.data.mask = index_data |
---|
1490 | #self.Npts_fit.SetValue(str(len(self.data.mask))) |
---|
1491 | self.set_npts2fit() |
---|
1492 | else: |
---|
1493 | index_data = ((self.qmin_x <= self.data.x)& \ |
---|
1494 | (self.data.x <= self.qmax_x)) |
---|
1495 | self.Npts_fit.SetValue(str(len(self.data.x[index_data]))) |
---|
1496 | |
---|
1497 | self.npts_x = self.Npts_total.GetValue() |
---|
1498 | self.create_default_data() |
---|
1499 | self._save_plotting_range() |
---|
1500 | else: |
---|
1501 | tcrtl.SetBackgroundColour("pink") |
---|
1502 | msg= "Model Error:wrong value entered!!!" |
---|
1503 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1504 | |
---|
1505 | self._draw_model() |
---|
1506 | self.save_current_state() |
---|
1507 | event = PageInfoEvent(page = self) |
---|
1508 | wx.PostEvent(self.parent, event) |
---|
1509 | self.state_change= False |
---|
1510 | return |
---|
1511 | |
---|
1512 | def _clear_Err_on_Fit(self): |
---|
1513 | """ |
---|
1514 | hide the error text control shown |
---|
1515 | after fitting |
---|
1516 | """ |
---|
1517 | if self.is_mac: |
---|
1518 | return |
---|
1519 | if hasattr(self,"text2_3"): |
---|
1520 | self.text2_3.Hide() |
---|
1521 | |
---|
1522 | if len(self.parameters)>0: |
---|
1523 | for item in self.parameters: |
---|
1524 | #Skip t ifhe angle parameters if 1D data |
---|
1525 | if self.data.__class__.__name__ != "Data2D" and \ |
---|
1526 | not self.enable2D: |
---|
1527 | if item in self.orientation_params: |
---|
1528 | continue |
---|
1529 | if item in self.param_toFit: |
---|
1530 | continue |
---|
1531 | ## hide statictext +/- |
---|
1532 | if len(item) < 4 : |
---|
1533 | continue |
---|
1534 | if item[3]!=None and item[3].IsShown(): |
---|
1535 | item[3].Hide() |
---|
1536 | ## hide textcrtl for error after fit |
---|
1537 | if item[4]!=None and item[4].IsShown(): |
---|
1538 | item[4].Hide() |
---|
1539 | |
---|
1540 | if len(self.fittable_param)>0: |
---|
1541 | for item in self.fittable_param: |
---|
1542 | #Skip t ifhe angle parameters if 1D data |
---|
1543 | if self.data.__class__.__name__ != "Data2D" and \ |
---|
1544 | not self.enable2D: |
---|
1545 | if item in self.orientation_params: |
---|
1546 | continue |
---|
1547 | if item in self.param_toFit: |
---|
1548 | continue |
---|
1549 | if len(item) < 4 : |
---|
1550 | continue |
---|
1551 | ## hide statictext +/- |
---|
1552 | if item[3]!=None and item[3].IsShown(): |
---|
1553 | item[3].Hide() |
---|
1554 | ## hide textcrtl for error after fit |
---|
1555 | if item[4]!=None and item[4].IsShown(): |
---|
1556 | item[4].Hide() |
---|
1557 | return |
---|
1558 | |
---|
1559 | def _get_defult_custom_smear(self): |
---|
1560 | """ |
---|
1561 | Get the defult values for custum smearing. |
---|
1562 | """ |
---|
1563 | # get the default values |
---|
1564 | if self.dxl == None: self.dxl = 0.0 |
---|
1565 | if self.dxw == None: self.dxw = "" |
---|
1566 | if self.dx_min == None: self.dx_min = SMEAR_SIZE_L |
---|
1567 | if self.dx_max == None: self.dx_max = SMEAR_SIZE_H |
---|
1568 | |
---|
1569 | def _get_smear_info(self): |
---|
1570 | """ |
---|
1571 | Get the smear info from data. |
---|
1572 | |
---|
1573 | :return: self.smear_type, self.dq_l and self.dq_r, |
---|
1574 | respectively the type of the smear, dq_min and |
---|
1575 | dq_max for pinhole smear data |
---|
1576 | while dxl and dxw for slit smear |
---|
1577 | |
---|
1578 | """ |
---|
1579 | |
---|
1580 | # default |
---|
1581 | self.smear_type = None |
---|
1582 | self.dq_l = None |
---|
1583 | self.dq_r = None |
---|
1584 | data = self.data |
---|
1585 | if self.data is None: |
---|
1586 | return |
---|
1587 | elif self.data.__class__.__name__ == "Data2D" or \ |
---|
1588 | self.enable2D: |
---|
1589 | if data.dqx_data == None or data.dqy_data ==None: |
---|
1590 | return |
---|
1591 | elif self.current_smearer != None and (data.dqx_data.any()!=0) and \ |
---|
1592 | (data.dqx_data.any()!=0): |
---|
1593 | self.smear_type = "Pinhole2d" |
---|
1594 | self.dq_l = format_number(numpy.average(data.dqx_data)) |
---|
1595 | self.dq_r = format_number(numpy.average(data.dqy_data)) |
---|
1596 | return |
---|
1597 | else: |
---|
1598 | return |
---|
1599 | # check if it is pinhole smear and get min max if it is. |
---|
1600 | if data.dx != None and all(data.dx !=0): |
---|
1601 | self.smear_type = "Pinhole" |
---|
1602 | self.dq_l = data.dx[0] |
---|
1603 | self.dq_r = data.dx[-1] |
---|
1604 | |
---|
1605 | # check if it is slit smear and get min max if it is. |
---|
1606 | elif data.dxl != None or data.dxw != None: |
---|
1607 | self.smear_type = "Slit" |
---|
1608 | if data.dxl != None and all(data.dxl !=0): |
---|
1609 | self.dq_l = data.dxl[0] |
---|
1610 | if data.dxw != None and all(data.dxw !=0): |
---|
1611 | self.dq_r = data.dxw[0] |
---|
1612 | #return self.smear_type,self.dq_l,self.dq_r |
---|
1613 | |
---|
1614 | def _show_smear_sizer(self): |
---|
1615 | """ |
---|
1616 | Show only the sizers depending on smear selection |
---|
1617 | """ |
---|
1618 | # smear disabled |
---|
1619 | if self.disable_smearer.GetValue(): |
---|
1620 | self.smear_description_none.Show(True) |
---|
1621 | # 2Dsmear |
---|
1622 | elif self._is_2D(): |
---|
1623 | self.smear_description_accuracy_type.Show(True) |
---|
1624 | self.smear_accuracy.Show(True) |
---|
1625 | self.smear_description_accuracy_type.Show(True) |
---|
1626 | self.smear_description_2d.Show(True) |
---|
1627 | self.smear_description_2d_x.Show(True) |
---|
1628 | self.smear_description_2d_y.Show(True) |
---|
1629 | if self.pinhole_smearer.GetValue(): |
---|
1630 | self.smear_pinhole_min.Show(True) |
---|
1631 | self.smear_pinhole_max.Show(True) |
---|
1632 | # smear from data |
---|
1633 | elif self.enable_smearer.GetValue(): |
---|
1634 | |
---|
1635 | self.smear_description_dqdata.Show(True) |
---|
1636 | if self.smear_type != None: |
---|
1637 | self.smear_description_smear_type.Show(True) |
---|
1638 | if self.smear_type == 'Slit': |
---|
1639 | self.smear_description_slit_height.Show(True) |
---|
1640 | self.smear_description_slit_width.Show(True) |
---|
1641 | elif self.smear_type == 'Pinhole': |
---|
1642 | self.smear_description_pin_min.Show(True) |
---|
1643 | self.smear_description_pin_max.Show(True) |
---|
1644 | self.smear_description_smear_type.Show(True) |
---|
1645 | self.smear_description_type.Show(True) |
---|
1646 | self.smear_data_left.Show(True) |
---|
1647 | self.smear_data_right.Show(True) |
---|
1648 | # custom pinhole smear |
---|
1649 | elif self.pinhole_smearer.GetValue(): |
---|
1650 | if self.smear_type == 'Pinhole': |
---|
1651 | self.smear_message_new_p.Show(True) |
---|
1652 | self.smear_description_pin_min.Show(True) |
---|
1653 | self.smear_description_pin_max.Show(True) |
---|
1654 | |
---|
1655 | self.smear_pinhole_min.Show(True) |
---|
1656 | self.smear_pinhole_max.Show(True) |
---|
1657 | # custom slit smear |
---|
1658 | elif self.slit_smearer.GetValue(): |
---|
1659 | self.smear_message_new_s.Show(True) |
---|
1660 | self.smear_description_slit_height.Show(True) |
---|
1661 | self.smear_slit_height.Show(True) |
---|
1662 | self.smear_description_slit_width.Show(True) |
---|
1663 | self.smear_slit_width.Show(True) |
---|
1664 | |
---|
1665 | def _hide_all_smear_info(self): |
---|
1666 | """ |
---|
1667 | Hide all smearing messages in the set_smearer sizer |
---|
1668 | """ |
---|
1669 | self.smear_description_none.Hide() |
---|
1670 | self.smear_description_dqdata.Hide() |
---|
1671 | self.smear_description_type.Hide() |
---|
1672 | self.smear_description_smear_type.Hide() |
---|
1673 | self.smear_description_accuracy_type.Hide() |
---|
1674 | self.smear_description_2d_x.Hide() |
---|
1675 | self.smear_description_2d_y.Hide() |
---|
1676 | self.smear_description_2d.Hide() |
---|
1677 | |
---|
1678 | self.smear_accuracy.Hide() |
---|
1679 | self.smear_data_left.Hide() |
---|
1680 | self.smear_data_right.Hide() |
---|
1681 | self.smear_description_pin_min.Hide() |
---|
1682 | self.smear_pinhole_min.Hide() |
---|
1683 | self.smear_description_pin_max.Hide() |
---|
1684 | self.smear_pinhole_max.Hide() |
---|
1685 | self.smear_description_slit_height.Hide() |
---|
1686 | self.smear_slit_height.Hide() |
---|
1687 | self.smear_description_slit_width.Hide() |
---|
1688 | self.smear_slit_width.Hide() |
---|
1689 | self.smear_message_new_p.Hide() |
---|
1690 | self.smear_message_new_s.Hide() |
---|
1691 | |
---|
1692 | def _set_accuracy_list(self): |
---|
1693 | """ |
---|
1694 | Set the list of an accuracy in 2D custum smear: Xhigh, High, Med, or Low |
---|
1695 | """ |
---|
1696 | # list of accuracy choices |
---|
1697 | list = ['Low','Med','High','Xhigh'] |
---|
1698 | for idx in range(len(list)): |
---|
1699 | self.smear_accuracy.Append(list[idx],idx) |
---|
1700 | |
---|
1701 | def _set_fun_box_list(self,fun_box): |
---|
1702 | """ |
---|
1703 | Set the list of func for multifunctional models |
---|
1704 | """ |
---|
1705 | # Check if it is multi_functional model |
---|
1706 | if self.model.__class__ not in self.model_list_box["Multi-Functions"] \ |
---|
1707 | and not self.temp_multi_functional: |
---|
1708 | return None |
---|
1709 | # Get the func name list |
---|
1710 | list = self.model.fun_list |
---|
1711 | if len(list) == 0: |
---|
1712 | return None |
---|
1713 | # build function (combo)box |
---|
1714 | ind = 0 |
---|
1715 | while(ind < len(list)): |
---|
1716 | for key, val in list.iteritems(): |
---|
1717 | if (val == ind): |
---|
1718 | fun_box.Append(key,val) |
---|
1719 | break |
---|
1720 | ind += 1 |
---|
1721 | |
---|
1722 | def _on_select_accuracy(self,event): |
---|
1723 | """ |
---|
1724 | Select an accuracy in 2D custom smear: Xhigh, High, Med, or Low |
---|
1725 | """ |
---|
1726 | #event.Skip() |
---|
1727 | # Check if the accuracy is same as before |
---|
1728 | #self.smear2d_accuracy = event.GetEventObject().GetValue() |
---|
1729 | self.smear2d_accuracy = self.smear_accuracy.GetValue() |
---|
1730 | if self.pinhole_smearer.GetValue(): |
---|
1731 | self.onPinholeSmear(event=None) |
---|
1732 | else: |
---|
1733 | self.onSmear(event=None) |
---|
1734 | if self.current_smearer != None: |
---|
1735 | self.current_smearer.set_accuracy(accuracy = |
---|
1736 | self.smear2d_accuracy) |
---|
1737 | event.Skip() |
---|
1738 | |
---|
1739 | def _on_fun_box(self,event): |
---|
1740 | """ |
---|
1741 | Select an func: Erf,Rparabola,LParabola |
---|
1742 | """ |
---|
1743 | fun_val = None |
---|
1744 | fun_box = event.GetEventObject() |
---|
1745 | name = fun_box.Name |
---|
1746 | value = fun_box.GetValue() |
---|
1747 | if self.model.fun_list.has_key(value): |
---|
1748 | fun_val = self.model.fun_list[value] |
---|
1749 | |
---|
1750 | self.model.setParam(name,fun_val) |
---|
1751 | # save state |
---|
1752 | self._copy_parameters_state(self.str_parameters, |
---|
1753 | self.state.str_parameters) |
---|
1754 | # update params |
---|
1755 | self._update_paramv_on_fit() |
---|
1756 | # draw |
---|
1757 | self._draw_model() |
---|
1758 | self.Refresh() |
---|
1759 | # get ready for new event |
---|
1760 | event.Skip() |
---|
1761 | |
---|
1762 | def _onMask(self, event): |
---|
1763 | """ |
---|
1764 | Build a panel to allow to edit Mask |
---|
1765 | """ |
---|
1766 | |
---|
1767 | from sans.guiframe.local_perspectives.plotting.masking \ |
---|
1768 | import MaskPanel as MaskDialog |
---|
1769 | |
---|
1770 | self.panel = MaskDialog(base=self, data=self.data, id=wx.NewId()) |
---|
1771 | #self.panel.Bind(wx.EVT_CLOSE, self._draw_masked_model) |
---|
1772 | self.panel.ShowModal() |
---|
1773 | #wx.PostEvent(self.parent, event) |
---|
1774 | |
---|
1775 | def _draw_masked_model(self, event): |
---|
1776 | """ |
---|
1777 | Draw model image w/mask |
---|
1778 | """ |
---|
1779 | #event.Skip() |
---|
1780 | is_valid_qrange = self._update_paramv_on_fit() |
---|
1781 | |
---|
1782 | if is_valid_qrange and self.model != None: |
---|
1783 | #self.panel.Show(0) |
---|
1784 | #self.panel.Destroy() # frame |
---|
1785 | self.panel.MakeModal(False) |
---|
1786 | event.Skip() |
---|
1787 | # try re draw the model plot if it exists |
---|
1788 | self._draw_model() |
---|
1789 | self.set_npts2fit() |
---|
1790 | elif self.model == None: |
---|
1791 | #self.panel.Show(0) |
---|
1792 | #self.panel.Destroy() # frame |
---|
1793 | self.panel.MakeModal(False) |
---|
1794 | event.Skip() |
---|
1795 | self.set_npts2fit() |
---|
1796 | msg= "No model is found on updating MASK in the model plot... " |
---|
1797 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1798 | else: |
---|
1799 | event.Skip() |
---|
1800 | msg = ' Please consider your Q range, too.' |
---|
1801 | self.panel.ShowMessage(msg) |
---|
1802 | |
---|
1803 | |
---|
1804 | def _set_smear(self, data): |
---|
1805 | """ |
---|
1806 | """ |
---|
1807 | if data is None: |
---|
1808 | return |
---|
1809 | self.current_smearer = smear_selection(data, self.model) |
---|
1810 | flag = self.disable_smearer.GetValue() |
---|
1811 | self.disable_smearer.SetValue(flag) |
---|
1812 | if self.current_smearer == None: |
---|
1813 | self.enable_smearer.Disable() |
---|
1814 | else: |
---|
1815 | self.enable_smearer.Enable() |
---|
1816 | if not flag: |
---|
1817 | self.onSmear(None) |
---|
1818 | |
---|
1819 | def _mac_sleep(self, sec=0.2): |
---|
1820 | """ |
---|
1821 | Give sleep to MAC |
---|
1822 | """ |
---|
1823 | if self.is_mac: |
---|
1824 | time.sleep(sec) |
---|
1825 | |
---|
1826 | def get_view_mode(self): |
---|
1827 | """ |
---|
1828 | return True if the panel allow 2D or False if 1D |
---|
1829 | """ |
---|
1830 | return self.enable2D |
---|
1831 | |
---|
1832 | def compute_data_set_range(self, data_list): |
---|
1833 | """ |
---|
1834 | find the range that include all data in the set |
---|
1835 | return the minimum and the maximum values |
---|
1836 | """ |
---|
1837 | if data_list is not None and data_list != []: |
---|
1838 | for data in data_list: |
---|
1839 | qmin, qmax, npts = self.compute_data_range(data) |
---|
1840 | self.qmin_data_set = min(self.qmin_data_set, qmin) |
---|
1841 | self.qmax_data_set = max(self.qmax_data_set, qmax) |
---|
1842 | self.npts_data_set += npts |
---|
1843 | return self.qmin_data_set, self.qmax_data_set, self.npts_data_set |
---|
1844 | |
---|
1845 | def compute_data_range(self, data): |
---|
1846 | """ |
---|
1847 | compute the minimum and the maximum range of the data |
---|
1848 | return the npts contains in data |
---|
1849 | :param data: |
---|
1850 | """ |
---|
1851 | qmin, qmax, npts = None, None, None |
---|
1852 | if data is not None: |
---|
1853 | if not hasattr(data,"data"): |
---|
1854 | qmin = min(data.x) |
---|
1855 | # Maximum value of data |
---|
1856 | qmax = max(data.x) |
---|
1857 | npts = len(data.x) |
---|
1858 | else: |
---|
1859 | qmin = 0 |
---|
1860 | x = max(math.fabs(data.xmin), math.fabs(data.xmax)) |
---|
1861 | y = max(math.fabs(data.ymin), math.fabs(data.ymax)) |
---|
1862 | ## Maximum value of data |
---|
1863 | qmax = math.sqrt(x*x + y*y) |
---|
1864 | npts = len(data.data) |
---|
1865 | return qmin, qmax, npts |
---|
1866 | |
---|
1867 | |
---|
1868 | def set_data(self, data): |
---|
1869 | """ |
---|
1870 | reset the current data |
---|
1871 | """ |
---|
1872 | from sans.guiframe.dataFitting import check_data_validity |
---|
1873 | id = None |
---|
1874 | flag = False |
---|
1875 | is_data = False |
---|
1876 | if self.data is not None: |
---|
1877 | is_data = check_data_validity(self.data) |
---|
1878 | if not is_data and data is not None: |
---|
1879 | flag = True |
---|
1880 | if data is not None: |
---|
1881 | id = data.id |
---|
1882 | if is_data: |
---|
1883 | self.graph_id = self.data.group_id |
---|
1884 | flag = (data.id != self.data.id) |
---|
1885 | self.data = data |
---|
1886 | if check_data_validity(data): |
---|
1887 | self.graph_id = data.group_id |
---|
1888 | self.data.group_id = self.graph_id |
---|
1889 | |
---|
1890 | if self.data is None: |
---|
1891 | data_name = "" |
---|
1892 | self._set_bookmark_flag(False) |
---|
1893 | self._keep.Enable(False) |
---|
1894 | self._set_save_flag(False) |
---|
1895 | else: |
---|
1896 | if self.model != None: |
---|
1897 | self._set_bookmark_flag(True) |
---|
1898 | self._keep.Enable(True) |
---|
1899 | |
---|
1900 | self._set_save_flag(True) |
---|
1901 | self._set_preview_flag(True) |
---|
1902 | |
---|
1903 | self._set_smear(data) |
---|
1904 | # more disables for 2D |
---|
1905 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
1906 | self.enable2D: |
---|
1907 | self.slit_smearer.Disable() |
---|
1908 | self.pinhole_smearer.Enable(True) |
---|
1909 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
1910 | if self.data.err_data == None or\ |
---|
1911 | (self.data.err_data == 1).all() or\ |
---|
1912 | (self.data.err_data == 0).all(): |
---|
1913 | self.dI_didata.Enable(False) |
---|
1914 | self.dI_noweight.SetValue(True) |
---|
1915 | self.weightbt_string = self.dI_noweight.GetLabelText() |
---|
1916 | else: |
---|
1917 | self.dI_didata.Enable(True) |
---|
1918 | self.dI_didata.SetValue(True) |
---|
1919 | self.weightbt_string = self.dI_didata.GetLabelText() |
---|
1920 | else: |
---|
1921 | self.slit_smearer.Enable(True) |
---|
1922 | self.pinhole_smearer.Enable(True) |
---|
1923 | if self.data.dy == None or\ |
---|
1924 | (self.data.dy == 1).all() or\ |
---|
1925 | (self.data.dy == 0).all(): |
---|
1926 | self.dI_didata.Enable(False) |
---|
1927 | self.dI_noweight.SetValue(True) |
---|
1928 | self.weightbt_string = self.dI_noweight.GetLabelText() |
---|
1929 | else: |
---|
1930 | self.dI_didata.Enable(True) |
---|
1931 | self.dI_didata.SetValue(True) |
---|
1932 | self.weightbt_string = self.dI_didata.GetLabelText() |
---|
1933 | # Enable weighting radio uttons |
---|
1934 | self.dI_noweight.Enable(True) |
---|
1935 | self.dI_sqrdata.Enable(True) |
---|
1936 | self.dI_idata.Enable(True) |
---|
1937 | |
---|
1938 | self.formfactorbox.Enable() |
---|
1939 | self.structurebox.Enable() |
---|
1940 | data_name = self.data.name |
---|
1941 | _, _, npts = self.compute_data_range(self.data) |
---|
1942 | #set maximum range for x in linear scale |
---|
1943 | if not hasattr(self.data, "data"): #Display only for 1D data fit |
---|
1944 | self.btEditMask.Disable() |
---|
1945 | self.EditMask_title.Disable() |
---|
1946 | else: |
---|
1947 | self.btEditMask.Enable() |
---|
1948 | self.EditMask_title.Enable() |
---|
1949 | |
---|
1950 | self.Npts_total.SetValue(str(npts)) |
---|
1951 | #default:number of data points selected to fit |
---|
1952 | self.Npts_fit.SetValue(str(npts)) |
---|
1953 | self.Npts_total.SetEditable(False) |
---|
1954 | self.Npts_total.SetBackgroundColour(\ |
---|
1955 | self.GetParent().GetBackgroundColour()) |
---|
1956 | |
---|
1957 | self.Npts_total.Bind(wx.EVT_MOUSE_EVENTS, self._npts_click) |
---|
1958 | self.dataSource.SetValue(data_name) |
---|
1959 | self.state.data = data |
---|
1960 | self.enable_fit_button() |
---|
1961 | # send graph_id to page_finder |
---|
1962 | self._manager.set_graph_id(uid=self.uid, graph_id=self.graph_id) |
---|
1963 | #focus the page |
---|
1964 | if check_data_validity(data): |
---|
1965 | self.data_box_description.SetForegroundColour(wx.BLUE) |
---|
1966 | |
---|
1967 | if self.batch_on: |
---|
1968 | self.slit_smearer.Enable(False) |
---|
1969 | self.pinhole_smearer.Enable(False) |
---|
1970 | self.btEditMask.Disable() |
---|
1971 | self.EditMask_title.Disable() |
---|
1972 | |
---|
1973 | |
---|
1974 | self.on_set_focus(None) |
---|
1975 | self.Refresh() |
---|
1976 | #update model plot with new data information |
---|
1977 | if flag: |
---|
1978 | #set model view button |
---|
1979 | if not self.enable_smearer.GetValue(): |
---|
1980 | self.disable_smearer.SetValue(True) |
---|
1981 | self.onSmear(None) |
---|
1982 | #self._set_smear(self.data) |
---|
1983 | |
---|
1984 | if self.data.__class__.__name__ == "Data2D": |
---|
1985 | self.enable2D = True |
---|
1986 | self.model_view.SetLabel("2D Mode") |
---|
1987 | else: |
---|
1988 | self.enable2D = False |
---|
1989 | self.model_view.SetLabel("1D Mode") |
---|
1990 | self.model_view.Disable() |
---|
1991 | #replace data plot on combo box selection |
---|
1992 | #by removing the previous selected data |
---|
1993 | wx.PostEvent(self._manager.parent, NewPlotEvent(action="remove", |
---|
1994 | group_id=self.graph_id, id=id)) |
---|
1995 | #plot the current selected data |
---|
1996 | wx.PostEvent(self._manager.parent, NewPlotEvent(plot=self.data, |
---|
1997 | title=str(self.data.title))) |
---|
1998 | #self._manager.store_data(uid=self.uid, data_list=self.data_list, |
---|
1999 | # caption=self.window_name) |
---|
2000 | self._draw_model() |
---|
2001 | |
---|
2002 | def _npts_click(self, event): |
---|
2003 | """ |
---|
2004 | Prevent further handling of the mouse event on Npts_total |
---|
2005 | by not calling Skip(). |
---|
2006 | """ |
---|
2007 | pass |
---|
2008 | |
---|
2009 | def reset_page(self, state,first=False): |
---|
2010 | """ |
---|
2011 | reset the state |
---|
2012 | """ |
---|
2013 | self.reset_page_helper(state) |
---|
2014 | |
---|
2015 | self.select_param(event = None) |
---|
2016 | #Save state_fit |
---|
2017 | self.save_current_state_fit() |
---|
2018 | self._lay_out() |
---|
2019 | self.Refresh() |
---|
2020 | |
---|
2021 | def get_range(self): |
---|
2022 | """ |
---|
2023 | return the fitting range |
---|
2024 | """ |
---|
2025 | return float(self.qmin_x) , float(self.qmax_x) |
---|
2026 | |
---|
2027 | def get_npts2fit(self): |
---|
2028 | """ |
---|
2029 | return numbers of data points within qrange |
---|
2030 | |
---|
2031 | :Note: This is for Park where chi2 is not normalized by Npts of fit |
---|
2032 | |
---|
2033 | """ |
---|
2034 | if self.data is None: |
---|
2035 | return |
---|
2036 | npts2fit = 0 |
---|
2037 | qmin,qmax = self.get_range() |
---|
2038 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
2039 | self.enable2D: |
---|
2040 | radius= numpy.sqrt( self.data.qx_data*self.data.qx_data + |
---|
2041 | self.data.qy_data*self.data.qy_data ) |
---|
2042 | index_data = (self.qmin_x <= radius)&(radius<= self.qmax_x) |
---|
2043 | index_data= (index_data)&(self.data.mask) |
---|
2044 | index_data = (index_data)&(numpy.isfinite(self.data.data)) |
---|
2045 | npts2fit = len(self.data.data[index_data]) |
---|
2046 | else: |
---|
2047 | for qx in self.data.x: |
---|
2048 | if qx >= qmin and qx <= qmax: |
---|
2049 | npts2fit += 1 |
---|
2050 | return npts2fit |
---|
2051 | |
---|
2052 | def set_npts2fit(self): |
---|
2053 | """ |
---|
2054 | setValue Npts for fitting |
---|
2055 | """ |
---|
2056 | self.Npts_fit.SetValue(str(self.get_npts2fit())) |
---|
2057 | |
---|
2058 | def get_chi2(self): |
---|
2059 | """ |
---|
2060 | return the current chi2 |
---|
2061 | """ |
---|
2062 | return self.tcChi.GetValue() |
---|
2063 | |
---|
2064 | def get_param_list(self): |
---|
2065 | """ |
---|
2066 | :return self.param_toFit: list containing references to TextCtrl |
---|
2067 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
2068 | |
---|
2069 | :raise: if return an empty list of parameter fit will nnote work |
---|
2070 | properly so raise ValueError,"missing parameter to fit" |
---|
2071 | """ |
---|
2072 | if self.param_toFit !=[]: |
---|
2073 | return self.param_toFit |
---|
2074 | else: |
---|
2075 | msg = "missing parameters to fit" |
---|
2076 | wx.MessageBox(msg, 'warning') |
---|
2077 | return False |
---|
2078 | #raise ValueError,"missing parameters to fit" |
---|
2079 | |
---|
2080 | def onsetValues(self, chisqr, p_name, out, cov): |
---|
2081 | """ |
---|
2082 | Build the panel from the fit result |
---|
2083 | |
---|
2084 | :param chisqr: Value of the goodness of fit metric |
---|
2085 | :param p_name: the name of parameters |
---|
2086 | :param out: list of parameter with the best value found during fitting |
---|
2087 | :param cov: Covariance matrix |
---|
2088 | |
---|
2089 | """ |
---|
2090 | if out == None or not numpy.isfinite(chisqr): |
---|
2091 | raise ValueError,"Fit error occured..." |
---|
2092 | |
---|
2093 | is_modified = False |
---|
2094 | has_error = False |
---|
2095 | dispersity = '' |
---|
2096 | |
---|
2097 | #Hide textctrl boxes of errors. |
---|
2098 | self._clear_Err_on_Fit() |
---|
2099 | |
---|
2100 | #Check if chi2 is finite |
---|
2101 | if chisqr != None and numpy.isfinite(chisqr): |
---|
2102 | #format chi2 |
---|
2103 | if self.engine_type == "park": |
---|
2104 | npt_fit = float(self.get_npts2fit()) |
---|
2105 | # Taking care this in parkemgine |
---|
2106 | #if npt_fit > 0: |
---|
2107 | # chisqr =chisqr/npt_fit |
---|
2108 | chi2 = format_number(chisqr, True) |
---|
2109 | self.tcChi.SetValue(chi2) |
---|
2110 | self.tcChi.Refresh() |
---|
2111 | else: |
---|
2112 | self.tcChi.SetValue("-") |
---|
2113 | |
---|
2114 | #Hide error title |
---|
2115 | if self.text2_3.IsShown() and not self.is_mac: |
---|
2116 | self.text2_3.Hide() |
---|
2117 | |
---|
2118 | try: |
---|
2119 | if self.enable_disp.GetValue(): |
---|
2120 | if hasattr(self,"text_disp_1" ): |
---|
2121 | if self.text_disp_1 != None and not self.is_mac: |
---|
2122 | self.text_disp_1.Hide() |
---|
2123 | except: |
---|
2124 | dispersity = None |
---|
2125 | pass |
---|
2126 | |
---|
2127 | |
---|
2128 | i = 0 |
---|
2129 | #Set the panel when fit result are list |
---|
2130 | for item in self.param_toFit: |
---|
2131 | if len(item)>5 and item != None: |
---|
2132 | ## reset error value to initial state |
---|
2133 | if not self.is_mac: |
---|
2134 | item[3].Hide() |
---|
2135 | item[4].Hide() |
---|
2136 | for ind in range(len(out)): |
---|
2137 | if item[1] == p_name[ind]: |
---|
2138 | break |
---|
2139 | if len(out)<=len(self.param_toFit) and out[ind] !=None: |
---|
2140 | val_out = format_number(out[ind], True) |
---|
2141 | item[2].SetValue(val_out) |
---|
2142 | |
---|
2143 | if(cov !=None and len(cov) == len(out)): |
---|
2144 | try: |
---|
2145 | if dispersity !=None: |
---|
2146 | if self.enable_disp.GetValue(): |
---|
2147 | if hasattr(self,"text_disp_1" ): |
---|
2148 | if self.text_disp_1!=None: |
---|
2149 | if not self.text_disp_1.IsShown()\ |
---|
2150 | and not self.is_mac: |
---|
2151 | self.text_disp_1.Show(True) |
---|
2152 | except: |
---|
2153 | pass |
---|
2154 | |
---|
2155 | if cov[ind]!=None : |
---|
2156 | if numpy.isfinite(float(cov[ind])): |
---|
2157 | val_err = format_number(cov[ind], True) |
---|
2158 | if not self.is_mac: |
---|
2159 | item[3].Show(True) |
---|
2160 | item[4].Show(True) |
---|
2161 | item[4].SetValue(val_err) |
---|
2162 | has_error = True |
---|
2163 | i += 1 |
---|
2164 | #Show error title when any errors displayed |
---|
2165 | if has_error: |
---|
2166 | if not self.text2_3.IsShown(): |
---|
2167 | self.text2_3.Show(True) |
---|
2168 | ## save current state |
---|
2169 | self.save_current_state() |
---|
2170 | |
---|
2171 | #self._lay_out() |
---|
2172 | if not self.is_mac: |
---|
2173 | self.Layout() |
---|
2174 | self.Refresh() |
---|
2175 | self._mac_sleep(0.1) |
---|
2176 | #plot model ( when drawing, do not update chisqr value again) |
---|
2177 | self._draw_model(update_chisqr=False, source = 'fit') |
---|
2178 | #PostStatusEvent |
---|
2179 | #msg = "Fit completed!dddd " |
---|
2180 | #wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
2181 | |
---|
2182 | def onWeighting(self, event): |
---|
2183 | """ |
---|
2184 | On Weighting radio button event, sets the weightbt_string |
---|
2185 | """ |
---|
2186 | self.weightbt_string = event.GetEventObject().GetLabelText() |
---|
2187 | self._set_weight() |
---|
2188 | |
---|
2189 | def _set_weight(self, is_2D=None): |
---|
2190 | """ |
---|
2191 | Set weight in fit problem |
---|
2192 | """ |
---|
2193 | # compute weight for the current data |
---|
2194 | from .utils import get_weight |
---|
2195 | flag_weight = self.get_weight_flag() |
---|
2196 | if is_2D == None: |
---|
2197 | is_2D = self._is_2D() |
---|
2198 | weight = get_weight(data=self.data, |
---|
2199 | is2d=is_2D, |
---|
2200 | flag=flag_weight) |
---|
2201 | self._manager.set_fit_weight(uid=self.uid, |
---|
2202 | flag=flag_weight, |
---|
2203 | is2d=is_2D, |
---|
2204 | fid=None) |
---|
2205 | |
---|
2206 | def onPinholeSmear(self, event): |
---|
2207 | """ |
---|
2208 | Create a custom pinhole smear object that will change the way residuals |
---|
2209 | are compute when fitting |
---|
2210 | |
---|
2211 | :Note: accuracy is given by strings'High','Med', 'Low' FOR 2d, |
---|
2212 | None for 1D |
---|
2213 | |
---|
2214 | """ |
---|
2215 | if self.model == None: |
---|
2216 | self.disable_smearer.SetValue(True) |
---|
2217 | if event == None: |
---|
2218 | return |
---|
2219 | msg="Please select a Model first..." |
---|
2220 | wx.MessageBox(msg, 'Info') |
---|
2221 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2222 | "Smear: %s"%msg)) |
---|
2223 | return |
---|
2224 | |
---|
2225 | # Need update param values |
---|
2226 | self._update_paramv_on_fit() |
---|
2227 | |
---|
2228 | # msg default |
---|
2229 | msg = None |
---|
2230 | if event != None: |
---|
2231 | tcrtl= event.GetEventObject() |
---|
2232 | # event case of radio button |
---|
2233 | if tcrtl.GetValue()== True: |
---|
2234 | self.dx_min = 0.0 |
---|
2235 | self.dx_max = 0.0 |
---|
2236 | is_new_pinhole = True |
---|
2237 | else: |
---|
2238 | is_new_pinhole = self._is_changed_pinhole() |
---|
2239 | else: |
---|
2240 | is_new_pinhole = True |
---|
2241 | # if any value is changed |
---|
2242 | if is_new_pinhole: |
---|
2243 | msg = self._set_pinhole_smear() |
---|
2244 | # hide all silt sizer |
---|
2245 | self._hide_all_smear_info() |
---|
2246 | |
---|
2247 | ##Calculate chi2 |
---|
2248 | temp_smearer = self.current_smearer |
---|
2249 | #self.compute_chisqr(smearer= temp_smearer) |
---|
2250 | |
---|
2251 | # show relevant slit sizers |
---|
2252 | self._show_smear_sizer() |
---|
2253 | |
---|
2254 | self.sizer_set_smearer.Layout() |
---|
2255 | self.Layout() |
---|
2256 | |
---|
2257 | if event != None: |
---|
2258 | event.Skip() |
---|
2259 | #self._undo.Enable(True) |
---|
2260 | self.save_current_state() |
---|
2261 | event = PageInfoEvent(page = self) |
---|
2262 | wx.PostEvent(self.parent, event) |
---|
2263 | |
---|
2264 | def _is_changed_pinhole(self): |
---|
2265 | """ |
---|
2266 | check if any of pinhole smear is changed |
---|
2267 | |
---|
2268 | :return: True or False |
---|
2269 | |
---|
2270 | """ |
---|
2271 | # get the values |
---|
2272 | pin_min = self.smear_pinhole_min.GetValue() |
---|
2273 | pin_max = self.smear_pinhole_max.GetValue() |
---|
2274 | |
---|
2275 | # Check changes in slit width |
---|
2276 | try: |
---|
2277 | dx_min = float(pin_min) |
---|
2278 | except: |
---|
2279 | return True |
---|
2280 | if self.dx_min != dx_min: |
---|
2281 | return True |
---|
2282 | |
---|
2283 | # Check changes in slit heigth |
---|
2284 | try: |
---|
2285 | dx_max = float(pin_max) |
---|
2286 | except: |
---|
2287 | return True |
---|
2288 | if self.dx_max != dx_max: |
---|
2289 | return True |
---|
2290 | return False |
---|
2291 | |
---|
2292 | def _set_pinhole_smear(self): |
---|
2293 | """ |
---|
2294 | Set custom pinhole smear |
---|
2295 | |
---|
2296 | :return: msg |
---|
2297 | |
---|
2298 | """ |
---|
2299 | # copy data |
---|
2300 | data = copy.deepcopy(self.data) |
---|
2301 | if self._is_2D(): |
---|
2302 | self.smear_type = 'Pinhole2d' |
---|
2303 | len_data = len(data.data) |
---|
2304 | data.dqx_data = numpy.zeros(len_data) |
---|
2305 | data.dqy_data = numpy.zeros(len_data) |
---|
2306 | else: |
---|
2307 | self.smear_type = 'Pinhole' |
---|
2308 | len_data = len(data.x) |
---|
2309 | data.dx = numpy.zeros(len_data) |
---|
2310 | data.dxl = None |
---|
2311 | data.dxw = None |
---|
2312 | msg = None |
---|
2313 | |
---|
2314 | get_pin_min = self.smear_pinhole_min |
---|
2315 | get_pin_max = self.smear_pinhole_max |
---|
2316 | |
---|
2317 | if not check_float(get_pin_min): |
---|
2318 | get_pin_min.SetBackgroundColour("pink") |
---|
2319 | msg= "Model Error:wrong value entered!!!" |
---|
2320 | elif not check_float(get_pin_max ): |
---|
2321 | get_pin_max.SetBackgroundColour("pink") |
---|
2322 | msg= "Model Error:wrong value entered!!!" |
---|
2323 | else: |
---|
2324 | if len_data < 2: len_data = 2 |
---|
2325 | self.dx_min = float(get_pin_min.GetValue()) |
---|
2326 | self.dx_max = float(get_pin_max.GetValue()) |
---|
2327 | if self.dx_min < 0: |
---|
2328 | get_pin_min.SetBackgroundColour("pink") |
---|
2329 | msg= "Model Error:This value can not be negative!!!" |
---|
2330 | elif self.dx_max <0: |
---|
2331 | get_pin_max.SetBackgroundColour("pink") |
---|
2332 | msg= "Model Error:This value can not be negative!!!" |
---|
2333 | elif self.dx_min != None and self.dx_max != None: |
---|
2334 | if self._is_2D(): |
---|
2335 | data.dqx_data[data.dqx_data==0] = self.dx_min |
---|
2336 | data.dqy_data[data.dqy_data==0] = self.dx_max |
---|
2337 | elif self.dx_min == self.dx_max: |
---|
2338 | data.dx[data.dx==0] = self.dx_min |
---|
2339 | else: |
---|
2340 | step = (self.dx_max - self.dx_min)/(len_data-1) |
---|
2341 | data.dx = numpy.arange(self.dx_min,self.dx_max+step/1.1, |
---|
2342 | step) |
---|
2343 | elif self.dx_min != None: |
---|
2344 | if self._is_2D(): data.dqx_data[data.dqx_data==0] = self.dx_min |
---|
2345 | else: data.dx[data.dx==0] = self.dx_min |
---|
2346 | elif self.dx_max != None: |
---|
2347 | if self._is_2D(): data.dqy_data[data.dqy_data==0] = self.dx_max |
---|
2348 | else: data.dx[data.dx==0] = self.dx_max |
---|
2349 | self.current_smearer = smear_selection(data, self.model) |
---|
2350 | # 2D need to set accuracy |
---|
2351 | if self._is_2D(): |
---|
2352 | self.current_smearer.set_accuracy(accuracy = \ |
---|
2353 | self.smear2d_accuracy) |
---|
2354 | |
---|
2355 | if msg != None: |
---|
2356 | wx.PostEvent(self._manager.parent, StatusEvent(status = msg )) |
---|
2357 | else: |
---|
2358 | get_pin_min.SetBackgroundColour("white") |
---|
2359 | get_pin_max.SetBackgroundColour("white") |
---|
2360 | ## set smearing value whether or not the data contain the smearing info |
---|
2361 | |
---|
2362 | self._manager.set_smearer(smearer=self.current_smearer, |
---|
2363 | fid=self.data.id, |
---|
2364 | qmin=float(self.qmin_x), |
---|
2365 | qmax= float(self.qmax_x), |
---|
2366 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
2367 | uid=self.uid) |
---|
2368 | return msg |
---|
2369 | |
---|
2370 | def update_pinhole_smear(self): |
---|
2371 | """ |
---|
2372 | called by kill_focus on pinhole TextCntrl |
---|
2373 | to update the changes |
---|
2374 | |
---|
2375 | :return: False when wrong value was entered |
---|
2376 | |
---|
2377 | """ |
---|
2378 | # msg default |
---|
2379 | msg = None |
---|
2380 | # check if any value is changed |
---|
2381 | if self._is_changed_pinhole(): |
---|
2382 | msg = self._set_pinhole_smear() |
---|
2383 | #self._undo.Enable(True) |
---|
2384 | wx.CallAfter(self.save_current_state) |
---|
2385 | |
---|
2386 | if msg != None: |
---|
2387 | return False |
---|
2388 | else: |
---|
2389 | return True |
---|
2390 | |
---|
2391 | def onSlitSmear(self, event): |
---|
2392 | """ |
---|
2393 | Create a custom slit smear object that will change the way residuals |
---|
2394 | are compute when fitting |
---|
2395 | """ |
---|
2396 | if self.model == None: |
---|
2397 | self.disable_smearer.SetValue(True) |
---|
2398 | if event == None: |
---|
2399 | return |
---|
2400 | msg="Please select a Model first..." |
---|
2401 | wx.MessageBox(msg, 'Info') |
---|
2402 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2403 | "Smear: %s"%msg)) |
---|
2404 | return |
---|
2405 | |
---|
2406 | # Need update param values |
---|
2407 | self._update_paramv_on_fit() |
---|
2408 | |
---|
2409 | # msg default |
---|
2410 | msg = None |
---|
2411 | # for event given |
---|
2412 | if event != None: |
---|
2413 | tcrtl= event.GetEventObject() |
---|
2414 | # event case of radio button |
---|
2415 | if tcrtl.GetValue(): |
---|
2416 | self.dxl = 0.0 |
---|
2417 | self.dxw = 0.0 |
---|
2418 | is_new_slit = True |
---|
2419 | else: |
---|
2420 | is_new_slit = self._is_changed_slit() |
---|
2421 | else: |
---|
2422 | is_new_slit = True |
---|
2423 | |
---|
2424 | # if any value is changed |
---|
2425 | if is_new_slit: |
---|
2426 | msg = self._set_slit_smear() |
---|
2427 | |
---|
2428 | # hide all silt sizer |
---|
2429 | self._hide_all_smear_info() |
---|
2430 | ##Calculate chi2 |
---|
2431 | #self.compute_chisqr(smearer= self.current_smearer) |
---|
2432 | # show relevant slit sizers |
---|
2433 | self._show_smear_sizer() |
---|
2434 | self.sizer_set_smearer.Layout() |
---|
2435 | self.Layout() |
---|
2436 | |
---|
2437 | if event != None: |
---|
2438 | event.Skip() |
---|
2439 | #self._undo.Enable(True) |
---|
2440 | self.save_current_state() |
---|
2441 | event = PageInfoEvent(page = self) |
---|
2442 | wx.PostEvent(self.parent, event) |
---|
2443 | if msg != None: |
---|
2444 | wx.PostEvent(self._manager.parent, StatusEvent(status = msg)) |
---|
2445 | |
---|
2446 | def _is_changed_slit(self): |
---|
2447 | """ |
---|
2448 | check if any of slit lengths is changed |
---|
2449 | |
---|
2450 | :return: True or False |
---|
2451 | |
---|
2452 | """ |
---|
2453 | # get the values |
---|
2454 | width = self.smear_slit_width.GetValue() |
---|
2455 | height = self.smear_slit_height.GetValue() |
---|
2456 | |
---|
2457 | # check and change the box bg color if it was pink |
---|
2458 | # but it should be white now |
---|
2459 | # because this is the case that _set_slit_smear() will not handle |
---|
2460 | if height.lstrip().rstrip()=="": |
---|
2461 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2462 | if width.lstrip().rstrip()=="": |
---|
2463 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2464 | |
---|
2465 | # Check changes in slit width |
---|
2466 | if width == "": |
---|
2467 | dxw = 0.0 |
---|
2468 | else: |
---|
2469 | try: |
---|
2470 | dxw = float(width) |
---|
2471 | except: |
---|
2472 | return True |
---|
2473 | if self.dxw != dxw: |
---|
2474 | return True |
---|
2475 | |
---|
2476 | # Check changes in slit heigth |
---|
2477 | if height == "": |
---|
2478 | dxl = 0.0 |
---|
2479 | else: |
---|
2480 | try: |
---|
2481 | dxl = float(height) |
---|
2482 | except: |
---|
2483 | return True |
---|
2484 | if self.dxl != dxl: |
---|
2485 | return True |
---|
2486 | |
---|
2487 | return False |
---|
2488 | |
---|
2489 | def _set_slit_smear(self): |
---|
2490 | """ |
---|
2491 | Set custom slit smear |
---|
2492 | |
---|
2493 | :return: message to inform the user about the validity |
---|
2494 | of the values entered for slit smear |
---|
2495 | """ |
---|
2496 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
2497 | self.enable2D: |
---|
2498 | return |
---|
2499 | temp_smearer = None |
---|
2500 | # make sure once more if it is smearer |
---|
2501 | data = copy.deepcopy(self.data) |
---|
2502 | data_len = len(data.x) |
---|
2503 | data.dx = None |
---|
2504 | data.dxl = None |
---|
2505 | data.dxw = None |
---|
2506 | msg = None |
---|
2507 | |
---|
2508 | try: |
---|
2509 | self.dxl = float(self.smear_slit_height.GetValue()) |
---|
2510 | data.dxl = self.dxl* numpy.ones(data_len) |
---|
2511 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2512 | except: |
---|
2513 | data.dxl = numpy.zeros(data_len) |
---|
2514 | if self.smear_slit_height.GetValue().lstrip().rstrip()!="": |
---|
2515 | self.smear_slit_height.SetBackgroundColour("pink") |
---|
2516 | msg = "Wrong value entered... " |
---|
2517 | else: |
---|
2518 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2519 | try: |
---|
2520 | self.dxw = float(self.smear_slit_width.GetValue()) |
---|
2521 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2522 | data.dxw = self.dxw* numpy.ones(data_len) |
---|
2523 | except: |
---|
2524 | data.dxw = numpy.zeros(data_len) |
---|
2525 | if self.smear_slit_width.GetValue().lstrip().rstrip()!="": |
---|
2526 | self.smear_slit_width.SetBackgroundColour("pink") |
---|
2527 | msg = "Wrong Fit value entered... " |
---|
2528 | else: |
---|
2529 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2530 | |
---|
2531 | self.current_smearer = smear_selection(data, self.model) |
---|
2532 | #temp_smearer = self.current_smearer |
---|
2533 | ## set smearing value whether or not the data contain the smearing info |
---|
2534 | self._manager.set_smearer(smearer=self.current_smearer, |
---|
2535 | fid=self.data.id, |
---|
2536 | qmin=float(self.qmin_x), |
---|
2537 | qmax= float(self.qmax_x), |
---|
2538 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
2539 | uid=self.uid) |
---|
2540 | return msg |
---|
2541 | |
---|
2542 | def update_slit_smear(self): |
---|
2543 | """ |
---|
2544 | called by kill_focus on pinhole TextCntrl |
---|
2545 | to update the changes |
---|
2546 | |
---|
2547 | :return: False when wrong value was entered |
---|
2548 | |
---|
2549 | """ |
---|
2550 | # msg default |
---|
2551 | msg = None |
---|
2552 | # check if any value is changed |
---|
2553 | if self._is_changed_slit(): |
---|
2554 | msg = self._set_slit_smear() |
---|
2555 | #self._undo.Enable(True) |
---|
2556 | self.save_current_state() |
---|
2557 | |
---|
2558 | if msg != None: |
---|
2559 | return False |
---|
2560 | else: |
---|
2561 | return True |
---|
2562 | |
---|
2563 | def onSmear(self, event): |
---|
2564 | """ |
---|
2565 | Create a smear object that will change the way residuals |
---|
2566 | are compute when fitting |
---|
2567 | """ |
---|
2568 | if event != None: |
---|
2569 | event.Skip() |
---|
2570 | if self.data is None: |
---|
2571 | return |
---|
2572 | |
---|
2573 | if self.model == None: |
---|
2574 | self.disable_smearer.SetValue(True) |
---|
2575 | if event == None: |
---|
2576 | return |
---|
2577 | msg="Please select a Model first..." |
---|
2578 | wx.MessageBox(msg, 'Info') |
---|
2579 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2580 | "Smear: %s"%msg)) |
---|
2581 | return |
---|
2582 | |
---|
2583 | # Need update param values |
---|
2584 | self._update_paramv_on_fit() |
---|
2585 | |
---|
2586 | temp_smearer = None |
---|
2587 | self._get_smear_info() |
---|
2588 | |
---|
2589 | #renew smear sizer |
---|
2590 | if self.smear_type != None: |
---|
2591 | self.smear_description_smear_type.SetValue(str(self.smear_type)) |
---|
2592 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
2593 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
2594 | |
---|
2595 | self._hide_all_smear_info() |
---|
2596 | |
---|
2597 | data = copy.deepcopy(self.data) |
---|
2598 | # make sure once more if it is smearer |
---|
2599 | temp_smearer = smear_selection(data, self.model) |
---|
2600 | if self.current_smearer != temp_smearer: |
---|
2601 | self.current_smearer = temp_smearer |
---|
2602 | if self.enable_smearer.GetValue(): |
---|
2603 | if hasattr(self.data,"dxl"): |
---|
2604 | |
---|
2605 | msg= ": Resolution smearing parameters" |
---|
2606 | if hasattr(self.data,"dxw"): |
---|
2607 | msg= ": Slit smearing parameters" |
---|
2608 | if self.current_smearer ==None: |
---|
2609 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2610 | "Data contains no smearing information")) |
---|
2611 | else: |
---|
2612 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2613 | "Data contains smearing information")) |
---|
2614 | |
---|
2615 | #self.smear_description_dqdata.Show(True) |
---|
2616 | self.smear_data_left.Show(True) |
---|
2617 | self.smear_data_right.Show(True) |
---|
2618 | temp_smearer= self.current_smearer |
---|
2619 | elif self.disable_smearer.GetValue(): |
---|
2620 | self.smear_description_none.Show(True) |
---|
2621 | elif self.pinhole_smearer.GetValue(): |
---|
2622 | self.onPinholeSmear(None) |
---|
2623 | elif self.slit_smearer.GetValue(): |
---|
2624 | self.onSlitSmear(None) |
---|
2625 | |
---|
2626 | self._show_smear_sizer() |
---|
2627 | |
---|
2628 | self.sizer_set_smearer.Layout() |
---|
2629 | self.Layout() |
---|
2630 | self._set_weight() |
---|
2631 | |
---|
2632 | ## set smearing value whether or not the data contain the smearing info |
---|
2633 | wx.CallAfter(self._manager.set_smearer, uid=self.uid, smearer=temp_smearer, |
---|
2634 | fid=self.data.id, |
---|
2635 | qmin=float(self.qmin_x), |
---|
2636 | qmax=float(self.qmax_x), |
---|
2637 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
2638 | draw=True) |
---|
2639 | |
---|
2640 | self.state.enable_smearer= self.enable_smearer.GetValue() |
---|
2641 | self.state.disable_smearer=self.disable_smearer.GetValue() |
---|
2642 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
2643 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
2644 | |
---|
2645 | def on_complete_chisqr(self, event): |
---|
2646 | """ |
---|
2647 | Display result chisqr on the panel |
---|
2648 | :event: activated by fitting/ complete after draw |
---|
2649 | """ |
---|
2650 | try: |
---|
2651 | if event == None: |
---|
2652 | output= "-" |
---|
2653 | elif not numpy.isfinite(event.output): |
---|
2654 | output= "-" |
---|
2655 | else: |
---|
2656 | output = event.output |
---|
2657 | self.tcChi.SetValue(str(format_number(output, True))) |
---|
2658 | self.state.tcChi = self.tcChi.GetValue() |
---|
2659 | except: |
---|
2660 | pass |
---|
2661 | |
---|
2662 | |
---|
2663 | def get_all_checked_params(self): |
---|
2664 | """ |
---|
2665 | Found all parameters current check and add them to list of parameters to |
---|
2666 | fit |
---|
2667 | """ |
---|
2668 | self.param_toFit = [] |
---|
2669 | for item in self.parameters: |
---|
2670 | if item[0].GetValue() and item not in self.param_toFit: |
---|
2671 | self.param_toFit.append(item) |
---|
2672 | for item in self.fittable_param: |
---|
2673 | if item[0].GetValue() and item not in self.param_toFit: |
---|
2674 | self.param_toFit.append(item) |
---|
2675 | self.save_current_state_fit() |
---|
2676 | |
---|
2677 | |
---|
2678 | event = PageInfoEvent(page = self) |
---|
2679 | wx.PostEvent(self.parent, event) |
---|
2680 | param2fit = [] |
---|
2681 | for item in self.param_toFit: |
---|
2682 | if item[0]: |
---|
2683 | param2fit.append(item[1]) |
---|
2684 | self.parent._manager.set_param2fit(self.uid, param2fit) |
---|
2685 | |
---|
2686 | def select_all_param(self, event): |
---|
2687 | """ |
---|
2688 | set to true or false all checkBox given the main checkbox value cb1 |
---|
2689 | """ |
---|
2690 | self.param_toFit = [] |
---|
2691 | if self.parameters !=[]: |
---|
2692 | if self.cb1.GetValue(): |
---|
2693 | for item in self.parameters: |
---|
2694 | ## for data2D select all to fit |
---|
2695 | if self.data.__class__.__name__== "Data2D" or \ |
---|
2696 | self.enable2D: |
---|
2697 | item[0].SetValue(True) |
---|
2698 | self.param_toFit.append(item) |
---|
2699 | else: |
---|
2700 | ## for 1D all parameters except orientation |
---|
2701 | if not item in self.orientation_params: |
---|
2702 | item[0].SetValue(True) |
---|
2703 | self.param_toFit.append(item ) |
---|
2704 | #if len(self.fittable_param)>0: |
---|
2705 | for item in self.fittable_param: |
---|
2706 | if self.data.__class__.__name__== "Data2D" or \ |
---|
2707 | self.enable2D: |
---|
2708 | item[0].SetValue(True) |
---|
2709 | self.param_toFit.append(item) |
---|
2710 | try: |
---|
2711 | if len(self.values[item[1]]) > 0: |
---|
2712 | item[0].SetValue(False) |
---|
2713 | except: |
---|
2714 | pass |
---|
2715 | |
---|
2716 | else: |
---|
2717 | ## for 1D all parameters except orientation |
---|
2718 | if not item in self.orientation_params_disp: |
---|
2719 | item[0].SetValue(True) |
---|
2720 | self.param_toFit.append(item ) |
---|
2721 | try: |
---|
2722 | if len(self.values[item[1]]) > 0: |
---|
2723 | item[0].SetValue(False) |
---|
2724 | except: |
---|
2725 | pass |
---|
2726 | |
---|
2727 | else: |
---|
2728 | for item in self.parameters: |
---|
2729 | item[0].SetValue(False) |
---|
2730 | for item in self.fittable_param: |
---|
2731 | item[0].SetValue(False) |
---|
2732 | self.param_toFit=[] |
---|
2733 | |
---|
2734 | self.save_current_state_fit() |
---|
2735 | |
---|
2736 | if event !=None: |
---|
2737 | #self._undo.Enable(True) |
---|
2738 | ## post state to fit panel |
---|
2739 | event = PageInfoEvent(page = self) |
---|
2740 | wx.PostEvent(self.parent, event) |
---|
2741 | param2fit = [] |
---|
2742 | for item in self.param_toFit: |
---|
2743 | if item[0]: |
---|
2744 | param2fit.append(item[1]) |
---|
2745 | self.parent._manager.set_param2fit(self.uid, param2fit) |
---|
2746 | |
---|
2747 | |
---|
2748 | def select_param(self,event): |
---|
2749 | """ |
---|
2750 | Select TextCtrl checked for fitting purpose and stores them |
---|
2751 | in self.param_toFit=[] list |
---|
2752 | """ |
---|
2753 | self.param_toFit = [] |
---|
2754 | for item in self.parameters: |
---|
2755 | #Skip t ifhe angle parameters if 1D data |
---|
2756 | if self.data.__class__.__name__ != "Data2D" and\ |
---|
2757 | not self.enable2D: |
---|
2758 | if item in self.orientation_params: |
---|
2759 | continue |
---|
2760 | #Select parameters to fit for list of primary parameters |
---|
2761 | if item[0].GetValue(): |
---|
2762 | if not (item in self.param_toFit): |
---|
2763 | self.param_toFit.append(item ) |
---|
2764 | else: |
---|
2765 | #remove parameters from the fitting list |
---|
2766 | if item in self.param_toFit: |
---|
2767 | self.param_toFit.remove(item) |
---|
2768 | |
---|
2769 | #Select parameters to fit for list of fittable parameters |
---|
2770 | # with dispersion |
---|
2771 | for item in self.fittable_param: |
---|
2772 | #Skip t ifhe angle parameters if 1D data |
---|
2773 | if self.data.__class__.__name__ != "Data2D" and\ |
---|
2774 | not self.enable2D: |
---|
2775 | if item in self.orientation_params: |
---|
2776 | continue |
---|
2777 | if item[0].GetValue(): |
---|
2778 | if not (item in self.param_toFit): |
---|
2779 | self.param_toFit.append(item) |
---|
2780 | else: |
---|
2781 | #remove parameters from the fitting list |
---|
2782 | if item in self.param_toFit: |
---|
2783 | self.param_toFit.remove(item) |
---|
2784 | |
---|
2785 | #Calculate num. of angle parameters |
---|
2786 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
2787 | self.enable2D: |
---|
2788 | len_orient_para = 0 |
---|
2789 | else: |
---|
2790 | len_orient_para = len(self.orientation_params) #assume even len |
---|
2791 | #Total num. of angle parameters |
---|
2792 | if len(self.fittable_param) > 0: |
---|
2793 | len_orient_para *= 2 |
---|
2794 | #Set the value of checkbox that selected every checkbox or not |
---|
2795 | if len(self.parameters)+len(self.fittable_param)-len_orient_para ==\ |
---|
2796 | len(self.param_toFit): |
---|
2797 | self.cb1.SetValue(True) |
---|
2798 | else: |
---|
2799 | self.cb1.SetValue(False) |
---|
2800 | |
---|
2801 | |
---|
2802 | |
---|
2803 | self.save_current_state_fit() |
---|
2804 | if event !=None: |
---|
2805 | #self._undo.Enable(True) |
---|
2806 | ## post state to fit panel |
---|
2807 | event = PageInfoEvent(page = self) |
---|
2808 | wx.PostEvent(self.parent, event) |
---|
2809 | |
---|
2810 | param2fit = [] |
---|
2811 | for item in self.param_toFit: |
---|
2812 | if item[0]: |
---|
2813 | param2fit.append(item[1]) |
---|
2814 | self.parent._manager.set_param2fit(self.uid, param2fit) |
---|
2815 | |
---|
2816 | def set_model_param_sizer(self, model): |
---|
2817 | """ |
---|
2818 | Build the panel from the model content |
---|
2819 | |
---|
2820 | :param model: the model selected in combo box for fitting purpose |
---|
2821 | |
---|
2822 | """ |
---|
2823 | self.sizer3.Clear(True) |
---|
2824 | self.parameters = [] |
---|
2825 | self.str_parameters = [] |
---|
2826 | self.param_toFit=[] |
---|
2827 | self.fittable_param=[] |
---|
2828 | self.fixed_param=[] |
---|
2829 | self.orientation_params=[] |
---|
2830 | self.orientation_params_disp=[] |
---|
2831 | |
---|
2832 | if model ==None: |
---|
2833 | self.sizer3.Layout() |
---|
2834 | self.SetupScrolling() |
---|
2835 | return |
---|
2836 | ## the panel is drawn using the current value of the fit engine |
---|
2837 | if self.engine_type==None and self._manager !=None: |
---|
2838 | self.engine_type= self._manager._return_engine_type() |
---|
2839 | |
---|
2840 | box_description= wx.StaticBox(self, -1,str("Model Parameters")) |
---|
2841 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
2842 | sizer = wx.GridBagSizer(5,5) |
---|
2843 | ## save the current model |
---|
2844 | self.model = model |
---|
2845 | |
---|
2846 | keys = self.model.getParamList() |
---|
2847 | #list of dispersion paramaters |
---|
2848 | self.disp_list=self.model.getDispParamList() |
---|
2849 | |
---|
2850 | def custom_compare(a,b): |
---|
2851 | """ |
---|
2852 | Custom compare to order, first by alphabets then second by number. |
---|
2853 | """ |
---|
2854 | # number at the last digit |
---|
2855 | a_last = a[len(a)-1] |
---|
2856 | b_last = b[len(b)-1] |
---|
2857 | # default |
---|
2858 | num_a = None |
---|
2859 | num_b = None |
---|
2860 | # split the names |
---|
2861 | a2 = a.lower().split('_') |
---|
2862 | b2 = b.lower().split('_') |
---|
2863 | # check length of a2, b2 |
---|
2864 | len_a2 = len(a2) |
---|
2865 | len_b2 = len(b2) |
---|
2866 | # check if it contains a int number(<10) |
---|
2867 | try: |
---|
2868 | num_a = int(a_last) |
---|
2869 | except: pass |
---|
2870 | try: |
---|
2871 | num_b = int(b_last) |
---|
2872 | except: pass |
---|
2873 | # Put 'scale' near the top; happens |
---|
2874 | # when numbered param name exists |
---|
2875 | if a == 'scale': |
---|
2876 | return -1 |
---|
2877 | # both have a number |
---|
2878 | if num_a != None and num_b != None: |
---|
2879 | if num_a > num_b: return -1 |
---|
2880 | # same number |
---|
2881 | elif num_a == num_b: |
---|
2882 | # different last names |
---|
2883 | if a2[len_a2-1] != b2[len_b2-1] and num_a != 0: |
---|
2884 | return -cmp(a2[len_a2-1], b2[len_b2-1]) |
---|
2885 | else: |
---|
2886 | return cmp(a, b) |
---|
2887 | else: return 1 |
---|
2888 | # one of them has a number |
---|
2889 | elif num_a != None: return 1 |
---|
2890 | elif num_b != None: return -1 |
---|
2891 | # no numbers |
---|
2892 | else: return cmp(a.lower(), b.lower()) |
---|
2893 | |
---|
2894 | |
---|
2895 | keys.sort(custom_compare) |
---|
2896 | |
---|
2897 | iy = 0 |
---|
2898 | ix = 0 |
---|
2899 | select_text = "Select All" |
---|
2900 | self.cb1 = wx.CheckBox(self, -1,str(select_text), (10, 10)) |
---|
2901 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
2902 | self.cb1.SetToolTipString("To check/uncheck all the boxes below.") |
---|
2903 | self.cb1.SetValue(True) |
---|
2904 | |
---|
2905 | sizer.Add(self.cb1,(iy, ix),(1,1),\ |
---|
2906 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2907 | ix +=1 |
---|
2908 | self.text2_2 = wx.StaticText(self, -1, 'Value') |
---|
2909 | sizer.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
2910 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2911 | ix +=2 |
---|
2912 | self.text2_3 = wx.StaticText(self, -1, 'Error') |
---|
2913 | sizer.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
2914 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2915 | if not self.is_mac: |
---|
2916 | self.text2_3.Hide() |
---|
2917 | ix +=1 |
---|
2918 | self.text2_min = wx.StaticText(self, -1, 'Min') |
---|
2919 | sizer.Add(self.text2_min,(iy, ix),(1,1),\ |
---|
2920 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2921 | #self.text2_min.Hide() |
---|
2922 | ix +=1 |
---|
2923 | self.text2_max = wx.StaticText(self, -1, 'Max') |
---|
2924 | sizer.Add(self.text2_max,(iy, ix),(1,1),\ |
---|
2925 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2926 | #self.text2_max.Hide() |
---|
2927 | ix += 1 |
---|
2928 | self.text2_4 = wx.StaticText(self, -1, '[Units]') |
---|
2929 | sizer.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
2930 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2931 | self.text2_4.Hide() |
---|
2932 | |
---|
2933 | CHECK_STATE = self.cb1.GetValue() |
---|
2934 | for item in keys: |
---|
2935 | if not item in self.disp_list and not item in \ |
---|
2936 | self.model.orientation_params: |
---|
2937 | |
---|
2938 | ##prepare a spot to store errors |
---|
2939 | if not self.model.details.has_key(item): |
---|
2940 | self.model.details [item] = ["",None,None] |
---|
2941 | |
---|
2942 | iy += 1 |
---|
2943 | ix = 0 |
---|
2944 | if (self.model.__class__ in \ |
---|
2945 | self.model_list_box["Multi-Functions"] or \ |
---|
2946 | self.temp_multi_functional)\ |
---|
2947 | and (item in self.model.non_fittable): |
---|
2948 | non_fittable_name = wx.StaticText(self, -1, item ) |
---|
2949 | sizer.Add(non_fittable_name,(iy, ix),(1,1),\ |
---|
2950 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 21) |
---|
2951 | ## add parameter value |
---|
2952 | ix += 1 |
---|
2953 | value= self.model.getParam(item) |
---|
2954 | if len(self.model.fun_list) > 0: |
---|
2955 | num = item.split('_')[1][5:7] |
---|
2956 | fun_box = wx.ComboBox(self, -1,size=(100,-1), |
---|
2957 | style=wx.CB_READONLY, name = '%s'% item) |
---|
2958 | self._set_fun_box_list(fun_box) |
---|
2959 | fun_box.SetSelection(0) |
---|
2960 | #self.fun_box.SetToolTipString("A function |
---|
2961 | # describing the interface") |
---|
2962 | wx.EVT_COMBOBOX(fun_box,-1, self._on_fun_box) |
---|
2963 | else: |
---|
2964 | fun_box = self.ModelTextCtrl(self, -1, |
---|
2965 | size=(_BOX_WIDTH,20), |
---|
2966 | style=wx.TE_PROCESS_ENTER, name ='%s'% item) |
---|
2967 | fun_box.SetToolTipString(\ |
---|
2968 | "Hit 'Enter' after typing to update the plot.") |
---|
2969 | fun_box.SetValue(format_number(value, True)) |
---|
2970 | sizer.Add(fun_box, (iy,ix),(1,1), wx.EXPAND) |
---|
2971 | self.str_parameters.append([None,item, fun_box, |
---|
2972 | None, None, None, |
---|
2973 | None, None]) |
---|
2974 | |
---|
2975 | |
---|
2976 | else: |
---|
2977 | ## add parameters name with checkbox for selecting to fit |
---|
2978 | cb = wx.CheckBox(self, -1, item) |
---|
2979 | cb.SetValue(CHECK_STATE) |
---|
2980 | cb.SetToolTipString(" Check mark to fit.") |
---|
2981 | #cb.SetValue(True) |
---|
2982 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
2983 | |
---|
2984 | sizer.Add( cb,( iy, ix),(1,1), |
---|
2985 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2986 | |
---|
2987 | ## add parameter value |
---|
2988 | ix += 1 |
---|
2989 | value= self.model.getParam(item) |
---|
2990 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
2991 | style=wx.TE_PROCESS_ENTER) |
---|
2992 | ctl1.SetToolTipString(\ |
---|
2993 | "Hit 'Enter' after typing to update the plot.") |
---|
2994 | ctl1.SetValue(format_number(value, True)) |
---|
2995 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
2996 | ## text to show error sign |
---|
2997 | ix += 1 |
---|
2998 | text2=wx.StaticText(self, -1, '+/-') |
---|
2999 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
3000 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3001 | if not self.is_mac: |
---|
3002 | text2.Hide() |
---|
3003 | ix += 1 |
---|
3004 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/1.2,20), style=0) |
---|
3005 | sizer.Add(ctl2, (iy,ix),(1,1), |
---|
3006 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3007 | if not self.is_mac: |
---|
3008 | ctl2.Hide() |
---|
3009 | |
---|
3010 | ix += 1 |
---|
3011 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/1.9,20), |
---|
3012 | style=wx.TE_PROCESS_ENTER, |
---|
3013 | text_enter_callback = self._onparamRangeEnter) |
---|
3014 | |
---|
3015 | sizer.Add(ctl3, (iy,ix),(1,1), |
---|
3016 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3017 | |
---|
3018 | ix += 1 |
---|
3019 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/1.9,20), |
---|
3020 | style=wx.TE_PROCESS_ENTER, |
---|
3021 | text_enter_callback = self._onparamRangeEnter) |
---|
3022 | sizer.Add(ctl4, (iy,ix),(1,1), |
---|
3023 | wx.EXPAND|wx.FIXED_MINSIZE, 0) |
---|
3024 | |
---|
3025 | ix +=1 |
---|
3026 | # Units |
---|
3027 | if self.model.details.has_key(item): |
---|
3028 | units = wx.StaticText(self, -1, |
---|
3029 | self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
3030 | else: |
---|
3031 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
3032 | sizer.Add(units, (iy,ix),(1,1), |
---|
3033 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3034 | |
---|
3035 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
3036 | self.parameters.append([cb,item, ctl1, |
---|
3037 | text2,ctl2, ctl3, ctl4,units]) |
---|
3038 | |
---|
3039 | iy+=1 |
---|
3040 | sizer.Add((10,10),(iy,ix),(1,1), |
---|
3041 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
3042 | |
---|
3043 | # type can be either Guassian or Array |
---|
3044 | if len(self.model.dispersion.values())>0: |
---|
3045 | type= self.model.dispersion.values()[0]["type"] |
---|
3046 | else: |
---|
3047 | type = "Gaussian" |
---|
3048 | |
---|
3049 | iy += 1 |
---|
3050 | ix = 0 |
---|
3051 | #Add tile for orientational angle |
---|
3052 | for item in keys: |
---|
3053 | if item in self.model.orientation_params: |
---|
3054 | orient_angle = wx.StaticText(self, -1, '[For 2D only]:') |
---|
3055 | sizer.Add(orient_angle,(iy, ix),(1,1), |
---|
3056 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
3057 | if not self.data.__class__.__name__ == "Data2D" and \ |
---|
3058 | not self.enable2D: |
---|
3059 | orient_angle.Hide() |
---|
3060 | else: |
---|
3061 | orient_angle.Show(True) |
---|
3062 | break |
---|
3063 | |
---|
3064 | #For Gaussian only |
---|
3065 | if type.lower() != "array": |
---|
3066 | for item in self.model.orientation_params: |
---|
3067 | if not item in self.disp_list: |
---|
3068 | ##prepare a spot to store min max |
---|
3069 | if not self.model.details.has_key(item): |
---|
3070 | self.model.details [item] = ["",None,None] |
---|
3071 | |
---|
3072 | iy += 1 |
---|
3073 | ix = 0 |
---|
3074 | ## add parameters name with checkbox for selecting to fit |
---|
3075 | cb = wx.CheckBox(self, -1, item ) |
---|
3076 | cb.SetValue(CHECK_STATE) |
---|
3077 | cb.SetToolTipString("Check mark to fit") |
---|
3078 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
3079 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3080 | self.enable2D: |
---|
3081 | cb.Show(True) |
---|
3082 | else: |
---|
3083 | cb.Hide() |
---|
3084 | sizer.Add( cb,( iy, ix),(1,1), |
---|
3085 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
3086 | |
---|
3087 | ## add parameter value |
---|
3088 | ix += 1 |
---|
3089 | value= self.model.getParam(item) |
---|
3090 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
3091 | style=wx.TE_PROCESS_ENTER) |
---|
3092 | ctl1.SetToolTipString(\ |
---|
3093 | "Hit 'Enter' after typing to update the plot.") |
---|
3094 | ctl1.SetValue(format_number(value, True)) |
---|
3095 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3096 | self.enable2D: |
---|
3097 | ctl1.Show(True) |
---|
3098 | else: |
---|
3099 | ctl1.Hide() |
---|
3100 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
3101 | ## text to show error sign |
---|
3102 | ix += 1 |
---|
3103 | text2=wx.StaticText(self, -1, '+/-') |
---|
3104 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
3105 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3106 | |
---|
3107 | text2.Hide() |
---|
3108 | ix += 1 |
---|
3109 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/1.2,20), style=0) |
---|
3110 | sizer.Add(ctl2, (iy,ix),(1,1), |
---|
3111 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3112 | |
---|
3113 | ctl2.Hide() |
---|
3114 | |
---|
3115 | |
---|
3116 | ix += 1 |
---|
3117 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/1.8,20), |
---|
3118 | style=wx.TE_PROCESS_ENTER, |
---|
3119 | text_enter_callback = self._onparamRangeEnter) |
---|
3120 | |
---|
3121 | sizer.Add(ctl3, (iy,ix),(1,1), |
---|
3122 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3123 | ctl3.Hide() |
---|
3124 | |
---|
3125 | ix += 1 |
---|
3126 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/1.8,20), |
---|
3127 | style=wx.TE_PROCESS_ENTER, |
---|
3128 | text_enter_callback = self._onparamRangeEnter) |
---|
3129 | sizer.Add(ctl4, (iy,ix),(1,1), |
---|
3130 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3131 | |
---|
3132 | ctl4.Hide() |
---|
3133 | |
---|
3134 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3135 | self.enable2D: |
---|
3136 | if self.is_mac: |
---|
3137 | text2.Show(True) |
---|
3138 | ctl2.Show(True) |
---|
3139 | ctl3.Show(True) |
---|
3140 | ctl4.Show(True) |
---|
3141 | |
---|
3142 | ix +=1 |
---|
3143 | # Units |
---|
3144 | if self.model.details.has_key(item): |
---|
3145 | units = wx.StaticText(self, -1, |
---|
3146 | self.model.details[item][0], |
---|
3147 | style=wx.ALIGN_LEFT) |
---|
3148 | else: |
---|
3149 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
3150 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3151 | self.enable2D: |
---|
3152 | units.Show(True) |
---|
3153 | |
---|
3154 | else: |
---|
3155 | units.Hide() |
---|
3156 | |
---|
3157 | sizer.Add(units, (iy,ix),(1,1), |
---|
3158 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3159 | |
---|
3160 | ##[cb state, name, value, "+/-", error of fit,min,max,units] |
---|
3161 | self.parameters.append([cb,item, ctl1, |
---|
3162 | text2,ctl2, ctl3, ctl4,units]) |
---|
3163 | self.orientation_params.append([cb,item, ctl1, |
---|
3164 | text2,ctl2, ctl3, ctl4,units]) |
---|
3165 | |
---|
3166 | iy+=1 |
---|
3167 | |
---|
3168 | #Display units text on panel |
---|
3169 | for item in keys: |
---|
3170 | if self.model.details.has_key(item): |
---|
3171 | self.text2_4.Show() |
---|
3172 | #Fill the list of fittable parameters |
---|
3173 | self.get_all_checked_params() |
---|
3174 | self.save_current_state_fit() |
---|
3175 | boxsizer1.Add(sizer) |
---|
3176 | self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
3177 | self.sizer3.Layout() |
---|
3178 | self.Layout() |
---|
3179 | #self.Refresh() |
---|
3180 | |
---|
3181 | def on_right_down(self, event): |
---|
3182 | """ |
---|
3183 | Get key stroke event |
---|
3184 | """ |
---|
3185 | if self.data == None: |
---|
3186 | return |
---|
3187 | # Figuring out key combo: Cmd for copy, Alt for paste |
---|
3188 | if event.AltDown() and event.ShiftDown(): |
---|
3189 | self._manager.show_ftol_dialog() |
---|
3190 | flag = True |
---|
3191 | elif event.AltDown() or event.ShiftDown(): |
---|
3192 | flag = False |
---|
3193 | else: |
---|
3194 | return |
---|
3195 | # make event free |
---|
3196 | event.Skip() |
---|
3197 | # messages depending on the flag |
---|
3198 | if not flag: |
---|
3199 | msg = " Could not open ftol dialog;" |
---|
3200 | msg += " Check if the Scipy fit engine is selected in the menubar." |
---|
3201 | infor = 'warning' |
---|
3202 | # inform msg to wx |
---|
3203 | wx.PostEvent( self.parent.parent, |
---|
3204 | StatusEvent(status= msg, info=infor)) |
---|
3205 | |
---|
3206 | |
---|
3207 | def _onModel2D(self, event): |
---|
3208 | """ |
---|
3209 | toggle view of model from 1D to 2D or 2D from 1D |
---|
3210 | """ |
---|
3211 | if self.model_view.GetLabelText() == "Show 2D": |
---|
3212 | self.model_view.SetLabel("Show 1D") |
---|
3213 | self.enable2D = True |
---|
3214 | |
---|
3215 | else: |
---|
3216 | self.model_view.SetLabel("Show 2D") |
---|
3217 | self.enable2D = False |
---|
3218 | self.Show(False) |
---|
3219 | self.create_default_data() |
---|
3220 | self._manager.store_data(self.uid, data_list=[self.data]) |
---|
3221 | |
---|
3222 | self.set_model_param_sizer(self.model) |
---|
3223 | self._set_sizer_dispersion() |
---|
3224 | self._set_weight(is_2D= self.enable2D) |
---|
3225 | self._set_smear_buttons() |
---|
3226 | self.Show(True) |
---|
3227 | self._draw_model() |
---|
3228 | |
---|
3229 | |
---|
3230 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
3231 | |
---|
3232 | def _set_smear_buttons(self): |
---|
3233 | """ |
---|
3234 | Set semarer radio buttons |
---|
3235 | """ |
---|
3236 | # more disables for 2D |
---|
3237 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3238 | self.enable2D: |
---|
3239 | self.slit_smearer.Disable() |
---|
3240 | self.pinhole_smearer.Enable(True) |
---|
3241 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
3242 | else: |
---|
3243 | self.slit_smearer.Enable(True) |
---|
3244 | self.pinhole_smearer.Enable(True) |
---|
3245 | |
---|
3246 | |
---|
3247 | class BGTextCtrl(wx.TextCtrl): |
---|
3248 | """ |
---|
3249 | Text control used to display outputs. |
---|
3250 | No editing allowed. The background is |
---|
3251 | grayed out. User can't select text. |
---|
3252 | """ |
---|
3253 | def __init__(self, *args, **kwds): |
---|
3254 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
3255 | self.SetEditable(False) |
---|
3256 | self.SetBackgroundColour(self.GetParent().parent.GetBackgroundColour()) |
---|
3257 | |
---|
3258 | # Bind to mouse event to avoid text highlighting |
---|
3259 | # The event will be skipped once the call-back |
---|
3260 | # is called. |
---|
3261 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
3262 | |
---|
3263 | def _click(self, event): |
---|
3264 | """ |
---|
3265 | Prevent further handling of the mouse event |
---|
3266 | by not calling Skip(). |
---|
3267 | """ |
---|
3268 | pass |
---|
3269 | |
---|