[cc31608] | 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 |
---|
[c21e87b] | 11 | from sans.dataloader.data_info import Data1D |
---|
[cc31608] | 12 | from sans.guiframe.events import StatusEvent |
---|
| 13 | from sans.guiframe.events import NewPlotEvent |
---|
| 14 | from sans.guiframe.utils import format_number,check_float |
---|
| 15 | |
---|
| 16 | (Chi2UpdateEvent, EVT_CHI2_UPDATE) = wx.lib.newevent.NewEvent() |
---|
| 17 | _BOX_WIDTH = 76 |
---|
| 18 | _DATA_BOX_WIDTH = 300 |
---|
| 19 | SMEAR_SIZE_L = 0.00 |
---|
| 20 | SMEAR_SIZE_H = 0.00 |
---|
| 21 | |
---|
| 22 | import basepage |
---|
| 23 | from basepage import BasicPage |
---|
| 24 | from basepage import PageInfoEvent |
---|
| 25 | from sans.models.qsmearing import smear_selection |
---|
| 26 | from fitpage import FitPage |
---|
| 27 | |
---|
| 28 | class BatchFitPage(FitPage): |
---|
| 29 | window_name = "BatchFit" |
---|
| 30 | window_caption = "BatchFit" |
---|
| 31 | |
---|
| 32 | def __init__(self,parent, color='rand'): |
---|
| 33 | """ |
---|
| 34 | Initialization of the Panel |
---|
| 35 | """ |
---|
| 36 | FitPage.__init__(self, parent, color=color) |
---|
| 37 | self.window_name = "BatchFit" |
---|
| 38 | self.window_caption = "BatchFit" |
---|
| 39 | |
---|
| 40 | def _fill_range_sizer(self): |
---|
| 41 | """ |
---|
| 42 | Fill the sizer containing the plotting range |
---|
| 43 | add access to npts |
---|
| 44 | """ |
---|
| 45 | is_2Ddata = False |
---|
| 46 | |
---|
| 47 | # Check if data is 2D |
---|
| 48 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 49 | self.enable2D: |
---|
| 50 | is_2Ddata = True |
---|
| 51 | |
---|
| 52 | title = "Fitting" |
---|
| 53 | #smear messages & titles |
---|
| 54 | smear_message_none = "No smearing is selected..." |
---|
| 55 | smear_message_dqdata = "The dQ data is being used for smearing..." |
---|
| 56 | smear_message_2d = "Higher accuracy is very time-expensive. Use it with care..." |
---|
| 57 | smear_message_new_ssmear = "Please enter only the value of interest to customize smearing..." |
---|
| 58 | smear_message_new_psmear = "Please enter both; the dQ will be generated by interpolation..." |
---|
| 59 | smear_message_2d_x_title = "<dQp>[1/A]:" |
---|
| 60 | smear_message_2d_y_title = "<dQs>[1/A]:" |
---|
| 61 | smear_message_pinhole_min_title = "dQ_low[1/A]:" |
---|
| 62 | smear_message_pinhole_max_title = "dQ_high[1/A]:" |
---|
| 63 | smear_message_slit_height_title = "Slit height[1/A]:" |
---|
| 64 | smear_message_slit_width_title = "Slit width[1/A]:" |
---|
| 65 | |
---|
| 66 | self._get_smear_info() |
---|
| 67 | |
---|
| 68 | #Sizers |
---|
| 69 | box_description_range = wx.StaticBox(self, -1,str(title)) |
---|
| 70 | boxsizer_range = wx.StaticBoxSizer(box_description_range, wx.VERTICAL) |
---|
| 71 | self.sizer_set_smearer = wx.BoxSizer(wx.VERTICAL) |
---|
| 72 | sizer_smearer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 73 | self.sizer_new_smear= wx.BoxSizer(wx.HORIZONTAL) |
---|
| 74 | self.sizer_set_masking = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 75 | sizer_chi2 = wx.BoxSizer(wx.VERTICAL) |
---|
| 76 | """ |
---|
| 77 | smear_set_box= wx.StaticBox(self, -1,'Set Instrumental Smearing') |
---|
| 78 | sizer_smearer_box = wx.StaticBoxSizer(smear_set_box, wx.HORIZONTAL) |
---|
| 79 | sizer_smearer_box.SetMinSize((_DATA_BOX_WIDTH,85)) |
---|
| 80 | """ |
---|
| 81 | sizer_fit = wx.GridSizer(2, 4, 2, 6) |
---|
| 82 | """ |
---|
| 83 | # combobox for smear2d accuracy selection |
---|
| 84 | self.smear_accuracy = wx.ComboBox(self, -1,size=(50,-1),style=wx.CB_READONLY) |
---|
| 85 | self._set_accuracy_list() |
---|
| 86 | self.smear_accuracy.SetValue(self.smear2d_accuracy) |
---|
| 87 | self.smear_accuracy.SetSelection(0) |
---|
| 88 | self.smear_accuracy.SetToolTipString("'Higher' uses more Gaussian points for smearing computation.") |
---|
| 89 | |
---|
| 90 | wx.EVT_COMBOBOX(self.smear_accuracy,-1, self._on_select_accuracy) |
---|
| 91 | """ |
---|
| 92 | #Fit button |
---|
| 93 | self.btFit = wx.Button(self,wx.NewId(),'Fit', size=(88,25)) |
---|
| 94 | self.default_bt_colour = self.btFit.GetDefaultAttributes() |
---|
| 95 | self.btFit.Bind(wx.EVT_BUTTON, self._onFit,id= self.btFit.GetId()) |
---|
| 96 | self.btFit.SetToolTipString("Start fitting.") |
---|
| 97 | """ |
---|
| 98 | #textcntrl for custom resolution |
---|
| 99 | self.smear_pinhole_max = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
| 100 | text_enter_callback = self.onPinholeSmear) |
---|
| 101 | self.smear_pinhole_min = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
| 102 | text_enter_callback = self.onPinholeSmear) |
---|
| 103 | self.smear_slit_height= self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
| 104 | text_enter_callback = self.onSlitSmear) |
---|
| 105 | self.smear_slit_width = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, |
---|
| 106 | text_enter_callback = self.onSlitSmear) |
---|
| 107 | |
---|
| 108 | ## smear |
---|
| 109 | self.smear_data_left= BGTextCtrl(self, -1, size=(_BOX_WIDTH-25,20), style=0) |
---|
| 110 | self.smear_data_left.SetValue(str(self.dq_l)) |
---|
| 111 | self.smear_data_right = BGTextCtrl(self, -1, size=(_BOX_WIDTH-25,20), style=0) |
---|
| 112 | self.smear_data_right.SetValue(str(self.dq_r)) |
---|
| 113 | |
---|
| 114 | #set default values for smear |
---|
| 115 | self.smear_pinhole_max.SetValue(str(self.dx_max)) |
---|
| 116 | self.smear_pinhole_min.SetValue(str(self.dx_min)) |
---|
| 117 | self.smear_slit_height.SetValue(str(self.dxl)) |
---|
| 118 | self.smear_slit_width.SetValue(str(self.dxw)) |
---|
| 119 | |
---|
| 120 | #Filling the sizer containing instruments smearing info. |
---|
| 121 | self.disable_smearer = wx.RadioButton(self, -1, 'None', style=wx.RB_GROUP) |
---|
| 122 | self.enable_smearer = wx.RadioButton(self, -1, 'Use dQ Data') |
---|
| 123 | #self.enable_smearer.SetToolTipString("Click to use the loaded dQ data for smearing.") |
---|
| 124 | self.pinhole_smearer = wx.RadioButton(self, -1, 'Custom Pinhole Smear') |
---|
| 125 | #self.pinhole_smearer.SetToolTipString("Click to input custom resolution for pinhole smearing.") |
---|
| 126 | self.slit_smearer = wx.RadioButton(self, -1, 'Custom Slit Smear') |
---|
| 127 | #self.slit_smearer.SetToolTipString("Click to input custom resolution for slit smearing.") |
---|
| 128 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.disable_smearer.GetId()) |
---|
| 129 | self.Bind(wx.EVT_RADIOBUTTON, self.onSmear, id=self.enable_smearer.GetId()) |
---|
| 130 | self.Bind(wx.EVT_RADIOBUTTON, self.onPinholeSmear, id=self.pinhole_smearer.GetId()) |
---|
| 131 | self.Bind(wx.EVT_RADIOBUTTON, self.onSlitSmear, id=self.slit_smearer.GetId()) |
---|
| 132 | self.disable_smearer.SetValue(True) |
---|
| 133 | |
---|
| 134 | # add 4 types of smearing to the sizer |
---|
| 135 | sizer_smearer.Add( self.disable_smearer,0, wx.LEFT, 10) |
---|
| 136 | sizer_smearer.Add((10,10)) |
---|
| 137 | sizer_smearer.Add( self.enable_smearer) |
---|
| 138 | sizer_smearer.Add((10,10)) |
---|
| 139 | sizer_smearer.Add( self.pinhole_smearer ) |
---|
| 140 | sizer_smearer.Add((10,10)) |
---|
| 141 | sizer_smearer.Add( self.slit_smearer ) |
---|
| 142 | sizer_smearer.Add((10,10)) |
---|
| 143 | """ |
---|
[3f36675] | 144 | """ |
---|
[cc31608] | 145 | # StaticText for chi2, N(for fitting), Npts |
---|
| 146 | self.tcChi = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
| 147 | self.tcChi.SetToolTipString("Chi2/Npts(Fit)") |
---|
| 148 | self.Npts_fit = BGTextCtrl(self, -1, "-", size=(75,20), style=0) |
---|
| 149 | self.Npts_fit.SetToolTipString(\ |
---|
| 150 | " Npts : number of points selected for fitting") |
---|
| 151 | self.Npts_total = self.ModelTextCtrl(self, -1, size=(_BOX_WIDTH, 20), |
---|
| 152 | style=wx.TE_PROCESS_ENTER, |
---|
| 153 | text_enter_callback=self._onQrangeEnter) |
---|
| 154 | self.Npts_total.SetValue(format_number(self.npts_x)) |
---|
| 155 | self.Npts_total.SetToolTipString(\ |
---|
| 156 | " Total Npts : total number of data points") |
---|
[3f36675] | 157 | """ |
---|
[cc31608] | 158 | # Update and Draw button |
---|
| 159 | self.draw_button = wx.Button(self,wx.NewId(),'Compute', size=(88,24)) |
---|
| 160 | self.draw_button.Bind(wx.EVT_BUTTON, \ |
---|
| 161 | self._onDraw,id= self.draw_button.GetId()) |
---|
| 162 | self.draw_button.SetToolTipString("Compute and Draw.") |
---|
[3f36675] | 163 | """ |
---|
[cc31608] | 164 | box_description_1= wx.StaticText(self, -1,' Chi2/Npts') |
---|
| 165 | box_description_2= wx.StaticText(self, -1,'Npts(Fit)') |
---|
| 166 | box_description_3= wx.StaticText(self, -1,'Total Npts') |
---|
| 167 | box_description_3.SetToolTipString( \ |
---|
| 168 | " Total Npts : total number of data points") |
---|
| 169 | #box_description_4= wx.StaticText(self, -1,' ') |
---|
[3f36675] | 170 | """ |
---|
[cc31608] | 171 | |
---|
[3f36675] | 172 | """ |
---|
[cc31608] | 173 | sizer_fit.Add(box_description_1,0,0) |
---|
| 174 | sizer_fit.Add(box_description_2,0,0) |
---|
[3f36675] | 175 | sizer_fit.Add(box_description_3,0,0) |
---|
| 176 | """ |
---|
[cc31608] | 177 | sizer_fit.Add(self.draw_button,0,0) |
---|
[3f36675] | 178 | """ |
---|
[cc31608] | 179 | sizer_fit.Add(self.tcChi,0,0) |
---|
| 180 | sizer_fit.Add(self.Npts_fit ,0,0) |
---|
| 181 | sizer_fit.Add(self.Npts_total,0,0) |
---|
[3f36675] | 182 | """ |
---|
[cc31608] | 183 | sizer_fit.Add(self.btFit,0,0) |
---|
| 184 | """ |
---|
| 185 | self.smear_description_none = wx.StaticText(self, -1, smear_message_none , style=wx.ALIGN_LEFT) |
---|
| 186 | self.smear_description_dqdata = wx.StaticText(self, -1, smear_message_dqdata , style=wx.ALIGN_LEFT) |
---|
| 187 | self.smear_description_type = wx.StaticText(self, -1, "Type:" , style=wx.ALIGN_LEFT) |
---|
| 188 | self.smear_description_accuracy_type = wx.StaticText(self, -1, "Accuracy:" , style=wx.ALIGN_LEFT) |
---|
| 189 | self.smear_description_smear_type = BGTextCtrl(self, -1, size=(57,20), style=0) |
---|
| 190 | self.smear_description_smear_type.SetValue(str(self.dq_l)) |
---|
| 191 | self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) |
---|
| 192 | self.smear_description_2d = wx.StaticText(self, -1, smear_message_2d , style=wx.ALIGN_LEFT) |
---|
| 193 | self.smear_message_new_s = wx.StaticText(self, -1, smear_message_new_ssmear , style=wx.ALIGN_LEFT) |
---|
| 194 | self.smear_message_new_p = wx.StaticText(self, -1, smear_message_new_psmear , style=wx.ALIGN_LEFT) |
---|
| 195 | self.smear_description_2d_x = wx.StaticText(self, -1, smear_message_2d_x_title , style=wx.ALIGN_LEFT) |
---|
| 196 | self.smear_description_2d_x.SetToolTipString(" dQp(parallel) in q_r direction.") |
---|
| 197 | self.smear_description_2d_y = wx.StaticText(self, -1, smear_message_2d_y_title , style=wx.ALIGN_LEFT) |
---|
| 198 | self.smear_description_2d_y.SetToolTipString(" dQs(perpendicular) in q_phi direction.") |
---|
| 199 | self.smear_description_pin_min = wx.StaticText(self, -1, smear_message_pinhole_min_title , style=wx.ALIGN_LEFT) |
---|
| 200 | self.smear_description_pin_max = wx.StaticText(self, -1, smear_message_pinhole_max_title , style=wx.ALIGN_LEFT) |
---|
| 201 | self.smear_description_slit_height = wx.StaticText(self, -1, smear_message_slit_height_title , style=wx.ALIGN_LEFT) |
---|
| 202 | self.smear_description_slit_width = wx.StaticText(self, -1, smear_message_slit_width_title , style=wx.ALIGN_LEFT) |
---|
| 203 | """ |
---|
| 204 | #arrange sizers |
---|
| 205 | #boxsizer1.Add( self.tcChi ) |
---|
| 206 | """ |
---|
| 207 | self.sizer_set_smearer.Add(sizer_smearer ) |
---|
| 208 | self.sizer_set_smearer.Add((10,10)) |
---|
| 209 | self.sizer_set_smearer.Add( self.smear_description_none,0, wx.CENTER, 10 ) |
---|
| 210 | self.sizer_set_smearer.Add( self.smear_description_dqdata,0, wx.CENTER, 10 ) |
---|
| 211 | self.sizer_set_smearer.Add( self.smear_description_2d,0, wx.CENTER, 10 ) |
---|
| 212 | self.sizer_new_smear.Add( self.smear_description_type,0, wx.CENTER, 10 ) |
---|
| 213 | self.sizer_new_smear.Add( self.smear_description_accuracy_type,0, wx.CENTER, 10 ) |
---|
| 214 | self.sizer_new_smear.Add( self.smear_accuracy ) |
---|
| 215 | self.sizer_new_smear.Add( self.smear_description_smear_type,0, wx.CENTER, 10 ) |
---|
| 216 | self.sizer_new_smear.Add((15,-1)) |
---|
| 217 | self.sizer_new_smear.Add( self.smear_description_2d_x,0, wx.CENTER, 10 ) |
---|
| 218 | self.sizer_new_smear.Add( self.smear_description_pin_min,0, wx.CENTER, 10 ) |
---|
| 219 | self.sizer_new_smear.Add( self.smear_description_slit_height,0, wx.CENTER, 10 ) |
---|
| 220 | |
---|
| 221 | self.sizer_new_smear.Add( self.smear_pinhole_min,0, wx.CENTER, 10 ) |
---|
| 222 | self.sizer_new_smear.Add( self.smear_slit_height,0, wx.CENTER, 10 ) |
---|
| 223 | self.sizer_new_smear.Add( self.smear_data_left,0, wx.CENTER, 10 ) |
---|
| 224 | self.sizer_new_smear.Add((20,-1)) |
---|
| 225 | self.sizer_new_smear.Add( self.smear_description_2d_y,0, wx.CENTER, 10 ) |
---|
| 226 | self.sizer_new_smear.Add( self.smear_description_pin_max,0, wx.CENTER, 10 ) |
---|
| 227 | self.sizer_new_smear.Add( self.smear_description_slit_width,0, wx.CENTER, 10 ) |
---|
| 228 | |
---|
| 229 | self.sizer_new_smear.Add( self.smear_pinhole_max,0, wx.CENTER, 10 ) |
---|
| 230 | self.sizer_new_smear.Add( self.smear_slit_width,0, wx.CENTER, 10 ) |
---|
| 231 | self.sizer_new_smear.Add( self.smear_data_right,0, wx.CENTER, 10 ) |
---|
| 232 | |
---|
| 233 | self.sizer_set_smearer.Add( self.smear_message_new_s,0, wx.CENTER, 10) |
---|
| 234 | self.sizer_set_smearer.Add( self.smear_message_new_p,0, wx.CENTER, 10) |
---|
| 235 | self.sizer_set_smearer.Add((5,2)) |
---|
| 236 | self.sizer_set_smearer.Add( self.sizer_new_smear,0, wx.CENTER, 10 ) |
---|
| 237 | |
---|
| 238 | # add all to chi2 sizer |
---|
| 239 | sizer_smearer_box.Add(self.sizer_set_smearer) |
---|
| 240 | |
---|
| 241 | sizer_chi2.Add(sizer_smearer_box) |
---|
| 242 | """ |
---|
| 243 | sizer_chi2.Add((-1,5)) |
---|
| 244 | """ |
---|
| 245 | # hide all smear messages and textctrl |
---|
| 246 | self._hide_all_smear_info() |
---|
| 247 | """ |
---|
| 248 | # get smear_selection |
---|
| 249 | self.current_smearer= smear_selection( self.data, self.model ) |
---|
| 250 | """ |
---|
| 251 | # Show only the relevant smear messages, etc |
---|
| 252 | if self.current_smearer == None: |
---|
| 253 | if not is_2Ddata: |
---|
| 254 | self.smear_description_none.Show(True) |
---|
| 255 | self.enable_smearer.Disable() |
---|
| 256 | else: |
---|
| 257 | self.smear_description_none.Show(True) |
---|
| 258 | #self.smear_description_2d.Show(True) |
---|
| 259 | #self.pinhole_smearer.Disable() |
---|
| 260 | self.slit_smearer.Disable() |
---|
| 261 | #self.enable_smearer.Disable() |
---|
| 262 | if self.data == None: |
---|
| 263 | self.slit_smearer.Disable() |
---|
| 264 | self.pinhole_smearer.Disable() |
---|
| 265 | self.enable_smearer.Disable() |
---|
| 266 | else: self._show_smear_sizer() |
---|
| 267 | """ |
---|
| 268 | boxsizer_range.Add(self.sizer_set_masking) |
---|
| 269 | #2D data? default |
---|
| 270 | is_2Ddata = False |
---|
| 271 | |
---|
| 272 | #check if it is 2D data |
---|
| 273 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 274 | self.enable2D: |
---|
| 275 | is_2Ddata = True |
---|
| 276 | |
---|
| 277 | self.sizer5.Clear(True) |
---|
| 278 | |
---|
| 279 | self.qmin = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20), |
---|
| 280 | style=wx.TE_PROCESS_ENTER, |
---|
| 281 | text_enter_callback = self._onQrangeEnter) |
---|
| 282 | self.qmin.SetValue(str(self.qmin_x)) |
---|
| 283 | self.qmin.SetToolTipString("Minimun value of Q in linear scale.") |
---|
| 284 | |
---|
| 285 | self.qmax = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH,20), |
---|
| 286 | style=wx.TE_PROCESS_ENTER, |
---|
| 287 | text_enter_callback=self._onQrangeEnter) |
---|
| 288 | self.qmax.SetValue(str(self.qmax_x)) |
---|
| 289 | self.qmax.SetToolTipString("Maximum value of Q in linear scale.") |
---|
| 290 | |
---|
| 291 | id = wx.NewId() |
---|
| 292 | self.reset_qrange =wx.Button(self,id,'Reset',size=(77,20)) |
---|
| 293 | |
---|
| 294 | self.reset_qrange.Bind(wx.EVT_BUTTON, self.on_reset_clicked,id=id) |
---|
| 295 | self.reset_qrange.SetToolTipString("Reset Q range to the default values") |
---|
| 296 | |
---|
| 297 | sizer_horizontal=wx.BoxSizer(wx.HORIZONTAL) |
---|
| 298 | sizer= wx.GridSizer(2, 4, 2, 6) |
---|
| 299 | |
---|
| 300 | self.btEditMask = wx.Button(self,wx.NewId(),'Editor', size=(88,23)) |
---|
| 301 | self.btEditMask.Bind(wx.EVT_BUTTON, self._onMask,id= self.btEditMask.GetId()) |
---|
| 302 | self.btEditMask.SetToolTipString("Edit Mask.") |
---|
| 303 | self.EditMask_title = wx.StaticText(self, -1, ' Masking(2D)') |
---|
| 304 | |
---|
| 305 | sizer.Add(wx.StaticText(self, -1, 'Q range')) |
---|
| 306 | sizer.Add(wx.StaticText(self, -1, ' Min[1/A]')) |
---|
| 307 | sizer.Add(wx.StaticText(self, -1, ' Max[1/A]')) |
---|
| 308 | sizer.Add(self.EditMask_title) |
---|
| 309 | #sizer.Add(wx.StaticText(self, -1, '')) |
---|
| 310 | sizer.Add(self.reset_qrange) |
---|
| 311 | sizer.Add(self.qmin) |
---|
| 312 | sizer.Add(self.qmax) |
---|
| 313 | #sizer.Add(self.theory_npts_tcrtl) |
---|
| 314 | sizer.Add(self.btEditMask) |
---|
| 315 | boxsizer_range.Add(sizer_chi2) |
---|
| 316 | boxsizer_range.Add((10,10)) |
---|
| 317 | boxsizer_range.Add(sizer) |
---|
| 318 | |
---|
| 319 | boxsizer_range.Add((10,15)) |
---|
| 320 | boxsizer_range.Add(sizer_fit) |
---|
| 321 | if is_2Ddata: |
---|
| 322 | self.btEditMask.Enable() |
---|
| 323 | self.EditMask_title.Enable() |
---|
| 324 | else: |
---|
| 325 | self.btEditMask.Disable() |
---|
| 326 | self.EditMask_title.Disable() |
---|
| 327 | """ |
---|
| 328 | ## save state |
---|
| 329 | self.save_current_state() |
---|
| 330 | """ |
---|
| 331 | self.sizer5.Add(boxsizer_range,0, wx.EXPAND | wx.ALL, 10) |
---|
| 332 | self.sizer5.Layout() |
---|
| 333 | |
---|
| 334 | def _on_select_model(self, event=None): |
---|
| 335 | """ |
---|
| 336 | call back for model selection |
---|
| 337 | """ |
---|
| 338 | |
---|
| 339 | self.Show(False) |
---|
| 340 | self._on_select_model_helper() |
---|
| 341 | self.set_model_param_sizer(self.model) |
---|
| 342 | if self.model is None: |
---|
| 343 | self._set_bookmark_flag(False) |
---|
| 344 | self._keep.Enable(False) |
---|
| 345 | self._set_save_flag(False) |
---|
| 346 | self.enable_disp.SetValue(False) |
---|
| 347 | self.disable_disp.SetValue(True) |
---|
| 348 | try: |
---|
| 349 | self.set_dispers_sizer() |
---|
| 350 | except: |
---|
| 351 | pass |
---|
| 352 | """ |
---|
| 353 | #self.btFit.SetFocus() |
---|
| 354 | self.state.enable_disp = self.enable_disp.GetValue() |
---|
| 355 | self.state.disable_disp = self.disable_disp.GetValue() |
---|
| 356 | self.state.pinhole_smearer = self.pinhole_smearer.GetValue() |
---|
| 357 | self.state.slit_smearer = self.slit_smearer.GetValue() |
---|
| 358 | """ |
---|
| 359 | self.state.structurecombobox = self.structurebox.GetCurrentSelection() |
---|
| 360 | self.state.formfactorcombobox = self.formfactorbox.GetCurrentSelection() |
---|
| 361 | |
---|
| 362 | if self.model != None: |
---|
| 363 | self._set_copy_flag(True) |
---|
| 364 | self._set_paste_flag(True) |
---|
| 365 | if self.data != None: |
---|
| 366 | self._set_bookmark_flag(True) |
---|
| 367 | self._keep.Enable(True) |
---|
| 368 | |
---|
| 369 | temp_smear = None |
---|
| 370 | """ |
---|
| 371 | #self._set_save_flag(True) |
---|
| 372 | # Reset smearer, model and data |
---|
| 373 | self._set_smear(self.data) |
---|
| 374 | try: |
---|
| 375 | # update smearer sizer |
---|
| 376 | self.onSmear(None) |
---|
| 377 | temp_smear = None |
---|
| 378 | if self.enable_smearer.GetValue(): |
---|
| 379 | # Set the smearer environments |
---|
| 380 | temp_smear = self.smearer |
---|
| 381 | except: |
---|
| 382 | raise |
---|
| 383 | ## error occured on chisqr computation |
---|
| 384 | #pass |
---|
| 385 | """ |
---|
| 386 | ## event to post model to fit to fitting plugins |
---|
| 387 | (ModelEventbox, EVT_MODEL_BOX) = wx.lib.newevent.NewEvent() |
---|
| 388 | |
---|
| 389 | ## set smearing value whether or not |
---|
| 390 | # the data contain the smearing info |
---|
| 391 | evt = ModelEventbox(model=self.model, |
---|
| 392 | smearer=temp_smear, |
---|
| 393 | qmin=float(self.qmin_x), |
---|
| 394 | uid=self.uid, |
---|
| 395 | qmax=float(self.qmax_x)) |
---|
| 396 | |
---|
| 397 | self._manager._on_model_panel(evt=evt) |
---|
| 398 | self.mbox_description.SetLabel("Model [%s]" % str(self.model.name)) |
---|
| 399 | self.state.model = self.model.clone() |
---|
| 400 | self.state.model.name = self.model.name |
---|
| 401 | |
---|
| 402 | |
---|
| 403 | if event != None: |
---|
| 404 | ## post state to fit panel |
---|
| 405 | new_event = PageInfoEvent(page = self) |
---|
| 406 | wx.PostEvent(self.parent, new_event) |
---|
| 407 | #update list of plugins if new plugin is available |
---|
| 408 | if self.plugin_rbutton.GetValue(): |
---|
| 409 | temp = self.parent.update_model_list() |
---|
| 410 | if temp: |
---|
| 411 | self.model_list_box = temp |
---|
| 412 | current_val = self.formfactorbox.GetValue() |
---|
| 413 | pos = self.formfactorbox.GetSelection() |
---|
| 414 | self._show_combox_helper() |
---|
| 415 | self.formfactorbox.SetSelection(pos) |
---|
| 416 | self.formfactorbox.SetValue(current_val) |
---|
| 417 | self._onDraw(event=None) |
---|
| 418 | else: |
---|
| 419 | self._draw_model() |
---|
| 420 | self.SetupScrolling() |
---|
| 421 | self.Show(True) |
---|
| 422 | |
---|
| 423 | def _update_paramv_on_fit(self): |
---|
| 424 | """ |
---|
| 425 | make sure that update param values just before the fitting |
---|
| 426 | """ |
---|
| 427 | #flag for qmin qmax check values |
---|
| 428 | flag = True |
---|
| 429 | self.fitrange = True |
---|
| 430 | is_modified = False |
---|
| 431 | |
---|
| 432 | #wx.PostEvent(self._manager.parent, StatusEvent(status=" \ |
---|
| 433 | #updating ... ",type="update")) |
---|
| 434 | |
---|
| 435 | ##So make sure that update param values on_Fit. |
---|
| 436 | #self._undo.Enable(True) |
---|
| 437 | if self.model !=None: |
---|
| 438 | ##Check the values |
---|
| 439 | self._check_value_enter( self.fittable_param ,is_modified) |
---|
| 440 | self._check_value_enter( self.fixed_param ,is_modified) |
---|
| 441 | self._check_value_enter( self.parameters ,is_modified) |
---|
| 442 | |
---|
| 443 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
| 444 | # Here we should check whether the boundaries have been modified. |
---|
| 445 | # If qmin and qmax have been modified, update qmin and qmax and |
---|
| 446 | # set the is_modified flag to True |
---|
| 447 | self.fitrange = self._validate_qrange(self.qmin, self.qmax) |
---|
| 448 | if self.fitrange: |
---|
| 449 | tempmin = float(self.qmin.GetValue()) |
---|
| 450 | if tempmin != self.qmin_x: |
---|
| 451 | self.qmin_x = tempmin |
---|
| 452 | tempmax = float(self.qmax.GetValue()) |
---|
| 453 | if tempmax != self.qmax_x: |
---|
| 454 | self.qmax_x = tempmax |
---|
| 455 | if tempmax == tempmin: |
---|
| 456 | flag = False |
---|
| 457 | temp_smearer = None |
---|
| 458 | """ |
---|
| 459 | if not self.disable_smearer.GetValue(): |
---|
| 460 | temp_smearer= self.current_smearer |
---|
| 461 | if self.slit_smearer.GetValue(): |
---|
| 462 | flag = self.update_slit_smear() |
---|
| 463 | elif self.pinhole_smearer.GetValue(): |
---|
| 464 | flag = self.update_pinhole_smear() |
---|
| 465 | else: |
---|
| 466 | self._manager.set_smearer(smearer=temp_smearer, |
---|
| 467 | uid=self.uid, |
---|
| 468 | qmin=float(self.qmin_x), |
---|
| 469 | qmax=float(self.qmax_x), |
---|
| 470 | draw=False) |
---|
| 471 | elif not self._is_2D(): |
---|
| 472 | self._manager.set_smearer(smearer=temp_smearer, |
---|
| 473 | qmin=float(self.qmin_x), |
---|
| 474 | uid=self.uid, |
---|
| 475 | qmax= float(self.qmax_x), |
---|
| 476 | draw=False) |
---|
| 477 | |
---|
| 478 | if self.data != None: |
---|
| 479 | index_data = ((self.qmin_x <= self.data.x)&\ |
---|
| 480 | (self.data.x <= self.qmax_x)) |
---|
| 481 | val = str(len(self.data.x[index_data==True])) |
---|
| 482 | self.Npts_fit.SetValue(val) |
---|
| 483 | else: |
---|
| 484 | # No data in the panel |
---|
| 485 | try: |
---|
| 486 | self.npts_x = float(self.Npts_total.GetValue()) |
---|
| 487 | except: |
---|
| 488 | flag = False |
---|
| 489 | return flag |
---|
| 490 | flag = True |
---|
| 491 | """ |
---|
| 492 | if self._is_2D(): |
---|
| 493 | # only 2D case set mask |
---|
| 494 | flag = self._validate_Npts() |
---|
| 495 | if not flag: |
---|
| 496 | return flag |
---|
| 497 | else: flag = False |
---|
| 498 | else: |
---|
| 499 | flag = False |
---|
| 500 | |
---|
| 501 | #For invalid q range, disable the mask editor and fit button, vs. |
---|
| 502 | if not self.fitrange: |
---|
| 503 | #self.btFit.Disable() |
---|
| 504 | if self._is_2D(): |
---|
| 505 | self.btEditMask.Disable() |
---|
| 506 | else: |
---|
| 507 | #self.btFit.Enable(True) |
---|
| 508 | if self._is_2D() and self.data != None: |
---|
| 509 | self.btEditMask.Enable(True) |
---|
| 510 | |
---|
| 511 | if not flag: |
---|
| 512 | msg = "Cannot Plot or Fit :Must select a " |
---|
| 513 | msg += " model or Fitting range is not valid!!! " |
---|
| 514 | wx.PostEvent(self.parent.parent, StatusEvent(status=msg)) |
---|
| 515 | |
---|
| 516 | self.save_current_state() |
---|
| 517 | |
---|
| 518 | return flag |
---|
| 519 | def save_current_state(self): |
---|
| 520 | """ |
---|
| 521 | Currently no save option implemented for batch page |
---|
| 522 | """ |
---|
| 523 | pass |
---|
| 524 | def save_current_state_fit(self): |
---|
| 525 | """ |
---|
| 526 | Currently no save option implemented for batch page |
---|
| 527 | """ |
---|
| 528 | pass |
---|
| 529 | def set_data(self, data): |
---|
| 530 | """ |
---|
| 531 | reset the current data |
---|
| 532 | """ |
---|
| 533 | id = None |
---|
| 534 | group_id = None |
---|
| 535 | flag = False |
---|
| 536 | if self.data is None and data is not None: |
---|
| 537 | flag = True |
---|
| 538 | if data is not None: |
---|
| 539 | id = data.id |
---|
| 540 | group_id = data.group_id |
---|
| 541 | if self.data is not None: |
---|
| 542 | flag = (data.id != self.data.id) |
---|
| 543 | self.data = data |
---|
| 544 | if self.data is None: |
---|
| 545 | data_min = "" |
---|
| 546 | data_max = "" |
---|
| 547 | data_name = "" |
---|
| 548 | self._set_bookmark_flag(False) |
---|
| 549 | self._keep.Enable(False) |
---|
| 550 | self._set_save_flag(False) |
---|
| 551 | else: |
---|
| 552 | if self.model != None: |
---|
| 553 | self._set_bookmark_flag(True) |
---|
| 554 | self._keep.Enable(True) |
---|
| 555 | self._set_save_flag(True) |
---|
| 556 | self._set_preview_flag(True) |
---|
| 557 | """ |
---|
| 558 | self._set_smear(data) |
---|
| 559 | # more disables for 2D |
---|
| 560 | if self.data.__class__.__name__ == "Data2D" or \ |
---|
| 561 | self.enable2D: |
---|
| 562 | self.slit_smearer.Disable() |
---|
| 563 | self.pinhole_smearer.Enable(True) |
---|
| 564 | self.default_mask = copy.deepcopy(self.data.mask) |
---|
| 565 | else: |
---|
| 566 | self.slit_smearer.Enable(True) |
---|
| 567 | self.pinhole_smearer.Enable(True) |
---|
| 568 | """ |
---|
| 569 | self.formfactorbox.Enable() |
---|
| 570 | self.structurebox.Enable() |
---|
| 571 | data_name = self.data.name |
---|
| 572 | #set maximum range for x in linear scale |
---|
| 573 | if not hasattr(self.data,"data"): #Display only for 1D data fit |
---|
| 574 | # Minimum value of data |
---|
| 575 | data_min = min(self.data.x) |
---|
| 576 | # Maximum value of data |
---|
| 577 | data_max = max(self.data.x) |
---|
[3f36675] | 578 | """ |
---|
[cc31608] | 579 | #number of total data points |
---|
| 580 | self.Npts_total.SetValue(str(len(self.data.x))) |
---|
| 581 | #default:number of data points selected to fit |
---|
| 582 | self.Npts_fit.SetValue(str(len(self.data.x))) |
---|
[3f36675] | 583 | """ |
---|
[cc31608] | 584 | self.btEditMask.Disable() |
---|
| 585 | self.EditMask_title.Disable() |
---|
| 586 | else: |
---|
| 587 | |
---|
| 588 | ## Minimum value of data |
---|
| 589 | data_min = 0 |
---|
| 590 | x = max(math.fabs(self.data.xmin), math.fabs(self.data.xmax)) |
---|
| 591 | y = max(math.fabs(self.data.ymin), math.fabs(self.data.ymax)) |
---|
| 592 | ## Maximum value of data |
---|
| 593 | data_max = math.sqrt(x*x + y*y) |
---|
[3f36675] | 594 | """ |
---|
[cc31608] | 595 | #number of total data points |
---|
| 596 | self.Npts_total.SetValue(str(len(self.data.data))) |
---|
| 597 | #default:number of data points selected to fit |
---|
| 598 | self.Npts_fit.SetValue(str(len(self.data.data))) |
---|
[3f36675] | 599 | """ |
---|
[cc31608] | 600 | self.btEditMask.Enable() |
---|
| 601 | self.EditMask_title.Enable() |
---|
[3f36675] | 602 | """ |
---|
[cc31608] | 603 | self.Npts_total.SetEditable(False) |
---|
| 604 | self.Npts_total.SetBackgroundColour(\ |
---|
| 605 | self.GetParent().GetBackgroundColour()) |
---|
| 606 | |
---|
| 607 | self.Npts_total.Bind(wx.EVT_MOUSE_EVENTS, self._npts_click) |
---|
| 608 | #self.Npts_total.Disable() |
---|
[3f36675] | 609 | """ |
---|
[cc31608] | 610 | self.dataSource.SetValue(data_name) |
---|
| 611 | self.qmin_x = data_min |
---|
| 612 | self.qmax_x = data_max |
---|
| 613 | #self.minimum_q.SetValue(str(data_min)) |
---|
| 614 | #self.maximum_q.SetValue(str(data_max)) |
---|
| 615 | self.qmin.SetValue(str(data_min)) |
---|
| 616 | self.qmax.SetValue(str(data_max)) |
---|
| 617 | self.qmin.SetBackgroundColour("white") |
---|
| 618 | self.qmax.SetBackgroundColour("white") |
---|
| 619 | self.state.data = data |
---|
| 620 | self.state.qmin = self.qmin_x |
---|
| 621 | self.state.qmax = self.qmax_x |
---|
| 622 | |
---|
| 623 | #update model plot with new data information |
---|
| 624 | if flag: |
---|
| 625 | #set model view button |
---|
| 626 | if self.data.__class__.__name__ == "Data2D": |
---|
| 627 | self.enable2D = True |
---|
| 628 | self.model_view.SetLabel("2D Mode") |
---|
| 629 | else: |
---|
| 630 | self.enable2D = False |
---|
| 631 | self.model_view.SetLabel("1D Mode") |
---|
| 632 | |
---|
| 633 | self.model_view.Disable() |
---|
| 634 | |
---|
| 635 | #replace data plot on combo box selection |
---|
| 636 | #by removing the previous selected data |
---|
[3f36675] | 637 | #wx.PostEvent(self._manager.parent, NewPlotEvent(action="remove", |
---|
| 638 | # group_id=group_id, id=id)) |
---|
| 639 | wx.PostEvent(self._manager.parent, |
---|
| 640 | NewPlotEvent(group_id=group_id, |
---|
| 641 | action="delete")) |
---|
[cc31608] | 642 | #plot the current selected data |
---|
| 643 | wx.PostEvent(self._manager.parent, NewPlotEvent(plot=self.data, |
---|
| 644 | title=str(self.data.title))) |
---|
| 645 | self._manager.store_data(uid=self.uid, data=data, |
---|
| 646 | data_list=self.data_list, |
---|
| 647 | caption=self.window_name) |
---|
| 648 | self._draw_model() |
---|
| 649 | |
---|
| 650 | |
---|
| 651 | |
---|
| 652 | class BGTextCtrl(wx.TextCtrl): |
---|
| 653 | """ |
---|
| 654 | Text control used to display outputs. |
---|
| 655 | No editing allowed. The background is |
---|
| 656 | grayed out. User can't select text. |
---|
| 657 | """ |
---|
| 658 | def __init__(self, *args, **kwds): |
---|
| 659 | wx.TextCtrl.__init__(self, *args, **kwds) |
---|
| 660 | self.SetEditable(False) |
---|
| 661 | self.SetBackgroundColour(self.GetParent().parent.GetBackgroundColour()) |
---|
| 662 | |
---|
| 663 | # Bind to mouse event to avoid text highlighting |
---|
| 664 | # The event will be skipped once the call-back |
---|
| 665 | # is called. |
---|
| 666 | self.Bind(wx.EVT_MOUSE_EVENTS, self._click) |
---|
| 667 | |
---|
| 668 | def _click(self, event): |
---|
| 669 | """ |
---|
| 670 | Prevent further handling of the mouse event |
---|
| 671 | by not calling Skip(). |
---|
| 672 | """ |
---|
| 673 | pass |
---|
| 674 | |
---|