From 96de422609a3fd3897c4460cdb269582454c4d9d Mon Sep 17 00:00:00 2001 From: Joe Zuntz Date: Tue, 13 Aug 2024 15:46:22 +0200 Subject: [PATCH] Enable resuming the importance sampler --- cosmosis/samplers/importance/importance_sampler.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cosmosis/samplers/importance/importance_sampler.py b/cosmosis/samplers/importance/importance_sampler.py index 88cb7d1c..e46d70dc 100644 --- a/cosmosis/samplers/importance/importance_sampler.py +++ b/cosmosis/samplers/importance/importance_sampler.py @@ -21,6 +21,7 @@ class ImportanceSampler(ParallelSampler): #so the postprocessors don't need to be re-written sampler_outputs = [] parallel_output = False + supports_resume = True def config(self): global importance_pipeline @@ -110,14 +111,25 @@ def load_samples(self, filename): self.current_index = 0 + def resume(self): + if self.output.resumed: + data = np.genfromtxt(self.output._filename, invalid_raise=False) + self.current_index = len(data) + if self.current_index >= self.number_samples: + logs.error(f"You told me to resume the chain, but it has already completed (with {self.current_index} samples).") + else: + logs.overview(f"Continuing importance sampling from existing chain - have {self.current_index} samples already") + + def execute(self): - self.output.comment("Importance sampling from %s"%self.input_filename) #Pick out a chunk of samples to run on start = self.current_index end = start+self.nstep samples_chunk = self.samples[start:end] + logs.overview(f"Importance sampling {start} - {end} of {self.number_samples} from {self.input_filename}") + #Run the pipeline on each of the samples if self.pool: results = self.pool.map(task, samples_chunk)