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: Makes an intervention on the Bayesian Network.
  
  For instance,
      `do_intervention('X', 'x')` will set :math:`P(X=x)` to 1, and :math:`P(X=y)` to 0
      `do_intervention('X', {'x': 0.2, 'y': 0.8})` will set :math:`P(X=x)` to 0.2, and :math:`P(X=y)` to 0.8
  
  Args:
      node: the node that the intervention acts upon.
      state: state to update node it.
          - if Hashable: the intervention updates the state to 1, and all other states to 0;
          - if Dict[Hashable, float]: update states to all state -> probabilitiy in the dict.
  
  Raises:
      ValueError: if performing intervention would create an isolated node.
docstr_tok: ["Makes","an","intervention","on","the","Bayesian","Network",".","For","instance",",","`","do_intervention","(","'","X","'",",","'","x","'",")","`","will","set",":","math",":","`","P","(","X=x",")","`","to","1",",","and",":","math",":","`","P","(","X=y",")","`","to","0","`","do_intervention","(","'","X","'",",","{","'","x","'",":","0.2",",","'","y","'",":","0.8","}",")","`","will","set",":","math",":","`","P","(","X=x",")","`","to","0.2",",","and",":","math",":","`","P","(","X=y",")","`","to","0.8","Args",":","node",":","the","node","that","the","intervention","acts","upon",".","state",":","state","to","update","node","it",".","-","if","Hashable",":","the","intervention","updates","the","state","to","1",",","and","all","other","states","to","0",";","-","if","Dict","[","Hashable",",","float","]",":","update","states","to","all","state","-",">","probabilitiy","in","the","dict",".","Raises",":","ValueError",":","if","performing","intervention","would","create","an","isolated","node","."]
code: def do_intervention(
      self,
      node: str,
      state: Optional[Union[Hashable, Dict[Hashable, float]]] = None,
  ):
      """
      Makes an intervention on the Bayesian Network.
  
      For instance,
          `do_intervention('X', 'x')` will set :math:`P(X=x)` to 1, and :math:`P(X=y)` to 0
          `do_intervention('X', {'x': 0.2, 'y': 0.8})` will set :math:`P(X=x)` to 0.2, and :math:`P(X=y)` to 0.8
  
      Args:
          node: the node that the intervention acts upon.
          state: state to update node it.
              - if Hashable: the intervention updates the state to 1, and all other states to 0;
              - if Dict[Hashable, float]: update states to all state -> probabilitiy in the dict.
  
      Raises:
          ValueError: if performing intervention would create an isolated node.
      """
      if not any(
          node in inspect.getargs(f.__code__)[0][1:]
          for _, f in self._node_functions.items()
      ):
          raise ValueError(
              "Do calculus cannot be applied because it would result in an isolate"
          )
  
      # initialise baseline marginals if not done previously
      if self._baseline_marginals is None:
          self._baseline_marginals = self._single_query(None)
  
      if isinstance(state, int):
          state = {s: float(s == state) for s in self._cpds[node]}
  
      self._do(node, state)
  
      # check for presence of separate subgraph after do-intervention
      self._remove_disconnected_nodes(node)
      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 5 for calling the InferenceEngine obj's underlying member method code with example usage: obj.do_intervention(node, state) without return types