def sample(self, model, data, nsamples=None, walker_initial_pos=None): if nsamples is not None: # deprecated as of 3.3 from holopy.fitting import fit_warning fit_warning('EmceeStrategy(nsamples=X)', 'passing nsamples to EmceeStrategy.sample') self.nsamples = nsamples if walker_initial_pos is not None: # deprecated as of 3.3 from holopy.fitting import fit_warning fit_warning('EmceeStrategy(walker_initial_pos=X)', 'passing walker_initial_pos to EmceeStrategy.sample') self.walker_initial_pos = walker_initial_pos time_start = time.time() if self.npixels is not None: data = make_subset_data(data, pixels=self.npixels, seed=self.seed) if self.walker_initial_pos is None: self.walker_initial_pos = model.generate_guess(self.nwalkers, seed=self.seed) sampler = sample_emcee(model=model, data=data, nwalkers=self.nwalkers, walker_initial_pos=self.walker_initial_pos, nsamples=self.nsamples, parallel=self.parallel, seed=self.seed) samples = emcee_samples_DataArray(sampler, model._parameters) lnprobs = emcee_lnprobs_DataArray(sampler) d_time = time.time() - time_start kwargs = {'lnprobs': lnprobs, 'samples': samples} return SamplingResult(data, model, self, d_time, kwargs)
def sample(self, model, data, nsamples=1000, walker_initial_pos=None): if self.pixels is not None: data = make_subset_data(data, pixels=self.pixels) if walker_initial_pos is None: walker_initial_pos = self.make_guess(model.parameters) sampler = sample_emcee(model=model, data=data, nwalkers=self.nwalkers, walker_initial_pos=walker_initial_pos, nsamples=nsamples, threads=self.threads, cleanup_threads=self.cleanup_threads, seed=self.seed) try: acor = sampler.acor except emcee.autocorr.AutocorrError: acor = None samples = emcee_samples_DataArray(sampler, model.parameters) lnprobs = emcee_lnprobs_DataArray(sampler) return SamplingResult(xr.Dataset({ 'samples': samples, 'lnprobs': lnprobs, 'data': data }), model=model, strategy=self)
def generate_sampling_result(): samples = np.array([[[1, 2], [11, 12], [3, 3]], [[0, 3], [1, 3], [5, 6]]]) samples = xr.DataArray(samples, dims=['walker', 'chain', 'parameter'], coords={'parameter': ['p1', 'p2']}) lnprobs = xr.DataArray([[10, 9, 8], [7, 6, 5]], dims=['walker', 'chain']) result = SamplingResult(DATA, MODEL, EmceeStrategy(), 10, kwargs={ 'samples': samples, 'lnprobs': lnprobs }) return result
def sample(self, data, strategy=None): strategy = self.validate_strategy(strategy, 'sample') kwargs = {'samples': None, 'lnprobs': None, 'intervals': None} return SamplingResult(data, self, strategy, 0, kwargs)