Jupyter notebook with Graphical Models

pyAgrum.lib.notebook aims to facilitate the use of pyAgrum with jupyter notebook (or lab).

_images/pyAgrum_all.png

Visualization of graphical models

Important

For many graphical representations functions, the parameter size is directly transferred to graphviz. Hence, Its format is a string containing an int. However if size ends in an exclamation point “!” (such as size=”4!”), then size is taken to be the desired minimum size. In this case, if both dimensions of the drawing are less than size, the drawing is scaled up uniformly until at least one dimension equals its dimension in size.

_images/effect_of_Graphviz_size.png
pyAgrum.lib.notebook.showBN(bn, size=None, nodeColor=None, arcWidth=None, arcLabel=None, arcColor=None, cmapNode=None, cmapArc=None)

show a Bayesian network

Parameters:
  • bn (pyAgrum.BayesNet) – the Bayesian network

  • size (str) – size (for graphviz) of the rendered graph

  • nodeColor (dict[Tuple(int,int),float]) – a nodeMap of values to be shown as color nodes (with special color for 0 and 1)

  • arcWidth (dict[Tuple(int,int),float]) – an arcMap of values to be shown as bold arcs

  • arcLabel (dict[Tuple(int,int),str]) – an arcMap of labels to be shown next to arcs

  • arcColor (dict[Tuple(int,int),float]) – an arcMap of values (between 0 and 1) to be shown as color of arcs

  • cmapNode (ColorMap) – color map to show the vals of Nodes ( (if cmapEdge is None, this color map is used also for edges)

  • cmapArc (ColorMap) – color map to show the vals of Arcs

  • showMsg (dict) – a nodeMap of values to be shown as tooltip

pyAgrum.lib.notebook.getBN(bn, size=None, nodeColor=None, arcWidth=None, arcLabel=None, arcColor=None, cmapNode=None, cmapArc=None)

get a HTML string for a Bayesian network

Parameters:
  • bn (pyAgrum.BayesNet) – the Bayesian network

  • size (str) – size (for graphviz) of the rendered graph

  • nodeColor (dict[Tuple(int,int),float]) – a nodeMap of values to be shown as color nodes (with special color for 0 and 1)

  • arcWidth (dict[Tuple(int,int),float]) – an arcMap of values to be shown as bold arcs

  • arcLabel (dict[Tuple(int,int),str]) – an arcMap of labels to be shown next to arcs

  • arcColor (dict[Tuple(int,int),float]) – an arcMap of values (between 0 and 1) to be shown as color of arcs

  • cmapNode (ColorMap) – color map to show the vals of Nodes

  • cmapArc (ColorMap) – color map to show the vals of Arcs

  • showMsg (dict) – a nodeMap of values to be shown as tooltip

Returns:

the desired representation of the Bayesian network

Return type:

pydot.Dot

pyAgrum.lib.notebook.showInfluenceDiagram(diag, size=None)

show an influence diagram as a graph

Parameters:
  • diag ("pyAgrum.InfluenceDiagram") – the influence diagram

  • size (str) – size (for graphviz) of the rendered graph

Returns:

the representation of the influence diagram

pyAgrum.lib.notebook.getInfluenceDiagram(diag, size=None)

get a HTML string for an influence diagram as a graph

Parameters:
  • diag ("pyAgrum.InfluenceDiagram") – the influence diagram

  • size (str) – size (for graphviz) of the rendered graph

Returns:

the HTML representation of the influence diagram

Return type:

str

pyAgrum.lib.notebook.showMN(*args, **kwargs)
pyAgrum.lib.notebook.getMN(*args, **kwargs)
pyAgrum.lib.notebook.showCN(cn, size=None, nodeColor=None, arcWidth=None, arcLabel=None, arcColor=None, cmapNode=None, cmapArc=None)

show a credal network

Parameters:
  • cn (pyAgrum.CredalNet) – the Credal network

  • size (str) – size (for graphviz) of the rendered graph

  • nodeColor (dict[int,float]) – a nodeMap of values to be shown as color nodes (with special color for 0 and 1)

  • arcWidth (dict[Tuple(int,int),float]) – an arcMap of values to be shown as bold arcs

  • arcLabel (dict[Tuple(int,int),float]) – an arcMap of labels to be shown next to arcs

  • arcColor (dict[Tuple(int,int),float]) – an arcMap of values (between 0 and 1) to be shown as color of arcs

  • cmapNode (matplotlib.color.colormap) – color map to show the vals of Nodes

  • cmapArc (matplotlib.color.colormap) – color map to show the vals of Arcs

  • showMsg (dict[int,str]) – a nodeMap of values to be shown as tooltip

Returns:

the graph

pyAgrum.lib.notebook.getCN(cn, size=None, nodeColor=None, arcWidth=None, arcLabel=None, arcColor=None, cmapNode=None, cmapArc=None)

get a HTML string for a credal network

Parameters:
  • cn (pyAgrum.CredalNet) – the Credal network

  • size (str) – size (for graphviz) of the rendered graph

  • nodeColor (dict[int,float]) – a nodeMap of values to be shown as color nodes (with special color for 0 and 1)

  • arcWidth (dict[Tuple(int,int),float]) – an arcMap of values to be shown as bold arcs

  • arcLabel (dict[Tuple(int,int),float]) – an arcMap of labels to be shown next to arcs

  • arcColor (dict[Tuple(int,int),float]) – an arcMap of values (between 0 and 1) to be shown as color of arcs

  • cmapNode (matplotlib.color.colormap) – color map to show the vals of Nodes

  • cmapArc (matplotlib.color.colormap) – color map to show the vals of Arcs

  • showMsg (dict[int,str]) – a nodeMap of values to be shown as tooltip

Returns:

the desired representation of the Credal Network

Return type:

pydot.Dot

pyAgrum.lib.notebook.showInference(model, **kwargs)

show pydot graph for an inference in a notebook

Parameters:
  • model (pyAgrum.GraphicalModel) – the model in which to infer (pyAgrum.BayesNet, pyAgrum.MarkovRandomField or pyAgrum.InfluenceDiagram)

  • engine (gum.Inference) – inference algorithm used. If None, gum.LazyPropagation will be used for BayesNet, gum.ShaferShenoy for gum.MarkovRandomField and gum.ShaferShenoyLIMIDInference for gum.InfluenceDiagram.

  • evs (Dict[int|str,int|str|List[float]]) – map of evidence

  • targets (Set[str]) – set of targets

  • size (string) – size (for graphviz) of the rendered graph

  • nodeColor (Dict[str,float]) – a nodeMap of values (between 0 and 1) to be shown as color of nodes (with special colors for 0 and 1)

  • factorColor (Dict[int,float]) – a nodeMap of values (between 0 and 1) to be shown as color of factors (in MarkovRandomField representation)

  • arcWidth (: Dict[(str,str),float]) – an arcMap of values to be shown as width of arcs

  • arcColor (: Dict[(str,str),float]) – a arcMap of values (between 0 and 1) to be shown as color of arcs

  • cmapNode (matplotlib.ColorMap) – map to show the color of nodes and arcs

  • cmapArc (matplotlib.ColorMap) – color map to show the vals of Arcs.

  • graph (pyAgrum.Graph) – only shows nodes that have their id in the graph (and not in the whole BN)

  • view (str) – graph | factorgraph | None (default) for Markov random field

Returns:

the desired representation of the inference

pyAgrum.lib.notebook.getInference(model, **kwargs)

get a HTML string for an inference in a notebook

Parameters:
  • model (pyAgrum.GraphicalModel) – the model in which to infer (pyAgrum.BayesNet, pyAgrum.MarkovRandomField or pyAgrum.InfluenceDiagram)

  • engine (gum.Inference) – inference algorithm used. If None, gum.LazyPropagation will be used for BayesNet, gum.ShaferShenoy for gum.MarkovRandomField and gum.ShaferShenoyLIMIDInference for gum.InfluenceDiagram.

  • evs (Dict[int|str,int|str|List[float]]) – map of evidence

  • targets (Set[str]) – set of targets

  • size (string) – size (for graphviz) of the rendered graph

  • nodeColor (Dict[str,float]) – a nodeMap of values (between 0 and 1) to be shown as color of nodes (with special colors for 0 and 1)

  • factorColor (Dict[int,float]) – a nodeMap of values (between 0 and 1) to be shown as color of factors (in MarkovRandomField representation)

  • arcWidth (: Dict[(str,str),float]) – an arcMap of values to be shown as width of arcs

  • arcColor (: Dict[(str,str),float]) – a arcMap of values (between 0 and 1) to be shown as color of arcs

  • cmapNode (matplotlib.ColorMap) – map to show the color of nodes and arcs

  • cmapArc (matplotlib.ColorMap) – color map to show the vals of Arcs.

  • graph (pyAgrum.Graph) – only shows nodes that have their id in the graph (and not in the whole BN)

  • view (str) – graph | factorgraph | None (default) for Markov random field

Returns:

the desired representation of the inference

pyAgrum.lib.notebook.showJunctionTree(bn, withNames=True, size=None)

Show a junction tree of a Bayesian network

Parameters:
  • bn (pyAgrum.BayesNet) – the model

  • withNames (bool) – names or id in the graph (names can created very large nodes)

  • size (float|str) – size (for graphviz) of the rendered graph

pyAgrum.lib.notebook.getJunctionTree(bn, withNames=True, size=None)

get a HTML string for a junction tree (more specifically a join tree)

Parameters:
  • bn ("pyAgrum.BayesNet") – the Bayesian network

  • withNames (Boolean) – display the variable names or the node id in the clique

  • size (str) – size (for graphviz) of the rendered graph

Returns:

str

the HTML representation of the graph

Visualization of Potentials

pyAgrum.lib.notebook.showProba(p, scale=None)

Show a mono-dim Potential (a marginal)

Parameters:
pyAgrum.lib.notebook.getPosterior(bn, evs, target)

shortcut for proba2histo(gum.getPosterior(bn,evs,target))

Parameters:
  • bn ("pyAgrum.BayesNet") – the BayesNet

  • evs (Dict[str|int:int|str|List[float]]) – map of evidence

  • target (str|int) – name of target variable

Returns:

the matplotlib graph

pyAgrum.lib.notebook.showPosterior(bn, evs, target)

shortcut for showProba(gum.getPosterior(bn,evs,target))

Parameters:
  • bn ("pyAgrum.BayesNet") – the BayesNet

  • evs (Dict[str|int:int|str|List[float]]) – map of evidence

  • target (str|int) – name of target variable

pyAgrum.lib.notebook.getPotential(pot, digits=None, withColors=None, varnames=None)

return a HTML string of a gum.Potential as a HTML table. The first dimension is special (horizontal) due to the representation of conditional probability table

Parameters:
  • pot (gum.Potential) – the potential to show

  • digits (int) – number of digits to show

  • withColors (bool) – background for proba cells or not

  • varnames (List[str]) – the aliases for variables name in the table

Returns:

the html representation of the Potential (as a string)

Return type:

str

pyAgrum.lib.notebook.showPotential(pot, digits=None, withColors=None, varnames=None)

show a gum.Potential as a HTML table. The first dimension is special (horizontal) due to the representation of conditional probability table

Parameters:
  • pot (gum.Potential) – the potential to show

  • digits (int) – number of digits to show

  • withColors (bool) – background color for proba cells or not

  • varnames (List[str]) – the aliases for variables name in the table

Visualization of graphs

pyAgrum.lib.notebook.getDot(dotstring, size=None)

get an HTML representation of a dot string

Parameters:
  • dotstring (str) – the dot string

  • size (float|str) – size (for graphviz) of the rendered graph

Return type:

str

Returns:

the HTML representation of the dot string

pyAgrum.lib.notebook.showDot(dotstring, size=None)

show a dot string as a graph

Parameters:
  • dotstring (str) – the dot string

  • size (float|str) – size (for graphviz) of the rendered graph

pyAgrum.lib.notebook.getGraph(gr, size=None)

get an HTML representation of a pydot graph

Parameters:
  • gr (pydot.Dot) – the graph

  • size (float|str) – the size (for graphviz) of the rendered graph

Returns:

the HTML representation of the graph (as a string)

Return type:

str

pyAgrum.lib.notebook.showGraph(gr, size=None)

show a pydot graph in a notebook

Parameters:
  • gr (pydot.Dot) – the graph

  • size (float|str) – the size (for graphviz) of the rendered graph

Visualization of approximation algorithm

pyAgrum.lib.notebook.animApproximationScheme(apsc, scale=<ufunc 'log10'>)

show an animated version of an approximation algorithm

Parameters:
  • apsc – the approximation algorithm

  • scale – a function to apply to the figure

Helpers

pyAgrum.lib.notebook.configuration()

Display the collection of dependance and versions

pyAgrum.lib.notebook.sideBySide(*args, **kwargs)

display side by side args as HMTL fragment (using string, _repr_html_() or str())

Parameters:
  • args (str) – HMTL fragments as string arg, arg._repr_html_() or str(arg)

  • captions (List[str], optional) – list of captions

  • valign (str) – vertical position in the row (top|middle|bottom, middle by default)

  • ncols (int) – number of columns (infinite by default)