Skip to content

Commit

Permalink
adding some blobs tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dfm committed Oct 20, 2017
1 parent ff4e5ec commit 592a696
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 9 deletions.
29 changes: 20 additions & 9 deletions docs/_static/notebooks/monitor.ipynb

Large diffs are not rendered by default.

Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions docs/user/upgrade.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.. _upgrade:

.. module:: emcee

Upgrading From Pre-0.3 Versions
===============================

The v0.3 release of emcee is the biggest update in years.

6 changes: 6 additions & 0 deletions emcee/ensemble.py
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,12 @@ def compute_log_prob(self, coords=None):
log_prob = np.array([float(l[0]) for l in results])
blob = [l[1:] for l in results]
blob = np.array(blob, dtype=np.atleast_1d(blob[0]).dtype)

# Deal with single blobs in a better way
if blob.shape[1] == 1:
m = [slice(None) for i in range(len(blob.shape))]
m[1] = 0
blob = blob[m]
except (IndexError, TypeError):
log_prob = np.array([float(l) for l in results])
blob = None
Expand Down
59 changes: 59 additions & 0 deletions tests/unit/test_blobs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# -*- coding: utf-8 -*-

from __future__ import division, print_function

import pytest
import numpy as np

from emcee import backends, EnsembleSampler

__all__ = ["test_blob_shape"]


class BlobLogProb(object):

def __init__(self, blob_function):
self.blob_function = blob_function

def __call__(self, params):
return -0.5 * np.sum(params**2), self.blob_function(params)


@pytest.mark.parametrize(
"backend", [backends.Backend, backends.hdf.TempHDFBackend],
)
def test_blob_shape(backend):
with backend() as be:
np.random.seed(42)

nblobs = 5
model = BlobLogProb(lambda x: np.random.randn(nblobs))

coords = np.random.randn(32, 3)
nwalkers, ndim = coords.shape
sampler = EnsembleSampler(nwalkers, ndim, model, backend=be)
nsteps = 10
sampler.run_mcmc(coords, nsteps)
assert sampler.get_blobs().shape == (nsteps, nwalkers, nblobs)

model = BlobLogProb(lambda x: np.random.randn())
be.reset(nwalkers, ndim)
sampler = EnsembleSampler(nwalkers, ndim, model, backend=be)
sampler.run_mcmc(coords, nsteps)
assert sampler.get_blobs().shape == (nsteps, nwalkers)

# HDF backend doesn't support the object type
if backend == backends.hdf.TempHDFBackend:
return

model = BlobLogProb(lambda x: "face")
be.reset(nwalkers, ndim)
sampler = EnsembleSampler(nwalkers, ndim, model, backend=be)
sampler.run_mcmc(coords, nsteps)
assert sampler.get_blobs().shape == (nsteps, nwalkers)

model = BlobLogProb(lambda x: (np.random.randn(nblobs), "face"))
be.reset(nwalkers, ndim)
sampler = EnsembleSampler(nwalkers, ndim, model, backend=be)
sampler.run_mcmc(coords, nsteps)
assert sampler.get_blobs().shape == (nsteps, nwalkers, 2)

0 comments on commit 592a696

Please sign in to comment.