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