class TestPerlNecklace(unittest.TestCase):
    """ Unit tests for PerlNecklace """
    
    def setUp(self):
        from sas.models.PearlNecklaceModel import PearlNecklaceModel
        self.pnl = PearlNecklaceModel()
        from sas.models.LinearPearlsModel import LinearPearlsModel
        self.lpm = LinearPearlsModel()
        from sas.models.SphereModel import SphereModel
        self.sphere = SphereModel()
        from sas.models.BarBellModel import BarBellModel
        self.bar = BarBellModel()
        
    def testwithsphere(self):
        """ Compare 1D model with sphere """
        self.pnl.setParam("radius", 60)
        self.pnl.setParam("num_pearls", 1)
        self.pnl.setParam("sld_pearl", 2e-06)
        self.pnl.setParam("sld_solv", 1e-06)
        self.assertAlmostEqual(self.pnl.run(0.001), self.sphere.run(0.001), 5)
        self.assertAlmostEqual(self.pnl.run(0.005), self.sphere.run(0.005), 5)
        self.assertAlmostEqual(self.pnl.run(0.01), self.sphere.run(0.01), 5)
        self.assertAlmostEqual(self.pnl.run(0.05), self.sphere.run(0.05), 5)
        self.assertAlmostEqual(self.pnl.run(0.1), self.sphere.run(0.1), 5)
        self.assertAlmostEqual(self.pnl.run(0.5), self.sphere.run(0.5), 5)
        
    def testwithbarbell(self):
        """ 
        Compare 1D model with barbell
        
        Note:  pearlnecklace assumes infinite thin rod
        """
        self.pnl.setParam("radius", 20)
        self.pnl.setParam("num_pearls", 2)
        self.pnl.setParam("sld_pearl", 1e-06)
        self.pnl.setParam("sld_string", 1e-06)
        self.pnl.setParam("sld_solv", 6.3e-06)
        self.pnl.setParam("thick_string", 0.1)
        self.pnl.setParam("edge_separation", 400)
        self.bar.setParam("rad_bar", 0.1)
        self.bar.setParam("rad_bell", 20)
        self.lpm.setParam("radius", 20)
        self.lpm.setParam("num_pearls", 2)
        self.lpm.setParam("sld_pearl", 1e-06)
        self.lpm.setParam("sld_solv", 6.3e-06)
        self.lpm.setParam("edge_separation", 400)
                
        self.assertAlmostEqual(self.pnl.run(0.001), self.bar.run(0.001), 1)
        self.assertAlmostEqual(self.pnl.run(0.005), self.bar.run(0.005), 1)
        self.assertAlmostEqual(self.pnl.run(0.01), self.bar.run(0.01), 1)
        self.assertAlmostEqual(self.pnl.run(0.05), self.bar.run(0.05), 1)
        self.assertAlmostEqual(self.pnl.run(0.1), self.bar.run(0.1), 1)
        self.assertAlmostEqual(self.pnl.run(0.5), self.bar.run(0.5), 1)
        
        self.assertAlmostEqual(self.pnl.run(0.001), self.lpm.run(0.001), 1)
        self.assertAlmostEqual(self.pnl.run(0.005), self.lpm.run(0.005), 1)
        self.assertAlmostEqual(self.pnl.run(0.01), self.lpm.run(0.01), 1)
        self.assertAlmostEqual(self.pnl.run(0.05), self.lpm.run(0.05), 1)
        self.assertAlmostEqual(self.pnl.run(0.1), self.lpm.run(0.1), 1)
        self.assertAlmostEqual(self.pnl.run(0.5), self.lpm.run(0.5), 1)
Beispiel #2
0
class TestSphere(unittest.TestCase):
    """ Unit tests for sphere model using evalDistribution function """
    def setUp(self):
        from sas.models.SphereModel import SphereModel
        self.comp = SphereModel()
        self.x = numpy.array([1.0, 2.0, 3.0, 4.0])
        self.y = self.x + 1

    def test1D(self):
        """ Test 1D model for a sphere  with vector as input"""
        answer = numpy.array(
            [5.63877831e-05, 2.57231782e-06, 2.73704050e-07, 2.54229069e-08])

        testvector = self.comp.evalDistribution(self.x)

        self.assertAlmostEqual(len(testvector), 4)
        for i in xrange(len(answer)):
            self.assertAlmostEqual(testvector[i], answer[i])

    def test1D_1(self):
        """ Test 2D model for a sphere  with scalar as input"""
        self.assertAlmostEqual(self.comp.run(1.0), 5.63877831e-05, 4)

    def test1D_2(self):
        """ Test 2D model for a sphere for 2 scalar """
        self.assertAlmostEqual(self.comp.run([1.0, 1.3]), 56.3878e-06, 4)

    def test1D_3(self):
        """ Test 2D model for a Shpere for 2 vectors as input """
        #x= numpy.reshape(self.x, [len(self.x),1])
        #y= numpy.reshape(self.y, [1,len(self.y)])
        vect = self.comp.evalDistribution([self.x, self.y])
        self.assertAlmostEqual(vect[0], 9.2985e-07, 4)
        self.assertAlmostEqual(vect[len(self.x) - 1], 1.3871e-08, 4)
class TestSphere(unittest.TestCase):
    """ Unit tests for sphere model using evalDistribution function """
    
    def setUp(self):
        from sas.models.SphereModel import SphereModel
        self.comp = SphereModel()
        self.x = numpy.array([1.0,2.0,3.0, 4.0])
        self.y = self.x +1
        
    def test1D(self):
        """ Test 1D model for a sphere  with vector as input"""
        answer = numpy.array([5.63877831e-05,2.57231782e-06,2.73704050e-07,2.54229069e-08])
       
       
        testvector= self.comp.evalDistribution(self.x)
       
        self.assertAlmostEqual(len(testvector),4)
        for i in xrange(len(answer)):
            self.assertAlmostEqual(testvector[i],answer[i])
       
    def test1D_1(self):
        """ Test 2D model for a sphere  with scalar as input"""
        self.assertAlmostEqual(self.comp.run(1.0),5.63877831e-05, 4)
         
    def test1D_2(self):
        """ Test 2D model for a sphere for 2 scalar """
        self.assertAlmostEqual(self.comp.run([1.0, 1.3]), 56.3878e-06, 4)
        
    def test1D_3(self):
        """ Test 2D model for a Shpere for 2 vectors as input """
        #x= numpy.reshape(self.x, [len(self.x),1])
        #y= numpy.reshape(self.y, [1,len(self.y)])
        vect = self.comp.evalDistribution([self.x,self.y])
        self.assertAlmostEqual(vect[0],9.2985e-07, 4)
        self.assertAlmostEqual(vect[len(self.x)-1],1.3871e-08, 4)
Beispiel #4
0
class TestPerlNecklace(unittest.TestCase):
    """ Unit tests for PerlNecklace """
    def setUp(self):
        from sas.models.PearlNecklaceModel import PearlNecklaceModel
        self.pnl = PearlNecklaceModel()
        from sas.models.LinearPearlsModel import LinearPearlsModel
        self.lpm = LinearPearlsModel()
        from sas.models.SphereModel import SphereModel
        self.sphere = SphereModel()
        from sas.models.BarBellModel import BarBellModel
        self.bar = BarBellModel()

    def testwithsphere(self):
        """ Compare 1D model with sphere """
        self.pnl.setParam("radius", 60)
        self.pnl.setParam("num_pearls", 1)
        self.pnl.setParam("sld_pearl", 2e-06)
        self.pnl.setParam("sld_solv", 1e-06)
        self.assertAlmostEqual(self.pnl.run(0.001), self.sphere.run(0.001), 5)
        self.assertAlmostEqual(self.pnl.run(0.005), self.sphere.run(0.005), 5)
        self.assertAlmostEqual(self.pnl.run(0.01), self.sphere.run(0.01), 5)
        self.assertAlmostEqual(self.pnl.run(0.05), self.sphere.run(0.05), 5)
        self.assertAlmostEqual(self.pnl.run(0.1), self.sphere.run(0.1), 5)
        self.assertAlmostEqual(self.pnl.run(0.5), self.sphere.run(0.5), 5)

    def testwithbarbell(self):
        """ 
        Compare 1D model with barbell
        
        Note:  pearlnecklace assumes infinite thin rod
        """
        self.pnl.setParam("radius", 20)
        self.pnl.setParam("num_pearls", 2)
        self.pnl.setParam("sld_pearl", 1e-06)
        self.pnl.setParam("sld_string", 1e-06)
        self.pnl.setParam("sld_solv", 6.3e-06)
        self.pnl.setParam("thick_string", 0.1)
        self.pnl.setParam("edge_separation", 400)
        self.bar.setParam("rad_bar", 0.1)
        self.bar.setParam("rad_bell", 20)
        self.lpm.setParam("radius", 20)
        self.lpm.setParam("num_pearls", 2)
        self.lpm.setParam("sld_pearl", 1e-06)
        self.lpm.setParam("sld_solv", 6.3e-06)
        self.lpm.setParam("edge_separation", 400)

        self.assertAlmostEqual(self.pnl.run(0.001), self.bar.run(0.001), 1)
        self.assertAlmostEqual(self.pnl.run(0.005), self.bar.run(0.005), 1)
        self.assertAlmostEqual(self.pnl.run(0.01), self.bar.run(0.01), 1)
        self.assertAlmostEqual(self.pnl.run(0.05), self.bar.run(0.05), 1)
        self.assertAlmostEqual(self.pnl.run(0.1), self.bar.run(0.1), 1)
        self.assertAlmostEqual(self.pnl.run(0.5), self.bar.run(0.5), 1)

        self.assertAlmostEqual(self.pnl.run(0.001), self.lpm.run(0.001), 1)
        self.assertAlmostEqual(self.pnl.run(0.005), self.lpm.run(0.005), 1)
        self.assertAlmostEqual(self.pnl.run(0.01), self.lpm.run(0.01), 1)
        self.assertAlmostEqual(self.pnl.run(0.05), self.lpm.run(0.05), 1)
        self.assertAlmostEqual(self.pnl.run(0.1), self.lpm.run(0.1), 1)
        self.assertAlmostEqual(self.pnl.run(0.5), self.lpm.run(0.5), 1)
Beispiel #5
0
 def setUp(self):
     from sas.models.PearlNecklaceModel import PearlNecklaceModel
     self.pnl = PearlNecklaceModel()
     from sas.models.LinearPearlsModel import LinearPearlsModel
     self.lpm = LinearPearlsModel()
     from sas.models.SphereModel import SphereModel
     self.sphere = SphereModel()
     from sas.models.BarBellModel import BarBellModel
     self.bar = BarBellModel()
 def setUp(self):
     from sas.models.SphereModel import SphereModel
     self.model= SphereModel()
     
     self.model.setParam('scale', 1.0)
     self.model.setParam('radius', 60.0)
     self.model.setParam('sldSph', 2.0)
     self.model.setParam('sldSolv', 1.0)
     self.model.setParam('background', 0.0)
    def setUp(self):
        from sas.models.SphereModel import SphereModel
        from sas.models.HardsphereStructure import HardsphereStructure
        from sas.models.DiamCylFunc import DiamCylFunc
        from sas.models.MultiplicationModel import MultiplicationModel

        self.model = SphereModel()
        self.model2 = HardsphereStructure()
        self.model3 = MultiplicationModel(self.model, self.model2)  
        self.modelD = DiamCylFunc() 
class TestSphere(unittest.TestCase):
    """ Unit tests for calculate_ER (sphere model) """
    def setUp(self):
        from sas.models.SphereModel import SphereModel
        self.comp = SphereModel()

    def test(self):
        """ Test 1D model for a sphere """
        self.comp.setParam("radius", 20)
        self.assertAlmostEqual(self.comp.calculate_ER(), 20)
Beispiel #9
0
def test_4():
    radius = 15

    density = .1
    vol = 4 / 3 * math.pi * radius * radius * radius
    npts = vol * density

    canvas = VolumeCanvas.VolumeCanvas()
    canvas.setParam('lores_density', density)
    #handle = canvas.add('sphere')
    #canvas.setParam('%s.radius' % handle, radius)
    #canvas.setParam('%s.contrast' % handle, 1.0)

    pdb = canvas.add('test.pdb')

    sphere = SphereModel()
    sphere.setParam('radius', radius)
    sphere.setParam('scale', 1.0)
    sphere.setParam('contrast', 1.0)

    # Simple sphere sum(Pr) = (rho*V)^2
    # each p(r) point has a volume of 1/density

    for i in range(35):
        q = 0.001 + 0.01 * i

        #sim_1 = 1.0e8*canvas.getIq(q)*4/3*math.pi/(density*density*density)
        sim_1 = canvas.getIq(q)
        ana_1 = sphere.run(q)
        #ana_1 = form_factor(q, radius)

        print("q=%g  sim=%g  ana=%g   ratio=%g" %
              (q, sim_1, ana_1, sim_1 / ana_1))
 def setUp(self):
     data = Loader().load("latex_smeared.xml")
     self.data_res = data[0]
     self.data_slit = data[1]
     
     self.sphere = SphereModel()
     self.sphere.setParam('background', 0)
     self.sphere.setParam('radius', 5000.0)
     self.sphere.setParam('scale', 0.4)
     self.sphere.setParam('sldSolv',0)
     self.sphere.setParam('sldSph',1e-6)
class TestSphere(unittest.TestCase):
    """ Unit tests for calculate_ER (sphere model) """
    
    def setUp(self):
        from sas.models.SphereModel import SphereModel
        self.comp = SphereModel()
        
    def test(self):
        """ Test 1D model for a sphere """
        self.comp.setParam("radius", 20)
        self.assertAlmostEqual(self.comp.calculate_ER(), 20)
Beispiel #12
0
 def __init__(self, radius=15, density = 0.01):
     from sas.models.SphereModel import SphereModel
     
     self.name = 'sphere'
     self.radius = radius
     self.density = density
     
     self.ana = SphereModel()
     self.ana.setParam('scale', 1.0)
     self.ana.setParam('contrast', 1.0)
     self.ana.setParam('background', 0.0)
     self.ana.setParam('radius', radius)
     self.create()
Beispiel #13
0
 def setUp(self):
     # NIST sample data
     self.data = Loader().load("CMSphere5.txt")
     # NIST smeared sphere w/ param values below
     self.answer = Loader().load("CMSphere5smearsphere.txt")
     # call spheremodel
     self.model = SphereModel()
     # setparams consistent with Igor default
     self.model.setParam('scale', 1.0)
     self.model.setParam('background', 0.01)
     self.model.setParam('radius', 60.0)
     self.model.setParam('sldSolv', 6.3e-06)
     self.model.setParam('sldSph', 1.0e-06)
Beispiel #14
0
class TestSphere(unittest.TestCase):
    """ Unit tests for sphere model """
    def setUp(self):
        from sas.models.SphereModel import SphereModel
        self.comp = SphereModel()

    def test1D(self):
        """ Test 1D model for a sphere """
        self.assertAlmostEqual(self.comp.run(1.0), 5.6387e-5, 4)

    def test1D_2(self):
        """ Test 2D model for a sphere """
        self.assertAlmostEqual(self.comp.run([1.0, 1.3]), 5.6387e-5, 4)
    def setUp(self):
        loader = Loader()
        ## IGOR/NIST computation
        self.output_gauss = loader.load('Gausssphere.txt')
        self.output_shulz = loader.load('Schulzsphere.txt')

        from sas.models.SphereModel import SphereModel
        self.model = SphereModel()

        self.model.setParam('scale', 0.01)
        self.model.setParam('radius', 60.0)
        self.model.setParam('sldSph', 1.e-6)
        self.model.setParam('sldSolv', 3.e-6)
        self.model.setParam('background', 0.001)
Beispiel #16
0
class SphereValidator(Validator):
    
    def __init__(self, radius=15, density = 0.01):
        from sas.models.SphereModel import SphereModel
        
        self.name = 'sphere'
        self.radius = radius
        self.density = density
        
        self.ana = SphereModel()
        self.ana.setParam('scale', 1.0)
        self.ana.setParam('contrast', 1.0)
        self.ana.setParam('background', 0.0)
        self.ana.setParam('radius', radius)
        self.create()
       
    def create(self):
        canvas = VolumeCanvas.VolumeCanvas()
        canvas.setParam('lores_density', self.density)
        handle = canvas.add('sphere')
        canvas.setParam('%s.radius' % handle, self.radius)
        canvas.setParam('scale' , 1.0)
        canvas.setParam('%s.contrast' % handle, 1.0)
        canvas.setParam('background' , 0.0)
        self.canvas = canvas   
Beispiel #17
0
class TestSphere(unittest.TestCase):
    """ Unit tests for sphere model """
    
    def setUp(self):
        from sas.models.SphereModel import SphereModel
        self.comp = SphereModel()
        
    def test1D(self):
        """ Test 1D model for a sphere """
        self.assertAlmostEqual(self.comp.run(1.0), 5.6387e-5, 4)
        
    def test1D_2(self):
        """ Test 2D model for a sphere """
        self.assertAlmostEqual(self.comp.run([1.0, 1.3]), 5.6387e-5, 4)
Beispiel #18
0
 def test_state_IO(self):
     """
         Check that a state oject is independent from the model object it
         was generated with
     """
     self.sphere.setParam('radius', 44.0)
     _, _, state, _, _ = self.sphere.__reduce_ex__(0)
     
     sphere_copy = SphereModel()
     sphere_copy.__setstate__(state)
     sphere_clone = sphere_copy.clone()
     self.assertEqual(sphere_copy.getParam('radius'), 44)
     
     self.sphere.setParam('radius', 33.0)
     
     self.assertEqual(sphere_clone.getParam('radius'), 44)
Beispiel #19
0
def test_1():

    radius = 15

    density = .1
    vol = 4 / 3 * math.pi * radius * radius * radius
    npts = vol * density

    canvas = VolumeCanvas.VolumeCanvas()
    canvas.setParam('lores_density', density)
    handle = canvas.add('sphere')
    canvas.setParam('%s.radius' % handle, radius)
    canvas.setParam('%s.contrast' % handle, 1.0)

    if False:
        # Time test
        t_0 = time.time()
        value_1 = 1.0e8 * canvas.getIq(0.1)
        print("density = 0.1:  output=%g  time=%g" %
              (value_1, time.time() - t_0))

        t_0 = time.time()
        canvas.setParam('lores_density', 1)
        value_1 = 1.0e8 * canvas.getIq(0.1)
        print("density = 1000:  output=%g  time=%g" %
              (value_1, time.time() - t_0))

        t_0 = time.time()
        canvas.setParam('lores_density', 0.01)
        value_1 = 1.0e8 * canvas.getIq(0.1)
        print("density = 0.00001:  output=%g  time=%g" %
              (value_1, time.time() - t_0))
        print()

    sphere = SphereModel()
    sphere.setParam('radius', radius)
    sphere.setParam('scale', 1.0)
    sphere.setParam('contrast', 1.0)

    # Simple sphere sum(Pr) = (rho*V)^2
    # each p(r) point has a volume of 1/density

    for i in range(35):
        q = 0.001 + 0.01 * i

        #sim_1 = 1.0e8*canvas.getIq(q)*4/3*math.pi/(density*density*density)
        sim_1 = canvas.getIq(q)
        ana_1 = sphere.run(q)
        #ana_1 = form_factor(q, radius)

        print("q=%g  sim=%g  ana=%g   ratio=%g" %
              (q, sim_1, ana_1, sim_1 / ana_1))
Beispiel #20
0
 def testWrongOrder(self):
     from sas.models.SphereModel import SphereModel
     self.set_coreshell_on_canvas(1, 0)
     
     # Core shell model
     sphere = SphereModel()
     # Core radius
     sphere.setParam('radius', self.outer_radius)
     # Shell thickness
     sphere.setParam('contrast', self.shell_sld)
     sphere.setParam('background', 0.0)
     sphere.setParam('scale', 1.0)
     
     ana = sphere.run(0.05)
     val, err = self.canvas.getIqError(0.05)
     #print 'wrong', ana, val, err
     self.assert_(math.fabs(ana-val)/ana < 1.1)
 def setUp(self):
     from sas.models.PearlNecklaceModel import PearlNecklaceModel
     self.pnl = PearlNecklaceModel()
     from sas.models.LinearPearlsModel import LinearPearlsModel
     self.lpm = LinearPearlsModel()
     from sas.models.SphereModel import SphereModel
     self.sphere = SphereModel()
     from sas.models.BarBellModel import BarBellModel
     self.bar = BarBellModel()
Beispiel #22
0
def test_1():
    
    radius = 15
    
    density = .1
    vol = 4/3*math.pi*radius*radius*radius
    npts = vol*density
     
    canvas = VolumeCanvas.VolumeCanvas()
    canvas.setParam('lores_density', density)
    handle = canvas.add('sphere')
    canvas.setParam('%s.radius' % handle, radius)
    canvas.setParam('%s.contrast' % handle, 1.0)
    
    
    if False:
        # Time test
        t_0 = time.time()
        value_1 = 1.0e8*canvas.getIq(0.1)
        print "density = 0.1:  output=%g  time=%g" % (value_1, time.time()-t_0)
        
        t_0 = time.time()
        canvas.setParam('lores_density', 1)
        value_1 = 1.0e8*canvas.getIq(0.1)
        print "density = 1000:  output=%g  time=%g" % (value_1, time.time()-t_0)
        
        t_0 = time.time()
        canvas.setParam('lores_density', 0.01)
        value_1 = 1.0e8*canvas.getIq(0.1)
        print "density = 0.00001:  output=%g  time=%g" % (value_1, time.time()-t_0)
        print
    
    
    sphere = SphereModel()
    sphere.setParam('radius', radius)
    sphere.setParam('scale', 1.0)
    sphere.setParam('contrast', 1.0)
        
        
    # Simple sphere sum(Pr) = (rho*V)^2    
    # each p(r) point has a volume of 1/density    
        
    for i in range(35):
        q = 0.001 + 0.01*i
        
        
        
        #sim_1 = 1.0e8*canvas.getIq(q)*4/3*math.pi/(density*density*density)
        sim_1 = canvas.getIq(q)
        ana_1 = sphere.run(q)
        #ana_1 = form_factor(q, radius)
        
        print "q=%g  sim=%g  ana=%g   ratio=%g" % (q, sim_1, ana_1, sim_1/ana_1)
    def setUp(self):
        from sas.models.SphereModel import SphereModel
        from sas.models.HardsphereStructure import HardsphereStructure
        from sas.models.DiamCylFunc import DiamCylFunc
        from sas.models.MultiplicationModel import MultiplicationModel

        self.model = SphereModel()
        self.model2 = HardsphereStructure()
        self.model3 = MultiplicationModel(self.model, self.model2)  
        self.modelD = DiamCylFunc() 
Beispiel #24
0
 def setUp(self):
     data = Loader().load("latex_smeared.xml")
     self.data_res = data[0]
     self.data_slit = data[1]
     
     self.sphere = SphereModel()
     self.sphere.setParam('background', 0)
     self.sphere.setParam('radius', 5000.0)
     self.sphere.setParam('scale', 0.4)
     self.sphere.setParam('sldSolv',0)
     self.sphere.setParam('sldSph',1e-6)
Beispiel #25
0
class TestSphere(unittest.TestCase):
    def setUp(self):
        self.sphere = SphereModel()

    def test_state_IO(self):
        """
            Check that a state oject is independent from the model object it
            was generated with
        """
        self.sphere.setParam('radius', 44.0)
        _, _, state, _, _ = self.sphere.__reduce_ex__(0)

        sphere_copy = SphereModel()
        sphere_copy.__setstate__(state)
        sphere_clone = sphere_copy.clone()
        self.assertEqual(sphere_copy.getParam('radius'), 44)

        self.sphere.setParam('radius', 33.0)

        self.assertEqual(sphere_clone.getParam('radius'), 44)
Beispiel #26
0
 def setUp(self):
     # NIST sample data
     self.data = Loader().load("CMSphere5.txt")
     # NIST smeared sphere w/ param values below
     self.answer = Loader().load("CMSphere5smearsphere.txt")
     # call spheremodel
     self.model = SphereModel()
     # setparams consistent with Igor default
     self.model.setParam('scale', 1.0)
     self.model.setParam('background', 0.01)
     self.model.setParam('radius', 60.0)
     self.model.setParam('sldSolv', 6.3e-06)
     self.model.setParam('sldSph', 1.0e-06)
    def setUp(self):
        loader = Loader()
        ## IGOR/NIST computation
        self.output_gauss=loader.load('Gausssphere.txt')
        self.output_shulz=loader.load('Schulzsphere.txt')

        from sas.models.SphereModel import SphereModel
        self.model= SphereModel()

        self.model.setParam('scale', 0.01)
        self.model.setParam('radius', 60.0)
        self.model.setParam('sldSph', 1.e-6)
        self.model.setParam('sldSolv', 3.e-6)
        self.model.setParam('background', 0.001)
Beispiel #28
0
 def setUp(self):
     """
         Set up canvas
     """
     from sas.models.SphereModel import SphereModel
     self.model = VolumeCanvas.VolumeCanvas()
 
     handle = self.model.add('sphere')
     
     radius = 10
     density = .1
     
     ana = SphereModel()
     ana.setParam('scale', 1.0)
     ana.setParam('contrast', 1.0)
     ana.setParam('background', 0.0)
     ana.setParam('radius', radius)
     self.ana = ana
     
     self.model.setParam('lores_density', density)
     self.model.setParam('%s.radius' % handle, radius)
     self.model.setParam('scale' , 1.0)
     self.model.setParam('%s.contrast' % handle, 1.0)
     self.model.setParam('background' , 0.0)
Beispiel #29
0
def test_5():
    from sas.models.SphereModel import SphereModel
    model = VolumeCanvas.VolumeCanvas()
    
    handle = model.add('sphere')
    
    radius = 10
    density = .1
    
    ana = SphereModel()
    ana.setParam('scale', 1.0)
    ana.setParam('contrast', 1.0)
    ana.setParam('background', 0.0)
    ana.setParam('radius', radius)
    
    model.setParam('lores_density', density)
    model.setParam('%s.radius' % handle, radius)
    model.setParam('scale' , 1.0)
    model.setParam('%s.contrast' % handle, 1.0)
    model.setParam('background' , 0.0)
    
    ana = ana.runXY([0.1, 0.1])
    sim = model.getIq2D(0.1, 0.1)
    print ana, sim, sim/ana, ana/sim
Beispiel #30
0
def test_4():
    radius = 15
    
    density = .1
    vol = 4/3*math.pi*radius*radius*radius
    npts = vol*density

    
    canvas = VolumeCanvas.VolumeCanvas()
    canvas.setParam('lores_density', density)
    #handle = canvas.add('sphere')
    #canvas.setParam('%s.radius' % handle, radius)
    #canvas.setParam('%s.contrast' % handle, 1.0)
    
    pdb = canvas.add('test.pdb')
    
    
    
    sphere = SphereModel()
    sphere.setParam('radius', radius)
    sphere.setParam('scale', 1.0)
    sphere.setParam('contrast', 1.0)
        
        
    # Simple sphere sum(Pr) = (rho*V)^2    
    # each p(r) point has a volume of 1/density    
        
    for i in range(35):
        q = 0.001 + 0.01*i
        
        
        
        #sim_1 = 1.0e8*canvas.getIq(q)*4/3*math.pi/(density*density*density)
        sim_1 = canvas.getIq(q)
        ana_1 = sphere.run(q)
        #ana_1 = form_factor(q, radius)
        
        print "q=%g  sim=%g  ana=%g   ratio=%g" % (q, sim_1, ana_1, sim_1/ana_1)
Beispiel #31
0
import sys
sys.path.append('../build/temp.macosx-10.11-x86_64-2.7/src/sas')

"""

Test plotting sphere

"""
import numpy as np
import matplotlib.pyplot as plt

from sas.models.SphereModel import SphereModel
comp = SphereModel()
comp.setParam("radius", 30.0)
comp.setParam("background", 0.01)

# Generate Q and calculate I
q = np.linspace(0.001, 1, num=200)
i = map(comp.run,q)

# Plot I(q)
plt.figure()
plt.plot(q,np.log(i))


qx = np.linspace(-1, 1, num=400)
qy = qx
xv, yv = np.meshgrid(qx, qy)
xv= xv.flatten()
yv= yv.flatten()
qxy = np.column_stack((xv,yv))
Beispiel #32
0
 def setUp(self):
     self.sphere = SphereModel()
 def setUp(self):
     from sas.models.SphereModel import SphereModel
     self.comp = SphereModel()
     self.x = numpy.array([1.0,2.0,3.0, 4.0])
     self.y = self.x +1
Beispiel #34
0
 def testGetIq(self):
     """ Test the output of I(q) to the analytical solution
         If the normalization is wrong, we will have to fix it.
         
         getIq() should call getPr() behind the scenes so that
         the user doesnt have to do it if he doesn't need to.
     """
     from sas.models.SphereModel import SphereModel
     sphere = SphereModel()
     sphere.setParam('radius', 10.0)
     sphere.setParam('contrast', 1.0)
     sphere.setParam('background', 0.0)
     sphere.setParam('scale', 1.0)
             
     handle = self.canvas.add('sphere')
     self.canvas.setParam('%s.radius' % handle, 10.0)
     self.canvas.setParam('%s.contrast' % handle, 1.0)
     
     
     sim_1 = self.canvas.getIq(0.001)
     ana_1 = sphere.run(0.001)
     sim_2 = self.canvas.getIq(0.01)
     ana_2 = sphere.run(0.01)
     
     # test the shape of the curve (calculate relative error 
     # on the output and it should be compatible with zero
     # THIS WILL DEPEND ON THE NUMBER OF SPACE POINTS:
     # that why we need some error analysis.
     self.assert_( (sim_2*ana_1/sim_1 - ana_2)/ana_2 < 0.1)
     
     # test the absolute amplitude
     self.assert_( math.fabs(sim_2-ana_2)/ana_2 < 0.1)
Beispiel #35
0
 def setUp(self):
     from sas.models.SphereModel import SphereModel
     self.comp = SphereModel()
     self.x = numpy.array([1.0, 2.0, 3.0, 4.0])
     self.y = self.x + 1
Beispiel #36
0
class smear_test_1Dpinhole(unittest.TestCase):
    
    def setUp(self):
        # NIST sample data
        self.data = Loader().load("CMSphere5.txt")
        # NIST smeared sphere w/ param values below
        self.answer = Loader().load("CMSphere5smearsphere.txt")
        # call spheremodel
        self.model = SphereModel()
        # setparams consistent with Igor default
        self.model.setParam('scale', 1.0)
        self.model.setParam('background', 0.01)
        self.model.setParam('radius', 60.0)
        self.model.setParam('sldSolv', 6.3e-06)
        self.model.setParam('sldSph', 1.0e-06)
        
    def test_q(self):
        """
        Compare Pinhole resolution smearing with NIST
        """
        # x values
        input = numpy.zeros(len(self.data.x))
        # set time
        st1 = time()
        # cal I w/o smear
        input = self.model.evalDistribution(self.data.x)
        # Cal_smear (first call)
        for i in range(1000):
            s = QSmearer(self.data, self.model)
        # stop and record time taken
        first_call_time = time()-st1
        # set new time
        st = time()
        # cal I w/o smear (this is not neccessary to call but just to be fare
        input = self.model.evalDistribution(self.data.x)
        # smear cal (after first call done above)
        for i in range(1000):
            output = s(input)

        # record time taken
        last_call_time = time()-st
        # compare the ratio of ((NIST_answer-SsanView_answer)/NIST_answer)
        # If the ratio less than 1%, pass the test 
        for i in range(len(self.data.x)):
            ratio  = math.fabs((self.answer.y[i]-output[i])/self.answer.y[i])
            if ratio > 0.006:
                ratio = 0.006
            self.assertEqual(math.fabs((self.answer.y[i]-output[i])/ \
                                       self.answer.y[i]), ratio) 
        # print
        print "\n NIST_time = 10sec:"
        print "Cal_time(1000 times of first_calls; ) = ",  first_call_time  
        print "Cal_time(1000 times of calls) = ",  last_call_time 
Beispiel #37
0
 def setUp(self):
     self.sphere = SphereModel()
Beispiel #38
0
class smear_testdata(unittest.TestCase):
    """
        Test fitting with the smearing operations
        The output of the fits should be compated to fits
        done with IGOR for the same models and data sets.
    """
    def setUp(self):
        data = Loader().load("latex_smeared.xml")
        self.data_res = data[0]
        self.data_slit = data[1]
        
        self.sphere = SphereModel()
        self.sphere.setParam('background', 0)
        self.sphere.setParam('radius', 5000.0)
        self.sphere.setParam('scale', 0.4)
        self.sphere.setParam('sldSolv',0)
        self.sphere.setParam('sldSph',1e-6)
        #self.sphere.setParam('radius.npts', 30)
        #self.sphere.setParam('radius.width',50)

    def test_reso(self):

        # Let the data module find out what smearing the
        # data needs
        smear = smear_selection(self.data_res)
        #self.assertEqual(smear.__class__.__name__, 'QSmearer')
        #self.assertEqual(smear.__class__.__name__, 'PySmearer')

        # Fit
        fitter = Fit()
        
        # Data: right now this is the only way to set the smearer object
        # We should improve that and have a way to get access to the
        # data for a given fit.
        fitter.set_data(self.data_res,1)
        fitter.fit_arrange_dict[1].data_list[0].smearer = smear

        # Model: maybe there's a better way to do this.
        # Ideally we should have to create a new model from our sas model.
        fitter.set_model(Model(self.sphere),1, ['radius','scale', 'background'])
        
        # Why do we have to do this...?
        fitter.select_problem_for_fit(id=1,value=1)

        # Perform the fit (might take a while)
        result1, = fitter.fit()
        
        #print "v",result1.pvec
        #print "dv",result1.stderr
        #print "chisq(v)",result1.fitness

        self.assertTrue( math.fabs(result1.pvec[0]-5000) < 20 )
        self.assertTrue( math.fabs(result1.pvec[1]-0.48) < 0.02 )
        self.assertTrue( math.fabs(result1.pvec[2]-0.060)  < 0.002 )


    def test_slit(self):
        smear = smear_selection(self.data_slit)
        #self.assertEqual(smear.__class__.__name__, 'SlitSmearer')
        #self.assertEqual(smear.__class__.__name__, 'PySmearer')

        fitter = Fit()
        
        # Data: right now this is the only way to set the smearer object
        # We should improve that and have a way to get access to the
        # data for a given fit.
        fitter.set_data(self.data_slit,1)
        fitter.fit_arrange_dict[1].data_list[0].smearer = smear
        fitter.fit_arrange_dict[1].data_list[0].qmax = 0.003
        
        # Model
        fitter.set_model(Model(self.sphere),1, ['radius','scale'])
        fitter.select_problem_for_fit(id=1,value=1)
        
        result1, = fitter.fit()

        #print "v",result1.pvec
        #print "dv",result1.stderr
        #print "chisq(v)",result1.fitness

        numpy.testing.assert_allclose(result1.pvec, [2323.466,0.22137], rtol=0.001)
Beispiel #39
0
 def setUp(self):
     self.model= SphereModel()
Beispiel #40
0
class TestEvalMethods(unittest.TestCase):
    """ Testing evalDistribution for C models """
    def setUp(self):
        self.model = SphereModel()

    def test_scalar_methods(self):
        """
            Simple test comparing the run(), runXY() and
            evalDistribution methods
        """
        q1 = self.model.run(0.001)
        q2 = self.model.runXY(0.001)
        q4 = self.model.run(0.002)
        qlist3 = numpy.asarray([0.001, 0.002])
        q3 = self.model.evalDistribution(qlist3)

        self.assertEqual(q1, q2)
        self.assertEqual(q1, q3[0])
        self.assertEqual(q4, q3[1])

    def test_XY_methods(self):
        """
            Compare to the runXY() method for 2D models.
            
                      +--------+--------+--------+
            qy=0.009  |        |        |        | 
                      +--------+--------+--------+
            qy-0.006  |        |        |        |
                      +--------+--------+--------+            
            qy=0.003  |        |        |        | 
                      +--------+--------+--------+
                      qx=0.001   0.002   0.003
            
        """
        # These are the expected values for all bins
        expected = numpy.zeros([3, 3])
        for i in range(3):
            for j in range(3):
                q_length = math.sqrt((0.001 * (i + 1.0)) * (0.001 *
                                                            (i + 1.0)) +
                                     (0.003 * (j + 1.0)) * (0.003 * (j + 1.0)))
                expected[i][j] = self.model.run(q_length)

        qx_values = [0.001, 0.002, 0.003]
        qy_values = [0.003, 0.006, 0.009]

        qx = numpy.asarray(qx_values)
        qy = numpy.asarray(qy_values)

        new_x = numpy.tile(qx, (len(qy), 1))
        new_y = numpy.tile(qy, (len(qx), 1))
        new_y = new_y.swapaxes(0, 1)

        #iq is 1d array now (since 03-12-2010)
        qx_prime = new_x.flatten()
        qy_prime = new_y.flatten()

        iq = self.model.evalDistribution([qx_prime, qy_prime])

        for i in range(3):
            for j in range(3):
                # convert index into 1d array
                k = i + len(qx) * j
                self.assertAlmostEquals(iq[k], expected[i][j])
Beispiel #41
0
class TestEvalMethods(unittest.TestCase):
    """ Testing evalDistribution for C models """

    def setUp(self):
        self.model= SphereModel()
        
    def test_scalar_methods(self):
        """
            Simple test comparing the run(), runXY() and
            evalDistribution methods
        """
        q1 = self.model.run(0.001)
        q2 = self.model.runXY(0.001)
        q4 = self.model.run(0.002)
        qlist3 = numpy.asarray([0.001, 0.002])
        q3 = self.model.evalDistribution(qlist3)

        self.assertEqual(q1, q2)
        self.assertEqual(q1, q3[0])
        self.assertEqual(q4, q3[1])
        
    def test_XY_methods(self):
        """
            Compare to the runXY() method for 2D models.
            
                      +--------+--------+--------+
            qy=0.009  |        |        |        | 
                      +--------+--------+--------+
            qy-0.006  |        |        |        |
                      +--------+--------+--------+            
            qy=0.003  |        |        |        | 
                      +--------+--------+--------+
                      qx=0.001   0.002   0.003
            
        """
        # These are the expected values for all bins
        expected = numpy.zeros([3,3])
        for i in range(3):
            for j in range(3):
                q_length = math.sqrt( (0.001*(i+1.0))*(0.001*(i+1.0)) + (0.003*(j+1.0))*(0.003*(j+1.0)) )
                expected[i][j] = self.model.run(q_length)
        
        qx_values = [0.001, 0.002, 0.003]
        qy_values = [0.003, 0.006, 0.009]
        
        qx = numpy.asarray(qx_values)
        qy = numpy.asarray(qy_values)
              
        new_x = numpy.tile(qx, (len(qy),1))
        new_y = numpy.tile(qy, (len(qx),1))
        new_y = new_y.swapaxes(0,1)
    
        #iq is 1d array now (since 03-12-2010)
        qx_prime = new_x.flatten()
        qy_prime = new_y.flatten()
        
        iq = self.model.evalDistribution([qx_prime, qy_prime])
        
        for i in range(3):
            for j in range(3):
                # convert index into 1d array
                k = i+len(qx)*j
                self.assertAlmostEquals(iq[k], expected[i][j])
Beispiel #42
0
    def testWrongOrder(self):
        from sas.models.SphereModel import SphereModel
        self.set_coreshell_on_canvas(1, 0)

        # Core shell model
        sphere = SphereModel()
        # Core radius
        sphere.setParam('radius', self.outer_radius)
        # Shell thickness
        sphere.setParam('contrast', self.shell_sld)
        sphere.setParam('background', 0.0)
        sphere.setParam('scale', 1.0)

        ana = sphere.run(0.05)
        val, err = self.canvas.getIqError(0.05)
        #print 'wrong', ana, val, err
        self.assert_(math.fabs(ana - val) / ana < 1.1)
Beispiel #43
0
    def testGetIq(self):
        """ Test the output of I(q) to the analytical solution
            If the normalization is wrong, we will have to fix it.
            
            getIq() should call getPr() behind the scenes so that
            the user doesnt have to do it if he doesn't need to.
        """
        from sas.models.SphereModel import SphereModel
        sphere = SphereModel()
        sphere.setParam('radius', 10.0)
        sphere.setParam('contrast', 1.0)
        sphere.setParam('background', 0.0)
        sphere.setParam('scale', 1.0)

        handle = self.canvas.add('sphere')
        self.canvas.setParam('%s.radius' % handle, 10.0)
        self.canvas.setParam('%s.contrast' % handle, 1.0)

        sim_1 = self.canvas.getIq(0.001)
        ana_1 = sphere.run(0.001)
        sim_2 = self.canvas.getIq(0.01)
        ana_2 = sphere.run(0.01)

        # test the shape of the curve (calculate relative error
        # on the output and it should be compatible with zero
        # THIS WILL DEPEND ON THE NUMBER OF SPACE POINTS:
        # that why we need some error analysis.
        self.assert_((sim_2 * ana_1 / sim_1 - ana_2) / ana_2 < 0.1)

        # test the absolute amplitude
        self.assert_(math.fabs(sim_2 - ana_2) / ana_2 < 0.1)
Beispiel #44
0
 def setUp(self):
     self.model = SphereModel()
Beispiel #45
0
class TestSphere(unittest.TestCase):
    """
        Testing C++ Cylinder model
    """
    def setUp(self):
        from sas.models.SphereModel import SphereModel
        self.model= SphereModel()
        
        self.model.setParam('scale', 1.0)
        self.model.setParam('radius', 60.0)
        self.model.setParam('sldSph', 2.0)
        self.model.setParam('sldSolv', 1.0)
        self.model.setParam('background', 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.assertTrue(math.fabs(self.model.run(0.001)-90412744456148.094)<=50.0)
        self.assertAlmostEqual(self.model.runXY([0.001,0.001]), 
                               90347660670656.391, 1)

    def test_dispersion(self):
        """
            Test with dispersion
        """
        from sas.models.DisperseModel import DisperseModel
        disp = DisperseModel(self.model, ['radius'], [10])
        disp.setParam('n_pts', 10)
        disp.setParam('radius.npts', 10)
        disp.setParam('radius.nsigmas', 2.5)
        self.assertTrue(math.fabs(disp.run(0.001)-96795008379475.219<50.0))
        
    def test_new_disp(self):
        from sas.models.dispersion_models import GaussianDispersion
        disp_rm = GaussianDispersion()
        self.model.set_dispersion('radius', disp_rm)
        self.model.dispersion['radius']['width'] = 0.1666666667
        self.model.dispersion['radius']['npts'] = 10
        self.model.dispersion['radius']['nsigmas'] = 2
Beispiel #46
0
 def setUp(self):
     self.sldloader = sas_gen.SLDReader()
     self.pdbloader = sas_gen.PDBReader()
     self.omfloader = sas_gen.OMFReader() 
     self.comp = SphereModel()
Beispiel #47
0
def test_5():
    from sas.models.SphereModel import SphereModel
    model = VolumeCanvas.VolumeCanvas()

    handle = model.add('sphere')

    radius = 10
    density = .1

    ana = SphereModel()
    ana.setParam('scale', 1.0)
    ana.setParam('contrast', 1.0)
    ana.setParam('background', 0.0)
    ana.setParam('radius', radius)

    model.setParam('lores_density', density)
    model.setParam('%s.radius' % handle, radius)
    model.setParam('scale', 1.0)
    model.setParam('%s.contrast' % handle, 1.0)
    model.setParam('background', 0.0)

    ana = ana.runXY([0.1, 0.1])
    sim = model.getIq2D(0.1, 0.1)
    print(ana, sim, sim / ana, ana / sim)
 def setUp(self):
     from sas.models.SphereModel import SphereModel
     self.comp = SphereModel()
class TestSphereGauss(unittest.TestCase):
    """
        Testing C++  Polydispersion w/ sphere comparing to IGOR/NIST computation
    """
    def setUp(self):
        loader = Loader()
        ## IGOR/NIST computation
        self.output_gauss=loader.load('Gausssphere.txt')
        self.output_shulz=loader.load('Schulzsphere.txt')

        from sas.models.SphereModel import SphereModel
        self.model= SphereModel()

        self.model.setParam('scale', 0.01)
        self.model.setParam('radius', 60.0)
        self.model.setParam('sldSph', 1.e-6)
        self.model.setParam('sldSolv', 3.e-6)
        self.model.setParam('background', 0.001)

    def test_gauss(self):
        from sas.models.dispersion_models import GaussianDispersion
        disp_g = GaussianDispersion()
        self.model.set_dispersion('radius', disp_g)
        self.model.dispersion['radius']['width'] = 0.2
        self.model.dispersion['radius']['npts'] = 100
        self.model.dispersion['radius']['nsigmas'] = 10
        for ind in range(len(self.output_gauss.x)):
            self.assertAlmostEqual(self.model.run(self.output_gauss.x[ind]), 
                                   self.output_gauss.y[ind], 2)
        
    def test_shulz(self):
        from sas.models.dispersion_models import SchulzDispersion
        disp_s = SchulzDispersion()
        self.model.set_dispersion('radius', disp_s)
        self.model.dispersion['radius']['width'] = 0.2
        self.model.dispersion['radius']['npts'] = 100
        self.model.dispersion['radius']['nsigmas'] = 10
        for ind in range(len(self.output_shulz.x)):
            self.assertAlmostEqual(self.model.run(self.output_gauss.x[ind]), 
                                   self.output_shulz.y[ind], 3)        
Beispiel #50
0
class smear_test_1Dpinhole(unittest.TestCase):
    
    def setUp(self):
        # NIST sample data
        self.data = Loader().load("CMSphere5.txt")
        # NIST smeared sphere w/ param values below
        self.answer = Loader().load("CMSphere5smearsphere.txt")
        # call spheremodel
        self.model = SphereModel()
        # setparams consistent with Igor default
        self.model.setParam('scale', 1.0)
        self.model.setParam('background', 0.01)
        self.model.setParam('radius', 60.0)
        self.model.setParam('sldSolv', 6.3e-06)
        self.model.setParam('sldSph', 1.0e-06)
        
    def test_q(self):
        """
        Compare Pinhole resolution smearing with NIST
        """
        # x values
        input = numpy.zeros(len(self.data.x))
        # set time
        st1 = time()
        # cal I w/o smear
        input = self.model.evalDistribution(self.data.x)
        # Cal_smear (first call)
        for i in range(1000):
            s = QSmearer(self.data, self.model)
        # stop and record time taken
        first_call_time = time()-st1
        # set new time
        st = time()
        # cal I w/o smear (this is not neccessary to call but just to be fare
        input = self.model.evalDistribution(self.data.x)
        # smear cal (after first call done above)
        for i in range(1000):
            output = s(input)

        # record time taken
        last_call_time = time()-st
        # compare the ratio of ((NIST_answer-SsanView_answer)/NIST_answer)
        # If the ratio less than 1%, pass the test 
        for i in range(len(self.data.x)):
            ratio  = math.fabs((self.answer.y[i]-output[i])/self.answer.y[i])
            if ratio > 0.006:
                ratio = 0.006
            self.assertEqual(math.fabs((self.answer.y[i]-output[i])/ \
                                       self.answer.y[i]), ratio) 
        # print
        print "\n NIST_time = 10sec:"
        print "Cal_time(1000 times of first_calls; ) = ",  first_call_time  
        print "Cal_time(1000 times of calls) = ",  last_call_time 
 def setUp(self):
     from sas.models.SphereModel import SphereModel
     self.comp = SphereModel()
class smear_testdata(unittest.TestCase):
    """
        Test fitting with the smearing operations
        The output of the fits should be compated to fits
        done with IGOR for the same models and data sets.
    """
    def setUp(self):
        data = Loader().load("latex_smeared.xml")
        self.data_res = data[0]
        self.data_slit = data[1]
        
        self.sphere = SphereModel()
        self.sphere.setParam('background', 0)
        self.sphere.setParam('radius', 5000.0)
        self.sphere.setParam('scale', 0.4)
        self.sphere.setParam('sldSolv',0)
        self.sphere.setParam('sldSph',1e-6)
        #self.sphere.setParam('radius.npts', 30)
        #self.sphere.setParam('radius.width',50)

    def test_reso(self):

        # Let the data module find out what smearing the
        # data needs
        smear = smear_selection(self.data_res)
        #self.assertEqual(smear.__class__.__name__, 'QSmearer')
        #self.assertEqual(smear.__class__.__name__, 'PySmearer')

        # Fit
        fitter = Fit()
        
        # Data: right now this is the only way to set the smearer object
        # We should improve that and have a way to get access to the
        # data for a given fit.
        fitter.set_data(self.data_res,1)
        fitter.fit_arrange_dict[1].data_list[0].smearer = smear

        # Model: maybe there's a better way to do this.
        # Ideally we should have to create a new model from our sas model.
        fitter.set_model(Model(self.sphere),1, ['radius','scale', 'background'])
        
        # Why do we have to do this...?
        fitter.select_problem_for_fit(id=1,value=1)

        # Perform the fit (might take a while)
        result1, = fitter.fit()
        
        #print "v",result1.pvec
        #print "dv",result1.stderr
        #print "chisq(v)",result1.fitness

        self.assertTrue( math.fabs(result1.pvec[0]-5000) < 20 )
        self.assertTrue( math.fabs(result1.pvec[1]-0.48) < 0.02 )
        self.assertTrue( math.fabs(result1.pvec[2]-0.060)  < 0.002 )


    def test_slit(self):
        smear = smear_selection(self.data_slit)
        #self.assertEqual(smear.__class__.__name__, 'SlitSmearer')
        #self.assertEqual(smear.__class__.__name__, 'PySmearer')

        fitter = Fit()
        
        # Data: right now this is the only way to set the smearer object
        # We should improve that and have a way to get access to the
        # data for a given fit.
        fitter.set_data(self.data_slit,1)
        fitter.fit_arrange_dict[1].data_list[0].smearer = smear
        fitter.fit_arrange_dict[1].data_list[0].qmax = 0.003
        
        # Model
        fitter.set_model(Model(self.sphere),1, ['radius','scale'])
        fitter.select_problem_for_fit(id=1,value=1)
        
        result1, = fitter.fit()

        #print "v",result1.pvec
        #print "dv",result1.stderr
        #print "chisq(v)",result1.fitness

        numpy.testing.assert_allclose(result1.pvec, [2323.466,0.22137], rtol=0.001)
class TestSphereGauss(unittest.TestCase):
    """
        Testing C++  Polydispersion w/ sphere comparing to IGOR/NIST computation
    """
    def setUp(self):
        loader = Loader()
        ## IGOR/NIST computation
        self.output_gauss = loader.load('Gausssphere.txt')
        self.output_shulz = loader.load('Schulzsphere.txt')

        from sas.models.SphereModel import SphereModel
        self.model = SphereModel()

        self.model.setParam('scale', 0.01)
        self.model.setParam('radius', 60.0)
        self.model.setParam('sldSph', 1.e-6)
        self.model.setParam('sldSolv', 3.e-6)
        self.model.setParam('background', 0.001)

    def test_gauss(self):
        from sas.models.dispersion_models import GaussianDispersion
        disp_g = GaussianDispersion()
        self.model.set_dispersion('radius', disp_g)
        self.model.dispersion['radius']['width'] = 0.2
        self.model.dispersion['radius']['npts'] = 100
        self.model.dispersion['radius']['nsigmas'] = 10
        for ind in range(len(self.output_gauss.x)):
            self.assertAlmostEqual(self.model.run(self.output_gauss.x[ind]),
                                   self.output_gauss.y[ind], 2)

    def test_shulz(self):
        from sas.models.dispersion_models import SchulzDispersion
        disp_s = SchulzDispersion()
        self.model.set_dispersion('radius', disp_s)
        self.model.dispersion['radius']['width'] = 0.2
        self.model.dispersion['radius']['npts'] = 100
        self.model.dispersion['radius']['nsigmas'] = 10
        for ind in range(len(self.output_shulz.x)):
            self.assertAlmostEqual(self.model.run(self.output_gauss.x[ind]),
                                   self.output_shulz.y[ind], 3)
class TestsphereHardS(unittest.TestCase):
    """ 
        Unit tests for SphereModel(Q) * HardsphereStructure(Q)
    """
    def setUp(self):
        from sas.models.SphereModel import SphereModel
        from sas.models.HardsphereStructure import HardsphereStructure
        from sas.models.DiamCylFunc import DiamCylFunc
        from sas.models.MultiplicationModel import MultiplicationModel

        self.model = SphereModel()
        self.model2 = HardsphereStructure()
        self.model3 = MultiplicationModel(self.model, self.model2)  
        self.modelD = DiamCylFunc() 

    #Radius of model1.calculate_ER should be equal to the output/2 of DiamFunctions
    def test_multplication_radius(self):
        """
            test multiplication model (check the effective radius & the output
             of the multiplication)
        """
        self.model.setParam("radius", 60)
        modelDrun = 60
        self.model2.setParam("volfraction", 0.2)
        self.model2.setParam("effect_radius", modelDrun )
        
        #Compare new method with old method         
        self.assertEqual(self.model3.run(0.1), self.model.run(0.1)*self.model2.run(0.1))
        
        #Compare radius from two different calculations. Note: modelD.run(0.0) is DIAMETER
        self.assertEqual(self.model.calculate_ER(), modelDrun)
        
        
    def testMultiplicationParam(self):
        """ Test Multiplication  (check the parameters)"""
        ## test details dictionary

        ## test parameters list
        list3= self.model3.getParamList()

        for item in self.model.getParamList():
            if not 'scale' in item: 
                self.assert_(item in list3)
        for item in self.model2.getParamList():
            #model3 parameters should not include effect_radius*
            if not 'effect_radius' in item:  
                self.assert_(item in list3)
            
        ## test set value for parameters and get paramaters
        self.model3.setParam("scale_factor", 15)
        self.assertEqual(self.model3.getParam("scale_factor"), 15)
        self.model3.setParam("radius", 20)
        self.assertEqual(self.model3.getParam("radius"), 20)
        self.model3.setParam("radius.width", 15)
        self.assertEqual(self.model3.getParam("radius.width"), 15)
        self.model3.setParam("scale_factor", 15)
        self.assertEqual(self.model3.getParam("scale_factor"), 15)
        self.assertEqual(self.model3.getParam("volfraction"), self.model.getParam("scale"))
        
        ## Dispersity 
        list3= self.model3.getDispParamList()
        self.assertEqual(list3, ['radius.npts', 'radius.nsigmas', 'radius.width'])
        
        from sas.models.dispersion_models import ArrayDispersion
        disp_th = ArrayDispersion()
        
        values_th = numpy.zeros(100)
        weights   = numpy.zeros(100)
        for i in range(100):
            values_th[i]=(math.pi/99.0*i)
            weights[i]=(1.0)
    
        disp_th.set_weights(values_th, weights)
        
        self.model3.set_dispersion('radius', disp_th)
        
        val_1d = self.model3.run(math.sqrt(0.0002))
        val_2d = self.model3.runXY([0.01,0.01]) 
        
        self.assertTrue(math.fabs(val_1d-val_2d)/val_1d < 0.02)
        model4= self.model3.clone()
        self.assertEqual(model4.getParam("radius"), 20)