Exemple #1
0
class TestRunMethods(unittest.TestCase):
    """ Tests run methods for oriented (2D) systems """

    def setUp(self):
        """ Set up ellipsoid """
        from sas.models.EllipsoidModel import EllipsoidModel
        
        radius_a = 10
        radius_b = 15
        density = 5
        
        self.ana = EllipsoidModel()
        self.ana.setParam('scale', 1.0)
        self.ana.setParam('contrast', 1.0)
        self.ana.setParam('background', 0.0)
        self.ana.setParam('radius_a', radius_a)
        self.ana.setParam('radius_b', radius_b)

       
        canvas = VolumeCanvas.VolumeCanvas()
        canvas.setParam('lores_density', density)
        self.handle = canvas.add('ellipsoid')
        canvas.setParam('%s.radius_x' % self.handle, radius_a)
        canvas.setParam('%s.radius_y' % self.handle, radius_b)
        canvas.setParam('%s.radius_z' % self.handle, radius_b)
        canvas.setParam('scale' , 1.0)
        canvas.setParam('%s.contrast' % self.handle, 1.0)
        canvas.setParam('background' , 0.0)
        self.canvas = canvas     
           
        self.ana.setParam('axis_theta', 1.57)
        self.ana.setParam('axis_phi', 0)
        
        self.canvas.setParam('%s.orientation' % self.handle, [0,0,0])
        

    def testRunXY_List(self):
        """ Testing ellipsoid along X """
        ana_val = self.ana.runXY([0.1, 0.2])
        sim_val = self.canvas.runXY([0.1, 0.2])
        #print ana_val, sim_val, sim_val/ana_val
        
        try:
            self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 )
        except:
            print "Error", ana_val, sim_val, sim_val/ana_val
            raise sys.exc_type, sys.exc_value

    def testRunXY_float(self):
        """ Testing ellipsoid along X """
        ana_val = self.ana.runXY(0.1)
        sim_val = self.canvas.runXY(0.1)
        #print ana_val, sim_val, sim_val/ana_val
        
        try:
            self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 )
        except:
            print "Error", ana_val, sim_val, sim_val/ana_val
            raise sys.exc_type, sys.exc_value

    def testRun_float(self):
        """ Testing ellipsoid along X """
        ana_val = self.ana.run(0.1)
        sim_val = self.canvas.run(0.1)
        #print ana_val, sim_val, sim_val/ana_val
        
        try:
            self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 )
        except:
            print "Error", ana_val, sim_val, sim_val/ana_val
            raise sys.exc_type, sys.exc_value

    def testRun_list(self):
        """ Testing ellipsoid along X """
        ana_val = self.ana.run([0.1, 33.0])
        sim_val = self.canvas.run([0.1, 33.0])
        #print ana_val, sim_val, sim_val/ana_val
        
        try:
            self.assert_( math.fabs(sim_val/ana_val-1.0)<0.05 )
        except:
            print "Error", ana_val, sim_val, sim_val/ana_val
            raise sys.exc_type, sys.exc_value
class TestEllipsoid(unittest.TestCase):
    """
        Testing C++ Cylinder model
    """
    def setUp(self):
        from sas.models.EllipsoidModel import EllipsoidModel
        self.model= EllipsoidModel()
        
        self.model.setParam('scale', 1.0)
        self.model.setParam('radius_a', 20.0)
        self.model.setParam('radius_b', 400.0)
        self.model.setParam('sldEll', 4.e-6)
        self.model.setParam('sldSolv', 1.e-6)
        self.model.setParam('background', 0.0)
        self.model.setParam('axis_theta', 0.0)
        self.model.setParam('axis_phi', 0.0)
        
    def test_simple(self):
        """
            Test simple 1D and 2D values
            Numbers taken from model that passed validation, before
            the update to C++ underlying class.
        """
        self.assertAlmostEqual(self.model.run(0.001), 
                               11808.842896863147, 3)
        self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 
                               11681.990374929677, 3)

    def test_dispersion(self):
        """
            Test with dispersion
        """
        from sas.models.DisperseModel import DisperseModel
        disp = DisperseModel(self.model, ['radius_a', 'radius_b'], [5, 50])
        disp.setParam('n_pts', 10)

        self.assertAlmostEqual(disp.run(0.001), 11948.72581312305, 3)
        self.assertAlmostEqual(disp.runXY([0.001,0.001]), 11811.972359807551, 3)
        
    def test_new_disp(self):
        from sas.models.dispersion_models import GaussianDispersion
        disp_rm = GaussianDispersion()
        self.model.set_dispersion('radius_a', disp_rm)
        self.model.dispersion['radius_a']['width'] = 0.25
        self.model.dispersion['radius_a']['npts'] = 10
        self.model.dispersion['radius_a']['nsigmas'] = 2

        disp_rr = GaussianDispersion()
        self.model.set_dispersion('radius_b', disp_rr)
        self.model.dispersion['radius_b']['width'] = 0.125
        self.model.dispersion['radius_b']['npts'] = 10
        self.model.dispersion['radius_b']['nsigmas'] = 2

        self.assertAlmostEqual(self.model.run(0.001), 
                               1.10650710*11948.72581312305, 3)
        self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 
                               1.105898*11811.972359807551, 2)

    def test_array(self):
        """
            Perform complete rotational average and
            compare to 1D
        """
        from sas.models.dispersion_models import ArrayDispersion
        disp_ph = ArrayDispersion()
        disp_th = ArrayDispersion()
        
        values_ph = numpy.zeros(100)
        values_th = numpy.zeros(100)
        weights   = numpy.zeros(100)
        for i in range(100):
            values_ph[i]=(360/99.0*i)
            values_th[i]=(180/99.0*i)
            weights[i]=(1.0)
        
        disp_ph.set_weights(values_ph, weights)
        disp_th.set_weights(values_th, weights)
        
        self.model.set_dispersion('axis_theta', disp_th)
        self.model.set_dispersion('axis_phi', disp_ph)
        
        val_1d = self.model.run(math.sqrt(0.0002))
        val_2d = self.model.runXY([0.01,0.01]) 
        
        self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02)