diff --git a/bilby/core/sampler/base_sampler.py b/bilby/core/sampler/base_sampler.py index a5ea48ebcbb58079aa227d794af8125a580e687e..6bf70b88c662ef905ac7784f16347e7ca524012f 100644 --- a/bilby/core/sampler/base_sampler.py +++ b/bilby/core/sampler/base_sampler.py @@ -70,6 +70,8 @@ class Sampler(object): only advisable for testing environments result: bilby.core.result.Result Container for the results of the sampling run + exit_code: int + System exit code to return on interrupt kwargs: dict Dictionary of keyword arguments that can be used in the external sampler @@ -91,7 +93,7 @@ class Sampler(object): self, likelihood, priors, outdir='outdir', label='label', use_ratio=False, plot=False, skip_import_verification=False, injection_parameters=None, meta_data=None, result_class=None, - likelihood_benchmark=False, soft_init=False, + likelihood_benchmark=False, soft_init=False, exit_code=130, **kwargs): self.likelihood = likelihood if isinstance(priors, PriorDict): @@ -114,6 +116,8 @@ class Sampler(object): self._constraint_parameter_keys = list() self._initialise_parameters() + self.exit_code = exit_code + if not soft_init: self._verify_parameters() self._time_likelihood() diff --git a/bilby/core/sampler/cpnest.py b/bilby/core/sampler/cpnest.py index 4c46e02f29b35a0a1dc5615c52b38ad67cab072e..63f454783ee9410dda379e56ef12aa0e143f4aac 100644 --- a/bilby/core/sampler/cpnest.py +++ b/bilby/core/sampler/cpnest.py @@ -105,7 +105,12 @@ class Cpnest(NestedSampler): logger.info( "Attempting to rerun with kwarg {} removed".format(kwarg)) self.kwargs.pop(kwarg) - out.run() + try: + out.run() + except SystemExit as e: + import sys + logger.info(f"Caught exit code {e.args[0]}, exiting with signal {self.exit_code}") + sys.exit(self.exit_code) if self.plot: out.plot() diff --git a/bilby/core/sampler/dynesty.py b/bilby/core/sampler/dynesty.py index 6dcf9fd13a793d9059875094513dabc712b2843a..a2d0be342ef0fbbce416b67ce92c79cbade16a00 100644 --- a/bilby/core/sampler/dynesty.py +++ b/bilby/core/sampler/dynesty.py @@ -147,6 +147,7 @@ class Dynesty(NestedSampler): super(Dynesty, self).__init__(likelihood=likelihood, priors=priors, outdir=outdir, label=label, use_ratio=use_ratio, plot=plot, skip_import_verification=skip_import_verification, + exit_code=exit_code, **kwargs) self.n_check_point = n_check_point self.check_point = check_point @@ -164,7 +165,6 @@ class Dynesty(NestedSampler): self.resume_file = '{}/{}_resume.pickle'.format(self.outdir, self.label) self.sampling_time = datetime.timedelta() - self.exit_code = exit_code try: signal.signal(signal.SIGTERM, self.write_current_state_and_exit) diff --git a/bilby/core/sampler/ptemcee.py b/bilby/core/sampler/ptemcee.py index 52afd644023fe2139966625912bff20e30342780..0e9b71dce449df910c0bfba229f4afea91a145ba 100644 --- a/bilby/core/sampler/ptemcee.py +++ b/bilby/core/sampler/ptemcee.py @@ -156,6 +156,7 @@ class Ptemcee(MCMCSampler): use_ratio=use_ratio, plot=plot, skip_import_verification=skip_import_verification, + exit_code=exit_code, **kwargs ) @@ -169,7 +170,6 @@ class Ptemcee(MCMCSampler): signal.signal(signal.SIGALRM, self.write_current_state_and_exit) # Checkpointing inputs - self.exit_code = exit_code self.resume = resume self.check_point_deltaT = check_point_deltaT self.check_point_plot = check_point_plot diff --git a/bilby/core/sampler/pymultinest.py b/bilby/core/sampler/pymultinest.py index 8fd24230a29da68df1ff26098ff155cc0d5cc707..f962c3149176c0e0013e0facfa7a52d013a02c5a 100644 --- a/bilby/core/sampler/pymultinest.py +++ b/bilby/core/sampler/pymultinest.py @@ -84,6 +84,7 @@ class Pymultinest(NestedSampler): use_ratio=use_ratio, plot=plot, skip_import_verification=skip_import_verification, + exit_code=exit_code, **kwargs ) self._apply_multinest_boundaries() diff --git a/bilby/core/sampler/ultranest.py b/bilby/core/sampler/ultranest.py index 50dd4fe710cf02bb9429b4a798f79226dc26e34c..1cba66f4d667d94cdda0667078091df20581ae80 100644 --- a/bilby/core/sampler/ultranest.py +++ b/bilby/core/sampler/ultranest.py @@ -91,10 +91,10 @@ class Ultranest(NestedSampler): use_ratio=use_ratio, plot=plot, skip_import_verification=skip_import_verification, + exit_code=exit_code, **kwargs, ) self._apply_ultranest_boundaries() - self.exit_code = exit_code signal.signal(signal.SIGTERM, self.write_current_state_and_exit) signal.signal(signal.SIGINT, self.write_current_state_and_exit)