Skip to content

Fix latent validation error in get_random() when nodes are isolated#3141

Open
anusa-saha wants to merge 1 commit intopgmpy:devfrom
anusa-saha:bugs
Open

Fix latent validation error in get_random() when nodes are isolated#3141
anusa-saha wants to merge 1 commit intopgmpy:devfrom
anusa-saha:bugs

Conversation

@anusa-saha
Copy link
Contributor

@anusa-saha anusa-saha commented Mar 22, 2026

The following checklist is mandatory.

Your PR will be closed if you remove the checklist or do not answer the questions to a satisfactory level. Use of LLMs is strictly forbidden for any part of this checklist (including for improving language), and will result in a ban if we find any use of LLMs.

Your checklist for this pull request

  • Have you followed all the steps from our Contributing Guide?
  • Does the PR fully address the linked issue and is within its defined scope? If you are still working on the PR, mark it as draft.
  • Are all the GitHub Actions checks passing? If not, mark your PR as draft while you fix it.

Please answer the following questions:

  • Did you use an LLM for any assistance with this PR? Please describe in detail (around a paragraph) how and what you used it for?
    Yes, to find minimal changes to remove the bugs

  • Are you able to fully explain your changes? We expect you to fully understand the algorithm and take full responsibility for any changes in this PR.
    Yes

  • What steps have you taken to verify that the changes correctly address the issue? And what edge cases have you considered? Other than running tests, what else have you verified?

from pgmpy.base import DAG
from pgmpy.models import DiscreteBayesianNetwork

dag = DAG()
dag.add_nodes_from(["X_0", "X_1", "X_2"])
dag.add_edge("X_0", "X_1")

dag.latents = {"X_0", "X_1", "X_2"}

# Raises ValueError because X_2 is not in graph (no edges)
model = DiscreteBayesianNetwork(dag.edges(), latents=dag.latents)

This causes an error for isolated edges

from pgmpy.base import DAG
from pgmpy.models import DiscreteBayesianNetwork

dag = DAG()
dag.add_nodes_from(["X_0", "X_1", "X_2"])
dag.add_edge("X_0", "X_1")

dag.latents = {"X_0", "X_1", "X_2"}

# Fix
model = DiscreteBayesianNetwork(dag)
model.latents = dag.latents
  • Has the LLM added try-except blocks? They will need to be removed; any error handling must be explicit.
    N/A

  • Have you used LLM for generating tests? They need to be compressed into a smaller number of tests without reducing coverage.
    N/A

Issue number(s) that this pull request fixes

List of changes to the codebase in this pull request

  • Replaced edge-based initialization with full DAG initialization: Instead of passing dag.edges() (which dropped isolated nodes), we now pass the entire dag to the model constructor so all nodes are preserved.
  • Moved latent assignment after model creation: Instead of validating latents during initialization (which caused errors), assigned model.latents = dag.latents after the graph is fully built, ensuring all latent variables exist in the model.

@anusa-saha
Copy link
Contributor Author

@ankurankan do i need to add a test for this as well??

@codecov
Copy link

codecov bot commented Mar 22, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 95.83%. Comparing base (9bffa73) to head (c52fe13).

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff           @@
##              dev    #3141   +/-   ##
=======================================
  Coverage   95.83%   95.83%           
=======================================
  Files         527      527           
  Lines       29842    29842           
=======================================
  Hits        28599    28599           
  Misses       1243     1243           
Files with missing lines Coverage Δ
pgmpy/models/DiscreteBayesianNetwork.py 95.41% <100.00%> (ø)
pgmpy/models/LinearGaussianBayesianNetwork.py 94.62% <100.00%> (ø)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Fix latent role assignment order in get_random()

1 participant