class_docstr: Base class for all inference algorithms.
  
  Converts BayesianNetwork and MarkovNetwork to a uniform representation so that inference
  algorithms can be applied. Also, it checks if all the associated CPDs / Factors are
  consistent with the model.
  
  Initialize inference for a model.
  
  Parameters
  ----------
  model: pgmpy.models.BayesianNetwork or pgmpy.models.MarkovNetwork or pgmpy.models.NoisyOrModel
      model for which to initialize the inference object.
  
  Examples
  --------
  >>> from pgmpy.inference import Inference
  >>> from pgmpy.models import BayesianNetwork
  >>> from pgmpy.factors.discrete import TabularCPD
  >>> student = BayesianNetwork([('diff', 'grade'), ('intel', 'grade')])
  >>> diff_cpd = TabularCPD('diff', 2, [[0.2], [0.8]])
  >>> intel_cpd = TabularCPD('intel', 2, [[0.3], [0.7]])
  >>> grade_cpd = TabularCPD('grade', 3, [[0.1, 0.1, 0.1, 0.1],
  ...                                     [0.1, 0.1, 0.1, 0.1],
  ...                                     [0.8, 0.8, 0.8, 0.8]],
  ...                        evidence=['diff', 'intel'], evidence_card=[2, 2])
  >>> student.add_cpds(diff_cpd, intel_cpd, grade_cpd)
  >>> model = Inference(student)
  
  >>> from pgmpy.models import MarkovNetwork
  >>> from pgmpy.factors.discrete import DiscreteFactor
  >>> import numpy as np
  >>> student = MarkovNetwork([('Alice', 'Bob'), ('Bob', 'Charles'),
  ...                        ('Charles', 'Debbie'), ('Debbie', 'Alice')])
  >>> factor_a_b = DiscreteFactor(['Alice', 'Bob'], cardinality=[2, 2],
  ...                             values=np.random.rand(4))
  >>> factor_b_c = DiscreteFactor(['Bob', 'Charles'], cardinality=[2, 2],
  ...                             values=np.random.rand(4))
  >>> factor_c_d = DiscreteFactor(['Charles', 'Debbie'], cardinality=[2, 2],
  ...                             values=np.random.rand(4))
  >>> factor_d_a = DiscreteFactor(['Debbie', 'Alice'], cardinality=[2, 2],
  ...                             values=np.random.rand(4))
  >>> student.add_factors(factor_a_b, factor_b_c, factor_c_d, factor_d_a)
  >>> model = Inference(student)
class_docstr_tok: ["Base","class","for","all","inference","algorithms",".","Converts","BayesianNetwork","and","MarkovNetwork","to","a","uniform","representation","so","that","inference","algorithms","can","be","applied",".","Also",",","it","checks","if","all","the","associated","CPDs","\/","Factors","are","consistent","with","the","model",".","Initialize","inference","for","a","model",".","Parameters","--","--","--","--","--","model",":","pgmpy.models.BayesianNetwork","or","pgmpy.models.MarkovNetwork","or","pgmpy.models.NoisyOrModel","model","for","which","to","initialize","the","inference","object",".","Examples","--","--","--","--",">",">",">","from","pgmpy.inference","import","Inference",">",">",">","from","pgmpy.models","import","BayesianNetwork",">",">",">","from","pgmpy.factors.discrete","import","TabularCPD",">",">",">","student","=","BayesianNetwork","(","[","(","'diff","'",",","'grade","'",")",",","(","'intel","'",",","'grade","'",")","]",")",">",">",">","diff_cpd","=","TabularCPD","(","'diff","'",",","2",",","[","[","0.2","]",",","[","0.8","]","]",")",">",">",">","intel_cpd","=","TabularCPD","(","'intel","'",",","2",",","[","[","0.3","]",",","[","0.7","]","]",")",">",">",">","grade_cpd","=","TabularCPD","(","'grade","'",",","3",",","[","[","0.1",",","0.1",",","0.1",",","0.1","]",",","...","[","0.1",",","0.1",",","0.1",",","0.1","]",",","...","[","0.8",",","0.8",",","0.8",",","0.8","]","]",",","...","evidence=","[","'diff","'",",","'intel","'","]",",","evidence_card=","[","2",",","2","]",")",">",">",">","student.add_cpds","(","diff_cpd",",","intel_cpd",",","grade_cpd",")",">",">",">","model","=","Inference","(","student",")",">",">",">","from","pgmpy.models","import","MarkovNetwork",">",">",">","from","pgmpy.factors.discrete","import","DiscreteFactor",">",">",">","import","numpy","as","np",">",">",">","student","=","MarkovNetwork","(","[","(","'Alice","'",",","'Bob","'",")",",","(","'Bob","'",",","'Charles","'",")",",","...","(","'Charles","'",",","'Debbie","'",")",",","(","'Debbie","'",",","'Alice","'",")","]",")",">",">",">","factor_a_b","=","DiscreteFactor","(","[","'Alice","'",",","'Bob","'","]",",","cardinality=","[","2",",","2","]",",","...","values=np.random.rand","(","4",")",")",">",">",">","factor_b_c","=","DiscreteFactor","(","[","'Bob","'",",","'Charles","'","]",",","cardinality=","[","2",",","2","]",",","...","values=np.random.rand","(","4",")",")",">",">",">","factor_c_d","=","DiscreteFactor","(","[","'Charles","'",",","'Debbie","'","]",",","cardinality=","[","2",",","2","]",",","...","values=np.random.rand","(","4",")",")",">",">",">","factor_d_a","=","DiscreteFactor","(","[","'Debbie","'",",","'Alice","'","]",",","cardinality=","[","2",",","2","]",",","...","values=np.random.rand","(","4",")",")",">",">",">","student.add_factors","(","factor_a_b",",","factor_b_c",",","factor_c_d",",","factor_d_a",")",">",">",">","model","=","Inference","(","student",")"]
docstr: Initializes all the data structures which will
  later be used by the inference algorithms.
docstr_tok: ["Initializes","all","the","data","structures","which","will","later","be","used","by","the","inference","algorithms","."]
code: def _initialize_structures(self):
      """
      Initializes all the data structures which will
      later be used by the inference algorithms.
      """
      if isinstance(self.model, JunctionTree):
          self.variables = set(chain(*self.model.nodes()))
      else:
          self.variables = self.model.nodes()
  
      self.cardinality = {}
      self.factors = defaultdict(list)
  
      if isinstance(self.model, BayesianNetwork):
          self.state_names_map = {}
          for node in self.model.nodes():
              cpd = self.model.get_cpds(node)
              if isinstance(cpd, TabularCPD):
                  self.cardinality[node] = cpd.variable_card
                  cpd = cpd.to_factor()
              for var in cpd.scope():
                  self.factors[var].append(cpd)
              self.state_names_map.update(cpd.no_to_name)
  
      elif isinstance(
          self.model, (MarkovNetwork, FactorGraph, JunctionTree)
      ):
          self.cardinality = self.model.get_cardinality()
  
          for factor in self.model.get_factors():
              for var in factor.variables:
                  self.factors[var].append(factor)
  
      elif isinstance(self.model, DynamicBayesianNetwork):
          self.start_bayesian_model = BayesianNetwork(
              self.model.get_intra_edges(0)
          )
          self.start_bayesian_model.add_cpds(
              *self.model.get_cpds(time_slice=0)
          )
          cpd_inter = [
              self.model.get_cpds(node)
              for node in self.model.get_interface_nodes(1)
          ]
          self.interface_nodes = self.model.get_interface_nodes(0)
          self.one_and_half_model = BayesianNetwork(
              self.model.get_inter_edges()
              + self.model.get_intra_edges(1)
          )
          self.one_and_half_model.add_cpds(
              *(self.model.get_cpds(time_slice=1) + cpd_inter)
          )
  
file_path: pgmpy/pgmpy/inference/base.py
imports: from collections import defaultdict
  from itertools import chain
  import numpy
  from pgmpy.factors.discrete import TabularCPD
  from pgmpy.models import BayesianNetwork, DynamicBayesianNetwork, FactorGraph, JunctionTree, MarkovNetwork
  from pgmpy.utils import compat_fns
label_desc: Use image node_id 2 for calling the Inference obj's underlying member method code with example usage: obj._initialize_structures() without return types