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_range(uid=self.uid,qmin=self.qmin_x, |
---|
1111 | qmax=self.qmax_x) |
---|
1112 | #single fit |
---|
1113 | self._manager.onFit(uid=self.uid) |
---|
1114 | self.btFit.SetLabel("Stop") |
---|
1115 | self.bind_fit_button() |
---|
1116 | |
---|
1117 | def get_weight_flag(self): |
---|
1118 | """ |
---|
1119 | Get flag corresponding to a given weighting dI data. |
---|
1120 | """ |
---|
1121 | button_list = [self.dI_noweight, |
---|
1122 | self.dI_didata, |
---|
1123 | self.dI_sqrdata, |
---|
1124 | self.dI_idata] |
---|
1125 | flag = 1 |
---|
1126 | for item in button_list: |
---|
1127 | if item.GetValue(): |
---|
1128 | if button_list.index(item) == 0: |
---|
1129 | flag = 0 #dy = numpy.ones_like(dy_data) |
---|
1130 | elif button_list.index(item) == 1: |
---|
1131 | flag = 1 #dy = dy_data |
---|
1132 | elif button_list.index(item) == 2: |
---|
1133 | flag = 2 #dy = numpy.sqrt(numpy.abs(data)) |
---|
1134 | elif button_list.index(item) == 3: |
---|
1135 | flag = 3 # dy = numpy.abs(data) |
---|
1136 | break |
---|
1137 | return flag |
---|
1138 | |
---|
1139 | def bind_fit_button(self): |
---|
1140 | """ |
---|
1141 | bind the fit button to either fit handler or stop fit handler |
---|
1142 | """ |
---|
1143 | self.btFit.Unbind(event=wx.EVT_BUTTON, id= self.btFit.GetId()) |
---|
1144 | if self.btFit.GetLabel().lower() == "stop": |
---|
1145 | self.fit_started = True |
---|
1146 | self.btFit.SetForegroundColour('red') |
---|
1147 | self.btFit.Bind(event=wx.EVT_BUTTON, handler=self._StopFit, |
---|
1148 | id=self.btFit.GetId()) |
---|
1149 | elif self.btFit.GetLabel().lower() == "fit": |
---|
1150 | self.fit_started = False |
---|
1151 | self.btFit.SetDefault() |
---|
1152 | self.btFit.SetForegroundColour('black') |
---|
1153 | #self.btFit.SetBackgroundColour(self.default_bt_colour) |
---|
1154 | self.btFit.Bind(event=wx.EVT_BUTTON, handler=self._onFit, |
---|
1155 | id=self.btFit.GetId()) |
---|
1156 | else: |
---|
1157 | msg = "FitPage: fit button has unknown label" |
---|
1158 | raise ValuerError, msg |
---|
1159 | self._manager._reset_schedule_problem(value=0) |
---|
1160 | |
---|
1161 | def is_fitting(self): |
---|
1162 | if self.fit_started: |
---|
1163 | self._StopFit(event=None) |
---|
1164 | |
---|
1165 | def _StopFit(self, event=None): |
---|
1166 | """ |
---|
1167 | Stop fit |
---|
1168 | """ |
---|
1169 | #time.sleep(0.1) |
---|
1170 | if event != None: |
---|
1171 | event.Skip() |
---|
1172 | #if self.engine_type=="scipy": |
---|
1173 | self._manager.stop_fit(self.uid) |
---|
1174 | self._manager._reset_schedule_problem(value=0) |
---|
1175 | self._on_fit_complete() |
---|
1176 | |
---|
1177 | def rename_model(self): |
---|
1178 | """ |
---|
1179 | find a short name for model |
---|
1180 | """ |
---|
1181 | if self.model is not None: |
---|
1182 | self.model.name = "M" + str(self.index_model) |
---|
1183 | |
---|
1184 | def _on_select_model(self, event=None): |
---|
1185 | """ |
---|
1186 | call back for model selection |
---|
1187 | """ |
---|
1188 | |
---|
1189 | self.Show(False) |
---|
1190 | self._on_select_model_helper() |
---|
1191 | self.set_model_param_sizer(self.model) |
---|
1192 | if self.model is None: |
---|
1193 | self._set_bookmark_flag(False) |
---|
1194 | self._keep.Enable(False) |
---|
1195 | self._set_save_flag(False) |
---|
1196 | self.enable_disp.SetValue(False) |
---|
1197 | self.disable_disp.SetValue(True) |
---|
1198 | try: |
---|
1199 | self.set_dispers_sizer() |
---|
1200 | except: |
---|
1201 | pass |
---|
1202 | #self.btFit.SetFocus() |
---|
1203 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
1204 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
1205 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
1206 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
1207 | |
---|
1208 | self.state.structurecombobox = self.structurebox.GetCurrentSelection() |
---|
1209 | self.state.formfactorcombobox = self.formfactorbox.GetCurrentSelection() |
---|
1210 | self.enable_fit_button() |
---|
1211 | if self.model != None: |
---|
1212 | self.rename_model() |
---|
1213 | self._set_copy_flag(True) |
---|
1214 | self._set_paste_flag(True) |
---|
1215 | if self.data != None: |
---|
1216 | self._set_bookmark_flag(True) |
---|
1217 | self._keep.Enable(True) |
---|
1218 | #self._set_save_flag(True) |
---|
1219 | # Reset smearer, model and data |
---|
1220 | self._set_smear(self.data) |
---|
1221 | |
---|
1222 | # more disables for 2D |
---|
1223 | self._set_smear_buttons() |
---|
1224 | |
---|
1225 | try: |
---|
1226 | # update smearer sizer |
---|
1227 | self.onSmear(None) |
---|
1228 | temp_smear = None |
---|
1229 | if not self.disable_smearer.GetValue(): |
---|
1230 | # Set the smearer environments |
---|
1231 | temp_smear = self.current_smearer |
---|
1232 | except: |
---|
1233 | raise |
---|
1234 | ## error occured on chisqr computation |
---|
1235 | #pass |
---|
1236 | ## event to post model to fit to fitting plugins |
---|
1237 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
1238 | |
---|
1239 | ## set smearing value whether or not |
---|
1240 | # the data contain the smearing info |
---|
1241 | evt = ModelEventbox(model=self.model, |
---|
1242 | smearer=temp_smear, |
---|
1243 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
1244 | qmin=float(self.qmin_x), |
---|
1245 | uid=self.uid, |
---|
1246 | caption=self.window_caption, |
---|
1247 | qmax=float(self.qmax_x)) |
---|
1248 | |
---|
1249 | self._manager._on_model_panel(evt=evt) |
---|
1250 | self.mbox_description.SetLabel("Model [%s]" % str(self.model.name)) |
---|
1251 | self.state.model = self.model.clone() |
---|
1252 | self.state.model.name = self.model.name |
---|
1253 | |
---|
1254 | |
---|
1255 | if event != None: |
---|
1256 | ## post state to fit panel |
---|
1257 | new_event = PageInfoEvent(page = self) |
---|
1258 | wx.PostEvent(self.parent, new_event) |
---|
1259 | #update list of plugins if new plugin is available |
---|
1260 | if self.plugin_rbutton.GetValue(): |
---|
1261 | temp = self.parent.update_model_list() |
---|
1262 | if temp: |
---|
1263 | self.model_list_box = temp |
---|
1264 | current_val = self.formfactorbox.GetValue() |
---|
1265 | pos = self.formfactorbox.GetSelection() |
---|
1266 | self._show_combox_helper() |
---|
1267 | self.formfactorbox.SetSelection(pos) |
---|
1268 | self.formfactorbox.SetValue(current_val) |
---|
1269 | self._onDraw(event=None) |
---|
1270 | else: |
---|
1271 | self._draw_model() |
---|
1272 | self.SetupScrolling() |
---|
1273 | self.Show(True) |
---|
1274 | |
---|
1275 | def _onparamEnter(self,event): |
---|
1276 | """ |
---|
1277 | when enter value on panel redraw model according to changed |
---|
1278 | """ |
---|
1279 | if self.model ==None: |
---|
1280 | msg="Please select a Model first..." |
---|
1281 | wx.MessageBox(msg, 'Info') |
---|
1282 | #wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
1283 | # "Parameters: %s"%msg)) |
---|
1284 | return |
---|
1285 | |
---|
1286 | #default flag |
---|
1287 | flag = False |
---|
1288 | self.fitrange = True |
---|
1289 | #get event object |
---|
1290 | tcrtl = event.GetEventObject() |
---|
1291 | |
---|
1292 | #wx.PostEvent(self._manager.parent, StatusEvent(status=" \ |
---|
1293 | # updating ... ",type="update")) |
---|
1294 | #Clear msg if previously shown. |
---|
1295 | msg= "" |
---|
1296 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1297 | |
---|
1298 | if check_float(tcrtl): |
---|
1299 | flag = self._onparamEnter_helper() |
---|
1300 | self.set_npts2fit() |
---|
1301 | if self.fitrange: |
---|
1302 | temp_smearer = None |
---|
1303 | if not self.disable_smearer.GetValue(): |
---|
1304 | temp_smearer= self.current_smearer |
---|
1305 | ## set smearing value whether or not |
---|
1306 | # the data contain the smearing info |
---|
1307 | if self.slit_smearer.GetValue(): |
---|
1308 | flag1 = self.update_slit_smear() |
---|
1309 | flag = flag or flag1 |
---|
1310 | elif self.pinhole_smearer.GetValue(): |
---|
1311 | flag1 = self.update_pinhole_smear() |
---|
1312 | flag = flag or flag1 |
---|
1313 | elif self.data.__class__.__name__ != "Data2D" and \ |
---|
1314 | not self.enable2D: |
---|
1315 | self._manager.set_smearer(smearer=temp_smearer, |
---|
1316 | fid=self.data.id, |
---|
1317 | uid=self.uid, |
---|
1318 | qmin= float(self.qmin_x), |
---|
1319 | qmax= float(self.qmax_x), |
---|
1320 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
1321 | draw=True) |
---|
1322 | if flag: |
---|
1323 | #self.compute_chisqr(smearer= temp_smearer) |
---|
1324 | |
---|
1325 | ## new state posted |
---|
1326 | if self.state_change: |
---|
1327 | #self._undo.Enable(True) |
---|
1328 | event = PageInfoEvent(page = self) |
---|
1329 | wx.PostEvent(self.parent, event) |
---|
1330 | self.state_change= False |
---|
1331 | else: |
---|
1332 | # invalid fit range: do nothing here: |
---|
1333 | # msg already displayed in validate |
---|
1334 | return |
---|
1335 | else: |
---|
1336 | self.save_current_state() |
---|
1337 | msg= "Cannot Plot :Must enter a number!!! " |
---|
1338 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1339 | |
---|
1340 | self.save_current_state() |
---|
1341 | return |
---|
1342 | |
---|
1343 | def _onparamRangeEnter(self, event): |
---|
1344 | """ |
---|
1345 | Check validity of value enter in the parameters range field |
---|
1346 | """ |
---|
1347 | #if self.check_invalid_panel(): |
---|
1348 | # return |
---|
1349 | tcrtl= event.GetEventObject() |
---|
1350 | #Clear msg if previously shown. |
---|
1351 | msg= "" |
---|
1352 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1353 | # Flag to register when a parameter has changed. |
---|
1354 | is_modified = False |
---|
1355 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
1356 | try: |
---|
1357 | value = float(tcrtl.GetValue()) |
---|
1358 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1359 | self._check_value_enter(self.fittable_param ,is_modified) |
---|
1360 | self._check_value_enter(self.parameters ,is_modified) |
---|
1361 | except: |
---|
1362 | tcrtl.SetBackgroundColour("pink") |
---|
1363 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1364 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1365 | return |
---|
1366 | else: |
---|
1367 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1368 | |
---|
1369 | #self._undo.Enable(True) |
---|
1370 | self.save_current_state() |
---|
1371 | event = PageInfoEvent(page = self) |
---|
1372 | wx.PostEvent(self.parent, event) |
---|
1373 | self.state_change= False |
---|
1374 | |
---|
1375 | |
---|
1376 | def _onQrangeEnter(self, event): |
---|
1377 | """ |
---|
1378 | Check validity of value enter in the Q range field |
---|
1379 | """ |
---|
1380 | tcrtl = event.GetEventObject() |
---|
1381 | #Clear msg if previously shown. |
---|
1382 | msg= "" |
---|
1383 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1384 | # Flag to register when a parameter has changed. |
---|
1385 | is_modified = False |
---|
1386 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
1387 | try: |
---|
1388 | value = float(tcrtl.GetValue()) |
---|
1389 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
1390 | |
---|
1391 | # If qmin and qmax have been modified, update qmin and qmax |
---|
1392 | if self._validate_qrange( self.qmin, self.qmax): |
---|
1393 | tempmin = float(self.qmin.GetValue()) |
---|
1394 | if tempmin != self.qmin_x: |
---|
1395 | self.qmin_x = tempmin |
---|
1396 | tempmax = float(self.qmax.GetValue()) |
---|
1397 | if tempmax != self.qmax_x: |
---|
1398 | self.qmax_x = tempmax |
---|
1399 | else: |
---|
1400 | tcrtl.SetBackgroundColour("pink") |
---|
1401 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1402 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1403 | return |
---|
1404 | |
---|
1405 | except: |
---|
1406 | tcrtl.SetBackgroundColour("pink") |
---|
1407 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1408 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1409 | return |
---|
1410 | #Check if # of points for theory model are valid(>0). |
---|
1411 | # check for 2d |
---|
1412 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
1413 | self.enable2D: |
---|
1414 | # set mask |
---|
1415 | radius= numpy.sqrt(self.data.qx_data*self.data.qx_data + |
---|
1416 | self.data.qy_data*self.data.qy_data ) |
---|
1417 | index_data = ((self.qmin_x <= radius)& \ |
---|
1418 | (radius<= self.qmax_x)) |
---|
1419 | index_data = (index_data)&(self.data.mask) |
---|
1420 | index_data = (index_data)&(numpy.isfinite(self.data.data)) |
---|
1421 | if len(index_data[index_data]) < 10: |
---|
1422 | msg = "Cannot Plot :No or too little npts in" |
---|
1423 | msg += " that data range!!! " |
---|
1424 | wx.PostEvent(self.parent.parent, |
---|
1425 | StatusEvent(status=msg)) |
---|
1426 | return |
---|
1427 | else: |
---|
1428 | self.data.mask = index_data |
---|
1429 | #self.Npts_fit.SetValue(str(len(self.data.mask))) |
---|
1430 | self.set_npts2fit() |
---|
1431 | else: |
---|
1432 | index_data = ((self.qmin_x <= self.data.x)& \ |
---|
1433 | (self.data.x <= self.qmax_x)) |
---|
1434 | self.Npts_fit.SetValue(str(len(self.data.x[index_data]))) |
---|
1435 | |
---|
1436 | self.npts_x = self.Npts_total.GetValue() |
---|
1437 | self.create_default_data() |
---|
1438 | self._save_plotting_range() |
---|
1439 | else: |
---|
1440 | tcrtl.SetBackgroundColour("pink") |
---|
1441 | msg= "Model Error:wrong value entered!!!" |
---|
1442 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1443 | |
---|
1444 | self._draw_model() |
---|
1445 | self.save_current_state() |
---|
1446 | event = PageInfoEvent(page = self) |
---|
1447 | wx.PostEvent(self.parent, event) |
---|
1448 | self.state_change= False |
---|
1449 | return |
---|
1450 | |
---|
1451 | def _clear_Err_on_Fit(self): |
---|
1452 | """ |
---|
1453 | hide the error text control shown |
---|
1454 | after fitting |
---|
1455 | """ |
---|
1456 | if self.is_mac: |
---|
1457 | return |
---|
1458 | if hasattr(self,"text2_3"): |
---|
1459 | self.text2_3.Hide() |
---|
1460 | |
---|
1461 | if len(self.parameters)>0: |
---|
1462 | for item in self.parameters: |
---|
1463 | #Skip t ifhe angle parameters if 1D data |
---|
1464 | if self.data.__class__.__name__ != "Data2D" and \ |
---|
1465 | not self.enable2D: |
---|
1466 | if item in self.orientation_params: |
---|
1467 | continue |
---|
1468 | if item in self.param_toFit: |
---|
1469 | continue |
---|
1470 | ## hide statictext +/- |
---|
1471 | if len(item) < 4 : |
---|
1472 | continue |
---|
1473 | if item[3]!=None and item[3].IsShown(): |
---|
1474 | item[3].Hide() |
---|
1475 | ## hide textcrtl for error after fit |
---|
1476 | if item[4]!=None and item[4].IsShown(): |
---|
1477 | item[4].Hide() |
---|
1478 | |
---|
1479 | if len(self.fittable_param)>0: |
---|
1480 | for item in self.fittable_param: |
---|
1481 | #Skip t ifhe angle parameters if 1D data |
---|
1482 | if self.data.__class__.__name__ != "Data2D" and \ |
---|
1483 | not self.enable2D: |
---|
1484 | if item in self.orientation_params: |
---|
1485 | continue |
---|
1486 | if item in self.param_toFit: |
---|
1487 | continue |
---|
1488 | if len(item) < 4 : |
---|
1489 | continue |
---|
1490 | ## hide statictext +/- |
---|
1491 | if item[3]!=None and item[3].IsShown(): |
---|
1492 | item[3].Hide() |
---|
1493 | ## hide textcrtl for error after fit |
---|
1494 | if item[4]!=None and item[4].IsShown(): |
---|
1495 | item[4].Hide() |
---|
1496 | return |
---|
1497 | |
---|
1498 | def _get_defult_custom_smear(self): |
---|
1499 | """ |
---|
1500 | Get the defult values for custum smearing. |
---|
1501 | """ |
---|
1502 | # get the default values |
---|
1503 | if self.dxl == None: self.dxl = 0.0 |
---|
1504 | if self.dxw == None: self.dxw = "" |
---|
1505 | if self.dx_min == None: self.dx_min = SMEAR_SIZE_L |
---|
1506 | if self.dx_max == None: self.dx_max = SMEAR_SIZE_H |
---|
1507 | |
---|
1508 | def _get_smear_info(self): |
---|
1509 | """ |
---|
1510 | Get the smear info from data. |
---|
1511 | |
---|
1512 | :return: self.smear_type, self.dq_l and self.dq_r, |
---|
1513 | respectively the type of the smear, dq_min and |
---|
1514 | dq_max for pinhole smear data |
---|
1515 | while dxl and dxw for slit smear |
---|
1516 | |
---|
1517 | """ |
---|
1518 | |
---|
1519 | # default |
---|
1520 | self.smear_type = None |
---|
1521 | self.dq_l = None |
---|
1522 | self.dq_r = None |
---|
1523 | data = self.data |
---|
1524 | if self.data is None: |
---|
1525 | return |
---|
1526 | elif self.data.__class__.__name__ == "Data2D" or \ |
---|
1527 | self.enable2D: |
---|
1528 | if data.dqx_data == None or data.dqy_data ==None: |
---|
1529 | return |
---|
1530 | elif self.current_smearer != None and (data.dqx_data.any()!=0) and \ |
---|
1531 | (data.dqx_data.any()!=0): |
---|
1532 | self.smear_type = "Pinhole2d" |
---|
1533 | self.dq_l = format_number(numpy.average(data.dqx_data)) |
---|
1534 | self.dq_r = format_number(numpy.average(data.dqy_data)) |
---|
1535 | return |
---|
1536 | else: |
---|
1537 | return |
---|
1538 | # check if it is pinhole smear and get min max if it is. |
---|
1539 | if data.dx != None and all(data.dx !=0): |
---|
1540 | self.smear_type = "Pinhole" |
---|
1541 | self.dq_l = data.dx[0] |
---|
1542 | self.dq_r = data.dx[-1] |
---|
1543 | |
---|
1544 | # check if it is slit smear and get min max if it is. |
---|
1545 | elif data.dxl != None or data.dxw != None: |
---|
1546 | self.smear_type = "Slit" |
---|
1547 | if data.dxl != None and all(data.dxl !=0): |
---|
1548 | self.dq_l = data.dxl[0] |
---|
1549 | if data.dxw != None and all(data.dxw !=0): |
---|
1550 | self.dq_r = data.dxw[0] |
---|
1551 | #return self.smear_type,self.dq_l,self.dq_r |
---|
1552 | |
---|
1553 | def _show_smear_sizer(self): |
---|
1554 | """ |
---|
1555 | Show only the sizers depending on smear selection |
---|
1556 | """ |
---|
1557 | # smear disabled |
---|
1558 | if self.disable_smearer.GetValue(): |
---|
1559 | self.smear_description_none.Show(True) |
---|
1560 | # 2Dsmear |
---|
1561 | elif self._is_2D(): |
---|
1562 | self.smear_description_accuracy_type.Show(True) |
---|
1563 | self.smear_accuracy.Show(True) |
---|
1564 | self.smear_description_accuracy_type.Show(True) |
---|
1565 | self.smear_description_2d.Show(True) |
---|
1566 | self.smear_description_2d_x.Show(True) |
---|
1567 | self.smear_description_2d_y.Show(True) |
---|
1568 | if self.pinhole_smearer.GetValue(): |
---|
1569 | self.smear_pinhole_min.Show(True) |
---|
1570 | self.smear_pinhole_max.Show(True) |
---|
1571 | # smear from data |
---|
1572 | elif self.enable_smearer.GetValue(): |
---|
1573 | |
---|
1574 | self.smear_description_dqdata.Show(True) |
---|
1575 | if self.smear_type != None: |
---|
1576 | self.smear_description_smear_type.Show(True) |
---|
1577 | if self.smear_type == 'Slit': |
---|
1578 | self.smear_description_slit_height.Show(True) |
---|
1579 | self.smear_description_slit_width.Show(True) |
---|
1580 | elif self.smear_type == 'Pinhole': |
---|
1581 | self.smear_description_pin_min.Show(True) |
---|
1582 | self.smear_description_pin_max.Show(True) |
---|
1583 | self.smear_description_smear_type.Show(True) |
---|
1584 | self.smear_description_type.Show(True) |
---|
1585 | self.smear_data_left.Show(True) |
---|
1586 | self.smear_data_right.Show(True) |
---|
1587 | # custom pinhole smear |
---|
1588 | elif self.pinhole_smearer.GetValue(): |
---|
1589 | if self.smear_type == 'Pinhole': |
---|
1590 | self.smear_message_new_p.Show(True) |
---|
1591 | self.smear_description_pin_min.Show(True) |
---|
1592 | self.smear_description_pin_max.Show(True) |
---|
1593 | |
---|
1594 | self.smear_pinhole_min.Show(True) |
---|
1595 | self.smear_pinhole_max.Show(True) |
---|
1596 | # custom slit smear |
---|
1597 | elif self.slit_smearer.GetValue(): |
---|
1598 | self.smear_message_new_s.Show(True) |
---|
1599 | self.smear_description_slit_height.Show(True) |
---|
1600 | self.smear_slit_height.Show(True) |
---|
1601 | self.smear_description_slit_width.Show(True) |
---|
1602 | self.smear_slit_width.Show(True) |
---|
1603 | |
---|
1604 | def _hide_all_smear_info(self): |
---|
1605 | """ |
---|
1606 | Hide all smearing messages in the set_smearer sizer |
---|
1607 | """ |
---|
1608 | self.smear_description_none.Hide() |
---|
1609 | self.smear_description_dqdata.Hide() |
---|
1610 | self.smear_description_type.Hide() |
---|
1611 | self.smear_description_smear_type.Hide() |
---|
1612 | self.smear_description_accuracy_type.Hide() |
---|
1613 | self.smear_description_2d_x.Hide() |
---|
1614 | self.smear_description_2d_y.Hide() |
---|
1615 | self.smear_description_2d.Hide() |
---|
1616 | |
---|
1617 | self.smear_accuracy.Hide() |
---|
1618 | self.smear_data_left.Hide() |
---|
1619 | self.smear_data_right.Hide() |
---|
1620 | self.smear_description_pin_min.Hide() |
---|
1621 | self.smear_pinhole_min.Hide() |
---|
1622 | self.smear_description_pin_max.Hide() |
---|
1623 | self.smear_pinhole_max.Hide() |
---|
1624 | self.smear_description_slit_height.Hide() |
---|
1625 | self.smear_slit_height.Hide() |
---|
1626 | self.smear_description_slit_width.Hide() |
---|
1627 | self.smear_slit_width.Hide() |
---|
1628 | self.smear_message_new_p.Hide() |
---|
1629 | self.smear_message_new_s.Hide() |
---|
1630 | |
---|
1631 | def _set_accuracy_list(self): |
---|
1632 | """ |
---|
1633 | Set the list of an accuracy in 2D custum smear: Xhigh, High, Med, or Low |
---|
1634 | """ |
---|
1635 | # list of accuracy choices |
---|
1636 | list = ['Low','Med','High','Xhigh'] |
---|
1637 | for idx in range(len(list)): |
---|
1638 | self.smear_accuracy.Append(list[idx],idx) |
---|
1639 | |
---|
1640 | def _set_fun_box_list(self,fun_box): |
---|
1641 | """ |
---|
1642 | Set the list of func for multifunctional models |
---|
1643 | """ |
---|
1644 | # Check if it is multi_functional model |
---|
1645 | if self.model.__class__ not in self.model_list_box["Multi-Functions"] \ |
---|
1646 | and not self.temp_multi_functional: |
---|
1647 | return None |
---|
1648 | # Get the func name list |
---|
1649 | list = self.model.fun_list |
---|
1650 | if len(list) == 0: |
---|
1651 | return None |
---|
1652 | # build function (combo)box |
---|
1653 | ind = 0 |
---|
1654 | while(ind < len(list)): |
---|
1655 | for key, val in list.iteritems(): |
---|
1656 | if (val == ind): |
---|
1657 | fun_box.Append(key,val) |
---|
1658 | break |
---|
1659 | ind += 1 |
---|
1660 | |
---|
1661 | def _on_select_accuracy(self,event): |
---|
1662 | """ |
---|
1663 | Select an accuracy in 2D custom smear: Xhigh, High, Med, or Low |
---|
1664 | """ |
---|
1665 | #event.Skip() |
---|
1666 | # Check if the accuracy is same as before |
---|
1667 | #self.smear2d_accuracy = event.GetEventObject().GetValue() |
---|
1668 | self.smear2d_accuracy = self.smear_accuracy.GetValue() |
---|
1669 | if self.pinhole_smearer.GetValue(): |
---|
1670 | self.onPinholeSmear(event=None) |
---|
1671 | else: |
---|
1672 | self.onSmear(event=None) |
---|
1673 | if self.current_smearer != None: |
---|
1674 | self.current_smearer.set_accuracy(accuracy = |
---|
1675 | self.smear2d_accuracy) |
---|
1676 | event.Skip() |
---|
1677 | |
---|
1678 | def _on_fun_box(self,event): |
---|
1679 | """ |
---|
1680 | Select an func: Erf,Rparabola,LParabola |
---|
1681 | """ |
---|
1682 | fun_val = None |
---|
1683 | fun_box = event.GetEventObject() |
---|
1684 | name = fun_box.Name |
---|
1685 | value = fun_box.GetValue() |
---|
1686 | if self.model.fun_list.has_key(value): |
---|
1687 | fun_val = self.model.fun_list[value] |
---|
1688 | |
---|
1689 | self.model.setParam(name,fun_val) |
---|
1690 | # save state |
---|
1691 | self._copy_parameters_state(self.str_parameters, |
---|
1692 | self.state.str_parameters) |
---|
1693 | # update params |
---|
1694 | self._update_paramv_on_fit() |
---|
1695 | # draw |
---|
1696 | self._draw_model() |
---|
1697 | self.Refresh() |
---|
1698 | # get ready for new event |
---|
1699 | event.Skip() |
---|
1700 | |
---|
1701 | def _onMask(self, event): |
---|
1702 | """ |
---|
1703 | Build a panel to allow to edit Mask |
---|
1704 | """ |
---|
1705 | |
---|
1706 | from sans.guiframe.local_perspectives.plotting.masking \ |
---|
1707 | import MaskPanel as MaskDialog |
---|
1708 | |
---|
1709 | self.panel = MaskDialog(base=self, data=self.data, id=wx.NewId()) |
---|
1710 | #self.panel.Bind(wx.EVT_CLOSE, self._draw_masked_model) |
---|
1711 | self.panel.ShowModal() |
---|
1712 | #wx.PostEvent(self.parent, event) |
---|
1713 | |
---|
1714 | def _draw_masked_model(self, event): |
---|
1715 | """ |
---|
1716 | Draw model image w/mask |
---|
1717 | """ |
---|
1718 | #event.Skip() |
---|
1719 | is_valid_qrange = self._update_paramv_on_fit() |
---|
1720 | |
---|
1721 | if is_valid_qrange and self.model != None: |
---|
1722 | #self.panel.Show(0) |
---|
1723 | #self.panel.Destroy() # frame |
---|
1724 | self.panel.MakeModal(False) |
---|
1725 | event.Skip() |
---|
1726 | # try re draw the model plot if it exists |
---|
1727 | self._draw_model() |
---|
1728 | self.set_npts2fit() |
---|
1729 | elif self.model == None: |
---|
1730 | #self.panel.Show(0) |
---|
1731 | #self.panel.Destroy() # frame |
---|
1732 | self.panel.MakeModal(False) |
---|
1733 | event.Skip() |
---|
1734 | self.set_npts2fit() |
---|
1735 | msg= "No model is found on updating MASK in the model plot... " |
---|
1736 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1737 | else: |
---|
1738 | event.Skip() |
---|
1739 | msg = ' Please consider your Q range, too.' |
---|
1740 | self.panel.ShowMessage(msg) |
---|
1741 | |
---|
1742 | |
---|
1743 | def _set_smear(self, data): |
---|
1744 | """ |
---|
1745 | """ |
---|
1746 | if data is None: |
---|
1747 | return |
---|
1748 | self.current_smearer = smear_selection(data, self.model) |
---|
1749 | self.disable_smearer.SetValue(True) |
---|
1750 | if self.current_smearer == None: |
---|
1751 | self.enable_smearer.Disable() |
---|
1752 | else: |
---|
1753 | self.enable_smearer.Enable() |
---|
1754 | |
---|
1755 | def _mac_sleep(self, sec=0.2): |
---|
1756 | """ |
---|
1757 | Give sleep to MAC |
---|
1758 | """ |
---|
1759 | if self.is_mac: |
---|
1760 | time.sleep(sec) |
---|
1761 | |
---|
1762 | def get_view_mode(self): |
---|
1763 | """ |
---|
1764 | return True if the panel allow 2D or False if 1D |
---|
1765 | """ |
---|
1766 | return self.enable2D |
---|
1767 | |
---|
1768 | def compute_data_set_range(self, data): |
---|
1769 | """ |
---|
1770 | find the range that include all data in the set |
---|
1771 | return the minimum and the maximum values |
---|
1772 | """ |
---|
1773 | if data is not None: |
---|
1774 | qmin, qmax, npts = self.compute_data_range(data) |
---|
1775 | self.qmin_data_set = min(self.qmin_data_set, qmin) |
---|
1776 | self.qmax_data_set = max(self.qmax_data_set, qmax) |
---|
1777 | self.npts_data_set += npts |
---|
1778 | |
---|
1779 | def compute_data_range(self, data): |
---|
1780 | """ |
---|
1781 | compute the minimum and the maximum range of the data |
---|
1782 | return the npts contains in data |
---|
1783 | :param data: |
---|
1784 | """ |
---|
1785 | qmin, qmax, npts = None, None, None |
---|
1786 | if data is not None: |
---|
1787 | if not hasattr(data,"data"): |
---|
1788 | qmin = min(data.x) |
---|
1789 | # Maximum value of data |
---|
1790 | qmax = max(data.x) |
---|
1791 | npts = len(data.x) |
---|
1792 | else: |
---|
1793 | qmin = 0 |
---|
1794 | x = max(math.fabs(data.xmin), math.fabs(data.xmax)) |
---|
1795 | y = max(math.fabs(data.ymin), math.fabs(data.ymax)) |
---|
1796 | ## Maximum value of data |
---|
1797 | qmax = math.sqrt(x*x + y*y) |
---|
1798 | npts = len(data.data) |
---|
1799 | return qmin, qmax, npts |
---|
1800 | |
---|
1801 | |
---|
1802 | def set_data(self, data): |
---|
1803 | """ |
---|
1804 | reset the current data |
---|
1805 | """ |
---|
1806 | id = None |
---|
1807 | group_id = None |
---|
1808 | flag = False |
---|
1809 | if self.data is not None: |
---|
1810 | group_id = self.data.group_id |
---|
1811 | |
---|
1812 | if self.data is None and data is not None: |
---|
1813 | flag = True |
---|
1814 | if data is not None: |
---|
1815 | id = data.id |
---|
1816 | if self.data is not None: |
---|
1817 | flag = (data.id != self.data.id) |
---|
1818 | self.data = data |
---|
1819 | self.data.group_id = group_id |
---|
1820 | if self.data is None: |
---|
1821 | data_min = "" |
---|
1822 | data_max = "" |
---|
1823 | data_name = "" |
---|
1824 | self._set_bookmark_flag(False) |
---|
1825 | self._keep.Enable(False) |
---|
1826 | self._set_save_flag(False) |
---|
1827 | else: |
---|
1828 | if self.model != None: |
---|
1829 | self._set_bookmark_flag(True) |
---|
1830 | self._keep.Enable(True) |
---|
1831 | |
---|
1832 | self._set_save_flag(True) |
---|
1833 | self._set_preview_flag(True) |
---|
1834 | |
---|
1835 | self._set_smear(data) |
---|
1836 | # more disables for 2D |
---|
1837 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
1838 | self.enable2D: |
---|
1839 | self.slit_smearer.Disable() |
---|
1840 | self.pinhole_smearer.Enable(True) |
---|
1841 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
1842 | if self.data.err_data == None or\ |
---|
1843 | (self.data.err_data == 1).all() or\ |
---|
1844 | (self.data.err_data == 0).all(): |
---|
1845 | self.dI_didata.Enable(False) |
---|
1846 | self.dI_noweight.SetValue(True) |
---|
1847 | self.weightbt_string = self.dI_noweight.GetLabelText() |
---|
1848 | else: |
---|
1849 | self.dI_didata.Enable(True) |
---|
1850 | self.dI_didata.SetValue(True) |
---|
1851 | self.weightbt_string = self.dI_didata.GetLabelText() |
---|
1852 | else: |
---|
1853 | self.slit_smearer.Enable(True) |
---|
1854 | self.pinhole_smearer.Enable(True) |
---|
1855 | if self.data.dy == None or\ |
---|
1856 | (self.data.dy == 1).all() or\ |
---|
1857 | (self.data.dy == 0).all(): |
---|
1858 | self.dI_didata.Enable(False) |
---|
1859 | self.dI_noweight.SetValue(True) |
---|
1860 | self.weightbt_string = self.dI_noweight.GetLabelText() |
---|
1861 | else: |
---|
1862 | self.dI_didata.Enable(True) |
---|
1863 | self.dI_didata.SetValue(True) |
---|
1864 | self.weightbt_string = self.dI_didata.GetLabelText() |
---|
1865 | # Enable weighting radio uttons |
---|
1866 | self.dI_noweight.Enable(True) |
---|
1867 | self.dI_sqrdata.Enable(True) |
---|
1868 | self.dI_idata.Enable(True) |
---|
1869 | |
---|
1870 | self.formfactorbox.Enable() |
---|
1871 | self.structurebox.Enable() |
---|
1872 | data_name = self.data.name |
---|
1873 | data_min, data_max = self.qmin_data_set, self.qmax_data_set |
---|
1874 | npts = self.npts_data_set |
---|
1875 | #set maximum range for x in linear scale |
---|
1876 | if not hasattr(self.data, "data"): #Display only for 1D data fit |
---|
1877 | self.btEditMask.Disable() |
---|
1878 | self.EditMask_title.Disable() |
---|
1879 | else: |
---|
1880 | self.btEditMask.Enable() |
---|
1881 | self.EditMask_title.Enable() |
---|
1882 | self.Npts_total.SetValue(str(npts)) |
---|
1883 | #default:number of data points selected to fit |
---|
1884 | self.Npts_fit.SetValue(str(npts)) |
---|
1885 | self.Npts_total.SetEditable(False) |
---|
1886 | self.Npts_total.SetBackgroundColour(\ |
---|
1887 | self.GetParent().GetBackgroundColour()) |
---|
1888 | |
---|
1889 | self.Npts_total.Bind(wx.EVT_MOUSE_EVENTS, self._npts_click) |
---|
1890 | self.dataSource.SetValue(data_name) |
---|
1891 | self.qmin_x = data_min |
---|
1892 | self.qmax_x = data_max |
---|
1893 | #self.minimum_q.SetValue(str(data_min)) |
---|
1894 | #self.maximum_q.SetValue(str(data_max)) |
---|
1895 | if data_min is None: |
---|
1896 | data_min = "" |
---|
1897 | if data_max is None: |
---|
1898 | data_max = "" |
---|
1899 | self.qmin.SetValue(str(data_min)) |
---|
1900 | self.qmax.SetValue(str(data_max)) |
---|
1901 | self.qmin.SetBackgroundColour("white") |
---|
1902 | self.qmax.SetBackgroundColour("white") |
---|
1903 | self.state.data = data |
---|
1904 | self.state.qmin = self.qmin_x |
---|
1905 | self.state.qmax = self.qmax_x |
---|
1906 | self.enable_fit_button() |
---|
1907 | #update model plot with new data information |
---|
1908 | if flag: |
---|
1909 | #set model view button |
---|
1910 | if self.data.__class__.__name__ == "Data2D": |
---|
1911 | self.enable2D = True |
---|
1912 | self.model_view.SetLabel("2D Mode") |
---|
1913 | else: |
---|
1914 | self.enable2D = False |
---|
1915 | self.model_view.SetLabel("1D Mode") |
---|
1916 | self.model_view.Disable() |
---|
1917 | #replace data plot on combo box selection |
---|
1918 | #by removing the previous selected data |
---|
1919 | wx.PostEvent(self._manager.parent, NewPlotEvent(action="remove", |
---|
1920 | group_id=group_id, id=id)) |
---|
1921 | #plot the current selected data |
---|
1922 | wx.PostEvent(self._manager.parent, NewPlotEvent(plot=self.data, |
---|
1923 | title=str(self.data.title))) |
---|
1924 | #self._manager.store_data(uid=self.uid, data_list=self.data_list, |
---|
1925 | # caption=self.window_name) |
---|
1926 | self._draw_model() |
---|
1927 | |
---|
1928 | def _npts_click(self, event): |
---|
1929 | """ |
---|
1930 | Prevent further handling of the mouse event on Npts_total |
---|
1931 | by not calling Skip(). |
---|
1932 | """ |
---|
1933 | pass |
---|
1934 | |
---|
1935 | def reset_page(self, state,first=False): |
---|
1936 | """ |
---|
1937 | reset the state |
---|
1938 | """ |
---|
1939 | self.reset_page_helper(state) |
---|
1940 | #import sans.guiframe.gui_manager |
---|
1941 | #evt = ModelEventbox(model=state.model) |
---|
1942 | #wx.PostEvent(self.event_owner, evt) |
---|
1943 | |
---|
1944 | if self.engine_type != None: |
---|
1945 | self._manager._on_change_engine(engine=self.engine_type) |
---|
1946 | |
---|
1947 | self.select_param(event = None) |
---|
1948 | #Save state_fit |
---|
1949 | self.save_current_state_fit() |
---|
1950 | self._lay_out() |
---|
1951 | self.Refresh() |
---|
1952 | |
---|
1953 | def get_range(self): |
---|
1954 | """ |
---|
1955 | return the fitting range |
---|
1956 | """ |
---|
1957 | return float(self.qmin_x) , float(self.qmax_x) |
---|
1958 | |
---|
1959 | def get_npts2fit(self): |
---|
1960 | """ |
---|
1961 | return numbers of data points within qrange |
---|
1962 | |
---|
1963 | :Note: This is for Park where chi2 is not normalized by Npts of fit |
---|
1964 | |
---|
1965 | """ |
---|
1966 | if self.data is None: |
---|
1967 | return |
---|
1968 | npts2fit = 0 |
---|
1969 | qmin,qmax = self.get_range() |
---|
1970 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
1971 | self.enable2D: |
---|
1972 | radius= numpy.sqrt( self.data.qx_data*self.data.qx_data + |
---|
1973 | self.data.qy_data*self.data.qy_data ) |
---|
1974 | index_data = (self.qmin_x <= radius)&(radius<= self.qmax_x) |
---|
1975 | index_data= (index_data)&(self.data.mask) |
---|
1976 | index_data = (index_data)&(numpy.isfinite(self.data.data)) |
---|
1977 | npts2fit = len(self.data.data[index_data]) |
---|
1978 | else: |
---|
1979 | for qx in self.data.x: |
---|
1980 | if qx >= qmin and qx <= qmax: |
---|
1981 | npts2fit += 1 |
---|
1982 | return npts2fit |
---|
1983 | |
---|
1984 | def set_npts2fit(self): |
---|
1985 | """ |
---|
1986 | setValue Npts for fitting |
---|
1987 | """ |
---|
1988 | self.Npts_fit.SetValue(str(self.get_npts2fit())) |
---|
1989 | |
---|
1990 | def get_chi2(self): |
---|
1991 | """ |
---|
1992 | return the current chi2 |
---|
1993 | """ |
---|
1994 | return self.tcChi.GetValue() |
---|
1995 | |
---|
1996 | def get_param_list(self): |
---|
1997 | """ |
---|
1998 | :return self.param_toFit: list containing references to TextCtrl |
---|
1999 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
2000 | |
---|
2001 | :raise: if return an empty list of parameter fit will nnote work |
---|
2002 | properly so raise ValueError,"missing parameter to fit" |
---|
2003 | """ |
---|
2004 | if self.param_toFit !=[]: |
---|
2005 | return self.param_toFit |
---|
2006 | else: |
---|
2007 | msg = "missing parameters to fit" |
---|
2008 | wx.MessageBox(msg, 'warning') |
---|
2009 | return False |
---|
2010 | #raise ValueError,"missing parameters to fit" |
---|
2011 | |
---|
2012 | def onsetValues(self, chisqr, p_name, out, cov): |
---|
2013 | """ |
---|
2014 | Build the panel from the fit result |
---|
2015 | |
---|
2016 | :param chisqr: Value of the goodness of fit metric |
---|
2017 | :param p_name: the name of parameters |
---|
2018 | :param out: list of parameter with the best value found during fitting |
---|
2019 | :param cov: Covariance matrix |
---|
2020 | |
---|
2021 | """ |
---|
2022 | if out == None or not numpy.isfinite(chisqr): |
---|
2023 | raise ValueError,"Fit error occured..." |
---|
2024 | |
---|
2025 | is_modified = False |
---|
2026 | has_error = False |
---|
2027 | dispersity = '' |
---|
2028 | |
---|
2029 | #Hide textctrl boxes of errors. |
---|
2030 | self._clear_Err_on_Fit() |
---|
2031 | |
---|
2032 | #Check if chi2 is finite |
---|
2033 | if chisqr != None and numpy.isfinite(chisqr): |
---|
2034 | #format chi2 |
---|
2035 | if self.engine_type == "park": |
---|
2036 | npt_fit = float(self.get_npts2fit()) |
---|
2037 | if npt_fit > 0: |
---|
2038 | chisqr =chisqr/npt_fit |
---|
2039 | chi2 = format_number(chisqr, True) |
---|
2040 | self.tcChi.SetValue(chi2) |
---|
2041 | self.tcChi.Refresh() |
---|
2042 | else: |
---|
2043 | self.tcChi.SetValue("-") |
---|
2044 | |
---|
2045 | #Hide error title |
---|
2046 | if self.text2_3.IsShown() and not self.is_mac: |
---|
2047 | self.text2_3.Hide() |
---|
2048 | |
---|
2049 | try: |
---|
2050 | if self.enable_disp.GetValue(): |
---|
2051 | if hasattr(self,"text_disp_1" ): |
---|
2052 | if self.text_disp_1 != None and not self.is_mac: |
---|
2053 | self.text_disp_1.Hide() |
---|
2054 | except: |
---|
2055 | dispersity = None |
---|
2056 | pass |
---|
2057 | #set the panel when fit result are float not list |
---|
2058 | if out.__class__== numpy.float64: |
---|
2059 | self.param_toFit[0][2].SetValue(format_number(out, True)) |
---|
2060 | |
---|
2061 | if self.param_toFit[0][4].IsShown and not self.is_mac: |
---|
2062 | self.param_toFit[0][4].Hide() |
---|
2063 | if cov !=None : |
---|
2064 | self.text2_3.Show(True) |
---|
2065 | try: |
---|
2066 | if self.enable_disp.GetValue(): |
---|
2067 | if hasattr(self,"text_disp_1" ): |
---|
2068 | if self.text_disp_1 !=None: |
---|
2069 | self.text_disp_1.Show(True) |
---|
2070 | except: |
---|
2071 | pass |
---|
2072 | |
---|
2073 | if cov[0]==None or not numpy.isfinite(cov[0]): |
---|
2074 | if self.param_toFit[0][3].IsShown and not self.is_mac: |
---|
2075 | self.param_toFit[0][3].Hide() |
---|
2076 | else: |
---|
2077 | self.param_toFit[0][3].Show(True) |
---|
2078 | self.param_toFit[0][4].Show(True) |
---|
2079 | self.param_toFit[0][4].SetValue(format_number(cov[0], True)) |
---|
2080 | has_error = True |
---|
2081 | else: |
---|
2082 | |
---|
2083 | i = 0 |
---|
2084 | #Set the panel when fit result are list |
---|
2085 | for item in self.param_toFit: |
---|
2086 | if len(item)>5 and item != None: |
---|
2087 | ## reset error value to initial state |
---|
2088 | if not self.is_mac: |
---|
2089 | item[3].Hide() |
---|
2090 | item[4].Hide() |
---|
2091 | |
---|
2092 | for ind in range(len(out)): |
---|
2093 | |
---|
2094 | if item[1] == p_name[ind]: |
---|
2095 | break |
---|
2096 | if len(out)<=len(self.param_toFit) and out[ind] !=None: |
---|
2097 | val_out = format_number(out[ind], True) |
---|
2098 | item[2].SetValue(val_out) |
---|
2099 | |
---|
2100 | |
---|
2101 | if(cov !=None): |
---|
2102 | |
---|
2103 | try: |
---|
2104 | if dispersity !=None: |
---|
2105 | if self.enable_disp.GetValue(): |
---|
2106 | if hasattr(self,"text_disp_1" ): |
---|
2107 | if self.text_disp_1!=None: |
---|
2108 | if not self.text_disp_1.IsShown()\ |
---|
2109 | and not self.is_mac: |
---|
2110 | self.text_disp_1.Show(True) |
---|
2111 | except: |
---|
2112 | pass |
---|
2113 | |
---|
2114 | if cov[ind]!=None : |
---|
2115 | if numpy.isfinite(float(cov[ind])): |
---|
2116 | val_err = format_number(cov[ind], True) |
---|
2117 | if not self.is_mac: |
---|
2118 | item[3].Show(True) |
---|
2119 | item[4].Show(True) |
---|
2120 | item[4].SetValue(val_err) |
---|
2121 | |
---|
2122 | has_error = True |
---|
2123 | i += 1 |
---|
2124 | #Show error title when any errors displayed |
---|
2125 | if has_error: |
---|
2126 | if not self.text2_3.IsShown(): |
---|
2127 | self.text2_3.Show(True) |
---|
2128 | ## save current state |
---|
2129 | self.save_current_state() |
---|
2130 | |
---|
2131 | #self._lay_out() |
---|
2132 | if not self.is_mac: |
---|
2133 | self.Layout() |
---|
2134 | self.Refresh() |
---|
2135 | self._mac_sleep(0.1) |
---|
2136 | #plot model ( when drawing, do not update chisqr value again) |
---|
2137 | self._draw_model(update_chisqr=False) |
---|
2138 | #PostStatusEvent |
---|
2139 | #msg = "Fit completed!dddd " |
---|
2140 | #wx.PostEvent(self._manager.parent, StatusEvent(status=msg)) |
---|
2141 | |
---|
2142 | def onWeighting(self, event): |
---|
2143 | """ |
---|
2144 | On Weighting radio button event, sets the weightbt_string |
---|
2145 | """ |
---|
2146 | self.weightbt_string = event.GetEventObject().GetLabelText() |
---|
2147 | |
---|
2148 | def onPinholeSmear(self, event): |
---|
2149 | """ |
---|
2150 | Create a custom pinhole smear object that will change the way residuals |
---|
2151 | are compute when fitting |
---|
2152 | |
---|
2153 | :Note: accuracy is given by strings'High','Med', 'Low' FOR 2d, |
---|
2154 | None for 1D |
---|
2155 | |
---|
2156 | """ |
---|
2157 | |
---|
2158 | #if self.check_invalid_panel(): |
---|
2159 | # return |
---|
2160 | if self.model ==None: |
---|
2161 | self.disable_smearer.SetValue(True) |
---|
2162 | msg="Please select a Model first..." |
---|
2163 | wx.MessageBox(msg, 'Info') |
---|
2164 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2165 | "Smear: %s"%msg)) |
---|
2166 | return |
---|
2167 | |
---|
2168 | # Need update param values |
---|
2169 | self._update_paramv_on_fit() |
---|
2170 | |
---|
2171 | # msg default |
---|
2172 | msg = None |
---|
2173 | if event != None: |
---|
2174 | tcrtl= event.GetEventObject() |
---|
2175 | # event case of radio button |
---|
2176 | if tcrtl.GetValue()== True: |
---|
2177 | self.dx_min = 0.0 |
---|
2178 | self.dx_max = 0.0 |
---|
2179 | is_new_pinhole = True |
---|
2180 | else: |
---|
2181 | is_new_pinhole = self._is_changed_pinhole() |
---|
2182 | else: |
---|
2183 | is_new_pinhole = True |
---|
2184 | # if any value is changed |
---|
2185 | if is_new_pinhole: |
---|
2186 | msg = self._set_pinhole_smear() |
---|
2187 | # hide all silt sizer |
---|
2188 | self._hide_all_smear_info() |
---|
2189 | |
---|
2190 | ##Calculate chi2 |
---|
2191 | temp_smearer = self.current_smearer |
---|
2192 | #self.compute_chisqr(smearer= temp_smearer) |
---|
2193 | |
---|
2194 | # show relevant slit sizers |
---|
2195 | self._show_smear_sizer() |
---|
2196 | |
---|
2197 | self.sizer_set_smearer.Layout() |
---|
2198 | self.Layout() |
---|
2199 | |
---|
2200 | if event != None: |
---|
2201 | event.Skip() |
---|
2202 | #self._undo.Enable(True) |
---|
2203 | self.save_current_state() |
---|
2204 | event = PageInfoEvent(page = self) |
---|
2205 | wx.PostEvent(self.parent, event) |
---|
2206 | |
---|
2207 | def _is_changed_pinhole(self): |
---|
2208 | """ |
---|
2209 | check if any of pinhole smear is changed |
---|
2210 | |
---|
2211 | :return: True or False |
---|
2212 | |
---|
2213 | """ |
---|
2214 | # get the values |
---|
2215 | pin_min = self.smear_pinhole_min.GetValue() |
---|
2216 | pin_max = self.smear_pinhole_max.GetValue() |
---|
2217 | |
---|
2218 | # Check changes in slit width |
---|
2219 | try: |
---|
2220 | dx_min = float(pin_min) |
---|
2221 | except: |
---|
2222 | return True |
---|
2223 | if self.dx_min != dx_min: |
---|
2224 | return True |
---|
2225 | |
---|
2226 | # Check changes in slit heigth |
---|
2227 | try: |
---|
2228 | dx_max = float(pin_max) |
---|
2229 | except: |
---|
2230 | return True |
---|
2231 | if self.dx_max != dx_max: |
---|
2232 | return True |
---|
2233 | return False |
---|
2234 | |
---|
2235 | def _set_pinhole_smear(self): |
---|
2236 | """ |
---|
2237 | Set custom pinhole smear |
---|
2238 | |
---|
2239 | :return: msg |
---|
2240 | |
---|
2241 | """ |
---|
2242 | # copy data |
---|
2243 | data = copy.deepcopy(self.data) |
---|
2244 | if self._is_2D(): |
---|
2245 | self.smear_type = 'Pinhole2d' |
---|
2246 | len_data = len(data.data) |
---|
2247 | data.dqx_data = numpy.zeros(len_data) |
---|
2248 | data.dqy_data = numpy.zeros(len_data) |
---|
2249 | else: |
---|
2250 | self.smear_type = 'Pinhole' |
---|
2251 | len_data = len(data.x) |
---|
2252 | data.dx = numpy.zeros(len_data) |
---|
2253 | data.dxl = None |
---|
2254 | data.dxw = None |
---|
2255 | msg = None |
---|
2256 | |
---|
2257 | get_pin_min = self.smear_pinhole_min |
---|
2258 | get_pin_max = self.smear_pinhole_max |
---|
2259 | |
---|
2260 | if not check_float(get_pin_min): |
---|
2261 | get_pin_min.SetBackgroundColour("pink") |
---|
2262 | msg= "Model Error:wrong value entered!!!" |
---|
2263 | elif not check_float(get_pin_max ): |
---|
2264 | get_pin_max.SetBackgroundColour("pink") |
---|
2265 | msg= "Model Error:wrong value entered!!!" |
---|
2266 | else: |
---|
2267 | if len_data < 2: len_data = 2 |
---|
2268 | self.dx_min = float(get_pin_min.GetValue()) |
---|
2269 | self.dx_max = float(get_pin_max.GetValue()) |
---|
2270 | if self.dx_min < 0: |
---|
2271 | get_pin_min.SetBackgroundColour("pink") |
---|
2272 | msg= "Model Error:This value can not be negative!!!" |
---|
2273 | elif self.dx_max <0: |
---|
2274 | get_pin_max.SetBackgroundColour("pink") |
---|
2275 | msg= "Model Error:This value can not be negative!!!" |
---|
2276 | elif self.dx_min != None and self.dx_max != None: |
---|
2277 | if self._is_2D(): |
---|
2278 | data.dqx_data[data.dqx_data==0] = self.dx_min |
---|
2279 | data.dqy_data[data.dqy_data==0] = self.dx_max |
---|
2280 | elif self.dx_min == self.dx_max: |
---|
2281 | data.dx[data.dx==0] = self.dx_min |
---|
2282 | else: |
---|
2283 | step = (self.dx_max - self.dx_min)/(len_data-1) |
---|
2284 | data.dx = numpy.arange(self.dx_min,self.dx_max+step/1.1, |
---|
2285 | step) |
---|
2286 | elif self.dx_min != None: |
---|
2287 | if self._is_2D(): data.dqx_data[data.dqx_data==0] = self.dx_min |
---|
2288 | else: data.dx[data.dx==0] = self.dx_min |
---|
2289 | elif self.dx_max != None: |
---|
2290 | if self._is_2D(): data.dqy_data[data.dqy_data==0] = self.dx_max |
---|
2291 | else: data.dx[data.dx==0] = self.dx_max |
---|
2292 | self.current_smearer = smear_selection(data, self.model) |
---|
2293 | # 2D need to set accuracy |
---|
2294 | if self._is_2D(): |
---|
2295 | self.current_smearer.set_accuracy(accuracy = \ |
---|
2296 | self.smear2d_accuracy) |
---|
2297 | |
---|
2298 | if msg != None: |
---|
2299 | wx.PostEvent(self._manager.parent, StatusEvent(status = msg )) |
---|
2300 | else: |
---|
2301 | get_pin_min.SetBackgroundColour("white") |
---|
2302 | get_pin_max.SetBackgroundColour("white") |
---|
2303 | ## set smearing value whether or not the data contain the smearing info |
---|
2304 | |
---|
2305 | self._manager.set_smearer(smearer=self.current_smearer, |
---|
2306 | fid=self.data.id, |
---|
2307 | qmin=float(self.qmin_x), |
---|
2308 | qmax= float(self.qmax_x), |
---|
2309 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
2310 | uid=self.uid) |
---|
2311 | return msg |
---|
2312 | |
---|
2313 | def update_pinhole_smear(self): |
---|
2314 | """ |
---|
2315 | called by kill_focus on pinhole TextCntrl |
---|
2316 | to update the changes |
---|
2317 | |
---|
2318 | :return: False when wrong value was entered |
---|
2319 | |
---|
2320 | """ |
---|
2321 | # msg default |
---|
2322 | msg = None |
---|
2323 | # check if any value is changed |
---|
2324 | if self._is_changed_pinhole(): |
---|
2325 | msg = self._set_pinhole_smear() |
---|
2326 | #self._undo.Enable(True) |
---|
2327 | self.save_current_state() |
---|
2328 | |
---|
2329 | if msg != None: |
---|
2330 | return False |
---|
2331 | else: |
---|
2332 | return True |
---|
2333 | |
---|
2334 | def onSlitSmear(self, event): |
---|
2335 | """ |
---|
2336 | Create a custom slit smear object that will change the way residuals |
---|
2337 | are compute when fitting |
---|
2338 | """ |
---|
2339 | |
---|
2340 | #if self.check_invalid_panel(): |
---|
2341 | # return |
---|
2342 | |
---|
2343 | if self.model ==None: |
---|
2344 | self.disable_smearer.SetValue(True) |
---|
2345 | |
---|
2346 | msg="Please select a Model first..." |
---|
2347 | wx.MessageBox(msg, 'Info') |
---|
2348 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2349 | "Smear: %s"%msg)) |
---|
2350 | return |
---|
2351 | |
---|
2352 | # Need update param values |
---|
2353 | self._update_paramv_on_fit() |
---|
2354 | |
---|
2355 | # msg default |
---|
2356 | msg = None |
---|
2357 | # for event given |
---|
2358 | if event != None: |
---|
2359 | tcrtl= event.GetEventObject() |
---|
2360 | # event case of radio button |
---|
2361 | if tcrtl.GetValue(): |
---|
2362 | self.dxl = 0.0 |
---|
2363 | self.dxw = 0.0 |
---|
2364 | is_new_slit = True |
---|
2365 | else: |
---|
2366 | is_new_slit = self._is_changed_slit() |
---|
2367 | else: |
---|
2368 | is_new_slit = True |
---|
2369 | |
---|
2370 | # if any value is changed |
---|
2371 | if is_new_slit: |
---|
2372 | msg = self._set_slit_smear() |
---|
2373 | |
---|
2374 | # hide all silt sizer |
---|
2375 | self._hide_all_smear_info() |
---|
2376 | ##Calculate chi2 |
---|
2377 | #self.compute_chisqr(smearer= self.current_smearer) |
---|
2378 | # show relevant slit sizers |
---|
2379 | self._show_smear_sizer() |
---|
2380 | self.sizer_set_smearer.Layout() |
---|
2381 | self.Layout() |
---|
2382 | |
---|
2383 | if event != None: |
---|
2384 | event.Skip() |
---|
2385 | #self._undo.Enable(True) |
---|
2386 | self.save_current_state() |
---|
2387 | event = PageInfoEvent(page = self) |
---|
2388 | wx.PostEvent(self.parent, event) |
---|
2389 | if msg != None: |
---|
2390 | wx.PostEvent(self._manager.parent, StatusEvent(status = msg)) |
---|
2391 | |
---|
2392 | def _is_changed_slit(self): |
---|
2393 | """ |
---|
2394 | check if any of slit lengths is changed |
---|
2395 | |
---|
2396 | :return: True or False |
---|
2397 | |
---|
2398 | """ |
---|
2399 | # get the values |
---|
2400 | width = self.smear_slit_width.GetValue() |
---|
2401 | height = self.smear_slit_height.GetValue() |
---|
2402 | |
---|
2403 | # check and change the box bg color if it was pink |
---|
2404 | # but it should be white now |
---|
2405 | # because this is the case that _set_slit_smear() will not handle |
---|
2406 | if height.lstrip().rstrip()=="": |
---|
2407 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2408 | if width.lstrip().rstrip()=="": |
---|
2409 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2410 | |
---|
2411 | # Check changes in slit width |
---|
2412 | if width == "": |
---|
2413 | dxw = 0.0 |
---|
2414 | else: |
---|
2415 | try: |
---|
2416 | dxw = float(width) |
---|
2417 | except: |
---|
2418 | return True |
---|
2419 | if self.dxw != dxw: |
---|
2420 | return True |
---|
2421 | |
---|
2422 | # Check changes in slit heigth |
---|
2423 | if height == "": |
---|
2424 | dxl = 0.0 |
---|
2425 | else: |
---|
2426 | try: |
---|
2427 | dxl = float(height) |
---|
2428 | except: |
---|
2429 | return True |
---|
2430 | if self.dxl != dxl: |
---|
2431 | return True |
---|
2432 | |
---|
2433 | return False |
---|
2434 | |
---|
2435 | def _set_slit_smear(self): |
---|
2436 | """ |
---|
2437 | Set custom slit smear |
---|
2438 | |
---|
2439 | :return: message to inform the user about the validity |
---|
2440 | of the values entered for slit smear |
---|
2441 | """ |
---|
2442 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
2443 | self.enable2D: |
---|
2444 | return |
---|
2445 | temp_smearer = None |
---|
2446 | # make sure once more if it is smearer |
---|
2447 | data = copy.deepcopy(self.data) |
---|
2448 | data_len = len(data.x) |
---|
2449 | data.dx = None |
---|
2450 | data.dxl = None |
---|
2451 | data.dxw = None |
---|
2452 | msg = None |
---|
2453 | |
---|
2454 | try: |
---|
2455 | self.dxl = float(self.smear_slit_height.GetValue()) |
---|
2456 | data.dxl = self.dxl* numpy.ones(data_len) |
---|
2457 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2458 | except: |
---|
2459 | data.dxl = numpy.zeros(data_len) |
---|
2460 | if self.smear_slit_height.GetValue().lstrip().rstrip()!="": |
---|
2461 | self.smear_slit_height.SetBackgroundColour("pink") |
---|
2462 | msg = "Wrong value entered... " |
---|
2463 | else: |
---|
2464 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
2465 | try: |
---|
2466 | self.dxw = float(self.smear_slit_width.GetValue()) |
---|
2467 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2468 | data.dxw = self.dxw* numpy.ones(data_len) |
---|
2469 | except: |
---|
2470 | data.dxw = numpy.zeros(data_len) |
---|
2471 | if self.smear_slit_width.GetValue().lstrip().rstrip()!="": |
---|
2472 | self.smear_slit_width.SetBackgroundColour("pink") |
---|
2473 | msg = "Wrong Fit value entered... " |
---|
2474 | else: |
---|
2475 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
2476 | |
---|
2477 | self.current_smearer = smear_selection(data, self.model) |
---|
2478 | #temp_smearer = self.current_smearer |
---|
2479 | ## set smearing value whether or not the data contain the smearing info |
---|
2480 | self._manager.set_smearer(smearer=self.current_smearer, |
---|
2481 | fid=self.data.id, |
---|
2482 | qmin=float(self.qmin_x), |
---|
2483 | qmax= float(self.qmax_x), |
---|
2484 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
2485 | uid=self.uid) |
---|
2486 | return msg |
---|
2487 | |
---|
2488 | def update_slit_smear(self): |
---|
2489 | """ |
---|
2490 | called by kill_focus on pinhole TextCntrl |
---|
2491 | to update the changes |
---|
2492 | |
---|
2493 | :return: False when wrong value was entered |
---|
2494 | |
---|
2495 | """ |
---|
2496 | # msg default |
---|
2497 | msg = None |
---|
2498 | # check if any value is changed |
---|
2499 | if self._is_changed_slit(): |
---|
2500 | msg = self._set_slit_smear() |
---|
2501 | #self._undo.Enable(True) |
---|
2502 | self.save_current_state() |
---|
2503 | |
---|
2504 | if msg != None: |
---|
2505 | return False |
---|
2506 | else: |
---|
2507 | return True |
---|
2508 | |
---|
2509 | def onSmear(self, event): |
---|
2510 | """ |
---|
2511 | Create a smear object that will change the way residuals |
---|
2512 | are compute when fitting |
---|
2513 | """ |
---|
2514 | if event != None: |
---|
2515 | event.Skip() |
---|
2516 | if self.data is None: |
---|
2517 | return |
---|
2518 | |
---|
2519 | if self.model == None: |
---|
2520 | self.disable_smearer.SetValue(True) |
---|
2521 | msg="Please select a Model first..." |
---|
2522 | wx.MessageBox(msg, 'Info') |
---|
2523 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2524 | "Smear: %s"%msg)) |
---|
2525 | return |
---|
2526 | |
---|
2527 | # Need update param values |
---|
2528 | self._update_paramv_on_fit() |
---|
2529 | |
---|
2530 | temp_smearer = None |
---|
2531 | self._get_smear_info() |
---|
2532 | |
---|
2533 | #renew smear sizer |
---|
2534 | if self.smear_type != None: |
---|
2535 | self.smear_description_smear_type.SetValue(str(self.smear_type)) |
---|
2536 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
2537 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
2538 | |
---|
2539 | self._hide_all_smear_info() |
---|
2540 | |
---|
2541 | data = copy.deepcopy(self.data) |
---|
2542 | # make sure once more if it is smearer |
---|
2543 | self.current_smearer = smear_selection(data, self.model) |
---|
2544 | |
---|
2545 | if self.enable_smearer.GetValue(): |
---|
2546 | if hasattr(self.data,"dxl"): |
---|
2547 | |
---|
2548 | msg= ": Resolution smearing parameters" |
---|
2549 | if hasattr(self.data,"dxw"): |
---|
2550 | msg= ": Slit smearing parameters" |
---|
2551 | if self.current_smearer ==None: |
---|
2552 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2553 | "Data contains no smearing information")) |
---|
2554 | else: |
---|
2555 | wx.PostEvent(self._manager.parent, StatusEvent(status=\ |
---|
2556 | "Data contains smearing information")) |
---|
2557 | |
---|
2558 | #self.smear_description_dqdata.Show(True) |
---|
2559 | self.smear_data_left.Show(True) |
---|
2560 | self.smear_data_right.Show(True) |
---|
2561 | temp_smearer= self.current_smearer |
---|
2562 | elif self.disable_smearer.GetValue(): |
---|
2563 | self.smear_description_none.Show(True) |
---|
2564 | |
---|
2565 | self._show_smear_sizer() |
---|
2566 | |
---|
2567 | self.sizer_set_smearer.Layout() |
---|
2568 | self.Layout() |
---|
2569 | ## set smearing value whether or not the data contain the smearing info |
---|
2570 | self._manager.set_smearer(uid=self.uid, smearer=temp_smearer, |
---|
2571 | fid=self.data.id, |
---|
2572 | qmin=float(self.qmin_x), |
---|
2573 | qmax=float(self.qmax_x), |
---|
2574 | enable_smearer=not self.disable_smearer.GetValue(), |
---|
2575 | draw=True) |
---|
2576 | |
---|
2577 | self.state.enable_smearer= self.enable_smearer.GetValue() |
---|
2578 | self.state.disable_smearer=self.disable_smearer.GetValue() |
---|
2579 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
2580 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
2581 | |
---|
2582 | def on_complete_chisqr(self, event): |
---|
2583 | """ |
---|
2584 | Display result chisqr on the panel |
---|
2585 | :event: activated by fitting/ complete after draw |
---|
2586 | """ |
---|
2587 | try: |
---|
2588 | if event == None: |
---|
2589 | output= "-" |
---|
2590 | elif not numpy.isfinite(event.output): |
---|
2591 | output= "-" |
---|
2592 | else: |
---|
2593 | output = event.output |
---|
2594 | self.tcChi.SetValue(str(format_number(output, True))) |
---|
2595 | self.state.tcChi = self.tcChi.GetValue() |
---|
2596 | except: |
---|
2597 | pass |
---|
2598 | |
---|
2599 | |
---|
2600 | def select_all_param(self,event): |
---|
2601 | """ |
---|
2602 | set to true or false all checkBox given the main checkbox value cb1 |
---|
2603 | """ |
---|
2604 | self.param_toFit=[] |
---|
2605 | if self.parameters !=[]: |
---|
2606 | if self.cb1.GetValue(): |
---|
2607 | for item in self.parameters: |
---|
2608 | ## for data2D select all to fit |
---|
2609 | if self.data.__class__.__name__== "Data2D" or \ |
---|
2610 | self.enable2D: |
---|
2611 | item[0].SetValue(True) |
---|
2612 | self.param_toFit.append(item ) |
---|
2613 | else: |
---|
2614 | ## for 1D all parameters except orientation |
---|
2615 | if not item in self.orientation_params: |
---|
2616 | item[0].SetValue(True) |
---|
2617 | self.param_toFit.append(item ) |
---|
2618 | #if len(self.fittable_param)>0: |
---|
2619 | for item in self.fittable_param: |
---|
2620 | if self.data.__class__.__name__== "Data2D" or \ |
---|
2621 | self.enable2D: |
---|
2622 | item[0].SetValue(True) |
---|
2623 | self.param_toFit.append(item ) |
---|
2624 | try: |
---|
2625 | if len(self.values[item[1]]) > 0: |
---|
2626 | item[0].SetValue(False) |
---|
2627 | except: |
---|
2628 | pass |
---|
2629 | |
---|
2630 | else: |
---|
2631 | ## for 1D all parameters except orientation |
---|
2632 | if not item in self.orientation_params_disp: |
---|
2633 | item[0].SetValue(True) |
---|
2634 | self.param_toFit.append(item ) |
---|
2635 | try: |
---|
2636 | if len(self.values[item[1]]) > 0: |
---|
2637 | item[0].SetValue(False) |
---|
2638 | except: |
---|
2639 | pass |
---|
2640 | |
---|
2641 | else: |
---|
2642 | for item in self.parameters: |
---|
2643 | item[0].SetValue(False) |
---|
2644 | for item in self.fittable_param: |
---|
2645 | item[0].SetValue(False) |
---|
2646 | self.param_toFit=[] |
---|
2647 | |
---|
2648 | self.save_current_state_fit() |
---|
2649 | |
---|
2650 | if event !=None: |
---|
2651 | #self._undo.Enable(True) |
---|
2652 | ## post state to fit panel |
---|
2653 | event = PageInfoEvent(page = self) |
---|
2654 | wx.PostEvent(self.parent, event) |
---|
2655 | param2fit = [] |
---|
2656 | for item in self.param_toFit: |
---|
2657 | if item[0]: |
---|
2658 | param2fit.append(item[1]) |
---|
2659 | self.parent._manager.set_param2fit(self.uid, param2fit) |
---|
2660 | |
---|
2661 | |
---|
2662 | def select_param(self,event): |
---|
2663 | """ |
---|
2664 | Select TextCtrl checked for fitting purpose and stores them |
---|
2665 | in self.param_toFit=[] list |
---|
2666 | """ |
---|
2667 | self.param_toFit=[] |
---|
2668 | for item in self.parameters: |
---|
2669 | #Skip t ifhe angle parameters if 1D data |
---|
2670 | if self.data.__class__.__name__ != "Data2D" and\ |
---|
2671 | not self.enable2D: |
---|
2672 | if item in self.orientation_params: |
---|
2673 | continue |
---|
2674 | #Select parameters to fit for list of primary parameters |
---|
2675 | if item[0].GetValue(): |
---|
2676 | if not (item in self.param_toFit): |
---|
2677 | self.param_toFit.append(item ) |
---|
2678 | else: |
---|
2679 | #remove parameters from the fitting list |
---|
2680 | if item in self.param_toFit: |
---|
2681 | self.param_toFit.remove(item) |
---|
2682 | |
---|
2683 | #Select parameters to fit for list of fittable parameters |
---|
2684 | # with dispersion |
---|
2685 | for item in self.fittable_param: |
---|
2686 | #Skip t ifhe angle parameters if 1D data |
---|
2687 | if self.data.__class__.__name__ != "Data2D" and\ |
---|
2688 | not self.enable2D: |
---|
2689 | if item in self.orientation_params: |
---|
2690 | continue |
---|
2691 | if item[0].GetValue(): |
---|
2692 | if not (item in self.param_toFit): |
---|
2693 | self.param_toFit.append(item) |
---|
2694 | else: |
---|
2695 | #remove parameters from the fitting list |
---|
2696 | if item in self.param_toFit: |
---|
2697 | self.param_toFit.remove(item) |
---|
2698 | |
---|
2699 | #Calculate num. of angle parameters |
---|
2700 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
2701 | self.enable2D: |
---|
2702 | len_orient_para = 0 |
---|
2703 | else: |
---|
2704 | len_orient_para = len(self.orientation_params) #assume even len |
---|
2705 | #Total num. of angle parameters |
---|
2706 | if len(self.fittable_param) > 0: |
---|
2707 | len_orient_para *= 2 |
---|
2708 | #Set the value of checkbox that selected every checkbox or not |
---|
2709 | if len(self.parameters)+len(self.fittable_param)-len_orient_para ==\ |
---|
2710 | len(self.param_toFit): |
---|
2711 | self.cb1.SetValue(True) |
---|
2712 | else: |
---|
2713 | self.cb1.SetValue(False) |
---|
2714 | self.save_current_state_fit() |
---|
2715 | if event !=None: |
---|
2716 | #self._undo.Enable(True) |
---|
2717 | ## post state to fit panel |
---|
2718 | event = PageInfoEvent(page = self) |
---|
2719 | wx.PostEvent(self.parent, event) |
---|
2720 | |
---|
2721 | param2fit = [] |
---|
2722 | for item in self.param_toFit: |
---|
2723 | if item[0]: |
---|
2724 | param2fit.append(item[1]) |
---|
2725 | self.parent._manager.set_param2fit(self.uid, param2fit) |
---|
2726 | |
---|
2727 | def set_model_param_sizer(self, model): |
---|
2728 | """ |
---|
2729 | Build the panel from the model content |
---|
2730 | |
---|
2731 | :param model: the model selected in combo box for fitting purpose |
---|
2732 | |
---|
2733 | """ |
---|
2734 | self.sizer3.Clear(True) |
---|
2735 | self.parameters = [] |
---|
2736 | self.str_parameters = [] |
---|
2737 | self.param_toFit=[] |
---|
2738 | self.fittable_param=[] |
---|
2739 | self.fixed_param=[] |
---|
2740 | self.orientation_params=[] |
---|
2741 | self.orientation_params_disp=[] |
---|
2742 | |
---|
2743 | if model ==None: |
---|
2744 | self.sizer3.Layout() |
---|
2745 | self.SetupScrolling() |
---|
2746 | return |
---|
2747 | ## the panel is drawn using the current value of the fit engine |
---|
2748 | if self.engine_type==None and self._manager !=None: |
---|
2749 | self.engine_type= self._manager._return_engine_type() |
---|
2750 | |
---|
2751 | box_description= wx.StaticBox(self, -1,str("Model Parameters")) |
---|
2752 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
2753 | sizer = wx.GridBagSizer(5,5) |
---|
2754 | ## save the current model |
---|
2755 | self.model = model |
---|
2756 | |
---|
2757 | keys = self.model.getParamList() |
---|
2758 | #list of dispersion paramaters |
---|
2759 | self.disp_list=self.model.getDispParamList() |
---|
2760 | |
---|
2761 | def custom_compare(a,b): |
---|
2762 | """ |
---|
2763 | Custom compare to order, first by alphabets then second by number. |
---|
2764 | """ |
---|
2765 | # number at the last digit |
---|
2766 | a_last = a[len(a)-1] |
---|
2767 | b_last = b[len(b)-1] |
---|
2768 | # default |
---|
2769 | num_a = None |
---|
2770 | num_b = None |
---|
2771 | # split the names |
---|
2772 | a2 = a.lower().split('_') |
---|
2773 | b2 = b.lower().split('_') |
---|
2774 | # check length of a2, b2 |
---|
2775 | len_a2 = len(a2) |
---|
2776 | len_b2 = len(b2) |
---|
2777 | # check if it contains a int number(<10) |
---|
2778 | try: |
---|
2779 | num_a = int(a_last) |
---|
2780 | except: pass |
---|
2781 | try: |
---|
2782 | num_b = int(b_last) |
---|
2783 | except: pass |
---|
2784 | # Put 'scale' near the top; happens |
---|
2785 | # when numbered param name exists |
---|
2786 | if a == 'scale': |
---|
2787 | return -1 |
---|
2788 | # both have a number |
---|
2789 | if num_a != None and num_b != None: |
---|
2790 | if num_a > num_b: return -1 |
---|
2791 | # same number |
---|
2792 | elif num_a == num_b: |
---|
2793 | # different last names |
---|
2794 | if a2[len_a2-1] != b2[len_b2-1] and num_a != 0: |
---|
2795 | return -cmp(a2[len_a2-1], b2[len_b2-1]) |
---|
2796 | else: |
---|
2797 | return cmp(a, b) |
---|
2798 | else: return 1 |
---|
2799 | # one of them has a number |
---|
2800 | elif num_a != None: return 1 |
---|
2801 | elif num_b != None: return -1 |
---|
2802 | # no numbers |
---|
2803 | else: return cmp(a.lower(), b.lower()) |
---|
2804 | |
---|
2805 | |
---|
2806 | keys.sort(custom_compare) |
---|
2807 | |
---|
2808 | iy = 0 |
---|
2809 | ix = 0 |
---|
2810 | select_text = "Select All" |
---|
2811 | self.cb1 = wx.CheckBox(self, -1,str(select_text), (10, 10)) |
---|
2812 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
2813 | self.cb1.SetToolTipString("To check/uncheck all the boxes below.") |
---|
2814 | self.cb1.SetValue(True) |
---|
2815 | |
---|
2816 | sizer.Add(self.cb1,(iy, ix),(1,1),\ |
---|
2817 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2818 | ix +=1 |
---|
2819 | self.text2_2 = wx.StaticText(self, -1, 'Value') |
---|
2820 | sizer.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
2821 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2822 | ix +=2 |
---|
2823 | self.text2_3 = wx.StaticText(self, -1, 'Error') |
---|
2824 | sizer.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
2825 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2826 | if not self.is_mac: |
---|
2827 | self.text2_3.Hide() |
---|
2828 | ix +=1 |
---|
2829 | self.text2_min = wx.StaticText(self, -1, 'Min') |
---|
2830 | sizer.Add(self.text2_min,(iy, ix),(1,1),\ |
---|
2831 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2832 | #self.text2_min.Hide() |
---|
2833 | ix +=1 |
---|
2834 | self.text2_max = wx.StaticText(self, -1, 'Max') |
---|
2835 | sizer.Add(self.text2_max,(iy, ix),(1,1),\ |
---|
2836 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2837 | #self.text2_max.Hide() |
---|
2838 | ix += 1 |
---|
2839 | self.text2_4 = wx.StaticText(self, -1, '[Units]') |
---|
2840 | sizer.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
2841 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2842 | self.text2_4.Hide() |
---|
2843 | |
---|
2844 | for item in keys: |
---|
2845 | if not item in self.disp_list and not item in \ |
---|
2846 | self.model.orientation_params: |
---|
2847 | |
---|
2848 | ##prepare a spot to store errors |
---|
2849 | if not self.model.details.has_key(item): |
---|
2850 | self.model.details [item] = ["",None,None] |
---|
2851 | |
---|
2852 | iy += 1 |
---|
2853 | ix = 0 |
---|
2854 | if (self.model.__class__ in \ |
---|
2855 | self.model_list_box["Multi-Functions"] or \ |
---|
2856 | self.temp_multi_functional)\ |
---|
2857 | and (item in self.model.non_fittable): |
---|
2858 | non_fittable_name = wx.StaticText(self, -1, item ) |
---|
2859 | sizer.Add(non_fittable_name,(iy, ix),(1,1),\ |
---|
2860 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 21) |
---|
2861 | ## add parameter value |
---|
2862 | ix += 1 |
---|
2863 | value= self.model.getParam(item) |
---|
2864 | if len(self.model.fun_list) > 0: |
---|
2865 | num = item.split('_')[1][5:7] |
---|
2866 | fun_box = wx.ComboBox(self, -1,size=(100,-1), |
---|
2867 | style=wx.CB_READONLY, name = '%s'% item) |
---|
2868 | self._set_fun_box_list(fun_box) |
---|
2869 | fun_box.SetSelection(0) |
---|
2870 | #self.fun_box.SetToolTipString("A function |
---|
2871 | # describing the interface") |
---|
2872 | wx.EVT_COMBOBOX(fun_box,-1, self._on_fun_box) |
---|
2873 | else: |
---|
2874 | fun_box = self.ModelTextCtrl(self, -1, |
---|
2875 | size=(_BOX_WIDTH,20), |
---|
2876 | style=wx.TE_PROCESS_ENTER, name ='%s'% item) |
---|
2877 | fun_box.SetToolTipString("Hit 'Enter' after typing.") |
---|
2878 | fun_box.SetValue(format_number(value, True)) |
---|
2879 | sizer.Add(fun_box, (iy,ix),(1,1), wx.EXPAND) |
---|
2880 | self.str_parameters.append([None,item, fun_box, |
---|
2881 | None, None, None, |
---|
2882 | None, None]) |
---|
2883 | |
---|
2884 | |
---|
2885 | else: |
---|
2886 | ## add parameters name with checkbox for selecting to fit |
---|
2887 | cb = wx.CheckBox(self, -1, item ) |
---|
2888 | cb.SetToolTipString(" Check mark to fit.") |
---|
2889 | #cb.SetValue(True) |
---|
2890 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
2891 | |
---|
2892 | sizer.Add( cb,( iy, ix),(1,1), |
---|
2893 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2894 | |
---|
2895 | ## add parameter value |
---|
2896 | ix += 1 |
---|
2897 | value= self.model.getParam(item) |
---|
2898 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
2899 | style=wx.TE_PROCESS_ENTER) |
---|
2900 | ctl1.SetToolTipString("Hit 'Enter' after typing.") |
---|
2901 | ctl1.SetValue(format_number(value, True)) |
---|
2902 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
2903 | ## text to show error sign |
---|
2904 | ix += 1 |
---|
2905 | text2=wx.StaticText(self, -1, '+/-') |
---|
2906 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
2907 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2908 | if not self.is_mac: |
---|
2909 | text2.Hide() |
---|
2910 | ix += 1 |
---|
2911 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/1.2,20), style=0) |
---|
2912 | sizer.Add(ctl2, (iy,ix),(1,1), |
---|
2913 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2914 | if not self.is_mac: |
---|
2915 | ctl2.Hide() |
---|
2916 | |
---|
2917 | ix += 1 |
---|
2918 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/1.9,20), |
---|
2919 | style=wx.TE_PROCESS_ENTER, |
---|
2920 | text_enter_callback = self._onparamRangeEnter) |
---|
2921 | |
---|
2922 | sizer.Add(ctl3, (iy,ix),(1,1), |
---|
2923 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2924 | |
---|
2925 | ix += 1 |
---|
2926 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/1.9,20), |
---|
2927 | style=wx.TE_PROCESS_ENTER, |
---|
2928 | text_enter_callback = self._onparamRangeEnter) |
---|
2929 | sizer.Add(ctl4, (iy,ix),(1,1), |
---|
2930 | wx.EXPAND|wx.FIXED_MINSIZE, 0) |
---|
2931 | |
---|
2932 | ix +=1 |
---|
2933 | # Units |
---|
2934 | if self.model.details.has_key(item): |
---|
2935 | units = wx.StaticText(self, -1, |
---|
2936 | self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
2937 | else: |
---|
2938 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
2939 | sizer.Add(units, (iy,ix),(1,1), |
---|
2940 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2941 | |
---|
2942 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
2943 | self.parameters.append([cb,item, ctl1, |
---|
2944 | text2,ctl2, ctl3, ctl4,units]) |
---|
2945 | |
---|
2946 | iy+=1 |
---|
2947 | sizer.Add((10,10),(iy,ix),(1,1), |
---|
2948 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2949 | |
---|
2950 | # type can be either Guassian or Array |
---|
2951 | if len(self.model.dispersion.values())>0: |
---|
2952 | type= self.model.dispersion.values()[0]["type"] |
---|
2953 | else: |
---|
2954 | type = "Gaussian" |
---|
2955 | |
---|
2956 | iy += 1 |
---|
2957 | ix = 0 |
---|
2958 | #Add tile for orientational angle |
---|
2959 | for item in keys: |
---|
2960 | if item in self.model.orientation_params: |
---|
2961 | orient_angle = wx.StaticText(self, -1, '[For 2D only]:') |
---|
2962 | sizer.Add(orient_angle,(iy, ix),(1,1), |
---|
2963 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2964 | if not self.data.__class__.__name__ == "Data2D" and \ |
---|
2965 | not self.enable2D: |
---|
2966 | orient_angle.Hide() |
---|
2967 | else: |
---|
2968 | orient_angle.Show(True) |
---|
2969 | break |
---|
2970 | |
---|
2971 | #For Gaussian only |
---|
2972 | if type.lower() != "array": |
---|
2973 | for item in self.model.orientation_params: |
---|
2974 | if not item in self.disp_list: |
---|
2975 | ##prepare a spot to store min max |
---|
2976 | if not self.model.details.has_key(item): |
---|
2977 | self.model.details [item] = ["",None,None] |
---|
2978 | |
---|
2979 | iy += 1 |
---|
2980 | ix = 0 |
---|
2981 | ## add parameters name with checkbox for selecting to fit |
---|
2982 | cb = wx.CheckBox(self, -1, item ) |
---|
2983 | cb.SetValue(False) |
---|
2984 | cb.SetToolTipString("Check mark to fit") |
---|
2985 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
2986 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
2987 | self.enable2D: |
---|
2988 | cb.Show(True) |
---|
2989 | else: |
---|
2990 | cb.Hide() |
---|
2991 | sizer.Add( cb,( iy, ix),(1,1), |
---|
2992 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2993 | |
---|
2994 | ## add parameter value |
---|
2995 | ix += 1 |
---|
2996 | value= self.model.getParam(item) |
---|
2997 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
2998 | style=wx.TE_PROCESS_ENTER) |
---|
2999 | ctl1.SetToolTipString("Hit 'Enter' after typing.") |
---|
3000 | ctl1.SetValue(format_number(value, True)) |
---|
3001 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3002 | self.enable2D: |
---|
3003 | ctl1.Show(True) |
---|
3004 | else: |
---|
3005 | ctl1.Hide() |
---|
3006 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
3007 | ## text to show error sign |
---|
3008 | ix += 1 |
---|
3009 | text2=wx.StaticText(self, -1, '+/-') |
---|
3010 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
3011 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3012 | |
---|
3013 | text2.Hide() |
---|
3014 | ix += 1 |
---|
3015 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH/1.2,20), style=0) |
---|
3016 | sizer.Add(ctl2, (iy,ix),(1,1), |
---|
3017 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3018 | |
---|
3019 | ctl2.Hide() |
---|
3020 | |
---|
3021 | |
---|
3022 | ix += 1 |
---|
3023 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/1.8,20), |
---|
3024 | style=wx.TE_PROCESS_ENTER, |
---|
3025 | text_enter_callback = self._onparamRangeEnter) |
---|
3026 | |
---|
3027 | sizer.Add(ctl3, (iy,ix),(1,1), |
---|
3028 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3029 | ctl3.Hide() |
---|
3030 | |
---|
3031 | ix += 1 |
---|
3032 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/1.8,20), |
---|
3033 | style=wx.TE_PROCESS_ENTER, |
---|
3034 | text_enter_callback = self._onparamRangeEnter) |
---|
3035 | sizer.Add(ctl4, (iy,ix),(1,1), |
---|
3036 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3037 | |
---|
3038 | ctl4.Hide() |
---|
3039 | |
---|
3040 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3041 | self.enable2D: |
---|
3042 | if self.is_mac: |
---|
3043 | text2.Show(True) |
---|
3044 | ctl2.Show(True) |
---|
3045 | ctl3.Show(True) |
---|
3046 | ctl4.Show(True) |
---|
3047 | |
---|
3048 | ix +=1 |
---|
3049 | # Units |
---|
3050 | if self.model.details.has_key(item): |
---|
3051 | units = wx.StaticText(self, -1, |
---|
3052 | self.model.details[item][0], |
---|
3053 | style=wx.ALIGN_LEFT) |
---|
3054 | else: |
---|
3055 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
3056 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3057 | self.enable2D: |
---|
3058 | units.Show(True) |
---|
3059 | |
---|
3060 | else: |
---|
3061 | units.Hide() |
---|
3062 | |
---|
3063 | sizer.Add(units, (iy,ix),(1,1), |
---|
3064 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
3065 | |
---|
3066 | ##[cb state, name, value, "+/-", error of fit,min,max,units] |
---|
3067 | self.parameters.append([cb,item, ctl1, |
---|
3068 | text2,ctl2, ctl3, ctl4,units]) |
---|
3069 | self.orientation_params.append([cb,item, ctl1, |
---|
3070 | text2,ctl2, ctl3, ctl4,units]) |
---|
3071 | |
---|
3072 | iy+=1 |
---|
3073 | |
---|
3074 | #Display units text on panel |
---|
3075 | for item in keys: |
---|
3076 | if self.model.details.has_key(item): |
---|
3077 | self.text2_4.Show() |
---|
3078 | #Fill the list of fittable parameters |
---|
3079 | self.select_all_param(event=None) |
---|
3080 | |
---|
3081 | self.save_current_state_fit() |
---|
3082 | boxsizer1.Add(sizer) |
---|
3083 | self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
3084 | self.sizer3.Layout() |
---|
3085 | self.Layout() |
---|
3086 | #self.Refresh() |
---|
3087 | |
---|
3088 | def on_right_down(self, event): |
---|
3089 | """ |
---|
3090 | Get key stroke event |
---|
3091 | """ |
---|
3092 | if self.data == None: |
---|
3093 | return |
---|
3094 | # Figuring out key combo: Cmd for copy, Alt for paste |
---|
3095 | if event.AltDown() and event.ShiftDown(): |
---|
3096 | self._manager.show_ftol_dialog() |
---|
3097 | flag = True |
---|
3098 | elif event.AltDown() or event.ShiftDown(): |
---|
3099 | flag = False |
---|
3100 | else: |
---|
3101 | return |
---|
3102 | # make event free |
---|
3103 | event.Skip() |
---|
3104 | # messages depending on the flag |
---|
3105 | if not flag: |
---|
3106 | msg = " Could not open ftol dialog;" |
---|
3107 | msg += " Check if the Scipy fit engine is selected in the menubar." |
---|
3108 | infor = 'warning' |
---|
3109 | # inform msg to wx |
---|
3110 | wx.PostEvent( self.parent.parent, |
---|
3111 | StatusEvent(status= msg, info=infor)) |
---|
3112 | |
---|
3113 | |
---|
3114 | def _onModel2D(self, event): |
---|
3115 | """ |
---|
3116 | toggle view of model from 1D to 2D or 2D from 1D |
---|
3117 | """ |
---|
3118 | if self.model_view.GetLabelText() == "Show 2D": |
---|
3119 | self.model_view.SetLabel("Show 1D") |
---|
3120 | self.enable2D = True |
---|
3121 | |
---|
3122 | else: |
---|
3123 | self.model_view.SetLabel("Show 2D") |
---|
3124 | self.enable2D = False |
---|
3125 | self.create_default_data() |
---|
3126 | self.set_model_param_sizer(self.model) |
---|
3127 | self._set_sizer_dispersion() |
---|
3128 | self._set_smear_buttons() |
---|
3129 | self._draw_model() |
---|
3130 | |
---|
3131 | |
---|
3132 | self.state.enable2D = copy.deepcopy(self.enable2D) |
---|
3133 | |
---|
3134 | def _set_smear_buttons(self): |
---|
3135 | """ |
---|
3136 | Set semarer radio buttons |
---|
3137 | """ |
---|
3138 | # more disables for 2D |
---|
3139 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
3140 | self.enable2D: |
---|
3141 | self.slit_smearer.Disable() |
---|
3142 | self.pinhole_smearer.Enable(True) |
---|
3143 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
3144 | else: |
---|
3145 | self.slit_smearer.Enable(True) |
---|
3146 | self.pinhole_smearer.Enable(True) |
---|
3147 | |
---|
3148 | |
---|
3149 | class BGTextCtrl(wx.TextCtrl): |
---|
3150 | """ |
---|
3151 | Text control used to display outputs. |
---|
3152 | No editing allowed. The background is |
---|
3153 | grayed out. User can't select text. |
---|
3154 | """ |
---|
3155 | def __init__(self, *args, **kwds): |
---|
3156 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
3157 | self.SetEditable(False) |
---|
3158 | self.SetBackgroundColour(self.GetParent().parent.GetBackgroundColour()) |
---|
3159 | |
---|
3160 | # Bind to mouse event to avoid text highlighting |
---|
3161 | # The event will be skipped once the call-back |
---|
3162 | # is called. |
---|
3163 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
3164 | |
---|
3165 | def _click(self, event): |
---|
3166 | """ |
---|
3167 | Prevent further handling of the mouse event |
---|
3168 | by not calling Skip(). |
---|
3169 | """ |
---|
3170 | pass |
---|
3171 | |
---|