class pyAgrum.MarkovNet(*args)

MarkovNet represents a Markov Network.

MarkovNet(name=’‘) -> MarkovNet
  • name (str) – the name of the Bayes Net
MarkovNet(source) -> MarkovNet
  • source (pyAgrum.MarkovNet) – the Markov network to copy
add(MarkovNet self, DiscreteVariable var)

add(MarkovNet self, str name, unsigned int nbrmod) -> int add(MarkovNet self, DiscreteVariable var, int id) -> int

Add a variable to the pyAgrum.MarkovNet.

  • variable (pyAgrum.DiscreteVariable) – the variable added
  • name (str) – the variable name
  • nbrmod (int) – the number of modalities for the new variable
  • id (int) – the variable forced id in the pyAgrum.MarkovNet

the id of the new node

Return type:


  • gum.DuplicateLabel – If is already used in this pyAgrum.MarkovNet.
  • gum.NotAllowed – If nbrmod is less than 2
  • gum.DuplicateElement – If id is already used.
addFactor(MarkovNet self, Vector_string varnames)

addFactor(MarkovNet self, Set vars) -> Potential addFactor(MarkovNet self, Potential factor) -> Potential addFactor(MarkovNet self, PyObject * seq) -> Potential

addStructureListener(whenNodeAdded=None, whenNodeDeleted=None, whenEdgeAdded=None, whenedgeDeleted=None)

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

  • whenNodeAdded (lambda expression) – a function for when a node is added
  • whenNodeDeleted (lambda expression) – a function for when a node is removed
  • whenEdgeAdded (lambda expression) – a function for when an edge is added
  • whenEdgeDeleted (lambda expression) – a function for when an edge is removed
beginTopologyTransformation(MarkovNet self)

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.

changeVariableLabel(MarkovNet self, int id, str old_label, str new_label)

changeVariableLabel(MarkovNet self, str name, str old_label, str new_label)

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

  • id (int) – the id of the node
  • name (str) – the name of the variable
  • old_label (str) – the new label
  • new_label (str) – the new label

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

changeVariableName(MarkovNet self, int id, str new_name)

changeVariableName(MarkovNet self, str name, str new_name)

Changes a variable’s name in the pyAgrum.MarkovNet.

This will change the pyAgrum.DiscreteVariable names in the pyAgrum.MarkovNet.

  • new_name (str) – the new name of the variable
  • NodeId (int) – the id of the node
  • name (str) – the name of the variable
  • gum.DuplicateLabel – If new_name is already used in this MarkovNet.
  • gum.NotFound – If no variable matches id.
clear(MarkovNet self)

Clear the whole MarkovNet

completeInstantiation(GraphicalModel self)

connected components from a graph/BN

Compute the connected components of a pyAgrum’s graph or Bayesian Network (more generally an object that has nodes, children/parents or neighbours methods)

The firstly visited node for each component is called a ‘root’ and is used as a key for the component. This root has been arbitrarily chosen during the algorithm.

Returns:dict of connected components (as set of nodeIds (int)) with a nodeId (root) of each component as key.
Return type:dict(int,Set[int])
dim(IMarkovNet self)
edges(MarkovNet self)
empty(GraphicalModel self)
endTopologyTransformation(MarkovNet self)

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

Return type:pyAgrum.MarkovNet
erase(MarkovNet self, int varId)

erase(MarkovNet self, str name) erase(MarkovNet self, DiscreteVariable var)

Remove a variable from the pyAgrum.MarkovNet.

Removes the corresponding variable from the pyAgrum.MarkovNet and from all of it’s children pyAgrum.Potential.

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

  • id (int) – The variable’s id to remove.
  • name (str) – The variable’s name to remove.
  • var (pyAgrum.DiscreteVariable) – A reference on the variable to remove.
eraseFactor(MarkovNet self, Set vars)

eraseFactor(MarkovNet self, Vector_string varnames) eraseFactor(MarkovNet self, PyObject * seq)

existsEdge(UGmodel self, int node1, int node2)

existsEdge(UGmodel self, str name1, str name2) -> bool

factor(MarkovNet self, Set varIds)

factor(MarkovNet self, Vector_string varnames) -> Potential factor(MarkovNet self, PyObject * nodeseq) -> Potential

Returns the factor of a set of variables (if existing).

  • VarId (Set[int]) – A variable’s id in the pyAgrum.MarkovNet.
  • name (Set[str]) – A variable’s name in the pyAgrum.MarkovNet.

The factor of the set of nodes.

Return type:



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

factors(MarkovNet self)
static fastPrototype(str dotlike, int domainSize=2)
Create a Markov network with a dot-like syntax which specifies:
  • the structure ‘a-b-c;b-d-e;’. The substring ‘a-b-c’ indicates a factor with the scope (a,b,c).
  • the type of the variables with different syntax:
    • by default, a variable is a gum.RangeVariable using the default domain size (second argument)
    • with ‘a[10]’, the variable is a gum.RangeVariable using 10 as domain size (from 0 to 9)
    • with ‘a[3,7]’, the variable is a gum.RangeVariable using a domainSize from 3 to 7
    • with ‘a[1,3.14,5,6.2]’, the variable is a gum.DiscretizedVariable using the given ticks (at least 3 values)
    • with ‘a{top|middle|bottom}’, the variable is a gum.LabelizedVariable using the given labels.


  • 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 pyAgrum.fastBN.


>>> import pyAgrum as gum
>>> bn=gum.MarkovNet.fastPrototype('A-B[1,3]-C{yes|No}-D[2,4]-E[1,2.5,3.9]',6)
  • dotlike (str) – the string containing the specification
  • domainSize (int) – the default domain size for variables

the resulting Markov network

Return type:


static fromBN(BayesNet bn)
generateFactor(MarkovNet self, Set vars)

Randomly generate factor parameters for a given factor in a given structure.

  • node (int) – The variable’s id.
  • name (str) – The variable’s name.
generateFactors(MarkovNet self)

Randomly generates factors parameters for a given structure.

graph(MarkovNet self)
hasSameStructure(UGmodel self, UGmodel other)
idFromName(MarkovNet self, str name)
loadUAI(MarkovNet self, str name, PyObject * l=(PyObject *) 0)

Load an UAI file.

  • name (str) – the name’s file
  • l (list) – list of functions to execute
  • gum.IOError – If file not found
  • gum.FatalError – If file is not valid
log10DomainSize(MarkovNet self)
maxNonOneParam(IMarkovNet self)
maxParam(IMarkovNet self)
maxVarDomainSize(IMarkovNet self)
minNonZeroParam(IMarkovNet self)
minParam(IMarkovNet self)
minimalCondSet(MarkovNet self, int target, PyObject * list)

minimalCondSet(MarkovNet self, PyObject * targets, PyObject * list) -> PyObject *

names(MarkovNet self)
neighbours(MarkovNet self, PyObject * norid)
nodeId(MarkovNet self, DiscreteVariable var)
nodes(MarkovNet self)
property(GraphicalModel self, str name)
propertyWithDefault(GraphicalModel self, str name, str byDefault)
saveUAI(MarkovNet self, str name)

Save the MarkovNet in an UAI file.

Parameters:name (str) – the file’s name
setProperty(GraphicalModel self, str name, str value)
size(MarkovNet self)
sizeEdges(UGmodel self)
smallestFactorFromNode(MarkovNet self, int node)
toDot(IMarkovNet self)
toDotAsFactorGraph(IMarkovNet self)
variable(MarkovNet self, int id)

variable(MarkovNet self, str name) -> DiscreteVariable

variableFromName(MarkovNet self, str name)
variableNodeMap(MarkovNet self)