Click here to hide/show the list of notebooks.
  pyAgrum on notebooks   pyAgrum jupyter
☰  configForPyAgrum 
pyAgrum 0.16.3   
Zipped notebooks   
generation: 2019-10-20 09:16  

gum.config is the configuration object for pyAgrum

In [1]:
import pyAgrum as gum
import pyAgrum.lib.notebook as gnb
print("="*35)
print(gum.config)
print("="*35)
===================================
[notebook]
  potential_visible_digits = 4
  horizontal_histogram_visible_digits = 2
  vertical_histogram_visible_digits = 2
  histogram_color = darkseagreen
  histogram_mode = compact
  potential_parent_values = merge
  figure_facecolor = #E0E0E0
  graph_format = svg
  default_graph_size = 5
  default_graph_inference_size = 8
  default_arc_color = #4A4A4A
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  evidence_bgcolor = sandybrown
  evidence_fgcolor = black
  default_node_cmap = Pastel1
  default_arc_cmap = BuGn
[dynamicBN]
  default_graph_size = 6
[influenceDiagram]
  default_graph_size = 6
  default_chance_bgcolor = #504A50
  default_chance_fgcolor = white
  default_chance_shape = ellipse
  default_utility_bgcolor = #4A5050
  default_utility_fgcolor = white
  default_utility_shape = diamond
  default_decision_bgcolor = #50504A
  default_decision_fgcolor = white
  default_decision_shape = box
[causal]
  show_latent_names = False
  latex_do_prefix = \hookrightarrow\mkern-6.5mu
  latex_do_suffix = 
  default_graph_size = 2.5
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  default_latent_bgcolor = #A08080
  default_latent_fgcolor = black
[ROC]
  draw_color = #008800
  fill_color = #AAEEAA
===================================

As far as it can be done with Python, gum.config is a singleton

In [2]:
cfg2=gum.PyAgrumConfiguration("")
print(id(cfg2))
print(id(gum.config))
139879651574288
139879651574288

The configuration is a mix between the defaults (which exactly define the possible section and property) and a file pyagrum.ini (if present in the current folder) that contains only the changed properties.

section and key are case insensitive.

In [3]:
!cat pyagrum.ini

config constantly keeps track of the differences between defaults and actual values :

In [4]:
gum.config['dynamicBN','default_graph_size']=10
gum.config.diff()
[dynamicBN]
  default_graph_size = 10
In [5]:
gum.config
Out[5]:
[dynamicBN]
  default_graph_size = 10

Accessors

Getter

In [6]:
print(gum.config) # __str__ shows all the properties (__repr__ shows only the diff)
[notebook]
  potential_visible_digits = 4
  horizontal_histogram_visible_digits = 2
  vertical_histogram_visible_digits = 2
  histogram_color = darkseagreen
  histogram_mode = compact
  potential_parent_values = merge
  figure_facecolor = #E0E0E0
  graph_format = svg
  default_graph_size = 5
  default_graph_inference_size = 8
  default_arc_color = #4A4A4A
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  evidence_bgcolor = sandybrown
  evidence_fgcolor = black
  default_node_cmap = Pastel1
  default_arc_cmap = BuGn
[dynamicBN]
  default_graph_size = 10
[influenceDiagram]
  default_graph_size = 6
  default_chance_bgcolor = #504A50
  default_chance_fgcolor = white
  default_chance_shape = ellipse
  default_utility_bgcolor = #4A5050
  default_utility_fgcolor = white
  default_utility_shape = diamond
  default_decision_bgcolor = #50504A
  default_decision_fgcolor = white
  default_decision_shape = box
[causal]
  show_latent_names = False
  latex_do_prefix = \hookrightarrow\mkern-6.5mu
  latex_do_suffix = 
  default_graph_size = 2.5
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  default_latent_bgcolor = #A08080
  default_latent_fgcolor = black
[ROC]
  draw_color = #008800
  fill_color = #AAEEAA
In [7]:
gum.config["notebook","evidence_bgcolor"]
Out[7]:
'sandybrown'

Setter

In [8]:
gum.config["notebook","evidence_bgcolor"]="papayawhip"
gum.config["causal","default_graph_size"]=1.9
In [9]:
gum.config # once again, only the diff with defaults
Out[9]:
[notebook]
  evidence_bgcolor = papayawhip
[dynamicBN]
  default_graph_size = 10
[causal]
  default_graph_size = 1.9

The structure section.key is fixed by default and readonly : one can only change the value of an existing property

In [10]:
try:
    gum.config["AAA","OOO"]=1
except SyntaxError as e:
    print("Syntax error : {}".format(e.msg))
Syntax error : You can not add section 'AAA' in pyAgrum configuration
In [11]:
try:
    gum.config["causal","OOO"]=1
except SyntaxError as e:
    print("Syntax error : {}".format(e.msg))
Syntax error : You can not add option 'causal,OOO' in pyAgrum configuration

All the properties are stored as string !

In [12]:
gum.config["notebook","default_graph_size"]
Out[12]:
'5'
In [13]:
gum.config["notebook","default_graph_size"]=10
gum.config["notebook","default_graph_size"]
Out[13]:
'10'

Reset the configuration and reload configuration

In [14]:
gum.config.reset() # back to defaults
gum.config # no diff => shows all the properties
Out[14]:
# no customized property
[notebook]
  potential_visible_digits = 4
  horizontal_histogram_visible_digits = 2
  vertical_histogram_visible_digits = 2
  histogram_color = darkseagreen
  histogram_mode = compact
  potential_parent_values = merge
  figure_facecolor = #E0E0E0
  graph_format = svg
  default_graph_size = 5
  default_graph_inference_size = 8
  default_arc_color = #4A4A4A
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  evidence_bgcolor = sandybrown
  evidence_fgcolor = black
  default_node_cmap = Pastel1
  default_arc_cmap = BuGn
[dynamicBN]
  default_graph_size = 6
[influenceDiagram]
  default_graph_size = 6
  default_chance_bgcolor = #504A50
  default_chance_fgcolor = white
  default_chance_shape = ellipse
  default_utility_bgcolor = #4A5050
  default_utility_fgcolor = white
  default_utility_shape = diamond
  default_decision_bgcolor = #50504A
  default_decision_fgcolor = white
  default_decision_shape = box
[causal]
  show_latent_names = False
  latex_do_prefix = \hookrightarrow\mkern-6.5mu
  latex_do_suffix = 
  default_graph_size = 2.5
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  default_latent_bgcolor = #A08080
  default_latent_fgcolor = black
[ROC]
  draw_color = #008800
  fill_color = #AAEEAA
In [15]:
try:
    gum.config.load() # reload pyagrum.ini
except FileNotFoundError:
    pass # no pyagrum.ini in the folder
gum.config 
Out[15]:
# no customized property
[notebook]
  potential_visible_digits = 4
  horizontal_histogram_visible_digits = 2
  vertical_histogram_visible_digits = 2
  histogram_color = darkseagreen
  histogram_mode = compact
  potential_parent_values = merge
  figure_facecolor = #E0E0E0
  graph_format = svg
  default_graph_size = 5
  default_graph_inference_size = 8
  default_arc_color = #4A4A4A
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  evidence_bgcolor = sandybrown
  evidence_fgcolor = black
  default_node_cmap = Pastel1
  default_arc_cmap = BuGn
[dynamicBN]
  default_graph_size = 6
[influenceDiagram]
  default_graph_size = 6
  default_chance_bgcolor = #504A50
  default_chance_fgcolor = white
  default_chance_shape = ellipse
  default_utility_bgcolor = #4A5050
  default_utility_fgcolor = white
  default_utility_shape = diamond
  default_decision_bgcolor = #50504A
  default_decision_fgcolor = white
  default_decision_shape = box
[causal]
  show_latent_names = False
  latex_do_prefix = \hookrightarrow\mkern-6.5mu
  latex_do_suffix = 
  default_graph_size = 2.5
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  default_latent_bgcolor = #A08080
  default_latent_fgcolor = black
[ROC]
  draw_color = #008800
  fill_color = #AAEEAA

Using configuration

In [16]:
bn=gum.fastBN("D->C<-A->B[4];A->E")
bn.cpt("B")
Out[16]:
B
A
0
1
2
3
0
0.24710.22520.45970.0680
1
0.06470.26040.63140.0436
In [17]:
gum.config["notebook","potential_visible_digits"]=1
bn.cpt("B")
Out[17]:
B
A
0
1
2
3
0
0.20.20.50.1
1
0.10.30.60.0
In [18]:
gnb.sideBySide(bn,gnb.getInference(bn,evs={"A":1},targets={"B"}))
G D D C C D->C A A A->C B B A->B E E A->E
structs Inference in   0.66ms D D C C D->C A A A->C B A->B E E A->E
In [19]:
gum.config["notebook","evidence_bgcolor"]="green"
gum.config["notebook","default_node_bgcolor"]="yellow"
gum.config["notebook","default_node_fgcolor"]="red"
gnb.sideBySide(bn,gnb.getInference(bn,evs={"A":1},targets={"B"}))
G D D C C D->C A A A->C B B A->B E E A->E
structs Inference in   0.72ms D D C C D->C A A A->C B A->B E E A->E
In [20]:
gum.config["notebook","default_graph_size"]=1
gnb.sideBySide(bn,gnb.getInference(bn,evs={"A":1},targets={"B"}))
G D D C C D->C A A A->C B B A->B E E A->E
structs Inference in   0.75ms D D C C D->C A A A->C B A->B E E A->E
In [21]:
gum.config["notebook","default_graph_inference_size"]=1
gnb.sideBySide(bn,gnb.getInference(bn,evs={"A":1},targets={"B"}))
G D D C C D->C A A A->C B B A->B E E A->E
structs Inference in   0.68ms D D C C D->C A A A->C B A->B E E A->E

find a specific property

In [22]:
#find anything containing arc
gum.config.grep("arc")
[notebook]
  default_arc_color = #4A4A4A
  default_arc_cmap = BuGn
In [23]:
#find anything containing default
gum.config.grep("default")
[notebook]
  default_graph_size = 1
  default_graph_inference_size = 1
  default_arc_color = #4A4A4A
  default_node_bgcolor = yellow
  default_node_fgcolor = red
  default_node_cmap = Pastel1
  default_arc_cmap = BuGn
[dynamicBN]
  default_graph_size = 6
[influenceDiagram]
  default_graph_size = 6
  default_chance_bgcolor = #504A50
  default_chance_fgcolor = white
  default_chance_shape = ellipse
  default_utility_bgcolor = #4A5050
  default_utility_fgcolor = white
  default_utility_shape = diamond
  default_decision_bgcolor = #50504A
  default_decision_fgcolor = white
  default_decision_shape = box
[causal]
  default_graph_size = 2.5
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  default_latent_bgcolor = #A08080
  default_latent_fgcolor = black
In [24]:
# if a section contains the search, all its properties are shown
gum.config.grep("caus")
[causal]
  show_latent_names = False
  latex_do_prefix = \hookrightarrow\mkern-6.5mu
  latex_do_suffix = 
  default_graph_size = 2.5
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  default_latent_bgcolor = #A08080
  default_latent_fgcolor = black

Saving current configuration in pyagrum.ini

In [25]:
gum.config.reset()
gum.config['notebook','default_arc_color'] = "#AAAAAA"
gum.config['notebook','evidence_bgcolor'] = "green"
gum.config.save()
In [26]:
!cat pyagrum.ini
[notebook]
  default_arc_color = #AAAAAA
  evidence_bgcolor = green
In [27]:
gum.config.reset() # back to defaults
gum.config.save() # store defaults back
!cat pyagrum.ini

In [28]:
from configparser import ConfigParser
c=ConfigParser()

gum.config['notebook','default_arc_color'] = "#AAAAAA"
gum.config['notebook','evidence_bgcolor'] = "green"
c.read_string(gum.config.__repr__())
In [29]:
[section for section in c.sections() if len(c[section])>0]
Out[29]:
['notebook']
In [30]:
gum.config.__repr__()
Out[30]:
'[notebook]\n  default_arc_color = #AAAAAA\n  evidence_bgcolor = green'
In [ ]: