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