Undirected Graphical Model
class pyagrum.MarkovRandomField(*args)
Section titled “class pyagrum.MarkovRandomField(*args)”MarkovRandomField represents a Markov random field.
MarkovRandomField(name=’’) -> MarkovRandomField : Parameters: : - name (str) – the name of the Bayes Net
MarkovRandomField(source) -> MarkovRandomField : Parameters: : - source (pyagrum.MarkovRandomField) – the Markov random field to copy
add(*args)
Section titled “add(*args)”Add a variable to the pyagrum.MarkovRandomField.
- 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.MarkovRandomField
- Returns: the id of the new node
- Return type: int
- Raises:
- pyagrum.DuplicateLabel – If variable.name() is already used in this pyagrum.MarkovRandomField.
- pyagrum.OperationNotAllowed – If nbrmod is less than 2
- pyagrum.DuplicateElement – If id is already used.
addFactor(*args)
Section titled “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:
Tensor
addStructureListener(whenNodeAdded=None, whenNodeDeleted=None, whenEdgeAdded=None, whenedgeDeleted=None)
Section titled “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
addVariables(listFastVariables, default_nbr_mod=2)
Section titled “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]
adjacencyMatrix()
Section titled “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
beginTopologyTransformation()
Section titled “beginTopologyTransformation()”- Return type:
None
changeVariableLabel(*args)
Section titled “changeVariableLabel(*args)”change the label of the variable associated to nodeId to the new value.
- Parameters:
- var (Union [**int ,**str ]) – a variable’s id (int) or name
- old_label (str) – the old 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)
Section titled “changeVariableName(*args)”Changes a variable’s name in the pyagrum.MarkovRandomField.
This will change the “pyagrum.DiscreteVariable” names in the pyagrum.MarkovRandomField.
- Parameters:
- car (Union [**int ,**str ]) – a variable’s id (int) or name
- new_name (str) – the new name of the variable
- Raises:
- pyagrum.DuplicateLabel – If new_name is already used in this MarkovRandomField.
- pyagrum.NotFound – If no variable matches id.
- Return type:
None
clear()
Section titled “clear()”Clear the whole MarkovRandomField
- Return type:
None
completeInstantiation()
Section titled “completeInstantiation()”Give an instantiation over all the variables of the model
- Returns: a complete Instantiation for the model
- Return type: pyagrum.Instantiation
connectedComponents()
Section titled “connectedComponents()”connected components from a graph/graphical models
Compute the connected components of a pyAgrum’s graph or graphical models (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])
- Return type:
int
edges()
Section titled “edges()”- Return type:
object
empty()
Section titled “empty()”Check if there are some variables in the model.
- Returns: True if there is no variable in the model.
- Return type: bool
endTopologyTransformation()
Section titled “endTopologyTransformation()”Terminates a sequence of insertions/deletions of arcs by adjusting all CPTs dimensions. End Multiple Change for all CPTs.
- Return type: pyagrum.MarkovRandomField
erase(*args)
Section titled “erase(*args)”Remove a variable from the pyagrum.MarkovRandomField.
Removes the corresponding variable from the pyagrum.MarkovRandomField and from all of it’s children “pyagrum.Tensor”.
If no variable matches the given id, then nothing is done.
- Parameters: var (Union [**int ,**str ,pyagrum.DiscreteVariable ]) – a variable’s id (int) or name of variable or a reference of this variable to remove.
- Return type:
None
eraseFactor(*args)
Section titled “eraseFactor(*args)”- Return type:
None
exists(*args)
Section titled “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
existsEdge(*args)
Section titled “existsEdge(*args)”- Return type:
bool
factor(*args)
Section titled “factor(*args)”Returns the factor of a set of variables (if existing).
- Parameters: vars (Union [**Set [**int ] ,**Set [**str ] ]) – A set of ids or names of variable the pyagrum.MarkovRandomField.
- Returns: The factor of the set of nodes.
- Return type: pyagrum.Tensor
- Raises: pyagrum.NotFound – If no variable’s id matches varId.
factors()
Section titled “factors()”- Return type:
list[set[int]]
family(*args)
Section titled “family(*args)”- Return type:
list[int]
static fastPrototype(*args)
Section titled “static fastPrototype(*args)”Create a Markov random field 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=pyagrum.MarkovRandomField.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 Markov random field
- Return type: pyagrum.MarkovRandomField
static fromBN(bn)
Section titled “static fromBN(bn)”- Parameters:
bn (
BayesNet) - Return type:
MarkovRandomField
generateFactor(vars)
Section titled “generateFactor(vars)”Randomly generate factor parameters for a given factor in a given structure.
- Parameters:
- node (Union [**int ,**str ]) – a variable’s id (int) or name
- vars (
list[int])
- Return type:
None
generateFactors()
Section titled “generateFactors()”Randomly generates factors parameters for a given structure.
- Return type:
None
graph()
Section titled “graph()”- Return type:
UndiGraph
hasSameStructure(other)
Section titled “hasSameStructure(other)”idFromName(name)
Section titled “idFromName(name)”- Parameters:
name (
str) - Return type:
int
ids(names)
Section titled “ids(names)”List of ids for a list of names of variables in the model
- Parameters:
- lov (List [**str ]) – List of variable names
- names (
list[str])
- Returns: The ids for the list of names of the graph variables
- Return type: List[int]
isIndependent(*args)
Section titled “isIndependent(*args)”check if nodes X and nodes Y are independent given nodes Z
- Parameters:
- X (str |**intList [**str |**int ]) – a list of of nodeIds or names
- Y (str |**intList [**str |**int ]) – a list of of nodeIds or names
- Z (str |**intList [**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
loadUAI(*args)
Section titled “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()
Section titled “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
maxNonOneParam()
Section titled “maxNonOneParam()”- Return type:
float
maxParam()
Section titled “maxParam()”- Return type:
float
maxVarDomainSize()
Section titled “maxVarDomainSize()”- Return type:
int
minNonZeroParam()
Section titled “minNonZeroParam()”- Return type:
float
minParam()
Section titled “minParam()”- Return type:
float
minimalCondSet(*args)
Section titled “minimalCondSet(*args)”- Return type:
object
names()
Section titled “names()”Set of names of variables in the model
- Returns: The names of the graph variables
- Return type: Set[str]
neighbours(norid)
Section titled “neighbours(norid)”- Parameters:
norid (
object) - Return type:
object
nodeId(var)
Section titled “nodeId(var)”- Parameters:
var (
DiscreteVariable) - Return type:
int
nodes()
Section titled “nodes()”- Return type:
object
nodeset(names)
Section titled “nodeset(names)”Set of ids for a list of names of variables in the model
- Parameters:
- lov (List [**str ]) – List of variable names
- names (
list[str])
- Returns: The set of ids for the list of names of the graph variables
- Return type: Set[int]
properties()
Section titled “properties()”- Return type:
list[str]
saveUAI(name)
Section titled “saveUAI(name)”Save the MarkovRandomField in an UAI file.
- Parameters: name (str) – the file’s name
- Return type:
None
size()
Section titled “size()”- Return type:
int
sizeEdges()
Section titled “sizeEdges()”- Return type:
int
smallestFactorFromNode(node)
Section titled “smallestFactorFromNode(node)”- Parameters:
node (
int) - Return type:
list[int]
property thisown
Section titled “property thisown”The membership flag
toDot()
Section titled “toDot()”- Return type:
str
toDotAsFactorGraph()
Section titled “toDotAsFactorGraph()”- Return type:
str
toFast(filename=None)
Section titled “toFast(filename=None)”Export the MRF 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
variable(*args)
Section titled “variable(*args)”- Return type:
DiscreteVariable
variableFromName(name)
Section titled “variableFromName(name)”- Parameters:
name (
str) - Return type:
DiscreteVariable