def test_not_implemented_error(self): # Convert data to list values = self.data_single.tolist() # Create an object with links to the model and time series problem = pints.SingleOutputProblem(self.model_single, self.times, values) # Create error measure error = pints.RootMeanSquaredError(problem) # Check that not implemented error is raised for evaluateS1 self.assertRaisesRegex(NotImplementedError, '', error.evaluateS1, 1)
def __init__(self, cell, protocols, transformation=None, cap_filter=True): # Store transformation object if transformation is None: transformation = transformations.NullTransformation() self._transformation = transformation # Store problems self._problems = [] # Set individual errors and weights weights = [] errors = [] for protocol in protocols: # Create protocol if protocol == 6: p = data.load_protocol_values(protocol) else: p = data.load_myokit_protocol(protocol) # Create forward model m = model.Model(p, cells.reversal_potential(cells.temperature(cell)), sine_wave=(protocol == 7), analytical=(protocol < 6), start_steady=False) # Load data, create single output problem log = data.load(cell, protocol, cap_filter=cap_filter) time = log.time() current = log['current'] # Create single output problem problem = pints.SingleOutputProblem(m, time, current) self._problems.append(problem) # Define error function errors.append(pints.RootMeanSquaredError(problem)) # Add weighting based on range weights.append(1 / (np.max(current) - np.min(current))) # Create weighted sum of errors self._f = pints.SumOfErrors(errors, weights)
def test_call_two_dim_array_single(self): # Convert data to array of shape (n_times, 1) values = np.reshape(self.data_single, (self.n_times, 1)) # Create an object with links to the model and time series problem = pints.SingleOutputProblem(self.model_single, self.times, values) # Create error measure error = pints.RootMeanSquaredError(problem) # Evaluate likelihood for test parameters test_parameters = [3] score = error(test_parameters) # Check that error returns expected value # Expected = sqrt(mean((input - 1) ** 2)) self.assertEqual(score, 2)
def test_root_mean_squared_error(self): """ Tests :class:`pints.RootMeanSquaredError`. """ p = MiniProblem() e = pints.RootMeanSquaredError(p) self.assertEqual(e.n_parameters(), 3) float(e([1, 2, 3])) self.assertEqual(e([-1, 2, 3]), 0) self.assertNotEqual(np.all(e([1, 2, 3])), 0) x = [0, 0, 0] y = np.sqrt((1 + 4 + 9) / 3) self.assertAlmostEqual(e(x), y) x = [1, 1, 1] y = np.sqrt((4 + 1 + 4) / 3) self.assertEqual(e(x), y) p = MultiMiniProblem() self.assertRaises(ValueError, pints.RootMeanSquaredError, p)
def setUpClass(cls): """ Prepare problem for tests. """ # Create toy model cls.model = pints.toy.stochastic.DegradationModel() cls.real_parameters = [0.1] cls.times = np.linspace(0, 10, 10) cls.values = cls.model.simulate(cls.real_parameters, cls.times) # Create an object (problem) 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 cls.log_prior = pints.UniformLogPrior([0.0], [0.3]) # Set error measure cls.error_measure = pints.RootMeanSquaredError(cls.problem)
def setUpClass(cls): """ Set up problem for tests. """ # Create toy model cls.model = toy.stochastic.DegradationModel() cls.real_parameters = [0.1] cls.times = np.linspace(0, 10, 10) cls.values = cls.model.simulate(cls.real_parameters, cls.times) # Create an object (problem) 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 cls.log_prior = pints.UniformLogPrior([0.0], [0.3]) cls.transition_kernel = pints.MultivariateGaussianLogPrior( np.zeros(1), 0.001 * np.identity(1)) # Set error measure cls.error_measure = pints.RootMeanSquaredError(cls.problem)
def test_root_mean_squared_error(self): # Tests :class:`pints.RootMeanSquaredError`. p = MiniProblem() e = pints.RootMeanSquaredError(p) self.assertEqual(e.n_parameters(), 3) float(e([1, 2, 3])) self.assertEqual(e([-1, 2, 3]), 0) self.assertNotEqual(np.all(e([1, 2, 3])), 0) x = [0, 0, 0] y = np.sqrt((1 + 4 + 9) / 3) self.assertAlmostEqual(e(x), y) x = [1, 1, 1] y = np.sqrt((4 + 1 + 4) / 3) self.assertEqual(e(x), y) p = MultiMiniProblem() self.assertRaisesRegex( ValueError, 'This measure is only defined for single output problems.', pints.RootMeanSquaredError, p)
def test_sum_of_errors(self): # Tests :class:`pints.SumOfErrors`. e1 = pints.SumOfSquaresError(MiniProblem()) e2 = pints.MeanSquaredError(MiniProblem()) e3 = pints.RootMeanSquaredError(BigMiniProblem()) e4 = pints.SumOfSquaresError(BadMiniProblem()) # Basic use e = pints.SumOfErrors([e1, e2]) x = [0, 0, 0] self.assertEqual(e.n_parameters(), 3) self.assertEqual(e(x), e1(x) + e2(x)) e = pints.SumOfErrors([e1, e2], [3.1, 4.5]) x = [0, 0, 0] self.assertEqual(e.n_parameters(), 3) self.assertEqual(e(x), 3.1 * e1(x) + 4.5 * e2(x)) e = pints.SumOfErrors([e1, e1, e1, e1, e1, e1], [1, 2, 3, 4, 5, 6]) self.assertEqual(e.n_parameters(), 3) self.assertEqual(e(x), e1(x) * 21) self.assertNotEqual(e(x), 0) with np.errstate(all='ignore'): e = pints.SumOfErrors([e4, e1, e1, e1, e1, e1], [10, 1, 1, 1, 1, 1]) self.assertEqual(e.n_parameters(), 3) self.assertEqual(e(x), float('inf')) e = pints.SumOfErrors([e4, e1, e1, e1, e1, e1], [0, 2, 0, 2, 0, 2]) self.assertEqual(e.n_parameters(), 3) self.assertTrue(e(x), 6 * e1(x)) e5 = pints.SumOfSquaresError(BadMiniProblem(float('-inf'))) e = pints.SumOfErrors([e1, e5, e1], [2.1, 3.4, 6.5]) self.assertTrue(np.isinf(e(x))) e = pints.SumOfErrors([e4, e5, e1], [2.1, 3.4, 6.5]) self.assertTrue(np.isinf(e(x))) e5 = pints.SumOfSquaresError(BadMiniProblem(float('nan'))) e = pints.SumOfErrors( [BadErrorMeasure(float('inf')), BadErrorMeasure(float('inf'))], [1, 1]) self.assertEqual(e(x), float('inf')) e = pints.SumOfErrors([ BadErrorMeasure(float('inf')), BadErrorMeasure(float('-inf')) ], [1, 1]) self.assertTrue(np.isnan(e(x))) e = pints.SumOfErrors( [BadErrorMeasure(5), BadErrorMeasure(float('nan'))], [1, 1]) self.assertTrue(np.isnan(e(x))) e = pints.SumOfErrors([e1, e5, e1], [2.1, 3.4, 6.5]) self.assertTrue(np.isnan(e(x))) e = pints.SumOfErrors([e4, e5, e1], [2.1, 3.4, 6.5]) self.assertTrue(np.isnan(e(x))) # Wrong number of ErrorMeasures self.assertRaises(ValueError, pints.SumOfErrors, [], []) # Wrong argument types self.assertRaises(TypeError, pints.SumOfErrors, [e1, e1], [e1, 1]) self.assertRaises(ValueError, pints.SumOfErrors, [e1, 3], [2, 1]) # Mismatching sizes self.assertRaises(ValueError, pints.SumOfErrors, [e1, e1, e1], [1, 1]) # Mismatching problem dimensions self.assertRaises(ValueError, pints.SumOfErrors, [e1, e1, e3], [1, 2, 3]) # Single-output derivatives model = pints.toy.ConstantModel(1) times = [1, 2, 3] p1 = pints.SingleOutputProblem(model, times, [1, 1, 1]) p2 = pints.SingleOutputProblem(model, times, [2, 2, 2]) e1 = pints.SumOfSquaresError(p1) e2 = pints.SumOfSquaresError(p2) e = pints.SumOfErrors([e1, e2], [1, 2]) x = [4] y, dy = e.evaluateS1(x) self.assertEqual(y, e(x)) self.assertEqual(dy.shape, (1, )) y1, dy1 = e1.evaluateS1(x) y2, dy2 = e2.evaluateS1(x) self.assertTrue(np.all(dy == dy1 + 2 * dy2)) # Multi-output derivatives model = pints.toy.ConstantModel(2) times = [1, 2, 3] p1 = pints.MultiOutputProblem(model, times, [[3, 2], [1, 7], [3, 2]]) p2 = pints.MultiOutputProblem(model, times, [[2, 3], [3, 4], [5, 6]]) e1 = pints.SumOfSquaresError(p1) e2 = pints.SumOfSquaresError(p2) e = pints.SumOfErrors([e1, e2], [1, 2]) x = [4, -2] y, dy = e.evaluateS1(x) self.assertEqual(y, e(x)) self.assertEqual(dy.shape, (2, )) y1, dy1 = e1.evaluateS1(x) y2, dy2 = e2.evaluateS1(x) self.assertTrue(np.all(dy == dy1 + 2 * dy2))