class_docstr: Class for representing factor graph.
  
  DiscreteFactor graph is a bipartite graph representing factorization of a function.
  They allow efficient computation of marginal distributions through sum-product
  algorithm.
  
  A factor graph contains two types of nodes. One type corresponds to random
  variables whereas the second type corresponds to factors over these variables.
  The graph only contains edges between variables and factor nodes. Each factor
  node is associated with one factor whose scope is the set of variables that
  are its neighbors.
  
  Parameters
  ----------
  data: input graph
      Data to initialize graph. If data=None (default) an empty graph is
      created. The data is an edge list.
  
  Examples
  --------
  Create an empty FactorGraph with no nodes and no edges
  
  >>> from pgmpy.models import FactorGraph
  >>> G = FactorGraph()
  
  G can be grown by adding variable nodes as well as factor nodes
  
  **Nodes:**
  
  Add a node at a time or a list of nodes.
  
  >>> G.add_node('a')
  >>> G.add_nodes_from(['a', 'b'])
  >>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
  >>> G.add_factors(phi1)
  >>> G.add_nodes_from([phi1])
  
  **Edges:**
  
  G can also be grown by adding edges.
  
  >>> G.add_edge('a', phi1)
  
  or a list of edges
  
  >>> G.add_edges_from([('a', phi1), ('b', phi1)])
docstr: Returns the cardinality of the node
  
  Parameters
  ----------
  node: any hashable python object (optional)
      The node whose cardinality we want. If node is not specified returns a
      dictionary with the given variable as keys and their respective cardinality
      as values.
  
  Returns
  -------
  int or dict : If node is specified returns the cardinality of the node.
                If node is not specified returns a dictionary with the given
                variable as keys and their respective cardinality as values.
  
  Examples
  --------
  >>> from pgmpy.models import FactorGraph
  >>> from pgmpy.factors.discrete import DiscreteFactor
  >>> G = FactorGraph()
  >>> G.add_nodes_from(['a', 'b', 'c'])
  >>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
  >>> phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
  >>> G.add_nodes_from([phi1, phi2])
  >>> G.add_edges_from([('a', phi1), ('b', phi1),
  ...                   ('b', phi2), ('c', phi2)])
  >>> G.add_factors(phi1, phi2)
  >>> G.get_cardinality()
  defaultdict(<class 'int'>, {'c': 2, 'b': 2, 'a': 2})
  
  >>> G.get_cardinality('a')
  2
docstr_tok: ["Returns","the","cardinality","of","the","node","Parameters","--","--","--","--","--","node",":","any","hashable","python","object","(","optional",")","The","node","whose","cardinality","we","want",".","If","node","is","not","specified","returns","a","dictionary","with","the","given","variable","as","keys","and","their","respective","cardinality","as","values",".","Returns","--","--","--","-","int","or","dict",":","If","node","is","specified","returns","the","cardinality","of","the","node",".","If","node","is","not","specified","returns","a","dictionary","with","the","given","variable","as","keys","and","their","respective","cardinality","as","values",".","Examples","--","--","--","--",">",">",">","from","pgmpy.models","import","FactorGraph",">",">",">","from","pgmpy.factors.discrete","import","DiscreteFactor",">",">",">","G","=","FactorGraph","(",")",">",">",">","G.add_nodes_from","(","[","'","a","'",",","'","b","'",",","'","c","'","]",")",">",">",">","phi1","=","DiscreteFactor","(","[","'","a","'",",","'","b","'","]",",","[","2",",","2","]",",","np.random.rand","(","4",")",")",">",">",">","phi2","=","DiscreteFactor","(","[","'","b","'",",","'","c","'","]",",","[","2",",","2","]",",","np.random.rand","(","4",")",")",">",">",">","G.add_nodes_from","(","[","phi1",",","phi2","]",")",">",">",">","G.add_edges_from","(","[","(","'","a","'",",","phi1",")",",","(","'","b","'",",","phi1",")",",","...","(","'","b","'",",","phi2",")",",","(","'","c","'",",","phi2",")","]",")",">",">",">","G.add_factors","(","phi1",",","phi2",")",">",">",">","G.get_cardinality","(",")","defaultdict","(","<","class","'int","'",">",",","{","'","c","'",":","2",",","'","b","'",":","2",",","'","a","'",":","2","}",")",">",">",">","G.get_cardinality","(","'","a","'",")","2"]
code: def get_cardinality(self, node=None):
      """
      Returns the cardinality of the node
  
      Parameters
      ----------
      node: any hashable python object (optional)
          The node whose cardinality we want. If node is not specified returns a
          dictionary with the given variable as keys and their respective cardinality
          as values.
  
      Returns
      -------
      int or dict : If node is specified returns the cardinality of the node.
                    If node is not specified returns a dictionary with the given
                    variable as keys and their respective cardinality as values.
  
      Examples
      --------
      >>> from pgmpy.models import FactorGraph
      >>> from pgmpy.factors.discrete import DiscreteFactor
      >>> G = FactorGraph()
      >>> G.add_nodes_from(['a', 'b', 'c'])
      >>> phi1 = DiscreteFactor(['a', 'b'], [2, 2], np.random.rand(4))
      >>> phi2 = DiscreteFactor(['b', 'c'], [2, 2], np.random.rand(4))
      >>> G.add_nodes_from([phi1, phi2])
      >>> G.add_edges_from([('a', phi1), ('b', phi1),
      ...                   ('b', phi2), ('c', phi2)])
      >>> G.add_factors(phi1, phi2)
      >>> G.get_cardinality()
      defaultdict(<class 'int'>, {'c': 2, 'b': 2, 'a': 2})
  
      >>> G.get_cardinality('a')
      2
      """
      if node:
          for factor in self.factors:
              for variable, cardinality in zip(
                  factor.scope(), factor.cardinality
              ):
                  if node == variable:
                      return cardinality
      else:
          cardinalities = defaultdict(int)
          for factor in self.factors:
              for variable, cardinality in zip(
                  factor.scope(), factor.cardinality
              ):
                  cardinalities[variable] = cardinality
          return cardinalities
  
imports: import itertools
  from collections import defaultdict
  import numpy
  from networkx.algorithms import bipartite
  from pgmpy.models.MarkovNetwork import MarkovNetwork
  from pgmpy.base import UndirectedGraph
  from pgmpy.factors.discrete import DiscreteFactor
  from pgmpy.factors import factor_product
label_desc: Use image node_id 5 for calling the FactorGraph obj's underlying member method code with example usage: obj.get_cardinality(node) and returns: cardinalities, cardinality