Functions from pyAgrum

Useful functions in pyAgrum

pyAgrum.getPosterior(model, *, target, evs=None)

Compute the posterior of a single target (variable) in a BN given evidence

getPosterior uses a VariableElimination inference. If more than one target is needed with the same set of evidence or if the same target is needed with more than one set of evidence, this function is not relevant since it creates a new inference engine every time it is called.

Parameters
  • bn (pyAgrum.BayesNet or pyAgrum.MarkovRandomField) – The probabilistic Graphical Model

  • target (string or int) – variable name or id (forced keyword argument)

  • evs (Dict[name|id:val, name|id : List[ val1, val2 ], ...]. (optional forced keyword argument)) – the (hard and soft) evidence

Returns

posterior (pyAgrum.Potential or other)

Input/Output for Bayesian networks

pyAgrum.availableBNExts()

Give the list of all formats known by pyAgrum to save a Bayesian network.

Returns

a string which lists all suffixes for supported BN file formats.

pyAgrum.loadBN(filename, listeners=None, verbose=False, **opts)

load a BN from a file with optional listeners and arguments

Parameters
  • filename (str) – the name of the input file

  • listeners (List[object]) – list of functions to execute when listening

  • verbose (bool) – whether to print or not warning messages

  • system (str) – (for O3PRM) name of the system to flatten in a BN

  • classpath (List[str]) – (for O3PRM) list of folders containing classes

Returns

a BN from a file using one of the availableBNExts() suffixes.

Return type

pyAgrum.BayesNet

Notes

Listeners could be added in order to monitor its loading.

Examples

>>> import pyAgrum as gum
>>>
>>> # creating listeners
>>> def foo_listener(progress):
>>>    if progress==200:
>>>        print(' BN loaded ')
>>>        return
>>>    elif progress==100:
>>>        car='%'
>>>    elif progress%10==0:
>>>        car='#'
>>>    else:
>>>        car='.'
>>>    print(car,end='',flush=True)
>>>
>>> def bar_listener(progress):
>>>    if progress==50:
>>>        print('50%')
>>>
>>> # loadBN with list of listeners
>>> gum.loadBN('./bn.bif',listeners=[foo_listener,bar_listener])
>>> # .........#.........#.........#.........#..50%
>>> # .......#.........#.........#.........#.........#.........% | bn loaded
pyAgrum.saveBN(bn, filename, allowModificationWhenSaving=None)

save a BN into a file using the format corresponding to one of the availableWriteBNExts() suffixes.

Parameters
  • bn (pyAgrum.BayesNet) – the BN to save

  • filename (str) – the name of the output file

  • allowModificationWhenSaving (bool) – whether syntax errors in the BN should throw a FatalError or can be corrected. Also controlled by pyAgrum.config[“BN”,”allow_modification_when_saving”].

Input/Output for Markov random fields

pyAgrum.availableMNExts()

Give the list of all formats known by pyAgrum to save a Markov random field.

Returns

a string which lists all suffixes for supported MRF file formats.

Return type

str

pyAgrum.loadMN(filename, listeners=None, verbose=False)

load a MRF from a file with optional listeners and arguments

Parameters
  • filename (str) – the name of the input file

  • listeners (List[Object]) – list of functions to execute

  • verbose (bool) – whether to print or not warning messages

Returns

  • pyAgrum.MarkovRandomField – a MRF from a file using one of the availableMNExts() suffixes.

  • Listeners could be added in order to monitor its loading.

Examples

>>> import pyAgrum as gum
>>>
>>> # creating listeners
>>> def foo_listener(progress):
>>>    if progress==200:
>>>        print(' BN loaded ')
>>>        return
>>>    elif progress==100:
>>>        car='%'
>>>    elif progress%10==0:
>>>        car='#'
>>>    else:
>>>        car='.'
>>>    print(car,end='',flush=True)
>>>
>>> def bar_listener(progress):
>>>    if progress==50:
>>>        print('50%')
>>>
>>> # loadBN with list of listeners
>>> gum.loadMN('./bn.uai',listeners=[foo_listener,bar_listener])
>>> # .........#.........#.........#.........#..50%
>>> # .......#.........#.........#.........#.........#.........% | bn loaded
pyAgrum.saveMN(mn, filename)

save a MRF into a file using the format corresponding to one of the availableWriteMNExts() suffixes.

Parameters

Input for influence diagram

pyAgrum.availableIDExts()

Give the list of all formats known by pyAgrum to save a influence diagram.

Returns

a string which lists all suffixes for supported ID file formats.

Return type

str

pyAgrum.loadID(filename)

read a gum.InfluenceDiagram from a ID file

Parameters

filename (str) – the name of the input file

Returns

the InfluenceDiagram

Return type

pyAgrum.InfluenceDiagram

pyAgrum.saveID(infdiag, filename)

save an ID into a file using the format corresponding to one of the availableWriteIDExts() suffixes.

Parameters