Skip to content

Commit

Permalink
Enable resuming the importance sampler
Browse files Browse the repository at this point in the history
  • Loading branch information
joezuntz committed Aug 13, 2024
1 parent 921e2a9 commit 96de422
Showing 1 changed file with 13 additions and 1 deletion.
14 changes: 13 additions & 1 deletion cosmosis/samplers/importance/importance_sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 96de422

Please sign in to comment.