Skip to content

2560: fail-fast when trace vars mismatches model vars #2658

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

Closed
wants to merge 2 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
15 changes: 15 additions & 0 deletions 2560.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pymc3 as pm
with pm.Model() as model:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should be a function in pymc3/tests/test_text_backend.py. Something like

def test_wont_dump_mismatched_vars():
    with pm.Model() as model:
        a = pm.Gamma('a', mu=10.0, sd=2.0)
        b = pm.Gamma('b', mu=10.0, sd=2.0)

        partial_trace = pm.sample(draws=100, tune=0, trace=[model.a, model.a_log__])
        assert len(partial_trace.varnames) == 2
    
        with pytest.raises(ValueError):
            pm.backends.text.dump('trace.text', partial_trace)

a = pm.Gamma('a', mu=10.0, sd=2.0)
b = pm.Gamma('b', mu=10.0, sd=2.0)

trace = pm.sample(trace=[model.a, model.a_log__])
assert len(trace.varnames) == 2
# you have to provide 4 vars:
# trace = pm.sample(trace=[model.a, model.a_log__, model.b, model.b_log__])

pm.backends.text.dump('trace.text', trace)

loaded = pm.backends.text.load('trace.text')
print("loaded: ", loaded)
x = loaded[0] #!!! Will throw a KeyError looking for 'b_log__'
9 changes: 8 additions & 1 deletion pymc3/backends/text.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import os
import pandas as pd

from ..model import modelcontext
from ..backends import base, ndarray
from . import tracetab as ttab
from ..theanof import floatX
Expand Down Expand Up @@ -184,7 +185,7 @@ def load(name, model=None):
return base.MultiTrace(straces)


def dump(name, trace, chains=None):
def dump(name, trace, model=None, chains=None):
"""Store values from NDArray trace as CSV files.

Parameters
Expand All @@ -200,6 +201,12 @@ def dump(name, trace, chains=None):
os.mkdir(name)
if chains is None:
chains = trace.chains
model = modelcontext(model)
model_vars = sorted([x.name for x in model.unobserved_RVs])
trace_vars = sorted(trace.varnames)

if model_vars != trace_vars:
raise ValueError('Variables mismatch: model_vars=' + str(model_vars) + ", trace_vars=" + str(trace_vars))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This error might be more explicit:

ValueError('Cannot serialize a trace that does not include all model variables.'
           '\n\tmodel variables: {}'
           '\n\ttrace variables: {}'.format(', '.join(model_vars), ', '.join(trace_vars))


for chain in chains:
filename = os.path.join(name, 'chain-{}.csv'.format(chain))
Expand Down