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