Skip to content

interactive notebooks

Creative Commons LicenseaGrUMinteractive online version

pyAgrum can easily interact with other applications. In this notebook, we propose for example some application tracks with notebook ipywidgets to make the exploration of graphical models and their inferences more interactive.

import pyagrum as gum
import pyagrum.lib.notebook as gnb
import glob
import os.path
from tqdm.auto import tqdm
class TqdmProgressBarLoadListener:
def __init__(self, filename: str):
self.pbar = tqdm(total=100, desc=filename, bar_format="{desc}: {percentage:3.0f}%|{bar}|")
def update(self, progress):
if progress == 200:
self.pbar.close()
else:
self.pbar.update()
self.pbar.display()
bns = {}
for name in ["res/alarm.dsl", "res/asia.bgum", "res/Diabetes.bgum"]:
progbar = TqdmProgressBarLoadListener(name)
bns[os.path.basename(name)] = gum.loadBN(name, listeners=[lambda progress: progbar.update(progress)])
res/alarm.dsl: 0%| |
res/asia.bgum: 0%| |
res/Diabetes.bgum: 0%| |

Which should give you something like

progess bars

ipywidget can be used with different types of objects. Let’s say that you have a class that show the arcs of a Bayesian network only the mutual information of this arc is above a certain threshold:

import pydot as dot
class InformationViewer:
def __init__(self, bn: gum.BayesNet):
self.bn = bn
ie = gum.LazyPropagation(bn)
self._min = float("inf")
self._max = float("-inf")
self._arcs = {}
for x, y in bn.arcs():
nameX = bn.variable(x).name()
nameY = bn.variable(y).name()
ie.addJointTarget({nameX, nameY})
info = gum.InformationTheory(ie, [nameX], [nameY])
m = info.mutualInformationXY()
if self._min > m:
self._min = m
if self._max < m:
self._max = m
self._arcs[x, y] = m
def min(self):
return self._min
def max(self):
return self._max
def showBN(self, minVal: float = 0):
graph = dot.Dot(graph_type="digraph", bgcolor="transparent")
bgcol = gum.config["notebook", "default_node_bgcolor"]
fgcol = gum.config["notebook", "default_node_fgcolor"]
for n in self.bn.names():
graph.add_node(dot.Node('"' + n + '"', style="filled", fillcolor=bgcol, fontcolor=fgcol))
for x, y in self.bn.arcs():
graph.add_edge(
dot.Edge(
'"' + self.bn.variable(x).name() + '"',
'"' + self.bn.variable(y).name() + '"',
style="invis" if self._arcs[x, y] < minVal else "",
)
)
size = gum.config["notebook", "default_graph_size"]
graph.set_size(size)
return graph
view = InformationViewer(bns["alarm.dsl"])
print(f"min={view.min()} ,max={view.max()}")
gnb.sideBySide(view.showBN(0.3), view.showBN(0.5), captions=["BN filtered by $MI>0.3$", "BN filtered by $MI>0.5$"])
min=7.940532588369706e-06 ,max=0.8850119269966232
G HREKG HREKG PAP PAP PCWP PCWP VENTLUNG VENTLUNG MINVOL MINVOL EXPCO2 EXPCO2 VENTLUNG->EXPCO2 VENTALV VENTALV VENTLUNG->VENTALV BP BP FIO2 FIO2 PVSAT PVSAT VENTTUBE VENTTUBE PRESS PRESS HYPOVOLEMIA HYPOVOLEMIA LVEDVOLUME LVEDVOLUME HYPOVOLEMIA->LVEDVOLUME STROKEVOLUME STROKEVOLUME HYPOVOLEMIA->STROKEVOLUME HRBP HRBP TPR TPR CATECHOL CATECHOL HRSAT HRSAT INTUBATION INTUBATION SHUNT SHUNT DISCONNECT DISCONNECT HR HR MINVOLSET MINVOLSET VENTMACH VENTMACH MINVOLSET->VENTMACH SAO2 SAO2 ARTCO2 ARTCO2 VENTALV->ARTCO2 VENTALV->PVSAT ERRCAUTER ERRCAUTER ERRCAUTER->HRSAT PULMEMBOLUS PULMEMBOLUS ANAPHYLAXIS ANAPHYLAXIS HR->HRBP HR->HRSAT CO CO LVEDVOLUME->PCWP CVP CVP LVEDVOLUME->CVP ERRLOWOUTPUT ERRLOWOUTPUT CO->BP HISTORY HISTORY KINKEDTUBE KINKEDTUBE LVFAILURE LVFAILURE VENTMACH->VENTTUBE INSUFFANESTH INSUFFANESTH STROKEVOLUME->CO
BN filtered by $MI>0.3$
G HREKG HREKG PAP PAP PCWP PCWP VENTLUNG VENTLUNG MINVOL MINVOL EXPCO2 EXPCO2 VENTLUNG->EXPCO2 VENTALV VENTALV VENTLUNG->VENTALV BP BP FIO2 FIO2 PVSAT PVSAT VENTTUBE VENTTUBE PRESS PRESS HYPOVOLEMIA HYPOVOLEMIA LVEDVOLUME LVEDVOLUME STROKEVOLUME STROKEVOLUME HRBP HRBP TPR TPR CATECHOL CATECHOL HRSAT HRSAT INTUBATION INTUBATION SHUNT SHUNT DISCONNECT DISCONNECT HR HR MINVOLSET MINVOLSET VENTMACH VENTMACH SAO2 SAO2 ARTCO2 ARTCO2 ERRCAUTER ERRCAUTER PULMEMBOLUS PULMEMBOLUS ANAPHYLAXIS ANAPHYLAXIS CO CO LVEDVOLUME->PCWP CVP CVP LVEDVOLUME->CVP ERRLOWOUTPUT ERRLOWOUTPUT HISTORY HISTORY KINKEDTUBE KINKEDTUBE LVFAILURE LVFAILURE VENTMACH->VENTTUBE INSUFFANESTH INSUFFANESTH STROKEVOLUME->CO
BN filtered by $MI>0.5$

Now we can use this class for animation :

import ipywidgets as widgets
def interactive_view(threshold: float):
return view.showBN(threshold)
widgets.interact(interactive_view, threshold=(view.min(), view.max(), (view.max() - view.min()) / 100.0));
interactive(children=(FloatSlider(value=0.4425099337646058, description='threshold', max=0.8850119269966232, m…

Which should give you something like

informationVisualisation

from ipywidgets import interact, fixed
bn = bns["asia.bgum"]
asia = list(bn["visit_to_Asia"].labels())
smoking = list(bn["smoking"].labels())
XraY = list(bn["positive_XraY"].labels())
cig_ped_day = gum.RangeVariable("cigarettes_per_day", "cigarettes_per_day in [0, 10]?", 0, 10)
bn.add(cig_ped_day)
@interact(
bn=fixed(bn),
visit_to_Asia=asia,
smoking=smoking,
positive_XraY=XraY,
smoked_cigarettes=(cig_ped_day.minVal(), cig_ped_day.maxVal(), 1),
)
def evidence_impact(bn, visit_to_Asia, smoking, positive_XraY, smoked_cigarettes):
evs = {
"visit_to_Asia": visit_to_Asia,
"smoking": smoking,
"positive_XraY": positive_XraY,
"cigarettes_per_day": smoked_cigarettes,
}
gnb.showInference(bn, evs=evs)
interactive(children=(Dropdown(description='visit_to_Asia', options=('0', '1'), value='0'), Dropdown(descripti…