def _generate_from_current(self): """Generates a waveform from the current parameters. """ try: new_waveform = self.generator(**self.current_params) return new_waveform except RuntimeError as e: if self.record_failures: import h5py from pycbc.io.hdf import dump_state global failed_counter if self.mpi_enabled: outname = 'failed/params_%s.hdf' % self.mpi_rank else: outname = 'failed/params.hdf' if not os.path.exists('failed'): os.makedirs('failed') with h5py.File(outname) as f: dump_state(self.current_params, f, dsetname=str(failed_counter)) failed_counter += 1 # we'll get a RuntimeError if lalsimulation failed to generate # the waveform for whatever reason strparams = ' | '.join(['{}: {}'.format( p, str(val)) for p, val in self.current_params.items()]) raise FailedWaveformError("Failed to generate waveform with " "parameters:\n{}\nError was: {}" .format(strparams, e))
def save_state(self, tnum_finished, filename): """Save the current state of the background buffers""" from pycbc.io.hdf import dump_state self.tnum_finished = tnum_finished logging.info('Writing checkpoint file at template %s', tnum_finished) fp = h5py.File(filename, 'w') dump_state(self, fp, protocol=cPickle.HIGHEST_PROTOCOL) fp.close()
def write_results(self, filename): """Writes samples, model stats, acceptance fraction, and random state to the given file. Parameters ----------- filename : str The file to write to. The file is opened using the ``io`` class in an an append state. """ with self.io(filename, 'a') as fp: # write samples fp.write_samples(self.samples, self.samples.keys()) # write log evidence fp.write_logevidence(self.logz, self.logz_err) # write full ultranest formatted results dump_state(self.result, fp, path='sampler_info', dsetname='presult')
def write_pickled_data_into_checkpoint_file(self, state): """Dump the sampler state into checkpoint file """ if 'sampler_info/saved_state' not in self: self.create_group('sampler_info/saved_state') dump_state(state, self, path='sampler_info/saved_state')