<commit_before>#!/usr/bin/env python3
#
# Tests the basic methods of the adaptive covariance base class.
#
# This file is part of PINTS (https://github.com/pints-team/pints/) which is
# released under the BSD 3-clause license. See accompanying LICENSE.md for
# copyright notice and full license details.
#
from __future__ import print_function, unicode_literals
import numpy as np
import unittest

import pints
import pints.toy as toy


class TestAdaptiveCovarianceMC(unittest.TestCase):
    """
    Tests the basic methods of the adaptive covariance MCMC routine.
    """

    @classmethod
    def setUpClass(cls):
        """ Set up problem for tests. """

        # Create toy model
        cls.model = toy.LogisticModel()
        cls.real_parameters = [0.015, 500]
        cls.times = np.linspace(0, 1000, 1000)
        cls.values = cls.model.simulate(cls.real_parameters, cls.times)

        # Add noise
        cls.noise = 10
        cls.values += np.random.normal(0, cls.noise, cls.values.shape)
        cls.real_parameters.append(cls.noise)
        cls.real_parameters = np.array(cls.real_parameters)

        # Create an object with links to the model and time series
        cls.problem = pints.SingleOutputProblem(
            cls.model, cls.times, cls.values)

        # Create a uniform prior over both the parameters and the new noise
        # variable
        cls.log_prior = pints.UniformLogPrior(
            [0.01, 400, cls.noise * 0.1],
            [0.02, 600, cls.noise * 100]
        )

        # Create a log likelihood
        cls.log_likelihood = pints.GaussianLogLikelihood(cls.problem)

        # Create an un-normalised log-posterior (log-likelihood + log-prior)
        cls.log_posterior = pints.LogPosterior(
            cls.log_likelihood, cls.log_prior)

        # Run MCMC sampler
        xs = [cls.real_parameters * 1.1,
              cls.real_parameters * 0.9,
              cls.real_parameters * 1.15,
              ]

        mcmc = pints.MCMCController(cls.log_posterior, 3, xs,
                                    method=pints.HaarioBardenetACMC)
        mcmc.set_max_iterations(200)
        mcmc.set_initial_phase_iterations(50)
        mcmc.set_log_to_screen(False)

        cls.chains = mcmc.run()
        cls.time = mcmc.time()

    def test_errors(self):
        # test errors occur when incorrectly calling MCMCSummary
        self.assertRaises(ValueError, pints.MCMCSummary, self.chains, -3)
        self.assertRaises(ValueError, pints.MCMCSummary, self.chains, 0)
        self.assertRaises(ValueError, pints.MCMCSummary, self.chains, 1.5,
                          ["param 1"])

    def test_running(self):
        # tests that object works as expected
        results = pints.MCMCSummary(self.chains)
        self.assertEqual(results.time(), None)<commit_msg>Removed unused code from mcmc summary test.<commit_after>#!/usr/bin/env python3
#
# Tests the basic methods of the adaptive covariance base class.
#
# This file is part of PINTS (https://github.com/pints-team/pints/) which is
# released under the BSD 3-clause license. See accompanying LICENSE.md for
# copyright notice and full license details.
#
from __future__ import print_function, unicode_literals
import numpy as np
import unittest

import pints
import pints.toy as toy


class TestAdaptiveCovarianceMC(unittest.TestCase):
    """
    Tests the basic methods of the adaptive covariance MCMC routine.
    """

    @classmethod
    def setUpClass(cls):
        """ Set up problem for tests. """

        # Create toy model
        cls.model = toy.LogisticModel()
        cls.real_parameters = [0.015, 500]
        cls.times = np.linspace(0, 1000, 1000)
        cls.values = cls.model.simulate(cls.real_parameters, cls.times)

        # Add noise
        cls.noise = 10
        cls.values += np.random.normal(0, cls.noise, cls.values.shape)
        cls.real_parameters.append(cls.noise)
        cls.real_parameters = np.array(cls.real_parameters)

        # Create an object with links to the model and time series
        cls.problem = pints.SingleOutputProblem(
            cls.model, cls.times, cls.values)

        # Create a uniform prior over both the parameters and the new noise
        # variable
        cls.log_prior = pints.UniformLogPrior(
            [0.01, 400, cls.noise * 0.1],
            [0.02, 600, cls.noise * 100]
        )

        # Create a log likelihood
        cls.log_likelihood = pints.GaussianLogLikelihood(cls.problem)

        # Create an un-normalised log-posterior (log-likelihood + log-prior)
        cls.log_posterior = pints.LogPosterior(
            cls.log_likelihood, cls.log_prior)

        # Run MCMC sampler
        xs = [cls.real_parameters * 1.1,
              cls.real_parameters * 0.9,
              cls.real_parameters * 1.15,
              ]

        mcmc = pints.MCMCController(cls.log_posterior, 3, xs,
                                    method=pints.HaarioBardenetACMC)
        mcmc.set_max_iterations(200)
        mcmc.set_initial_phase_iterations(50)
        mcmc.set_log_to_screen(False)

        cls.chains = mcmc.run()

    def test_errors(self):
        # test errors occur when incorrectly calling MCMCSummary
        self.assertRaises(ValueError, pints.MCMCSummary, self.chains, -3)
        self.assertRaises(ValueError, pints.MCMCSummary, self.chains, 0)
        self.assertRaises(ValueError, pints.MCMCSummary, self.chains, 1.5,
                          ["param 1"])

    def test_running(self):
        # tests that object works as expected
        results = pints.MCMCSummary(self.chains)
        self.assertEqual(results.time(), None)
{'fraction_non_alphanumeric': 0.09228576518795653, 'fraction_numerical': 0.019953678959558168, 'mean_word_length': 2.367726454709058, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, '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': '7567094', 'n_tokens_mistral': 1862, 'n_tokens_neox': 1702, 'n_words': 427}