Model

class pyAgrum.MarkovNet(*args)

MarkovNet represents a Markov Network.

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

add(self, name, nbrmod) -> int add(self, var, id) -> int

Add a variable to the pyAgrum.MarkovNet.

Parameters:
  • 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
Returns:

the id of the new node

Return type:

int

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

addFactor(self, vars) -> Potential addFactor(self, factor) -> Potential addFactor(self, seq) -> Potential

addStructureListener(whenNodeAdded=None, whenNodeDeleted=None, whenEdgeAdded=None, whenedgeDeleted=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
  • whenEdgeAdded (lambda expression) – a function for when an edge is added
  • whenEdgeDeleted (lambda expression) – a function for when an edge is removed
beginTopologyTransformation(self)
changeVariableLabel(self, id, old_label, new_label)

changeVariableLabel(self, name, old_label, new_label)

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

Parameters:
  • 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
Raises:

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

changeVariableName(self, id, new_name)

changeVariableName(self, name, new_name)

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

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

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

Clear the whole MarkovNet

completeInstantiation(self)
connectedComponents()

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(self)
edges(self)
empty(self)
endTopologyTransformation(self)

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

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

erase(self, name) erase(self, 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.

Parameters:
  • 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(self, vars)

eraseFactor(self, varnames) eraseFactor(self, seq)

exists(self, node)
existsEdge(self, node1, node2)

existsEdge(self, name1, name2) -> bool

factor(self, varIds)

factor(self, varnames) -> Potential factor(self, nodeseq) -> Potential

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

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

The factor of the set of nodes.

Return type:

pyAgrum.Potential

Raises:

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

factors(self)
static fastPrototype(dotlike, domainSize=2)
Create a Markov network with a modified 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 (cf documentation).

Examples

>>> 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)
Parameters:
  • dotlike (str) – the string containing the specification
  • domainSize (int) – the default domain size for variables
Returns:

the resulting Markov network

Return type:

pyAgrum.MarkovNet

static fromBN(bn)
generateFactor(self, vars)

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

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

Randomly generates factors parameters for a given structure.

graph(self)
hasSameStructure(self, other)
idFromName(self, name)
ids(self, names)
isIndependent(self, X, Y, Z)

isIndependent(self, X, Y) -> bool

loadUAI(self, name, l=(PyObject *) 0)

Load an UAI file.

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

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

names(self)
neighbours(self, norid)
nodeId(self, var)
nodes(self)
nodeset(self, names)
property(self, name)
propertyWithDefault(self, name, byDefault)
saveUAI(self, name)

Save the MarkovNet in an UAI file.

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

variable(self, name) -> DiscreteVariable

variableFromName(self, name)
variableNodeMap(self)