Skip to content

Commit

Permalink
chore: response to review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Batch21 committed Jan 20, 2025
1 parent 47d3f34 commit cfbfba5
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"type": "Threshold",
"meta": {
"name": "my-threshold-param"
},
"metric": {
"type": "Node",
"name": "river-1"
},
"threshold": {
"type": "Constant",
"value": 5.0
},
"predicate": "LT",
"returned_metrics": [
{
"type": "Constant",
"value": 1.5
},
{
"type": "Constant",
"value": 0.5
}
]
}
28 changes: 15 additions & 13 deletions pywr-schema/src/parameters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,7 @@ impl Parameter {
Self::Min(p) => pywr_core::parameters::ParameterType::Parameter(p.add_to_model(network, args)?),
Self::Negative(p) => pywr_core::parameters::ParameterType::Parameter(p.add_to_model(network, args)?),
Self::Polynomial1D(p) => pywr_core::parameters::ParameterType::Parameter(p.add_to_model(network, args)?),
Self::Threshold(p) => match p.add_to_model(network, args)? {
pywr_core::parameters::ParameterType::Index(i) => pywr_core::parameters::ParameterType::Index(i),
pywr_core::parameters::ParameterType::Parameter(p) => {
pywr_core::parameters::ParameterType::Parameter(p)
}
pywr_core::parameters::ParameterType::Multi(_) => {
return Err(SchemaError::LoadParameter {
name: p.meta.name.to_string(),
error: String::from("Threshold parameter cannot be a multi-parameter."),
})
}
},
Self::Threshold(p) => p.add_to_model(network, args)?,
Self::TablesArray(p) => pywr_core::parameters::ParameterType::Parameter(p.add_to_model(network, args)?),
Self::Python(p) => p.add_to_model(network, args)?,
Self::Delay(p) => pywr_core::parameters::ParameterType::Parameter(p.add_to_model(network, args)?),
Expand Down Expand Up @@ -722,8 +711,21 @@ mod tests {
let p = entry.unwrap().path();
if p.is_file() {
let data = fs::read_to_string(&p).unwrap_or_else(|_| panic!("Failed to read file: {:?}", p));
let _: Parameter =

let value: serde_json::Value =
serde_json::from_str(&data).unwrap_or_else(|_| panic!("Failed to deserialize: {:?}", p));

match value {
serde_json::Value::Object(o) => {
let _ = serde_json::from_value::<Parameter>(serde_json::Value::Object(o))
.unwrap_or_else(|_| panic!("Failed to deserialize: {:?}", p));
}
serde_json::Value::Array(_) => {
let _ = serde_json::from_value::<Vec<Parameter>>(value)
.unwrap_or_else(|_| panic!("Failed to deserialize: {:?}", p));
}
_ => panic!("Expected JSON object: {:?}", p),
}
}
}
}
Expand Down
21 changes: 15 additions & 6 deletions pywr-schema/src/parameters/thresholds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,19 +58,28 @@ impl From<Predicate> for pywr_core::parameters::Predicate {
/// A parameter that compares a metric against a threshold metric
///
/// The metrics are compared using the given predicate and the result is returned as an index. If the comparison
/// evaluates to true the index is 1, otherwise it is 0.
/// evaluates to true the index is 1, otherwise it is 0. When values are provided for the `returned_metrics` attribute,
/// these values are returned instead of the index. If the predicate comparison evaluates to false the first value is
/// returned, if it is true the second value is returned.
///
/// The parameter has different representations in core depending on the `returned_metrics` attribute. If values are
/// given then two parameters are added to the model. The first a [`pywr_core::parameters::ThresholdParameter`], which
/// is set as the index parameter of a [`pywr_core::parameters::IndexedArrayParameter`] containing the `returned_metrics`
/// set for `returned_metrics` then two parameters are added to the model. The first a
/// [`pywr_core::parameters::ThresholdParameter`], which is set as the index parameter of a
/// [`pywr_core::parameters::IndexedArrayParameter`] containing the `returned_metrics`
/// values.
///
/// An equivalent representation could be achieved by defining the two parameters in the schema directly:
/// # Examples
///
/// ```JSON
#[doc = include_str!("doc_examples/threshold_returned_values.json")]
#[doc = include_str!("doc_examples/threshold_returned_values1.json")]
/// ```
/// Note that the name specified in the model JSON for the parameter in this example is assigned to the core
/// `IndexedArrayParameter`. The core `ThresholdParameter` is given an additional sub-name of `threshold`.
///
///
/// An equivalent representation could be achieved by defining the two parameters separately in the model JSON:
/// ```JSON
#[doc = include_str!("doc_examples/threshold_returned_values2.json")]
/// ```
#[derive(serde::Deserialize, serde::Serialize, Debug, Clone, JsonSchema, PywrVisitAll)]
#[serde(deny_unknown_fields)]
pub struct ThresholdParameter {
Expand Down

0 comments on commit cfbfba5

Please sign in to comment.