Changeset 66bfacf in sasview


Ignore:
Timestamp:
Dec 6, 2010 5:32:46 PM (13 years ago)
Author:
Gervaise Alina <gervyh@…>
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:
bdcc69e
Parents:
82826c4
Message:

add a unitest for gui sld calculator

Location:
calculatorview
Files:
1 added
2 edited

Legend:

Unmodified
Added
Removed
  • calculatorview/perspectives/calculator/sld_panel.py

    r6996942 r66bfacf  
    6868        self.neutron_inc_ctl = None 
    6969        self.neutron_length_ctl = None 
     70        self.button_calculate = None 
    7071        #Draw the panel 
    7172        self._do_layout() 
     
    274275     
    275276        id = wx.NewId() 
    276         button_calculate = wx.Button(self, id, "Calculate") 
    277         button_calculate.SetToolTipString("Calculate SLD.") 
     277        self.button_calculate = wx.Button(self, id, "Calculate") 
     278        self.button_calculate.SetToolTipString("Calculate SLD.") 
    278279        self.Bind(wx.EVT_BUTTON, self.calculateSld, id=id)    
    279280         
    280281        sizer_button.Add((250, 20), 1, wx.EXPAND|wx.ADJUST_MINSIZE, 0) 
    281         sizer_button.Add(button_calculate, 0, wx.RIGHT|wx.ADJUST_MINSIZE, 20) 
     282        sizer_button.Add(self.button_calculate, 0, wx.RIGHT|wx.ADJUST_MINSIZE, 20) 
    282283        sizer3.Add(sizer_button) 
    283284        #---------layout---------------- 
     
    309310        """Check validity user inputs""" 
    310311        flag = True 
    311         
     312        msg = "" 
    312313        if check_float(self.density_ctl): 
    313314            self.density = float(self.density_ctl.GetValue()) 
    314315        else: 
    315316            flag = False 
    316             raise ValueError,"Error for Density value :expect float" 
     317            msg += "Error for Density value :expect float" 
    317318     
    318319        self.wavelength = self.wavelength_ctl.GetValue() 
    319320        if self.wavelength.lstrip().rstrip() == "": 
    320321            self.wavelength = WAVELENGTH 
     322            self.wavelength_ctl.SetValue(str(WAVELENGTH)) 
     323            self.wavelength_ctl.SetBackgroundColour(wx.WHITE) 
     324            self.wavelength_ctl.Refresh() 
     325            msg += "Default value for wavelength is 6.0" 
    321326        else: 
    322327            if check_float(self.wavelength_ctl): 
     
    324329            else: 
    325330                flag = False 
    326                 raise ValueError, "Error for wavelength value :expect float" 
     331                msg += "Error for wavelength value :expect float" 
    327332                 
    328333        self.compound = self.compound_ctl.GetValue().lstrip().rstrip() 
    329334        if self.compound != "": 
    330             self.compound_ctl.SetBackgroundColour(wx.WHITE) 
    331             self.compound_ctl.Refresh() 
     335            try : 
     336                formula(self.compound) 
     337                self.compound_ctl.SetBackgroundColour(wx.WHITE) 
     338                self.compound_ctl.Refresh() 
     339            except: 
     340                self.compound_ctl.SetBackgroundColour("pink") 
     341                self.compound_ctl.Refresh() 
     342                flag = False 
     343                msg += "Enter correct formula" 
    332344        else: 
    333345            self.compound_ctl.SetBackgroundColour("pink") 
    334346            self.compound_ctl.Refresh() 
    335347            flag = False 
    336             raise ValueError, "Enter a formula" 
    337         return flag  
     348            msg += "Enter a formula" 
     349        return flag, msg 
    338350         
    339351    def calculate_sld_helper(self, element, density, molecule_formula): 
     
    357369            Calculate the neutron scattering density length of a molecule 
    358370        """ 
     371        self.clear_outputs() 
    359372        try: 
    360373            #Check validity user inputs 
    361             if self.check_inputs(): 
    362                 #get ready to compute 
    363                 try: 
    364                     self.sld_formula = formula(self.compound, 
    365                                                 density=self.density) 
    366                 except: 
    367                     if self.base is not None: 
    368                         msg = "SLD Calculator: %s"  % (sys.exc_value) 
    369                         wx.PostEvent(self.base, StatusEvent(status=msg)) 
    370                     else: 
    371                         raise 
    372                     return 
    373                  
    374                  
    375                 (sld_real, sld_im, _), (_, absorp, incoh), \ 
    376                             length = neutron_scattering(compound=self.compound, 
    377                                        density=self.density,  
    378                                        wavelength=self.wavelength)  
    379                 cu_real, cu_im = self.calculate_sld_helper(element="Cu", 
    380                                                      density=self.density, 
    381                                             molecule_formula=self.sld_formula) 
    382                 mo_real, mo_im = self.calculate_sld_helper(element="Mo",  
    383                                                            density=self.density, 
    384                                          molecule_formula=self.sld_formula) 
    385                 # set neutron sld values 
    386                 val = format_number(sld_real * _SCALE) 
    387                 self.neutron_sld_reel_ctl.SetValue(val) 
    388                 val = format_number(math.fabs(sld_im) * _SCALE) 
    389                 self.neutron_sld_im_ctl.SetValue(val) 
    390                 # Compute the Cu SLD 
    391                 self.cu_ka_sld_reel_ctl.SetValue(format_number(cu_real *_SCALE)) 
    392                 val = format_number(math.fabs(cu_im )* _SCALE) 
    393                 self.cu_ka_sld_im_ctl.SetValue(val) 
    394                 # Compute the Mo SLD 
    395                 self.mo_ka_sld_reel_ctl.SetValue(format_number(mo_real *_SCALE)) 
    396                 val = format_number(math.fabs(mo_im)* _SCALE) 
    397                 self.mo_ka_sld_im_ctl.SetValue(val) 
    398                 # set incoherence and absorption 
    399                 self.neutron_inc_ctl.SetValue(format_number(incoh)) 
    400                 self.neutron_abs_ctl.SetValue(format_number(absorp)) 
    401                 # Neutron length 
    402                 self.neutron_length_ctl.SetValue(format_number(length)) 
    403                 # display wavelength 
    404                 self.wavelength_ctl.SetValue(str(self.wavelength)) 
     374            flag, msg = self.check_inputs() 
     375            if self.base is not None and msg.lstrip().rstrip() != "": 
     376                msg = "SLD Calculator: %s" % str(msg) 
     377                wx.PostEvent(self.base, StatusEvent(status=msg)) 
     378            if not flag: 
     379               return  
     380            #get ready to compute 
     381            self.sld_formula = formula(self.compound, 
     382                                            density=self.density) 
     383            (sld_real, sld_im, _), (_, absorp, incoh), \ 
     384                        length = neutron_scattering(compound=self.compound, 
     385                                   density=self.density,  
     386                                   wavelength=self.wavelength)  
     387            cu_real, cu_im = self.calculate_sld_helper(element="Cu", 
     388                                                 density=self.density, 
     389                                        molecule_formula=self.sld_formula) 
     390            mo_real, mo_im = self.calculate_sld_helper(element="Mo",  
     391                                                       density=self.density, 
     392                                     molecule_formula=self.sld_formula) 
     393            # set neutron sld values 
     394            val = format_number(sld_real * _SCALE) 
     395            self.neutron_sld_reel_ctl.SetValue(val) 
     396            val = format_number(math.fabs(sld_im) * _SCALE) 
     397            self.neutron_sld_im_ctl.SetValue(val) 
     398            # Compute the Cu SLD 
     399            self.cu_ka_sld_reel_ctl.SetValue(format_number(cu_real *_SCALE)) 
     400            val = format_number(math.fabs(cu_im )* _SCALE) 
     401            self.cu_ka_sld_im_ctl.SetValue(val) 
     402            # Compute the Mo SLD 
     403            self.mo_ka_sld_reel_ctl.SetValue(format_number(mo_real *_SCALE)) 
     404            val = format_number(math.fabs(mo_im)* _SCALE) 
     405            self.mo_ka_sld_im_ctl.SetValue(val) 
     406            # set incoherence and absorption 
     407            self.neutron_inc_ctl.SetValue(format_number(incoh)) 
     408            self.neutron_abs_ctl.SetValue(format_number(absorp)) 
     409            # Neutron length 
     410            self.neutron_length_ctl.SetValue(format_number(length)) 
     411            # display wavelength 
     412            self.wavelength_ctl.SetValue(str(self.wavelength)) 
    405413        except: 
    406             raise 
    407             """ 
    408414            if self.base is not None: 
    409415                msg = "SLD Calculator: %s"%(sys.exc_value) 
    410416                wx.PostEvent(self.base, StatusEvent(status=msg)) 
    411             else: 
    412                 raise 
    413             return    
    414             """ 
    415417        if event is not None: 
    416418            event.Skip() 
    417     
     419             
     420    def clear_outputs(self): 
     421        """ 
     422        Clear the outputs textctrl 
     423        """ 
     424        self.neutron_sld_reel_ctl.SetValue("") 
     425        self.neutron_sld_im_ctl.SetValue("") 
     426        self.mo_ka_sld_reel_ctl.SetValue("") 
     427        self.mo_ka_sld_im_ctl.SetValue("") 
     428        self.cu_ka_sld_reel_ctl.SetValue("") 
     429        self.cu_ka_sld_im_ctl.SetValue("") 
     430        self.neutron_abs_ctl.SetValue("") 
     431        self.neutron_inc_ctl.SetValue("") 
     432        self.neutron_length_ctl.SetValue("") 
     433         
     434         
    418435class SldWindow(wx.Frame): 
    419436    """ 
  • calculatorview/test/utest_sld.py

    r810f196 r66bfacf  
    4545        #Compute incoherence , absorption, and incoherence 
    4646        (sld_real,sld_im,sld_inc), (coh,abs,incoh), length = neutron_scattering(self.compound, 
    47                                        self.density, self.wavelength)  
     47                                       density=self.density, wavelength=self.wavelength)  
    4848        cu_reel, cu_im = calculate_xray_sld(element="Cu", density=self.density, 
    4949                                  molecule_formula=self.sld_formula) 
     
    8585        #Compute incoherence , absorption, and incoherence 
    8686        (sld_real,sld_im,sld_inc), (coh,abs,incoh), length = neutron_scattering(self.compound, 
    87                                        self.density, self.wavelength)  
     87                                       density=self.density, wavelength=self.wavelength)  
    8888        cu_reel, cu_im = calculate_xray_sld(element="Cu", density=self.density, 
    8989                                  molecule_formula=self.sld_formula) 
     
    125125        #Compute incoherence , absorption, and incoherence 
    126126        (sld_real,sld_im,sld_inc), (coh,abs,incoh), length = neutron_scattering(self.compound, 
    127                                        self.density, self.wavelength)  
     127                                density=self.density, wavelength=self.wavelength)  
    128128        cu_reel, cu_im = calculate_xray_sld(element="Cu", density=self.density, 
    129129                                  molecule_formula=self.sld_formula) 
Note: See TracChangeset for help on using the changeset viewer.