def __init__(self, name_or_function, **kwargs): """ Initialise new instance. @param name: A valid name registered with the FunctionFactory. @param kwargs: Parameters (but not attributes) of this function. To set attributes use `attr` property. Example: f = Function('TabulatedFunction', Scaling=2.0) f.attr['Workspace'] = 'workspace_with_data' """ from mantid.simpleapi import FunctionFactory if isinstance(name_or_function, str): self.function = FunctionFactory.createFunction(name_or_function) else: self.function = name_or_function if 'prefix' in kwargs: self.prefix = kwargs['prefix'] del kwargs['prefix'] else: self.prefix = '' # Function attributes. self._attrib = FunctionAttributes(self.function, self.prefix) # Function parameters. self._params = FunctionParameters(self.function, self.prefix) # The rest of kw arguments are treated as function parameters for param in kwargs: self._params[param] = kwargs[param]
def test_constraints_single_spectrum(self): from mantid.simpleapi import Gaussian, LinearBackground, 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)
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)
def test_new_function_init(self): class BFunction(IFunction1D): def __init__(self): super(BFunction, self).__init__() def init(self): self.declareParameter("C", 0.0) def function1D(self, xvals): c = self.getParameterValue("C") return c * xvals FunctionFactory.subscribe(BFunction) fun = BFunction() self.assertEqual(str(fun), 'name=BFunction,C=0') fun.C = 2 self.assertEqual(fun(2), 4) fun = BFunction(C=3) self.assertEqual(fun.C, 3) self.assertEqual(fun(2), 6)
def test_new_function(self): class AFunction(IFunction1D): def init(self): self.declareParameter("C", 0.0) def function1D(self, xvals): c = self.getParameterValue("C") return c * xvals FunctionFactory.subscribe(AFunction) fun = AFunction() self.assertEqual(str(fun), 'name=AFunction,C=0') fun.C = 2 self.assertEqual(fun(2), 4) fun = AFunction(C=3) self.assertEqual(fun.C, 3) self.assertEqual(fun(2), 6) self.assertTrue((fun([2, 3, 4]) == np.array([6, 9, 12])).all()) self.assertTrue((fun(np.array([2, 3, 4])) == np.array([6, 9, 12])).all())
def _makeFunction(self): from mantid.simpleapi import FunctionFactory self.function = FunctionFactory.createFunction('CrystalFieldFunction')
def _makeFunction(self): self.function = FunctionFactory.createFunction('CrystalFieldFunction')