Coloring and exporting graphical models as image (pdf, png)

Creative Commons License

aGrUM

interactive online version

In [1]:
from pylab import *
import matplotlib.pyplot as plt

In [2]:
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb

bn=gum.fastBN("a->b->c->d;b->e->d->f;g->c")
gnb.flow.row(bn,gnb.getInference(bn))
G f f b b c c b->c e e b->e d d c->d g g g->c e->d a a a->b d->f
structs Inference in   1.00ms a 2022-06-05T19:26:55.937218 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ b 2022-06-05T19:26:56.082220 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ a->b c 2022-06-05T19:26:56.166216 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ b->c e 2022-06-05T19:26:56.276219 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ b->e d 2022-06-05T19:26:56.221213 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ c->d f 2022-06-05T19:26:56.334216 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ d->f e->d g 2022-06-05T19:26:56.411214 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ g->c

customizing colours and width for model and inference

In [3]:
def nodevalue(n):
    return 0.5 if n in "aeiou" else 0.7
def arcvalue(a):
    return (10-a[0])*a[1]
def arcvalue2(a):
    return (a[0]+a[1]+5)/22
gnb.showBN(bn,
           nodeColor={n:nodevalue(n) for n in bn.names()},
           arcWidth={a:arcvalue(a) for a in bn.arcs()},
           arcColor={a:arcvalue2(a) for a in bn.arcs()})
../_images/notebooks_98-Tools_colouringAndExportingBNs_5_0.svg
In [4]:
gnb.showInference(bn,
           targets={"a","g","f","b"},
           evs={'e':0},
           nodeColor={n:nodevalue(n) for n in bn.names()},
           arcWidth={a:arcvalue(a) for a in bn.arcs()})
../_images/notebooks_98-Tools_colouringAndExportingBNs_6_0.svg
In [5]:
gnb.flow.row(gnb.getBN(bn,
                       nodeColor={n:nodevalue(n) for n in bn.names()},
                       arcWidth={a:arcvalue(a) for a in bn.arcs()}),
             gnb.getInference(bn,
                              nodeColor={n:nodevalue(n) for n in bn.names()},
                              arcWidth={a:arcvalue(a) for a in bn.arcs()})
            )
G f f b b c c b->c e e b->e d d c->d g g g->c e->d a a a->b d->f
structs Inference in   2.00ms a 2022-06-05T19:26:57.733441 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ b 2022-06-05T19:26:57.790444 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ a->b c 2022-06-05T19:26:57.846445 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ b->c e 2022-06-05T19:26:57.974448 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ b->e d 2022-06-05T19:26:57.919445 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ c->d f 2022-06-05T19:26:58.029443 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ d->f e->d g 2022-06-05T19:26:58.091725 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ g->c
In [6]:
import matplotlib.pyplot as plt
mycmap=plt.get_cmap('Reds')
formyarcs=plt.get_cmap('winter')
gnb.flow.row(gnb.getBN(bn,
                       nodeColor={n:nodevalue(n) for n in bn.names()},
                       arcColor={a:arcvalue2(a) for a in bn.arcs()},
                       cmap=mycmap,
                       cmapArc=formyarcs),
             gnb.getInference(bn,
                              nodeColor={n:nodevalue(n) for n in bn.names()},
                              arcColor={a:arcvalue2(a) for a in bn.arcs()},
                              arcWidth={a:arcvalue(a) for a in bn.arcs()},
                              cmap=mycmap,
                              cmapArc=formyarcs)
              )
G f f b b c c b->c e e b->e d d c->d g g g->c e->d a a a->b d->f
structs Inference in   1.00ms a 2022-06-05T19:26:58.594754 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ b 2022-06-05T19:26:58.665752 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ a->b c 2022-06-05T19:26:58.723752 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ b->c e 2022-06-05T19:26:58.865756 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ b->e d 2022-06-05T19:26:58.776754 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ c->d f 2022-06-05T19:26:58.919755 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ d->f e->d g 2022-06-05T19:26:59.017753 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ g->c

Exporting model and inference as image

Exporting as image (pdf, png, etc.) has been gathered in 2 functions : pyAgrum.lib.image.export() and pyAgrum.lib.image.exportInference(). The argument are the same as for pyAgrum.notebook.show{Model} and pyAgrum.notebook.show{Inference}.

In [7]:
import pyAgrum.lib.image as gumimage
from IPython.display import Image # to display the exported images
In [8]:
gumimage.export(bn,"out/test_export.png")

Image(filename='out/test_export.png')
Out[8]:
../_images/notebooks_98-Tools_colouringAndExportingBNs_11_0.png
In [9]:
bn = gum.fastBN("a->b->d;a->c->d[3]->e;f->b")
gumimage.export(bn,"out/test_export.png",
                nodeColor={'a': 1,
                           'b': 0.3,
                           'c': 0.4,
                           'd': 0.1,
                           'e': 0.2,
                           'f': 0.5},
                arcColor={(0, 1): 0.2,
                          (1, 2): 0.5},
                arcWidth={(0, 3): 0.4,
                          (3, 2): 0.5,
                          (2,4) :0.6})

Image(filename='out/test_export.png')
Out[9]:
../_images/notebooks_98-Tools_colouringAndExportingBNs_12_0.png
In [10]:
gumimage.exportInference(bn,"out/test_export.png")

Image(filename='out/test_export.png')
Out[10]:
../_images/notebooks_98-Tools_colouringAndExportingBNs_13_0.png
In [11]:
gumimage.export(bn,"out/test_export.pdf")

Link to out/test_export.pdf

exporting inference with evidence

In [12]:
bn=gum.loadBN("res/alarm.dsl")
gumimage.exportInference(bn,"out/test_export.pdf",
                         evs={"CO":1,"VENTLUNG":1},
                         targets={"VENTALV",
                                  "CATECHOL",
                                  "HR",
                                  "MINVOLSET",
                                  "ANAPHYLAXIS",
                                  "STROKEVOLUME",
                                  "ERRLOWOUTPUT",
                                  "HBR",
                                  "PULMEMBOLUS",
                                  "HISTORY",
                                  "BP",
                                  "PRESS",
                                  "CO"},
                         size="15!")

Link to out/test_export.pdf

Other models

Other models can also use these functions.

In [13]:
infdiag=gum.loadID("res/OilWildcatter.bifxml")
gumimage.export(infdiag,"out/test_export.pdf")

Link to out/test_export.pdf

In [14]:
gumimage.exportInference(infdiag,"out/test_export.pdf")

Link to out/test_export.pdf

Exporting any object with toDot() method

In [15]:
import pyAgrum.causal as csl
obs1 = gum.fastBN("Smoking->Cancer")
modele3 = csl.CausalModel(obs1, [("Genotype", ["Smoking","Cancer"])], True)
gumimage.export(modele3,"out/test_export.png") # a causal model has a toDot method.
Image(filename='out/test_export.png')
Out[15]:
../_images/notebooks_98-Tools_colouringAndExportingBNs_25_0.png
In [16]:
bn=gum.fastBN("a->b->c->d;b->e->d->f;g->c")
ie=gum.LazyPropagation(bn)
jt=ie.junctionTree()
gumimage.export(jt,"out/test_export.png") # a JunctionTree has a method jt.toDot()
Image(filename='out/test_export.png')
Out[16]:
../_images/notebooks_98-Tools_colouringAndExportingBNs_26_0.png

… or even a string in dot syntax

In [17]:
gumimage.export(jt.toDotWithNames(bn),"out/test_export.png") # jt.toDotWithNames(bn) creates a dot-string for a junction tree with names of variables
Image(filename='out/test_export.png')
Out[17]:
../_images/notebooks_98-Tools_colouringAndExportingBNs_28_0.png

Exporting to pyplot

In [18]:
import matplotlib.pyplot as plt

bn=gum.fastBN("A->B->C<-D")

plt.imshow(gumimage.export(bn))
plt.show()

plt.imshow(gumimage.exportInference(bn,size="15!"))
plt.show()

plt.figure(figsize = (10,10))
plt.imshow(gumimage.exportInference(bn,size="15!"))
plt.show()
../_images/notebooks_98-Tools_colouringAndExportingBNs_30_0.svg
../_images/notebooks_98-Tools_colouringAndExportingBNs_30_1.svg
../_images/notebooks_98-Tools_colouringAndExportingBNs_30_2.svg
In [ ]: