smash.Model.set_rr_parameters#
- Model.set_rr_parameters(key, value)[source]#
Set the values of a Model rainfall-runoff parameter.
This method performs an in-place operation on the Model object.
- Parameters:
- keystr
The name of the rainfall-runoff parameter.
- valuefloat or
numpy.ndarray
The value(s) to set to the rainfall-runoff parameter. If the value is a
numpy.ndarray
, its shape must be broadcastable into the rainfall-runoff parameter shape.
See also
Model.get_rr_parameters
Get the values of a Model rainfall-runoff parameter.
Model.rr_parameters
Model rainfall-runoff parameters.
Examples
>>> from smash.factory import load_dataset >>> setup, mesh = load_dataset("cance") >>> model = smash.Model(setup, mesh)
Set a specific value to a rainfall-runoff parameter grid
>>> model.set_rr_parameters("cp", 150)
Access its value
>>> model.get_rr_parameters("cp") array([[150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, 150, ... 150, 150, 150, 150, 150, 150]], dtype=float32)
Set a grid with a shape equivalent to the rainfall-runoff parameter filled with random values between
10
and500
Get the rainfall-runoff parameter shape
>>> shape = model.get_rr_parameters("cp").shape >>> shape (28, 28)
Generate the random grid
>>> np.random.seed(99) >>> random_arr = np.random.randint(10, 500, shape) >>> random_arr array([[139, 45, 195, 178, 211, 242, 220, 78, 207, 446, 395, 417, 264, 301, 65, 459, 186, 231, 69, 496, 373, 254, 225, 140, 202, 150, ... 107, 386]])
Set to the rainfall-runoff parameter the random grid
>>> model.set_rr_parameters("cp", random_arr) >>> model.get_rr_parameters("cp") array([[139., 45., 195., 178., 211., 242., 220., 78., 207., 446., 395., 417., 264., 301., 65., 459., 186., 231., 69., 496., 373., 254., ... 243., 424., 301., 413., 107., 386.]], dtype=float32)
Note
This method is equivalent to directly slicing the
rr_parameters.values
array (as shown below) and change the values but is simpler andsafer
to use.Access the rainfall-runoff parameter keys
>>> model.rr_parameters.keys array(['ci', 'cp', 'ct', 'kexc', 'llr'], dtype='<U4')
Get the index of the rainfall-runoff parameter
'cp'
>>> ind = np.argwhere(model.rr_parameters.keys == "cp").item() >>> ind 1
Slice the
rr_parameters.values
array on the last axis and change its values>>> model.rr_parameters.values[..., ind] = 56 >>> model.rr_parameters.values[..., ind] array([[56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., 56., ... 56., 56.]], dtype=float32)
Warning
In that case, there’s no problem to set the value
56
to the rainfall-runoff parameter'cp'
, but each rainfall-runoff parameter has a feasibility domain, and that outside this domain, the model cannot run. For example, the feasibility domain of'cp'
is \(]0, +\inf[\).Trying to set a negative value to the rainfall-runoff parameter
'cp'
without the setter>>> model.rr_parameters.values[..., ind] = -47 >>> model.rr_parameters.values[..., ind] array([[-47., -47., -47., -47., -47., -47., -47., -47., -47., -47., -47., -47., -47., -47., -47., -47., -47., -47., -47., -47., -47., -47., ... -47., -47., -47., -47., -47., -47.]], dtype=float32)
No particular problem doing this but trying with the setter
>>> model.set_rr_parameters("cp", -47) ValueError: Invalid value for model rr_parameter 'cp'. rr_parameter domain [-47, -47] is not included in the feasible domain ]0, inf[
Finally, trying to run the Model with a negative value set to the rainfall-runoff parameter
'cp'
leads to the same error.>>> model.forward_run() ValueError: Invalid value for model rr_parameter 'cp'. rr_parameter domain [-47, -47] is not included in the feasible domain ]0, inf[