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