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

Refactor sample return #6546

Merged
merged 4 commits into from
Feb 26, 2023
Merged
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
36 changes: 32 additions & 4 deletions pymc/sampling/mcmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ def sample(
compute_convergence_checks: bool = True,
keep_warning_stat: bool = False,
return_inferencedata: bool = True,
idata_kwargs: dict = None,
idata_kwargs: Optional[Dict[str, Any]] = None,
callback=None,
mp_ctx=None,
model: Optional[Model] = None,
Expand Down Expand Up @@ -687,7 +687,36 @@ def sample(

t_sampling = time.time() - t_start

# Wrap chain traces in a MultiTrace
# Packaging, validating and returning the result was extracted
# into a function to make it easier to test and refactor.
return _sample_return(
traces=traces,
tune=tune,
t_sampling=t_sampling,
discard_tuned_samples=discard_tuned_samples,
compute_convergence_checks=compute_convergence_checks,
return_inferencedata=return_inferencedata,
keep_warning_stat=keep_warning_stat,
idata_kwargs=idata_kwargs or {},
model=model,
)


def _sample_return(
*,
traces: Sequence[IBaseTrace],
tune: int,
t_sampling: float,
discard_tuned_samples: bool,
compute_convergence_checks: bool,
return_inferencedata: bool,
keep_warning_stat: bool,
idata_kwargs: Dict[str, Any],
model: Model,
) -> Union[InferenceData, MultiTrace]:
"""Final step of `pm.sampler` that picks/slices chains,
runs diagnostics and converts to the desired return type."""
# Pick and slice chains to keep the maximum number of samples
if discard_tuned_samples:
traces, length = _choose_chains(traces, tune)
else:
Expand Down Expand Up @@ -725,8 +754,7 @@ def sample(
idata = None
if compute_convergence_checks or return_inferencedata:
ikwargs: Dict[str, Any] = dict(model=model, save_warmup=not discard_tuned_samples)
if idata_kwargs:
ikwargs.update(idata_kwargs)
ikwargs.update(idata_kwargs)
idata = pm.to_inference_data(mtrace, **ikwargs)

if compute_convergence_checks:
Expand Down
6 changes: 6 additions & 0 deletions tests/backends/test_ndarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,12 @@ def test_multitrace_nonunique(self):
with pytest.raises(ValueError):
base.MultiTrace([self.strace0, self.strace1])

def test_multitrace_iter_notimplemented(self):
mtrace = base.MultiTrace([self.strace0])
with pytest.raises(NotImplementedError):
for _ in mtrace:
pass


class TestSqueezeCat:
def setup_method(self):
Expand Down
Loading