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