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