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