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