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_edge_color = darkgreen
  histogram_use_percent = True
  histogram_discretized_visualisation = histogram
  histogram_discretized_scale = 1.0
  histogram_mode = compact
  histogram_epsilon = 1e-8
  potential_parent_values = merge
  figure_facecolor = #E0E0E0
  flow_background_color = transparent
  flow_border_color = transparent
  flow_border_width = 0
  graph_format = svg
  show_inference_time = True
  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_graph_size = 5
  default_graph_inference_size = 8
  graph_rankdir = TB
  graph_layout = dot
  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
[BN]
  allow_modification_when_saving = False
[factorgraph]
  default_node_bgcolor = coral
  default_node_fgcolor = black
  default_factor_bgcolor = burlywood
  edge_length = 0.7
  edge_length_inference = 0.9
  graph_layout = neato
[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 = \text{do}(
  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
[Pickle]
  add_version = True
  add_date = True
===================================

pyagrum.ini

As far as it can be done with Python, gum.config is a singleton. 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 [2]:
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 [3]:
# if there is a local modification, __repr__ shows only the diff other wise it show all properties
gum.config
Out[3]:
[dynamicBN]
  default_graph_size = 10
In [4]:
# __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_edge_color = darkgreen
  histogram_use_percent = True
  histogram_discretized_visualisation = histogram
  histogram_discretized_scale = 1.0
  histogram_mode = compact
  histogram_epsilon = 1e-8
  potential_parent_values = merge
  figure_facecolor = #E0E0E0
  flow_background_color = transparent
  flow_border_color = transparent
  flow_border_width = 0
  graph_format = svg
  show_inference_time = True
  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_graph_size = 5
  default_graph_inference_size = 8
  graph_rankdir = TB
  graph_layout = dot
  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
[BN]
  allow_modification_when_saving = False
[factorgraph]
  default_node_bgcolor = coral
  default_node_fgcolor = black
  default_factor_bgcolor = burlywood
  edge_length = 0.7
  edge_length_inference = 0.9
  graph_layout = neato
[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 = \text{do}(
  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
[Pickle]
  add_version = True
  add_date = True

Accessors

Getter

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

Setter

In [6]:
gum.config["notebook","evidence_bgcolor"]="papayawhip"
gum.config["causal","default_graph_size"]=1.9
In [7]:
gum.config # once again, only the diff with defaults
Out[7]:
[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 [8]:
try:
    gum.config["AAA","OOO"]=1
except SyntaxError as e:
    print("Syntax error : {}".format(e.msg))
Syntax error : Section 'AAA' unknown in pyAgrum configuration.
In [9]:
try:
    gum.config["causal","OOO"]=1
except SyntaxError as e:
    print("Syntax error : {}".format(e.msg))
Syntax error : Key 'causal,OOO' unknown in pyAgrum configuration.

properties as string

All the properties are stored as string !

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

… but can be typed using asInt, asFloat, asBool

In [12]:
gum.config['dynamicBN','default_graph_size']
Out[12]:
'10'
In [13]:
gum.config.asInt['dynamicBN','default_graph_size']
Out[13]:
10
In [14]:
print(type(gum.config['dynamicBN','default_graph_size']))
print(type(gum.config.asInt['dynamicBN','default_graph_size']))
<class 'str'>
<class 'int'>
In [15]:
if gum.config["causal","show_latent_names"]=="False":
    print("not very convenient test")
if not gum.config.asBool["causal","show_latent_names"]:
    print("better test")
not very convenient test
better test

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 [16]:
gum.config.reset() # back to defaults
gum.config # no diff => shows all the properties
Out[16]:
# 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_edge_color = darkgreen
  histogram_use_percent = True
  histogram_discretized_visualisation = histogram
  histogram_discretized_scale = 1.0
  histogram_mode = compact
  histogram_epsilon = 1e-8
  potential_parent_values = merge
  figure_facecolor = #E0E0E0
  flow_background_color = transparent
  flow_border_color = transparent
  flow_border_width = 0
  graph_format = svg
  show_inference_time = True
  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_graph_size = 5
  default_graph_inference_size = 8
  graph_rankdir = TB
  graph_layout = dot
  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
[BN]
  allow_modification_when_saving = False
[factorgraph]
  default_node_bgcolor = coral
  default_node_fgcolor = black
  default_factor_bgcolor = burlywood
  edge_length = 0.7
  edge_length_inference = 0.9
  graph_layout = neato
[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 = \text{do}(
  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
[Pickle]
  add_version = True
  add_date = True
In [17]:
try:
    gum.config.load() # reload pyagrum.ini
except FileNotFoundError:
    pass # no pyagrum.ini in the folder
gum.config
Out[17]:
# 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_edge_color = darkgreen
  histogram_use_percent = True
  histogram_discretized_visualisation = histogram
  histogram_discretized_scale = 1.0
  histogram_mode = compact
  histogram_epsilon = 1e-8
  potential_parent_values = merge
  figure_facecolor = #E0E0E0
  flow_background_color = transparent
  flow_border_color = transparent
  flow_border_width = 0
  graph_format = svg
  show_inference_time = True
  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_graph_size = 5
  default_graph_inference_size = 8
  graph_rankdir = TB
  graph_layout = dot
  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
[BN]
  allow_modification_when_saving = False
[factorgraph]
  default_node_bgcolor = coral
  default_node_fgcolor = black
  default_factor_bgcolor = burlywood
  edge_length = 0.7
  edge_length_inference = 0.9
  graph_layout = neato
[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 = \text{do}(
  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
[Pickle]
  add_version = True
  add_date = True

Using configuration

In [18]:
import pyAgrum.lib.notebook as gnb

bn=gum.fastBN("D->C<-A->B[4];A->E")
bn.cpt("B")
Out[18]:
B
A
0
1
2
3
0
0.16620.13830.25050.4450
1
0.20410.39580.24580.1543
In [19]:
gum.config["notebook","potential_visible_digits"]=1
bn.cpt("B")
Out[19]:
B
A
0
1
2
3
0
0.20.10.30.4
1
0.20.40.20.2
In [20]:
gum.config['notebook', 'potential_color_0']="#AA00AA"
gum.config['notebook', 'potential_color_1']="#00FFAA"
bn.cpt("B")
Out[20]:
B
A
0
1
2
3
0
0.20.10.30.4
1
0.20.40.20.2
In [21]:
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.16620.13830.25050.4450
1
0.20410.39580.24580.1543

Ugly float
B
A
0
1
2
3
0
0.20.10.30.4
1
0.20.40.20.2

Ugly 1digit-float
B
A
0
1
2
3
0
323/1943181/1309395/1577174/391
1
372/1823515/1301439/1786187/1212

Simple fraction
B
A
0
1
2
3
0
$$\frac{323}{1943}$$$$\frac{181}{1309}$$$$\frac{395}{1577}$$$$\frac{174}{391}$$
1
$$\frac{372}{1823}$$$$\frac{515}{1301}$$$$\frac{439}{1786}$$$$\frac{187}{1212}$$

Sophisticated fraction with LaTeX
In [22]:
gnb.sideBySide(bn,gnb.getInference(bn,evs={"A":1},targets={"B"}))
G A A E E A->E C C A->C B B A->B D D D->C
structs Inference in   1.96ms D D C C D->C A A A->C B 2024-06-07T18:18:44.182230 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ A->B E E A->E
In [23]:
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 A A E E A->E C C A->C B B A->B D D D->C
structs Inference in   0.63ms D D C C D->C A A A->C B 2024-06-07T18:18:44.829247 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ A->B E E A->E
In [24]:
gum.config["notebook","default_graph_size"]=1
gnb.sideBySide(bn,gnb.getInference(bn,evs={"A":1},targets={"B"}))
G A A E E A->E C C A->C B B A->B D D D->C
structs Inference in   0.66ms D D C C D->C A A A->C B 2024-06-07T18:18:45.402591 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ A->B E E A->E
In [25]:
gum.config["notebook","default_graph_inference_size"]="1"
gnb.sideBySide(bn,gnb.getInference(bn,evs={"A":1},targets={"B"}))
G A A E E A->E C C A->C B B A->B D D D->C
structs Inference in   0.63ms D D C C D->C A A A->C B 2024-06-07T18:18:45.892412 image/svg+xml Matplotlib v3.9.0, https://matplotlib.org/ A->B E E A->E

Finding a specific property

In [26]:
#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 [27]:
#find anything containing default
gum.config.grep("default")
[core]
  default_maxnumberofthreads = 24
[notebook]
  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_graph_size = 1
  default_graph_inference_size = 1
  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 [28]:
# if a section contains the search, all its properties are shown
gum.config.grep("caus")
[causal]
  show_latent_names = False
  latex_do_prefix = \text{do}(
  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 [29]:
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 [30]:
gum.config.reset() # back to defaults
gum.config.save() # store defaults back

From PyAgrumConfiguration to ConfigParser

In [31]:
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']