gstools.field.PGS
- class gstools.field.PGS(dim, fields)[source]
Bases:
objectA class to generate plurigaussian field simulations (PGS).
See e.g. [Ricketts2023] and [Armstrong2011] for more details.
- Parameters:
dim (
int) – dimension of the fieldfields (
listornumpy.ndarray) – For dim > 1 a list of spatial random fields (SRFs), with len(fields) == dim. For dim == 1, the SRF can be directly given, instead of a list. This class supports structured and unstructured meshes. All fields must have the same shapes.
Notes
Using plurigaussian fields for conditioning fields is still a beta feature.
References
[Ricketts2023]Ricketts, E.J., Freeman, B.L., Cleall, P.J. et al. A Statistical Finite Element Method Integrating a Plurigaussian Random Field Generator for Multi-scale Modelling of Solute Transport in Concrete. Transp Porous Med 148, 95–121 (2023) https://doi.org/10.1007/s11242-023-01930-8
[Armstrong2011]Armstrong, Margaret, et al. Plurigaussian simulations in geosciences. Springer Science & Business Media, 2011. https://doi.org/10.1007/978-3-642-19607-2
Methods
DecisionTree(config)Build and traverse a decision tree for assigning lithotype labels.
__call__([lithotypes, tree])Generate the plurigaussian field via spatial lithotype or decision tree.
calc_lithotype_axes(lithotypes_shape)Calculate the axes on which the lithorypes are defined.
compute_lithotype([tree])Compute lithotype from input SRFs using a
DecisionTree.transform_coords(lithotypes_shape, pos)Transform position from correlation coords to L indices.
- class DecisionTree(config)[source]
Bases:
objectBuild and traverse a decision tree for assigning lithotype labels.
This class constructs a tree of
DecisionNodeandLeafNodeinstances from a configuration mapping. Once built, it can classify input data by following the decision branches to a leaf action.- Parameters:
config (dict) – Mapping of node IDs to node specifications. Each entry must include:
‘type’: ‘decision’ or ‘leaf’
For decision nodes:
‘func’ (callable) and ‘args’ (dict)
Optional ‘yes_branch’ and ‘no_branch’ keys naming other nodes
For leaf nodes:
Notes
Call
build_tree()to link nodes and obtain the root before usingdecide().The tree is immutable once built; rebuild to apply a new config.
Methods
DecisionNode(func, args[, yes_branch, no_branch])Internal node that evaluates a condition and routes to child branches.
LeafNode(action)Terminal node that returns a stored action when reached.
Construct the decision tree structure from the configuration.
decide(data)Traverse the built tree to make a decision for the given data.
- class DecisionNode(func, args, yes_branch=None, no_branch=None)[source]
Bases:
objectInternal node that evaluates a condition and routes to child branches.
A
DecisionNodewraps a boolean function and two optional branches (yes_branch and no_branch), which may be furtherDecisionNodeorLeafNodeinstances.- Parameters:
func (callable) – A function that evaluates a condition on the input data. Must accept data as first argument and keyword args.
args (dict) – Keyword arguments to pass to
funcwhen called.yes_branch (
DecisionNodeorLeafNode, optional) – Node to traverse iffunc(data, **args)returns True.no_branch (
DecisionNodeorLeafNode, optional) – Node to traverse iffunc(data, **args)returns False.
Methods
decide(data)Evaluate the decision function and traverse to the next node.
- class LeafNode(action)[source]
Bases:
objectTerminal node that returns a stored action when reached.
A
LeafNoderepresents the outcome of a decision path. Itsactionvalue is returned directly bydecide()- Parameters:
action (any) – The value to return when this leaf is reached.
Methods
decide(_data)Return the leaf action, terminating the traversal.
- build_tree()[source]
Construct the decision tree structure from the configuration.
Iterates through the config to create
DecisionNodeandLeafNodeinstances, then links decision nodes to their yes/no branches.- Returns:
root – The root node of the constructed decision tree.
- Return type:
- Raises:
KeyError – If ‘root’ is not defined in the configuration.
- decide(data)[source]
Traverse the built tree to make a decision for the given data.
- Parameters:
data (dict) – A mapping of feature names to values, passed to decision functions in each
DecisionNode.- Returns:
The action value from the reached
LeafNode, or None if a branch is missing.- Return type:
result
- Raises:
ValueError – If the tree has not been built (i.e.,
build_tree()not called).
- __call__(lithotypes=None, tree=None)[source]
Generate the plurigaussian field via spatial lithotype or decision tree.
Either a lithotype field or a decision tree config must be provided. If lithotypes is given, map lithotype codes to the PGS via index scaling. If tree is given, build and apply a DecisionTree to assign phase labels.
- Parameters:
lithotypes (
numpy.ndarray, optional) – dim-dimensional structured lithotype field. Shape may differ from fields, as indices are automatically scaled. Mutually exclusive with tree.tree (dict, optional) – Configuration dict for constructing a
DecisionTree. Must contain node specifications. Mutually exclusive with lithotypes.
- Returns:
pgs – Plurigaussian field array: either the mapped lithotype field or the labels assigned by the decision tree, matching the simulation domain.
- Return type:
- Raises:
ValueError – If neither or both of lithotypes and tree are provided.
ValueError – If lithotypes shape does not match dim or number of fields.
ValueError – If dim != len(fields) when using lithotypes.
- calc_lithotype_axes(lithotypes_shape)[source]
Calculate the axes on which the lithorypes are defined.
With the centroid of the correlations of the SRFs at the center, the axes are calculated, which hold all correlations. These axes are used for the lithotypes field.
- Parameters:
lithotypes_shape (
tuple) – The shape of the lithotypes field.- Returns:
pos_lith – the axes holding all field correlations
- Return type:
- compute_lithotype(tree=None)[source]
Compute lithotype from input SRFs using a
DecisionTree.If self._tree is not set, a tree configuration must be provided via the tree argument. The method then builds or reuses the
DecisionTreeand applies it to the coordinates of the plurigaussian fields to assign a lithotype phase at each point.- Parameters:
tree (dict or None, optional) – Configuration for the
DecisionTree. If None, self._tree must already be defined. Defaults to None.- Returns:
lithotype – Discrete label array of shape equal to the simulation domain, where each entry is the phase index determined by the tree.
- Return type:
- Raises:
ValueError – If no
DecisionTreeis available or if self._dim does not equal the number of provided fields.
- transform_coords(lithotypes_shape, pos)[source]
Transform position from correlation coords to L indices.
This is a helper method to get the lithoty pes indices for given correlated field values.
- Parameters:
lithotypes_shape (
tuple) – The shape of the lithotypes field.pos (
numpy.ndarray) – The position in field coordinates, which will be transformed.
- Returns:
pos_trans – the transformed position tuple
- Return type: