gum.config :the configuration object for pyAgrum

Creative Commons License

aGrUM

interactive online version

In [1]:
import pyAgrum as gum
print("="*35)
print(gum.config)
print("="*35)
===================================
[core]
  default_maxnumberofthreads = 24
[notebook]
  potential_visible_digits = 4
  potential_with_colors = True
  potential_color_0 = #FF7F64
  potential_color_1 = #7FFF64
  potential_with_fraction = False
  potential_fraction_limit = 50
  potential_fraction_round_error = 1e-6
  potential_fraction_with_latex = True
  histogram_horizontal_visible_digits = 2
  histogram_vertical_visible_digits = 2
  histogram_horizontal_threshold = 8
  histogram_line_threshold = 40
  histogram_color = darkseagreen
  histogram_use_percent = True
  histogram_mode = compact
  potential_parent_values = merge
  figure_facecolor = #E0E0E0
  graph_format = svg
  show_inference_time = True
  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
  default_edge_cmap = BuGn
  default_markovnetwork_view = factorgraph
  junctiontree_graph_size = 10
  junctiontree_with_names = True
  junctiontree_separator_bgcolor = palegreen
  junctiontree_separator_fgcolor = black
  junctiontree_separator_fontsize = 8
  junctiontree_clique_bgcolor = burlywood
  junctiontree_clique_fgcolor = black
  junctiontree_clique_fontsize = 10
  junctiontree_map_cliquescale = 0.3
  junctiontree_map_sepscale = 0.1
  junctiontree_map_edgelen = 1
  graphdiff_missing_style = dashed
  graphdiff_missing_color = red
  graphdiff_overflow_style = dashed
  graphdiff_overflow_color = purple
  graphdiff_reversed_style = solid
  graphdiff_reversed_color = purple
  graphdiff_correct_style = solid
  graphdiff_correct_color = grey
[factorgraph]
  default_node_bgcolor = coral
  default_node_fgcolor = black
  default_factor_bgcolor = burlywood
  edge_length = 0.7
  edge_length_inference = 0.9
[dynamicBN]
  default_graph_size = 6
[influenceDiagram]
  default_graph_size = 6
  default_chance_bgcolor = #808080
  default_chance_fgcolor = white
  default_utility_bgcolor = #50508A
  default_utility_fgcolor = white
  default_decision_bgcolor = #9A5050
  default_decision_fgcolor = white
  chance_shape = ellipse
  utility_shape = hexagon
  decision_shape = box
  decision_arc_style = tapered, bold, dotted
  utility_arc_style = dashed
  default_id_size = 6
  default_id_inference_size = 6
  utility_visible_digits = 2
  utility_show_stdev = True
  utility_show_loss = False
[credalnet]
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  histo_max_color = #BBFFAA
[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
===================================

gum.config as singleton

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))
1552049215552
1552049215552

pyagrum.ini

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.

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

In [3]:
gum.config['dynamicBN','default_graph_size']=10

print("Configuration only for the current session")
print('-'*40)
gum.config.diff()
Configuration only for the current session
----------------------------------------
[dynamicBN]
  default_graph_size = 10
In [4]:
# if there is a local modification, __repr__ shows only the diff other wise it show all properties
gum.config
Out[4]:
[dynamicBN]
  default_graph_size = 10
In [5]:
# __str__ shows all the properties
print(gum.config)
[core]
  default_maxnumberofthreads = 24
[notebook]
  potential_visible_digits = 4
  potential_with_colors = True
  potential_color_0 = #FF7F64
  potential_color_1 = #7FFF64
  potential_with_fraction = False
  potential_fraction_limit = 50
  potential_fraction_round_error = 1e-6
  potential_fraction_with_latex = True
  histogram_horizontal_visible_digits = 2
  histogram_vertical_visible_digits = 2
  histogram_horizontal_threshold = 8
  histogram_line_threshold = 40
  histogram_color = darkseagreen
  histogram_use_percent = True
  histogram_mode = compact
  potential_parent_values = merge
  figure_facecolor = #E0E0E0
  graph_format = svg
  show_inference_time = True
  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
  default_edge_cmap = BuGn
  default_markovnetwork_view = factorgraph
  junctiontree_graph_size = 10
  junctiontree_with_names = True
  junctiontree_separator_bgcolor = palegreen
  junctiontree_separator_fgcolor = black
  junctiontree_separator_fontsize = 8
  junctiontree_clique_bgcolor = burlywood
  junctiontree_clique_fgcolor = black
  junctiontree_clique_fontsize = 10
  junctiontree_map_cliquescale = 0.3
  junctiontree_map_sepscale = 0.1
  junctiontree_map_edgelen = 1
  graphdiff_missing_style = dashed
  graphdiff_missing_color = red
  graphdiff_overflow_style = dashed
  graphdiff_overflow_color = purple
  graphdiff_reversed_style = solid
  graphdiff_reversed_color = purple
  graphdiff_correct_style = solid
  graphdiff_correct_color = grey
[factorgraph]
  default_node_bgcolor = coral
  default_node_fgcolor = black
  default_factor_bgcolor = burlywood
  edge_length = 0.7
  edge_length_inference = 0.9
[dynamicBN]
  default_graph_size = 10
[influenceDiagram]
  default_graph_size = 6
  default_chance_bgcolor = #808080
  default_chance_fgcolor = white
  default_utility_bgcolor = #50508A
  default_utility_fgcolor = white
  default_decision_bgcolor = #9A5050
  default_decision_fgcolor = white
  chance_shape = ellipse
  utility_shape = hexagon
  decision_shape = box
  decision_arc_style = tapered, bold, dotted
  utility_arc_style = dashed
  default_id_size = 6
  default_id_inference_size = 6
  utility_visible_digits = 2
  utility_show_stdev = True
  utility_show_loss = False
[credalnet]
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  histo_max_color = #BBFFAA
[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

Accessors

Getter

In [6]:
print(gum.config["notebook","evidence_bgcolor"])
print(gum.config.get("notebook","evidence_bgcolor"))
sandybrown
sandybrown

Setter

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

constant structure, mutable content

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

In [9]:
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 [10]:
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

properties as string

All the properties are stored as string !

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

Reset, reload, save

The configuration can be saved in the current folder (gum.config.save()).

The configuration can be restored from the default (reset) or the current saved state (reload).

In [13]:
gum.config.reset() # back to defaults
gum.config # no diff => shows all the properties
Out[13]:
# no customized property
[core]
  default_maxnumberofthreads = 24
[notebook]
  potential_visible_digits = 4
  potential_with_colors = True
  potential_color_0 = #FF7F64
  potential_color_1 = #7FFF64
  potential_with_fraction = False
  potential_fraction_limit = 50
  potential_fraction_round_error = 1e-6
  potential_fraction_with_latex = True
  histogram_horizontal_visible_digits = 2
  histogram_vertical_visible_digits = 2
  histogram_horizontal_threshold = 8
  histogram_line_threshold = 40
  histogram_color = darkseagreen
  histogram_use_percent = True
  histogram_mode = compact
  potential_parent_values = merge
  figure_facecolor = #E0E0E0
  graph_format = svg
  show_inference_time = True
  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
  default_edge_cmap = BuGn
  default_markovnetwork_view = factorgraph
  junctiontree_graph_size = 10
  junctiontree_with_names = True
  junctiontree_separator_bgcolor = palegreen
  junctiontree_separator_fgcolor = black
  junctiontree_separator_fontsize = 8
  junctiontree_clique_bgcolor = burlywood
  junctiontree_clique_fgcolor = black
  junctiontree_clique_fontsize = 10
  junctiontree_map_cliquescale = 0.3
  junctiontree_map_sepscale = 0.1
  junctiontree_map_edgelen = 1
  graphdiff_missing_style = dashed
  graphdiff_missing_color = red
  graphdiff_overflow_style = dashed
  graphdiff_overflow_color = purple
  graphdiff_reversed_style = solid
  graphdiff_reversed_color = purple
  graphdiff_correct_style = solid
  graphdiff_correct_color = grey
[factorgraph]
  default_node_bgcolor = coral
  default_node_fgcolor = black
  default_factor_bgcolor = burlywood
  edge_length = 0.7
  edge_length_inference = 0.9
[dynamicBN]
  default_graph_size = 6
[influenceDiagram]
  default_graph_size = 6
  default_chance_bgcolor = #808080
  default_chance_fgcolor = white
  default_utility_bgcolor = #50508A
  default_utility_fgcolor = white
  default_decision_bgcolor = #9A5050
  default_decision_fgcolor = white
  chance_shape = ellipse
  utility_shape = hexagon
  decision_shape = box
  decision_arc_style = tapered, bold, dotted
  utility_arc_style = dashed
  default_id_size = 6
  default_id_inference_size = 6
  utility_visible_digits = 2
  utility_show_stdev = True
  utility_show_loss = False
[credalnet]
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  histo_max_color = #BBFFAA
[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 [14]:
try:
    gum.config.load() # reload pyagrum.ini
except FileNotFoundError:
    pass # no pyagrum.ini in the folder
gum.config
Out[14]:
# no customized property
[core]
  default_maxnumberofthreads = 24
[notebook]
  potential_visible_digits = 4
  potential_with_colors = True
  potential_color_0 = #FF7F64
  potential_color_1 = #7FFF64
  potential_with_fraction = False
  potential_fraction_limit = 50
  potential_fraction_round_error = 1e-6
  potential_fraction_with_latex = True
  histogram_horizontal_visible_digits = 2
  histogram_vertical_visible_digits = 2
  histogram_horizontal_threshold = 8
  histogram_line_threshold = 40
  histogram_color = darkseagreen
  histogram_use_percent = True
  histogram_mode = compact
  potential_parent_values = merge
  figure_facecolor = #E0E0E0
  graph_format = svg
  show_inference_time = True
  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
  default_edge_cmap = BuGn
  default_markovnetwork_view = factorgraph
  junctiontree_graph_size = 10
  junctiontree_with_names = True
  junctiontree_separator_bgcolor = palegreen
  junctiontree_separator_fgcolor = black
  junctiontree_separator_fontsize = 8
  junctiontree_clique_bgcolor = burlywood
  junctiontree_clique_fgcolor = black
  junctiontree_clique_fontsize = 10
  junctiontree_map_cliquescale = 0.3
  junctiontree_map_sepscale = 0.1
  junctiontree_map_edgelen = 1
  graphdiff_missing_style = dashed
  graphdiff_missing_color = red
  graphdiff_overflow_style = dashed
  graphdiff_overflow_color = purple
  graphdiff_reversed_style = solid
  graphdiff_reversed_color = purple
  graphdiff_correct_style = solid
  graphdiff_correct_color = grey
[factorgraph]
  default_node_bgcolor = coral
  default_node_fgcolor = black
  default_factor_bgcolor = burlywood
  edge_length = 0.7
  edge_length_inference = 0.9
[dynamicBN]
  default_graph_size = 6
[influenceDiagram]
  default_graph_size = 6
  default_chance_bgcolor = #808080
  default_chance_fgcolor = white
  default_utility_bgcolor = #50508A
  default_utility_fgcolor = white
  default_decision_bgcolor = #9A5050
  default_decision_fgcolor = white
  chance_shape = ellipse
  utility_shape = hexagon
  decision_shape = box
  decision_arc_style = tapered, bold, dotted
  utility_arc_style = dashed
  default_id_size = 6
  default_id_inference_size = 6
  utility_visible_digits = 2
  utility_show_stdev = True
  utility_show_loss = False
[credalnet]
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  histo_max_color = #BBFFAA
[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 [15]:
import pyAgrum.lib.notebook as gnb

bn=gum.fastBN("D->C<-A->B[4];A->E")
bn.cpt("B")
Out[15]:
B
A
0
1
2
3
0
0.09080.65970.24550.0040
1
0.05270.29980.41160.2360
In [16]:
gum.config["notebook","potential_visible_digits"]=1
bn.cpt("B")
Out[16]:
B
A
0
1
2
3
0
0.10.70.20.0
1
0.10.30.40.2
In [17]:
gum.config['notebook', 'potential_color_0']="#AA00AA"
gum.config['notebook', 'potential_color_1']="#00FFAA"
bn.cpt("B")
Out[17]:
B
A
0
1
2
3
0
0.10.70.20.0
1
0.10.30.40.2
In [18]:
gum.config["notebook","potential_visible_digits"]=4
gnb.flow.add(bn.cpt("B"),"Ugly float")

gum.config["notebook","potential_visible_digits"]=1
gnb.flow.add(bn.cpt("B"),"Ugly 1digit-float")

gum.config['notebook', 'potential_with_fraction']=True
gum.config['notebook', 'potential_fraction_with_latex']=False
gum.config['notebook', 'potential_fraction_limit']=2000
gnb.flow.add(bn.cpt("B"),"Simple fraction")

gum.config['notebook', 'potential_fraction_with_latex']=True
gnb.flow.add(bn.cpt("B"),"Sophisticated fraction with LaTeX")
gnb.flow.display()
B
A
0
1
2
3
0
0.09080.65970.24550.0040
1
0.05270.29980.41160.2360

Ugly float
B
A
0
1
2
3
0
0.10.70.20.0
1
0.10.30.40.2

Ugly 1digit-float
B
A
0
1
2
3
0
169/1861411/623425/17317/1770
1
45/854360/1201761/1849383/1623

Simple fraction
B
A
0
1
2
3
0
$$\frac{169}{1861}$$$$\frac{411}{623}$$$$\frac{425}{1731}$$$$\frac{7}{1770}$$
1
$$\frac{45}{854}$$$$\frac{360}{1201}$$$$\frac{761}{1849}$$$$\frac{383}{1623}$$

Sophisticated fraction with LaTeX
In [19]:
gnb.sideBySide(bn,gnb.getInference(bn,evs={"A":1},targets={"B"}))
G D D C C D->C B B A A A->C A->B E E A->E
structs Inference in   0.00ms D D C C D->C A A A->C B 2022-06-05T19:27:49.617516 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ A->B E E A->E
In [20]:
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 B B A A A->C A->B E E A->E
structs Inference in   1.00ms D D C C D->C A A A->C B 2022-06-05T19:27:50.006515 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ A->B E E A->E
In [21]:
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 B B A A A->C A->B E E A->E
structs Inference in   1.00ms D D C C D->C A A A->C B 2022-06-05T19:27:50.366513 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ A->B E E A->E
In [22]:
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 B B A A A->C A->B E E A->E
structs Inference in   0.00ms D D C C D->C A A A->C B 2022-06-05T19:27:50.696509 image/svg+xml Matplotlib v3.5.2, https://matplotlib.org/ A->B E E A->E

Finding a specific property

In [23]:
#find anything containing arc
gum.config.grep("arc")
[notebook]
  default_arc_color = #4A4A4A
  default_arc_cmap = BuGn
[influenceDiagram]
  decision_arc_style = tapered, bold, dotted
  utility_arc_style = dashed
In [24]:
#find anything containing default
gum.config.grep("default")
[core]
  default_maxnumberofthreads = 24
[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
  default_edge_cmap = BuGn
  default_markovnetwork_view = factorgraph
[factorgraph]
  default_node_bgcolor = coral
  default_node_fgcolor = black
  default_factor_bgcolor = burlywood
[dynamicBN]
  default_graph_size = 6
[influenceDiagram]
  default_graph_size = 6
  default_chance_bgcolor = #808080
  default_chance_fgcolor = white
  default_utility_bgcolor = #50508A
  default_utility_fgcolor = white
  default_decision_bgcolor = #9A5050
  default_decision_fgcolor = white
  default_id_size = 6
  default_id_inference_size = 6
[credalnet]
  default_node_bgcolor = #404040
  default_node_fgcolor = white
[causal]
  default_graph_size = 2.5
  default_node_bgcolor = #404040
  default_node_fgcolor = white
  default_latent_bgcolor = #A08080
  default_latent_fgcolor = black
In [25]:
# 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 [26]:
gum.config.reset() # back to defaults
gum.config['notebook','default_arc_color'] = "#AAAAAA"
gum.config['notebook','evidence_bgcolor'] = "green"
gum.config.save() # store curent changes
In [27]:
gum.config.reset() # back to defaults
gum.config.save() # store defaults back

From PyAgrumConfiguration to ConfigParser

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__())
print(c.sections())
['notebook']
In [ ]: