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