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 DataLoader.data_info import Data1D |
---|
12 | from sans.guicomm.events import StatusEvent |
---|
13 | from sans.guiframe.utils import format_number,check_float |
---|
14 | #from sans.guiframe.local_perspectives.plotting.Masking import ModelPanel2D as panel2D |
---|
15 | |
---|
16 | ## event to post model to fit to fitting plugins |
---|
17 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
18 | |
---|
19 | ## event to know the selected fit engine |
---|
20 | (FitterTypeEvent, EVT_FITTER_TYPE) = wx.lib.newevent.NewEvent() |
---|
21 | (FitStopEvent, EVT_FIT_STOP) = wx.lib.newevent.NewEvent() |
---|
22 | (Chi2UpdateEvent, EVT_CHI2_UPDATE) = wx.lib.newevent.NewEvent() |
---|
23 | _BOX_WIDTH = 76 |
---|
24 | _DATA_BOX_WIDTH = 300 |
---|
25 | SMEAR_SIZE_L = 0.005 |
---|
26 | SMEAR_SIZE_H = 0.006 |
---|
27 | |
---|
28 | import basepage |
---|
29 | from basepage import BasicPage |
---|
30 | from basepage import PageInfoEvent |
---|
31 | from DataLoader.qsmearing import smear_selection |
---|
32 | |
---|
33 | class FitPage(BasicPage): |
---|
34 | """ |
---|
35 | FitPanel class contains fields allowing to display results when |
---|
36 | fitting a model and one data |
---|
37 | @note: For Fit to be performed the user should check at least one parameter |
---|
38 | on fit Panel window. |
---|
39 | |
---|
40 | """ |
---|
41 | |
---|
42 | def __init__(self,parent, page_info): |
---|
43 | BasicPage.__init__(self, parent, page_info) |
---|
44 | |
---|
45 | """ |
---|
46 | Initialization of the Panel |
---|
47 | """ |
---|
48 | ## total number of point: float |
---|
49 | self.npts=None |
---|
50 | ## thread for compute Chisqr |
---|
51 | self.calc_Chisqr=None |
---|
52 | ## default fitengine type |
---|
53 | self.engine_type = None |
---|
54 | ## smear default |
---|
55 | self.smearer = None |
---|
56 | self.current_smearer = None |
---|
57 | ## 2D smear accuracy default |
---|
58 | self.smear2d_accuracy = 'Low' |
---|
59 | ## slit smear: |
---|
60 | self.dxl = None |
---|
61 | self.dxw = None |
---|
62 | ## pinhole smear |
---|
63 | self.dx_min = None |
---|
64 | self.dx_max = None |
---|
65 | |
---|
66 | ## model data (theory) calculated on drawing. |
---|
67 | self.theory_data = None |
---|
68 | |
---|
69 | ## draw sizer |
---|
70 | self._fill_datainfo_sizer() |
---|
71 | |
---|
72 | # get smear info from data |
---|
73 | self._get_smear_info() |
---|
74 | |
---|
75 | self._fill_model_sizer( self.sizer1) |
---|
76 | |
---|
77 | self._get_defult_custom_smear() |
---|
78 | |
---|
79 | self._fill_range_sizer() |
---|
80 | |
---|
81 | if self.data is None: |
---|
82 | self.formfactorbox.Disable() |
---|
83 | self.structurebox.Disable() |
---|
84 | else: |
---|
85 | self.smearer = smear_selection( self.data ) |
---|
86 | if self.smearer ==None: |
---|
87 | self.enable_smearer.Disable() |
---|
88 | #if self.data.__class__.__name__ =="Data2D": |
---|
89 | #if self.model != None: |
---|
90 | #self.smear_description_2d.Show(True) |
---|
91 | |
---|
92 | self.disp_cb_dict = {} |
---|
93 | ## to update the panel according to the fit engine type selected |
---|
94 | self.Bind(EVT_FITTER_TYPE,self._on_engine_change) |
---|
95 | self.Bind(EVT_FIT_STOP,self._on_fit_complete) |
---|
96 | self.Bind(EVT_CHI2_UPDATE, self.on_complete_chisqr) |
---|
97 | |
---|
98 | def _on_fit_complete(self, event): |
---|
99 | """ |
---|
100 | When fit is complete ,reset the fit button label. |
---|
101 | """ |
---|
102 | #self.btFit.SetLabel("Fit") |
---|
103 | #self.btFit.Unbind(event=wx.EVT_BUTTON, id=self.btFit.GetId()) |
---|
104 | #self.btFit.Bind(event=wx.EVT_BUTTON, handler=self._onFit,id=self.btFit.GetId()) |
---|
105 | pass |
---|
106 | |
---|
107 | def _is_2D(self): |
---|
108 | """ |
---|
109 | Check if data_name is Data2D |
---|
110 | @return: True or False |
---|
111 | """ |
---|
112 | |
---|
113 | if self.data.__class__.__name__ =="Data2D": |
---|
114 | return True |
---|
115 | return False |
---|
116 | |
---|
117 | def _on_engine_change(self, event): |
---|
118 | """ |
---|
119 | get an event containing the current name of the fit engine type |
---|
120 | @param event: FitterTypeEvent containing the name of the current engine |
---|
121 | """ |
---|
122 | self.engine_type = event.type |
---|
123 | if len(self.parameters)==0: |
---|
124 | self.Layout() |
---|
125 | return |
---|
126 | if event.type =="park": |
---|
127 | self.btFit.SetLabel("Fit") |
---|
128 | |
---|
129 | for item in self.parameters: |
---|
130 | if event.type =="scipy" : |
---|
131 | item[5].SetValue("") |
---|
132 | item[5].Hide() |
---|
133 | item[6].SetValue("") |
---|
134 | item[6].Hide() |
---|
135 | self.text2_min.Hide() |
---|
136 | self.text2_max.Hide() |
---|
137 | |
---|
138 | else: |
---|
139 | item[5].Show(True) |
---|
140 | item[6].Show(True) |
---|
141 | self.text2_min.Show(True) |
---|
142 | self.text2_max.Show(True) |
---|
143 | for item in self.fittable_param: |
---|
144 | if item[5]!=None and item[6]!=None and not item in self.orientation_params_disp: |
---|
145 | if event.type =="scipy" and not item in self.orientation_params: |
---|
146 | item[5].SetValue("") |
---|
147 | item[5].Hide() |
---|
148 | item[6].SetValue("") |
---|
149 | item[6].Hide() |
---|
150 | self.text2_min.Hide() |
---|
151 | self.text2_max.Hide() |
---|
152 | self.text_disp_min.Hide() |
---|
153 | self.text_disp_max.Hide() |
---|
154 | else: |
---|
155 | item[5].Show(True) |
---|
156 | item[6].Show(True) |
---|
157 | self.text2_min.Show(True) |
---|
158 | self.text2_max.Show(True) |
---|
159 | self.text_disp_min.Show(True) |
---|
160 | self.text_disp_max.Show(True) |
---|
161 | |
---|
162 | for item in self.orientation_params: |
---|
163 | if item[5]!=None and item[6]!=None: |
---|
164 | if event.type =="scipy" or self.data.__class__.__name__ !="Data2D": |
---|
165 | item[5].SetValue("") |
---|
166 | item[5].Hide() |
---|
167 | item[6].SetValue("") |
---|
168 | item[6].Hide() |
---|
169 | else: |
---|
170 | item[5].Show(True) |
---|
171 | item[6].Show(True) |
---|
172 | |
---|
173 | for item in self.orientation_params_disp: |
---|
174 | if item[5]!=None and item[6]!=None: |
---|
175 | if event.type =="scipy" or self.data.__class__.__name__ !="Data2D": |
---|
176 | item[5].SetValue("") |
---|
177 | item[5].Hide() |
---|
178 | item[6].SetValue("") |
---|
179 | item[6].Hide() |
---|
180 | else: |
---|
181 | item[5].Show(True) |
---|
182 | item[6].Show(True) |
---|
183 | self.Layout() |
---|
184 | self.Refresh() |
---|
185 | |
---|
186 | def _fill_range_sizer(self): |
---|
187 | """ |
---|
188 | Fill the sizer containing the plotting range |
---|
189 | add access to npts |
---|
190 | """ |
---|
191 | is_2Ddata = False |
---|
192 | |
---|
193 | # Check if data is 2D |
---|
194 | if self.data.__class__.__name__ == 'Data2D': |
---|
195 | is_2Ddata = True |
---|
196 | |
---|
197 | title = "Fitting" |
---|
198 | #smear messages & titles |
---|
199 | smear_message_none = "No smearing is selected..." |
---|
200 | smear_message_dqdata = "The dQ data is being used for smearing..." |
---|
201 | smear_message_2d = "Higher accuracy is very time-expensive. Use it with care..." |
---|
202 | smear_message_new_ssmear = "Please enter only the value of interest to customize smearing..." |
---|
203 | smear_message_new_psmear = "Please enter both; the dQ will be generated by interpolation..." |
---|
204 | smear_message_2d_x_title = "<dQx>[1/A]:" |
---|
205 | smear_message_2d_y_title = "<dQy>[1/A]:" |
---|
206 | smear_message_pinhole_min_title = "dQ_low[1/A]:" |
---|
207 | smear_message_pinhole_max_title = "dQ_high[1/A]:" |
---|
208 | smear_message_slit_height_title = "Slit height[1/A]:" |
---|
209 | smear_message_slit_width_title = "Slit width[1/A]:" |
---|
210 | |
---|
211 | self._get_smear_info() |
---|
212 | |
---|
213 | #Sizers |
---|
214 | box_description_range = wx.StaticBox(self, -1,str(title)) |
---|
215 | boxsizer_range = wx.StaticBoxSizer(box_description_range, wx.VERTICAL) |
---|
216 | self.sizer_set_smearer = wx.BoxSizer(wx.VERTICAL) |
---|
217 | sizer_smearer = wx.BoxSizer(wx.HORIZONTAL) |
---|
218 | self.sizer_new_smear= wx.BoxSizer(wx.HORIZONTAL) |
---|
219 | self.sizer_set_masking = wx.BoxSizer(wx.HORIZONTAL) |
---|
220 | sizer_chi2 = wx.BoxSizer(wx.VERTICAL) |
---|
221 | smear_set_box= wx.StaticBox(self, -1,'Set Instrumental Smearing') |
---|
222 | sizer_smearer_box = wx.StaticBoxSizer(smear_set_box, wx.HORIZONTAL) |
---|
223 | sizer_smearer_box.SetMinSize((_DATA_BOX_WIDTH,85)) |
---|
224 | sizer_fit = wx.GridSizer(2, 4,2,6) |
---|
225 | |
---|
226 | # combobox for smear2d accuracy selection |
---|
227 | self.smear_accuracy = wx.ComboBox(self, -1,size=(50,-1),style=wx.CB_READONLY) |
---|
228 | self._set_accuracy_list() |
---|
229 | self.smear_accuracy.SetValue(self.smear2d_accuracy) |
---|
230 | self.smear_accuracy.SetSelection(0) |
---|
231 | self.smear_accuracy.SetToolTipString("'Higher' uses more Gaussian points for smearing computation.") |
---|
232 | |
---|
233 | |
---|
234 | |
---|
235 | wx.EVT_COMBOBOX(self.smear_accuracy,-1, self._on_select_accuracy) |
---|
236 | |
---|
237 | #Fit button |
---|
238 | self.btFit = wx.Button(self,wx.NewId(),'Fit', size=(88,25)) |
---|
239 | self.btFit.Bind(wx.EVT_BUTTON, self._onFit,id= self.btFit.GetId()) |
---|
240 | self.btFit.SetToolTipString("Start fitting.") |
---|
241 | |
---|
242 | #textcntrl for custom resolution |
---|
243 | self.smear_pinhole_max = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
244 | text_enter_callback = self.onPinholeSmear) |
---|
245 | self.smear_pinhole_min = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
246 | text_enter_callback = self.onPinholeSmear) |
---|
247 | self.smear_slit_height= self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
248 | text_enter_callback = self.onSlitSmear) |
---|
249 | self.smear_slit_width = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
250 | text_enter_callback = self.onSlitSmear) |
---|
251 | |
---|
252 | ## smear |
---|
253 | self.smear_data_left= BGTextCtrl(self, -1, size=(_BOX_WIDTH-25,20), style=0) |
---|
254 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
255 | self.smear_data_right = BGTextCtrl(self, -1, size=(_BOX_WIDTH-25,20), style=0) |
---|
256 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
257 | |
---|
258 | #set default values for smear |
---|
259 | self.smear_pinhole_max.SetValue(str(self.dx_max)) |
---|
260 | self.smear_pinhole_min.SetValue(str(self.dx_min)) |
---|
261 | self.smear_slit_height.SetValue(str(self.dxl)) |
---|
262 | self.smear_slit_width.SetValue(str(self.dxw)) |
---|
263 | |
---|
264 | #Filling the sizer containing instruments smearing info. |
---|
265 | self.disable_smearer = wx.RadioButton(self, -1, 'None', style=wx.RB_GROUP) |
---|
266 | self.enable_smearer = wx.RadioButton(self, -1, 'Use dQ Data') |
---|
267 | #self.enable_smearer.SetToolTipString("Click to use the loaded dQ data for smearing.") |
---|
268 | self.pinhole_smearer = wx.RadioButton(self, -1, 'Custom Pinhole Smear') |
---|
269 | #self.pinhole_smearer.SetToolTipString("Click to input custom resolution for pinhole smearing.") |
---|
270 | self.slit_smearer = wx.RadioButton(self, -1, 'Custom Slit Smear') |
---|
271 | #self.slit_smearer.SetToolTipString("Click to input custom resolution for slit smearing.") |
---|
272 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.disable_smearer.GetId()) |
---|
273 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.enable_smearer.GetId()) |
---|
274 | self.Bind(wx.EVT_RADIOBUTTON, self.onPinholeSmear, id=self.pinhole_smearer.GetId()) |
---|
275 | self.Bind(wx.EVT_RADIOBUTTON, self.onSlitSmear, id=self.slit_smearer.GetId()) |
---|
276 | self.disable_smearer.SetValue(True) |
---|
277 | |
---|
278 | # add 4 types of smearing to the sizer |
---|
279 | sizer_smearer.Add( self.disable_smearer,0, wx.LEFT, 10) |
---|
280 | sizer_smearer.Add((10,10)) |
---|
281 | sizer_smearer.Add( self.enable_smearer) |
---|
282 | sizer_smearer.Add((10,10)) |
---|
283 | sizer_smearer.Add( self.pinhole_smearer ) |
---|
284 | sizer_smearer.Add((10,10)) |
---|
285 | sizer_smearer.Add( self.slit_smearer ) |
---|
286 | sizer_smearer.Add((10,10)) |
---|
287 | |
---|
288 | # StaticText for chi2, N(for fitting), Npts |
---|
289 | self.tcChi = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
290 | self.tcChi.SetToolTipString("Chi2/Npts") |
---|
291 | self.Npts_fit = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
292 | self.Npts_fit.SetToolTipString(" Npts : number of points selected for fitting") |
---|
293 | self.Npts_total = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
294 | self.Npts_total.SetToolTipString(" Total Npts : total number of data points") |
---|
295 | box_description_1= wx.StaticText(self, -1,' Chi2/Npts') |
---|
296 | box_description_2= wx.StaticText(self, -1,' Npts') |
---|
297 | box_description_3= wx.StaticText(self, -1,' Total Npts') |
---|
298 | box_description_4= wx.StaticText(self, -1,' ') |
---|
299 | |
---|
300 | |
---|
301 | sizer_fit.Add(box_description_1,0,0) |
---|
302 | sizer_fit.Add(box_description_2,0,0) |
---|
303 | sizer_fit.Add(box_description_3,0,0) |
---|
304 | sizer_fit.Add(box_description_4,0,0) |
---|
305 | sizer_fit.Add(self.tcChi,0,0) |
---|
306 | sizer_fit.Add(self.Npts_fit ,0,0) |
---|
307 | sizer_fit.Add(self.Npts_total,0,0) |
---|
308 | sizer_fit.Add(self.btFit,0,0) |
---|
309 | |
---|
310 | # StaticText for smear |
---|
311 | #self.tcChi = wx.StaticText(self, -1, "-", style=wx.ALIGN_LEFT) |
---|
312 | self.smear_description_none = wx.StaticText(self, -1, smear_message_none , style=wx.ALIGN_LEFT) |
---|
313 | self.smear_description_dqdata = wx.StaticText(self, -1, smear_message_dqdata , style=wx.ALIGN_LEFT) |
---|
314 | self.smear_description_type = wx.StaticText(self, -1, "Type:" , style=wx.ALIGN_LEFT) |
---|
315 | self.smear_description_accuracy_type = wx.StaticText(self, -1, "Accuracy:" , style=wx.ALIGN_LEFT) |
---|
316 | self.smear_description_smear_type = BGTextCtrl(self, -1, size=(57,20), style=0) |
---|
317 | self.smear_description_smear_type.SetValue(str(self.dq_l)) |
---|
318 | self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) |
---|
319 | self.smear_description_2d = wx.StaticText(self, -1, smear_message_2d , style=wx.ALIGN_LEFT) |
---|
320 | self.smear_message_new_s = wx.StaticText(self, -1, smear_message_new_ssmear , style=wx.ALIGN_LEFT) |
---|
321 | self.smear_message_new_p = wx.StaticText(self, -1, smear_message_new_psmear , style=wx.ALIGN_LEFT) |
---|
322 | self.smear_description_2d_x = wx.StaticText(self, -1, smear_message_2d_x_title , style=wx.ALIGN_LEFT) |
---|
323 | self.smear_description_2d_y = wx.StaticText(self, -1, smear_message_2d_y_title , style=wx.ALIGN_LEFT) |
---|
324 | self.smear_description_pin_min = wx.StaticText(self, -1, smear_message_pinhole_min_title , style=wx.ALIGN_LEFT) |
---|
325 | self.smear_description_pin_max = wx.StaticText(self, -1, smear_message_pinhole_max_title , style=wx.ALIGN_LEFT) |
---|
326 | self.smear_description_slit_height = wx.StaticText(self, -1, smear_message_slit_height_title , style=wx.ALIGN_LEFT) |
---|
327 | self.smear_description_slit_width = wx.StaticText(self, -1, smear_message_slit_width_title , style=wx.ALIGN_LEFT) |
---|
328 | |
---|
329 | #arrange sizers |
---|
330 | #boxsizer1.Add( self.tcChi ) |
---|
331 | self.sizer_set_smearer.Add(sizer_smearer ) |
---|
332 | self.sizer_set_smearer.Add((10,10)) |
---|
333 | self.sizer_set_smearer.Add( self.smear_description_none,0, wx.CENTER, 10 ) |
---|
334 | self.sizer_set_smearer.Add( self.smear_description_dqdata,0, wx.CENTER, 10 ) |
---|
335 | self.sizer_set_smearer.Add( self.smear_description_2d,0, wx.CENTER, 10 ) |
---|
336 | self.sizer_new_smear.Add( self.smear_description_type,0, wx.CENTER, 10 ) |
---|
337 | self.sizer_new_smear.Add( self.smear_description_accuracy_type,0, wx.CENTER, 10 ) |
---|
338 | self.sizer_new_smear.Add( self.smear_accuracy ) |
---|
339 | self.sizer_new_smear.Add( self.smear_description_smear_type,0, wx.CENTER, 10 ) |
---|
340 | self.sizer_new_smear.Add((15,-1)) |
---|
341 | self.sizer_new_smear.Add( self.smear_description_2d_x,0, wx.CENTER, 10 ) |
---|
342 | self.sizer_new_smear.Add( self.smear_description_pin_min,0, wx.CENTER, 10 ) |
---|
343 | self.sizer_new_smear.Add( self.smear_description_slit_height,0, wx.CENTER, 10 ) |
---|
344 | |
---|
345 | self.sizer_new_smear.Add( self.smear_pinhole_min,0, wx.CENTER, 10 ) |
---|
346 | self.sizer_new_smear.Add( self.smear_slit_height,0, wx.CENTER, 10 ) |
---|
347 | self.sizer_new_smear.Add( self.smear_data_left,0, wx.CENTER, 10 ) |
---|
348 | self.sizer_new_smear.Add((20,-1)) |
---|
349 | self.sizer_new_smear.Add( self.smear_description_2d_y,0, wx.CENTER, 10 ) |
---|
350 | self.sizer_new_smear.Add( self.smear_description_pin_max,0, wx.CENTER, 10 ) |
---|
351 | self.sizer_new_smear.Add( self.smear_description_slit_width,0, wx.CENTER, 10 ) |
---|
352 | |
---|
353 | self.sizer_new_smear.Add( self.smear_pinhole_max,0, wx.CENTER, 10 ) |
---|
354 | self.sizer_new_smear.Add( self.smear_slit_width,0, wx.CENTER, 10 ) |
---|
355 | self.sizer_new_smear.Add( self.smear_data_right,0, wx.CENTER, 10 ) |
---|
356 | |
---|
357 | self.sizer_set_smearer.Add( self.smear_message_new_s,0, wx.CENTER, 10) |
---|
358 | self.sizer_set_smearer.Add( self.smear_message_new_p,0, wx.CENTER, 10) |
---|
359 | self.sizer_set_smearer.Add((5,2)) |
---|
360 | self.sizer_set_smearer.Add( self.sizer_new_smear,0, wx.CENTER, 10 ) |
---|
361 | |
---|
362 | # add all to chi2 sizer |
---|
363 | sizer_smearer_box.Add(self.sizer_set_smearer) |
---|
364 | sizer_chi2.Add(sizer_smearer_box) |
---|
365 | sizer_chi2.Add((-1,5)) |
---|
366 | |
---|
367 | # hide all smear messages and textctrl |
---|
368 | self._hide_all_smear_info() |
---|
369 | |
---|
370 | # get smear_selection |
---|
371 | self.current_smearer= smear_selection( self.data ) |
---|
372 | |
---|
373 | # Show only the relevant smear messages, etc |
---|
374 | if self.current_smearer == None: |
---|
375 | if not is_2Ddata: |
---|
376 | self.smear_description_none.Show(True) |
---|
377 | self.enable_smearer.Disable() |
---|
378 | else: |
---|
379 | self.smear_description_none.Show(True) |
---|
380 | #self.smear_description_2d.Show(True) |
---|
381 | #self.pinhole_smearer.Disable() |
---|
382 | self.slit_smearer.Disable() |
---|
383 | #self.enable_smearer.Disable() |
---|
384 | else: self._show_smear_sizer() |
---|
385 | boxsizer_range.Add(self.sizer_set_masking) |
---|
386 | |
---|
387 | #Set sizer for Fitting section |
---|
388 | self._set_range_sizer( title=title,box_sizer=boxsizer_range, object1=sizer_chi2, object= sizer_fit) |
---|
389 | |
---|
390 | def _fill_datainfo_sizer(self): |
---|
391 | """ |
---|
392 | fill sizer 0 with data info |
---|
393 | """ |
---|
394 | ## no loaded data , don't fill the sizer |
---|
395 | if self.data is None: |
---|
396 | data_min = "" |
---|
397 | data_max = "" |
---|
398 | data_name = "" |
---|
399 | else: |
---|
400 | data_name = self.data.name |
---|
401 | #set maximum range for x in linear scale |
---|
402 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
403 | # Minimum value of data |
---|
404 | data_min = min(self.data.x) |
---|
405 | # Maximum value of data |
---|
406 | data_max = max(self.data.x) |
---|
407 | else: |
---|
408 | ## Minimum value of data |
---|
409 | data_min = 0 |
---|
410 | x = max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
411 | y = max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
412 | ## Maximum value of data |
---|
413 | data_max = math.sqrt(x*x + y*y) |
---|
414 | |
---|
415 | box_description= wx.StaticBox(self, -1, 'Data') |
---|
416 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
417 | #---------------------------------------------------------- |
---|
418 | sizer_data = wx.BoxSizer(wx.HORIZONTAL) |
---|
419 | #Filling the sizer containing data related fields |
---|
420 | #self.dataSource = wx.StaticText(self, -1,str(self.data.name)) |
---|
421 | self.dataSource = BGTextCtrl(self, -1) |
---|
422 | self.dataSource.SetValue(str(data_name)) |
---|
423 | #self.dataSource.SetEditable(False) |
---|
424 | self.dataSource.SetMinSize((_DATA_BOX_WIDTH, -1)) |
---|
425 | sizer_data.Add(wx.StaticText(self, -1, 'Source Name : ')) |
---|
426 | sizer_data.Add(self.dataSource ) |
---|
427 | sizer_data.Add( (0,5) ) |
---|
428 | |
---|
429 | #---------sizer 2 draw-------------------------------- |
---|
430 | text4_3 = wx.StaticText(self, -1, 'Total Q Range (1/A)', |
---|
431 | style=wx.ALIGN_LEFT) |
---|
432 | sizer_range = wx.BoxSizer(wx.HORIZONTAL) |
---|
433 | sizer_range.Add( text4_3 ,0, wx.RIGHT, 10) |
---|
434 | self.minimum_q = BGTextCtrl(self, -1, size=(_BOX_WIDTH,20)) |
---|
435 | self.minimum_q.SetValue(str(data_min)) |
---|
436 | #self.minimum_q.SetEditable(False) |
---|
437 | self.maximum_q = BGTextCtrl(self, -1,size=(_BOX_WIDTH,20)) |
---|
438 | self.maximum_q.SetValue(str(data_max)) |
---|
439 | #self.maximum_q.SetEditable(False) |
---|
440 | sizer_range.Add(wx.StaticText(self, -1, "Min: "),0, wx.LEFT, 10) |
---|
441 | sizer_range.Add(self.minimum_q,0, wx.LEFT, 10) |
---|
442 | sizer_range.Add(wx.StaticText(self, -1, "Max: "),0, wx.LEFT, 10) |
---|
443 | sizer_range.Add(self.maximum_q,0, wx.LEFT, 10) |
---|
444 | |
---|
445 | ## set q range to plot |
---|
446 | self.qmin_x = data_min |
---|
447 | self.qmax_x = data_max |
---|
448 | |
---|
449 | boxsizer1.Add(sizer_data,0, wx.ALL, 10) |
---|
450 | boxsizer1.Add(sizer_range, 0 , wx.LEFT, 10) |
---|
451 | #------------------------------------------------------------ |
---|
452 | self.sizer0.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
453 | self.sizer0.Layout() |
---|
454 | |
---|
455 | def _fill_model_sizer(self, sizer): |
---|
456 | """ |
---|
457 | fill sizer containing model info |
---|
458 | """ |
---|
459 | ##Add model function Details button in fitpanel. |
---|
460 | ##The following 3 lines are for Mac. Let JHC know before modifying... |
---|
461 | title = "Model" |
---|
462 | box_description= wx.StaticBox(self, -1,str(title)) |
---|
463 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
464 | |
---|
465 | id = wx.NewId() |
---|
466 | self.model_help =wx.Button(self,id,'Details', size=(80,23)) |
---|
467 | self.model_help.Bind(wx.EVT_BUTTON, self.on_model_help_clicked,id=id) |
---|
468 | self.model_help.SetToolTipString("Model Function Help") |
---|
469 | |
---|
470 | ## class base method to add view 2d button |
---|
471 | self._set_model_sizer(sizer=sizer, box_sizer=boxsizer1, title="Model",object=self.model_help ) |
---|
472 | |
---|
473 | |
---|
474 | def _set_sizer_dispersion(self, dispersity): |
---|
475 | """ |
---|
476 | draw sizer with gaussian dispersity parameters |
---|
477 | """ |
---|
478 | self.fittable_param=[] |
---|
479 | self.fixed_param=[] |
---|
480 | self.orientation_params_disp=[] |
---|
481 | |
---|
482 | self.sizer4_4.Clear(True) |
---|
483 | if self.model==None: |
---|
484 | ##no model is selected |
---|
485 | return |
---|
486 | if not self.enable_disp.GetValue(): |
---|
487 | ## the user didn't select dispersity display |
---|
488 | return |
---|
489 | |
---|
490 | self._reset_dispersity() |
---|
491 | |
---|
492 | # Create the dispersion objects |
---|
493 | for item in self.model.dispersion.keys(): |
---|
494 | #disp_model = GaussianDispersion() |
---|
495 | disp_model = dispersity() |
---|
496 | self._disp_obj_dict[item] = disp_model |
---|
497 | self.model.set_dispersion(item, disp_model) |
---|
498 | self.state._disp_obj_dict[item]= disp_model |
---|
499 | |
---|
500 | |
---|
501 | ix=0 |
---|
502 | iy=1 |
---|
503 | disp = wx.StaticText(self, -1, ' ') |
---|
504 | self.sizer4_4.Add(disp,( iy, ix),(1,1), |
---|
505 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
506 | ix += 1 |
---|
507 | values = wx.StaticText(self, -1, 'Sigma (STD)') |
---|
508 | values.SetToolTipString("Polydispersity multiplied by the value of the original parameter.") |
---|
509 | self.sizer4_4.Add(values,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
510 | ix +=2 |
---|
511 | self.text_disp_1 = wx.StaticText(self, -1, '') |
---|
512 | self.sizer4_4.Add( self.text_disp_1,(iy, ix),(1,1),\ |
---|
513 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
514 | self.text_disp_1.Hide() |
---|
515 | |
---|
516 | |
---|
517 | ix +=1 |
---|
518 | self.text_disp_min = wx.StaticText(self, -1, 'Min') |
---|
519 | self.sizer4_4.Add(self.text_disp_min,(iy, ix),(1,1),\ |
---|
520 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
521 | self.text_disp_min.Hide() |
---|
522 | ix +=1 |
---|
523 | self.text_disp_max = wx.StaticText(self, -1, 'Max') |
---|
524 | self.sizer4_4.Add(self.text_disp_max,(iy, ix),(1,1),\ |
---|
525 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
526 | self.text_disp_max.Hide() |
---|
527 | |
---|
528 | |
---|
529 | ix += 1 |
---|
530 | npts = wx.StaticText(self, -1, 'Npts') |
---|
531 | npts.SetToolTipString("Number of points for weighting.") |
---|
532 | self.sizer4_4.Add(npts,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
533 | ix += 1 |
---|
534 | nsigmas = wx.StaticText(self, -1, 'Nsigmas') |
---|
535 | nsigmas.SetToolTipString("Number of sigmas between which the range of the distribution function will be used for weighting. The value '3' covers 99.5% for Gaussian distribution function.") |
---|
536 | self.sizer4_4.Add(nsigmas,( iy, ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
537 | |
---|
538 | if self.engine_type=="park": |
---|
539 | self.text_disp_max.Show(True) |
---|
540 | self.text_disp_min.Show(True) |
---|
541 | |
---|
542 | for item in self.model.dispersion.keys(): |
---|
543 | if not item in self.model.orientation_params: |
---|
544 | if not self.disp_cb_dict.has_key(item): |
---|
545 | self.disp_cb_dict[item]= None |
---|
546 | name1=item+".width" |
---|
547 | name2=item+".npts" |
---|
548 | name3=item+".nsigmas" |
---|
549 | if not self.model.details.has_key(name1): |
---|
550 | self.model.details [name1] = ["",None,None] |
---|
551 | |
---|
552 | iy += 1 |
---|
553 | for p in self.model.dispersion[item].keys(): |
---|
554 | |
---|
555 | if p=="width": |
---|
556 | ix = 0 |
---|
557 | cb = wx.CheckBox(self, -1, name1, (10, 10)) |
---|
558 | cb.SetToolTipString("Check for fitting") |
---|
559 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
560 | self.sizer4_4.Add( cb,( iy, ix),(1,1), |
---|
561 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
562 | ix = 1 |
---|
563 | value= self.model.getParam(name1) |
---|
564 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
565 | style=wx.TE_PROCESS_ENTER) |
---|
566 | ctl1.SetToolTipString("Polydispersity multiplied by the value of '%s'."%item) |
---|
567 | ctl1.SetValue(str (format_number(value))) |
---|
568 | self.sizer4_4.Add(ctl1, (iy,ix),(1,1),wx.EXPAND) |
---|
569 | ## text to show error sign |
---|
570 | ix = 2 |
---|
571 | text2=wx.StaticText(self, -1, '+/-') |
---|
572 | self.sizer4_4.Add(text2,(iy, ix),(1,1), |
---|
573 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
574 | text2.Hide() |
---|
575 | |
---|
576 | ix = 3 |
---|
577 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
578 | |
---|
579 | self.sizer4_4.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
580 | |
---|
581 | ctl2.Hide() |
---|
582 | |
---|
583 | ix = 4 |
---|
584 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
585 | text_enter_callback = self._onparamRangeEnter) |
---|
586 | |
---|
587 | self.sizer4_4.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
588 | ctl3.Hide() |
---|
589 | |
---|
590 | ix = 5 |
---|
591 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
592 | text_enter_callback = self._onparamRangeEnter) |
---|
593 | |
---|
594 | self.sizer4_4.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
595 | |
---|
596 | ctl4.Hide() |
---|
597 | |
---|
598 | if self.engine_type=="park": |
---|
599 | ctl3.Show(True) |
---|
600 | ctl4.Show(True) |
---|
601 | |
---|
602 | self.fittable_param.append([cb,name1,ctl1,text2, |
---|
603 | ctl2, ctl3, ctl4,None]) |
---|
604 | |
---|
605 | elif p=="npts": |
---|
606 | ix = 6 |
---|
607 | value= self.model.getParam(name2) |
---|
608 | Tctl = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
609 | style=wx.TE_PROCESS_ENTER) |
---|
610 | |
---|
611 | Tctl.SetValue(str (format_number(value))) |
---|
612 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
613 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
614 | self.fixed_param.append([None,name2, Tctl,None,None, |
---|
615 | None, None,None]) |
---|
616 | elif p=="nsigmas": |
---|
617 | ix = 7 |
---|
618 | value= self.model.getParam(name3) |
---|
619 | Tct2 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
620 | style=wx.TE_PROCESS_ENTER) |
---|
621 | |
---|
622 | Tct2.SetValue(str (format_number(value))) |
---|
623 | self.sizer4_4.Add(Tct2, (iy,ix),(1,1), |
---|
624 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
625 | ix +=1 |
---|
626 | self.sizer4_4.Add((20,20), (iy,ix),(1,1), |
---|
627 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
628 | |
---|
629 | self.fixed_param.append([None,name3, Tct2 |
---|
630 | ,None,None,None, None,None]) |
---|
631 | |
---|
632 | ix =0 |
---|
633 | iy +=1 |
---|
634 | self.sizer4_4.Add((20,20),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
635 | for item in self.model.dispersion.keys(): |
---|
636 | if item in self.model.orientation_params: |
---|
637 | if not self.disp_cb_dict.has_key(item): |
---|
638 | self.disp_cb_dict[item]= None |
---|
639 | name1=item+".width" |
---|
640 | name2=item+".npts" |
---|
641 | name3=item+".nsigmas" |
---|
642 | if not self.model.details.has_key(name1): |
---|
643 | self.model.details [name1] = ["",None,None] |
---|
644 | |
---|
645 | |
---|
646 | iy += 1 |
---|
647 | for p in self.model.dispersion[item].keys(): |
---|
648 | |
---|
649 | if p=="width": |
---|
650 | ix = 0 |
---|
651 | cb = wx.CheckBox(self, -1, name1, (10, 10)) |
---|
652 | cb.SetToolTipString("Check for fitting") |
---|
653 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
654 | self.sizer4_4.Add( cb,( iy, ix),(1,1), |
---|
655 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
656 | if self.data.__class__.__name__ =="Data2D": |
---|
657 | cb.Show(True) |
---|
658 | elif cb.IsShown(): |
---|
659 | cb.Hide() |
---|
660 | ix = 1 |
---|
661 | value= self.model.getParam(name1) |
---|
662 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
663 | style=wx.TE_PROCESS_ENTER) |
---|
664 | ctl1.SetToolTipString("Polydispersity multiplied by the value of '%s'."%item) |
---|
665 | ctl1.SetValue(str (format_number(value))) |
---|
666 | if self.data.__class__.__name__ =="Data2D": |
---|
667 | ctl1.Show(True) |
---|
668 | elif ctl1.IsShown(): |
---|
669 | ctl1.Hide() |
---|
670 | self.sizer4_4.Add(ctl1, (iy,ix),(1,1),wx.EXPAND) |
---|
671 | ## text to show error sign |
---|
672 | ix = 2 |
---|
673 | text2=wx.StaticText(self, -1, '+/-') |
---|
674 | self.sizer4_4.Add(text2,(iy, ix),(1,1), |
---|
675 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
676 | text2.Hide() |
---|
677 | |
---|
678 | ix = 3 |
---|
679 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
680 | |
---|
681 | self.sizer4_4.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
682 | ctl2.Hide() |
---|
683 | |
---|
684 | ix = 4 |
---|
685 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
686 | text_enter_callback = self._onparamRangeEnter) |
---|
687 | |
---|
688 | self.sizer4_4.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
689 | |
---|
690 | ctl3.Hide() |
---|
691 | |
---|
692 | ix = 5 |
---|
693 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
694 | text_enter_callback = self._onparamRangeEnter) |
---|
695 | self.sizer4_4.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
696 | ctl4.Hide() |
---|
697 | #if self.data.__class__.__name__ =="Data2D": |
---|
698 | #ctl4.Enable(True) |
---|
699 | #elif ctl4.Shown(): |
---|
700 | #ctl4.Hide() |
---|
701 | |
---|
702 | if self.engine_type=="park" and self.data.__class__.__name__ =="Data2D": |
---|
703 | ctl3.Show(True) |
---|
704 | ctl4.Show(True) |
---|
705 | |
---|
706 | |
---|
707 | |
---|
708 | |
---|
709 | self.fittable_param.append([cb,name1,ctl1,text2, |
---|
710 | ctl2, ctl3, ctl4,None]) |
---|
711 | self.orientation_params_disp.append([cb,name1,ctl1,text2, |
---|
712 | ctl2, ctl3, ctl4,None]) |
---|
713 | elif p=="npts": |
---|
714 | ix = 6 |
---|
715 | value= self.model.getParam(name2) |
---|
716 | Tctl = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
717 | style=wx.TE_PROCESS_ENTER) |
---|
718 | |
---|
719 | Tctl.SetValue(str (format_number(value))) |
---|
720 | if self.data.__class__.__name__ =="Data2D": |
---|
721 | Tctl.Show(True) |
---|
722 | else: |
---|
723 | Tctl.Hide() |
---|
724 | self.sizer4_4.Add(Tctl, (iy,ix),(1,1), |
---|
725 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
726 | self.fixed_param.append([None,name2, Tctl,None,None, |
---|
727 | None, None,None]) |
---|
728 | self.orientation_params_disp.append([None,name2, Tctl,None,None, |
---|
729 | None, None,None]) |
---|
730 | elif p=="nsigmas": |
---|
731 | ix = 7 |
---|
732 | value= self.model.getParam(name3) |
---|
733 | Tct2 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), |
---|
734 | style=wx.TE_PROCESS_ENTER) |
---|
735 | |
---|
736 | Tct2.SetValue(str (format_number(value))) |
---|
737 | if self.data.__class__.__name__ =="Data2D": |
---|
738 | Tct2.Show(True) |
---|
739 | else: |
---|
740 | Tct2.Hide() |
---|
741 | self.sizer4_4.Add(Tct2, (iy,ix),(1,1), |
---|
742 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
743 | ix +=1 |
---|
744 | |
---|
745 | self.fixed_param.append([None,name3, Tct2 |
---|
746 | ,None,None, None, None,None]) |
---|
747 | |
---|
748 | self.orientation_params_disp.append([None,name3, Tct2 |
---|
749 | ,None,None, None, None,None]) |
---|
750 | |
---|
751 | |
---|
752 | self.state.disp_cb_dict = copy.deepcopy(self.disp_cb_dict) |
---|
753 | |
---|
754 | self.state.model = self.model.clone() |
---|
755 | ## save state into |
---|
756 | self.state.cb1 = self.cb1.GetValue() |
---|
757 | self._copy_parameters_state(self.parameters, self.state.parameters) |
---|
758 | self._copy_parameters_state(self.orientation_params_disp, |
---|
759 | self.state.orientation_params_disp) |
---|
760 | self._copy_parameters_state(self.fittable_param, self.state.fittable_param) |
---|
761 | self._copy_parameters_state(self.fixed_param, self.state.fixed_param) |
---|
762 | |
---|
763 | |
---|
764 | wx.PostEvent(self.parent, StatusEvent(status=\ |
---|
765 | " Selected Distribution: Gaussian")) |
---|
766 | #Fill the list of fittable parameters |
---|
767 | self.select_all_param(event=None) |
---|
768 | |
---|
769 | self.Layout() |
---|
770 | |
---|
771 | |
---|
772 | def _onFit(self, event): |
---|
773 | """ |
---|
774 | Allow to fit |
---|
775 | """ |
---|
776 | #make sure all parameter values are updated. |
---|
777 | if self.check_invalid_panel(): |
---|
778 | return |
---|
779 | if self.model ==None: |
---|
780 | msg="Please select a Model first..." |
---|
781 | wx.MessageBox(msg, 'Info') |
---|
782 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
783 | "Fit: %s"%msg)) |
---|
784 | return |
---|
785 | |
---|
786 | flag = self._update_paramv_on_fit() |
---|
787 | |
---|
788 | if not flag: |
---|
789 | msg= "Fitting range or parameters are invalid" |
---|
790 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
791 | return |
---|
792 | |
---|
793 | if len(self.param_toFit) <= 0: |
---|
794 | msg= "Select at least one parameter to fit" |
---|
795 | wx.PostEvent(self.parent.parent, StatusEvent(status= msg )) |
---|
796 | return |
---|
797 | |
---|
798 | |
---|
799 | self.select_param(event =None) |
---|
800 | |
---|
801 | #Clear errors if exist from previous fitting |
---|
802 | #self._clear_Err_on_Fit() |
---|
803 | |
---|
804 | # Remove or do not allow fitting on the Q=0 point, especially when y(q=0)=None at x[0]. |
---|
805 | self.qmin_x = float(self.qmin.GetValue()) |
---|
806 | self.qmax_x = float( self.qmax.GetValue()) |
---|
807 | self.manager._reset_schedule_problem( value=0) |
---|
808 | self.manager.schedule_for_fit( value=1,page=self,fitproblem =None) |
---|
809 | self.manager.set_fit_range(page= self,qmin= self.qmin_x, qmax= self.qmax_x) |
---|
810 | |
---|
811 | #single fit |
---|
812 | self.manager.onFit() |
---|
813 | ## allow stopping the fit |
---|
814 | #if self.engine_type=="scipy": |
---|
815 | # self.btFit.SetLabel("Stop") |
---|
816 | # self.btFit.Unbind(event=wx.EVT_BUTTON, id= self.btFit.GetId()) |
---|
817 | # self.btFit.Bind(event= wx.EVT_BUTTON, handler=self._StopFit, id=self.btFit.GetId()) |
---|
818 | #else: |
---|
819 | # self.btFit.SetLabel("Fit") |
---|
820 | # self.btFit.Bind(event= wx.EVT_BUTTON, handler=self._onFit, id=self.btFit.GetId()) |
---|
821 | |
---|
822 | |
---|
823 | |
---|
824 | def _StopFit(self, event): |
---|
825 | """ |
---|
826 | Stop fit |
---|
827 | """ |
---|
828 | self.btFit.SetLabel("Fit") |
---|
829 | if self.engine_type=="scipy": |
---|
830 | self.manager.stop_fit() |
---|
831 | self.btFit.Unbind(event=wx.EVT_BUTTON, id=self.btFit.GetId()) |
---|
832 | self.btFit.Bind(event=wx.EVT_BUTTON, handler=self._onFit,id=self.btFit.GetId()) |
---|
833 | |
---|
834 | |
---|
835 | def _on_select_model(self, event): |
---|
836 | """ |
---|
837 | call back for model selection |
---|
838 | """ |
---|
839 | self._on_select_model_helper() |
---|
840 | self.set_model_param_sizer(self.model) |
---|
841 | |
---|
842 | self.enable_disp.SetValue(False) |
---|
843 | self.disable_disp.SetValue(True) |
---|
844 | try: |
---|
845 | self.set_dispers_sizer() |
---|
846 | except: |
---|
847 | pass |
---|
848 | self.btFit.SetFocus() |
---|
849 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
850 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
851 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
852 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
853 | |
---|
854 | self.state.structurecombobox = self.structurebox.GetCurrentSelection() |
---|
855 | self.state.formfactorcombobox = self.formfactorbox.GetCurrentSelection() |
---|
856 | |
---|
857 | if self.model != None: |
---|
858 | try: |
---|
859 | temp_smear= None |
---|
860 | if self.enable_smearer.GetValue(): |
---|
861 | temp_smear= self.smearer |
---|
862 | #self.compute_chisqr(temp_smear) |
---|
863 | except: |
---|
864 | ## error occured on chisqr computation |
---|
865 | pass |
---|
866 | if self.data is not None and self.data.__class__.__name__ !="Data2D": |
---|
867 | ## set smearing value whether or not the data contain the smearing info |
---|
868 | evt = ModelEventbox(model=self.model, |
---|
869 | smearer=temp_smear, |
---|
870 | qmin= float(self.qmin_x), |
---|
871 | qmax= float(self.qmax_x)) |
---|
872 | else: |
---|
873 | evt = ModelEventbox(model=self.model) |
---|
874 | wx.PostEvent(self.event_owner, evt) |
---|
875 | |
---|
876 | if event !=None: |
---|
877 | #self._undo.Enable(True) |
---|
878 | ## post state to fit panel |
---|
879 | event = PageInfoEvent(page = self) |
---|
880 | wx.PostEvent(self.parent, event) |
---|
881 | |
---|
882 | |
---|
883 | def _onparamEnter(self,event): |
---|
884 | """ |
---|
885 | when enter value on panel redraw model according to changed |
---|
886 | """ |
---|
887 | if self.model ==None: |
---|
888 | msg="Please select a Model first..." |
---|
889 | wx.MessageBox(msg, 'Info') |
---|
890 | #wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
891 | # "Parameters: %s"%msg)) |
---|
892 | return |
---|
893 | |
---|
894 | #default flag |
---|
895 | flag = False |
---|
896 | self.fitrange = True |
---|
897 | #get event object |
---|
898 | tcrtl= event.GetEventObject() |
---|
899 | wx.PostEvent(self.manager.parent, StatusEvent(status=" \ |
---|
900 | updating ... ",type="update")) |
---|
901 | #Clear msg if previously shown. |
---|
902 | msg= "" |
---|
903 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
904 | |
---|
905 | if check_float(tcrtl): |
---|
906 | flag = self._onparamEnter_helper() |
---|
907 | self.set_npts2fit() |
---|
908 | if self.fitrange: |
---|
909 | temp_smearer = None |
---|
910 | if not self.disable_smearer.GetValue(): |
---|
911 | temp_smearer= self.current_smearer |
---|
912 | ## set smearing value whether or not the data contain the smearing info |
---|
913 | if self.slit_smearer.GetValue(): |
---|
914 | flag1 = self.update_slit_smear() |
---|
915 | flag = flag or flag1 |
---|
916 | elif self.pinhole_smearer.GetValue(): |
---|
917 | flag1 = self.update_pinhole_smear() |
---|
918 | flag = flag or flag1 |
---|
919 | elif self.data.__class__.__name__ !="Data2D": |
---|
920 | self.manager.set_smearer(smearer=temp_smearer, qmin= float(self.qmin_x), |
---|
921 | qmax= float(self.qmax_x)) |
---|
922 | if flag: |
---|
923 | #self.compute_chisqr(smearer= temp_smearer) |
---|
924 | |
---|
925 | ## new state posted |
---|
926 | if self.state_change: |
---|
927 | #self._undo.Enable(True) |
---|
928 | event = PageInfoEvent(page = self) |
---|
929 | wx.PostEvent(self.parent, event) |
---|
930 | self.state_change= False |
---|
931 | else: |
---|
932 | return # invalid fit range: do nothing here: msg already displayed in validate |
---|
933 | else: |
---|
934 | self.save_current_state() |
---|
935 | msg= "Cannot Plot :Must enter a number!!! " |
---|
936 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
937 | |
---|
938 | self.save_current_state() |
---|
939 | return |
---|
940 | |
---|
941 | def _onparamRangeEnter(self, event): |
---|
942 | """ |
---|
943 | Check validity of value enter in the parameters range field |
---|
944 | """ |
---|
945 | if self.check_invalid_panel(): |
---|
946 | return |
---|
947 | tcrtl= event.GetEventObject() |
---|
948 | #Clear msg if previously shown. |
---|
949 | msg= "" |
---|
950 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
951 | # Flag to register when a parameter has changed. |
---|
952 | is_modified = False |
---|
953 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
954 | try: |
---|
955 | value = float(tcrtl.GetValue()) |
---|
956 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
957 | self._check_value_enter(self.fittable_param ,is_modified) |
---|
958 | self._check_value_enter(self.parameters ,is_modified) |
---|
959 | except: |
---|
960 | tcrtl.SetBackgroundColour("pink") |
---|
961 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
962 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
963 | return |
---|
964 | else: |
---|
965 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
966 | |
---|
967 | #self._undo.Enable(True) |
---|
968 | self.save_current_state() |
---|
969 | event = PageInfoEvent(page = self) |
---|
970 | wx.PostEvent(self.parent, event) |
---|
971 | self.state_change= False |
---|
972 | |
---|
973 | |
---|
974 | def _onQrangeEnter(self, event): |
---|
975 | """ |
---|
976 | Check validity of value enter in the Q range field |
---|
977 | """ |
---|
978 | if self.check_invalid_panel(): |
---|
979 | return |
---|
980 | tcrtl= event.GetEventObject() |
---|
981 | #Clear msg if previously shown. |
---|
982 | msg= "" |
---|
983 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
984 | # Flag to register when a parameter has changed. |
---|
985 | is_modified = False |
---|
986 | if tcrtl.GetValue().lstrip().rstrip()!="": |
---|
987 | try: |
---|
988 | value = float(tcrtl.GetValue()) |
---|
989 | tcrtl.SetBackgroundColour(wx.WHITE) |
---|
990 | |
---|
991 | # If qmin and qmax have been modified, update qmin and qmax |
---|
992 | if self._validate_qrange( self.qmin, self.qmax): |
---|
993 | tempmin = float(self.qmin.GetValue()) |
---|
994 | if tempmin != self.qmin_x: |
---|
995 | self.qmin_x = tempmin |
---|
996 | tempmax = float(self.qmax.GetValue()) |
---|
997 | if tempmax != self.qmax_x: |
---|
998 | self.qmax_x = tempmax |
---|
999 | else: |
---|
1000 | tcrtl.SetBackgroundColour("pink") |
---|
1001 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1002 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1003 | return |
---|
1004 | |
---|
1005 | except: |
---|
1006 | tcrtl.SetBackgroundColour("pink") |
---|
1007 | msg= "Model Error:wrong value entered : %s"% sys.exc_value |
---|
1008 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1009 | return |
---|
1010 | #Check if # of points for theory model are valid(>0). |
---|
1011 | # check for 2d |
---|
1012 | if self.data.__class__.__name__ =="Data2D": |
---|
1013 | # set mask |
---|
1014 | radius= numpy.sqrt( self.data.qx_data*self.data.qx_data + self.data.qy_data*self.data.qy_data ) |
---|
1015 | index_data = ((self.qmin <= radius)&(radius<= self.qmax)) |
---|
1016 | index_data = (index_data)&(self.data.mask) |
---|
1017 | index_data = (index_data)&(numpy.isfinite(self.data.data)) |
---|
1018 | if len(index_data[index_data]) < 10: |
---|
1019 | msg= "Cannot Plot :No or too little npts in that data range!!! " |
---|
1020 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1021 | return |
---|
1022 | else: |
---|
1023 | self.data.mask = index_data |
---|
1024 | self.Npts_fit.Setvalue(str(len(self.data.mask))) |
---|
1025 | else: |
---|
1026 | index_data = ((self.qmin <= self.data.x)&(self.data.x <= self.qmax)) |
---|
1027 | self.Npts_fit.SetValue(str(len(self.data.x[index_data]))) |
---|
1028 | |
---|
1029 | else: |
---|
1030 | tcrtl.SetBackgroundColour("pink") |
---|
1031 | msg= "Model Error:wrong value entered!!!" |
---|
1032 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1033 | |
---|
1034 | self._draw_model() |
---|
1035 | ##update chi2 |
---|
1036 | #self.compute_chisqr(smearer= temp_smearer) |
---|
1037 | #self._undo.Enable(True) |
---|
1038 | self.save_current_state() |
---|
1039 | event = PageInfoEvent(page = self) |
---|
1040 | wx.PostEvent(self.parent, event) |
---|
1041 | self.state_change= False |
---|
1042 | |
---|
1043 | return |
---|
1044 | def _clear_Err_on_Fit(self): |
---|
1045 | """ |
---|
1046 | hide the error text control shown |
---|
1047 | after fitting |
---|
1048 | """ |
---|
1049 | |
---|
1050 | if hasattr(self,"text2_3"): |
---|
1051 | self.text2_3.Hide() |
---|
1052 | |
---|
1053 | if len(self.parameters)>0: |
---|
1054 | for item in self.parameters: |
---|
1055 | #Skip t ifhe angle parameters if 1D data |
---|
1056 | if self.data.__class__.__name__ !="Data2D": |
---|
1057 | if item in self.orientation_params: |
---|
1058 | continue |
---|
1059 | if item in self.param_toFit: |
---|
1060 | continue |
---|
1061 | ## hide statictext +/- |
---|
1062 | if item[3]!=None and item[3].IsShown(): |
---|
1063 | item[3].Hide() |
---|
1064 | ## hide textcrtl for error after fit |
---|
1065 | if item[4]!=None and item[4].IsShown(): |
---|
1066 | item[4].Hide() |
---|
1067 | |
---|
1068 | if len(self.fittable_param)>0: |
---|
1069 | for item in self.fittable_param: |
---|
1070 | #Skip t ifhe angle parameters if 1D data |
---|
1071 | if self.data.__class__.__name__ !="Data2D": |
---|
1072 | if item in self.orientation_params: |
---|
1073 | continue |
---|
1074 | if item in self.param_toFit: |
---|
1075 | continue |
---|
1076 | ## hide statictext +/- |
---|
1077 | if item[3]!=None and item[3].IsShown(): |
---|
1078 | item[3].Hide() |
---|
1079 | ## hide textcrtl for error after fit |
---|
1080 | if item[4]!=None and item[4].IsShown(): |
---|
1081 | item[4].Hide() |
---|
1082 | return |
---|
1083 | |
---|
1084 | def _get_defult_custom_smear(self): |
---|
1085 | """ |
---|
1086 | Get the defult values for custum smearing. |
---|
1087 | """ |
---|
1088 | |
---|
1089 | # get the default values |
---|
1090 | if self.dxl == None: self.dxl = 0.0 |
---|
1091 | if self.dxw == None: self.dxw = "" |
---|
1092 | if self.dx_min == None: self.dx_min = SMEAR_SIZE_L |
---|
1093 | if self.dx_max == None: self.dx_max = SMEAR_SIZE_H |
---|
1094 | |
---|
1095 | def _get_smear_info(self): |
---|
1096 | """ |
---|
1097 | Get the smear info from data. |
---|
1098 | @return: self.smear_type, self.dq_l and self.dq_r, |
---|
1099 | respectively the type of the smear, dq_min and dq_max for pinhole smear data |
---|
1100 | while dxl and dxw for slit smear |
---|
1101 | """ |
---|
1102 | |
---|
1103 | # default |
---|
1104 | self.smear_type = None |
---|
1105 | self.dq_l = None |
---|
1106 | self.dq_r = None |
---|
1107 | data = self.data |
---|
1108 | if self.data is None: |
---|
1109 | return |
---|
1110 | elif self.data.__class__.__name__ == 'Data2D': |
---|
1111 | if data.dqx_data == None or data.dqy_data ==None: |
---|
1112 | return |
---|
1113 | elif self.smearer != None and (data.dqx_data.any()!=0) and (data.dqx_data.any()!=0): |
---|
1114 | self.smear_type = "Pinhole2d" |
---|
1115 | self.dq_l = format_number(numpy.average(data.dqx_data)) |
---|
1116 | self.dq_r = format_number(numpy.average(data.dqy_data)) |
---|
1117 | return |
---|
1118 | else: |
---|
1119 | return |
---|
1120 | |
---|
1121 | |
---|
1122 | |
---|
1123 | # check if it is pinhole smear and get min max if it is. |
---|
1124 | if data.dx != None and all(data.dx !=0): |
---|
1125 | self.smear_type = "Pinhole" |
---|
1126 | self.dq_l = data.dx[0] |
---|
1127 | self.dq_r = data.dx[-1] |
---|
1128 | |
---|
1129 | # check if it is slit smear and get min max if it is. |
---|
1130 | elif data.dxl != None or data.dxw != None: |
---|
1131 | self.smear_type = "Slit" |
---|
1132 | if data.dxl != None and all(data.dxl !=0): |
---|
1133 | self.dq_l = data.dxl[0] |
---|
1134 | if data.dxw != None and all(data.dxw !=0): |
---|
1135 | self.dq_r = data.dxw[0] |
---|
1136 | |
---|
1137 | #return self.smear_type,self.dq_l,self.dq_r |
---|
1138 | |
---|
1139 | def _show_smear_sizer(self): |
---|
1140 | """ |
---|
1141 | Show only the sizers depending on smear selection |
---|
1142 | """ |
---|
1143 | # smear disabled |
---|
1144 | if self.disable_smearer.GetValue(): |
---|
1145 | self.smear_description_none.Show(True) |
---|
1146 | # 2Dsmear |
---|
1147 | elif self._is_2D(): |
---|
1148 | self.smear_description_accuracy_type.Show(True) |
---|
1149 | self.smear_accuracy.Show(True) |
---|
1150 | self.smear_description_accuracy_type.Show(True) |
---|
1151 | self.smear_description_2d.Show(True) |
---|
1152 | self.smear_description_2d_x.Show(True) |
---|
1153 | self.smear_description_2d_y.Show(True) |
---|
1154 | if self.pinhole_smearer.GetValue(): |
---|
1155 | self.smear_pinhole_min.Show(True) |
---|
1156 | self.smear_pinhole_max.Show(True) |
---|
1157 | # smear from data |
---|
1158 | elif self.enable_smearer.GetValue(): |
---|
1159 | |
---|
1160 | self.smear_description_dqdata.Show(True) |
---|
1161 | if self.smear_type != None: |
---|
1162 | self.smear_description_smear_type.Show(True) |
---|
1163 | if self.smear_type == 'Slit': |
---|
1164 | self.smear_description_slit_height.Show(True) |
---|
1165 | self.smear_description_slit_width.Show(True) |
---|
1166 | elif self.smear_type == 'Pinhole': |
---|
1167 | self.smear_description_pin_min.Show(True) |
---|
1168 | self.smear_description_pin_max.Show(True) |
---|
1169 | self.smear_description_smear_type.Show(True) |
---|
1170 | self.smear_description_type.Show(True) |
---|
1171 | self.smear_data_left.Show(True) |
---|
1172 | self.smear_data_right.Show(True) |
---|
1173 | # custom pinhole smear |
---|
1174 | elif self.pinhole_smearer.GetValue(): |
---|
1175 | if self.smear_type == 'Pinhole': |
---|
1176 | self.smear_message_new_p.Show(True) |
---|
1177 | self.smear_description_pin_min.Show(True) |
---|
1178 | self.smear_description_pin_max.Show(True) |
---|
1179 | |
---|
1180 | self.smear_pinhole_min.Show(True) |
---|
1181 | self.smear_pinhole_max.Show(True) |
---|
1182 | # custom slit smear |
---|
1183 | elif self.slit_smearer.GetValue(): |
---|
1184 | self.smear_message_new_s.Show(True) |
---|
1185 | self.smear_description_slit_height.Show(True) |
---|
1186 | self.smear_slit_height.Show(True) |
---|
1187 | self.smear_description_slit_width.Show(True) |
---|
1188 | self.smear_slit_width.Show(True) |
---|
1189 | |
---|
1190 | def _hide_all_smear_info(self): |
---|
1191 | """ |
---|
1192 | Hide all smearing messages in the set_smearer sizer |
---|
1193 | """ |
---|
1194 | self.smear_description_none.Hide() |
---|
1195 | self.smear_description_dqdata.Hide() |
---|
1196 | self.smear_description_type.Hide() |
---|
1197 | self.smear_description_smear_type.Hide() |
---|
1198 | self.smear_description_accuracy_type.Hide() |
---|
1199 | self.smear_description_2d_x.Hide() |
---|
1200 | self.smear_description_2d_y.Hide() |
---|
1201 | self.smear_description_2d.Hide() |
---|
1202 | |
---|
1203 | self.smear_accuracy.Hide() |
---|
1204 | self.smear_data_left.Hide() |
---|
1205 | self.smear_data_right.Hide() |
---|
1206 | self.smear_description_pin_min.Hide() |
---|
1207 | self.smear_pinhole_min.Hide() |
---|
1208 | self.smear_description_pin_max.Hide() |
---|
1209 | self.smear_pinhole_max.Hide() |
---|
1210 | self.smear_description_slit_height.Hide() |
---|
1211 | self.smear_slit_height.Hide() |
---|
1212 | self.smear_description_slit_width.Hide() |
---|
1213 | self.smear_slit_width.Hide() |
---|
1214 | self.smear_message_new_p.Hide() |
---|
1215 | self.smear_message_new_s.Hide() |
---|
1216 | |
---|
1217 | def _set_accuracy_list(self): |
---|
1218 | """ |
---|
1219 | Set the list of an accuracy in 2D custum smear: Xhigh, High, Med, or Low |
---|
1220 | """ |
---|
1221 | # list of accuracy choices |
---|
1222 | list = ['Low','Med','High','Xhigh'] |
---|
1223 | for idx in range(len(list)): |
---|
1224 | self.smear_accuracy.Append(list[idx],idx) |
---|
1225 | |
---|
1226 | def _on_select_accuracy(self,event): |
---|
1227 | """ |
---|
1228 | Select an accuracy in 2D custom smear: Xhigh, High, Med, or Low |
---|
1229 | """ |
---|
1230 | #event.Skip() |
---|
1231 | |
---|
1232 | # Check if the accuracy is same as before |
---|
1233 | |
---|
1234 | #self.smear2d_accuracy = event.GetEventObject().GetValue() |
---|
1235 | self.smear2d_accuracy = self.smear_accuracy.GetValue() |
---|
1236 | if self.pinhole_smearer.GetValue(): |
---|
1237 | self.onPinholeSmear(event=None) |
---|
1238 | else: |
---|
1239 | self.onSmear(event=None) |
---|
1240 | if self.current_smearer != None: |
---|
1241 | self.current_smearer.set_accuracy(accuracy = self.smear2d_accuracy) |
---|
1242 | |
---|
1243 | event.Skip() |
---|
1244 | |
---|
1245 | def _onMask(self, event): |
---|
1246 | """ |
---|
1247 | Build a panel to allow to edit Mask |
---|
1248 | """ |
---|
1249 | |
---|
1250 | from sans.guiframe.local_perspectives.plotting.masking import MaskPanel as MaskDialog |
---|
1251 | |
---|
1252 | self.panel = MaskDialog(self, data=self.data,id =-1 ) |
---|
1253 | self.panel.Bind(wx.EVT_CLOSE, self._draw_masked_model) |
---|
1254 | self.panel.ShowModal() |
---|
1255 | #wx.PostEvent(self.parent, event) |
---|
1256 | |
---|
1257 | def _draw_masked_model(self,event): |
---|
1258 | """ |
---|
1259 | Draw model image w/mask |
---|
1260 | """ |
---|
1261 | event.Skip() |
---|
1262 | |
---|
1263 | is_valid_qrange = self._update_paramv_on_fit() |
---|
1264 | |
---|
1265 | if is_valid_qrange: |
---|
1266 | # try re draw the model plot if it exists |
---|
1267 | self._draw_model() |
---|
1268 | self.panel.Destroy() # frame |
---|
1269 | self.set_npts2fit() |
---|
1270 | elif self.model == None: |
---|
1271 | self.panel.Destroy() |
---|
1272 | self.set_npts2fit() |
---|
1273 | msg= "No model is found on updating MASK in the model plot... " |
---|
1274 | wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) |
---|
1275 | else: |
---|
1276 | msg = ' Please consider your Q range, too.' |
---|
1277 | self.panel.ShowMessage(msg) |
---|
1278 | |
---|
1279 | def set_data(self, data): |
---|
1280 | """ |
---|
1281 | reset the current data |
---|
1282 | """ |
---|
1283 | self.data = data |
---|
1284 | |
---|
1285 | |
---|
1286 | if self.data is None: |
---|
1287 | data_min = "" |
---|
1288 | data_max = "" |
---|
1289 | data_name = "" |
---|
1290 | self.formfactorbox.Disable() |
---|
1291 | self.structurebox.Disable() |
---|
1292 | else: |
---|
1293 | self.smearer = smear_selection( self.data ) |
---|
1294 | self.disable_smearer.SetValue(True) |
---|
1295 | if self.smearer == None: |
---|
1296 | self.enable_smearer.Disable() |
---|
1297 | else: |
---|
1298 | self.enable_smearer.Enable() |
---|
1299 | |
---|
1300 | # more disables for 2D |
---|
1301 | if self.data.__class__.__name__ =="Data2D": |
---|
1302 | self.slit_smearer.Disable() |
---|
1303 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
1304 | |
---|
1305 | |
---|
1306 | self.formfactorbox.Enable() |
---|
1307 | self.structurebox.Enable() |
---|
1308 | data_name = self.data.name |
---|
1309 | #set maximum range for x in linear scale |
---|
1310 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
1311 | # Minimum value of data |
---|
1312 | data_min = min(self.data.x) |
---|
1313 | # Maximum value of data |
---|
1314 | data_max = max(self.data.x) |
---|
1315 | #number of total data points |
---|
1316 | self.Npts_total.SetValue(str(len(self.data.x))) |
---|
1317 | #default:number of data points selected to fit |
---|
1318 | self.Npts_fit.SetValue(str(len(self.data.x))) |
---|
1319 | self.btEditMask.Disable() |
---|
1320 | self.EditMask_title.Disable() |
---|
1321 | else: |
---|
1322 | ## Minimum value of data |
---|
1323 | data_min = 0 |
---|
1324 | x = max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
1325 | y = max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
1326 | ## Maximum value of data |
---|
1327 | data_max = math.sqrt(x*x + y*y) |
---|
1328 | #number of total data points |
---|
1329 | self.Npts_total.SetValue(str(len(self.data.data))) |
---|
1330 | #default:number of data points selected to fit |
---|
1331 | self.Npts_fit.SetValue(str(len(self.data.data))) |
---|
1332 | self.btEditMask.Enable() |
---|
1333 | self.EditMask_title.Enable() |
---|
1334 | |
---|
1335 | |
---|
1336 | self.dataSource.SetValue(data_name) |
---|
1337 | self.qmin_x = data_min |
---|
1338 | self.qmax_x = data_max |
---|
1339 | self.minimum_q.SetValue(str(data_min)) |
---|
1340 | self.maximum_q.SetValue(str(data_max)) |
---|
1341 | self.qmin.SetValue(str(data_min)) |
---|
1342 | self.qmax.SetValue(str(data_max)) |
---|
1343 | self.qmin.SetBackgroundColour("white") |
---|
1344 | self.qmax.SetBackgroundColour("white") |
---|
1345 | self.state.data = data |
---|
1346 | self.state.qmin = self.qmin_x |
---|
1347 | self.state.qmax = self.qmax_x |
---|
1348 | |
---|
1349 | |
---|
1350 | def reset_page(self, state,first=False): |
---|
1351 | """ |
---|
1352 | reset the state |
---|
1353 | """ |
---|
1354 | self.reset_page_helper(state) |
---|
1355 | import sans.guiframe.gui_manager |
---|
1356 | evt = ModelEventbox(model=state.model) |
---|
1357 | wx.PostEvent(self.event_owner, evt) |
---|
1358 | |
---|
1359 | if self.engine_type != None: |
---|
1360 | self.manager._on_change_engine(engine=self.engine_type) |
---|
1361 | |
---|
1362 | self.select_param(event = None) |
---|
1363 | #Save state_fit |
---|
1364 | self.save_current_state_fit() |
---|
1365 | self._lay_out() |
---|
1366 | self.Refresh() |
---|
1367 | |
---|
1368 | def get_range(self): |
---|
1369 | """ |
---|
1370 | return the fitting range |
---|
1371 | """ |
---|
1372 | return float(self.qmin_x) , float(self.qmax_x) |
---|
1373 | |
---|
1374 | def get_npts2fit(self): |
---|
1375 | """ |
---|
1376 | return numbers of data points within qrange |
---|
1377 | Note: This is for Park where chi2 is not normalized by Npts of fit |
---|
1378 | """ |
---|
1379 | npts2fit = 0 |
---|
1380 | qmin,qmax = self.get_range() |
---|
1381 | if self.data.__class__.__name__ =="Data2D": |
---|
1382 | radius= numpy.sqrt( self.data.qx_data*self.data.qx_data + self.data.qy_data*self.data.qy_data ) |
---|
1383 | index_data = (self.qmin_x <= radius)&(radius<= self.qmax_x) |
---|
1384 | index_data= (index_data)&(self.data.mask) |
---|
1385 | index_data = (index_data)&(numpy.isfinite(self.data.data)) |
---|
1386 | npts2fit = len(self.data.data[index_data]) |
---|
1387 | else: |
---|
1388 | for qx in self.data.x: |
---|
1389 | if qx >= qmin and qx <= qmax: |
---|
1390 | npts2fit += 1 |
---|
1391 | return npts2fit |
---|
1392 | |
---|
1393 | def set_npts2fit(self): |
---|
1394 | """ |
---|
1395 | setValue Npts for fitting |
---|
1396 | """ |
---|
1397 | self.Npts_fit.SetValue(str(self.get_npts2fit())) |
---|
1398 | |
---|
1399 | |
---|
1400 | def get_chi2(self): |
---|
1401 | """ |
---|
1402 | return the current chi2 |
---|
1403 | """ |
---|
1404 | return self.tcChi.GetValue() |
---|
1405 | |
---|
1406 | def get_param_list(self): |
---|
1407 | """ |
---|
1408 | @return self.param_toFit: list containing references to TextCtrl |
---|
1409 | checked.Theses TextCtrl will allow reference to parameters to fit. |
---|
1410 | @raise: if return an empty list of parameter fit will nnote work |
---|
1411 | properly so raise ValueError,"missing parameter to fit" |
---|
1412 | """ |
---|
1413 | if self.param_toFit !=[]: |
---|
1414 | return self.param_toFit |
---|
1415 | else: |
---|
1416 | raise ValueError,"missing parameter to fit" |
---|
1417 | |
---|
1418 | def onsetValues(self,chisqr,p_name, out,cov): |
---|
1419 | """ |
---|
1420 | Build the panel from the fit result |
---|
1421 | @param chisqr:Value of the goodness of fit metric |
---|
1422 | @p_name: the name of parameters |
---|
1423 | @param out:list of parameter with the best value found during fitting |
---|
1424 | @param cov:Covariance matrix |
---|
1425 | |
---|
1426 | """ |
---|
1427 | if out == None or not numpy.isfinite(chisqr): |
---|
1428 | raise ValueError,"Fit error occured..." |
---|
1429 | |
---|
1430 | is_modified = False |
---|
1431 | has_error = False |
---|
1432 | |
---|
1433 | #Hide textctrl boxes of errors. |
---|
1434 | self._clear_Err_on_Fit() |
---|
1435 | |
---|
1436 | #Check if chi2 is finite |
---|
1437 | if chisqr != None or numpy.isfinite(chisqr): |
---|
1438 | #format chi2 |
---|
1439 | if self.engine_type == "park": |
---|
1440 | npt_fit = float(self.get_npts2fit()) |
---|
1441 | if npt_fit > 0: |
---|
1442 | chisqr =chisqr/npt_fit |
---|
1443 | chi2 = format_number(chisqr) |
---|
1444 | self.tcChi.SetValue(chi2) |
---|
1445 | self.tcChi.Refresh() |
---|
1446 | else: |
---|
1447 | self.tcChi.SetValue("-") |
---|
1448 | |
---|
1449 | #Hide error title |
---|
1450 | if self.text2_3.IsShown(): |
---|
1451 | self.text2_3.Hide() |
---|
1452 | |
---|
1453 | try: |
---|
1454 | n = self.disp_box.GetCurrentSelection() |
---|
1455 | dispersity= self.disp_box.GetClientData(n) |
---|
1456 | if dispersity !=None and self.enable_disp.GetValue(): |
---|
1457 | name= dispersity.__name__ |
---|
1458 | if name == "GaussianDispersion": |
---|
1459 | if hasattr(self,"text_disp_1" ): |
---|
1460 | if self.text_disp_1 !=None: |
---|
1461 | self.text_disp_1.Hide() |
---|
1462 | except: |
---|
1463 | dispersty = None |
---|
1464 | pass |
---|
1465 | #set the panel when fit result are float not list |
---|
1466 | if out.__class__== numpy.float64: |
---|
1467 | self.param_toFit[0][2].SetValue(format_number(out)) |
---|
1468 | |
---|
1469 | if self.param_toFit[0][4].IsShown: |
---|
1470 | self.param_toFit[0][4].Hide() |
---|
1471 | if cov !=None : |
---|
1472 | self.text2_3.Show(True) |
---|
1473 | try: |
---|
1474 | if dispersity !=None: |
---|
1475 | name= dispersity.__name__ |
---|
1476 | if name == "GaussianDispersion" and self.enable_disp.GetValue(): |
---|
1477 | if hasattr(self,"text_disp_1" ): |
---|
1478 | if self.text_disp_1 !=None: |
---|
1479 | self.text_disp_1.Show(True) |
---|
1480 | except: |
---|
1481 | pass |
---|
1482 | |
---|
1483 | if cov[0]==None or not numpy.isfinite(cov[0]): |
---|
1484 | if self.param_toFit[0][3].IsShown: |
---|
1485 | self.param_toFit[0][3].Hide() |
---|
1486 | else: |
---|
1487 | self.param_toFit[0][3].Show(True) |
---|
1488 | self.param_toFit[0][4].Show(True) |
---|
1489 | self.param_toFit[0][4].SetValue(format_number(cov[0])) |
---|
1490 | has_error = True |
---|
1491 | else: |
---|
1492 | |
---|
1493 | i = 0 |
---|
1494 | #Set the panel when fit result are list |
---|
1495 | for item in self.param_toFit: |
---|
1496 | if len(item)>5 and item != None: |
---|
1497 | ## reset error value to initial state |
---|
1498 | item[3].Hide() |
---|
1499 | item[4].Hide() |
---|
1500 | |
---|
1501 | for ind in range(len(out)): |
---|
1502 | |
---|
1503 | if item[1] == p_name[ind]: |
---|
1504 | break |
---|
1505 | if len(out)<=len(self.param_toFit) and out[ind] !=None: |
---|
1506 | val_out = format_number(out[ind]) |
---|
1507 | item[2].SetValue(val_out) |
---|
1508 | |
---|
1509 | if(cov !=None): |
---|
1510 | |
---|
1511 | try: |
---|
1512 | if dispersity !=None: |
---|
1513 | name= dispersity.__name__ |
---|
1514 | if name == "GaussianDispersion" and self.enable_disp.GetValue(): |
---|
1515 | if hasattr(self,"text_disp_1" ): |
---|
1516 | if self.text_disp_1!=None: |
---|
1517 | if not self.text_disp_1.IsShown(): |
---|
1518 | self.text_disp_1.Show(True) |
---|
1519 | except: |
---|
1520 | pass |
---|
1521 | |
---|
1522 | if cov[ind]!=None : |
---|
1523 | if numpy.isfinite(float(cov[ind])): |
---|
1524 | val_err = format_number(cov[ind]) |
---|
1525 | item[3].Show(True) |
---|
1526 | item[4].Show(True) |
---|
1527 | item[4].SetValue(val_err) |
---|
1528 | |
---|
1529 | has_error = True |
---|
1530 | i += 1 |
---|
1531 | #Show error title when any errors displayed |
---|
1532 | if has_error: |
---|
1533 | if not self.text2_3.IsShown(): |
---|
1534 | self.text2_3.Show(True) |
---|
1535 | |
---|
1536 | ## save current state |
---|
1537 | self.save_current_state() |
---|
1538 | #plot model |
---|
1539 | self._draw_model() |
---|
1540 | self._lay_out() |
---|
1541 | #PostStatusEvent |
---|
1542 | msg = "Fit completed! " |
---|
1543 | wx.PostEvent(self.manager.parent, StatusEvent(status=msg)) |
---|
1544 | |
---|
1545 | def onPinholeSmear(self, event): |
---|
1546 | """ |
---|
1547 | Create a custom pinhole smear object that will change the way residuals |
---|
1548 | are compute when fitting |
---|
1549 | @ accuracy: given by strings'High','Med', 'Low' FOR 2d, None for 1D |
---|
1550 | """ |
---|
1551 | |
---|
1552 | if self.check_invalid_panel(): |
---|
1553 | return |
---|
1554 | if self.model ==None: |
---|
1555 | self.disable_smearer.SetValue(True) |
---|
1556 | msg="Please select a Model first..." |
---|
1557 | wx.MessageBox(msg, 'Info') |
---|
1558 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
1559 | "Smear: %s"%msg)) |
---|
1560 | return |
---|
1561 | |
---|
1562 | # Need update param values |
---|
1563 | self._update_paramv_on_fit() |
---|
1564 | |
---|
1565 | # msg default |
---|
1566 | msg = None |
---|
1567 | if event != None: |
---|
1568 | tcrtl= event.GetEventObject() |
---|
1569 | # event case of radio button |
---|
1570 | if tcrtl.GetValue()== True: |
---|
1571 | self.dx_min = 0.0 |
---|
1572 | self.dx_max = 0.0 |
---|
1573 | is_new_pinhole = True |
---|
1574 | else: |
---|
1575 | is_new_pinhole = self._is_changed_pinhole() |
---|
1576 | else: |
---|
1577 | is_new_pinhole = True |
---|
1578 | # if any value is changed |
---|
1579 | if is_new_pinhole: |
---|
1580 | msg = self._set_pinhole_smear() |
---|
1581 | # hide all silt sizer |
---|
1582 | self._hide_all_smear_info() |
---|
1583 | |
---|
1584 | ##Calculate chi2 |
---|
1585 | temp_smearer = self.current_smearer |
---|
1586 | #self.compute_chisqr(smearer= temp_smearer) |
---|
1587 | |
---|
1588 | # show relevant slit sizers |
---|
1589 | self._show_smear_sizer() |
---|
1590 | |
---|
1591 | self.sizer_set_smearer.Layout() |
---|
1592 | self.Layout() |
---|
1593 | |
---|
1594 | if event != None: |
---|
1595 | event.Skip() |
---|
1596 | #self._undo.Enable(True) |
---|
1597 | self.save_current_state() |
---|
1598 | event = PageInfoEvent(page = self) |
---|
1599 | wx.PostEvent(self.parent, event) |
---|
1600 | |
---|
1601 | def _is_changed_pinhole(self): |
---|
1602 | """ |
---|
1603 | check if any of pinhole smear is changed |
---|
1604 | return: True or False |
---|
1605 | """ |
---|
1606 | # get the values |
---|
1607 | pin_min = self.smear_pinhole_min.GetValue() |
---|
1608 | pin_max = self.smear_pinhole_max.GetValue() |
---|
1609 | |
---|
1610 | # Check changes in slit width |
---|
1611 | try: |
---|
1612 | dx_min = float(pin_min) |
---|
1613 | except: |
---|
1614 | return True |
---|
1615 | if self.dx_min != dx_min: |
---|
1616 | return True |
---|
1617 | |
---|
1618 | # Check changes in slit heigth |
---|
1619 | try: |
---|
1620 | dx_max = float(pin_max) |
---|
1621 | except: |
---|
1622 | return True |
---|
1623 | if self.dx_max != dx_max: |
---|
1624 | return True |
---|
1625 | return False |
---|
1626 | |
---|
1627 | def _set_pinhole_smear(self): |
---|
1628 | """ |
---|
1629 | Set custom pinhole smear |
---|
1630 | return: msg |
---|
1631 | """ |
---|
1632 | # copy data |
---|
1633 | data = copy.deepcopy(self.data) |
---|
1634 | if self._is_2D(): |
---|
1635 | self.smear_type = 'Pinhole2d' |
---|
1636 | len_data = len(data.data) |
---|
1637 | data.dqx_data = numpy.zeros(len_data) |
---|
1638 | data.dqy_data = numpy.zeros(len_data) |
---|
1639 | else: |
---|
1640 | self.smear_type = 'Pinhole' |
---|
1641 | len_data = len(data.x) |
---|
1642 | data.dx = numpy.zeros(len_data) |
---|
1643 | data.dxl = None |
---|
1644 | data.dxw = None |
---|
1645 | msg = None |
---|
1646 | |
---|
1647 | get_pin_min = self.smear_pinhole_min |
---|
1648 | get_pin_max = self.smear_pinhole_max |
---|
1649 | |
---|
1650 | if not check_float(get_pin_min): |
---|
1651 | get_pin_min.SetBackgroundColour("pink") |
---|
1652 | msg= "Model Error:wrong value entered!!!" |
---|
1653 | elif not check_float(get_pin_max ): |
---|
1654 | get_pin_max.SetBackgroundColour("pink") |
---|
1655 | msg= "Model Error:wrong value entered!!!" |
---|
1656 | else: |
---|
1657 | if len_data < 2: len_data = 2 |
---|
1658 | self.dx_min = float(get_pin_min.GetValue()) |
---|
1659 | self.dx_max = float(get_pin_max.GetValue()) |
---|
1660 | if self.dx_min < 0: |
---|
1661 | get_pin_min.SetBackgroundColour("pink") |
---|
1662 | msg= "Model Error:This value can not be negative!!!" |
---|
1663 | elif self.dx_max <0: |
---|
1664 | get_pin_max.SetBackgroundColour("pink") |
---|
1665 | msg= "Model Error:This value can not be negative!!!" |
---|
1666 | elif self.dx_min != None and self.dx_max != None: |
---|
1667 | if self._is_2D(): |
---|
1668 | data.dqx_data[data.dqx_data==0] = self.dx_min |
---|
1669 | data.dqy_data[data.dqy_data==0] = self.dx_max |
---|
1670 | elif self.dx_min == self.dx_max: |
---|
1671 | data.dx[data.dx==0] = self.dx_min |
---|
1672 | else: |
---|
1673 | step = (self.dx_max - self.dx_min)/(len_data-1) |
---|
1674 | data.dx = numpy.arange(self.dx_min,self.dx_max+step/1.1,step) |
---|
1675 | elif self.dx_min != None: |
---|
1676 | if self._is_2D(): data.dqx_data[data.dqx_data==0] = self.dx_min |
---|
1677 | else: data.dx[data.dx==0] = self.dx_min |
---|
1678 | elif self.dx_max != None: |
---|
1679 | if self._is_2D(): data.dqy_data[data.dqy_data==0] = self.dx_max |
---|
1680 | else: data.dx[data.dx==0] = self.dx_max |
---|
1681 | self.current_smearer = smear_selection(data) |
---|
1682 | # 2D need to set accuracy |
---|
1683 | if self._is_2D(): |
---|
1684 | self.current_smearer.set_accuracy(accuracy = self.smear2d_accuracy) |
---|
1685 | |
---|
1686 | if msg != None: |
---|
1687 | wx.PostEvent(self.manager.parent, StatusEvent(status = msg )) |
---|
1688 | else: |
---|
1689 | get_pin_min.SetBackgroundColour("white") |
---|
1690 | get_pin_max.SetBackgroundColour("white") |
---|
1691 | ## set smearing value whether or not the data contain the smearing info |
---|
1692 | self.manager.set_smearer(smearer=self.current_smearer, qmin= float(self.qmin_x),qmax= float(self.qmax_x)) |
---|
1693 | return msg |
---|
1694 | |
---|
1695 | def update_pinhole_smear(self): |
---|
1696 | """ |
---|
1697 | called by kill_focus on pinhole TextCntrl |
---|
1698 | to update the changes |
---|
1699 | return: msg: False when wrong value was entered |
---|
1700 | """ |
---|
1701 | # msg default |
---|
1702 | msg = None |
---|
1703 | # check if any value is changed |
---|
1704 | if self._is_changed_pinhole(): |
---|
1705 | msg = self._set_pinhole_smear() |
---|
1706 | #self._undo.Enable(True) |
---|
1707 | self.save_current_state() |
---|
1708 | |
---|
1709 | if msg != None: |
---|
1710 | return False |
---|
1711 | else: |
---|
1712 | return True |
---|
1713 | |
---|
1714 | def onSlitSmear(self, event): |
---|
1715 | """ |
---|
1716 | Create a custom slit smear object that will change the way residuals |
---|
1717 | are compute when fitting |
---|
1718 | """ |
---|
1719 | |
---|
1720 | if self.check_invalid_panel(): |
---|
1721 | return |
---|
1722 | |
---|
1723 | if self.model ==None: |
---|
1724 | self.disable_smearer.SetValue(True) |
---|
1725 | |
---|
1726 | msg="Please select a Model first..." |
---|
1727 | wx.MessageBox(msg, 'Info') |
---|
1728 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
1729 | "Smear: %s"%msg)) |
---|
1730 | return |
---|
1731 | |
---|
1732 | # Need update param values |
---|
1733 | self._update_paramv_on_fit() |
---|
1734 | |
---|
1735 | # msg default |
---|
1736 | msg = None |
---|
1737 | # for event given |
---|
1738 | if event != None: |
---|
1739 | tcrtl= event.GetEventObject() |
---|
1740 | # event case of radio button |
---|
1741 | if tcrtl.GetValue(): |
---|
1742 | self.dxl = 0.0 |
---|
1743 | self.dxw = 0.0 |
---|
1744 | is_new_slit = True |
---|
1745 | else: |
---|
1746 | is_new_slit = self._is_changed_slit() |
---|
1747 | else: |
---|
1748 | is_new_slit = True |
---|
1749 | |
---|
1750 | # if any value is changed |
---|
1751 | if is_new_slit: |
---|
1752 | msg = self._set_slit_smear() |
---|
1753 | |
---|
1754 | # hide all silt sizer |
---|
1755 | self._hide_all_smear_info() |
---|
1756 | ##Calculate chi2 |
---|
1757 | #self.compute_chisqr(smearer= self.current_smearer) |
---|
1758 | # show relevant slit sizers |
---|
1759 | self._show_smear_sizer() |
---|
1760 | self.sizer_set_smearer.Layout() |
---|
1761 | self.Layout() |
---|
1762 | |
---|
1763 | if event != None: |
---|
1764 | event.Skip() |
---|
1765 | #self._undo.Enable(True) |
---|
1766 | self.save_current_state() |
---|
1767 | event = PageInfoEvent(page = self) |
---|
1768 | wx.PostEvent(self.parent, event) |
---|
1769 | if msg != None: |
---|
1770 | wx.PostEvent(self.manager.parent, StatusEvent(status = msg)) |
---|
1771 | |
---|
1772 | |
---|
1773 | def _is_changed_slit(self): |
---|
1774 | """ |
---|
1775 | check if any of slit lengths is changed |
---|
1776 | return: True or False |
---|
1777 | """ |
---|
1778 | |
---|
1779 | # get the values |
---|
1780 | width = self.smear_slit_width.GetValue() |
---|
1781 | height = self.smear_slit_height.GetValue() |
---|
1782 | |
---|
1783 | # check and change the box bg color if it was pink but it should be white now |
---|
1784 | # because this is the case that _set_slit_smear() will not handle |
---|
1785 | if height.lstrip().rstrip()=="": |
---|
1786 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
1787 | if width.lstrip().rstrip()=="": |
---|
1788 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
1789 | |
---|
1790 | # Check changes in slit width |
---|
1791 | if width == "": |
---|
1792 | dxw = 0.0 |
---|
1793 | else: |
---|
1794 | try: |
---|
1795 | dxw = float(width) |
---|
1796 | except: |
---|
1797 | return True |
---|
1798 | if self.dxw != dxw: |
---|
1799 | return True |
---|
1800 | |
---|
1801 | # Check changes in slit heigth |
---|
1802 | if height == "": |
---|
1803 | dxl = 0.0 |
---|
1804 | else: |
---|
1805 | try: |
---|
1806 | dxl = float(height) |
---|
1807 | except: |
---|
1808 | return True |
---|
1809 | if self.dxl != dxl: |
---|
1810 | return True |
---|
1811 | |
---|
1812 | return False |
---|
1813 | |
---|
1814 | def _set_slit_smear(self): |
---|
1815 | """ |
---|
1816 | Set custom slit smear |
---|
1817 | return: msg |
---|
1818 | """ |
---|
1819 | temp_smearer = None |
---|
1820 | # make sure once more if it is smearer |
---|
1821 | data = copy.deepcopy(self.data) |
---|
1822 | data_len = len(data.x) |
---|
1823 | data.dx = None |
---|
1824 | data.dxl = None |
---|
1825 | data.dxw = None |
---|
1826 | msg = None |
---|
1827 | |
---|
1828 | try: |
---|
1829 | self.dxl = float(self.smear_slit_height.GetValue()) |
---|
1830 | data.dxl = self.dxl* numpy.ones(data_len) |
---|
1831 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
1832 | except: |
---|
1833 | data.dxl = numpy.zeros(data_len) |
---|
1834 | if self.smear_slit_height.GetValue().lstrip().rstrip()!="": |
---|
1835 | self.smear_slit_height.SetBackgroundColour("pink") |
---|
1836 | msg = "Wrong value entered... " |
---|
1837 | else: |
---|
1838 | self.smear_slit_height.SetBackgroundColour(wx.WHITE) |
---|
1839 | try: |
---|
1840 | self.dxw = float(self.smear_slit_width.GetValue()) |
---|
1841 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
1842 | data.dxw = self.dxw* numpy.ones(data_len) |
---|
1843 | except: |
---|
1844 | data.dxw = numpy.zeros(data_len) |
---|
1845 | if self.smear_slit_width.GetValue().lstrip().rstrip()!="": |
---|
1846 | self.smear_slit_width.SetBackgroundColour("pink") |
---|
1847 | msg = "Wrong Fit value entered... " |
---|
1848 | else: |
---|
1849 | self.smear_slit_width.SetBackgroundColour(wx.WHITE) |
---|
1850 | |
---|
1851 | self.current_smearer = smear_selection(data) |
---|
1852 | #temp_smearer = self.current_smearer |
---|
1853 | ## set smearing value whether or not the data contain the smearing info |
---|
1854 | self.manager.set_smearer(smearer=self.current_smearer, qmin= float(self.qmin_x), qmax= float(self.qmax_x)) |
---|
1855 | |
---|
1856 | return msg |
---|
1857 | |
---|
1858 | |
---|
1859 | def update_slit_smear(self): |
---|
1860 | """ |
---|
1861 | called by kill_focus on pinhole TextCntrl |
---|
1862 | to update the changes |
---|
1863 | return: msg: False when wrong value was entered |
---|
1864 | """ |
---|
1865 | # msg default |
---|
1866 | msg = None |
---|
1867 | # check if any value is changed |
---|
1868 | if self._is_changed_slit(): |
---|
1869 | msg = self._set_slit_smear() |
---|
1870 | #self._undo.Enable(True) |
---|
1871 | self.save_current_state() |
---|
1872 | |
---|
1873 | if msg != None: |
---|
1874 | return False |
---|
1875 | else: |
---|
1876 | return True |
---|
1877 | |
---|
1878 | |
---|
1879 | def onSmear(self, event): |
---|
1880 | """ |
---|
1881 | Create a smear object that will change the way residuals |
---|
1882 | are compute when fitting |
---|
1883 | """ |
---|
1884 | if event != None: |
---|
1885 | event.Skip() |
---|
1886 | if self.check_invalid_panel(): |
---|
1887 | return |
---|
1888 | if self.model ==None: |
---|
1889 | self.disable_smearer.SetValue(True) |
---|
1890 | msg="Please select a Model first..." |
---|
1891 | wx.MessageBox(msg, 'Info') |
---|
1892 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
1893 | "Smear: %s"%msg)) |
---|
1894 | return |
---|
1895 | |
---|
1896 | # Need update param values |
---|
1897 | self._update_paramv_on_fit() |
---|
1898 | |
---|
1899 | temp_smearer = None |
---|
1900 | self._get_smear_info() |
---|
1901 | |
---|
1902 | #renew smear sizer |
---|
1903 | if self.smear_type != None: |
---|
1904 | self.smear_description_smear_type.SetValue(str(self.smear_type)) |
---|
1905 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
1906 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
1907 | |
---|
1908 | self._hide_all_smear_info() |
---|
1909 | |
---|
1910 | data = copy.deepcopy(self.data) |
---|
1911 | # make sure once more if it is smearer |
---|
1912 | self.current_smearer = smear_selection(data) |
---|
1913 | |
---|
1914 | if self.enable_smearer.GetValue(): |
---|
1915 | if hasattr(self.data,"dxl"): |
---|
1916 | |
---|
1917 | msg= ": Resolution smearing parameters" |
---|
1918 | if hasattr(self.data,"dxw"): |
---|
1919 | msg= ": Slit smearing parameters" |
---|
1920 | if self.smearer ==None: |
---|
1921 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
1922 | "Data contains no smearing information")) |
---|
1923 | else: |
---|
1924 | wx.PostEvent(self.manager.parent, StatusEvent(status=\ |
---|
1925 | "Data contains smearing information")) |
---|
1926 | |
---|
1927 | #self.smear_description_dqdata.Show(True) |
---|
1928 | self.smear_data_left.Show(True) |
---|
1929 | self.smear_data_right.Show(True) |
---|
1930 | temp_smearer= self.current_smearer |
---|
1931 | elif self.disable_smearer.GetValue(): |
---|
1932 | self.smear_description_none.Show(True) |
---|
1933 | |
---|
1934 | self._show_smear_sizer() |
---|
1935 | |
---|
1936 | self.sizer_set_smearer.Layout() |
---|
1937 | self.Layout() |
---|
1938 | ## set smearing value whether or not the data contain the smearing info |
---|
1939 | self.manager.set_smearer(smearer=temp_smearer, qmin= float(self.qmin_x), |
---|
1940 | qmax= float(self.qmax_x)) |
---|
1941 | |
---|
1942 | ##Calculate chi2 |
---|
1943 | #self.compute_chisqr(smearer= temp_smearer) |
---|
1944 | |
---|
1945 | self.state.enable_smearer= self.enable_smearer.GetValue() |
---|
1946 | self.state.disable_smearer=self.disable_smearer.GetValue() |
---|
1947 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
1948 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
1949 | |
---|
1950 | def on_complete_chisqr(self, event): |
---|
1951 | """ |
---|
1952 | print result chisqr |
---|
1953 | @event: activated by fitting/ complete after draw |
---|
1954 | """ |
---|
1955 | try: |
---|
1956 | if event ==None: |
---|
1957 | output= "-" |
---|
1958 | else: |
---|
1959 | output = event.output |
---|
1960 | self.tcChi.SetValue(str(format_number(output))) |
---|
1961 | |
---|
1962 | self.state.tcChi =self.tcChi |
---|
1963 | except: |
---|
1964 | pass |
---|
1965 | |
---|
1966 | def complete_chisqr(self, output, elapsed=None): |
---|
1967 | """ |
---|
1968 | print result chisqr |
---|
1969 | """ |
---|
1970 | try: |
---|
1971 | if output ==None: |
---|
1972 | output= "-" |
---|
1973 | |
---|
1974 | self.tcChi.SetValue(str(format_number(output))) |
---|
1975 | |
---|
1976 | self.state.tcChi =self.tcChi |
---|
1977 | |
---|
1978 | except: |
---|
1979 | pass |
---|
1980 | |
---|
1981 | |
---|
1982 | def compute_chisqr1D(self, smearer=None): |
---|
1983 | """ |
---|
1984 | Compute chisqr for 1D |
---|
1985 | """ |
---|
1986 | try: |
---|
1987 | self.qmin_x = float(self.qmin.GetValue()) |
---|
1988 | self.qmax_x = float(self.qmax.GetValue()) |
---|
1989 | ##return residuals within self.qmin_x and self.qmax_x |
---|
1990 | from gui_thread import CalcChisqr1D |
---|
1991 | ## If a thread is already started, stop it |
---|
1992 | if self.calc_Chisqr!= None and self.calc_Chisqr.isrunning(): |
---|
1993 | self.calc_Chisqr.stop() |
---|
1994 | |
---|
1995 | self.calc_Chisqr= CalcChisqr1D( data1d= self.data, |
---|
1996 | model= self.model, |
---|
1997 | smearer=smearer, |
---|
1998 | qmin=self.qmin_x, |
---|
1999 | qmax=self.qmax_x, |
---|
2000 | completefn = self.complete_chisqr, |
---|
2001 | updatefn = None) |
---|
2002 | |
---|
2003 | self.calc_Chisqr.queue() |
---|
2004 | |
---|
2005 | except: |
---|
2006 | raise ValueError," Could not compute Chisqr for %s Model 2D: "%self.model.name |
---|
2007 | |
---|
2008 | |
---|
2009 | |
---|
2010 | |
---|
2011 | |
---|
2012 | def compute_chisqr2D(self,smearer=None): |
---|
2013 | """ |
---|
2014 | compute chi square given a model and data 2D and set the value |
---|
2015 | to the tcChi txtcrl |
---|
2016 | """ |
---|
2017 | try: |
---|
2018 | self.qmin_x = float(self.qmin.GetValue()) |
---|
2019 | self.qmax_x = float(self.qmax.GetValue()) |
---|
2020 | |
---|
2021 | ##return residuals within self.qmin_x and self.qmax_x |
---|
2022 | from gui_thread import CalcChisqr2D |
---|
2023 | ## If a thread is already started, stop it |
---|
2024 | if self.calc_Chisqr!= None and self.calc_Chisqr.isrunning(): |
---|
2025 | self.calc_Chisqr.stop() |
---|
2026 | if smearer !=None: |
---|
2027 | smearer.set_accuracy(accuracy = self.smear2d_accuracy) |
---|
2028 | self.calc_Chisqr= CalcChisqr2D( data2d= self.data, |
---|
2029 | model= self.model, |
---|
2030 | smearer=smearer, |
---|
2031 | qmin= self.qmin_x, |
---|
2032 | qmax = self.qmax_x, |
---|
2033 | completefn = self.complete_chisqr, |
---|
2034 | updatefn = None) |
---|
2035 | |
---|
2036 | self.calc_Chisqr.queue() |
---|
2037 | |
---|
2038 | except: |
---|
2039 | raise |
---|
2040 | |
---|
2041 | |
---|
2042 | def compute_chisqr(self ,output=None, smearer=None): |
---|
2043 | """ |
---|
2044 | compute chi square given a model and data 1D and set the value |
---|
2045 | to the tcChi txtcrl |
---|
2046 | """ |
---|
2047 | flag = self._validate_qrange( self.qmin, self.qmax) |
---|
2048 | if flag== True: |
---|
2049 | try: |
---|
2050 | if hasattr(self.data,"data"): |
---|
2051 | self.compute_chisqr2D(smearer=smearer) |
---|
2052 | return |
---|
2053 | else: |
---|
2054 | self.compute_chisqr1D(smearer=smearer) |
---|
2055 | return |
---|
2056 | except: |
---|
2057 | wx.PostEvent(self.parent.parent, StatusEvent(status=\ |
---|
2058 | "Chisqr Error: %s"% sys.exc_value)) |
---|
2059 | return |
---|
2060 | |
---|
2061 | |
---|
2062 | def select_all_param(self,event): |
---|
2063 | """ |
---|
2064 | set to true or false all checkBox given the main checkbox value cb1 |
---|
2065 | """ |
---|
2066 | |
---|
2067 | self.param_toFit=[] |
---|
2068 | if self.parameters !=[]: |
---|
2069 | if self.cb1.GetValue(): |
---|
2070 | for item in self.parameters: |
---|
2071 | ## for data2D select all to fit |
---|
2072 | if self.data.__class__.__name__=="Data2D": |
---|
2073 | item[0].SetValue(True) |
---|
2074 | self.param_toFit.append(item ) |
---|
2075 | else: |
---|
2076 | ## for 1D all parameters except orientation |
---|
2077 | if not item in self.orientation_params: |
---|
2078 | item[0].SetValue(True) |
---|
2079 | self.param_toFit.append(item ) |
---|
2080 | #if len(self.fittable_param)>0: |
---|
2081 | for item in self.fittable_param: |
---|
2082 | if self.data.__class__.__name__=="Data2D": |
---|
2083 | item[0].SetValue(True) |
---|
2084 | self.param_toFit.append(item ) |
---|
2085 | else: |
---|
2086 | ## for 1D all parameters except orientation |
---|
2087 | if not item in self.orientation_params_disp: |
---|
2088 | item[0].SetValue(True) |
---|
2089 | self.param_toFit.append(item ) |
---|
2090 | else: |
---|
2091 | for item in self.parameters: |
---|
2092 | item[0].SetValue(False) |
---|
2093 | for item in self.fittable_param: |
---|
2094 | item[0].SetValue(False) |
---|
2095 | self.param_toFit=[] |
---|
2096 | |
---|
2097 | self.save_current_state_fit() |
---|
2098 | |
---|
2099 | if event !=None: |
---|
2100 | #self._undo.Enable(True) |
---|
2101 | ## post state to fit panel |
---|
2102 | event = PageInfoEvent(page = self) |
---|
2103 | wx.PostEvent(self.parent, event) |
---|
2104 | |
---|
2105 | |
---|
2106 | |
---|
2107 | def select_param(self,event): |
---|
2108 | """ |
---|
2109 | Select TextCtrl checked for fitting purpose and stores them |
---|
2110 | in self.param_toFit=[] list |
---|
2111 | """ |
---|
2112 | self.param_toFit=[] |
---|
2113 | for item in self.parameters: |
---|
2114 | #Skip t ifhe angle parameters if 1D data |
---|
2115 | if self.data.__class__.__name__ !="Data2D": |
---|
2116 | if item in self.orientation_params: |
---|
2117 | continue |
---|
2118 | #Select parameters to fit for list of primary parameters |
---|
2119 | if item[0].GetValue(): |
---|
2120 | if not (item in self.param_toFit): |
---|
2121 | self.param_toFit.append(item ) |
---|
2122 | else: |
---|
2123 | #remove parameters from the fitting list |
---|
2124 | if item in self.param_toFit: |
---|
2125 | self.param_toFit.remove(item) |
---|
2126 | |
---|
2127 | #Select parameters to fit for list of fittable parameters with dispersion |
---|
2128 | for item in self.fittable_param: |
---|
2129 | #Skip t ifhe angle parameters if 1D data |
---|
2130 | if self.data.__class__.__name__ !="Data2D": |
---|
2131 | if item in self.orientation_params: |
---|
2132 | continue |
---|
2133 | if item[0].GetValue(): |
---|
2134 | if not (item in self.param_toFit): |
---|
2135 | self.param_toFit.append(item) |
---|
2136 | else: |
---|
2137 | #remove parameters from the fitting list |
---|
2138 | if item in self.param_toFit: |
---|
2139 | self.param_toFit.remove(item) |
---|
2140 | |
---|
2141 | #Calculate num. of angle parameters |
---|
2142 | if self.data.__class__.__name__ =="Data2D": |
---|
2143 | len_orient_para = 0 |
---|
2144 | else: |
---|
2145 | len_orient_para = len(self.orientation_params) #assume even len |
---|
2146 | #Total num. of angle parameters |
---|
2147 | if len(self.fittable_param) > 0: |
---|
2148 | len_orient_para *= 2 |
---|
2149 | #Set the value of checkbox that selected every checkbox or not |
---|
2150 | if len(self.parameters)+len(self.fittable_param)-len_orient_para ==len(self.param_toFit): |
---|
2151 | self.cb1.SetValue(True) |
---|
2152 | else: |
---|
2153 | self.cb1.SetValue(False) |
---|
2154 | self.save_current_state_fit() |
---|
2155 | if event !=None: |
---|
2156 | #self._undo.Enable(True) |
---|
2157 | ## post state to fit panel |
---|
2158 | event = PageInfoEvent(page = self) |
---|
2159 | wx.PostEvent(self.parent, event) |
---|
2160 | |
---|
2161 | |
---|
2162 | |
---|
2163 | def set_model_param_sizer(self, model): |
---|
2164 | """ |
---|
2165 | Build the panel from the model content |
---|
2166 | @param model: the model selected in combo box for fitting purpose |
---|
2167 | """ |
---|
2168 | self.sizer3.Clear(True) |
---|
2169 | self.parameters = [] |
---|
2170 | self.param_toFit=[] |
---|
2171 | self.fittable_param=[] |
---|
2172 | self.fixed_param=[] |
---|
2173 | self.orientation_params=[] |
---|
2174 | self.orientation_params_disp=[] |
---|
2175 | |
---|
2176 | if model ==None: |
---|
2177 | self.sizer3.Layout() |
---|
2178 | self.SetScrollbars(20,20,25,65) |
---|
2179 | return |
---|
2180 | ## the panel is drawn using the current value of the fit engine |
---|
2181 | if self.engine_type==None and self.manager !=None: |
---|
2182 | self.engine_type= self.manager._return_engine_type() |
---|
2183 | |
---|
2184 | |
---|
2185 | box_description= wx.StaticBox(self, -1,str("Model Parameters")) |
---|
2186 | boxsizer1 = wx.StaticBoxSizer(box_description, wx.VERTICAL) |
---|
2187 | sizer = wx.GridBagSizer(5,5) |
---|
2188 | ## save the current model |
---|
2189 | self.model = model |
---|
2190 | |
---|
2191 | keys = self.model.getParamList() |
---|
2192 | #list of dispersion paramaters |
---|
2193 | self.disp_list=self.model.getDispParamList() |
---|
2194 | |
---|
2195 | keys.sort() |
---|
2196 | |
---|
2197 | iy = 0 |
---|
2198 | ix = 0 |
---|
2199 | select_text = "Select All" |
---|
2200 | self.cb1 = wx.CheckBox(self, -1,str(select_text), (10, 10)) |
---|
2201 | wx.EVT_CHECKBOX(self, self.cb1.GetId(), self.select_all_param) |
---|
2202 | self.cb1.SetToolTipString("To check/uncheck all the boxes below.") |
---|
2203 | #self.cb1.SetValue(True) |
---|
2204 | |
---|
2205 | sizer.Add(self.cb1,(iy, ix),(1,1),\ |
---|
2206 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2207 | ix +=1 |
---|
2208 | self.text2_2 = wx.StaticText(self, -1, 'Values') |
---|
2209 | sizer.Add(self.text2_2,(iy, ix),(1,1),\ |
---|
2210 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2211 | ix +=2 |
---|
2212 | self.text2_3 = wx.StaticText(self, -1, 'Errors') |
---|
2213 | sizer.Add(self.text2_3,(iy, ix),(1,1),\ |
---|
2214 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2215 | self.text2_3.Hide() |
---|
2216 | ix +=1 |
---|
2217 | self.text2_min = wx.StaticText(self, -1, 'Min') |
---|
2218 | sizer.Add(self.text2_min,(iy, ix),(1,1),\ |
---|
2219 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2220 | self.text2_min.Hide() |
---|
2221 | ix +=1 |
---|
2222 | self.text2_max = wx.StaticText(self, -1, 'Max') |
---|
2223 | sizer.Add(self.text2_max,(iy, ix),(1,1),\ |
---|
2224 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2225 | self.text2_max.Hide() |
---|
2226 | ix += 1 |
---|
2227 | self.text2_4 = wx.StaticText(self, -1, '[Units]') |
---|
2228 | sizer.Add(self.text2_4,(iy, ix),(1,1),\ |
---|
2229 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2230 | self.text2_4.Hide() |
---|
2231 | if self.engine_type=="park": |
---|
2232 | self.text2_max.Show(True) |
---|
2233 | self.text2_min.Show(True) |
---|
2234 | |
---|
2235 | for item in keys: |
---|
2236 | if not item in self.disp_list and not item in self.model.orientation_params: |
---|
2237 | |
---|
2238 | ##prepare a spot to store errors |
---|
2239 | if not self.model.details.has_key(item): |
---|
2240 | self.model.details [item] = ["",None,None] |
---|
2241 | |
---|
2242 | iy += 1 |
---|
2243 | ix = 0 |
---|
2244 | ## add parameters name with checkbox for selecting to fit |
---|
2245 | cb = wx.CheckBox(self, -1, item ) |
---|
2246 | cb.SetToolTipString(" Check for fitting.") |
---|
2247 | #cb.SetValue(True) |
---|
2248 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
2249 | |
---|
2250 | sizer.Add( cb,( iy, ix),(1,1), |
---|
2251 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2252 | |
---|
2253 | ## add parameter value |
---|
2254 | ix += 1 |
---|
2255 | value= self.model.getParam(item) |
---|
2256 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
2257 | style=wx.TE_PROCESS_ENTER) |
---|
2258 | ctl1.SetToolTipString("Hit 'Enter' after typing.") |
---|
2259 | ctl1.SetValue(format_number(value)) |
---|
2260 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
2261 | ## text to show error sign |
---|
2262 | ix += 1 |
---|
2263 | text2=wx.StaticText(self, -1, '+/-') |
---|
2264 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
2265 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2266 | text2.Hide() |
---|
2267 | ix += 1 |
---|
2268 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
2269 | sizer.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2270 | ctl2.Hide() |
---|
2271 | |
---|
2272 | ix += 1 |
---|
2273 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
2274 | text_enter_callback = self._onparamRangeEnter) |
---|
2275 | |
---|
2276 | sizer.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2277 | ctl3.Hide() |
---|
2278 | |
---|
2279 | ix += 1 |
---|
2280 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
2281 | text_enter_callback = self._onparamRangeEnter) |
---|
2282 | sizer.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2283 | |
---|
2284 | ctl4.Hide() |
---|
2285 | |
---|
2286 | if self.engine_type=="park": |
---|
2287 | ctl3.Show(True) |
---|
2288 | ctl4.Show(True) |
---|
2289 | ix +=1 |
---|
2290 | # Units |
---|
2291 | if self.model.details.has_key(item): |
---|
2292 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
2293 | else: |
---|
2294 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
2295 | sizer.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2296 | |
---|
2297 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
2298 | self.parameters.append([cb,item, ctl1, |
---|
2299 | text2,ctl2, ctl3, ctl4,units]) |
---|
2300 | |
---|
2301 | iy+=1 |
---|
2302 | sizer.Add((10,10),(iy,ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2303 | |
---|
2304 | # type can be either Guassian or Array |
---|
2305 | if len(self.model.dispersion.values())>0: |
---|
2306 | type= self.model.dispersion.values()[0]["type"] |
---|
2307 | else: |
---|
2308 | type = "Gaussian" |
---|
2309 | |
---|
2310 | iy += 1 |
---|
2311 | ix = 0 |
---|
2312 | #Add tile for orientational angle |
---|
2313 | for item in keys: |
---|
2314 | if item in self.model.orientation_params: |
---|
2315 | orient_angle = wx.StaticText(self, -1, '[For 2D only]:') |
---|
2316 | sizer.Add(orient_angle,(iy, ix),(1,1), wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 15) |
---|
2317 | if not self.data.__class__.__name__ =="Data2D": |
---|
2318 | orient_angle.Hide() |
---|
2319 | else: |
---|
2320 | orient_angle.Show(True) |
---|
2321 | break |
---|
2322 | |
---|
2323 | #For Gaussian only |
---|
2324 | if type.lower() != "array": |
---|
2325 | for item in self.model.orientation_params: |
---|
2326 | if not item in self.disp_list: |
---|
2327 | ##prepare a spot to store min max |
---|
2328 | if not self.model.details.has_key(item): |
---|
2329 | self.model.details [item] = ["",None,None] |
---|
2330 | |
---|
2331 | iy += 1 |
---|
2332 | ix = 0 |
---|
2333 | ## add parameters name with checkbox for selecting to fit |
---|
2334 | cb = wx.CheckBox(self, -1, item ) |
---|
2335 | cb.SetValue(False) |
---|
2336 | cb.SetToolTipString("Check for fitting") |
---|
2337 | wx.EVT_CHECKBOX(self, cb.GetId(), self.select_param) |
---|
2338 | if self.data.__class__.__name__ =="Data2D": |
---|
2339 | cb.Show(True) |
---|
2340 | else: |
---|
2341 | cb.Hide() |
---|
2342 | sizer.Add( cb,( iy, ix),(1,1), |
---|
2343 | wx.LEFT|wx.EXPAND|wx.ADJUST_MINSIZE, 5) |
---|
2344 | |
---|
2345 | ## add parameter value |
---|
2346 | ix += 1 |
---|
2347 | value= self.model.getParam(item) |
---|
2348 | ctl1 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH,20), |
---|
2349 | style=wx.TE_PROCESS_ENTER) |
---|
2350 | ctl1.SetToolTipString("Hit 'Enter' after typing.") |
---|
2351 | ctl1.SetValue(format_number(value)) |
---|
2352 | if self.data.__class__.__name__ =="Data2D": |
---|
2353 | ctl1.Show(True) |
---|
2354 | else: |
---|
2355 | ctl1.Hide() |
---|
2356 | sizer.Add(ctl1, (iy,ix),(1,1), wx.EXPAND) |
---|
2357 | ## text to show error sign |
---|
2358 | ix += 1 |
---|
2359 | text2=wx.StaticText(self, -1, '+/-') |
---|
2360 | sizer.Add(text2,(iy, ix),(1,1),\ |
---|
2361 | wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2362 | text2.Hide() |
---|
2363 | ix += 1 |
---|
2364 | ctl2 = wx.TextCtrl(self, -1, size=(_BOX_WIDTH,20), style=0) |
---|
2365 | sizer.Add(ctl2, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2366 | ctl2.Hide() |
---|
2367 | |
---|
2368 | |
---|
2369 | ix += 1 |
---|
2370 | ctl3 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
2371 | text_enter_callback = self._onparamRangeEnter) |
---|
2372 | |
---|
2373 | sizer.Add(ctl3, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2374 | ctl3.Hide() |
---|
2375 | |
---|
2376 | ix += 1 |
---|
2377 | ctl4 = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH/2,20), style=wx.TE_PROCESS_ENTER, |
---|
2378 | text_enter_callback = self._onparamRangeEnter) |
---|
2379 | sizer.Add(ctl4, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2380 | |
---|
2381 | ctl4.Hide() |
---|
2382 | |
---|
2383 | if self.engine_type =="park" and self.data.__class__.__name__ =="Data2D": |
---|
2384 | ctl3.Show(True) |
---|
2385 | ctl4.Show(True) |
---|
2386 | |
---|
2387 | ix +=1 |
---|
2388 | # Units |
---|
2389 | if self.model.details.has_key(item): |
---|
2390 | units = wx.StaticText(self, -1, self.model.details[item][0], style=wx.ALIGN_LEFT) |
---|
2391 | else: |
---|
2392 | units = wx.StaticText(self, -1, "", style=wx.ALIGN_LEFT) |
---|
2393 | if self.data.__class__.__name__ =="Data2D": |
---|
2394 | units.Show(True) |
---|
2395 | |
---|
2396 | else: |
---|
2397 | units.Hide() |
---|
2398 | |
---|
2399 | sizer.Add(units, (iy,ix),(1,1), wx.EXPAND|wx.ADJUST_MINSIZE, 0) |
---|
2400 | |
---|
2401 | ##[cb state, name, value, "+/-", error of fit, min, max , units] |
---|
2402 | self.parameters.append([cb,item, ctl1, |
---|
2403 | text2,ctl2, ctl3, ctl4,units]) |
---|
2404 | self.orientation_params.append([cb,item, ctl1, |
---|
2405 | text2,ctl2, ctl3, ctl4,units]) |
---|
2406 | |
---|
2407 | iy+=1 |
---|
2408 | |
---|
2409 | #Display units text on panel |
---|
2410 | for item in keys: |
---|
2411 | if self.model.details.has_key(item): |
---|
2412 | self.text2_4.Show() |
---|
2413 | #Fill the list of fittable parameters |
---|
2414 | self.select_all_param(event=None) |
---|
2415 | |
---|
2416 | self.save_current_state_fit() |
---|
2417 | boxsizer1.Add(sizer) |
---|
2418 | self.sizer3.Add(boxsizer1,0, wx.EXPAND | wx.ALL, 10) |
---|
2419 | self.sizer3.Layout() |
---|
2420 | self.Layout() |
---|
2421 | self.Refresh() |
---|
2422 | self.SetScrollbars(20,20,25,65) |
---|
2423 | |
---|
2424 | class BGTextCtrl(wx.TextCtrl): |
---|
2425 | """ |
---|
2426 | Text control used to display outputs. |
---|
2427 | No editing allowed. The background is |
---|
2428 | grayed out. User can't select text. |
---|
2429 | """ |
---|
2430 | def __init__(self, *args, **kwds): |
---|
2431 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
2432 | self.SetEditable(False) |
---|
2433 | self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) |
---|
2434 | |
---|
2435 | # Bind to mouse event to avoid text highlighting |
---|
2436 | # The event will be skipped once the call-back |
---|
2437 | # is called. |
---|
2438 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
2439 | |
---|
2440 | def _click(self, event): |
---|
2441 | """ |
---|
2442 | Prevent further handling of the mouse event |
---|
2443 | by not calling Skip(). |
---|
2444 | """ |
---|
2445 | pass |
---|
2446 | |
---|
2447 | class HelpWindow(wx.Frame): |
---|
2448 | def __init__(self, parent, id, title): |
---|
2449 | wx.Frame.__init__(self, parent, id, title, size=(570, 400)) |
---|
2450 | |
---|
2451 | from sans.models.CylinderModel import CylinderModel |
---|
2452 | model = CylinderModel() |
---|
2453 | |
---|
2454 | from danse.common.plottools.plottables import Data1D |
---|
2455 | data= Data1D(x=[1,2], y=[3,4], dy=[0.1, 0,1]) |
---|
2456 | |
---|
2457 | from fitpanel import PageInfo |
---|
2458 | myinfo = PageInfo(self, model, data=data ) |
---|
2459 | |
---|
2460 | ## add data |
---|
2461 | |
---|
2462 | from models import ModelList |
---|
2463 | mylist= ModelList() |
---|
2464 | |
---|
2465 | from sans.models.SphereModel import SphereModel |
---|
2466 | from sans.models.SquareWellStructure import SquareWellStructure |
---|
2467 | from sans.models.DebyeModel import DebyeModel |
---|
2468 | from sans.models.LineModel import LineModel |
---|
2469 | name= "shapes" |
---|
2470 | list1= [SphereModel] |
---|
2471 | mylist.set_list( name, list1) |
---|
2472 | |
---|
2473 | name= "Shape-independent" |
---|
2474 | list1= [DebyeModel] |
---|
2475 | mylist.set_list( name, list1) |
---|
2476 | |
---|
2477 | name= "Structure Factors" |
---|
2478 | list1= [SquareWellStructure] |
---|
2479 | mylist.set_list( name, list1) |
---|
2480 | |
---|
2481 | name= "Added models" |
---|
2482 | list1= [LineModel] |
---|
2483 | mylist.set_list( name, list1) |
---|
2484 | |
---|
2485 | myinfo.model_list_box = mylist.get_list() |
---|
2486 | |
---|
2487 | self.page = FitPage(self, myinfo) |
---|
2488 | |
---|
2489 | self.Centre() |
---|
2490 | self.Show(True) |
---|
2491 | |
---|
2492 | if __name__=="__main__": |
---|
2493 | app = wx.App() |
---|
2494 | HelpWindow(None, -1, 'HelpWindow') |
---|
2495 | app.MainLoop() |
---|
2496 | |
---|