1 | |
---|
2 | |
---|
3 | import sys |
---|
4 | import wx |
---|
5 | import wx.lib.newevent |
---|
6 | import numpy |
---|
7 | import copy |
---|
8 | import math |
---|
9 | import time |
---|
10 | from sans.models.dispersion_models import ArrayDispersion, GaussianDispersion |
---|
11 | #from sans.dataloader.data_info import Data1D |
---|
12 | from sans.guiframe.events import StatusEvent |
---|
13 | from sans.guiframe.events import NewPlotEvent |
---|
14 | from sans.guiframe.dataFitting import check_data_validity |
---|
15 | from sans.guiframe.utils import format_number |
---|
16 | from sans.guiframe.utils import check_float |
---|
17 | |
---|
18 | (Chi2UpdateEvent, EVT_CHI2_UPDATE) = wx.lib.newevent.NewEvent() |
---|
19 | _BOX_WIDTH = 76 |
---|
20 | _DATA_BOX_WIDTH = 300 |
---|
21 | SMEAR_SIZE_L = 0.00 |
---|
22 | SMEAR_SIZE_H = 0.00 |
---|
23 | |
---|
24 | import basepage |
---|
25 | from basepage import BasicPage |
---|
26 | from basepage import PageInfoEvent |
---|
27 | from sans.models.qsmearing import smear_selection |
---|
28 | |
---|
29 | class FitPage(BasicPage): |
---|
30 | """ |
---|
31 | FitPanel class contains fields allowing to display results when |
---|
32 | fitting a model and one data |
---|
33 | |
---|
34 | :note: For Fit to be performed the user should check at least one parameter |
---|
35 | on fit Panel window. |
---|
36 | """ |
---|
37 | |
---|
38 | def __init__(self,parent, color='rand'): |
---|
39 | """ |
---|
40 | Initialization of the Panel |
---|
41 | """ |
---|
42 | BasicPage.__init__(self, parent, color=color) |
---|
43 | |
---|
44 | ## draw sizer |
---|
45 | self._fill_data_sizer() |
---|
46 | self.is_2D = None |
---|
47 | self.fit_started = False |
---|
48 | self.weightbt_string = None |
---|
49 | # get smear info from data |
---|
50 | self._get_smear_info() |
---|
51 | self._fill_model_sizer( self.sizer1) |
---|
52 | self._get_defult_custom_smear() |
---|
53 | self._fill_range_sizer() |
---|
54 | self._set_smear(self.data) |
---|
55 | self.Bind(EVT_CHI2_UPDATE, self.on_complete_chisqr) |
---|
56 | # bind key event |
---|
57 | self.Bind(wx.EVT_RIGHT_DOWN, self.on_right_down) |
---|
58 | self._set_bookmark_flag(False) |
---|
59 | self._set_save_flag(False) |
---|
60 | self._set_preview_flag(False) |
---|
61 | self._set_copy_flag(False) |
---|
62 | self._set_paste_flag(False) |
---|
63 | self.btFit.SetFocus() |
---|
64 | self.enable_fit_button() |
---|
65 | self.fill_data_combobox(data_list=self.data_list) |
---|
66 | #create a default data for an empty panel |
---|
67 | self.create_default_data() |
---|
68 | |
---|
69 | def enable_fit_button(self): |
---|
70 | """ |
---|
71 | Enable fit button if data is valid and model is valid |
---|
72 | """ |
---|
73 | flag = check_data_validity(self.data) & (self.model is not None) |
---|
74 | self.btFit.Enable(flag) |
---|
75 | |
---|
76 | def _fill_data_sizer(self): |
---|
77 | """ |
---|
78 | fill sizer 0 with data info |
---|
79 | """ |
---|
80 | box_description= wx.StaticBox(self, -1, 'I(q) Data Source') |
---|
81 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
82 | #---------------------------------------------------------- |
---|
83 | sizer_data = wx.BoxSizer(wx.HORIZONTAL) |
---|
84 | self.dataSource = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
85 | wx.EVT_COMBOBOX(self.dataSource, -1, self.on_select_data) |
---|
86 | self.dataSource.SetMinSize((_DATA_BOX_WIDTH, -1)) |
---|
87 | sizer_data.Add(wx.StaticText(self, -1, 'Name : ')) |
---|
88 | sizer_data.Add(self.dataSource) |
---|
89 | sizer_data.Add( (0,5) ) |
---|
90 | boxsizer1.Add(sizer_data,0, wx.ALL, 10) |
---|
91 | self.sizer0.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
92 | self.sizer0.Layout() |
---|
93 | |
---|
94 | def enable_datasource(self): |
---|
95 | """ |
---|
96 | Enable or disable data source control depending on existing data |
---|
97 | """ |
---|
98 | if not self.data_list: |
---|
99 | self.dataSource.Disable() |
---|
100 | else: |
---|
101 | self.dataSource.Enable() |
---|
102 | |
---|
103 | def fill_data_combobox(self, data_list): |
---|
104 | """ |
---|
105 | Get a list of data and fill the corresponding combobox |
---|
106 | """ |
---|
107 | self.dataSource.Clear() |
---|
108 | self.data_list = data_list |
---|
109 | self.enable_datasource() |
---|
110 | if len(data_list) > 0: |
---|
111 | #find the maximum range covering all data |
---|
112 | qmin, qmax, npts = self.compute_data_set_range(data_list) |
---|
113 | self.qmin_data_set = qmin |
---|
114 | self.qmax_data_set = qmax |
---|
115 | self.npts_data_set = npts |
---|
116 | |
---|
117 | self.qmin.SetValue(str(self.qmin_data_set)) |
---|
118 | self.qmax.SetValue(str(self.qmax_data_set)) |
---|
119 | self.qmin.SetBackgroundColour("white") |
---|
120 | self.qmax.SetBackgroundColour("white") |
---|
121 | self.qmin_x = self.qmin_data_set |
---|
122 | self.qmax_x = self.qmax_data_set |
---|
123 | self.state.qmin = self.qmin_x |
---|
124 | self.state.qmax = self.qmax_x |
---|
125 | |
---|
126 | for data in self.data_list: |
---|
127 | if data is not None: |
---|
128 | self.dataSource.Append(str(data.name), clientData=data) |
---|
129 | self.dataSource.SetSelection(0) |
---|
130 | self.on_select_data(event=None) |
---|
131 | |
---|
132 | def on_select_data(self, event=None): |
---|
133 | """ |
---|
134 | """ |
---|
135 | if event is None and self.dataSource.GetCount() > 0: |
---|
136 | data = self.dataSource.GetClientData(0) |
---|
137 | self.set_data(data) |
---|
138 | elif self.dataSource.GetCount() > 0: |
---|
139 | pos = self.dataSource.GetSelection() |
---|
140 | data = self.dataSource.GetClientData(pos) |
---|
141 | self.set_data(data) |
---|
142 | |
---|
143 | |
---|
144 | |
---|
145 | def _on_fit_complete(self): |
---|
146 | """ |
---|
147 | When fit is complete ,reset the fit button label. |
---|
148 | """ |
---|
149 | self.btFit.SetLabel("Fit") |
---|
150 | self.bind_fit_button() |
---|
151 | |
---|
152 | def _is_2D(self): |
---|
153 | """ |
---|
154 | Check if data_name is Data2D |
---|
155 | |
---|
156 | :return: True or False |
---|
157 | |
---|
158 | """ |
---|
159 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
160 | self.enable2D: |
---|
161 | return True |
---|
162 | return False |
---|
163 | |
---|
164 | def _on_engine_change(self, name): |
---|
165 | """ |
---|
166 | get the current name of the fit engine type |
---|
167 | and update the panel accordingly |
---|
168 | """ |
---|
169 | |
---|
170 | self.engine_type = str(name) |
---|
171 | self.state.engine_type = self.engine_type |
---|
172 | if not self.is_mac: |
---|
173 | if len(self.parameters) == 0: |
---|
174 | self.Layout() |
---|
175 | return |
---|
176 | self.Layout() |
---|
177 | self.Refresh() |
---|
178 | |
---|
179 | |
---|
180 | |
---|
181 | def _fill_range_sizer(self): |
---|
182 | """ |
---|
183 | Fill the sizer containing the plotting range |
---|
184 | add access to npts |
---|
185 | """ |
---|
186 | is_2Ddata = False |
---|
187 | |
---|
188 | # Check if data is 2D |
---|
189 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
190 | self.enable2D: |
---|
191 | is_2Ddata = True |
---|
192 | |
---|
193 | title = "Fitting" |
---|
194 | #smear messages & titles |
---|
195 | smear_message_none = "No smearing is selected..." |
---|
196 | smear_message_dqdata = "The dQ data is being used for smearing..." |
---|
197 | smear_message_2d = \ |
---|
198 | "Higher accuracy is very time-expensive. Use it with care..." |
---|
199 | smear_message_new_ssmear = \ |
---|
200 | "Please enter only the value of interest to customize smearing..." |
---|
201 | smear_message_new_psmear = \ |
---|
202 | "Please enter both; the dQ will be generated by interpolation..." |
---|
203 | smear_message_2d_x_title = "<dQp>[1/A]:" |
---|
204 | smear_message_2d_y_title = "<dQs>[1/A]:" |
---|
205 | smear_message_pinhole_min_title = "dQ_low[1/A]:" |
---|
206 | smear_message_pinhole_max_title = "dQ_high[1/A]:" |
---|
207 | smear_message_slit_height_title = "Slit height[1/A]:" |
---|
208 | smear_message_slit_width_title = "Slit width[1/A]:" |
---|
209 | |
---|
210 | self._get_smear_info() |
---|
211 | |
---|
212 | #Sizers |
---|
213 | box_description_range = wx.StaticBox(self, -1,str(title)) |
---|
214 | boxsizer_range = wx.StaticBoxSizer(box_description_range, wx.VERTICAL) |
---|
215 | self.sizer_set_smearer = wx.BoxSizer(wx.VERTICAL) |
---|
216 | sizer_smearer = wx.BoxSizer(wx.HORIZONTAL) |
---|
217 | self.sizer_new_smear= wx.BoxSizer(wx.HORIZONTAL) |
---|
218 | self.sizer_set_masking = wx.BoxSizer(wx.HORIZONTAL) |
---|
219 | sizer_chi2 = wx.BoxSizer(wx.VERTICAL) |
---|
220 | smear_set_box= wx.StaticBox(self, -1,'Set Instrumental Smearing') |
---|
221 | sizer_smearer_box = wx.StaticBoxSizer(smear_set_box, wx.HORIZONTAL) |
---|
222 | sizer_smearer_box.SetMinSize((_DATA_BOX_WIDTH, 60)) |
---|
223 | |
---|
224 | weighting_set_box= wx.StaticBox(self, -1,\ |
---|
225 | 'Set Weighting by Selecting dI Source') |
---|
226 | weighting_box = wx.StaticBoxSizer(weighting_set_box, wx.HORIZONTAL) |
---|
227 | sizer_weighting = wx.BoxSizer(wx.HORIZONTAL) |
---|
228 | weighting_box.SetMinSize((_DATA_BOX_WIDTH, 40)) |
---|
229 | #Filling the sizer containing weighting info. |
---|
230 | self.dI_noweight = wx.RadioButton(self, -1, 'No Weighting', |
---|
231 | style=wx.RB_GROUP) |
---|
232 | self.dI_didata = wx.RadioButton(self, -1, 'Use dI Data') |
---|
233 | self.dI_sqrdata = wx.RadioButton(self, -1, 'Use |sqrt(I Data)|') |
---|
234 | self.dI_idata = wx.RadioButton(self, -1, 'Use |I Data|') |
---|
235 | self.Bind(wx.EVT_RADIOBUTTON, self.onWeighting, |
---|
236 | id=self.dI_noweight.GetId()) |
---|
237 | self.Bind(wx.EVT_RADIOBUTTON, self.onWeighting, |
---|
238 | id=self.dI_didata.GetId()) |
---|
239 | self.Bind(wx.EVT_RADIOBUTTON, self.onWeighting, |
---|
240 | id=self.dI_sqrdata.GetId()) |
---|
241 | self.Bind(wx.EVT_RADIOBUTTON, self.onWeighting, |
---|
242 | id=self.dI_idata.GetId()) |
---|
243 | self.dI_didata.SetValue(True) |
---|
244 | # add 4 types of weighting to the sizer |
---|
245 | sizer_weighting.Add( self.dI_noweight,0, wx.LEFT, 10) |
---|
246 | sizer_weighting.Add((14,10)) |
---|
247 | sizer_weighting.Add( self.dI_didata) |
---|
248 | sizer_weighting.Add((14,10)) |
---|
249 | sizer_weighting.Add( self.dI_sqrdata) |
---|
250 | sizer_weighting.Add((14,10)) |
---|
251 | sizer_weighting.Add( self.dI_idata) |
---|
252 | sizer_weighting.Add((10,10)) |
---|
253 | self.dI_noweight.Enable(False) |
---|
254 | self.dI_didata.Enable(False) |
---|
255 | self.dI_sqrdata.Enable(False) |
---|
256 | self.dI_idata.Enable(False) |
---|
257 | weighting_box.Add(sizer_weighting) |
---|
258 | |
---|
259 | sizer_fit = wx.GridSizer(2, 4, 2, 6) |
---|
260 | |
---|
261 | # combobox for smear2d accuracy selection |
---|
262 | self.smear_accuracy = wx.ComboBox(self, -1,size=(50,-1), |
---|
263 | style=wx.CB_READONLY) |
---|
264 | self._set_accuracy_list() |
---|
265 | self.smear_accuracy.SetValue(self.smear2d_accuracy) |
---|
266 | self.smear_accuracy.SetSelection(0) |
---|
267 | self.smear_accuracy.SetToolTipString(\ |
---|
268 | "'Higher' uses more Gaussian points for smearing computation.") |
---|
269 | |
---|
270 | wx.EVT_COMBOBOX(self.smear_accuracy,-1, self._on_select_accuracy) |
---|
271 | |
---|
272 | #Fit button |
---|
273 | self.btFit = wx.Button(self,wx.NewId(),'Fit', size=(88,25)) |
---|
274 | self.default_bt_colour = self.btFit.GetDefaultAttributes() |
---|
275 | self.btFit.Bind(wx.EVT_BUTTON, self._onFit,id= self.btFit.GetId()) |
---|
276 | self.btFit.SetToolTipString("Start fitting.") |
---|
277 | |
---|
278 | #textcntrl for custom resolution |
---|
279 | self.smear_pinhole_max = self.ModelTextCtrl(self, -1, |
---|
280 | size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
281 | text_enter_callback = self.onPinholeSmear) |
---|
282 | self.smear_pinhole_min = self.ModelTextCtrl(self, -1, |
---|
283 | size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
284 | text_enter_callback = self.onPinholeSmear) |
---|
285 | self.smear_slit_height= self.ModelTextCtrl(self, -1, |
---|
286 | size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
287 | text_enter_callback = self.onSlitSmear) |
---|
288 | self.smear_slit_width = self.ModelTextCtrl(self, -1, |
---|
289 | size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
290 | text_enter_callback = self.onSlitSmear) |
---|
291 | |
---|
292 | ## smear |
---|
293 | self.smear_data_left= BGTextCtrl(self, -1, |
---|
294 | size=(_BOX_WIDTH-25,20), style=0) |
---|
295 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
296 | self.smear_data_right = BGTextCtrl(self, -1, |
---|
297 | size=(_BOX_WIDTH-25,20), style=0) |
---|
298 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
299 | |
---|
300 | #set default values for smear |
---|
301 | self.smear_pinhole_max.SetValue(str(self.dx_max)) |
---|
302 | self.smear_pinhole_min.SetValue(str(self.dx_min)) |
---|
303 | self.smear_slit_height.SetValue(str(self.dxl)) |
---|
304 | self.smear_slit_width.SetValue(str(self.dxw)) |
---|
305 | |
---|
306 | #Filling the sizer containing instruments smearing info. |
---|
307 | self.disable_smearer = wx.RadioButton(self, -1, |
---|
308 | 'None', style=wx.RB_GROUP) |
---|
309 | self.enable_smearer = wx.RadioButton(self, -1, |
---|
310 | 'Use dQ Data') |
---|
311 | #self.enable_smearer.SetToolTipString( |
---|
312 | #"Click to use the loaded dQ data for smearing.") |
---|
313 | self.pinhole_smearer = wx.RadioButton(self, -1, |
---|
314 | 'Custom Pinhole Smear') |
---|
315 | #self.pinhole_smearer.SetToolTipString |
---|
316 | #("Click to input custom resolution for pinhole smearing.") |
---|
317 | self.slit_smearer = wx.RadioButton(self, -1, 'Custom Slit Smear') |
---|
318 | #self.slit_smearer.SetToolTipString |
---|
319 | #("Click to input custom resolution for slit smearing.") |
---|
320 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, |
---|
321 | id=self.disable_smearer.GetId()) |
---|
322 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, |
---|
323 | id=self.enable_smearer.GetId()) |
---|
324 | self.Bind(wx.EVT_RADIOBUTTON, self.onPinholeSmear, |
---|
325 | id=self.pinhole_smearer.GetId()) |
---|
326 | self.Bind(wx.EVT_RADIOBUTTON, self.onSlitSmear, |
---|
327 | id=self.slit_smearer.GetId()) |
---|
328 | self.disable_smearer.SetValue(True) |
---|
329 | |
---|
330 | # add 4 types of smearing to the sizer |
---|
331 | sizer_smearer.Add( self.disable_smearer,0, wx.LEFT, 10) |
---|
332 | sizer_smearer.Add((10,10)) |
---|
333 | sizer_smearer.Add( self.enable_smearer) |
---|
334 | sizer_smearer.Add((10,10)) |
---|
335 | sizer_smearer.Add( self.pinhole_smearer ) |
---|
336 | sizer_smearer.Add((10,10)) |
---|
337 | sizer_smearer.Add( self.slit_smearer ) |
---|
338 | sizer_smearer.Add((10,10)) |
---|
339 | |
---|
340 | # StaticText for chi2, N(for fitting), Npts |
---|
341 | self.tcChi = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
342 | self.tcChi.SetToolTipString("Chi2/Npts(Fit)") |
---|
343 | self.Npts_fit = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
344 | self.Npts_fit.SetToolTipString(\ |
---|
345 | " Npts : number of points selected for fitting") |
---|
346 | self.Npts_total = self.ModelTextCtrl(self, -1, |
---|
347 | size=(_BOX_WIDTH, 20), |
---|
348 | style=wx.TE_PROCESS_ENTER, |
---|
349 | text_enter_callback=self._onQrangeEnter) |
---|
350 | self.Npts_total.SetValue(format_number(self.npts_x)) |
---|
351 | self.Npts_total.SetToolTipString(\ |
---|
352 | " Total Npts : total number of data points") |
---|
353 | |
---|
354 | # Update and Draw button |
---|
355 | self.draw_button = wx.Button(self,wx.NewId(),'Compute', size=(88,24)) |
---|
356 | self.draw_button.Bind(wx.EVT_BUTTON, \ |
---|
357 | self._onDraw,id= self.draw_button.GetId()) |
---|
358 | self.draw_button.SetToolTipString("Compute and Draw.") |
---|
359 | |
---|
360 | box_description_1= wx.StaticText(self, -1,' Chi2/Npts') |
---|
361 | box_description_2= wx.StaticText(self, -1,'Npts(Fit)') |
---|
362 | box_description_3= wx.StaticText(self, -1,'Total Npts') |
---|
363 | box_description_3.SetToolTipString( \ |
---|
364 | " Total Npts : total number of data points") |
---|
365 | #box_description_4= wx.StaticText(self, -1,' ') |
---|
366 | |
---|
367 | |
---|
368 | sizer_fit.Add(box_description_1,0,0) |
---|
369 | sizer_fit.Add(box_description_2,0,0) |
---|
370 | sizer_fit.Add(box_description_3,0,0) |
---|
371 | sizer_fit.Add(self.draw_button,0,0) |
---|
372 | sizer_fit.Add(self.tcChi,0,0) |
---|
373 | sizer_fit.Add(self.Npts_fit ,0,0) |
---|
374 | sizer_fit.Add(self.Npts_total,0,0) |
---|
375 | sizer_fit.Add(self.btFit,0,0) |
---|
376 | |
---|
377 | # StaticText for smear |
---|
378 | #self.tcChi = wx.StaticText(self, -1, "-", style=wx.ALIGN_LEFT) |
---|
379 | self.smear_description_none = wx.StaticText(self, -1, |
---|
380 | smear_message_none , style=wx.ALIGN_LEFT) |
---|
381 | self.smear_description_dqdata = wx.StaticText(self, |
---|
382 | -1, smear_message_dqdata , style=wx.ALIGN_LEFT) |
---|
383 | self.smear_description_type = wx.StaticText(self, |
---|
384 | -1, "Type:" , style=wx.ALIGN_LEFT) |
---|
385 | self.smear_description_accuracy_type = wx.StaticText(self, -1, |
---|
386 | "Accuracy:" , style=wx.ALIGN_LEFT) |
---|
387 | self.smear_description_smear_type = BGTextCtrl(self, -1, |
---|
388 | size=(57,20), style=0) |
---|
389 | self.smear_description_smear_type.SetValue(str(self.dq_l)) |
---|
390 | self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) |
---|
391 | self.smear_description_2d = wx.StaticText(self, -1, |
---|
392 | smear_message_2d , style=wx.ALIGN_LEFT) |
---|
393 | self.smear_message_new_s = wx.StaticText(self, -1, |
---|
394 | smear_message_new_ssmear, style=wx.ALIGN_LEFT) |
---|
395 | self.smear_message_new_p = wx.StaticText(self, -1, |
---|
396 | smear_message_new_psmear , style=wx.ALIGN_LEFT) |
---|
397 | self.smear_description_2d_x = wx.StaticText(self, -1, |
---|
398 | smear_message_2d_x_title , style=wx.ALIGN_LEFT) |
---|
399 | self.smear_description_2d_x.SetToolTipString(\ |
---|
400 | " dQp(parallel) in q_r direction.") |
---|
401 | self.smear_description_2d_y = wx.StaticText(self, -1, |
---|
402 | smear_message_2d_y_title , style=wx.ALIGN_LEFT) |
---|
403 | self.smear_description_2d_y.SetToolTipString(\ |
---|
404 | " dQs(perpendicular) in q_phi direction.") |
---|
405 | self.smear_description_pin_min = wx.StaticText(self, -1, |
---|
406 | smear_message_pinhole_min_title , style=wx.ALIGN_LEFT) |
---|
407 | self.smear_description_pin_max = wx.StaticText(self, -1, |
---|
408 | smear_message_pinhole_max_title , style=wx.ALIGN_LEFT) |
---|
409 | self.smear_description_slit_height = wx.StaticText(self, -1, |
---|
410 | smear_message_slit_height_title , style=wx.ALIGN_LEFT) |
---|
411 | self.smear_description_slit_width = wx.StaticText(self, -1, |
---|
412 | smear_message_slit_width_title , style=wx.ALIGN_LEFT) |
---|
413 | |
---|
414 | #arrange sizers |
---|
415 | #boxsizer1.Add( self.tcChi ) |
---|
416 | self.sizer_set_smearer.Add(sizer_smearer ) |
---|
417 | self.sizer_set_smearer.Add((10,10)) |
---|
418 | self.sizer_set_smearer.Add( self.smear_description_none, |
---|
419 | 0, wx.CENTER, 10 ) |
---|
420 | self.sizer_set_smearer.Add( self.smear_description_dqdata, |
---|
421 | 0, wx.CENTER, 10 ) |
---|
422 | self.sizer_set_smearer.Add( self.smear_description_2d, |
---|
423 | 0, wx.CENTER, 10 ) |
---|
424 | self.sizer_new_smear.Add( self.smear_description_type, |
---|
425 | 0, wx.CENTER, 10 ) |
---|
426 | self.sizer_new_smear.Add( self.smear_description_accuracy_type, |
---|
427 | 0, wx.CENTER, 10 ) |
---|
428 | self.sizer_new_smear.Add( self.smear_accuracy ) |
---|
429 | self.sizer_new_smear.Add( self.smear_description_smear_type, |
---|
430 | 0, wx.CENTER, 10 ) |
---|
431 | self.sizer_new_smear.Add((15,-1)) |
---|
432 | self.sizer_new_smear.Add( self.smear_description_2d_x, |
---|
433 | 0, wx.CENTER, 10 ) |
---|
434 | self.sizer_new_smear.Add( self.smear_description_pin_min, |
---|
435 | 0, wx.CENTER, 10 ) |
---|
436 | self.sizer_new_smear.Add( self.smear_description_slit_height, |
---|
437 | 0, wx.CENTER, 10 ) |
---|
438 | |
---|
439 | self.sizer_new_smear.Add( self.smear_pinhole_min, |
---|
440 | 0, wx.CENTER, 10 ) |
---|
441 | self.sizer_new_smear.Add( self.smear_slit_height, |
---|
442 | 0, wx.CENTER, 10 ) |
---|
443 | self.sizer_new_smear.Add( self.smear_data_left, |
---|
444 | 0, wx.CENTER, 10 ) |
---|
445 | self.sizer_new_smear.Add((20,-1)) |
---|
446 | self.sizer_new_smear.Add( self.smear_description_2d_y, |
---|
447 | 0, wx.CENTER, 10 ) |
---|
448 | self.sizer_new_smear.Add( self.smear_description_pin_max, |
---|
449 | 0, wx.CENTER, 10 ) |
---|
450 | self.sizer_new_smear.Add( self.smear_description_slit_width, |
---|
451 | 0, wx.CENTER, 10 ) |
---|
452 | |
---|
453 | self.sizer_new_smear.Add( self.smear_pinhole_max,0, wx.CENTER, 10 ) |
---|
454 | self.sizer_new_smear.Add( self.smear_slit_width,0, wx.CENTER, 10 ) |
---|
455 | self.sizer_new_smear.Add( self.smear_data_right,0, wx.CENTER, 10 ) |
---|
456 | |
---|
457 | self.sizer_set_smearer.Add( self.smear_message_new_s,0, wx.CENTER, 10) |
---|
458 | self.sizer_set_smearer.Add( self.smear_message_new_p,0, wx.CENTER, 10) |
---|
459 | self.sizer_set_smearer.Add((5,2)) |
---|
460 | self.sizer_set_smearer.Add( self.sizer_new_smear,0, wx.CENTER, 10 ) |
---|
461 | |
---|
462 | # add all to chi2 sizer |
---|
463 | sizer_smearer_box.Add(self.sizer_set_smearer) |
---|
464 | sizer_chi2.Add(sizer_smearer_box) |
---|
465 | sizer_chi2.Add((-1,5)) |
---|
466 | sizer_chi2.Add(weighting_box) |
---|
467 | sizer_chi2.Add((-1,5)) |
---|
468 | |
---|
469 | # hide all smear messages and textctrl |
---|
470 | self._hide_all_smear_info() |
---|
471 | |
---|
472 | # get smear_selection |
---|
473 | self.current_smearer= smear_selection( self.data, self.model ) |
---|
474 | |
---|
475 | # Show only the relevant smear messages, etc |
---|
476 | if self.current_smearer == None: |
---|
477 | if not is_2Ddata: |
---|
478 | self.smear_description_none.Show(True) |
---|
479 | self.enable_smearer.Disable() |
---|
480 | else: |
---|
481 | self.smear_description_none.Show(True) |
---|
482 | #self.smear_description_2d.Show(True) |
---|
483 | #self.pinhole_smearer.Disable() |
---|
484 | self.slit_smearer.Disable() |
---|
485 | #self.enable_smearer.Disable() |
---|
486 | if self.data == None: |
---|
487 | self.slit_smearer.Disable() |
---|
488 | self.pinhole_smearer.Disable() |
---|
489 | self.enable_smearer.Disable() |
---|
490 | else: self._show_smear_sizer() |
---|
491 | boxsizer_range.Add(self.sizer_set_masking) |
---|
492 | #2D data? default |
---|
493 | is_2Ddata = False |
---|
494 | |
---|
495 | #check if it is 2D data |
---|
496 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
497 | self.enable2D: |
---|
498 | is_2Ddata = True |
---|
499 | |
---|
500 | self.sizer5.Clear(True) |
---|
501 | |
---|
502 | self.qmin = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20), |
---|
503 | style=wx.TE_PROCESS_ENTER, |
---|
504 | text_enter_callback = self._onQrangeEnter) |
---|
505 | self.qmin.SetValue(str(self.qmin_x)) |
---|
506 | self.qmin.SetToolTipString("Minimun value of Q in linear scale.") |
---|
507 | |
---|
508 | self.qmax = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20), |
---|
509 | style=wx.TE_PROCESS_ENTER, |
---|
510 | text_enter_callback=self._onQrangeEnter) |
---|
511 | self.qmax.SetValue(str(self.qmax_x)) |
---|
512 | self.qmax.SetToolTipString("Maximum value of Q in linear scale.") |
---|
513 | """ |
---|
514 | self.theory_npts_tcrtl = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH, 20), |
---|
515 | style=wx.TE_PROCESS_ENTER, |
---|
516 | text_enter_callback=self._onQrangeEnter) |
---|
517 | self.theory_npts_tcrtl.SetValue(format_number(self.npts_x)) |
---|
518 | self.theory_npts_tcrtl.SetToolTipString("Number of point to plot.") |
---|
519 | """ |
---|
520 | id = wx.NewId() |
---|
521 | self.reset_qrange =wx.Button(self,id,'Reset',size=(77,20)) |
---|
522 | |
---|
523 | self.reset_qrange.Bind(wx.EVT_BUTTON, self.on_reset_clicked,id=id) |
---|
524 | self.reset_qrange.SetToolTipString("Reset Q range to the default values") |
---|
525 | |
---|
526 | sizer_horizontal=wx.BoxSizer(wx.HORIZONTAL) |
---|
527 | sizer= wx.GridSizer(2, 4, 2, 6) |
---|
528 | |
---|
529 | self.btEditMask = wx.Button(self,wx.NewId(),'Editor', size=(88,23)) |
---|
530 | self.btEditMask.Bind(wx.EVT_BUTTON, self._onMask,id= self.btEditMask.GetId()) |
---|
531 | self.btEditMask.SetToolTipString("Edit Mask.") |
---|
532 | self.EditMask_title = wx.StaticText(self, -1, ' Masking(2D)') |
---|
533 | |
---|
534 | sizer.Add(wx.StaticText(self, -1, ' Q range')) |
---|
535 | sizer.Add(wx.StaticText(self, -1, ' Min[1/A]')) |
---|
536 | sizer.Add(wx.StaticText(self, -1, ' Max[1/A]')) |
---|
537 | sizer.Add(self.EditMask_title) |
---|
538 | #sizer.Add(wx.StaticText(self, -1, '')) |
---|
539 | sizer.Add(self.reset_qrange) |
---|
540 | sizer.Add(self.qmin) |
---|
541 | sizer.Add(self.qmax) |
---|
542 | #sizer.Add(self.theory_npts_tcrtl) |
---|
543 | sizer.Add(self.btEditMask) |
---|
544 | boxsizer_range.Add(sizer_chi2) |
---|
545 | boxsizer_range.Add((10,10)) |
---|
546 | boxsizer_range.Add(sizer) |
---|
547 | |
---|
548 | boxsizer_range.Add((10,15)) |
---|
549 | boxsizer_range.Add(sizer_fit) |
---|
550 | if is_2Ddata: |
---|
551 | self.btEditMask.Enable() |
---|
552 | self.EditMask_title.Enable() |
---|
553 | else: |
---|
554 | self.btEditMask.Disable() |
---|
555 | self.EditMask_title.Disable() |
---|
556 | ## save state |
---|
557 | self.save_current_state() |
---|
558 | self.sizer5.Add(boxsizer_range,0, wx.EXPAND | wx.ALL, 10) |
---|
559 | self.sizer5.Layout() |
---|
560 | |
---|
561 | |
---|
562 | def _fill_model_sizer(self, sizer): |
---|
563 | """ |
---|
564 | fill sizer containing model info |
---|
565 | """ |
---|
566 | ##Add model function Details button in fitpanel. |
---|
567 | ##The following 3 lines are for Mac. Let JHC know before modifying... |
---|
568 | title = "Model" |
---|
569 | self.formfactorbox = None |
---|
570 | self.multifactorbox = None |
---|
571 | self.mbox_description= wx.StaticBox(self, -1,str(title)) |
---|
572 | boxsizer1 = wx.StaticBoxSizer(self.mbox_description, wx.VERTICAL) |
---|
573 | |
---|
574 | id = wx.NewId() |
---|
575 | self.model_help =wx.Button(self,id,'Details', size=(80,23)) |
---|
576 | self.model_help.Bind(wx.EVT_BUTTON, self.on_model_help_clicked,id=id) |
---|
577 | self.model_help.SetToolTipString("Model Function Help") |
---|
578 | id = wx.NewId() |
---|
579 | self.model_view = wx.Button(self, id,"Show 2D", size=(80, 23)) |
---|
580 | self.model_view.Bind(wx.EVT_BUTTON, self._onModel2D, id=id) |
---|
581 | hint = "toggle view of model from 1D to 2D or 2D to 1D" |
---|
582 | self.model_view.SetToolTipString(hint) |
---|
583 | |
---|
584 | self.shape_rbutton = wx.RadioButton(self, -1, 'Shapes', |
---|
585 | style=wx.RB_GROUP) |
---|
586 | self.shape_indep_rbutton = wx.RadioButton(self, -1, "Shape-Independent") |
---|
587 | self.struct_rbutton = wx.RadioButton(self, -1, "Structure Factor ") |
---|
588 | self.plugin_rbutton = wx.RadioButton(self, -1, "Customized Models") |
---|
589 | |
---|
590 | self.Bind(wx.EVT_RADIOBUTTON, self._show_combox, |
---|
591 | id= self.shape_rbutton.GetId()) |
---|
592 | self.Bind(wx.EVT_RADIOBUTTON, self._show_combox, |
---|
593 | id= self.shape_indep_rbutton.GetId()) |
---|
594 | self.Bind(wx.EVT_RADIOBUTTON, self._show_combox, |
---|
595 | id= self.struct_rbutton.GetId()) |
---|
596 | self.Bind(wx.EVT_RADIOBUTTON, self._show_combox, |
---|
597 | id= self.plugin_rbutton.GetId()) |
---|
598 | #MAC needs SetValue |
---|
599 | self.shape_rbutton.SetValue(True) |
---|
600 | |
---|
601 | sizer_radiobutton = wx.GridSizer(2, 3, 5, 5) |
---|
602 | sizer_radiobutton.Add(self.shape_rbutton) |
---|
603 | sizer_radiobutton.Add(self.shape_indep_rbutton) |
---|
604 | #sizer_radiobutton.Add((5, 5)) |
---|
605 | sizer_radiobutton.Add(self.model_view,1, wx.LEFT, 20) |
---|
606 | sizer_radiobutton.Add(self.plugin_rbutton) |
---|
607 | sizer_radiobutton.Add(self.struct_rbutton) |
---|
608 | #sizer_radiobutton.Add((5, 5)) |
---|
609 | sizer_radiobutton.Add(self.model_help,1, wx.LEFT, 20) |
---|
610 | |
---|
611 | sizer_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
612 | mutifactor_selection = wx.BoxSizer(wx.HORIZONTAL) |
---|
613 | |
---|
614 | self.text1 = wx.StaticText(self,-1,"" ) |
---|
615 | self.text2 = wx.StaticText(self,-1,"P(Q)*S(Q)" ) |
---|
616 | self.mutifactor_text = wx.StaticText( self,-1,"No. of Shells: ") |
---|
617 | self.mutifactor_text1 = wx.StaticText( self,-1,"" ) |
---|
618 | self.show_sld_button = wx.Button( self,-1,"Show SLD Profile" ) |
---|
619 | self.show_sld_button.Bind(wx.EVT_BUTTON,self._on_show_sld) |
---|
620 | |
---|
621 | self.formfactorbox = wx.ComboBox(self, -1,style=wx.CB_READONLY) |
---|
622 | if self.model!= None: |
---|
623 | self.formfactorbox.SetValue(self.model.name) |
---|
624 | self.structurebox = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
625 | self.multifactorbox = wx.ComboBox(self, -1, style=wx.CB_READONLY) |
---|
626 | self.initialize_combox() |
---|
627 | wx.EVT_COMBOBOX(self.formfactorbox, -1, self._on_select_model) |
---|
628 | |
---|
629 | wx.EVT_COMBOBOX(self.structurebox, -1, self._on_select_model) |
---|
630 | wx.EVT_COMBOBOX(self.multifactorbox, -1, self._on_select_model) |
---|
631 | ## check model type to show sizer |
---|
632 | if self.model !=None: |
---|
633 | self._set_model_sizer_selection(self.model) |
---|
634 | |
---|
635 | sizer_selection.Add(self.text1) |
---|
636 | sizer_selection.Add((5, 5)) |
---|
637 | sizer_selection.Add(self.formfactorbox) |
---|
638 | sizer_selection.Add((5, 5)) |
---|
639 | sizer_selection.Add(self.text2) |
---|
640 | sizer_selection.Add((5, 5)) |
---|
641 | sizer_selection.Add(self.structurebox) |
---|
642 | |
---|
643 | mutifactor_selection.Add((10,5)) |
---|
644 | mutifactor_selection.Add(self.mutifactor_text) |
---|
645 | mutifactor_selection.Add(self.multifactorbox) |
---|
646 | mutifactor_selection.Add((5, 5)) |
---|
647 | mutifactor_selection.Add(self.mutifactor_text1) |
---|
648 | mutifactor_selection.Add((10, 5)) |
---|
649 | mutifactor_selection.Add(self.show_sld_button) |
---|
650 | |
---|
651 | |
---|
652 | boxsizer1.Add(sizer_radiobutton) |
---|
653 | boxsizer1.Add((10, 10)) |
---|
654 | boxsizer1.Add(sizer_selection) |
---|
655 | boxsizer1.Add((10, 10)) |
---|
656 | boxsizer1.Add(mutifactor_selection) |
---|
657 | |
---|
658 | self._set_multfactor_combobox() |
---|
659 | self.multifactorbox.SetSelection(1) |
---|
660 | self.show_sld_button.Hide() |
---|
661 | sizer.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
662 | sizer.Layout() |
---|
663 | |
---|
664 | def _set_sizer_dispersion(self): |
---|
665 | """ |
---|
666 | draw sizer with gaussian dispersity parameters |
---|
667 | """ |
---|
668 | self.fittable_param=[] |
---|
669 | self.fixed_param=[] |
---|
670 | self.orientation_params_disp=[] |
---|
671 | |
---|
672 | self.sizer4_4.Clear(True) |
---|
673 | if self.model==None: |
---|
674 | ##no model is selected |
---|
675 | return |
---|
676 | if not self.enable_disp.GetValue(): |
---|
677 | ## the user didn't select dispersity display |
---|
678 | return |
---|
679 | |
---|
680 | self._reset_dispersity() |
---|
681 | |
---|
682 | ## fill a sizer with the combobox to select dispersion type |
---|
683 | #sizer_select_dispers = wx.BoxSizer(wx.HORIZONTAL) |
---|
684 | model_disp = wx.StaticText(self, -1, 'Function') |
---|
685 | |
---|
686 | import sans.models.dispersion_models |
---|
687 | self.polydisp= sans.models.dispersion_models.models |
---|
688 | |
---|
689 | ix = 0 |
---|
690 | iy = 0 |
---|
691 | disp = wx.StaticText(self, -1, ' ') |
---|
692 | self.sizer4_4.Add(disp,( iy, ix),(1,1), |
---|
693 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
694 | ix += 1 |
---|
695 | values = wx.StaticText(self, -1, 'PD[ratio]') |
---|
696 | polytext = "Polydispersity (= STD/mean); " |
---|
697 | polytext += "the standard deviation over the mean value." |
---|
698 | values.SetToolTipString(polytext) |
---|
699 | |
---|
700 | self.sizer4_4.Add(values,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, |
---|
701 | 0) |
---|
702 | ix +=2 |
---|
703 | if self.is_mac: |
---|
704 | err_text = 'Error' |
---|
705 | else: |
---|
706 | err_text = '' |
---|
707 | self.text_disp_1 = wx.StaticText(self, -1, err_text) |
---|
708 | self.sizer4_4.Add( self.text_disp_1,(iy, ix),(1,1),\ |
---|
709 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
710 | |
---|
711 | |
---|
712 | ix +=1 |
---|
713 | self.text_disp_min = wx.StaticText(self, -1, 'Min') |
---|
714 | self.sizer4_4.Add(self.text_disp_min,(iy, ix),(1,1),\ |
---|
715 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
716 | |
---|
717 | ix +=1 |
---|
718 | self.text_disp_max = wx.StaticText(self, -1, 'Max') |
---|
719 | self.sizer4_4.Add(self.text_disp_max,(iy, ix),(1,1),\ |
---|
720 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
721 | |
---|
722 | ix += 1 |
---|
723 | npts = wx.StaticText(self, -1, 'Npts') |
---|
724 | npts.SetToolTipString("Number of sampling points for the numerical\n\ |
---|
725 | integration over the distribution function.") |
---|
726 | self.sizer4_4.Add(npts,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
727 | ix += 1 |
---|
728 | nsigmas = wx.StaticText(self, -1, 'Nsigs') |
---|
729 | nsigmas.SetToolTipString(" Number of sigmas between which the range\n\ |
---|
730 | of the distribution function will be used for weighting. \n\ |
---|
731 | The value '3' covers 99.5% for Gaussian distribution \n\ |
---|
732 | function. Note: Not recommended to change this value.") |
---|
733 | self.sizer4_4.Add(nsigmas,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, |
---|
734 | 0) |
---|
735 | ix +=1 |
---|
736 | self.sizer4_4.Add(model_disp,(iy, ix),(1,1),\ |
---|
737 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
738 | |
---|
739 | if self.engine_type=="park": |
---|
740 | self.text_disp_max.Show(True) |
---|
741 | self.text_disp_min.Show(True) |
---|
742 | |
---|
743 | for item in self.model.dispersion.keys(): |
---|
744 | if not item in self.model.orientation_params: |
---|
745 | if not self.disp_cb_dict.has_key(item): |
---|
746 | self.disp_cb_dict[item]= None |
---|
747 | name0="Distribution of " + item |
---|
748 | name1=item+".width" |
---|
749 | name2=item+".npts" |
---|
750 | name3=item+".nsigmas" |
---|
751 | if not self.model.details.has_key(name1): |
---|
752 | self.model.details [name1] = ["",None,None] |
---|
753 | |
---|
754 | iy += 1 |
---|
755 | for p in self.model.dispersion[item].keys(): |
---|
756 | |
---|
757 | if p=="width": |
---|
758 | ix = 0 |
---|
759 | cb = wx.CheckBox(self, -1, name0, (10, 10)) |
---|
760 | cb.SetToolTipString("Check mark to fit") |
---|
761 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
762 | self.sizer4_4.Add( cb,( iy, ix),(1,1), |
---|
763 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, |
---|
764 | 5) |
---|
765 | ix = 1 |
---|
766 | value= self.model.getParam(name1) |
---|
767 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/1.3,20) |
---|
768 | ,style=wx.TE_PROCESS_ENTER) |
---|
769 | ctl1.SetLabel('PD[ratio]') |
---|
770 | poly_text = "Polydispersity (STD/mean) of %s\n" % item |
---|
771 | poly_text += "STD: the standard deviation" |
---|
772 | poly_text += " from the mean value." |
---|
773 | ctl1.SetToolTipString(poly_text) |
---|
774 | ctl1.SetValue(str (format_number(value, True))) |
---|
775 | self.sizer4_4.Add(ctl1, (iy,ix),(1,1),wx.EXPAND) |
---|
776 | ## text to show error sign |
---|
777 | ix = 2 |
---|
778 | text2=wx.StaticText(self, -1, '+/-') |
---|
779 | self.sizer4_4.Add(text2,(iy, ix),(1,1), |
---|
780 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
781 | if not self.is_mac: |
---|
782 | text2.Hide() |
---|
783 | |
---|
784 | ix = 3 |
---|
785 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/1.3,20), |
---|
786 | style=0) |
---|
787 | |
---|
788 | self.sizer4_4.Add(ctl2, (iy,ix),(1,1), |
---|
789 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
790 | if not self.is_mac: |
---|
791 | ctl2.Hide() |
---|
792 | |
---|
793 | ix = 4 |
---|
794 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2, |
---|
795 | 20), style=wx.TE_PROCESS_ENTER, |
---|
796 | text_enter_callback = self._onparamRangeEnter) |
---|
797 | |
---|
798 | self.sizer4_4.Add(ctl3, (iy,ix),(1,1), |
---|
799 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
800 | |
---|
801 | |
---|
802 | ix = 5 |
---|
803 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2, |
---|
804 | 20), style=wx.TE_PROCESS_ENTER, |
---|
805 | text_enter_callback = self._onparamRangeEnter) |
---|
806 | |
---|
807 | self.sizer4_4.Add(ctl4, (iy,ix),(1,1), |
---|
808 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
809 | |
---|
810 | |
---|
811 | if self.engine_type=="park": |
---|
812 | ctl3.Show(True) |
---|
813 | ctl4.Show(True) |
---|
814 | |
---|
815 | elif p=="npts": |
---|
816 | ix = 6 |
---|
817 | value= self.model.getParam(name2) |
---|
818 | Tctl = self.ModelTextCtrl(self, -1, |
---|
819 | size=(_BOX_WIDTH/2.2,20), |
---|
820 | style=wx.TE_PROCESS_ENTER) |
---|
821 | |
---|
822 | Tctl.SetValue(str (format_number(value))) |
---|
823 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
824 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
825 | self.fixed_param.append([None,name2, Tctl,None,None, |
---|
826 | None, None,None]) |
---|
827 | elif p=="nsigmas": |
---|
828 | ix = 7 |
---|
829 | value= self.model.getParam(name3) |
---|
830 | Tct2 = self.ModelTextCtrl(self, -1, |
---|
831 | size=(_BOX_WIDTH/2.2,20), |
---|
832 | style=wx.TE_PROCESS_ENTER) |
---|
833 | |
---|
834 | Tct2.SetValue(str (format_number(value))) |
---|
835 | self.sizer4_4.Add(Tct2, (iy,ix),(1,1), |
---|
836 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
837 | #ix +=1 |
---|
838 | #self.sizer4_4.Add((20,20), (iy,ix),(1,1), |
---|
839 | # wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
840 | |
---|
841 | self.fixed_param.append([None, name3, Tct2, |
---|
842 | None, None, None, |
---|
843 | None, None]) |
---|
844 | |
---|
845 | |
---|
846 | ix = 8 |
---|
847 | disp_box = wx.ComboBox(self, -1,size=(65,-1), |
---|
848 | style=wx.CB_READONLY, name = '%s'% name1) |
---|
849 | for key, value in self.polydisp.iteritems(): |
---|
850 | name_disp = str(key) |
---|
851 | disp_box.Append(name_disp,value) |
---|
852 | disp_box.SetStringSelection("gaussian") |
---|
853 | wx.EVT_COMBOBOX(disp_box,-1, self._on_disp_func) |
---|
854 | self.sizer4_4.Add(disp_box,(iy,ix),(1,1), wx.EXPAND) |
---|
855 | self.fittable_param.append([cb,name1,ctl1,text2, |
---|
856 | ctl2, ctl3, ctl4, disp_box]) |
---|
857 | |
---|
858 | ix =0 |
---|
859 | iy +=1 |
---|
860 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), |
---|
861 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
862 | first_orient = True |
---|
863 | for item in self.model.dispersion.keys(): |
---|
864 | if item in self.model.orientation_params: |
---|
865 | if not self.disp_cb_dict.has_key(item): |
---|
866 | self.disp_cb_dict[item]= None |
---|
867 | name0="Distribution of " + item |
---|
868 | name1=item+".width" |
---|
869 | name2=item+".npts" |
---|
870 | name3=item+".nsigmas" |
---|
871 | |
---|
872 | if not self.model.details.has_key(name1): |
---|
873 | self.model.details [name1] = ["",None,None] |
---|
874 | |
---|
875 | |
---|
876 | iy += 1 |
---|
877 | for p in self.model.dispersion[item].keys(): |
---|
878 | |
---|
879 | if p=="width": |
---|
880 | ix = 0 |
---|
881 | cb = wx.CheckBox(self, -1, name0, (10, 10)) |
---|
882 | cb.SetToolTipString("Check mark to fit") |
---|
883 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
884 | self.sizer4_4.Add( cb,( iy, ix),(1,1), |
---|
885 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, |
---|
886 | 5) |
---|
887 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
888 | self.enable2D: |
---|
889 | cb.Show(True) |
---|
890 | elif cb.IsShown(): |
---|
891 | cb.Hide() |
---|
892 | ix = 1 |
---|
893 | value= self.model.getParam(name1) |
---|
894 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/1.3, |
---|
895 | 20),style=wx.TE_PROCESS_ENTER) |
---|
896 | poly_tip = "Absolute Sigma for %s." % item |
---|
897 | ctl1.SetToolTipString(poly_tip) |
---|
898 | ctl1.SetValue(str (format_number(value, True))) |
---|
899 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
900 | self.enable2D: |
---|
901 | if first_orient: |
---|
902 | values.SetLabel('PD[ratio], Sig[deg]') |
---|
903 | poly_text = "PD(polydispersity for lengths):\n" |
---|
904 | poly_text += "It should be a value between" |
---|
905 | poly_text += "0 and 1\n" |
---|
906 | poly_text += "Sigma for angles: \n" |
---|
907 | poly_text += "It is the STD (ratio*mean)" |
---|
908 | poly_text += " of the distribution.\n " |
---|
909 | |
---|
910 | values.SetToolTipString(poly_text) |
---|
911 | first_orient = False |
---|
912 | ctl1.Show(True) |
---|
913 | elif ctl1.IsShown(): |
---|
914 | ctl1.Hide() |
---|
915 | |
---|
916 | self.sizer4_4.Add(ctl1, (iy,ix),(1,1),wx.EXPAND) |
---|
917 | ## text to show error sign |
---|
918 | ix = 2 |
---|
919 | text2=wx.StaticText(self, -1, '+/-') |
---|
920 | self.sizer4_4.Add(text2,(iy, ix),(1,1), |
---|
921 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
922 | |
---|
923 | text2.Hide() |
---|
924 | |
---|
925 | ix = 3 |
---|
926 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/1.3,20), |
---|
927 | style=0) |
---|
928 | |
---|
929 | self.sizer4_4.Add(ctl2, (iy,ix),(1,1), |
---|
930 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
931 | |
---|
932 | ctl2.Hide() |
---|
933 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
934 | self.enable2D: |
---|
935 | if self.is_mac: |
---|
936 | text2.Show(True) |
---|
937 | ctl2.Show(True) |
---|
938 | |
---|
939 | ix = 4 |
---|
940 | ctl3 = self.ModelTextCtrl(self, -1, |
---|
941 | size=(_BOX_WIDTH/2,20), |
---|
942 | style=wx.TE_PROCESS_ENTER, |
---|
943 | text_enter_callback = self._onparamRangeEnter) |
---|
944 | |
---|
945 | self.sizer4_4.Add(ctl3, (iy,ix),(1,1), |
---|
946 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
947 | |
---|
948 | ctl3.Hide() |
---|
949 | |
---|
950 | ix = 5 |
---|
951 | ctl4 = self.ModelTextCtrl(self, -1, |
---|
952 | size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
953 | text_enter_callback = self._onparamRangeEnter) |
---|
954 | self.sizer4_4.Add(ctl4, (iy,ix),(1,1), |
---|
955 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
956 | ctl4.Hide() |
---|
957 | |
---|
958 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
959 | self.enable2D: |
---|
960 | ctl3.Show(True) |
---|
961 | ctl4.Show(True) |
---|
962 | |
---|
963 | elif p=="npts": |
---|
964 | ix = 6 |
---|
965 | value= self.model.getParam(name2) |
---|
966 | Tctl = self.ModelTextCtrl(self, -1, |
---|
967 | size=(_BOX_WIDTH/2.2, 20), |
---|
968 | style=wx.TE_PROCESS_ENTER) |
---|
969 | |
---|
970 | Tctl.SetValue(str (format_number(value))) |
---|
971 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
972 | self.enable2D: |
---|
973 | Tctl.Show(True) |
---|
974 | else: |
---|
975 | Tctl.Hide() |
---|
976 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
977 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
978 | self.fixed_param.append([None,name2, Tctl,None,None, |
---|
979 | None, None,None]) |
---|
980 | self.orientation_params_disp.append([None,name2, |
---|
981 | Tctl,None,None, |
---|
982 | None, None,None]) |
---|
983 | elif p=="nsigmas": |
---|
984 | ix = 7 |
---|
985 | value= self.model.getParam(name3) |
---|
986 | Tct2 = self.ModelTextCtrl(self, -1, |
---|
987 | size=(_BOX_WIDTH/2.2,20), |
---|
988 | style=wx.TE_PROCESS_ENTER) |
---|
989 | |
---|
990 | Tct2.SetValue(str (format_number(value))) |
---|
991 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
992 | self.enable2D: |
---|
993 | Tct2.Show(True) |
---|
994 | else: |
---|
995 | Tct2.Hide() |
---|
996 | self.sizer4_4.Add(Tct2, (iy,ix),(1,1), |
---|
997 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
998 | |
---|
999 | |
---|
1000 | self.fixed_param.append([None,name3, Tct2 |
---|
1001 | ,None,None, None, None,None]) |
---|
1002 | |
---|
1003 | self.orientation_params_disp.append([None,name3, |
---|
1004 | Tct2 ,None,None, None, None,None]) |
---|
1005 | |
---|
1006 | |
---|
1007 | ix = 8 |
---|
1008 | disp_box = wx.ComboBox(self, -1,size=(65,-1), |
---|
1009 | style=wx.CB_READONLY, name = '%s'% name1) |
---|
1010 | for key, value in self.polydisp.iteritems(): |
---|
1011 | name_disp = str(key) |
---|
1012 | disp_box.Append(name_disp,value) |
---|
1013 | disp_box.SetStringSelection("gaussian") |
---|
1014 | wx.EVT_COMBOBOX(disp_box,-1, self._on_disp_func) |
---|
1015 | self.sizer4_4.Add(disp_box,(iy,ix),(1,1), wx.EXPAND) |
---|
1016 | self.fittable_param.append([cb,name1,ctl1,text2, |
---|
1017 | ctl2, ctl3, ctl4, disp_box]) |
---|
1018 | self.orientation_params_disp.append([cb,name1,ctl1, |
---|
1019 | text2, ctl2, ctl3, ctl4, disp_box]) |
---|
1020 | |
---|
1021 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
1022 | self.enable2D: |
---|
1023 | disp_box.Show(True) |
---|
1024 | else: |
---|
1025 | disp_box.Hide() |
---|
1026 | |
---|
1027 | |
---|
1028 | self.state.disp_cb_dict = copy.deepcopy(self.disp_cb_dict) |
---|
1029 | |
---|
1030 | self.state.model = self.model.clone() |
---|
1031 | ## save state into |
---|
1032 | self.state.cb1 = self.cb1.GetValue() |
---|
1033 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
1034 | self._copy_parameters_state(self.orientation_params_disp, |
---|
1035 | self.state.orientation_params_disp) |
---|
1036 | self._copy_parameters_state(self.fittable_param, |
---|
1037 | self.state.fittable_param) |
---|
1038 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
1039 | |
---|
1040 | |
---|
1041 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
1042 | " Selected Distribution: Gaussian")) |
---|
1043 | #Fill the list of fittable parameters |
---|
1044 | self.select_all_param(event=None) |
---|
1045 | |
---|
1046 | self.Layout() |
---|
1047 | |
---|
1048 | |
---|
1049 | def _onDraw(self, event): |
---|
1050 | """ |
---|
1051 | Update and Draw the model |
---|
1052 | """ |
---|
1053 | if self.model ==None: |
---|
1054 | msg="Please select a Model first..." |
---|
1055 | wx.MessageBox(msg, 'Info') |
---|
1056 | |
---|
1057 | return |
---|
1058 | flag = self._update_paramv_on_fit() |
---|
1059 | wx.CallAfter(self._onparamEnter_helper) |
---|
1060 | if not flag: |
---|
1061 | msg= "The parameters are invalid" |
---|
1062 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
1063 | return |
---|
1064 | |
---|
1065 | |
---|
1066 | def _onFit(self, event): |
---|
1067 | """ |
---|
1068 | Allow to fit |
---|
1069 | """ |
---|
1070 | if event != None: |
---|
1071 | event.Skip() |
---|
1072 | if len(self.parent._manager.fit_thread_list)>0 and\ |
---|
1073 | self.parent._manager._fit_engine != "park" and\ |
---|
1074 | self._manager.sim_page.uid == self.uid: |
---|
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 | flag = self.disable_smearer.GetValue() |
---|
1783 | self.disable_smearer.SetValue(flag) |
---|
1784 | if self.current_smearer == None: |
---|
1785 | self.enable_smearer.Disable() |
---|
1786 | else: |
---|
1787 | self.enable_smearer.Enable() |
---|
1788 | if not flag: |
---|
1789 | self.onSmear(None) |
---|
1790 | |
---|
1791 | def _mac_sleep(self, sec=0.2): |
---|
1792 | """ |
---|
1793 | Give sleep to MAC |
---|
1794 | """ |
---|
1795 | if self.is_mac: |
---|
1796 | time.sleep(sec) |
---|
1797 | |
---|
1798 | def get_view_mode(self): |
---|
1799 | """ |
---|
1800 | return True if the panel allow 2D or False if 1D |
---|
1801 | """ |
---|
1802 | return self.enable2D |
---|
1803 | |
---|
1804 | def compute_data_set_range(self, data_list): |
---|
1805 | """ |
---|
1806 | find the range that include all data in the set |
---|
1807 | return the minimum and the maximum values |
---|
1808 | """ |
---|
1809 | if data_list is not None and data_list != []: |
---|
1810 | for data in data_list: |
---|
1811 | qmin, qmax, npts = self.compute_data_range(data) |
---|
1812 | self.qmin_data_set = min(self.qmin_data_set, qmin) |
---|
1813 | self.qmax_data_set = max(self.qmax_data_set, qmax) |
---|
1814 | self.npts_data_set += npts |
---|
1815 | return self.qmin_data_set, self.qmax_data_set, self.npts_data_set |
---|
1816 | |
---|
1817 | def compute_data_range(self, data): |
---|
1818 | """ |
---|
1819 | compute the minimum and the maximum range of the data |
---|
1820 | return the npts contains in data |
---|
1821 | :param data: |
---|
1822 | """ |
---|
1823 | qmin, qmax, npts = None, None, None |
---|
1824 | if data is not None: |
---|
1825 | if not hasattr(data,"data"): |
---|
1826 | qmin = min(data.x) |
---|
1827 | # Maximum value of data |
---|
1828 | qmax = max(data.x) |
---|
1829 | npts = len(data.x) |
---|
1830 | else: |
---|
1831 | qmin = 0 |
---|
1832 | x = max(math.fabs(data.xmin), math.fabs(data.xmax)) |
---|
1833 | y = max(math.fabs(data.ymin), math.fabs(data.ymax)) |
---|
1834 | ## Maximum value of data |
---|
1835 | qmax = math.sqrt(x*x + y*y) |
---|
1836 | npts = len(data.data) |
---|
1837 | return qmin, qmax, npts |
---|
1838 | |
---|
1839 | |
---|
1840 | def set_data(self, data): |
---|
1841 | """ |
---|
1842 | reset the current data |
---|
1843 | """ |
---|
1844 | from sans.guiframe.dataFitting import check_data_validity |
---|
1845 | id = None |
---|
1846 | flag = False |
---|
1847 | is_data = False |
---|
1848 | if self.data is not None: |
---|
1849 | is_data = check_data_validity(self.data) |
---|
1850 | if not is_data and data is not None: |
---|
1851 | flag = True |
---|
1852 | if data is not None: |
---|
1853 | id = data.id |
---|
1854 | if is_data: |
---|
1855 | self.graph_id = self.data.group_id |
---|
1856 | flag = (data.id != self.data.id) |
---|
1857 | self.data = data |
---|
1858 | if check_data_validity(data): |
---|
1859 | self.graph_id = data.group_id |
---|
1860 | self.data.group_id = self.graph_id |
---|
1861 | |
---|
1862 | if self.data is None: |
---|
1863 | data_name = "" |
---|
1864 | self._set_bookmark_flag(False) |
---|
1865 | self._keep.Enable(False) |
---|
1866 | self._set_save_flag(False) |
---|
1867 | else: |
---|
1868 | if self.model != None: |
---|
1869 | self._set_bookmark_flag(True) |
---|
1870 | self._keep.Enable(True) |
---|
1871 | |
---|
1872 | self._set_save_flag(True) |
---|
1873 | self._set_preview_flag(True) |
---|
1874 | |
---|
1875 | self._set_smear(data) |
---|
1876 | # more disables for 2D |
---|
1877 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
1878 | self.enable2D: |
---|
1879 | self.slit_smearer.Disable() |
---|
1880 | self.pinhole_smearer.Enable(True) |
---|
1881 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
1882 | if self.data.err_data == None or\ |
---|
1883 | (self.data.err_data == 1).all() or\ |
---|
1884 | (self.data.err_data == 0).all(): |
---|
1885 | self.dI_didata.Enable(False) |
---|
1886 | self.dI_noweight.SetValue(True) |
---|
1887 | self.weightbt_string = self.dI_noweight.GetLabelText() |
---|
1888 | else: |
---|
1889 | self.dI_didata.Enable(True) |
---|
1890 | self.dI_didata.SetValue(True) |
---|
1891 | self.weightbt_string = self.dI_didata.GetLabelText() |
---|
1892 | else: |
---|
1893 | self.slit_smearer.Enable(True) |
---|
1894 | self.pinhole_smearer.Enable(True) |
---|
1895 | if self.data.dy == None or\ |
---|
1896 | (self.data.dy == 1).all() or\ |
---|
1897 | (self.data.dy == 0).all(): |
---|
1898 | self.dI_didata.Enable(False) |
---|
1899 | self.dI_noweight.SetValue(True) |
---|
1900 | self.weightbt_string = self.dI_noweight.GetLabelText() |
---|
1901 | else: |
---|
1902 | self.dI_didata.Enable(True) |
---|
1903 | self.dI_didata.SetValue(True) |
---|
1904 | self.weightbt_string = self.dI_didata.GetLabelText() |
---|
1905 | # Enable weighting radio uttons |
---|
1906 | self.dI_noweight.Enable(True) |
---|
1907 | self.dI_sqrdata.Enable(True) |
---|
1908 | self.dI_idata.Enable(True) |
---|
1909 | |
---|
1910 | self.formfactorbox.Enable() |
---|
1911 | self.structurebox.Enable() |
---|
1912 | data_name = self.data.name |
---|
1913 | _, _, npts = self.compute_data_range(self.data) |
---|
1914 | #set maximum range for x in linear scale |
---|
1915 | if not hasattr(self.data, "data"): #Display only for 1D data fit |
---|
1916 | self.btEditMask.Disable() |
---|
1917 | self.EditMask_title.Disable() |
---|
1918 | else: |
---|
1919 | self.btEditMask.Enable() |
---|
1920 | self.EditMask_title.Enable() |
---|
1921 | self.Npts_total.SetValue(str(npts)) |
---|
1922 | #default:number of data points selected to fit |
---|
1923 | self.Npts_fit.SetValue(str(npts)) |
---|
1924 | self.Npts_total.SetEditable(False) |
---|
1925 | self.Npts_total.SetBackgroundColour(\ |
---|
1926 | self.GetParent().GetBackgroundColour()) |
---|
1927 | |
---|
1928 | self.Npts_total.Bind(wx.EVT_MOUSE_EVENTS, self._npts_click) |
---|
1929 | self.dataSource.SetValue(data_name) |
---|
1930 | self.state.data = data |
---|
1931 | self.enable_fit_button() |
---|
1932 | # send graph_id to page_finder |
---|
1933 | self._manager.set_graph_id(uid=self.uid, graph_id=self.graph_id) |
---|
1934 | #update model plot with new data information |
---|
1935 | if flag: |
---|
1936 | #set model view button |
---|
1937 | self.current_smearer = smear_selection(self.data, self.model) |
---|
1938 | if self.data.__class__.__name__ == "Data2D": |
---|
1939 | self.enable2D = True |
---|
1940 | self.model_view.SetLabel("2D Mode") |
---|
1941 | else: |
---|
1942 | self.enable2D = False |
---|
1943 | self.model_view.SetLabel("1D Mode") |
---|
1944 | self.model_view.Disable() |
---|
1945 | #replace data plot on combo box selection |
---|
1946 | #by removing the previous selected data |
---|
1947 | wx.PostEvent(self._manager.parent, NewPlotEvent(action="remove", |
---|
1948 | group_id=self.graph_id, id=id)) |
---|
1949 | #plot the current selected data |
---|
1950 | wx.PostEvent(self._manager.parent, NewPlotEvent(plot=self.data, |
---|
1951 | title=str(self.data.title))) |
---|
1952 | #self._manager.store_data(uid=self.uid, data_list=self.data_list, |
---|
1953 | # caption=self.window_name) |
---|
1954 | self._draw_model() |
---|
1955 | |
---|
1956 | def _npts_click(self, event): |
---|
1957 | """ |
---|
1958 | Prevent further handling of the mouse event on Npts_total |
---|
1959 | by not calling Skip(). |
---|
1960 | """ |
---|
1961 | pass |
---|
1962 | |
---|
1963 | def reset_page(self, state,first=False): |
---|
1964 | """ |
---|
1965 | reset the state |
---|
1966 | """ |
---|
1967 | self.reset_page_helper(state) |
---|
1968 | #import sans.guiframe.gui_manager |
---|
1969 | #evt = ModelEventbox(model=state.model) |
---|
1970 | #wx.PostEvent(self.event_owner, evt) |
---|
1971 | |
---|
1972 | if self.engine_type != None: |
---|
1973 | self._manager._on_change_engine(engine=self.engine_type) |
---|
1974 | |
---|
1975 | self.select_param(event = None) |
---|
1976 | #Save state_fit |
---|
1977 | self.save_current_state_fit() |
---|
1978 | self._lay_out() |
---|
1979 | self.Refresh() |
---|
1980 | |
---|
1981 | def get_range(self): |
---|
1982 | """ |
---|
1983 | return the fitting range |
---|
1984 | """ |
---|
1985 | return float(self.qmin_x) , float(self.qmax_x) |
---|
1986 | |
---|
1987 | def get_npts2fit(self): |
---|
1988 | """ |
---|
1989 | return numbers of data points within qrange |
---|
1990 | |
---|
1991 | :Note: This is for Park where chi2 is not normalized by Npts of fit |
---|
1992 | |
---|
1993 | """ |
---|
1994 | if self.data is None: |
---|
1995 | return |
---|
1996 | npts2fit = 0 |
---|
1997 | qmin,qmax = self.get_range() |
---|
1998 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
1999 | self.enable2D: |
---|
2000 | radius= numpy.sqrt( self.data.qx_data*self.data.qx_data + |
---|
2001 | self.data.qy_data*self.data.qy_data ) |
---|
2002 | index_data = (self.qmin_x <= radius)&(radius<= self.qmax_x) |
---|
2003 | index_data= (index_data)&(self.data.mask) |
---|
2004 | index_data = (index_data)&(numpy.isfinite(self.data.data)) |
---|
2005 | npts2fit = len(self.data.data[index_data]) |
---|
2006 | else: |
---|
2007 | for qx in self.data.x: |
---|
2008 | if qx >= qmin and qx <= qmax: |
---|
2009 | npts2fit += 1 |
---|
2010 | return npts2fit |
---|
2011 | |
---|
2012 | def set_npts2fit(self): |
---|
2013 | """ |
---|
2014 | setValue Npts for fitting |
---|
2015 | """ |
---|
2016 | self.Npts_fit.SetValue(str(self.get_npts2fit())) |
---|
2017 | |
---|
2018 | def get_chi2(self): |
---|
2019 | """ |
---|
2020 | return the current chi2 |
---|
2021 | """ |
---|
2022 | return self.tcChi.GetValue() |
---|
2023 | |
---|
2024 | def get_param_list(self): |
---|
2025 | """ |
---|
2026 | :return self.param_toFit: list containing references to TextCtrl |
---|
2027 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
2028 | |
---|
2029 | :raise: if return an empty list of parameter fit will nnote work |
---|
2030 | properly so raise ValueError,"missing parameter to fit" |
---|
2031 | """ |
---|
2032 | if self.param_toFit !=[]: |
---|
2033 | return self.param_toFit |
---|
2034 | else: |
---|
2035 | msg = "missing parameters to fit" |
---|
2036 | wx.MessageBox(msg, 'warning') |
---|
2037 | return False |
---|
2038 | #raise ValueError,"missing parameters to fit" |
---|
2039 | |
---|
2040 | def onsetValues(self, chisqr, p_name, out, cov): |
---|
2041 | """ |
---|
2042 | Build the panel from the fit result |
---|
2043 | |
---|
2044 | :param chisqr: Value of the goodness of fit metric |
---|
2045 | :param p_name: the name of parameters |
---|
2046 | :param out: list of parameter with the best value found during fitting |
---|
2047 | :param cov: Covariance matrix |
---|
2048 | |
---|
2049 | """ |
---|
2050 | if out == None or not numpy.isfinite(chisqr): |
---|
2051 | raise ValueError,"Fit error occured..." |
---|
2052 | |
---|
2053 | is_modified = False |
---|
2054 | has_error = False |
---|
2055 | dispersity = '' |
---|
2056 | |
---|
2057 | #Hide textctrl boxes of errors. |
---|
2058 | self._clear_Err_on_Fit() |
---|
2059 | |
---|
2060 | #Check if chi2 is finite |
---|
2061 | if chisqr != None and numpy.isfinite(chisqr): |
---|
2062 | #format chi2 |
---|
2063 | if self.engine_type == "park": |
---|
2064 | npt_fit = float(self.get_npts2fit()) |
---|
2065 | # Taking care this in parkemgine |
---|
2066 | #if npt_fit > 0: |
---|
2067 | # chisqr =chisqr/npt_fit |
---|
2068 | chi2 = format_number(chisqr, True) |
---|
2069 | self.tcChi.SetValue(chi2) |
---|
2070 | self.tcChi.Refresh() |
---|
2071 | else: |
---|
2072 | self.tcChi.SetValue("-") |
---|
2073 | |
---|
2074 | #Hide error title |
---|
2075 | if self.text2_3.IsShown() and not self.is_mac: |
---|
2076 | self.text2_3.Hide() |
---|
2077 | |
---|
2078 | try: |
---|
2079 | if self.enable_disp.GetValue(): |
---|
2080 | if hasattr(self,"text_disp_1" ): |
---|
2081 | if self.text_disp_1 != None and not self.is_mac: |
---|
2082 | self.text_disp_1.Hide() |
---|
2083 | except: |
---|
2084 | dispersity = None |
---|
2085 | pass |
---|
2086 | |
---|
2087 | |
---|
2088 | i = 0 |
---|
2089 | #Set the panel when fit result are list |
---|
2090 | for item in self.param_toFit: |
---|
2091 | if len(item)>5 and item != None: |
---|
2092 | ## reset error value to initial state |
---|
2093 | if not self.is_mac: |
---|
2094 | item[3].Hide() |
---|
2095 | item[4].Hide() |
---|
2096 | for ind in range(len(out)): |
---|
2097 | if item[1] == p_name[ind]: |
---|
2098 | break |
---|
2099 | if len(out)<=len(self.param_toFit) and out[ind] !=None: |
---|
2100 | val_out = format_number(out[ind], True) |
---|
2101 | item[2].SetValue(val_out) |
---|
2102 | |
---|
2103 | if(cov !=None): |
---|
2104 | try: |
---|
2105 | if dispersity !=None: |
---|
2106 | if self.enable_disp.GetValue(): |
---|
2107 | if hasattr(self,"text_disp_1" ): |
---|
2108 | if self.text_disp_1!=None: |
---|
2109 | if not self.text_disp_1.IsShown()\ |
---|
2110 | and not self.is_mac: |
---|
2111 | self.text_disp_1.Show(True) |
---|
2112 | except: |
---|
2113 | pass |
---|
2114 | |
---|
2115 | if cov[ind]!=None : |
---|
2116 | if numpy.isfinite(float(cov[ind])): |
---|
2117 | val_err = format_number(cov[ind], True) |
---|
2118 | if not self.is_mac: |
---|
2119 | item[3].Show(True) |
---|
2120 | item[4].Show(True) |
---|
2121 | item[4].SetValue(val_err) |
---|
2122 | has_error = True |
---|
2123 | i += 1 |
---|
2124 | #Show error title when any errors displayed |
---|
2125 | if has_error: |
---|
2126 | if not self.text2_3.IsShown(): |
---|
2127 | self.text2_3.Show(True) |
---|
2128 | ## save current state |
---|
2129 | self.save_current_state() |
---|
2130 | |
---|
2131 | #self._lay_out() |
---|
2132 | if not self.is_mac: |
---|
2133 | self.Layout() |
---|
2134 | self.Refresh() |
---|
2135 | self._mac_sleep(0.1) |
---|
2136 | #plot model ( when drawing, do not update chisqr value again) |
---|
2137 | self._draw_model(update_chisqr=False, source = 'fit') |
---|
2138 | #PostStatusEvent |
---|
2139 | #msg = "Fit completed!dddd " |
---|
2140 | #wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
2141 | |
---|
2142 | def onWeighting(self, event): |
---|
2143 | """ |
---|
2144 | On Weighting radio button event, sets the weightbt_string |
---|
2145 | """ |
---|
2146 | self.weightbt_string = event.GetEventObject().GetLabelText() |
---|
2147 | self._set_weight() |
---|
2148 | |
---|
2149 | def _set_weight(self, is_2D=None): |
---|
2150 | """ |
---|
2151 | Set weight in fit problem |
---|
2152 | """ |
---|
2153 | # compute weight for the current data |
---|
2154 | from .utils import get_weight |
---|
2155 | flag_weight = self.get_weight_flag() |
---|
2156 | if is_2D == None: |
---|
2157 | is_2D = self._is_2D() |
---|
2158 | weight = get_weight(data=self.data, |
---|
2159 | is2d=is_2D, |
---|
2160 | flag=flag_weight) |
---|
2161 | self._manager.set_fit_weight(uid=self.uid, |
---|
2162 | flag=flag_weight, |
---|
2163 | is2d=is_2D, |
---|
2164 | fid=None) |
---|
2165 | |
---|
2166 | def onPinholeSmear(self, event): |
---|
2167 | """ |
---|
2168 | Create a custom pinhole smear object that will change the way residuals |
---|
2169 | are compute when fitting |
---|
2170 | |
---|
2171 | :Note: accuracy is given by strings'High','Med', 'Low' FOR 2d, |
---|
2172 | None for 1D |
---|
2173 | |
---|
2174 | """ |
---|
2175 | |
---|
2176 | #if self.check_invalid_panel(): |
---|
2177 | # return |
---|
2178 | if self.model ==None: |
---|
2179 | self.disable_smearer.SetValue(True) |
---|
2180 | msg="Please select a Model first..." |
---|
2181 | wx.MessageBox(msg, 'Info') |
---|
2182 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2183 | "Smear: %s"%msg)) |
---|
2184 | return |
---|
2185 | |
---|
2186 | # Need update param values |
---|
2187 | self._update_paramv_on_fit() |
---|
2188 | |
---|
2189 | # msg default |
---|
2190 | msg = None |
---|
2191 | if event != None: |
---|
2192 | tcrtl= event.GetEventObject() |
---|
2193 | # event case of radio button |
---|
2194 | if tcrtl.GetValue()== True: |
---|
2195 | self.dx_min = 0.0 |
---|
2196 | self.dx_max = 0.0 |
---|
2197 | is_new_pinhole = True |
---|
2198 | else: |
---|
2199 | is_new_pinhole = self._is_changed_pinhole() |
---|
2200 | else: |
---|
2201 | is_new_pinhole = True |
---|
2202 | # if any value is changed |
---|
2203 | if is_new_pinhole: |
---|
2204 | msg = self._set_pinhole_smear() |
---|
2205 | # hide all silt sizer |
---|
2206 | self._hide_all_smear_info() |
---|
2207 | |
---|
2208 | ##Calculate chi2 |
---|
2209 | temp_smearer = self.current_smearer |
---|
2210 | #self.compute_chisqr(smearer= temp_smearer) |
---|
2211 | |
---|
2212 | # show relevant slit sizers |
---|
2213 | self._show_smear_sizer() |
---|
2214 | |
---|
2215 | self.sizer_set_smearer.Layout() |
---|
2216 | self.Layout() |
---|
2217 | |
---|
2218 | if event != None: |
---|
2219 | event.Skip() |
---|
2220 | #self._undo.Enable(True) |
---|
2221 | self.save_current_state() |
---|
2222 | event = PageInfoEvent(page = self) |
---|
2223 | wx.PostEvent(self.parent, event) |
---|
2224 | |
---|
2225 | def _is_changed_pinhole(self): |
---|
2226 | """ |
---|
2227 | check if any of pinhole smear is changed |
---|
2228 | |
---|
2229 | :return: True or False |
---|
2230 | |
---|
2231 | """ |
---|
2232 | # get the values |
---|
2233 | pin_min = self.smear_pinhole_min.GetValue() |
---|
2234 | pin_max = self.smear_pinhole_max.GetValue() |
---|
2235 | |
---|
2236 | # Check changes in slit width |
---|
2237 | try: |
---|
2238 | dx_min = float(pin_min) |
---|
2239 | except: |
---|
2240 | return True |
---|
2241 | if self.dx_min != dx_min: |
---|
2242 | return True |
---|
2243 | |
---|
2244 | # Check changes in slit heigth |
---|
2245 | try: |
---|
2246 | dx_max = float(pin_max) |
---|
2247 | except: |
---|
2248 | return True |
---|
2249 | if self.dx_max != dx_max: |
---|
2250 | return True |
---|
2251 | return False |
---|
2252 | |
---|
2253 | def _set_pinhole_smear(self): |
---|
2254 | """ |
---|
2255 | Set custom pinhole smear |
---|
2256 | |
---|
2257 | :return: msg |
---|
2258 | |
---|
2259 | """ |
---|
2260 | # copy data |
---|
2261 | data = copy.deepcopy(self.data) |
---|
2262 | if self._is_2D(): |
---|
2263 | self.smear_type = 'Pinhole2d' |
---|
2264 | len_data = len(data.data) |
---|
2265 | data.dqx_data = numpy.zeros(len_data) |
---|
2266 | data.dqy_data = numpy.zeros(len_data) |
---|
2267 | else: |
---|
2268 | self.smear_type = 'Pinhole' |
---|
2269 | len_data = len(data.x) |
---|
2270 | data.dx = numpy.zeros(len_data) |
---|
2271 | data.dxl = None |
---|
2272 | data.dxw = None |
---|
2273 | msg = None |
---|
2274 | |
---|
2275 | get_pin_min = self.smear_pinhole_min |
---|
2276 | get_pin_max = self.smear_pinhole_max |
---|
2277 | |
---|
2278 | if not check_float(get_pin_min): |
---|
2279 | get_pin_min.SetBackgroundColour("pink") |
---|
2280 | msg= "Model Error:wrong value entered!!!" |
---|
2281 | elif not check_float(get_pin_max ): |
---|
2282 | get_pin_max.SetBackgroundColour("pink") |
---|
2283 | msg= "Model Error:wrong value entered!!!" |
---|
2284 | else: |
---|
2285 | if len_data < 2: len_data = 2 |
---|
2286 | self.dx_min = float(get_pin_min.GetValue()) |
---|
2287 | self.dx_max = float(get_pin_max.GetValue()) |
---|
2288 | if self.dx_min < 0: |
---|
2289 | get_pin_min.SetBackgroundColour("pink") |
---|
2290 | msg= "Model Error:This value can not be negative!!!" |
---|
2291 | elif self.dx_max <0: |
---|
2292 | get_pin_max.SetBackgroundColour("pink") |
---|
2293 | msg= "Model Error:This value can not be negative!!!" |
---|
2294 | elif self.dx_min != None and self.dx_max != None: |
---|
2295 | if self._is_2D(): |
---|
2296 | data.dqx_data[data.dqx_data==0] = self.dx_min |
---|
2297 | data.dqy_data[data.dqy_data==0] = self.dx_max |
---|
2298 | elif self.dx_min == self.dx_max: |
---|
2299 | data.dx[data.dx==0] = self.dx_min |
---|
2300 | else: |
---|
2301 | step = (self.dx_max - self.dx_min)/(len_data-1) |
---|
2302 | data.dx = numpy.arange(self.dx_min,self.dx_max+step/1.1, |
---|
2303 | step) |
---|
2304 | elif self.dx_min != None: |
---|
2305 | if self._is_2D(): data.dqx_data[data.dqx_data==0] = self.dx_min |
---|
2306 | else: data.dx[data.dx==0] = self.dx_min |
---|
2307 | elif self.dx_max != None: |
---|
2308 | if self._is_2D(): data.dqy_data[data.dqy_data==0] = self.dx_max |
---|
2309 | else: data.dx[data.dx==0] = self.dx_max |
---|
2310 | self.current_smearer = smear_selection(data, self.model) |
---|
2311 | # 2D need to set accuracy |
---|
2312 | if self._is_2D(): |
---|
2313 | self.current_smearer.set_accuracy(accuracy = \ |
---|
2314 | self.smear2d_accuracy) |
---|
2315 | |
---|
2316 | if msg != None: |
---|
2317 | wx.PostEvent(self._manager.parent, StatusEvent(status = msg )) |
---|
2318 | else: |
---|
2319 | get_pin_min.SetBackgroundColour("white") |
---|
2320 | get_pin_max.SetBackgroundColour("white") |
---|
2321 | ## set smearing value whether or not the data contain the smearing info |
---|
2322 | |
---|
2323 | self._manager.set_smearer(smearer=self.current_smearer, |
---|
2324 | fid=self.data.id, |
---|
2325 | qmin=float(self.qmin_x), |
---|
2326 | qmax= float(self.qmax_x), |
---|
2327 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
2328 | uid=self.uid) |
---|
2329 | return msg |
---|
2330 | |
---|
2331 | def update_pinhole_smear(self): |
---|
2332 | """ |
---|
2333 | called by kill_focus on pinhole TextCntrl |
---|
2334 | to update the changes |
---|
2335 | |
---|
2336 | :return: False when wrong value was entered |
---|
2337 | |
---|
2338 | """ |
---|
2339 | # msg default |
---|
2340 | msg = None |
---|
2341 | # check if any value is changed |
---|
2342 | if self._is_changed_pinhole(): |
---|
2343 | msg = self._set_pinhole_smear() |
---|
2344 | #self._undo.Enable(True) |
---|
2345 | wx.CallAfter(self.save_current_state) |
---|
2346 | |
---|
2347 | if msg != None: |
---|
2348 | return False |
---|
2349 | else: |
---|
2350 | return True |
---|
2351 | |
---|
2352 | def onSlitSmear(self, event): |
---|
2353 | """ |
---|
2354 | Create a custom slit smear object that will change the way residuals |
---|
2355 | are compute when fitting |
---|
2356 | """ |
---|
2357 | |
---|
2358 | #if self.check_invalid_panel(): |
---|
2359 | # return |
---|
2360 | |
---|
2361 | if self.model ==None: |
---|
2362 | self.disable_smearer.SetValue(True) |
---|
2363 | |
---|
2364 | msg="Please select a Model first..." |
---|
2365 | wx.MessageBox(msg, 'Info') |
---|
2366 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2367 | "Smear: %s"%msg)) |
---|
2368 | return |
---|
2369 | |
---|
2370 | # Need update param values |
---|
2371 | self._update_paramv_on_fit() |
---|
2372 | |
---|
2373 | # msg default |
---|
2374 | msg = None |
---|
2375 | # for event given |
---|
2376 | if event != None: |
---|
2377 | tcrtl= event.GetEventObject() |
---|
2378 | # event case of radio button |
---|
2379 | if tcrtl.GetValue(): |
---|
2380 | self.dxl = 0.0 |
---|
2381 | self.dxw = 0.0 |
---|
2382 | is_new_slit = True |
---|
2383 | else: |
---|
2384 | is_new_slit = self._is_changed_slit() |
---|
2385 | else: |
---|
2386 | is_new_slit = True |
---|
2387 | |
---|
2388 | # if any value is changed |
---|
2389 | if is_new_slit: |
---|
2390 | msg = self._set_slit_smear() |
---|
2391 | |
---|
2392 | # hide all silt sizer |
---|
2393 | self._hide_all_smear_info() |
---|
2394 | ##Calculate chi2 |
---|
2395 | #self.compute_chisqr(smearer= self.current_smearer) |
---|
2396 | # show relevant slit sizers |
---|
2397 | self._show_smear_sizer() |
---|
2398 | self.sizer_set_smearer.Layout() |
---|
2399 | self.Layout() |
---|
2400 | |
---|
2401 | if event != None: |
---|
2402 | event.Skip() |
---|
2403 | #self._undo.Enable(True) |
---|
2404 | self.save_current_state() |
---|
2405 | event = PageInfoEvent(page = self) |
---|
2406 | wx.PostEvent(self.parent, event) |
---|
2407 | if msg != None: |
---|
2408 | wx.PostEvent(self._manager.parent, StatusEvent(status = msg)) |
---|
2409 | |
---|
2410 | def _is_changed_slit(self): |
---|
2411 | """ |
---|
2412 | check if any of slit lengths is changed |
---|
2413 | |
---|
2414 | :return: True or False |
---|
2415 | |
---|
2416 | """ |
---|
2417 | # get the values |
---|
2418 | width = self.smear_slit_width.GetValue() |
---|
2419 | height = self.smear_slit_height.GetValue() |
---|
2420 | |
---|
2421 | # check and change the box bg color if it was pink |
---|
2422 | # but it should be white now |
---|
2423 | # because this is the case that _set_slit_smear() will not handle |
---|
2424 | if height.lstrip().rstrip()=="": |
---|
2425 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2426 | if width.lstrip().rstrip()=="": |
---|
2427 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2428 | |
---|
2429 | # Check changes in slit width |
---|
2430 | if width == "": |
---|
2431 | dxw = 0.0 |
---|
2432 | else: |
---|
2433 | try: |
---|
2434 | dxw = float(width) |
---|
2435 | except: |
---|
2436 | return True |
---|
2437 | if self.dxw != dxw: |
---|
2438 | return True |
---|
2439 | |
---|
2440 | # Check changes in slit heigth |
---|
2441 | if height == "": |
---|
2442 | dxl = 0.0 |
---|
2443 | else: |
---|
2444 | try: |
---|
2445 | dxl = float(height) |
---|
2446 | except: |
---|
2447 | return True |
---|
2448 | if self.dxl != dxl: |
---|
2449 | return True |
---|
2450 | |
---|
2451 | return False |
---|
2452 | |
---|
2453 | def _set_slit_smear(self): |
---|
2454 | """ |
---|
2455 | Set custom slit smear |
---|
2456 | |
---|
2457 | :return: message to inform the user about the validity |
---|
2458 | of the values entered for slit smear |
---|
2459 | """ |
---|
2460 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
2461 | self.enable2D: |
---|
2462 | return |
---|
2463 | temp_smearer = None |
---|
2464 | # make sure once more if it is smearer |
---|
2465 | data = copy.deepcopy(self.data) |
---|
2466 | data_len = len(data.x) |
---|
2467 | data.dx = None |
---|
2468 | data.dxl = None |
---|
2469 | data.dxw = None |
---|
2470 | msg = None |
---|
2471 | |
---|
2472 | try: |
---|
2473 | self.dxl = float(self.smear_slit_height.GetValue()) |
---|
2474 | data.dxl = self.dxl* numpy.ones(data_len) |
---|
2475 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2476 | except: |
---|
2477 | data.dxl = numpy.zeros(data_len) |
---|
2478 | if self.smear_slit_height.GetValue().lstrip().rstrip()!="": |
---|
2479 | self.smear_slit_height.SetBackgroundColour("pink") |
---|
2480 | msg = "Wrong value entered... " |
---|
2481 | else: |
---|
2482 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2483 | try: |
---|
2484 | self.dxw = float(self.smear_slit_width.GetValue()) |
---|
2485 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2486 | data.dxw = self.dxw* numpy.ones(data_len) |
---|
2487 | except: |
---|
2488 | data.dxw = numpy.zeros(data_len) |
---|
2489 | if self.smear_slit_width.GetValue().lstrip().rstrip()!="": |
---|
2490 | self.smear_slit_width.SetBackgroundColour("pink") |
---|
2491 | msg = "Wrong Fit value entered... " |
---|
2492 | else: |
---|
2493 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2494 | |
---|
2495 | self.current_smearer = smear_selection(data, self.model) |
---|
2496 | #temp_smearer = self.current_smearer |
---|
2497 | ## set smearing value whether or not the data contain the smearing info |
---|
2498 | self._manager.set_smearer(smearer=self.current_smearer, |
---|
2499 | fid=self.data.id, |
---|
2500 | qmin=float(self.qmin_x), |
---|
2501 | qmax= float(self.qmax_x), |
---|
2502 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
2503 | uid=self.uid) |
---|
2504 | return msg |
---|
2505 | |
---|
2506 | def update_slit_smear(self): |
---|
2507 | """ |
---|
2508 | called by kill_focus on pinhole TextCntrl |
---|
2509 | to update the changes |
---|
2510 | |
---|
2511 | :return: False when wrong value was entered |
---|
2512 | |
---|
2513 | """ |
---|
2514 | # msg default |
---|
2515 | msg = None |
---|
2516 | # check if any value is changed |
---|
2517 | if self._is_changed_slit(): |
---|
2518 | msg = self._set_slit_smear() |
---|
2519 | #self._undo.Enable(True) |
---|
2520 | self.save_current_state() |
---|
2521 | |
---|
2522 | if msg != None: |
---|
2523 | return False |
---|
2524 | else: |
---|
2525 | return True |
---|
2526 | |
---|
2527 | def onSmear(self, event): |
---|
2528 | """ |
---|
2529 | Create a smear object that will change the way residuals |
---|
2530 | are compute when fitting |
---|
2531 | """ |
---|
2532 | if event != None: |
---|
2533 | event.Skip() |
---|
2534 | if self.data is None: |
---|
2535 | return |
---|
2536 | |
---|
2537 | if self.model == None: |
---|
2538 | self.disable_smearer.SetValue(True) |
---|
2539 | msg="Please select a Model first..." |
---|
2540 | wx.MessageBox(msg, 'Info') |
---|
2541 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2542 | "Smear: %s"%msg)) |
---|
2543 | return |
---|
2544 | |
---|
2545 | # Need update param values |
---|
2546 | self._update_paramv_on_fit() |
---|
2547 | |
---|
2548 | temp_smearer = None |
---|
2549 | self._get_smear_info() |
---|
2550 | |
---|
2551 | #renew smear sizer |
---|
2552 | if self.smear_type != None: |
---|
2553 | self.smear_description_smear_type.SetValue(str(self.smear_type)) |
---|
2554 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
2555 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
2556 | |
---|
2557 | self._hide_all_smear_info() |
---|
2558 | |
---|
2559 | data = copy.deepcopy(self.data) |
---|
2560 | # make sure once more if it is smearer |
---|
2561 | self.current_smearer = smear_selection(data, self.model) |
---|
2562 | |
---|
2563 | if self.enable_smearer.GetValue(): |
---|
2564 | if hasattr(self.data,"dxl"): |
---|
2565 | |
---|
2566 | msg= ": Resolution smearing parameters" |
---|
2567 | if hasattr(self.data,"dxw"): |
---|
2568 | msg= ": Slit smearing parameters" |
---|
2569 | if self.current_smearer ==None: |
---|
2570 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2571 | "Data contains no smearing information")) |
---|
2572 | else: |
---|
2573 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2574 | "Data contains smearing information")) |
---|
2575 | |
---|
2576 | #self.smear_description_dqdata.Show(True) |
---|
2577 | self.smear_data_left.Show(True) |
---|
2578 | self.smear_data_right.Show(True) |
---|
2579 | temp_smearer= self.current_smearer |
---|
2580 | elif self.disable_smearer.GetValue(): |
---|
2581 | self.smear_description_none.Show(True) |
---|
2582 | |
---|
2583 | self._show_smear_sizer() |
---|
2584 | |
---|
2585 | self.sizer_set_smearer.Layout() |
---|
2586 | self.Layout() |
---|
2587 | self._set_weight() |
---|
2588 | |
---|
2589 | ## set smearing value whether or not the data contain the smearing info |
---|
2590 | wx.CallAfter(self._manager.set_smearer, uid=self.uid, smearer=temp_smearer, |
---|
2591 | fid=self.data.id, |
---|
2592 | qmin=float(self.qmin_x), |
---|
2593 | qmax=float(self.qmax_x), |
---|
2594 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
2595 | draw=True) |
---|
2596 | |
---|
2597 | self.state.enable_smearer= self.enable_smearer.GetValue() |
---|
2598 | self.state.disable_smearer=self.disable_smearer.GetValue() |
---|
2599 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
2600 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
2601 | |
---|
2602 | def on_complete_chisqr(self, event): |
---|
2603 | """ |
---|
2604 | Display result chisqr on the panel |
---|
2605 | :event: activated by fitting/ complete after draw |
---|
2606 | """ |
---|
2607 | try: |
---|
2608 | if event == None: |
---|
2609 | output= "-" |
---|
2610 | elif not numpy.isfinite(event.output): |
---|
2611 | output= "-" |
---|
2612 | else: |
---|
2613 | output = event.output |
---|
2614 | self.tcChi.SetValue(str(format_number(output, True))) |
---|
2615 | self.state.tcChi = self.tcChi.GetValue() |
---|
2616 | except: |
---|
2617 | pass |
---|
2618 | |
---|
2619 | |
---|
2620 | def select_all_param(self,event): |
---|
2621 | """ |
---|
2622 | set to true or false all checkBox given the main checkbox value cb1 |
---|
2623 | """ |
---|
2624 | self.param_toFit=[] |
---|
2625 | if self.parameters !=[]: |
---|
2626 | if self.cb1.GetValue(): |
---|
2627 | for item in self.parameters: |
---|
2628 | ## for data2D select all to fit |
---|
2629 | if self.data.__class__.__name__== "Data2D" or \ |
---|
2630 | self.enable2D: |
---|
2631 | item[0].SetValue(True) |
---|
2632 | self.param_toFit.append(item ) |
---|
2633 | else: |
---|
2634 | ## for 1D all parameters except orientation |
---|
2635 | if not item in self.orientation_params: |
---|
2636 | item[0].SetValue(True) |
---|
2637 | self.param_toFit.append(item ) |
---|
2638 | #if len(self.fittable_param)>0: |
---|
2639 | for item in self.fittable_param: |
---|
2640 | if self.data.__class__.__name__== "Data2D" or \ |
---|
2641 | self.enable2D: |
---|
2642 | item[0].SetValue(True) |
---|
2643 | self.param_toFit.append(item ) |
---|
2644 | try: |
---|
2645 | if len(self.values[item[1]]) > 0: |
---|
2646 | item[0].SetValue(False) |
---|
2647 | except: |
---|
2648 | pass |
---|
2649 | |
---|
2650 | else: |
---|
2651 | ## for 1D all parameters except orientation |
---|
2652 | if not item in self.orientation_params_disp: |
---|
2653 | item[0].SetValue(True) |
---|
2654 | self.param_toFit.append(item ) |
---|
2655 | try: |
---|
2656 | if len(self.values[item[1]]) > 0: |
---|
2657 | item[0].SetValue(False) |
---|
2658 | except: |
---|
2659 | pass |
---|
2660 | |
---|
2661 | else: |
---|
2662 | for item in self.parameters: |
---|
2663 | item[0].SetValue(False) |
---|
2664 | for item in self.fittable_param: |
---|
2665 | item[0].SetValue(False) |
---|
2666 | self.param_toFit=[] |
---|
2667 | |
---|
2668 | self.save_current_state_fit() |
---|
2669 | |
---|
2670 | if event !=None: |
---|
2671 | #self._undo.Enable(True) |
---|
2672 | ## post state to fit panel |
---|
2673 | event = PageInfoEvent(page = self) |
---|
2674 | wx.PostEvent(self.parent, event) |
---|
2675 | param2fit = [] |
---|
2676 | for item in self.param_toFit: |
---|
2677 | if item[0]: |
---|
2678 | param2fit.append(item[1]) |
---|
2679 | self.parent._manager.set_param2fit(self.uid, param2fit) |
---|
2680 | |
---|
2681 | |
---|
2682 | def select_param(self,event): |
---|
2683 | """ |
---|
2684 | Select TextCtrl checked for fitting purpose and stores them |
---|
2685 | in self.param_toFit=[] list |
---|
2686 | """ |
---|
2687 | self.param_toFit=[] |
---|
2688 | for item in self.parameters: |
---|
2689 | #Skip t ifhe angle parameters if 1D data |
---|
2690 | if self.data.__class__.__name__ != "Data2D" and\ |
---|
2691 | not self.enable2D: |
---|
2692 | if item in self.orientation_params: |
---|
2693 | continue |
---|
2694 | #Select parameters to fit for list of primary parameters |
---|
2695 | if item[0].GetValue(): |
---|
2696 | if not (item in self.param_toFit): |
---|
2697 | self.param_toFit.append(item ) |
---|
2698 | else: |
---|
2699 | #remove parameters from the fitting list |
---|
2700 | if item in self.param_toFit: |
---|
2701 | self.param_toFit.remove(item) |
---|
2702 | |
---|
2703 | #Select parameters to fit for list of fittable parameters |
---|
2704 | # with dispersion |
---|
2705 | for item in self.fittable_param: |
---|
2706 | #Skip t ifhe angle parameters if 1D data |
---|
2707 | if self.data.__class__.__name__ != "Data2D" and\ |
---|
2708 | not self.enable2D: |
---|
2709 | if item in self.orientation_params: |
---|
2710 | continue |
---|
2711 | if item[0].GetValue(): |
---|
2712 | if not (item in self.param_toFit): |
---|
2713 | self.param_toFit.append(item) |
---|
2714 | else: |
---|
2715 | #remove parameters from the fitting list |
---|
2716 | if item in self.param_toFit: |
---|
2717 | self.param_toFit.remove(item) |
---|
2718 | |
---|
2719 | #Calculate num. of angle parameters |
---|
2720 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
2721 | self.enable2D: |
---|
2722 | len_orient_para = 0 |
---|
2723 | else: |
---|
2724 | len_orient_para = len(self.orientation_params) #assume even len |
---|
2725 | #Total num. of angle parameters |
---|
2726 | if len(self.fittable_param) > 0: |
---|
2727 | len_orient_para *= 2 |
---|
2728 | #Set the value of checkbox that selected every checkbox or not |
---|
2729 | if len(self.parameters)+len(self.fittable_param)-len_orient_para ==\ |
---|
2730 | len(self.param_toFit): |
---|
2731 | self.cb1.SetValue(True) |
---|
2732 | else: |
---|
2733 | self.cb1.SetValue(False) |
---|
2734 | self.save_current_state_fit() |
---|
2735 | if event !=None: |
---|
2736 | #self._undo.Enable(True) |
---|
2737 | ## post state to fit panel |
---|
2738 | event = PageInfoEvent(page = self) |
---|
2739 | wx.PostEvent(self.parent, event) |
---|
2740 | |
---|
2741 | param2fit = [] |
---|
2742 | for item in self.param_toFit: |
---|
2743 | if item[0]: |
---|
2744 | param2fit.append(item[1]) |
---|
2745 | self.parent._manager.set_param2fit(self.uid, param2fit) |
---|
2746 | |
---|
2747 | def set_model_param_sizer(self, model): |
---|
2748 | """ |
---|
2749 | Build the panel from the model content |
---|
2750 | |
---|
2751 | :param model: the model selected in combo box for fitting purpose |
---|
2752 | |
---|
2753 | """ |
---|
2754 | self.sizer3.Clear(True) |
---|
2755 | self.parameters = [] |
---|
2756 | self.str_parameters = [] |
---|
2757 | self.param_toFit=[] |
---|
2758 | self.fittable_param=[] |
---|
2759 | self.fixed_param=[] |
---|
2760 | self.orientation_params=[] |
---|
2761 | self.orientation_params_disp=[] |
---|
2762 | |
---|
2763 | if model ==None: |
---|
2764 | self.sizer3.Layout() |
---|
2765 | self.SetupScrolling() |
---|
2766 | return |
---|
2767 | ## the panel is drawn using the current value of the fit engine |
---|
2768 | if self.engine_type==None and self._manager !=None: |
---|
2769 | self.engine_type= self._manager._return_engine_type() |
---|
2770 | |
---|
2771 | box_description= wx.StaticBox(self, -1,str("Model Parameters")) |
---|
2772 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
2773 | sizer = wx.GridBagSizer(5,5) |
---|
2774 | ## save the current model |
---|
2775 | self.model = model |
---|
2776 | |
---|
2777 | keys = self.model.getParamList() |
---|
2778 | #list of dispersion paramaters |
---|
2779 | self.disp_list=self.model.getDispParamList() |
---|
2780 | |
---|
2781 | def custom_compare(a,b): |
---|
2782 | """ |
---|
2783 | Custom compare to order, first by alphabets then second by number. |
---|
2784 | """ |
---|
2785 | # number at the last digit |
---|
2786 | a_last = a[len(a)-1] |
---|
2787 | b_last = b[len(b)-1] |
---|
2788 | # default |
---|
2789 | num_a = None |
---|
2790 | num_b = None |
---|
2791 | # split the names |
---|
2792 | a2 = a.lower().split('_') |
---|
2793 | b2 = b.lower().split('_') |
---|
2794 | # check length of a2, b2 |
---|
2795 | len_a2 = len(a2) |
---|
2796 | len_b2 = len(b2) |
---|
2797 | # check if it contains a int number(<10) |
---|
2798 | try: |
---|
2799 | num_a = int(a_last) |
---|
2800 | except: pass |
---|
2801 | try: |
---|
2802 | num_b = int(b_last) |
---|
2803 | except: pass |
---|
2804 | # Put 'scale' near the top; happens |
---|
2805 | # when numbered param name exists |
---|
2806 | if a == 'scale': |
---|
2807 | return -1 |
---|
2808 | # both have a number |
---|
2809 | if num_a != None and num_b != None: |
---|
2810 | if num_a > num_b: return -1 |
---|
2811 | # same number |
---|
2812 | elif num_a == num_b: |
---|
2813 | # different last names |
---|
2814 | if a2[len_a2-1] != b2[len_b2-1] and num_a != 0: |
---|
2815 | return -cmp(a2[len_a2-1], b2[len_b2-1]) |
---|
2816 | else: |
---|
2817 | return cmp(a, b) |
---|
2818 | else: return 1 |
---|
2819 | # one of them has a number |
---|
2820 | elif num_a != None: return 1 |
---|
2821 | elif num_b != None: return -1 |
---|
2822 | # no numbers |
---|
2823 | else: return cmp(a.lower(), b.lower()) |
---|
2824 | |
---|
2825 | |
---|
2826 | keys.sort(custom_compare) |
---|
2827 | |
---|
2828 | iy = 0 |
---|
2829 | ix = 0 |
---|
2830 | select_text = "Select All" |
---|
2831 | self.cb1 = wx.CheckBox(self, -1,str(select_text), (10, 10)) |
---|
2832 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
2833 | self.cb1.SetToolTipString("To check/uncheck all the boxes below.") |
---|
2834 | self.cb1.SetValue(True) |
---|
2835 | |
---|
2836 | sizer.Add(self.cb1,(iy, ix),(1,1),\ |
---|
2837 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2838 | ix +=1 |
---|
2839 | self.text2_2 = wx.StaticText(self, -1, 'Value') |
---|
2840 | sizer.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
2841 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2842 | ix +=2 |
---|
2843 | self.text2_3 = wx.StaticText(self, -1, 'Error') |
---|
2844 | sizer.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
2845 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2846 | if not self.is_mac: |
---|
2847 | self.text2_3.Hide() |
---|
2848 | ix +=1 |
---|
2849 | self.text2_min = wx.StaticText(self, -1, 'Min') |
---|
2850 | sizer.Add(self.text2_min,(iy, ix),(1,1),\ |
---|
2851 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2852 | #self.text2_min.Hide() |
---|
2853 | ix +=1 |
---|
2854 | self.text2_max = wx.StaticText(self, -1, 'Max') |
---|
2855 | sizer.Add(self.text2_max,(iy, ix),(1,1),\ |
---|
2856 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2857 | #self.text2_max.Hide() |
---|
2858 | ix += 1 |
---|
2859 | self.text2_4 = wx.StaticText(self, -1, '[Units]') |
---|
2860 | sizer.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
2861 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2862 | self.text2_4.Hide() |
---|
2863 | |
---|
2864 | for item in keys: |
---|
2865 | if not item in self.disp_list and not item in \ |
---|
2866 | self.model.orientation_params: |
---|
2867 | |
---|
2868 | ##prepare a spot to store errors |
---|
2869 | if not self.model.details.has_key(item): |
---|
2870 | self.model.details [item] = ["",None,None] |
---|
2871 | |
---|
2872 | iy += 1 |
---|
2873 | ix = 0 |
---|
2874 | if (self.model.__class__ in \ |
---|
2875 | self.model_list_box["Multi-Functions"] or \ |
---|
2876 | self.temp_multi_functional)\ |
---|
2877 | and (item in self.model.non_fittable): |
---|
2878 | non_fittable_name = wx.StaticText(self, -1, item ) |
---|
2879 | sizer.Add(non_fittable_name,(iy, ix),(1,1),\ |
---|
2880 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 21) |
---|
2881 | ## add parameter value |
---|
2882 | ix += 1 |
---|
2883 | value= self.model.getParam(item) |
---|
2884 | if len(self.model.fun_list) > 0: |
---|
2885 | num = item.split('_')[1][5:7] |
---|
2886 | fun_box = wx.ComboBox(self, -1,size=(100,-1), |
---|
2887 | style=wx.CB_READONLY, name = '%s'% item) |
---|
2888 | self._set_fun_box_list(fun_box) |
---|
2889 | fun_box.SetSelection(0) |
---|
2890 | #self.fun_box.SetToolTipString("A function |
---|
2891 | # describing the interface") |
---|
2892 | wx.EVT_COMBOBOX(fun_box,-1, self._on_fun_box) |
---|
2893 | else: |
---|
2894 | fun_box = self.ModelTextCtrl(self, -1, |
---|
2895 | size=(_BOX_WIDTH,20), |
---|
2896 | style=wx.TE_PROCESS_ENTER, name ='%s'% item) |
---|
2897 | fun_box.SetToolTipString(\ |
---|
2898 | "Hit 'Enter' after typing to update the plot.") |
---|
2899 | fun_box.SetValue(format_number(value, True)) |
---|
2900 | sizer.Add(fun_box, (iy,ix),(1,1), wx.EXPAND) |
---|
2901 | self.str_parameters.append([None,item, fun_box, |
---|
2902 | None, None, None, |
---|
2903 | None, None]) |
---|
2904 | |
---|
2905 | |
---|
2906 | else: |
---|
2907 | ## add parameters name with checkbox for selecting to fit |
---|
2908 | cb = wx.CheckBox(self, -1, item ) |
---|
2909 | cb.SetToolTipString(" Check mark to fit.") |
---|
2910 | #cb.SetValue(True) |
---|
2911 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
2912 | |
---|
2913 | sizer.Add( cb,( iy, ix),(1,1), |
---|
2914 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2915 | |
---|
2916 | ## add parameter value |
---|
2917 | ix += 1 |
---|
2918 | value= self.model.getParam(item) |
---|
2919 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
2920 | style=wx.TE_PROCESS_ENTER) |
---|
2921 | ctl1.SetToolTipString(\ |
---|
2922 | "Hit 'Enter' after typing to update the plot.") |
---|
2923 | ctl1.SetValue(format_number(value, True)) |
---|
2924 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
2925 | ## text to show error sign |
---|
2926 | ix += 1 |
---|
2927 | text2=wx.StaticText(self, -1, '+/-') |
---|
2928 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
2929 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2930 | if not self.is_mac: |
---|
2931 | text2.Hide() |
---|
2932 | ix += 1 |
---|
2933 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/1.2,20), style=0) |
---|
2934 | sizer.Add(ctl2, (iy,ix),(1,1), |
---|
2935 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2936 | if not self.is_mac: |
---|
2937 | ctl2.Hide() |
---|
2938 | |
---|
2939 | ix += 1 |
---|
2940 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/1.9,20), |
---|
2941 | style=wx.TE_PROCESS_ENTER, |
---|
2942 | text_enter_callback = self._onparamRangeEnter) |
---|
2943 | |
---|
2944 | sizer.Add(ctl3, (iy,ix),(1,1), |
---|
2945 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2946 | |
---|
2947 | ix += 1 |
---|
2948 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/1.9,20), |
---|
2949 | style=wx.TE_PROCESS_ENTER, |
---|
2950 | text_enter_callback = self._onparamRangeEnter) |
---|
2951 | sizer.Add(ctl4, (iy,ix),(1,1), |
---|
2952 | wx.EXPAND|wx.FIXED_MINSIZE, 0) |
---|
2953 | |
---|
2954 | ix +=1 |
---|
2955 | # Units |
---|
2956 | if self.model.details.has_key(item): |
---|
2957 | units = wx.StaticText(self, -1, |
---|
2958 | self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
2959 | else: |
---|
2960 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
2961 | sizer.Add(units, (iy,ix),(1,1), |
---|
2962 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2963 | |
---|
2964 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
2965 | self.parameters.append([cb,item, ctl1, |
---|
2966 | text2,ctl2, ctl3, ctl4,units]) |
---|
2967 | |
---|
2968 | iy+=1 |
---|
2969 | sizer.Add((10,10),(iy,ix),(1,1), |
---|
2970 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2971 | |
---|
2972 | # type can be either Guassian or Array |
---|
2973 | if len(self.model.dispersion.values())>0: |
---|
2974 | type= self.model.dispersion.values()[0]["type"] |
---|
2975 | else: |
---|
2976 | type = "Gaussian" |
---|
2977 | |
---|
2978 | iy += 1 |
---|
2979 | ix = 0 |
---|
2980 | #Add tile for orientational angle |
---|
2981 | for item in keys: |
---|
2982 | if item in self.model.orientation_params: |
---|
2983 | orient_angle = wx.StaticText(self, -1, '[For 2D only]:') |
---|
2984 | sizer.Add(orient_angle,(iy, ix),(1,1), |
---|
2985 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2986 | if not self.data.__class__.__name__ == "Data2D" and \ |
---|
2987 | not self.enable2D: |
---|
2988 | orient_angle.Hide() |
---|
2989 | else: |
---|
2990 | orient_angle.Show(True) |
---|
2991 | break |
---|
2992 | |
---|
2993 | #For Gaussian only |
---|
2994 | if type.lower() != "array": |
---|
2995 | for item in self.model.orientation_params: |
---|
2996 | if not item in self.disp_list: |
---|
2997 | ##prepare a spot to store min max |
---|
2998 | if not self.model.details.has_key(item): |
---|
2999 | self.model.details [item] = ["",None,None] |
---|
3000 | |
---|
3001 | iy += 1 |
---|
3002 | ix = 0 |
---|
3003 | ## add parameters name with checkbox for selecting to fit |
---|
3004 | cb = wx.CheckBox(self, -1, item ) |
---|
3005 | cb.SetValue(False) |
---|
3006 | cb.SetToolTipString("Check mark to fit") |
---|
3007 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
3008 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3009 | self.enable2D: |
---|
3010 | cb.Show(True) |
---|
3011 | else: |
---|
3012 | cb.Hide() |
---|
3013 | sizer.Add( cb,( iy, ix),(1,1), |
---|
3014 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
3015 | |
---|
3016 | ## add parameter value |
---|
3017 | ix += 1 |
---|
3018 | value= self.model.getParam(item) |
---|
3019 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
3020 | style=wx.TE_PROCESS_ENTER) |
---|
3021 | ctl1.SetToolTipString(\ |
---|
3022 | "Hit 'Enter' after typing to update the plot.") |
---|
3023 | ctl1.SetValue(format_number(value, True)) |
---|
3024 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3025 | self.enable2D: |
---|
3026 | ctl1.Show(True) |
---|
3027 | else: |
---|
3028 | ctl1.Hide() |
---|
3029 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
3030 | ## text to show error sign |
---|
3031 | ix += 1 |
---|
3032 | text2=wx.StaticText(self, -1, '+/-') |
---|
3033 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
3034 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3035 | |
---|
3036 | text2.Hide() |
---|
3037 | ix += 1 |
---|
3038 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/1.2,20), style=0) |
---|
3039 | sizer.Add(ctl2, (iy,ix),(1,1), |
---|
3040 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3041 | |
---|
3042 | ctl2.Hide() |
---|
3043 | |
---|
3044 | |
---|
3045 | ix += 1 |
---|
3046 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/1.8,20), |
---|
3047 | style=wx.TE_PROCESS_ENTER, |
---|
3048 | text_enter_callback = self._onparamRangeEnter) |
---|
3049 | |
---|
3050 | sizer.Add(ctl3, (iy,ix),(1,1), |
---|
3051 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3052 | ctl3.Hide() |
---|
3053 | |
---|
3054 | ix += 1 |
---|
3055 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/1.8,20), |
---|
3056 | style=wx.TE_PROCESS_ENTER, |
---|
3057 | text_enter_callback = self._onparamRangeEnter) |
---|
3058 | sizer.Add(ctl4, (iy,ix),(1,1), |
---|
3059 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3060 | |
---|
3061 | ctl4.Hide() |
---|
3062 | |
---|
3063 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3064 | self.enable2D: |
---|
3065 | if self.is_mac: |
---|
3066 | text2.Show(True) |
---|
3067 | ctl2.Show(True) |
---|
3068 | ctl3.Show(True) |
---|
3069 | ctl4.Show(True) |
---|
3070 | |
---|
3071 | ix +=1 |
---|
3072 | # Units |
---|
3073 | if self.model.details.has_key(item): |
---|
3074 | units = wx.StaticText(self, -1, |
---|
3075 | self.model.details[item][0], |
---|
3076 | style=wx.ALIGN_LEFT) |
---|
3077 | else: |
---|
3078 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
3079 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3080 | self.enable2D: |
---|
3081 | units.Show(True) |
---|
3082 | |
---|
3083 | else: |
---|
3084 | units.Hide() |
---|
3085 | |
---|
3086 | sizer.Add(units, (iy,ix),(1,1), |
---|
3087 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3088 | |
---|
3089 | ##[cb state, name, value, "+/-", error of fit,min,max,units] |
---|
3090 | self.parameters.append([cb,item, ctl1, |
---|
3091 | text2,ctl2, ctl3, ctl4,units]) |
---|
3092 | self.orientation_params.append([cb,item, ctl1, |
---|
3093 | text2,ctl2, ctl3, ctl4,units]) |
---|
3094 | |
---|
3095 | iy+=1 |
---|
3096 | |
---|
3097 | #Display units text on panel |
---|
3098 | for item in keys: |
---|
3099 | if self.model.details.has_key(item): |
---|
3100 | self.text2_4.Show() |
---|
3101 | #Fill the list of fittable parameters |
---|
3102 | self.select_all_param(event=None) |
---|
3103 | |
---|
3104 | self.save_current_state_fit() |
---|
3105 | boxsizer1.Add(sizer) |
---|
3106 | self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
3107 | self.sizer3.Layout() |
---|
3108 | self.Layout() |
---|
3109 | #self.Refresh() |
---|
3110 | |
---|
3111 | def on_right_down(self, event): |
---|
3112 | """ |
---|
3113 | Get key stroke event |
---|
3114 | """ |
---|
3115 | if self.data == None: |
---|
3116 | return |
---|
3117 | # Figuring out key combo: Cmd for copy, Alt for paste |
---|
3118 | if event.AltDown() and event.ShiftDown(): |
---|
3119 | self._manager.show_ftol_dialog() |
---|
3120 | flag = True |
---|
3121 | elif event.AltDown() or event.ShiftDown(): |
---|
3122 | flag = False |
---|
3123 | else: |
---|
3124 | return |
---|
3125 | # make event free |
---|
3126 | event.Skip() |
---|
3127 | # messages depending on the flag |
---|
3128 | if not flag: |
---|
3129 | msg = " Could not open ftol dialog;" |
---|
3130 | msg += " Check if the Scipy fit engine is selected in the menubar." |
---|
3131 | infor = 'warning' |
---|
3132 | # inform msg to wx |
---|
3133 | wx.PostEvent( self.parent.parent, |
---|
3134 | StatusEvent(status= msg, info=infor)) |
---|
3135 | |
---|
3136 | |
---|
3137 | def _onModel2D(self, event): |
---|
3138 | """ |
---|
3139 | toggle view of model from 1D to 2D or 2D from 1D |
---|
3140 | """ |
---|
3141 | if self.model_view.GetLabelText() == "Show 2D": |
---|
3142 | self.model_view.SetLabel("Show 1D") |
---|
3143 | self.enable2D = True |
---|
3144 | |
---|
3145 | else: |
---|
3146 | self.model_view.SetLabel("Show 2D") |
---|
3147 | self.enable2D = False |
---|
3148 | self.create_default_data() |
---|
3149 | self._manager.store_data(self.uid, data_list=[self.data]) |
---|
3150 | |
---|
3151 | self.set_model_param_sizer(self.model) |
---|
3152 | self._set_sizer_dispersion() |
---|
3153 | self._set_weight(is_2D= self.enable2D) |
---|
3154 | self._set_smear_buttons() |
---|
3155 | self._draw_model() |
---|
3156 | |
---|
3157 | |
---|
3158 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
3159 | |
---|
3160 | def _set_smear_buttons(self): |
---|
3161 | """ |
---|
3162 | Set semarer radio buttons |
---|
3163 | """ |
---|
3164 | # more disables for 2D |
---|
3165 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3166 | self.enable2D: |
---|
3167 | self.slit_smearer.Disable() |
---|
3168 | self.pinhole_smearer.Enable(True) |
---|
3169 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
3170 | else: |
---|
3171 | self.slit_smearer.Enable(True) |
---|
3172 | self.pinhole_smearer.Enable(True) |
---|
3173 | |
---|
3174 | |
---|
3175 | class BGTextCtrl(wx.TextCtrl): |
---|
3176 | """ |
---|
3177 | Text control used to display outputs. |
---|
3178 | No editing allowed. The background is |
---|
3179 | grayed out. User can't select text. |
---|
3180 | """ |
---|
3181 | def __init__(self, *args, **kwds): |
---|
3182 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
3183 | self.SetEditable(False) |
---|
3184 | self.SetBackgroundColour(self.GetParent().parent.GetBackgroundColour()) |
---|
3185 | |
---|
3186 | # Bind to mouse event to avoid text highlighting |
---|
3187 | # The event will be skipped once the call-back |
---|
3188 | # is called. |
---|
3189 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
3190 | |
---|
3191 | def _click(self, event): |
---|
3192 | """ |
---|
3193 | Prevent further handling of the mouse event |
---|
3194 | by not calling Skip(). |
---|
3195 | """ |
---|
3196 | pass |
---|
3197 | |
---|