class_docstr: An ``InferenceEngine`` provides methods to query marginals based on observations and
  make interventions (Do-Calculus) on a ``BayesianNetwork``.
  
  Example:
  ::
      >>> # Create a Bayesian Network with a manually defined DAG
      >>> from causalnex.structure.structuremodel import StructureModel
      >>> from causalnex.network import BayesianNetwork
      >>> from causalnex.inference import InferenceEngine
      >>>
      >>> sm = StructureModel()
      >>> sm.add_edges_from([
      >>>                    ('rush_hour', 'traffic'),
      >>>                    ('weather', 'traffic')
      >>>                    ])
      >>> data = pd.DataFrame({
      >>>                      'rush_hour': [True, False, False, False, True, False, True],
      >>>                      'weather': ['Terrible', 'Good', 'Bad', 'Good', 'Bad', 'Bad', 'Good'],
      >>>                      'traffic': ['heavy', 'light', 'heavy', 'light', 'heavy', 'heavy', 'heavy']
      >>>                      })
      >>> bn = BayesianNetwork(sm)
      >>> # Inference can only be performed on the `BayesianNetwork` with learned nodes states and CPDs
      >>> bn = bn.fit_node_states_and_cpds(data)
      >>>
      >>> # Create an `InferenceEngine` to query marginals and make interventions
      >>> ie = InferenceEngine(bn)
      >>> # Query the marginals as learned from data
      >>> ie.query()['traffic']
      {'heavy': 0.7142857142857142, 'light': 0.2857142857142857}
      >>> # Query the marginals given observations
      >>> ie.query({'rush_hour': True, 'weather': 'Terrible'})['traffic']
      {'heavy': 1.0, 'light': 0.0}
      >>> # Make an intervention on the `BayesianNetwork`
      >>> ie.do_intervention('rush_hour', False)
      >>> # Query marginals on the intervened `BayesianNetwork`
      >>> ie.query()['traffic']
      {'heavy': 0.5, 'light': 0.5}
      >>> # Reset interventions
      >>> ie.reset_do('rush_hour')
      >>> ie.query()['traffic']
      {'heavy': 0.7142857142857142, 'light': 0.2857142857142857}
class_docstr_tok: ["An","``","InferenceEngine","``","provides","methods","to","query","marginals","based","on","observations","and","make","interventions","(","Do-Calculus",")","on","a","``","BayesianNetwork","``",".","Example",":",":",":",">",">",">","#","Create","a","Bayesian","Network","with","a","manually","defined","DAG",">",">",">","from","causalnex.structure.structuremodel","import","StructureModel",">",">",">","from","causalnex.network","import","BayesianNetwork",">",">",">","from","causalnex.inference","import","InferenceEngine",">",">",">",">",">",">","sm","=","StructureModel","(",")",">",">",">","sm.add_edges_from","(","[",">",">",">","(","'rush_hour","'",",","'traffic","'",")",",",">",">",">","(","'weather","'",",","'traffic","'",")",">",">",">","]",")",">",">",">","data","=","pd.DataFrame","(","{",">",">",">","'rush_hour","'",":","[","True",",","False",",","False",",","False",",","True",",","False",",","True","]",",",">",">",">","'weather","'",":","[","'Terrible","'",",","'Good","'",",","'Bad","'",",","'Good","'",",","'Bad","'",",","'Bad","'",",","'Good","'","]",",",">",">",">","'traffic","'",":","[","'heavy","'",",","'light","'",",","'heavy","'",",","'light","'",",","'heavy","'",",","'heavy","'",",","'heavy","'","]",">",">",">","}",")",">",">",">","bn","=","BayesianNetwork","(","sm",")",">",">",">","#","Inference","can","only","be","performed","on","the","`","BayesianNetwork","`","with","learned","nodes","states","and","CPDs",">",">",">","bn","=","bn.fit_node_states_and_cpds","(","data",")",">",">",">",">",">",">","#","Create","an","`","InferenceEngine","`","to","query","marginals","and","make","interventions",">",">",">","ie","=","InferenceEngine","(","bn",")",">",">",">","#","Query","the","marginals","as","learned","from","data",">",">",">","ie.query","(",")","[","'traffic","'","]","{","'heavy","'",":","0.7142857142857142",",","'light","'",":","0.2857142857142857","}",">",">",">","#","Query","the","marginals","given","observations",">",">",">","ie.query","(","{","'rush_hour","'",":","True",",","'weather","'",":","'Terrible","'","}",")","[","'traffic","'","]","{","'heavy","'",":","1.0",",","'light","'",":","0.0","}",">",">",">","#","Make","an","intervention","on","the","`","BayesianNetwork","`",">",">",">","ie.do_intervention","(","'rush_hour","'",",","False",")",">",">",">","#","Query","marginals","on","the","intervened","`","BayesianNetwork","`",">",">",">","ie.query","(",")","[","'traffic","'","]","{","'heavy","'",":","0.5",",","'light","'",":","0.5","}",">",">",">","#","Reset","interventions",">",">",">","ie.reset_do","(","'rush_hour","'",")",">",">",">","ie.query","(",")","[","'traffic","'","]","{","'heavy","'",":","0.7142857142857142",",","'light","'",":","0.2857142857142857","}"]
docstr: Creates a new ``InferenceEngine`` from an existing ``BayesianNetwork``.
  
  It is expected that structure and probability distribution has already been learned
  for the ``BayesianNetwork`` that is to be used for inference.
  This Bayesian Network cannot contain any isolated nodes.
  
  Args:
      bn: Bayesian Network that inference will act on.
  
  Raises:
      ValueError: if the Bayesian Network contains isolates, or if a variable name is invalid,
                  or if the CPDs have not been learned yet.
docstr_tok: ["Creates","a","new","``","InferenceEngine","``","from","an","existing","``","BayesianNetwork","``",".","It","is","expected","that","structure","and","probability","distribution","has","already","been","learned","for","the","``","BayesianNetwork","``","that","is","to","be","used","for","inference",".","This","Bayesian","Network","can","not","contain","any","isolated","nodes",".","Args",":","bn",":","Bayesian","Network","that","inference","will","act","on",".","Raises",":","ValueError",":","if","the","Bayesian","Network","contains","isolates",",","or","if","a","variable","name","is","invalid",",","or","if","the","CPDs","have","not","been","learned","yet","."]
code: def __init__(self, bn: BayesianNetwork):
      """
      Creates a new ``InferenceEngine`` from an existing ``BayesianNetwork``.
  
      It is expected that structure and probability distribution has already been learned
      for the ``BayesianNetwork`` that is to be used for inference.
      This Bayesian Network cannot contain any isolated nodes.
  
      Args:
          bn: Bayesian Network that inference will act on.
  
      Raises:
          ValueError: if the Bayesian Network contains isolates, or if a variable name is invalid,
                      or if the CPDs have not been learned yet.
      """
      bad_nodes = [
          node
          for node in bn.nodes
          if not re.match("^[0-9a-zA-Z_]+$", node)
      ]
  
      if bad_nodes:
          raise ValueError(
              "Variable names must match ^[0-9a-zA-Z_]+$ - please fix the "
              f"following nodes: {bad_nodes}"
          )
  
      if not bn.cpds:
          raise ValueError(
              "Bayesian Network does not contain any CPDs. You should fit CPDs "
              "before doing inference (see `BayesianNetwork.fit_cpds`)."
          )
  
      self._cpds = None
      self._detached_cpds = {}
      self._baseline_marginals = None
  
      self._create_cpds_dict_bn(bn)
      self._generate_domains_bn(bn)
      self._generate_bbn()
  
imports: import copy
  import inspect
  import math
  import re
  import types
  from typing import Any, Callable, Dict, Hashable, List, Optional, Tuple, Union
  import networkx
  import pandas
  from pathos import multiprocessing
  from causalnex.ebaybbn import build_bbn
  from causalnex.network import BayesianNetwork
label_desc: Use image node_id 1 to create a new InferenceEngine object  with example: obj = InferenceEngine(bn)