def test_fix(self): g = FunctionWrapper( "Gaussian", Height=8.5, Sigma=1.2, PeakCentre=15) g.fix("Sigma") g_str = str(g) self.assertEqual(g_str.count("ties="),1) self.assertEqual(g_str.count("ties=(Sigma=1.2)"),1) g0 = FunctionWrapper( "Gaussian", Height=7.5, Sigma=1.2, PeakCentre=10) g1 = FunctionWrapper( "Gaussian", Height=8.5, Sigma=1.2, PeakCentre=11) c = CompositeFunctionWrapper(g0, g1) c.fix("f1.Sigma") c_str = str(c) self.assertEqual(c_str.count("ties="),1) self.assertEqual(c_str.count("ties=(Sigma=1.2"),1) # remove non-existent tie and test it has no effect c.untie("f1.Height") cu_str = str(c) self.assertEqual(c_str, cu_str) # remove actual tie c.untie("f1.Sigma") cz_str = str(c) self.assertEqual(cz_str.count("ties="),0)
def test_tie(self): g = FunctionWrapper( "Gaussian", Height=8.5, Sigma=1.2, PeakCentre=15) g.tie(Sigma="0.1*Height") g_str = str(g) self.assertEqual(g_str.count("ties="),1) self.assertEqual(g_str.count("ties=(Sigma=0.1*Height)"),1) g0 = FunctionWrapper( "Gaussian", Height=7.5, Sigma=1.2, PeakCentre=10) g1 = FunctionWrapper( "Gaussian", Height=8.5, Sigma=1.2, PeakCentre=11) c = CompositeFunctionWrapper(g0, g1) c.tie({"f1.Sigma":"f0.Sigma"}) c_str = str(c) self.assertEqual(c_str.count("ties="),1) self.assertEqual(c_str.count("ties=(f1.Sigma=f0.Sigma)"),1) c.untie("f1.Sigma") cz_str = str(c) self.assertEqual(cz_str.count("ties="),0)
def test_incremental_add(self): g0 = FunctionWrapper( "Gaussian", Height=7.5, Sigma=1.2, PeakCentre=10) g1 = FunctionWrapper( "Gaussian", Height=8.5, Sigma=1.25, PeakCentre=12) lb = FunctionWrapper("LinearBackground") c = CompositeFunctionWrapper( lb, g0) c += g1 c_str = str(c) self.assertEqual(c_str.count("("),0) self.assertEqual(c_str.count("LinearBackground"),1) self.assertEqual(c_str.count("Gaussian"),2) lb_str = str(lb) c0_str = str(c[0]) self.assertEqual(c0_str, lb_str) g0_str = str(g0) c1_str = str(c[1]) self.assertEqual(c1_str, g0_str) g1_str = str(g1) c2_str = str(c[2]) self.assertEqual(c2_str, g1_str)
def test_constrain(self): g = FunctionWrapper("Gaussian", Height=8.5, Sigma=1.2, PeakCentre=15) g.constrain("Sigma < 2.0, Height > 7.0") g_str = str(g) self.assertEqual(g_str.count("constraints="), 1) self.assertEqual(g_str.count("Sigma<2"), 1) self.assertEqual(g_str.count("7<Height"), 1) g.unconstrain("Height") g1_str = str(g) self.assertEqual(g1_str.count("constraints="), 1) self.assertEqual(g1_str.count("constraints=(Sigma<2)"), 1) g.unconstrain("Sigma") gz_str = str(g) self.assertEqual(gz_str.count("constraints="), 0)
def do_a_fit(x, function, guess, target, fixes=None, atol=0.01): r"""Carry out a fit and compare to target parameters Parameters ---------- x : sequence of floats Domain values for evaluating the function . function : str Registered function name. guess : dict Parameter names with their initial values. target : dict Parameter names with the values to be obtained after the fit. fixes : list List of fitting parameters to fix during the fit atol : float Absolute tolerance parameter when evaluating expected_output against output values. Returns ------- list [0] Evaluation of the comparison between evaluated and expected values. [1] output of the call to Fit algorithm """ target_model = FunctionWrapper(function, **target) y = target_model(x) e = np.ones(len(x)) w = CreateWorkspace(x, y, e, Nspec=1) model = FunctionWrapper(function, **guess) if fixes is not None: [model.fix(p) for p in fixes] fit = Fit(model, w, NIterations=2000) otarget = OrderedDict(target) return np.allclose([fit.Function[p] for p in otarget.keys()], list(otarget.values()), atol), fit
def test_add(self): g0 = FunctionWrapper( "Gaussian", Height=7.5, Sigma=1.2, PeakCentre=10) g1 = FunctionWrapper( "Gaussian", Height=8.5, Sigma=1.25, PeakCentre=12) lb = FunctionWrapper("LinearBackground") c = lb + g0 + g1 self.assertTrue( isinstance( c, CompositeFunctionWrapper) ) c_str = str(c) self.assertEqual(c_str.count("("),0) self.assertEqual(c_str.count("LinearBackground"),1) self.assertEqual(c_str.count("Gaussian"),2) lb_str = str(lb) c0_str = str(c[0]) self.assertEqual(c0_str, lb_str) g0_str = str(g0) c1_str = str(c[1]) self.assertEqual(c1_str, g0_str) g1_str = str(g1) c2_str = str(c[2]) self.assertEqual(c2_str, g1_str)
def test_mul(self): g0 = FunctionWrapper( "Gaussian", Height=7.5, Sigma=1.2, PeakCentre=10) g1 = FunctionWrapper( "Gaussian", Height=8.5, Sigma=1.25, PeakCentre=12) lb = FunctionWrapper("LinearBackground") p = lb * g0 * g1 self.assertTrue( isinstance( p, ProductFunctionWrapper) ) p_str = str(p) self.assertEqual(p_str.count("("),0) self.assertEqual(p_str.count("LinearBackground"),1) self.assertEqual(p_str.count("Gaussian"),2) lb_str = str(lb) p0_str = str(p[0]) self.assertEqual(p0_str, lb_str) g0_str = str(g0) p1_str = str(p[1]) self.assertEqual(p1_str, g0_str) g1_str = str(g1) p2_str = str(p[2]) self.assertEqual(p2_str, g1_str)
def test_constrain(self): g = FunctionWrapper( "Gaussian", Height=8.5, Sigma=1.2, PeakCentre=15) g.constrain("Sigma < 2.0, Height > 7.0") g_str = str(g) self.assertEqual(g_str.count("constraints="),1) self.assertEqual(g_str.count("Sigma<2"),1) self.assertEqual(g_str.count("7<Height"),1) g.unconstrain("Height") g1_str = str(g) self.assertEqual(g1_str.count("constraints="),1) self.assertEqual(g1_str.count("constraints=(Sigma<2)"),1) g.unconstrain("Sigma") gz_str = str(g) self.assertEqual(gz_str.count("constraints="),0)
def test_free(self): g = FunctionWrapper( "Gaussian", Height=8.5, Sigma=1.2, PeakCentre=15) g.constrain("Sigma < 2.0, Height > 7.0") g.tie({"PeakCentre":"2*Height"}) g.free("Height") g1_str = str(g) self.assertEqual(g1_str.count("ties="),1) self.assertEqual(g1_str.count("constraints="),1) self.assertEqual(g1_str.count("constraints=(Sigma<2)"),1) g.free("PeakCentre") g2_str = str(g) self.assertEqual(g2_str.count("ties="),0) self.assertEqual(g2_str.count("constraints="),1) g.free("Sigma") gz_str = str(g) self.assertEqual(gz_str.count("constraints="),0)
def test_convolution_creation(self): g0 = FunctionWrapper( "Gaussian", Height=7.5, Sigma=1.2, PeakCentre=10) g1 = FunctionWrapper( "Gaussian", Height=8.5, Sigma=1.2, PeakCentre=11) testhelpers.assertRaisesNothing(self, ConvolutionWrapper, g0, g1)
def test_dot_operator(self): g = FunctionWrapper( "Gaussian", Height=7.5, Sigma=1.2, PeakCentre=10) self.assertAlmostEqual(g.Height,7.5,10) g.Height = 8 self.assertAlmostEqual(g.Height,8,10)
def test_minimal_positional_arguments_with_functionwrapper_work(self): if platform.system() == 'Darwin': # crashes return fb = FunctionWrapper("FlatBackground") testhelpers.assertRaisesNothing(self, Fit, fb, self._raw_ws)
def test_write_array_elements(self): g = FunctionWrapper( "Gaussian", Height=7.5, Sigma=1.2, PeakCentre=10) g["Height"] = 8 self.assertAlmostEqual(g["Height"],8,10)
def test_name(self): g = FunctionWrapper( "Gaussian", Height=7.5, Sigma=1.2, PeakCentre=10) self.assertEqual(g.name,"Gaussian")
def create_function_string(function_name, **function_params): return str(FunctionWrapper(function_name, **function_params))
def create_model(function_name, **function_params): func = FunctionWrapper(function_name, **function_params) return lambda x: func(x)
def check_output(function_name, input, expected_output, tolerance, **function_params): func = FunctionWrapper(function_name, **function_params) output = func(input) return np.allclose(output, expected_output, atol=0.0001), output