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(*args)¶
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
pyAgrum.DuplicateLabel – If variable.name() is already used in this pyAgrum.MarkovNet.
pyAgrum.NotAllowed – If nbrmod is less than 2
pyAgrum.DuplicateElement – If id is already used.
- addFactor(*args)¶
Add a factor from a list or a set of id or str. If the argument is a set, the order is the order of the IDs of the variables
- Parameters
seq (sequence (list or set) of int or string) – The sequence (ordered or not) of node id or names
- Return type
- 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()¶
- Return type
None
- changeVariableLabel(*args)¶
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
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.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
pyAgrum.DuplicateLabel – If new_name is already used in this MarkovNet.
pyAgrum.NotFound – If no variable matches id.
- Return type
None
- clear()¶
Clear the whole MarkovNet
- Return type
None
- completeInstantiation()¶
- Return type
- 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()¶
- Return type
int
- edges()¶
- Return type
object
- empty()¶
- 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
- erase(*args)¶
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.
- Return type
None
- eraseFactor(*args)¶
- Return type
None
- exists(node)¶
- Parameters
node (
int
) –- Return type
bool
- existsEdge(*args)¶
- Return type
bool
- factor(*args)¶
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
- Raises
pyAgrum.NotFound – If no variable’s id matches varId.
- factors()¶
- Return type
List
[Set
[int
]]
- 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 substringa-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=pyAgrum.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
- generateFactor(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.
vars (
List
[int
]) –
- Return type
None
- generateFactors()¶
Randomly generates factors parameters for a given structure.
- Return type
None
- hasSameStructure(other)¶
- idFromName(name)¶
- Parameters
name (
str
) –- Return type
int
- ids(names)¶
- isIndependent(*args)¶
- Return type
bool
- loadUAI(*args)¶
Load an UAI file.
- Parameters
name (str) – the name’s file
l (list) – list of functions to execute
- Raises
pyAgrum.IOError – If file not found
pyAgrum.FatalError – If file is not valid
- Return type
str
- log10DomainSize()¶
- Return type
float
- maxNonOneParam()¶
- Return type
float
- maxParam()¶
- Return type
float
- maxVarDomainSize()¶
- Return type
int
- minNonZeroParam()¶
- Return type
float
- minParam()¶
- Return type
float
- minimalCondSet(*args)¶
- Return type
object
- names()¶
- Returns
The names of the graph variables
- Return type
List[str]
- neighbours(norid)¶
- Parameters
norid (
object
) –- Return type
object
- nodeId(var)¶
- Parameters
var (
DiscreteVariable
) –- Return type
int
- nodes()¶
- Return type
object
- nodeset(names)¶
- Parameters
names (
Vector_string
) –- Return type
List
[int
]
- saveUAI(name)¶
Save the MarkovNet in an UAI file.
- Parameters
name (str) – the file’s name
- Return type
None
- size()¶
- Return type
int
- sizeEdges()¶
- Return type
int
- smallestFactorFromNode(node)¶
- Parameters
node (
int
) –- Return type
List
[int
]
- property thisown¶
The membership flag
- toDot()¶
- Return type
str
- toDotAsFactorGraph()¶
- Return type
str
- variable(*args)¶
- Return type
- variableFromName(name)¶
- Parameters
name (
str
) –- Return type
- variableNodeMap()¶