Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multitraces should iterate over samples from all chains #2460

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions pymc3/backends/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
See the docstring for pymc3.backends for more information (including
creating custom backends).
"""
import itertools as itl
import numpy as np
from ..model import modelcontext
import warnings
Expand Down Expand Up @@ -247,6 +248,9 @@ def __init__(self, straces):
raise ValueError("Chains are not unique.")
self._straces[strace.chain] = strace

def __iter__(self):
return itl.chain.from_iterable(self._straces[chain] for chain in self.chains)

def __repr__(self):
template = '<{}: {} chains, {} iterations, {} variables>'
return template.format(self.__class__.__name__,
Expand Down
12 changes: 12 additions & 0 deletions pymc3/tests/test_ndarray_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,18 @@ def test_merge_traces_nonunique(self):
base.merge_traces([mtrace0, mtrace1])


class TestMultiTrace_combine_chains(bf.ModelBackendSampledTestCase):
name = None
backend = ndarray.NDArray
shape = ()

def test_iter(self):
mtrace = self.mtrace
expected_len = sum(len(mtrace._straces[chain]) for chain in mtrace.chains)

assert len(list(mtrace)) == expected_len


class TestMultiTrace_add_values(bf.ModelBackendSampledTestCase):
name = None
backend = ndarray.NDArray
Expand Down