From 41da58dca7633c68bf9aff930e2a1fd7d6ae01a9 Mon Sep 17 00:00:00 2001 From: Michael Clerx Date: Tue, 7 Nov 2023 13:07:06 +0000 Subject: [PATCH 1/4] Removed units from time headers in controllers. Closes #1467. --- examples/optimisation/snes.ipynb | 4 ++-- pints/_abc/__init__.py | 4 +++- pints/_mcmc/__init__.py | 4 +++- pints/_nested/__init__.py | 4 +++- pints/_optimisers/__init__.py | 4 +++- 5 files changed, 14 insertions(+), 6 deletions(-) diff --git a/examples/optimisation/snes.ipynb b/examples/optimisation/snes.ipynb index 177974060..9b2f87f2b 100644 --- a/examples/optimisation/snes.ipynb +++ b/examples/optimisation/snes.ipynb @@ -132,7 +132,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3", + "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, @@ -146,7 +146,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.9.7" + "version": "3.11.6" } }, "nbformat": 4, diff --git a/pints/_abc/__init__.py b/pints/_abc/__init__.py index 2313f88cc..a3e49a892 100644 --- a/pints/_abc/__init__.py +++ b/pints/_abc/__init__.py @@ -235,7 +235,9 @@ def run(self): logger.add_counter('Eval.', max_value=max_eval_guess) logger.add_float('Acceptance rate') self._sampler._log_init(logger) - logger.add_time('Time m:s') + # Note: removed units from time field, see + # https://github.com/pints-team/pints/issues/1467 + logger.add_time('Time') # Start sampling timer = pints.Timer() diff --git a/pints/_mcmc/__init__.py b/pints/_mcmc/__init__.py index f0ec64baf..39e3390e1 100644 --- a/pints/_mcmc/__init__.py +++ b/pints/_mcmc/__init__.py @@ -665,7 +665,9 @@ def run(self): logger.add_counter('Eval.', max_value=max_eval_guess) for sampler in self._samplers: sampler._log_init(logger) - logger.add_time('Time m:s') + # Note: deleted time units from header, see + # https://github.com/pints-team/pints/issues/1467 + logger.add_time('Time') # Pre-allocate arrays for chain storage # Note: we store the inverse transformed (to model space) parameters diff --git a/pints/_nested/__init__.py b/pints/_nested/__init__.py index a7a15259a..c8c24462d 100644 --- a/pints/_nested/__init__.py +++ b/pints/_nested/__init__.py @@ -409,7 +409,9 @@ def _initialise_logger(self): # Add fields to log self._logger.add_counter('Iter.', max_value=self._iterations) self._logger.add_counter('Eval.', max_value=self._iterations * 10) - self._logger.add_time('Time m:s') + # Note: removed units from time field, see + # https://github.com/pints-team/pints/issues/1467 + self._logger.add_time('Time') self._logger.add_float('Delta_log(z)') self._logger.add_float('Acceptance rate') diff --git a/pints/_optimisers/__init__.py b/pints/_optimisers/__init__.py index 5887051d3..363a4ee2a 100644 --- a/pints/_optimisers/__init__.py +++ b/pints/_optimisers/__init__.py @@ -635,7 +635,9 @@ def run(self): logger.add_float('Best') logger.add_float('Current') self._optimiser._log_init(logger) - logger.add_time('Time m:s') + # Note: No units shown in time field, for the reason why see + # https://github.com/pints-team/pints/issues/1467 + logger.add_time('Time') # Start searching timer = pints.Timer() From 5affaaf1b103d8149ca139fff8c6c6ec4588dba9 Mon Sep 17 00:00:00 2001 From: Michael Clerx Date: Tue, 7 Nov 2023 13:14:36 +0000 Subject: [PATCH 2/4] Updating tests and changelog --- CHANGELOG.md | 1 + pints/tests/test_abc_controller.py | 8 ++++---- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d7258a691..fc09fc49f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ All notable changes to this project will be documented in this file. ### Deprecated ### Removed ### Fixed +- [#1503](https://github.com/pints-team/pints/pull/1503) Stopped showing time units in controller logs to fix occassional errors. ## [0.5.0] - 2023-07-27 diff --git a/pints/tests/test_abc_controller.py b/pints/tests/test_abc_controller.py index 1d7e2f7a7..6ca494d92 100755 --- a/pints/tests/test_abc_controller.py +++ b/pints/tests/test_abc_controller.py @@ -18,7 +18,7 @@ LOG_SCREEN_1 = [ 'Using Rejection ABC', 'Running in sequential mode.', - 'Iter. Eval. Acceptance rate Time m:s', + 'Iter. Eval. Acceptance rate Time ', '1 127 0.00787401575 0:00.0', '2 167 0.0119760479 0:00.0', '3 209 0.014354067 0:00.0', @@ -28,7 +28,7 @@ LOG_SCREEN_2 = [ 'Using Rejection ABC', 'Running in parallel with 2 worker processess.', - 'Iter. Eval. Acceptance rate Time m:s', + 'Iter. Eval. Acceptance rate Time ', '1 20 0.05 0:00.0', '2 22 0.0909090909 0:00.0', '3 144 0.0208333333 0:00.0', @@ -38,7 +38,7 @@ ] LOG_FILE = [ - 'Iter. Eval. Acceptance rate Time m:s', + 'Iter. Eval. Acceptance rate Time ', '1 127 0.00787401575 0:00.0', '2 167 0.0119760479 0:00.0', '3 209 0.014354067 0:00.0', @@ -46,7 +46,7 @@ ] LOG_FILE_CSV = [ - '"Iter.","Eval.","Acceptance rate","Time m:s"', + '"Iter.","Eval.","Acceptance rate","Time"', '1,127,7.87401574803149595e-03,0:00.0', '2,167,0.0119760479,0:00.0', '3,209,0.014354067,0:00.0', From fa17729a186cf7c58fc779f6f49f2f0ec6e887cb Mon Sep 17 00:00:00 2001 From: Michael Clerx Date: Tue, 7 Nov 2023 13:47:04 +0000 Subject: [PATCH 3/4] Updated tests for #1467. --- CHANGELOG.md | 2 +- pints/_mcmc/__init__.py | 2 +- pints/tests/test_mcmc_adaptive.py | 2 +- pints/tests/test_mcmc_controller.py | 12 ++++++--- pints/tests/test_nested_controller.py | 4 +-- pints/tests/test_opt_adam.py | 18 ++++++------- pints/tests/test_opt_controller.py | 4 +-- pints/tests/test_opt_irpropmin.py | 38 +++++++++++++-------------- pints/tests/test_opt_nelder_mead.py | 2 +- pints/tests/test_opt_pso.py | 4 +-- 10 files changed, 46 insertions(+), 42 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index fc09fc49f..cdeb45232 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,10 +7,10 @@ All notable changes to this project will be documented in this file. ### Added - [#1499](https://github.com/pints-team/pints/pull/1499) Added a log-uniform prior class. ### Changed +- [#1503](https://github.com/pints-team/pints/pull/1503) Stopped showing time units in controller logs, because the units change depending on the output type (see #1467). ### Deprecated ### Removed ### Fixed -- [#1503](https://github.com/pints-team/pints/pull/1503) Stopped showing time units in controller logs to fix occassional errors. ## [0.5.0] - 2023-07-27 diff --git a/pints/_mcmc/__init__.py b/pints/_mcmc/__init__.py index 39e3390e1..ff566bf54 100644 --- a/pints/_mcmc/__init__.py +++ b/pints/_mcmc/__init__.py @@ -666,7 +666,7 @@ def run(self): for sampler in self._samplers: sampler._log_init(logger) # Note: deleted time units from header, see - # https://github.com/pints-team/pints/issues/1467 + # https://github.com/pints-team/pints/issues/1467 logger.add_time('Time') # Pre-allocate arrays for chain storage diff --git a/pints/tests/test_mcmc_adaptive.py b/pints/tests/test_mcmc_adaptive.py index 77de55970..a89a4bd81 100755 --- a/pints/tests/test_mcmc_adaptive.py +++ b/pints/tests/test_mcmc_adaptive.py @@ -145,7 +145,7 @@ def test_logging(self): self.assertIn('Accept.', text) self.assertIn('Eval.', text) self.assertIn('Iter.', text) - self.assertIn('Time m:s', text) + self.assertIn('Time', text) def test_replace(self): # Tests the replace() method diff --git a/pints/tests/test_mcmc_controller.py b/pints/tests/test_mcmc_controller.py index fb03de62e..98220805b 100755 --- a/pints/tests/test_mcmc_controller.py +++ b/pints/tests/test_mcmc_controller.py @@ -14,6 +14,7 @@ import unittest.mock import numpy as np import numpy.testing as npt +import warnings from shared import StreamCapture, TemporaryDirectory @@ -24,7 +25,7 @@ 'Using Haario-Bardenet adaptive covariance MCMC', 'Generating 3 chains.', 'Running in sequential mode.', - 'Iter. Eval. Accept. Accept. Accept. Time m:s', + 'Iter. Eval. Accept. Accept. Accept. Time ', '0 3 0 0 0 0:00.0', '1 6 0 0 0.5 0:00.0', '2 9 0 0 0.333 0:00.0', @@ -35,7 +36,7 @@ ] LOG_FILE = [ - 'Iter. Eval. Accept. Accept. Accept. Time m:s', + 'Iter. Eval. Accept. Accept. Accept. Time ', '0 3 0 0 0 0:00.0', '1 6 0 0 0.5 0:00.0', '2 9 0 0 0.333 0:00.0', @@ -791,8 +792,11 @@ def test_log_pdf_storage_in_memory_single_complex(self): def test_deprecated_alias(self): - mcmc = pints.MCMCSampling( - self.log_posterior, 1, [self.real_parameters]) + with warnings.catch_warnings(record=True) as w: + mcmc = pints.MCMCSampling( + self.log_posterior, 1, [self.real_parameters]) + self.assertEqual(len(w), 1) + self.assertIn('deprecated', str(w[-1].message)) self.assertIsInstance(mcmc, pints.MCMCController) def test_exception_on_multi_use(self): diff --git a/pints/tests/test_nested_controller.py b/pints/tests/test_nested_controller.py index eb238962e..e6efdfe65 100755 --- a/pints/tests/test_nested_controller.py +++ b/pints/tests/test_nested_controller.py @@ -160,7 +160,7 @@ def test_logging(self): self.assertEqual(lines[1], 'Number of active points: 400') self.assertEqual(lines[2], 'Total number of iterations: 20') self.assertEqual(lines[3], 'Total number of posterior samples: 2') - self.assertEqual(lines[4], ('Iter. Eval. Time m:s Delta_log(z) ' + + self.assertEqual(lines[4], ('Iter. Eval. Time Delta_log(z) ' + 'Acceptance rate')) pattern = re.compile('[0-9]+[ ]+[0-9]+[ ]+[0-9]{1}:[0-9]{2}.[0-9]{1}') for line in lines[5:]: @@ -182,7 +182,7 @@ def test_logging(self): lines = f.read().splitlines() self.assertEqual(c.text(), '') self.assertEqual(len(lines), 23) - self.assertEqual(lines[0], ('Iter. Eval. Time m:s Delta_log(z) ' + + self.assertEqual(lines[0], ('Iter. Eval. Time Delta_log(z) ' + 'Acceptance rate')) pattern = re.compile('[0-9]+[ ]+[0-9]+[ ]+[0-9]{1}:[0-9]{2}.[0-9]{1}') for line in lines[5:]: diff --git a/pints/tests/test_opt_adam.py b/pints/tests/test_opt_adam.py index eba59a714..39f541c4c 100755 --- a/pints/tests/test_opt_adam.py +++ b/pints/tests/test_opt_adam.py @@ -19,15 +19,15 @@ method = pints.Adam -log_1 = ''' -Minimising error measure -Using Adam -Running in sequential mode. -Iter. Eval. Best Current b1 b2 Time m:s -0 1 0.02 0.02 0.9 0.999 0:00.0 -1 2 5e-17 5e-17 0.81 0.998001 0:00.0 -2 3 5e-17 0.00898 0.729 0.997003 0:00.0 -'''.strip() +log_1 = '\n'.join(( + 'Minimising error measure', + 'Using Adam', + 'Running in sequential mode.', + 'Iter. Eval. Best Current b1 b2 Time ', + '0 1 0.02 0.02 0.9 0.999 0:00.0', + '1 2 5e-17 5e-17 0.81 0.998001 0:00.0', + '2 3 5e-17 0.00898 0.729 0.997003 0:00.0', +)) class TestAdam(unittest.TestCase): diff --git a/pints/tests/test_opt_controller.py b/pints/tests/test_opt_controller.py index dec7cc15b..eae324d23 100755 --- a/pints/tests/test_opt_controller.py +++ b/pints/tests/test_opt_controller.py @@ -251,7 +251,7 @@ def test_logging(self): self.assertEqual(lines[2], 'Running in sequential mode.') self.assertEqual(lines[3], 'Population size: 6') self.assertEqual(lines[4], - 'Iter. Eval. Best Current Time m:s') + 'Iter. Eval. Best Current Time ') self.assertEqual(lines[5][:-3], '0 3 -4.140462 -4.140462 0:0') self.assertEqual(lines[6][:-3], @@ -290,7 +290,7 @@ def test_logging(self): self.assertEqual(lines[2], 'Running in sequential mode.') self.assertEqual(lines[3], 'Population size: 4') self.assertEqual(lines[4], - 'Iter. Eval. Best Current Time m:s') + 'Iter. Eval. Best Current Time ') self.assertEqual(lines[5][:-3], '0 4 6.471867 6.471867 0:0') self.assertEqual(lines[6][:-3], diff --git a/pints/tests/test_opt_irpropmin.py b/pints/tests/test_opt_irpropmin.py index cc2dbb5a5..4a4a30893 100755 --- a/pints/tests/test_opt_irpropmin.py +++ b/pints/tests/test_opt_irpropmin.py @@ -19,25 +19,25 @@ method = pints.IRPropMin -log_1 = ''' -Minimising error measure -Using iRprop- -Running in sequential mode. -Iter. Eval. Best Current Min. step Max. step Bound corr. Time m:s -0 1 0.02 0.02 0.12 0.12 0:00.0 -1 2 0.0008 0.0008 0.06 0.06 0:00.0 -'''.strip() - -log_2 = ''' -Minimising error measure -Using iRprop- -Running in sequential mode. -Iter. Eval. Best Current Min. step Max. step Bound corr. Time m:s -0 1 0.02 0.02 0.11 0.11 0:00.0 -1 2 0.0002 0.0002 0.055 0.055 0:00.0 -2 3 0.0002 0.0002 0.055 0.055 0:00.0 -3 4 0.0002 0.00405 0.03 0.03 0:00.0 -'''.strip() +log_1 = '\n'.join(( + 'Minimising error measure', + 'Using iRprop-', + 'Running in sequential mode.', + 'Iter. Eval. Best Current Min. step Max. step Bound corr. Time ', + '0 1 0.02 0.02 0.12 0.12 0:00.0', + '1 2 0.0008 0.0008 0.06 0.06 0:00.0', +)) + +log_2 = '\n'.join(( + 'Minimising error measure', + 'Using iRprop-', + 'Running in sequential mode.', + 'Iter. Eval. Best Current Min. step Max. step Bound corr. Time ', + '0 1 0.02 0.02 0.11 0.11 0:00.0', + '1 2 0.0002 0.0002 0.055 0.055 0:00.0', + '2 3 0.0002 0.0002 0.055 0.055 0:00.0', + '3 4 0.0002 0.00405 0.03 0.03 0:00.0', +)) class TestIRPropMin(unittest.TestCase): diff --git a/pints/tests/test_opt_nelder_mead.py b/pints/tests/test_opt_nelder_mead.py index 8ea169490..89f85ad1c 100755 --- a/pints/tests/test_opt_nelder_mead.py +++ b/pints/tests/test_opt_nelder_mead.py @@ -170,7 +170,7 @@ def test_rosenbrock(self): 'Minimising error measure', 'Using Nelder-Mead', 'Running in sequential mode.', - 'Iter. Eval. Best Current Time m:s', + 'Iter. Eval. Best Current Time', '0 3 865.9531 865.9531 0:00.0', '1 4 832.5452 832.5452 0:00.0', '2 5 832.5452 832.5452 0:00.0', diff --git a/pints/tests/test_opt_pso.py b/pints/tests/test_opt_pso.py index 9b9795a2f..29b70a446 100755 --- a/pints/tests/test_opt_pso.py +++ b/pints/tests/test_opt_pso.py @@ -130,7 +130,7 @@ def test_logging(self): self.assertEqual( lines[2], 'Running in parallel with 4 worker processes.') self.assertEqual(lines[3], 'Population size: 6') - self.assertEqual(lines[4], 'Iter. Eval. Best Current Time m:s') + self.assertEqual(lines[4], 'Iter. Eval. Best Current Time ') pint = '[0-9]+[ ]+' pflt = '[0-9.-]+[ ]+' @@ -158,7 +158,7 @@ def test_logging(self): self.assertEqual( lines[0], 'Iter. Eval. Best Current f0 f1 f2 ' - 'f3 f4 f5 Time m:s' + 'f3 f4 f5 Time ' ) pattern = re.compile(pint * 2 + pflt * 8 + ptim) From 7e6d2289f40b131968caf3db4338f2a1107d8998 Mon Sep 17 00:00:00 2001 From: Michael Clerx Date: Tue, 7 Nov 2023 13:51:52 +0000 Subject: [PATCH 4/4] Reverted accidental notebook update --- examples/optimisation/snes.ipynb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/examples/optimisation/snes.ipynb b/examples/optimisation/snes.ipynb index 9b2f87f2b..177974060 100644 --- a/examples/optimisation/snes.ipynb +++ b/examples/optimisation/snes.ipynb @@ -132,7 +132,7 @@ ], "metadata": { "kernelspec": { - "display_name": "Python 3 (ipykernel)", + "display_name": "Python 3", "language": "python", "name": "python3" }, @@ -146,7 +146,7 @@ "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", - "version": "3.11.6" + "version": "3.9.7" } }, "nbformat": 4,