From 5574c0a721786f0831ba15c706f49425f116a160 Mon Sep 17 00:00:00 2001
From: Gregory Ashton <gregory.ashton@ligo.org>
Date: Sun, 5 May 2019 21:02:31 -0500
Subject: [PATCH] Minor adjustments to dynesty

1) Print the progress to STDOUT rather than STDERR - this will keep the
log file clean
2) Add SIGALRM to checkpoint signals
3) Change some logging messages to help diagnose checkpointing issues
4) Remove write current state when it has just been read
5) Improve documentation on how the periodic/reflective boundaries are
implemented
---
 CHANGELOG.md                  |  1 +
 bilby/core/sampler/dynesty.py | 24 ++++++++++++------------
 2 files changed, 13 insertions(+), 12 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 4f1cf9a60..047568dd7 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -13,6 +13,7 @@ Periodic boundaries are supported as before.
 Reflective boundaries are supported in `dynesty` and `cpnest`.  
 - Added state plotting for dynesty. Use `check_point_plot=True` in the `run_sampler` 
 function to create trace plots during the dynesty checkpoints
+- Dynesty now prints the progress to STDOUT rather than STDERR
 
 ### Removed
 -
diff --git a/bilby/core/sampler/dynesty.py b/bilby/core/sampler/dynesty.py
index 18a72c3be..8e21ec5af 100644
--- a/bilby/core/sampler/dynesty.py
+++ b/bilby/core/sampler/dynesty.py
@@ -119,6 +119,7 @@ class Dynesty(NestedSampler):
 
         signal.signal(signal.SIGTERM, self.write_current_state_and_exit)
         signal.signal(signal.SIGINT, self.write_current_state_and_exit)
+        signal.signal(signal.SIGALRM, self.write_current_state_and_exit)
 
     @property
     def sampler_function_kwargs(self):
@@ -181,8 +182,8 @@ class Dynesty(NestedSampler):
             niter, key, logz, logzerr, delta_logz, dlogz)
 
         # Printing.
-        sys.stderr.write(print_str)
-        sys.stderr.flush()
+        sys.stdout.write(print_str)
+        sys.stdout.flush()
 
     def _apply_dynesty_boundaries(self):
         if self.kwargs['periodic'] is None:
@@ -294,12 +295,11 @@ class Dynesty(NestedSampler):
             state is mostly written back to disk.
         """
 
-        logger.debug("Reading resume file {}".format(self.resume_file))
-
         if os.path.isfile(self.resume_file):
+            logger.info("Reading resume file {}".format(self.resume_file))
             with open(self.resume_file, 'rb') as file:
                 saved = pickle.load(file)
-            logger.debug(
+            logger.info(
                 "Succesfuly read resume file {}".format(self.resume_file))
 
             self.sampler.saved_u = list(saved['unit_cube_samples'])
@@ -325,13 +325,11 @@ class Dynesty(NestedSampler):
             self.sampler.live_bound = saved['live_bound']
             self.sampler.live_it = saved['live_it']
             self.sampler.added_live = saved['added_live']
-            if continuing:
-                self.write_current_state(plot=False)
             return True
 
         else:
             logger.debug(
-                "Failed to read resume file {}".format(self.resume_file))
+                "No resume file {}".format(self.resume_file))
             return False
 
     def write_current_state_and_exit(self, signum=None, frame=None):
@@ -452,11 +450,13 @@ class Dynesty(NestedSampler):
 
         Notes
         -----
-        Since dynesty allows periodic parameters to wander outside the unit
-        We also allow parameters with reflective boundaries to wander outside
+        Since dynesty allows periodic parameters to wander outside the unit,
+        here we transform them depending of if they should be periodic or
+        reflective. For reflective boundaries, theta < 0 you shift to |theta|
+        and when theta > 1 you return 2 - theta. For periodic boundaries,
+        if theta < 0, you shift to 1-|theta| and when theta > 1 you shift to
+        |theta| - 1 (i.e. wrap around).
 
-        The logic ensures that when theta < 0 you shift to |theta| and when
-        theta > 1 you return 2 - theta
         """
         theta[self._periodic] = np.mod(theta[self._periodic], 1)
         theta_ref = theta[self._reflective]
-- 
GitLab