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

[WIP] Adds some checks to variable elimination to give more meaningful errors #1133

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
13 changes: 13 additions & 0 deletions pgmpy/inference/ExactInference.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,19 @@ def _get_elimination_order(self, variables, evidence, elimination_order):
-------
list: A list of variables names in the order they need to be eliminated.
"""
for var in variables:
if var not in self.model.nodes():
raise ValueError(
"The variable: {var} is not in the model".format(var=var)
)

if evidence is not None:
for var, state in evidence.items():
if var not in self.model.nodes():
raise ValueError(
"The variable: {var} is not in the model".format(var=var)
)

to_eliminate = (
set(self.variables)
- set(variables)
Expand Down
15 changes: 15 additions & 0 deletions pgmpy/tests/test_inference/test_ExactInference.py
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,21 @@ def test_induced_width(self):
)
self.assertEqual(2, result_width)

def test_query_unknown_var(self):
self.assertRaises(
ValueError, self.bayesian_inference.query, {"variables": ["Unknown"]}
)
self.assertRaises(
ValueError,
self.bayesian_inference.query,
{"variables": ["J"], "evidence": {"Unknown": 1}},
)
self.assertRaises(
ValueError,
self.bayesian_inference.query,
{"variables": ["Unknown"], "evidence": {"Unknown": 1}},
)

def tearDown(self):
del self.bayesian_inference
del self.bayesian_model
Expand Down