masktools¶
This module implements masking features to define which entries of Parameter or
Sequence_ arrays are relevant and which are not.
Module masktools implements the following members:
BaseMaskBase class for definingCustomMaskandDefaultMaskclasses.
CustomMaskMask that awaits one sets allboolvalues manually.
DefaultMaskA mask with all entries beingTrueof the same shape as its masterVariableobject.
IndexMaskA mask that depends on a referenced index parameter.
SubmodelIndexMaskA mask that depends on a referenced index parameter of another model.
MasksBase class for handling groups of masks.
- class hydpy.core.masktools.BaseMask(array=None, doc: str | None = None, **kwargs)[source]¶
Bases:
ndarray[tuple[Any, …],dtype[bool]]Base class for defining
CustomMaskandDefaultMaskclasses.
- class hydpy.core.masktools.CustomMask(array=None, doc: str | None = None, **kwargs)[source]¶
Bases:
BaseMaskMask that awaits one sets all
boolvalues manually.Class
CustomMaskis the most basic applicable mask and provides no special features except for allowing itsboolvalues to be defined manually. Use it when you require a masking behaviour not captured by an available mask.Like the more advanced masks,
CustomMaskcan work via Python’s descriptor protocol, but it is primarily thought to be applied directly:>>> from hydpy.core.masktools import CustomMask >>> mask1 = CustomMask([[True, False, False], ... [True, False, False]])
Note that calling any mask object (not only those of type
CustomMask) returns a new mask without changing the existing one.>>> mask2 = mask1([[False, True, False], ... [False, True, False]]) >>> mask1 CustomMask([[ True, False, False], [ True, False, False]]) >>> mask2 CustomMask([[False, True, False], [False, True, False]])
All masks stem from
ndarray. Here are some useful examples of working with them:>>> mask3 = mask1 + mask2 >>> mask3 CustomMask([[ True, True, False], [ True, True, False]]) >>> mask3 ^ mask1 CustomMask([[False, True, False], [False, True, False]]) >>> ~mask3 CustomMask([[False, False, True], [False, False, True]]) >>> mask1 & mask2 CustomMask([[False, False, False], [False, False, False]])
Use the in operator to check if a mask defines a subset of another:
>>> mask1 in mask3 True >>> mask3 in mask1 False
- class hydpy.core.masktools.DefaultMask(variable: variabletools.Variable | None = None, doc: str | None = None, **kwargs)[source]¶
Bases:
BaseMaskA mask with all entries being
Trueof the same shape as its masterVariableobject.See the documentation on class
CustomMaskfor the basic usage of classDefaultMask.The following example shows how to apply
DefaultMaskvia Python’s descriptor protocol, which should be the common situation:>>> from hydpy.core.parametertools import Parameter >>> from hydpy.core.masktools import DefaultMask >>> class Par1(Parameter): ... shape = (2, 3) ... defaultmask = DefaultMask() >>> Par1(None).defaultmask DefaultMask([[ True, True, True], [ True, True, True]])
Alternatively, you can directly connect a
DefaultMaskwith aVariableobject:>>> class Par2(Parameter): ... shape = (2,) >>> mask = DefaultMask(Par2(None)) >>> mask DefaultMask([ True, True])
- variable: variabletools.Variable | None¶
- classmethod new(variable: variabletools.Variable, **kwargs) Self[source]¶
Return a new
DefaultMaskobject associated with the givenVariableobject.
- class hydpy.core.masktools.IndexMask(variable: variabletools.Variable | None = None, doc: str | None = None, **kwargs)[source]¶
Bases:
DefaultMaskA mask that depends on a referenced index parameter.
IndexMaskmust be subclassed. See the masksCompleteandSoilof base modelhlandfor two concrete example classes, which are members of the parameter classesParameterCompleteandParameterSoil. The documentation on these parameter classes provides some application examples. Further, see the documentation on classCustomMaskfor the basic usage of classDefaultMask.- variable: variabletools.Variable¶
The variable for which
IndexMaskdetermines the relevant entries.
- classmethod new(variable: variabletools.Variable, **kwargs) Self[source]¶
Return a new
IndexMaskobject of the same shape as the parameter referenced bypropertyrefindices.Entries are only
Trueif the integer values of the respective entries of the referenced index parameter are members of the class attribute tuplerelevant.Before calling new (explicitly or implicitly), one must prepare the variable returned by property
refindices:>>> from hydpy.models.hland import * >>> parameterstep() >>> states.sm.mask Traceback (most recent call last): ... RuntimeError: The mask of parameter `sm` of element `?` cannot be determined as long as parameter `zonetype` is not prepared properly.
>>> nmbzones(4) >>> zonetype(FIELD, FOREST, ILAKE, GLACIER) >>> states.sm.mask Soil([ True, True, False, False])
If the shape of the
refindicesparameter is zero (which is not allowed forhland), the returned mask is empty:>>> zonetype.shape = 0 >>> states.shape = 0 >>> states.sm.mask Soil([])
- classmethod get_refindices(variable: variabletools.Variable) parametertools.NameParameter[source]¶
Return the
Parameterobject to determine which entries ofIndexMaskmust beTrueand whichFalse.The given variable must be the concrete
Variableobject theIndexMaskis responsible for.Needs to be overwritten by subclasses:
>>> from hydpy.core.parametertools import Parameter >>> from hydpy.core.masktools import IndexMask >>> class Par(Parameter): ... mask = IndexMask() >>> Par(None).mask Traceback (most recent call last): ... NotImplementedError: Method `get_refindices` of class `IndexMask` must be overridden, which is not the case for class `IndexMask`.
- property refindices: NameParameter¶
Parameterobject for determining which entries ofIndexMaskareTrueand whichFalse.
- static get_refinement(variable: variabletools.Variable) variabletools.Variable | None[source]¶
If available, return a boolean variable for selecting only the relevant entries of the considered variable.
- property refinement: variabletools.Variable | None¶
If available, a boolean variable for selecting only the relevant entries of the considered variable.
- class hydpy.core.masktools.SubmodelIndexMask(variable: variabletools.Variable | None = None, doc: str | None = None, **kwargs)[source]¶
Bases:
IndexMaskA mask that depends on a referenced index parameter of another model.
- classmethod get_refindices(variable: variabletools.Variable) parametertools.NameParameter[source]¶
Return the
Parameterobject to determine which entries ofSubmodelIndexMaskmust beTrueand whichFalse.SubmodelIndexMaskworks only for givenZipParameterinstances and tries to return the currently handledrefindicesparameter instance.
- class hydpy.core.masktools.Masks[source]¶
Bases:
objectBase class for handling groups of masks.
Maskssubclasses are basically just containers, which are defined similar asSubParametersandSubSequencessubclasses:>>> from hydpy.core.masktools import Masks >>> from hydpy.core.masktools import IndexMask, DefaultMask, CustomMask >>> class Masks(Masks): ... CLASSES = (IndexMask, DefaultMask) >>> masks = Masks()
The contained mask classes are available via attribute access in lower case letters:
>>> masks indexmask of module hydpy.core.masktools defaultmask of module hydpy.core.masktools >>> masks.indexmask is IndexMask True >>> "indexmask" in dir(masks) True
The in operator is supported:
>>> IndexMask in masks True >>> CustomMask in masks False >>> "mask" in masks Traceback (most recent call last): ... TypeError: The given value `mask` of type `str` is neither a Mask class nor a Mask instance.
Using item access, strings (in whatever case), mask classes, and mask objects are accepted:
>>> masks["IndexMask"] is IndexMask True >>> masks["indexmask"] is IndexMask True >>> masks[IndexMask] is IndexMask True >>> masks[CustomMask()] Traceback (most recent call last): ... RuntimeError: While trying to retrieve a mask based on key `CustomMask([])`, the following error occurred: The key does not define an available mask. >>> masks["test"] Traceback (most recent call last): ... RuntimeError: While trying to retrieve a mask based on key `'test'`, the following error occurred: The key does not define an available mask. >>> masks[1] Traceback (most recent call last): ... TypeError: While trying to retrieve a mask based on key `1`, the following error occurred: The given key is neither a `string` a `mask` type.
- class hydpy.core.masktools.NodeMasks[source]¶
Bases:
MasksMaskssubclass for classNode.At the moment, the purpose of class
NodeMasksis to make the implementation ofModelSequenceandNodeSequencemore similar. It will become relevant for applications as soon as we support 1-dimensional node sequences.