Changeset 4faf4ba in sasview for sansview/perspectives/fitting
- Timestamp:
- Aug 19, 2009 12:13:50 AM (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:
- 191b369
- Parents:
- 27953d1
- Location:
- sansview/perspectives/fitting
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
sansview/perspectives/fitting/fitting.py
r1b001a7 r4faf4ba 26 26 from sans.fit.AbstractFitEngine import Model 27 27 from sans.fit.AbstractFitEngine import FitAbort 28 28 29 29 30 from fitproblem import FitProblem … … 126 127 127 128 self.menu1.AppendSeparator() 128 129 129 id1 = wx.NewId() 130 130 simul_help = "Allow to edit fit engine with multiple model and data" 131 131 self.menu1.Append(id1, '&Simultaneous Page',simul_help) 132 132 wx.EVT_MENU(owner, id1, self.on_add_sim_page) 133 #menu for SLD Calculator 134 self.menu1.AppendSeparator() 135 sld_menu = wx.Menu() 136 sld_id = wx.NewId() 137 sld_help= "Compute the scattering length density of molecules" 138 self.menu1.Append(sld_id, "SLD Calculator",sld_help) 139 wx.EVT_MENU(owner,sld_id, self.onCalculateSld) 133 140 134 141 #menu for model 135 142 menu2 = wx.Menu() 136 137 143 self.menu_mng.populate_menu(menu2, owner) 138 144 id2 = wx.NewId() … … 516 522 return 517 523 518 519 524 def onCalculateSld(self, event): 525 """ 526 Compute the scattering length density of molecula 527 """ 528 from sldPanel import SldWindow 529 frame = SldWindow(base=self.parent) 530 frame.Show(True) 531 532 520 533 def _onEVT_SLICER_PANEL(self, event): 521 534 """ -
sansview/perspectives/fitting/helpPanel.py
r6fb01b6 r4faf4ba 59 59 60 60 class HelpWindow(wx.Frame): 61 def __init__(self, parent, id, title ):61 def __init__(self, parent, id, title= 'HelpWindow', pageToOpen=None): 62 62 wx.Frame.__init__(self, parent, id, title, size=(700, 450)) 63 63 """ … … 118 118 <li><a href ="doc/model_use_help.html" target ="showframe">Visualize Model</a><br></li> 119 119 <li><a href ="doc/averaging_help.html" target ="showframe">Data Averaging</a><br></li> 120 <li><a href ="doc/sld_calculator_help.html" target ="showframe">SLD Calculator</a><br></li> 120 121 <li><a href ="doc/model_functions.html" target ="showframe">Model Functions</a><br></li> 121 122 </ul> … … 126 127 self.lhelp.Bind(wx.html.EVT_HTML_LINK_CLICKED,self.OnLinkClicked ) 127 128 128 # 'HelpWindow' from menu bar, title=model name from 'More Details' button in model penal129 if title != 'HelpWindow':130 self.rhelp.LoadPage( "doc/model_functions.html")129 #open the help frame a the current page 130 if pageToOpen!= None: 131 self.rhelp.LoadPage(str( pageToOpen)) 131 132 132 133 vbox.Add(self.rhelp,1, wx.EXPAND) -
sansview/perspectives/fitting/model_thread.py
rcad821b r4faf4ba 122 122 123 123 ######Temp. FIX for Qrange w/ smear. #ToDo: Should not pass all the data to 'run' or 'smear'... 124 new_index = (self.qmin > self.x) |(self.x > self.qmax)125 output[new_index] = None124 #new_index = (self.qmin > self.x) |(self.x > self.qmax) 125 #output[new_index] = None 126 126 127 127 elapsed = time.time()-self.starttime -
sansview/perspectives/fitting/modelpage.py
r58c6ba6 r4faf4ba 433 433 else: 434 434 name = self.model.name 435 frame = HelpWindow(None, -1, name)435 frame = HelpWindow(None, -1, pageToOpen="doc/model_functions.html") 436 436 frame.Show(True) 437 437 if frame.rhelp.HasAnchor(name): -
sansview/perspectives/fitting/sldPanel.py
r785c8233 r4faf4ba 1 """ 2 This module intends to compute the neutron scattering length density of molecule 3 @author: Gervaise B. Alina 4 """ 5 1 6 import wx 7 import sys 8 9 import periodictable 10 from periodictable import formula 11 from periodictable.xsf import xray_energy, xray_sld_from_atoms 12 from periodictable.constants import avogadro_number 13 import periodictable.nsf 14 neutron_sld_from_atoms= periodictable.nsf.neutron_sld_from_atoms 15 16 from sans.guiframe.utils import format_number, check_float 17 from sans.guicomm.events import StatusEvent 18 19 2 20 _BOX_WIDTH = 76 3 21 _STATICBOX_WIDTH = 350 22 _SCALE = 1e-6 23 _DEFAULT_WAVELENGTH = 1.798 24 4 25 5 26 class SldPanel(wx.Panel): 6 def __init__(self, parent, id = -1): 27 """ 28 Provides the SLD calculator GUI. 29 """ 30 def __init__(self, parent,base=None, id = -1): 7 31 wx.Panel.__init__(self, parent, id = id) 8 32 # Object that receive status event 33 self.base= base 9 34 self._do_layout() 10 35 self.SetAutoLayout(True) … … 96 121 self.neutron_inc_ctl.SetEditable(False) 97 122 self.neutron_inc_ctl.SetToolTipString("Neutron Inc. Xs") 123 neutron_inc_units_txt = wx.StaticText(self, -1, unit_cm1) 98 124 99 125 neutron_abs_txt = wx.StaticText(self, -1, 'Neutron Abs. Xs') … … 101 127 self.neutron_abs_ctl.SetEditable(False) 102 128 self.neutron_abs_ctl.SetToolTipString("Neutron Abs. Xs") 129 neutron_abs_units_txt = wx.StaticText(self, -1, unit_cm1) 103 130 104 131 neutron_length_txt = wx.StaticText(self, -1, 'Neutron 1/e length') … … 106 133 self.neutron_length_ctl.SetEditable(False) 107 134 self.neutron_length_ctl.SetToolTipString("Neutron 1/e length") 135 neutron_length_units_txt = wx.StaticText(self, -1, unit_cm) 108 136 iy = 0 109 137 ix = 0 … … 161 189 sizer_output.Add(self.neutron_inc_ctl,(iy, ix),(1,1), 162 190 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 191 ix +=2 192 sizer_output.Add(neutron_inc_units_txt,(iy, ix),(1,1), 193 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 163 194 iy += 1 164 195 ix = 0 … … 168 199 sizer_output.Add(self.neutron_abs_ctl,(iy, ix),(1,1), 169 200 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 201 ix +=2 202 sizer_output.Add(neutron_abs_units_txt,(iy, ix),(1,1), 203 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 170 204 iy += 1 171 205 ix = 0 … … 174 208 ix +=1 175 209 sizer_output.Add(self.neutron_length_ctl,(iy, ix),(1,1), 176 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 210 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 211 ix +=2 212 sizer_output.Add(neutron_length_units_txt,(iy, ix),(1,1), 213 wx.EXPAND|wx.ADJUST_MINSIZE, 0) 177 214 boxsizer2.Add( sizer_output ) 178 215 sizer2.Add(boxsizer2,0, wx.EXPAND | wx.ALL, 10) 179 216 #-----Button sizer------------ 180 217 id = wx.NewId() 218 button_help = wx.Button(self, id, "Help") 219 button_help.SetToolTipString("Help SlD calculations and formula.") 220 self.Bind(wx.EVT_BUTTON, self.onHelp, id = id) 221 222 id = wx.NewId() 181 223 button_calculate = wx.Button(self, id, "Calculate") 182 224 button_calculate.SetToolTipString("Calculate SlD of neutrons.") 183 #self.Bind(wx.EVT_BUTTON, self._calculateSld, id = id) 184 sizer_button.Add((20, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) 185 sizer_button.Add(button_calculate, 0, wx.RIGHT|wx.ADJUST_MINSIZE, 10) 225 self.Bind(wx.EVT_BUTTON, self.calculateSld, id = id) 226 227 sizer_button.Add((200, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) 228 sizer_button.Add(button_help, 0, wx.RIGHT|wx.ADJUST_MINSIZE,10) 229 sizer_button.Add(button_calculate, 0, wx.RIGHT|wx.ADJUST_MINSIZE,20) 186 230 sizer3.Add(sizer_button) 187 231 #---------layout---------------- … … 194 238 self.SetSizer(vbox) 195 239 196 240 def check_inputs(self): 241 """Check validity user inputs""" 242 flag= True 243 244 if check_float(self.density_ctl): 245 self.density = float(self.density_ctl.GetValue()) 246 else: 247 flag=False 248 raise ValueError,"Error for Density value :expect float" 249 250 self.wavelength= self.wavelength_ctl.GetValue() 251 if self.wavelength.lstrip().rstrip()=="": 252 self.wavelength = _DEFAULT_WAVELENGTH 253 else: 254 if check_float(self.wavelength_ctl): 255 self.wavelength= float(self.wavelength) 256 else: 257 flag = False 258 raise ValueError,"Error for wavelenth value :expect float" 259 260 self.formulata_text= self.compound_ctl.GetValue().lstrip().rstrip() 261 if self.formulata_text!="": 262 self.compound_ctl.SetBackgroundColour(wx.WHITE) 263 self.compound_ctl.Refresh() 264 else: 265 self.compound_ctl.SetBackgroundColour("pink") 266 self.compound_ctl.Refresh() 267 flag=False 268 raise ValueError, "Enter a formula" 269 return flag 270 271 def onHelp(self, event): 272 """ 273 provide more hint on the SLD calculator 274 """ 275 from helpPanel import HelpWindow 276 frame = HelpWindow(None, -1, pageToOpen="doc/sld_calculator_help.html") 277 frame.Show(True) 278 name = "SLD_calculator" 279 if frame.rhelp.HasAnchor(name): 280 frame.rhelp.ScrollToAnchor(name) 281 else: 282 msg= "Cannot find SLD Calculator description " 283 msg +="Please.Search in the Help window" 284 wx.PostEvent(self.base, StatusEvent(status = msg )) 285 286 287 def calculateSld(self, event): 288 """ 289 Calculate the neutron scattering density length of a molecule 290 """ 291 try: 292 #Check validity user inputs 293 if self.check_inputs(): 294 #get ready to compute 295 try: 296 self.new_formula = formula(self.formulata_text, density= self.density) 297 atom = self.new_formula.atoms 298 except: 299 if self.base !=None: 300 msg= "SLD Calculator: %s" % (sys.exc_value) 301 wx.PostEvent(self.base, StatusEvent(status= msg )) 302 else: 303 raise 304 return 305 # Compute the Cu SLD 306 Cu_reel, Cu_im = self.calculateXRaySld( "Cu", density= self.density, 307 user_formula= self.new_formula) 308 self.cu_ka_sld_reel_ctl.SetValue(format_number(Cu_reel*_SCALE)) 309 self.cu_ka_sld_im_ctl.SetValue(format_number(Cu_im*_SCALE)) 310 # Compute the Mo SLD 311 Mo_reel, Mo_im = self.calculateXRaySld( "Mo", density= self.density, 312 user_formula= self.new_formula) 313 self.mo_ka_sld_reel_ctl.SetValue(format_number(Mo_reel*_SCALE)) 314 self.mo_ka_sld_im_ctl.SetValue(format_number(Mo_im*_SCALE)) 315 316 coh,absorp,inc= self.calculateNSld(self.density, wavelength= self.wavelength, 317 user_formula= self.new_formula) 318 #Don't know if value is return in cm or cm^(-1).assume return in cm 319 # to match result of neutron inc of Alan calculator 320 inc= inc*1/10 321 #Doesn't match result of Alan calculator for absorption factor of 2 322 #multiplication of 100 is going around 323 absorp= absorp *2*100 324 volume= (self.new_formula.mass /self.density)/avogadro_number*1.0e24 325 #im: imaginary part of neutron SLD 326 im=0 327 for el, count in atom.iteritems(): 328 if el.neutron.b_c_i !=None: 329 im += el.neutron.b_c_i*count 330 im = im/volume 331 332 self.neutron_sld_reel_ctl.SetValue(format_number(coh*_SCALE)) 333 self.neutron_sld_im_ctl.SetValue(format_number(im*_SCALE)) 334 self.neutron_inc_ctl.SetValue(format_number(inc )) 335 self.neutron_abs_ctl.SetValue(format_number(absorp)) 336 #Don't know if value is return in cm or cm^(-1).assume return in cm 337 # to match result of neutron inc of Alan calculator 338 length= (coh+ absorp+ inc)/volume 339 self.neutron_length_ctl.SetValue(format_number(length)) 340 except: 341 if self.base !=None: 342 msg= "SLD Calculator: %s" % (sys.exc_value) 343 wx.PostEvent(self.base, StatusEvent(status= msg )) 344 else: 345 raise 346 return 347 348 def calculateXRaySld(self, element, density,user_formula): 349 """ 350 Get an element and compute the corresponding SLD for a given formula 351 @param element: elementis a string of existing atom 352 @param formula: molecule enters by the user 353 """ 354 try: 355 myformula = formula(str (element)) 356 if len(myformula.atoms)!=1: 357 return 358 element= myformula.atoms.keys()[0] 359 energy = xray_energy(element.K_alpha) 360 atom = user_formula.atoms 361 atom_reel, atom_im = xray_sld_from_atoms( atom, 362 density=density, 363 energy= energy ) 364 return atom_reel, atom_im 365 except: 366 if self.base !=None: 367 msg= "SLD Calculator: %s" % (sys.exc_value) 368 wx.PostEvent(self.base, StatusEvent(status= msg )) 369 else: 370 raise 371 return 372 373 def calculateNSld(self,density,wavelength,user_formula ): 374 """ 375 Compute the neutron SLD for a given molecule 376 @return absorp: absorption 377 @return coh: coherence cross section 378 @return inc: incoherence cross section 379 380 """ 381 if density ==0: 382 raise ZeroDivisionError,"integer division or modulo by zero for density" 383 return 384 atom = user_formula.atoms 385 coh,absorp,inc = neutron_sld_from_atoms(atom,density,wavelength) 386 387 return coh,absorp,inc 388 389 390 391 392 197 393 class SldWindow(wx.Frame): 198 def __init__(self, parent , id, title):394 def __init__(self, parent=None, id=1, title="SLD Calculator",base=None): 199 395 wx.Frame.__init__(self, parent, id, title, size=(400, 400)) 200 396 201 self.panel = SldPanel(self )397 self.panel = SldPanel(self, base=base) 202 398 self.Centre() 203 399 self.Show(True)
Note: See TracChangeset
for help on using the changeset viewer.