Model for Bayesian Network

a Bayesian network (alarm)

The Bayesian network is the main graphical model of pyagrum. A Bayesian network is a directed probabilistic graphical model based on a DAG. It represents a joint distribution over a set of random variables. In pyAgrum, the variables are (for now) only discrete.

A Bayesian network uses a directed acyclic graph (DAG) to represent conditional independence in the joint distribution. These conditional independence allow to factorize the joint distribution, thereby allowing to compactly represent very large ones.

\[P(X_1,\cdots,X_n)=\prod_{i=1}^n P(X_i | Parents(X_i))\]

Moreover, inference algorithms can also use this graph to speed up the computations. Finally, the Bayesian networks can be learnt from data.

See also

Input/Output Formats for Bayesian Networks

Supported file formats for saving and loading Bayesian networks.

Inference

Exact and approximated inference algorithms.

Learning

Learning Bayesian network structure and parameters from data.

Tools for Bayesian networks

Utility tools: comparison, information theory, random generation, etc.

class pyagrum.BayesNet(*args)

BayesNet represents a Bayesian network.

BayesNet(name=’’) -> BayesNet
Parameters:
  • name (str) – the name of the Bayes Net

BayesNet(source) -> BayesNet
Parameters:
  • source (pyagrum.BayesNet) – the Bayesian network to copy

add(*args)

Add a variable to the pyagrum.BayesNet.

Parameters:
  • variable (pyagrum.DiscreteVariable) – the variable added

  • descr (str) – the description of the variable (following fast syntax)

  • nbrmod (int) – the number of modalities for the new variable

  • id (int) – the variable forced id in the pyagrum.BayesNet

Returns:

the id of the new node

Return type:

int

Raises:
  • pyagrum.DuplicateLabel – If variable.name() or id is already used in this pyagrum.BayesNet.

  • pyagrum.NotAllowed – If nbrmod is less than 2

addAMPLITUDE(var)

Others aggregators

Parameters:
Returns:

the id of the added value

Return type:

int

addAND(var)

Add a variable, it’s associate node and an AND implementation.

The id of the new variable is automatically generated.

Parameters:
Returns:

the id of the added variable.

Return type:

int

Raises:

pyagrum.SizeError – If variable.domainSize()>2

addArc(*args)

Add an arc in the BN, and update arc.head’s CPT.

Parameters:
  • head (int | str) – a variable’s id (int) or name

  • tail (int | str) – a variable’s id (int) or name

Raises:
Return type:

None

addArcs(listArcs)

add a list of arcs in te model.

Parameters:

listArcs (list[tuple[int|str,int|str]]) – the list of arcs

addCOUNT(var, value=1)

Others aggregators

Parameters:
Returns:

the id of the added value

Return type:

int

addEXISTS(var, value=1)

Others aggregators

Parameters:
Returns:

the id of the added value

Return type:

int

addFORALL(var, value=1)

Others aggregators

Parameters:
Returns:

the id of the added variable.

Return type:

int

addLogit(*args)

Add a variable, its associate node and a Logit implementation.

(The id of the new variable can be automatically generated.)

Parameters:
  • variable (pyagrum.DiscreteVariable) – The variable added by copy

  • externalWeight (float) – the added external weight

  • id (int) – The proposed id for the variable.

Returns:

the id of the added variable.

Return type:

int

Raises:

pyagrum.DuplicateElement – If id is already used

addMAX(var)

Others aggregators

Parameters:
Returns:

the id of the added value

Return type:

int

addMEDIAN(var)

Others aggregators

Parameters:
Returns:

the id of the added value

Return type:

int

addMIN(var)

Others aggregators

Parameters:
Returns:

the id of the added value

Return type:

int

addNoisyAND(*args)

Add a variable, its associate node and a noisyAND implementation.

(The id of the new variable can be automatically generated.)

Parameters:
  • variable (pyagrum.DiscreteVariable) – The variable added by copy

  • externalWeight (float) – the added external weight

  • id (int) – The proposed id for the variable.

Returns:

the id of the added variable.

Return type:

int

Raises:

pyagrum.DuplicateElement – If id is already used

addNoisyOR(*args)

Add a variable, it’s associate node and a noisyOR implementation.

Since it seems that the ‘classical’ noisyOR is the Compound noisyOR, we keep the addNoisyOR as an alias for addNoisyORCompound.

(The id of the new variable can be automatically generated.)

Parameters:
  • variable (pyagrum.DiscreteVariable) – The variable added by copy

  • externalWeight (float) – the added external weight

  • id (int) – The proposed id for the variable.

Returns:

the id of the added variable.

Return type:

int

Raises:

pyagrum.DuplicateElement – If id is already used

addNoisyORCompound(*args)

Add a variable, it’s associate node and a noisyOR implementation.

Since it seems that the ‘classical’ noisyOR is the Compound noisyOR, we keep the addNoisyOR as an alias for addNoisyORCompound.

(The id of the new variable can be automatically generated.)

Parameters:
  • variable (pyagrum.DiscreteVariable) – The variable added by copy

  • externalWeight (float) – the added external weight

  • id (int) – The proposed id for the variable.

Returns:

the id of the added variable.

Return type:

int

Raises:

pyagrum.DuplicateElement – If id is already used

addNoisyORNet(*args)

Add a variable, its associate node and a noisyOR implementation.

Since it seems that the ‘classical’ noisyOR is the Compound noisyOR, we keep the addNoisyOR as an alias for addNoisyORCompound.

(The id of the new variable can be automatically generated.)

Parameters:
  • variable (pyagrum.DiscreteVariable) – The variable added by copy

  • externalWeight (float) – the added external weight

  • id (int) – The proposed id for the variable.

Returns:

the id of the added variable.

Return type:

int

addOR(var)

Add a variable, it’s associate node and an OR implementation.

The id of the new variable is automatically generated.

Warning

If parents are not boolean, all value>1 is True

Parameters:
Returns:

the id of the added variable.

Return type:

int

Raises:

pyagrum.SizeError – If variable.domainSize()>2

addSUM(var)

Others aggregators

Parameters:
Returns:

the id of the added value

Return type:

int

addStructureListener(whenNodeAdded=None, whenNodeDeleted=None, whenArcAdded=None, whenArcDeleted=None)

Add the listeners in parameters to the list of existing ones.

Parameters:
  • whenNodeAdded (lambda expression) – a function for when a node is added

  • whenNodeDeleted (lambda expression) – a function for when a node is removed

  • whenArcAdded (lambda expression) – a function for when an arc is added

  • whenArcDeleted (lambda expression) – a function for when an arc is removed

addVariables(listFastVariables, default_nbr_mod=2)

Add a list of variable in the form of ‘fast’ syntax.

Parameters:
  • listFastVariables (list[str]) – the list of variables in ‘fast’ syntax.

  • default_nbr_mod (int) – the number of modalities for the variable if not specified following fast syntax. Note that default_nbr_mod=1 is mandatory to create variables with only one modality (for utility for instance).

Returns:

the list of created ids.

Return type:

list[int]

addWeightedArc(*args)

Add an arc in the BN, and update arc.head’s CPT.

Parameters:
  • head (int | str) – a variable’s id (int) or name

  • tail (int | str) – a variable’s id (int) or name

  • causalWeight (float) – the added causal weight

Raises:
Return type:

None

adjacencyMatrix()

adjacency matrix from a graph/graphical models

Compute the adjacency matrix of a pyAgrum’s graph or graphical models (more generally an object that has nodes, children/parents or neighbours methods)

Returns:

adjacency matrix (as numpy.ndarray) with nodeId as key.

Return type:

numpy.ndarray

ancestors(norid)

give the set of nodeid of ancestors of a node

Parameters:

norid (str|int) – the name or the id of the node

Returns:

the set of ids of the ancestors of node norid.

Return type:

set

arcs()
Returns:

The lisf of arcs in the IBayesNet

Return type:

list

beginTopologyTransformation()

When inserting/removing arcs, node CPTs change their dimension with a cost in time. begin Multiple Change for all CPTs These functions delay the CPTs change to be done just once at the end of a sequence of topology modification, begins a sequence of insertions/deletions of arcs without changing the dimensions of the CPTs.

Return type:

None

changeTensor(*args)

change the CPT associated to nodeId to newPot delete the old CPT associated to nodeId.

Parameters:
  • var (int | str) – the current name or the id of the variable

  • newPot (pyagrum.Tensor) – the new tensor

Raises:

pyagrum.NotAllowed – If newPot has not the same signature as __probaMap[NodeId]

Return type:

None

changeVariableLabel(*args)

change the label of the variable associated to nodeId to the new value.

Parameters:
  • var (int | str) – the current name or the id of the variable

  • old_label (str) – the new label

  • new_label (str) – the new label

Raises:

pyagrum.NotFound – if id/name is not a variable or if old_label does not exist.

Return type:

None

changeVariableName(*args)

Changes a variable’s name in the pyagrum.BayesNet.

This will change the pyagrum.DiscreteVariable names in the pyagrum.BayesNet.

Parameters:
  • var (int | str) – the current name or the id of the variable

  • new_name (str) – the new name of the variable

Raises:
Return type:

None

check()

Check if the BayesNet is consistent (variables, CPT, …)

Returns:

list of found issues

Return type:

list of str

children(norid)
Parameters:
  • id (int) – the id of the parent

  • norid (object)

Returns:

the set of all the children

Return type:

Set

clear()

Clear the whole BayesNet

Return type:

None

completeInstantiation()

Give an instantiation over all the variables of the model

Returns:

a complete Instantiation for the model

Return type:

pyagrum.Instantiation

connectedComponents()

Return the weakly connected components of the graph.

Each node is mapped to the id of its component root (an arbitrarily chosen node from the same component).

Returns:

mapping node id → component root id

Return type:

dict[int, int]

See also

connectedComponentsList

returns a dict[int, set[int]] grouping nodes by component

connectedComponentsCount

returns the number of components

connectedComponentsCount()

number of connected components

Returns:

the number of connected components in the graph.

Return type:

int

connectedComponentsList()

connected components as a dict of sets

Returns:

dict of connected components (as sets of nodeIds) keyed by an arbitrary root nodeId per component.

Return type:

dict(int, set[int])

contextualize(*args)

Return a new Bayesian network conditioned on observations and interventions.

Parameters:
  • observations (dict) – a dict mapping variable names (str) or ids (int) to observed values (str or int)

  • interventions (dict) – a dict mapping variable names (str) or ids (int) to intervened values (str or int)

Returns:

a new BayesNet representing the interventional/observational distribution

Return type:

pyagrum.BayesNet

Raises:

pyagrum.InvalidArgument – if arguments are not dicts

cpt(*args)

Returns the CPT of a variable.

Parameters:

VarId (int | str) – a variable’s id (int) or name

Returns:

The variable’s CPT.

Return type:

pyagrum.Tensor

Raises:

pyagrum.NotFound – If no variable’s id matches varId.

dag()
Returns:

a constant reference to the dag of this BayesNet.

Return type:

pyagrum.DAG

descendants(norid)

give the set of nodeid of descendants of a node

Parameters:

norid (str|int) – the name or the id of the node

Returns:

the set of ids of the descendants of node norid.

Return type:

set

dim()

Returns the dimension (the number of free parameters) in this BayesNet.

Returns:

the dimension of the BayesNet

Return type:

int

empty()

Check if there are some variables in the model.

Returns:

True if there is no variable in the model.

Return type:

bool

endTopologyTransformation()

Terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions. End Multiple Change for all CPTs.

Return type:

pyagrum.BayesNet

erase(*args)

Remove a variable from the pyagrum.BayesNet.

Removes the corresponding variable from the pyagrum.BayesNet and from all of it’s children pyagrum.Tensor.

If no variable matches the given id, then nothing is done.

Parameters:

var (int | str | pyagrum.DiscreteVariable) – the current name, the id of the variable or a reference to the variable

Return type:

None

eraseArc(*args)

Removes an arc in the BN, and update head’s CTP.

If (tail, head) doesn’t exist, the nothing happens.

Parameters:
  • arc (pyagrum.Arc when calling eraseArc(arc)) – The arc to be removed.

  • head (int | str) – a variable’s id (int) or name for the head when calling eraseArc(head,tail)

  • tail (int | str) – a variable’s id (int) or name for the tail when calling eraseArc(head,tail)

Return type:

None

evEq(name, value)

This method is used to set an observation on a quasi-continuous variables (pyagrum.DiscretizedVariable with a large number of ticks) that the variable is equal to a given value.

Notes

  • see also Tensor.evEq

  • see also BayesNet.evGt, BayesNet.evLt, BayesNet.evIn

Examples

>>> import pyagrum as gum
>>> bn = pyagrum.fastBN('A[0:10:20]'') # DiscretizedVariable from 0 to 10 in 20 steps
>>> print(bn.evEqu('A',5)
  A                                                                                                                                                                                                    |
[0;0.5[  |[0.5;1[  |[1;1.5[  |[1.5;2[  |[2;2.5[  |[2.5;3[  |[3;3.5[  |[3.5;4[  |[4;4.5[  |[4.5;5[  |[5;5.5[  |[5.5;6[  |[6;6.5[  |[6.5;7[  |[7;7.5[  |[7.5;8[  |[8;8.5[  |[8.5;9[  |[9;9.5[  |[9.5;10] |
---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|
 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 1.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  |
Parameters:
  • var (int | str) – the current name or the id of the variable

  • value (float)

  • name (str)

Returns:

The representation of the evidence as a pyagrum.Tensor.

Return type:

pyagrum.Tensor

evGt(name, value)

This method is used to set an observation on a quasi-continuous variables (pyagrum.DiscretizedVariable with a large number of ticks) that the variable is greater than a given value.

Notes

  • see also Tensor.evGt

  • see also BayesNet.evEq, BayesNet.evLt, BayesNet.evIn

Examples

>>> import pyagrum as gum
>>> bn = pyagrum.fastBN('A[0:10:20]'') # DiscretizedVariable from 0 to 10 in 20 steps
>>> print(bn.evGt('A',5)
  A                                                                                                                                                                                                    |
[0;0.5[  |[0.5;1[  |[1;1.5[  |[1.5;2[  |[2;2.5[  |[2.5;3[  |[3;3.5[  |[3.5;4[  |[4;4.5[  |[4.5;5[  |[5;5.5[  |[5.5;6[  |[6;6.5[  |[6.5;7[  |[7;7.5[  |[7.5;8[  |[8;8.5[  |[8.5;9[  |[9;9.5[  |[9.5;10] |
---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|---------|
 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 0.0000  | 1.0000  | 1.0000  | 1.0000  | 1.0000  | 1.0000  | 1.0000  | 1.0000  | 1.0000  | 1.0000  | 1.0000  |
Parameters:
  • var (int | str) – the current name or the id of the variable

  • value (float)

  • name (str)

Returns:

The representation of the evidence as a pyagrum.Tensor.

Return type:

pyagrum.Tensor

evIn(name, val1, val2)

This method is used to set an observation on a quasi-continuous variables (pyagrum.DiscretizedVariable with a large number of ticks) that the variable is in a given range.

Notes

  • see also Tensor.evIn

  • see also BayesNet.evEq, BayesNet.evGt, BayesNet.evLt

Parameters:
  • var (int | str) – the current name or the id of the variable

  • valueMin (float) – the minimum value

  • valueMax (float) – the maximum value

  • name (str)

  • val1 (float)

  • val2 (float)

Returns:

The representation of the evidence as a pyagrum.Tensor.

Return type:

pyagrum.Tensor

evLt(name, value)

This method is used to set an observation on a quasi-continuous variables (pyagrum.DiscretizedVariable with a large number of ticks) that the variable is less than a given value.

Notes

  • see also Tensor.evLt

  • see also BayesNet.evEq, BayesNet.evGt, BayesNet.evIn

Parameters:
  • var (int | str) – the current name or the id of the variable

  • value (float)

  • name (str)

Returns:

The representation of the evidence as a pyagrum.Tensor.

Return type:

pyagrum.Tensor

exists(*args)

Check if a node with this name or id exists

Parameters:

norid (str|int) – name or id of the searched node

Returns:

True if there is a node with such a name or id

Return type:

bool

existsArc(*args)

Check if an arc exists

Parameters:
  • tail (str|int) – the name or id of the tail of the arc

  • head (str|int) – the name or the id of the head of the arc

Returns:

True if tail->head is an arc.

Return type:

bool

existsProperty(name)

Check whether a property key exists in the model’s metadata.

Parameters:

name (str) – the property name

Returns:

True if the property exists

Return type:

bool

family(norid)

give the set of parents of a node and the node

Parameters:

norid (str|int) – the node

Returns:

the set of nodeId of the family of the node norid

Return type:

set

static fastPrototype(*args)
Create a Bayesian network with a dot-like syntax which specifies:
  • the structure ‘a->b->c;b->d<-e;’.

  • the type of the variables with different syntax:

    • by default, a variable is a binary pyagrum.RangeVariable using the default domain size ([2])

    • with ‘a[10]’, the variable is a pyagrum.RangeVariable using 10 as domain size (from 0 to 9)

    • with ‘a[3,7]’, the variable is a pyagrum.RangeVariable using a domainSize from 3 to 7

    • with ‘a[1,3.14,5,6.2]’, the variable is a pyagrum.DiscretizedVariable using the given ticks (at least 3 values)

    • with ‘a[0.0:3.14:10]’, the variable is a pyagrum.DiscretizedVariable of 10 intervals of same width from 0 to 3.14 (including both)

    • with ‘a{top|middle|bottom}’, the variable is a pyagrum.LabelizedVariable using the given labels.

    • with ‘a{-1|5|0|3}’, the variable is a pyagrum.IntegerVariable using the sorted given values.

    • with ‘a{-0.5|5.01|0|3.1415}’, the variable is a pyagrum.NumericalDiscreteVariable using the sorted given values.

Notes

  • If the dot-like string contains such a specification more than once for a variable, the first specification will be used.

  • the CPTs are randomly generated.

  • see also the function pyagrum.fastBN.

Examples

>>> import pyagrum as gum
>>> bn=pyagrum.BayesNet.fastPrototype('A->B[1,3]<-C{yes|No}->D[2,4]<-E[1,2.5,3.9]',6)
Parameters:
  • dotlike (str) – the string containing the specification

  • domainSize (int or str) – the default domain size or the default domain for variables

Returns:

the resulting Bayesian network

Return type:

pyagrum.BayesNet

generateCPT(*args)

Randomly generate CPT for a given node in a given structure.

Parameters:

node (int | str) – a variable’s id (int) or name

Return type:

None

generateCPTs()

Randomly generates CPTs for a given structure.

Return type:

None

hasSameStructure(other)
Parameters:

pyagrum.DAGmodel – a direct acyclic model

Returns:

True if all the named node are the same and all the named arcs are the same

Return type:

bool

idFromName(name)

Return the node id of a variable given its name.

Parameters:

name (str) – the name of the variable

Returns:

the node id of the variable

Return type:

int

Raises:

pyagrum.NotFound – if no variable with this name exists in the model

ids(names)

List of ids for a list of names of variables in the model

Parameters:
  • lov (list of str) – List of variable names

  • names (tuple[str, ...])

Returns:

The ids for the list of names of the graph variables

Return type:

list of int

isIndependent(*args)

check if nodes X and nodes Y are independent given nodes Z

Parameters:
  • X (str|int|list of str|int) – a list of of nodeIds or names

  • Y (str|int|list of str|int) – a list of of nodeIds or names

  • Z (str|int|list of str|int) – a list of of nodeIds or names

Raises:

InvalidArgument – if X and Y share variables

Returns:

True if X and Y are independent given Z in the model

Return type:

bool

jointProbability(i)
Parameters:

i (pyagrum.instantiation) – an instantiation of the variables

Returns:

a parameter of the joint probability for the BayesNet

Return type:

float

Warning

a variable not present in the instantiation is assumed to be instantiated to 0

loadBIF(name, l=None)

Load a BIF file.

Parameters:
  • name (str) – the file’s name

  • l (list) – list of functions to execute

Raises:
Return type:

str

loadBIFXML(name, l=None)

Load a BIFXML file.

Parameters:
  • name (str) – the name’s file

  • l (list) – list of functions to execute

Raises:
Return type:

str

loadDSL(name, l=None)

Load a DSL file.

Parameters:
  • name (str) – the file’s name

  • l (list) – list of functions to execute

Raises:
Return type:

str

loadGUM(name, l=None, binary=False)

Load a jgum (JSON) or bgum (binary/msgpack) file.

Parameters:
  • name (str) – the file’s path (extension: .jgum for JSON, .bgum for binary)

  • binary (bool) – if True, read as bgum (msgpack) regardless of extension (default: False)

Raises:
Return type:

None

See also

JGUM / BGUM Format Reference

complete format reference

Parameters:

l (Optional[object])

loadGUMstring(content)

Deserialize a BayesNet from a jgum JSON string.

Parameters:

content (str) – a JSON string in jgum format

Raises:

pyagrum.FatalError – If the string is not valid jgum JSON or the type field does not match "BN"

Return type:

None

See also

JGUM / BGUM Format Reference

complete format reference

loadNET(name, l=None)

Load a NET file.

Parameters:
  • name (str) – the name’s file

  • l (list) – list of functions to execute

Raises:
Return type:

str

loadO3PRM(*args)

Load an O3PRM file.

Warning

The O3PRM language is the only language allowing to manipulate not only DiscretizedVariable but also RangeVariable and LabelizedVariable.

Parameters:
  • name (str) – the file’s name

  • system (str) – the system’s name

  • classpath (str) – the classpath

  • l (list) – list of functions to execute

Raises:
Return type:

str

loadUAI(name, l=None)

Load an UAI file.

Parameters:
  • name (str) – the name’s file

  • l (list) – list of functions to execute

Raises:
Return type:

str

loadXDSL(name, l=None)

Load a XDSL file.

Parameters:
  • name (str) – the file’s name

  • l (list) – list of functions to execute

Raises:
Return type:

str

log10DomainSize()

returns the log10 of the domain size of the model defined as the product of the domain sizes of the variables in the model.

Returns:

the log10 domain size.

Return type:

float

log2JointProbability(i)
Parameters:

i (pyagrum.instantiation) – an instantiation of the variables

Returns:

a parameter of the log joint probability for the BayesNet

Return type:

float

Warning

a variable not present in the instantiation is assumed to be instantiated to 0

maxNonOneParam()
Returns:

The biggest value (not equal to 1) in the CPTs of the BayesNet

Return type:

float

maxParam()
Returns:

the biggest value in the CPTs of the BayesNet

Return type:

float

maxVarDomainSize()
Returns:

the biggest domain size among the variables of the BayesNet

Return type:

int

memoryFootprint()

Returns the memory footprint in bytes of the Bayesian network.

Returns:

the memory footprint in bytes of the Bayesian network

Return type:

int

minNonZeroParam()
Returns:

the smallest value (not equal to 0) in the CPTs of the IBayesNet

Return type:

float

minParam()
Returns:

the smallest value in the CPTs of the IBayesNet

Return type:

float

minimalCondSet(*args)

Returns, given one or many targets and a list of variables, the minimal set of those needed to calculate the target/targets.

Parameters:
  • target (int) – The id of the target

  • targets (list of int) – The ids of the targets

  • list (list of int) – The list of available variables

Returns:

The minimal set of variables

Return type:

set

moralGraph()

Returns the moral graph of the BayesNet, formed by adding edges between all pairs of nodes that have a common child, and then making all edges in the graph undirected.

Returns:

The moral graph

Return type:

pyagrum.UndiGraph

moralizedAncestralGraph(nodes)

build a UndiGraph by moralizing the Ancestral Graph of a list of nodes

Parameters:

nodes (str|int|list of str|int) – the list of of nodeIds or names

Warning

pyagrum.UndiGraph only knows NodeId. Hence the moralized ancestral graph does not include the names of the variables.graph

Returns:

the moralized ancestral graph of the nodes

Return type:

pyagrum.UndiGraph

names()

Set of names of variables in the model

Returns:

The names of the graph variables

Return type:

set

nodeId(var)

Return the node id of a variable.

Parameters:

var (pyagrum.DiscreteVariable) – the variable

Returns:

the node id of the variable

Return type:

int

Raises:

pyagrum.NotFound – if the variable does not exist in the model

nodes()
Returns:

the set of ids

Return type:

set

nodeset(names)

Set of ids for a list of names of variables in the model

Parameters:
  • lov (list of str) – List of variable names

  • names (tuple[str, ...])

Returns:

The set of ids for the list of names of the graph variables

Return type:

set

parents(norid)
Parameters:
  • id – The id of the child node

  • norid (object)

Returns:

the set of the parents ids.

Return type:

Set

properties()

Return the keys of all metadata properties of the model.

Returns:

tuple of property names (use property() to retrieve a value by key)

Return type:

tuple[str, …]

property(name)

Returns the value associated to this property.

Properties are a way to keep some (name,value) together with de model.

Parameters:

name (str) – the name of the property

Raises:

NotFound – if no name property is found

Returns:

The value associated to this name

Return type:

str

propertyWithDefault(name, byDefault)

Returns the value associated to this property or the default value if there is no such property.

Properties are a way to keep some information (name,value) together with de model.

Parameters:
  • name (str) – the name of the property

  • byDefault (str) – the value by default if no property has been found.

Returns:

The value associated to this name or the value by default.

Return type:

str

reverseArc(*args)

Reverses an arc while preserving the same joint distribution.

Parameters:
  • tail – (int) the id of the tail variable

  • head – (int) the id of the head variable

  • tail – (str) the name of the tail variable

  • head – (str) the name of the head variable

  • arc (pyagrum.Arc) – an arc

Raises:

pyagrum.InvalidArc – If the arc does not exist or if its reversal would induce a directed cycle.

Return type:

None

saveBIF(name, allowModificationWhenSaving=False)

Save the BayesNet in a BIF file.

Parameters:
  • name (str) – the file’s name

  • allowModificationWhenSaving (bool) – False by default. if true, syntax errors are corrected when saving the file. If false, they throw a FatalError.

Return type:

None

saveBIFXML(name, allowModificationWhenSaving=False)

Save the BayesNet in a BIFXML file.

Parameters:
  • name (str) – the file’s name

  • allowModificationWhenSaving (bool) – False by default. if true, syntax errors are corrected when saving the file. If false, they throw a FatalError.

Return type:

None

saveDSL(name, allowModificationWhenSaving=False)

Save the BayesNet in a DSL file.

Parameters:
  • name (str) – the file’s name

  • allowModificationWhenSaving (bool) – False by default. if true, syntax errors are corrected when saving the file. If false, they throw a FatalError.

Return type:

None

saveGUM(name, binary=False, indent=2)

Save the BayesNet in a jgum (JSON) or bgum (binary/msgpack) file.

Metadata properties (software, creation, lastModification) are updated automatically.

Parameters:
  • name (str) – the file’s path

  • binary (bool) – if True, write as bgum (msgpack); otherwise write as jgum (JSON) (default: False)

  • indent (int) – indentation level for JSON output; -1 for compact, 2 for pretty-printed (default: 2)

Return type:

None

See also

JGUM / BGUM Format Reference

complete format reference

saveGUMstring(indent=2)

Serialize the BayesNet to a jgum JSON string.

Metadata properties (software, creation, lastModification) are updated automatically.

Parameters:

indent (int) – indentation level; -1 for compact, 2 for pretty-printed (default: 2)

Returns:

a JSON string representing the BayesNet in jgum format

Return type:

str

See also

JGUM / BGUM Format Reference

complete format reference

saveNET(name, allowModificationWhenSaving=False)

Save the BayesNet in a NET file.

Parameters:
  • name (str) – the file’s name

  • allowModificationWhenSaving (bool) – False by default. if true, syntax errors are corrected when saving the file. If false, they throw a FatalError.

Return type:

None

saveO3PRM(name, allowModificationWhenSaving=False)

Save the BayesNet in an O3PRM file.

Warning

The O3PRM language is the only language allowing to manipulate not only DiscretizedVariable but also RangeVariable and LabelizedVariable.

Parameters:
  • name (str) – the file’s name

  • allowModificationWhenSaving (bool) – False by default. if true, syntax errors are corrected when saving the file. If false, they throw a FatalError.

Return type:

None

saveUAI(name, allowModificationWhenSaving=False)

Save the BayesNet in an UAI file.

Parameters:
  • name (str) – the file’s name

  • allowModificationWhenSaving (bool) – False by default. if true, syntax errors are corrected when saving the file. If false, they throw a FatalError.

Return type:

None

saveXDSL(name, allowModificationWhenSaving=False)

Save the BayesNet in a XDSL file.

Parameters:
  • name (str) – the file’s name

  • allowModificationWhenSaving (bool) – (not used). if true, syntax errors are corrected when saving the file. If false, they throw a FatalError.

Return type:

None

setProperty(name, value)

Create or change the couple (name,value) in the properties.

Properties are a way to keep some information (name,value) together with de model.

Parameters:
  • name (str) – the name of the property

  • value (str) – the value of the property.

Return type:

None

size()
Returns:

the number of nodes in the graph

Return type:

int

sizeArcs()
Returns:

the number of arcs in the graph

Return type:

int

static spaceCplxToString(dSize, dim, usedMem)

Return a human-readable string summarising the space complexity of a graphical model.

Parameters:
  • dSize (float) – log10 of the joint domain size

  • dim (int) – number of independent parameters

  • usedMem (int) – memory footprint in bytes

Returns:

a string of the form 'domainSize: X, dim: Y, mem: Z'

Return type:

str

toDot()
Returns:

a friendly display of the graph in DOT format

Return type:

str

toFast(filename=None)

Export the Bayesian network as fast syntax (in a string or in a python file)

Parameters:

filename (Optional[str]) – the name of the file (including the prefix), if None , use sys.stdout

Return type:

str

topologicalOrder()
Returns:

the list of the nodes Ids in a topological order

Return type:

List

Raises:

pyagrum.InvalidDirectedCycle – If this graph contains cycles

updateMetaData()

Update the model’s built-in metadata (version, creation date, last modification date).

This method is called automatically by writers before saving the model to a file.

Return type:

None

variable(*args)
Parameters:
  • id (int) – a variable’s id

  • name (str) – a variable’s name

Returns:

the variable

Return type:

pyagrum.DiscreteVariable

Raises:

pyagrum.IndexError – If the graph does not contain the variable

variableFromName(name)

Return the variable with the given name.

Parameters:

name (str) – the name of the variable

Returns:

the variable

Return type:

pyagrum.DiscreteVariable

Raises:

pyagrum.NotFound – if no variable with this name exists in the model

variableNodeMap()

Return the variable-to-node mapping of the model.

Returns:

the internal variable-to-node bijection

Return type:

pyagrum.VariableNodeMap

variables(*args)

Return the set of variables corresponding to a list of names or a set of node ids.

Parameters:

args (list[str] or set[int]) – variable names or node ids

Returns:

the set of corresponding variables

Return type:

pyagrum.VariableSet