def test_set_background_peak_only(self):
     from mantid.fitfunctions import Gaussian
     cf = CrystalFieldMultiSite(Ions='Ce',
                                Symmetries='C2v',
                                Temperatures=[20],
                                FWHM=[1.0],
                                BackgroundPeak=Gaussian(Sigma=1))
     self.assertEquals('"name=Gaussian,Height=0,PeakCentre=0,Sigma=1"',
                       cf['Background'])
     self.assertEquals(cf.background.peak.param['Sigma'], 1)
     self.assertEquals(cf.background.param['Sigma'], 1)
     cf.background.peak.param['Sigma'] = 0
     self.assertEquals(cf.background.param['Sigma'], 0)
 def test_set_background_composite(self):
     from mantid.fitfunctions import Gaussian, LinearBackground
     cf = CrystalFieldMultiSite(Ions='Ce',
                                Symmetries='C2v',
                                Temperatures=[20],
                                FWHM=[1.0],
                                Background=Gaussian(PeakCentre=1) +
                                LinearBackground())
     self.assertEquals(
         '"name=Gaussian,Height=0,PeakCentre=1,Sigma=0;name=LinearBackground,A0=0,A1=0"',
         cf['Background'])
     cf.background.param['f1.A0'] = 1
     cf.background.param['f0.PeakCentre'] = 0.5
     self.assertEquals(cf.background.param['f1.A0'], 1)
     self.assertEquals(cf.background.param['f0.PeakCentre'], 0.5)
    def test_constraints_multi_spectrum_and_ion(self):
        from mantid.fitfunctions import FlatBackground, Gaussian

        cf = CrystalFieldMultiSite(Ions=['Ce', 'Pr'],
                                   Symmetries=['C2v', 'C2v'],
                                   Temperatures=[44, 50],
                                   FWHM=[1.1, 0.9],
                                   Background=FlatBackground(),
                                   BackgroundPeak=Gaussian(Height=10,
                                                           Sigma=0.3),
                                   parameters={
                                       'ion0.B20': 0.37737,
                                       'ion0.B22': 3.9770,
                                       'ion1.B40': -0.031787,
                                       'ion1.B42': -0.11611,
                                       'ion1.B44': -0.12544
                                   })

        cf.constraints('sp0.IntensityScaling > 0',
                       '0 < sp1.IntensityScaling < 2', 'ion0.B22 < 4')

        cf.ties({'sp0.bg.f0.Height': 10.1})
        cf.constraints('sp0.bg.f0.Sigma > 0.1')
        cf.ties({'sp1.bg.f0.Height': 20.2})
        cf.constraints('sp1.bg.f0.Sigma > 0.2')

        cf.ties({
            'ion0.sp1.pk2.FWHM': '2*ion0.sp1.pk1.FWHM',
            'ion1.sp1.pk3.FWHM': '2*ion1.sp1.pk2.FWHM'
        })
        cf.constraints('ion0.sp0.pk1.FWHM < 2.2')
        cf.constraints('ion0.sp1.pk1.FWHM > 1.1',
                       '1 < ion0.sp1.pk4.FWHM < 2.2')

        s = str(cf.function)
        self.assertTrue('0<sp0.IntensityScaling' in s)
        self.assertTrue('sp1.IntensityScaling<2' in s)
        self.assertTrue('sp0.bg.f0.Height=10.1' in s)
        self.assertTrue('sp1.bg.f0.Height=20.2' in s)
        self.assertTrue('0.1<sp0.bg.f0.Sigma' in s)
        self.assertTrue('0.2<sp1.bg.f0.Sigma' in s)
        self.assertTrue('ion0.sp0.pk1.FWHM<2.2' in s)
        self.assertTrue('1.1<ion0.sp1.pk1.FWHM' in s)
        self.assertTrue('1<ion0.sp1.pk4.FWHM<2.2' in s)
        self.assertTrue('ion0.sp1.pk2.FWHM=2*ion0.sp1.pk1.FWHM' in s)
        self.assertTrue('ion1.sp1.pk3.FWHM=2*ion1.sp1.pk2.FWHM' in s)
 def test_set_background_with_peak(self):
     from mantid.fitfunctions import Gaussian
     cf = CrystalFieldMultiSite(Ions='Ce',
                                Symmetries='C2v',
                                Temperatures=[20],
                                FWHM=[1.0],
                                Background='name=LinearBackground',
                                BackgroundPeak=Gaussian(Height=1))
     self.assertEquals(
         '"name=Gaussian,Height=1,PeakCentre=0,Sigma=0;name=LinearBackground"',
         cf['Background'])
     self.assertEquals(cf.background.peak.param['Height'], 1)
     self.assertEquals(cf.background.param['f0.Height'], 1)
     self.assertEquals(cf.background.background.param['A0'], 0)
     cf.background.peak.param['Height'] = 0
     cf.background.background.param['A0'] = 1
     self.assertEquals(cf.background.peak.param['Height'], 0)
     self.assertEquals(cf.background.background.param['A0'], 1)
    def test_constraints_single_spectrum(self):
        from mantid.fitfunctions import Gaussian, LinearBackground
        from mantid.simpleapi import FunctionFactory

        cf = CrystalFieldMultiSite(Ions=['Ce'],
                                   Symmetries=['C2v'],
                                   Temperatures=[50],
                                   FWHM=[0.9],
                                   B20=0.37737,
                                   B22=3.9770,
                                   B40=-0.031787,
                                   B42=-0.11611,
                                   B44=-0.12544,
                                   Background=LinearBackground(A0=1.0),
                                   BackgroundPeak=Gaussian(Height=10,
                                                           Sigma=0.3))

        cf.ties(B40='B20/2')
        cf.constraints('IntensityScaling > 0', 'B22 < 4')
        cf.constraints('pk0.FWHM < 2.2', 'pk1.FWHM >= 0.1')
        cf.ties({'pk2.FWHM': '2*pk1.FWHM', 'pk3.FWHM': '2*pk2.FWHM'})
        cf.background.peak.ties(Height=10.1)
        cf.background.peak.constraints('Sigma > 0')
        cf.background.background.ties(A0=0.1)
        cf.background.background.constraints('A1 > 0')

        s = cf.makeSpectrumFunction()
        self.assertTrue('0<IntensityScaling' in s)
        self.assertTrue('B22<4' in s)
        self.assertTrue('0<bg.f0.Sigma' in s)
        self.assertTrue('0<bg.f1.A1' in s)
        self.assertTrue('Height=10.1' in s)
        self.assertTrue('A0=0.1' in s)
        self.assertTrue('pk0.FWHM<2.2' in s)
        self.assertTrue('0.1<pk1.FWHM' in s)
        self.assertTrue('pk2.FWHM=2*pk1.FWHM' in s)
        self.assertTrue('pk3.FWHM=2*pk2.FWHM' in s)

        # Test that ties and constraints are correctly defined
        fun = FunctionFactory.createInitialized(s)
        self.assertTrue(fun is not None)