Skip to content

Commit

Permalink
Honor discard_tuned_samples during KeyboardInterrupt (#3785)
Browse files Browse the repository at this point in the history
* Honor discard_tuned_samples during KeyboardInterrupt

* Do not compute convergence checks without samples
  • Loading branch information
aseyboldt authored Jul 1, 2020
1 parent 747db63 commit 8560f1e
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
9 changes: 8 additions & 1 deletion pymc3/backends/report.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,14 @@ def raise_ok(self, level='error'):
if errors:
raise ValueError('Serious convergence issues during sampling.')

def _run_convergence_checks(self, idata:arviz.InferenceData, model):
def _run_convergence_checks(self, idata: arviz.InferenceData, model):
if not hasattr(idata, 'posterior'):
msg = "No posterior samples. Unable to run convergence checks"
warn = SamplerWarning(WarningType.BAD_PARAMS, msg, 'info',
None, None, None)
self._add_warnings([warn])
return

if idata.posterior.sizes['chain'] == 1:
msg = ("Only one chain was sampled, this makes it impossible to "
"run some convergence checks")
Expand Down
7 changes: 6 additions & 1 deletion pymc3/sampling.py
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ def sample(
"random_seed": random_seed,
"cores": cores,
"callback": callback,
"discard_tuned_samples": discard_tuned_samples,
}

sample_args.update(kwargs)
Expand Down Expand Up @@ -1347,6 +1348,7 @@ def _mp_sample(
trace=None,
model=None,
callback=None,
discard_tuned_samples=True,
**kwargs
):
"""Main iteration for multiprocess sampling.
Expand Down Expand Up @@ -1439,7 +1441,10 @@ def _mp_sample(
raise
return MultiTrace(traces)
except KeyboardInterrupt:
traces, length = _choose_chains(traces, tune)
if discard_tuned_samples:
traces, length = _choose_chains(traces, tune)
else:
traces, length = _choose_chains(traces, 0)
return MultiTrace(traces)[:length]
finally:
for trace in traces:
Expand Down

0 comments on commit 8560f1e

Please sign in to comment.