Changeset f72333f in sasview
- Timestamp:
- May 10, 2010 6:50:05 PM (15 years ago)
- Branches:
- master, ESS_GUI, ESS_GUI_Docs, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_iss879, ESS_GUI_iss959, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc, costrafo411, magnetic_scatt, release-4.1.1, release-4.1.2, release-4.2.2, release_4.0.1, ticket-1009, ticket-1094-headless, ticket-1242-2d-resolution, ticket-1243, ticket-1249, ticket885, unittest-saveload
- Children:
- 4fbc93e
- Parents:
- 2d409fa
- Files:
-
- 1 added
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
DataLoader/qsmearing.py
r4834cba rf72333f 13 13 import math 14 14 import logging, sys 15 from DataLoader.smearing_2d import Smearer2D 15 16 16 17 def smear_selection(data1D): … … 34 35 # object, just return None 35 36 if data1D.__class__.__name__ != 'Data1D': 36 return None 37 if data1D == None: 38 return None 39 elif data1D.dqx_data == None or data1D.dqy_data == None: 40 return None 41 return Smearer2D(data1D) 37 42 38 43 if not hasattr(data1D, "dx") and not hasattr(data1D, "dxl") and not hasattr(data1D, "dxw"): -
park_integration/AbstractFitEngine.py
r36bc34e rf72333f 261 261 self.qmin= None 262 262 self.qmax= None 263 self.smearer = None 263 264 self.set_data(sans_data2d ) 265 264 266 265 267 def set_data(self, sans_data2d, qmin=None, qmax=None ): … … 294 296 self.index_model = (self.index_model) & (self.mask) 295 297 self.index_model = (self.index_model) & (numpy.isfinite(self.data)) 296 298 299 def set_smearer(self,smearer): 300 """ 301 Set smearer 302 """ 303 if smearer == None: 304 return 305 self.smearer = smearer 306 self.smearer.set_index(self.index_model) 307 self.smearer.get_data() 308 297 309 def setFitRange(self,qmin=None,qmax=None): 298 310 """ to set the fit range""" … … 317 329 """ 318 330 @return the residuals 319 """ 331 """ 332 if self.smearer != None: 333 fn.set_index(self.index_model) 334 # Get necessary data from self.data and set the data for smearing 335 fn.get_data() 336 337 gn = fn.get_value() 338 else: 339 gn = fn([self.qx_data[self.index_model],self.qy_data[self.index_model]]) 320 340 # use only the data point within ROI range 321 res=(self.data[self.index_model] - fn([self.qx_data[self.index_model], 322 self.qy_data[self.index_model]]))/self.res_err_data[self.index_model] 341 res=(self.data[self.index_model] - gn)/self.res_err_data[self.index_model] 323 342 return res 324 343 -
sansview/perspectives/fitting/basepage.py
r65b77529 rf72333f 14 14 (PreviousStateEvent, EVT_PREVIOUS_STATE) = wx.lib.newevent.NewEvent() 15 15 (NextStateEvent, EVT_NEXT_STATE) = wx.lib.newevent.NewEvent() 16 16 17 _BOX_WIDTH = 76 17 18 _QMIN_DEFAULT = 0.001 … … 55 56 self.data = None 56 57 self.mask = None 58 self.theory = None 57 59 self.state = PageState(parent=parent) 58 60 ## dictionary containing list of models … … 136 138 #self.onSave(event=None) 137 139 self.Bind(wx.EVT_CONTEXT_MENU, self.onContextMenu) 138 140 139 141 ## create the basic structure of the panel with empty sizer 140 142 self.define_page_structure() … … 1097 1099 flag = self.update_pinhole_smear() 1098 1100 else: 1099 self.manager.set_smearer (smearer=temp_smearer, qmin= float(self.qmin_x),1101 self.manager.set_smearer_nodraw(smearer=temp_smearer, qmin= float(self.qmin_x), 1100 1102 qmax= float(self.qmax_x)) 1101 1103 elif not is_2Ddata: … … 1362 1364 temp_smear= self.current_smearer 1363 1365 1364 self.manager.draw_model(self.model, data=self.data, 1366 self.manager.draw_model(self.model, 1367 data=self.data, 1365 1368 smearer= temp_smear, 1366 1369 qmin=float(self.qmin_x), 1367 1370 qmax=float(self.qmax_x), 1368 1371 qstep= float(self.num_points), 1369 enable2D=self.enable2D) 1370 1372 enable2D=self.enable2D) 1371 1373 1372 1374 def _set_model_sizer(self,sizer, box_sizer, title="", object=None): … … 1507 1509 item must model class 1508 1510 """ 1511 st = time.time() 1509 1512 for models in list: 1510 1513 model= models() … … 1514 1517 name = model.name 1515 1518 combobox.Append(name,models) 1516 1519 1517 1520 return 0 1518 1521 … … 1629 1632 self.structurebox.Enable() 1630 1633 self.text2.Enable() 1631 if self.data.__class__.__name__ =="Data2D":1632 self.smear_description_2d.Show(True)1634 #if self.data.__class__.__name__ =="Data2D": 1635 #self.smear_description_2d.Show(True) 1633 1636 1634 1637 s_id = self.structurebox.GetCurrentSelection() … … 2152 2155 if ON_MAC == True: 2153 2156 time.sleep(1) 2154 2157 2158 2155 2159 def on_reset_clicked(self,event): 2156 2160 """ … … 2167 2171 self.qmin_x = data_min 2168 2172 self.qmax_x = math.sqrt(x*x + y*y) 2173 # check smearing 2174 if not self.disable_smearer.GetValue(): 2175 temp_smearer= self.current_smearer 2176 ## set smearing value whether or not the data contain the smearing info 2177 if self.pinhole_smearer.GetValue(): 2178 flag = self.update_pinhole_smear() 2179 else: 2180 flag = True 2169 2181 elif self.data.__class__.__name__ != "Data2D": 2170 2182 self.qmin_x = min(self.data.x) … … 2205 2217 #reset the q range values 2206 2218 self._reset_plotting_range(self.state) 2207 self.compute_chisqr(smearer=self.current_smearer)2219 #self.compute_chisqr(smearer=self.current_smearer) 2208 2220 #Re draw plot 2209 2221 self._draw_model() -
sansview/perspectives/fitting/fitpage.py
r2d409fa rf72333f 20 20 (FitterTypeEvent, EVT_FITTER_TYPE) = wx.lib.newevent.NewEvent() 21 21 (FitStopEvent, EVT_FIT_STOP) = wx.lib.newevent.NewEvent() 22 (Chi2UpdateEvent, EVT_CHI2_UPDATE) = wx.lib.newevent.NewEvent() 22 23 _BOX_WIDTH = 76 23 24 _DATA_BOX_WIDTH = 300 … … 54 55 self.smearer = None 55 56 self.current_smearer = None 57 ## 2D smear accuracy default 58 self.smear2d_accuracy = 'Low' 59 ## slit smear: 56 60 self.dxl = None 57 61 self.dxw = None 62 ## pinhole smear 58 63 self.dx_min = None 59 64 self.dx_max = None 60 65 66 ## model data (theory) calculated on drawing. 67 self.theory_data = None 68 61 69 ## draw sizer 62 70 self._fill_datainfo_sizer() … … 78 86 if self.smearer ==None: 79 87 self.enable_smearer.Disable() 80 if self.data.__class__.__name__ =="Data2D":81 if self.model != None:82 self.smear_description_2d.Show(True)88 #if self.data.__class__.__name__ =="Data2D": 89 #if self.model != None: 90 #self.smear_description_2d.Show(True) 83 91 84 92 self.disp_cb_dict = {} … … 86 94 self.Bind(EVT_FITTER_TYPE,self._on_engine_change) 87 95 self.Bind(EVT_FIT_STOP,self._on_fit_complete) 88 96 self.Bind(EVT_CHI2_UPDATE, self.on_complete_chisqr) 89 97 90 98 def _on_fit_complete(self, event): … … 97 105 pass 98 106 99 107 def _is_2D(self): 108 """ 109 Check if data_name is Data2D 110 @return: True or False 111 """ 112 113 if self.data.__class__.__name__ =="Data2D": 114 return True 115 return False 116 100 117 def _on_engine_change(self, event): 101 118 """ … … 182 199 smear_message_none = "No smearing is selected..." 183 200 smear_message_dqdata = "The dQ data is being used for smearing..." 184 smear_message_2d = " No smearing is supported for 2D model fitting..."201 smear_message_2d = "Higher accuracy is very time-expensive. Use it with care..." 185 202 smear_message_new_ssmear = "Please enter only the value of interest to customize smearing..." 186 203 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]:" 204 smear_message_2d_x_title = "<dQx>[1/A]:" 205 smear_message_2d_y_title = "<dQy>[1/A]:" 206 smear_message_pinhole_min_title = "dQ_low[1/A]:" 207 smear_message_pinhole_max_title = "dQ_high[1/A]:" 189 208 smear_message_slit_height_title = "Slit height[1/A]:" 190 209 smear_message_slit_width_title = "Slit width[1/A]:" … … 205 224 sizer_fit = wx.GridSizer(2, 4,2,6) 206 225 226 # combobox for smear2d accuracy selection 227 self.smear_accuracy = wx.ComboBox(self, -1,size=(50,-1),style=wx.CB_READONLY) 228 self._set_accuracy_list() 229 self.smear_accuracy.SetValue(self.smear2d_accuracy) 230 self.smear_accuracy.SetSelection(0) 231 self.smear_accuracy.SetToolTipString("'Higher' uses more Gaussian points for smearing computation.") 232 233 234 235 wx.EVT_COMBOBOX(self.smear_accuracy,-1, self._on_select_accuracy) 236 207 237 #Fit button 208 238 self.btFit = wx.Button(self,wx.NewId(),'Fit', size=(88,25)) … … 210 240 self.btFit.SetToolTipString("Start fitting.") 211 241 212 #textcntrl for newresolution213 self.smear_pinhole_max = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-2 0,20),style=wx.TE_PROCESS_ENTER,242 #textcntrl for custom resolution 243 self.smear_pinhole_max = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, 214 244 text_enter_callback = self.onPinholeSmear) 215 self.smear_pinhole_min = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-2 0,20),style=wx.TE_PROCESS_ENTER,245 self.smear_pinhole_min = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, 216 246 text_enter_callback = self.onPinholeSmear) 217 self.smear_slit_height= self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-2 0,20),style=wx.TE_PROCESS_ENTER,247 self.smear_slit_height= self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, 218 248 text_enter_callback = self.onSlitSmear) 219 self.smear_slit_width = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-2 0,20),style=wx.TE_PROCESS_ENTER,249 self.smear_slit_width = self.ModelTextCtrl(self, -1,size=(_BOX_WIDTH-25,20),style=wx.TE_PROCESS_ENTER, 220 250 text_enter_callback = self.onSlitSmear) 221 self.smear_data_left= BGTextCtrl(self, -1, size=(_BOX_WIDTH-20,20), style=0) 251 252 ## smear 253 self.smear_data_left= BGTextCtrl(self, -1, size=(_BOX_WIDTH-25,20), style=0) 222 254 self.smear_data_left.SetValue(str(self.dq_l)) 223 self.smear_data_right = BGTextCtrl(self, -1, size=(_BOX_WIDTH-2 0,20), style=0)255 self.smear_data_right = BGTextCtrl(self, -1, size=(_BOX_WIDTH-25,20), style=0) 224 256 self.smear_data_right.SetValue(str(self.dq_r)) 225 257 226 #set default values 258 #set default values for smear 227 259 self.smear_pinhole_max.SetValue(str(self.dx_max)) 228 260 self.smear_pinhole_min.SetValue(str(self.dx_min)) … … 281 313 self.smear_description_dqdata = wx.StaticText(self, -1, smear_message_dqdata , style=wx.ALIGN_LEFT) 282 314 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) 315 self.smear_description_accuracy_type = wx.StaticText(self, -1, "Accuracy:" , style=wx.ALIGN_LEFT) 316 self.smear_description_smear_type = BGTextCtrl(self, -1, size=(57,20), style=0) 284 317 self.smear_description_smear_type.SetValue(str(self.dq_l)) 285 318 self.SetBackgroundColour(self.GetParent().GetBackgroundColour()) … … 287 320 self.smear_message_new_s = wx.StaticText(self, -1, smear_message_new_ssmear , style=wx.ALIGN_LEFT) 288 321 self.smear_message_new_p = wx.StaticText(self, -1, smear_message_new_psmear , style=wx.ALIGN_LEFT) 322 self.smear_description_2d_x = wx.StaticText(self, -1, smear_message_2d_x_title , style=wx.ALIGN_LEFT) 323 self.smear_description_2d_y = wx.StaticText(self, -1, smear_message_2d_y_title , style=wx.ALIGN_LEFT) 289 324 self.smear_description_pin_min = wx.StaticText(self, -1, smear_message_pinhole_min_title , style=wx.ALIGN_LEFT) 290 325 self.smear_description_pin_max = wx.StaticText(self, -1, smear_message_pinhole_max_title , style=wx.ALIGN_LEFT) … … 300 335 self.sizer_set_smearer.Add( self.smear_description_2d,0, wx.CENTER, 10 ) 301 336 self.sizer_new_smear.Add( self.smear_description_type,0, wx.CENTER, 10 ) 337 self.sizer_new_smear.Add( self.smear_description_accuracy_type,0, wx.CENTER, 10 ) 338 self.sizer_new_smear.Add( self.smear_accuracy ) 302 339 self.sizer_new_smear.Add( self.smear_description_smear_type,0, wx.CENTER, 10 ) 303 340 self.sizer_new_smear.Add((15,-1)) 341 self.sizer_new_smear.Add( self.smear_description_2d_x,0, wx.CENTER, 10 ) 304 342 self.sizer_new_smear.Add( self.smear_description_pin_min,0, wx.CENTER, 10 ) 305 343 self.sizer_new_smear.Add( self.smear_description_slit_height,0, wx.CENTER, 10 ) 344 306 345 self.sizer_new_smear.Add( self.smear_pinhole_min,0, wx.CENTER, 10 ) 307 346 self.sizer_new_smear.Add( self.smear_slit_height,0, wx.CENTER, 10 ) 308 347 self.sizer_new_smear.Add( self.smear_data_left,0, wx.CENTER, 10 ) 309 348 self.sizer_new_smear.Add((20,-1)) 349 self.sizer_new_smear.Add( self.smear_description_2d_y,0, wx.CENTER, 10 ) 310 350 self.sizer_new_smear.Add( self.smear_description_pin_max,0, wx.CENTER, 10 ) 311 351 self.sizer_new_smear.Add( self.smear_description_slit_width,0, wx.CENTER, 10 ) 352 312 353 self.sizer_new_smear.Add( self.smear_pinhole_max,0, wx.CENTER, 10 ) 313 354 self.sizer_new_smear.Add( self.smear_slit_width,0, wx.CENTER, 10 ) … … 336 377 self.enable_smearer.Disable() 337 378 else: 338 self.smear_description_2d.Show(True) 339 self.pinhole_smearer.Disable() 379 self.smear_description_none.Show(True) 380 #self.smear_description_2d.Show(True) 381 #self.pinhole_smearer.Disable() 340 382 self.slit_smearer.Disable() 341 self.enable_smearer.Disable()383 #self.enable_smearer.Disable() 342 384 else: self._show_smear_sizer() 343 385 boxsizer_range.Add(self.sizer_set_masking) … … 811 853 if self.enable_smearer.GetValue(): 812 854 temp_smear= self.smearer 813 self.compute_chisqr(temp_smear)855 #self.compute_chisqr(temp_smear) 814 856 except: 815 857 ## error occured on chisqr computation … … 864 906 qmax= float(self.qmax_x)) 865 907 if flag: 866 self.compute_chisqr(smearer= temp_smearer)908 #self.compute_chisqr(smearer= temp_smearer) 867 909 868 910 ## new state posted … … 975 1017 wx.PostEvent(self.parent.parent, StatusEvent(status = msg )) 976 1018 977 self.compute_chisqr() 1019 self._draw_model() 1020 ##update chi2 1021 #self.compute_chisqr(smearer= temp_smearer) 978 1022 #self._undo.Enable(True) 979 1023 self.save_current_state() … … 981 1025 wx.PostEvent(self.parent, event) 982 1026 self.state_change= False 983 self._draw_model() 1027 984 1028 return 985 1029 def _clear_Err_on_Fit(self): … … 1046 1090 self.dq_l = None 1047 1091 self.dq_r = None 1048 1049 if self.data is None or\ 1050 self.data.__class__.__name__ == 'Data2D': 1092 data = self.data 1093 if self.data is None: 1051 1094 return 1052 1053 data = self.data 1095 elif self.data.__class__.__name__ == 'Data2D': 1096 if data.dqx_data == None or data.dqy_data ==None: 1097 return 1098 elif self.smearer != None and all(data.dqx_data !=0)and all(data.dqy_data !=0): 1099 self.smear_type = "Pinhole2d" 1100 self.dq_l = format_number(numpy.average(data.dqx_data)) 1101 self.dq_r = format_number(numpy.average(data.dqy_data)) 1102 return 1103 else: 1104 return 1105 1106 1054 1107 1055 1108 # check if it is pinhole smear and get min max if it is. 1056 1109 if data.dx != None and all(data.dx !=0): 1057 1110 self.smear_type = "Pinhole" 1058 self.dq_l = numpy.min(data.dx)1059 self.dq_r = numpy.max(data.dx)1111 self.dq_l = data.dx[0] 1112 self.dq_r = data.dx[-1] 1060 1113 1061 1114 # check if it is slit smear and get min max if it is. … … 1073 1126 Show only the sizers depending on smear selection 1074 1127 """ 1128 # smear disabled 1075 1129 if self.disable_smearer.GetValue(): 1076 1130 self.smear_description_none.Show(True) 1131 # 2Dsmear 1132 elif self._is_2D(): 1133 self.smear_description_accuracy_type.Show(True) 1134 self.smear_accuracy.Show(True) 1135 self.smear_description_accuracy_type.Show(True) 1136 self.smear_description_2d.Show(True) 1137 self.smear_description_2d_x.Show(True) 1138 self.smear_description_2d_y.Show(True) 1139 if self.pinhole_smearer.GetValue(): 1140 self.smear_pinhole_min.Show(True) 1141 self.smear_pinhole_max.Show(True) 1142 # smear from data 1077 1143 elif self.enable_smearer.GetValue(): 1144 1078 1145 self.smear_description_dqdata.Show(True) 1079 1146 if self.smear_type != None: 1080 self.smear_description_type.Show(True)1081 1147 self.smear_description_smear_type.Show(True) 1082 1148 if self.smear_type == 'Slit': 1083 1149 self.smear_description_slit_height.Show(True) 1084 self.smear_description_slit_width.Show(True) 1150 self.smear_description_slit_width.Show(True) 1085 1151 elif self.smear_type == 'Pinhole': 1086 1152 self.smear_description_pin_min.Show(True) 1087 1153 self.smear_description_pin_max.Show(True) 1154 self.smear_description_smear_type.Show(True) 1155 self.smear_description_type.Show(True) 1088 1156 self.smear_data_left.Show(True) 1089 1157 self.smear_data_right.Show(True) 1158 # custom pinhole smear 1090 1159 elif self.pinhole_smearer.GetValue(): 1091 self.smear_message_new_p.Show(True) 1092 self.smear_description_pin_min.Show(True) 1160 if self.smear_type == 'Pinhole': 1161 self.smear_message_new_p.Show(True) 1162 self.smear_description_pin_min.Show(True) 1163 self.smear_description_pin_max.Show(True) 1164 1093 1165 self.smear_pinhole_min.Show(True) 1094 self.smear_description_pin_max.Show(True)1095 1166 self.smear_pinhole_max.Show(True) 1167 # custom slit smear 1096 1168 elif self.slit_smearer.GetValue(): 1097 1169 self.smear_message_new_s.Show(True) … … 1109 1181 self.smear_description_type.Hide() 1110 1182 self.smear_description_smear_type.Hide() 1183 self.smear_description_accuracy_type.Hide() 1184 self.smear_description_2d_x.Hide() 1185 self.smear_description_2d_y.Hide() 1111 1186 self.smear_description_2d.Hide() 1187 1188 self.smear_accuracy.Hide() 1112 1189 self.smear_data_left.Hide() 1113 1190 self.smear_data_right.Hide() … … 1123 1200 self.smear_message_new_s.Hide() 1124 1201 1202 def _set_accuracy_list(self): 1203 """ 1204 Set the list of an accuracy in 2D custum smear: Xhigh, High, Med, or Low 1205 """ 1206 # list of accuracy choices 1207 list = ['Low','Med','High','Xhigh'] 1208 for idx in range(len(list)): 1209 self.smear_accuracy.Append(list[idx],idx) 1210 1211 def _on_select_accuracy(self,event): 1212 """ 1213 Select an accuracy in 2D custom smear: Xhigh, High, Med, or Low 1214 """ 1215 #event.Skip() 1216 accuracy = event.GetEventObject() 1217 1218 self.smear2d_accuracy = accuracy.GetValue() 1219 if self.pinhole_smearer.GetValue(): 1220 self.onPinholeSmear(event=None) 1221 1222 else: self.onSmear(event=None) 1223 if self.current_smearer !=None: 1224 self.current_smearer.set_accuracy(accuracy = self.smear2d_accuracy) 1225 event.Skip() 1125 1226 def _onMask(self, event): 1126 1227 """ … … 1136 1237 1137 1238 def _draw_masked_model(self,event): 1239 """ 1240 Draw model image w/mask 1241 """ 1138 1242 event.Skip() 1139 1243 … … 1177 1281 # more disables for 2D 1178 1282 if self.data.__class__.__name__ =="Data2D": 1179 if self.model != None:1180 self.smear_description_2d.Show(True)1181 self.smear_description_none.Hide()1182 self.pinhole_smearer.Disable()1183 1283 self.slit_smearer.Disable() 1184 1284 self.default_mask = copy.deepcopy(self.data.mask) … … 1437 1537 "Smear: %s"%msg)) 1438 1538 return 1539 1439 1540 # Need update param values 1440 self._update_paramv_on_fit() 1541 self._update_paramv_on_fit() 1542 1441 1543 # msg default 1442 1544 msg = None … … 1457 1559 # hide all silt sizer 1458 1560 self._hide_all_smear_info() 1459 1561 1460 1562 ##Calculate chi2 1461 self.compute_chisqr(smearer= self.current_smearer) 1563 temp_smearer = self.current_smearer 1564 #self.compute_chisqr(smearer= temp_smearer) 1565 1462 1566 # show relevant slit sizers 1463 1567 self._show_smear_sizer() 1568 1464 1569 self.sizer_set_smearer.Layout() 1465 1570 self.Layout() … … 1505 1610 # copy data 1506 1611 data = copy.deepcopy(self.data) 1507 data_len = len(data.x) 1508 data.dx = None 1509 data.dxl = None 1510 data.dxw = None 1612 if self._is_2D(): 1613 self.smear_type = 'Pinhole2d' 1614 len_data = len(data.data) 1615 data.dqx_data = numpy.zeros(len_data) 1616 data.dqy_data = numpy.zeros(len_data) 1617 else: 1618 self.smear_type = 'Pinhole' 1619 len_data = len(data.x) 1620 data.dx = numpy.zeros(len_data) 1621 data.dxl = None 1622 data.dxw = None 1511 1623 msg = None 1512 1624 1513 # make sure once more if it is smearer1514 data = copy.deepcopy(self.data)1515 len_data = len(data.x)1516 data.dx = numpy.zeros(len_data)1517 1625 get_pin_min = self.smear_pinhole_min 1518 1626 get_pin_max = self.smear_pinhole_max 1519 1627 1520 if self._validate_qrange(get_pin_min, get_pin_max ): 1628 if not check_float(get_pin_min): 1629 get_pin_min.SetBackgroundColour("pink") 1630 msg= "Model Error:wrong value entered!!!" 1631 elif not check_float(get_pin_max ): 1632 get_pin_max.SetBackgroundColour("pink") 1633 msg= "Model Error:wrong value entered!!!" 1634 else: 1521 1635 if len_data < 2: len_data = 2 1522 1636 self.dx_min = float(get_pin_min.GetValue()) 1523 1637 self.dx_max = float(get_pin_max.GetValue()) 1524 if self.dx_min != None and self.dx_max != None: 1525 if self.dx_min == self.dx_max: 1638 if self.dx_min < 0: 1639 get_pin_min.SetBackgroundColour("pink") 1640 msg= "Model Error:This value can not be negative!!!" 1641 elif self.dx_max <0: 1642 get_pin_max.SetBackgroundColour("pink") 1643 msg= "Model Error:This value can not be negative!!!" 1644 elif self.dx_min != None and self.dx_max != None: 1645 if self._is_2D(): 1646 data.dqx_data[data.dqx_data==0] = self.dx_min 1647 data.dqy_data[data.dqy_data==0] = self.dx_max 1648 elif self.dx_min == self.dx_max: 1526 1649 data.dx[data.dx==0] = self.dx_min 1527 1650 else: 1528 step = math.fabs(self.dx_max - self.dx_min)/(len_data-1)1651 step = (self.dx_max - self.dx_min)/(len_data-1) 1529 1652 data.dx = numpy.arange(self.dx_min,self.dx_max+step/1.1,step) 1530 1653 elif self.dx_min != None: 1531 data.dx[data.dx==0] = self.dx_min 1654 if self._is_2D(): data.dqx_data[data.dqx_data==0] = self.dx_min 1655 else: data.dx[data.dx==0] = self.dx_min 1532 1656 elif self.dx_max != None: 1533 data.dx[data.dx==0] = self.dx_max 1657 if self._is_2D(): data.dqy_data[data.dqy_data==0] = self.dx_max 1658 else: data.dx[data.dx==0] = self.dx_max 1534 1659 self.current_smearer = smear_selection(data) 1535 1660 1661 if msg != None: 1662 wx.PostEvent(self.manager.parent, StatusEvent(status = msg )) 1536 1663 else: 1537 get_pin_min.SetBackgroundColour("pink") 1538 get_pin_max.SetBackgroundColour("pink") 1539 msg= "Model Error:wrong value entered!!!" 1540 wx.PostEvent(self.manager.parent, StatusEvent(status = msg )) 1664 get_pin_min.SetBackgroundColour("white") 1665 get_pin_max.SetBackgroundColour("white") 1541 1666 ## set smearing value whether or not the data contain the smearing info 1542 1667 self.manager.set_smearer(smearer=self.current_smearer, qmin= float(self.qmin_x),qmax= float(self.qmax_x)) … … 1579 1704 # Need update param values 1580 1705 self._update_paramv_on_fit() 1706 1581 1707 # msg default 1582 1708 msg = None … … 1600 1726 self._hide_all_smear_info() 1601 1727 ##Calculate chi2 1602 self.compute_chisqr(smearer= self.current_smearer)1728 #self.compute_chisqr(smearer= self.current_smearer) 1603 1729 # show relevant slit sizers 1604 1730 self._show_smear_sizer() … … 1725 1851 are compute when fitting 1726 1852 """ 1853 if event != None: 1854 event.Skip() 1727 1855 if self.check_invalid_panel(): 1728 1856 return … … 1732 1860 "Smear: %s"%msg)) 1733 1861 return 1862 1734 1863 # Need update param values 1735 1864 self._update_paramv_on_fit() 1736 1865 1737 1866 temp_smearer = None 1738 1867 self._get_smear_info() … … 1743 1872 self.smear_data_left.SetValue(str(self.dq_l)) 1744 1873 self.smear_data_right.SetValue(str(self.dq_r)) 1745 1874 1746 1875 self._hide_all_smear_info() 1747 1876 … … 1749 1878 # make sure once more if it is smearer 1750 1879 self.current_smearer = smear_selection(data) 1751 1880 1752 1881 if self.enable_smearer.GetValue(): 1753 temp_smearer= self.current_smearer1754 1882 if hasattr(self.data,"dxl"): 1755 1883 … … 1762 1890 else: 1763 1891 wx.PostEvent(self.manager.parent, StatusEvent(status=\ 1764 "Data contains smearing information %s"%msg)) 1765 self.smear_description_dqdata.Show(True) 1892 "Data contains smearing information")) 1893 1894 #self.smear_description_dqdata.Show(True) 1766 1895 self.smear_data_left.Show(True) 1767 1896 self.smear_data_right.Show(True) 1897 temp_smearer= self.current_smearer 1768 1898 elif self.disable_smearer.GetValue(): 1769 1899 self.smear_description_none.Show(True) 1770 1900 1771 1901 self._show_smear_sizer() 1902 1772 1903 self.sizer_set_smearer.Layout() 1773 1904 self.Layout() … … 1775 1906 self.manager.set_smearer(smearer=temp_smearer, qmin= float(self.qmin_x), 1776 1907 qmax= float(self.qmax_x)) 1908 1777 1909 ##Calculate chi2 1778 self.compute_chisqr(smearer= temp_smearer)1910 #self.compute_chisqr(smearer= temp_smearer) 1779 1911 1780 1912 self.state.enable_smearer= self.enable_smearer.GetValue() … … 1782 1914 self.state.pinhole_smearer = self.pinhole_smearer.GetValue() 1783 1915 self.state.slit_smearer = self.slit_smearer.GetValue() 1916 1917 def on_complete_chisqr(self, event): 1918 """ 1919 print result chisqr 1920 @event: activated by fitting/ complete after draw 1921 """ 1922 try: 1923 if event ==None: 1924 output= "-" 1925 else: 1926 output = event.output 1927 self.tcChi.SetValue(str(format_number(output))) 1928 1929 self.state.tcChi =self.tcChi 1930 except: 1931 pass 1784 1932 1785 1933 def complete_chisqr(self, output, elapsed=None): … … 1794 1942 1795 1943 self.state.tcChi =self.tcChi 1796 1944 1797 1945 except: 1798 1946 pass … … 1829 1977 1830 1978 1831 def compute_chisqr2D(self ):1979 def compute_chisqr2D(self,smearer=None): 1832 1980 """ 1833 1981 compute chi square given a model and data 2D and set the value … … 1843 1991 if self.calc_Chisqr!= None and self.calc_Chisqr.isrunning(): 1844 1992 self.calc_Chisqr.stop() 1845 1993 if smearer !=None: 1994 smearer.set_accuracy(accuracy = self.smear2d_accuracy) 1846 1995 self.calc_Chisqr= CalcChisqr2D( data2d= self.data, 1847 1996 model= self.model, 1997 smearer=smearer, 1848 1998 qmin= self.qmin_x, 1849 1999 qmax = self.qmax_x, … … 1857 2007 1858 2008 1859 def compute_chisqr(self , smearer=None):2009 def compute_chisqr(self ,output=None, smearer=None): 1860 2010 """ 1861 2011 compute chi square given a model and data 1D and set the value … … 1866 2016 try: 1867 2017 if hasattr(self.data,"data"): 1868 self.compute_chisqr2D( )2018 self.compute_chisqr2D(smearer=smearer) 1869 2019 return 1870 2020 else: -
sansview/perspectives/fitting/fitting.py
r784e2fa rf72333f 44 44 (PageInfoEvent, EVT_PAGE_INFO) = wx.lib.newevent.NewEvent() 45 45 46 46 from fitpage import Chi2UpdateEvent 47 47 class PlotInfo: 48 48 """ … … 104 104 #dictionary containing data name and error on dy of that data 105 105 self.err_dy = {} 106 106 self.theory_data = None 107 107 108 108 … … 146 146 self.fit_panel.set_model_list(self.menu_mng.get_model_list()) 147 147 owner.Bind(fitpage.EVT_MODEL_BOX,self._on_model_panel) 148 148 149 149 #create menubar items 150 150 return [(id, self.menu1, "Fitting")] … … 370 370 wx.PostEvent(self.parent, StatusEvent(status="Fitting \ 371 371 is cancelled" , type="stop")) 372 373 def set_smearer_nodraw(self,smearer, qmin=None, qmax=None): 374 """ 375 Get a smear object and store it to a fit problem 376 @param smearer: smear object to allow smearing data 377 """ 378 self.current_pg=self.fit_panel.get_current_page() 379 self.page_finder[self.current_pg].set_smearer(smearer) 380 ## draw model 1D with smeared data 381 data = self.page_finder[self.current_pg].get_fit_data() 382 model = self.page_finder[self.current_pg].get_model() 383 ## if user has already selected a model to plot 384 ## redraw the model with data smeared 385 386 smear =self.page_finder[self.current_pg].get_smearer() 372 387 373 388 def set_smearer(self,smearer, qmin=None, qmax=None): … … 383 398 ## if user has already selected a model to plot 384 399 ## redraw the model with data smeared 385 400 386 401 smear =self.page_finder[self.current_pg].get_smearer() 387 402 if model!= None: … … 405 420 406 421 """ 407 ## draw model 1D with no loaded data 408 self._draw_model1D( model= model, data= data,enable1D=enable1D, smearer= smearer, 409 qmin= qmin, qmax= qmax, qstep= qstep ) 410 ## draw model 2D with no initial data 411 self._draw_model2D(model=model, 412 data = data, 413 enable2D= enable2D, 414 qmin=qmin, 415 qmax=qmax, 416 qstep=qstep) 422 423 if data.__class__.__name__ !="Data2D": 424 ## draw model 1D with no loaded data 425 self._draw_model1D( model= model, data= data, 426 enable1D=enable1D, 427 smearer= smearer, 428 qmin= qmin, qmax= qmax, qstep= qstep ) 429 else: 430 ## draw model 2D with no initial data 431 self._draw_model2D(model=model, 432 data = data, 433 enable2D= enable2D, 434 smearer= smearer, 435 qmin=qmin, 436 qmax=qmax, 437 qstep=qstep) 417 438 418 439 def onFit(self): … … 600 621 wx.PostEvent(self.parent, StatusEvent(status=msg, info="error")) 601 622 return 602 623 624 603 625 def _onEVT_SLICER_PANEL(self, event): 604 626 """ … … 1087 1109 1088 1110 1089 def _complete1D(self, x,y, elapsed, model,data=None):1111 def _complete1D(self, x,y, elapsed,index,model,data=None): 1090 1112 """ 1091 1113 Complete plotting 1D data … … 1106 1128 1107 1129 title= new_plot.name 1130 # x, y are only in range of index 1131 self.theory_data = new_plot 1108 1132 #new_plot.perspective = self.get_perspective() 1109 1133 # Pass the reset flag to let the plotting event handler … … 1116 1140 else: 1117 1141 wx.PostEvent(self.parent, NewPlotEvent(plot=new_plot,title= str(title))) 1142 # Chisqr in fitpage 1143 current_pg=self.fit_panel.get_current_page() 1144 wx.PostEvent(current_pg,Chi2UpdateEvent(output=self._cal_chisqr(data=data,index=index))) 1118 1145 msg = "Plot 1D complete !" 1119 1146 wx.PostEvent( self.parent, StatusEvent(status=msg , type="stop" )) … … 1134 1161 1135 1162 1136 def _complete2D(self, image,data, model, elapsed, qmin, qmax,qstep=DEFAULT_NPTS):1163 def _complete2D(self, image,data, model, elapsed,index,qmin, qmax,qstep=DEFAULT_NPTS): 1137 1164 """ 1138 1165 Complete get the result of modelthread and create model 2D … … 1165 1192 theory.xmin= data.xmin 1166 1193 theory.xmax= data.xmax 1167 1168 1194 1195 self.theory_data = theory 1169 1196 ## plot 1170 1197 wx.PostEvent(self.parent, NewPlotEvent(plot=theory, 1171 1198 title="Analytical model 2D ", reset=True )) 1199 # Chisqr in fitpage 1200 current_pg=self.fit_panel.get_current_page() 1201 wx.PostEvent(current_pg,Chi2UpdateEvent(output=self._cal_chisqr(data=data,index=index))) 1172 1202 msg = "Plot 2D complete !" 1173 1203 wx.PostEvent( self.parent, StatusEvent( status= msg , type="stop" )) 1174 1204 1175 1205 def _on_data_error(self, event): 1176 1206 """ … … 1179 1209 """ 1180 1210 self.err_dy = event.err_dy 1181 #print "receiving error dy",self.err_dy1182 1211 1183 def _draw_model2D(self,model,data=None, description=None, enable2D=False,1212 def _draw_model2D(self,model,data=None, smearer= None,description=None, enable2D=False, 1184 1213 qmin=DEFAULT_QMIN, qmax=DEFAULT_QMAX, qstep=DEFAULT_NPTS): 1185 1214 """ … … 1208 1237 x= data.x_bins 1209 1238 y= data.y_bins 1210 1239 1211 1240 if not enable2D: 1212 return 1241 return None,None 1213 1242 try: 1214 1243 from model_thread import Calc2D … … 1216 1245 if self.calc_2D != None and self.calc_2D.isrunning(): 1217 1246 self.calc_2D.stop() 1247 1218 1248 self.calc_2D = Calc2D( x= x, 1219 1249 y= y, 1220 1250 model= model, 1221 1251 data = data, 1252 smearer = smearer, 1222 1253 qmin= qmin, 1223 1254 qmax= qmax, … … 1226 1257 updatefn= self._update2D ) 1227 1258 self.calc_2D.queue() 1228 1259 1229 1260 except: 1230 1261 msg= " Error occurred when drawing %s Model 2D: "%model.name 1231 1262 msg+= " %s"%sys.exc_value 1232 1263 wx.PostEvent( self.parent, StatusEvent(status= msg )) 1233 return 1264 1234 1265 1235 1266 def _draw_model1D(self, model, data=None, smearer= None, … … 1257 1288 1258 1289 if not enable1D: 1259 return 1290 return 1260 1291 1261 1292 try: … … 1273 1304 updatefn = self._update1D ) 1274 1305 self.calc_1D.queue() 1275 1306 1276 1307 except: 1277 1308 msg= " Error occurred when drawing %s Model 1D: "%model.name 1278 1309 msg+= " %s"%sys.exc_value 1279 1310 wx.PostEvent( self.parent, StatusEvent(status= msg )) 1280 return 1281 1282 1311 1312 def _cal_chisqr(self, data=None, index=None): 1313 """ 1314 Get handy Chisqr using the output from draw1D and 2D, 1315 instead of calling expansive CalcChisqr in guithread 1316 """ 1317 # default chisqr 1318 chisqr = None 1319 # return None if data == None 1320 if data == None: return chisqr 1321 1322 # Get data: data I, theory I, and data dI in order 1323 if data.__class__.__name__ =="Data2D": 1324 if index == None: index = numpy.ones(len(data.data),ntype=bool) 1325 fn = data.data[index] 1326 gn = self.theory_data.data[index] 1327 en = data.err_data[index] 1328 else: 1329 # 1 d theory from model_thread is only in the range of index 1330 if index == None: index = numpy.ones(len(data.y),ntype=bool) 1331 1332 fn = data.y[index] 1333 gn = self.theory_data.y 1334 en = data.dy[index] 1335 1336 # residual 1337 res = (fn - gn)/en 1338 # get chisqr only w/finite 1339 chisqr = numpy.average(res[numpy.isfinite(res)]*res[numpy.isfinite(res)]) 1340 1341 return chisqr 1342 1343 1283 1344 def profile(fn, *args, **kw): 1284 1345 import cProfile, pstats, os -
sansview/perspectives/fitting/gui_thread.py
r425ef040 rf72333f 10 10 from data_util.calcthread import CalcThread 11 11 from sans.fit.AbstractFitEngine import FitData2D, FitData1D, SansAssembly 12 12 from DataLoader.smearing_2d import Smearer2D 13 13 14 14 class CalcChisqr1D(CalcThread): … … 87 87 88 88 def __init__(self,data2d, model, 89 smearer, 89 90 qmin, 90 91 qmax, … … 105 106 msg= str(data2d.__class__.__name__) 106 107 raise ValueError, "need Data2D to compute chisqr. Current class %s"%msg 107 108 self.smearer = smearer 108 109 self.fitdata = FitData2D(sans_data2d=data2d ,data=data2d.data, err_data=data2d.err_data) 109 110 self.fitdata.setFitRange(qmin=qmin,qmax=qmax) 110 111 111 112 self.model = model 112 113 113 114 self.starttime = 0 114 115 … … 128 129 """ 129 130 self.starttime = time.time() 130 131 131 132 output= None 132 133 res=[] 133 134 try: 134 res = self.fitdata.residuals(self.model.evalDistribution) 135 if self.smearer == None: 136 fn = self.model.evalDistribution 137 else: 138 # Set model in smearer 139 self.smearer.set_model(self.model) 140 # set fn 141 fn = self.smearer 142 # set self.smearer != None in fitdata 143 self.fitdata.set_smearer(fn) 144 145 res = self.fitdata.residuals(fn) 135 146 sum=0 136 147 for item in res: -
sansview/perspectives/fitting/model_thread.py
r51a71a3 rf72333f 3 3 import sys 4 4 import numpy,math 5 5 from DataLoader.smearing_2d import Smearer2D 6 import fitpage 6 7 class Calc2D(CalcThread): 7 8 """ … … 12 13 """ 13 14 14 def __init__(self, x, y, data,model, qmin, qmax,qstep,15 def __init__(self, x, y, data,model,smearer,qmin, qmax,qstep, 15 16 completefn = None, 16 17 updatefn = None, … … 31 32 # the model on to calculate 32 33 self.model = model 34 self.smearer = smearer#(data=self.data,model=self.model) 33 35 self.starttime = 0 34 36 … … 51 53 self.qx_data = self.data.qx_data 52 54 self.qy_data = self.data.qy_data 55 self.dqx_data = self.data.dqx_data 56 self.dqy_data = self.data.dqy_data 53 57 self.mask = self.data.mask 54 58 else: … … 86 90 # Only qmin value will be consider for the detector 87 91 index_model = index_data 88 89 value = self.model.evalDistribution([self.qx_data[index_model],self.qy_data[index_model]] ) 92 93 if self.smearer != None: 94 # Set smearer w/ data, model and index. 95 fn = self.smearer 96 fn.set_model(self.model) 97 fn.set_index(index_model) 98 # Get necessary data from self.data and set the data for smearing 99 fn.get_data() 100 # Calculate smeared Intensity (by Gaussian averaging): DataLoader/smearing2d/Smearer2D() 101 value = fn.get_value() 102 103 else: 104 # calculation w/o smearing 105 value = self.model.evalDistribution([self.qx_data[index_model],self.qy_data[index_model]]) 90 106 91 107 output = numpy.zeros(len(self.qx_data)) 92 108 93 109 # output default is None 94 # This method is to distinguish between masked point and data point = 0.110 # This method is to distinguish between masked point(nan) and data point = 0. 95 111 output = output/output 96 112 # set value for self.mask==True, else still None to Plottools 97 113 output[index_model] = value 114 98 115 elapsed = time.time()-self.starttime 99 116 self.complete( image = output, … … 101 118 model = self.model, 102 119 elapsed = elapsed, 120 index = index_model, 103 121 qmin = self.qmin, 104 122 qmax = self.qmax, 105 123 qstep = self.qstep ) 106 124 107 108 109 125 110 126 class Calc1D(CalcThread): … … 153 169 154 170 self.complete(x= self.x[index], y= output[index], 155 elapsed=elapsed, model= self.model, data=self.data) 156 157 171 elapsed=elapsed,index=index, model= self.model, data=self.data) 172 173 174 def results(self): 175 """ 176 Send resuts of the computation 177 """ 178 return [self.out, self.index] 158 179 159 180 class CalcCommandline:
Note: See TracChangeset
for help on using the changeset viewer.