<commit_before>#!/usr/bin/env python
#
# Tests the easy optimisation methods fmin and curve_fit.
#
# This file is part of PINTS.
#  Copyright (c) 2017-2018, University of Oxford.
#  For licensing information, see the LICENSE file distributed with the PINTS
#  software package.
#
import pints
import unittest
import numpy as np


class TestEasyOptimisation(unittest.TestCase):
    """
    Tests the easy optimisation methods fmin and curve_fit.
    """
    def test_fmin(self):
        """
        Tests :meth:`pints.fmin()`.
        """
        # Note: This just wraps around `Optimisation`, so testing done here is
        # for wrapper code, not main functionality!

        # Basic test
        def f(x):
            return (x[0] - 3) ** 2 + (x[1] + 5) ** 2

        np.random.seed(1)
        xopt, fopt = pints.fmin(f, [1, 1], method=pints.XNES)
        self.assertAlmostEqual(xopt[0], 3)
        self.assertAlmostEqual(xopt[1], -5)

        # Function must be callable
        self.assertRaisesRegexp(ValueError, 'callable', pints.fmin, 3, [1])

        # Test with boundaries
        xopt, fopt = pints.fmin(
            f, [1, 1], boundaries=([-10, -10], [10, 10]), method=pints.SNES)
        self.assertAlmostEqual(xopt[0], 3)
        self.assertAlmostEqual(xopt[1], -5)

        # Test with extra arguments
        def g(x, y, z):
            return (x[0] - 3) ** 2 + (x[1] + 5) ** 2 + y / z
        xopt, fopt = pints.fmin(g, [1, 1], args=[1, 2], method=pints.XNES)
        self.assertAlmostEqual(xopt[0], 3)
        self.assertAlmostEqual(xopt[1], -5)

        # Test with parallelisation
        pints.fmin(f, [1, 1], parallel=True, method=pints.XNES)

    def test_curve_fit(self):
        """
        Tests :meth:`pints.curve_fit()`.
        """
        # Note: This just wraps around `Optimisation`, so testing done here is
        # for wrapper code, not main functionality!
        np.random.seed(1)

        # Basic test
        def f(x, a, b, c):
            return a + b * x + c * x ** 2

        x = np.linspace(-5, 5, 100)
        e = np.random.normal(loc=0, scale=0.1, size=x.shape)
        y = f(x, 9, 3, 1) + e

        p0 = [0, 0, 0]
        np.random.seed(1)
        popt = pints.curve_fit(f, x, y, p0, method=pints.XNES)
        self.assertTrue(np.abs(popt[0] - 9) < 0.1)
        self.assertTrue(np.abs(popt[1] - 3) < 0.1)
        self.assertTrue(np.abs(popt[2] - 1) < 0.1)

        # Function must be callable
        self.assertRaisesRegexp(
            ValueError, 'callable', pints.curve_fit, 3, x, y, p0)

        # Test with boundaries
        pints.curve_fit(
            f, x, y, p0,
            boundaries=([-10, -10, -10], [10, 10, 10]), method=pints.XNES)
        self.assertTrue(np.abs(popt[0] - 9) < 0.1)
        self.assertTrue(np.abs(popt[1] - 3) < 0.1)
        self.assertTrue(np.abs(popt[2] - 1) < 0.1)

        # Test with invalid sizes of `x` and `y`
        x = np.linspace(-5, 5, 99)
        self.assertRaisesRegexp(
            ValueError, 'dimension', pints.curve_fit, f, x, y, p0)

        # Test with parallelisation
        pints.curve_fit(f, x, y, p0, parallel=True, method=pints.XNES)


if __name__ == '__main__':
    print('Add -v for more debug output')
    import sys
    if '-v' in sys.argv:
        debug = True
    unittest.main()<commit_msg>Fixed curve_fit test.<commit_after>#!/usr/bin/env python
#
# Tests the easy optimisation methods fmin and curve_fit.
#
# This file is part of PINTS.
#  Copyright (c) 2017-2018, University of Oxford.
#  For licensing information, see the LICENSE file distributed with the PINTS
#  software package.
#
import pints
import unittest
import numpy as np


class TestEasyOptimisation(unittest.TestCase):
    """
    Tests the easy optimisation methods fmin and curve_fit.
    """
    def test_fmin(self):
        """
        Tests :meth:`pints.fmin()`.
        """
        # Note: This just wraps around `Optimisation`, so testing done here is
        # for wrapper code, not main functionality!

        # Basic test
        def f(x):
            return (x[0] - 3) ** 2 + (x[1] + 5) ** 2

        np.random.seed(1)
        xopt, fopt = pints.fmin(f, [1, 1], method=pints.XNES)
        self.assertAlmostEqual(xopt[0], 3)
        self.assertAlmostEqual(xopt[1], -5)

        # Function must be callable
        self.assertRaisesRegexp(ValueError, 'callable', pints.fmin, 3, [1])

        # Test with boundaries
        xopt, fopt = pints.fmin(
            f, [1, 1], boundaries=([-10, -10], [10, 10]), method=pints.SNES)
        self.assertAlmostEqual(xopt[0], 3)
        self.assertAlmostEqual(xopt[1], -5)

        # Test with extra arguments
        def g(x, y, z):
            return (x[0] - 3) ** 2 + (x[1] + 5) ** 2 + y / z
        xopt, fopt = pints.fmin(g, [1, 1], args=[1, 2], method=pints.XNES)
        self.assertAlmostEqual(xopt[0], 3)
        self.assertAlmostEqual(xopt[1], -5)

        # Test with parallelisation
        pints.fmin(f, [1, 1], parallel=True, method=pints.XNES)

    def test_curve_fit(self):
        """
        Tests :meth:`pints.curve_fit()`.
        """
        # Note: This just wraps around `Optimisation`, so testing done here is
        # for wrapper code, not main functionality!
        np.random.seed(1)

        # Basic test
        def f(x, a, b, c):
            return a + b * x + c * x ** 2

        x = np.linspace(-5, 5, 100)
        e = np.random.normal(loc=0, scale=0.1, size=x.shape)
        y = f(x, 9, 3, 1) + e

        p0 = [0, 0, 0]
        np.random.seed(1)
        popt = pints.curve_fit(f, x, y, p0, method=pints.XNES)
        self.assertTrue(np.abs(popt[0] - 9) < 0.1)
        self.assertTrue(np.abs(popt[1] - 3) < 0.1)
        self.assertTrue(np.abs(popt[2] - 1) < 0.1)

        # Function must be callable
        self.assertRaisesRegexp(
            ValueError, 'callable', pints.curve_fit, 3, x, y, p0)

        # Test with boundaries
        pints.curve_fit(
            f, x, y, p0,
            boundaries=([-10, -10, -10], [10, 10, 10]), method=pints.XNES)
        self.assertTrue(np.abs(popt[0] - 9) < 0.1)
        self.assertTrue(np.abs(popt[1] - 3) < 0.1)
        self.assertTrue(np.abs(popt[2] - 1) < 0.1)

        # Test with parallelisation
        pints.curve_fit(f, x, y, p0, parallel=True, method=pints.XNES)

        # Test with invalid sizes of `x` and `y`
        x = np.linspace(-5, 5, 99)
        self.assertRaisesRegexp(
            ValueError, 'dimension', pints.curve_fit, f, x, y, p0)


if __name__ == '__main__':
    print('Add -v for more debug output')
    import sys
    if '-v' in sys.argv:
        debug = True
    unittest.main()
{'fraction_non_alphanumeric': 0.14339679661529162, 'fraction_numerical': 0.03535811423390753, 'mean_word_length': 2.1192271442035815, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6621935', 'n_tokens_mistral': 2555, 'n_tokens_neox': 2354, 'n_words': 627}