<commit_before>#!/usr/bin/env python3
#
# Tests if the Fitzhugh-Nagumo toy model runs.
#
# 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 unittest
import pints
import pints.toy
import numpy as np
from scipy.interpolate import interp1d


class TestFitzhughNagumoModel(unittest.TestCase):
    """
    Tests if the Fitzhugh-Nagumo toy model runs.
    """

    def test_run(self):

        # Test basic properties
        model = pints.toy.FitzhughNagumoModel()
        self.assertEqual(model.n_parameters(), 3)
        self.assertEqual(model.n_outputs(), 2)

        # Test simulation
        x = model.suggested_parameters()
        times = model.suggested_times()
        values = model.simulate(x, times)
        self.assertEqual(values.shape, (len(times), 2))

        # Simulation with sensitivities
        values, dvalues_dp = model.simulateS1(x, times)
        self.assertEqual(values.shape, (len(times), 2))
        self.assertEqual(dvalues_dp.shape, (len(times), 2, 3))

        # Test alternative starting position
        model = pints.toy.FitzhughNagumoModel([0.1, 0.1])
        values = model.simulate(x, times)
        self.assertEqual(values.shape, (len(times), 2))

        # Times can't be negative
        times = [-1, 2, 3, 4]
        self.assertRaises(ValueError, model.simulate, x, times)

        # Initial value must have size 2
        pints.toy.FitzhughNagumoModel([1, 1])
        self.assertRaises(ValueError, pints.toy.FitzhughNagumoModel, [1])

    def test_values(self):
        # value-based tests of Fitzhugh-Nagumo model
        parameters = [0.2, 0.4, 2.5]
        y0 = [-2, 1.5]
        times = np.linspace(0, 20, 201)
        model = pints.toy.FitzhughNagumoModel(y0)
        values = model.simulate(parameters, times)
        self.assertAlmostEqual(values[200, 0], 1.675726, places=6)
        self.assertAlmostEqual(values[200, 1], -0.226142, places=6)

    def test_sensitivities(self):
        # compares sensitivities against standards
        model = pints.toy.FitzhughNagumoModel([2, 3])
        parameters = [0.2, 0.7, 2.8]
        times_finer = np.linspace(0, 20, 500)
        sols, sens = model.simulateS1(parameters, times_finer)
        # interpolate to test sensitivity at defined times
        f = interp1d(times_finer, sens[:, 0][:, 2])
        self.assertTrue(np.abs(f([7])[0] - 5.0137868240051535) <= 0.01)
        f = interp1d(times_finer, sens[:, 1][:, 1])
        self.assertTrue(np.abs(f([12])[0] - 0.8288255034841188) <= 0.01)

<commit_msg>Updated test for Fitzhugh-Nagumo model.<commit_after>#!/usr/bin/env python3
#
# Tests if the Fitzhugh-Nagumo toy model runs.
#
# 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 unittest
import pints
import pints.toy
import numpy as np


class TestFitzhughNagumoModel(unittest.TestCase):
    """
    Tests if the Fitzhugh-Nagumo toy model runs.
    """

    def test_run(self):

        # Test basic properties
        model = pints.toy.FitzhughNagumoModel()
        self.assertEqual(model.n_parameters(), 3)
        self.assertEqual(model.n_outputs(), 2)

        # Test simulation
        x = model.suggested_parameters()
        times = model.suggested_times()
        values = model.simulate(x, times)
        self.assertEqual(values.shape, (len(times), 2))

        # Simulation with sensitivities
        values, dvalues_dp = model.simulateS1(x, times)
        self.assertEqual(values.shape, (len(times), 2))
        self.assertEqual(dvalues_dp.shape, (len(times), 2, 3))

        # Test alternative starting position
        model = pints.toy.FitzhughNagumoModel([0.1, 0.1])
        values = model.simulate(x, times)
        self.assertEqual(values.shape, (len(times), 2))

        # Times can't be negative
        times = [-1, 2, 3, 4]
        self.assertRaises(ValueError, model.simulate, x, times)

        # Initial value must have size 2
        pints.toy.FitzhughNagumoModel([1, 1])
        self.assertRaises(ValueError, pints.toy.FitzhughNagumoModel, [1])

    def test_values(self):
        # value-based tests of Fitzhugh-Nagumo model
        parameters = [0.2, 0.4, 2.5]
        y0 = [-2, 1.5]
        times = np.linspace(0, 20, 201)
        model = pints.toy.FitzhughNagumoModel(y0)
        values = model.simulate(parameters, times)
        self.assertAlmostEqual(values[200, 0], 1.675726, places=6)
        self.assertAlmostEqual(values[200, 1], -0.226142, places=6)

    def test_sensitivities(self):
        # compares sensitivities against standards
        model = pints.toy.FitzhughNagumoModel([2, 3])
        parameters = [0.2, 0.7, 2.8]

        # Slow way to do it
        times_finer = np.linspace(0, 20, 501)
        sols, sens = model.simulateS1(parameters, times_finer)
        print(sens[:, 0][:, 2][175])
        self.assertEqual(times_finer[175], 7)   # t=7
        self.assertAlmostEqual(sens[:, 0][:, 2][175], 5.01378, 5)
        self.assertEqual(times_finer[300], 12)  # t=12
        self.assertAlmostEqual(sens[:, 1][:, 1][300], 0.82883, 4)

        # Way to do it that works with current code
        times = [0, 7, 12]
        sols, sens = model.simulateS1(parameters, times)
        print(sens)
        print(sens[:, 0][:, 2])
        self.assertAlmostEqual(sens[:, 0][:, 2][1], 5.01378, 5)
        self.assertAlmostEqual(sens[:, 1][:, 1][2], 0.82883, 4)

        # Even better way to do it that reveals a bug in the current code!
        sols, sens = model.simulateS1(parameters, [7, 12])
        print(sens)
        print(sens[:, 0][:, 2])
        self.assertAlmostEqual(sens[:, 0][:, 2][0], 5.01378, 5)
        self.assertAlmostEqual(sens[:, 1][:, 1][1], 0.82883, 4)


{'fraction_non_alphanumeric': 0.12882392929979605, 'fraction_numerical': 0.053704962610469066, 'mean_word_length': 2.729404309252218, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4143900', 'n_tokens_mistral': 2260, 'n_tokens_neox': 2010, 'n_words': 477}